How to make bootloader for USB flash memory ?

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.
Pem3
Posts: 9
Joined: Sat Nov 23, 2013 5:08 pm

How to make bootloader for USB flash memory ?

Post by Pem3 »

Hey. I wanted to move my bootloader with my small kernel from floppy to usb.
I formated usb as fat32. After that i downloaded tool HxD to edit first 512 bytes of usb memory flash and then i put there my bootloader's 512 bytes. After i rebooted my pc to test it (usb flash was found in bios and selected as primary device) then i got bios message that no bootable device found.

What is wrong with usb ?

I have checked after that and first 512 bytes of 1st sector were correct as my bootloader's file.

What may be reason that usb is not discovered as bootable by bios ?
User avatar
mathematician
Member
Member
Posts: 437
Joined: Fri Dec 15, 2006 5:26 pm
Location: Church Stretton Uk

Re: How to make bootloader for USB flash memory ?

Post by mathematician »

It is probably looking for a master boot record and partition table.
The continuous image of a connected set is connected.
Octocontrabass
Member
Member
Posts: 5604
Joined: Mon Mar 25, 2013 7:01 pm

Re: How to make bootloader for USB flash memory ?

Post by Octocontrabass »

USB devices can either be formatted like a large floppy disk (sector 0 contains the volume boot record) or a hard disk (sector 0 contains the master boot record and partition table). You might be dealing with the latter, in which case you'll need to ensure that sector 0 contains a MBR that will load your VBR. In HxD, choose "Removable Disk 1" instead of a drive letter.

Some BIOSes are picky about what they'll accept. Make sure you have the boot signature at the end of the MBR and VBR. If you have a MBR, make sure the partition table is correct and the appropriate partition is marked as bootable. Make sure the values in the BPB are correct.
Pem3
Posts: 9
Joined: Sat Nov 23, 2013 5:08 pm

Re: How to make bootloader for USB flash memory ?

Post by Pem3 »

mathematician wrote:It is probably looking for a master boot record and partition table.

Well I have done it:
1. format usb flash using windows tool with fat32
2. edit with HxD tool first 512 bytes (i put there my bootloader)
Octocontrabass wrote:USB devices can either be formatted like a large floppy disk (sector 0 contains the volume boot record) or a hard disk (sector 0 contains the master boot record and partition table). You might be dealing with the latter, in which case you'll need to ensure that sector 0 contains a MBR that will load your VBR. In HxD, choose "Removable Disk 1" instead of a drive letter.

Some BIOSes are picky about what they'll accept. Make sure you have the boot signature at the end of the MBR and VBR. If you have a MBR, make sure the partition table is correct and the appropriate partition is marked as bootable. Make sure the values in the BPB are correct.
Well I dont understand it
Could you explain how to deal with it more clearly ? ;)
For floppy i have been using WinImage tool where i have only selected my bootloader and thats all.
How to prepear it to work correctly ?
mikegonta
Member
Member
Posts: 229
Joined: Thu May 19, 2011 5:13 am
Contact:

Re: How to make bootloader for USB flash memory ?

Post by mikegonta »

Pem3 wrote:Hey. I wanted to move my bootloader with my small kernel from floppy to usb.
Start with that first. Simply copy (with HxD) the floppy disk image to the flash drive. Boot and run.
Mike Gonta
look and see - many look but few see

https://mikegonta.com
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

Re: How to make bootloader for USB flash memory ?

Post by BMW »

I have done exactly what you are trying to do. You could try the following (assuming you are using Windows):

Open a CMD window and execute the following commands: (remove ')

Code: Select all

'diskpart'
'list disk'
'select disk #' (replace # with the corresponding disk number - MAKE SURE IT IS YOUR FLASH DRIVE NOT YOUR HARD DRIVE)
'clean'
'create partition primary'
'select partition 1'
'active'
'format FS=FAT32'
'assign'
'exit'
Now try putting your bootloader on with HxD and booting from it.
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
Pem3
Posts: 9
Joined: Sat Nov 23, 2013 5:08 pm

Re: How to make bootloader for USB flash memory ?

Post by Pem3 »

mikegonta wrote:
Pem3 wrote:Hey. I wanted to move my bootloader with my small kernel from floppy to usb.
Start with that first. Simply copy (with HxD) the floppy disk image to the flash drive. Boot and run.
I have checked what kind of bytes are first 512 bytes of floppy image file and they seem to be some kind of meta data
Becouse last bytes (byte 510 and 511) were not aa55

BMW wrote:I have done exactly what you are trying to do. You could try the following (assuming you are using Windows):

Open a CMD window and execute the following commands: (remove ')

Code: Select all

'diskpart'
'list disk'
'select disk #' (replace # with the corresponding disk number - MAKE SURE IT IS YOUR FLASH DRIVE NOT YOUR HARD DRIVE)
'clean'
'create partition primary'
'select partition 1'
'active'
'format FS=FAT32'
'assign'
'exit'
Now try putting your bootloader on with HxD and booting from it.

Ok, but how to put it that way ?
Formating makes some byte's modification of first 512 bytes
my boot file is equal 512 bytes
so how to deal with it ?
Pem3
Posts: 9
Joined: Sat Nov 23, 2013 5:08 pm

Re: How to make bootloader for USB flash memory ?

Post by Pem3 »

I have formated as fat32 and loaded my bootloader code, but its not booting anyway ?!

According to this article http://www.easeus.com/resource/fat32-disk-structure.htm , first 446 bytes are my code.

So i "pasted" my first 446 bytes of my booloader into these memory space. My bootloader is aboout 300 bytes so some of these 446 are zeros. After these 446 bytes is partition 1st information that i left alone (16 bytes). Next are 3*16 bytes with zeros - info about other partitions. And last 2 bytes are 0x55 and 0xaa.


What is wrong here ?

BIOS not see this usb flash memory as correct bootable device. Why ?

or meaby I am understanding it in wrong way... in floppy there was 1 partition for 1,44MB
in fat32 i can have up to 4 partitions
and how about my bootloader ? where to put code of it ?
in 1st sector of usb memory flash or 1st sector of partition ?

if 1st sector of partitin then how to calculate what sector of partition data as first (according to http://www.easeus.com/resource/fat32-disk-structure.htm) ?
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

Re: How to make bootloader for USB flash memory ?

Post by BMW »

You could try putting the BPB entries in your bootloader for the FAT filesystem.

Do you have the magic bytes at the end of your bootloader (0x55, 0xAA)?

Maybe it's your computer, have you tried it on an emulator?
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
Octocontrabass
Member
Member
Posts: 5604
Joined: Mon Mar 25, 2013 7:01 pm

Re: How to make bootloader for USB flash memory ?

Post by Octocontrabass »

Pem3 wrote:After these 446 bytes is partition 1st information that i left alone (16 bytes). Next are 3*16 bytes with zeros - info about other partitions. And last 2 bytes are 0x55 and 0xaa.


What is wrong here ?
You need two bootloaders.

The first, in sector 0, is the MBR. You can copy Microsoft's code for now. You must also mark your partition as bootable (the first byte of the partition info must be 0x80).

The second, at the start of the partition, is the VBR. (When you open the drive letter in HxD, it will appear as sector 0 even though it isn't really at the beginning of the disk.) That's where you put your FAT32 code. Don't overwrite the BPB!


Some computers will not boot your drive unless both of these bootloaders have 0x55, 0xAA in the last two bytes.
Pem3
Posts: 9
Joined: Sat Nov 23, 2013 5:08 pm

Re: How to make bootloader for USB flash memory ?

Post by Pem3 »

BMW wrote:You could try putting the BPB entries in your bootloader for the FAT filesystem.
I don't understand this answer. Could you possible write it more simply ? ;)
BMW wrote:Do you have the magic bytes at the end of your bootloader (0x55, 0xAA)?
Yes
BMW wrote:Maybe it's your computer, have you tried it on an emulator?
I have tried it with

I haven't tried it with Bochs yet, because I didn't find any solution for current bochs version to use usb.
I tried it with PC and VirutalBox, result is same - no bootable disc found.

Octocontrabass wrote:
Pem3 wrote:After these 446 bytes is partition 1st information that i left alone (16 bytes). Next are 3*16 bytes with zeros - info about other partitions. And last 2 bytes are 0x55 and 0xaa.


What is wrong here ?
You need two bootloaders.

The first, in sector 0, is the MBR. You can copy Microsoft's code for now. You must also mark your partition as bootable (the first byte of the partition info must be 0x80).

The second, at the start of the partition, is the VBR. (When you open the drive letter in HxD, it will appear as sector 0 even though it isn't really at the beginning of the disk.) That's where you put your FAT32 code. Don't overwrite the BPB!


Some computers will not boot your drive unless both of these bootloaders have 0x55, 0xAA in the last two bytes.
How do the first bootloader know where is my second bootloader ? Bios loads first 512 bytes (1st bootloader) into 0x7c00. How about second bootloader ? How code is hidden inside these magic bytes of http://thestarman.pcministry.com/asm/mb ... br.htm#CHS ?

What do you mean fat32 code ?

Which bytes are BPB ?
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

Re: How to make bootloader for USB flash memory ?

Post by BMW »

Pem3 wrote: Which bytes are BPB ?
BPB == Bios Parameter Block

http://wiki.osdev.org/FAT#BPB_.28BIOS_P ... r_Block.29
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
Octocontrabass
Member
Member
Posts: 5604
Joined: Mon Mar 25, 2013 7:01 pm

Re: How to make bootloader for USB flash memory ?

Post by Octocontrabass »

Pem3 wrote:How do the first bootloader know where is my second bootloader ?
The first bootloader (MBR) reads the partition table to find the partition where you've put the second bootloader (VBR; your code).
Pem3 wrote:What do you mean fat32 code ?
Your code boots a FAT32 volume, correct?
mrstobbe
Member
Member
Posts: 62
Joined: Fri Nov 08, 2013 7:40 pm

Re: How to make bootloader for USB flash memory ?

Post by mrstobbe »

Pem3 wrote:I tried it with PC and VirutalBox
I'm sorry. Seriously so, I'm about to be a jackass... but I assume you mean Windows when you say "PC". Please don't do that. Please... I'm begging you. You mean Windows... just say "Windows". I could go on a rant here (and am obviously geared up for one), but I won't.

Super sorry everyone. I hate to be that guy, but that hit a button hard. Enough so that I was willing to write this and apologize in it at the same time, and hit submit.
Pem3
Posts: 9
Joined: Sat Nov 23, 2013 5:08 pm

Re: How to make bootloader for USB flash memory ?

Post by Pem3 »

Octocontrabass wrote:
Pem3 wrote:How do the first bootloader know where is my second bootloader ?
The first bootloader (MBR) reads the partition table to find the partition where you've put the second bootloader (VBR; your code).
Ok, clear now. And how about bytes -> code. Is there available asembler code of these bytes (1st bootloader) ? Dunno how to find it by keywords in google, which keywords to use.
Octocontrabass wrote:
Pem3 wrote:What do you mean fat32 code ?
Your code boots a FAT32 volume, correct?
Still don't understand what do you mean by fat32 code ;) Do you mean by FAT32 code of both: bootloader code and BPB ?
So for fat32 it will be ( http://wiki.osdev.org/FAT#BPB_.28BIOS_P ... r_Block.29 ) :
0 - 2: jmp instruction to my boot code
3-35: bpb's data
36-89; ebpb's data
90-509; boot code
510-511; signature of 0xaa55

Is it correct ?

Because it's not working. I pasted my code from 90 byte (0x5a) and same result - message "reebot and select proper boot device"
mrstobbe wrote:
Pem3 wrote:I tried it with PC and VirutalBox
I'm sorry. Seriously so, I'm about to be a jackass... but I assume you mean Windows when you say "PC". Please don't do that. Please... I'm begging you. You mean Windows... just say "Windows". I could go on a rant here (and am obviously geared up for one), but I won't.

Super sorry everyone. I hate to be that guy, but that hit a button hard. Enough so that I was willing to write this and apologize in it at the same time, and hit submit.
I don't understand you answer... By saying "PC" i had on my mind situation where I:
1. restart PC by clicking button at pc's tover
2. go bios and select 1st bootable device - my usb memory flash drive
3. save bios settings and restart pc
4. check result of prepared bootable usb memory flash drive

By saying "VirtualBox" I meant:
1. Download and mount Plop Live ( http://www.plop.at/ ) to be possible to use USB as boot device (VirtualBox doesn't support it directly)
2. Mount my USB memory flash drive
3. restart virtualbox machine
4. check result of prepared bootable usb memory flash drive

I don't understand you. Shall I understand your answer as laughing of my person or what ? Have I written something funny or wrong ? I know that my question in this topic is funny as for person who was able to write in assembler a little advanced software like boot-loader and kernel, but so far I haven't worked directly with system files and so far I didn't know how is it working, yea have fun of that. I really don't understand your attitude.
Post Reply