Ask HN: How to Visualize Logical Block Addressing?

2 pointsposted 6 hours ago
by shivajikobardan

Item id: 46465507

1 Comments

fuzzfactor

2 hours ago

Quite simply LBA just starts at sector 0 (otherwise known as block 0) and goes up from there, ideally without concerning the OS with anything like the CHS number.

Doesn't mean every OS itself is completely unconcerned :\

Old OS's like DOS worked with CHS geometry to begin with, but it didn't take long after HDDs got affordable that the CHS no longer truly represented the actual number of platters, heads, and sectors per platter. Note that a cylinder was already a logical construct and was usually composed of sectors contained on more than one platter.

Storage is one thing but for booting some OSs it won't boot, unless the key bootsector(s) that the OS is supposed to write to the HDD are effectively written to a cylinder boundary. Which interestingly on the same HDD can be placed at different LBA sector number locations depending on how the partitioning/formatting/bootfile-writing process (3 different things) was undertaken. And it can still be way different whether you start with a fully zeroed HDD or not. When needed, the internal HDD electronics present things like DOS with a translated geometry they understand after being processed by the mainboard. Autodetection of geometry by old or new OS's is another variable which can influence where the key filesystem bits get laid down, LBA detection itself was intentionally way more consistent from the beginning of LBA, but still not perfect either.

HDDs have mainly been SATA in the 21st century, but a good example is 10-year-old HDDs where the WD would almost always default to CHS 1023-254-63 but Toshiba retained the more traditional CHS 16383-16-63. These differences just don't come into play most of the time in the 21st century but are still there under the surface.

I think this is why I have (more than) two different SSDs from "identical" fleet laptops, each having similar Lenovo FRU part numbers, some from Union and others from Samsung. Same manufacturing month, completely interchangeable among a number of Lenovos of similar vintage, as expected after all are imaged from the same master HDD.

In an old AMD desktop though, only the Samsungs will boot even though the images were all laid down according to LBA. Put them back in the Lenovos and they all boot just fine. Files all fully intact and accessible at all times, it's just a booting deficiency.

In traditional legacy BIOS, early CHS storage geometries on different vendors' HDDs were sometimes unique for each vendor based on physical construction. If you had a HDD there was an interface PCB in an ISA slot that had to have its jumpers properly set for a particular geometry.

Eventually Integrated Drive Electronics (IDE) HDDs arrived which plugged straight in to the motherboard with a ribbon cable. These were pretty smart to begin with and it got to where it was generally expected that a new HDD would automatically detect and conform to whatever motherboard it might get installed in. OTOH motherboards got smarter too at the same time so if you got a new MB to go with your old not-so-smart HDD you could use the MB BIOS to set the geometry yourself in case the MB wasn't as smart as you wanted it to be.

CHS really only went up to 8GB so in advance of reaching that point LBA was layered on by all vendors, both MB and HDD. Different (augmented?) firmware basically.

OTOH LBA is a continuum which visualizes about like a number line but if CHS comes into play for some reason there is usually some electronics that will translate that to some kind of LBA relationship. Whether leaning heavily on the motherboard, internal drive circuitry or a combination of both.

It can really get hairy of you want maximum reliability from USB drives and SSD's which are usually not as adaptable as HDDs.

Here's something that's not really out-of-date completely or anything, but you need a lot of different references which are never enough so you need to pick up the ball and run the distance to your own goal lines yourself going forward:

https://thestarman.pcministry.com/asm/mbr/PartTables.htm

To see what's in the sectors a disk editor is used, which can basically view and/or change any byte in any sector to anything you want. Obviously you can really screw up some data this way if you are not careful. It's good to have an editor that displays both the hex and the text side-by-side.

I like DMDE, and this is a good way to visualize the CHS values of the partition table as they relate to the LBA.

DMDE can be downloaded free for personal use and run without installing, on Windows, Linux, or even DOS.

Obtain an established (but erasable) HDD or SSD that was "initialized" and partitioned in legacy BIOS configuration using MBR layout.

Therefore sector 0 will contain the (MBR &) partition table having entries for up to 4 different primary partitions.

Plug this MBR drive in as a secondary drive to a working PC so you can boot the PC the regular way using its own drive, and simply use your working PC to examine the additional drive.

Start DMDE. There will be some helpful hints for data recovery, ignore that for the next few steps. Choose the correct drive to examine. Leave the default "Show Partitions" checkbox checked. Hit OK.

A partition info popup showing some details will appear, close it (can always be opened again from the main menu). Now at the Help menu you can disable the "Step-by-step permanent" hints.

You are left looking at the main hex editor window. Sector 0 (it's special) will be "interpreted" by DMDE for your viewing pleasure when it comes to CHS and the LBA values that correspond, for the (primary) partition(s).

Other than that starting with sector 1 there is not such interpretation (unless you pick a sector and invoke a non-hex Mode from the menu) and each sector is displayed byte-for-byte, hex on the left and text on the right panel. 512 bytes per sector the traditional way. You can scroll down as far as you would like, way past doom by some measures :)

Most drives have 4K bytes per physical sector now but the 512/sector display convention still works just fine for disk editing, and even data recovery as good as ever.