Page 1 of 1

Is there a tutorial for booting from USB?

Posted: Wed Dec 10, 2014 6:05 am
by z0q
Dear Developers,

I would like to learn how to write a bootloader for USB sticks and how it works. Preferably in a deeper way (e.g. machine code).

I have a few questions:
- Is there a good tutorial about this?
- Is this bootloader rewriteable?

Thanks in advance,

Z0q :)

Re: Is there a tutorial for booting from USB?

Posted: Wed Dec 10, 2014 6:56 am
by Octocontrabass
z0q wrote:- Is there a good tutorial about this?
Any tutorial that covers writing a bootloader for a floppy disk or hard disk using BIOS int 0x13 is equally applicable to USB flash drives. The BIOS will choose to emulate your flash drive as either a floppy disk or hard disk based on its contents. Floppy disk emulation may not allow you to access the entire flash drive, so you should use hard disk emulation. In order to use hard disk emulation, your flash drive must have a valid partition table with one bootable partition.

Alternately, you may choose to write a UEFI bootloader instead of a BIOS bootloader. This only requires a FAT32 filesystem on the flash drive, from which the UEFI firmware will load your bootloader file.
z0q wrote:- Is this bootloader rewriteable?
Is your USB flash drive rewritable?

Re: Is there a tutorial for booting from USB?

Posted: Wed Dec 10, 2014 7:22 am
by Muazzam
A USB drive boot loader works same as a either a hard disk boot loader or either a floppy boot loader. You can start as writing floppy boot loader. A good step by step tutorial of writing floppy boot loader is at http://www.brokenthorn.com/Resources/OSDevIndex.html in assembly language. Which is same for USB drive.

Re: Is there a tutorial for booting from USB?

Posted: Wed Dec 10, 2014 8:48 am
by z0q
Thank you for your replies :)

Yes, it is rewriteable, but I have to write in the special Boot sector right? I remember that I could throw away floppies after writing to their boot sectors with a work in progress bootloader.

How can I write to the boot sector of the USB stick?

Re: Is there a tutorial for booting from USB?

Posted: Wed Dec 10, 2014 9:06 am
by Octocontrabass
z0q wrote:I remember that I could throw away floppies after writing to their boot sectors with a work in progress bootloader.
This is because you overwrote the FAT12 BPB. That data is necessary for operating systems to read and write files from a FAT12-formatted floppy disk. Reformatting the floppy disk will restore its original functionality, but erase your bootloader.

If your flash drive bootloader doesn't include the necessary data structures, the same thing will happen when you put it on the flash drive. Just like the floppy disk, reformatting the flash drive will restore its original functionality and erase your bootloader.

Re: Is there a tutorial for booting from USB?

Posted: Wed Dec 10, 2014 9:10 am
by Combuster
Is there a good tutorial about this?
The Wiki has a page on Beginner Mistakes, and the second item reads:
Is there a tutorial on..?

Because this place can not and does not cater for beginner developers, the question for some other place that does provide a tutorial, good explanations or easy to understand reading is often requested. However, they do not exist. Difficult subjects can not be described with light prose, just like there are enough things that are too complicated for a monkey to properly learn. If you have trouble reading official documentation, this would be a good time to practice.
You might want to read the rest of the wiki as well considering you have missed the basics.

Re: Is there a tutorial for booting from USB?

Posted: Wed Dec 10, 2014 9:16 am
by M2004
Mike Gonta has written some stuff about usb booting:

USB Booting Secrets: http://board.flatassembler.net/topic.php?t=12389

more USB Booting Secrets: http://board.flatassembler.net/topic.php?t=12469

Regards
M2004

Re: Is there a tutorial for booting from USB?

Posted: Wed Dec 10, 2014 9:29 am
by Octocontrabass
M2004 wrote:Mike Gonta has written some stuff about usb booting:
"USB Booting Secret # 3" is wrong, BIOSes in floppy disk emulation mode will typically not provide LBA access. This is why hard disk emulation is the preferred mode for USB booting.

Re: Is there a tutorial for booting from USB?

Posted: Wed Dec 10, 2014 9:33 am
by Muazzam
z0q wrote:Thank you for your replies :)

Yes, it is rewriteable, but I have to write in the special Boot sector right? I remember that I could throw away floppies after writing to their boot sectors with a work in progress bootloader.

How can I write to the boot sector of the USB stick?
You can write boot sector to the first sector of USB drive (without partitioning) with special disk image utilities such as "dd" in Linux. dd on Windows is also available (using some tools).

Re: Is there a tutorial for booting from USB?

Posted: Tue Mar 17, 2015 3:26 pm
by z0q
Thank you all. My USB stick does now boot like a floppy.

I would like it to boot like a hard disk. Where do I start?

Sincerely :)

Re: Is there a tutorial for booting from USB?

Posted: Tue Mar 17, 2015 5:02 pm
by beyondsociety
To treat your flash drive as hard disk emulation, you must have a valid partition file and one Bootable partition.

Re: Is there a tutorial for booting from USB?

Posted: Tue Mar 17, 2015 5:31 pm
by freecrac

Re: Is there a tutorial for booting from USB?

Posted: Wed Mar 18, 2015 5:58 am
by z0q
How do I add my bootloader after formatting with HP USB Disk Storage Format Tool?

I have formatted my USB stick to FAT32 and manually edited the boot sector in HxD and overwrote the boot code with mine in binary. Now it boots and the drive is still accessible in Windows.

Would that be a good approach? I haven't reached the part of loading a kernel yet.

Thank you :)

Re: Is there a tutorial for booting from USB?

Posted: Wed Mar 25, 2015 12:48 pm
by z0q
Bump!

Re: Is there a tutorial for booting from USB?

Posted: Wed Mar 25, 2015 4:01 pm
by iansjack
Any tool that will write sectors to a disk can be used - the "dd" command is probably the easiest way. Using a hex editor to manually edit the codes is probably the hardest, and most error-prone, way to accomplish this task.

If you lack the knowledge to manipulate disk sectors in this way, and don't have the ability to use Google to find the answer, you are going to find OS development a very frustrating hobby. Do yourself a favour and learn how to research topics with the aid of Google. Everything that you have asked here is easily found.