Copying bootloader to FAT flash drive corrupts partition

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.
Post Reply
dragonfire353
Posts: 13
Joined: Tue Oct 01, 2013 11:45 pm

Copying bootloader to FAT flash drive corrupts partition

Post by dragonfire353 »

Hello, I'm using a mac and have formatted a flash drive as fat under disk utility. I then used the dd command in the terminal to write my 512 byte bootloader to the flash drive. This works in virtual environments and does get booted on my regular pc, but on both my mac and pc it becomes unreadable and asks to be erased. Also if I got to the boot selection screen on my mac the usb drive doesn't pop up as bootable. I believe I had the same problem if i formatted it under windows, also tried ubuntu. Does anybody have any ideas? I kinda need to be able to copy a kernel.bin when I get that far and work with a non corrupted filesystem to load my kernel from lol
User avatar
mark3094
Member
Member
Posts: 164
Joined: Mon Feb 14, 2011 10:32 pm
Location: Australia
Contact:

Re: Copying bootloader to FAT flash drive corrupts partition

Post by mark3094 »

dragonfire353 wrote:I then used the dd command in the terminal to write my 512 byte bootloader to the flash drive
Are you writing this to sector zero? If so, your bootloader will also be your MBR. This needs to have the partition information included, as well as the boot code, if you want the flash drive to be readable as well as bootable.

Could that be your issue?
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: Copying bootloader to FAT flash drive corrupts partition

Post by egos »

dragonfire353 wrote:I then used the dd command in the terminal to write my 512 byte bootloader to the flash drive.
You should write jump instruction (first 2-3 bytes), skip BPB+ structure, and then write remainder. For example (FAT32, FSInfo holds in sector 1, additional boot code holds in sector 2):

Code: Select all

dd bs=1 count=3 if=boot.bin of=%1 2>NUL
dd bs=1 count=422 if=boot.bin of=%1 skip=90 seek=90 2>NUL
dd count=1 if=boot.bin of=%1 skip=1 seek=2 2>NUL
Here is my installation script (which does same thing from packed image):

Code: Select all

include "../../biscript.inc"
include "fat32.inc"

move 3
pass BS_SIZE-3 ; bytes on the disk
move 512-BS_SIZE
ifeq BS.FSInfo,1
ifeq BS.FSInfo+1,0
pass 512
move 512
If you have seen bad English in my words, tell me what's wrong, please.
dragonfire353
Posts: 13
Joined: Tue Oct 01, 2013 11:45 pm

Re: Copying bootloader to FAT flash drive corrupts partition

Post by dragonfire353 »

Thank you both, but even if I just to provide a jump instruction to pass over the partition table, the BIOS only loads the first sector, 512 bytes, from the storage device, and I would need the that special boot signature at 511 and 512. Would I have to cut up my bootloader in a strange way? Is there a better way of doing this? How do other operating systems do this?

Thank you for the info!
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: Copying bootloader to FAT flash drive corrupts partition

Post by Combuster »

Universal approach:
- Read MBR to a file
- Overwrite the parts that are free to change
- Put the modified back bootsector back where it came from.

Some formatting tools, as well as things like the GRUB installer are able to take a bootsector and replace the parts needed for proper partition/filesystem detection in such a fashion already, but it's nothing a few wisely chosen DD commands can't fix for you instead.

hint: conv=notrunc
"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 ]
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: Copying bootloader to FAT flash drive corrupts partition

Post by egos »

Usually this approach is used in disk tools. But sometimes I use it "by hand". For example:

backup-dump-setup.cmd

Code: Select all

call c3 %1 backup.bin
fasm dump.asc dump.bin
call c3 dump.bin %1
dump.asc

Code: Select all

include "fat32.inc"

BACKUP equ "backup.bin"
BOOT equ "boot.bin"

file BOOT,3
file BACKUP:$,BS_SIZE-$
file BOOT:$,512-$
load FSInfo word from BS.FSInfo
if FSInfo=1
file BACKUP:$,512
file BOOT:512,512
else
file BOOT:512,512
file BACKUP:$,512
end if
If you have seen bad English in my words, tell me what's wrong, please.
linguofreak
Member
Member
Posts: 510
Joined: Wed Mar 09, 2011 3:55 am

Re: Copying bootloader to FAT flash drive corrupts partition

Post by linguofreak »

dragonfire353 wrote:Thank you both, but even if I just to provide a jump instruction to pass over the partition table, the BIOS only loads the first sector, 512 bytes, from the storage device, and I would need the that special boot signature at 511 and 512.


You need that in any case.
Would I have to cut up my bootloader in a strange way?
If your bootloader is longer than the amount of unreserved space in the MBR (447 bytes), you'll need to break it up into multiple parts.
Is there a better way of doing this? How do other operating systems do this?
Generally the boot sector parses the filesystem and finds and loads a file that contains the actual bootloader.
dragonfire353
Posts: 13
Joined: Tue Oct 01, 2013 11:45 pm

Re: Copying bootloader to FAT flash drive corrupts partition

Post by dragonfire353 »

Thank everyone who has replied so far, you all have been most helpful and responsive and I'm impressed by the community :D

One last question for any mac users, if I were to make my own filesystem, so I could have everything as "my own code", would there be some way to make mac recognize the system as bootable even though it is not a standard filesystem? I'm assuming it doesn't show devices as bootable even with the magic number at the end if it is not a filesystem it recognizes from my experience with corrupting the first sector of a fat partition and it still being recognized as bootable on my pc but not my mac
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: Copying bootloader to FAT flash drive corrupts partition

Post by dozniak »

dragonfire353 wrote:way to make mac recognize the system as bootable even though it is not a standard filesystem?
Yes, you need to write EFI module to support it.
Learn to read.
Post Reply