MBR issues

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
nexos
Member
Member
Posts: 1081
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

MBR issues

Post 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?
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: MBR issues

Post 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?
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: MBR issues

Post 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
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

Re: MBR issues

Post 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
Last edited by PeterX on Thu Jun 18, 2020 12:08 pm, edited 1 time in total.
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

Re: MBR issues

Post 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
nexos
Member
Member
Posts: 1081
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

Re: MBR issues

Post 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.
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

Re: MBR issues

Post 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
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: MBR issues

Post 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
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

Re: MBR issues

Post 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
nexos
Member
Member
Posts: 1081
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

Re: MBR issues

Post by nexos »

Thank you, @bzt, that answered my question!
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg
Post Reply