Booting from USB?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
ThisMayWork
Member
Member
Posts: 65
Joined: Sat Mar 22, 2014 1:14 pm
Location: /bin

Booting from USB?

Post 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
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
User avatar
Muazzam
Member
Member
Posts: 543
Joined: Mon Jun 16, 2014 5:59 am
Location: Shahpur, Layyah, Pakistan

Re: Booting from USB?

Post by Muazzam »

Do you want to test on real hardware or qemu?
User avatar
ThisMayWork
Member
Member
Posts: 65
Joined: Sat Mar 22, 2014 1:14 pm
Location: /bin

Re: Booting from USB?

Post 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).
"Programming is an art form that fights back."
-Kudzu
User avatar
Combuster
Member
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?

Post by Combuster »

Floppy bootsectors don't work on partitioned media.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
ThisMayWork
Member
Member
Posts: 65
Joined: Sat Mar 22, 2014 1:14 pm
Location: /bin

Re: Booting from USB?

Post 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?
"Programming is an art form that fights back."
-Kudzu
User avatar
Combuster
Member
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?

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
ThisMayWork
Member
Member
Posts: 65
Joined: Sat Mar 22, 2014 1:14 pm
Location: /bin

Re: Booting from USB?

Post 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?
"Programming is an art form that fights back."
-Kudzu
User avatar
Muazzam
Member
Member
Posts: 543
Joined: Mon Jun 16, 2014 5:59 am
Location: Shahpur, Layyah, Pakistan

Re: Booting from USB?

Post 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)
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: Booting from USB?

Post 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.
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
User avatar
Kazinsal
Member
Member
Posts: 559
Joined: Wed Jul 13, 2011 7:38 pm
Libera.chat IRC: Kazinsal
Location: Vancouver
Contact:

Re: Booting from USB?

Post 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.
bigbob
Member
Member
Posts: 122
Joined: Tue Oct 01, 2013 2:50 am
Location: Budapest, Hungary
Contact:

Re: Booting from USB?

Post 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.
User avatar
ThisMayWork
Member
Member
Posts: 65
Joined: Sat Mar 22, 2014 1:14 pm
Location: /bin

Re: Booting from USB?

Post 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?
"Programming is an art form that fights back."
-Kudzu
Octocontrabass
Member
Member
Posts: 5590
Joined: Mon Mar 25, 2013 7:01 pm

Re: Booting from USB?

Post 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.)
User avatar
ThisMayWork
Member
Member
Posts: 65
Joined: Sat Mar 22, 2014 1:14 pm
Location: /bin

Re: Booting from USB?

Post 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)
"Programming is an art form that fights back."
-Kudzu
User avatar
Combuster
Member
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?

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply