-
Notifications
You must be signed in to change notification settings - Fork 0
/
init-x.kaba
310 lines (241 loc) · 5.84 KB
/
init-x.kaba
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#----------------------------------------------------------------------------*\
#| Eusebius - Init |
#| -> A20-Gate aktivieren |
#| -> GDT laden |
#| -> Protected Mode starten (32 bit) |
#| -> Kernel-Image laden und starten |
#| |
#| zuletzt geaendert: 2008.09.16 (c) by MichiSoft TM |
#\*----------------------------------------------------------------------------
void main() #------------------------------------------------------------- # RealMode!!!
asm{
bits_16
# Debug
mov al, 'k'
mov ah, 0x0e
mov bx, 0x0000
int 0x10
# mehr als 1mb Ram erlauben (Gate 20A... :~~[ )
mov ax, 0x2401
int 0x15
in al, 0x92
or al, 0x02
out 0x92, al
# Speicher-Menge (RAM) feststellen
mov ax, 0xe801
int 0x15
mov [_var_mem_size], bx # #64kb
shr ax, 0x06
add [_var_mem_size], ax # #1kb
# Kernel-Image laden
# _reset_drive:
mov dl, 0x00
mov ah, 0x00
int 0x13
or ah, ah
#jnz _reset_drive
xor ax, ax
mov es, ax
# Ziel des Images
mov ax, 0x1000
mov es, ax
mov bx, 0x0000 # 0x1000:0x0000 = 0x10000 real mode segmentation is ugly
mov ah, 0x02 # "ReadSector"
mov al, 0x80 # 128 sectors * 512 bytes = 64k
mov cx, 0x001d # sector 0x1f, cylinder 0
mov dx, 0x0080 # head 0x00, disc 0
# int 0x13
mov ax, 0x2000
mov es, ax
mov bx, 0x0000 # 0x2000:0x0000 = 0x20000 real mode segmentation is ugly
mov ah, 0x02 # "ReadSector"
mov al, 0x80 # 128 sectors * 512 bytes = 64k
mov cx, 0x001f # sector 0x1f, cylinder 0
mov dx, 0x0280 # head 0x02, disc 0
# int 0x13
# mov al, [0x5008] # Größe: 1Sektor = 512b
# mov cx, [0x5004]
# mov dx, [0x5006]
# int 0x13
or ah, ah
#jnz _reset_drive
# set destination
mov ax, 0x0000 # 0x0030
mov [_rsp_dest_hi], ax
mov ax, 0xa000 # 0x0000
mov [_rsp_dest_lo], ax
# set source
mov ax, 0x001c
mov [_rsp_lba], ax
mov ax, 0x0001
mov [_rsp_count], ax
_read_loop_start:
# if count == 0 break
mov ax, [_rsp_count]
cmp ax, 0x0000
jz _after_read_loop
mov al, 'M'
mov ah, 0x0e
mov bx, 0x0000
int 0x10
jmp _read_sector
_after_read:
# count --
mov ax, [_rsp_count]
dec ax
mov [_rsp_count], ax
# lba ++
mov ax, [_rsp_lba]
inc ax
mov [_rsp_lba], ax
# dest ++
mov ax, [_rsp_dest_lo]
add ax, 0x0200
mov [_rsp_dest_lo], ax
cmp ax, 0x0000
jnz _after_dest
mov ax, [_rsp_dest_hi]
add ax, 0x0100
mov [_rsp_dest_hi], ax
_after_dest:
jmp _read_loop_start
_after_read_loop:
mov al, 'X'
mov ah, 0x0e
mov bx, 0x0000
int 0x10
mov al, [0xa012]
mov ah, 0x0e
mov bx, 0x0000
int 0x10
jmp $
# Speicher-Segmente laden
lgdt [_gdtr]
# Debug
mov al, '3'
mov ah, 0x0e
mov bx, 0x0000
int 0x10
# 32bit Protected-Mode aktivieren
cli
mov eax, cr0
or al, 0x01
mov cr0, eax
# flush cpu prefetch
jmp _flush
_flush:
# die Segment-Register vorbereiten
mov ax, 0x0010 # Descriptor[2]
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
# Stack initialisieren
mov esp, 0x0027fff8
# in den 32bit ProtectedMode springen
db 0x66 # weil im 16:32 Format...
jmp_far [_pmode_ptr] # 0x0008:_pmode
#-------------------------------------------------------------
# ProtectedMode!!!
_pmode:
bits_32
#jmp $
mov eax, 0x000b8000
mov [eax], 'M'
add eax, 0x00000001
mov [eax], 0x20
# für den Stack nach einem C-Aufruf!
mov ebp, esp
mov eax, 0x000b8002
mov [eax], '1'
add eax, 0x00000001
mov [eax], 0x20
# Variablen speichern
mov eax, [_var_mem_size]
shl eax, 0x06
mov [0x00005100], eax
xor eax, eax
# -> Kernel starten
mov eax, 0x00010000
jmp eax
# zur Sicherheit....
jmp $
bits_16
_read_sector:
# Calculate cylinder, head and sector:
# Cylinder = (LBA / SectorsPerTrack) / NumHeads
# Sector = (LBA mod SectorsPerTrack) + 1
# Head = (LBA / SectorsPerTrack) mod NumHeads
mov ax, [_rsp_lba]
mov bx, [_drive_sectors]
xor dx, dx
div ax, bx
inc dx
mov cl, dl
mov bx, [_drive_heads]
xor dx, dx
div ax, bx
mov ch, al
xchg dl, dh
mov dl, 0x00 # disc 0
# destination
mov ax, [_rsp_dest_hi]
shl ax, 0x08 # 0x1200:0x3456 = 0x123456 real mode segmentation is ugly
mov es, ax
mov bx, [_rsp_dest_lo]
# DEBUG
#mov ax, 0x0000
#mov es, ax
#mov bx, 0xa000 # 0x1000:0x0000 = 0x10000 real mode segmentation is ugly
mov ah, 0x02 # "ReadSector"
mov al, 0x01 # 128 sectors * 512 bytes = 64k
mov cx, 0x001d # sector 0x1f, cylinder 0
mov dx, 0x0080 # head 0x00, disc 0
int 0x13
jmp _after_read
jmp _after_read
# read
mov ah, 0x02 # "ReadSector"
mov al, 0x01 # 1 sector = 512 bytes
int 0x13
# mov cx, 0x001d # sector 0x1f, cylinder 0
# mov dx, 0x0080 # head 0x00, disc 0
# mov ah, 0x02 # "ReadSector"
# mov al, 0x01 # 1 sector = 512 bytes
# int 0x13
jmp _after_read
_rsp_dest_hi:
dw 0x0000
_rsp_dest_lo:
dw 0x0000
_rsp_lba:
dw 0x0000
_rsp_count:
dw 0x0000
_drive_heads:
dw 0x0010 # 16
_drive_sectors:
dw 0x003f # 63
#-------------------------------------------------------------
# Daten
# Variablen
_var_mem_size:
dd 0x00000000
_gdt:
# [0] 0x00 null descriptor
dd 0x00000000
dd 0x00000000
# [1] 0x08 code descriptor
dd 0x0000ffff
dd 0x00cf9a00
# [2] 0x10 data descriptor
dd 0x0000ffff
dd 0x00cf9200
_gdtr:
dw 0x0017 # 3 descriptors
dd _gdt
_pmode_ptr:
dd _pmode
dw 0x0008 # Descriptor[1] : ...
}