forked from mit-pdos/xv6-public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootmain.c
98 lines (80 loc) · 2.16 KB
/
bootmain.c
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
// Boot loader.
//
// The BIOS loads boot sector (bootasm.S) from sector 1 of the disk
// into memory and executes it. The boot sector puts the processor
// in 32-bit mode and calls cmain below, which loads an ELF kernel
// image from the disk starting at sector 2 and then jumps to the
// kernel entry routine.
#include "types.h"
#include "elf.h"
#include "x86.h"
#define SECTSIZE 512
void readseg(uint, uint, uint);
void
cmain(void)
{
struct elfhdr *elf;
struct proghdr *ph, *eph;
void (*entry)(void);
elf = (struct elfhdr*)0x10000; // scratch space
// Read 1st page off disk
readseg((uint)elf, SECTSIZE*8, 0);
// Is this an ELF executable?
if(elf->magic != ELF_MAGIC)
goto bad;
// Load each program segment (ignores ph flags).
ph = (struct proghdr*)((uchar*)elf + elf->phoff);
eph = ph + elf->phnum;
for(; ph < eph; ph++)
readseg(ph->va, ph->memsz, ph->offset);
// Call the entry point from the ELF header.
// Does not return!
entry = (void(*)(void))(elf->entry & 0xFFFFFF);
entry();
bad:
outw(0x8A00, 0x8A00);
outw(0x8A00, 0x8E00);
for(;;)
;
}
void
waitdisk(void)
{
// Wait for disk ready.
while((inb(0x1F7) & 0xC0) != 0x40)
;
}
// Read a single sector at offset into dst.
void
readsect(void *dst, uint offset)
{
// Issue command.
waitdisk();
outb(0x1F2, 1); // count = 1
outb(0x1F3, offset);
outb(0x1F4, offset >> 8);
outb(0x1F5, offset >> 16);
outb(0x1F6, (offset >> 24) | 0xE0);
outb(0x1F7, 0x20); // cmd 0x20 - read sectors
// Read data.
waitdisk();
insl(0x1F0, dst, SECTSIZE/4);
}
// Read 'count' bytes at 'offset' from kernel into virtual address 'va'.
// Might copy more than asked.
void
readseg(uint va, uint count, uint offset)
{
uint eva;
va &= 0xFFFFFF;
eva = va + count;
// Round down to sector boundary.
va &= ~(SECTSIZE - 1);
// Translate from bytes to sectors; kernel starts at sector 1.
offset = (offset / SECTSIZE) + 1;
// If this is too slow, we could read lots of sectors at a time.
// We'd write more to memory than asked, but it doesn't matter --
// we load in increasing order.
for(; va < eva; va += SECTSIZE, offset++)
readsect((uchar*)va, offset);
}