LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-24-2005, 06:05 AM   #1
spank
Member
 
Registered: Aug 2003
Location: Romania
Distribution: Ubuntu 6.06
Posts: 278

Rep: Reputation: 30
Question mbr.bin


I have a school homework to read the partition table from a binary file mbr.bin
root@marlboro ~/fdisk # file mbr.bin
mbr.bin: x86 boot sector, code offset 0x48
http://cs.pub.ro/~programare/Teme_de_casa/tema6/mbr.bin

how can i do that?

i've tryed

char a[512];
f=fopen("mbr.bin","rb");
fread(a,1,512,f);
but i don't know how to interpret the data from the char array a.
Please pretty please help!

Last edited by spank; 12-24-2005 at 07:04 AM.
 
Old 12-24-2005, 04:40 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
You need to know how the MBR structure looks like. You can find the info here: http://www.ata-atapi.com/hiwmbr.htm Then just extract what you want.
 
Old 12-25-2005, 06:15 AM   #3
spank
Member
 
Registered: Aug 2003
Location: Romania
Distribution: Ubuntu 6.06
Posts: 278

Original Poster
Rep: Reputation: 30
i've read some docs. now i don't understand why the first byte from mbr.bin is displayed "eb" in a hex editor and if i read it in c

fread(a,1,512,f);
x=a[0];
printf("%x ",x);

it shows me "ffffffeb". This happens not only with the first byte. But there are some that are displayed the same like in the hex editor
Help!
 
Old 12-25-2005, 02:06 PM   #4
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
In you case it's all probably about types. You have a buffer of chars (or unsigned chars), but x is probably int, right? Char is one byte, int four (ia386 architecture). There are casts here and they cause the mess.
 
Old 12-25-2005, 02:13 PM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
In other words:
ORIGINAL VERSION (any byte >= 128 will be treated as a negative number):
Code:
  char a[512];
  char x;
  f=fopen("mbr.bin","rb");
  fread(a,1,512,f);
  x=a[0];
  printf("%x ",x);
<= THE BYTE VALUE "0xeb" WILL DISPLAY AS "ffffffeb"


ALTERNATIVE VERSION #1:
Code:
  unsigned char a[512];
  unsigned char x;
  f=fopen("mbr.bin","rb");
  fread(a,1,512,f);
  x=a[0];
  printf("0x%x ",x);
<= THE BYTE VALUE "0xeb" WILL DISPLAY AS "0xeb"

ALTERNATIVE VERSION #2:
Code:
  char a[512];
  char x;
  f=fopen("mbr.bin","rb");
  fread(a,1,512,f);
  x=a[0];
  printf("0x%x ", (unsigned char)x);
<= THE BYTE VALUE "0xeb" WILL *PROBABLY* DISPLAY AS "0xeb"

Last edited by paulsm4; 12-25-2005 at 04:02 PM.
 
Old 12-25-2005, 03:31 PM   #6
spank
Member
 
Registered: Aug 2003
Location: Romania
Distribution: Ubuntu 6.06
Posts: 278

Original Poster
Rep: Reputation: 30
great!! really helps!

now i have to read the HCS, this is a serie of 3 bytes. i dont know how to separe every byte into bits. how can i do that ?
 
Old 12-25-2005, 04:09 PM   #7
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
You get to write a function. For example:
Code:
  unsigned char a[80]
  char s[80;

  // Zero out string
  s[0] = '\0';

  // For each byte...
  for (int ibyte = 0; ibyte < 3; ibyte++)
  {
    // For each bit...
    for (int ibit = 0; ibit < 8; ibit++)
    {
      if ( ((a[ibyte] >> ibit) & 0x1)
      {
        strcat (s, "1");
      }
      else
      {
        strcat (s, "0");
      }
    }
  }
Just a quick'n'dirty example (I'm not even sure if it'll compile, much less certain it's correct or not): you'll undoubtedly come up with something better/more applicable yourself. Good luck!

And Merry Christmas!

Last edited by paulsm4; 12-25-2005 at 04:10 PM.
 
Old 12-26-2005, 10:28 AM   #8
spank
Member
 
Registered: Aug 2003
Location: Romania
Distribution: Ubuntu 6.06
Posts: 278

Original Poster
Rep: Reputation: 30
Code:
char *byte2bit(unsigned char a)
{
        char *s=(char*)calloc(9,sizeof(char));
        int i;
        for(i=0;i<CHAR_BIT;i++) *(s+i)=(a>>i&1)+'0';
        return s;
}
i've done this for every byte... but your code works very well. thank you!
happy holidays to you to!
 
Old 12-26-2005, 02:46 PM   #9
spank
Member
 
Registered: Aug 2003
Location: Romania
Distribution: Ubuntu 6.06
Posts: 278

Original Poster
Rep: Reputation: 30
next problem... i have a 4 byte entry that is an unsigned int, the problem is that i used a char to read the binary file. how can i obtain the 4 byte int ?
 
Old 12-27-2005, 03:43 AM   #10
spank
Member
 
Registered: Aug 2003
Location: Romania
Distribution: Ubuntu 6.06
Posts: 278

Original Poster
Rep: Reputation: 30
solved it
Code:
#include <stdio.h>

int main(void)
{
   unsigned char b[] = {0x12,0x34,0x56,0x78};
   unsigned long value = (b[3] << 24) | (b[2] << 16)  | (b[1] << 8) | b[0];
   printf("value = 0x%08lX\n", value);
   return 0;
}
 
Old 12-27-2005, 10:15 AM   #11
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Cool - good work!

Sincerely .. PSM
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
mkisofs: Error - boot image './isolinux/isolinux.bin' has not an allowable size. ogross74 Red Hat 3 05-05-2016 02:22 AM
oh uh,i can't find the boot image 'isolinux.bin' shams Linux - Software 7 07-17-2007 02:34 PM
bin for root? xaphalanx Linux - Enterprise 1 12-26-2005 05:38 PM
A stupid rm -rf no more /boot,/bin,/etc what would you do? nx5000 Debian 4 12-24-2005 06:19 PM
Multiple 'mozilla-bin' in process list.. Why? orgee Linux - Software 2 09-29-2004 07:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration