This source file includes following definitions.
- bootmain
- waitdisk
- readsect
- readseg
1
2
3
4
5
6
7
8 #include "types.h"
9 #include "elf.h"
10 #include "x86.h"
11 #include "memlayout.h"
12
13 #define SECTSIZE 512
14
15 void readseg(uchar*, uint, uint);
16
17 void
18 bootmain(void)
19 {
20 struct elfhdr *elf;
21 struct proghdr *ph, *eph;
22 void (*entry)(void);
23 uchar* pa;
24
25 elf = (struct elfhdr*)0x10000;
26
27
28 readseg((uchar*)elf, 4096, 0);
29
30
31 if(elf->magic != ELF_MAGIC)
32 return;
33
34
35 ph = (struct proghdr*)((uchar*)elf + elf->phoff);
36 eph = ph + elf->phnum;
37 for(; ph < eph; ph++){
38 pa = (uchar*)ph->paddr;
39 readseg(pa, ph->filesz, ph->off);
40 if(ph->memsz > ph->filesz)
41 stosb(pa + ph->filesz, 0, ph->memsz - ph->filesz);
42 }
43
44
45
46 entry = (void(*)(void))(elf->entry);
47 entry();
48 }
49
50 void
51 waitdisk(void)
52 {
53
54 while((inb(0x1F7) & 0xC0) != 0x40)
55 ;
56 }
57
58
59 void
60 readsect(void *dst, uint offset)
61 {
62
63 waitdisk();
64 outb(0x1F2, 1);
65 outb(0x1F3, offset);
66 outb(0x1F4, offset >> 8);
67 outb(0x1F5, offset >> 16);
68 outb(0x1F6, (offset >> 24) | 0xE0);
69 outb(0x1F7, 0x20);
70
71
72 waitdisk();
73 insl(0x1F0, dst, SECTSIZE/4);
74 }
75
76
77
78 void
79 readseg(uchar* pa, uint count, uint offset)
80 {
81 uchar* epa;
82
83 epa = pa + count;
84
85
86 pa -= offset % SECTSIZE;
87
88
89 offset = (offset / SECTSIZE) + 1;
90
91
92
93
94 for(; pa < epa; pa += SECTSIZE, offset++)
95 readsect(pa, offset);
96 }