2006-09-06 19:50:20 +02:00
|
|
|
// This a dirt simple boot loader, whose sole job is to boot
|
|
|
|
// an elf kernel image from the first IDE hard disk.
|
|
|
|
//
|
|
|
|
// DISK LAYOUT
|
|
|
|
// * This program(boot.S and main.c) is the bootloader. It should
|
|
|
|
// be stored in the first sector of the disk.
|
|
|
|
//
|
|
|
|
// * The 2nd sector onward holds the kernel image.
|
|
|
|
//
|
|
|
|
// * The kernel image must be in ELF format.
|
|
|
|
//
|
|
|
|
// BOOT UP STEPS
|
|
|
|
// * when the CPU boots it loads the BIOS into memory and executes it
|
|
|
|
//
|
|
|
|
// * the BIOS intializes devices, sets of the interrupt routines, and
|
|
|
|
// reads the first sector of the boot device(e.g., hard-drive)
|
|
|
|
// into memory and jumps to it.
|
|
|
|
//
|
|
|
|
// * Assuming this boot loader is stored in the first sector of the
|
|
|
|
// hard-drive, this code takes over...
|
|
|
|
//
|
|
|
|
// * control starts in bootloader.S -- which sets up protected mode,
|
|
|
|
// and a stack so C code then run, then calls cmain()
|
|
|
|
//
|
2006-09-06 21:08:14 +02:00
|
|
|
// * cmain() in this file takes over,
|
|
|
|
// reads in the kernel and jumps to it.
|
|
|
|
|
|
|
|
#include "types.h"
|
|
|
|
#include "elf.h"
|
|
|
|
#include "x86.h"
|
2006-06-12 17:22:12 +02:00
|
|
|
|
2006-09-06 19:04:06 +02:00
|
|
|
#define SECTSIZE 512
|
2006-09-06 19:27:19 +02:00
|
|
|
#define ELFHDR ((struct elfhdr*) 0x10000) // scratch space
|
2006-06-12 17:22:12 +02:00
|
|
|
|
2006-07-20 11:07:53 +02:00
|
|
|
void readsect(void*, uint);
|
|
|
|
void readseg(uint, uint, uint);
|
2006-06-12 17:22:12 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
cmain(void)
|
|
|
|
{
|
2006-09-06 19:04:06 +02:00
|
|
|
struct proghdr *ph, *eph;
|
2006-06-12 17:22:12 +02:00
|
|
|
|
2006-09-06 19:04:06 +02:00
|
|
|
// read 1st page off disk
|
|
|
|
readseg((uint) ELFHDR, SECTSIZE*8, 0);
|
2006-06-12 17:22:12 +02:00
|
|
|
|
2006-09-06 19:04:06 +02:00
|
|
|
// is this a valid ELF?
|
2006-09-06 19:27:19 +02:00
|
|
|
if(ELFHDR->magic != ELF_MAGIC)
|
2006-09-06 19:04:06 +02:00
|
|
|
goto bad;
|
2006-06-12 17:22:12 +02:00
|
|
|
|
2006-09-06 19:04:06 +02:00
|
|
|
// load each program segment (ignores ph flags)
|
2006-09-06 19:27:19 +02:00
|
|
|
ph = (struct proghdr*) ((uchar*) ELFHDR + ELFHDR->phoff);
|
2006-09-06 19:04:06 +02:00
|
|
|
eph = ph + ELFHDR->phnum;
|
2006-09-06 19:27:19 +02:00
|
|
|
for(; ph < eph; ph++)
|
2006-09-06 19:04:06 +02:00
|
|
|
readseg(ph->va, ph->memsz, ph->offset);
|
2006-06-12 17:22:12 +02:00
|
|
|
|
2006-09-06 19:04:06 +02:00
|
|
|
// call the entry point from the ELF header
|
|
|
|
// note: does not return!
|
2006-09-06 19:27:19 +02:00
|
|
|
((void(*)(void)) (ELFHDR->entry & 0xFFFFFF))();
|
2006-06-12 17:22:12 +02:00
|
|
|
|
|
|
|
bad:
|
2006-09-06 19:04:06 +02:00
|
|
|
outw(0x8A00, 0x8A00);
|
|
|
|
outw(0x8A00, 0x8E00);
|
2006-09-06 20:47:51 +02:00
|
|
|
for(;;)
|
2006-09-06 19:50:20 +02:00
|
|
|
;
|
2006-06-12 17:22:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read 'count' bytes at 'offset' from kernel into virtual address 'va'.
|
|
|
|
// Might copy more than asked
|
|
|
|
void
|
2006-07-20 11:07:53 +02:00
|
|
|
readseg(uint va, uint count, uint offset)
|
2006-06-12 17:22:12 +02:00
|
|
|
{
|
2006-09-06 19:04:06 +02:00
|
|
|
uint end_va;
|
|
|
|
|
|
|
|
va &= 0xFFFFFF;
|
|
|
|
end_va = va + count;
|
2006-09-06 19:27:19 +02:00
|
|
|
|
2006-09-06 19:04:06 +02:00
|
|
|
// round down to sector boundary
|
|
|
|
va &= ~(SECTSIZE - 1);
|
|
|
|
|
|
|
|
// translate from bytes to sectors, and 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.
|
2006-09-06 19:27:19 +02:00
|
|
|
while(va < end_va) {
|
2006-09-06 19:04:06 +02:00
|
|
|
readsect((uchar*) va, offset);
|
|
|
|
va += SECTSIZE;
|
|
|
|
offset++;
|
|
|
|
}
|
2006-06-12 17:22:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
waitdisk(void)
|
|
|
|
{
|
2006-09-06 19:04:06 +02:00
|
|
|
// wait for disk reaady
|
2006-09-06 19:27:19 +02:00
|
|
|
while((inb(0x1F7) & 0xC0) != 0x40)
|
2006-09-06 19:50:20 +02:00
|
|
|
;
|
2006-06-12 17:22:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-07-20 11:07:53 +02:00
|
|
|
readsect(void *dst, uint offset)
|
2006-06-12 17:22:12 +02:00
|
|
|
{
|
2006-09-06 19:04:06 +02:00
|
|
|
// wait for disk to be ready
|
|
|
|
waitdisk();
|
2006-06-12 17:22:12 +02:00
|
|
|
|
2006-09-06 19:04:06 +02:00
|
|
|
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 17:22:12 +02:00
|
|
|
|
2006-09-06 19:04:06 +02:00
|
|
|
// wait for disk to be ready
|
|
|
|
waitdisk();
|
2006-06-12 17:22:12 +02:00
|
|
|
|
2006-09-06 19:04:06 +02:00
|
|
|
// read a sector
|
|
|
|
insl(0x1F0, dst, SECTSIZE/4);
|
2006-06-12 17:22:12 +02:00
|
|
|
}
|
|
|
|
|