summaryrefslogtreecommitdiff
path: root/boot2/disk_load_16.asm
blob: f326bec1bae8f8344d1618331a785e6e13d69818 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;; Miguel's FoolOS Helper Functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;disk_load_16
;

[bits 16]

STR_ERROR:
    db "Disk Read Error",0

STR_SPACE:
    db " ",0

STR_OK:
    db "Kernel Loaded",0
STR_PROGRESS:
    db ".",0

bpbSectorsPerTrack:
    dw 18

bpbHeadsPerCylinder:
    dw 2

LBA:
    dw 10 ; current lba

disk_load_16:

   ; check if LBA is supported
    mov    ah,0x41
    mov    bx,0x55aa
    int    0x13                                                    
    jnc disk_load_lba
    jmp disk_load_chs


;#######################

disk_load_lba:

    pusha

    mov bx,0x1800	;target es:bx
    mov es,bx
    mov bx,0

    next_sectors_lba:

    jmp skip_debug_lba
    ;show es - target
    pusha
    mov bx,es

    ;mov al,bh
    ;call print_hex_byte

    ;mov al,bl
    ;call print_hex_byte

    mov bx,[LBA]

    mov al,bh
    call print_hex_byte

    mov al,bl
    call print_hex_byte

    popa
    ;--

    skip_debug_lba:

    mov [lba_addr_sector],es
    mov ax,[LBA]
    mov [lba_first_sector],ax

    mov dl,[BOOT_DRIVE]

    mov ah,0x42
    mov bx,0
    mov ds,bx
    lea si,[lba_adr]        
    int 0x13  
    jc disk_read_error

    mov bx,es
    add bx,0x0800

    mov ax,[LBA]
    add ax,64 ; 64 sectors = 0x2000*4 bytes 
    mov [LBA],ax

    cmp bx,0x9000
    je disk_load_finish

    mov es,bx ;next target es:bx
    mov bx,0

    jmp next_sectors_lba

;#######################


disk_load_chs:

    pusha

    ; get boot drive geometry
    mov ah,8
    mov dl,[BOOT_DRIVE]
    int 0x13

    add dh,1
    and cl,0x3f

    mov [bpbSectorsPerTrack],cl
    mov [bpbHeadsPerCylinder],dh
    ;

    ;show geometry
    mov al,dh
    call print_hex_byte

    mov bx, STR_SPACE
    call print_string

    mov al,cl
    call print_hex_byte

    call print_nextline
    popa 
    ;

    pusha


    mov bx,0x1800	;target es:bx
    mov es,bx
    mov bx,0

    next_sectors_chs:

    ;jmp skip_debug
    pusha

    mov bx, STR_SPACE
    call print_string

    ;show es - target
    mov bx,es

    mov al,bh
    call print_hex_byte

    mov al,bl
    call print_hex_byte

    mov bx, STR_SPACE
    call print_string

    ;show next LBA numer
    mov ax,[LBA]
    mov al,ah
    call  print_hex_byte

    mov ax,[LBA]
    call  print_hex_byte

    mov bx, STR_SPACE
    call print_string

    popa

    mov ax,[LBA]

    ;calculate chs
    call lba_to_chs

    ;show chs sector/ head / cylinder
    mov al,cl
    call  print_hex_byte

    mov al,dh
    call  print_hex_byte

    mov al,ch
    call  print_hex_byte

    call print_nextline

    skip_debug:


    mov ax,[LBA]

    ;calculate chs
    call lba_to_chs

    ; and now READ it!
    mov al,1		;number of sectors to read
    mov dl,[BOOT_DRIVE]

    mov ah,0x02		;BIOS read sector func
    int 0x13		;bios interrupt

    jc disk_read_error

    mov bx,es
    add bx,0x0020
    cmp bx,0x9000
    je disk_load_finish

    mov es,bx
    mov bx,0

    mov ax,[LBA]
    add ax,1
    mov [LBA],ax

    jmp next_sectors_chs

disk_read_error:
    mov bx, STR_ERROR
    call print_string
    call print_nextline
    jmp $

disk_load_finish:
    
    mov bx, STR_PROGRESS
    call print_string
;    call print_nextline

    popa
    ret



;lba to chs translation: 
; input :   ax - lba
; output:   cl - sector
;	    dh - head
;	    ch - cylinder / track

lba_to_chs:

          xor     dx, dx                              ; prepare dx:ax for operation
          div     WORD [bpbSectorsPerTrack]           ; divide by sectors per track
          inc     dl                                  ; add 1 (obsolute sector formula)
          mov     cl, dl
 
          xor     dx, dx                              ; prepare dx:ax for operation
          div     WORD [bpbHeadsPerCylinder]          ; mod by number of heads (Absolue head formula)
          mov     dh,dl			             ; everything else was already done from the first formula
 
          mov     ch, al		            ; not much else to do :)
          ret

;; here we hold the lba addr
lba_adr:

    dw 0x10	    ; size of packet ( 16 byte)
    dw 64	    ; number of sectors to read

    ; target is 0x10000

    dw 0x0000	    ; target addr. offset

lba_addr_sector:
    dw 0x1800	    ; target addr. sector

lba_first_sector:
    dw 10	    ; first sector to read
    dw 0

    dd 0 

    ;