Giter VIP home page Giter VIP logo

node-mbr's Introduction

Master Boot Record (MBR)

npm npm license npm downloads

Install via npm

$ npm install --save mbr

Used by

Related Modules

  • gpt – Parse, construct & verify GUID Partition Tables
  • apple-partition-map – Parse / construct Apple Partition Maps
  • blockdevice – Read from / write to block devices
  • disk – Disk / image toolbox

Handling Extended / Logical Partitions

Logical partitions are not entries in the MBR. They're constructed from the contents of an extended partition's Extended Boot Records (EBR), which are essentially the same in structure as an MBR and can be parsed with this module as well.

In order to read logical partitions, find a partition entry that's marked as extended partition container, then read that partition's first 512 bytes (the EBR), parse that, then look for additional EBRs in the extended partition. For more detail on how extended / logical partitions work, see Wikipedia / Extended Boot Record

Usage

For a complete API reference, see doc/README.md

var MBR = require( 'mbr' )
// Obtain a Buffer of an MBR
var buffer = fs.readFileSync( 'mbr.bin' )

mbr.bin

Offset   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

000000   FA B8 00 10 8E D0 BC 00 B0 B8 00 00 8E D8 8E C0   ú¸...м.°¸...Ø.À
000010   FB BE 00 7C BF 00 06 B9 00 02 F3 A4 EA 21 06 00   û¾.|¿..¹..ó¤ê!..
000020   00 BE BE 07 38 04 75 0B 83 C6 10 81 FE FE 07 75   .¾¾.8.u..Æ..þþ.u
000030   F3 EB 16 B4 02 B0 01 BB 00 7C B2 80 8A 74 01 8B   óë.´.°.».|²..t..
000040   4C 02 CD 13 EA 00 7C 00 00 EB FE 00 00 00 00 00   L.Í.ê.|..ëþ.....
000050   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
  --
0001A0   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
0001B0   00 00 00 00 00 00 00 00 B9 77 74 8B 00 00 80 00   ........¹wt.....
0001C0   01 40 0C 03 60 7F 00 20 00 00 00 A0 00 00 00 00   .@..`.. ... ....
0001D0   41 80 83 03 E0 FF 00 C0 00 00 00 80 05 00 00 03   A...àÿ.À........
0001E0   E0 FF 83 03 E0 FF 00 40 06 00 00 80 05 00 00 03   àÿ..àÿ.@........
0001F0   E0 FF 0F 03 E0 FF 00 C0 0B 00 00 60 20 00 55 AA   àÿ..àÿ.À...` .Uª
> console.log( new MBR( buffer ) )
{
  physicalDrive: 0,
  timestamp: { seconds: 0, minutes: 0, hours: 0 },
  signature: 2339665849,
  copyProtected: false,
  partitions: [{
    status: 128,
    type: 12,
    sectors: 40960,
    firstLBA: 8192,
    firstCHS: <CHS { cylinder: 0, head: 0, sector: 0 }>,
    lastCHS: <CHS { cylinder: 0, head: 0, sector: 0 }>
  }, {
    status: 0,
    type: 131,
    sectors: 360448,
    firstLBA: 49152,
    firstCHS: <CHS { cylinder: 0, head: 0, sector: 0 }>,
    lastCHS: <CHS { cylinder: 0, head: 0, sector: 0 }>
  }, {
    status: 0,
    type: 131,
    sectors: 360448,
    firstLBA: 409600,
    firstCHS: <CHS { cylinder: 0, head: 0, sector: 0 }>,
    lastCHS: <CHS { cylinder: 0, head: 0, sector: 0 }>
  }, {
    status: 0,
    type: 15,
    sectors: 2121728,
    firstLBA: 770048,
    firstCHS: <CHS { cylinder: 0, head: 0, sector: 0 }>,
    lastCHS: <CHS { cylinder: 0, head: 0, sector: 0 }>
  }],
  code: [{
    offset: 0,
    data: <Buffer fa b8 00 10 8e d0 bc 00 b0 b8...>
  }, {
    offset: 224,
    data: <Buffer 00 00 00 00 00 00 00 00 00 00...>
  }]
}

node-mbr's People

Contributors

jhermsmeier avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

node-mbr's Issues

No support for logical partitions within extended partitions

There is currently no support for displaying information about logical partitions within extended partitions.

For example, on a disk image that has 3 primary partitions and two logical contained within an extended partition, parted has the following output:

Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type      File system  Flags
 1      4194kB  25.2MB  21.0MB  primary   fat16        boot, lba
 2      25.2MB  554MB   528MB   primary   ext3
 3      554MB   1082MB  528MB   primary
 4      1082MB  1103MB  21.0MB  extended               lba
 5      1086MB  1095MB  8389kB  logical
 6      1099MB  1103MB  4194kB  logical

Incorrect firstLBA & sectors in logical partitions

Hi, I have a disk with the following partitions:

$ fdisk ~/Desktop/resin.img
Disk: /Users/jviotti/Desktop/resin.img  geometry: 717/64/63 [2891776 sectors]
Signature: 0xAA55
         Starting       Ending
 #: id  cyl  hd sec -  cyl  hd sec [     start -       size]
------------------------------------------------------------------------
 1: 0C 1023 254  63 - 1023 254  63 [      8192 -      40960] Win95 FAT32L
 2: 83 1023 254  63 - 1023 254  63 [     49152 -     360448] Linux files*
 3: 83 1023 254  63 - 1023 254  63 [    409600 -     360448] Linux files*
 4: 05 1023 254  63 - 1023 254  63 [    770048 -    2121728] Extended DOS
Signature: 0xAA55
         Starting       Ending
 #: id  cyl  hd sec -  cyl  hd sec [     start -       size]
------------------------------------------------------------------------
*1: 0B 1023 254  63 - 1023 254  63 [    778240 -       8192] Win95 FAT-32
 2: 05 1023 254  63 - 1023 254  63 [    786432 -    2105344] Extended DOS
 3: 00    0   0   0 -    0   0   0 [         0 -          0] unused
 4: 00    0   0   0 -    0   0   0 [         0 -          0] unused
Signature: 0xAA55
         Starting       Ending
 #: id  cyl  hd sec -  cyl  hd sec [     start -       size]
------------------------------------------------------------------------
 1: 83 1023 254  63 - 1023 254  63 [    794624 -    2097152] Linux files*
 2: 00    0   0   0 -    0   0   0 [         0 -          0] unused
 3: 00    0   0   0 -    0   0   0 [         0 -          0] unused
 4: 00    0   0   0 -    0   0   0 [         0 -          0] unused

Parsing the MBR returns the correct firstLBA for the primary partitions:

  partitions:
   [ { status: 0,
       type: 12,
       sectors: 40960,
       firstLBA: 8192,
       firstCHS: <CHS { cylinder: 0, head: 0, sector: 0 }>,
       lastCHS: <CHS { cylinder: 0, head: 0, sector: 0 }> },
     { status: 0,
       type: 131,
       sectors: 360448,
       firstLBA: 49152,
       firstCHS: <CHS { cylinder: 0, head: 0, sector: 0 }>,
       lastCHS: <CHS { cylinder: 0, head: 0, sector: 0 }> },
     { status: 0,
       type: 131,
       sectors: 360448,
       firstLBA: 409600,
       firstCHS: <CHS { cylinder: 0, head: 0, sector: 0 }>,
       lastCHS: <CHS { cylinder: 0, head: 0, sector: 0 }> },
     { status: 0,
       type: 5,
       sectors: 2121728,
       firstLBA: 770048,
       firstCHS: <CHS { cylinder: 0, head: 0, sector: 0 }>,
       lastCHS: <CHS { cylinder: 0, head: 0, sector: 0 }> } ],

This perfectly matches fdisk:

 #: id  cyl  hd sec -  cyl  hd sec [     start -       size]
------------------------------------------------------------------------
 1: 0C 1023 254  63 - 1023 254  63 [      8192 -      40960] Win95 FAT32L
 2: 83 1023 254  63 - 1023 254  63 [     49152 -     360448] Linux files*
 3: 83 1023 254  63 - 1023 254  63 [    409600 -     360448] Linux files*
 4: 05 1023 254  63 - 1023 254  63 [    770048 -    2121728] Extended DOS

I happen to know the fourth partition is an extended partition. Reading it's EBR returns:

  partitions:
   [ { status: 0,
       type: 12,
       sectors: 40960,
       firstLBA: 8192,
       firstCHS: <CHS { cylinder: 0, head: 0, sector: 0 }>,
       lastCHS: <CHS { cylinder: 0, head: 0, sector: 0 }> },
     { status: 0,
       type: 131,
       sectors: 360448,
       firstLBA: 49152,
       firstCHS: <CHS { cylinder: 0, head: 0, sector: 0 }>,
       lastCHS: <CHS { cylinder: 0, head: 0, sector: 0 }> },
     { status: 0,
       type: 131,
       sectors: 360448,
       firstLBA: 409600,
       firstCHS: <CHS { cylinder: 0, head: 0, sector: 0 }>,
       lastCHS: <CHS { cylinder: 0, head: 0, sector: 0 }> },
     { status: 0,
       type: 5,
       sectors: 2121728,
       firstLBA: 770048,
       firstCHS: <CHS { cylinder: 0, head: 0, sector: 0 }>,
       lastCHS: <CHS { cylinder: 0, head: 0, sector: 0 }> } ],

Now compare that to the output of fdisk:

         Starting       Ending
 #: id  cyl  hd sec -  cyl  hd sec [     start -       size]
------------------------------------------------------------------------
*1: 0B 1023 254  63 - 1023 254  63 [    778240 -       8192] Win95 FAT-32
 2: 05 1023 254  63 - 1023 254  63 [    786432 -    2105344] Extended DOS
 3: 00    0   0   0 -    0   0   0 [         0 -          0] unused
 4: 00    0   0   0 -    0   0   0 [         0 -          0] unused
Signature: 0xAA55
         Starting       Ending
 #: id  cyl  hd sec -  cyl  hd sec [     start -       size]
------------------------------------------------------------------------
 1: 83 1023 254  63 - 1023 254  63 [    794624 -    2097152] Linux files*
 2: 00    0   0   0 -    0   0   0 [         0 -          0] unused
 3: 00    0   0   0 -    0   0   0 [         0 -          0] unused
 4: 00    0   0   0 -    0   0   0 [         0 -          0] unused

The first logical partition says:

       sectors: 40960,
       firstLBA: 8192,

Assuming the returned firstLBA is relative to the primary partitions firstLBA, I get 770048 + 8192 = 778240.

fdisk says:

*1: 0B 1023 254  63 - 1023 254  63 [    778240 -       8192] Win95 FAT-32

The start offset clearly matches, however fdisk says the size is 8192, while node-mgr says 40960.

The second logical partition says:

       sectors: 360448,
       firstLBA: 49152,

If I take the second logical partition firstLBA, assuming is relative I get 770048 + 49152 = 819200, which doesn't match fdisk:

 2: 05 1023 254  63 - 1023 254  63 [    786432 -    2105344] Extended DOS

And so on with the following logical partitions.

Am I doing anything wrong?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.