|
本帖最后由 wintoflash 于 2021-4-16 22:01 编辑
你是怎么内置的,直接 include 吗?
我感觉可以用 grub-mkimage 生成 bootx64.efi 的时候,把内置菜单和字库插进去,发布的时候给用户提供 kernel.img,这样就能让用户自定义内置菜单了。
grub-mkimage64 -d $GRUB4DOS_TEMP -p /GRUB2 -o BOOTX64.EFI -O x86_64-efi -c menu.lst
GRUB2 里面是这样实现读取内置菜单的:
- enum
- {
- OBJ_TYPE_ELF,
- OBJ_TYPE_MEMDISK,
- OBJ_TYPE_CONFIG,
- OBJ_TYPE_PREFIX,
- OBJ_TYPE_PUBKEY,
- OBJ_TYPE_DTB
- };
- /* The module header. */
- struct grub_module_header
- {
- /* The type of object. */
- grub_uint32_t type;
- /* The size of object (including this header). */
- grub_uint32_t size;
- };
- /* "gmim" (GRUB Module Info Magic). */
- #define GRUB_MODULE_MAGIC 0x676d696d
- struct grub_module_info32
- {
- /* Magic number so we know we have modules present. */
- grub_uint32_t magic;
- /* The offset of the modules. */
- grub_uint32_t offset;
- /* The size of all modules plus this header. */
- grub_uint32_t size;
- };
- struct grub_module_info64
- {
- /* Magic number so we know we have modules present. */
- grub_uint32_t magic;
- grub_uint32_t padding;
- /* The offset of the modules. */
- grub_uint64_t offset;
- /* The size of all modules plus this header. */
- grub_uint64_t size;
- };
- #if GRUB_TARGET_SIZEOF_VOID_P == 8
- #define grub_module_info grub_module_info64
- #else
- #define grub_module_info grub_module_info32
- #endif
- #define FOR_MODULES(var) for (\
- var = (grub_modbase && ((((struct grub_module_info *) grub_modbase)->magic) == GRUB_MODULE_MAGIC)) ? (struct grub_module_header *) \
- (grub_modbase + (((struct grub_module_info *) grub_modbase)->offset)) : 0;\
- var && (grub_addr_t) var \
- < (grub_modbase + (((struct grub_module_info *) grub_modbase)->size)); \
- var = (struct grub_module_header *) \
- (((grub_uint32_t *) var) + ((((struct grub_module_header *) var)->size + sizeof (grub_addr_t) - 1) / sizeof (grub_addr_t)) * (sizeof (grub_addr_t) / sizeof (grub_uint32_t))))
- /* Search the mods section from the PE32/PE32+ image. This code uses
- a PE32 header, but should work with PE32+ as well. */
- grub_addr_t
- grub_efi_modules_addr (void)
- {
- grub_efi_loaded_image_t *image;
- struct grub_pe32_header *header;
- struct grub_pe32_coff_header *coff_header;
- struct grub_pe32_section_table *sections;
- struct grub_pe32_section_table *section;
- struct grub_module_info *info;
- grub_uint16_t i;
- image = grub_efi_get_loaded_image (grub_efi_image_handle);
- if (! image)
- return 0;
- header = image->image_base;
- coff_header = &(header->coff_header);
- sections
- = (struct grub_pe32_section_table *) ((char *) coff_header
- + sizeof (*coff_header)
- + coff_header->optional_header_size);
- for (i = 0, section = sections;
- i < coff_header->num_sections;
- i++, section++)
- {
- if (grub_strcmp (section->name, "mods") == 0)
- break;
- }
- if (i == coff_header->num_sections)
- {
- grub_dprintf("sections", "section %d is last section; invalid.\n", i);
- return 0;
- }
- info = (struct grub_module_info *) ((char *) image->image_base
- + section->virtual_address);
- if (section->name[0] != '.' && info->magic != GRUB_MODULE_MAGIC)
- {
- grub_dprintf("sections",
- "section %d has bad magic %08x, should be %08x\n",
- i, info->magic, GRUB_MODULE_MAGIC);
- return 0;
- }
- grub_dprintf("sections", "returning section info for section %d: "%s"\n",
- i, section->name);
- return (grub_addr_t) info;
- }
- grub_addr_t grub_modbase = grub_efi_modules_addr ();
- static char *load_config;
- struct grub_module_header *header;
- FOR_MODULES (header)
- {
- /* Not an embedded config, skip. */
- if (header->type != OBJ_TYPE_CONFIG)
- continue;
- load_config = grub_malloc (header->size - sizeof (struct grub_module_header) + 1);
- if (!load_config)
- {
- grub_print_error ();
- break;
- }
- grub_memcpy (load_config, (char *) header + sizeof (struct grub_module_header),
- header->size - sizeof (struct grub_module_header));
- load_config[header->size - sizeof (struct grub_module_header)] = 0;
- break;
- }
复制代码
|
|