Is there a tutorial for booting from USB?

Programming, for all ages and all languages.
Post Reply
z0q
Posts: 5
Joined: Tue Dec 09, 2014 9:57 am

Is there a tutorial for booting from USB?

Post 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 :)
Octocontrabass
Member
Member
Posts: 5513
Joined: Mon Mar 25, 2013 7:01 pm

Re: Is there a tutorial for booting from USB?

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

Re: Is there a tutorial for booting from USB?

Post 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.
z0q
Posts: 5
Joined: Tue Dec 09, 2014 9:57 am

Re: Is there a tutorial for booting from USB?

Post 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?
Octocontrabass
Member
Member
Posts: 5513
Joined: Mon Mar 25, 2013 7:01 pm

Re: Is there a tutorial for booting from USB?

Post 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.
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: Is there a tutorial for booting from USB?

Post 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.
"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 ]
M2004
Member
Member
Posts: 65
Joined: Sun Mar 07, 2010 2:12 am

Re: Is there a tutorial for booting from USB?

Post 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
Octocontrabass
Member
Member
Posts: 5513
Joined: Mon Mar 25, 2013 7:01 pm

Re: Is there a tutorial for booting from USB?

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

Re: Is there a tutorial for booting from USB?

Post 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).
z0q
Posts: 5
Joined: Tue Dec 09, 2014 9:57 am

Re: Is there a tutorial for booting from USB?

Post 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 :)
User avatar
beyondsociety
Member
Member
Posts: 39
Joined: Tue Oct 17, 2006 10:35 pm
Location: Eagle, ID (USA)
Contact:

Re: Is there a tutorial for booting from USB?

Post by beyondsociety »

To treat your flash drive as hard disk emulation, you must have a valid partition file and one Bootable partition.
"I think it may be time for some guru meditation"
"Barbarians don't do advanced wizardry"
freecrac
Member
Member
Posts: 69
Joined: Thu Sep 20, 2012 5:11 am
Location: germany hamburg

Re: Is there a tutorial for booting from USB?

Post by freecrac »

z0q
Posts: 5
Joined: Tue Dec 09, 2014 9:57 am

Re: Is there a tutorial for booting from USB?

Post 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 :)
z0q
Posts: 5
Joined: Tue Dec 09, 2014 9:57 am

Re: Is there a tutorial for booting from USB?

Post by z0q »

Bump!
User avatar
iansjack
Member
Member
Posts: 4685
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Is there a tutorial for booting from USB?

Post 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.
Post Reply