Page 1 of 1
MBR issues
Posted: Thu Jun 18, 2020 8:58 am
by nexos
Hello,
I am working on developing a bootloader that works with multiple partitions of a hard disk. It use BootProg. I was wondering how I go about writing out a bootloader to a partition. Also, does fdisk or parted write a bootstrap program for me?
Re: MBR issues
Posted: Thu Jun 18, 2020 9:23 am
by iansjack
nexos wrote:I was wondering how I go about writing out a bootloader to a partition.
You can write sectors to the disk with dd.
does fdisk or parted write a bootstrap program for me?
I'm not sure what you mean, but I suspect the answer is no. I thought you said you were using BootProg to boot the computer.
Are you quite sure that you understand enough about hard disks, partitions, etc. to reinvent the wheel?
Re: MBR issues
Posted: Thu Jun 18, 2020 11:22 am
by bzt
Hi,
I don't get it, are you using BootProg or are you developing your own? Not really important, just asking. By the way, the
Standard MBR, embedded in "FDISK.EXE /MBR", is capable of handling multiple partitions. I feel you are sieging an open gate.
Don't use "dd" on MBR. Although it will install the boot code fine, it will also destroy all your data (which includes the partitioning table).
Here is a small (ca. 100 SLoC in C) tool that I wrote for my boot loader. It reads the MBR (or VBR) from the disk at line 75. Then it copies the code from _binary____boot_bin_start and the data from the original boot sector onto a new buffer at line 82. (Then it will locate stage2 and save it's starting sector too, irrelevant to you). Finally it saves the new MBR buffer at line 103.
Cheers,
bzt
Re: MBR issues
Posted: Thu Jun 18, 2020 11:49 am
by PeterX
nexos wrote:Hello,
I am working on developing a bootloader that works with multiple partitions of a hard disk. It use BootProg. I was wondering how I go about writing out a bootloader to a partition. Also, does fdisk or parted write a bootstrap program for me?
Are you developing on Linux (there you have dd) or Windows?
Partitioning software doesn't write bootcode to the disk. EDIT: Ok, you got me on that, fdisk can be explicitely told to do so.
Greetings
Peter
Re: MBR issues
Posted: Thu Jun 18, 2020 11:57 am
by PeterX
bzt wrote:Don't use "dd" on MBR. Although it will install the boot code fine, it will also destroy all your data (which includes the partitioning table).
Not neccesarily! If you write only the first 446 bytes and the last two bytes, it's ok.
Code: Select all
dd if=mbr.bin of=/dev/sda bs=1 count=446
dd if=mbr.bin of=/dev/sda bs=1 seek=510 skip=510
Greetings
Peter
Re: MBR issues
Posted: Thu Jun 18, 2020 12:21 pm
by nexos
I am using BootProg, but I am talking about the code that loads the active partition. Does fdisk write that out? And I am using Linux.
Re: MBR issues
Posted: Thu Jun 18, 2020 5:14 pm
by PeterX
Additional comment: When writing to a VBR (partition boot sector) you must use a different dd-command:
Code: Select all
dd if=vbr.bin of=/dev/sda1 bs=1 count=3
dd if=vbr.bin of=/dev/sda1 bs=1 seek=? skip=?
Replace the "?" with the offset directly after the BPB (BIOS Parameter Block), assuming you want to use the FAT file system.
It is still unclear if you want to write your own loader or use Alexej's BootProg (or both, one in MBR other in VBR).
The generic MBR (see above for a link) chainloads a partition bootsector (VBR) and if that is BootProg, it loads your DOS-program file.
Greetings
Peter
Re: MBR issues
Posted: Thu Jun 18, 2020 7:24 pm
by bzt
nexos wrote:I am using BootProg, but I am talking about the code that loads the active partition.
I see. That's the original MBR boot code from IBM, but also any Microsoft boot code (from MS-DOS 1.0 to Win 98) will do. In short, they look for the boot flag in the partition records (0x80), and they chainload the VBR for the partition which has it. THe page I've linked has a VERY detailed description on the asm code how it's done.
nexos wrote:Does fdisk write that out? And I am using Linux.
I'm afraid not, only fdisk under Win supports that. On Linux, you'll need the
ms-sys package. If it's not provided by your distro's repo, just get it from sourceforge, it is trivial to compile.
@PeterX: yes, but it's easy to mess up. Your dd commands won't work either, btw, because you've forgot the "conv=notrunc" option
Cheers,
bzt
Re: MBR issues
Posted: Fri Jun 19, 2020 2:41 am
by PeterX
bzt wrote:nexos wrote:Your dd commands won't work either, btw, because you've forgot the "conv=notrunc" option
On my PC it works. I never used "notrunc" simply because I was not aware of it. I will check that option in the man page or on the web.
EDIT:Here they say:
https://stackoverflow.com/questions/205 ... sk-with-dd
notrunc is only important to prevent truncation when writing into a file. This has no effect on a block device such as sda or sdb.
Greetings
Peter
Re: MBR issues
Posted: Fri Jun 19, 2020 5:19 am
by nexos
Thank you, @bzt, that answered my question!