1
1
BITS 16
2
2
3
+ %include "boot/boot.inc"
4
+
3
5
start:
4
6
mov ax , 07C0h ; Set up 4K stack space after this bootloader
5
7
add ax , 32 ; 512 / 16 bytes per paragraph
@@ -24,7 +26,79 @@ start:
24
26
mov si , text_string ; Put string position into SI
25
27
call print_string ; Call our string-printing routine
26
28
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
28
102
29
103
30
104
text_string db 'Hello cocotiOS!' , 0
0 commit comments