Skip to content

Commit 537867a

Browse files
committed
feat: MBR load and jmp to a simple loader
1 parent 8ac7eae commit 537867a

File tree

4 files changed

+85
-3
lines changed

4 files changed

+85
-3
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,18 @@ A simple OS implementation for x86_64
1414
$ cd boot
1515
```
1616
```
17-
$ nasm -o mbr.bin mbr.S
17+
$ nasm -I ../include/ -o mbr.bin mbr.S
18+
```
19+
```
20+
$ nasm -I ../include/ -o loader.bin loader.S
1821
```
1922
```
2023
$ dd if=./mbr.bin of=../img/cocotiOS.img bs=512 count=1 conv=notrunc
2124
```
25+
```
26+
$ dd if=./loader.bin of=../img/cocotiOS.img bs=512 count=1 seek=1 conv=notrunc
27+
```
28+
2229
```
2330
$ cd ../img/
2431
```
@@ -34,4 +41,5 @@ $ qemu-system-x86_64 cocotiOS.img
3441

3542
1. [mikeos: write your own os](http://mikeos.sourceforge.net/write-your-own-os.html#gofurther)
3643
2. [wiki: int 10h](https://www.wikiwand.com/en/INT_10H)
37-
44+
3. [x86 Assembly/Control Flow](https://en.wikibooks.org/wiki/X86_Assembly/Control_Flow)
45+
4. [Writing a boot loader in Assembly and C](https://www.codeproject.com/Articles/664165/Writing-a-boot-loader-in-Assembly-and-C-Part)

boot/mbr.S

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
BITS 16
22

3+
%include "boot/boot.inc"
4+
35
start:
46
mov ax, 07C0h ; Set up 4K stack space after this bootloader
57
add ax, 32 ; 512 / 16 bytes per paragraph
@@ -24,7 +26,79 @@ start:
2426
mov si, text_string ; Put string position into SI
2527
call print_string ; Call our string-printing routine
2628

27-
jmp $ ; Jump here - infinite loop!
29+
;jmp $ ; Jump here - infinite loop!
30+
31+
mov eax, LOADER_START_SECTOR
32+
mov bx, LOADER_BASE_ADDR ; The addr we want to write from loader
33+
mov cx, 1 ; The sector num we want to load later
34+
call rd_disk_m_16 ; Start to load sector
35+
36+
jmp LOADER_BASE_ADDR
37+
38+
; Read n sectors
39+
rd_disk_m_16:
40+
; eax: LBA sector num
41+
mov esi, eax ; Backup eax
42+
mov di, cx ; Backup cx
43+
44+
; R/W disk
45+
; step 1: Set the sector num which load later
46+
mov dx, 0x1f2
47+
mov al, cl
48+
out dx, al ; Sector num for loading
49+
50+
mov eax, esi ; Restore ax
51+
52+
; step 2: Write LBA addr to 0x1f3~0x1f6
53+
54+
; Write COM port 0x1f3 for setting LBA addr 7~0 bit
55+
mov dx, 0x1f3
56+
out dx, al
57+
58+
; Write COM port 0x1f4 for setting LBA addr 15~8 bit
59+
mov cl, 8
60+
shr eax, cl
61+
mov dx, 0x1f4
62+
out dx, al
63+
64+
; Write COM port 0x1f5 for setting LBA addr 23~16 bit
65+
shr eax, cl
66+
and al, 0x0f ; 24~27bit of LBA
67+
or al, 0xe0 ; Set 7~4 bit as 1110 represents LBA mode
68+
mov dx, 0x1f6
69+
out dx, al
70+
71+
; step 3: Write the read instruction, 0x20 to COM port 0x1f7
72+
mov dx, 0x1f7
73+
mov al, 0x20
74+
out dx, al
75+
76+
; step 4: Check the disk status
77+
.not_ready
78+
; Use the same COM port, write is for command, read is for disk status
79+
nop
80+
in al, dx
81+
and al, 0x88 ; Third bit represents that disk
82+
; controller is ready to transfer data
83+
; 7th bit represents that disk is busy
84+
cmp al, 0x08
85+
jnz .not_ready ; Wait if not ready
86+
87+
; step 5: Read data from 0x1f0 COM port
88+
mov ax, di
89+
mov dx, 256
90+
mul dx
91+
mov cx, ax ; di is the sector num for loading,
92+
; 512 bytes per sector and load two bytes
93+
; per transfer. Thus total di*512/2 times.
94+
95+
mov dx, 0x1f0
96+
.go_on_read
97+
in ax, dx
98+
mov [bx], ax
99+
add bx, 2
100+
loop .go_on_read
101+
ret
28102

29103

30104
text_string db 'Hello cocotiOS!', 0

boot/mbr.bin

0 Bytes
Binary file not shown.

img/cocotiOS.img

70 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)