-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootc.c
67 lines (46 loc) · 1.37 KB
/
bootc.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
#include "types.h"
#include "asm.h"
#include "elf.h"
#define KERN_LINK_PHYS 0x00100000
#define SECT_SIZE 512
#define ATA_RDY (1 << 6)
#define ATA_BSY (1 << 7)
typedef void (*func_t)(void);
#define CHECK_ATA_READY(status) \
(((status & 0xff) & (ATA_RDY | ATA_BSY)) == ATA_RDY)
char *read_sect(char *dst, uint32_t lba) {
while (!CHECK_ATA_READY(inb(0x1f7)))
;
outb(0x1f6, 0xe0 | ((lba >> 24) & 0x0f));
outb(0x1f2, 1);
outb(0x1f3, (uint8_t)lba);
outb(0x1f4, (uint8_t)((lba >> 8) & 0xff));
outb(0x1f5, (uint8_t)((lba >> 16) & 0xff));
outb(0x1f7, 0x20);
while (!CHECK_ATA_READY(inb(0x1f7)))
;
insd(0x1f0, (uint8_t *)dst, SECT_SIZE / 4);
return dst + SECT_SIZE;
}
void read_offset(char *dst, uint32_t offset, uint32_t size) {
uint32_t lba = offset / SECT_SIZE;
char *p = dst - (offset % SECT_SIZE);
while (p < dst + size) {
p = read_sect(p, lba++);
}
}
void bootc(void) {
int i;
Elf32_Ehdr *ehdr;
Elf32_Phdr *phdr;
ehdr = (Elf32_Ehdr *)KERN_LINK_PHYS;
read_offset((char *)ehdr, SECT_SIZE, SECT_SIZE);
for (i = 0; i < ehdr->e_phnum; i++) {
phdr = ELF_PHDR(ehdr, i);
if (phdr->p_type & PT_LOAD) {
read_offset((char *)phdr->p_paddr, SECT_SIZE + phdr->p_offset, phdr->p_filesz);
}
}
func_t entry = (func_t)ehdr->e_entry;
entry();
}