Nathan Johnson
Nathan Johnson

Reputation: 55

How Do I Put My Bootloader And Kernel On A USB

I've written a bootloader and Basic kernel as a fun side project while i'm learning 2 stage bootloaders, I want to load my bootloader at sector 1 (or the MBR) of the USB and the Kernel at sector 2. I've compiled both into Bootloader.bin & Kernel.bin using NASM. I just need a little help on actually writing them onto the USB. I have access to both Windows and Linux so any answers a appreciated.

Bootloader.asm

[BITS 16]
[ORG 0x7C00]

ResetDisk:
XOR AH, AH
INT 0x13
JC ResetDisk

ReadDisk:
MOV BX, 0x8000
MOV ES, BX
MOV BX, 0x0000

MOV AH, 0x02
MOV AL, 1
MOV CH, 0x00
MOV CL, 0x02
MOV DH, 0x00
INT 0x13
JC ResetDisk
JMP 0x8000:0x0000

TIMES 510-($-$$) DB 0
DW 0xAA55

Kernel.asm

[BITS 16]
[ORG 0x8000]

MOV SI, HelloString
CALL PrintString
JMP $

PrintChar:
MOV AH, 0x0E
MOV BH, 0x00
MOV BL, 0x0F
RET

PrintString:
MOV AL, [SI]
INC SI
OR AL, AL
JZ Exit
CALL PrintChar
JMP PrintString
Exit:
RET

HelloString DB 'Hello World!',0

TIMES 512-($-$$) DB 0

Upvotes: 0

Views: 442

Answers (1)

user3160514
user3160514

Reputation:

Figure out which device represents the USB key in /dev (the key itself, NOT a partition on it), then you can simply use dd or a similar tool to copy your data.

Example, where /dev/xxx is your USB key:

cat Bootloader.bin Kernel.bin > image.bin
sudo dd if=image.bin of=/dev/xxx bs=4k

Note that you may have to umount any mounted partition that comes from the USB key first. Also note that, but this goes without saying, make sure there is nothing important on the USB key first.

Upvotes: 2

Related Questions