HDD 부트섹터 0번지 읽어오기 질문

j92music의 이미지

GPT Partition 구성을 보기 위하여 HDD 0번지를 읽어와 보려고하는데요.

IT EXPERT 임베디드 개발자를 위한 파일시스템의 원리와 실습 예제1에 나온것은 윈도우용이더라구요.

이것을 리눅스에서도 동작할 수 있게 수정하는 방법을 가르쳐 주세요.

혹 windows.h 파일만 가져다 놓으면 되는건가요? 무뇌한 질문이온줄 알지만 이렇게 질문 올립니다.

아래는 원본소스입니다. HDD 0번지 읽기
include <> 하면 안보여서 <>를 지웠습니다.
------------------------------------------------------

#include stdio.h
#include windows.h
#include stdlib.h

#define U8 unsigned char
#define S8 char
#define U16 unsigned short
#define U32 unsigned int
#define U64 unsigned __int64

U32 HDD_read (U8 drv, U32 SecAddr, U32 blocks, U8* buf);
U32 HDD_write(U8 drv, U32 SecAddr, U32 blocks, U8* buf);
void HexDump (U8 *addr, U32 len);

int main(void) {
U8 dumpData[512];

HDD_read(0, 0, 1, dumpData);
HexDump( dumpData, 512);
return 0;
}

U32 HDD_write(U8 drv, U32 SecAddr, U32 blocks, U8* buf) {
U32 ret = 0;
U32 ldistanceLow, ldistanceHigh, dwpointer, bytestoread, numread;

char cur_drv[100];
HANDLE g_hDevice;

sprintf(cur_drv,"\\\\.\\PhysicalDrive%d",(U32)drv);
g_hDevice=CreateFile(cur_drv, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

if(g_hDevice==INVALID_HANDLE_VALUE) return 0;

ldistanceLow = SecAddr << 9;
ldistanceHigh = SecAddr >> (32 - 9);
dwpointer = SetFilePointer(g_hDevice, ldistanceLow, (long *)&ldistanceHigh, FILE_BEGIN);

if(dwpointer != 0xFFFFFFFF) {
bytestoread = blocks * 512;
ret = WriteFile(g_hDevice, buf, bytestoread, (unsigned long*)&numread, NULL);
if(ret) ret = 1;
else ret = 0;
}

CloseHandle(g_hDevice);
return ret;
}

U32 HDD_read (U8 drv, U32 SecAddr, U32 blocks, U8* buf){
U32 ret;
U32 ldistanceLow, ldistanceHigh, dwpointer, bytestoread, numread;

char cur_drv[100];
HANDLE g_hDevice;

sprintf(cur_drv,"\\\\.\\PhysicalDrive%d",(U32)drv);
g_hDevice=CreateFile(cur_drv, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

if(g_hDevice==INVALID_HANDLE_VALUE) return 0;

ldistanceLow = SecAddr << 9;
ldistanceHigh = SecAddr >> (32 - 9);
dwpointer = SetFilePointer(g_hDevice, ldistanceLow, (long *)&ldistanceHigh, FILE_BEGIN);

if(dwpointer != 0xFFFFFFFF) {
bytestoread = blocks * 512;
ret = ReadFile(g_hDevice, buf, bytestoread, (unsigned long*)&numread, NULL);
if(ret) ret = 1;
else ret = 0;
}

CloseHandle(g_hDevice);
return ret;
}

void HexDump (U8 *addr, U32 len){
U8 *s=addr, *endPtr=(U8*)((U32)addr+len);
U32 i, remainder=len%16;

printf("\n Offset Hex Value Ascii value\n");

// print out 16 byte blocks.
while (s+16<=endPtr){

// offset 출력.
printf("0x%08lx ", (long)(s-addr));

// 16 bytes 단위로 내용 출력.
for (i=0; i<16; i++){
printf("%02x ", s[i]);
}
printf(" ");

for (i=0; i<16; i++){
if (s[i]>=32 && s[i]<=125)printf("%c", s[i]);
else printf(".");
}
s += 16;
printf("\n");
}

// Print out remainder.
if (remainder){

// offset 출력.
printf("0x%08lx ", (long)(s-addr));

// 16 bytes 단위로 출력하고 남은 것 출력.
for (i=0; i printf("%02x ", s[i]);
}
for (i=0; i<(16-remainder); i++){
printf(" ");
}

printf(" ");
for (i=0; i if (s[i]>=32 && s[i]<=125) printf("%c", s[i]);
else printf(".");
}
for (i=0; i<(16-remainder); i++){
printf(" ");
}
printf("\n");
}
return;
} // HexDump.

------------------------------------------------------------

답변해주신 모든 분들, 글 읽어주신 분들 모두 갑사드립니다.

Necromancer의 이미지

딴거 필요없이 /dev/hda나 /dev/sda를 open(), read() 하면 됩니다. 루트권한 필요하고요.

하드디스크 파라미터 정보를 읽는 것은 ioctl()을 이용해냐 하는데 이것은 util-linux에 포함된
fdisk 소스를 보시면 있습니다.

소스를 보니 HDD_read, HDD_write 부분만 재작성하면 될 것 같습니다.
U8같은 변수형들은 해당 typedef를 추가해주셔야 하고요.

Written By the Black Knight of Destruction

Written By the Black Knight of Destruction

j92music의 이미지

답변 감사드립니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.