📈
ucore-analysis
  • Introduction
  • lab1
    • boot
      • bootasm
      • bootmain
    • kern
      • debug
        • kmonitor
        • panic
      • init
        • init
      • libs
        • readline
      • mm
        • pmm
      • trap
        • trap
        • trapentry
        • vectors
    • libs
    • tools
  • lab解析
    • lab1
      • 练习1
      • 练习2
      • 练习3
      • 练习4
      • 练习6
      • 扩展练习
      • Piazza优质问题/笔记收集
    • lab2
      • 练习1
      • 练习2
      • 练习3
    • lab3
    • lab4
    • lab5
    • lab6
    • lab7
    • lab8
  • uCore代码
    • boot
      • asm.h
      • bootasm.S
      • bootmain.c
      • (lab1) bootasm.S
    • kern
      • debug
        • assert.h
        • kdebug.c
        • kdebug.h
        • kmonitor.c
        • kmonitor.h
        • panic.c
        • stab.h
        • (lab1) kdebug.c
      • driver
        • clock.c
        • clock.h
        • console.c
        • console.h
        • ide.c
        • ide.h
        • intr.c
        • intr.h
        • kbdreg.h
        • picirq.c
        • picirq.h
      • fs
        • devs
          • dev.c
          • dev_disk0.c
          • dev.h
          • dev_stdin.c
          • dev_stdout.c
        • sfs
          • bitmap.c
          • bitmap.h
          • sfs.c
          • sfs_fs.c
          • sfs.h
          • sfs_inode.c
          • sfs_io.c
          • sfs_lock.c
        • swap
          • swapfs.c
          • swapfs.h
        • vfs
          • inode.c
          • inode.h
          • README.md
          • vfs.c
          • vfsdev.c
          • vfsfile.c
          • vfs.h
          • vfslookup.c
          • vfspath.c
        • file.c
        • file.h
        • fs.c
        • fs.h
        • iobuf.c
        • iobuf.h
        • sysfile.c
        • sysfile.h
      • init
        • entry.S
        • init.c
        • (lab1) init.c
      • libs
        • readline.c
        • stdio.c
        • string.c
      • mm
        • default_pmm.c
        • default_pmm.h
        • kmalloc.c
        • kmalloc.h
        • memlayout.h
        • mmu.h
        • pmm.c
        • pmm.h
        • swap.c
        • swap_fifo.c
        • swap_fifo.h
        • swap.h
        • vmm.c
        • vmm.h
        • (lab2) pmm.c
        • (lab3) vmm.c
      • process
        • entry.S
        • proc.c
        • proc.h
        • switch.S
        • (lab4) proc.c
        • (lab5) proc.c
      • schedule
        • default_sched.c
        • default_sched.h
        • default_sched_stride.c
        • sched.c
        • sched.h
      • sync
        • check_sync.c
        • monitor.c
        • monitor.h
        • sem.c
        • sem.h
        • sync.h
        • wait.c
        • wait.h
      • syscall
        • syscall.c
        • syscall.h
      • trap
        • trap.c
        • trapentry.S
        • trap.h
        • vectors.S
        • (lab1) trap.c
    • libs
      • atomic.h
      • defs.h
      • dirent.h
      • elf.h
      • error.h
      • hash.c
      • list.h
      • printfmt.c
      • rand.c
      • skew_heap.h
      • stat.h
      • stdarg.h
      • stdio.h
      • stdlib.h
      • string.c
      • string.h
      • unistd.h
      • x86.h
    • tools
      • boot.ld
      • function.mk
      • gdbinit
      • grade.sh
      • kernel.ld
      • mksfs.c
      • sign.c
      • user.ld
      • vector.c
    • user
      • libs
        • dir.c
        • dir.h
        • file.c
        • file.h
        • initcode.S
        • lock.h
        • panic.c
        • stdio.c
        • syscall.c
        • syscall.h
        • ulib.c
        • ulib.h
        • umain.c
      • badarg.c
      • badsegment.c
      • divzero.c
      • exit.c
      • faultread.c
      • faultreadkernel.c
      • forktest.c
      • forktree.c
      • hello.c
      • ls.c
      • matrix.c
      • pgdir.c
      • priority.c
      • sfs_filetest1.c
      • sh.c
      • sleep.c
      • sleepkill.c
      • softint.c
      • spin.c
      • testbss.c
      • waitkill.c
      • yield.c
    • Makefile
    • (lab1) Makefile
  • 附录:工具使用
    • 如何编辑该文档
    • 讨论区的维护方法
    • 使用Travis CI自动化更新gitbook
Powered by GitBook
On this page
  • 练习4:分析bootloader加载ELF格式的OS的过程
  • 相关文件
  • bootloader如何读取硬盘扇区的
  • 背景知识
  • 问题分析
  • 参考文献
  • bootloader是如何加载ELF格式的OS
  • 背景知识
  • 问题分析
  • 参考文献

Was this helpful?

  1. lab解析
  2. lab1

练习4

Previous练习3Next练习6

Last updated 4 years ago

Was this helpful?

练习4:分析bootloader加载ELF格式的OS的过程

  • bootloader如何读取硬盘扇区的

  • bootloader是如何加载ELF格式的OS

相关文件

bootloader如何读取硬盘扇区的

背景知识

  1. 什么是bootloader

    bootloader是为了加载内核而编写的程序。

  2. bootloader的作用是什么

    1. 将内核加载到内存

    2. 向内核提供正常工作所需的信息

    3. 切换到内核所需的环境

    4. 将控制权转移给内核

  3. 什么是扇区

    磁盘上的每个磁道被等分为若干个弧段,这些弧段便是磁盘的扇区。硬盘的读写以扇区为基本单位。

  4. 如何读写扇区

    硬盘相关IO地址及其对应功能:

    0x1f0: 读数据,当0x1f7不为忙状态时,可以读

    0x1f2: 要读写的扇区数,每次读写前,你需要表明你要读写几个扇区。最小是1个扇区

    0x1f3: 如果是LBA模式,就是LBA参数的0-7位

    0x1f4: 如果是LBA模式,就是LBA参数的8-15位

    0x1f5: 如果是LBA模式,就是LBA参数的16-23位

    0x1f6: 第0~3位:如果是LBA模式就是24-27位 第4位:为0主盘;为1从盘

    0x1f7: 状态和命令寄存器。操作时先给命令,再读取,如果不是忙状态就从0x1f0端口读数据

    读取扇区的流程大致如下:

    1. 等待磁盘准备好

    2. 发出读取扇区的命令

    3. 等待磁盘准备好

    4. 把磁盘扇区数据读到指定内存

问题分析

  1. bootloader是如何读取硬盘扇区的

    参考以下代码:

    //    File:    bootmain.c
    //    Line:    43-61
    
    /* readsect - read a single sector at @secno into @dst */
    static void
    readsect(void *dst, uint32_t secno) {
        // wait for disk to be ready
        waitdisk();
    
        outb(0x1F2, 1);                         // count = 1
        outb(0x1F3, secno & 0xFF);
        outb(0x1F4, (secno >> 8) & 0xFF);
        outb(0x1F5, (secno >> 16) & 0xFF);
        outb(0x1F6, ((secno >> 24) & 0xF) | 0xE0);
        outb(0x1F7, 0x20);                      // cmd 0x20 - read sectors
    
        // wait for disk to be ready
        waitdisk();
    
        // read a sector
        insl(0x1F0, dst, SECTSIZE / 4);
    }

    其中,waitdisk函数的代码为:

    //    File:    bootmain.c
    //    Line:    36-41
    
    /* waitdisk - wait for disk ready */
    static void
    waitdisk(void) {
        while ((inb(0x1F7) & 0xC0) != 0x40)
            /* do nothing */;
    }

    由以上代码可知,bootloader读取硬盘扇区步骤为

    1. 从0x1F7端口读硬盘状态,直到硬盘不忙

    2. 向0x1F2端口发送读写的扇区数量

    3. 向0x1F3~0x1F6端口发送LBA参数

    4. 向0x1F7端口发送读扇区的指令

    5. 从0x1F7端口读硬盘状态,直到硬盘不忙

    6. 从0x1F0端口读入扇区内容至dst

参考文献

bootloader是如何加载ELF格式的OS

背景知识

  1. ELF是什么

    ELF是类Unix系统上的一种文件格式,常用于二进制文件、可执行文件、目标代码、共享库和核心转储格式文件。

  2. ELF的格式是什么

    参考代码:

    //    File:    elf.h
    //    Line:    8-25
    
    /* file header */
    struct elfhdr {
        uint32_t e_magic;     // must equal ELF_MAGIC
        uint8_t e_elf[12];
        uint16_t e_type;      // 1=relocatable, 2=executable, 3=shared object, 4=core image
        uint16_t e_machine;   // 3=x86, 4=68K, etc.
        uint32_t e_version;   // file version, always 1
        uint32_t e_entry;     // entry point if executable
        uint32_t e_phoff;     // file position of program header or 0
        uint32_t e_shoff;     // file position of section header or 0
        uint32_t e_flags;     // architecture-specific flags, usually 0
        uint16_t e_ehsize;    // size of this elf header
        uint16_t e_phentsize; // size of an entry in program header
        uint16_t e_phnum;     // number of entries in program header or 0
        uint16_t e_shentsize; // size of an entry in section header
        uint16_t e_shnum;     // number of entries in section header or 0
        uint16_t e_shstrndx;  // section number that contains section name strings
    };

问题分析

  1. bootloader是如何加载ELF格式的OS

    参考代码:

    //    File:    bootmain.c
    //    Line:    85-115
    
    /* bootmain - the entry of bootloader */
    void
    bootmain(void) {
        // read the 1st page off disk
        readseg((uintptr_t)ELFHDR, SECTSIZE * 8, 0);
    
        // is this a valid ELF?
        if (ELFHDR->e_magic != ELF_MAGIC) {
            goto bad;
        }
    
        struct proghdr *ph, *eph;
    
        // load each program segment (ignores ph flags)
        ph = (struct proghdr *)((uintptr_t)ELFHDR + ELFHDR->e_phoff);
        eph = ph + ELFHDR->e_phnum;
        for (; ph < eph; ph ++) {
            readseg(ph->p_va & 0xFFFFFF, ph->p_memsz, ph->p_offset);
        }
    
        // call the entry point from the ELF header
        // note: does not return
        ((void (*)(void))(ELFHDR->e_entry & 0xFFFFFF))();
    
    bad:
        outw(0x8A00, 0x8A00);
        outw(0x8A00, 0x8E00);
    
        /* do nothing */
        while (1);
    }

    由代码可知,bootloader加载OS的流程为:

    1. 将硬盘第一个扇区的内容读入ELFHDR

    2. 验证ELFHDR是否为合格的ELF格式

    3. 加载ELF的每个程序段

    4. 调用ELF头内的OS入口,加载OS成功

参考文献

练习4文档
相关阅读:硬盘访问概述
相关阅读:ELF文件格式概述
bootmain.c
elf.h
硬盘访问概述
Bootloader
ELF文件格式概述
ELF
关于lab1中的bootloader和ucore的连接问题