Tuesday, October 24, 2017

Why and when does a quick format to FAT32 write different structures to sector 0 on a USB flash drive?



Introduction: For a research project I am trying to write a bootloader to a USB flash drive. After some experimenting, I succeeded to do this. I would like to know how I can reproduce this.




What I did: Recently I bought a new USB flash drive and opened the physical disk in HxD (a hexadecimal editor for Windows). It was already formatted as FAT32, but sector 0 had a completely different structure than described on this webpage



On this webpage it is clear that there are two strings present: MSDOS5.0 and FAT32. Both of them were not present in sector 0.



After experimenting and trying to quick format the drive in Windows to FAT32 the default way, sector 0 remained having different structures than the one described above. When I tried to quick format it again after using mkisofs for Windows and writing the ISO to the USB Flash drive using PowerISO, the structure of sector 0 finally changed to the one described above.



My question: Why did it change? And how can I reproduce this for new USB flash drives?



Edit: I need the sector to be in the same structure as described on the webpage for my project.


Answer





I am trying to write a bootloader to a USB device.




"USB device" is ambiguous. There are USB video and audio devices as well as various storage devices.
By "USB drive" you presumably mean a USB Flash drive, rather than a USB HDD (which always has a MBR in the first sector, rather than a filesystem boot sector).




It was already formatted as FAT32, but sector 0 had a completely different structure than described on this webpage





The Flash drive seems to have a MBR (Master Boot Record) with a partition table (in sector 0), and the FAT filesystem was located in a primary partition.
As that webpage mentions, those are sector dumps of boot sectors, which are installed in the first sector of a filesystem which would normally reside in a partition (or the first sector of a floppy diskette).




And how can I reproduce this for new USB drives?




I can install the filesystem boot sector in sector 0 of a USB Flash drive by





  1. zeroing out any MBR in sector zero. On a Linux system issue
    sudo dd if=/dev/zero of=/dev/sdX count=64
    This maybe the crucial step, especially if there is already an MBR. I don't know how to do this step on Windows.


  2. Use Windows to format the USB Flash drive (Windows will insist on formatting it before you can use it anyway). I used Win7 to 'quick format' the drive to get the results below.




The resulting sector 0 on the USB Flash drive resembles what you want (i.e. it's not a MBR and there's no partition table).
enter image description here



Addendum





I am not sure if I understand the difference between an MBR and a Filesystem boot sector correctly




The Master Boot Record, MBR, is installed in the first sector of a PC- partitioned mass storage device. At the tail end of the sector is a partition table that divides the device into smaller logical devices. One partition will be marked "active", and the boot code in the MBR will load and execute the first sector of that "active" partition.



The first sector of a bootable partition (or the first sector of a floppy diskette) contains a bootloader. The filesystem installed in that partition will have appropriate code in that first sector to continue the boot process when an OS is installed.
When there is no installed OS or next boot program to load (e.g. BOOTMGR), the bootloader outputs text that indicates that this is not a bootable device/partition (e.g. "Remove disks or other media. Disk error. Press any key to restart.")



IOW you want the USB Flash drive to resemble a super-high-capacity diskette instead of a (partitioned) HDD.
Beware of filesystem limits, e.g. the most portable filesystem, FAT32, is limited to 32GB.



FYI I'm cognizant of this quirk with USB Flash drives and SDcards because the omission of the MBR & partition table renders such storage media unreadable on some Linux embedded devices (that expect partitions). Tutorials on initializing bootable VFAT media for embedded Linux often specify partitioning & formmatting on Linux to avoid this Windows quirk.



No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...