Page 1 of 2
Booting from USB?
Posted: Thu Nov 27, 2014 7:19 am
by ThisMayWork
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
Re: Booting from USB?
Posted: Thu Nov 27, 2014 7:23 am
by Muazzam
Do you want to test on real hardware or qemu?
Re: Booting from USB?
Posted: Thu Nov 27, 2014 7:28 am
by ThisMayWork
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).
Re: Booting from USB?
Posted: Thu Nov 27, 2014 7:42 am
by Combuster
Floppy bootsectors don't work on partitioned media.
Re: Booting from USB?
Posted: Thu Nov 27, 2014 7:45 am
by ThisMayWork
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?
Re: Booting from USB?
Posted: Thu Nov 27, 2014 7:48 am
by Combuster
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.
Re: Booting from USB?
Posted: Thu Nov 27, 2014 7:53 am
by ThisMayWork
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?
Re: Booting from USB?
Posted: Thu Nov 27, 2014 8:07 am
by Muazzam
Combuster wrote:Floppy bootsectors don't work on partitioned media.
But why? For me it always worked. What about floppy emulation provided by BIOS? (Flash drive is same as pen drive or USB Key)
Re: Booting from USB?
Posted: Thu Nov 27, 2014 9:48 am
by SpyderTL
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.
Re: Booting from USB?
Posted: Thu Nov 27, 2014 10:43 am
by Kazinsal
muazzam wrote:Combuster wrote:Floppy bootsectors don't work on partitioned media.
But why? For me it always worked. What about floppy emulation provided by BIOS? (Flash drive is same as pen drive or USB Key)
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.
Re: Booting from USB?
Posted: Thu Nov 27, 2014 11:55 am
by bigbob
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.
Re: Booting from USB?
Posted: Thu Nov 27, 2014 4:09 pm
by ThisMayWork
Kazinsal wrote:muazzam wrote:Combuster wrote:Floppy bootsectors don't work on partitioned media.
But why? For me it always worked. What about floppy emulation provided by BIOS? (Flash drive is same as pen drive or USB Key)
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.
That's probably enough for me, and what I tried but failed to do... Can you share some details?
Re: Booting from USB?
Posted: Thu Nov 27, 2014 5:17 pm
by Octocontrabass
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.)
Re: Booting from USB?
Posted: Fri Nov 28, 2014 1:49 am
by ThisMayWork
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.)
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)
Re: Booting from USB?
Posted: Fri Nov 28, 2014 2:42 am
by Combuster
FAT12 (Can't change this
Sure you can. It saves you a ton of other problems you're currently setting yourself up for.