2006-04-30 Yoshinori K. Okuji <okuji@enbug.org>

Extend the loader so that GRUB can accept a loader which comes
        back to GRUB when a loaded image exits. Also, this change adds
        support for a chainloader on EFI.

        * term/efi/console.c: Include grub/misc.h.
        (grub_console_checkkey): Display a scan code on the top for
        debugging. This will be removed once the EFI port gets stable.
        Correct the scan code mapping.

        * kern/efi/mm.c (sort_memory_map): Sort in a descending order to
        allocate memory from larger regions, in order to reduce the number
        of allocated regions. Otherwise, the MacOSX loader panics.
        (filter_memory_map): Avoid less than 1MB for compatibility with
        other loaders.
        (add_memory_regions): Allocate from the tail of a region, if
        possible, to avoid allocating a region near to 1MB, for the MacOSX
        loader.

        * kern/efi/init.c (grub_efi_set_prefix): Specify
        GRUB_EFI_IMAGE_HANDLE to grub_efi_get_loaded_image.

        * kern/efi/efi.c (grub_efi_get_loaded_image): Accept a new
        argument IMAGE_HANDLE and specify it to get a loaded image.
        (grub_arch_modules_addr): Specify GRUB_EFI_IMAGE_HANDLE to
        grub_efi_get_loaded_image.
        (grub_efi_get_filename): Divide the legnth by the size of
        grub_efi_char16_t.
        (grub_efi_get_device_path): New function.
        (grub_efi_print_device_path): Print End Device Path nodes. Divide
        the length by the size of grub_efi_char16_t for a file path device
        path node.

        * kern/loader.c (grub_loader_noreturn): New variable.
        (grub_loader_set): Accept a new argument NORETURN. Set
        GRUB_LOADER_NORETURN to NORETURN.
        All callers changed.
        (grub_loader_boot): If GRUB_LOADER_NORETURN is false, do not call
        grub_machine_fini.

        * include/grub/efi/efi.h (grub_efi_get_device_path): New
        prototype.
        (grub_efi_get_loaded_image): Take an argument to specify an image
        handle.

        * include/grub/loader.h (grub_loader_set): Added one more argument
        NORETURN.

        * disk/efi/efidisk.c (make_devices): Use grub_efi_get_device_path
        instead of grub_efi_open_protocol.
        (grub_efidisk_get_device_name): Likewise.
        (grub_efidisk_close): Print a newline.
        (grub_efidisk_get_device_handle): Fixed to use
        GRUB_EFI_DEVICE_PATH_SUBTYPE instead of
        GRUB_EFI_DEVICE_PATH_TYPE.

        * disk/efi/efidisk.c (device_path_guid): Moved to ...
        * kern/efi/efi.c (device_path_guid): ... here.

        * conf/i386-efi.rmk (pkgdata_MODULES): Added _chain.mod and
        chain.mod.
        (kernel_mod_HEADERS): Added efi/disk.h.
        (_chain_mod_SOURCES): New variable.
        (_chain_mod_CFLAGS): Likewise.
        (_chain_mod_LDFLAGS): Likewise.
        (chain_mod_SOURCES): Likewise.
        (chain_mod_CFLAGS): Likewise.
        (chain_mod_LDFLAGS): Likewise.

        * DISTLIST: Added include/grub/efi/chainloader.h,
        loader/efi/chainloader.c and loader/efi/chainloader_normal.c.

        * include/grub/efi/chainloader.h: New file.
        * loader/efi/chainloader.c: Likewise.
        * loader/efi/chainloader_normal.c: Likewise.
This commit is contained in:
okuji 2006-04-30 21:09:37 +00:00
parent c0111d6e92
commit 7f362539b7
19 changed files with 743 additions and 64 deletions

View file

@ -184,23 +184,23 @@ sort_memory_map (grub_efi_memory_descriptor_t *memory_map,
d1 < memory_map_end;
d1 = NEXT_MEMORY_DESCRIPTOR (d1, desc_size))
{
grub_efi_memory_descriptor_t *min_desc = d1;
grub_efi_memory_descriptor_t *max_desc = d1;
for (d2 = NEXT_MEMORY_DESCRIPTOR (d1, desc_size);
d2 < memory_map_end;
d2 = NEXT_MEMORY_DESCRIPTOR (d2, desc_size))
{
if (min_desc->num_pages > d2->num_pages)
min_desc = d2;
if (max_desc->num_pages < d2->num_pages)
max_desc = d2;
}
if (min_desc != d1)
if (max_desc != d1)
{
grub_efi_memory_descriptor_t tmp;
tmp = *d1;
*d1 = *min_desc;
*min_desc = tmp;
*d1 = *max_desc;
*max_desc = tmp;
}
}
}
@ -223,16 +223,17 @@ filter_memory_map (grub_efi_memory_descriptor_t *memory_map,
#if GRUB_HOST_SIZEOF_VOID_P < 8
&& desc->physical_start <= 0xffffffff
#endif
&& desc->physical_start + PAGES_TO_BYTES (desc->num_pages) > 0x100000
&& desc->num_pages != 0)
{
grub_memcpy (filtered_desc, desc, desc_size);
/* Avoid the page at the address zero, because this is really
confusing for C programs. */
if (filtered_desc->physical_start == 0)
/* Avoid less than 1MB, because some loaders seem to be confused. */
if (desc->physical_start < 0x100000)
{
filtered_desc->physical_start = 0x1000;
filtered_desc->num_pages--;
desc->num_pages -= BYTES_TO_PAGES (0x100000
- desc->physical_start);
desc->physical_start = 0x100000;
}
#if GRUB_HOST_SIZEOF_VOID_P < 8
@ -285,16 +286,21 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map,
desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))
{
grub_efi_uint64_t pages;
grub_efi_physical_address_t start;
void *addr;
start = desc->physical_start;
pages = desc->num_pages;
if (pages > required_pages)
pages = required_pages;
{
start += PAGES_TO_BYTES (pages - required_pages);
pages = required_pages;
}
addr = grub_efi_allocate_pages (desc->physical_start, pages);
addr = grub_efi_allocate_pages (start, pages);
if (! addr)
grub_fatal ("cannot allocate conventional memory %p with %u pages",
(void *) ((grub_addr_t) desc->physical_start),
(void *) ((grub_addr_t) start),
(unsigned) pages);
grub_mm_init_region (addr, PAGES_TO_BYTES (pages));
@ -308,6 +314,27 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map,
grub_fatal ("too little memory");
}
#if 0
/* Print the memory map. */
static void
print_memory_map (grub_efi_memory_descriptor_t *memory_map,
grub_efi_uintn_t desc_size,
grub_efi_memory_descriptor_t *memory_map_end)
{
grub_efi_memory_descriptor_t *desc;
int i;
for (desc = memory_map, i = 0;
desc < memory_map_end;
desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size), i++)
{
grub_printf ("MD: t=%x, p=%llx, v=%llx, n=%llx, a=%llx\n",
desc->type, desc->physical_start, desc->virtual_start,
desc->num_pages, desc->attribute);
}
}
#endif
void
grub_efi_mm_init (void)
{
@ -363,6 +390,19 @@ grub_efi_mm_init (void)
/* Allocate memory regions for GRUB's memory management. */
add_memory_regions (filtered_memory_map, desc_size,
filtered_memory_map_end, required_pages);
#if 0
/* For debug. */
map_size = MEMORY_MAP_SIZE;
if (grub_efi_get_memory_map (&map_size, memory_map, 0, &desc_size, 0) < 0)
grub_fatal ("cannot get memory map");
grub_printf ("printing memory map\n");
print_memory_map (memory_map, desc_size,
NEXT_MEMORY_DESCRIPTOR (memory_map, map_size));
grub_abort ();
#endif
/* Release the memory maps. */
grub_efi_free_pages ((grub_addr_t) memory_map,