主要涉及磁盘的读写操作和ELF文件的解析

内核

在这里插入图片描述

entry是内核的入口, _start是内核入口地址, ebp指向栈底, 也就是调用链的起始地址
在这里插入图片描述
分配内存作为堆栈, 将高地址赋给esp栈顶寄存器

磁盘访问

在这里插入图片描述
使用第一个磁盘控制器的两个磁盘

void waitdisk(void)
{
   
	// wait for disk reaady
	while ((inb(0x1F7) & 0xC0) != 0x40)
		/* do nothing */;
}

轮询等待磁盘准备完毕

void readsect(void *dst, uint32_t offset)
{
   
	// wait for disk to be ready
	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

	// wait for disk to be ready
	waitdisk();

	// read a sector
	insl(0x1F0, dst, SECTSIZE/4);
}

从磁盘上读取一个扇区的内容到dst地址处, offset是要读取第几个扇区, oub是IO指令, 就是向IO端口写入一个字节数据

	outb(0x1F3, offset);
	outb(0x1F4, offset >> 8);
	outb(0x1F5, offset >> 16);
	outb(0x1F6, (offset >> 24) | 0xE0);

这里指定扇区位置

	insl(0x1F0, dst, SECTSIZE/4);

每次读取4个字节, 最后一个参数是读取的次数

读取Segment

在这里插入图片描述
一个Segment可以包含多个Section, 例如.text就是在Segment0

在这里插入图片描述
程序头表描述一个Segment的属性信息, 比如在文件中的便宜, 物理地址和虚拟地址, 大小

void readseg(uint32_t pa, uint32_t count, uint32_t offset)
{
   
	uint32_t end_pa;

	end_pa = pa + count;

	// round down to sector boundary
	pa &= ~(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.
	while (pa < end_pa) {
   
		// Since we haven't enabled paging yet and we're using
		// an identity segment mapping (see boot.S), we can
		// use physical addresses directly. This won't be the
		// case once JOS enables the MMU.
		readsect((uint8_t*) pa, offset);
		pa += SECTSIZE;
		offset++;
	}
}

根据属性信息读取一个Segment内容

在这里插入图片描述
程序头表的结构体
在这里插入图片描述
程序头的结构体
在这里插入图片描述
第一页存放的是ELF头部信息, 也存储了程序头表的起始位置

代码

#include <x86.h>
#include <elf.h>

/********************************************************************** * 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 boot.S -- which sets up protected mode, * and a stack so C code then run, then calls bootmain() * * * bootmain() in this file takes over, reads in the kernel and jumps to it. **********************************************************************/

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wint-to-pointer-cast"
#define SECTSIZE 512
#define ELFHDR ((struct Elf *) 0x10000) // scratch space

void readsect(void*, uint32_t);
void readseg(uint32_t, uint32_t, uint32_t);

void bootmain(void)
{
   
	struct Proghdr *ph, *eph;
	// Lab1-0,your code here
	// 1.read 1st page off disk

	// 2.is this a valid ELF? check magic.

	// 3.load each program segment (ignores ph flags)


	// 4.call the entry point from the ELF header


bad:
	outw(0x8A00, 0x8A00);
	outw(0x8A00, 0x8E00);
	while (1)
		/* do nothing */;
}

// Read 'count' bytes at 'offset' from kernel into physical address 'pa'.
// Might copy more than asked
void readseg(uint32_t pa, uint32_t count, uint32_t offset)
{
   
	uint32_t end_pa;

	end_pa = pa + count;

	// round down to sector boundary
	pa &= ~(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.
	while (pa < end_pa) {
   
		// Since we haven't enabled paging yet and we're using
		// an identity segment mapping (see boot.S), we can
		// use physical addresses directly. This won't be the
		// case once JOS enables the MMU.
		readsect((uint8_t*) pa, offset);
		pa += SECTSIZE;
		offset++;
	}
}

void waitdisk(void)
{
   
	// wait for disk reaady
	while ((inb(0x1F7) & 0xC0) != 0x40)
		/* do nothing */;
}

void readsect(void *dst, uint32_t offset)
{
   
	// wait for disk to be ready
	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

	// wait for disk to be ready
	waitdisk();

	// read a sector
	insl(0x1F0, dst, SECTSIZE/4);
}


#pragma clang diagnostic pop