grub源码分析之diskboot.img

同上篇boot.img生成过程类似,不再一一分析,仅记录编译过程log。

https://blog.csdn.net/baidu_31504167/article/details/89136605

第一步:根据diskboot.S生成diskboot_image-diskboot.o文件。
【编译过程log如下】
gcc -DHAVE_CONFIG_H -I. -I…/…/grub-core -I… -Wall -W -DGRUB_MACHINE_PCBIOS=1 -DGRUB_MACHINE=I386_PC -m32 -nostdinc -isystem /usr/lib/gcc/x86_64-linux-gnu/4.8/include -I…/…/include -I…/include -DGRUB_FILE=“boot/i386/pc/diskboot.S” -I. -I…/…/grub-core -I… -I…/… -I…/…/include -I…/include -I…/…/grub-core/lib/libgcrypt-grub/src/ -D_FILE_OFFSET_BITS=64 -g -m32 -msoft-float -DGRUB_FILE=“boot/i386/pc/diskboot.S” -I. -I…/…/grub-core -I… -I…/… -I…/…/include -I…/include -I…/…/grub-core/lib/libgcrypt-grub/src/ -DASM_FILE=1 -MT boot/i386/pc/diskboot_image-diskboot.o -MD -MP -MF boot/i386/pc/.deps-core/diskboot_image-diskboot.Tpo -c -o boot/i386/pc/diskboot_image-diskboot.o `test -f ‘boot/i386/pc/diskboot.S’ || echo ‘…/…/grub-core/’`boot/i386/pc/diskboot.S

第二步:将diskboot_image-diskboot.o文件链接成diskboot.image文件。
链接选项中最重要的是将代码段定义在内存的绝对地址0x8000上。
【编译过程log如下】
gcc -Os -Wall -W -Wshadow -Wpointer-arith -Wundef -Wchar-subscripts -Wcomment -Wdeprecated-declarations -Wdisabled-optimization -Wdiv-by-zero -Wfloat-equal -Wformat-extra-args -Wformat-security -Wformat-y2k -Wimplicit -Wimplicit-function-declaration -Wimplicit-int -Wmain -Wmissing-braces -Wmissing-format-attribute -Wmultichar -Wparentheses -Wreturn-type -Wsequence-point -Wshadow -Wsign-compare -Wswitch -Wtrigraphs -Wunknown-pragmas -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value -Wunused-variable -Wwrite-strings -Wnested-externs -Wstrict-prototypes -g -Wredundant-decls -Wmissing-prototypes -Wmissing-declarations -Wextra -Wattributes -Wendif-labels -Winit-self -Wint-to-pointer-cast -Winvalid-pch -Wmissing-field-initializers -Wnonnull -Woverflow -Wvla -Wpointer-to-int-cast -Wstrict-aliasing -Wvariadic-macros -Wvolatile-register-var -Wpointer-sign -Wmissing-include-dirs -Wmissing-prototypes -Wmissing-declarations -Wformat=2 -march=i386 -m32 -mrtd -mregparm=3 -falign-jumps=1 -falign-loops=1 -falign-functions=1 -freg-struct-return -mno-mmx -mno-sse -mno-sse2 -mno-sse3 -mno-3dnow -msoft-float -fno-dwarf2-cfi-asm -mno-stack-arg-probe -fno-asynchronous-unwind-tables -fno-unwind-tables -Qn -fno-stack-protector -Wtrampolines -Werror -fno-builtin -m32 -Wl,-melf_i386 -Wl,–build-id=none -nostdlib -Wl,-N -Wl,-S -Wl,-N -Wl,-Ttext,0x8000 -o diskboot.image boot/i386/pc/diskboot_image-diskboot.o

第三步:利用objcopy命令根据diskboot.image生成diskboot.img文件。
其中利用-R参数删除一些不必要的section。
【编译过程log如下】
if test x0 = x1; then …/grub-macho2img diskboot.image diskboot.img; else objcopy -O binary --strip-unneeded -R .note -R .comment -R .note.gnu.build-id -R .MIPS.abiflags -R .reginfo -R .rel.dyn -R .note.gnu.gold-version -R .ARM.exidx diskboot.image diskboot.img; fi

/boot/grub2/i386-pc/boot.img 文件是一个针对x86架构的引导镜像文件,它包含了用于引导启动计算机的程序和参数等信息。 以下是/boot/grub2/i386-pc/boot.img文件的源码注释: ``` /* * This file contains the source code for the x86 version of the GRUB * boot loader. It is responsible for loading the operating system kernel * from the hard disk and transferring control to it. */ #include <grub/types.h> #include <grub/i386/pc/boot.h> #include <grub/i386/pc/loadfile.h> /* * The main function of the boot loader. It is called by the BIOS when the * computer is powered on or reset. It initializes the hardware, loads the * kernel from the hard disk, and transfers control to it. */ int main (void) { /* Initialize the hardware */ init_hw (); /* Load the kernel from the hard disk */ if (load_kernel () != 0) { /* Error loading the kernel */ return 1; } /* Transfer control to the kernel */ transfer_control (); /* Should never get here */ return 0; } /* * Initializes the hardware, including the console output and disk system. */ void init_hw (void) { /* Initialize the console */ init_console (); /* Initialize the disk system */ init_disk (); } /* * Loads the kernel from the hard disk into memory. */ int load_kernel (void) { /* Read the kernel from the hard disk */ if (read_kernel () != 0) { /* Error reading the kernel */ return 1; } /* Initialize the kernel */ init_kernel (); return 0; } /* * Transfers control to the kernel by jumping to its entry point. */ void transfer_control (void) { /* Jump to the kernel entry point */ jump_to_kernel (); } ``` 以上是x86架构/boot/grub2/i386-pc/boot.img文件的源码和注释。其中,主要包括了引导加载程序的主函数、硬件初始化函数、内核加载函数和转移控制函数等。通过这些函数的调用,boot.img文件可以成功地将计算机引导到操作系统内核。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值