Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: How to read the mbr-sector ( sector zero of /dev/sda ) and print-out the partition table?

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Location
    belgium
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How to read the mbr-sector ( sector zero of /dev/sda ) and print-out the partition table?

    Hello ,
    As a java-newbee I wonder how todo this simple( in C that-is) task ?
    Which IO-class to use for reading a binary record of 512-bytes and how to access the 4 entries of the partition-table ( no pointers in java to handle this ) ?
    I know howto todo it in C see under) , but in Java I dont even know howto start and begin ?

    // mbr0.c 
    #define SECTOR_SIZE 512
    #include <stdio.h>
    #include <stdlib.h>
    typedef struct s_table_entry 
      {  unsigned char boot_status ;   // 0x80 = bootable 0 = non-boot other=?
         unsigned char chs_begin[3];   // cyl,head,sector adres of first sector
         unsigned char type ;   	     // partition type   
         unsigned char chs_end[3];     // cyl,head,sector adres of last sector
         unsigned int  begin ;         // logical block adres first sector
         unsigned int  length ;        // length of partition in sectors 512 bytes 
      } table_entry ;   // size 16 bytes 
     
    unsigned char buf512[512];
    size_t bytes_readed;
    short I;
    table_entry* ptable_entry = buf512+446; 
    FILE*  fdev ;  
    int main(int argc , char* args[] )   
     { fdev = fopen("/dev/sda","rb");
       if(fdev == NULL  )
        { puts("\n OOPS cant open device /dev/sda: ??");
          return 99 ;
        } 
       bytes_readed = fread(buf512,1,512,fdev);
     
       if(bytes_readed  < SECTOR_SIZE ) 
        {  printf("\n OOPS readed only  %d bytes ?? ",bytes_readed) ;
           return bytes_readed ;
        }
       for  (I=0;I<4;I++)
        { printf("\n  entry:%2d boot:%3u start:%12u sectors:%12u ",I ,ptable_entry->boot_status , ptable_entry->begin,ptable_entry->length);
          ptable_entry++;
        }
        fclose(fdev);
        puts(" ");
      return 0 ;  
     
    } // -----------------------------------------------------
    TIA for any hint !
    Erics


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to read the mbr-sector ( sector zero of /dev/sda ) and print-out the partition table?

    I don't think it's possible.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Location
    belgium
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to read the mbr-sector ( sector zero of /dev/sda ) and print-out the partition table?

    Quote Originally Posted by Norm View Post
    I don't think it's possible.
    Thank You for replying Norm , but I find it hard to believe Java not having a class or method somewhere to handle this ( in Cobol yes impossible , but Java )?
    So I suppose my next Java question ( I'll post it to morrow ) will be in the same impossible-to-do category ( the title of a new thread ?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to read the mbr-sector ( sector zero of /dev/sda ) and print-out the partition table?

    Stay away from the hardware and the insides of the OS.
    Think application code, not systems code.
    I was a VM Systems programmer. I maintained and modified the VM code in assembly language code. I rarely did any applications code. In other words, no customer ever used anything I wrote.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to read the mbr-sector ( sector zero of /dev/sda ) and print-out the partition table?

    You can read binary files using either RandomAccessFile or InputStream (eg DataInputStream). Just read (or seek) to the place you want and read in the bytes, mapping them to your data model manually. These classes let you read in primitives (byte, int, long, etc...) but be careful of endianess for primitives larger than a single byte...you can't read in and map directly to a struct in a single call without doing so manually or using serialization (not necessarily applicable in this scenario). Note there might be a library available to facilitate this, but I'm not familiar with one.

  6. #6
    Junior Member
    Join Date
    Jun 2014
    Location
    belgium
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to read the mbr-sector ( sector zero of /dev/sda ) and print-out the partition table?

    My Thanks for everyone !
    I think i solved the problem by using a dataInputStream ( as suggested by copeg ) and skipping the first 446 bytes ( the initial bootcode ) , but alas the printed-out figures were all wrong ??
    I was certain I readed the correct MBR-sector as the two last bytes of sector0 of the hard-disk carry a magic number , the 2 bytes were there but in the "wrong" order ??
    Sounds like an endian-problem right ?

    Made two test-programs : a C-version and a Java-version which wrote some ints to a file and hexdumped the result :
    ( the ints written were in hex : 0xAB 0xABC 0x ABCD and a short= 1 : )
    results:
    eric@sysvaio:~$ hexdump IntJtest.bin
    0000000 0000 0a00 0000 ab00 0000 bc0a 0000 cdab
    0000010 0100
    0000012
    eric@sysvaio:~$ hexdump IntCtest.bin
    0000000 000a 0000 00ab 0000 0abc 0000 abcd 0000
    0000010 0001
    0000012
    This is on a Intel-machine : why do C and Java on the same machine interpret an int on a different manner ?
    Life is complicated enough already without this crap !
    Hopeless!
    Erics

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to read the mbr-sector ( sector zero of /dev/sda ) and print-out the partition table?

    What order are the bytes in on the disk? What happens if you read the data as bytes vs as int?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to read the mbr-sector ( sector zero of /dev/sda ) and print-out the partition table?

    Java is big endian, regardless of the machine it runs on. Thus little endian data will be read incorrectly when using the readInt, readLong, etc...of DataInputStream. If you believe this to be an endian problem, you will have to read the bytes yourself and bit shift them as necessary.

  9. The Following User Says Thank You to copeg For This Useful Post:

    erics (June 24th, 2014)

  10. #9
    Junior Member
    Join Date
    Jun 2014
    Location
    belgium
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to read the mbr-sector ( sector zero of /dev/sda ) and print-out the partition table?

    Thank You Copeg and Norm for the replies .
    First time I heard about the big-endianness of Java , have a couple of Java-books around , not one of them does mention this fact !
    One should expect as Java is designed to run on different platforms , the endianness to be dependent on the target-machine or at least as an command-line option for the Javac-compiler ?

    Ok now I know , I' ll have to tackle the beast , I have found there is even a java-class which maybe can be used for the job : " bytebuffer " .

    Yes indeed computing is a lot of fun !
    Greetz.
    Eric

    --- Update ---

    Hello !
    Voila , got it right now .
    To tackle the endian-problem it is not even necessary to use the bytebuffer-class but simply the "reversebytes"-method of the Integer-class !
    By the way : inspecting the first sector of the hard-disk via reading /dev/sda works only in a linux-box , don't know how todo this on an MS-Windows-platform !
    Suppose Windows will not even allow it ?
    Thanks everybody for all the hints , being aware I'll have a lot to learn .
    erics

Similar Threads

  1. Counting sector from a grid
    By arunk in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 27th, 2012, 06:53 AM
  2. Print table need some idea
    By Anc in forum AWT / Java Swing
    Replies: 0
    Last Post: May 30th, 2012, 09:31 AM
  3. Replies: 1
    Last Post: December 12th, 2011, 06:03 AM