在GRUB2中,最开始的初始化行为是加载image handle。
参考:bootimage-example/GRUB_ARCHITECTURE_AND_INIT.md at main · liweinan/bootimage-example · GitHub
Image文件的加载
在/core/kern/efi/efi.c文件中,最终追踪下去就是grub efi open protocol函数作为整个efi初始化的最开始,执行对image handle的加载程序。
grub_efi_open_protocol (grub_efi_handle_t handle, grub_guid_t *protocol, grub_efi_uint32_t attributes)
{
grub_efi_boot_services_t *b; // 这里需要解释一下,这个boot services起始就是系统表里的boot services,这是一个很大的表,目前在这个模块中可以看作是进行内存管理的
grub_efi_status_t status; // 就是一个uint64
viod *interface
b = grub_efi_system_table->boot_services;
status = b->open_protocol (handle,protocol,&interface,grub_efi_image_handle,0,attributes);
if (status != GRUB_EFI_SUCCESS)
return 0;
return interface;
}可以说,这里是调用了boot services中的open protocol函数,即下面这个东西:

所谓的open protocol的主要意思,应当理解为将一段二进制代码搬运到内存中去的过程。
所谓的GUID,其实是image handle标识。
GRUB_EFI_LOADED_IMAGE_GUID的定义如下
#define GRUB_EFI_LOADED_IMAGE_GUID \
{ 0x5b1b31a1, 0x9562, 0x11d2, \
{ 0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b} \
}这里的这个GUID,按照目前浅显的理解来说,就是GRUB本地生成的image中,会规定一个GUID为这个值,然后在执行init的过程中,会以这个值去检查。