* same with xv6
[mascara-docs.git] / i386 / MIT / course / src / src.lab / boot / main.c
blob42ca4bb3b938aa310b53ee54d80bfc0eba6a0742
1 #include <inc/x86.h>
2 #include <inc/elf.h>
4 /**********************************************************************
5 * This a dirt simple boot loader, whose sole job is to boot
6 * an ELF kernel image from the first IDE hard disk.
8 * DISK LAYOUT
9 * * This program(boot.S and main.c) is the bootloader. It should
10 * be stored in the first sector of the disk.
12 * * The 2nd sector onward holds the kernel image.
14 * * The kernel image must be in ELF format.
16 * BOOT UP STEPS
17 * * when the CPU boots it loads the BIOS into memory and executes it
19 * * the BIOS intializes devices, sets of the interrupt routines, and
20 * reads the first sector of the boot device(e.g., hard-drive)
21 * into memory and jumps to it.
23 * * Assuming this boot loader is stored in the first sector of the
24 * hard-drive, this code takes over...
26 * * control starts in boot.S -- which sets up protected mode,
27 * and a stack so C code then run, then calls bootmain()
29 * * bootmain() in this file takes over, reads in the kernel and jumps to it.
30 **********************************************************************/
32 #define SECTSIZE 512
33 #define ELFHDR ((struct Elf *) 0x10000) // scratch space
35 void readsect(void*, uint32_t);
36 void readseg(uint32_t, uint32_t, uint32_t);
38 void
39 bootmain(void)
41 struct Proghdr *ph, *eph;
43 // read 1st page off disk
44 readseg((uint32_t) ELFHDR, SECTSIZE*8, 0);
46 // is this a valid ELF?
47 if (ELFHDR->e_magic != ELF_MAGIC)
48 goto bad;
50 // load each program segment (ignores ph flags)
51 ph = (struct Proghdr *) ((uint8_t *) ELFHDR + ELFHDR->e_phoff);
52 eph = ph + ELFHDR->e_phnum;
53 for (; ph < eph; ph++)
54 // p_pa is the load address of this segment (as well
55 // as the physical address)
56 readseg(ph->p_pa, ph->p_memsz, ph->p_offset);
58 // call the entry point from the ELF header
59 // note: does not return!
60 ((void (*)(void)) (ELFHDR->e_entry))();
62 bad:
63 outw(0x8A00, 0x8A00);
64 outw(0x8A00, 0x8E00);
65 while (1)
66 /* do nothing */;
69 // Read 'count' bytes at 'offset' from kernel into physical address 'pa'.
70 // Might copy more than asked
71 void
72 readseg(uint32_t pa, uint32_t count, uint32_t offset)
74 uint32_t end_pa;
76 end_pa = pa + count;
78 // round down to sector boundary
79 pa &= ~(SECTSIZE - 1);
81 // translate from bytes to sectors, and kernel starts at sector 1
82 offset = (offset / SECTSIZE) + 1;
84 // If this is too slow, we could read lots of sectors at a time.
85 // We'd write more to memory than asked, but it doesn't matter --
86 // we load in increasing order.
87 while (pa < end_pa) {
88 // Since we haven't enabled paging yet and we're using
89 // an identity segment mapping (see boot.S), we can
90 // use physical addresses directly. This won't be the
91 // case once JOS enables the MMU.
92 readsect((uint8_t*) pa, offset);
93 pa += SECTSIZE;
94 offset++;
98 void
99 waitdisk(void)
101 // wait for disk reaady
102 while ((inb(0x1F7) & 0xC0) != 0x40)
103 /* do nothing */;
106 void
107 readsect(void *dst, uint32_t offset)
109 // wait for disk to be ready
110 waitdisk();
112 outb(0x1F2, 1); // count = 1
113 outb(0x1F3, offset);
114 outb(0x1F4, offset >> 8);
115 outb(0x1F5, offset >> 16);
116 outb(0x1F6, (offset >> 24) | 0xE0);
117 outb(0x1F7, 0x20); // cmd 0x20 - read sectors
119 // wait for disk to be ready
120 waitdisk();
122 // read a sector
123 insl(0x1F0, dst, SECTSIZE/4);