-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootloader.asm
More file actions
581 lines (472 loc) · 10.4 KB
/
Copy pathbootloader.asm
File metadata and controls
581 lines (472 loc) · 10.4 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
; ============================================================
; TinyBootloader v2.0
; 2-Stufen-Bootloader mit LBA/CHS Auto-Erkennung
; und integrierter Mini-Shell
;
; Build: nasm -f bin -o tinyboot.img bootloader.asm
; Run: qemu-system-i386 -fda tinyboot.img
; ============================================================
; Speicherbelegung:
; 0x7C00 - 0x7DFF Stage 1 (512 Byte, von BIOS geladen)
; 0x7E00 - 0x87FF Stage 2 (2560 Byte, von Stage 1 geladen)
; 0x8800 - 0xFFFF Stack (abwaerts) + frei
; ============================================================
org 0x7C00
; ============================================================
; STAGE 1 (Sektor 0, 512 Byte)
; ============================================================
entry:
; Segmente initialisieren (DS = ES = SS = 0)
xor ax, ax
mov ds, ax
mov es, ax
mov ss, ax
mov sp, 0x7C00
; Boot-Laufwerk speichern (von BIOS in DL uebergeben)
mov [boot_drive], dl
; Bildschirm loeschen
call clearscreen
; Boot-Meldung ausgeben
mov si, msg_boot
call print
call print_newline
mov si, msg_loading
call print
; LBA-Unterstuetzung pruefen
call detect_lba
jc .use_chs
; LBA lesen (INT 13h AH=42h)
mov si, msg_lba
call print
mov si, dap
mov dl, [boot_drive]
mov ah, 0x42
int 0x13
jnc .done
jmp .error
.use_chs:
; CHS lesen (INT 13h AH=02h) - Fallback fuer Diskette
mov si, msg_chs
call print
mov ah, 0x02
mov al, 5 ; 5 Sektoren lesen
mov ch, 0 ; Cylinder 0
mov cl, 2 ; Start-Sektor 2
mov dh, 0 ; Head 0
mov dl, [boot_drive]
mov bx, 0x7E00 ; Zieladresse
int 0x13
jnc .done
.error:
mov si, msg_error
call print
.halt:
cli
hlt
jmp .halt
.done:
mov si, msg_ok
call print
; Boot-Laufwerk an Stage 2 uebergeben
mov dl, [boot_drive]
jmp 0x0000:0x7E00
; ============================================================
; FUNKTIONEN (Stage 1)
; ============================================================
; Bildschirm loeschen via BIOS (INT 10h, AH=07h)
clearscreen:
push ax
push bx
push cx
push dx
mov ah, 0x07
mov al, 0x00
mov bh, 0x07
mov cx, 0x0000
mov dx, 0x184F
int 0x10
pop dx
pop cx
pop bx
pop ax
ret
; String ausgeben (SI = Adresse, null-terminiert)
print:
push ax
push bx
push si
mov ah, 0x0E
mov bh, 0x00
mov bl, 0x07
.loop:
lodsb
test al, al
jz .done
int 0x10
jmp .loop
.done:
pop si
pop bx
pop ax
ret
; Zeilenumbruch (CR + LF)
print_newline:
push ax
mov al, 0x0D
int 0x10
mov al, 0x0A
int 0x10
pop ax
ret
; LBA-Unterstuetzung prüfen (INT 13h AH=41h)
; Ausgabe: CF=0 wenn LBA verfuegbar
detect_lba:
mov ah, 0x41
mov bx, 0x55AA
mov dl, [boot_drive]
int 0x13
ret
; ============================================================
; DATEN (Stage 1)
; ============================================================
boot_drive: db 0
msg_boot: db "TinyBootloader v2.0", 0
msg_loading: db "Loading Stage 2...", 0
msg_lba: db " [LBA]", 0
msg_chs: db " [CHS]", 0
msg_ok: db " OK", 0x0D, 0x0A, 0
msg_error: db 0x0D, 0x0A, "Error: Disk read failed!", 0
; Disk Address Packet fuer LBA (INT 13h AH=42h)
dap:
db 0x10 ; Packetgroesse (16 Byte)
db 0 ; reserviert
dw 5 ; Anzahl Sektoren (Stage 2)
dw 0x7E00 ; Ziel-Offset
dw 0 ; Ziel-Segment
dq 1 ; Start-LBA (Sektor 1)
; Padding auf 512 Byte + Boot-Signatur
times 510 - ($ - $$) db 0
dw 0xAA55
; ============================================================
; STAGE 2 (Sektoren 1-5, ~2560 Byte)
; Wird von Stage 1 bei 0x7E00 geladen
; ============================================================
stage2_entry:
; Boot-Laufwerk aus DL sichern
mov [stage2_boot_drive], dl
; Erfolgsmeldung
mov si, msg_stage2
call stage2_print
call stage2_print_newline
; Info: LBA- oder CHS-Modus?
mov si, msg_stage2_info
call stage2_print
call stage2_print_newline
.shell_loop:
; Prompt ausgeben
mov si, msg_prompt
call stage2_print
; Eingabe lesen
mov di, input_buffer
call read_line
; Leere Eingabe ignorieren
mov si, input_buffer
cmp byte [si], 0
je .shell_loop
; Befehl erkennen
mov di, cmd_help
call strcmp
jc .cmd_help
mov di, cmd_cls
call strcmp
jc .cmd_cls
mov di, cmd_info
call strcmp
jc .cmd_info
mov di, cmd_hex
call strcmp
jc .cmd_hex
mov di, cmd_hello
call strcmp
jc .cmd_hello
mov di, cmd_reboot
call strcmp
jc .cmd_reboot
; Unbekannter Befehl
mov si, msg_unknown
call stage2_print
mov si, input_buffer
call stage2_print
call stage2_print_newline
jmp .shell_loop
.cmd_help:
mov si, msg_help_text
call stage2_print
jmp .shell_loop
.cmd_cls:
call stage2_clearscreen
jmp .shell_loop
.cmd_info:
call cmd_info_handler
jmp .shell_loop
.cmd_hex:
mov si, msg_hex_out
call stage2_print
mov ax, 0x1A3F
call print_hex
call stage2_print_newline
jmp .shell_loop
.cmd_hello:
mov si, msg_hello
call stage2_print
call stage2_print_newline
jmp .shell_loop
.cmd_reboot:
mov si, msg_reboot
call stage2_print
int 0x19
; ============================================================
; FUNKTIONEN (Stage 2)
; ============================================================
; Bildschirm loeschen
stage2_clearscreen:
push ax
push bx
push cx
push dx
mov ah, 0x07
mov al, 0x00
mov bh, 0x07
mov cx, 0x0000
mov dx, 0x184F
int 0x10
pop dx
pop cx
pop bx
pop ax
ret
; String ausgeben (SI = Adresse)
stage2_print:
push ax
push bx
push si
mov ah, 0x0E
mov bh, 0x00
mov bl, 0x07
.loop:
lodsb
test al, al
jz .done
int 0x10
jmp .loop
.done:
pop si
pop bx
pop ax
ret
; Zeilenumbruch
stage2_print_newline:
push ax
mov al, 0x0D
int 0x10
mov al, 0x0A
int 0x10
pop ax
ret
; Ein Zeichen ausgeben (AL)
stage2_print_char:
push ax
push bx
mov ah, 0x0E
mov bh, 0x00
mov bl, 0x07
int 0x10
pop bx
pop ax
ret
; Tastatureingabe lesen (bis Enter oder Backspace)
; DI = Zielpuffer, max. 255 Zeichen
; Ausgabe: Puffer mit null-terminiertem String
read_line:
push ax
push di
push cx
xor cx, cx
.loop:
mov ah, 0x00
int 0x16
cmp al, 0x0D ; Enter?
je .done
cmp al, 0x08 ; Backspace?
je .backspace
cmp al, 0x20 ; Steuerzeichen?
jb .loop
cmp al, 0x7E
ja .loop
cmp cx, 254 ; Puffer voll?
jae .loop
stosb
inc cx
call stage2_print_char
jmp .loop
.backspace:
test cx, cx
jz .loop
dec cx
dec di
mov al, 0x08
call stage2_print_char
mov al, 0x20
call stage2_print_char
mov al, 0x08
call stage2_print_char
jmp .loop
.done:
xor al, al
stosb
call stage2_print_newline
pop cx
pop di
pop ax
ret
; Zwei null-terminierte Strings vergleichen (case-insensitive)
; SI = String 1, DI = String 2
; Ausgabe: CF=1 wenn gleich, CF=0 wenn ungleich
strcmp:
push ax
push si
push di
.loop:
lodsb
call to_lower
mov ah, al
mov al, [di]
inc di
call to_lower
cmp ah, al
jne .not_equal
test al, al
jz .equal
jmp .loop
.equal:
stc
jmp .done
.not_equal:
clc
.done:
pop di
pop si
pop ax
ret
; Zeichen in AL in Kleinbuchstaben umwandeln
to_lower:
cmp al, 'A'
jb .done
cmp al, 'Z'
ja .done
add al, 0x20
.done:
ret
; AX als Hex-Zahl ausgeben (z.B. "0x1A3F")
print_hex:
push ax
push bx
push cx
push dx
mov cx, 4
mov bx, ax
mov al, '0'
call stage2_print_char
mov al, 'x'
call stage2_print_char
.loop:
rol bx, 4
mov al, bl
and al, 0x0F
cmp al, 10
jb .digit
add al, 'A' - 10
jmp .print
.digit:
add al, '0'
.print:
call stage2_print_char
dec cx
jnz .loop
pop dx
pop cx
pop bx
pop ax
ret
; ============================================================
; BEFEHLS-HANDLER (Stage 2)
; ============================================================
cmd_info_handler:
push ax
push bx
push cx
push dx
; RAM anzeigen
mov si, msg_info_mem
call stage2_print
int 0x12
call print_hex
mov si, msg_info_kb
call stage2_print
call stage2_print_newline
; Boot-Laufwerk anzeigen
mov si, msg_info_drive
call stage2_print
mov al, [stage2_boot_drive]
xor ah, ah
call print_hex
call stage2_print_newline
; LBA/CHS
mov si, msg_info_mode
call stage2_print
call stage2_print_newline
pop dx
pop cx
pop bx
pop ax
ret
; ============================================================
; DATEN (Stage 2)
; ============================================================
stage2_boot_drive: db 0
msg_stage2: db "Stage 2 erfolgreich geladen!", 0
msg_stage2_info: db "Gib 'help' fuer eine Befehlsliste ein.", 0
msg_prompt: db 0x0D, 0x0A, "> ", 0
cmd_help: db "help", 0
cmd_cls: db "cls", 0
cmd_info: db "info", 0
cmd_hex: db "hex", 0
cmd_hello: db "hello", 0
cmd_reboot: db "reboot", 0
msg_help_text:
db "Verfuegbare Befehle:", 0x0D, 0x0A
db " help - Diese Hilfe anzeigen", 0x0D, 0x0A
db " cls - Bildschirm loeschen", 0x0D, 0x0A
db " info - System-Informationen", 0x0D, 0x0A
db " hex - Hexadezimal-Test (0x1A3F)", 0x0D, 0x0A
db " hello - Hallo Welt ausgeben", 0x0D, 0x0A
db " reboot - Computer neustarten", 0x0D, 0x0A
db 0
msg_unknown: db "Unbekannter Befehl: ", 0
msg_hello: db "Hallo Welt!", 0
msg_reboot: db "Reboot...", 0
msg_hex_out: db "HEX: ", 0
msg_info_mem: db "RAM: 0x", 0
msg_info_kb: db " KB", 0
msg_info_drive: db "Drive: 0x", 0
msg_info_mode: db "Modus: LBA/CHS Auto", 0
; Eingabe-Puffer (256 Byte)
input_buffer: times 256 db 0
; ============================================================
; DISKETTEN-IMAGE AUF 1.44MB AUFFUELLEN
; ============================================================
; 1.44 MB = 1474560 Bytes
; Stage 1 = Sektor 0 (512 Byte)
; Stage 2 = 5 Sektoren (2560 Byte)
; Rest = Null-Bytes als freier Platz
times 1474560 - ($ - $$) db 0