* same with xv6
[mascara-docs.git] / i386 / ucla / src / lab3 / boot / main.c
blob4c57c029590cce9ce9dbc949e2c7bde200c4df9c
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 disk's sector 0 (the first sector).
12 * * The 2nd sector onward holds the kernel image.
14 * * The kernel image must be in ELF executable 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 up 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.
31 **********************************************************************/
33 #define SECTSIZE 512
34 #define PAGESIZE 4096
35 #define ELFHDR ((struct Elf *) 0x10000) // scratch space
37 void readsect(void *addr, uint32_t sectornum);
38 void readseg(uint32_t va, uint32_t filesz, uint32_t memsz, uint32_t offset);
40 asmlinkage void
41 bootmain(void)
43 struct Proghdr *ph, *eph;
45 // read 1st page off disk
46 readseg((uint32_t) ELFHDR, PAGESIZE, PAGESIZE, 0);
48 // is this a valid ELF?
49 if (ELFHDR->e_magic != ELF_MAGIC)
50 goto bad;
52 // load each program segment (ignores ph flags)
53 ph = (struct Proghdr *) ((uint8_t *) ELFHDR + ELFHDR->e_phoff);
54 eph = ph + ELFHDR->e_phnum;
55 for (; ph < eph; ph++)
56 readseg(ph->p_va, ph->p_filesz, 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 & 0xFFFFFF))();
62 bad:
63 outw(0x8A00, 0x8A00);
64 outw(0x8A00, 0x8E00);
65 /* boot.S will spin for us */
68 // Read 'filesz' bytes at 'offset' from kernel into virtual address 'va',
69 // then clear the memory from 'va+filesz' up to 'va+memsz' (set it to 0).
70 void
71 readseg(uint32_t va, uint32_t filesz, uint32_t memsz, uint32_t offset)
73 uint32_t end_va;
75 va &= 0xFFFFFF;
76 end_va = va + filesz;
77 memsz += va;
79 // round down to sector boundary
80 va &= ~(SECTSIZE - 1);
82 // translate from bytes to sectors, and kernel starts at sector 1
83 offset = (offset / SECTSIZE) + 1;
85 // If this is too slow, we could read lots of sectors at a time.
86 // We'd write more to memory than asked, but it doesn't matter --
87 // we load in increasing order.
88 while (va < end_va) {
89 readsect((uint8_t*) va, offset);
90 va += SECTSIZE;
91 offset++;
94 // clear bss segment
95 while (end_va < memsz)
96 *((uint8_t*) end_va++) = 0;
99 void
100 waitdisk(void)
102 // wait for disk reaady
103 while ((inb(0x1F7) & 0xC0) != 0x40)
104 /* do nothing */;
107 void
108 readsect(void *dst, uint32_t offset)
110 // wait for disk to be ready
111 waitdisk();
113 outb(0x1F2, 1); // count = 1
114 outb(0x1F3, offset);
115 outb(0x1F4, offset >> 8);
116 outb(0x1F5, offset >> 16);
117 outb(0x1F6, (offset >> 24) | 0xE0);
118 outb(0x1F7, 0x20); // cmd 0x20 - read sectors
120 // wait for disk to be ready
121 waitdisk();
123 // read a sector
124 insl(0x1F0, dst, SECTSIZE/4);