基于Grub 2.00的x86内核引导流程--源代码情景分析(2)
来源:互联网 发布:php广告系统 编辑:程序博客网 时间:2024/05/16 11:50
2. 基于Grub2.00的x86内核引导流程
有了磁盘简介的铺垫,现切入主题Grub 2.00内核引导源代码分析。整个分析过程基本都是一个逆向分析的过程,先介绍其用法及相关描述[1],然后分析Makefile以确定生成某个可执行文件的依赖链,从而得出其对应的源文件,最后分析源文件解析其动作。
[1]对描述中涉及到英文的部分,为保留原意不致扭曲而不做翻译。毕竟在GNU Grub和Linux内核源代码中,所有文档注释都是英文描述。所以,英语,x86汇编语言,Makefile是内核开发过程中绕不过去的坎。
- Stage 1: boot.img is stored in the master bootrecord (MBR) or optionally in any of thevolume boot records (VBRs), because a PC bootsector is 512 bytes, the size of boot.img is exactly 512 bytes. And it addressesthe next stage by an LBA48 address (For LBA 48bit addressing, the1024-cylinder limitation of GRUB legacy is avoided); the sole function of`boot.img' is to load the first sector of core.img from a local disk andjump to it.
- Stage 1.5: core.img is by default written to thesectors between the MBR and the first partition, when these sectors are freeand available. For legacy reasons, the first partition of a hard drive does notbegin at sector 1 (counting begins with 0) but at sector 63, leaving a gap of62 sectors of empty space. That space is not part of any partition or filesystem, and therefore not prone to any problems related with it. Onceexecuted, core.img will load its configuration file and any othermodules needed, particularly file system drivers; at installation time, it is generatedfrom diskboot.img and configured to load the stage 2 byits file path.
- Stage 2: files belonging to the stage 2 areall being held in the /boot/grub directory, which is a subdirectory ofthe /boot directory specified by the Filesystem Hierarchy Standard (FHS).
Grub的x86源代码主要分为四个部分,分别为位于目录grub-core/boot/i386/pc下的boot.S, diskboot.S, startup_raw.S,以及grub-core/kern目录下的Grub main.c主模块相关代码 。
2.1 Stage 1: boot.img分析
GNU的软件工程基本都依赖于Makefile去描述一个可执行文件的来龙去脉,由其编译过程可以整理出某个可执行文件的诞生过程:由哪些源文件编译,然后链接哪些库,以及还可能额外存在的二进制文件操作,比如objcopy,strip等等。为了弄清楚boot.img的来龙去脉,需要理顺boot.img在Makefile中的依赖链:
由Makefile中的依赖链条可知:MBR中存放的bootloader的源代码就是boot.S,当然在编译过程中还涉及到一些其他细节,在此就不再展开。
先来分析boot.S。我原本打算摘一些重要的代码片段出来分析,但是之后发现boot.S中的代码是一个一气呵成的执行流,彼此之间相互铺垫但又甚是简洁明了。鉴于boot.S是MBR中的bootloader,我们有时候好奇在加电开机的瞬间到底发生了什么,操作系统到底是怎么一步步被引导起来的,下面就是加电开机“宇宙大爆炸”的瞬间:
结论:MBR中的boot.S,其唯一功能是加载硬盘第二个扇区的内容到内存中,并跳转至其入口函数继续执行。如是更加深入理解之前Stage1中对boot.img的描述:”the sole function of `boot.img' is to load the first sectorof core.img from a local disk and jump to it”。
- 基于Grub 2.00的x86内核引导流程--源代码情景分析(2)
- 基于Grub 2.00的x86内核引导流程--源代码情景分析(1)
- 系统管理指南:基本管理 第11 章• x86: 基于GRUB 的引导(任务)
- Linux内核源代码情景分析-基于socket的进程间通信
- linux内核源代码情景分析
- Linux内核源代码情景分析-外部设备存储空间的地址映射
- Linux内核源代码情景分析-文件系统的安装
- Linux内核源代码情景分析-文件系统安装后的访问
- Linux内核源代码情景分析-文件的打开
- Linux内核源代码情景分析-文件的写
- 关于Linux内核源代码情景分析的点点滴滴
- Linux 内核源代码情景分析 chap 2 存储管理(一)
- Linux 内核源代码情景分析 chap 2 存储管理 (三)
- Linux 内核源代码情景分析 chap 2 存储管理 (四)
- Linux内存管理的基本框架(Linux内核源代码情景分析读书笔记连载)
- Linux地址映射的全过程(Linux内核源代码情景分析读书笔记连载#)
- 基于x86的Redboot启动流程分析
- x86 linux系统内核引导流程梳理
- CSS学习笔记二:css 画立体图形
- html javascript函数(一)
- xman day03 pwn海无涯,回头是岸
- jdk 源码分析(11)java ArrayBlockingQueue 缓存队列分析
- 基于libpcan库can总线操作的Barrett 机械手控制及腕部六维力传感器驱动
- 基于Grub 2.00的x86内核引导流程--源代码情景分析(2)
- QML 设计自定义样式CheckBox,可改变字体大小
- 机器学习有感
- JAVA Ftp编程--远程下载文件
- 【剑指offer】面试题58(2):左旋转字符串
- 素数筛选法打表模板
- 【面试】2017.8.3菜鸟内推第三面
- ZCMU新人训练赛I
- 设计模式之观察者模式