Booting from USB?
- ThisMayWork
- Member
- Posts: 65
- Joined: Sat Mar 22, 2014 1:14 pm
- Location: /bin
Booting from USB?
My OS boots normally from a floppy disk. I now want to test on real hardware and boot from a USB flash drive. I formatted a flash drive to FAT 12, copied my bootloader over using dd (the same way i copy it to the floppy disk) and then mounted it on OSX and copied over my kernel files. However, QEMU does not find a bootable device when I direct it to my driver's mount point (/dev/disk2s1). Am I doing something totally wrong?
UPDATE: Tested on real hardware, it isn't recognised as a bootable device on Macbook White Unibody
EDIT: Bootloader copy command: dd if=stage1.bin of=/dev/disk2s1 bs=512 count=1 conv=notrunc
UPDATE: Tested on real hardware, it isn't recognised as a bootable device on Macbook White Unibody
EDIT: Bootloader copy command: dd if=stage1.bin of=/dev/disk2s1 bs=512 count=1 conv=notrunc
Last edited by ThisMayWork on Thu Nov 27, 2014 7:33 am, edited 1 time in total.
"Programming is an art form that fights back."
-Kudzu
-Kudzu
Re: Booting from USB?
Do you want to test on real hardware or qemu?
- ThisMayWork
- Member
- Posts: 65
- Joined: Sat Mar 22, 2014 1:14 pm
- Location: /bin
Re: Booting from USB?
My post was unclear. Sorry about that. I want to test on real hardware, but before trying it out on a computer I decided to check if my live USB worked correctly on QEMU (and it didn't, apparently).
"Programming is an art form that fights back."
-Kudzu
-Kudzu
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: Booting from USB?
Floppy bootsectors don't work on partitioned media.
- ThisMayWork
- Member
- Posts: 65
- Joined: Sat Mar 22, 2014 1:14 pm
- Location: /bin
Re: Booting from USB?
Is there any way to fake the computer into thinking the flash drive is an "unpartitioned media"? Maybe use the main partition as a floppy?
"Programming is an art form that fights back."
-Kudzu
-Kudzu
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: Booting from USB?
Did you actually look up what partitions are? If you know what they are you can also find a way to get rid of them.
- ThisMayWork
- Member
- Posts: 65
- Joined: Sat Mar 22, 2014 1:14 pm
- Location: /bin
Re: Booting from USB?
I need to do my research, but before that, one more quick question. I believe an MBR looks something like this:
-Bootloader
-Partition 1
-Partition 2
.
.
.
-0x55
-0xAA
That means all I need to do is describe Partition 1 in the correct place in my boot code, right? Also, if I do this correctly, it will still be applicable to floppy disks without any changes, right?
-Bootloader
-Partition 1
-Partition 2
.
.
.
-0x55
-0xAA
That means all I need to do is describe Partition 1 in the correct place in my boot code, right? Also, if I do this correctly, it will still be applicable to floppy disks without any changes, right?
"Programming is an art form that fights back."
-Kudzu
-Kudzu
Re: Booting from USB?
But why? For me it always worked. What about floppy emulation provided by BIOS? (Flash drive is same as pen drive or USB Key)Combuster wrote:Floppy bootsectors don't work on partitioned media.
Re: Booting from USB?
For now, I would recommend just bypassing the file system altogether. Just use DD to copy your boot loader directly to the first block on your USB drive.
Also, make sure you use the value in the DL register if you need to load any files from the USB drive in your boot loader.
Then, for Bochs, you can just use /dev/disk2 as your drive image.
This is not quite the same as booting real hardware from your USB drive, but it's close, and this approach should work for both Bochs and real hardware.
Also, make sure you use the value in the DL register if you need to load any files from the USB drive in your boot loader.
Then, for Bochs, you can just use /dev/disk2 as your drive image.
This is not quite the same as booting real hardware from your USB drive, but it's close, and this approach should work for both Bochs and real hardware.
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
- Kazinsal
- Member
- Posts: 559
- Joined: Wed Jul 13, 2011 7:38 pm
- Libera.chat IRC: Kazinsal
- Location: Vancouver
- Contact:
Re: Booting from USB?
That's not partitioned media. That's pretending the first physical 2880 512-byte sectors of the USB drive are a floppy drive and making the rest effectively unusable.muazzam wrote:But why? For me it always worked. What about floppy emulation provided by BIOS? (Flash drive is same as pen drive or USB Key)Combuster wrote:Floppy bootsectors don't work on partitioned media.
Re: Booting from USB?
Your original question was how to have a bootable USB-drive from a floppy-image, if I am not mistaken.
Some time ago I experimented with it, and managed to boot from a USB-drive by using the "file.img" that I used with Bochs.
These are my notes:
"On linux a bootable CD can easily be created:
genisoimage -R -b file.img -o cdrom.iso cddir/
And write cdrom.iso to a CD.
The -b option means bootable CD and floppy-emulation will be done during boot.
file.img is our floppy-imagefile. The path to file.img is relative to the folder that will be burned to CD (cddir in this case). So now file.img is in cddir with all the other file we want to write to CD.
If our BIOS supports booting from an USB we can do the same thing with a pen-drive."
As far as I can remember, just write the image(or iso!?) to the USB-disk as we did with the CD above.
EDIT: of course, this is done with floppy-emulation.
Some time ago I experimented with it, and managed to boot from a USB-drive by using the "file.img" that I used with Bochs.
These are my notes:
"On linux a bootable CD can easily be created:
genisoimage -R -b file.img -o cdrom.iso cddir/
And write cdrom.iso to a CD.
The -b option means bootable CD and floppy-emulation will be done during boot.
file.img is our floppy-imagefile. The path to file.img is relative to the folder that will be burned to CD (cddir in this case). So now file.img is in cddir with all the other file we want to write to CD.
If our BIOS supports booting from an USB we can do the same thing with a pen-drive."
As far as I can remember, just write the image(or iso!?) to the USB-disk as we did with the CD above.
EDIT: of course, this is done with floppy-emulation.
- ThisMayWork
- Member
- Posts: 65
- Joined: Sat Mar 22, 2014 1:14 pm
- Location: /bin
Re: Booting from USB?
That's probably enough for me, and what I tried but failed to do... Can you share some details?Kazinsal wrote:That's not partitioned media. That's pretending the first physical 2880 512-byte sectors of the USB drive are a floppy drive and making the rest effectively unusable.muazzam wrote:But why? For me it always worked. What about floppy emulation provided by BIOS? (Flash drive is same as pen drive or USB Key)Combuster wrote:Floppy bootsectors don't work on partitioned media.
"Programming is an art form that fights back."
-Kudzu
-Kudzu
-
- Member
- Posts: 5590
- Joined: Mon Mar 25, 2013 7:01 pm
Re: Booting from USB?
In order to make a filesystem bootable on a flash drive, you have two options: partitioned and unpartitioned.
A partitioned device needs a MBR/partition table with one partition marked bootable in LBA 0, and your FAT bootloader in the first sector of the bootable partition. You can borrow or write your own DOS-like MBR, but you can't customize it too much since some BIOSes will emulate a DOS MBR instead of running the one on the disk.
An unpartitioned device starts the FAT filesystem on LBA 0, just like a floppy disk. However, you have access to the entire device instead of just a floppy-disk-sized chunk at the beginning. Be aware that LBA addressing will probably not be available when you do this. (I have never tried a device too large for CHS addressing, so I don't know what will happen if you do that.)
You can use the same FAT bootloader for both of these, as long as you obey the "number of hidden sectors" field in the BPB. Some of Microsoft's boot sectors do this.
(I've spent way too much time writing boot code.)
A partitioned device needs a MBR/partition table with one partition marked bootable in LBA 0, and your FAT bootloader in the first sector of the bootable partition. You can borrow or write your own DOS-like MBR, but you can't customize it too much since some BIOSes will emulate a DOS MBR instead of running the one on the disk.
An unpartitioned device starts the FAT filesystem on LBA 0, just like a floppy disk. However, you have access to the entire device instead of just a floppy-disk-sized chunk at the beginning. Be aware that LBA addressing will probably not be available when you do this. (I have never tried a device too large for CHS addressing, so I don't know what will happen if you do that.)
You can use the same FAT bootloader for both of these, as long as you obey the "number of hidden sectors" field in the BPB. Some of Microsoft's boot sectors do this.
(I've spent way too much time writing boot code.)
- ThisMayWork
- Member
- Posts: 65
- Joined: Sat Mar 22, 2014 1:14 pm
- Location: /bin
Re: Booting from USB?
Going with the second option, that means I just have to format my flash drive in FAT12 (Can't change this, my bootloader will only read my kernel off FAT12) and then directly copy my bootloader on it using dd, right? LBA 0 is just the first 'block' of the drive. (I think)Octocontrabass wrote: An unpartitioned device starts the FAT filesystem on LBA 0, just like a floppy disk. However, you have access to the entire device instead of just a floppy-disk-sized chunk at the beginning. Be aware that LBA addressing will probably not be available when you do this. (I have never tried a device too large for CHS addressing, so I don't know what will happen if you do that.)
"Programming is an art form that fights back."
-Kudzu
-Kudzu
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: Booting from USB?
Sure you can. It saves you a ton of other problems you're currently setting yourself up for.FAT12 (Can't change this