Page 1 of 1
Copying bootloader to FAT flash drive corrupts partition
Posted: Tue Oct 01, 2013 11:52 pm
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
Re: Copying bootloader to FAT flash drive corrupts partition
Posted: Wed Oct 02, 2013 1:02 am
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?
Re: Copying bootloader to FAT flash drive corrupts partition
Posted: Wed Oct 02, 2013 2:12 am
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
Re: Copying bootloader to FAT flash drive corrupts partition
Posted: Wed Oct 02, 2013 9:44 am
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!
Re: Copying bootloader to FAT flash drive corrupts partition
Posted: Wed Oct 02, 2013 10:41 am
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
Re: Copying bootloader to FAT flash drive corrupts partition
Posted: Wed Oct 02, 2013 12:52 pm
by egos
Usually this approach is used in disk tools. But sometimes I use it "by hand". For example:
backup-dump-setup.cmdCode: Select all
call c3 %1 backup.bin
fasm dump.asc dump.bin
call c3 dump.bin %1
dump.ascCode: 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
Re: Copying bootloader to FAT flash drive corrupts partition
Posted: Sat Oct 05, 2013 1:20 am
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.
Re: Copying bootloader to FAT flash drive corrupts partition
Posted: Sat Oct 05, 2013 11:44 pm
by dragonfire353
Thank everyone who has replied so far, you all have been most helpful and responsive and I'm impressed by the community
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
Re: Copying bootloader to FAT flash drive corrupts partition
Posted: Sun Oct 06, 2013 1:31 am
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.