/* * VAS_EBOOT -- GRand Unified Bootloader * Copyright (C) 2006,2007,2008,2009,2010 Free Software Foundation, Inc. * * VAS_EBOOT is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * VAS_EBOOT is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with VAS_EBOOT. If not, see . */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include VAS_EBOOT_MOD_LICENSE ("GPLv3+"); #ifdef VAS_EBOOT_MACHINE_PCBIOS #include #endif #ifdef VAS_EBOOT_MACHINE_EFI #include #include #define HAS_VGA_TEXT 0 #define DEFAULT_VIDEO_MODE "auto" #define ACCEPTS_PURE_TEXT 0 #elif defined (VAS_EBOOT_MACHINE_IEEE1275) #include #define HAS_VGA_TEXT 0 #define DEFAULT_VIDEO_MODE "text" #define ACCEPTS_PURE_TEXT 1 #else #include #include #define HAS_VGA_TEXT 1 #define DEFAULT_VIDEO_MODE "text" #define ACCEPTS_PURE_TEXT 1 #endif static VasEBoot_dl_t my_mod; static VasEBoot_size_t linux_mem_size; static int loaded; static void *prot_mode_mem; static VasEBoot_addr_t prot_mode_target; static void *initrd_mem; static VasEBoot_addr_t initrd_mem_target; static VasEBoot_size_t prot_init_space; static struct VasEBoot_relocator *relocator = NULL; static void *efi_mmap_buf; static VasEBoot_size_t maximal_cmdline_size; static struct linux_kernel_params linux_params; static char *linux_cmdline; #ifdef VAS_EBOOT_MACHINE_EFI static VasEBoot_efi_uintn_t efi_mmap_size; #else static const VasEBoot_size_t efi_mmap_size = 0; #endif /* FIXME */ #if 0 struct idt_descriptor { VasEBoot_uint16_t limit; void *base; } VAS_EBOOT_PACKED; static struct idt_descriptor idt_desc = { 0, 0 }; #endif static inline VasEBoot_size_t page_align (VasEBoot_size_t size) { return (size + (1 << 12) - 1) & (~((1 << 12) - 1)); } /* Helper for find_mmap_size. */ static int count_hook (VasEBoot_uint64_t addr __attribute__ ((unused)), VasEBoot_uint64_t size __attribute__ ((unused)), VasEBoot_memory_type_t type __attribute__ ((unused)), void *data) { VasEBoot_size_t *count = data; (*count)++; return 0; } /* Find the optimal number of pages for the memory map. */ static VasEBoot_size_t find_mmap_size (void) { VasEBoot_size_t count = 0, mmap_size; VasEBoot_mmap_iterate (count_hook, &count); mmap_size = count * sizeof (struct VasEBoot_boot_e820_entry); /* Increase the size a bit for safety, because VAS_EBOOT allocates more on later. */ mmap_size += (1 << 12); return page_align (mmap_size); } static void free_pages (void) { VasEBoot_relocator_unload (relocator); relocator = NULL; prot_mode_mem = initrd_mem = 0; prot_mode_target = initrd_mem_target = 0; } /* Allocate pages for the real mode code and the protected mode code for linux as well as a memory map buffer. */ static VasEBoot_err_t allocate_pages (VasEBoot_size_t prot_size, VasEBoot_size_t *align, VasEBoot_size_t min_align, int relocatable, VasEBoot_uint64_t preferred_address) { VasEBoot_err_t err; if (prot_size == 0) prot_size = 1; prot_size = page_align (prot_size); /* Initialize the memory pointers with NULL for convenience. */ free_pages (); relocator = VasEBoot_relocator_new (); if (!relocator) { err = VasEBoot_errno; goto fail; } /* FIXME: Should request low memory from the heap when this feature is implemented. */ { VasEBoot_relocator_chunk_t ch; if (relocatable) { err = VasEBoot_relocator_alloc_chunk_align (relocator, &ch, preferred_address, preferred_address, prot_size, 1, VAS_EBOOT_RELOCATOR_PREFERENCE_LOW, 1); for (; err && *align + 1 > min_align; (*align)--) { VasEBoot_errno = VAS_EBOOT_ERR_NONE; err = VasEBoot_relocator_alloc_chunk_align (relocator, &ch, 0x1000000, UP_TO_TOP32 (prot_size), prot_size, 1 << *align, VAS_EBOOT_RELOCATOR_PREFERENCE_LOW, 1); } if (err) goto fail; } else err = VasEBoot_relocator_alloc_chunk_addr (relocator, &ch, preferred_address, prot_size); if (err) goto fail; prot_mode_mem = get_virtual_current_address (ch); prot_mode_target = get_physical_target_address (ch); } VasEBoot_dprintf ("linux", "prot_mode_mem = %p, prot_mode_target = %lx, prot_size = %x\n", prot_mode_mem, (unsigned long) prot_mode_target, (unsigned) prot_size); return VAS_EBOOT_ERR_NONE; fail: free_pages (); return err; } static VasEBoot_err_t VasEBoot_e820_add_region (struct VasEBoot_boot_e820_entry *e820_entry, int *e820_num, VasEBoot_uint64_t start, VasEBoot_uint64_t size, VasEBoot_uint32_t type) { int n = *e820_num; if ((n > 0) && (e820_entry[n - 1].addr + e820_entry[n - 1].size == start) && (e820_entry[n - 1].type == type)) e820_entry[n - 1].size += size; else { e820_entry[n].addr = start; e820_entry[n].size = size; e820_entry[n].type = type; (*e820_num)++; } return VAS_EBOOT_ERR_NONE; } static VasEBoot_err_t VasEBoot_linux_setup_video (struct linux_kernel_params *params) { struct VasEBoot_video_edid_info edid_info; struct VasEBoot_video_mode_info mode_info; void *framebuffer; VasEBoot_err_t err; VasEBoot_video_driver_id_t driver_id; const char *gfxlfbvar = VasEBoot_env_get ("gfxpayloadforcelfb"); driver_id = VasEBoot_video_get_driver_id (); if (driver_id == VAS_EBOOT_VIDEO_DRIVER_NONE) return 1; err = VasEBoot_video_get_edid (&edid_info); if (err != VAS_EBOOT_ERR_NONE) VasEBoot_memset (&edid_info, 0, sizeof (edid_info)); /* * We cannot transfer any extensions. Therefore clear * the extension flag from the checksum and set the * field to zero. Adding the extension flag to the * checksum does the trick. */ edid_info.checksum += edid_info.extension_flag; edid_info.extension_flag = 0; err = VasEBoot_video_get_info_and_fini (&mode_info, &framebuffer); if (err) { VasEBoot_errno = VAS_EBOOT_ERR_NONE; return 1; } params->screen_info.lfb_width = mode_info.width; params->screen_info.lfb_height = mode_info.height; params->screen_info.lfb_depth = mode_info.bpp; params->screen_info.lfb_linelength = mode_info.pitch; params->screen_info.lfb_base = (VasEBoot_size_t) framebuffer; #if defined (VAS_EBOOT_MACHINE_EFI) && defined (__x86_64__) params->screen_info.ext_lfb_base = (VasEBoot_size_t) (((VasEBoot_uint64_t)(VasEBoot_size_t) framebuffer) >> 32); params->screen_info.capabilities |= VIDEO_CAPABILITY_64BIT_BASE; #endif params->screen_info.lfb_size = ALIGN_UP (params->screen_info.lfb_linelength * params->screen_info.lfb_height, 65536); params->screen_info.red_size = mode_info.red_mask_size; params->screen_info.red_pos = mode_info.red_field_pos; params->screen_info.green_size = mode_info.green_mask_size; params->screen_info.green_pos = mode_info.green_field_pos; params->screen_info.blue_size = mode_info.blue_mask_size; params->screen_info.blue_pos = mode_info.blue_field_pos; params->screen_info.rsvd_size = mode_info.reserved_mask_size; params->screen_info.rsvd_pos = mode_info.reserved_field_pos; if (gfxlfbvar && (gfxlfbvar[0] == '1' || gfxlfbvar[0] == 'y')) params->screen_info.orig_video_isVGA = VAS_EBOOT_VIDEO_LINUX_TYPE_SIMPLE; else { switch (driver_id) { case VAS_EBOOT_VIDEO_DRIVER_VBE: params->screen_info.lfb_size >>= 16; params->screen_info.orig_video_isVGA = VAS_EBOOT_VIDEO_LINUX_TYPE_VESA; break; case VAS_EBOOT_VIDEO_DRIVER_EFI_UGA: case VAS_EBOOT_VIDEO_DRIVER_EFI_GOP: params->screen_info.orig_video_isVGA = VAS_EBOOT_VIDEO_LINUX_TYPE_EFIFB; break; /* FIXME: check if better id is available. */ case VAS_EBOOT_VIDEO_DRIVER_SM712: case VAS_EBOOT_VIDEO_DRIVER_SIS315PRO: case VAS_EBOOT_VIDEO_DRIVER_VGA: case VAS_EBOOT_VIDEO_DRIVER_CIRRUS: case VAS_EBOOT_VIDEO_DRIVER_BOCHS: case VAS_EBOOT_VIDEO_DRIVER_RADEON_FULOONG2E: case VAS_EBOOT_VIDEO_DRIVER_RADEON_YEELOONG3A: case VAS_EBOOT_VIDEO_DRIVER_IEEE1275: case VAS_EBOOT_VIDEO_DRIVER_COREBOOT: /* Make gcc happy. */ case VAS_EBOOT_VIDEO_DRIVER_XEN: case VAS_EBOOT_VIDEO_DRIVER_SDL: case VAS_EBOOT_VIDEO_DRIVER_NONE: case VAS_EBOOT_VIDEO_ADAPTER_CAPTURE: params->screen_info.orig_video_isVGA = VAS_EBOOT_VIDEO_LINUX_TYPE_SIMPLE; break; } } #ifdef VAS_EBOOT_MACHINE_PCBIOS /* VESA packed modes may come with zeroed mask sizes, which need to be set here according to DAC Palette width. If we don't, this results in Linux displaying a black screen. */ if (driver_id == VAS_EBOOT_VIDEO_DRIVER_VBE && mode_info.bpp <= 8) { struct VasEBoot_vbe_info_block controller_info; int status; int width = 8; status = VasEBoot_vbe_bios_get_controller_info (&controller_info); if (status == VAS_EBOOT_VBE_STATUS_OK && (controller_info.capabilities & VAS_EBOOT_VBE_CAPABILITY_DACWIDTH)) status = VasEBoot_vbe_bios_set_dac_palette_width (&width); if (status != VAS_EBOOT_VBE_STATUS_OK) /* 6 is default after mode reset. */ width = 6; params->screen_info.red_size = params->screen_info.green_size = params->screen_info.blue_size = width; params->screen_info.rsvd_size = 0; } #endif VasEBoot_memcpy (params->edid_info, &edid_info, sizeof (params->edid_info)); return VAS_EBOOT_ERR_NONE; } /* Context for VasEBoot_linux_boot. */ struct VasEBoot_linux_boot_ctx { VasEBoot_addr_t real_mode_target; VasEBoot_size_t real_size; struct linux_kernel_params *params; int e820_num; }; /* Helper for VasEBoot_linux_boot. */ static int VasEBoot_linux_boot_mmap_find (VasEBoot_uint64_t addr, VasEBoot_uint64_t size, VasEBoot_memory_type_t type, void *data) { struct VasEBoot_linux_boot_ctx *ctx = data; /* We must put real mode code in the traditional space. */ if (type != VAS_EBOOT_MEMORY_AVAILABLE || addr > 0x90000) return 0; if (addr + size < 0x10000) return 0; if (addr < 0x10000) { size += addr - 0x10000; addr = 0x10000; } if (addr + size > 0x90000) size = 0x90000 - addr; if (ctx->real_size + efi_mmap_size > size) return 0; VasEBoot_dprintf ("linux", "addr = %lx, size = %x, need_size = %x\n", (unsigned long) addr, (unsigned) size, (unsigned) (ctx->real_size + efi_mmap_size)); ctx->real_mode_target = ((addr + size) - (ctx->real_size + efi_mmap_size)); return 1; } /* VAS_EBOOT types conveniently match E820 types. */ static int VasEBoot_linux_boot_mmap_fill (VasEBoot_uint64_t addr, VasEBoot_uint64_t size, VasEBoot_memory_type_t type, void *data) { struct VasEBoot_linux_boot_ctx *ctx = data; if (VasEBoot_e820_add_region (ctx->params->e820_table, &ctx->e820_num, addr, size, type)) return 1; return 0; } static VasEBoot_err_t VasEBoot_linux_boot (void) { VasEBoot_err_t err = 0; const char *modevar; char *tmp; struct VasEBoot_relocator32_state state; void *real_mode_mem; struct VasEBoot_linux_boot_ctx ctx = { .real_mode_target = 0 }; VasEBoot_size_t mmap_size; VasEBoot_size_t cl_offset; #ifdef VAS_EBOOT_MACHINE_IEEE1275 { const char *bootpath; VasEBoot_ssize_t len; bootpath = VasEBoot_env_get ("root"); if (bootpath) VasEBoot_ieee1275_set_property (VasEBoot_ieee1275_chosen, "bootpath", bootpath, VasEBoot_strlen (bootpath) + 1, &len); linux_params.olpc_ofw_header.ofw_magic = VAS_EBOOT_LINUX_OFW_SIGNATURE; linux_params.olpc_ofw_header.ofw_version = 1; linux_params.olpc_ofw_header.cif_handler = (VasEBoot_uint32_t) VasEBoot_ieee1275_entry_fn; linux_params.olpc_ofw_header.irq_desc_table = 0; } #endif modevar = VasEBoot_env_get ("gfxpayload"); /* Now all graphical modes are acceptable. May change in future if we have modes without framebuffer. */ if (modevar && *modevar != 0) { tmp = VasEBoot_xasprintf ("%s;" DEFAULT_VIDEO_MODE, modevar); if (! tmp) return VasEBoot_errno; #if ACCEPTS_PURE_TEXT err = VasEBoot_video_set_mode (tmp, 0, 0); #else err = VasEBoot_video_set_mode (tmp, VAS_EBOOT_VIDEO_MODE_TYPE_PURE_TEXT, 0); #endif VasEBoot_free (tmp); } else /* We can't go back to text mode from coreboot fb. */ #ifdef VAS_EBOOT_MACHINE_COREBOOT if (VasEBoot_video_get_driver_id () == VAS_EBOOT_VIDEO_DRIVER_COREBOOT) err = VAS_EBOOT_ERR_NONE; else #endif { #if ACCEPTS_PURE_TEXT err = VasEBoot_video_set_mode (DEFAULT_VIDEO_MODE, 0, 0); #else err = VasEBoot_video_set_mode (DEFAULT_VIDEO_MODE, VAS_EBOOT_VIDEO_MODE_TYPE_PURE_TEXT, 0); #endif } if (err) { VasEBoot_print_error (); VasEBoot_puts_ (N_("Booting in blind mode")); VasEBoot_errno = VAS_EBOOT_ERR_NONE; } if (VasEBoot_linux_setup_video (&linux_params)) { #if defined (VAS_EBOOT_MACHINE_PCBIOS) || defined (VAS_EBOOT_MACHINE_COREBOOT) || defined (VAS_EBOOT_MACHINE_QEMU) linux_params.screen_info.orig_video_isVGA = VAS_EBOOT_VIDEO_LINUX_TYPE_TEXT; linux_params.screen_info.orig_video_mode = 0x3; #else linux_params.screen_info.orig_video_isVGA = 0; linux_params.screen_info.orig_video_mode = 0; linux_params.screen_info.orig_video_cols = 0; linux_params.screen_info.orig_video_lines = 0; #endif } #ifndef VAS_EBOOT_MACHINE_IEEE1275 if (linux_params.screen_info.orig_video_isVGA == VAS_EBOOT_VIDEO_LINUX_TYPE_TEXT) #endif { VasEBoot_term_output_t term; int found = 0; FOR_ACTIVE_TERM_OUTPUTS(term) if (VasEBoot_strcmp (term->name, "vga_text") == 0 || VasEBoot_strcmp (term->name, "console") == 0 || VasEBoot_strcmp (term->name, "ofconsole") == 0) { struct VasEBoot_term_coordinate pos = VasEBoot_term_getxy (term); linux_params.screen_info.orig_x = pos.x; linux_params.screen_info.orig_y = pos.y; linux_params.screen_info.orig_video_cols = VasEBoot_term_width (term); linux_params.screen_info.orig_video_lines = VasEBoot_term_height (term); found = 1; break; } if (!found) { linux_params.screen_info.orig_x = 0; linux_params.screen_info.orig_y = 0; linux_params.screen_info.orig_video_cols = 80; linux_params.screen_info.orig_video_lines = 25; } } #ifdef VAS_EBOOT_KERNEL_USE_RSDP_ADDR linux_params.acpi_rsdp_addr = VasEBoot_le_to_cpu64 (VasEBoot_rsdp_addr); #endif mmap_size = find_mmap_size (); /* Make sure that each size is aligned to a page boundary. */ cl_offset = ALIGN_UP (mmap_size + sizeof (linux_params), 4096); if (cl_offset < ((VasEBoot_size_t) linux_params.hdr.setup_sects << VAS_EBOOT_DISK_SECTOR_BITS)) cl_offset = ALIGN_UP ((VasEBoot_size_t) (linux_params.hdr.setup_sects << VAS_EBOOT_DISK_SECTOR_BITS), 4096); ctx.real_size = ALIGN_UP (cl_offset + maximal_cmdline_size, 4096); #ifdef VAS_EBOOT_MACHINE_EFI efi_mmap_size = VasEBoot_efi_find_mmap_size (); if (efi_mmap_size == 0) return VasEBoot_errno; #endif VasEBoot_dprintf ("linux", "real_size = %x, mmap_size = %x\n", (unsigned) ctx.real_size, (unsigned) mmap_size); #ifdef VAS_EBOOT_MACHINE_EFI VasEBoot_efi_mmap_iterate (VasEBoot_linux_boot_mmap_find, &ctx, 1); if (! ctx.real_mode_target) VasEBoot_efi_mmap_iterate (VasEBoot_linux_boot_mmap_find, &ctx, 0); #else VasEBoot_mmap_iterate (VasEBoot_linux_boot_mmap_find, &ctx); #endif VasEBoot_dprintf ("linux", "real_mode_target = %lx, real_size = %x, efi_mmap_size = %x\n", (unsigned long) ctx.real_mode_target, (unsigned) ctx.real_size, (unsigned) efi_mmap_size); if (! ctx.real_mode_target) return VasEBoot_error (VAS_EBOOT_ERR_OUT_OF_MEMORY, "cannot allocate real mode pages"); { VasEBoot_relocator_chunk_t ch; VasEBoot_size_t sz; if (VasEBoot_add (ctx.real_size, efi_mmap_size, &sz)) return VAS_EBOOT_ERR_OUT_OF_RANGE; err = VasEBoot_relocator_alloc_chunk_addr (relocator, &ch, ctx.real_mode_target, sz); if (err) return err; real_mode_mem = get_virtual_current_address (ch); } efi_mmap_buf = (VasEBoot_uint8_t *) real_mode_mem + ctx.real_size; VasEBoot_dprintf ("linux", "real_mode_mem = %p\n", real_mode_mem); ctx.params = real_mode_mem; *ctx.params = linux_params; ctx.params->hdr.cmd_line_ptr = ctx.real_mode_target + cl_offset; VasEBoot_memcpy ((char *) ctx.params + cl_offset, linux_cmdline, maximal_cmdline_size); VasEBoot_dprintf ("linux", "code32_start = %x\n", (unsigned) ctx.params->hdr.code32_start); ctx.e820_num = 0; if (VasEBoot_mmap_iterate (VasEBoot_linux_boot_mmap_fill, &ctx)) return VasEBoot_errno; ctx.params->e820_entries = ctx.e820_num; #ifdef VAS_EBOOT_MACHINE_EFI { VasEBoot_efi_uintn_t efi_desc_size; VasEBoot_size_t efi_mmap_target; VasEBoot_efi_uint32_t efi_desc_version; ctx.params->secure_boot = VasEBoot_efi_get_secureboot (); err = VasEBoot_efi_finish_boot_services (&efi_mmap_size, efi_mmap_buf, NULL, &efi_desc_size, &efi_desc_version); if (err) return err; /* Note that no boot services are available from here. */ efi_mmap_target = ctx.real_mode_target + ((VasEBoot_uint8_t *) efi_mmap_buf - (VasEBoot_uint8_t *) real_mode_mem); /* Pass EFI parameters. */ if (VasEBoot_le_to_cpu16 (ctx.params->hdr.version) >= 0x0208) { ctx.params->efi_info.v0208.efi_memdesc_size = efi_desc_size; ctx.params->efi_info.v0208.efi_memdesc_version = efi_desc_version; ctx.params->efi_info.v0208.efi_memmap = efi_mmap_target; ctx.params->efi_info.v0208.efi_memmap_size = efi_mmap_size; #ifdef __x86_64__ ctx.params->efi_info.v0208.efi_memmap_hi = (efi_mmap_target >> 32); #endif } else if (VasEBoot_le_to_cpu16 (ctx.params->hdr.version) >= 0x0206) { ctx.params->efi_info.v0206.efi_memdesc_size = efi_desc_size; ctx.params->efi_info.v0206.efi_memdesc_version = efi_desc_version; ctx.params->efi_info.v0206.efi_memmap = efi_mmap_target; ctx.params->efi_info.v0206.efi_memmap_size = efi_mmap_size; } else if (VasEBoot_le_to_cpu16 (ctx.params->hdr.version) >= 0x0204) { ctx.params->efi_info.v0204.efi_memdesc_size = efi_desc_size; ctx.params->efi_info.v0204.efi_memdesc_version = efi_desc_version; ctx.params->efi_info.v0204.efi_memmap = efi_mmap_target; ctx.params->efi_info.v0204.efi_memmap_size = efi_mmap_size; } } #endif #if defined (__x86_64__) && defined (VAS_EBOOT_MACHINE_EFI) if (VasEBoot_le_to_cpu16 (ctx.params->hdr.version) >= 0x020c && (linux_params.hdr.xloadflags & LINUX_X86_XLF_KERNEL_64) != 0) { struct VasEBoot_relocator64_efi_state state64; state64.rsi = ctx.real_mode_target; state64.rip = ctx.params->hdr.code32_start + LINUX_X86_STARTUP64_OFFSET; return VasEBoot_relocator64_efi_boot (relocator, state64); } #endif /* FIXME. */ /* asm volatile ("lidt %0" : : "m" (idt_desc)); */ state.ebp = state.edi = state.ebx = 0; state.esi = ctx.real_mode_target; state.esp = ctx.real_mode_target; state.eip = ctx.params->hdr.code32_start; return VasEBoot_relocator32_boot (relocator, state, 0); } static VasEBoot_err_t VasEBoot_linux_unload (void) { VasEBoot_dl_unref (my_mod); loaded = 0; VasEBoot_free (linux_cmdline); linux_cmdline = 0; return VAS_EBOOT_ERR_NONE; } static VasEBoot_err_t VasEBoot_cmd_linux (VasEBoot_command_t cmd __attribute__ ((unused)), int argc, char *argv[]) { VasEBoot_file_t file = 0; struct linux_i386_kernel_header lh; VasEBoot_uint8_t setup_sects; VasEBoot_size_t real_size, prot_size, prot_file_size; VasEBoot_ssize_t len; int i; VasEBoot_size_t align, min_align; int relocatable; VasEBoot_uint64_t preferred_address = VAS_EBOOT_LINUX_BZIMAGE_ADDR; VasEBoot_dl_ref (my_mod); if (argc == 0) { VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, N_("filename expected")); goto fail; } file = VasEBoot_file_open (argv[0], VAS_EBOOT_FILE_TYPE_LINUX_KERNEL); if (! file) goto fail; if (VasEBoot_file_read (file, &lh, sizeof (lh)) != sizeof (lh)) { if (!VasEBoot_errno) VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, N_("premature end of file %s"), argv[0]); goto fail; } if (lh.boot_flag != VasEBoot_cpu_to_le16_compile_time (0xaa55)) { VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, "invalid magic number"); goto fail; } if (lh.setup_sects > VAS_EBOOT_LINUX_MAX_SETUP_SECTS) { VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, "too many setup sectors"); goto fail; } /* FIXME: 2.03 is not always good enough (Linux 2.4 can be 2.03 and still not support 32-bit boot. */ if (lh.header != VasEBoot_cpu_to_le32_compile_time (VAS_EBOOT_LINUX_I386_MAGIC_SIGNATURE) || VasEBoot_le_to_cpu16 (lh.version) < 0x0203) { VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, "version too old for 32-bit boot" #ifdef VAS_EBOOT_MACHINE_PCBIOS " (try with `linux16')" #endif ); goto fail; } if (! (lh.loadflags & VAS_EBOOT_LINUX_FLAG_BIG_KERNEL)) { VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, "zImage doesn't support 32-bit boot" #ifdef VAS_EBOOT_MACHINE_PCBIOS " (try with `linux16')" #endif ); goto fail; } if (VasEBoot_le_to_cpu16 (lh.version) >= 0x0206) maximal_cmdline_size = VasEBoot_le_to_cpu32 (lh.cmdline_size) + 1; else maximal_cmdline_size = 256; if (maximal_cmdline_size < 128) maximal_cmdline_size = 128; setup_sects = lh.setup_sects; /* If SETUP_SECTS is not set, set it to the default (4). */ if (! setup_sects) setup_sects = VAS_EBOOT_LINUX_DEFAULT_SETUP_SECTS; real_size = setup_sects << VAS_EBOOT_DISK_SECTOR_BITS; prot_file_size = VasEBoot_file_size (file) - real_size - VAS_EBOOT_DISK_SECTOR_SIZE; if (VasEBoot_le_to_cpu16 (lh.version) >= 0x205 && lh.kernel_alignment != 0 && ((lh.kernel_alignment - 1) & lh.kernel_alignment) == 0) { for (align = 0; align < 32; align++) if (VasEBoot_le_to_cpu32 (lh.kernel_alignment) & (1 << align)) break; relocatable = lh.relocatable; } else { align = 0; relocatable = 0; } if (VasEBoot_le_to_cpu16 (lh.version) >= 0x020a) { min_align = lh.min_alignment; prot_size = VasEBoot_le_to_cpu32 (lh.init_size); prot_init_space = page_align (prot_size); if (relocatable) preferred_address = VasEBoot_le_to_cpu64 (lh.pref_address); } else { min_align = align; prot_size = prot_file_size; /* Usually, the compression ratio is about 50%. */ prot_init_space = page_align (prot_size) * 3; } if (allocate_pages (prot_size, &align, min_align, relocatable, preferred_address)) goto fail; VasEBoot_memset (&linux_params, 0, sizeof (linux_params)); /* * The Linux 32-bit boot protocol defines the setup header end * to be at 0x202 + the byte value at 0x201. */ len = 0x202 + *((char *) &lh.jump + 1); /* Verify the struct is big enough so we do not write past the end. */ if (len > (char *) &linux_params.edd_mbr_sig_buffer - (char *) &linux_params) { VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, "Linux setup header too big"); goto fail; } VasEBoot_memcpy (&linux_params.hdr.setup_sects, &lh.setup_sects, len - 0x1F1); /* We've already read lh so there is no need to read it second time. */ len -= sizeof(lh); if ((len > 0) && (VasEBoot_file_read (file, (char *) &linux_params + sizeof (lh), len) != len)) { if (!VasEBoot_errno) VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, N_("premature end of file %s"), argv[0]); goto fail; } linux_params.hdr.code32_start = prot_mode_target + lh.code32_start - VAS_EBOOT_LINUX_BZIMAGE_ADDR; linux_params.hdr.kernel_alignment = ((VasEBoot_uint32_t) 1 << align); linux_params.hdr.boot_flag = 0; linux_params.hdr.type_of_loader = VAS_EBOOT_LINUX_BOOT_LOADER_TYPE; /* These two are used (instead of cmd_line_ptr) by older versions of Linux, and otherwise ignored. */ linux_params.screen_info.cl_magic = VAS_EBOOT_LINUX_CL_MAGIC; linux_params.screen_info.cl_offset = 0x1000; linux_params.hdr.ramdisk_image = 0; linux_params.hdr.ramdisk_size = 0; linux_params.hdr.heap_end_ptr = VAS_EBOOT_LINUX_HEAP_END_OFFSET; linux_params.hdr.loadflags |= VAS_EBOOT_LINUX_FLAG_CAN_USE_HEAP; /* These are not needed to be precise, because Linux uses these values only to raise an error when the decompression code cannot find good space. */ linux_params.screen_info.ext_mem_k = ((32 * 0x100000) >> 10); linux_params.alt_mem_k = ((32 * 0x100000) >> 10); /* Ignored by Linux. */ linux_params.screen_info.orig_video_page = 0; /* Only used when `video_mode == 0x7', otherwise ignored. */ linux_params.screen_info.orig_video_ega_bx = 0; linux_params.screen_info.orig_video_points = 16; /* XXX */ #ifdef VAS_EBOOT_MACHINE_EFI #ifdef __x86_64__ if (VasEBoot_le_to_cpu16 (linux_params.hdr.version) < 0x0208 && ((VasEBoot_addr_t) VasEBoot_efi_system_table >> 32) != 0) { VasEBoot_errno = VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, "kernel does not support 64-bit addressing"); goto fail; } #endif if (VasEBoot_le_to_cpu16 (linux_params.hdr.version) >= 0x0208) { linux_params.efi_info.v0208.efi_loader_signature = VAS_EBOOT_LINUX_EFI_SIGNATURE; linux_params.efi_info.v0208.efi_systab = (VasEBoot_uint32_t) (VasEBoot_addr_t) VasEBoot_efi_system_table; #ifdef __x86_64__ linux_params.efi_info.v0208.efi_systab_hi = (VasEBoot_uint32_t) ((VasEBoot_uint64_t) VasEBoot_efi_system_table >> 32); #endif } else if (VasEBoot_le_to_cpu16 (linux_params.hdr.version) >= 0x0206) { linux_params.efi_info.v0206.efi_loader_signature = VAS_EBOOT_LINUX_EFI_SIGNATURE; linux_params.efi_info.v0206.efi_systab = (VasEBoot_uint32_t) (VasEBoot_addr_t) VasEBoot_efi_system_table; } else if (VasEBoot_le_to_cpu16 (linux_params.hdr.version) >= 0x0204) { linux_params.efi_info.v0204.efi_loader_signature = VAS_EBOOT_LINUX_EFI_SIGNATURE_0204; linux_params.efi_info.v0204.efi_systab = (VasEBoot_uint32_t) (VasEBoot_addr_t) VasEBoot_efi_system_table; } #endif /* The other parameters are filled when booting. */ VasEBoot_file_seek (file, real_size + VAS_EBOOT_DISK_SECTOR_SIZE); VasEBoot_dprintf ("linux", "bzImage, setup=0x%x, size=0x%x\n", (unsigned) real_size, (unsigned) prot_size); /* Look for memory size and video mode specified on the command line. */ linux_mem_size = 0; for (i = 1; i < argc; i++) #ifdef VAS_EBOOT_MACHINE_PCBIOS if (VasEBoot_memcmp (argv[i], "vga=", 4) == 0) { /* Video mode selection support. */ char *val = argv[i] + 4; unsigned vid_mode = VAS_EBOOT_LINUX_VID_MODE_NORMAL; struct VasEBoot_vesa_mode_table_entry *linux_mode; VasEBoot_err_t err; char *buf; VasEBoot_dl_load ("vbe"); if (VasEBoot_strcmp (val, "normal") == 0) vid_mode = VAS_EBOOT_LINUX_VID_MODE_NORMAL; else if (VasEBoot_strcmp (val, "ext") == 0) vid_mode = VAS_EBOOT_LINUX_VID_MODE_EXTENDED; else if (VasEBoot_strcmp (val, "ask") == 0) { VasEBoot_puts_ (N_("Legacy `ask' parameter no longer supported.")); /* We usually would never do this in a loader, but "vga=ask" means user requested interaction, so it can't hurt to request keyboard input. */ VasEBoot_wait_after_message (); goto fail; } else vid_mode = (VasEBoot_uint16_t) VasEBoot_strtoul (val, 0, 0); switch (vid_mode) { case 0: case VAS_EBOOT_LINUX_VID_MODE_NORMAL: VasEBoot_env_set ("gfxpayload", "text"); VasEBoot_printf_ (N_("%s is deprecated. " "Use set gfxpayload=%s before " "linux command instead.\n"), argv[i], "text"); break; case 1: case VAS_EBOOT_LINUX_VID_MODE_EXTENDED: /* FIXME: support 80x50 text. */ VasEBoot_env_set ("gfxpayload", "text"); VasEBoot_printf_ (N_("%s is deprecated. " "Use set gfxpayload=%s before " "linux command instead.\n"), argv[i], "text"); break; default: /* Ignore invalid values. */ if (vid_mode < VAS_EBOOT_VESA_MODE_TABLE_START || vid_mode > VAS_EBOOT_VESA_MODE_TABLE_END) { VasEBoot_env_set ("gfxpayload", "text"); /* TRANSLATORS: "x" has to be entered in, like an identifier, so please don't use better Unicode codepoints. */ VasEBoot_printf_ (N_("%s is deprecated. VGA mode %d isn't recognized. " "Use set gfxpayload=WIDTHxHEIGHT[xDEPTH] " "before linux command instead.\n"), argv[i], vid_mode); break; } linux_mode = &VasEBoot_vesa_mode_table[vid_mode - VAS_EBOOT_VESA_MODE_TABLE_START]; buf = VasEBoot_xasprintf ("%ux%ux%u,%ux%u", linux_mode->width, linux_mode->height, linux_mode->depth, linux_mode->width, linux_mode->height); if (! buf) goto fail; VasEBoot_printf_ (N_("%s is deprecated. " "Use set gfxpayload=%s before " "linux command instead.\n"), argv[i], buf); err = VasEBoot_env_set ("gfxpayload", buf); VasEBoot_free (buf); if (err) goto fail; } } else #endif /* VAS_EBOOT_MACHINE_PCBIOS */ if (VasEBoot_memcmp (argv[i], "mem=", 4) == 0) { const char *val = argv[i] + 4; linux_mem_size = VasEBoot_strtoul (val, &val, 0); if (VasEBoot_errno) { VasEBoot_errno = VAS_EBOOT_ERR_NONE; linux_mem_size = 0; } else { int shift = 0; switch (VasEBoot_tolower (val[0])) { case 'g': shift += 10; /* FALLTHROUGH */ case 'm': shift += 10; /* FALLTHROUGH */ case 'k': shift += 10; /* FALLTHROUGH */ default: break; } /* Check an overflow. */ if (linux_mem_size > (~0UL >> shift)) linux_mem_size = 0; else linux_mem_size <<= shift; } } else if (VasEBoot_memcmp (argv[i], "quiet", sizeof ("quiet") - 1) == 0) { linux_params.hdr.loadflags |= VAS_EBOOT_LINUX_FLAG_QUIET; } /* Create kernel command line. */ linux_cmdline = VasEBoot_zalloc (maximal_cmdline_size + 1); if (!linux_cmdline) goto fail; VasEBoot_memcpy (linux_cmdline, LINUX_IMAGE, sizeof (LINUX_IMAGE)); { VasEBoot_err_t err; err = VasEBoot_create_loader_cmdline (argc, argv, linux_cmdline + sizeof (LINUX_IMAGE) - 1, maximal_cmdline_size - (sizeof (LINUX_IMAGE) - 1), VAS_EBOOT_VERIFY_KERNEL_CMDLINE); if (err) goto fail; } len = prot_file_size; if (VasEBoot_file_read (file, prot_mode_mem, len) != len && !VasEBoot_errno) VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, N_("premature end of file %s"), argv[0]); if (VasEBoot_errno == VAS_EBOOT_ERR_NONE) { VasEBoot_loader_set (VasEBoot_linux_boot, VasEBoot_linux_unload, 0 /* set noreturn=0 in order to avoid VasEBoot_console_fini() */); loaded = 1; } fail: if (file) VasEBoot_file_close (file); if (VasEBoot_errno != VAS_EBOOT_ERR_NONE) { VasEBoot_dl_unref (my_mod); loaded = 0; } return VasEBoot_errno; } static VasEBoot_err_t VasEBoot_cmd_initrd (VasEBoot_command_t cmd __attribute__ ((unused)), int argc, char *argv[]) { VasEBoot_size_t size = 0, aligned_size = 0; VasEBoot_addr_t addr_min, addr_max; VasEBoot_addr_t addr; VasEBoot_err_t err; struct VasEBoot_linux_initrd_context initrd_ctx = { 0, 0, 0 }; if (argc == 0) { VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, N_("filename expected")); goto fail; } if (! loaded) { VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, N_("you need to load the kernel first")); goto fail; } if (VasEBoot_initrd_init (argc, argv, &initrd_ctx)) goto fail; size = VasEBoot_get_initrd_size (&initrd_ctx); aligned_size = ALIGN_UP (size, 4096); /* Get the highest address available for the initrd. */ if (VasEBoot_le_to_cpu16 (linux_params.hdr.version) >= 0x0203) { addr_max = VasEBoot_cpu_to_le32 (linux_params.hdr.initrd_addr_max); /* XXX in reality, Linux specifies a bogus value, so it is necessary to make sure that ADDR_MAX does not exceed 0x3fffffff. */ if (addr_max > VAS_EBOOT_LINUX_INITRD_MAX_ADDRESS) addr_max = VAS_EBOOT_LINUX_INITRD_MAX_ADDRESS; } else addr_max = VAS_EBOOT_LINUX_INITRD_MAX_ADDRESS; if (linux_mem_size != 0 && linux_mem_size < addr_max) addr_max = linux_mem_size; /* Linux 2.3.xx has a bug in the memory range check, so avoid the last page. Linux 2.2.xx has a bug in the memory range check, which is worse than that of Linux 2.3.xx, so avoid the last 64kb. */ addr_max -= 0x10000; addr_min = (VasEBoot_addr_t) prot_mode_target + prot_init_space; /* Make sure the maximum address is able to store the initrd. */ if (addr_max < aligned_size) { VasEBoot_error (VAS_EBOOT_ERR_OUT_OF_RANGE, N_("the size of initrd is bigger than addr_max")); goto fail; } /* Put the initrd as high as possible, 4KiB aligned. */ addr = (addr_max - aligned_size) & ~0xFFF; VasEBoot_dprintf ("linux", "Initrd at addr 0x%" PRIxVAS_EBOOT_ADDR " which is expected in" " ranger 0x%" PRIxVAS_EBOOT_ADDR " ~ 0x%" PRIxVAS_EBOOT_ADDR "\n", addr, addr_min, addr_max); if (addr < addr_min) { VasEBoot_error (VAS_EBOOT_ERR_OUT_OF_RANGE, "the initrd is too big"); goto fail; } { VasEBoot_relocator_chunk_t ch; err = VasEBoot_relocator_alloc_chunk_align (relocator, &ch, addr_min, addr, aligned_size, 0x1000, VAS_EBOOT_RELOCATOR_PREFERENCE_HIGH, 1); if (err) goto fail; initrd_mem = get_virtual_current_address (ch); initrd_mem_target = get_physical_target_address (ch); } if (VasEBoot_initrd_load (&initrd_ctx, initrd_mem)) goto fail; VasEBoot_dprintf ("linux", "Initrd (%p) at 0x%" PRIxVAS_EBOOT_ADDR ", size=0x%" PRIxVAS_EBOOT_SIZE "\n", initrd_mem, initrd_mem_target, size); linux_params.hdr.ramdisk_image = initrd_mem_target; linux_params.hdr.ramdisk_size = size; linux_params.hdr.root_dev = 0x0100; /* XXX */ fail: VasEBoot_initrd_close (&initrd_ctx); return VasEBoot_errno; } #ifndef VAS_EBOOT_MACHINE_EFI static VasEBoot_command_t cmd_linux, cmd_initrd; VAS_EBOOT_MOD_INIT(linux) { cmd_linux = VasEBoot_register_command ("linux", VasEBoot_cmd_linux, 0, N_("Load Linux.")); cmd_initrd = VasEBoot_register_command ("initrd", VasEBoot_cmd_initrd, 0, N_("Load initrd.")); my_mod = mod; } VAS_EBOOT_MOD_FINI(linux) { VasEBoot_unregister_command (cmd_linux); VasEBoot_unregister_command (cmd_initrd); } #else extern VasEBoot_err_t __attribute__((alias("VasEBoot_cmd_linux"))) VasEBoot_cmd_linux_x86_legacy (VasEBoot_command_t cmd, int argc, char *argv[]); extern VasEBoot_err_t __attribute__((alias("VasEBoot_cmd_initrd"))) VasEBoot_cmd_initrd_x86_legacy (VasEBoot_command_t cmd, int argc, char *argv[]); #endif