Boot Loader即引导程序,它在BIOS执行完毕后被执行,它的代码在JOS中由两部分组成,boot.S汇编语言文本文件和main.c的C语言文本文件,之前说到汇编代码跳转到叫做bootmain的地方,它是main.c的一个函数:
void
bootmain(void)
{
struct Proghdr *ph, *eph;
// read 1st page off disk
readseg((uint32_t) ELFHDR, SECTSIZE*8, 0);
// is this a valid ELF?
if (ELFHDR->e_magic != ELF_MAGIC)
goto bad;
// load each program segment (ignores ph flags)
ph = (struct Proghdr *) ((uint8_t *) ELFHDR + ELFHDR->e_phoff);
eph = ph + ELFHDR->e_phnum;
for (; ph < eph; ph++)
// p_pa is the load address of this segment (as well
// as the physical address)
readseg(ph->p_pa, ph->p_memsz, ph->p_offset);
// call the entry point from the ELF header
// note: does not return!
((void (*)(void)) (ELFHDR->e_entry))();
bad:
outw(0x8A00, 0x8A00);
outw(0x8A00, 0x8E00);
while (1)
/* do nothing */;
}
粗略地看这个函数,它的功能正如注释所说,是read segment,就是读取磁盘的一段数据,readseg这个函数的具体实现不再描述,但是简单研究会发现,它

本文深入分析了计算机的启动流程,以JOS操作系统为例,讲解了从boot loader读取kernel到内核启动的过程。内容涉及readseg函数的原始I/O实现、内核加载地址、Elf文件解析、页表设置以及汇编代码在开启分页和进程切换中的作用。通过理解这些步骤,读者可以更好地了解操作系统如何从启动到运行内核的整个过程。
最低0.47元/天 解锁文章
1021

被折叠的 条评论
为什么被折叠?



