Write MBR Code

前端 未结 11 2019
攒了一身酷
攒了一身酷 2021-02-01 08:20

I am an electrical engineer who has recently discovered the need to modify the code in the MBR. Basically I need the ability to execute code on the HDD before, the OS starts up

11条回答
  •  深忆病人
    2021-02-01 09:02

    I think your best way is with linux, it has nasm for compiling, dd for cluster copying (which means MBR as well), and even a boot loader menu (lilo for example) if you don't want to mess with your actual partitions.

    I had to make my own boot sequence last year. Basically, I had this:

    LILO boot menu:
        -> WindowsXP
        -> linux
    

    I wanted to do something seperately on an MBR, without affecting the actual install, so I created a new (small) partition and added that to the LILO list (omitting details here), which gave this:

    LILO boot menu:
        -> WindowsXP
        -> linux
        -> TESTMBR
    

    That way, since every partition has its own MBR as well, I could put any whacky code I wanted in there without the risk of locking myself out (which is a little annoying to fix).

    To actually change that MBR I did this:

    1. Backup actual MBR, eg dd if=/dev/sda3 of=/home/you/mbr−backup count=1
    2. Edit code in a file: boot.asm
    3. Compile with nasm: nasm boot.asm -o boot.bin -f bin correcting if errors
    4. Copy the freshly created MBR to drive: dd if=boot.bin of=/dev/sda3
    5. Reboot.
    6. Choose TESTMBR in the menu.
    7. See how it goes.

    Sure, you can do that directly on the drive's MBR instead of the partition's MBR like I did here, but for my own case it was more practical.

    Concerning the actual code-jumping-out-of-MBR, you'll need to use the INT 13,42 interruption, which loads any cluster on a disk. For the purpose of my test I just had to display its contents, but I can take a closer look if you want.

    Hope that could help, sorry for the long reply.

提交回复
热议问题