4

Recently I have written a bootloader in x86 Assembly. The bootloader does not do anything special right now, but I am planning to load my own kernel with this bootloader. I am able to write the bootloader to my FAT32 formatted USB Flash drive and it will be loaded by the BIOS correctly.

How can I make the USB Flash drive bootable by both BIOS and UEFI?

Michiel Pater
  • 22,377
  • 5
  • 43
  • 57

2 Answers2

7

Removable media does not need to be GPT formatted in order for UEFI to boot from it. You need to create efi/boot folder on a FAT partition on a removable medium and place your UEFI bootloader there. File name must be bootx64.efi for X86-64 architecture. Booting in Legacy or BIOS mode will be handled without changes - via MBR. In pure UEFI boot mode it will read /efi/boot/bootx64.efi file.
Please note also, that FAT partition should be addressed by the first MBR partition entry and be active.

Alexander Zhak
  • 9,140
  • 4
  • 46
  • 72
  • Thank you. My USB Flash drive does not have an MBR. It is simply a single FAT32 partition. Would that be okay? And am I able to load my Assembly code from the UEFI bootloader? – Michiel Pater Mar 30 '15 at 13:09
  • 1
    Hmm, frankly speaking I never tried plugging in USB sticks formatted as floppies (without MBR/GPT partitioning). UEFI enters long mode prior to loading `bootx64.efi`. Basically, UEFI executable is a bit customized PE file (Win DLL). You can then load your kernel and whatever you want using UEFI services. BIOS interrupts won't work. Please refer to the specification for details www.uefi.org/sites/default/files/resources/2_4_Errata_B.pdf. – Alexander Zhak Mar 30 '15 at 13:17
  • By the way, I did not manage to format my USB Flash drive to FAT32 with MBR and write my bootloader to the active partition. Could you give me some direction on how to do this in Windows 7? If I format my USB to FAT32, it has only one partition and no MBR. – Michiel Pater Mar 30 '15 at 13:36
  • 1
    MBR is a structure which resides in the very first sector of the medium. It contains small boot program (440 bytes max) which loads boot sector of an active partition and a partition table (4 entries) describing partiotions on the medium. If you formatted drive under Windows, then MBR is already there. – Alexander Zhak Mar 30 '15 at 13:49
  • And how do I know which sector is the boot sector of the active partition where I can write my bootloader? – Michiel Pater Mar 30 '15 at 14:06
  • 2
    Please read about MBR structure on the Internet. It's not a hard, but rather large topic for comments. I suggest starting with http://wiki.osdev.org/MBR , http://thestarman.pcministry.com/asm/mbr/95BMEMBR.htm , http://en.wikipedia.org/wiki/Master_boot_record – Alexander Zhak Mar 30 '15 at 14:14
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/74109/discussion-between-michiel-pater-and-alexander-zhak). – Michiel Pater Mar 30 '15 at 14:49
  • I have my bootloader on an USB stick. It boots fine at my University's lab, which are specially configured to boot those sticks. But I can't make it boot on my old HP Elitebook (2530p). The bios is configured to boot from USB. Is there anything that might be preventing it to boot? – francisaugusto Mar 21 '16 at 16:36
-3

To do this you will need to restart your computer. Once it first launches you have to press the boot options key. This is generally F2 or Del. From there you can choose which device your computer will boot from.

However, be careful and make sure you are 100% sure you know what you are doing. I would recommend instead you install a VM and use that as the test subject for the OS you are making. This way you do not have to restart your computer as much and there's less risk of damaging it.

CMilby
  • 624
  • 1
  • 6
  • 23
  • 2
    Thank you, but I know how to boot from UEFI. My question is `How can I make the USB Flash drive bootable by both BIOS and UEFI?`. – Michiel Pater Mar 30 '15 at 12:43