Browse Source

add boot2, not works yet

master
abbycin 1 year ago
parent
commit
10b1d728a1
6 changed files with 1525 additions and 0 deletions
  1. 0
    0
      boot/Makefile
  2. 0
    0
      boot/bochsrc
  3. 0
    0
      boot/boot.asm
  4. 11
    0
      boot2/Makefile
  5. 1263
    0
      boot2/bochsrc
  6. 251
    0
      boot2/boot2.asm

booter/Makefile → boot/Makefile View File


booter/bochsrc → boot/bochsrc View File


booter/boot.asm → boot/boot.asm View File


+ 11
- 0
boot2/Makefile View File

@@ -0,0 +1,11 @@
all: boot2.bin
dd if=boot2.bin of=boot.img bs=512 count=1 conv=notrunc

boot2.bin: boot2.asm
nasm $^ -o $@

run:
bochs -f bochsrc

clean:
rm -f *.img *.bin *.txt

+ 1263
- 0
boot2/bochsrc
File diff suppressed because it is too large
View File


+ 251
- 0
boot2/boot2.asm View File

@@ -0,0 +1,251 @@
org 0x7c00

BaseOfStack equ 0x7c00
BaseOfLoader equ 0x10000
OffsetOfLoader equ 0x0

RootDirSectors equ 14
SectorNumOfRootDirStart equ 19
SectionNumOfFAT1Start equ 1
SectorBalance equ 17

jmp short Label_Start
nop

; fat12 related
BS_OEMName db "serenity" ; at most 8 bytes
BPB_BytesPerSec dw 512
BPB_SecPerClus db 1
BPB_RsvdSecCnt dw 1
BPB_NumFATs db 2
BPB_RootEntCnt dw 224
BPB_TotSec16 dw 2280
BPB_Media db 0xf0
BPB_FATSz dw 9
BPB_SecPerTrk dw 18
BPB_NumHeads dw 2
BPB_HiddSec dd 0
BPB_TotSec32 dd 0
BS_DrvNum db 0
BS_Reserved1 db 0
BS_BootSig db 0x29
BS_VolID db 0
BS_VolLab db "order2chaos" ; at most 11 bytes
BS_FileSysType db "FAT12"

; we can reuse code in boot.asm, but we write new one
Label_Start:
; initialize segment registers
mov ax, cs
mov ds, ax
mov es, ax
mov ss, ax
mov sp, BaseOfStack

; clear screen, BIOS commands
mov ax, 0x600
mov bx, 0x700
mov cx, 0
mov dx, 0x184f
int 0x10

; set focus
mov ax, 0x200
mov bx, 0
mov dx, 0
int 0x10

; display message
mov ax, 0x130
mov bx, 0xf
mov dx, 0
mov cx, 0xa
push ax
mov ax, ds
mov es, ax
pop ax
mov bp, Msg_StartBoot
int 0x10

; reset floppy
xor ah, ah
xor dl, dl
int 0x13

; search loader.bin in FAT12 filesystem
mov word [SectorNo], SectorNumOfRootDirStart

Label_Search_In_Root_Dir_Begin:
cmp word [RootDirSizeforLoop], 0
je Label_No_Loader
dec word [RootDirSizeforLoop]
mov ax, 0x0
mov es, ax
mov bx, 0x8000
mov ax, [SectorNo]
mov cl, 1
call Func_ReadOneSector
mov si, LoaderFileName
mov di, 0x8000
cld
mov dx, 0x10

Label_Search_For_Loader:
cmp dx, 0
jz Label_Goto_Next_Sector_In_Root_Dir
dec dx
mov cx, 11

Label_Cmp_FileName:
cmp cx, 0
jz Label_FileName_Found
dec cx
lodsb
cmp al, byte [es:di]
jz Label_Go_On
jmp Label_Different

Label_Go_On:
inc di
jmp Label_Cmp_FileName

Label_Different:
add di, 0xffe0
add di, 0x20
mov si, LoaderFileName
jmp Label_Search_For_Loader

Label_Goto_Next_Sector_In_Root_Dir:
inc word [SectorNo]
jmp Label_Search_In_Root_Dir_Begin

Label_No_Loader:
mov ax, 0x1301
mov bx, 0x8c
mov dx, 0x100
mov cx, 21
push ax
mov ax, ds
mov es, ax
pop ax
mov bp, Msg_Loader_NotFound
int 0x10
jmp $

Label_FileName_Found:
mov ax, RootDirSectors
add di, 0xffe0
add di, 0x1a
mov cx, word [es:di]
push cx
add cx, ax
add cx, SectorBalance
mov ax, BaseOfLoader
mov es, ax
mov bx, OffsetOfLoader
mov ax, cx

Label_Go_On_Loading_File:
push ax
push bx
mov ah, 0xe
mov al, '.'
mov bl, 0xf
int 0x10
pop bx
pop ax

mov cl, 1
call Func_ReadOneSector
pop ax
call Func_GetFATEntry
cmp ax, 0xfff
jz Label_File_Loaded
push ax
mov dx, RootDirSectors
add ax, dx
add ax, SectorBalance
add bx, [BPB_BytesPerSec]
jmp Label_Go_On_Loading_File

Label_File_Loaded:
jmp $

Func_ReadOneSector:
push bp
mov bp, sp
sub esp, 2
mov byte [bp - 2], cl
push bx
mov bl, [BPB_SecPerTrk]
div bl
inc ah
mov cl, ah
mov dh, al
shr al, 1
mov ch, al
add dh, 1
pop bx
mov dl, [BS_DrvNum]

Label_Go_On_Reading:
mov ah, 2
mov al, byte [bp - 2]
int 0x13
jc Label_Go_On_Reading
add esp, 2
pop bp
ret

Func_GetFATEntry:
push es
push bx
push ax
mov ax, 0x0
mov es, ax
pop ax
mov byte [fff], 0
mov bx, 3
mul bx
mov bx, 2
div bx
cmp dx, 0
jz Label_Even
mov byte [fff], 1

Label_Even:
xor dx, dx
mov bx, [BPB_BytesPerSec]
div bx
push dx
mov bx, 0x8
add ax, SectorNumOfRootDirStart
mov cl, 2
call Func_ReadOneSector

pop dx
add bx, dx
mov ax, [es:bx]
cmp byte [fff], 1
jnz Label_Even_2
shr ax, 4

Label_Even_2:
add ax, 0xfff
pop bx
pop es
ret

; variables
fff db 0
SectorNo dw 0
RootDirSizeforLoop dw RootDirSectors

; messages
Msg_StartBoot db "start booting..."
LoaderFileName db "loader.bin",0
Msg_Loader_NotFound db "loader is not found"

; fill with 0
times 510 - ($ - $$) db 0
dw 0xaa55

Loading…
Cancel
Save