From ef16a95229e9490cc550c60ef33f77745de41810 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 19 Apr 2019 17:43:08 +0200 Subject: [PATCH] first and second stage basic bootloader --- Makefile | 27 +++++++++++++++++++ README.md | 13 +++++++++ boot.asm | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++ boot.bin | Bin 0 -> 512 bytes disk.img | Bin 0 -> 737280 bytes program.asm | 28 +++++++++++++++++++ program.bin | Bin 0 -> 48 bytes 7 files changed, 144 insertions(+) create mode 100644 Makefile create mode 100644 README.md create mode 100644 boot.asm create mode 100644 boot.bin create mode 100755 disk.img create mode 100644 program.asm create mode 100644 program.bin diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..267777a --- /dev/null +++ b/Makefile @@ -0,0 +1,27 @@ +all : image + +compile : + nasm -f bin -o boot.bin boot.asm + nasm -f bin -o program.bin program.asm + + +image : compile + sudo dd if=/dev/zero of=disk.img bs=1024 count=720 + sudo dd if=boot.bin of=disk.img bs=512 count=1 conv=notrunc + sudo dd if=program.bin of=disk.img bs=512 seek=1 count=1 conv=notrunc + + +flashusb : image + #sudo umount /dev/sdb + sudo dd if=boot.bin of=/dev/sdb bs=512 count=1 + sudo dd if=program.bin of=/dev/sdb bs=512 seek=1 count=1 + +run : image + qemu-system-x86_64 disk.img + +clean : + rm *.img + rm *.bin + + + diff --git a/README.md b/README.md new file mode 100644 index 0000000..1f386a2 --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +#Useful resources + + + +##interrupts +http://stanislavs.org/helppc/idx_interrupt.html + +##BIOS colors +https://en.wikipedia.org/wiki/BIOS_color_attributes + +##Second stage bootloader booting from usb +https://stackoverflow.com/questions/36044706/enable-the-boot-loader-to-load-the-second-sector-of-a-usb + diff --git a/boot.asm b/boot.asm new file mode 100644 index 0000000..53932f5 --- /dev/null +++ b/boot.asm @@ -0,0 +1,76 @@ +[bits 16] +[org 0x7c00] + + ; Use the boot drive number passed to us by BIOS in register DL +start: + xor ax,ax ; We want a segment of 0 for DS for this question + mov ds,ax ; Set AX to appropriate segment value for your situation + mov es,ax ; In this case we'll default to ES=DS + mov bx,0x8000 ; Stack segment can be any usable memory + + mov ss,bx ; This places it with the top of the stack @ 0x80000. + mov sp,ax ; Set SP=0 so the bottom of stack will be @ 0x8FFFF + + cld ; Set the direction flag to be positive direction + + mov si, wolf_wel_msg + call wolf_print + + mov si, wolf_kernel_load + call wolf_print + + pushf + stc + + mov ah,00 + int 13h + + read_sector: + mov ax, 0x0 + mov es, ax ; ES = 0 + mov bx, 0x1000 ; BX = 0x1000. ES:BX=0x0:0x1000 + ; ES:BX = starting address to read sector(s) into + mov ah, 02 ; Int 13h/AH=2 = Read Sectors From Drive + mov al, 01 ; Sectors to read = 1 + mov ch, 00 ; CH=Cylinder. Second sector of disk + ; is at Cylinder 0 not 1 + mov cl, 02 ; Sector to read = 2 + mov dh, 00 ; Head to read = 0 + ; DL hasn't been destroyed by our bootloader code and still + ; contains boot drive # passed to our bootloader by the BIOS + int 13h + + jc wolf_error + popf + jmp 0x0:0x1000 + cli + hlt + + wolf_error: + mov si, wolf_error_msg + call wolf_print + mov si, wolf_error_msg1 + call wolf_print + mov ah,00 + int 16h + xor ax,ax + int 19h + + wolf_print: + lodsb + or al,al + jz exit + mov ah,0x0e + int 10h + jmp wolf_print + exit: + ret + +; Moved the data before the boot signature but after the code +wolf_wel_msg db 'Welcome to this publication...',0x0D,0x0A,0 +wolf_kernel_load db 'Loading program',0x0D,0x0A,0 +wolf_error_msg db 'Program not found!',0x0D,0x0A,0 +wolf_error_msg1 db 'Press any key to restart..',0 + +times 510-($-$$) db 0 +dw 0xAA55 diff --git a/boot.bin b/boot.bin new file mode 100644 index 0000000000000000000000000000000000000000..25cacf7c2e6be6b844e3868b3936b346b8350392 GIT binary patch literal 512 zcmXp!(08Nnz;1?yzRR6Q{_KmZd11$}ufFDm1;d=5TNus??_gj6NegUY+Q7J#VI$Kv zpjZ*d+*b?&3=F@%?3-Bgf{$U}vYHnh3_#Ulh6m0{uHiUP!nTF)tibE9hr?5IlJj#@ z6-x3IN-{Ew6$(m|ax#+>OEUBG^z`(2xfp!%6H_wt(iIAd^3#hFbAciO5SBt-eu+X_ peraBcB3LlBxL6@EuTmj9wGw1nQEG8XVo`~n9>XZ0AOu2J0RV%CO}qdA literal 0 HcmV?d00001 diff --git a/disk.img b/disk.img new file mode 100755 index 0000000000000000000000000000000000000000..5fd68f7845efd9f937a781416970d6cfb8b691d5 GIT binary patch literal 737280 zcmeIvF>4e-6bJC}if{-C3jxtaCSPF5bygPEf@y+Q!kVnfVX_c99Jk`R7=Yo3j}rO><_cl|JbePz(juD$NJ z^}=*%mYjb7bT{AGD0lN%m$BX+j^f__gPq|-ezTFE&@s!+bP~S#b~_Epe%^;==Ou-Xp3 ze7JM<{q G>-EFZUKF1I literal 0 HcmV?d00001