How can I write a bootloader of my operating system to a USB stick?

时间:2015-09-14 15:19:13

标签: assembly operating-system boot bootloader

I tried to make a bootloader for my own operating system. I tried to write it to a USB stick with imageusb program (from format: img, iso and bin, nothing works). Then I tried to boot it, but I didn't find it in the BIOS boot menu. I coded it with assembly. How can I boot the operating system using my own bootloader?

Here is part of my code:

[BITS 16]
[ORG 0x7C00]

JMP Main

Main:

MOV SI, Text
CALL PrintString
CALL NextLine

MOV SI, PressKeyForBoot
CALL PrintString
CALL Boot
JMP $

PrintCharacter:
MOV AH, 0x0E
MOV BH, 0x00
MOV BL, 0x07

INT 0x10
RET

NextLine:
MOV AL, 0
stosb
mov AH, 0x0E
MOV AL, 0x0D
INT 0x10
MOV AL, 0x0A
INT 0x10
ret

Boot:
CALL RebootKey
db 0x0ea
dw 0x0000
dw 0xffff

RebootKey:
mov ah, 0
int 0x16  
cmp ah, 01h
jne RebootKey

PrintString:
next_character:
MOV AL, [SI]
INC SI
OR AL, AL
JZ exit_function
CALL PrintCharacter
JMP next_character
exit_function:
RET


Text db 'Loading...', 0
PressKeyForBoot db 'Press ESC key to reboot.', 0
TIMES 510 - ($ - $$) db 0
DW 0xAA55

1 个答案:

答案 0 :(得分:3)

To write your bootloader code to the first sector of the usb stick you can use dmde on Windows. Open dmde program and choose a correct physical device. On the next screen press f2 to show raw binary data of the sectors. You have to write the bootloader to first 512 bytes of the usb stick. The bootloader signature 0xAA55 should be the last two bytes of the first sector (i.e. 510th, 511th). To write data use ctrl+e, to save changes use ctrl+w. For more information, look at the menu bar on the top of dmde window.

After doing so, make sure, that your usb stick has a greater priority in the BIOS boot order setting than any other disks with valid loaders.

Also, @RossRidge is right about the uselessness of marking any partition as active while the block device has a bootloader in the first sector.

P.S. Here is my article (Russian only) about making your own bootloader for a USB stick.