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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
|
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"
/>
<title>reveal.js</title>
<link rel="stylesheet" href="dist/reset.css" />
<link rel="stylesheet" href="dist/reveal.css" />
<link rel="stylesheet" href="dist/theme/white.css" />
<!-- Theme used for syntax highlighted code -->
<link rel="stylesheet" href="plugin/highlight/highlight.css" />
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h2>x86 Bootloader</h2>
<small>CT, Marvin Borner, TGI13.1</small>
<aside class="notes">
<ul>
<li>Ich lass einiges aus, weil großes Projekt</li>
<li>Ansonsten nachher fragen</li>
</ul>
</aside>
</section>
<section>
<h2>Inhalt</h2>
<ul>
<li>Was ist/macht ein Bootloader?</li>
<li>Boot-Ablauf</li>
<li>Umsetzung</li>
<li>Protokolle</li>
<li>x86-Architektur</li>
<li>Code</li>
<li>Demo</li>
<li>Fazit</li>
</ul>
</section>
<section>
<section>
<h2>Was ist ein Bootloader?</h2>
<p class="fragment">Bindeglied in Boot-Prozess</p>
<img
class="fragment"
src="img/process.png"
alt="Boot-Prozess Beispiel (Legacy)"
/>
<aside class="notes">
<ul>
<li>
Nur Legacy Boot behandelt (kein UEFI, GPT,
...)
</li>
<li>
Generell: MBR lädt Bootloader, Bootloader
lädt Kernel
</li>
</ul>
</aside>
</section>
<section>
<h2>Was macht ein Bootloader?</h2>
<ul>
<li class="fragment">
Initialisiert CPU, GPU, I/O
</li>
<li class="fragment">
Simple Treiber (Festplatte, Grafik)
</li>
<li class="fragment">
Häufig GUI zur Auswahl und Konfiguration
</li>
<li class="fragment">Sucht nach Hardware-Infos</li>
<li class="fragment">
Lädt gewählten Kernel mit Infos
</li>
</ul>
</section>
</section>
<section>
<!-- boot sequence osdev -->
<h2 style="position: relative; top: 100px">Ablauf</h2>
<section>
<p>
<b>B</b>asic <b>I</b>nput <b>O</b>utput
<b>S</b>ystem
</p>
<ul>
<li class="fragment">Hardware Initialisierung</li>
<li class="fragment">
Stellt BIOS interrupts zur Verfügung
</li>
</ul>
<aside class="notes">
<ul>
<li>
LEGACY: Kommt aus 80ern, trotzdem noch
überall vertreten (-> UEFI)
</li>
<li>
Power Button wird gedrückt - hardcoded jump
zu BIOS
</li>
</ul>
</aside>
</section>
<section>
<p><b>P</b>ower <b>O</b>n <b>S</b>elf <b>T</b>est</p>
<ul>
<li class="fragment">Diagnose</li>
<li class="fragment">Bootbares Gerät?</li>
</ul>
<aside class="notes">
<ul>
<li>Gehört eigtl. noch zu BIOS</li>
</ul>
</aside>
</section>
<section>
<p><b>M</b>aster <b>B</b>oot <b>R</b>ecord</p>
<ul>
<li class="fragment">512B :O</li>
<li class="fragment">Magic <code>0xAA55</code></li>
<li class="fragment">
Von BIOS zu <code>0x7C00</code>
</li>
<li class="fragment">
Bei Festplatten: Partitionstabelle @445
</li>
<!-- Implementationsabhängig! -->
<b class="fragment">
→ Rest ist implementationsabhängig
</b>
</ul>
<aside class="notes">
<ul>
<li>Also eigentlich nur 446B</li>
<li>
Bei 0x7C00 aus historischen Gründen; 1KiB
entfernt von 32KiB; erster 86-PC hatte 32KiB
RAM; 0x0-0x7C00 für OS)
</li>
</ul>
</aside>
</section>
</section>
<!--<section>
<h2 style="position: relative; top: 100px">Arten</h2>
<section>
<h4>Krasser Code-Golfer</h4>
<p class="fragment">Komplett in MBR (512B bzw. 446B)</p>
<p class="fragment">Nachteil: Nahezu unmöglich</p>
<p class="fragment">Ggf. für embedded Systeme</p>
</section>
<section>
<h4>Single-Stage</h4>
<p class="fragment">
Nur kleiner Kernel-loader in MBR, keine
Initialisierung
</p>
<p class="fragment">Kernel macht den Rest</p>
<p class="fragment">
Nachteil: Nicht mit allen Kernels kompatibel
</p>
</section>
<section>
<h4>Multi-Stage</h4>
<p class="fragment">MBR lädt nur zweite Stage</p>
<p class="fragment">Ggf. noch mehr Stages</p>
<p class="fragment">Meine Wahl: Dual-Stage</p>
</section>
</section>-->
<section>
<h2 style="position: relative; top: 80px">Umsetzung</h2>
<section>
<img
src="img/disk.svg"
width="400"
alt="Festplatte"
style="position: relative; top: 70px"
/>
<aside class="notes">
<ul>
<li>Dual-Stage</li>
<li>64. Sektor = 64 * 512 = 0x8000 = 32768</li>
<li>1. Stage == Bootsektor</li>
<li>2. Stage == Loader</li>
</ul>
</aside>
</section>
<section>
<h4>1. Stage: MBR; <code>0x7C00-0x7E00</code></h4>
<ul>
<li class="fragment">Generelle Diagnose</li>
<li class="fragment">
<!-- Bits beziehen sich auf Adressierung, Register sind dennoch nutzbar -->
CPU von <b>Real</b> (16 Bit) zu
<b>Protected</b> mode (32 Bit)
<!-- Long mode -->
</li>
<li class="fragment">
Lädt 2. Stage von boot disk
</li>
</ul>
<aside class="notes">
<ul>
<li>
Boot disk wird von BIOS weitergegeben (in dl
Reg)
</li>
</ul>
</aside>
</section>
<section>
<h4>2. Stage: <code>0x7E00-0xFC00</code> (~32KiB)</h4>
<ul>
<li class="fragment">
Diagnose (memory, disk, ...)
</li>
<li class="fragment">
Initialisierung (A20, GDT, IDT, Register, ...)
</li>
<li class="fragment">
Partition und Festplatte suchen
</li>
<li class="fragment">
Dateisystem erkennen und konfigurieren
</li>
<li class="fragment">
GUI für Auswahl mittels config Datei
</li>
<li class="fragment">Kernel laden</li>
</ul>
<aside class="notes">
<ul>
<li>
0xFC00 wegen Partitionstart in 64. Sektor
(0x8000 -> 0x7C00 + 0x8000 = 0xFC00)
</li>
<li>Ggf. config zeigen</li>
</ul>
</aside>
</section>
</section>
<section>
<h2>Protokolle</h2>
<ul>
<li class="fragment">Multiboot 1/2</li>
<li class="fragment">Linux, Windows, Mac OS</li>
</ul>
<aside class="notes">
<ul>
<li>Protokolle für Datenweitergabe an Kernel</li>
<li>MB unterstützt sehr viele Betriebssysteme</li>
<li>MB1 komplett, MB2 nur so bisschen</li>
<li>OS-spezifisch häufig sehr komplex</li>
</ul>
</aside>
</section>
<!-- <section> -->
<!-- <h2>Relokation</h2> -->
<!-- <ul> -->
<!-- <li class="fragment"> -->
<!-- MBR (<code>0x7C00</code>) zu z.B. -->
<!-- <code>0x0600</code> -->
<!-- </li> -->
<!-- <li class="fragment"> -->
<!-- Etwas anderes (Kernel, anderen Bootloader) zu -->
<!-- <code>0x7C00</code> -->
<!-- </li> -->
<!-- <li class="fragment"> -->
<!-- Vorteil: Mehr Platz (~30KiB), eindeutige Adresse, -->
<!-- mehrere Bootloader chainloaden -->
<!-- </li> -->
<!-- <li class="fragment">z.B. Windows Boot</li> -->
<!-- </ul> -->
<!-- </section> -->
<section>
<h2>x86-Architektur</h2>
<section>
<h4>Grundlegende Register</h4>
<table style="font-size: 70%">
<tbody>
<tr>
<th>64bit</th>
<th>32bit</th>
<th>16bit</th>
<th>8bit-h</th>
<th>8bit-l</th>
<th>Verwendung</th>
</tr>
<tr>
<td>rax</td>
<td>eax</td>
<td>ax</td>
<td>ah</td>
<td>al</td>
<td>Accumulator</td>
</tr>
<tr>
<td>rbx</td>
<td>ebx</td>
<td>bx</td>
<td>bh</td>
<td>bl</td>
<td>Base</td>
</tr>
<tr>
<td>rcx</td>
<td>ecx</td>
<td>cx</td>
<td>ch</td>
<td>cl</td>
<td>Counter</td>
</tr>
<tr>
<td>rdx</td>
<td>edx</td>
<td>dx</td>
<td>dh</td>
<td>dl</td>
<!-- Disk id -->
<td>Data</td>
</tr>
<tr>
<td>rbp</td>
<td>ebp</td>
<td>bp</td>
<td>/</td>
<td>/</td>
<td>Base pointer</td>
</tr>
<tr>
<td>rsp</td>
<td>esp</td>
<td>sp</td>
<td>/</td>
<td>/</td>
<td>Stack pointer</td>
</tr>
<tr>
<td>rip</td>
<td>eip</td>
<td>ip</td>
<td>/</td>
<td>/</td>
<td>Instruction pointer</td>
</tr>
</tbody>
</table>
<aside class="notes">
<b>Eigentlich wie 8051 ASM</b>
<ul>
<li>A: Arith, I/O, ...</li>
<li>B: Base pointer für memory access</li>
<li>C: Loop counter</li>
<li>D: Eigentlich alles</li>
<li>---</li>
<li>SP: dec/inc je push/pop</li>
<li>BP: Frame pointer - Prolog</li>
<li>---</li>
<li>
IP: Wie PC (wird auf SP als return address
gepusht)
</li>
</ul>
</aside>
</section>
</section>
<section>
<h2>Sprachen</h2>
<ul>
<li class="fragment">
ASM in MBR: Kann direkt mit Registern umgehen, kein
Bootstrapping/Overhead
</li>
<li class="fragment">
C in 2. Stage: Lowlevel (<code>__asm__</code>,
<code>*(u8*)0x7E00</code>), kein Interpreter
</li>
</ul>
</section>
<section>
<section data-transition="fade-out">
<h2>1. Stage (MBR)</h2>
<pre><code class="fragment" data-line-numbers data-trim>
org 0x7C00
bits 16
mbr:
; Register leeren (manche BIOS sind komisch)
xor bx, bx
mov ds, bx
mov es, bx
mov ss, bx
; Manche Register sind wichtig (z.B. dl mit boot disk)
mov sp, 0x7C00 ; Stack: 0x7C00 -> wächst nach unten
; Text mode VGA mit BIOS interrupts leeren
mov ax, SCREEN_CLEAR
int SCREEN_INT
call disk_support ; Ist disk unterstützt?
jmp load_stage ; 2. Stage laden und ausführen
</code></pre>
<aside class="notes">
<ul>
<li>Nicht alles, nur Ausschnitte</li>
<li>VGA = Video Graphics Array</li>
</ul>
</aside>
</section>
<section data-transition="fade">
<pre
style="font-size: 13pt"
><code data-line-numbers data-trim>
bits 16
load_stage:
mov bx, loader
mov [dap.dest], bx
call disk_read ; Nutzt dap, dl und BIOS INTs
lgdt [gdt] ; Lade GDT
; Protected mode (32-Bit)
mov eax, cr0
or ax, 1 ; PE (Protection Enable) Bit
mov cr0, eax
; 0x08 GDT offset -> 1. Eintrag -> Code segment (cs)
jmp 0x08:protected
bits 32
protected:
; Segment register auf 2. GDT Eintrag (non-executable)
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
push dx ; Disk (in dl) zu kernel pushen
call loader ; Zweite Stage!
[...]
dw 0xAA55 ; MBR magic
loader: incbin "build/loader.bin" ; 2. Stage (C)
</code></pre>
<aside class="notes">
<ul>
<li>GDT entry ist 8 Byte</li>
</ul>
</aside>
</section>
<!--<section>
<pre><code class="c" data-line-numbers data-trim>
static u8 boot_disk = 0;
int start(u8 disk)
{
boot_disk = disk;
memory_map();
a20_enable();
vga_clear();
serial_install();
pic_install();
idt_install();
pci_probe();
config_read();
gui_draw();
return 0;
}
</code></pre>
</section>-->
<section data-background="img/uml4.png">
<!-- <img -->
<!-- style=" -->
<!-- height: auto; -->
<!-- width: 105%; -->
<!-- left: -20px; -->
<!-- position: relative; -->
<!-- max-width: unset; -->
<!-- max-height: unset; -->
<!-- " -->
<!-- src="img/uml3.png" -->
<!-- alt="UML-Diagramm" -->
<!-- /> -->
<aside class="notes">
<ul>
<li>PIC = Programmable Interrupt Controller</li>
<li>
PCI = Peripheral Component Interconnect
(bus)
</li>
<li>VGA = Video Graphics Array</li>
<li>IDE = Integrated Drive Electronics</li>
<li>ELF = Executable and Linkable Format</li>
<li>
A20 = Damit alle 32 Adressierungsleitungen
verwendet werden können (Sonst nur 2^20 =
1MiB adressierbar)
</li>
</ul>
</aside>
</section>
<section data-background="img/uml3.png"></section>
</section>
<section><h2>Demo</h2></section>
<section>
<h2>Fazit</h2>
<ul>
<li class="fragment">
Schwierig jede Hardware zu unterstützen
</li>
<li class="fragment">Lieber GRUB/...</li>
</ul>
<aside class="notes">
<ul>
<li>Interessant</li>
<li>UEFI, PCIE, SATA, USB, XHCI, ...</li>
<li>Lieber GRUB für Kompatibilität</li>
</ul>
</aside>
</section>
<section>
<h2>Fragen?</h2>
<ul class="fragment">
<li>Segmentation, GDT</li>
<li>Interrupts, IDT, PIC</li>
<li>Multiboot 1/2</li>
<li>Mehr Code-Beispiele</li>
</ul>
</section>
<section>
<h2>Quellen</h2>
<section>
<p>Bilder</p>
<br />
<ul>
<li>
https://en.wikipedia.org/wiki/BIOS#/media/File:Legacy_BIOS_boot_process.png
</li>
</ul>
</section>
<section>
<p>Informationen (aufgerufen 13.07.2021)</p>
<br />
<ul>
<li>https://en.wikipedia.org/wiki/Bootloader</li>
<li>https://en.wikipedia.org/wiki/BIOS</li>
<li>https://wiki.osdev.org/</li>
<li>https://github.com/marvinborner/Melvix</li>
<li>
Intel 64 and IA-32 Architectures Software
Developer's Manual
</li>
</ul>
</section>
</section>
</div>
</div>
<script src="dist/reveal.js"></script>
<script src="plugin/notes/notes.js"></script>
<script src="plugin/markdown/markdown.js"></script>
<script src="plugin/highlight/highlight.js"></script>
<script>
Reveal.initialize({
hash: true,
controls: false,
dependencies: [
{
src: "plugin/cursor/cursor.js",
async: true,
},
],
// Learn about plugins: https://revealjs.com/plugins/
plugins: [RevealMarkdown, RevealHighlight, RevealNotes],
});
</script>
</body>
</html>
|