Copying bootloader to FAT flash drive corrupts partition
-
- Posts: 13
- Joined: Tue Oct 01, 2013 11:45 pm
Copying bootloader to FAT flash drive corrupts partition
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
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.dragonfire353 wrote:I then used the dd command in the terminal to write my 512 byte bootloader to the flash drive
Could that be your issue?
Re: Copying bootloader to FAT flash drive corrupts partition
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):dragonfire353 wrote:I then used the dd command in the terminal to write my 512 byte bootloader to the flash drive.
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
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.
-
- Posts: 13
- Joined: Tue Oct 01, 2013 11:45 pm
Re: Copying bootloader to FAT flash drive corrupts partition
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!
Thank you for the info!
- Combuster
- 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
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
- 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
Usually this approach is used in disk tools. But sometimes I use it "by hand". For example:
backup-dump-setup.cmd
dump.asc
backup-dump-setup.cmd
Code: Select all
call c3 %1 backup.bin
fasm dump.asc dump.bin
call c3 dump.bin %1
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.
-
- Member
- Posts: 510
- Joined: Wed Mar 09, 2011 3:55 am
Re: Copying bootloader to FAT flash drive corrupts partition
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.
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.Would I have to cut up my bootloader in a strange way?
Generally the boot sector parses the filesystem and finds and loads a file that contains the actual bootloader.Is there a better way of doing this? How do other operating systems do this?
-
- Posts: 13
- Joined: Tue Oct 01, 2013 11:45 pm
Re: Copying bootloader to FAT flash drive corrupts partition
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
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
Yes, you need to write EFI module to support it.dragonfire353 wrote:way to make mac recognize the system as bootable even though it is not a standard filesystem?
Learn to read.