Skip to content
Snippets Groups Projects
bootmain.c 2.21 KiB
Newer Older
rsc's avatar
rsc committed
// Boot loader.
// 
rtm's avatar
rtm committed
// 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.
rsc's avatar
rsc committed

#include "types.h"
#include "elf.h"
#include "x86.h"
Frans Kaashoek's avatar
Frans Kaashoek committed
#include "memlayout.h"
rtm's avatar
rtm committed

#define SECTSIZE  512
rtm's avatar
rtm committed

rsc's avatar
rsc committed
void readseg(uchar*, uint, uint);
rtm's avatar
rtm committed

void
rsc's avatar
rsc committed
bootmain(void)
rtm's avatar
rtm committed
{
rsc's avatar
rsc committed
  struct elfhdr *elf;
  struct proghdr *ph, *eph;
rsc's avatar
rsc committed
  void (*entry)(void);
Frans Kaashoek's avatar
Frans Kaashoek committed
  uchar* pa;
rtm's avatar
rtm committed

rsc's avatar
rsc committed
  elf = (struct elfhdr*)0x10000;  // scratch space
rtm's avatar
rtm committed

rsc's avatar
rsc committed
  // Read 1st page off disk
rsc's avatar
rsc committed
  readseg((uchar*)elf, 4096, 0);
rsc's avatar
rsc committed

  // Is this an ELF executable?
  if(elf->magic != ELF_MAGIC)
rsc's avatar
rsc committed
    return;  // let bootasm.S handle error
rtm's avatar
rtm committed

rsc's avatar
rsc committed
  // Load each program segment (ignores ph flags).
  ph = (struct proghdr*)((uchar*)elf + elf->phoff);
  eph = ph + elf->phnum;
Russ Cox's avatar
Russ Cox committed
  for(; ph < eph; ph++){
    pa = (uchar*)ph->paddr;
    readseg(pa, ph->filesz, ph->off);
rsc's avatar
rsc committed
    if(ph->memsz > ph->filesz)
Frans Kaashoek's avatar
Frans Kaashoek committed
      stosb(pa + ph->filesz, 0, ph->memsz - ph->filesz);
rsc's avatar
rsc committed
  }
rtm's avatar
rtm committed

rsc's avatar
rsc committed
  // Call the entry point from the ELF header.
  // Does not return!
Frans Kaashoek's avatar
Frans Kaashoek committed
  entry = (void(*)(void))(elf->entry & 0xFFFFFF);
rsc's avatar
rsc committed
  entry();
rtm's avatar
rtm committed
}

void
waitdisk(void)
{
rsc's avatar
rsc committed
  // Wait for disk ready.
rsc's avatar
rsc committed
  while((inb(0x1F7) & 0xC0) != 0x40)
rsc's avatar
rsc committed
    ;
rtm's avatar
rtm committed
}

rsc's avatar
 
rsc committed
// Read a single sector at offset into dst.
rtm's avatar
rtm committed
void
rtm's avatar
rtm committed
readsect(void *dst, uint offset)
rtm's avatar
rtm committed
{
rsc's avatar
rsc committed
  // Issue command.
  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
rtm's avatar
rtm committed

rsc's avatar
rsc committed
  // Read data.
  waitdisk();
  insl(0x1F0, dst, SECTSIZE/4);
rtm's avatar
rtm committed
}

// Read 'count' bytes at 'offset' from kernel into physical address 'pa'.
rsc's avatar
 
rsc committed
// Might copy more than asked.
void
readseg(uchar* pa, uint count, uint offset)
rsc's avatar
 
rsc committed
{
rsc's avatar
 
rsc committed

rsc's avatar
 
rsc committed

rsc's avatar
rsc committed
  // Round down to sector boundary.
rsc's avatar
 
rsc committed

rsc's avatar
rsc committed
  // Translate from bytes to sectors; kernel starts at sector 1.
rsc's avatar
 
rsc committed
  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.
  for(; pa < epa; pa += SECTSIZE, offset++)
    readsect(pa, offset);
rsc's avatar
 
rsc committed
}