Skip to content

Commit 9bce18f

Browse files
committed
os
1 parent 0e88ea4 commit 9bce18f

File tree

8 files changed

+189
-0
lines changed

8 files changed

+189
-0
lines changed

os/part1/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.PHONY: clean, .force-rebuild
2+
all: bootloader.bin
3+
4+
bootloader.bin: os.asm .force-rebuild
5+
nasm -fbin os.asm -o os.bin
6+
7+
clean:
8+
rm *.bin

os/part1/kernel/kernel.asm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
; Dummy file
2+
3+
times 512 db 0

os/part1/os.asm

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
stage1_start:
2+
times 90 db 0 ; BPB (BIOS Parameter Block) will go here
3+
%include "stage1/bootstage1.asm"
4+
stage1_end:
5+
6+
stage2_start:
7+
%include "stage2/bootstage2.asm"
8+
align 512, db 0
9+
stage2_end:
10+
11+
kernel_start:
12+
%include "kernel/kernel.asm"
13+
align 512, db 0
14+
kernel_end:

os/part1/os.bin

1.5 KB
Binary file not shown.

os/part1/stage1/bootstage1.asm

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
BITS 16 ; On the x86, the BIOS (and consequently the bootloader) runs in 16-bit Real Mode.
2+
ORG 0x7C00 ; We are loaded/booted by BIOS into this memory address.
3+
4+
Stage1_entrypoint: ; Main entry point where BIOS leaves us. Some BIOS may load us at 0x0000:0x7C00 while others at 0x07C0:0x0000.
5+
jmp 0x0000:.setup_segments ; We do a far jump to accommodate for this issue (CS is reloaded to 0x0000).
6+
.setup_segments: ; Next, we set all segment registers to zero.
7+
xor ax, ax
8+
mov ss, ax
9+
mov ds, ax
10+
mov es, ax
11+
mov fs, ax
12+
mov gs, ax
13+
mov sp, Stage1_entrypoint ; We set up a temporary stack so that it starts growing below Stage1_entrypoint (i.e. the stack base will be located at 0:0x7c00).
14+
cld ; Clear the direction flag (i.e. go forward in memory when using instructions like lodsb).
15+
16+
; Loading stage 2 from disk into RAM
17+
mov [disk], dl ; Storing disk number. BIOS loads into dl the "drive number" of the booted device.
18+
mov ax, (stage2_start-stage1_start)/512 ; ax: start sector
19+
mov cx, (kernel_end-stage2_start)/512 ; cx: number of sectors (512 bytes) to read
20+
mov bx, stage2_start ; bx: offset of buffer
21+
xor dx, dx ; dx: segment of buffer
22+
call Real_mode_read_disk
23+
24+
mov si, stage1_message
25+
call Real_mode_println
26+
27+
; Jump to the entry point of stage 2 (commented out for now)
28+
;;; jmp Stage2_entrypoint
29+
.halt: hlt
30+
jmp .halt ; Infinite loop (it prevents us from going off and executing other junk in memory).
31+
32+
33+
; Include
34+
%include "stage1/disk.asm"
35+
%include "stage1/print.asm"
36+
37+
38+
times 510-($-$$) db 0 ; Padding
39+
dw 0xAA55 ; The last two bytes of the boot sector should have the 0xAA55 signature.
40+
; Otherwise, we'll get an error message from BIOS that it didn't find a bootable disk.
41+
; This signature is represented in binary as 1010101001010101. The alternating bit
42+
; pattern was thought to be a protection against certain (drive or controller) failures.

os/part1/stage1/disk.asm

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
BITS 16
2+
3+
;---Initialized data------------------------------------------------------------
4+
5+
disk db 0x80
6+
7+
disk_error_message dw 11
8+
db 'Disk error!'
9+
10+
11+
DAP:
12+
;*******************************************************************************;
13+
; Disk Address Packet ;
14+
;-------------------------------------------------------------------------------;
15+
; Offset Size Description ;
16+
; 0 1 size of packet (16 bytes) ;
17+
; 1 1 always 0 ;
18+
; 2 2 number of sectors to load (max = 127 on some BIOS) ;
19+
; 4 2 16-bit offset of target buffer ;
20+
; 4 2 16-bit segment of target buffer ;
21+
; 8 4 lower 32 bits of 48-bit starting LBA ;
22+
; 12 4 upper 32 bits of 48-bit starting LBA ;
23+
;*******************************************************************************;
24+
db 0x10 ; size of packet = 16 bytes
25+
db 0 ; always 0
26+
.num_sectors: dw 127 ; number of sectors to load (max = 127 on some BIOS)
27+
.buf_offset: dw 0x0 ; 16-bit offset of target buffer
28+
.buf_segment: dw 0x0 ; 16-bit segment of target buffer
29+
.LBA_lower: dd 0x0 ; lower 32 bits of 48-bit starting LBA
30+
.LBA_upper: dd 0x0 ; upper 32 bits of 48-bit starting LBA
31+
32+
33+
34+
;---Code------------------------------------------------------------------------
35+
36+
Real_mode_read_disk:
37+
;**********************************************************;
38+
; Load disk sectors to memory (int 13h, function code 42h) ;
39+
;----------------------------------------------------------;
40+
; ax: start sector ;
41+
; cx: number of sectors (512 bytes) to read ;
42+
; bx: offset of buffer ;
43+
; dx: segment of buffer ;
44+
;**********************************************************;
45+
.start:
46+
cmp cx, 127 ; (max sectors to read in one call = 127)
47+
jbe .good_size
48+
pusha
49+
mov cx, 127
50+
call Real_mode_read_disk
51+
popa
52+
add eax, 127
53+
add dx, 127 * 512 / 16
54+
sub cx, 127
55+
jmp .start
56+
57+
.good_size:
58+
mov [DAP.LBA_lower], ax
59+
mov [DAP.num_sectors], cx
60+
mov [DAP.buf_segment], dx
61+
mov [DAP.buf_offset], bx
62+
mov dl, [disk]
63+
mov si, DAP
64+
mov ah, 0x42
65+
int 0x13
66+
jc .print_error
67+
ret
68+
.print_error:
69+
mov si, disk_error_message
70+
call Real_mode_println
71+
.halt: hlt
72+
jmp .halt; Infinite loop. We cannot recover from disk error.

os/part1/stage1/print.asm

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
BITS 16
2+
3+
;---Initialized data------------------------------------------------------------
4+
5+
newline dw 2
6+
db 13,10 ; \r\n
7+
8+
stage1_message dw 17
9+
db 'Stage 1 finished.'
10+
11+
;---Code------------------------------------------------------------------------
12+
13+
Real_mode_print:
14+
;*********************************************************************************;
15+
; Prints a string (in real mode) ;
16+
;---------------------------------------------------------------------------------;
17+
; si: pointer to string (first 16 bits = the number of characters in the string.) ;
18+
;*********************************************************************************;
19+
push ax
20+
push cx
21+
push si
22+
mov cx, word [si] ; first 16 bits = the number of characters in the string
23+
add si, 2
24+
.string_loop: ; print all the characters in the string
25+
lodsb
26+
mov ah, 0eh
27+
int 10h
28+
loop .string_loop, cx
29+
pop si
30+
pop cx
31+
pop ax
32+
ret
33+
34+
35+
Real_mode_println:
36+
;***********************************************************;
37+
; Prints a string (in real mode) and a newline (\r\n) ;
38+
;-----------------------------------------------------------;
39+
; si: pointer to string ;
40+
; (first 16 bits = the number of characters in the string.) ;
41+
;***********************************************************;
42+
push si
43+
call Real_mode_print
44+
mov si, newline
45+
call Real_mode_print
46+
pop si
47+
ret

os/part1/stage2/bootstage2.asm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
; Dummy file
2+
3+
times 512 db 0

0 commit comments

Comments
 (0)