Write MBR Code

前端 未结 11 2140
攒了一身酷
攒了一身酷 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:20

    There are various ways of writing to the boot sector of a drive, and there is a general reference I used back when I was experimenting with homebrew OS development: http://wiki.osdev.org/

    I personally just boot under linux and use dd:

    1. Backup first

      dd if=/dev/sda of=~/windows_bootloader.bin bs=512 count=1

    2. Disassemble the bootloader

      ndisasm -b16 -o7C00h ~/windows_bootloader.bin > ~/windows_bootloader.asm

    3. Make your modifications and reassemble

      nasm ~/windows_bootloader.asm -f bin ~/modified_bootloader.bin

    4. Overwrite the bootloader

      dd if=~/modified_bootloader.bin of=/dev/sda bs=512 count=1

    This assumes your that 'sda' is the correct block device. And note that the step 4 doesn't just copy the file to /dev/sda (which it could, but then you might overwrite more than just the first sector if the output binary > 512 Bytes )

    Obviously you're not going to want to debug this approach on a live system. It will save you a lot of headaches to use some kind of x86 emulator like bochs, qemu or VMWare Server.

    However as Michael Burr has stated, this will probably be a bad idea. Modifying the Windows bootloader, will probably leave you with little or no room for your own code.

提交回复
热议问题