1

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.

  • Sector 0 of the disk contains the partition table. It’s still mostly MBR, especially on portable devices. You might want to take a look at the Linux FAT32 driver. This question is probably better suited for Stack Overflow, by the way.+ – Daniel B Mar 20 '15 at 8:55
  • "USB device" is ambiguous. 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). A Flash drive should not be called a "disk". Linux always treats a USB Flash drive like a HDD. Windows can treat a USB Flash drive like a HDD, but typically will format it as if it's just a partition, i.e. no MBR at sector 0, and installs the filesystem starting at sector 0. Also the ISO filesystem is not correct for a HDD or Flash drive. – sawdust Mar 20 '15 at 19:31
  • 1
    That’s not correct, @sawdust. Windows also needs a partition table on USB “removable” drives. As such, the actual filesystem never starts at sector 0. On Linux, you could do this, though. – Daniel B Mar 20 '15 at 21:49
  • @DanielB - "That’s not correct" -- You're as ambiguous as the OP. What's not correct, everything I wrote? Is a HDD connected through a USB-SATA adapter a "USB “removable” drive"? – sawdust Mar 20 '15 at 22:51
  • @sawdust Nothing ambiguous about it. Because, you know, there’s a thing called context. // Surprisingly, Windows does indeed support unpartitioned devices, but this is in no way the standard case. Of course I won’t buy a new thumb drive just to see what’s on it, but I bet there’ll be a partition. A removable drive is a drive with the removable bit set. But you already knew that. – Daniel B Mar 20 '15 at 23:18
1

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.

  • Thank you very much for your answer. 1) Yes, I mean a USB Flash drive. 2) In HxD (a Hexadecimal editor for Windows) I opened my Flash drive for reading and writing via "Extras" > "Open disk...". It is visible under "Physical disks" and labeled as "Removable Disk 1". Did you use Quick Format or the regular Format in Windows? Thank you. – Michiel Pater Mar 20 '15 at 23:07
  • 1
    "Did you use Quick Format or the regular Format in Windows?" -- Quick, but I doubt that it makes a difference. Zeroing out the sectors may be more important. – sawdust Mar 20 '15 at 23:13
  • I have updated my question to be more clear about that I am talking about a Flash drive. Do you think I could achieve the same result in Windows by zeroing out sector 0? Or do I have to zero out all sectors? I am not sure, but if I remember correctly, I have tried to zero out sector 0 once too. I am not sure if I understand the difference between an MBR and a Filesystem boot sector correctly. Thank you. – Michiel Pater Mar 20 '15 at 23:20
  • "Or do I have to zero out all sectors?" Not all the sectors, but at least the first "track". When using the dd command with the count= specifier, it is just as easy to specify the first 64 sectors as specifying just one sector. On a Windows PC, I used to use a Western Digital or Seagate utility CD to zero out sectors. Now, if necessaary, I reboot with a Linux LiveCD installed on a USB Flash drive. See also the edited answer. – sawdust Mar 21 '15 at 0:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.