2007-08-27 12:48:03 +00:00
|
|
|
// Boot loader.
|
|
|
|
//
|
2007-09-15 20:05:47 +00:00
|
|
|
// Part of the boot sector, along with bootasm.S, which calls bootmain().
|
|
|
|
// bootasm.S has put the processor into protected 32-bit mode.
|
|
|
|
// bootmain() loads an ELF kernel image from the disk starting at
|
|
|
|
// sector 1 and then jumps to the kernel entry routine.
|
2006-09-06 19:08:14 +00:00
|
|
|
|
|
|
|
#include "types.h"
|
|
|
|
#include "elf.h"
|
|
|
|
#include "x86.h"
|
2011-07-29 11:31:27 +00:00
|
|
|
#include "memlayout.h"
|
2006-06-12 15:22:12 +00:00
|
|
|
|
2006-09-06 17:04:06 +00:00
|
|
|
#define SECTSIZE 512
|
2006-06-12 15:22:12 +00:00
|
|
|
|
2009-03-08 21:41:30 +00:00
|
|
|
void readseg(uchar*, uint, uint);
|
2006-06-12 15:22:12 +00:00
|
|
|
|
|
|
|
void
|
2007-08-28 13:01:10 +00:00
|
|
|
bootmain(void)
|
2006-06-12 15:22:12 +00:00
|
|
|
{
|
2007-08-24 20:28:08 +00:00
|
|
|
struct elfhdr *elf;
|
2006-09-06 17:04:06 +00:00
|
|
|
struct proghdr *ph, *eph;
|
2007-08-24 20:28:08 +00:00
|
|
|
void (*entry)(void);
|
2011-07-29 11:31:27 +00:00
|
|
|
uchar* pa;
|
2006-06-12 15:22:12 +00:00
|
|
|
|
2007-08-24 20:28:08 +00:00
|
|
|
elf = (struct elfhdr*)0x10000; // scratch space
|
2006-06-12 15:22:12 +00:00
|
|
|
|
2007-08-24 20:28:08 +00:00
|
|
|
// Read 1st page off disk
|
2009-03-08 21:41:30 +00:00
|
|
|
readseg((uchar*)elf, 4096, 0);
|
2007-08-24 20:28:08 +00:00
|
|
|
|
|
|
|
// Is this an ELF executable?
|
|
|
|
if(elf->magic != ELF_MAGIC)
|
2009-03-08 21:41:30 +00:00
|
|
|
return; // let bootasm.S handle error
|
2006-06-12 15:22:12 +00:00
|
|
|
|
2007-08-24 20:28:08 +00:00
|
|
|
// Load each program segment (ignores ph flags).
|
|
|
|
ph = (struct proghdr*)((uchar*)elf + elf->phoff);
|
|
|
|
eph = ph + elf->phnum;
|
2011-01-11 18:01:13 +00:00
|
|
|
for(; ph < eph; ph++){
|
2011-07-29 11:31:27 +00:00
|
|
|
pa = (uchar*)ph->pa;
|
|
|
|
readseg(pa, ph->filesz, ph->offset);
|
2009-03-08 21:41:30 +00:00
|
|
|
if(ph->memsz > ph->filesz)
|
2011-07-29 11:31:27 +00:00
|
|
|
stosb(pa + ph->filesz, 0, ph->memsz - ph->filesz);
|
2009-03-08 21:41:30 +00:00
|
|
|
}
|
2006-06-12 15:22:12 +00:00
|
|
|
|
2007-08-24 20:28:08 +00:00
|
|
|
// Call the entry point from the ELF header.
|
|
|
|
// Does not return!
|
2011-07-29 11:31:27 +00:00
|
|
|
entry = (void(*)(void))(elf->entry & 0xFFFFFF);
|
2007-08-24 20:28:08 +00:00
|
|
|
entry();
|
2006-06-12 15:22:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
waitdisk(void)
|
|
|
|
{
|
2007-08-24 20:28:08 +00:00
|
|
|
// Wait for disk ready.
|
2006-09-06 17:27:19 +00:00
|
|
|
while((inb(0x1F7) & 0xC0) != 0x40)
|
2006-09-06 17:50:20 +00:00
|
|
|
;
|
2006-06-12 15:22:12 +00:00
|
|
|
}
|
|
|
|
|
2007-08-22 06:01:32 +00:00
|
|
|
// Read a single sector at offset into dst.
|
2006-06-12 15:22:12 +00:00
|
|
|
void
|
2006-07-20 09:07:53 +00:00
|
|
|
readsect(void *dst, uint offset)
|
2006-06-12 15:22:12 +00:00
|
|
|
{
|
2007-08-24 20:28:08 +00:00
|
|
|
// Issue command.
|
2006-09-06 17:04:06 +00:00
|
|
|
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
|
2006-06-12 15:22:12 +00:00
|
|
|
|
2007-08-24 20:28:08 +00:00
|
|
|
// Read data.
|
2006-09-06 17:04:06 +00:00
|
|
|
waitdisk();
|
|
|
|
insl(0x1F0, dst, SECTSIZE/4);
|
2006-06-12 15:22:12 +00:00
|
|
|
}
|
|
|
|
|
2007-08-22 06:01:32 +00:00
|
|
|
// Read 'count' bytes at 'offset' from kernel into virtual address 'va'.
|
|
|
|
// Might copy more than asked.
|
|
|
|
void
|
2009-03-08 21:41:30 +00:00
|
|
|
readseg(uchar* va, uint count, uint offset)
|
2007-08-22 06:01:32 +00:00
|
|
|
{
|
2009-03-08 21:41:30 +00:00
|
|
|
uchar* eva;
|
2007-08-22 06:01:32 +00:00
|
|
|
|
2007-08-24 20:28:08 +00:00
|
|
|
eva = va + count;
|
2007-08-22 06:01:32 +00:00
|
|
|
|
2007-08-24 20:28:08 +00:00
|
|
|
// Round down to sector boundary.
|
2009-03-08 21:41:30 +00:00
|
|
|
va -= offset % SECTSIZE;
|
2007-08-22 06:01:32 +00:00
|
|
|
|
2007-08-24 20:28:08 +00:00
|
|
|
// Translate from bytes to sectors; kernel starts at sector 1.
|
2007-08-22 06:01:32 +00:00
|
|
|
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.
|
2007-08-24 20:28:08 +00:00
|
|
|
for(; va < eva; va += SECTSIZE, offset++)
|
2009-03-08 21:41:30 +00:00
|
|
|
readsect(va, offset);
|
2007-08-22 06:01:32 +00:00
|
|
|
}
|