From 95c7fc3f55cca3c6fc75efe7cc098427dd433fef Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 10 Jan 2010 00:30:33 +0100 Subject: [PATCH 001/178] First compileable newreloc --- conf/i386-pc.rmk | 7 +- conf/i386.rmk | 3 +- include/grub/i386/relocator.h | 6 +- include/grub/misc.h | 2 + include/grub/mm_private.h | 62 ++++ include/grub/relocator.h | 42 +++ include/grub/relocator_private.h | 58 ++++ kern/mm.c | 71 ++-- lib/i386/relocator.c | 162 ++++++---- lib/i386/relocator32.S | 158 +++++++++ lib/i386/relocator_asm.S | 216 ++----------- lib/relocator.c | 540 ++++++++++++++++++++++++++----- lib/x86_64/relocator_asm.S | 85 +++++ 13 files changed, 1007 insertions(+), 405 deletions(-) create mode 100644 include/grub/mm_private.h create mode 100644 include/grub/relocator.h create mode 100644 include/grub/relocator_private.h create mode 100644 lib/i386/relocator32.S create mode 100644 lib/x86_64/relocator_asm.S diff --git a/conf/i386-pc.rmk b/conf/i386-pc.rmk index afdf47e5a..234d18193 100644 --- a/conf/i386-pc.rmk +++ b/conf/i386-pc.rmk @@ -64,7 +64,7 @@ kernel_img_HEADERS = boot.h cache.h device.h disk.h dl.h elf.h elfload.h \ partition.h msdos_partition.h reader.h symbol.h term.h time.h types.h \ machine/biosdisk.h machine/boot.h machine/console.h machine/init.h \ machine/memory.h machine/loader.h machine/vga.h machine/vbe.h \ - machine/kernel.h machine/pxe.h i386/pit.h list.h handler.h command.h i18n.h + machine/kernel.h machine/pxe.h i386/pit.h list.h handler.h command.h i18n.h mm_private.h kernel_img_CFLAGS = $(COMMON_CFLAGS) $(TARGET_IMG_CFLAGS) kernel_img_ASFLAGS = $(COMMON_ASFLAGS) kernel_img_LDFLAGS = $(COMMON_LDFLAGS) $(TARGET_IMG_LDFLAGS)$(GRUB_KERNEL_MACHINE_LINK_ADDR) $(COMMON_CFLAGS) @@ -116,7 +116,7 @@ bin_SCRIPTS += grub-mkrescue grub_mkrescue_SOURCES = util/grub-mkrescue.in pkglib_MODULES = biosdisk.mod chain.mod \ - multiboot.mod reboot.mod halt.mod \ + reboot.mod halt.mod \ vbe.mod vbetest.mod vbeinfo.mod play.mod serial.mod \ vga.mod memdisk.mod pci.mod lspci.mod \ aout.mod bsd.mod pxe.mod pxecmd.mod datetime.mod date.mod \ @@ -179,7 +179,7 @@ linux_mod_SOURCES = loader/i386/linux.c linux_mod_CFLAGS = $(COMMON_CFLAGS) linux_mod_LDFLAGS = $(COMMON_LDFLAGS) -pkglib_MODULES += xnu.mod +#pkglib_MODULES += xnu.mod xnu_mod_SOURCES = loader/xnu_resume.c loader/i386/xnu.c loader/i386/pc/xnu.c \ loader/macho32.c loader/macho64.c loader/macho.c loader/xnu.c xnu_mod_CFLAGS = $(COMMON_CFLAGS) @@ -202,6 +202,7 @@ serial_mod_CFLAGS = $(COMMON_CFLAGS) serial_mod_LDFLAGS = $(COMMON_LDFLAGS) # For multiboot.mod. +#pkglib_MODULES += multiboot.mod multiboot_mod_SOURCES = loader/i386/multiboot.c \ loader/i386/multiboot_helper.S \ loader/i386/pc/multiboot2.c \ diff --git a/conf/i386.rmk b/conf/i386.rmk index c3f036d0f..674170d01 100644 --- a/conf/i386.rmk +++ b/conf/i386.rmk @@ -16,7 +16,8 @@ vga_text_mod_CFLAGS = $(COMMON_CFLAGS) vga_text_mod_LDFLAGS = $(COMMON_LDFLAGS) pkglib_MODULES += relocator.mod -relocator_mod_SOURCES = lib/i386/relocator.c lib/i386/relocator_asm.S lib/i386/relocator_backward.S +relocator_mod_SOURCES = lib/relocator.c lib/i386/relocator32.S \ + lib/i386/relocator_asm.S lib/i386/relocator.c relocator_mod_CFLAGS = $(COMMON_CFLAGS) relocator_mod_ASFLAGS = $(COMMON_ASFLAGS) relocator_mod_LDFLAGS = $(COMMON_LDFLAGS) diff --git a/include/grub/i386/relocator.h b/include/grub/i386/relocator.h index ef7fe23aa..2027a275c 100644 --- a/include/grub/i386/relocator.h +++ b/include/grub/i386/relocator.h @@ -21,6 +21,7 @@ #include #include +#include struct grub_relocator32_state { @@ -32,10 +33,7 @@ struct grub_relocator32_state grub_uint32_t eip; }; -void *grub_relocator32_alloc (grub_size_t size); -grub_err_t grub_relocator32_boot (void *relocator, grub_uint32_t dest, +grub_err_t grub_relocator32_boot (struct grub_relocator *rel, struct grub_relocator32_state state); -void *grub_relocator32_realloc (void *relocator, grub_size_t size); -void grub_relocator32_free (void *relocator); #endif /* ! GRUB_RELOCATOR_CPU_HEADER */ diff --git a/include/grub/misc.h b/include/grub/misc.h index c5eb953e2..b7af4afc4 100644 --- a/include/grub/misc.h +++ b/include/grub/misc.h @@ -43,6 +43,8 @@ #define ALIGN_UP(addr, align) \ ((addr + (typeof (addr)) align - 1) & ~((typeof (addr)) align - 1)) +#define ALIGN_DOWN(addr, align) \ + ((addr) & ~((typeof (addr)) align - 1)) #define ARRAY_SIZE(array) (sizeof (array) / sizeof (array[0])) #define COMPILE_TIME_ASSERT(cond) switch (0) { case 1: case !(cond): ; } diff --git a/include/grub/mm_private.h b/include/grub/mm_private.h new file mode 100644 index 000000000..2927f16c4 --- /dev/null +++ b/include/grub/mm_private.h @@ -0,0 +1,62 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2010 Free Software Foundation, Inc. + * + * GRUB 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. + * + * GRUB 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 GRUB. If not, see . + */ + +#ifndef GRUB_MM_PRIVATE_H +#define GRUB_MM_PRIVATE_H 1 + +#include + +/* Magic words. */ +#define GRUB_MM_FREE_MAGIC 0x2d3c2808 +#define GRUB_MM_ALLOC_MAGIC 0x6db08fa4 + +typedef struct grub_mm_header +{ + struct grub_mm_header *next; + grub_size_t size; + grub_size_t magic; +#if GRUB_CPU_SIZEOF_VOID_P == 4 + char padding[4]; +#elif GRUB_CPU_SIZEOF_VOID_P == 8 + char padding[8]; +#else +# error "unknown word size" +#endif +} +*grub_mm_header_t; + +#if GRUB_CPU_SIZEOF_VOID_P == 4 +# define GRUB_MM_ALIGN_LOG2 4 +#elif GRUB_CPU_SIZEOF_VOID_P == 8 +# define GRUB_MM_ALIGN_LOG2 5 +#endif + +#define GRUB_MM_ALIGN (1 << GRUB_MM_ALIGN_LOG2) + +typedef struct grub_mm_region +{ + struct grub_mm_header *first; + struct grub_mm_region *next; + grub_size_t pre_size; + grub_size_t size; +} +*grub_mm_region_t; + +extern grub_mm_region_t EXPORT_VAR (grub_mm_base); + +#endif diff --git a/include/grub/relocator.h b/include/grub/relocator.h new file mode 100644 index 000000000..2ea74b775 --- /dev/null +++ b/include/grub/relocator.h @@ -0,0 +1,42 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2009 Free Software Foundation, Inc. + * + * GRUB 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. + * + * GRUB 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 GRUB. If not, see . + */ + +#ifndef GRUB_RELOCATOR_HEADER +#define GRUB_RELOCATOR_HEADER 1 + +#include +#include + +struct grub_relocator; + +struct grub_relocator *grub_relocator_new (void); + +grub_err_t +grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, void **src, + grub_addr_t target, grub_size_t size); + +grub_err_t +grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, + grub_addr_t *target, + grub_addr_t min_addr, grub_addr_t max_addr, + grub_size_t size, grub_size_t align); + +void +grub_relocator_unload (struct grub_relocator *rel); + +#endif diff --git a/include/grub/relocator_private.h b/include/grub/relocator_private.h new file mode 100644 index 000000000..cc68305c8 --- /dev/null +++ b/include/grub/relocator_private.h @@ -0,0 +1,58 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2009 Free Software Foundation, Inc. + * + * GRUB 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. + * + * GRUB 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 GRUB. If not, see . + */ + +#ifndef GRUB_RELOCATOR_PRIVATE_HEADER +#define GRUB_RELOCATOR_PRIVATE_HEADER 1 + +#include +#include + +extern grub_size_t grub_relocator_align; +extern grub_size_t grub_relocator_forward_size; +extern grub_size_t grub_relocator_backward_size; +extern grub_size_t grub_relocator_jumper_size; + +struct grub_relocator +{ + struct grub_relocator_chunk *chunks; + grub_addr_t postchunks; + grub_addr_t highestaddr; + grub_addr_t highestnonpostaddr; + grub_size_t relocators_size; +}; + +struct grub_relocator_chunk +{ + struct grub_relocator_chunk *next; + grub_addr_t src; + grub_addr_t target; + grub_size_t size; +}; + +void +grub_cpu_relocator_init (void); +grub_err_t +grub_relocator_prepare_relocs (struct grub_relocator *rel, grub_addr_t addr, + grub_addr_t *relstart); +void grub_cpu_relocator_forward (void *rels, void *src, void *tgt, + grub_size_t size); +void grub_cpu_relocator_backward (void *rels, void *src, void *tgt, + grub_size_t size); +void grub_cpu_relocator_jumper (void *rels, grub_addr_t addr); + +#endif diff --git a/kern/mm.c b/kern/mm.c index ef97b018e..f1733f251 100644 --- a/kern/mm.c +++ b/kern/mm.c @@ -65,6 +65,7 @@ #include #include #include +#include #ifdef MM_DEBUG # undef grub_malloc @@ -74,45 +75,9 @@ # undef grub_memalign #endif -/* Magic words. */ -#define GRUB_MM_FREE_MAGIC 0x2d3c2808 -#define GRUB_MM_ALLOC_MAGIC 0x6db08fa4 - -typedef struct grub_mm_header -{ - struct grub_mm_header *next; - grub_size_t size; - grub_size_t magic; -#if GRUB_CPU_SIZEOF_VOID_P == 4 - char padding[4]; -#elif GRUB_CPU_SIZEOF_VOID_P == 8 - char padding[8]; -#else -# error "unknown word size" -#endif -} -*grub_mm_header_t; - -#if GRUB_CPU_SIZEOF_VOID_P == 4 -# define GRUB_MM_ALIGN_LOG2 4 -#elif GRUB_CPU_SIZEOF_VOID_P == 8 -# define GRUB_MM_ALIGN_LOG2 5 -#endif - -#define GRUB_MM_ALIGN (1 << GRUB_MM_ALIGN_LOG2) - -typedef struct grub_mm_region -{ - struct grub_mm_header *first; - struct grub_mm_region *next; - grub_addr_t addr; - grub_size_t size; -} -*grub_mm_region_t; - -static grub_mm_region_t base; +grub_mm_region_t grub_mm_base; /* Get a header from the pointer PTR, and set *P and *R to a pointer to the header and a pointer to its region, respectively. PTR must @@ -123,9 +88,9 @@ get_header_from_pointer (void *ptr, grub_mm_header_t *p, grub_mm_region_t *r) if ((grub_addr_t) ptr & (GRUB_MM_ALIGN - 1)) grub_fatal ("unaligned pointer %p", ptr); - for (*r = base; *r; *r = (*r)->next) - if ((grub_addr_t) ptr > (*r)->addr - && (grub_addr_t) ptr <= (*r)->addr + (*r)->size) + for (*r = grub_mm_base; *r; *r = (*r)->next) + if ((grub_addr_t) ptr > (grub_addr_t) ((*r) + 1) + && (grub_addr_t) ptr <= (grub_addr_t) ((*r) + 1) + (*r)->size) break; if (! *r) @@ -153,22 +118,21 @@ grub_mm_init_region (void *addr, grub_size_t size) return; /* Allocate a region from the head. */ - r = (grub_mm_region_t) (((grub_addr_t) addr + GRUB_MM_ALIGN - 1) - & (~(GRUB_MM_ALIGN - 1))); + r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN); size -= (char *) r - (char *) addr + sizeof (*r); - h = (grub_mm_header_t) ((char *) r + GRUB_MM_ALIGN); + h = (grub_mm_header_t) (r + 1); h->next = h; h->magic = GRUB_MM_FREE_MAGIC; h->size = (size >> GRUB_MM_ALIGN_LOG2); r->first = h; - r->addr = (grub_addr_t) h; + r->pre_size = (grub_addr_t) r - (grub_addr_t) addr; r->size = (h->size << GRUB_MM_ALIGN_LOG2); /* Find where to insert this region. Put a smaller one before bigger ones, to prevent fragmentation. */ - for (p = &base, q = *p; q; p = &(q->next), q = *p) + for (p = &grub_mm_base, q = *p; q; p = &(q->next), q = *p) if (q->size > r->size) break; @@ -268,13 +232,14 @@ grub_real_malloc (grub_mm_header_t *first, grub_size_t n, grub_size_t align) */ grub_mm_header_t r; + extra += (p->size - extra - n) & (~(align - 1)); r = p + extra + n; r->magic = GRUB_MM_FREE_MAGIC; r->size = p->size - extra - n; - r->next = p->next; + r->next = p; p->size = extra; - p->next = r; + q->next = r; p += extra; p->size = n; p->magic = GRUB_MM_ALLOC_MAGIC; @@ -310,7 +275,7 @@ grub_memalign (grub_size_t align, grub_size_t size) again: - for (r = base; r; r = r->next) + for (r = grub_mm_base; r; r = r->next) { void *p; @@ -471,7 +436,7 @@ grub_mm_dump_free (void) { grub_mm_region_t r; - for (r = base; r; r = r->next) + for (r = grub_mm_base; r; r = r->next) { grub_mm_header_t p; @@ -498,13 +463,13 @@ grub_mm_dump (unsigned lineno) grub_mm_region_t r; grub_printf ("called at line %u\n", lineno); - for (r = base; r; r = r->next) + for (r = grub_mm_base; r; r = r->next) { grub_mm_header_t p; - for (p = (grub_mm_header_t) ((r->addr + GRUB_MM_ALIGN - 1) - & (~(GRUB_MM_ALIGN - 1))); - (grub_addr_t) p < r->addr + r->size; + for (p = (grub_mm_header_t) ALIGN_UP ((grub_addr_t) (r + 1), + GRUB_MM_ALIGN); + (grub_addr_t) p < (grub_addr_t) (r+1) + r->size; p++) { switch (p->magic) diff --git a/lib/i386/relocator.c b/lib/i386/relocator.c index 453f73fdd..d4555e5f3 100644 --- a/lib/i386/relocator.c +++ b/lib/i386/relocator.c @@ -24,79 +24,115 @@ #include #include +#include -extern grub_uint8_t grub_relocator32_forward_start; -extern grub_uint8_t grub_relocator32_forward_end; -extern grub_uint8_t grub_relocator32_backward_start; -extern grub_uint8_t grub_relocator32_backward_end; +extern grub_uint8_t grub_relocator32_start; +extern grub_uint8_t grub_relocator32_end; +extern grub_uint8_t grub_relocator_forward_start; +extern grub_uint8_t grub_relocator_forward_end; +extern grub_uint8_t grub_relocator_backward_start; +extern grub_uint8_t grub_relocator_backward_end; -extern grub_uint32_t grub_relocator32_backward_dest; -extern grub_uint32_t grub_relocator32_backward_size; -extern grub_addr_t grub_relocator32_backward_src; +extern void *grub_relocator_backward_dest; +extern void *grub_relocator_backward_src; +extern grub_size_t grub_relocator_backward_size; -extern grub_uint32_t grub_relocator32_forward_dest; -extern grub_uint32_t grub_relocator32_forward_size; -extern grub_addr_t grub_relocator32_forward_src; +extern void *grub_relocator_forward_dest; +extern void *grub_relocator_forward_src; +extern grub_size_t grub_relocator_forward_size; -extern grub_uint32_t grub_relocator32_forward_eax; -extern grub_uint32_t grub_relocator32_forward_ebx; -extern grub_uint32_t grub_relocator32_forward_ecx; -extern grub_uint32_t grub_relocator32_forward_edx; -extern grub_uint32_t grub_relocator32_forward_eip; -extern grub_uint32_t grub_relocator32_forward_esp; +extern grub_uint32_t grub_relocator32_eax; +extern grub_uint32_t grub_relocator32_ebx; +extern grub_uint32_t grub_relocator32_ecx; +extern grub_uint32_t grub_relocator32_edx; +extern grub_uint32_t grub_relocator32_eip; +extern grub_uint32_t grub_relocator32_esp; -extern grub_uint32_t grub_relocator32_backward_eax; -extern grub_uint32_t grub_relocator32_backward_ebx; -extern grub_uint32_t grub_relocator32_backward_ecx; -extern grub_uint32_t grub_relocator32_backward_edx; -extern grub_uint32_t grub_relocator32_backward_eip; -extern grub_uint32_t grub_relocator32_backward_esp; +#define RELOCATOR_SIZEOF(x) (&grub_relocator##x##_end - &grub_relocator##x##_start) -#define RELOCATOR_SIZEOF(x) (&grub_relocator32_##x##_end - &grub_relocator32_##x##_start) -#define RELOCATOR_ALIGN 16 -#define PREFIX(x) grub_relocator32_ ## x +grub_size_t grub_relocator_align = 1; +grub_size_t grub_relocator_forward_size; +grub_size_t grub_relocator_backward_size; +grub_size_t grub_relocator_jumper_size = 10; -static void -write_call_relocator_bw (void *ptr, void *src, grub_uint32_t dest, - grub_size_t size, struct grub_relocator32_state state) +void +grub_cpu_relocator_init (void) { - grub_relocator32_backward_dest = dest; - grub_relocator32_backward_src = PTR_TO_UINT64 (src); - grub_relocator32_backward_size = size; - - grub_relocator32_backward_eax = state.eax; - grub_relocator32_backward_ebx = state.ebx; - grub_relocator32_backward_ecx = state.ecx; - grub_relocator32_backward_edx = state.edx; - grub_relocator32_backward_eip = state.eip; - grub_relocator32_backward_esp = state.esp; - - grub_memmove (ptr, - &grub_relocator32_backward_start, - RELOCATOR_SIZEOF (backward)); - ((void (*) (void)) ptr) (); + grub_relocator_forward_size = RELOCATOR_SIZEOF(_forward); + grub_relocator_backward_size = RELOCATOR_SIZEOF(_backward); } -static void -write_call_relocator_fw (void *ptr, void *src, grub_uint32_t dest, - grub_size_t size, struct grub_relocator32_state state) +void +grub_cpu_relocator_jumper (void *rels, grub_addr_t addr) { - - grub_relocator32_forward_dest = dest; - grub_relocator32_forward_src = PTR_TO_UINT64 (src); - grub_relocator32_forward_size = size; - - grub_relocator32_forward_eax = state.eax; - grub_relocator32_forward_ebx = state.ebx; - grub_relocator32_forward_ecx = state.ecx; - grub_relocator32_forward_edx = state.edx; - grub_relocator32_forward_eip = state.eip; - grub_relocator32_forward_esp = state.esp; - - grub_memmove (ptr, - &grub_relocator32_forward_start, - RELOCATOR_SIZEOF (forward)); - ((void (*) (void)) ptr) (); + grub_uint8_t *ptr; + ptr = rels; + /* jmp $addr */ + *(grub_uint8_t *) ptr = 0xe9; + ptr++; + *(grub_uint32_t *) ptr = addr - (grub_uint32_t) (ptr + 4); + ptr += 4; + /* movl $addr, %eax (for relocator) */ + *(grub_uint8_t *) ptr = 0xb8; + ptr++; + *(grub_uint32_t *) ptr = addr; } -#include "../relocator.c" +void +grub_cpu_relocator_backward (void *ptr, void *src, void *dest, + grub_size_t size) +{ + grub_relocator_backward_dest = dest; + grub_relocator_backward_src = src; + grub_relocator_backward_size = size; + + grub_memmove (ptr, + &grub_relocator_backward_start, + RELOCATOR_SIZEOF (_backward)); +} + +void +grub_cpu_relocator_forward (void *ptr, void *src, void *dest, + grub_size_t size) +{ + grub_relocator_forward_dest = dest; + grub_relocator_forward_src = src; + grub_relocator_forward_size = size; + + grub_memmove (ptr, + &grub_relocator_forward_start, + RELOCATOR_SIZEOF (_forward)); +} + +grub_err_t +grub_relocator32_boot (struct grub_relocator *rel, + struct grub_relocator32_state state) +{ + grub_addr_t target; + void *src; + grub_err_t err; + grub_addr_t relst; + err = grub_relocator_alloc_chunk_align (rel, &src, &target, 0, + (0xffffffff - RELOCATOR_SIZEOF (32)) + + 1, RELOCATOR_SIZEOF (32), 16); + if (err) + return err; + + grub_relocator32_eax = state.eax; + grub_relocator32_ebx = state.ebx; + grub_relocator32_ecx = state.ecx; + grub_relocator32_edx = state.edx; + grub_relocator32_eip = state.eip; + grub_relocator32_esp = state.esp; + + grub_memmove (src, &grub_relocator32_start, RELOCATOR_SIZEOF (32)); + + err = grub_relocator_prepare_relocs (rel, target, &relst); + if (err) + return err; + asm volatile ("cli"); + ((void (*) (void)) relst) (); + + /* Not reached. */ + return GRUB_ERR_NONE; +} diff --git a/lib/i386/relocator32.S b/lib/i386/relocator32.S new file mode 100644 index 000000000..f69e0bdc8 --- /dev/null +++ b/lib/i386/relocator32.S @@ -0,0 +1,158 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2009,2010 Free Software Foundation, Inc. + * + * GRUB 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. + * + * GRUB 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 GRUB. If not, see . + */ + +#include +#include + +#ifdef __x86_64__ +#define RAX %rax +#define RSI %rdi +#else +#define RAX %eax +#define RSI %esi +#endif + +/* The code segment of the protected mode. */ +#define CODE_SEGMENT 0x10 + +/* The data segment of the protected mode. */ +#define DATA_SEGMENT 0x18 + + .p2align 4 /* force 16-byte alignment */ + +VARIABLE(grub_relocator32_start) +LOCAL(base): + /* %rax contains now our new 'base'. */ + mov RAX, RSI + add $(LOCAL(cont0) - LOCAL(base)), RAX + jmp *RAX +LOCAL(cont0): + lea (LOCAL(cont1) - LOCAL(base)) (RSI, 1), RAX + movl %eax, (LOCAL(jump_vector) - LOCAL(base)) (RSI, 1) + + lea (LOCAL(gdt) - LOCAL(base)) (RSI, 1), RAX + mov RAX, (LOCAL(gdt_addr) - LOCAL(base)) (RSI, 1) + + /* Switch to compatibility mode. */ + + lgdt (LOCAL(gdtdesc) - LOCAL(base)) (RSI, 1) + + /* Update %cs. */ + ljmp *(LOCAL(jump_vector) - LOCAL(base)) (RSI, 1) + +LOCAL(cont1): + .code32 + + /* Update other registers. */ + movl $DATA_SEGMENT, %eax + movl %eax, %ds + movl %eax, %es + movl %eax, %fs + movl %eax, %gs + movl %eax, %ss + + /* Disable paging. */ + movl %cr0, %eax + andl $(~GRUB_MEMORY_CPU_CR0_PAGING_ON), %eax + movl %eax, %cr0 + + /* Disable amd64. */ + movl $GRUB_MEMORY_CPU_AMD64_MSR, %ecx + rdmsr + andl $(~GRUB_MEMORY_CPU_AMD64_MSR_ON), %eax + wrmsr + + /* Turn off PAE. */ + movl %cr4, %eax + andl $GRUB_MEMORY_CPU_CR4_PAE_ON, %eax + movl %eax, %cr4 + + jmp LOCAL(cont2) +LOCAL(cont2): + .code32 + + /* mov imm32, %eax */ + .byte 0xb8 +VARIABLE(grub_relocator32_esp) + .long 0 + + movl %eax, %esp + + /* mov imm32, %eax */ + .byte 0xb8 +VARIABLE(grub_relocator32_eax) + .long 0 + + /* mov imm32, %ebx */ + .byte 0xbb +VARIABLE(grub_relocator32_ebx) + .long 0 + + /* mov imm32, %ecx */ + .byte 0xb9 +VARIABLE(grub_relocator32_ecx) + .long 0 + + /* mov imm32, %edx */ + .byte 0xba +VARIABLE(grub_relocator32_edx) + .long 0 + + /* Cleared direction flag is of no problem with any current + payload and makes this implementation easier. */ + cld + + .byte 0xea +VARIABLE(grub_relocator32_eip) + .long 0 + .word CODE_SEGMENT + + /* GDT. Copied from loader/i386/linux.c. */ + .p2align 4 +LOCAL(gdt): + /* NULL. */ + .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + + /* Reserved. */ + .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + + /* Code segment. */ + .byte 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x9A, 0xCF, 0x00 + + /* Data segment. */ + .byte 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x92, 0xCF, 0x00 + + .p2align 4 +LOCAL(gdtdesc): + .word 0x27 +LOCAL(gdt_addr): +#ifdef __x86_64__ + /* Filled by the code. */ + .quad 0 +#else + /* Filled by the code. */ + .long 0 +#endif + + .p2align 4 +LOCAL(jump_vector): + /* Jump location. Is filled by the code */ + .long 0 + .long CODE_SEGMENT + +VARIABLE(grub_relocator32_end) diff --git a/lib/i386/relocator_asm.S b/lib/i386/relocator_asm.S index 6b803db13..f8fc0c08a 100644 --- a/lib/i386/relocator_asm.S +++ b/lib/i386/relocator_asm.S @@ -19,230 +19,60 @@ #include #include -#ifdef BACKWARD -#define RELOCATOR_VARIABLE(x) VARIABLE(grub_relocator32_backward_ ## x) -#else -#define RELOCATOR_VARIABLE(x) VARIABLE(grub_relocator32_forward_ ## x) -#endif -#ifdef __x86_64__ -#define RAX %rax -#define RCX %rcx -#define RDI %rdi -#define RSI %rdi -#else -#define RAX %eax -#define RCX %ecx -#define RDI %edi -#define RSI %esi -#endif - -/* The code segment of the protected mode. */ -#define CODE_SEGMENT 0x10 - -/* The data segment of the protected mode. */ -#define DATA_SEGMENT 0x18 - - .p2align 4 /* force 16-byte alignment */ - -RELOCATOR_VARIABLE(start) -#ifdef BACKWARD -LOCAL(base): -#endif - cli - -#ifndef __x86_64__ +VARIABLE(grub_relocator_backward_start) /* mov imm32, %eax */ .byte 0xb8 -RELOCATOR_VARIABLE(dest) +VARIABLE(grub_relocator_backward_dest) .long 0 movl %eax, %edi /* mov imm32, %eax */ .byte 0xb8 -RELOCATOR_VARIABLE(src) +VARIABLE(grub_relocator_backward_src) .long 0 movl %eax, %esi /* mov imm32, %ecx */ .byte 0xb9 -RELOCATOR_VARIABLE(size) +VARIABLE(grub_relocator_backward_size) .long 0 -#else - xorq %rax, %rax - - /* mov imm32, %eax */ - .byte 0xb8 -RELOCATOR_VARIABLE(dest) - .long 0 - movq %rax, %rdi - - /* mov imm64, %rax */ - .byte 0x48 - .byte 0xb8 -RELOCATOR_VARIABLE(src) - .long 0, 0 - movq %rax, %rsi - - xorq %rcx, %rcx - /* mov imm32, %ecx */ - .byte 0xb9 -RELOCATOR_VARIABLE(size) - .long 0 - -#endif - - mov RDI, RAX - -#ifdef BACKWARD - add RCX, RSI - add RCX, RDI -#endif - -#ifndef BACKWARD - add RCX, RAX -#endif - add $0x3, RCX - shr $2, RCX + + add %ecx, %esi + add %ecx, %edi -#ifdef BACKWARD - /* Backward movsl is implicitly off-by-four. compensate that. */ - sub $4, RSI - sub $4, RDI + /* Backward movsb is implicitly off-by-one. compensate that. */ + sub $1, %esi + sub $1, %edi /* Backward copy. */ std rep - movsl + movsb +VARIABLE(grub_relocator_backward_end) -#else - /* Forward copy. */ - cld - rep - movsl -#endif - - /* %rax contains now our new 'base'. */ - mov RAX, RSI - add $(LOCAL(cont0) - LOCAL(base)), RAX - jmp *RAX -LOCAL(cont0): - lea (LOCAL(cont1) - LOCAL(base)) (RSI, 1), RAX - movl %eax, (LOCAL(jump_vector) - LOCAL(base)) (RSI, 1) - - lea (LOCAL(gdt) - LOCAL(base)) (RSI, 1), RAX - mov RAX, (LOCAL(gdt_addr) - LOCAL(base)) (RSI, 1) - - /* Switch to compatibility mode. */ - - lgdt (LOCAL(gdtdesc) - LOCAL(base)) (RSI, 1) - - /* Update %cs. */ - ljmp *(LOCAL(jump_vector) - LOCAL(base)) (RSI, 1) - -LOCAL(cont1): - .code32 - - /* Update other registers. */ - movl $DATA_SEGMENT, %eax - movl %eax, %ds - movl %eax, %es - movl %eax, %fs - movl %eax, %gs - movl %eax, %ss - - /* Disable paging. */ - movl %cr0, %eax - andl $(~GRUB_MEMORY_CPU_CR0_PAGING_ON), %eax - movl %eax, %cr0 - - /* Disable amd64. */ - movl $GRUB_MEMORY_CPU_AMD64_MSR, %ecx - rdmsr - andl $(~GRUB_MEMORY_CPU_AMD64_MSR_ON), %eax - wrmsr - - /* Turn off PAE. */ - movl %cr4, %eax - andl $GRUB_MEMORY_CPU_CR4_PAE_ON, %eax - movl %eax, %cr4 - - jmp LOCAL(cont2) -LOCAL(cont2): - .code32 +VARIABLE(grub_relocator_forward_start) /* mov imm32, %eax */ .byte 0xb8 -RELOCATOR_VARIABLE (esp) +VARIABLE(grub_relocator_forward_dest) .long 0 + movl %eax, %edi - movl %eax, %esp - - /* mov imm32, %eax */ + /* mov imm32, %rax */ .byte 0xb8 -RELOCATOR_VARIABLE (eax) - .long 0 - - /* mov imm32, %ebx */ - .byte 0xbb -RELOCATOR_VARIABLE (ebx) +VARIABLE(grub_relocator_forward_src) .long 0 + movl %eax, %esi /* mov imm32, %ecx */ .byte 0xb9 -RELOCATOR_VARIABLE (ecx) +VARIABLE(grub_relocator_forward_size) .long 0 - /* mov imm32, %edx */ - .byte 0xba -RELOCATOR_VARIABLE (edx) - .long 0 - - /* Cleared direction flag is of no problem with any current - payload and makes this implementation easier. */ + /* Forward copy. */ cld - - .byte 0xea -RELOCATOR_VARIABLE (eip) - .long 0 - .word CODE_SEGMENT - - /* GDT. Copied from loader/i386/linux.c. */ - .p2align 4 -LOCAL(gdt): - /* NULL. */ - .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 - - /* Reserved. */ - .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 - - /* Code segment. */ - .byte 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x9A, 0xCF, 0x00 - - /* Data segment. */ - .byte 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x92, 0xCF, 0x00 - - .p2align 4 -LOCAL(gdtdesc): - .word 0x27 -LOCAL(gdt_addr): -#ifdef __x86_64__ - /* Filled by the code. */ - .quad 0 -#else - /* Filled by the code. */ - .long 0 -#endif - - .p2align 4 -LOCAL(jump_vector): - /* Jump location. Is filled by the code */ - .long 0 - .long CODE_SEGMENT - -#ifndef BACKWARD -LOCAL(base): -#endif - -RELOCATOR_VARIABLE(end) + rep + movsb +VARIABLE(grub_relocator_forward_end) diff --git a/lib/relocator.c b/lib/relocator.c index 6a5acc548..25ebd27bb 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -16,122 +16,486 @@ * along with GRUB. If not, see . */ -#define MAX_OVERHEAD ((RELOCATOR_SIZEOF (forward) + RELOCATOR_ALIGN) \ - + (RELOCATOR_SIZEOF (backward) + RELOCATOR_ALIGN) \ - + (RELOCATOR_SIZEOF (forward) + RELOCATOR_ALIGN) \ - + (RELOCATOR_SIZEOF (backward) + RELOCATOR_ALIGN)) -#define PRE_REGION_SIZE (RELOCATOR_SIZEOF (backward) + RELOCATOR_ALIGN) +#include +#include +#include +#include -void * -PREFIX (alloc) (grub_size_t size) +/* TODO: use more efficient data structures if necessary. */ + +struct grub_relocator * +grub_relocator_new (void) { - char *playground; + struct grub_relocator *ret; - playground = grub_malloc (size + MAX_OVERHEAD); - if (!playground) - return 0; + grub_cpu_relocator_init (); - *(grub_size_t *) playground = size; - - return playground + PRE_REGION_SIZE; + ret = grub_zalloc (sizeof (struct grub_relocator)); + if (!ret) + return NULL; + + ret->postchunks = ~(grub_addr_t) 0; + ret->relocators_size = grub_relocator_jumper_size; + return ret; } -void * -PREFIX (realloc) (void *relocator, grub_size_t size) +static grub_mm_header_t +get_best_header (struct grub_relocator *rel, + grub_addr_t start, grub_addr_t end, grub_addr_t align, + grub_size_t size, + grub_mm_region_t rb, grub_mm_header_t *prev, + grub_addr_t *best_addr, int from_low_priv, int collisioncheck) { - char *playground; + grub_mm_header_t h, hp; + grub_mm_header_t hb = NULL, hbp = NULL; - if (!relocator) - return PREFIX (alloc) (size); + auto void try_addr (grub_addr_t allowable_start, grub_addr_t allowable_end); + void try_addr (grub_addr_t allowable_start, grub_addr_t allowable_end) + { + if (from_low_priv) + { + grub_addr_t addr; - playground = (char *) relocator - PRE_REGION_SIZE; + addr = ALIGN_UP (allowable_start, align); - playground = grub_realloc (playground, size + MAX_OVERHEAD); - if (!playground) - return 0; + if (addr < start) + addr = ALIGN_UP (start, align); - *(grub_size_t *) playground = size; + if (collisioncheck) + while (1) + { + struct grub_relocator_chunk *chunk; + for (chunk = rel->chunks; chunk; chunk = chunk->next) + if ((chunk->target <= addr + && addr < chunk->target + chunk->size) + || (chunk->target <= addr + size + && addr + size < chunk->target + chunk->size) + || (addr <= chunk->target && chunk->target < addr + size) + || (addr <= chunk->target + chunk->size + && chunk->target + chunk->size < addr + size)) + { + addr = ALIGN_UP (chunk->target + chunk->size, align); + break; + } + if (!chunk) + break; + } - return playground + PRE_REGION_SIZE; + if (allowable_end <= addr + size) + return; + + if (addr > end) + return; + + if (hb == NULL || *best_addr > addr) + { + hb = h; + hbp = hp; + *best_addr = addr; + } + } + else + { + grub_addr_t addr; + + addr = ALIGN_DOWN (allowable_end - size, align); + + if (addr > end) + addr = ALIGN_DOWN (end, align); + + if (collisioncheck) + while (1) + { + struct grub_relocator_chunk *chunk; + for (chunk = rel->chunks; chunk; chunk = chunk->next) + if ((chunk->target <= addr + && addr < chunk->target + chunk->size) + || (chunk->target <= addr + size + && addr + size < chunk->target + chunk->size) + || (addr <= chunk->target && chunk->target < addr + size) + || (addr <= chunk->target + chunk->size + && chunk->target + chunk->size < addr + size)) + { + addr = ALIGN_DOWN (chunk->target - size, align); + break; + } + if (!chunk) + break; + } + + if (allowable_start > addr) + return; + + if (addr < start) + return; + + if (hb == NULL || *best_addr < addr) + { + hb = h; + hbp = hp; + *best_addr = addr; + } + } + } + + for (hp = NULL, h = rb->first; h; hp = h, h = h->next) + { + grub_addr_t allowable_start, allowable_end; + allowable_start = (grub_addr_t) h; + allowable_end = (grub_addr_t) (h + 1 + h->size); + + try_addr (allowable_start, allowable_end); + + if ((grub_addr_t) h == (grub_addr_t) (rb + 1)) + try_addr (allowable_start - sizeof (*rb) - rb->pre_size, + allowable_end - sizeof (*rb)); + } + *prev = hbp; + return hb; } -void -PREFIX(free) (void *relocator) +static int +malloc_in_range (struct grub_relocator *rel, + grub_addr_t start, grub_addr_t end, grub_addr_t align, + grub_size_t size, grub_addr_t *res, int from_low_priv, + int collisioncheck) { - if (relocator) - grub_free ((char *) relocator - PRE_REGION_SIZE); + grub_mm_region_t rb = NULL, rbp = NULL; + grub_mm_header_t hb = NULL, hbp = NULL; + grub_addr_t best_addr; + + again: + + { + grub_mm_region_t r, rp; + for (rp = NULL, r = grub_mm_base; r; rp = r, r = r->next) + { + if ((grub_addr_t) r + r->size + sizeof (*r) > start + && (grub_addr_t) r <= end && r->size + sizeof (*r) >= size + && (rb == NULL || from_low_priv ? rb > r : rb < r)) + { + rb = r; + rbp = rp; + } + } + } + + if (!rb) + return 0; + + hb = get_best_header (rel, start, end, align, size, rb, &hbp, &best_addr, + from_low_priv, collisioncheck); + if (!hb) + { + if (from_low_priv) + start = (grub_addr_t) (rb + rb->size + sizeof (*rb)); + else + end = (grub_addr_t) rb - 1; + goto again; + } + + /* Special case: relocating region start. */ + if (best_addr < (grub_addr_t) hbp) + { + grub_addr_t newreg_start, newreg_raw_start = best_addr + size; + grub_addr_t newreg_size, newreg_presize; + grub_mm_header_t new_header; + newreg_start = ALIGN_UP (newreg_raw_start, GRUB_MM_ALIGN); + newreg_presize = newreg_start - newreg_raw_start; + newreg_size = rb->size - (newreg_start - (grub_addr_t) rb); + if ((hb->size << GRUB_MM_ALIGN_LOG2) >= newreg_start + + (grub_addr_t) rb) + { + grub_mm_header_t newhnext = hb->next; + grub_size_t newhsize = ((hb->size << GRUB_MM_ALIGN_LOG2) + - newreg_start + - (grub_addr_t) rb) >> GRUB_MM_ALIGN_LOG2; + new_header = (void *) (newreg_start + sizeof (*rb)); + new_header->next = newhnext; + new_header->size = newhsize; + new_header->magic = GRUB_MM_FREE_MAGIC; + } + else + { + new_header = hb->next; + } + if (hbp || new_header) + { + struct grub_mm_header *newregfirst = rb->first; + struct grub_mm_region *newregnext = rb->next; + struct grub_mm_region *newreg = (void *) newreg_start; + if (hbp) + hbp->next = new_header; + else + newregfirst = new_header; + newreg->first = newregfirst; + newreg->next = newregnext; + newreg->pre_size = newreg_presize; + newreg->size = newreg_size; + if (rbp) + rbp->next = newreg; + else + grub_mm_base = newreg; + } + else + { + if (rbp) + rbp->next = rb->next; + else + grub_mm_base = rb->next; + } + *res = best_addr; + return 1; + } + { + struct grub_mm_header *foll = NULL; + + if (best_addr + size <= (grub_addr_t) (hb + hb->size)) + { + foll = (void *) ALIGN_UP (best_addr + size, GRUB_MM_ALIGN); + foll->magic = GRUB_MM_FREE_MAGIC; + foll->size = hb->size - (foll - hb); + } + + if (best_addr - (grub_addr_t) hb >= sizeof (*hb)) + { + hb->size = (best_addr - (grub_addr_t) hb) >> GRUB_MM_ALIGN_LOG2; + if (foll) + { + foll->next = hb; + if (hbp) + hbp->next = foll; + else + rb->first = foll; + } + } + else + { + if (foll) + foll->next = hb->next; + else + foll = hb->next; + if (hbp) + hbp->next = foll; + else + rb->first = foll; + } + *res = best_addr; + return 1; + } } grub_err_t -PREFIX (boot) (void *relocator, grub_uint32_t dest, - struct grub_relocator32_state state) +grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, void **src, + grub_addr_t target, grub_size_t size) { - grub_size_t size; - char *playground; + struct grub_relocator_chunk *chunk; + grub_addr_t start; + grub_addr_t min_addr = 0, max_addr; - playground = (char *) relocator - PRE_REGION_SIZE; - size = *(grub_size_t *) playground; + max_addr = rel->postchunks; - grub_dprintf ("relocator", - "Relocator: source: %p, destination: 0x%x, size: 0x%lx\n", - relocator, (unsigned) dest, (unsigned long) size); - - /* Very unlikely condition: Relocator may risk overwrite itself. - Just move it a bit up. */ - if ((grub_addr_t) dest < (grub_addr_t) relocator - + (RELOCATOR_SIZEOF (backward) + RELOCATOR_ALIGN) - && (grub_addr_t) dest + (RELOCATOR_SIZEOF (forward) + RELOCATOR_ALIGN) - > (grub_addr_t) relocator) + /* Keep chunks in memory in the same order as they'll be after relocation. */ + for (chunk = rel->chunks; chunk; chunk = chunk->next) { - void *relocator_new = ((grub_uint8_t *) relocator) - + (RELOCATOR_SIZEOF (forward) + RELOCATOR_ALIGN) - + (RELOCATOR_SIZEOF (backward) + RELOCATOR_ALIGN); - grub_dprintf ("relocator", "Overwrite condition detected moving " - "relocator from %p to %p\n", relocator, relocator_new); - grub_memmove (relocator_new, relocator, - (RELOCATOR_SIZEOF (forward) + RELOCATOR_ALIGN) - + size - + (RELOCATOR_SIZEOF (backward) + RELOCATOR_ALIGN)); - relocator = relocator_new; + if (chunk->target > target && chunk->src > max_addr) + max_addr = chunk->src; + if (chunk->target + chunk->size <= target + && chunk->src + chunk->size < min_addr + && chunk->src < rel->postchunks) + min_addr = chunk->src + chunk->size; + if ((chunk->target <= target && target < chunk->target + chunk->size) + || (target <= chunk->target && chunk->target < target + size)) + { + return grub_error (GRUB_ERR_BAD_ARGUMENT, "overlap detected"); + } } - if ((grub_addr_t) dest >= (grub_addr_t) relocator) - { - int overhead; - overhead = dest - - ALIGN_UP (dest - RELOCATOR_SIZEOF (backward) - RELOCATOR_ALIGN, - RELOCATOR_ALIGN); - grub_dprintf ("relocator", - "Backward relocator: code %p, source: %p, " - "destination: 0x%x, size: 0x%lx\n", - (char *) relocator - overhead, - (char *) relocator - overhead, - (unsigned) dest - overhead, - (unsigned long) size + overhead); + chunk = grub_malloc (sizeof (struct grub_relocator_chunk)); + if (!chunk) + return grub_errno; - write_call_relocator_bw ((char *) relocator - overhead, - (char *) relocator - overhead, - dest - overhead, size + overhead, state); + do + { + /* A trick to improve Linux allocation. */ +#if defined (__i386__) || defined (__x86_64__) + if (target < 0x100000) + if (malloc_in_range (rel, rel->highestnonpostaddr, ~(grub_addr_t)0, 0, + size, &start, 1, 0)) + { + if (rel->postchunks < start) + rel->postchunks = start; + break; + } +#endif + if (malloc_in_range (rel, target, max_addr, 1, size, &start, 1, 0)) + break; + + if (malloc_in_range (rel, min_addr, target, 0, size, &start, 1, 0)) + break; + + grub_free (chunk); + return grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory"); } - else + while (0); + + if (rel->highestaddr < target + size) + rel->highestaddr = target + size; + + if (rel->highestaddr < start + size) + rel->highestaddr = start + size; + + if (start < rel->postchunks) { - int overhead; - - overhead = ALIGN_UP (dest + size, RELOCATOR_ALIGN) - + RELOCATOR_SIZEOF (forward) - (dest + size); - grub_dprintf ("relocator", - "Forward relocator: code %p, source: %p, " - "destination: 0x%x, size: 0x%lx\n", - (char *) relocator + size + overhead - - RELOCATOR_SIZEOF (forward), - relocator, (unsigned) dest, - (unsigned long) size + overhead); - - write_call_relocator_fw ((char *) relocator + size + overhead - - RELOCATOR_SIZEOF (forward), - relocator, dest, size + overhead, state); + if (rel->highestnonpostaddr < target + size) + rel->highestnonpostaddr = target + size; + + if (rel->highestnonpostaddr < start + size) + rel->highestnonpostaddr = start + size; } - /* Not reached. */ + if (start < target) + rel->relocators_size += grub_relocator_backward_size; + if (start > target) + rel->relocators_size += grub_relocator_forward_size; + + chunk->src = start; + chunk->target = target; + chunk->size = size; + chunk->next = rel->chunks; + rel->chunks = chunk; + *src = (void *) start; + return GRUB_ERR_NONE; +} + +grub_err_t +grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, + grub_addr_t *target, + grub_addr_t min_addr, grub_addr_t max_addr, + grub_size_t size, grub_size_t align) +{ + grub_addr_t min_addr2 = 0, max_addr2; + struct grub_relocator_chunk *chunk; + grub_addr_t start; + + chunk = grub_malloc (sizeof (struct grub_relocator_chunk)); + if (!chunk) + return grub_errno; + + if (malloc_in_range (rel, min_addr, max_addr, align, + size, &start, 1, 1)) + { + chunk->src = start; + chunk->target = start; + chunk->size = size; + chunk->next = rel->chunks; + rel->chunks = chunk; + *src = (void *) start; + *target = start; + return GRUB_ERR_NONE; + } + + max_addr2 = rel->postchunks; + + /* Keep chunks in memory in the same order as they'll be after + relocation. */ + for (chunk = rel->chunks; chunk; chunk = chunk->next) + { + if (chunk->target > max_addr && chunk->src > max_addr2) + max_addr2 = chunk->src; + if (chunk->target + chunk->size <= min_addr + && chunk->src + chunk->size < min_addr2 + && chunk->src < rel->postchunks) + min_addr2 = chunk->src + chunk->size; + } + + if (!malloc_in_range (rel, min_addr2, max_addr2, align, + size, &start, 1, 1)) + { + grub_free (chunk); + return grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory"); + } + + chunk->target = ALIGN_UP (min_addr, align); + while (1) + { + struct grub_relocator_chunk *chunk2; + for (chunk2 = rel->chunks; chunk2; chunk2 = chunk2->next) + if ((chunk2->target <= chunk->target + && chunk->target < chunk2->target + chunk2->size) + || (chunk2->target <= chunk->target + size + && chunk->target + size < chunk2->target + chunk2->size) + || (chunk->target <= chunk2->target && chunk2->target + < chunk->target + size) + || (chunk->target <= chunk2->target + chunk2->size + && chunk2->target + chunk2->size < chunk->target + size)) + { + chunk->target = ALIGN_UP (chunk2->target + chunk2->size, align); + break; + } + if (!chunk2) + break; + } + + if (start < chunk->target) + rel->relocators_size += grub_relocator_backward_size; + if (start > chunk->target) + rel->relocators_size += grub_relocator_forward_size; + + chunk->src = start; + chunk->size = size; + chunk->next = rel->chunks; + rel->chunks = chunk; + *src = (void *) start; + *target = chunk->target; + return GRUB_ERR_NONE; +} + +void +grub_relocator_unload (struct grub_relocator *rel) +{ + struct grub_relocator_chunk *chunk, *next; + for (chunk = rel->chunks; chunk; chunk = next) + { + grub_fatal ("Relocator unloading isn't implemented yet"); + next = chunk->next; + grub_free (chunk); + } +} + +grub_err_t +grub_relocator_prepare_relocs (struct grub_relocator *rel, grub_addr_t addr, + grub_addr_t *relstart) +{ + struct grub_relocator_chunk *chunk; + grub_addr_t rels; + grub_addr_t rels0; + + if (!malloc_in_range (rel, 0, ~(grub_addr_t)0, grub_relocator_align, + rel->relocators_size, &rels0, 1, 1)) + return grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory"); + rels = rels0; + + for (chunk = rel->chunks; chunk; chunk = chunk->next) + { + if (chunk->src < chunk->target) + { + grub_cpu_relocator_backward ((void *) rels, + (void *) chunk->src, + (void *) chunk->target, + chunk->size); + rels += grub_relocator_backward_size; + } + if (chunk->src > chunk->target) + { + grub_cpu_relocator_forward ((void *) rels, + (void *) chunk->src, + (void *) chunk->target, + chunk->size); + rels += grub_relocator_forward_size; + } + } + grub_cpu_relocator_jumper ((void *) rels, addr); + *relstart = rels0; return GRUB_ERR_NONE; } diff --git a/lib/x86_64/relocator_asm.S b/lib/x86_64/relocator_asm.S new file mode 100644 index 000000000..6db44f2f7 --- /dev/null +++ b/lib/x86_64/relocator_asm.S @@ -0,0 +1,85 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2009 Free Software Foundation, Inc. + * + * GRUB 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. + * + * GRUB 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 GRUB. If not, see . + */ + +#include +#include + +VARIABLE(grub_relocator_backward_start) + /* mov imm32, %rax */ + .byte 0x48 + .byte 0xb8 +RELOCATOR_VARIABLE(dest) + .long 0, 0 + movq %rax, %rdi + + /* mov imm64, %rax */ + .byte 0x48 + .byte 0xb8 +RELOCATOR_VARIABLE(src) + .long 0, 0 + movq %rax, %rsi + + /* mov imm32, %ecx */ + .byte 0x48 + .byte 0xb9 +RELOCATOR_VARIABLE(size) + .long 0, 0 + + add %rcx, %rsi + add %rcx, %rdi + + + /* Backward movsb is implicitly off-by-one. compensate that. */ + sub $1, %rsi + sub $1, %rdi + + /* Backward copy. */ + std + + rep + movsb +VARIABLE(grub_relocator_backward_end) + + +VARIABLE(grub_relocator_forward_start) + /* mov imm64, %rax */ + .byte 0x48 + .byte 0xb8 +VARIABLE(grub_relocator_forward_dest) + .long 0, 0 + movq %rax, %rdi + + /* mov imm64, %rax */ + .byte 0x48 + .byte 0xb8 +VARIABLE(grub_relocator_forward_src) + .long 0, 0 + movq %rax, %rsi + + xorq %rcx, %rcx + /* mov imm64, %rcx */ + .byte 0x48 + .byte 0xb9 +VARIABLE(grub_relocator_forward_size) + .long 0, 0 + + /* Forward copy. */ + cld + rep + movsb +VARIABLE(grub_relocator_forward_end) From 14e43c6e0258b844db41c2240df9fbee1ae6918c Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 10 Jan 2010 15:06:17 +0100 Subject: [PATCH 002/178] First working newreloc --- conf/i386-pc.rmk | 4 +- include/grub/i386/multiboot.h | 4 +- kern/i386/pc/init.c | 8 +-- lib/i386/relocator.c | 13 +++-- lib/relocator.c | 62 +++++++++++++++++------- loader/i386/multiboot.c | 91 ++++++++++++++++++++--------------- loader/i386/multiboot_elfxx.c | 47 ++++++------------ 7 files changed, 127 insertions(+), 102 deletions(-) diff --git a/conf/i386-pc.rmk b/conf/i386-pc.rmk index 1978c7caa..75e1ffdaa 100644 --- a/conf/i386-pc.rmk +++ b/conf/i386-pc.rmk @@ -202,14 +202,14 @@ serial_mod_CFLAGS = $(COMMON_CFLAGS) serial_mod_LDFLAGS = $(COMMON_LDFLAGS) # For multiboot.mod. -#pkglib_MODULES += multiboot.mod +pkglib_MODULES += multiboot.mod multiboot_mod_SOURCES = loader/i386/multiboot.c \ loader/multiboot_loader.c multiboot_mod_CFLAGS = $(COMMON_CFLAGS) multiboot_mod_LDFLAGS = $(COMMON_LDFLAGS) multiboot_mod_ASFLAGS = $(COMMON_ASFLAGS) -#pkglib_MODULES += multiboot2.mod +pkglib_MODULES += multiboot2.mod multiboot2_mod_SOURCES = loader/i386/multiboot.c \ loader/multiboot_loader.c multiboot2_mod_CFLAGS = $(COMMON_CFLAGS) -DGRUB_USE_MULTIBOOT2 diff --git a/include/grub/i386/multiboot.h b/include/grub/i386/multiboot.h index 584955449..a3ff3f4da 100644 --- a/include/grub/i386/multiboot.h +++ b/include/grub/i386/multiboot.h @@ -27,9 +27,7 @@ void grub_multiboot2_real_boot (grub_addr_t entry, struct multiboot_info *mbi) __attribute__ ((noreturn)); +extern struct grub_relocator *grub_multiboot_relocator; extern grub_uint32_t grub_multiboot_payload_eip; -extern char *grub_multiboot_payload_orig; -extern grub_addr_t grub_multiboot_payload_dest; -extern grub_size_t grub_multiboot_payload_size; #endif /* ! GRUB_MULTIBOOT_CPU_HEADER */ diff --git a/kern/i386/pc/init.c b/kern/i386/pc/init.c index 0f2374c6d..3885bba7a 100644 --- a/kern/i386/pc/init.c +++ b/kern/i386/pc/init.c @@ -190,7 +190,7 @@ grub_machine_init (void) from 1MB. This region is partially used for loading OS images. For now, 1/4 of this is added to free memory. */ for (i = 0; i < num_regions; i++) - if (mem_regions[i].addr == 0x100000) + /* if (mem_regions[i].addr == 0x100000) { grub_size_t quarter = mem_regions[i].size >> 2; @@ -199,11 +199,11 @@ grub_machine_init (void) grub_mm_init_region ((void *) (grub_os_area_addr + grub_os_area_size), quarter); } - else + else*/ grub_mm_init_region ((void *) mem_regions[i].addr, mem_regions[i].size); - if (! grub_os_area_addr) - grub_fatal ("no upper memory"); + // if (! grub_os_area_addr) + //grub_fatal ("no upper memory"); grub_tsc_init (); } diff --git a/lib/i386/relocator.c b/lib/i386/relocator.c index d4555e5f3..1129ee8ef 100644 --- a/lib/i386/relocator.c +++ b/lib/i386/relocator.c @@ -19,9 +19,9 @@ #include #include -#include #include #include +#include #include #include @@ -67,15 +67,16 @@ grub_cpu_relocator_jumper (void *rels, grub_addr_t addr) { grub_uint8_t *ptr; ptr = rels; + /* movl $addr, %eax (for relocator) */ + *(grub_uint8_t *) ptr = 0xb8; + ptr++; + *(grub_uint32_t *) ptr = addr; + ptr += 4; /* jmp $addr */ *(grub_uint8_t *) ptr = 0xe9; ptr++; *(grub_uint32_t *) ptr = addr - (grub_uint32_t) (ptr + 4); ptr += 4; - /* movl $addr, %eax (for relocator) */ - *(grub_uint8_t *) ptr = 0xb8; - ptr++; - *(grub_uint32_t *) ptr = addr; } void @@ -112,6 +113,7 @@ grub_relocator32_boot (struct grub_relocator *rel, void *src; grub_err_t err; grub_addr_t relst; + err = grub_relocator_alloc_chunk_align (rel, &src, &target, 0, (0xffffffff - RELOCATOR_SIZEOF (32)) + 1, RELOCATOR_SIZEOF (32), 16); @@ -130,6 +132,7 @@ grub_relocator32_boot (struct grub_relocator *rel, err = grub_relocator_prepare_relocs (rel, target, &relst); if (err) return err; + asm volatile ("cli"); ((void (*) (void)) relst) (); diff --git a/lib/relocator.c b/lib/relocator.c index 25ebd27bb..a2ebf99a7 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -68,12 +68,18 @@ get_best_header (struct grub_relocator *rel, for (chunk = rel->chunks; chunk; chunk = chunk->next) if ((chunk->target <= addr && addr < chunk->target + chunk->size) - || (chunk->target <= addr + size + || (chunk->target < addr + size && addr + size < chunk->target + chunk->size) || (addr <= chunk->target && chunk->target < addr + size) - || (addr <= chunk->target + chunk->size + || (addr < chunk->target + chunk->size && chunk->target + chunk->size < addr + size)) { + grub_dprintf ("relocator", + "collision 0x%llx+0x%llx, 0x%llx+0x%llx\n", + (unsigned long long) chunk->target, + (unsigned long long) chunk->size, + (unsigned long long) addr, + (unsigned long long) size); addr = ALIGN_UP (chunk->target + chunk->size, align); break; } @@ -110,10 +116,10 @@ get_best_header (struct grub_relocator *rel, for (chunk = rel->chunks; chunk; chunk = chunk->next) if ((chunk->target <= addr && addr < chunk->target + chunk->size) - || (chunk->target <= addr + size + || (chunk->target < addr + size && addr + size < chunk->target + chunk->size) || (addr <= chunk->target && chunk->target < addr + size) - || (addr <= chunk->target + chunk->size + || (addr < chunk->target + chunk->size && chunk->target + chunk->size < addr + size)) { addr = ALIGN_DOWN (chunk->target - size, align); @@ -138,18 +144,29 @@ get_best_header (struct grub_relocator *rel, } } - for (hp = NULL, h = rb->first; h; hp = h, h = h->next) + hp = rb->first; + h = hp->next; + grub_dprintf ("relocator", "alive\n"); + do { grub_addr_t allowable_start, allowable_end; allowable_start = (grub_addr_t) h; allowable_end = (grub_addr_t) (h + 1 + h->size); - + try_addr (allowable_start, allowable_end); - + if ((grub_addr_t) h == (grub_addr_t) (rb + 1)) - try_addr (allowable_start - sizeof (*rb) - rb->pre_size, - allowable_end - sizeof (*rb)); + { + grub_dprintf ("relocator", "Trying region start 0x%llx\n", + (unsigned long long) (allowable_start + - sizeof (*rb) - rb->pre_size)); + try_addr (allowable_start - sizeof (*rb) - rb->pre_size, + allowable_end - sizeof (*rb)); + } + hp = h; + h = hp->next; } + while (hp && hp != rb->first); *prev = hbp; return hb; } @@ -172,7 +189,7 @@ malloc_in_range (struct grub_relocator *rel, { if ((grub_addr_t) r + r->size + sizeof (*r) > start && (grub_addr_t) r <= end && r->size + sizeof (*r) >= size - && (rb == NULL || from_low_priv ? rb > r : rb < r)) + && (rb == NULL || (from_low_priv ? rb > r : rb < r))) { rb = r; rbp = rp; @@ -183,8 +200,13 @@ malloc_in_range (struct grub_relocator *rel, if (!rb) return 0; + grub_dprintf ("relocator", "trying region %p - %p\n", rb, rb + rb->size + 1); + hb = get_best_header (rel, start, end, align, size, rb, &hbp, &best_addr, from_low_priv, collisioncheck); + + grub_dprintf ("relocator", "best header %p\n", hb); + if (!hb) { if (from_low_priv) @@ -263,9 +285,8 @@ malloc_in_range (struct grub_relocator *rel, if (foll) { foll->next = hb; - if (hbp) - hbp->next = foll; - else + hbp->next = foll; + if (rb->first == hbp) rb->first = foll; } } @@ -275,9 +296,7 @@ malloc_in_range (struct grub_relocator *rel, foll->next = hb->next; else foll = hb->next; - if (hbp) - hbp->next = foll; - else + if (rb->first == hbp) rb->first = foll; } *res = best_addr; @@ -315,6 +334,9 @@ grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, void **src, if (!chunk) return grub_errno; + grub_dprintf ("relocator", "min_addr = 0x%llx, max_addr = 0x%llx\n", + (unsigned long long) min_addr, (unsigned long long) max_addr); + do { /* A trick to improve Linux allocation. */ @@ -339,6 +361,9 @@ grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, void **src, } while (0); + grub_dprintf ("relocator", "allocated 0x%llx/0x%llx\n", + (unsigned long long) start, (unsigned long long) target); + if (rel->highestaddr < target + size) rel->highestaddr = target + size; @@ -415,7 +440,8 @@ grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, grub_free (chunk); return grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory"); } - + + /* FIXME: check memory map. */ chunk->target = ALIGN_UP (min_addr, align); while (1) { @@ -455,6 +481,8 @@ void grub_relocator_unload (struct grub_relocator *rel) { struct grub_relocator_chunk *chunk, *next; + if (!rel) + return; for (chunk = rel->chunks; chunk; chunk = next) { grub_fatal ("Relocator unloading isn't implemented yet"); diff --git a/loader/i386/multiboot.c b/loader/i386/multiboot.c index f4869594e..5dbfc3bfd 100644 --- a/loader/i386/multiboot.c +++ b/loader/i386/multiboot.c @@ -28,7 +28,7 @@ */ /* The bits in the required part of flags field we don't support. */ -#define UNSUPPORTED_FLAGS 0x0000fffc +#define UNSUPPORTED_FLAGS 0x0000fff0 #include #include @@ -55,12 +55,10 @@ extern grub_dl_t my_mod; static struct multiboot_info *mbi, *mbi_dest; +struct grub_relocator *grub_multiboot_relocator = NULL; static grub_size_t code_size; -char *grub_multiboot_payload_orig; -grub_addr_t grub_multiboot_payload_dest; -grub_size_t grub_multiboot_payload_size; grub_uint32_t grub_multiboot_payload_eip; static grub_err_t @@ -78,9 +76,7 @@ grub_multiboot_boot (void) .esp = 0x7ff00 }; - grub_relocator32_boot (grub_multiboot_payload_orig, - grub_multiboot_payload_dest, - state); + grub_relocator32_boot (grub_multiboot_relocator, state); /* Not reached. */ return GRUB_ERR_NONE; @@ -101,10 +97,10 @@ grub_multiboot_unload (void) } grub_free ((void *) mbi->mods_addr); } - grub_relocator32_free (grub_multiboot_payload_orig); + grub_relocator_unload (grub_multiboot_relocator); + grub_multiboot_relocator = NULL; mbi = NULL; - grub_multiboot_payload_orig = NULL; grub_dl_unref (my_mod); return GRUB_ERR_NONE; @@ -224,6 +220,10 @@ grub_multiboot (int argc, char *argv[]) int i; int cmdline_argc; char **cmdline_argv; + int mbichunk_size; + void *mbichunk; + grub_addr_t mbichunk_dest; + grub_err_t err; grub_loader_unset (); @@ -271,8 +271,8 @@ grub_multiboot (int argc, char *argv[]) goto fail; } - grub_relocator32_free (grub_multiboot_payload_orig); - grub_multiboot_payload_orig = NULL; + grub_relocator_unload (grub_multiboot_relocator); + grub_multiboot_relocator = NULL; mmap_length = grub_get_multiboot_mmap_len (); @@ -289,68 +289,81 @@ grub_multiboot (int argc, char *argv[]) boot_loader_name_length = sizeof(PACKAGE_STRING); -#define cmdline_addr(x) ((void *) ((x) + code_size)) +#define cmdline_addr(x) ((void *) (((grub_uint8_t *) x))) #define boot_loader_name_addr(x) \ - ((void *) ((x) + code_size + cmdline_length)) -#define mbi_addr(x) ((void *) ((x) + code_size + cmdline_length + boot_loader_name_length)) -#define mmap_addr(x) ((void *) ((x) + code_size + cmdline_length + boot_loader_name_length + sizeof (struct multiboot_info))) + ((void *) (((grub_uint8_t *) x) + cmdline_length)) +#define mbi_addr(x) ((void *) (((grub_uint8_t *) x) + cmdline_length + boot_loader_name_length)) +#define mmap_addr(x) ((void *) (((grub_uint8_t *) x) + cmdline_length + boot_loader_name_length + sizeof (struct multiboot_info))) - grub_multiboot_payload_size = cmdline_length + mbichunk_size = cmdline_length /* boot_loader_name_length might need to grow for mbi,etc to be aligned (see below) */ + boot_loader_name_length + 3 + sizeof (struct multiboot_info) + mmap_length; + grub_multiboot_relocator = grub_relocator_new (); + + if (!grub_multiboot_relocator) + goto fail; + if (header->flags & MULTIBOOT_AOUT_KLUDGE) { int offset = ((char *) header - buffer - (header->header_addr - header->load_addr)); int load_size = ((header->load_end_addr == 0) ? file->size - offset : header->load_end_addr - header->load_addr); + void *source; if (header->bss_end_addr) code_size = (header->bss_end_addr - header->load_addr); else code_size = load_size; - grub_multiboot_payload_dest = header->load_addr; - grub_multiboot_payload_size += code_size; - - grub_multiboot_payload_orig - = grub_relocator32_alloc (grub_multiboot_payload_size); - - if (! grub_multiboot_payload_orig) - goto fail; + err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator, + &source, header->load_addr, + code_size); + if (err) + { + grub_dprintf ("multiboot_loader", "Error loading aout kludge\n"); + goto fail; + } if ((grub_file_seek (file, offset)) == (grub_off_t) -1) goto fail; - grub_file_read (file, (void *) grub_multiboot_payload_orig, load_size); + grub_file_read (file, source, load_size); if (grub_errno) goto fail; if (header->bss_end_addr) - grub_memset ((void *) (grub_multiboot_payload_orig + load_size), 0, + grub_memset ((grub_uint32_t *) source + load_size, 0, header->bss_end_addr - header->load_addr - load_size); grub_multiboot_payload_eip = header->entry_addr; - } else if (grub_multiboot_load_elf (file, buffer) != GRUB_ERR_NONE) goto fail; /* This provides alignment for the MBI, the memory map and the backward relocator. */ - boot_loader_name_length += (0x04 - ((unsigned long) mbi_addr (grub_multiboot_payload_dest) & 0x03)); + boot_loader_name_length += (0x04 - ((unsigned long) mbi_addr (0) & 0x03)); + + err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, &mbichunk, + &mbichunk_dest, + 0, (0xffffffff - mbichunk_size) + 1, + mbichunk_size, 4); + if (err) + { + grub_dprintf ("multiboot_loader", "Error allocating mbi chunk\n"); + goto fail; + } + mbi = mbi_addr (mbichunk); + mbi_dest = mbi_addr (mbichunk_dest); - mbi = mbi_addr (grub_multiboot_payload_orig); - mbi_dest = mbi_addr (grub_multiboot_payload_dest); grub_memset (mbi, 0, sizeof (struct multiboot_info)); mbi->mmap_length = mmap_length; - grub_fill_multiboot_mmap (mmap_addr (grub_multiboot_payload_orig)); + grub_fill_multiboot_mmap (mmap_addr (mbichunk)); - /* FIXME: grub_uint32_t will break for addresses above 4 GiB, but is mandated - by the spec. Is there something we can do about it? */ - mbi->mmap_addr = (grub_uint32_t) mmap_addr (grub_multiboot_payload_dest); + mbi->mmap_addr = (grub_uint32_t) mmap_addr (mbichunk_dest); mbi->flags |= MULTIBOOT_INFO_MEM_MAP; /* Convert from bytes to kilobytes. */ @@ -358,7 +371,7 @@ grub_multiboot (int argc, char *argv[]) mbi->mem_upper = grub_mmap_get_upper () / 1024; mbi->flags |= MULTIBOOT_INFO_MEMORY; - cmdline = p = cmdline_addr (grub_multiboot_payload_orig); + cmdline = p = cmdline_addr (mbichunk); if (! cmdline) goto fail; @@ -374,17 +387,17 @@ grub_multiboot (int argc, char *argv[]) *p = 0; mbi->flags |= MULTIBOOT_INFO_CMDLINE; - mbi->cmdline = (grub_uint32_t) cmdline_addr (grub_multiboot_payload_dest); + mbi->cmdline = (grub_uint32_t) cmdline_addr (mbichunk_dest); - grub_strcpy (boot_loader_name_addr (grub_multiboot_payload_orig), PACKAGE_STRING); + grub_strcpy (boot_loader_name_addr (mbichunk), PACKAGE_STRING); mbi->flags |= MULTIBOOT_INFO_BOOT_LOADER_NAME; - mbi->boot_loader_name = (grub_uint32_t) boot_loader_name_addr (grub_multiboot_payload_dest); + mbi->boot_loader_name = (grub_uint32_t) boot_loader_name_addr (mbichunk_dest); if (grub_multiboot_get_bootdev (&mbi->boot_device)) mbi->flags |= MULTIBOOT_INFO_BOOTDEV; - grub_loader_set (grub_multiboot_boot, grub_multiboot_unload, 1); + grub_loader_set (grub_multiboot_boot, grub_multiboot_unload, 0); fail: if (file) diff --git a/loader/i386/multiboot_elfxx.c b/loader/i386/multiboot_elfxx.c index 80db25144..155de5801 100644 --- a/loader/i386/multiboot_elfxx.c +++ b/loader/i386/multiboot_elfxx.c @@ -51,7 +51,6 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, void *buffer) { Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer; char *phdr_base; - int lowest_segment = -1, highest_segment = -1; int i; if (ehdr->e_ident[EI_CLASS] != ELFCLASSXX) @@ -82,54 +81,38 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, void *buffer) phdr_base = (char *) buffer + ehdr->e_phoff; #define phdr(i) ((Elf_Phdr *) (phdr_base + (i) * ehdr->e_phentsize)) - for (i = 0; i < ehdr->e_phnum; i++) - if (phdr(i)->p_type == PT_LOAD && phdr(i)->p_filesz != 0) - { - /* Beware that segment 0 isn't necessarily loadable */ - if (lowest_segment == -1 - || phdr(i)->p_paddr < phdr(lowest_segment)->p_paddr) - lowest_segment = i; - if (highest_segment == -1 - || phdr(i)->p_paddr > phdr(highest_segment)->p_paddr) - highest_segment = i; - } - - if (lowest_segment == -1) - return grub_error (GRUB_ERR_BAD_OS, "ELF contains no loadable segments"); - - code_size = (phdr(highest_segment)->p_paddr + phdr(highest_segment)->p_memsz) - phdr(lowest_segment)->p_paddr; - grub_multiboot_payload_dest = phdr(lowest_segment)->p_paddr; - - grub_multiboot_payload_size += code_size; - - grub_multiboot_payload_orig - = grub_relocator32_alloc (grub_multiboot_payload_size); - - if (!grub_multiboot_payload_orig) - return grub_errno; - /* Load every loadable segment in memory. */ for (i = 0; i < ehdr->e_phnum; i++) { if (phdr(i)->p_type == PT_LOAD && phdr(i)->p_filesz != 0) { - char *load_this_module_at = (char *) (grub_multiboot_payload_orig + (long) (phdr(i)->p_paddr - phdr(lowest_segment)->p_paddr)); + grub_err_t err; + void *source; grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx, memsz=0x%lx, vaddr=0x%lx\n", i, (long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz, (long) phdr(i)->p_vaddr); + err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator, + &source, phdr(i)->p_paddr, + phdr(i)->p_memsz); + if (err) + { + grub_dprintf ("multiboot_loader", "Error loading phdr %d\n", i); + return err; + } + if (grub_file_seek (file, (grub_off_t) phdr(i)->p_offset) == (grub_off_t) -1) return grub_error (GRUB_ERR_BAD_OS, "invalid offset in program header"); - if (grub_file_read (file, load_this_module_at, phdr(i)->p_filesz) + if (grub_file_read (file, source, phdr(i)->p_filesz) != (grub_ssize_t) phdr(i)->p_filesz) return grub_error (GRUB_ERR_BAD_OS, "couldn't read segment from file"); if (phdr(i)->p_filesz < phdr(i)->p_memsz) - grub_memset (load_this_module_at + phdr(i)->p_filesz, 0, + grub_memset ((grub_uint8_t *) source + phdr(i)->p_filesz, 0, phdr(i)->p_memsz - phdr(i)->p_filesz); } } @@ -138,8 +121,8 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, void *buffer) if (phdr(i)->p_vaddr <= ehdr->e_entry && phdr(i)->p_vaddr + phdr(i)->p_memsz > ehdr->e_entry) { - grub_multiboot_payload_eip = grub_multiboot_payload_dest - + (ehdr->e_entry - phdr(i)->p_vaddr) + (phdr(i)->p_paddr - phdr(lowest_segment)->p_paddr); + grub_multiboot_payload_eip = (ehdr->e_entry - phdr(i)->p_vaddr) + + phdr(i)->p_paddr; break; } From 7e267737b6c6db0bac679e25404d6801aad5e8a6 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 10 Jan 2010 18:59:31 +0100 Subject: [PATCH 003/178] Add align to .S files --- lib/i386/relocator_asm.S | 2 ++ lib/x86_64/relocator_asm.S | 2 ++ 2 files changed, 4 insertions(+) diff --git a/lib/i386/relocator_asm.S b/lib/i386/relocator_asm.S index f8fc0c08a..a3530182a 100644 --- a/lib/i386/relocator_asm.S +++ b/lib/i386/relocator_asm.S @@ -19,6 +19,8 @@ #include #include + .p2align 2 + VARIABLE(grub_relocator_backward_start) /* mov imm32, %eax */ .byte 0xb8 diff --git a/lib/x86_64/relocator_asm.S b/lib/x86_64/relocator_asm.S index 6db44f2f7..8d99f5224 100644 --- a/lib/x86_64/relocator_asm.S +++ b/lib/x86_64/relocator_asm.S @@ -19,6 +19,8 @@ #include #include + .p2align 2 + VARIABLE(grub_relocator_backward_start) /* mov imm32, %rax */ .byte 0x48 From d2601bb7bcf3aef6d435118e0200dc179f7b3e4d Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Mon, 11 Jan 2010 11:32:41 +0100 Subject: [PATCH 004/178] decrease scope of code_size --- loader/i386/multiboot.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/loader/i386/multiboot.c b/loader/i386/multiboot.c index 2a1411002..c5a7f7f9d 100644 --- a/loader/i386/multiboot.c +++ b/loader/i386/multiboot.c @@ -49,7 +49,6 @@ extern grub_dl_t my_mod; struct grub_relocator *grub_multiboot_relocator = NULL; -static grub_size_t code_size; grub_uint32_t grub_multiboot_payload_eip; static grub_err_t @@ -182,6 +181,7 @@ grub_multiboot (int argc, char *argv[]) (header->header_addr - header->load_addr)); int load_size = ((header->load_end_addr == 0) ? file->size - offset : header->load_end_addr - header->load_addr); + grub_size_t code_size; void *source; if (header->bss_end_addr) From b56495543a29c4df518aa2a68fed50cde96bce8d Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Mon, 11 Jan 2010 13:40:59 +0100 Subject: [PATCH 005/178] Fix variable name collision --- lib/i386/relocator.c | 8 ++++---- lib/i386/relocator_asm.S | 4 ++-- lib/x86_64/relocator_asm.S | 9 ++++----- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/i386/relocator.c b/lib/i386/relocator.c index 1129ee8ef..2396999bb 100644 --- a/lib/i386/relocator.c +++ b/lib/i386/relocator.c @@ -35,11 +35,11 @@ extern grub_uint8_t grub_relocator_backward_end; extern void *grub_relocator_backward_dest; extern void *grub_relocator_backward_src; -extern grub_size_t grub_relocator_backward_size; +extern grub_size_t grub_relocator_backward_chunk_size; extern void *grub_relocator_forward_dest; extern void *grub_relocator_forward_src; -extern grub_size_t grub_relocator_forward_size; +extern grub_size_t grub_relocator_forward_chunk_size; extern grub_uint32_t grub_relocator32_eax; extern grub_uint32_t grub_relocator32_ebx; @@ -85,7 +85,7 @@ grub_cpu_relocator_backward (void *ptr, void *src, void *dest, { grub_relocator_backward_dest = dest; grub_relocator_backward_src = src; - grub_relocator_backward_size = size; + grub_relocator_backward_chunk_size = size; grub_memmove (ptr, &grub_relocator_backward_start, @@ -98,7 +98,7 @@ grub_cpu_relocator_forward (void *ptr, void *src, void *dest, { grub_relocator_forward_dest = dest; grub_relocator_forward_src = src; - grub_relocator_forward_size = size; + grub_relocator_forward_chunk_size = size; grub_memmove (ptr, &grub_relocator_forward_start, diff --git a/lib/i386/relocator_asm.S b/lib/i386/relocator_asm.S index a3530182a..f273586fe 100644 --- a/lib/i386/relocator_asm.S +++ b/lib/i386/relocator_asm.S @@ -36,7 +36,7 @@ VARIABLE(grub_relocator_backward_src) /* mov imm32, %ecx */ .byte 0xb9 -VARIABLE(grub_relocator_backward_size) +VARIABLE(grub_relocator_backward_chunk_size) .long 0 add %ecx, %esi @@ -70,7 +70,7 @@ VARIABLE(grub_relocator_forward_src) /* mov imm32, %ecx */ .byte 0xb9 -VARIABLE(grub_relocator_forward_size) +VARIABLE(grub_relocator_forward_chunk_size) .long 0 /* Forward copy. */ diff --git a/lib/x86_64/relocator_asm.S b/lib/x86_64/relocator_asm.S index 8d99f5224..62b909f57 100644 --- a/lib/x86_64/relocator_asm.S +++ b/lib/x86_64/relocator_asm.S @@ -25,27 +25,26 @@ VARIABLE(grub_relocator_backward_start) /* mov imm32, %rax */ .byte 0x48 .byte 0xb8 -RELOCATOR_VARIABLE(dest) +VARIABLE(grub_relocator_backward_dest) .long 0, 0 movq %rax, %rdi /* mov imm64, %rax */ .byte 0x48 .byte 0xb8 -RELOCATOR_VARIABLE(src) +VARIABLE(grub_relocator_backward_src) .long 0, 0 movq %rax, %rsi /* mov imm32, %ecx */ .byte 0x48 .byte 0xb9 -RELOCATOR_VARIABLE(size) +VARIABLE(grub_relocator_backward_chunk_size) .long 0, 0 add %rcx, %rsi add %rcx, %rdi - /* Backward movsb is implicitly off-by-one. compensate that. */ sub $1, %rsi sub $1, %rdi @@ -77,7 +76,7 @@ VARIABLE(grub_relocator_forward_src) /* mov imm64, %rcx */ .byte 0x48 .byte 0xb9 -VARIABLE(grub_relocator_forward_size) +VARIABLE(grub_relocator_forward_chunk_size) .long 0, 0 /* Forward copy. */ From 796d2fa20e81702ba6c99a1d9b6a1b1aa0dbd713 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Mon, 11 Jan 2010 13:42:45 +0100 Subject: [PATCH 006/178] Remove uselees instruction --- lib/x86_64/relocator_asm.S | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/x86_64/relocator_asm.S b/lib/x86_64/relocator_asm.S index 62b909f57..2ab6d8cb7 100644 --- a/lib/x86_64/relocator_asm.S +++ b/lib/x86_64/relocator_asm.S @@ -36,7 +36,7 @@ VARIABLE(grub_relocator_backward_src) .long 0, 0 movq %rax, %rsi - /* mov imm32, %ecx */ + /* mov imm64, %rcx */ .byte 0x48 .byte 0xb9 VARIABLE(grub_relocator_backward_chunk_size) @@ -72,7 +72,6 @@ VARIABLE(grub_relocator_forward_src) .long 0, 0 movq %rax, %rsi - xorq %rcx, %rcx /* mov imm64, %rcx */ .byte 0x48 .byte 0xb9 From 669a1c01fb993f061cd41f9593018e1159fa3b36 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Mon, 11 Jan 2010 13:43:31 +0100 Subject: [PATCH 007/178] Fix various mistakes --- lib/relocator.c | 84 +++++++++++++++++++++++++++++-------------------- 1 file changed, 50 insertions(+), 34 deletions(-) diff --git a/lib/relocator.c b/lib/relocator.c index a2ebf99a7..4b638cd66 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -35,7 +35,8 @@ grub_relocator_new (void) return NULL; ret->postchunks = ~(grub_addr_t) 0; - ret->relocators_size = grub_relocator_jumper_size; + ret->relocators_size = grub_relocator_jumper_size; + grub_dprintf ("relocator", "relocators_size=%d\n", ret->relocators_size); return ret; } @@ -198,14 +199,17 @@ malloc_in_range (struct grub_relocator *rel, } if (!rb) - return 0; + { + grub_dprintf ("relocator", "no suitable region found\n"); + return 0; + } grub_dprintf ("relocator", "trying region %p - %p\n", rb, rb + rb->size + 1); hb = get_best_header (rel, start, end, align, size, rb, &hbp, &best_addr, from_low_priv, collisioncheck); - grub_dprintf ("relocator", "best header %p\n", hb); + grub_dprintf ("relocator", "best header %p/%x\n", hb, best_addr); if (!hb) { @@ -226,13 +230,15 @@ malloc_in_range (struct grub_relocator *rel, newreg_presize = newreg_start - newreg_raw_start; newreg_size = rb->size - (newreg_start - (grub_addr_t) rb); if ((hb->size << GRUB_MM_ALIGN_LOG2) >= newreg_start - + (grub_addr_t) rb) + - (grub_addr_t) rb) { grub_mm_header_t newhnext = hb->next; grub_size_t newhsize = ((hb->size << GRUB_MM_ALIGN_LOG2) - - newreg_start - - (grub_addr_t) rb) >> GRUB_MM_ALIGN_LOG2; + - (newreg_start + - (grub_addr_t) rb)) >> GRUB_MM_ALIGN_LOG2; new_header = (void *) (newreg_start + sizeof (*rb)); + if (newhnext == hb->next) + newhnext = newhnext; new_header->next = newhnext; new_header->size = newhsize; new_header->magic = GRUB_MM_FREE_MAGIC; @@ -240,32 +246,25 @@ malloc_in_range (struct grub_relocator *rel, else { new_header = hb->next; + if (new_header == hb) + new_header = (void *) (newreg_start + sizeof (*rb)); } - if (hbp || new_header) - { - struct grub_mm_header *newregfirst = rb->first; - struct grub_mm_region *newregnext = rb->next; - struct grub_mm_region *newreg = (void *) newreg_start; - if (hbp) - hbp->next = new_header; - else - newregfirst = new_header; - newreg->first = newregfirst; - newreg->next = newregnext; - newreg->pre_size = newreg_presize; - newreg->size = newreg_size; - if (rbp) - rbp->next = newreg; - else - grub_mm_base = newreg; - } - else - { - if (rbp) - rbp->next = rb->next; - else - grub_mm_base = rb->next; - } + { + struct grub_mm_header *newregfirst = rb->first; + struct grub_mm_region *newregnext = rb->next; + struct grub_mm_region *newreg = (void *) newreg_start; + hbp->next = new_header; + if (newregfirst == hb) + newregfirst = new_header; + newreg->first = newregfirst; + newreg->next = newregnext; + newreg->pre_size = newreg_presize; + newreg->size = newreg_size; + if (rbp) + rbp->next = newreg; + else + grub_mm_base = newreg; + } *res = best_addr; return 1; } @@ -296,8 +295,12 @@ malloc_in_range (struct grub_relocator *rel, foll->next = hb->next; else foll = hb->next; - if (rb->first == hbp) + + if (rb->first == hb) rb->first = foll; + if (rb->first == hb) + rb->first = (void *) (rb + 1); + hbp->next = foll; } *res = best_addr; return 1; @@ -320,7 +323,7 @@ grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, void **src, if (chunk->target > target && chunk->src > max_addr) max_addr = chunk->src; if (chunk->target + chunk->size <= target - && chunk->src + chunk->size < min_addr + && chunk->src + chunk->size > min_addr && chunk->src < rel->postchunks) min_addr = chunk->src + chunk->size; if ((chunk->target <= target && target < chunk->target + chunk->size) @@ -356,6 +359,7 @@ grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, void **src, if (malloc_in_range (rel, min_addr, target, 0, size, &start, 1, 0)) break; + grub_dprintf ("relocator", "not allocated\n"); grub_free (chunk); return grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory"); } @@ -379,11 +383,15 @@ grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, void **src, rel->highestnonpostaddr = start + size; } + grub_dprintf ("relocator", "relocators_size=%d\n", rel->relocators_size); + if (start < target) rel->relocators_size += grub_relocator_backward_size; if (start > target) rel->relocators_size += grub_relocator_forward_size; + grub_dprintf ("relocator", "relocators_size=%d\n", rel->relocators_size); + chunk->src = start; chunk->target = target; chunk->size = size; @@ -410,6 +418,8 @@ grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, if (malloc_in_range (rel, min_addr, max_addr, align, size, &start, 1, 1)) { + grub_dprintf ("relocator", "allocated 0x%llx/0x%llx\n", + (unsigned long long) start, (unsigned long long) start); chunk->src = start; chunk->target = start; chunk->size = size; @@ -499,11 +509,17 @@ grub_relocator_prepare_relocs (struct grub_relocator *rel, grub_addr_t addr, grub_addr_t rels; grub_addr_t rels0; - if (!malloc_in_range (rel, 0, ~(grub_addr_t)0, grub_relocator_align, + grub_dprintf ("relocator", "Preparing relocs (size=%d)\n", + rel->relocators_size); + + if (!malloc_in_range (rel, 0, ~(grub_addr_t)0 - rel->relocators_size + 1, + grub_relocator_align, rel->relocators_size, &rels0, 1, 1)) return grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory"); rels = rels0; + grub_dprintf ("relocator", "Relocs allocated\n"); + for (chunk = rel->chunks; chunk; chunk = chunk->next) { if (chunk->src < chunk->target) From cb1b2ad7e080849ef4c3c8e0a1cc9292d71e0949 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Mon, 11 Jan 2010 13:43:44 +0100 Subject: [PATCH 008/178] Reenable XNU --- conf/i386-pc.rmk | 2 +- include/grub/i386/xnu.h | 1 - include/grub/xnu.h | 9 +-- loader/i386/xnu.c | 99 ++++++++++++--------------- loader/xnu.c | 144 +++++++++++++++++++--------------------- loader/xnu_resume.c | 43 ++++++++---- 6 files changed, 148 insertions(+), 150 deletions(-) diff --git a/conf/i386-pc.rmk b/conf/i386-pc.rmk index 6992d3edc..c155047f6 100644 --- a/conf/i386-pc.rmk +++ b/conf/i386-pc.rmk @@ -179,7 +179,7 @@ linux_mod_SOURCES = loader/i386/linux.c linux_mod_CFLAGS = $(COMMON_CFLAGS) linux_mod_LDFLAGS = $(COMMON_LDFLAGS) -#pkglib_MODULES += xnu.mod +pkglib_MODULES += xnu.mod xnu_mod_SOURCES = loader/xnu_resume.c loader/i386/xnu.c loader/i386/pc/xnu.c \ loader/macho32.c loader/macho64.c loader/macho.c loader/xnu.c xnu_mod_CFLAGS = $(COMMON_CFLAGS) diff --git a/include/grub/i386/xnu.h b/include/grub/i386/xnu.h index 3be2c3bcc..d3ac5ba0c 100644 --- a/include/grub/i386/xnu.h +++ b/include/grub/i386/xnu.h @@ -117,5 +117,4 @@ grub_err_t grub_xnu_boot (void); grub_err_t grub_xnu_set_video (struct grub_xnu_boot_params *bootparams_relloc); grub_err_t grub_cpu_xnu_fill_devicetree (void); -extern grub_uint32_t grub_xnu_heap_will_be_at; #endif diff --git a/include/grub/xnu.h b/include/grub/xnu.h index 6ce17c25e..0b942d855 100644 --- a/include/grub/xnu.h +++ b/include/grub/xnu.h @@ -87,7 +87,7 @@ extern struct grub_xnu_devtree_key *grub_xnu_devtree_root; void grub_xnu_free_devtree (struct grub_xnu_devtree_key *cur); -grub_err_t grub_xnu_writetree_toheap (void **start, grub_size_t *size); +grub_err_t grub_xnu_writetree_toheap (grub_addr_t *target, grub_size_t *size); struct grub_xnu_devtree_key *grub_xnu_create_value (struct grub_xnu_devtree_key **parent, char *name); @@ -102,11 +102,12 @@ grub_err_t grub_xnu_scan_dir_for_kexts (char *dirname, char *osbundlerequired, int maxrecursion); grub_err_t grub_xnu_load_kext_from_dir (char *dirname, char *osbundlerequired, int maxrecursion); -void *grub_xnu_heap_malloc (int size); +grub_err_t grub_xnu_heap_malloc (int size, void **src, grub_addr_t *target); grub_err_t grub_xnu_fill_devicetree (void); -extern grub_uint32_t grub_xnu_heap_real_start; +extern struct grub_relocator *grub_xnu_relocator; + extern grub_size_t grub_xnu_heap_size; -extern void *grub_xnu_heap_start; extern struct grub_video_bitmap *grub_xnu_bitmap; extern int grub_xnu_is_64bit; +extern grub_addr_t grub_xnu_heap_target_start; #endif diff --git a/loader/i386/xnu.c b/loader/i386/xnu.c index 23a8a6f7b..ce520bcde 100644 --- a/loader/i386/xnu.c +++ b/loader/i386/xnu.c @@ -35,7 +35,6 @@ #include char grub_xnu_cmdline[1024]; -grub_uint32_t grub_xnu_heap_will_be_at; grub_uint32_t grub_xnu_entry_point, grub_xnu_arg1, grub_xnu_stack; /* Aliases set for some tables. */ @@ -832,26 +831,25 @@ grub_xnu_boot_resume (void) state.eip = grub_xnu_entry_point; state.eax = grub_xnu_arg1; - return grub_relocator32_boot (grub_xnu_heap_start, grub_xnu_heap_will_be_at, - state); + return grub_relocator32_boot (grub_xnu_relocator, state); } /* Boot xnu. */ grub_err_t grub_xnu_boot (void) { - struct grub_xnu_boot_params *bootparams_relloc; - grub_off_t bootparams_relloc_off; - grub_off_t mmap_relloc_off; + struct grub_xnu_boot_params *bootparams; + grub_addr_t bootparams_target; grub_err_t err; grub_efi_uintn_t memory_map_size = 0; grub_efi_memory_descriptor_t *memory_map; + grub_addr_t memory_map_target; grub_efi_uintn_t map_key = 0; grub_efi_uintn_t descriptor_size = 0; grub_efi_uint32_t descriptor_version = 0; grub_uint64_t firstruntimepage, lastruntimepage; grub_uint64_t curruntimepage; - void *devtree; + grub_addr_t devtree_target; grub_size_t devtreelen; int i; struct grub_relocator32_state state; @@ -895,26 +893,25 @@ grub_xnu_boot (void) } /* Relocate the boot parameters to heap. */ - bootparams_relloc = grub_xnu_heap_malloc (sizeof (*bootparams_relloc)); - if (! bootparams_relloc) - return grub_errno; - bootparams_relloc_off = (grub_uint8_t *) bootparams_relloc - - (grub_uint8_t *) grub_xnu_heap_start; + err = grub_xnu_heap_malloc (sizeof (*bootparams), + (void **) &bootparams, &bootparams_target); + if (err) + return err; /* Set video. */ - err = grub_xnu_set_video (bootparams_relloc); + err = grub_xnu_set_video (bootparams); if (err != GRUB_ERR_NONE) { grub_print_error (); grub_errno = GRUB_ERR_NONE; grub_printf ("Booting in blind mode\n"); - bootparams_relloc->lfb_mode = 0; - bootparams_relloc->lfb_width = 0; - bootparams_relloc->lfb_height = 0; - bootparams_relloc->lfb_depth = 0; - bootparams_relloc->lfb_line_len = 0; - bootparams_relloc->lfb_base = 0; + bootparams->lfb_mode = 0; + bootparams->lfb_width = 0; + bootparams->lfb_height = 0; + bootparams->lfb_depth = 0; + bootparams->lfb_line_len = 0; + bootparams->lfb_base = 0; } if (grub_autoefi_get_memory_map (&memory_map_size, memory_map, @@ -925,38 +922,29 @@ grub_xnu_boot (void) /* We will do few allocations later. Reserve some space for possible memory map growth. */ memory_map_size += 20 * descriptor_size; - memory_map = grub_xnu_heap_malloc (memory_map_size); - if (! memory_map) - return grub_errno; - mmap_relloc_off = (grub_uint8_t *) memory_map - - (grub_uint8_t *) grub_xnu_heap_start; - - err = grub_xnu_writetree_toheap (&devtree, &devtreelen); + err = grub_xnu_heap_malloc (memory_map_size, + (void **) &memory_map, &memory_map_target); if (err) return err; - bootparams_relloc = (struct grub_xnu_boot_params *) - (bootparams_relloc_off + (grub_uint8_t *) grub_xnu_heap_start); - grub_memcpy (bootparams_relloc->cmdline, grub_xnu_cmdline, - sizeof (bootparams_relloc->cmdline)); + err = grub_xnu_writetree_toheap (&devtree_target, &devtreelen); + if (err) + return err; - bootparams_relloc->devtree - = ((grub_uint8_t *) devtree - (grub_uint8_t *) grub_xnu_heap_start) - + grub_xnu_heap_will_be_at; - bootparams_relloc->devtreelen = devtreelen; + grub_memcpy (bootparams->cmdline, grub_xnu_cmdline, + sizeof (bootparams->cmdline)); - memory_map = (grub_efi_memory_descriptor_t *) - ((grub_uint8_t *) grub_xnu_heap_start + mmap_relloc_off); + bootparams->devtree = devtree_target; + bootparams->devtreelen = devtreelen; if (grub_autoefi_get_memory_map (&memory_map_size, memory_map, &map_key, &descriptor_size, &descriptor_version) <= 0) return grub_errno; - bootparams_relloc->efi_system_table - = PTR_TO_UINT32 (grub_autoefi_system_table); + bootparams->efi_system_table = PTR_TO_UINT32 (grub_autoefi_system_table); - firstruntimepage = (((grub_addr_t) grub_xnu_heap_will_be_at + firstruntimepage = (((grub_addr_t) grub_xnu_heap_target_start + grub_xnu_heap_size + GRUB_XNU_PAGESIZE - 1) / GRUB_XNU_PAGESIZE) + 20; curruntimepage = firstruntimepage; @@ -977,7 +965,7 @@ grub_xnu_boot (void) <= PTR_TO_UINT64 (grub_autoefi_system_table) && curdesc->physical_start + (curdesc->num_pages << 12) > PTR_TO_UINT64 (grub_autoefi_system_table)) - bootparams_relloc->efi_system_table + bootparams->efi_system_table = PTR_TO_UINT64 (grub_autoefi_system_table) - curdesc->physical_start + curdesc->virtual_start; if (SIZEOF_OF_UINTN == 8 && grub_xnu_is_64bit) @@ -987,25 +975,25 @@ grub_xnu_boot (void) lastruntimepage = curruntimepage; - bootparams_relloc->efi_mmap = grub_xnu_heap_will_be_at + mmap_relloc_off; - bootparams_relloc->efi_mmap_size = memory_map_size; - bootparams_relloc->efi_mem_desc_size = descriptor_size; - bootparams_relloc->efi_mem_desc_version = descriptor_version; + bootparams->efi_mmap = memory_map_target; + bootparams->efi_mmap_size = memory_map_size; + bootparams->efi_mem_desc_size = descriptor_size; + bootparams->efi_mem_desc_version = descriptor_version; - bootparams_relloc->heap_start = grub_xnu_heap_will_be_at; - bootparams_relloc->heap_size = grub_xnu_heap_size; - bootparams_relloc->efi_runtime_first_page = firstruntimepage; + bootparams->heap_start = grub_xnu_heap_target_start; + bootparams->heap_size = grub_xnu_heap_size; + bootparams->efi_runtime_first_page = firstruntimepage; - bootparams_relloc->efi_runtime_npages = lastruntimepage - firstruntimepage; - bootparams_relloc->efi_uintnbits = SIZEOF_OF_UINTN * 8; + bootparams->efi_runtime_npages = lastruntimepage - firstruntimepage; + bootparams->efi_uintnbits = SIZEOF_OF_UINTN * 8; - bootparams_relloc->verminor = GRUB_XNU_BOOTARGS_VERMINOR; - bootparams_relloc->vermajor = GRUB_XNU_BOOTARGS_VERMAJOR; + bootparams->verminor = GRUB_XNU_BOOTARGS_VERMINOR; + bootparams->vermajor = GRUB_XNU_BOOTARGS_VERMAJOR; /* Parameters for asm helper. */ - grub_xnu_stack = bootparams_relloc->heap_start - + bootparams_relloc->heap_size + GRUB_XNU_PAGESIZE; - grub_xnu_arg1 = bootparams_relloc_off + grub_xnu_heap_will_be_at; + grub_xnu_stack = bootparams->heap_start + + bootparams->heap_size + GRUB_XNU_PAGESIZE; + grub_xnu_arg1 = bootparams_target; if (! grub_autoefi_exit_boot_services (map_key)) return grub_error (GRUB_ERR_IO, "can't exit boot services"); @@ -1016,8 +1004,7 @@ grub_xnu_boot (void) state.eip = grub_xnu_entry_point; state.eax = grub_xnu_arg1; state.esp = grub_xnu_stack; - return grub_relocator32_boot (grub_xnu_heap_start, grub_xnu_heap_will_be_at, - state); + return grub_relocator32_boot (grub_xnu_relocator, state); } static grub_command_t cmd_devprop_load; diff --git a/loader/xnu.c b/loader/xnu.c index c3dcee3ed..07a0b726e 100644 --- a/loader/xnu.c +++ b/loader/xnu.c @@ -39,45 +39,28 @@ static int driverspackagenum = 0; static int driversnum = 0; int grub_xnu_is_64bit = 0; -void *grub_xnu_heap_start = 0; +grub_addr_t grub_xnu_heap_target_start = 0; grub_size_t grub_xnu_heap_size = 0; - -/* Allocate heap by 32MB-blocks. */ -#define GRUB_XNU_HEAP_ALLOC_BLOCK 0x2000000 +struct grub_relocator *grub_xnu_relocator; static grub_err_t grub_xnu_register_memory (char *prefix, int *suffix, - void *addr, grub_size_t size); -void * -grub_xnu_heap_malloc (int size) + grub_addr_t addr, grub_size_t size); +grub_err_t +grub_xnu_heap_malloc (int size, void **src, grub_addr_t *target) { - void *val; - int oldblknum, newblknum; + grub_err_t err; + + err = grub_relocator_alloc_chunk_addr (grub_xnu_relocator, src, + grub_xnu_heap_target_start + + grub_xnu_heap_size, size); + if (err) + return err; - /* The page after the heap is used for stack. Ensure it's usable. */ - if (grub_xnu_heap_size) - oldblknum = (grub_xnu_heap_size + GRUB_XNU_PAGESIZE - + GRUB_XNU_HEAP_ALLOC_BLOCK - 1) / GRUB_XNU_HEAP_ALLOC_BLOCK; - else - oldblknum = 0; - newblknum = (grub_xnu_heap_size + size + GRUB_XNU_PAGESIZE - + GRUB_XNU_HEAP_ALLOC_BLOCK - 1) / GRUB_XNU_HEAP_ALLOC_BLOCK; - if (oldblknum != newblknum) - { - /* FIXME: instruct realloc to allocate at 1MB if possible once - advanced mm is ready. */ - grub_xnu_heap_start - = XNU_RELOCATOR (realloc) (grub_xnu_heap_start, - newblknum - * GRUB_XNU_HEAP_ALLOC_BLOCK); - if (!grub_xnu_heap_start) - return NULL; - } - - val = (grub_uint8_t *) grub_xnu_heap_start + grub_xnu_heap_size; + *target = grub_xnu_heap_target_start + grub_xnu_heap_size; grub_xnu_heap_size += size; - grub_dprintf ("xnu", "val=%p\n", val); - return val; + grub_dprintf ("xnu", "val=%p\n", *src); + return GRUB_ERR_NONE; } /* Make sure next block of the heap will be aligned. @@ -86,11 +69,9 @@ grub_xnu_heap_malloc (int size) grub_err_t grub_xnu_align_heap (int align) { - int align_overhead = align - grub_xnu_heap_size % align; - if (align_overhead == align) - return GRUB_ERR_NONE; - if (! grub_xnu_heap_malloc (align_overhead)) - return grub_errno; + grub_xnu_heap_size + = ALIGN_UP (grub_xnu_heap_target_start+ grub_xnu_heap_size, align) + - grub_xnu_heap_target_start; return GRUB_ERR_NONE; } @@ -206,13 +187,14 @@ grub_xnu_writetree_toheap_real (void *curptr, } grub_err_t -grub_xnu_writetree_toheap (void **start, grub_size_t *size) +grub_xnu_writetree_toheap (grub_addr_t *target, grub_size_t *size) { struct grub_xnu_devtree_key *chosen; struct grub_xnu_devtree_key *memorymap; struct grub_xnu_devtree_key *driverkey; struct grub_xnu_extdesc *extdesc; grub_err_t err; + void *src; err = grub_xnu_align_heap (GRUB_XNU_PAGESIZE); if (err) @@ -243,16 +225,17 @@ grub_xnu_writetree_toheap (void **start, grub_size_t *size) /* Allocate the space based on the size with dummy value. */ *size = grub_xnu_writetree_get_size (grub_xnu_devtree_root, "/"); - *start = grub_xnu_heap_malloc (*size + GRUB_XNU_PAGESIZE - - *size % GRUB_XNU_PAGESIZE); + err = grub_xnu_heap_malloc (ALIGN_UP (*size + 1, GRUB_XNU_PAGESIZE), + &src, target); + if (err) + return err; /* Put real data in the dummy. */ - extdesc->addr = (grub_uint8_t *) *start - (grub_uint8_t *) grub_xnu_heap_start - + grub_xnu_heap_will_be_at; + extdesc->addr = *target; extdesc->size = (grub_uint32_t) *size; /* Write the tree to heap. */ - grub_xnu_writetree_toheap_real (*start, grub_xnu_devtree_root, "/"); + grub_xnu_writetree_toheap_real (src, grub_xnu_devtree_root, "/"); return GRUB_ERR_NONE; } @@ -337,8 +320,9 @@ grub_xnu_unload (void) /* Free loaded image. */ driversnum = 0; driverspackagenum = 0; - grub_free (grub_xnu_heap_start); - grub_xnu_heap_start = 0; + grub_relocator_unload (grub_xnu_relocator); + grub_xnu_relocator = NULL; + grub_xnu_heap_target_start = 0; grub_xnu_heap_size = 0; grub_xnu_unlock (); return GRUB_ERR_NONE; @@ -353,6 +337,7 @@ grub_cmd_xnu_kernel (grub_command_t cmd __attribute__ ((unused)), grub_uint32_t startcode, endcode; int i; char *ptr, *loadaddr; + grub_addr_t loadaddr_target; if (argc < 1) return grub_error (GRUB_ERR_BAD_ARGUMENT, "file name required"); @@ -380,15 +365,18 @@ grub_cmd_xnu_kernel (grub_command_t cmd __attribute__ ((unused)), grub_dprintf ("xnu", "endcode = %lx, startcode = %lx\n", (unsigned long) endcode, (unsigned long) startcode); - loadaddr = grub_xnu_heap_malloc (endcode - startcode); - grub_xnu_heap_will_be_at = startcode; + grub_xnu_relocator = grub_relocator_new (); + if (!grub_xnu_relocator) + return grub_errno; + grub_xnu_heap_target_start = startcode; + err = grub_xnu_heap_malloc (endcode - startcode, (void **) &loadaddr, + &loadaddr_target); - if (! loadaddr) + if (err) { grub_macho_close (macho); grub_xnu_unload (); - return grub_error (GRUB_ERR_OUT_OF_MEMORY, - "not enough memory to load kernel"); + return err; } /* Load kernel. */ @@ -451,6 +439,7 @@ grub_cmd_xnu_kernel64 (grub_command_t cmd __attribute__ ((unused)), grub_uint64_t startcode, endcode; int i; char *ptr, *loadaddr; + grub_addr_t loadaddr_target; if (argc < 1) return grub_error (GRUB_ERR_BAD_ARGUMENT, "file name required"); @@ -481,15 +470,18 @@ grub_cmd_xnu_kernel64 (grub_command_t cmd __attribute__ ((unused)), grub_dprintf ("xnu", "endcode = %lx, startcode = %lx\n", (unsigned long) endcode, (unsigned long) startcode); - loadaddr = grub_xnu_heap_malloc (endcode - startcode); - grub_xnu_heap_will_be_at = startcode; + grub_xnu_relocator = grub_relocator_new (); + if (!grub_xnu_relocator) + return grub_errno; + grub_xnu_heap_target_start = startcode; + err = grub_xnu_heap_malloc (endcode - startcode, (void **) &loadaddr, + &loadaddr_target); - if (! loadaddr) + if (err) { grub_macho_close (macho); grub_xnu_unload (); - return grub_error (GRUB_ERR_OUT_OF_MEMORY, - "not enough memory to load kernel"); + return err; } /* Load kernel. */ @@ -547,7 +539,7 @@ grub_cmd_xnu_kernel64 (grub_command_t cmd __attribute__ ((unused)), and increment SUFFIX. */ static grub_err_t grub_xnu_register_memory (char *prefix, int *suffix, - void *addr, grub_size_t size) + grub_addr_t addr, grub_size_t size) { struct grub_xnu_devtree_key *chosen; struct grub_xnu_devtree_key *memorymap; @@ -585,8 +577,7 @@ grub_xnu_register_memory (char *prefix, int *suffix, = (struct grub_xnu_extdesc *) grub_malloc (sizeof (*extdesc)); if (! driverkey->data) return grub_error (GRUB_ERR_OUT_OF_MEMORY, "can't register extension"); - extdesc->addr = grub_xnu_heap_will_be_at + - ((grub_uint8_t *) addr - (grub_uint8_t *) grub_xnu_heap_start); + extdesc->addr = addr; extdesc->size = (grub_uint32_t) size; return GRUB_ERR_NONE; } @@ -628,7 +619,8 @@ grub_xnu_load_driver (char *infoplistname, grub_file_t binaryfile) grub_file_t infoplist; struct grub_xnu_extheader *exthead; int neededspace = sizeof (*exthead); - grub_uint8_t *buf; + grub_uint8_t *buf, *buf0; + grub_addr_t buf_target; grub_size_t infoplistsize = 0, machosize = 0; char *name, *nameend; int namelen; @@ -683,7 +675,10 @@ grub_xnu_load_driver (char *infoplistname, grub_file_t binaryfile) err = grub_xnu_align_heap (GRUB_XNU_PAGESIZE); if (err) return err; - buf = grub_xnu_heap_malloc (neededspace); + err = grub_xnu_heap_malloc (neededspace, (void **) &buf0, &buf_target); + if (err) + return err; + buf = buf0; exthead = (struct grub_xnu_extheader *) buf; grub_memset (exthead, 0, sizeof (*exthead)); @@ -692,8 +687,7 @@ grub_xnu_load_driver (char *infoplistname, grub_file_t binaryfile) /* Load the binary. */ if (macho) { - exthead->binaryaddr = (buf - (grub_uint8_t *) grub_xnu_heap_start) - + grub_xnu_heap_will_be_at; + exthead->binaryaddr = buf_target + (buf - buf0); exthead->binarysize = machosize; if (grub_xnu_is_64bit) err = grub_macho_readfile64 (macho, buf); @@ -712,8 +706,7 @@ grub_xnu_load_driver (char *infoplistname, grub_file_t binaryfile) /* Load the plist. */ if (infoplist) { - exthead->infoplistaddr = (buf - (grub_uint8_t *) grub_xnu_heap_start) - + grub_xnu_heap_will_be_at; + exthead->infoplistaddr = buf_target + (buf - buf0); exthead->infoplistsize = infoplistsize + 1; if (grub_file_read (infoplist, buf, infoplistsize) != (grub_ssize_t) (infoplistsize)) @@ -729,15 +722,14 @@ grub_xnu_load_driver (char *infoplistname, grub_file_t binaryfile) } grub_errno = GRUB_ERR_NONE; - exthead->nameaddr = (buf - (grub_uint8_t *) grub_xnu_heap_start) - + grub_xnu_heap_will_be_at; + exthead->nameaddr = (buf - buf0) + buf_target; exthead->namesize = namelen + 1; grub_memcpy (buf, name, namelen); buf[namelen] = 0; buf += namelen + 1; /* Announce to kernel */ - return grub_xnu_register_memory ("Driver-", &driversnum, exthead, + return grub_xnu_register_memory ("Driver-", &driversnum, buf_target, neededspace); } @@ -748,6 +740,7 @@ grub_cmd_xnu_mkext (grub_command_t cmd __attribute__ ((unused)), { grub_file_t file; void *loadto; + grub_addr_t loadto_target; grub_err_t err; grub_off_t readoff = 0; grub_ssize_t readlen = -1; @@ -836,11 +829,11 @@ grub_cmd_xnu_mkext (grub_command_t cmd __attribute__ ((unused)), return err; } - loadto = grub_xnu_heap_malloc (readlen); - if (! loadto) + err = grub_xnu_heap_malloc (readlen, &loadto, &loadto_target); + if (err) { grub_file_close (file); - return grub_errno; + return err; } /* Read the file. */ @@ -855,7 +848,7 @@ grub_cmd_xnu_mkext (grub_command_t cmd __attribute__ ((unused)), /* Pass it to kernel. */ return grub_xnu_register_memory ("DriversPackage-", &driverspackagenum, - loadto, readlen); + loadto_target, readlen); } static grub_err_t @@ -864,6 +857,7 @@ grub_cmd_xnu_ramdisk (grub_command_t cmd __attribute__ ((unused)), { grub_file_t file; void *loadto; + grub_addr_t loadto_target; grub_err_t err; grub_size_t size; @@ -884,9 +878,9 @@ grub_cmd_xnu_ramdisk (grub_command_t cmd __attribute__ ((unused)), size = grub_file_size (file); - loadto = grub_xnu_heap_malloc (size); - if (! loadto) - return grub_errno; + err = grub_xnu_heap_malloc (size, &loadto, &loadto_target); + if (err) + return err; if (grub_file_read (file, loadto, size) != (grub_ssize_t) (size)) { @@ -894,7 +888,7 @@ grub_cmd_xnu_ramdisk (grub_command_t cmd __attribute__ ((unused)), grub_error_push (); return grub_error (GRUB_ERR_BAD_OS, "couldn't read file %s", args[0]); } - return grub_xnu_register_memory ("RAMDisk", 0, loadto, size); + return grub_xnu_register_memory ("RAMDisk", 0, loadto_target, size); } /* Returns true if the kext should be loaded according to plist diff --git a/loader/xnu_resume.c b/loader/xnu_resume.c index e6620e7b7..a7d5fbad8 100644 --- a/loader/xnu_resume.c +++ b/loader/xnu_resume.c @@ -45,10 +45,12 @@ grub_xnu_resume (char *imagename) grub_file_t file; grub_size_t total_header_size; struct grub_xnu_hibernate_header hibhead; - grub_uint8_t *buf; - + void *code; + void *image; grub_uint32_t codedest; grub_uint32_t codesize; + grub_addr_t target_image; + grub_err_t err; file = grub_file_open (imagename); if (! file) @@ -94,18 +96,35 @@ grub_xnu_resume (char *imagename) /* Try to allocate necessary space. FIXME: mm isn't good enough yet to handle huge allocations. */ - grub_xnu_hibernate_image = buf = XNU_RELOCATOR (alloc) (hibhead.image_size - + codesize - + GRUB_XNU_PAGESIZE); - if (! buf) + grub_xnu_relocator = grub_relocator_new (); + if (!grub_xnu_relocator) { grub_file_close (file); return grub_errno; } + err = grub_relocator_alloc_chunk_addr (grub_xnu_relocator, &code, + codedest, codesize + GRUB_XNU_PAGESIZE); + if (err) + { + grub_file_close (file); + return err; + } + + err = grub_relocator_alloc_chunk_align (grub_xnu_relocator, &image, + &target_image, 0, + (0xffffffff - hibhead.image_size) + 1, + hibhead.image_size, + GRUB_XNU_PAGESIZE); + if (err) + { + grub_file_close (file); + return err; + } + /* Read code part. */ if (grub_file_seek (file, total_header_size) == (grub_off_t) -1 - || grub_file_read (file, buf, codesize) + || grub_file_read (file, code, codesize) != (grub_ssize_t) codesize) { grub_file_close (file); @@ -114,8 +133,7 @@ grub_xnu_resume (char *imagename) /* Read image. */ if (grub_file_seek (file, 0) == (grub_off_t) -1 - || grub_file_read (file, buf + codesize + GRUB_XNU_PAGESIZE, - hibhead.image_size) + || grub_file_read (file, image, hibhead.image_size) != (grub_ssize_t) hibhead.image_size) { grub_file_close (file); @@ -124,12 +142,11 @@ grub_xnu_resume (char *imagename) grub_file_close (file); /* Setup variables needed by asm helper. */ - grub_xnu_heap_will_be_at = codedest; - grub_xnu_heap_start = buf; - grub_xnu_heap_size = codesize + GRUB_XNU_PAGESIZE + hibhead.image_size; + grub_xnu_heap_target_start = codedest; + grub_xnu_heap_size = target_image + hibhead.image_size - codedest; grub_xnu_stack = (codedest + hibhead.stack); grub_xnu_entry_point = (codedest + hibhead.entry_point); - grub_xnu_arg1 = codedest + codesize + GRUB_XNU_PAGESIZE; + grub_xnu_arg1 = target_image; grub_dprintf ("xnu", "entry point 0x%x\n", codedest + hibhead.entry_point); grub_dprintf ("xnu", "image at 0x%x\n", From 55b40bc68a08ab582d01251398ee65f2e377fc76 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Mon, 11 Jan 2010 14:59:01 +0100 Subject: [PATCH 009/178] Ported linux to relocator framework --- include/grub/i386/relocator.h | 1 + lib/i386/relocator.c | 2 + lib/i386/relocator32.S | 7 ++ lib/relocator.c | 26 ++++--- loader/i386/linux.c | 125 +++++++++++++++------------------- 5 files changed, 83 insertions(+), 78 deletions(-) diff --git a/include/grub/i386/relocator.h b/include/grub/i386/relocator.h index 2027a275c..e4c9e7ca7 100644 --- a/include/grub/i386/relocator.h +++ b/include/grub/i386/relocator.h @@ -31,6 +31,7 @@ struct grub_relocator32_state grub_uint32_t ecx; grub_uint32_t edx; grub_uint32_t eip; + grub_uint32_t esi; }; grub_err_t grub_relocator32_boot (struct grub_relocator *rel, diff --git a/lib/i386/relocator.c b/lib/i386/relocator.c index 2396999bb..11e673cf7 100644 --- a/lib/i386/relocator.c +++ b/lib/i386/relocator.c @@ -47,6 +47,7 @@ extern grub_uint32_t grub_relocator32_ecx; extern grub_uint32_t grub_relocator32_edx; extern grub_uint32_t grub_relocator32_eip; extern grub_uint32_t grub_relocator32_esp; +extern grub_uint32_t grub_relocator32_esi; #define RELOCATOR_SIZEOF(x) (&grub_relocator##x##_end - &grub_relocator##x##_start) @@ -126,6 +127,7 @@ grub_relocator32_boot (struct grub_relocator *rel, grub_relocator32_edx = state.edx; grub_relocator32_eip = state.eip; grub_relocator32_esp = state.esp; + grub_relocator32_esi = state.esi; grub_memmove (src, &grub_relocator32_start, RELOCATOR_SIZEOF (32)); diff --git a/lib/i386/relocator32.S b/lib/i386/relocator32.S index f69e0bdc8..23bf65302 100644 --- a/lib/i386/relocator32.S +++ b/lib/i386/relocator32.S @@ -93,6 +93,13 @@ VARIABLE(grub_relocator32_esp) movl %eax, %esp + /* mov imm32, %eax */ + .byte 0xb8 +VARIABLE(grub_relocator32_esi) + .long 0 + + movl %eax, %esi + /* mov imm32, %eax */ .byte 0xb8 VARIABLE(grub_relocator32_eax) diff --git a/lib/relocator.c b/lib/relocator.c index 4b638cd66..71517d94b 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -99,6 +99,7 @@ get_best_header (struct grub_relocator *rel, hb = h; hbp = hp; *best_addr = addr; + grub_dprintf ("relocator", "picked %p/%x\n", hb, addr); } } else @@ -141,6 +142,7 @@ get_best_header (struct grub_relocator *rel, hb = h; hbp = hp; *best_addr = addr; + grub_dprintf ("relocator", "picked %p/%x\n", hb, addr); } } } @@ -188,8 +190,12 @@ malloc_in_range (struct grub_relocator *rel, grub_mm_region_t r, rp; for (rp = NULL, r = grub_mm_base; r; rp = r, r = r->next) { - if ((grub_addr_t) r + r->size + sizeof (*r) > start - && (grub_addr_t) r <= end && r->size + sizeof (*r) >= size + grub_dprintf ("relocator", "region %p. %d %d %d\n", r, + (grub_addr_t) r + r->size + sizeof (*r) >= start, + (grub_addr_t) r < end && r->size + sizeof (*r) >= size, + (rb == NULL || (from_low_priv ? rb > r : rb < r))); + if ((grub_addr_t) r + r->size + sizeof (*r) >= start + && (grub_addr_t) r < end && r->size + sizeof (*r) >= size && (rb == NULL || (from_low_priv ? rb > r : rb < r))) { rb = r; @@ -221,7 +227,7 @@ malloc_in_range (struct grub_relocator *rel, } /* Special case: relocating region start. */ - if (best_addr < (grub_addr_t) hbp) + if (best_addr < (grub_addr_t) hb) { grub_addr_t newreg_start, newreg_raw_start = best_addr + size; grub_addr_t newreg_size, newreg_presize; @@ -320,7 +326,8 @@ grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, void **src, /* Keep chunks in memory in the same order as they'll be after relocation. */ for (chunk = rel->chunks; chunk; chunk = chunk->next) { - if (chunk->target > target && chunk->src > max_addr) + if (chunk->target > target && chunk->src < max_addr + && chunk->src < rel->postchunks) max_addr = chunk->src; if (chunk->target + chunk->size <= target && chunk->src + chunk->size > min_addr @@ -345,10 +352,10 @@ grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, void **src, /* A trick to improve Linux allocation. */ #if defined (__i386__) || defined (__x86_64__) if (target < 0x100000) - if (malloc_in_range (rel, rel->highestnonpostaddr, ~(grub_addr_t)0, 0, - size, &start, 1, 0)) + if (malloc_in_range (rel, rel->highestnonpostaddr, ~(grub_addr_t)0, 1, + size, &start, 0, 1)) { - if (rel->postchunks < start) + if (rel->postchunks > start) rel->postchunks = start; break; } @@ -356,7 +363,7 @@ grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, void **src, if (malloc_in_range (rel, target, max_addr, 1, size, &start, 1, 0)) break; - if (malloc_in_range (rel, min_addr, target, 0, size, &start, 1, 0)) + if (malloc_in_range (rel, min_addr, target, 1, size, &start, 0, 0)) break; grub_dprintf ("relocator", "not allocated\n"); @@ -436,7 +443,8 @@ grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, relocation. */ for (chunk = rel->chunks; chunk; chunk = chunk->next) { - if (chunk->target > max_addr && chunk->src > max_addr2) + if (chunk->target > max_addr && chunk->src > max_addr2 + && chunk->src < rel->postchunks) max_addr2 = chunk->src; if (chunk->target + chunk->size <= min_addr && chunk->src + chunk->size < min_addr2 diff --git a/loader/i386/linux.c b/loader/i386/linux.c index 43c455cd6..e8d06b0e7 100644 --- a/loader/i386/linux.c +++ b/loader/i386/linux.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #define GRUB_LINUX_CL_OFFSET 0x1000 @@ -44,36 +45,18 @@ static grub_dl_t my_mod; static grub_size_t linux_mem_size; static int loaded; static void *real_mode_mem; +static grub_addr_t real_mode_target; static void *prot_mode_mem; +static grub_addr_t prot_mode_target; static void *initrd_mem; +static grub_addr_t initrd_mem_target; static grub_uint32_t real_mode_pages; static grub_uint32_t prot_mode_pages; static grub_uint32_t initrd_pages; +static struct grub_relocator *relocator = NULL; -static grub_uint8_t gdt[] __attribute__ ((aligned(16))) = - { - /* NULL. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - /* Reserved. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - /* Code segment. */ - 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x9A, 0xCF, 0x00, - /* Data segment. */ - 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x92, 0xCF, 0x00 - }; - -struct gdt_descriptor -{ - grub_uint16_t limit; - void *base; -} __attribute__ ((packed)); - -static struct gdt_descriptor gdt_desc = - { - sizeof (gdt) - 1, - gdt - }; - +/* FIXME */ +#if 0 struct idt_descriptor { grub_uint16_t limit; @@ -85,6 +68,7 @@ static struct idt_descriptor idt_desc = 0, 0 }; +#endif #ifdef GRUB_MACHINE_PCBIOS struct linux_vesafb_res @@ -290,15 +274,19 @@ find_mmap_size (void) static void free_pages (void) { + grub_relocator_unload (relocator); + relocator = NULL; real_mode_mem = prot_mode_mem = initrd_mem = 0; + real_mode_target = 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 int +static grub_err_t allocate_pages (grub_size_t prot_size) { grub_size_t real_size, mmap_size; + grub_err_t err; /* Make sure that each size is aligned to a page boundary. */ real_size = GRUB_LINUX_CL_END_OFFSET; @@ -316,6 +304,13 @@ allocate_pages (grub_size_t prot_size) /* Initialize the memory pointers with NULL for convenience. */ free_pages (); + relocator = grub_relocator_new (); + if (!relocator) + { + err = grub_errno; + goto fail; + } + /* FIXME: Should request low memory from the heap when this feature is implemented. */ @@ -339,32 +334,42 @@ allocate_pages (grub_size_t prot_size) if (real_size + mmap_size > size) return 0; - real_mode_mem = - (void *) (grub_size_t) ((addr + size) - (real_size + mmap_size)); + real_mode_target = ((addr + size) - (real_size + mmap_size)); return 1; } return 0; } grub_mmap_iterate (hook); - if (! real_mode_mem) + if (! real_mode_target) { - grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate real mode pages"); + err = grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate real mode pages"); goto fail; } - prot_mode_mem = (void *) 0x100000; + err = grub_relocator_alloc_chunk_addr (relocator, &real_mode_mem, + real_mode_target, + (real_size + mmap_size)); + if (err) + goto fail; + + prot_mode_target = 0x100000; + + err = grub_relocator_alloc_chunk_addr (relocator, &prot_mode_mem, + prot_mode_target, prot_size); + if (err) + goto fail; grub_dprintf ("linux", "real_mode_mem = %lx, real_mode_pages = %x, " "prot_mode_mem = %lx, prot_mode_pages = %x\n", (unsigned long) real_mode_mem, (unsigned) real_mode_pages, (unsigned long) prot_mode_mem, (unsigned) prot_mode_pages); - return 1; + return GRUB_ERR_NONE; fail: free_pages (); - return 0; + return err; } static void @@ -460,16 +465,12 @@ grub_linux_boot (void) int e820_num; grub_err_t err = 0; char *modevar, *tmp; + struct grub_relocator32_state state; params = real_mode_mem; - grub_dprintf ("linux", "code32_start = %x, idt_desc = %lx, gdt_desc = %lx\n", - (unsigned) params->code32_start, - (unsigned long) &(idt_desc.limit), - (unsigned long) &(gdt_desc.limit)); - grub_dprintf ("linux", "idt = %x:%lx, gdt = %x:%lx\n", - (unsigned) idt_desc.limit, (unsigned long) idt_desc.base, - (unsigned) gdt_desc.limit, (unsigned long) gdt_desc.base); + grub_dprintf ("linux", "code32_start = %x\n", + (unsigned) params->code32_start); auto int NESTED_FUNC_ATTR hook (grub_uint64_t, grub_uint64_t, grub_uint32_t); int NESTED_FUNC_ATTR hook (grub_uint64_t addr, grub_uint64_t size, grub_uint32_t type) @@ -568,31 +569,12 @@ grub_linux_boot (void) } } -#ifdef __x86_64__ - - grub_memcpy ((char *) prot_mode_mem + (prot_mode_pages << 12), - grub_linux_trampoline_start, - grub_linux_trampoline_end - grub_linux_trampoline_start); - - ((void (*) (unsigned long, void *)) ((char *) prot_mode_mem - + (prot_mode_pages << 12))) - (params->code32_start, real_mode_mem); -#else - - /* Hardware interrupts are not safe any longer. */ - asm volatile ("cli" : : ); - - /* Load the IDT and the GDT for the bootstrap. */ - asm volatile ("lidt %0" : : "m" (idt_desc)); - asm volatile ("lgdt %0" : : "m" (gdt_desc)); - - /* Enter Linux. */ - asm volatile ("jmp *%2" : : "b" (0), "S" (real_mode_mem), "g" (params->code32_start)); - -#endif - - /* Never reach here. */ - return GRUB_ERR_NONE; + /* FIXME. */ + /* asm volatile ("lidt %0" : : "m" (idt_desc)); */ + state.ebx = 0; + state.esi = real_mode_target; + state.eip = params->code32_start; + return grub_relocator32_boot (relocator, state); } static grub_err_t @@ -678,7 +660,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), real_size = setup_sects << GRUB_DISK_SECTOR_BITS; prot_size = grub_file_size (file) - real_size - GRUB_DISK_SECTOR_SIZE; - if (! allocate_pages (prot_size)) + if (allocate_pages (prot_size)) goto fail; params = (struct linux_kernel_params *) real_mode_mem; @@ -701,7 +683,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), params->cl_magic = GRUB_LINUX_CL_MAGIC; params->cl_offset = 0x1000; - params->cmd_line_ptr = (unsigned long) real_mode_mem + 0x1000; + params->cmd_line_ptr = real_mode_target + 0x1000; params->ramdisk_image = 0; params->ramdisk_size = 0; @@ -910,6 +892,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), grub_ssize_t size; grub_addr_t addr_min, addr_max; grub_addr_t addr; + grub_err_t err; struct linux_kernel_header *lh; if (argc == 0) @@ -957,7 +940,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), addr_max -= 0x10000; /* Usually, the compression ratio is about 50%. */ - addr_min = (grub_addr_t) prot_mode_mem + ((prot_mode_pages * 3) << 12) + addr_min = (grub_addr_t) prot_mode_target + ((prot_mode_pages * 3) << 12) + page_align (size); if (addr_max > grub_os_area_addr + grub_os_area_size) @@ -972,7 +955,11 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), goto fail; } - initrd_mem = (void *) addr; + err = grub_relocator_alloc_chunk_align (relocator, &initrd_mem, + &initrd_mem_target, + addr_min, addr, size, 0x1000); + if (err) + return err; if (grub_file_read (file, initrd_mem, size) != size) { @@ -983,7 +970,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), grub_dprintf ("linux", "Initrd, addr=0x%x, size=0x%x\n", (unsigned) addr, (unsigned) size); - lh->ramdisk_image = addr; + lh->ramdisk_image = initrd_mem_target; lh->ramdisk_size = size; lh->root_dev = 0x0100; /* XXX */ From d45dca5ab33950e15369158466088d24921bae66 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Mon, 11 Jan 2010 22:54:20 +0100 Subject: [PATCH 010/178] Fix few bugs in relocators --- lib/relocator.c | 96 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 63 insertions(+), 33 deletions(-) diff --git a/lib/relocator.c b/lib/relocator.c index 71517d94b..5d30b8ffe 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -313,6 +313,29 @@ malloc_in_range (struct grub_relocator *rel, } } +static void +adjust_limits (struct grub_relocator *rel, + grub_addr_t *min_addr, grub_addr_t *max_addr, + grub_addr_t in_min, grub_addr_t in_max) +{ + struct grub_relocator_chunk *chunk; + + *min_addr = 0; + *max_addr = rel->postchunks; + + /* Keep chunks in memory in the same order as they'll be after relocation. */ + for (chunk = rel->chunks; chunk; chunk = chunk->next) + { + if (chunk->target > in_max && chunk->src < *max_addr + && chunk->src < rel->postchunks) + *max_addr = chunk->src; + if (chunk->target + chunk->size <= in_min + && chunk->src + chunk->size > *min_addr + && chunk->src < rel->postchunks) + *min_addr = chunk->src + chunk->size; + } +} + grub_err_t grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, void **src, grub_addr_t target, grub_size_t size) @@ -321,24 +344,13 @@ grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, void **src, grub_addr_t start; grub_addr_t min_addr = 0, max_addr; - max_addr = rel->postchunks; + adjust_limits (rel, &min_addr, &max_addr, target, target); - /* Keep chunks in memory in the same order as they'll be after relocation. */ for (chunk = rel->chunks; chunk; chunk = chunk->next) - { - if (chunk->target > target && chunk->src < max_addr - && chunk->src < rel->postchunks) - max_addr = chunk->src; - if (chunk->target + chunk->size <= target - && chunk->src + chunk->size > min_addr - && chunk->src < rel->postchunks) - min_addr = chunk->src + chunk->size; - if ((chunk->target <= target && target < chunk->target + chunk->size) - || (target <= chunk->target && chunk->target < target + size)) - { - return grub_error (GRUB_ERR_BAD_ARGUMENT, "overlap detected"); - } - } + if ((chunk->target <= target && target < chunk->target + chunk->size) + || (target <= chunk->target && chunk->target < target + size)) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "overlap detected"); + chunk = grub_malloc (sizeof (struct grub_relocator_chunk)); if (!chunk) @@ -366,6 +378,14 @@ grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, void **src, if (malloc_in_range (rel, min_addr, target, 1, size, &start, 0, 0)) break; + if (malloc_in_range (rel, rel->highestnonpostaddr, ~(grub_addr_t)0, 1, + size, &start, 0, 1)) + { + if (rel->postchunks > start) + rel->postchunks = start; + break; + } + grub_dprintf ("relocator", "not allocated\n"); grub_free (chunk); return grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory"); @@ -404,6 +424,9 @@ grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, void **src, chunk->size = size; chunk->next = rel->chunks; rel->chunks = chunk; + grub_dprintf ("relocator", "cur = %p, next = %p\n", rel->chunks, + rel->chunks->next); + *src = (void *) start; return GRUB_ERR_NONE; } @@ -418,6 +441,8 @@ grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, struct grub_relocator_chunk *chunk; grub_addr_t start; + grub_dprintf ("relocator", "chunks = %p\n", rel->chunks); + chunk = grub_malloc (sizeof (struct grub_relocator_chunk)); if (!chunk) return grub_errno; @@ -427,6 +452,7 @@ grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, { grub_dprintf ("relocator", "allocated 0x%llx/0x%llx\n", (unsigned long long) start, (unsigned long long) start); + grub_dprintf ("relocator", "chunks = %p\n", rel->chunks); chunk->src = start; chunk->target = start; chunk->size = size; @@ -437,27 +463,27 @@ grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, return GRUB_ERR_NONE; } - max_addr2 = rel->postchunks; - - /* Keep chunks in memory in the same order as they'll be after - relocation. */ - for (chunk = rel->chunks; chunk; chunk = chunk->next) - { - if (chunk->target > max_addr && chunk->src > max_addr2 - && chunk->src < rel->postchunks) - max_addr2 = chunk->src; - if (chunk->target + chunk->size <= min_addr - && chunk->src + chunk->size < min_addr2 - && chunk->src < rel->postchunks) - min_addr2 = chunk->src + chunk->size; - } + adjust_limits (rel, &min_addr2, &max_addr2, min_addr, max_addr); + grub_dprintf ("relocator", "Adjusted limits from %x-%x to %x-%x\n", + min_addr, max_addr, min_addr2, max_addr2); - if (!malloc_in_range (rel, min_addr2, max_addr2, align, - size, &start, 1, 1)) + do { - grub_free (chunk); + if (malloc_in_range (rel, min_addr2, max_addr2, align, + size, &start, 1, 1)) + break; + + if (malloc_in_range (rel, rel->highestnonpostaddr, ~(grub_addr_t)0, 1, + size, &start, 0, 1)) + { + if (rel->postchunks > start) + rel->postchunks = start; + break; + } + return grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory"); } + while (0); /* FIXME: check memory map. */ chunk->target = ALIGN_UP (min_addr, align); @@ -490,6 +516,8 @@ grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, chunk->size = size; chunk->next = rel->chunks; rel->chunks = chunk; + grub_dprintf ("relocator", "cur = %p, next = %p\n", rel->chunks, + rel->chunks->next); *src = (void *) start; *target = chunk->target; return GRUB_ERR_NONE; @@ -530,6 +558,8 @@ grub_relocator_prepare_relocs (struct grub_relocator *rel, grub_addr_t addr, for (chunk = rel->chunks; chunk; chunk = chunk->next) { + grub_dprintf ("relocator", "chunk %p->%p\n", + (void *) chunk->src, (void *) chunk->target); if (chunk->src < chunk->target) { grub_cpu_relocator_backward ((void *) rels, From 108408aa397171ba226e2a23166a71bdeb25510d Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Mon, 11 Jan 2010 22:54:40 +0100 Subject: [PATCH 011/178] BSD on relocators --- include/grub/aout.h | 4 +- include/grub/elfload.h | 8 + include/grub/i386/bsd.h | 9 +- kern/elf.c | 4 +- loader/aout.c | 12 +- loader/i386/bsd.c | 342 ++++++++++++++++++++++++++++++---------- loader/i386/bsdXX.c | 73 ++++++--- 7 files changed, 338 insertions(+), 114 deletions(-) diff --git a/include/grub/aout.h b/include/grub/aout.h index c5650ddf8..08aebba18 100644 --- a/include/grub/aout.h +++ b/include/grub/aout.h @@ -85,7 +85,7 @@ union grub_aout_header int EXPORT_FUNC(grub_aout_get_type) (union grub_aout_header *header); grub_err_t EXPORT_FUNC(grub_aout_load) (grub_file_t file, int offset, - grub_addr_t load_addr, int load_size, - grub_addr_t bss_end_addr); + void *load_addr, int load_size, + grub_size_t bss_size); #endif /* ! GRUB_AOUT_HEADER */ diff --git a/include/grub/elfload.h b/include/grub/elfload.h index 6e09e0d05..1e640c198 100644 --- a/include/grub/elfload.h +++ b/include/grub/elfload.h @@ -54,5 +54,13 @@ int grub_elf_is_elf64 (grub_elf_t); grub_size_t grub_elf64_size (grub_elf_t); grub_err_t grub_elf64_load (grub_elf_t, grub_elf64_load_hook_t, grub_addr_t *, grub_size_t *); +grub_err_t +grub_elf32_phdr_iterate (grub_elf_t elf, + int NESTED_FUNC_ATTR (*hook) (grub_elf_t, Elf32_Phdr *, void *), + void *hook_arg); +grub_err_t +grub_elf64_phdr_iterate (grub_elf_t elf, + int NESTED_FUNC_ATTR (*hook) (grub_elf_t, Elf64_Phdr *, void *), + void *hook_arg); #endif /* ! GRUB_ELFLOAD_HEADER */ diff --git a/include/grub/i386/bsd.h b/include/grub/i386/bsd.h index 8ffaf7d18..b37a86c7f 100644 --- a/include/grub/i386/bsd.h +++ b/include/grub/i386/bsd.h @@ -20,6 +20,7 @@ #define GRUB_BSD_CPU_HEADER 1 #include +#include enum bsd_kernel_types { @@ -197,7 +198,7 @@ struct grub_openbsd_bootargs struct grub_netbsd_bootinfo { grub_uint32_t bi_count; - void *bi_data[1]; + grub_addr_t bi_data[1]; }; #define NETBSD_BTINFO_BOOTPATH 0 @@ -255,9 +256,11 @@ struct grub_netbsd_btinfo_bootdisk void grub_unix_real_boot (grub_addr_t entry, ...) __attribute__ ((cdecl,noreturn)); -grub_err_t grub_freebsd_load_elfmodule32 (grub_file_t file, int argc, +grub_err_t grub_freebsd_load_elfmodule32 (struct grub_relocator *relocator, + grub_file_t file, int argc, char *argv[], grub_addr_t *kern_end); -grub_err_t grub_freebsd_load_elfmodule_obj64 (grub_file_t file, int argc, +grub_err_t grub_freebsd_load_elfmodule_obj64 (struct grub_relocator *relocator, + grub_file_t file, int argc, char *argv[], grub_addr_t *kern_end); grub_err_t grub_freebsd_load_elf_meta32 (grub_file_t file, diff --git a/kern/elf.c b/kern/elf.c index 7625f6acd..c70071d6e 100644 --- a/kern/elf.c +++ b/kern/elf.c @@ -140,7 +140,7 @@ grub_elf32_load_phdrs (grub_elf_t elf) return GRUB_ERR_NONE; } -static grub_err_t +grub_err_t grub_elf32_phdr_iterate (grub_elf_t elf, int NESTED_FUNC_ATTR (*hook) (grub_elf_t, Elf32_Phdr *, void *), void *hook_arg) @@ -321,7 +321,7 @@ grub_elf64_load_phdrs (grub_elf_t elf) return GRUB_ERR_NONE; } -static grub_err_t +grub_err_t grub_elf64_phdr_iterate (grub_elf_t elf, int NESTED_FUNC_ATTR (*hook) (grub_elf_t, Elf64_Phdr *, void *), void *hook_arg) diff --git a/loader/aout.c b/loader/aout.c index 0254b6ae0..611960f92 100644 --- a/loader/aout.c +++ b/loader/aout.c @@ -39,9 +39,8 @@ grub_aout_get_type (union grub_aout_header *header) grub_err_t grub_aout_load (grub_file_t file, int offset, - grub_addr_t load_addr, - int load_size, - grub_addr_t bss_end_addr) + void *load_addr, + int load_size, grub_size_t bss_size) { if ((grub_file_seek (file, offset)) == (grub_off_t) - 1) return grub_errno; @@ -49,14 +48,13 @@ grub_aout_load (grub_file_t file, int offset, if (!load_size) load_size = file->size - offset; - grub_file_read (file, (void *) load_addr, load_size); + grub_file_read (file, load_addr, load_size); if (grub_errno) return grub_errno; - if (bss_end_addr) - grub_memset ((char *) load_addr + load_size, 0, - bss_end_addr - load_addr - load_size); + if (bss_size) + grub_memset ((char *) load_addr + load_size, 0, bss_size); return GRUB_ERR_NONE; } diff --git a/loader/i386/bsd.c b/loader/i386/bsd.c index 0785a3fc1..9ee8a4b12 100644 --- a/loader/i386/bsd.c +++ b/loader/i386/bsd.c @@ -42,6 +42,8 @@ #include #include #include +#include +#include #define ALIGN_DWORD(a) ALIGN_UP (a, 4) #define ALIGN_QWORD(a) ALIGN_UP (a, 8) @@ -53,12 +55,14 @@ static int kernel_type = KERNEL_TYPE_NONE; static grub_dl_t my_mod; static grub_addr_t entry, entry_hi, kern_start, kern_end; +static void *kern_chunk_src; static grub_uint32_t bootflags; static char *mod_buf; static grub_uint32_t mod_buf_len, mod_buf_max, kern_end_mdofs; static int is_elf_kernel, is_64bit; static char *netbsd_root = NULL; static grub_uint32_t openbsd_root; +struct grub_relocator *relocator = NULL; static const struct grub_arg_option freebsd_opts[] = { @@ -442,24 +446,41 @@ static grub_err_t grub_freebsd_boot (void) { struct grub_freebsd_bootinfo bi; - char *p; + grub_uint8_t *p, *p0; + grub_addr_t p_target; + grub_size_t p_size = 0; grub_uint32_t bootdev, biosdev, unit, slice, part; + grub_err_t err; auto int iterate_env (struct grub_env_var *var); int iterate_env (struct grub_env_var *var) { if ((!grub_memcmp (var->name, "kFreeBSD.", sizeof("kFreeBSD.") - 1)) && (var->name[sizeof("kFreeBSD.") - 1])) { - grub_strcpy (p, &var->name[sizeof("kFreeBSD.") - 1]); - p += grub_strlen (p); + grub_strcpy ((char *) p, &var->name[sizeof("kFreeBSD.") - 1]); + p += grub_strlen ((char *) p); *(p++) = '='; - grub_strcpy (p, var->value); - p += grub_strlen (p) + 1; + grub_strcpy ((char *) p, var->value); + p += grub_strlen ((char *) p) + 1; } return 0; } + auto int iterate_env_count (struct grub_env_var *var); + int iterate_env_count (struct grub_env_var *var) + { + if ((!grub_memcmp (var->name, "kFreeBSD.", sizeof("kFreeBSD.") - 1)) && (var->name[sizeof("kFreeBSD.") - 1])) + { + p_size += grub_strlen (&var->name[sizeof("kFreeBSD.") - 1]); + p_size++; + p_size += grub_strlen (var->value) + 1; + } + + return 0; + } + + grub_memset (&bi, 0, sizeof (bi)); bi.bi_version = FREEBSD_BOOTINFO_VERSION; bi.bi_size = sizeof (bi); @@ -470,35 +491,50 @@ grub_freebsd_boot (void) bi.bi_bios_dev = biosdev; - p = (char *) kern_end; + p_size = 0; + grub_env_iterate (iterate_env_count); + if (p_size) + p_size = ALIGN_PAGE (kern_end + p_size + 1) - kern_end; + if (is_elf_kernel) + p_size = ALIGN_PAGE (kern_end + p_size + mod_buf_len) - kern_end; + + if (is_64bit) + p_size += 4096 * 4; + + err = grub_relocator_alloc_chunk_addr (relocator, (void **) &p, + kern_end, p_size); + if (err) + return err; + kern_end += p_size; + p0 = p; + p_target = kern_end; grub_env_iterate (iterate_env); - if (p != (char *) kern_end) + if (p != p0) { *(p++) = 0; - bi.bi_envp = kern_end; - kern_end = ALIGN_PAGE ((grub_uint32_t) p); + bi.bi_envp = p_target; } if (is_elf_kernel) { - grub_addr_t md_ofs; + grub_uint8_t *md_ofs; int ofs; if (grub_freebsd_add_meta (FREEBSD_MODINFO_END, 0, 0)) return grub_errno; - grub_memcpy ((char *) kern_end, mod_buf, mod_buf_len); - bi.bi_modulep = kern_end; + grub_memcpy (p, mod_buf, mod_buf_len); + bi.bi_modulep = (p - p0) + p_target; + md_ofs = p + kern_end_mdofs; - kern_end = ALIGN_PAGE (kern_end + mod_buf_len); + p = (ALIGN_PAGE ((p - p0) + p_target) - p_target) + p0; if (is_64bit) - kern_end += 4096 * 4; + p += 4096 * 4; - md_ofs = bi.bi_modulep + kern_end_mdofs; ofs = (is_64bit) ? 16 : 12; *((grub_uint32_t *) md_ofs) = kern_end; md_ofs -= ofs; @@ -519,11 +555,11 @@ grub_freebsd_boot (void) struct gdt_descriptor *gdtdesc; - pagetable = (grub_uint8_t *) (kern_end - 16384); + pagetable = p - 16384; fill_bsd64_pagetable (pagetable); /* Create GDT. */ - gdt = (grub_uint32_t *) (kern_end - 4096); + gdt = (grub_uint32_t *) (p - 4096); gdt[0] = 0; gdt[1] = 0; gdt[2] = 0; @@ -532,12 +568,12 @@ grub_freebsd_boot (void) gdt[5] = 0x00008000; /* Create GDT descriptor. */ - gdtdesc = (struct gdt_descriptor *) (kern_end - 4096 + 24); + gdtdesc = (struct gdt_descriptor *) (p - 4096 + 24); gdtdesc->limit = 24; gdtdesc->base = gdt; /* Prepare trampoline. */ - trampoline = (grub_uint8_t *) (kern_end - 4096 + 24 + trampoline = (grub_uint8_t *) (p - 4096 + 24 + sizeof (struct gdt_descriptor)); launch_trampoline = (void __attribute__ ((cdecl, regparm (0))) (*) (grub_addr_t entry_lo, ...)) trampoline; @@ -556,8 +592,31 @@ grub_freebsd_boot (void) kern_end); } else - grub_unix_real_boot (entry, bootflags | FREEBSD_RB_BOOTINFO, bootdev, - 0, 0, 0, &bi, bi.bi_modulep, kern_end); + { + struct grub_relocator32_state state; + grub_uint32_t *stack; + grub_addr_t stack_target; + err = grub_relocator_alloc_chunk_align (relocator, (void **) &stack, + &stack_target, + 0x10000, 0x90000, + 9 * sizeof (grub_uint32_t) + + sizeof (bi), 4); + if (err) + return err; + grub_memcpy (&stack[8], &bi, sizeof (bi)); + state.eip = entry; + state.esp = stack_target; + stack[0] = entry; /* "Return" address. */ + stack[1] = bootflags | FREEBSD_RB_BOOTINFO; + stack[2] = bootdev; + stack[3] = 0; + stack[4] = 0; + stack[5] = 0; + stack[6] = stack_target + 9 * sizeof (grub_uint32_t); + stack[7] = bi.bi_modulep; + stack[8] = kern_end; + return grub_relocator32_boot (relocator, state); + } /* Not reached. */ return GRUB_ERR_NONE; @@ -566,9 +625,23 @@ grub_freebsd_boot (void) static grub_err_t grub_openbsd_boot (void) { - char *buf = (char *) GRUB_BSD_TEMP_BUFFER; + grub_uint8_t *buf, *buf0; + grub_uint32_t *stack; + grub_addr_t buf_target, argbuf_target_start, argbuf_target_end; + grub_size_t buf_size; struct grub_openbsd_bios_mmap *pm; struct grub_openbsd_bootargs *pa; + struct grub_relocator32_state state; + grub_err_t err; + + auto int NESTED_FUNC_ATTR count_hook (grub_uint64_t, grub_uint64_t, grub_uint32_t); + int NESTED_FUNC_ATTR count_hook (grub_uint64_t addr __attribute__ ((unused)), + grub_uint64_t size __attribute__ ((unused)), + grub_uint32_t type __attribute__ ((unused))) + { + buf_size += sizeof (struct grub_openbsd_bios_mmap); + return 1; + } auto int NESTED_FUNC_ATTR hook (grub_uint64_t, grub_uint64_t, grub_uint32_t); int NESTED_FUNC_ATTR hook (grub_uint64_t addr, grub_uint64_t size, grub_uint32_t type) @@ -599,6 +672,21 @@ grub_openbsd_boot (void) return 0; } + buf_target = GRUB_BSD_TEMP_BUFFER; + buf_size = sizeof (struct grub_openbsd_bootargs) + 9 * sizeof (grub_uint32_t); + grub_mmap_iterate (count_hook); + buf_size += sizeof (struct grub_openbsd_bootargs); + + err = grub_relocator_alloc_chunk_addr (relocator, (void **) &buf, + buf_target, buf_size); + if (err) + return err; + buf0 = buf; + stack = (grub_uint32_t *) buf; + buf = (grub_uint8_t *) (stack + 9); + + argbuf_target_start = buf - buf0 + buf_target; + pa = (struct grub_openbsd_bootargs *) buf; pa->ba_type = OPENBSD_BOOTARG_MMAP; @@ -610,20 +698,29 @@ grub_openbsd_boot (void) pm->len = 0; pm->type = 0; pm++; + buf = (grub_uint8_t *) pm; pa->ba_size = (char *) pm - (char *) pa; - pa->ba_next = (struct grub_openbsd_bootargs *) pm; + pa->ba_next = (struct grub_openbsd_bootargs *) (buf - buf0 + buf_target); pa = pa->ba_next; pa->ba_type = OPENBSD_BOOTARG_END; pa++; + buf = (grub_uint8_t *) pa; + argbuf_target_end = buf - buf0 + buf_target; - grub_unix_real_boot (entry, bootflags, openbsd_root, OPENBSD_BOOTARG_APIVER, - 0, (grub_uint32_t) (grub_mmap_get_upper () >> 10), - (grub_uint32_t) (grub_mmap_get_lower () >> 10), - (char *) pa - buf, buf); + state.eip = entry; + state.esp = ((grub_uint8_t *) stack - buf0) + buf_target; + stack[0] = entry; + stack[1] = bootflags; + stack[2] = openbsd_root; + stack[3] = OPENBSD_BOOTARG_APIVER; + stack[4] = 0; + stack[5] = grub_mmap_get_upper () >> 10; + stack[6] = grub_mmap_get_lower () >> 10; + stack[7] = argbuf_target_end - argbuf_target_start; + stack[8] = argbuf_target_start; - /* Not reached. */ - return GRUB_ERR_NONE; + return grub_relocator32_boot (relocator, state); } static grub_err_t @@ -633,7 +730,11 @@ grub_netbsd_boot (void) int count = 0; struct grub_netbsd_btinfo_mmap_header *mmap; struct grub_netbsd_btinfo_mmap_entry *pm; - void *curarg; + void *curarg, *arg0; + grub_addr_t arg_target, stack_target; + grub_uint32_t *stack; + grub_err_t err; + struct grub_relocator32_state state; auto int NESTED_FUNC_ATTR count_hook (grub_uint64_t, grub_uint64_t, grub_uint32_t); int NESTED_FUNC_ATTR count_hook (grub_uint64_t addr __attribute__ ((unused)), @@ -675,14 +776,18 @@ grub_netbsd_boot (void) grub_mmap_iterate (count_hook); - if (kern_end + sizeof (struct grub_netbsd_btinfo_rootdevice) - + sizeof (struct grub_netbsd_bootinfo) - + sizeof (struct grub_netbsd_btinfo_mmap_header) - + count * sizeof (struct grub_netbsd_btinfo_mmap_entry) - > grub_os_area_addr + grub_os_area_size) - return grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory"); + arg_target = kern_end; + err = grub_relocator_alloc_chunk_addr + (relocator, &curarg, arg_target, + sizeof (struct grub_netbsd_btinfo_rootdevice) + + sizeof (struct grub_netbsd_bootinfo) + + sizeof (struct grub_netbsd_btinfo_mmap_header) + + count * sizeof (struct grub_netbsd_btinfo_mmap_entry)); + if (err) + return err; - curarg = mmap = (struct grub_netbsd_btinfo_mmap_header *) kern_end; + arg0 = curarg; + mmap = curarg; pm = (struct grub_netbsd_btinfo_mmap_entry *) (mmap + 1); grub_mmap_iterate (fill_hook); @@ -703,22 +808,36 @@ grub_netbsd_boot (void) bootinfo = (struct grub_netbsd_bootinfo *) (rootdev + 1); bootinfo->bi_count = 2; - bootinfo->bi_data[0] = mmap; - bootinfo->bi_data[1] = rootdev; + bootinfo->bi_data[0] = ((grub_uint8_t *) mmap - (grub_uint8_t *) arg0) + + arg_target; + bootinfo->bi_data[1] = ((grub_uint8_t *) rootdev - (grub_uint8_t *) arg0) + + arg_target; } else { bootinfo = (struct grub_netbsd_bootinfo *) curarg; bootinfo->bi_count = 1; - bootinfo->bi_data[0] = mmap; + bootinfo->bi_data[0] = ((grub_uint8_t *) mmap - (grub_uint8_t *) arg0) + + arg_target; } - grub_unix_real_boot (entry, bootflags, 0, bootinfo, - 0, (grub_uint32_t) (grub_mmap_get_upper () >> 10), - (grub_uint32_t) (grub_mmap_get_lower () >> 10)); + err = grub_relocator_alloc_chunk_align (relocator, (void **) &stack, + &stack_target, 0x10000, 0x90000, + 7 * sizeof (grub_uint32_t), 4); + if (err) + return err; - /* Not reached. */ - return GRUB_ERR_NONE; + state.eip = entry; + state.esp = stack_target; + stack[0] = entry; + stack[1] = bootflags; + stack[2] = 0; + stack[3] = ((grub_uint8_t *) bootinfo - (grub_uint8_t *) arg0) + arg_target; + stack[4] = 0; + stack[5] = grub_mmap_get_upper () >> 10; + stack[6] = grub_mmap_get_lower () >> 10; + + return grub_relocator32_boot (relocator, state); } static grub_err_t @@ -737,15 +856,20 @@ grub_bsd_unload (void) grub_free (netbsd_root); netbsd_root = NULL; + grub_relocator_unload (relocator); + relocator = NULL; + return GRUB_ERR_NONE; } static grub_err_t grub_bsd_load_aout (grub_file_t file) { - grub_addr_t load_addr, bss_end_addr; + grub_addr_t load_addr, load_end; int ofs, align_page; union grub_aout_header ah; + grub_err_t err; + grub_size_t bss_size; if ((grub_file_seek (file, 0)) == (grub_off_t) - 1) return grub_errno; @@ -775,7 +899,7 @@ grub_bsd_load_aout (grub_file_t file) return grub_error (GRUB_ERR_BAD_OS, "load address below 1M"); kern_start = load_addr; - kern_end = load_addr + ah.aout32.a_text + ah.aout32.a_data; + load_end = kern_end = load_addr + ah.aout32.a_text + ah.aout32.a_data; if (align_page) kern_end = ALIGN_PAGE (kern_end); @@ -785,13 +909,44 @@ grub_bsd_load_aout (grub_file_t file) if (align_page) kern_end = ALIGN_PAGE (kern_end); - bss_end_addr = kern_end; + bss_size = kern_end - load_end; } else - bss_end_addr = 0; + bss_size = 0; - return grub_aout_load (file, ofs, load_addr, - ah.aout32.a_text + ah.aout32.a_data, bss_end_addr); + relocator = grub_relocator_new (); + if (!relocator) + return grub_errno; + + err = grub_relocator_alloc_chunk_addr (relocator, &kern_chunk_src, + kern_start, kern_end - kern_start); + if (err) + return err; + + return grub_aout_load (file, ofs, kern_chunk_src, + ah.aout32.a_text + ah.aout32.a_data, + bss_size); +} + +static int NESTED_FUNC_ATTR +grub_bsd_elf32_size_hook (grub_elf_t elf __attribute__ ((unused)), + Elf32_Phdr *phdr, void *arg __attribute__ ((unused))) +{ + Elf32_Addr paddr; + + if (phdr->p_type != PT_LOAD + && phdr->p_type != PT_DYNAMIC) + return 1; + + paddr = phdr->p_paddr & 0xFFFFFF; + + if (paddr < kern_start) + kern_start = paddr; + + if (paddr + phdr->p_memsz > kern_end) + kern_end = paddr + phdr->p_memsz; + + return 1; } static grub_err_t @@ -810,20 +965,30 @@ grub_bsd_elf32_hook (Elf32_Phdr * phdr, grub_addr_t * addr, int *do_load) phdr->p_paddr &= 0xFFFFFF; paddr = phdr->p_paddr; - if ((paddr < grub_os_area_addr) - || (paddr + phdr->p_memsz > grub_os_area_addr + grub_os_area_size)) - return grub_error (GRUB_ERR_OUT_OF_RANGE, "address 0x%x is out of range", - paddr); + *addr = (grub_addr_t) (paddr - kern_start + (grub_uint8_t *) kern_chunk_src); - if ((!kern_start) || (paddr < kern_start)) + return GRUB_ERR_NONE; +} + +static int NESTED_FUNC_ATTR +grub_bsd_elf64_size_hook (grub_elf_t elf __attribute__ ((unused)), + Elf64_Phdr *phdr, void *arg __attribute__ ((unused))) +{ + Elf64_Addr paddr; + + if (phdr->p_type != PT_LOAD + && phdr->p_type != PT_DYNAMIC) + return 1; + + paddr = phdr->p_paddr & 0xffffff; + + if (paddr < kern_start) kern_start = paddr; if (paddr + phdr->p_memsz > kern_end) kern_end = paddr + phdr->p_memsz; - *addr = paddr; - - return GRUB_ERR_NONE; + return 1; } static grub_err_t @@ -841,18 +1006,7 @@ grub_bsd_elf64_hook (Elf64_Phdr * phdr, grub_addr_t * addr, int *do_load) *do_load = 1; paddr = phdr->p_paddr & 0xffffff; - if ((paddr < grub_os_area_addr) - || (paddr + phdr->p_memsz > grub_os_area_addr + grub_os_area_size)) - return grub_error (GRUB_ERR_OUT_OF_RANGE, "address 0x%x is out of range", - paddr); - - if ((!kern_start) || (paddr < kern_start)) - kern_start = paddr; - - if (paddr + phdr->p_memsz > kern_end) - kern_end = paddr + phdr->p_memsz; - - *addr = paddr; + *addr = (grub_addr_t) (paddr - kern_start + (grub_uint8_t *) kern_chunk_src); return GRUB_ERR_NONE; } @@ -860,11 +1014,22 @@ grub_bsd_elf64_hook (Elf64_Phdr * phdr, grub_addr_t * addr, int *do_load) static grub_err_t grub_bsd_load_elf (grub_elf_t elf) { - kern_start = kern_end = 0; + grub_err_t err; + + kern_end = 0; + kern_start = ~0; if (grub_elf_is_elf32 (elf)) { entry = elf->ehdr.ehdr32.e_entry & 0xFFFFFF; + err = grub_elf32_phdr_iterate (elf, grub_bsd_elf32_size_hook, NULL); + if (err) + return err; + err = grub_relocator_alloc_chunk_addr (relocator, &kern_chunk_src, + kern_start, kern_end - kern_start); + if (err) + return err; + return grub_elf32_load (elf, grub_bsd_elf32_hook, 0, 0); } else if (grub_elf_is_elf64 (elf)) @@ -885,6 +1050,15 @@ grub_bsd_load_elf (grub_elf_t elf) entry = elf->ehdr.ehdr64.e_entry & 0x0fffffff; entry_hi = 0; } + + err = grub_elf64_phdr_iterate (elf, grub_bsd_elf64_size_hook, NULL); + if (err) + return err; + err = grub_relocator_alloc_chunk_addr (relocator, &kern_chunk_src, + kern_start, kern_end - kern_start); + if (err) + return err; + return grub_elf64_load (elf, grub_bsd_elf64_hook, 0, 0); } else @@ -911,6 +1085,8 @@ grub_bsd_load (int argc, char *argv[]) if (!file) goto fail; + relocator = grub_relocator_new (); + elf = grub_elf_file (file); if (elf) { @@ -1059,7 +1235,7 @@ grub_cmd_netbsd (grub_extcmd_t cmd, int argc, char *argv[]) if (grub_bsd_load (argc, argv) == GRUB_ERR_NONE) { - grub_loader_set (grub_netbsd_boot, grub_bsd_unload, 1); + grub_loader_set (grub_netbsd_boot, grub_bsd_unload, 0); if (cmd->state[NETBSD_ROOT_ARG].set) netbsd_root = grub_strdup (cmd->state[NETBSD_ROOT_ARG].arg); } @@ -1164,10 +1340,11 @@ grub_cmd_freebsd_module (grub_command_t cmd __attribute__ ((unused)), int argc, char *argv[]) { grub_file_t file = 0; - grub_err_t err; int modargc; char **modargv; char *type; + grub_err_t err; + void *src; if (kernel_type == KERNEL_TYPE_NONE) return grub_error (GRUB_ERR_BAD_ARGUMENT, @@ -1192,13 +1369,12 @@ grub_cmd_freebsd_module (grub_command_t cmd __attribute__ ((unused)), if ((!file) || (!file->size)) goto fail; - if (kern_end + file->size > grub_os_area_addr + grub_os_area_size) - { - grub_error (GRUB_ERR_OUT_OF_RANGE, "not enough memory for the module"); - goto fail; - } + err = grub_relocator_alloc_chunk_addr (relocator, &src, kern_end, + file->size); + if (err) + goto fail; - grub_file_read (file, (void *) kern_end, file->size); + grub_file_read (file, src, file->size); if (grub_errno) goto fail; @@ -1264,9 +1440,11 @@ grub_cmd_freebsd_module_elf (grub_command_t cmd __attribute__ ((unused)), } if (is_64bit) - err = grub_freebsd_load_elfmodule_obj64 (file, argc, argv, &kern_end); + err = grub_freebsd_load_elfmodule_obj64 (relocator, file, + argc, argv, &kern_end); else - err = grub_freebsd_load_elfmodule32 (file, argc, argv, &kern_end); + err = grub_freebsd_load_elfmodule32 (relocator, file, + argc, argv, &kern_end); grub_file_close (file); return err; diff --git a/loader/i386/bsdXX.c b/loader/i386/bsdXX.c index b4d574821..2622287f9 100644 --- a/loader/i386/bsdXX.c +++ b/loader/i386/bsdXX.c @@ -4,19 +4,16 @@ #include #include #include +#include #define ALIGN_PAGE(a) ALIGN_UP (a, 4096) static inline grub_err_t load (grub_file_t file, void *where, grub_off_t off, grub_size_t size) { - if (PTR_TO_UINT32 (where) + size > grub_os_area_addr + grub_os_area_size) - return grub_error (GRUB_ERR_OUT_OF_RANGE, - "not enough memory for the module"); if (grub_file_seek (file, off) == (grub_off_t) -1) return grub_errno; - if (grub_file_read (file, where, size) - != (grub_ssize_t) size) + if (grub_file_read (file, where, size) != (grub_ssize_t) size) { if (grub_errno) return grub_errno; @@ -75,7 +72,8 @@ read_headers (grub_file_t file, Elf_Ehdr *e, char **shdr) platforms. So I keep both versions. */ #if OBJSYM grub_err_t -SUFFIX (grub_freebsd_load_elfmodule_obj) (grub_file_t file, int argc, +SUFFIX (grub_freebsd_load_elfmodule_obj) (struct grub_relocator *relocator, + grub_file_t file, int argc, char *argv[], grub_addr_t *kern_end) { Elf_Ehdr e; @@ -83,6 +81,8 @@ SUFFIX (grub_freebsd_load_elfmodule_obj) (grub_file_t file, int argc, char *shdr; grub_addr_t curload, module; grub_err_t err; + grub_size_t chunk_size = 0; + void *chunk_src; err = read_headers (file, &e, &shdr); if (err) @@ -90,6 +90,25 @@ SUFFIX (grub_freebsd_load_elfmodule_obj) (grub_file_t file, int argc, curload = module = ALIGN_PAGE (*kern_end); + for (s = (Elf_Shdr *) shdr; s < (Elf_Shdr *) ((char *) shdr + + e.e_shnum * e.e_shentsize); + s = (Elf_Shdr *) ((char *) s + e.e_shentsize)) + { + if (s->sh_size == 0) + continue; + + if (s->sh_addralign) + chunk_size = ALIGN_UP (chunk_size + *kern_end, s->sh_addralign) + - *kern_end; + + chunk_size += s->sh_size; + } + + err = grub_relocator_alloc_chunk_addr (relocator, &chunk_src, + module, chunk_size); + if (err) + return err; + for (s = (Elf_Shdr *) shdr; s < (Elf_Shdr *) ((char *) shdr + e.e_shnum * e.e_shentsize); s = (Elf_Shdr *) ((char *) s + e.e_shentsize)) @@ -109,15 +128,14 @@ SUFFIX (grub_freebsd_load_elfmodule_obj) (grub_file_t file, int argc, { default: case SHT_PROGBITS: - err = load (file, UINT_TO_PTR (curload), s->sh_offset, s->sh_size); + err = load (file, (grub_uint8_t *) chunk_src + curload - *kern_end, + s->sh_offset, s->sh_size); if (err) return err; break; case SHT_NOBITS: - if (curload + s->sh_size > grub_os_area_addr + grub_os_area_size) - return grub_error (GRUB_ERR_OUT_OF_RANGE, - "not enough memory for the module"); - grub_memset (UINT_TO_PTR (curload), 0, s->sh_size); + grub_memset ((grub_uint8_t *) chunk_src + curload - *kern_end, 0, + s->sh_size); break; } curload += s->sh_size; @@ -143,7 +161,8 @@ SUFFIX (grub_freebsd_load_elfmodule_obj) (grub_file_t file, int argc, #else grub_err_t -SUFFIX (grub_freebsd_load_elfmodule) (grub_file_t file, int argc, char *argv[], +SUFFIX (grub_freebsd_load_elfmodule) (struct grub_relocator *relocator, + grub_file_t file, int argc, char *argv[], grub_addr_t *kern_end) { Elf_Ehdr e; @@ -151,6 +170,8 @@ SUFFIX (grub_freebsd_load_elfmodule) (grub_file_t file, int argc, char *argv[], char *shdr; grub_addr_t curload, module; grub_err_t err; + grub_size_t chunk_size = 0; + void *chunk_src; err = read_headers (file, &e, &shdr); if (err) @@ -158,6 +179,24 @@ SUFFIX (grub_freebsd_load_elfmodule) (grub_file_t file, int argc, char *argv[], curload = module = ALIGN_PAGE (*kern_end); + for (s = (Elf_Shdr *) shdr; s < (Elf_Shdr *) ((char *) shdr + + e.e_shnum * e.e_shentsize); + s = (Elf_Shdr *) ((char *) s + e.e_shentsize)) + { + if (s->sh_size == 0) + continue; + + if (! (s->sh_flags & SHF_ALLOC)) + continue; + if (chunk_size < s->sh_addr + s->sh_size) + chunk_size = s->sh_addr + s->sh_size; + } + + err = grub_relocator_alloc_chunk_addr (relocator, &chunk_src, + module, chunk_size); + if (err) + return err; + for (s = (Elf_Shdr *) shdr; s < (Elf_Shdr *) ((char *) shdr + e.e_shnum * e.e_shentsize); s = (Elf_Shdr *) ((char *) s + e.e_shentsize)) @@ -176,17 +215,15 @@ SUFFIX (grub_freebsd_load_elfmodule) (grub_file_t file, int argc, char *argv[], { default: case SHT_PROGBITS: - err = load (file, UINT_TO_PTR (module + s->sh_addr), + err = load (file, (grub_uint8_t *) chunk_src + module + + s->sh_addr - *kern_end, s->sh_offset, s->sh_size); if (err) return err; break; case SHT_NOBITS: - if (module + s->sh_addr + s->sh_size - > grub_os_area_addr + grub_os_area_size) - return grub_error (GRUB_ERR_OUT_OF_RANGE, - "not enough memory for the module"); - grub_memset (UINT_TO_PTR (module + s->sh_addr), 0, s->sh_size); + grub_memset ((grub_uint8_t *) chunk_src + module + + s->sh_addr - *kern_end, 0, s->sh_size); break; } if (curload < module + s->sh_addr + s->sh_size) From 8468cbeccd5f544ca97517f2a12469ff401e52e9 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 12 Jan 2010 17:46:17 +0100 Subject: [PATCH 012/178] Fix typo in relocator32.S --- lib/i386/relocator32.S | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/i386/relocator32.S b/lib/i386/relocator32.S index 23bf65302..4e0553c03 100644 --- a/lib/i386/relocator32.S +++ b/lib/i386/relocator32.S @@ -79,7 +79,7 @@ LOCAL(cont1): /* Turn off PAE. */ movl %cr4, %eax - andl $GRUB_MEMORY_CPU_CR4_PAE_ON, %eax + andl $(~GRUB_MEMORY_CPU_CR4_PAE_ON), %eax movl %eax, %cr4 jmp LOCAL(cont2) From 1d24828f209ee2855ac0ff542856607d1f8b3e22 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 12 Jan 2010 17:47:14 +0100 Subject: [PATCH 013/178] Fix out of memory hang. Add sanity checks --- lib/relocator.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/relocator.c b/lib/relocator.c index 5d30b8ffe..3085932c5 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -22,6 +22,10 @@ #include /* TODO: use more efficient data structures if necessary. */ +/* FIXME: implement unload. */ +/* FIXME: check memory map. */ +/* FIXME: try to request memory from firmware. */ +/* FIXME: sort chunk when programming relocators. */ struct grub_relocator * grub_relocator_new (void) @@ -180,11 +184,13 @@ malloc_in_range (struct grub_relocator *rel, grub_size_t size, grub_addr_t *res, int from_low_priv, int collisioncheck) { - grub_mm_region_t rb = NULL, rbp = NULL; + grub_mm_region_t rb, rbp; grub_mm_header_t hb = NULL, hbp = NULL; grub_addr_t best_addr; again: + + rb = NULL, rbp = NULL; { grub_mm_region_t r, rp; @@ -344,6 +350,9 @@ grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, void **src, grub_addr_t start; grub_addr_t min_addr = 0, max_addr; + if (target > ~size) + return grub_error (GRUB_ERR_OUT_OF_RANGE, "address is out of range"); + adjust_limits (rel, &min_addr, &max_addr, target, target); for (chunk = rel->chunks; chunk; chunk = chunk->next) @@ -356,8 +365,10 @@ grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, void **src, if (!chunk) return grub_errno; - grub_dprintf ("relocator", "min_addr = 0x%llx, max_addr = 0x%llx\n", - (unsigned long long) min_addr, (unsigned long long) max_addr); + grub_dprintf ("relocator", + "min_addr = 0x%llx, max_addr = 0x%llx, target = 0x%llx\n", + (unsigned long long) min_addr, (unsigned long long) max_addr, + (unsigned long long) target); do { @@ -441,6 +452,9 @@ grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, struct grub_relocator_chunk *chunk; grub_addr_t start; + if (max_addr > ~size) + max_addr = ~size; + grub_dprintf ("relocator", "chunks = %p\n", rel->chunks); chunk = grub_malloc (sizeof (struct grub_relocator_chunk)); From 14933205d1109ea759949bfceced54553824a331 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 12 Jan 2010 17:48:51 +0100 Subject: [PATCH 014/178] Relocator64 support --- conf/i386.rmk | 1 + include/grub/i386/memory.h | 3 +- include/grub/i386/relocator.h | 16 +++ lib/i386/relocator.c | 54 ++++++++- lib/i386/relocator64.S | 206 ++++++++++++++++++++++++++++++++++ 5 files changed, 277 insertions(+), 3 deletions(-) create mode 100644 lib/i386/relocator64.S diff --git a/conf/i386.rmk b/conf/i386.rmk index 674170d01..72ea6d465 100644 --- a/conf/i386.rmk +++ b/conf/i386.rmk @@ -17,6 +17,7 @@ vga_text_mod_LDFLAGS = $(COMMON_LDFLAGS) pkglib_MODULES += relocator.mod relocator_mod_SOURCES = lib/relocator.c lib/i386/relocator32.S \ + lib/i386/relocator64.S \ lib/i386/relocator_asm.S lib/i386/relocator.c relocator_mod_CFLAGS = $(COMMON_CFLAGS) relocator_mod_ASFLAGS = $(COMMON_ASFLAGS) diff --git a/include/grub/i386/memory.h b/include/grub/i386/memory.h index 466947cc6..fe2f6e4e1 100644 --- a/include/grub/i386/memory.h +++ b/include/grub/i386/memory.h @@ -22,7 +22,8 @@ /* The flag for protected mode. */ #define GRUB_MEMORY_CPU_CR0_PE_ON 0x1 -#define GRUB_MEMORY_CPU_CR4_PAE_ON 0x00000040 +#define GRUB_MEMORY_CPU_CR4_PAE_ON 0x00000020 +#define GRUB_MEMORY_CPU_CR4_PSE_ON 0x00000010 #define GRUB_MEMORY_CPU_CR0_PAGING_ON 0x80000000 #define GRUB_MEMORY_CPU_AMD64_MSR 0xc0000080 #define GRUB_MEMORY_CPU_AMD64_MSR_ON 0x00000100 diff --git a/include/grub/i386/relocator.h b/include/grub/i386/relocator.h index e4c9e7ca7..ac49dd29e 100644 --- a/include/grub/i386/relocator.h +++ b/include/grub/i386/relocator.h @@ -34,7 +34,23 @@ struct grub_relocator32_state grub_uint32_t esi; }; +struct grub_relocator64_state +{ + grub_uint64_t rsp; + grub_uint64_t rax; + grub_uint64_t rbx; + grub_uint64_t rcx; + grub_uint64_t rdx; + grub_uint64_t rip; + grub_uint64_t rsi; + grub_addr_t cr3; +}; + grub_err_t grub_relocator32_boot (struct grub_relocator *rel, struct grub_relocator32_state state); +grub_err_t grub_relocator64_boot (struct grub_relocator *rel, + struct grub_relocator64_state state, + grub_addr_t min_addr, grub_addr_t max_addr); + #endif /* ! GRUB_RELOCATOR_CPU_HEADER */ diff --git a/lib/i386/relocator.c b/lib/i386/relocator.c index 11e673cf7..e81dd8e1e 100644 --- a/lib/i386/relocator.c +++ b/lib/i386/relocator.c @@ -26,8 +26,6 @@ #include #include -extern grub_uint8_t grub_relocator32_start; -extern grub_uint8_t grub_relocator32_end; extern grub_uint8_t grub_relocator_forward_start; extern grub_uint8_t grub_relocator_forward_end; extern grub_uint8_t grub_relocator_backward_start; @@ -41,6 +39,8 @@ extern void *grub_relocator_forward_dest; extern void *grub_relocator_forward_src; extern grub_size_t grub_relocator_forward_chunk_size; +extern grub_uint8_t grub_relocator32_start; +extern grub_uint8_t grub_relocator32_end; extern grub_uint32_t grub_relocator32_eax; extern grub_uint32_t grub_relocator32_ebx; extern grub_uint32_t grub_relocator32_ecx; @@ -49,6 +49,18 @@ extern grub_uint32_t grub_relocator32_eip; extern grub_uint32_t grub_relocator32_esp; extern grub_uint32_t grub_relocator32_esi; +extern grub_uint8_t grub_relocator64_start; +extern grub_uint8_t grub_relocator64_end; +extern grub_uint64_t grub_relocator64_rax; +extern grub_uint64_t grub_relocator64_rbx; +extern grub_uint64_t grub_relocator64_rcx; +extern grub_uint64_t grub_relocator64_rdx; +extern grub_uint64_t grub_relocator64_rip; +extern grub_uint64_t grub_relocator64_rip_addr; +extern grub_uint64_t grub_relocator64_rsp; +extern grub_uint64_t grub_relocator64_rsi; +extern grub_addr_t grub_relocator64_cr3; + #define RELOCATOR_SIZEOF(x) (&grub_relocator##x##_end - &grub_relocator##x##_start) grub_size_t grub_relocator_align = 1; @@ -141,3 +153,41 @@ grub_relocator32_boot (struct grub_relocator *rel, /* Not reached. */ return GRUB_ERR_NONE; } + +grub_err_t +grub_relocator64_boot (struct grub_relocator *rel, + struct grub_relocator64_state state, + grub_addr_t min_addr, grub_addr_t max_addr) +{ + grub_addr_t target; + void *src; + grub_err_t err; + grub_addr_t relst; + + err = grub_relocator_alloc_chunk_align (rel, &src, &target, min_addr, + max_addr - RELOCATOR_SIZEOF (64), + RELOCATOR_SIZEOF (64), 16); + if (err) + return err; + + grub_relocator64_rax = state.rax; + grub_relocator64_rbx = state.rbx; + grub_relocator64_rcx = state.rcx; + grub_relocator64_rdx = state.rdx; + grub_relocator64_rip = state.rip; + grub_relocator64_rsp = state.rsp; + grub_relocator64_rsi = state.rsi; + grub_relocator64_cr3 = state.cr3; + + grub_memmove (src, &grub_relocator64_start, RELOCATOR_SIZEOF (64)); + + err = grub_relocator_prepare_relocs (rel, target, &relst); + if (err) + return err; + + asm volatile ("cli"); + ((void (*) (void)) relst) (); + + /* Not reached. */ + return GRUB_ERR_NONE; +} diff --git a/lib/i386/relocator64.S b/lib/i386/relocator64.S new file mode 100644 index 000000000..42f61e32e --- /dev/null +++ b/lib/i386/relocator64.S @@ -0,0 +1,206 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2009,2010 Free Software Foundation, Inc. + * + * GRUB 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. + * + * GRUB 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 GRUB. If not, see . + */ + +#include +#include + +#ifdef __x86_64__ +#define RAX %rax +#define RSI %rdi +#else +#define RAX %eax +#define RSI %esi +#endif + +#define CODE32_SEGMENT 0x18 +#define CODE64_SEGMENT 0x08 + +/* The data segment of the protected mode. */ +#define DATA_SEGMENT 0x10 + + .p2align 4 /* force 16-byte alignment */ + +VARIABLE(grub_relocator64_start) +LOCAL(base): + /* %rax contains now our new 'base'. */ + mov RAX, RSI + + add $(LOCAL(cont0) - LOCAL(base)), RAX + jmp *RAX +LOCAL(cont0): + lea (LOCAL(cont1) - LOCAL(base)) (RSI, 1), RAX + mov RAX, (LOCAL(jump_vector) - LOCAL(base)) (RSI, 1) + + lea (LOCAL(gdt) - LOCAL(base)) (RSI, 1), RAX + mov RAX, (LOCAL(gdt_addr) - LOCAL(base)) (RSI, 1) + +#ifndef __x86_64__ + /* Disable paging. */ + movl %cr0, %eax + andl $(~GRUB_MEMORY_CPU_CR0_PAGING_ON), %eax + movl %eax, %cr0 + + /* Turn on PAE. */ + movl %cr4, %eax + orl $(GRUB_MEMORY_CPU_CR4_PAE_ON | GRUB_MEMORY_CPU_CR4_PSE_ON), %eax + movl %eax, %cr4 + + /* mov imm32, %eax */ + .byte 0xb8 +VARIABLE(grub_relocator64_cr3) + .long 0 + movl %eax, %cr3 + + /* Turn on amd64. */ + movl $GRUB_MEMORY_CPU_AMD64_MSR, %ecx + rdmsr + orl $GRUB_MEMORY_CPU_AMD64_MSR_ON, %eax + wrmsr + + /* Enable paging. */ + movl %cr0, %eax + orl $GRUB_MEMORY_CPU_CR0_PAGING_ON, %eax + movl %eax, %cr0 +#else + /* mov imm64, %rax */ + .byte 0x48 + .byte 0xb8 +VARIABLE(grub_relocator64_cr3) + .quad 0 + movl %rax, %cr3 +#endif + /* Load GDT. */ + lgdt (LOCAL(gdtdesc) - LOCAL(base)) (RSI, 1) + + /* Update %cs. */ + ljmp *(LOCAL(jump_vector) - LOCAL(base)) (RSI, 1) + +LOCAL(cont1): + .code64 + + /* mov imm64, %rax */ + .byte 0x48 + .byte 0xb8 +VARIABLE(grub_relocator64_rsp) + .quad 0 + + movq %rax, %rsp + + /* mov imm64, %rax */ + .byte 0x48 + .byte 0xb8 +VARIABLE(grub_relocator64_rsi) + .quad 0 + + movq %rax, %rsi + + /* mov imm64, %rax */ + .byte 0x48 + .byte 0xb8 +VARIABLE(grub_relocator64_rax) + .quad 0 + + /* mov imm64, %rbx */ + .byte 0x48 + .byte 0xbb +VARIABLE(grub_relocator64_rbx) + .quad 0 + + /* mov imm64, %rcx */ + .byte 0x48 + .byte 0xb9 +VARIABLE(grub_relocator64_rcx) + .quad 0 + + /* mov imm64, %rdx */ + .byte 0x48 + .byte 0xba +VARIABLE(grub_relocator64_rdx) + .quad 0 + + /* Cleared direction flag is of no problem with any current + payload and makes this implementation easier. */ + cld + + jmp *LOCAL(jump_addr) (%rip) + +LOCAL(jump_addr): +VARIABLE(grub_relocator64_rip) + .quad 0 + + .p2align 4 +LOCAL(gdt): + /* NULL. */ + .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + + /* 64-bit segment. */ + .word 0xffff /* Limit xffff. */ + .word 0x0000 /* Base xxxx0000. */ + .byte 0x00 /* Base xx00xxxx. */ + .byte (0x8 /* Type 8. */ | (1 << 4) /* Code. */ \ + | (0 << 5) /* Ring 0. */ | (1 << 7) /* Present. */) + .byte (0xf /* Limit fxxxx. */ | (0 << 4) /* AVL flag. */ \ + | (1 << 5) /* 64-bit. */ | (0 << 6) \ + | (1 << 7) /* 4K granular. */) + .byte 0x00 /* Base 00xxxxxx. */ + + /* Data segment*/ + .word 0xffff /* Limit xffff. */ + .word 0x0000 /* Base xxxx0000. */ + .byte 0x00 /* Base xx00xxxx. */ + .byte (0x0 /* Type 0. */ | (0 << 4) /* Data. */ \ + | (0 << 5) /* Ring 0. */ | (1 << 7) /* Present. */) + .byte (0xf /* Limit fxxxx. */ | (0 << 4) /* AVL flag. */ \ + | (0 << 5) /* Data. */ | (0 << 6) \ + | (1 << 7) /* 4K granular. */) + .byte 0x00 /* Base 00xxxxxx. */ + + /* Compatibility segment. */ + .word 0xffff /* Limit xffff. */ + .word 0x0000 /* Base xxxx0000. */ + .byte 0x00 /* Base xx00xxxx. */ + .byte (0x8 /* Type 8. */ | (1 << 4) /* Code. */ \ + | (0 << 5) /* Ring 0. */ | (1 << 7) /* Present. */) + .byte (0xf /* Limit fxxxx. */ | (0 << 4) /* AVL flag. */ \ + | (0 << 5) /* 32-bit. */ | (1 << 6) /* 32-bit. */ \ + | (1 << 7) /* 4K granular. */) + .byte 0x00 /* Base 00xxxxxx. */ + + .p2align 4 +LOCAL(gdtdesc): + .word 0x20 +LOCAL(gdt_addr): +#ifdef __x86_64__ + /* Filled by the code. */ + .quad 0 +#else + /* Filled by the code. */ + .long 0 +#endif + + .p2align 4 +LOCAL(jump_vector): + /* Jump location. Is filled by the code */ +#ifdef __x86_64__ + .quad 0 +#else + .long 0 +#endif + .long CODE64_SEGMENT + +VARIABLE(grub_relocator64_end) From 73910decff8e257368636be617ee37d4c416b93b Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 12 Jan 2010 17:50:11 +0100 Subject: [PATCH 015/178] Fix various bugs in *bsd. Freebsd64 on relocators --- include/grub/i386/bsd.h | 6 ++- loader/i386/bsd.c | 84 ++++++++++++++----------------------- loader/i386/bsdXX.c | 46 +++++++++++--------- loader/i386/bsd_pagetable.c | 19 +++++---- 4 files changed, 73 insertions(+), 82 deletions(-) diff --git a/include/grub/i386/bsd.h b/include/grub/i386/bsd.h index b37a86c7f..4f5d5d1ee 100644 --- a/include/grub/i386/bsd.h +++ b/include/grub/i386/bsd.h @@ -263,9 +263,11 @@ grub_err_t grub_freebsd_load_elfmodule_obj64 (struct grub_relocator *relocator, grub_file_t file, int argc, char *argv[], grub_addr_t *kern_end); -grub_err_t grub_freebsd_load_elf_meta32 (grub_file_t file, +grub_err_t grub_freebsd_load_elf_meta32 (struct grub_relocator *relocator, + grub_file_t file, grub_addr_t *kern_end); -grub_err_t grub_freebsd_load_elf_meta64 (grub_file_t file, +grub_err_t grub_freebsd_load_elf_meta64 (struct grub_relocator *relocator, + grub_file_t file, grub_addr_t *kern_end); grub_err_t grub_freebsd_add_meta (grub_uint32_t type, void *data, diff --git a/loader/i386/bsd.c b/loader/i386/bsd.c index 9ee8a4b12..90cd8a9a4 100644 --- a/loader/i386/bsd.c +++ b/loader/i386/bsd.c @@ -436,12 +436,6 @@ grub_freebsd_list_modules (void) /* This function would be here but it's under different license. */ #include "bsd_pagetable.c" -struct gdt_descriptor -{ - grub_uint16_t limit; - void *base; -} __attribute__ ((packed)); - static grub_err_t grub_freebsd_boot (void) { @@ -499,15 +493,15 @@ grub_freebsd_boot (void) p_size = ALIGN_PAGE (kern_end + p_size + mod_buf_len) - kern_end; if (is_64bit) - p_size += 4096 * 4; + p_size += 4096 * 3; err = grub_relocator_alloc_chunk_addr (relocator, (void **) &p, kern_end, p_size); if (err) return err; - kern_end += p_size; - p0 = p; p_target = kern_end; + p0 = p; + kern_end += p_size; grub_env_iterate (iterate_env); @@ -547,49 +541,30 @@ grub_freebsd_boot (void) if (is_64bit) { - grub_uint32_t *gdt; - grub_uint8_t *trampoline; - void (*launch_trampoline) (grub_addr_t entry_lo, ...) - __attribute__ ((cdecl, regparm (0))); + struct grub_relocator64_state state; grub_uint8_t *pagetable; + grub_uint32_t *stack; + grub_addr_t stack_target; - struct gdt_descriptor *gdtdesc; + err = grub_relocator_alloc_chunk_align (relocator, (void **) &stack, + &stack_target, + 0x10000, 0x90000, + 3 * sizeof (grub_uint32_t) + + sizeof (bi), 4); + if (err) + return err; - pagetable = p - 16384; - fill_bsd64_pagetable (pagetable); + pagetable = p - (4096 * 3); + fill_bsd64_pagetable (pagetable, (pagetable - p0) + p_target); - /* Create GDT. */ - gdt = (grub_uint32_t *) (p - 4096); - gdt[0] = 0; - gdt[1] = 0; - gdt[2] = 0; - gdt[3] = 0x00209800; - gdt[4] = 0; - gdt[5] = 0x00008000; + state.cr3 = (pagetable - p0) + p_target; + state.rsp = stack_target; + state.rip = (((grub_uint64_t) entry_hi) << 32) | entry; - /* Create GDT descriptor. */ - gdtdesc = (struct gdt_descriptor *) (p - 4096 + 24); - gdtdesc->limit = 24; - gdtdesc->base = gdt; - - /* Prepare trampoline. */ - trampoline = (grub_uint8_t *) (p - 4096 + 24 - + sizeof (struct gdt_descriptor)); - launch_trampoline = (void __attribute__ ((cdecl, regparm (0))) - (*) (grub_addr_t entry_lo, ...)) trampoline; - grub_bsd64_trampoline_gdt = (grub_uint32_t) gdtdesc; - grub_bsd64_trampoline_selfjump - = (grub_uint32_t) (trampoline + 6 - + ((grub_uint8_t *) &grub_bsd64_trampoline_selfjump - - &grub_bsd64_trampoline_start)); - - /* Copy trampoline. */ - grub_memcpy (trampoline, &grub_bsd64_trampoline_start, - &grub_bsd64_trampoline_end - &grub_bsd64_trampoline_start); - - /* Launch trampoline. */ - launch_trampoline (entry, entry_hi, pagetable, bi.bi_modulep, - kern_end); + stack[0] = entry; + stack[1] = bi.bi_modulep; + stack[2] = kern_end; + return grub_relocator64_boot (relocator, state, 0, 0x40000000); } else { @@ -936,7 +911,7 @@ grub_bsd_elf32_size_hook (grub_elf_t elf __attribute__ ((unused)), if (phdr->p_type != PT_LOAD && phdr->p_type != PT_DYNAMIC) - return 1; + return 0; paddr = phdr->p_paddr & 0xFFFFFF; @@ -946,7 +921,7 @@ grub_bsd_elf32_size_hook (grub_elf_t elf __attribute__ ((unused)), if (paddr + phdr->p_memsz > kern_end) kern_end = paddr + phdr->p_memsz; - return 1; + return 0; } static grub_err_t @@ -978,7 +953,7 @@ grub_bsd_elf64_size_hook (grub_elf_t elf __attribute__ ((unused)), if (phdr->p_type != PT_LOAD && phdr->p_type != PT_DYNAMIC) - return 1; + return 0; paddr = phdr->p_paddr & 0xffffff; @@ -988,7 +963,7 @@ grub_bsd_elf64_size_hook (grub_elf_t elf __attribute__ ((unused)), if (paddr + phdr->p_memsz > kern_end) kern_end = paddr + phdr->p_memsz; - return 1; + return 0; } static grub_err_t @@ -1054,6 +1029,9 @@ grub_bsd_load_elf (grub_elf_t elf) err = grub_elf64_phdr_iterate (elf, grub_bsd_elf64_size_hook, NULL); if (err) return err; + + grub_dprintf ("bsd", "kern_start = %x, kern_end = %x\n", kern_start, + kern_end); err = grub_relocator_alloc_chunk_addr (relocator, &kern_chunk_src, kern_start, kern_end - kern_start); if (err) @@ -1154,9 +1132,9 @@ grub_cmd_freebsd (grub_extcmd_t cmd, int argc, char *argv[]) return grub_errno; if (is_64bit) - err = grub_freebsd_load_elf_meta64 (file, &kern_end); + err = grub_freebsd_load_elf_meta64 (relocator, file, &kern_end); else - err = grub_freebsd_load_elf_meta32 (file, &kern_end); + err = grub_freebsd_load_elf_meta32 (relocator, file, &kern_end); if (err) return err; diff --git a/loader/i386/bsdXX.c b/loader/i386/bsdXX.c index 2622287f9..8f5cfa750 100644 --- a/loader/i386/bsdXX.c +++ b/loader/i386/bsdXX.c @@ -249,24 +249,28 @@ SUFFIX (grub_freebsd_load_elfmodule) (struct grub_relocator *relocator, grub_freebsd_add_meta_module (argv[0], FREEBSD_MODTYPE_ELF_MODULE, argc - 1, argv + 1, module, curload - module); - return SUFFIX (grub_freebsd_load_elf_meta) (file, kern_end); + return SUFFIX (grub_freebsd_load_elf_meta) (relocator, file, kern_end); } #endif grub_err_t -SUFFIX (grub_freebsd_load_elf_meta) (grub_file_t file, grub_addr_t *kern_end) +SUFFIX (grub_freebsd_load_elf_meta) (struct grub_relocator *relocator, + grub_file_t file, grub_addr_t *kern_end) { grub_err_t err; Elf_Ehdr e; Elf_Shdr *s; char *shdr; unsigned symoff, stroff, symsize, strsize; - grub_addr_t curload; grub_freebsd_addr_t symstart, symend, symentsize, dynamic; Elf_Sym *sym; + void *sym_chunk; + grub_uint8_t *curload; + grub_freebsd_addr_t symtarget; const char *str; unsigned i; + grub_size_t chunk_size; err = read_headers (file, &e, &shdr); if (err) @@ -293,19 +297,24 @@ SUFFIX (grub_freebsd_load_elf_meta) (grub_file_t file, grub_addr_t *kern_end) stroff = s->sh_offset; strsize = s->sh_size; - if (*kern_end + 4 * sizeof (grub_freebsd_addr_t) + symsize + strsize - > grub_os_area_addr + grub_os_area_size) - return grub_error (GRUB_ERR_OUT_OF_RANGE, - "not enough memory for kernel symbols"); + chunk_size = 2 * sizeof (grub_freebsd_addr_t) + + ALIGN_UP (symsize + strsize, sizeof (grub_freebsd_addr_t)); + symtarget = ALIGN_UP (*kern_end, sizeof (grub_freebsd_addr_t)); + err = grub_relocator_alloc_chunk_addr (relocator, &sym_chunk, + symtarget, chunk_size); + if (err) + return err; - symstart = curload = ALIGN_UP (*kern_end, sizeof (grub_freebsd_addr_t)); - *((grub_freebsd_addr_t *) UINT_TO_PTR (curload)) = symsize; + symstart = symtarget; + symend = symstart + chunk_size; + + curload = sym_chunk; + *((grub_freebsd_addr_t *) curload) = symsize; curload += sizeof (grub_freebsd_addr_t); if (grub_file_seek (file, symoff) == (grub_off_t) -1) return grub_errno; - sym = (Elf_Sym *) UINT_TO_PTR (curload); - if (grub_file_read (file, UINT_TO_PTR (curload), symsize) != - (grub_ssize_t) symsize) + sym = (Elf_Sym *) curload; + if (grub_file_read (file, curload, symsize) != (grub_ssize_t) symsize) { if (! grub_errno) return grub_error (GRUB_ERR_BAD_OS, "invalid ELF"); @@ -313,21 +322,17 @@ SUFFIX (grub_freebsd_load_elf_meta) (grub_file_t file, grub_addr_t *kern_end) } curload += symsize; - *((grub_freebsd_addr_t *) UINT_TO_PTR (curload)) = strsize; + *((grub_freebsd_addr_t *) curload) = strsize; curload += sizeof (grub_freebsd_addr_t); if (grub_file_seek (file, stroff) == (grub_off_t) -1) return grub_errno; - str = (char *) UINT_TO_PTR (curload); - if (grub_file_read (file, UINT_TO_PTR (curload), strsize) - != (grub_ssize_t) strsize) + str = (char *) curload; + if (grub_file_read (file, curload, strsize) != (grub_ssize_t) strsize) { if (! grub_errno) return grub_error (GRUB_ERR_BAD_OS, "invalid ELF"); return grub_errno; } - curload += strsize; - curload = ALIGN_UP (curload, sizeof (grub_freebsd_addr_t)); - symend = curload; for (i = 0; i * symentsize < symsize; @@ -360,7 +365,8 @@ SUFFIX (grub_freebsd_load_elf_meta) (grub_file_t file, grub_addr_t *kern_end) sizeof (symend)); if (err) return err; - *kern_end = ALIGN_PAGE (curload); + + *kern_end = ALIGN_PAGE (symend); return GRUB_ERR_NONE; } diff --git a/loader/i386/bsd_pagetable.c b/loader/i386/bsd_pagetable.c index 0fd393707..13348cc83 100644 --- a/loader/i386/bsd_pagetable.c +++ b/loader/i386/bsd_pagetable.c @@ -50,9 +50,10 @@ static void -fill_bsd64_pagetable (grub_uint8_t *target) +fill_bsd64_pagetable (grub_uint8_t *src, grub_addr_t target) { grub_uint64_t *pt2, *pt3, *pt4; + grub_addr_t pt2t, pt3t, pt4t; int i; #define PG_V 0x001 @@ -60,11 +61,15 @@ fill_bsd64_pagetable (grub_uint8_t *target) #define PG_U 0x004 #define PG_PS 0x080 - pt4 = (grub_uint64_t *) target; - pt3 = (grub_uint64_t *) (target + 4096); - pt2 = (grub_uint64_t *) (target + 8192); + pt4 = (grub_uint64_t *) src; + pt3 = (grub_uint64_t *) (src + 4096); + pt2 = (grub_uint64_t *) (src + 8192); - grub_memset ((char *) target, 0, 4096 * 3); + pt4t = target; + pt3t = target + 4096; + pt2t = target + 8192; + + grub_memset (src, 0, 4096 * 3); /* * This is kinda brutal, but every single 1GB VM memory segment points to @@ -74,11 +79,11 @@ fill_bsd64_pagetable (grub_uint8_t *target) for (i = 0; i < 512; i++) { /* Each slot of the level 4 pages points to the same level 3 page */ - pt4[i] = (grub_addr_t) &pt3[0]; + pt4[i] = (grub_addr_t) pt3t; pt4[i] |= PG_V | PG_RW | PG_U; /* Each slot of the level 3 pages points to the same level 2 page */ - pt3[i] = (grub_addr_t) &pt2[0]; + pt3[i] = (grub_addr_t) pt2t; pt3[i] |= PG_V | PG_RW | PG_U; /* The level 2 page slots are mapped with 2MB pages for 1GB. */ From 611f8f0eb94204d907ef908d0301a6fd3c815594 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 12 Jan 2010 17:57:04 +0100 Subject: [PATCH 016/178] Remove unused BSD helpers --- conf/i386-coreboot.rmk | 2 +- conf/i386-pc.rmk | 2 +- loader/i386/bsd_helper.S | 45 ------------- loader/i386/bsd_trampoline.S | 124 ----------------------------------- 4 files changed, 2 insertions(+), 171 deletions(-) delete mode 100644 loader/i386/bsd_helper.S delete mode 100644 loader/i386/bsd_trampoline.S diff --git a/conf/i386-coreboot.rmk b/conf/i386-coreboot.rmk index d73c9f0e2..8676aaea3 100644 --- a/conf/i386-coreboot.rmk +++ b/conf/i386-coreboot.rmk @@ -163,7 +163,7 @@ aout_mod_LDFLAGS = $(COMMON_LDFLAGS) # For bsd.mod pkglib_MODULES += bsd.mod -bsd_mod_SOURCES = loader/i386/bsd.c loader/i386/bsd32.c loader/i386/bsd64.c loader/i386/bsd_helper.S loader/i386/bsd_trampoline.S +bsd_mod_SOURCES = loader/i386/bsd.c loader/i386/bsd32.c loader/i386/bsd64.c bsd_mod_CFLAGS = $(COMMON_CFLAGS) bsd_mod_LDFLAGS = $(COMMON_LDFLAGS) bsd_mod_ASFLAGS = $(COMMON_ASFLAGS) diff --git a/conf/i386-pc.rmk b/conf/i386-pc.rmk index c155047f6..36ca76950 100644 --- a/conf/i386-pc.rmk +++ b/conf/i386-pc.rmk @@ -264,7 +264,7 @@ aout_mod_CFLAGS = $(COMMON_CFLAGS) aout_mod_LDFLAGS = $(COMMON_LDFLAGS) # For bsd.mod -bsd_mod_SOURCES = loader/i386/bsd.c loader/i386/bsd32.c loader/i386/bsd64.c loader/i386/bsd_helper.S loader/i386/bsd_trampoline.S +bsd_mod_SOURCES = loader/i386/bsd.c loader/i386/bsd32.c loader/i386/bsd64.c bsd_mod_CFLAGS = $(COMMON_CFLAGS) bsd_mod_LDFLAGS = $(COMMON_LDFLAGS) bsd_mod_ASFLAGS = $(COMMON_ASFLAGS) diff --git a/loader/i386/bsd_helper.S b/loader/i386/bsd_helper.S deleted file mode 100644 index 25aee3a80..000000000 --- a/loader/i386/bsd_helper.S +++ /dev/null @@ -1,45 +0,0 @@ -/* - * GRUB -- GRand Unified Bootloader - * Copyright (C) 1999,2000,2001,2002,2003,2005,2006,2007,2008, 2009 Free Software Foundation, Inc. - * - * GRUB 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. - * - * GRUB 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 GRUB. If not, see . - */ - -#include - - .p2align 2 - - - .code32 - -/* - * Use cdecl calling convention for *BSD kernels. - */ - -FUNCTION(grub_unix_real_boot) - - /* Interrupts should be disabled. */ - cli - - /* Discard `grub_unix_real_boot' return address. */ - popl %eax - - /* Fetch `entry' address ... */ - popl %eax - - /* - * ... and put our return address in its place. The kernel will - * ignore it, but it expects %esp to point to it. - */ - call *%eax diff --git a/loader/i386/bsd_trampoline.S b/loader/i386/bsd_trampoline.S deleted file mode 100644 index a568fff4d..000000000 --- a/loader/i386/bsd_trampoline.S +++ /dev/null @@ -1,124 +0,0 @@ -/* - * GRUB -- GRand Unified Bootloader - * Copyright (c) 2003 Peter Wemm - * Copyright (C) 2009 Free Software Foundation, Inc. - * - * GRUB 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. - * - * GRUB 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 GRUB. If not, see . - */ - -/* Based on the code from FreeBSD originally distributed under the - following terms: */ - -/*- - * Copyright (c) 2003 Peter Wemm - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - - -#define MSR_EFER 0xc0000080 -#define EFER_LME 0x00000100 -#define CR4_PAE 0x00000020 -#define CR4_PSE 0x00000010 -#define CR0_PG 0x80000000 - -#include - - .p2align 2 - - .code32 - - -VARIABLE(grub_bsd64_trampoline_start) - - /* Discard `grub_unix_real_boot' return address. */ - popl %eax - - /* entry */ - popl %edi - - /* entry_hi */ - popl %esi - - cli - - /* Turn on EFER.LME. */ - movl $MSR_EFER, %ecx - rdmsr - orl $EFER_LME, %eax - wrmsr - - /* Turn on PAE. */ - movl %cr4, %eax - orl $(CR4_PAE | CR4_PSE), %eax - movl %eax, %cr4 - - /* Set %cr3 for PT4. */ - popl %eax - movl %eax, %cr3 - - /* Push a dummy return address. */ - pushl %eax - - /* Turn on paging (implicitly sets EFER.LMA). */ - movl %cr0, %eax - orl $CR0_PG, %eax - movl %eax, %cr0 - - /* Now we're in compatibility mode. set %cs for long mode. */ - /* lgdt */ - .byte 0x0f - .byte 0x01 - .byte 0x15 -VARIABLE (grub_bsd64_trampoline_gdt) - .long 0x0 - - /* ljmp */ - .byte 0xea -VARIABLE (grub_bsd64_trampoline_selfjump) - .long 0x0 - .word 0x08 - - .code64 - -bsd64_longmode: - /* We're still running V=P, jump to entry point. */ - movl %esi, %eax - salq $32, %rax - orq %rdi, %rax - pushq %rax - ret -VARIABLE(grub_bsd64_trampoline_end) From 49a716be3b0b089763745cb3322f05dbb1f7c65f Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 12 Jan 2010 22:14:26 +0100 Subject: [PATCH 017/178] Possibility to prefer higher or lower chunks in relocator --- include/grub/relocator.h | 7 ++++++- lib/i386/relocator.c | 6 ++++-- lib/relocator.c | 21 +++++++++++++++++---- loader/i386/bsd.c | 9 ++++++--- loader/i386/linux.c | 6 ++---- loader/i386/multiboot.c | 3 ++- loader/i386/multiboot_mbi.c | 3 ++- loader/xnu_resume.c | 3 ++- 8 files changed, 41 insertions(+), 17 deletions(-) diff --git a/include/grub/relocator.h b/include/grub/relocator.h index 2ea74b775..32bab7053 100644 --- a/include/grub/relocator.h +++ b/include/grub/relocator.h @@ -34,7 +34,12 @@ grub_err_t grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, grub_addr_t *target, grub_addr_t min_addr, grub_addr_t max_addr, - grub_size_t size, grub_size_t align); + grub_size_t size, grub_size_t align, + int preference); + +#define GRUB_RELOCATOR_PREFERENCE_NONE 0 +#define GRUB_RELOCATOR_PREFERENCE_LOW 1 +#define GRUB_RELOCATOR_PREFERENCE_HIGH 2 void grub_relocator_unload (struct grub_relocator *rel); diff --git a/lib/i386/relocator.c b/lib/i386/relocator.c index e81dd8e1e..6e1e13b04 100644 --- a/lib/i386/relocator.c +++ b/lib/i386/relocator.c @@ -129,7 +129,8 @@ grub_relocator32_boot (struct grub_relocator *rel, err = grub_relocator_alloc_chunk_align (rel, &src, &target, 0, (0xffffffff - RELOCATOR_SIZEOF (32)) - + 1, RELOCATOR_SIZEOF (32), 16); + + 1, RELOCATOR_SIZEOF (32), 16, + GRUB_RELOCATOR_PREFERENCE_NONE); if (err) return err; @@ -166,7 +167,8 @@ grub_relocator64_boot (struct grub_relocator *rel, err = grub_relocator_alloc_chunk_align (rel, &src, &target, min_addr, max_addr - RELOCATOR_SIZEOF (64), - RELOCATOR_SIZEOF (64), 16); + RELOCATOR_SIZEOF (64), 16, + GRUB_RELOCATOR_PREFERENCE_NONE); if (err) return err; diff --git a/lib/relocator.c b/lib/relocator.c index 3085932c5..a5b3c6daf 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -446,7 +446,8 @@ grub_err_t grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, grub_addr_t *target, grub_addr_t min_addr, grub_addr_t max_addr, - grub_size_t size, grub_size_t align) + grub_size_t size, grub_size_t align, + int preference) { grub_addr_t min_addr2 = 0, max_addr2; struct grub_relocator_chunk *chunk; @@ -455,6 +456,11 @@ grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, if (max_addr > ~size) max_addr = ~size; +#ifdef GRUB_MACHINE_PCBIOS + if (min_addr < 0x1000) + min_addr = 0x1000; +#endif + grub_dprintf ("relocator", "chunks = %p\n", rel->chunks); chunk = grub_malloc (sizeof (struct grub_relocator_chunk)); @@ -462,7 +468,8 @@ grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, return grub_errno; if (malloc_in_range (rel, min_addr, max_addr, align, - size, &start, 1, 1)) + size, &start, + preference != GRUB_RELOCATOR_PREFERENCE_HIGH, 1)) { grub_dprintf ("relocator", "allocated 0x%llx/0x%llx\n", (unsigned long long) start, (unsigned long long) start); @@ -500,7 +507,10 @@ grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, while (0); /* FIXME: check memory map. */ - chunk->target = ALIGN_UP (min_addr, align); + if (preference == GRUB_RELOCATOR_PREFERENCE_HIGH) + chunk->target = ALIGN_DOWN (max_addr, align); + else + chunk->target = ALIGN_UP (min_addr, align); while (1) { struct grub_relocator_chunk *chunk2; @@ -514,7 +524,10 @@ grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, || (chunk->target <= chunk2->target + chunk2->size && chunk2->target + chunk2->size < chunk->target + size)) { - chunk->target = ALIGN_UP (chunk2->target + chunk2->size, align); + if (preference == GRUB_RELOCATOR_PREFERENCE_HIGH) + chunk->target = ALIGN_DOWN (chunk2->target, align); + else + chunk->target = ALIGN_UP (chunk2->target + chunk2->size, align); break; } if (!chunk2) diff --git a/loader/i386/bsd.c b/loader/i386/bsd.c index 90cd8a9a4..28bcde15e 100644 --- a/loader/i386/bsd.c +++ b/loader/i386/bsd.c @@ -550,7 +550,8 @@ grub_freebsd_boot (void) &stack_target, 0x10000, 0x90000, 3 * sizeof (grub_uint32_t) - + sizeof (bi), 4); + + sizeof (bi), 4, + GRUB_RELOCATOR_PREFERENCE_NONE); if (err) return err; @@ -575,7 +576,8 @@ grub_freebsd_boot (void) &stack_target, 0x10000, 0x90000, 9 * sizeof (grub_uint32_t) - + sizeof (bi), 4); + + sizeof (bi), 4, + GRUB_RELOCATOR_PREFERENCE_NONE); if (err) return err; grub_memcpy (&stack[8], &bi, sizeof (bi)); @@ -798,7 +800,8 @@ grub_netbsd_boot (void) err = grub_relocator_alloc_chunk_align (relocator, (void **) &stack, &stack_target, 0x10000, 0x90000, - 7 * sizeof (grub_uint32_t), 4); + 7 * sizeof (grub_uint32_t), 4, + GRUB_RELOCATOR_PREFERENCE_NONE); if (err) return err; diff --git a/loader/i386/linux.c b/loader/i386/linux.c index e8d06b0e7..b6298d0bb 100644 --- a/loader/i386/linux.c +++ b/loader/i386/linux.c @@ -943,9 +943,6 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), addr_min = (grub_addr_t) prot_mode_target + ((prot_mode_pages * 3) << 12) + page_align (size); - if (addr_max > grub_os_area_addr + grub_os_area_size) - addr_max = grub_os_area_addr + grub_os_area_size; - /* Put the initrd as high as possible, 4KiB aligned. */ addr = (addr_max - size) & ~0xFFF; @@ -957,7 +954,8 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), err = grub_relocator_alloc_chunk_align (relocator, &initrd_mem, &initrd_mem_target, - addr_min, addr, size, 0x1000); + addr_min, addr, size, 0x1000, + GRUB_RELOCATOR_PREFERENCE_HIGH); if (err) return err; diff --git a/loader/i386/multiboot.c b/loader/i386/multiboot.c index c5a7f7f9d..2f9cc73c9 100644 --- a/loader/i386/multiboot.c +++ b/loader/i386/multiboot.c @@ -259,7 +259,8 @@ grub_module (int argc, char *argv[]) err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, &module, &target, 0, (0xffffffff - size) + 1, - size, MULTIBOOT_MOD_ALIGN); + size, MULTIBOOT_MOD_ALIGN, + GRUB_RELOCATOR_PREFERENCE_NONE); if (err) goto fail; diff --git a/loader/i386/multiboot_mbi.c b/loader/i386/multiboot_mbi.c index ddaca7a1b..4fc9a7ac1 100644 --- a/loader/i386/multiboot_mbi.c +++ b/loader/i386/multiboot_mbi.c @@ -125,7 +125,8 @@ grub_multiboot_make_mbi (grub_uint32_t *target) err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, (void **) &ptrorig, &ptrdest, 0, 0xffffffff - bufsize, - bufsize, 4); + bufsize, 4, + GRUB_RELOCATOR_PREFERENCE_NONE); if (err) return err; diff --git a/loader/xnu_resume.c b/loader/xnu_resume.c index a7d5fbad8..2d47df601 100644 --- a/loader/xnu_resume.c +++ b/loader/xnu_resume.c @@ -115,7 +115,8 @@ grub_xnu_resume (char *imagename) &target_image, 0, (0xffffffff - hibhead.image_size) + 1, hibhead.image_size, - GRUB_XNU_PAGESIZE); + GRUB_XNU_PAGESIZE, + GRUB_RELOCATOR_PREFERENCE_NONE); if (err) { grub_file_close (file); From cdab631686052592b70daf341d0ccd783a598592 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 12 Jan 2010 22:15:50 +0100 Subject: [PATCH 018/178] Relocator16 support --- conf/i386.rmk | 2 +- include/grub/i386/relocator.h | 15 +++ lib/i386/relocator.c | 51 ++++++++ lib/i386/relocator16.S | 225 ++++++++++++++++++++++++++++++++++ 4 files changed, 292 insertions(+), 1 deletion(-) create mode 100644 lib/i386/relocator16.S diff --git a/conf/i386.rmk b/conf/i386.rmk index 72ea6d465..2efd9895a 100644 --- a/conf/i386.rmk +++ b/conf/i386.rmk @@ -17,7 +17,7 @@ vga_text_mod_LDFLAGS = $(COMMON_LDFLAGS) pkglib_MODULES += relocator.mod relocator_mod_SOURCES = lib/relocator.c lib/i386/relocator32.S \ - lib/i386/relocator64.S \ + lib/i386/relocator64.S lib/i386/relocator16.S \ lib/i386/relocator_asm.S lib/i386/relocator.c relocator_mod_CFLAGS = $(COMMON_CFLAGS) relocator_mod_ASFLAGS = $(COMMON_ASFLAGS) diff --git a/include/grub/i386/relocator.h b/include/grub/i386/relocator.h index ac49dd29e..f32413a1b 100644 --- a/include/grub/i386/relocator.h +++ b/include/grub/i386/relocator.h @@ -34,6 +34,18 @@ struct grub_relocator32_state grub_uint32_t esi; }; +struct grub_relocator16_state +{ + grub_uint16_t cs; + grub_uint16_t ds; + grub_uint16_t es; + grub_uint16_t fs; + grub_uint16_t gs; + grub_uint16_t ss; + grub_uint16_t sp; + grub_uint16_t ip; +}; + struct grub_relocator64_state { grub_uint64_t rsp; @@ -46,6 +58,9 @@ struct grub_relocator64_state grub_addr_t cr3; }; +grub_err_t grub_relocator16_boot (struct grub_relocator *rel, + struct grub_relocator16_state state); + grub_err_t grub_relocator32_boot (struct grub_relocator *rel, struct grub_relocator32_state state); diff --git a/lib/i386/relocator.c b/lib/i386/relocator.c index 6e1e13b04..5757bb6df 100644 --- a/lib/i386/relocator.c +++ b/lib/i386/relocator.c @@ -39,6 +39,17 @@ extern void *grub_relocator_forward_dest; extern void *grub_relocator_forward_src; extern grub_size_t grub_relocator_forward_chunk_size; +extern grub_uint8_t grub_relocator16_start; +extern grub_uint8_t grub_relocator16_end; +extern grub_uint16_t grub_relocator16_cs; +extern grub_uint16_t grub_relocator16_ip; +extern grub_uint16_t grub_relocator16_ds; +extern grub_uint16_t grub_relocator16_es; +extern grub_uint16_t grub_relocator16_fs; +extern grub_uint16_t grub_relocator16_gs; +extern grub_uint16_t grub_relocator16_ss; +extern grub_uint16_t grub_relocator16_sp; + extern grub_uint8_t grub_relocator32_start; extern grub_uint8_t grub_relocator32_end; extern grub_uint32_t grub_relocator32_eax; @@ -155,6 +166,46 @@ grub_relocator32_boot (struct grub_relocator *rel, return GRUB_ERR_NONE; } +grub_err_t +grub_relocator16_boot (struct grub_relocator *rel, + struct grub_relocator16_state state) +{ + grub_addr_t target; + void *src; + grub_err_t err; + grub_addr_t relst; + + err = grub_relocator_alloc_chunk_align (rel, &src, &target, 0, + 0xa0000 - RELOCATOR_SIZEOF (16), + RELOCATOR_SIZEOF (16), 16, + GRUB_RELOCATOR_PREFERENCE_NONE); + if (err) + return err; + + grub_relocator16_cs = state.cs; + grub_relocator16_ip = state.ip; + + grub_relocator16_ds = state.ds; + grub_relocator16_es = state.es; + grub_relocator16_fs = state.fs; + grub_relocator16_gs = state.gs; + + grub_relocator16_ss = state.ss; + grub_relocator16_sp = state.sp; + + grub_memmove (src, &grub_relocator16_start, RELOCATOR_SIZEOF (16)); + + err = grub_relocator_prepare_relocs (rel, target, &relst); + if (err) + return err; + + asm volatile ("cli"); + ((void (*) (void)) relst) (); + + /* Not reached. */ + return GRUB_ERR_NONE; +} + grub_err_t grub_relocator64_boot (struct grub_relocator *rel, struct grub_relocator64_state state, diff --git a/lib/i386/relocator16.S b/lib/i386/relocator16.S new file mode 100644 index 000000000..d35adecd8 --- /dev/null +++ b/lib/i386/relocator16.S @@ -0,0 +1,225 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2009,2010 Free Software Foundation, Inc. + * + * GRUB 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. + * + * GRUB 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 GRUB. If not, see . + */ + +#include +#include + +#ifdef __x86_64__ +#define RAX %rax +#define RSI %rdi +#else +#define RAX %eax +#define RSI %esi +#endif + +/* The code segment of the protected mode. */ +#define CODE_SEGMENT 0x08 + +/* The data segment of the protected mode. */ +#define DATA_SEGMENT 0x10 + +#define PSEUDO_REAL_CSEG 0x18 + +#define PSEUDO_REAL_DSEG 0x20 + + .p2align 4 /* force 16-byte alignment */ + +VARIABLE(grub_relocator16_start) +LOCAL(base): + /* %rax contains now our new 'base'. */ + mov RAX, RSI + add $(LOCAL(cont0) - LOCAL(base)), RAX + jmp *RAX +LOCAL(cont0): + lea (LOCAL(cont1) - LOCAL(base)) (RSI, 1), RAX + movl %eax, (LOCAL(jump_vector) - LOCAL(base)) (RSI, 1) + + lea (LOCAL(gdt) - LOCAL(base)) (RSI, 1), RAX + mov RAX, (LOCAL(gdt_addr) - LOCAL(base)) (RSI, 1) + + movl %esi, %eax + movw %ax, (LOCAL (cs_base_bytes12) - LOCAL (base)) (RSI, 1) + shrl $16, %eax + movb %al, (LOCAL (cs_base_byte3) - LOCAL (base)) (RSI, 1) + + /* Switch to compatibility mode. */ + + lgdt (LOCAL(gdtdesc) - LOCAL(base)) (RSI, 1) + + /* Update %cs. */ + ljmp *(LOCAL(jump_vector) - LOCAL(base)) (RSI, 1) + +LOCAL(cont1): + .code32 + + /* Disable paging. */ + movl %cr0, %eax + andl $(~GRUB_MEMORY_CPU_CR0_PAGING_ON), %eax + movl %eax, %cr0 + + /* Disable amd64. */ + movl $GRUB_MEMORY_CPU_AMD64_MSR, %ecx + rdmsr + andl $(~GRUB_MEMORY_CPU_AMD64_MSR_ON), %eax + wrmsr + + /* Turn off PAE. */ + movl %cr4, %eax + andl $GRUB_MEMORY_CPU_CR4_PAE_ON, %eax + movl %eax, %cr4 + + /* Update other registers. */ + movl $PSEUDO_REAL_DSEG, %eax + movl %eax, %ds + movl %eax, %es + movl %eax, %fs + movl %eax, %gs + movl %eax, %ss + + movl %esi, %eax + shrl $4, %eax + movw %ax, (LOCAL (segment) - LOCAL (base)) (RSI, 1) + + /* jump to a 16 bit segment */ + ljmp $PSEUDO_REAL_CSEG, $(LOCAL (cont2) - LOCAL(base)) +LOCAL(cont2): + .code16 + + /* clear the PE bit of CR0 */ + movl %cr0, %eax + andl $(~GRUB_MEMORY_CPU_CR0_PE_ON), %eax + movl %eax, %cr0 + + /* flush prefetch queue, reload %cs */ + /* ljmp */ + .byte 0xea + .word LOCAL(cont3)-LOCAL(base) +LOCAL(segment): + .word 0 + +LOCAL(cont3): + /* we are in real mode now + * set up the real mode segment registers : DS, SS, ES + */ + /* movw imm16, %ax. */ + .byte 0xb8 +VARIABLE(grub_relocator16_ds) + .word 0 + movw %ax, %ds + + /* movw imm16, %ax. */ + .byte 0xb8 +VARIABLE(grub_relocator16_es) + .word 0 + movw %ax, %es + + /* movw imm16, %ax. */ + .byte 0xb8 +VARIABLE(grub_relocator16_fs) + .word 0 + movw %ax, %fs + + /* movw imm16, %ax. */ + .byte 0xb8 +VARIABLE(grub_relocator16_gs) + .word 0 + movw %ax, %gs + + /* movw imm16, %ax. */ + .byte 0xb8 +VARIABLE(grub_relocator16_ss) + .word 0 + movw %ax, %ss + + /* movw imm16, %ax. */ + .byte 0xb8 +VARIABLE(grub_relocator16_sp) + .word 0 + movw %ax, %ss + + /* Cleared direction flag is of no problem with any current + payload and makes this implementation easier. */ + cld + + /* ljmp */ + .byte 0xea +VARIABLE(grub_relocator16_ip) + .word 0 +VARIABLE(grub_relocator16_cs) + .word 0 + + .code32 + + /* GDT. Copied from loader/i386/linux.c. */ + .p2align 4 +LOCAL(gdt): + .word 0, 0 + .byte 0, 0, 0, 0 + + /* -- code segment -- + * base = 0x00000000, limit = 0xFFFFF (4 KiB Granularity), present + * type = 32bit code execute/read, DPL = 0 + */ + .word 0xFFFF, 0 + .byte 0, 0x9A, 0xCF, 0 + + /* -- data segment -- + * base = 0x00000000, limit 0xFFFFF (4 KiB Granularity), present + * type = 32 bit data read/write, DPL = 0 + */ + .word 0xFFFF, 0 + .byte 0, 0x92, 0xCF, 0 + + /* -- 16 bit real mode CS -- + * base = 0x00000000, limit 0x0FFFF (1 B Granularity), present + * type = 16 bit code execute/read only/conforming, DPL = 0 + */ + .word 0xFFFF +LOCAL(cs_base_bytes12): + .word 0 +LOCAL(cs_base_byte3): + .byte 0 + + .byte 0x9E, 0, 0 + + /* -- 16 bit real mode DS -- + * base = 0x00000000, limit 0x0FFFF (1 B Granularity), present + * type = 16 bit data read/write, DPL = 0 + */ + .word 0xFFFF, 0 + .byte 0, 0x92, 0, 0 + + .p2align 4 +LOCAL(gdtdesc): + .word 0x27 +LOCAL(gdt_addr): +#ifdef __x86_64__ + /* Filled by the code. */ + .quad 0 +#else + /* Filled by the code. */ + .long 0 +#endif + + .p2align 4 +LOCAL(jump_vector): + /* Jump location. Is filled by the code */ + .long 0 + .long CODE_SEGMENT + +VARIABLE(grub_relocator16_end) From 14dacc815aa9a923ffe68c9e355ea38e7c90ce15 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 12 Jan 2010 22:16:33 +0100 Subject: [PATCH 019/178] Clarify type of cmd_line_ptr --- include/grub/i386/linux.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/grub/i386/linux.h b/include/grub/i386/linux.h index 8a5a84da3..ecda4ed94 100644 --- a/include/grub/i386/linux.h +++ b/include/grub/i386/linux.h @@ -124,7 +124,7 @@ struct linux_kernel_header grub_uint32_t bootsect_kludge; /* obsolete */ grub_uint16_t heap_end_ptr; /* Free memory after setup end */ grub_uint16_t pad1; /* Unused */ - char *cmd_line_ptr; /* Points to the kernel command line */ + grub_uint32_t cmd_line_ptr; /* Points to the kernel command line */ grub_uint32_t initrd_addr_max; /* Highest address for initrd */ } __attribute__ ((packed)); From c911e8791afe1cca5610d3b06aa6815a9ed845a2 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 12 Jan 2010 22:17:26 +0100 Subject: [PATCH 020/178] Port linux16 to relocator framework --- include/grub/i386/loader.h | 37 ---------- loader/i386/pc/linux.c | 144 ++++++++++++++++++++++++------------- 2 files changed, 93 insertions(+), 88 deletions(-) diff --git a/include/grub/i386/loader.h b/include/grub/i386/loader.h index 0df5f757f..e69de29bb 100644 --- a/include/grub/i386/loader.h +++ b/include/grub/i386/loader.h @@ -1,37 +0,0 @@ -/* - * GRUB -- GRand Unified Bootloader - * Copyright (C) 2002,2003,2004,2007,2008,2009 Free Software Foundation, Inc. - * - * GRUB 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. - * - * GRUB 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 GRUB. If not, see . - */ - -#ifndef GRUB_LOADER_CPU_HEADER -#define GRUB_LOADER_CPU_HEADER 1 - -#include -#include -#include - -extern grub_addr_t EXPORT_VAR(grub_os_area_addr); -extern grub_size_t EXPORT_VAR(grub_os_area_size); - -#ifdef GRUB_MACHINE_PCBIOS -extern grub_uint32_t EXPORT_VAR(grub_linux_prot_size); -extern char *EXPORT_VAR(grub_linux_tmp_addr); -extern char *EXPORT_VAR(grub_linux_real_addr); -extern grub_int32_t EXPORT_VAR(grub_linux_is_bzimage); -grub_err_t EXPORT_FUNC(grub_linux16_boot) (void); -#endif - -#endif /* ! GRUB_LOADER_CPU_HEADER */ diff --git a/loader/i386/pc/linux.c b/loader/i386/pc/linux.c index 24bb39555..6c29029f2 100644 --- a/loader/i386/pc/linux.c +++ b/loader/i386/pc/linux.c @@ -31,6 +31,8 @@ #include #include #include +#include +#include #define GRUB_LINUX_CL_OFFSET 0x9000 #define GRUB_LINUX_CL_END_OFFSET 0x90FF @@ -39,12 +41,34 @@ static grub_dl_t my_mod; static grub_size_t linux_mem_size; static int loaded; +static struct grub_relocator *relocator = NULL; +static grub_addr_t grub_linux_real_target; +static char *grub_linux_real_chunk; +static grub_size_t grub_linux16_prot_size; + +static grub_err_t +grub_linux16_boot (void) +{ + grub_uint16_t segment; + struct grub_relocator16_state state; + + segment = grub_linux_real_target >> 4; + state.gs = state.fs = state.es = state.ds = state.ss = segment; + state.sp = GRUB_LINUX_SETUP_STACK; + state.cs = segment + 0x20; + state.ip = 0; + + return grub_relocator16_boot (relocator, state); +} + static grub_err_t grub_linux_unload (void) { grub_dl_unref (my_mod); loaded = 0; + grub_relocator_unload (relocator); + relocator = NULL; return GRUB_ERR_NONE; } @@ -55,10 +79,14 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), grub_file_t file = 0; struct linux_kernel_header lh; grub_uint8_t setup_sects; - grub_size_t real_size, prot_size; + grub_size_t real_size; grub_ssize_t len; int i; char *dest; + char *grub_linux_prot_chunk; + int grub_linux_is_bzimage; + grub_addr_t grub_linux_prot_target; + grub_err_t err; grub_dl_ref (my_mod); @@ -72,14 +100,6 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), if (! file) goto fail; - if ((grub_size_t) grub_file_size (file) > grub_os_area_size) - { - grub_error (GRUB_ERR_OUT_OF_RANGE, "too big kernel (0x%x > 0x%x)", - (grub_size_t) grub_file_size (file), - grub_os_area_size); - goto fail; - } - if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh)) { grub_error (GRUB_ERR_READ_ERROR, "cannot read the Linux header"); @@ -109,12 +129,11 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), lh.type_of_loader = GRUB_LINUX_BOOT_LOADER_TYPE; /* Put the real mode part at as a high location as possible. */ - grub_linux_real_addr - = (char *) UINT_TO_PTR (grub_mmap_get_lower () - - GRUB_LINUX_SETUP_MOVE_SIZE); + grub_linux_real_target = grub_mmap_get_lower () + - GRUB_LINUX_SETUP_MOVE_SIZE; /* But it must not exceed the traditional area. */ - if (grub_linux_real_addr > (char *) GRUB_LINUX_OLD_REAL_MODE_ADDR) - grub_linux_real_addr = (char *) GRUB_LINUX_OLD_REAL_MODE_ADDR; + if (grub_linux_real_target > GRUB_LINUX_OLD_REAL_MODE_ADDR) + grub_linux_real_target = GRUB_LINUX_OLD_REAL_MODE_ADDR; if (grub_le_to_cpu16 (lh.version) >= 0x0201) { @@ -123,7 +142,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), } if (grub_le_to_cpu16 (lh.version) >= 0x0202) - lh.cmd_line_ptr = grub_linux_real_addr + GRUB_LINUX_CL_OFFSET; + lh.cmd_line_ptr = grub_linux_real_target + GRUB_LINUX_CL_OFFSET; else { lh.cl_magic = grub_cpu_to_le16 (GRUB_LINUX_CL_MAGIC); @@ -139,7 +158,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS; - grub_linux_real_addr = (char *) GRUB_LINUX_OLD_REAL_MODE_ADDR; + grub_linux_real_target = GRUB_LINUX_OLD_REAL_MODE_ADDR; } /* If SETUP_SECTS is not set, set it to the default (4). */ @@ -147,31 +166,36 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS; real_size = setup_sects << GRUB_DISK_SECTOR_BITS; - prot_size = grub_file_size (file) - real_size - GRUB_DISK_SECTOR_SIZE; - - grub_linux_tmp_addr = (char *) GRUB_LINUX_BZIMAGE_ADDR + prot_size; + grub_linux16_prot_size = grub_file_size (file) + - real_size - GRUB_DISK_SECTOR_SIZE; if (! grub_linux_is_bzimage - && ((char *) GRUB_LINUX_ZIMAGE_ADDR + prot_size > grub_linux_real_addr)) + && GRUB_LINUX_ZIMAGE_ADDR + grub_linux16_prot_size + > grub_linux_real_target) { grub_error (GRUB_ERR_BAD_OS, "too big zImage (0x%x > 0x%x), use bzImage instead", - (char *) GRUB_LINUX_ZIMAGE_ADDR + prot_size, - (grub_size_t) grub_linux_real_addr); + (char *) GRUB_LINUX_ZIMAGE_ADDR + grub_linux16_prot_size, + (grub_size_t) grub_linux_real_target); goto fail; } - if (grub_linux_real_addr + GRUB_LINUX_SETUP_MOVE_SIZE - > (char *) UINT_TO_PTR (grub_mmap_get_lower ())) + if (grub_linux_real_target + GRUB_LINUX_SETUP_MOVE_SIZE + > grub_mmap_get_lower ()) { grub_error (GRUB_ERR_OUT_OF_RANGE, "too small lower memory (0x%x > 0x%x)", - grub_linux_real_addr + GRUB_LINUX_SETUP_MOVE_SIZE, + grub_linux_real_target + GRUB_LINUX_SETUP_MOVE_SIZE, (int) grub_mmap_get_lower ()); goto fail; } grub_printf (" [Linux-%s, setup=0x%x, size=0x%x]\n", - grub_linux_is_bzimage ? "bzImage" : "zImage", real_size, prot_size); + grub_linux_is_bzimage ? "bzImage" : "zImage", real_size, + grub_linux16_prot_size); + + relocator = grub_relocator_new (); + if (!relocator) + goto fail; for (i = 1; i < argc; i++) if (grub_memcmp (argv[i], "vga=", 4) == 0) @@ -229,11 +253,18 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), } } + err = grub_relocator_alloc_chunk_addr (relocator, (void **) + &grub_linux_real_chunk, + grub_linux_real_target, + GRUB_LINUX_SETUP_MOVE_SIZE); + if (err) + return err; + /* Put the real mode code at the temporary address. */ - grub_memmove (grub_linux_tmp_addr, &lh, sizeof (lh)); + grub_memmove (grub_linux_real_chunk, &lh, sizeof (lh)); len = real_size + GRUB_DISK_SECTOR_SIZE - sizeof (lh); - if (grub_file_read (file, grub_linux_tmp_addr + sizeof (lh), len) != len) + if (grub_file_read (file, grub_linux_real_chunk + sizeof (lh), len) != len) { grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file"); goto fail; @@ -242,21 +273,21 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), if (lh.header != grub_cpu_to_le32 (GRUB_LINUX_MAGIC_SIGNATURE) || grub_le_to_cpu16 (lh.version) < 0x0200) /* Clear the heap space. */ - grub_memset (grub_linux_tmp_addr + grub_memset (grub_linux_real_chunk + ((setup_sects + 1) << GRUB_DISK_SECTOR_BITS), 0, ((GRUB_LINUX_MAX_SETUP_SECTS - setup_sects - 1) << GRUB_DISK_SECTOR_BITS)); /* Specify the boot file. */ - dest = grub_stpcpy (grub_linux_tmp_addr + GRUB_LINUX_CL_OFFSET, + dest = grub_stpcpy (grub_linux_real_chunk + GRUB_LINUX_CL_OFFSET, "BOOT_IMAGE="); dest = grub_stpcpy (dest, argv[0]); /* Copy kernel parameters. */ for (i = 1; i < argc - && dest + grub_strlen (argv[i]) + 1 < (grub_linux_tmp_addr + && dest + grub_strlen (argv[i]) + 1 < (grub_linux_real_chunk + GRUB_LINUX_CL_END_OFFSET); i++) { @@ -264,14 +295,25 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), dest = grub_stpcpy (dest, argv[i]); } - len = prot_size; - if (grub_file_read (file, (void *) GRUB_LINUX_BZIMAGE_ADDR, len) != len) + if (grub_linux_is_bzimage) + grub_linux_prot_target = GRUB_LINUX_BZIMAGE_ADDR; + else + grub_linux_prot_target = GRUB_LINUX_ZIMAGE_ADDR; + err = grub_relocator_alloc_chunk_addr (relocator, + (void **) &grub_linux_prot_chunk, + grub_linux_prot_target, + grub_linux16_prot_size); + if (err) + return err; + + len = grub_linux16_prot_size; + if (grub_file_read (file, grub_linux_prot_chunk, grub_linux16_prot_size) + != (grub_ssize_t) grub_linux16_prot_size) grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file"); if (grub_errno == GRUB_ERR_NONE) { - grub_linux_prot_size = prot_size; - grub_loader_set (grub_linux16_boot, grub_linux_unload, 1); + grub_loader_set (grub_linux16_boot, grub_linux_unload, 0); loaded = 1; } @@ -284,6 +326,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), { grub_dl_unref (my_mod); loaded = 0; + grub_relocator_unload (relocator); } return grub_errno; @@ -295,8 +338,11 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), { grub_file_t file = 0; grub_ssize_t size; - grub_addr_t addr_max, addr_min, addr; + grub_addr_t addr_max, addr_min; struct linux_kernel_header *lh; + grub_uint8_t *initrd_chunk; + grub_addr_t initrd_addr; + grub_err_t err; if (argc == 0) { @@ -310,7 +356,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), goto fail; } - lh = (struct linux_kernel_header *) grub_linux_tmp_addr; + lh = (struct linux_kernel_header *) grub_linux_real_chunk; if (!(lh->header == grub_cpu_to_le32 (GRUB_LINUX_MAGIC_SIGNATURE) && grub_le_to_cpu16 (lh->version) >= 0x0200)) @@ -342,10 +388,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), worse than that of Linux 2.3.xx, so avoid the last 64kb. */ addr_max -= 0x10000; - if (addr_max > grub_os_area_addr + grub_os_area_size) - addr_max = grub_os_area_addr + grub_os_area_size; - - addr_min = (grub_addr_t) grub_linux_tmp_addr + GRUB_LINUX_CL_END_OFFSET; + addr_min = GRUB_LINUX_BZIMAGE_ADDR + grub_linux16_prot_size; file = grub_file_open (argv[0]); if (!file) @@ -353,22 +396,21 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), size = grub_file_size (file); - /* Put the initrd as high as possible, 4KiB aligned. */ - addr = (addr_max - size) & ~0xFFF; + err = grub_relocator_alloc_chunk_align (relocator, (void **) &initrd_chunk, + &initrd_addr, + addr_min, addr_max - size, + size, 0x1000, + GRUB_RELOCATOR_PREFERENCE_HIGH); + if (err) + return err; - if (addr < addr_min) - { - grub_error (GRUB_ERR_OUT_OF_RANGE, "the initrd is too big"); - goto fail; - } - - if (grub_file_read (file, (void *) addr, size) != size) + if (grub_file_read (file, initrd_chunk, size) != size) { grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file"); goto fail; } - lh->ramdisk_image = addr; + lh->ramdisk_image = initrd_addr; lh->ramdisk_size = size; fail: From 1b86596ae19abef99da535cefc70305b64f501f7 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 12 Jan 2010 22:17:44 +0100 Subject: [PATCH 021/178] Remove OS area --- kern/i386/pc/init.c | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/kern/i386/pc/init.c b/kern/i386/pc/init.c index 3885bba7a..382e097e4 100644 --- a/kern/i386/pc/init.c +++ b/kern/i386/pc/init.c @@ -43,9 +43,6 @@ struct mem_region static struct mem_region mem_regions[MAX_REGIONS]; static int num_regions; -grub_addr_t grub_os_area_addr; -grub_size_t grub_os_area_size; - void grub_arch_sync_caches (void *address __attribute__ ((unused)), grub_size_t len __attribute__ ((unused))) @@ -186,25 +183,9 @@ grub_machine_init (void) compact_mem_regions (); - /* Add the memory regions to free memory, except for the region starting - from 1MB. This region is partially used for loading OS images. - For now, 1/4 of this is added to free memory. */ for (i = 0; i < num_regions; i++) - /* if (mem_regions[i].addr == 0x100000) - { - grub_size_t quarter = mem_regions[i].size >> 2; - - grub_os_area_addr = mem_regions[i].addr; - grub_os_area_size = mem_regions[i].size - quarter; - grub_mm_init_region ((void *) (grub_os_area_addr + grub_os_area_size), - quarter); - } - else*/ grub_mm_init_region ((void *) mem_regions[i].addr, mem_regions[i].size); - // if (! grub_os_area_addr) - //grub_fatal ("no upper memory"); - grub_tsc_init (); } From e6e7b4ea1f4d29fd17a72cf891e604d4af0e1ad3 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 12 Jan 2010 22:22:36 +0100 Subject: [PATCH 022/178] Remove i386/loader.h --- include/grub/i386/loader.h | 0 include/grub/i386/pc/loader.h | 1 - loader/i386/bsd.c | 1 - loader/i386/bsdXX.c | 1 - 4 files changed, 3 deletions(-) delete mode 100644 include/grub/i386/loader.h diff --git a/include/grub/i386/loader.h b/include/grub/i386/loader.h deleted file mode 100644 index e69de29bb..000000000 diff --git a/include/grub/i386/pc/loader.h b/include/grub/i386/pc/loader.h index 3e031413b..bfbcaac5a 100644 --- a/include/grub/i386/pc/loader.h +++ b/include/grub/i386/pc/loader.h @@ -20,7 +20,6 @@ #define GRUB_LOADER_MACHINE_HEADER 1 #include -#include /* This is an asm part of the chainloader. */ void EXPORT_FUNC(grub_chainloader_real_boot) (int drive, void *part_addr) __attribute__ ((noreturn)); diff --git a/loader/i386/bsd.c b/loader/i386/bsd.c index 28bcde15e..9c42f6a5c 100644 --- a/loader/i386/bsd.c +++ b/loader/i386/bsd.c @@ -17,7 +17,6 @@ */ #include -#include #include #include #include diff --git a/loader/i386/bsdXX.c b/loader/i386/bsdXX.c index 8f5cfa750..ad6c1bc75 100644 --- a/loader/i386/bsdXX.c +++ b/loader/i386/bsdXX.c @@ -3,7 +3,6 @@ #include #include #include -#include #include #define ALIGN_PAGE(a) ALIGN_UP (a, 4096) From e39029dd1ea7c7aa9718940da2baecd3712bf21f Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 12 Jan 2010 22:24:41 +0100 Subject: [PATCH 023/178] Remove i386/loader.S --- kern/i386/loader.S | 120 ----------------------------------------- kern/i386/pc/startup.S | 2 - 2 files changed, 122 deletions(-) delete mode 100644 kern/i386/loader.S diff --git a/kern/i386/loader.S b/kern/i386/loader.S deleted file mode 100644 index 3e9c71327..000000000 --- a/kern/i386/loader.S +++ /dev/null @@ -1,120 +0,0 @@ -/* - * GRUB -- GRand Unified Bootloader - * Copyright (C) 1999,2000,2001,2002,2003,2005,2006,2007,2008 Free Software Foundation, Inc. - * - * GRUB 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. - * - * GRUB 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 GRUB. If not, see . - */ - - -/* - * Note: These functions defined in this file may be called from C. - * Be careful of that you must not modify some registers. Quote - * from gcc-2.95.2/gcc/config/i386/i386.h: - - 1 for registers not available across function calls. - These must include the FIXED_REGISTERS and also any - registers that can be used without being saved. - The latter must include the registers where values are returned - and the register where structure-value addresses are passed. - Aside from that, you can include as many other registers as you like. - - ax,dx,cx,bx,si,di,bp,sp,st,st1,st2,st3,st4,st5,st6,st7,arg -{ 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } - */ - -/* - * Note: GRUB is compiled with the options -mrtd and -mregparm=3. - * So the first three arguments are passed in %eax, %edx, and %ecx, - * respectively, and if a function has a fixed number of arguments - * and the number if greater than three, the function must return - * with "ret $N" where N is ((the number of arguments) - 3) * 4. - */ - -/* - * This is the area for all of the special variables. - */ - - .p2align 2 /* force 4-byte alignment */ - -/* - * void grub_linux_boot_zimage (void) - */ -VARIABLE(grub_linux_prot_size) - .long 0 -VARIABLE(grub_linux_tmp_addr) - .long 0 -VARIABLE(grub_linux_real_addr) - .long 0 -VARIABLE(grub_linux_is_bzimage) - .long 0 - -FUNCTION(grub_linux16_boot) - /* Must be done before zImage copy. */ - call EXT_C(grub_dl_unload_all) - - movl EXT_C(grub_linux_is_bzimage), %ebx - test %ebx, %ebx - jne bzimage - - /* copy the kernel */ - movl EXT_C(grub_linux_prot_size), %ecx - addl $3, %ecx - shrl $2, %ecx - movl $GRUB_LINUX_BZIMAGE_ADDR, %esi - movl $GRUB_LINUX_ZIMAGE_ADDR, %edi - cld - rep - movsl - -bzimage: - movl EXT_C(grub_linux_real_addr), %ebx - - /* copy the real mode code */ - movl EXT_C(grub_linux_tmp_addr), %esi - movl %ebx, %edi - movl $GRUB_LINUX_SETUP_MOVE_SIZE, %ecx - cld - rep - movsb - - /* change %ebx to the segment address */ - shrl $4, %ebx - movl %ebx, %eax - addl $0x20, %eax - movw %ax, linux_setup_seg - - /* XXX new stack pointer in safe area for calling functions */ - movl $0x4000, %esp - call EXT_C(grub_stop_floppy) - - /* final setup for linux boot */ - call prot_to_real - .code16 - - cli - movw %bx, %ss - movw $GRUB_LINUX_SETUP_STACK, %sp - - movw %bx, %ds - movw %bx, %es - movw %bx, %fs - movw %bx, %gs - - /* ljmp */ - .byte 0xea - .word 0 -linux_setup_seg: - .word 0 - .code32 - diff --git a/kern/i386/pc/startup.S b/kern/i386/pc/startup.S index 23f3f398e..1a44792d5 100644 --- a/kern/i386/pc/startup.S +++ b/kern/i386/pc/startup.S @@ -564,8 +564,6 @@ FUNCTION(grub_chainloader_real_boot) ljmp $0, $GRUB_MEMORY_MACHINE_BOOT_LOADER_ADDR .code32 -#include "../loader.S" - /* * int grub_biosdisk_rw_int13_extensions (int ah, int drive, void *dap) * From 11aadbadfbff8c8235494c466f07e66d62a8e2ef Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 12 Jan 2010 23:03:56 +0100 Subject: [PATCH 024/178] fix compilation on coreboot --- conf/i386-coreboot.rmk | 8 ++++---- conf/i386-efi.rmk | 4 ++-- conf/i386-ieee1275.rmk | 6 +++--- conf/i386-pc.rmk | 2 +- conf/powerpc-ieee1275.rmk | 4 ++-- conf/sparc64-ieee1275.rmk | 4 ++-- conf/x86_64-efi.rmk | 2 +- include/grub/i386/coreboot/loader.h | 1 - include/grub/i386/qemu/loader.h | 1 - kern/i386/coreboot/init.c | 18 +----------------- loader/i386/linux.c | 1 - loader/i386/multiboot.c | 1 - 12 files changed, 16 insertions(+), 36 deletions(-) delete mode 100644 include/grub/i386/coreboot/loader.h delete mode 100644 include/grub/i386/qemu/loader.h diff --git a/conf/i386-coreboot.rmk b/conf/i386-coreboot.rmk index 8676aaea3..6b3d32847 100644 --- a/conf/i386-coreboot.rmk +++ b/conf/i386-coreboot.rmk @@ -32,10 +32,10 @@ kernel_img_SOURCES = kern/i386/coreboot/startup.S \ term/i386/pc/vga_text.c term/i386/vga_common.c \ symlist.c kernel_img_HEADERS = boot.h cache.h device.h disk.h dl.h elf.h elfload.h \ - env.h err.h file.h fs.h kernel.h loader.h misc.h mm.h net.h parser.h \ + env.h err.h file.h fs.h kernel.h misc.h mm.h net.h parser.h \ partition.h msdos_partition.h reader.h symbol.h term.h time.h types.h \ machine/boot.h machine/console.h machine/init.h \ - machine/memory.h machine/loader.h list.h handler.h command.h i18n.h + machine/memory.h list.h handler.h command.h i18n.h mm_private.h kernel_img_CFLAGS = $(COMMON_CFLAGS) kernel_img_ASFLAGS = $(COMMON_ASFLAGS) kernel_img_LDFLAGS = $(COMMON_LDFLAGS) -Wl,-N,-S,-Ttext,$(GRUB_KERNEL_MACHINE_LINK_ADDR),-Bstatic @@ -76,10 +76,10 @@ kernel_img_SOURCES = kern/i386/qemu/startup.S \ term/i386/pc/vga_text.c term/i386/vga_common.c \ symlist.c kernel_img_HEADERS = boot.h cache.h device.h disk.h dl.h elf.h elfload.h \ - env.h err.h file.h fs.h kernel.h loader.h misc.h mm.h net.h parser.h \ + env.h err.h file.h fs.h kernel.h misc.h mm.h net.h parser.h \ partition.h msdos_partition.h reader.h symbol.h term.h time.h types.h \ machine/boot.h machine/console.h machine/init.h \ - machine/memory.h machine/loader.h list.h handler.h command.h i18n.h + machine/memory.h list.h handler.h command.h i18n.h mm_private.h kernel_img_CFLAGS = $(COMMON_CFLAGS) -DGRUB_BOOT_MACHINE_LINK_ADDR=$(GRUB_BOOT_MACHINE_LINK_ADDR) kernel_img_ASFLAGS = $(COMMON_ASFLAGS) -DGRUB_KERNEL_MACHINE_LINK_ADDR=$(GRUB_KERNEL_MACHINE_LINK_ADDR) kernel_img_LDFLAGS = $(COMMON_LDFLAGS) $(TARGET_IMG_LDFLAGS)$(GRUB_KERNEL_MACHINE_LINK_ADDR) diff --git a/conf/i386-efi.rmk b/conf/i386-efi.rmk index f3281a1bc..673349858 100644 --- a/conf/i386-efi.rmk +++ b/conf/i386-efi.rmk @@ -49,9 +49,9 @@ kernel_img_SOURCES = kern/i386/efi/startup.S kern/main.c kern/device.c \ kern/generic/rtc_get_time_ms.c \ kern/generic/millisleep.c kernel_img_HEADERS = boot.h cache.h device.h disk.h dl.h elf.h elfload.h \ - env.h err.h file.h fs.h kernel.h loader.h misc.h mm.h net.h parser.h \ + env.h err.h file.h fs.h kernel.h misc.h mm.h net.h parser.h \ partition.h msdos_partition.h reader.h symbol.h term.h time.h types.h \ - efi/efi.h efi/time.h efi/disk.h i386/pit.h list.h handler.h command.h i18n.h + efi/efi.h efi/time.h efi/disk.h i386/pit.h list.h handler.h command.h i18n.h mm_private.h kernel_img_CFLAGS = $(COMMON_CFLAGS) kernel_img_ASFLAGS = $(COMMON_ASFLAGS) kernel_img_LDFLAGS = $(COMMON_LDFLAGS) diff --git a/conf/i386-ieee1275.rmk b/conf/i386-ieee1275.rmk index 5c3a5aaf6..b9e192796 100644 --- a/conf/i386-ieee1275.rmk +++ b/conf/i386-ieee1275.rmk @@ -30,10 +30,10 @@ kernel_img_SOURCES = kern/i386/ieee1275/startup.S \ disk/ieee1275/ofdisk.c \ symlist.c kernel_img_HEADERS = cache.h device.h disk.h dl.h elf.h elfload.h \ - env.h err.h file.h fs.h kernel.h loader.h misc.h mm.h net.h parser.h \ + env.h err.h file.h fs.h kernel.h misc.h mm.h net.h parser.h \ partition.h msdos_partition.h reader.h symbol.h term.h time.h types.h \ - ieee1275/ieee1275.h machine/kernel.h machine/loader.h machine/memory.h \ - list.h handler.h command.h i18n.h + ieee1275/ieee1275.h machine/kernel.h machine/memory.h \ + list.h handler.h command.h i18n.h mm_private.h kernel_img_CFLAGS = $(COMMON_CFLAGS) kernel_img_ASFLAGS = $(COMMON_ASFLAGS) kernel_img_LDFLAGS = $(COMMON_LDFLAGS) -Wl,-N,-S,-Ttext,0x10000,-Bstatic diff --git a/conf/i386-pc.rmk b/conf/i386-pc.rmk index 36ca76950..6f7b4f26c 100644 --- a/conf/i386-pc.rmk +++ b/conf/i386-pc.rmk @@ -60,7 +60,7 @@ kernel_img_SOURCES = kern/i386/pc/startup.S \ term/i386/pc/console.c term/i386/vga_common.c \ symlist.c kernel_img_HEADERS = boot.h cache.h device.h disk.h dl.h elf.h elfload.h \ - env.h err.h file.h fs.h kernel.h loader.h misc.h mm.h net.h parser.h \ + env.h err.h file.h fs.h kernel.h misc.h mm.h net.h parser.h \ partition.h msdos_partition.h reader.h symbol.h term.h time.h types.h \ machine/biosdisk.h machine/boot.h machine/console.h machine/init.h \ machine/memory.h machine/loader.h machine/vga.h machine/vbe.h \ diff --git a/conf/powerpc-ieee1275.rmk b/conf/powerpc-ieee1275.rmk index 854ad50b7..fd6230771 100644 --- a/conf/powerpc-ieee1275.rmk +++ b/conf/powerpc-ieee1275.rmk @@ -15,9 +15,9 @@ DEFSYMFILES += kernel_syms.lst kernel_img_HEADERS = boot.h cache.h device.h disk.h dl.h elf.h elfload.h \ env.h err.h file.h fs.h kernel.h misc.h mm.h net.h parser.h reader.h \ - symbol.h term.h time.h types.h powerpc/libgcc.h loader.h partition.h \ + symbol.h term.h time.h types.h powerpc/libgcc.h partition.h \ msdos_partition.h ieee1275/ieee1275.h machine/kernel.h handler.h list.h \ - command.h i18n.h + command.h i18n.h mm_private.h symlist.c: $(addprefix include/grub/,$(kernel_img_HEADERS)) config.h gensymlist.sh /bin/sh gensymlist.sh $(filter %.h,$^) > $@ || (rm -f $@; exit 1) diff --git a/conf/sparc64-ieee1275.rmk b/conf/sparc64-ieee1275.rmk index 4ba098619..a056ddc67 100644 --- a/conf/sparc64-ieee1275.rmk +++ b/conf/sparc64-ieee1275.rmk @@ -27,11 +27,11 @@ MOSTLYCLEANFILES += symlist.c kernel_syms.lst DEFSYMFILES += kernel_syms.lst kernel_img_HEADERS = boot.h cache.h device.h disk.h dl.h elf.h elfload.h \ - env.h err.h file.h fs.h kernel.h loader.h misc.h mm.h net.h parser.h \ + env.h err.h file.h fs.h kernel.h misc.h mm.h net.h parser.h \ partition.h msdos_partition.h reader.h symbol.h term.h time.h types.h \ list.h handler.h command.h i18n.h \ sparc64/libgcc.h ieee1275/ieee1275.h machine/kernel.h \ - sparc64/ieee1275/ieee1275.h + sparc64/ieee1275/ieee1275.h mm_private.h kernel_img_SOURCES = kern/sparc64/ieee1275/crt0.S kern/ieee1275/cmain.c \ kern/ieee1275/ieee1275.c kern/main.c kern/device.c \ kern/disk.c kern/dl.c kern/err.c kern/file.c kern/fs.c \ diff --git a/conf/x86_64-efi.rmk b/conf/x86_64-efi.rmk index 4f6ace057..19220610d 100644 --- a/conf/x86_64-efi.rmk +++ b/conf/x86_64-efi.rmk @@ -51,7 +51,7 @@ kernel_img_HEADERS = boot.h cache.h device.h disk.h dl.h elf.h elfload.h \ env.h err.h file.h fs.h kernel.h loader.h misc.h mm.h net.h parser.h \ partition.h msdos_partition.h reader.h symbol.h term.h time.h types.h \ efi/efi.h efi/time.h efi/disk.h machine/loader.h i386/pit.h list.h \ - handler.h command.h i18n.h + handler.h command.h i18n.h mm_private.h kernel_img_CFLAGS = $(COMMON_CFLAGS) kernel_img_ASFLAGS = $(COMMON_ASFLAGS) kernel_img_LDFLAGS = $(COMMON_LDFLAGS) diff --git a/include/grub/i386/coreboot/loader.h b/include/grub/i386/coreboot/loader.h deleted file mode 100644 index d3f36bba5..000000000 --- a/include/grub/i386/coreboot/loader.h +++ /dev/null @@ -1 +0,0 @@ -#include diff --git a/include/grub/i386/qemu/loader.h b/include/grub/i386/qemu/loader.h deleted file mode 100644 index d3f36bba5..000000000 --- a/include/grub/i386/qemu/loader.h +++ /dev/null @@ -1 +0,0 @@ -#include diff --git a/kern/i386/coreboot/init.c b/kern/i386/coreboot/init.c index 550a2a60a..7d8b1270b 100644 --- a/kern/i386/coreboot/init.c +++ b/kern/i386/coreboot/init.c @@ -41,9 +41,6 @@ extern char _start[]; extern char _end[]; -grub_addr_t grub_os_area_addr; -grub_size_t grub_os_area_size; - grub_uint32_t grub_get_rtc (void) { @@ -105,20 +102,7 @@ grub_machine_init (void) } } - if (addr == GRUB_MEMORY_MACHINE_UPPER_START - || (addr >= GRUB_MEMORY_MACHINE_LOWER_SIZE - && addr <= GRUB_MEMORY_MACHINE_UPPER_START - && (addr + size > GRUB_MEMORY_MACHINE_UPPER_START))) - { - grub_size_t quarter = size >> 2; - - grub_os_area_addr = addr; - grub_os_area_size = size - quarter; - grub_mm_init_region ((void *) (grub_os_area_addr + grub_os_area_size), - quarter); - } - else - grub_mm_init_region ((void *) (grub_addr_t) addr, (grub_size_t) size); + grub_mm_init_region ((void *) (grub_addr_t) addr, (grub_size_t) size); return 0; } diff --git a/loader/i386/linux.c b/loader/i386/linux.c index b6298d0bb..ec6a5bb3b 100644 --- a/loader/i386/linux.c +++ b/loader/i386/linux.c @@ -18,7 +18,6 @@ #include #include -#include #include #include #include diff --git a/loader/i386/multiboot.c b/loader/i386/multiboot.c index 2f9cc73c9..be11fe20b 100644 --- a/loader/i386/multiboot.c +++ b/loader/i386/multiboot.c @@ -31,7 +31,6 @@ #define UNSUPPORTED_FLAGS 0x0000fff0 #include -#include #include #include #include From 73f6ce4ab24fe5b7e1965402e62b8811014a4588 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 12 Jan 2010 23:30:52 +0100 Subject: [PATCH 025/178] x86_64 support for relocator --- conf/i386.rmk | 2 +- conf/x86_64-efi.rmk | 4 +++- lib/i386/relocator.c | 28 +++++++++++++++++++++------- lib/i386/relocator16.S | 4 ++-- lib/i386/relocator32.S | 2 +- lib/i386/relocator64.S | 4 ++-- lib/relocator.c | 16 ++++++++-------- 7 files changed, 38 insertions(+), 22 deletions(-) diff --git a/conf/i386.rmk b/conf/i386.rmk index 2efd9895a..d7417f444 100644 --- a/conf/i386.rmk +++ b/conf/i386.rmk @@ -18,7 +18,7 @@ vga_text_mod_LDFLAGS = $(COMMON_LDFLAGS) pkglib_MODULES += relocator.mod relocator_mod_SOURCES = lib/relocator.c lib/i386/relocator32.S \ lib/i386/relocator64.S lib/i386/relocator16.S \ - lib/i386/relocator_asm.S lib/i386/relocator.c + lib/$(target_cpu)/relocator_asm.S lib/i386/relocator.c relocator_mod_CFLAGS = $(COMMON_CFLAGS) relocator_mod_ASFLAGS = $(COMMON_ASFLAGS) relocator_mod_LDFLAGS = $(COMMON_LDFLAGS) diff --git a/conf/x86_64-efi.rmk b/conf/x86_64-efi.rmk index 19220610d..36d6579de 100644 --- a/conf/x86_64-efi.rmk +++ b/conf/x86_64-efi.rmk @@ -167,7 +167,9 @@ xnu_mod_LDFLAGS = $(COMMON_LDFLAGS) xnu_mod_ASFLAGS = $(COMMON_ASFLAGS) pkglib_MODULES += relocator.mod -relocator_mod_SOURCES = lib/i386/relocator.c lib/i386/relocator_asm.S lib/i386/relocator_backward.S +relocator_mod_SOURCES = lib/relocator.c lib/i386/relocator32.S \ + lib/i386/relocator64.S lib/i386/relocator16.S \ + lib/$(target_cpu)/relocator_asm.S lib/i386/relocator.c relocator_mod_CFLAGS = $(COMMON_CFLAGS) relocator_mod_ASFLAGS = $(COMMON_ASFLAGS) relocator_mod_LDFLAGS = $(COMMON_LDFLAGS) diff --git a/lib/i386/relocator.c b/lib/i386/relocator.c index 5757bb6df..d040c80ab 100644 --- a/lib/i386/relocator.c +++ b/lib/i386/relocator.c @@ -77,7 +77,11 @@ extern grub_addr_t grub_relocator64_cr3; grub_size_t grub_relocator_align = 1; grub_size_t grub_relocator_forward_size; grub_size_t grub_relocator_backward_size; -grub_size_t grub_relocator_jumper_size = 10; +#ifdef __x86_64__ +grub_size_t grub_relocator_jumper_size = 12; +#else +grub_size_t grub_relocator_jumper_size = 7; +#endif void grub_cpu_relocator_init (void) @@ -91,16 +95,26 @@ grub_cpu_relocator_jumper (void *rels, grub_addr_t addr) { grub_uint8_t *ptr; ptr = rels; - /* movl $addr, %eax (for relocator) */ +#ifdef __x86_64__ + /* movq imm64, %rax (for relocator) */ + *(grub_uint8_t *) ptr = 0x48; + ptr++; + *(grub_uint8_t *) ptr = 0xb8; + ptr++; + *(grub_uint64_t *) ptr = addr; + ptr += sizeof (grub_uint64_t); +#else + /* movl imm32, %eax (for relocator) */ *(grub_uint8_t *) ptr = 0xb8; ptr++; *(grub_uint32_t *) ptr = addr; - ptr += 4; - /* jmp $addr */ - *(grub_uint8_t *) ptr = 0xe9; + ptr += sizeof (grub_uint32_t); +#endif + /* jmp $eax/$rax */ + *(grub_uint8_t *) ptr = 0xff; + ptr++; + *(grub_uint8_t *) ptr = 0xe0; ptr++; - *(grub_uint32_t *) ptr = addr - (grub_uint32_t) (ptr + 4); - ptr += 4; } void diff --git a/lib/i386/relocator16.S b/lib/i386/relocator16.S index d35adecd8..7d65e4dbe 100644 --- a/lib/i386/relocator16.S +++ b/lib/i386/relocator16.S @@ -21,7 +21,7 @@ #ifdef __x86_64__ #define RAX %rax -#define RSI %rdi +#define RSI %rsi #else #define RAX %eax #define RSI %esi @@ -93,7 +93,7 @@ LOCAL(cont1): movl %esi, %eax shrl $4, %eax - movw %ax, (LOCAL (segment) - LOCAL (base)) (RSI, 1) + movw %ax, (LOCAL (segment) - LOCAL (base)) (%esi, 1) /* jump to a 16 bit segment */ ljmp $PSEUDO_REAL_CSEG, $(LOCAL (cont2) - LOCAL(base)) diff --git a/lib/i386/relocator32.S b/lib/i386/relocator32.S index 4e0553c03..4f79151e2 100644 --- a/lib/i386/relocator32.S +++ b/lib/i386/relocator32.S @@ -21,7 +21,7 @@ #ifdef __x86_64__ #define RAX %rax -#define RSI %rdi +#define RSI %rsi #else #define RAX %eax #define RSI %esi diff --git a/lib/i386/relocator64.S b/lib/i386/relocator64.S index 42f61e32e..05627fb90 100644 --- a/lib/i386/relocator64.S +++ b/lib/i386/relocator64.S @@ -21,7 +21,7 @@ #ifdef __x86_64__ #define RAX %rax -#define RSI %rdi +#define RSI %rsi #else #define RAX %eax #define RSI %esi @@ -82,7 +82,7 @@ VARIABLE(grub_relocator64_cr3) .byte 0xb8 VARIABLE(grub_relocator64_cr3) .quad 0 - movl %rax, %cr3 + movq %rax, %cr3 #endif /* Load GDT. */ lgdt (LOCAL(gdtdesc) - LOCAL(base)) (RSI, 1) diff --git a/lib/relocator.c b/lib/relocator.c index a5b3c6daf..84ead99b1 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -40,7 +40,7 @@ grub_relocator_new (void) ret->postchunks = ~(grub_addr_t) 0; ret->relocators_size = grub_relocator_jumper_size; - grub_dprintf ("relocator", "relocators_size=%d\n", ret->relocators_size); + grub_dprintf ("relocator", "relocators_size=%ld\n", ret->relocators_size); return ret; } @@ -103,7 +103,7 @@ get_best_header (struct grub_relocator *rel, hb = h; hbp = hp; *best_addr = addr; - grub_dprintf ("relocator", "picked %p/%x\n", hb, addr); + grub_dprintf ("relocator", "picked %p/%lx\n", hb, addr); } } else @@ -146,7 +146,7 @@ get_best_header (struct grub_relocator *rel, hb = h; hbp = hp; *best_addr = addr; - grub_dprintf ("relocator", "picked %p/%x\n", hb, addr); + grub_dprintf ("relocator", "picked %p/%lx\n", hb, addr); } } } @@ -221,7 +221,7 @@ malloc_in_range (struct grub_relocator *rel, hb = get_best_header (rel, start, end, align, size, rb, &hbp, &best_addr, from_low_priv, collisioncheck); - grub_dprintf ("relocator", "best header %p/%x\n", hb, best_addr); + grub_dprintf ("relocator", "best header %p/%lx\n", hb, best_addr); if (!hb) { @@ -421,14 +421,14 @@ grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, void **src, rel->highestnonpostaddr = start + size; } - grub_dprintf ("relocator", "relocators_size=%d\n", rel->relocators_size); + grub_dprintf ("relocator", "relocators_size=%ld\n", rel->relocators_size); if (start < target) rel->relocators_size += grub_relocator_backward_size; if (start > target) rel->relocators_size += grub_relocator_forward_size; - grub_dprintf ("relocator", "relocators_size=%d\n", rel->relocators_size); + grub_dprintf ("relocator", "relocators_size=%ld\n", rel->relocators_size); chunk->src = start; chunk->target = target; @@ -485,7 +485,7 @@ grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, } adjust_limits (rel, &min_addr2, &max_addr2, min_addr, max_addr); - grub_dprintf ("relocator", "Adjusted limits from %x-%x to %x-%x\n", + grub_dprintf ("relocator", "Adjusted limits from %lx-%lx to %lx-%lx\n", min_addr, max_addr, min_addr2, max_addr2); do @@ -572,7 +572,7 @@ grub_relocator_prepare_relocs (struct grub_relocator *rel, grub_addr_t addr, grub_addr_t rels; grub_addr_t rels0; - grub_dprintf ("relocator", "Preparing relocs (size=%d)\n", + grub_dprintf ("relocator", "Preparing relocs (size=%ld)\n", rel->relocators_size); if (!malloc_in_range (rel, 0, ~(grub_addr_t)0 - rel->relocators_size + 1, From 37480ee490b0f52d160f0f79fecbbbd0f07e6afd Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 13 Jan 2010 13:41:15 +0100 Subject: [PATCH 026/178] Added needed casts --- lib/relocator.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/relocator.c b/lib/relocator.c index 84ead99b1..889497334 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -40,7 +40,8 @@ grub_relocator_new (void) ret->postchunks = ~(grub_addr_t) 0; ret->relocators_size = grub_relocator_jumper_size; - grub_dprintf ("relocator", "relocators_size=%ld\n", ret->relocators_size); + grub_dprintf ("relocator", "relocators_size=%lu\n", + (unsigned long) ret->relocators_size); return ret; } @@ -103,7 +104,8 @@ get_best_header (struct grub_relocator *rel, hb = h; hbp = hp; *best_addr = addr; - grub_dprintf ("relocator", "picked %p/%lx\n", hb, addr); + grub_dprintf ("relocator", "picked %p/%lx\n", hb, + (unsigned long) addr); } } else @@ -146,7 +148,8 @@ get_best_header (struct grub_relocator *rel, hb = h; hbp = hp; *best_addr = addr; - grub_dprintf ("relocator", "picked %p/%lx\n", hb, addr); + grub_dprintf ("relocator", "picked %p/%lx\n", hb, + (unsigned long) addr); } } } @@ -221,7 +224,8 @@ malloc_in_range (struct grub_relocator *rel, hb = get_best_header (rel, start, end, align, size, rb, &hbp, &best_addr, from_low_priv, collisioncheck); - grub_dprintf ("relocator", "best header %p/%lx\n", hb, best_addr); + grub_dprintf ("relocator", "best header %p/%lx\n", hb, + (unsigned long) best_addr); if (!hb) { @@ -421,14 +425,16 @@ grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, void **src, rel->highestnonpostaddr = start + size; } - grub_dprintf ("relocator", "relocators_size=%ld\n", rel->relocators_size); + grub_dprintf ("relocator", "relocators_size=%ld\n", + (unsigned long) rel->relocators_size); if (start < target) rel->relocators_size += grub_relocator_backward_size; if (start > target) rel->relocators_size += grub_relocator_forward_size; - grub_dprintf ("relocator", "relocators_size=%ld\n", rel->relocators_size); + grub_dprintf ("relocator", "relocators_size=%ld\n", + (unsigned long) rel->relocators_size); chunk->src = start; chunk->target = target; @@ -486,7 +492,8 @@ grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, adjust_limits (rel, &min_addr2, &max_addr2, min_addr, max_addr); grub_dprintf ("relocator", "Adjusted limits from %lx-%lx to %lx-%lx\n", - min_addr, max_addr, min_addr2, max_addr2); + (unsigned long) min_addr, (unsigned long) max_addr, + (unsigned long) min_addr2, (unsigned long) max_addr2); do { @@ -573,7 +580,7 @@ grub_relocator_prepare_relocs (struct grub_relocator *rel, grub_addr_t addr, grub_addr_t rels0; grub_dprintf ("relocator", "Preparing relocs (size=%ld)\n", - rel->relocators_size); + (unsigned long) rel->relocators_size); if (!malloc_in_range (rel, 0, ~(grub_addr_t)0 - rel->relocators_size + 1, grub_relocator_align, From 19eb83d4860cb688e357e47eaebfde475da6b8c9 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 13 Jan 2010 14:02:32 +0100 Subject: [PATCH 027/178] Include i386.rmk into x86_64-efi.rmk --- conf/x86_64-efi.rmk | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/conf/x86_64-efi.rmk b/conf/x86_64-efi.rmk index 36d6579de..235cf2b3d 100644 --- a/conf/x86_64-efi.rmk +++ b/conf/x86_64-efi.rmk @@ -77,11 +77,6 @@ acpi_mod_SOURCES = commands/acpi.c commands/efi/acpi.c acpi_mod_CFLAGS = $(COMMON_CFLAGS) acpi_mod_LDFLAGS = $(COMMON_LDFLAGS) -# For ata.mod -ata_mod_SOURCES = disk/ata.c -ata_mod_CFLAGS = $(COMMON_CFLAGS) -ata_mod_LDFLAGS = $(COMMON_LDFLAGS) - # For mmap.mod. mmap_mod_SOURCES = mmap/mmap.c mmap/i386/uppermem.c mmap/i386/mmap.c \ mmap/efi/mmap.c @@ -99,7 +94,7 @@ appleldr_mod_CFLAGS = $(COMMON_CFLAGS) appleldr_mod_LDFLAGS = $(COMMON_LDFLAGS) # For linux.mod. -linux_mod_SOURCES = loader/i386/efi/linux.c loader/i386/linux_trampoline.S +linux_mod_SOURCES = loader/i386/linux.c linux_mod_CFLAGS = $(COMMON_CFLAGS) linux_mod_ASFLAGS = $(COMMON_ASFLAGS) linux_mod_LDFLAGS = $(COMMON_LDFLAGS) @@ -166,12 +161,5 @@ xnu_mod_CFLAGS = $(COMMON_CFLAGS) xnu_mod_LDFLAGS = $(COMMON_LDFLAGS) xnu_mod_ASFLAGS = $(COMMON_ASFLAGS) -pkglib_MODULES += relocator.mod -relocator_mod_SOURCES = lib/relocator.c lib/i386/relocator32.S \ - lib/i386/relocator64.S lib/i386/relocator16.S \ - lib/$(target_cpu)/relocator_asm.S lib/i386/relocator.c -relocator_mod_CFLAGS = $(COMMON_CFLAGS) -relocator_mod_ASFLAGS = $(COMMON_ASFLAGS) -relocator_mod_LDFLAGS = $(COMMON_LDFLAGS) - +include $(srcdir)/conf/i386.mk include $(srcdir)/conf/common.mk From ca732b36c1e0dcc4d7eee64628374ecfe5f925d3 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 13 Jan 2010 14:03:18 +0100 Subject: [PATCH 028/178] Video driver ID. --- include/grub/video.h | 10 ++++++++++ video/efi_gop.c | 1 + video/efi_uga.c | 1 + video/i386/pc/vbe.c | 1 + video/video.c | 8 ++++++++ 5 files changed, 21 insertions(+) diff --git a/include/grub/video.h b/include/grub/video.h index 4145db465..985fa9208 100644 --- a/include/grub/video.h +++ b/include/grub/video.h @@ -159,10 +159,19 @@ struct grub_video_palette_data grub_uint8_t a; /* Reserved bits value (0-255). */ }; +typedef enum grub_video_driver_id + { + GRUB_VIDEO_DRIVER_NONE, + GRUB_VIDEO_DRIVER_VBE, + GRUB_VIDEO_DRIVER_EFI_UGA, + GRUB_VIDEO_DRIVER_EFI_GOP + } grub_video_driver_id_t; + struct grub_video_adapter { /* The video adapter name. */ const char *name; + grub_video_driver_id_t id; /* Initialize the video adapter. */ grub_err_t (*init) (void); @@ -309,5 +318,6 @@ grub_err_t grub_video_get_active_render_target (struct grub_video_render_target grub_err_t grub_video_set_mode (const char *modestring, int NESTED_FUNC_ATTR (*hook) (grub_video_adapter_t p, struct grub_video_mode_info *mode_info)); +grub_video_driver_id_t grub_video_get_driver_id (void); #endif /* ! GRUB_VIDEO_HEADER */ diff --git a/video/efi_gop.c b/video/efi_gop.c index 30863c1ed..13ef0ddae 100644 --- a/video/efi_gop.c +++ b/video/efi_gop.c @@ -353,6 +353,7 @@ grub_video_gop_get_info_and_fini (struct grub_video_mode_info *mode_info, static struct grub_video_adapter grub_video_gop_adapter = { .name = "EFI GOP driver", + .id = GRUB_VIDEO_DRIVER_EFI_GOP, .init = grub_video_gop_init, .fini = grub_video_gop_fini, diff --git a/video/efi_uga.c b/video/efi_uga.c index 12ca35cde..7ef7594cc 100644 --- a/video/efi_uga.c +++ b/video/efi_uga.c @@ -300,6 +300,7 @@ grub_video_uga_get_info_and_fini (struct grub_video_mode_info *mode_info, static struct grub_video_adapter grub_video_uga_adapter = { .name = "EFI UGA driver", + .id = GRUB_VIDEO_DRIVER_EFI_UGA, .init = grub_video_uga_init, .fini = grub_video_uga_fini, diff --git a/video/i386/pc/vbe.c b/video/i386/pc/vbe.c index 17d9b3282..918bab0b0 100644 --- a/video/i386/pc/vbe.c +++ b/video/i386/pc/vbe.c @@ -557,6 +557,7 @@ grub_video_vbe_get_info_and_fini (struct grub_video_mode_info *mode_info, static struct grub_video_adapter grub_video_vbe_adapter = { .name = "VESA BIOS Extension Video Driver", + .id = GRUB_VIDEO_DRIVER_VBE, .init = grub_video_vbe_init, .fini = grub_video_vbe_fini, diff --git a/video/video.c b/video/video.c index 682bebcbf..e678c2982 100644 --- a/video/video.c +++ b/video/video.c @@ -93,6 +93,14 @@ grub_video_get_info (struct grub_video_mode_info *mode_info) return grub_video_adapter_active->get_info (mode_info); } +grub_video_driver_id_t +grub_video_get_driver_id (void) +{ + if (! grub_video_adapter_active) + return GRUB_VIDEO_DRIVER_NONE; + return grub_video_adapter_active->id; +} + /* Get information about active video mode. */ grub_err_t grub_video_get_info_and_fini (struct grub_video_mode_info *mode_info, From c8142599fc5d26cbd22a5dae5cc23738ee65c2e5 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 13 Jan 2010 14:03:54 +0100 Subject: [PATCH 029/178] Merge i386/efi/linux.c into i386/linux.c --- include/grub/i386/linux.h | 6 +- loader/i386/linux.c | 184 ++++++++++++++++++++++++++++++++------ 2 files changed, 162 insertions(+), 28 deletions(-) diff --git a/include/grub/i386/linux.h b/include/grub/i386/linux.h index ecda4ed94..f2c93e419 100644 --- a/include/grub/i386/linux.h +++ b/include/grub/i386/linux.h @@ -79,9 +79,9 @@ struct grub_e820_mmap grub_uint32_t type; } __attribute__((packed)); -#define GRUB_VIDEO_TYPE_TEXT 0x01 -#define GRUB_VIDEO_TYPE_VLFB 0x23 /* VESA VGA in graphic mode */ -#define GRUB_VIDEO_TYPE_EFI 0x70 +#define GRUB_VIDEO_LINUX_TYPE_EGA_TEXT 0x01 +#define GRUB_VIDEO_LINUX_TYPE_VLFB 0x23 /* VESA VGA in graphic mode */ +#define GRUB_VIDEO_LINUX_TYPE_SIMPLE_LFB 0x70 /* Linear framebuffer without any additional functions. */ /* For the Linux/i386 boot protocol version 2.03. */ struct linux_kernel_header diff --git a/loader/i386/linux.c b/loader/i386/linux.c index ec6a5bb3b..691d0912d 100644 --- a/loader/i386/linux.c +++ b/loader/i386/linux.c @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -31,11 +32,20 @@ #include #include #include -#include -#include #include #include +#ifdef GRUB_MACHINE_EFI +#include +#define HAS_VGA_TEXT 0 +#define DEFAULT_VIDEO_MODE "800x600" +#else +#include +#include +#define HAS_VGA_TEXT 1 +#define DEFAULT_VIDEO_MODE "text" +#endif + #define GRUB_LINUX_CL_OFFSET 0x1000 #define GRUB_LINUX_CL_END_OFFSET 0x2000 @@ -53,6 +63,8 @@ static grub_uint32_t real_mode_pages; static grub_uint32_t prot_mode_pages; static grub_uint32_t initrd_pages; static struct grub_relocator *relocator = NULL; +static void *efi_mmap_buf; +static grub_size_t efi_mmap_size; /* FIXME */ #if 0 @@ -244,6 +256,48 @@ page_align (grub_size_t size) return (size + (1 << 12) - 1) & (~((1 << 12) - 1)); } +#ifdef GRUB_MACHINE_EFI +/* Find the optimal number of pages for the memory map. Is it better to + move this code to efi/mm.c? */ +static grub_efi_uintn_t +find_efi_mmap_size (void) +{ + static grub_efi_uintn_t mmap_size = 0; + + if (mmap_size != 0) + return mmap_size; + + mmap_size = (1 << 12); + while (1) + { + int ret; + grub_efi_memory_descriptor_t *mmap; + grub_efi_uintn_t desc_size; + + mmap = grub_malloc (mmap_size); + if (! mmap) + return 0; + + ret = grub_efi_get_memory_map (&mmap_size, mmap, 0, &desc_size, 0); + grub_free (mmap); + + if (ret < 0) + grub_fatal ("cannot get memory map"); + else if (ret > 0) + break; + + mmap_size += (1 << 12); + } + + /* Increase the size a bit for safety, because GRUB allocates more on + later, and EFI itself may allocate more. */ + mmap_size += (1 << 12); + + return page_align (mmap_size); +} + +#endif + /* Find the optimal number of pages for the memory map. */ static grub_size_t find_mmap_size (void) @@ -292,12 +346,18 @@ allocate_pages (grub_size_t prot_size) prot_size = page_align (prot_size); mmap_size = find_mmap_size (); +#ifdef GRUB_MACHINE_EFI + efi_mmap_size = find_efi_mmap_size (); +#else + efi_mmap_size = 0; +#endif + grub_dprintf ("linux", "real_size = %x, prot_size = %x, mmap_size = %x\n", (unsigned) real_size, (unsigned) prot_size, (unsigned) mmap_size); /* Calculate the number of pages; Combine the real mode code with the memory map buffer for simplicity. */ - real_mode_pages = ((real_size + mmap_size) >> 12); + real_mode_pages = ((real_size + mmap_size + efi_mmap_size) >> 12); prot_mode_pages = (prot_size >> 12); /* Initialize the memory pointers with NULL for convenience. */ @@ -330,10 +390,10 @@ allocate_pages (grub_size_t prot_size) if (addr + size > 0x90000) size = 0x90000 - addr; - if (real_size + mmap_size > size) + if (real_size + mmap_size + efi_mmap_size > size) return 0; - real_mode_target = ((addr + size) - (real_size + mmap_size)); + real_mode_target = ((addr + size) - (real_size + mmap_size + efi_mmap_size)); return 1; } @@ -348,11 +408,13 @@ allocate_pages (grub_size_t prot_size) err = grub_relocator_alloc_chunk_addr (relocator, &real_mode_mem, real_mode_target, - (real_size + mmap_size)); + (real_size + mmap_size + + efi_mmap_size)); if (err) goto fail; + efi_mmap_buf = (grub_uint8_t *) real_mode_mem + real_size + mmap_size; - prot_mode_target = 0x100000; + prot_mode_target = GRUB_LINUX_BZIMAGE_ADDR; err = grub_relocator_alloc_chunk_addr (relocator, &prot_mode_mem, prot_mode_target, prot_size); @@ -393,17 +455,28 @@ grub_e820_add_region (struct grub_e820_mmap *e820_map, int *e820_num, } } -static int +static grub_err_t grub_linux_setup_video (struct linux_kernel_params *params) { struct grub_video_mode_info mode_info; void *framebuffer; - int ret; + grub_err_t err; - ret = grub_video_get_info_and_fini (&mode_info, &framebuffer); + switch (grub_video_get_driver_id ()) + { + case GRUB_VIDEO_DRIVER_VBE: + params->have_vga = GRUB_VIDEO_LINUX_TYPE_VLFB; + break; - if (ret) - return 1; + default: + params->have_vga = GRUB_VIDEO_LINUX_TYPE_SIMPLE_LFB; + break; + } + + err = grub_video_get_info_and_fini (&mode_info, &framebuffer); + + if (err) + return err; params->lfb_width = mode_info.width; params->lfb_height = mode_info.height; @@ -449,7 +522,7 @@ grub_linux_setup_video (struct linux_kernel_params *params) } #endif - return 0; + return GRUB_ERR_NONE; } #ifdef __x86_64__ @@ -520,15 +593,15 @@ grub_linux_boot (void) if (modevar && *modevar != 0) { tmp = grub_malloc (grub_strlen (modevar) - + sizeof (";text")); + + sizeof (";" DEFAULT_VIDEO_MODE)); if (! tmp) return grub_errno; - grub_sprintf (tmp, "%s;text", modevar); + grub_sprintf (tmp, "%s;" DEFAULT_VIDEO_MODE, modevar); err = grub_video_set_mode (tmp, 0); grub_free (tmp); } else - err = grub_video_set_mode ("text", 0); + err = grub_video_set_mode (DEFAULT_VIDEO_MODE, 0); if (err) { @@ -537,17 +610,26 @@ grub_linux_boot (void) grub_errno = GRUB_ERR_NONE; } - if (! grub_linux_setup_video (params)) - params->have_vga = GRUB_VIDEO_TYPE_VLFB; - else + err = grub_linux_setup_video (params); + if (err) { - params->have_vga = GRUB_VIDEO_TYPE_TEXT; + grub_print_error (); + grub_errno = GRUB_ERR_NONE; +#if HAS_VGA_TEXT + params->have_vga = GRUB_VIDEO_LINUX_TYPE_EGA_TEXT; + params->video_mode = 0x3; params->video_width = 80; params->video_height = 25; +#else + params->have_vga = 0; + params->video_mode = 0; + params->video_width = 0; + params->video_height = 0; +#endif } /* Initialize these last, because terminal position could be affected by printfs above. */ - if (params->have_vga == GRUB_VIDEO_TYPE_TEXT) + if (params->have_vga == GRUB_VIDEO_LINUX_TYPE_EGA_TEXT) { grub_term_output_t term; int found = 0; @@ -568,6 +650,40 @@ grub_linux_boot (void) } } +#ifdef GRUB_MACHINE_EFI + { + grub_efi_uintn_t efi_map_key, efi_desc_size; + grub_efi_uint32_t efi_desc_version; + if (grub_efi_get_memory_map (&efi_mmap_size, efi_mmap_buf, &efi_map_key, + &efi_desc_size, &efi_desc_version) <= 0) + grub_fatal ("cannot get memory map"); + + if (! grub_efi_exit_boot_services (efi_map_key)) + grub_fatal ("cannot exit boot services"); + + /* Note that no boot services are available from here. */ + + /* Pass EFI parameters. */ + if (grub_le_to_cpu16 (params->version) >= 0x0206) + { + params->v0206.efi_mem_desc_size = efi_desc_size; + params->v0206.efi_mem_desc_version = efi_desc_version; + params->v0206.efi_mmap = (grub_uint32_t) (unsigned long) efi_mmap_buf; + params->v0206.efi_mmap_size = efi_mmap_size; +#ifdef __x86_64__ + params->v0206.efi_mmap_hi = (grub_uint32_t) ((grub_uint64_t) efi_mmap_buf >> 32); +#endif + } + else if (grub_le_to_cpu16 (params->version) >= 0x0204) + { + params->v0204.efi_mem_desc_size = efi_desc_size; + params->v0204.efi_mem_desc_version = efi_desc_version; + params->v0204.efi_mmap = (grub_uint32_t) (unsigned long) efi_mmap_buf; + params->v0204.efi_mmap_size = efi_mmap_size; + } + } +#endif + /* FIXME. */ /* asm volatile ("lidt %0" : : "m" (idt_desc)); */ state.ebx = 0; @@ -675,7 +791,12 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), goto fail; } +#ifdef GRUB_MACHINE_EFI + /* XXX Linux assumes that only elilo can boot Linux on EFI!!! */ + params->type_of_loader = (LINUX_LOADER_ID_ELILO << 4); +#else params->type_of_loader = (LINUX_LOADER_ID_GRUB << 4); +#endif /* These two are used (instead of cmd_line_ptr) by older versions of Linux, and otherwise ignored. */ @@ -698,14 +819,27 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), /* Ignored by Linux. */ params->video_page = 0; - /* Must be non-zero even in text mode, or Linux will think there's no VGA. */ - params->video_mode = 0x3; - /* Only used when `video_mode == 0x7', otherwise ignored. */ params->video_ega_bx = 0; params->font_size = 16; /* XXX */ +#ifdef GRUB_MACHINE_EFI + if (grub_le_to_cpu16 (params->version) >= 0x0206) + { + params->v0206.efi_signature = GRUB_LINUX_EFI_SIGNATURE; + params->v0206.efi_system_table = (grub_uint32_t) (unsigned long) grub_efi_system_table; +#ifdef __x86_64__ + params->v0206.efi_system_table_hi = (grub_uint32_t) ((grub_uint64_t) grub_efi_system_table >> 32); +#endif + } + else if (grub_le_to_cpu16 (params->version) >= 0x0204) + { + params->v0204.efi_signature = GRUB_LINUX_EFI_SIGNATURE_0204; + params->v0204.efi_system_table = (grub_uint32_t) (unsigned long) grub_efi_system_table; + } +#endif + /* The other parameters are filled when booting. */ grub_file_seek (file, real_size + GRUB_DISK_SECTOR_SIZE); @@ -859,7 +993,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), } len = prot_size; - if (grub_file_read (file, (void *) GRUB_LINUX_BZIMAGE_ADDR, len) != len) + if (grub_file_read (file, prot_mode_mem, len) != len) grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file"); if (grub_errno == GRUB_ERR_NONE) From 4d27140f7f8d94598c9182e73e3dff0c9d299ded Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 13 Jan 2010 14:05:39 +0100 Subject: [PATCH 030/178] Removed efi linux loader (now it's in i386/linux.c) --- conf/i386-efi.rmk | 2 +- loader/i386/efi/linux.c | 1022 -------------------------------- loader/i386/linux_trampoline.S | 129 ---- 3 files changed, 1 insertion(+), 1152 deletions(-) delete mode 100644 loader/i386/efi/linux.c delete mode 100644 loader/i386/linux_trampoline.S diff --git a/conf/i386-efi.rmk b/conf/i386-efi.rmk index 673349858..fb5b11d04 100644 --- a/conf/i386-efi.rmk +++ b/conf/i386-efi.rmk @@ -94,7 +94,7 @@ appleldr_mod_CFLAGS = $(COMMON_CFLAGS) appleldr_mod_LDFLAGS = $(COMMON_LDFLAGS) # For linux.mod. -linux_mod_SOURCES = loader/i386/efi/linux.c +linux_mod_SOURCES = loader/i386/linux.c linux_mod_CFLAGS = $(COMMON_CFLAGS) linux_mod_LDFLAGS = $(COMMON_LDFLAGS) diff --git a/loader/i386/efi/linux.c b/loader/i386/efi/linux.c deleted file mode 100644 index 1abcc06db..000000000 --- a/loader/i386/efi/linux.c +++ /dev/null @@ -1,1022 +0,0 @@ -/* - * GRUB -- GRand Unified Bootloader - * Copyright (C) 2006,2007,2008,2009 Free Software Foundation, Inc. - * - * GRUB 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. - * - * GRUB 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 GRUB. If not, see . - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define GRUB_LINUX_CL_OFFSET 0x1000 -#define GRUB_LINUX_CL_END_OFFSET 0x2000 - -#define NEXT_MEMORY_DESCRIPTOR(desc, size) \ - ((grub_efi_memory_descriptor_t *) ((char *) (desc) + (size))) - -static grub_dl_t my_mod; - -static grub_size_t linux_mem_size; -static int loaded; -static void *real_mode_mem; -static void *prot_mode_mem; -static void *initrd_mem; -static grub_efi_uintn_t real_mode_pages; -static grub_efi_uintn_t prot_mode_pages; -static grub_efi_uintn_t initrd_pages; -static void *mmap_buf; - -static grub_uint8_t gdt[] __attribute__ ((aligned(16))) = - { - /* NULL. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - /* Reserved. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - /* Code segment. */ - 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x9A, 0xCF, 0x00, - /* Data segment. */ - 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x92, 0xCF, 0x00 - }; - -struct gdt_descriptor -{ - grub_uint16_t limit; - void *base; -} __attribute__ ((packed)); - -static struct gdt_descriptor gdt_desc = - { - sizeof (gdt) - 1, - gdt - }; - -struct idt_descriptor -{ - grub_uint16_t limit; - void *base; -} __attribute__ ((packed)); - -static struct idt_descriptor idt_desc = - { - 0, - 0 - }; - -static inline grub_size_t -page_align (grub_size_t size) -{ - return (size + (1 << 12) - 1) & (~((1 << 12) - 1)); -} - -/* Find the optimal number of pages for the memory map. Is it better to - move this code to efi/mm.c? */ -static grub_efi_uintn_t -find_mmap_size (void) -{ - static grub_efi_uintn_t mmap_size = 0; - - if (mmap_size != 0) - return mmap_size; - - mmap_size = (1 << 12); - while (1) - { - int ret; - grub_efi_memory_descriptor_t *mmap; - grub_efi_uintn_t desc_size; - - mmap = grub_malloc (mmap_size); - if (! mmap) - return 0; - - ret = grub_efi_get_memory_map (&mmap_size, mmap, 0, &desc_size, 0); - grub_free (mmap); - - if (ret < 0) - grub_fatal ("cannot get memory map"); - else if (ret > 0) - break; - - mmap_size += (1 << 12); - } - - /* Increase the size a bit for safety, because GRUB allocates more on - later, and EFI itself may allocate more. */ - mmap_size += (1 << 12); - - return page_align (mmap_size); -} - -static void -free_pages (void) -{ - if (real_mode_mem) - { - grub_efi_free_pages ((grub_addr_t) real_mode_mem, real_mode_pages); - real_mode_mem = 0; - } - - if (prot_mode_mem) - { - grub_efi_free_pages ((grub_addr_t) prot_mode_mem, prot_mode_pages); - prot_mode_mem = 0; - } - - if (initrd_mem) - { - grub_efi_free_pages ((grub_addr_t) initrd_mem, initrd_pages); - initrd_mem = 0; - } -} - -/* Allocate pages for the real mode code and the protected mode code - for linux as well as a memory map buffer. */ -static int -allocate_pages (grub_size_t prot_size) -{ - grub_efi_uintn_t desc_size; - grub_efi_memory_descriptor_t *mmap, *mmap_end; - grub_efi_uintn_t mmap_size, tmp_mmap_size; - grub_efi_memory_descriptor_t *desc; - grub_size_t real_size; - - /* Make sure that each size is aligned to a page boundary. */ - real_size = GRUB_LINUX_CL_END_OFFSET; - prot_size = page_align (prot_size); - mmap_size = find_mmap_size (); - - grub_dprintf ("linux", "real_size = %x, prot_size = %x, mmap_size = %x\n", - (unsigned) real_size, (unsigned) prot_size, (unsigned) mmap_size); - - /* Calculate the number of pages; Combine the real mode code with - the memory map buffer for simplicity. */ - real_mode_pages = ((real_size + mmap_size) >> 12); - prot_mode_pages = (prot_size >> 12); - - /* Initialize the memory pointers with NULL for convenience. */ - real_mode_mem = 0; - prot_mode_mem = 0; - - /* Read the memory map temporarily, to find free space. */ - mmap = grub_malloc (mmap_size); - if (! mmap) - return 0; - - tmp_mmap_size = mmap_size; - if (grub_efi_get_memory_map (&tmp_mmap_size, mmap, 0, &desc_size, 0) <= 0) - grub_fatal ("cannot get memory map"); - - mmap_end = NEXT_MEMORY_DESCRIPTOR (mmap, tmp_mmap_size); - - /* First, find free pages for the real mode code - and the memory map buffer. */ - for (desc = mmap; - desc < mmap_end; - desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size)) - { - /* Probably it is better to put the real mode code in the traditional - space for safety. */ - if (desc->type == GRUB_EFI_CONVENTIONAL_MEMORY - && desc->physical_start <= 0x90000 - && desc->num_pages >= real_mode_pages) - { - grub_efi_physical_address_t physical_end; - grub_efi_physical_address_t addr; - - physical_end = desc->physical_start + (desc->num_pages << 12); - if (physical_end > 0x90000) - physical_end = 0x90000; - - grub_dprintf ("linux", "physical_start = %x, physical_end = %x\n", - (unsigned) desc->physical_start, - (unsigned) physical_end); - addr = physical_end - real_size - mmap_size; - if (addr < 0x10000) - continue; - - grub_dprintf ("linux", "trying to allocate %u pages at %lx\n", - (unsigned) real_mode_pages, (unsigned long) addr); - real_mode_mem = grub_efi_allocate_pages (addr, real_mode_pages); - if (! real_mode_mem) - grub_fatal ("cannot allocate pages"); - - desc->num_pages -= real_mode_pages; - break; - } - } - - if (! real_mode_mem) - { - grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate real mode pages"); - goto fail; - } - - mmap_buf = (void *) ((char *) real_mode_mem + real_size); - - /* Next, find free pages for the protected mode code. */ - /* XXX what happens if anything is using this address? */ - prot_mode_mem = grub_efi_allocate_pages (0x100000, prot_mode_pages + 1); - if (! prot_mode_mem) - { - grub_error (GRUB_ERR_OUT_OF_MEMORY, - "cannot allocate protected mode pages"); - goto fail; - } - - grub_dprintf ("linux", "real_mode_mem = %lx, real_mode_pages = %x, " - "prot_mode_mem = %lx, prot_mode_pages = %x\n", - (unsigned long) real_mode_mem, (unsigned) real_mode_pages, - (unsigned long) prot_mode_mem, (unsigned) prot_mode_pages); - - grub_free (mmap); - return 1; - - fail: - grub_free (mmap); - free_pages (); - return 0; -} - -static void -grub_e820_add_region (struct grub_e820_mmap *e820_map, int *e820_num, - grub_uint64_t start, grub_uint64_t size, - grub_uint32_t type) -{ - int n = *e820_num; - - if (n >= GRUB_E820_MAX_ENTRY) - grub_fatal ("Too many e820 memory map entries"); - - if ((n > 0) && (e820_map[n - 1].addr + e820_map[n - 1].size == start) && - (e820_map[n - 1].type == type)) - e820_map[n - 1].size += size; - else - { - e820_map[n].addr = start; - e820_map[n].size = size; - e820_map[n].type = type; - (*e820_num)++; - } -} - -#ifdef __x86_64__ -extern grub_uint8_t grub_linux_trampoline_start[]; -extern grub_uint8_t grub_linux_trampoline_end[]; -#endif - -static grub_err_t -grub_linux_boot (void) -{ - struct linux_kernel_params *params; - grub_efi_uintn_t mmap_size; - grub_efi_uintn_t map_key; - grub_efi_uintn_t desc_size; - grub_efi_uint32_t desc_version; - int e820_num; - - params = real_mode_mem; - - grub_dprintf ("linux", "code32_start = %x, idt_desc = %lx, gdt_desc = %lx\n", - (unsigned) params->code32_start, - (unsigned long) &(idt_desc.limit), - (unsigned long) &(gdt_desc.limit)); - grub_dprintf ("linux", "idt = %x:%lx, gdt = %x:%lx\n", - (unsigned) idt_desc.limit, (unsigned long) idt_desc.base, - (unsigned) gdt_desc.limit, (unsigned long) gdt_desc.base); - - auto int NESTED_FUNC_ATTR hook (grub_uint64_t, grub_uint64_t, grub_uint32_t); - int NESTED_FUNC_ATTR hook (grub_uint64_t addr, grub_uint64_t size, grub_uint32_t type) - { - switch (type) - { - case GRUB_MACHINE_MEMORY_AVAILABLE: - grub_e820_add_region (params->e820_map, &e820_num, - addr, size, GRUB_E820_RAM); - break; - -#ifdef GRUB_MACHINE_MEMORY_ACPI - case GRUB_MACHINE_MEMORY_ACPI: - grub_e820_add_region (params->e820_map, &e820_num, - addr, size, GRUB_E820_ACPI); - break; -#endif - -#ifdef GRUB_MACHINE_MEMORY_NVS - case GRUB_MACHINE_MEMORY_NVS: - grub_e820_add_region (params->e820_map, &e820_num, - addr, size, GRUB_E820_NVS); - break; -#endif - -#ifdef GRUB_MACHINE_MEMORY_CODE - case GRUB_MACHINE_MEMORY_CODE: - grub_e820_add_region (params->e820_map, &e820_num, - addr, size, GRUB_E820_EXEC_CODE); - break; -#endif - - default: - grub_e820_add_region (params->e820_map, &e820_num, - addr, size, GRUB_E820_RESERVED); - } - return 0; - } - - e820_num = 0; - grub_mmap_iterate (hook); - params->mmap_size = e820_num; - - mmap_size = find_mmap_size (); - if (grub_efi_get_memory_map (&mmap_size, mmap_buf, &map_key, - &desc_size, &desc_version) <= 0) - grub_fatal ("cannot get memory map"); - - if (! grub_efi_exit_boot_services (map_key)) - grub_fatal ("cannot exit boot services"); - - /* Note that no boot services are available from here. */ - - /* Pass EFI parameters. */ - if (grub_le_to_cpu16 (params->version) >= 0x0206) - { - params->v0206.efi_mem_desc_size = desc_size; - params->v0206.efi_mem_desc_version = desc_version; - params->v0206.efi_mmap = (grub_uint32_t) (unsigned long) mmap_buf; - params->v0206.efi_mmap_size = mmap_size; -#ifdef __x86_64__ - params->v0206.efi_mmap_hi = (grub_uint32_t) ((grub_uint64_t) mmap_buf >> 32); -#endif - } - else if (grub_le_to_cpu16 (params->version) >= 0x0204) - { - params->v0204.efi_mem_desc_size = desc_size; - params->v0204.efi_mem_desc_version = desc_version; - params->v0204.efi_mmap = (grub_uint32_t) (unsigned long) mmap_buf; - params->v0204.efi_mmap_size = mmap_size; - } - -#ifdef __x86_64__ - - grub_memcpy ((char *) prot_mode_mem + (prot_mode_pages << 12), - grub_linux_trampoline_start, - grub_linux_trampoline_end - grub_linux_trampoline_start); - - ((void (*) (unsigned long, void *)) ((char *) prot_mode_mem - + (prot_mode_pages << 12))) - (params->code32_start, real_mode_mem); - -#else - - /* Hardware interrupts are not safe any longer. */ - asm volatile ("cli" : : ); - - /* Load the IDT and the GDT for the bootstrap. */ - asm volatile ("lidt %0" : : "m" (idt_desc)); - asm volatile ("lgdt %0" : : "m" (gdt_desc)); - - /* Pass parameters. */ - asm volatile ("movl %0, %%ecx" : : "m" (params->code32_start)); - asm volatile ("movl %0, %%esi" : : "m" (real_mode_mem)); - - asm volatile ("xorl %%ebx, %%ebx" : : ); - - /* Enter Linux. */ - asm volatile ("jmp *%%ecx" : : ); - -#endif - - /* Never reach here. */ - return GRUB_ERR_NONE; -} - -static grub_err_t -grub_linux_unload (void) -{ - free_pages (); - grub_dl_unref (my_mod); - loaded = 0; - return GRUB_ERR_NONE; -} - -static grub_efi_guid_t uga_draw_guid = GRUB_EFI_UGA_DRAW_GUID; - - -#define RGB_MASK 0xffffff -#define RGB_MAGIC 0x121314 -#define LINE_MIN 800 -#define LINE_MAX 4096 -#define FBTEST_STEP (0x10000 >> 2) -#define FBTEST_COUNT 8 - -static int -find_line_len (grub_uint32_t *fb_base, grub_uint32_t *line_len) -{ - grub_uint32_t *base = (grub_uint32_t *) (grub_target_addr_t) *fb_base; - int i; - - for (i = 0; i < FBTEST_COUNT; i++, base += FBTEST_STEP) - { - if ((*base & RGB_MASK) == RGB_MAGIC) - { - int j; - - for (j = LINE_MIN; j <= LINE_MAX; j++) - { - if ((base[j] & RGB_MASK) == RGB_MAGIC) - { - *fb_base = (grub_uint32_t) (grub_target_addr_t) base; - *line_len = j << 2; - - return 1; - } - } - - break; - } - } - - return 0; -} - -static int -find_framebuf (grub_uint32_t *fb_base, grub_uint32_t *line_len) -{ - int found = 0; - - auto int NESTED_FUNC_ATTR find_card (grub_pci_device_t dev, - grub_pci_id_t pciid); - - int NESTED_FUNC_ATTR find_card (grub_pci_device_t dev, - grub_pci_id_t pciid) - { - grub_pci_address_t addr; - - addr = grub_pci_make_address (dev, 2); - if (grub_pci_read (addr) >> 24 == 0x3) - { - int i; - - grub_printf ("Display controller: %d:%d.%d\nDevice id: %x\n", - grub_pci_get_bus (dev), grub_pci_get_device (dev), - grub_pci_get_function (dev), pciid); - addr += 8; - for (i = 0; i < 6; i++, addr += 4) - { - grub_uint32_t old_bar1, old_bar2, type; - grub_uint64_t base64; - - old_bar1 = grub_pci_read (addr); - if ((! old_bar1) || (old_bar1 & GRUB_PCI_ADDR_SPACE_IO)) - continue; - - type = old_bar1 & GRUB_PCI_ADDR_MEM_TYPE_MASK; - if (type == GRUB_PCI_ADDR_MEM_TYPE_64) - { - if (i == 5) - break; - - old_bar2 = grub_pci_read (addr + 4); - } - else - old_bar2 = 0; - - base64 = old_bar2; - base64 <<= 32; - base64 |= (old_bar1 & GRUB_PCI_ADDR_MEM_MASK); - - grub_printf ("%s(%d): 0x%llx\n", - ((old_bar1 & GRUB_PCI_ADDR_MEM_PREFETCH) ? - "VMEM" : "MMIO"), i, - (unsigned long long) base64); - - if ((old_bar1 & GRUB_PCI_ADDR_MEM_PREFETCH) && (! found)) - { - *fb_base = base64; - if (find_line_len (fb_base, line_len)) - found++; - } - - if (type == GRUB_PCI_ADDR_MEM_TYPE_64) - { - i++; - addr += 4; - } - } - } - - return found; - } - - grub_pci_iterate (find_card); - return found; -} - -static int -grub_linux_setup_video (struct linux_kernel_params *params) -{ - grub_efi_uga_draw_protocol_t *c; - grub_uint32_t width, height, depth, rate, pixel, fb_base, line_len; - int ret; - - c = grub_efi_locate_protocol (&uga_draw_guid, 0); - if (! c) - return 1; - - if (efi_call_5 (c->get_mode, c, &width, &height, &depth, &rate)) - return 1; - - grub_printf ("Video mode: %ux%u-%u@%u\n", width, height, depth, rate); - - grub_efi_set_text_mode (0); - pixel = RGB_MAGIC; - efi_call_10 (c->blt, c, (struct grub_efi_uga_pixel *) &pixel, - GRUB_EFI_UGA_VIDEO_FILL, 0, 0, 0, 0, 1, height, 0); - ret = find_framebuf (&fb_base, &line_len); - grub_efi_set_text_mode (1); - - if (! ret) - { - grub_printf ("Can\'t find frame buffer address\n"); - return 1; - } - - grub_printf ("Frame buffer base: 0x%x\n", fb_base); - grub_printf ("Video line length: %d\n", line_len); - - params->lfb_width = width; - params->lfb_height = height; - params->lfb_depth = depth; - params->lfb_line_len = line_len; - - params->lfb_base = fb_base; - params->lfb_size = (line_len * params->lfb_height + 65535) >> 16; - - params->red_mask_size = 8; - params->red_field_pos = 16; - params->green_mask_size = 8; - params->green_field_pos = 8; - params->blue_mask_size = 8; - params->blue_field_pos = 0; - params->reserved_mask_size = 8; - params->reserved_field_pos = 24; - - params->have_vga = GRUB_VIDEO_TYPE_VLFB; - params->vid_mode = 0x338; /* 1024x768x32 */ - - return 0; -} - -static grub_err_t -grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), - int argc, char *argv[]) -{ - grub_file_t file = 0; - struct linux_kernel_header lh; - struct linux_kernel_params *params; - grub_uint8_t setup_sects; - grub_size_t real_size, prot_size; - grub_ssize_t len; - int i; - char *dest; - - grub_dl_ref (my_mod); - - if (argc == 0) - { - grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified"); - goto fail; - } - - file = grub_file_open (argv[0]); - if (! file) - goto fail; - - if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh)) - { - grub_error (GRUB_ERR_READ_ERROR, "cannot read the Linux header"); - goto fail; - } - - if (lh.boot_flag != grub_cpu_to_le16 (0xaa55)) - { - grub_error (GRUB_ERR_BAD_OS, "invalid magic number"); - goto fail; - } - - if (lh.setup_sects > GRUB_LINUX_MAX_SETUP_SECTS) - { - grub_error (GRUB_ERR_BAD_OS, "too many setup sectors"); - goto fail; - } - - /* EFI support is quite new, so reject old versions. */ - if (lh.header != grub_cpu_to_le32 (GRUB_LINUX_MAGIC_SIGNATURE) - || grub_le_to_cpu16 (lh.version) < 0x0203) - { - grub_error (GRUB_ERR_BAD_OS, "too old version"); - goto fail; - } - - /* I'm not sure how to support zImage on EFI. */ - if (! (lh.loadflags & GRUB_LINUX_FLAG_BIG_KERNEL)) - { - grub_error (GRUB_ERR_BAD_OS, "zImage is not supported"); - goto fail; - } - - setup_sects = lh.setup_sects; - - /* If SETUP_SECTS is not set, set it to the default (4). */ - if (! setup_sects) - setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS; - - real_size = setup_sects << GRUB_DISK_SECTOR_BITS; - prot_size = grub_file_size (file) - real_size - GRUB_DISK_SECTOR_SIZE; - - if (! allocate_pages (prot_size)) - goto fail; - - params = (struct linux_kernel_params *) real_mode_mem; - grub_memset (params, 0, GRUB_LINUX_CL_END_OFFSET); - grub_memcpy (¶ms->setup_sects, &lh.setup_sects, sizeof (lh) - 0x1F1); - - params->ps_mouse = params->padding10 = 0; - - len = 0x400 - sizeof (lh); - if (grub_file_read (file, (char *) real_mode_mem + sizeof (lh), len) != len) - { - grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file"); - goto fail; - } - - /* XXX Linux assumes that only elilo can boot Linux on EFI!!! */ - params->type_of_loader = (LINUX_LOADER_ID_ELILO << 4); - - params->cl_magic = GRUB_LINUX_CL_MAGIC; - params->cl_offset = 0x1000; - params->cmd_line_ptr = (unsigned long) real_mode_mem + 0x1000; - params->ramdisk_image = 0; - params->ramdisk_size = 0; - - params->heap_end_ptr = GRUB_LINUX_HEAP_END_OFFSET; - params->loadflags |= GRUB_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. */ - params->ext_mem = ((32 * 0x100000) >> 10); - params->alt_mem = ((32 * 0x100000) >> 10); - - { - grub_term_output_t term; - int found = 0; - FOR_ACTIVE_TERM_OUTPUTS(term) - if (grub_strcmp (term->name, "vga_text") == 0 - || grub_strcmp (term->name, "console") == 0) - { - grub_uint16_t pos = grub_term_getxy (term); - params->video_cursor_x = pos >> 8; - params->video_cursor_y = pos & 0xff; - params->video_width = grub_term_width (term); - params->video_height = grub_term_height (term); - found = 1; - break; - } - if (!found) - { - params->video_cursor_x = 0; - params->video_cursor_y = 0; - params->video_width = 80; - params->video_height = 25; - } - } - params->video_page = 0; /* ??? */ - params->video_mode = grub_efi_system_table->con_out->mode->mode; - params->video_ega_bx = 0; - params->have_vga = 0; - params->font_size = 16; /* XXX */ - - if (grub_le_to_cpu16 (params->version) >= 0x0206) - { - params->v0206.efi_signature = GRUB_LINUX_EFI_SIGNATURE; - params->v0206.efi_system_table = (grub_uint32_t) (unsigned long) grub_efi_system_table; -#ifdef __x86_64__ - params->v0206.efi_system_table_hi = (grub_uint32_t) ((grub_uint64_t) grub_efi_system_table >> 32); -#endif - } - else if (grub_le_to_cpu16 (params->version) >= 0x0204) - { - params->v0204.efi_signature = GRUB_LINUX_EFI_SIGNATURE_0204; - params->v0204.efi_system_table = (grub_uint32_t) (unsigned long) grub_efi_system_table; - } - -#if 0 - /* The structure is zeroed already. */ - - /* No VBE on EFI. */ - params->lfb_width = 0; - params->lfb_height = 0; - params->lfb_depth = 0; - params->lfb_base = 0; - params->lfb_size = 0; - params->lfb_line_len = 0; - params->red_mask_size = 0; - params->red_field_pos = 0; - params->green_mask_size = 0; - params->green_field_pos = 0; - params->blue_mask_size = 0; - params->blue_field_pos = 0; - params->reserved_mask_size = 0; - params->reserved_field_pos = 0; - params->vesapm_segment = 0; - params->vesapm_offset = 0; - params->lfb_pages = 0; - params->vesa_attrib = 0; - - /* No APM on EFI. */ - params->apm_version = 0; - params->apm_code_segment = 0; - params->apm_entry = 0; - params->apm_16bit_code_segment = 0; - params->apm_data_segment = 0; - params->apm_flags = 0; - params->apm_code_len = 0; - params->apm_data_len = 0; - - /* XXX is there any way to use SpeedStep on EFI? */ - params->ist_signature = 0; - params->ist_command = 0; - params->ist_event = 0; - params->ist_perf_level = 0; - - /* Let the kernel probe the information. */ - grub_memset (params->hd0_drive_info, 0, sizeof (params->hd0_drive_info)); - grub_memset (params->hd1_drive_info, 0, sizeof (params->hd1_drive_info)); - - /* No MCA on EFI. */ - params->rom_config_len = 0; - - /* No need to fake the BIOS's memory map. */ - params->mmap_size = 0; - - /* Let the kernel probe the information. */ - params->ps_mouse = 0; - - /* Clear padding for future compatibility. */ - grub_memset (params->padding1, 0, sizeof (params->padding1)); - grub_memset (params->padding2, 0, sizeof (params->padding2)); - grub_memset (params->padding3, 0, sizeof (params->padding3)); - grub_memset (params->padding4, 0, sizeof (params->padding4)); - grub_memset (params->padding5, 0, sizeof (params->padding5)); - grub_memset (params->padding6, 0, sizeof (params->padding6)); - grub_memset (params->padding7, 0, sizeof (params->padding7)); - grub_memset (params->padding8, 0, sizeof (params->padding8)); - grub_memset (params->padding9, 0, sizeof (params->padding9)); - -#endif - - /* The other EFI parameters are filled when booting. */ - - grub_file_seek (file, real_size + GRUB_DISK_SECTOR_SIZE); - - /* XXX there is no way to know if the kernel really supports EFI. */ - grub_printf (" [Linux-bzImage, setup=0x%x, size=0x%x]\n", - (unsigned) real_size, (unsigned) prot_size); - - grub_linux_setup_video (params); - - /* Detect explicitly specified memory size, if any. */ - linux_mem_size = 0; - for (i = 1; i < argc; i++) - if (grub_memcmp (argv[i], "mem=", 4) == 0) - { - char *val = argv[i] + 4; - - linux_mem_size = grub_strtoul (val, &val, 0); - - if (grub_errno) - { - grub_errno = GRUB_ERR_NONE; - linux_mem_size = 0; - } - else - { - int shift = 0; - - switch (grub_tolower (val[0])) - { - case 'g': - shift += 10; - case 'm': - shift += 10; - case 'k': - shift += 10; - default: - break; - } - - /* Check an overflow. */ - if (linux_mem_size > (~0UL >> shift)) - linux_mem_size = 0; - else - linux_mem_size <<= shift; - } - } - else if (grub_memcmp (argv[i], "video=efifb", 11) == 0) - { - if (params->have_vga) - params->have_vga = GRUB_VIDEO_TYPE_EFI; - } - - /* Specify the boot file. */ - dest = grub_stpcpy ((char *) real_mode_mem + GRUB_LINUX_CL_OFFSET, - "BOOT_IMAGE="); - dest = grub_stpcpy (dest, argv[0]); - - /* Copy kernel parameters. */ - for (i = 1; - i < argc - && dest + grub_strlen (argv[i]) + 1 < ((char *) real_mode_mem - + GRUB_LINUX_CL_END_OFFSET); - i++) - { - *dest++ = ' '; - dest = grub_stpcpy (dest, argv[i]); - } - - len = prot_size; - if (grub_file_read (file, (void *) GRUB_LINUX_BZIMAGE_ADDR, len) != len) - grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file"); - - if (grub_errno == GRUB_ERR_NONE) - { - grub_loader_set (grub_linux_boot, grub_linux_unload, 1); - loaded = 1; - } - - fail: - - if (file) - grub_file_close (file); - - if (grub_errno != GRUB_ERR_NONE) - { - grub_dl_unref (my_mod); - loaded = 0; - } - - return grub_errno; -} - -static grub_err_t -grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), - int argc, char *argv[]) -{ - grub_file_t file = 0; - grub_ssize_t size; - grub_addr_t addr_min, addr_max; - grub_addr_t addr; - grub_efi_uintn_t mmap_size; - grub_efi_memory_descriptor_t *desc; - grub_efi_uintn_t desc_size; - struct linux_kernel_header *lh; - - if (argc == 0) - { - grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified"); - goto fail; - } - - if (! loaded) - { - grub_error (GRUB_ERR_BAD_ARGUMENT, "you need to load the kernel first"); - goto fail; - } - - file = grub_file_open (argv[0]); - if (! file) - goto fail; - - size = grub_file_size (file); - initrd_pages = (page_align (size) >> 12); - - lh = (struct linux_kernel_header *) real_mode_mem; - - addr_max = (grub_cpu_to_le32 (lh->initrd_addr_max) << 10); - 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; - - /* Usually, the compression ratio is about 50%. */ - addr_min = (grub_addr_t) prot_mode_mem + ((prot_mode_pages * 3) << 12) - + page_align (size); - - /* Find the highest address to put the initrd. */ - mmap_size = find_mmap_size (); - if (grub_efi_get_memory_map (&mmap_size, mmap_buf, 0, &desc_size, 0) <= 0) - grub_fatal ("cannot get memory map"); - - addr = 0; - for (desc = mmap_buf; - desc < NEXT_MEMORY_DESCRIPTOR (mmap_buf, mmap_size); - desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size)) - { - if (desc->type == GRUB_EFI_CONVENTIONAL_MEMORY - && desc->num_pages >= initrd_pages) - { - grub_efi_physical_address_t physical_end; - - physical_end = desc->physical_start + (desc->num_pages << 12); - if (physical_end > addr_max) - physical_end = addr_max; - - if (physical_end < page_align (size)) - continue; - - physical_end -= page_align (size); - - if ((physical_end >= addr_min) && - (physical_end >= desc->physical_start) && - (physical_end > addr)) - addr = physical_end; - } - } - - if (addr == 0) - { - grub_error (GRUB_ERR_OUT_OF_MEMORY, "no free pages available"); - goto fail; - } - - initrd_mem = grub_efi_allocate_pages (addr, initrd_pages); - if (! initrd_mem) - grub_fatal ("cannot allocate pages"); - - if (grub_file_read (file, initrd_mem, size) != size) - { - grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file"); - goto fail; - } - - grub_printf (" [Initrd, addr=0x%x, size=0x%x]\n", - (unsigned) addr, (unsigned) size); - - lh->ramdisk_image = addr; - lh->ramdisk_size = size; - lh->root_dev = 0x0100; /* XXX */ - - fail: - if (file) - grub_file_close (file); - - return grub_errno; -} - -static grub_command_t cmd_linux, cmd_initrd; - -GRUB_MOD_INIT(linux) -{ - cmd_linux = grub_register_command ("linux", grub_cmd_linux, - 0, N_("Load Linux.")); - cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd, - 0, N_("Load initrd.")); - my_mod = mod; -} - -GRUB_MOD_FINI(linux) -{ - grub_unregister_command (cmd_linux); - grub_unregister_command (cmd_initrd); -} diff --git a/loader/i386/linux_trampoline.S b/loader/i386/linux_trampoline.S deleted file mode 100644 index 4acea7b11..000000000 --- a/loader/i386/linux_trampoline.S +++ /dev/null @@ -1,129 +0,0 @@ -/* - * GRUB -- GRand Unified Bootloader - * Copyright (C) 2009 Free Software Foundation, Inc. - * - * GRUB 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. - * - * GRUB 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 GRUB. If not, see . - */ - -#include - - - .p2align 4 /* force 16-byte alignment */ -VARIABLE(grub_linux_trampoline_start) - cli - /* %rdi contains protected memory start and %rsi - contains real memory start. */ - - mov %rsi, %rbx - - call base -base: - pop %rsi - -#ifdef APPLE_CC - lea (cont1 - base) (%esi, 1), %rax - mov %eax, (jump_vector - base) (%esi, 1) - - lea (gdt - base) (%esi, 1), %rax - mov %rax, (gdtaddr - base) (%esi, 1) - - /* Switch to compatibility mode. */ - - lidt (idtdesc - base) (%esi, 1) - lgdt (gdtdesc - base) (%esi, 1) - - /* Update %cs. Thanks to David Miller for pointing this mistake out. */ - ljmp *(jump_vector - base) (%esi, 1) -#else - lea (cont1 - base) (%rsi, 1), %rax - mov %eax, (jump_vector - base) (%rsi, 1) - - lea (gdt - base) (%rsi, 1), %rax - mov %rax, (gdtaddr - base) (%rsi, 1) - - /* Switch to compatibility mode. */ - - lidt (idtdesc - base) (%rsi, 1) - lgdt (gdtdesc - base) (%rsi, 1) - - /* Update %cs. Thanks to David Miller for pointing this mistake out. */ - ljmp *(jump_vector - base) (%rsi, 1) -#endif - -cont1: - .code32 - - /* Update other registers. */ - mov $0x18, %eax - mov %eax, %ds - mov %eax, %es - mov %eax, %fs - mov %eax, %gs - mov %eax, %ss - - /* Disable paging. */ - mov %cr0, %eax - and $0x7fffffff, %eax - mov %eax, %cr0 - - /* Disable amd64. */ - mov $0xc0000080, %ecx - rdmsr - and $0xfffffeff, %eax - wrmsr - - /* Turn off PAE. */ - movl %cr4, %eax - and $0xffffffcf, %eax - mov %eax, %cr4 - - jmp cont2 -cont2: - .code32 - - mov %ebx, %esi - - jmp *%edi - - /* GDT. */ - .p2align 4 -gdt: - /* NULL. */ - .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 - - /* Reserved. */ - .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 - - /* Code segment. */ - .byte 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x9A, 0xCF, 0x00 - - /* Data segment. */ - .byte 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x92, 0xCF, 0x00 - -gdtdesc: - .word 31 -gdtaddr: - .quad gdt - -idtdesc: - .word 0 -idtaddr: - .quad 0 - - .p2align 4 -jump_vector: - /* Jump location. Is filled by the code */ - .long 0 - .long 0x10 -VARIABLE(grub_linux_trampoline_end) From 8b889c332a127949f729e70bc0d06724413705a4 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 13 Jan 2010 17:25:49 +0100 Subject: [PATCH 031/178] Fix bug when whole region is free --- lib/relocator.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/relocator.c b/lib/relocator.c index 889497334..090a6c7e3 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -199,9 +199,9 @@ malloc_in_range (struct grub_relocator *rel, grub_mm_region_t r, rp; for (rp = NULL, r = grub_mm_base; r; rp = r, r = r->next) { - grub_dprintf ("relocator", "region %p. %d %d %d\n", r, + grub_dprintf ("relocator", "region %p. %d %d %d %d\n", r, (grub_addr_t) r + r->size + sizeof (*r) >= start, - (grub_addr_t) r < end && r->size + sizeof (*r) >= size, + (grub_addr_t) r < end, r->size + sizeof (*r) >= size, (rb == NULL || (from_low_priv ? rb > r : rb < r))); if ((grub_addr_t) r + r->size + sizeof (*r) >= start && (grub_addr_t) r < end && r->size + sizeof (*r) >= size @@ -224,7 +224,7 @@ malloc_in_range (struct grub_relocator *rel, hb = get_best_header (rel, start, end, align, size, rb, &hbp, &best_addr, from_low_priv, collisioncheck); - grub_dprintf ("relocator", "best header %p/%lx\n", hb, + grub_dprintf ("relocator", "best header %p/%p/%lx\n", hb, hbp, (unsigned long) best_addr); if (!hb) @@ -253,8 +253,8 @@ malloc_in_range (struct grub_relocator *rel, - (newreg_start - (grub_addr_t) rb)) >> GRUB_MM_ALIGN_LOG2; new_header = (void *) (newreg_start + sizeof (*rb)); - if (newhnext == hb->next) - newhnext = newhnext; + if (newhnext == hb) + newhnext = new_header; new_header->next = newhnext; new_header->size = newhsize; new_header->magic = GRUB_MM_FREE_MAGIC; @@ -280,6 +280,18 @@ malloc_in_range (struct grub_relocator *rel, rbp->next = newreg; else grub_mm_base = newreg; + { + grub_mm_header_t h = newreg->first, hp = NULL; + do + { + if ((void *) h < (void *) (newreg + 1)) + grub_fatal ("Failed to adjust memory region: %p, %p, %p, %p, %p", + newreg, newreg->first, h, hp, hb); + hp = h; + h = h->next; + } + while (h != newreg->first); + } } *res = best_addr; return 1; From 8071fb79972a62d06a8bb61321b07aa52ec61adb Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 13 Jan 2010 17:26:47 +0100 Subject: [PATCH 032/178] ieee1275 support in linux.c --- loader/i386/linux.c | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/loader/i386/linux.c b/loader/i386/linux.c index 9fdf9b445..f7abcf094 100644 --- a/loader/i386/linux.c +++ b/loader/i386/linux.c @@ -39,6 +39,10 @@ #include #define HAS_VGA_TEXT 0 #define DEFAULT_VIDEO_MODE "800x600" +#elif defined (GRUB_MACHINE_IEEE1275) +#include +#define HAS_VGA_TEXT 0 +#define DEFAULT_VIDEO_MODE "text" #else #include #include @@ -541,6 +545,20 @@ grub_linux_boot (void) params = real_mode_mem; +#ifdef GRUB_MACHINE_IEEE1275 + { + char *bootpath; + grub_ssize_t len; + + bootpath = grub_env_get ("root"); + if (bootpath) + grub_ieee1275_set_property (grub_ieee1275_chosen, + "bootpath", bootpath, + grub_strlen (bootpath) + 1, + &len); + } +#endif + grub_dprintf ("linux", "code32_start = %x\n", (unsigned) params->code32_start); @@ -618,8 +636,6 @@ grub_linux_boot (void) #if HAS_VGA_TEXT params->have_vga = GRUB_VIDEO_LINUX_TYPE_EGA_TEXT; params->video_mode = 0x3; - params->video_width = 80; - params->video_height = 25; #else params->have_vga = 0; params->video_mode = 0; @@ -629,17 +645,22 @@ grub_linux_boot (void) } /* Initialize these last, because terminal position could be affected by printfs above. */ +#ifndef GRUB_MACHINE_IEEE1275 if (params->have_vga == GRUB_VIDEO_LINUX_TYPE_EGA_TEXT) +#endif { grub_term_output_t term; int found = 0; FOR_ACTIVE_TERM_OUTPUTS(term) if (grub_strcmp (term->name, "vga_text") == 0 - || grub_strcmp (term->name, "console") == 0) + || grub_strcmp (term->name, "console") == 0 + || grub_strcmp (term->name, "ofconsole") == 0) { grub_uint16_t pos = grub_term_getxy (term); params->video_cursor_x = pos >> 8; params->video_cursor_y = pos & 0xff; + params->video_width = grub_term_width (term); + params->video_height = grub_term_height (term); found = 1; break; } @@ -647,9 +668,20 @@ grub_linux_boot (void) { params->video_cursor_x = 0; params->video_cursor_y = 0; + params->video_width = 80; + params->video_height = 25; } } +#ifdef GRUB_MACHINE_IEEE1275 + { + params->ofw_signature = GRUB_LINUX_OFW_SIGNATURE; + params->ofw_num_items = 1; + params->ofw_cif_handler = (grub_uint32_t) grub_ieee1275_entry_fn; + params->ofw_idt = 0; + } +#endif + #ifdef GRUB_MACHINE_EFI { grub_efi_uintn_t efi_map_key, efi_desc_size; @@ -688,6 +720,7 @@ grub_linux_boot (void) /* asm volatile ("lidt %0" : : "m" (idt_desc)); */ state.ebx = 0; state.esi = real_mode_target; + state.esp = real_mode_target; state.eip = params->code32_start; return grub_relocator32_boot (relocator, state); } From 9efe1428614ef9d08bee26a4e739e87eaad07109 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 13 Jan 2010 17:27:45 +0100 Subject: [PATCH 033/178] Use linux.c on i386-ieee1275 --- conf/i386-ieee1275.rmk | 2 +- loader/i386/ieee1275/linux.c | 311 ----------------------------------- 2 files changed, 1 insertion(+), 312 deletions(-) delete mode 100644 loader/i386/ieee1275/linux.c diff --git a/conf/i386-ieee1275.rmk b/conf/i386-ieee1275.rmk index b9e192796..28d454ffd 100644 --- a/conf/i386-ieee1275.rmk +++ b/conf/i386-ieee1275.rmk @@ -97,7 +97,7 @@ serial_mod_CFLAGS = $(COMMON_CFLAGS) serial_mod_LDFLAGS = $(COMMON_LDFLAGS) # For linux.mod. -linux_mod_SOURCES = loader/i386/ieee1275/linux.c +linux_mod_SOURCES = loader/i386/linux.c linux_mod_CFLAGS = $(COMMON_CFLAGS) linux_mod_LDFLAGS = $(COMMON_LDFLAGS) diff --git a/loader/i386/ieee1275/linux.c b/loader/i386/ieee1275/linux.c deleted file mode 100644 index 8780804fd..000000000 --- a/loader/i386/ieee1275/linux.c +++ /dev/null @@ -1,311 +0,0 @@ -/* linux.c - boot Linux zImage or bzImage */ -/* - * GRUB -- GRand Unified Bootloader - * Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2007,2008,2009,2010 Free Software Foundation, Inc. - * - * GRUB 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. - * - * GRUB 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 GRUB. If not, see . - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define GRUB_OFW_LINUX_PARAMS_ADDR 0x90000 -#define GRUB_OFW_LINUX_KERNEL_ADDR 0x100000 -#define GRUB_OFW_LINUX_INITRD_ADDR 0x800000 - -#define GRUB_OFW_LINUX_CL_OFFSET 0x1e00 -#define GRUB_OFW_LINUX_CL_LENGTH 0x100 - -static grub_dl_t my_mod; - -static grub_size_t kernel_size; -static char *kernel_addr, *kernel_cmdline; -static grub_size_t initrd_size; - -static grub_err_t -grub_linux_unload (void) -{ - grub_free (kernel_cmdline); - grub_free (kernel_addr); - kernel_cmdline = 0; - kernel_addr = 0; - initrd_size = 0; - - grub_dl_unref (my_mod); - - return GRUB_ERR_NONE; -} - -/* -static int -grub_ieee1275_debug (void) -{ - struct enter_args - { - struct grub_ieee1275_common_hdr common; - } - args; - - INIT_IEEE1275_COMMON (&args.common, "enter", 0, 0); - - if (IEEE1275_CALL_ENTRY_FN (&args) == -1) - return -1; - - return 0; -} -*/ - -static grub_err_t -grub_linux_boot (void) -{ - struct linux_kernel_params *params; - struct linux_kernel_header *lh; - char *prot_code; - char *bootpath; - grub_ssize_t len; - - bootpath = grub_env_get ("root"); - if (bootpath) - grub_ieee1275_set_property (grub_ieee1275_chosen, - "bootpath", bootpath, - grub_strlen (bootpath) + 1, - &len); - - params = (struct linux_kernel_params *) GRUB_OFW_LINUX_PARAMS_ADDR; - lh = (struct linux_kernel_header *) params; - - grub_memset ((char *) params, 0, GRUB_OFW_LINUX_CL_OFFSET); - - params->alt_mem = grub_mmap_get_upper () >> 10; - params->ext_mem = params->alt_mem; - - lh->cmd_line_ptr = (char *) - (GRUB_OFW_LINUX_PARAMS_ADDR + GRUB_OFW_LINUX_CL_OFFSET); - - params->cl_magic = GRUB_LINUX_CL_MAGIC; - params->cl_offset = GRUB_OFW_LINUX_CL_OFFSET; - - { - grub_term_output_t term; - int found = 0; - FOR_ACTIVE_TERM_OUTPUTS(term) - if (grub_strcmp (term->name, "ofconsole") == 0) - { - grub_uint16_t pos = grub_term_getxy (term); - params->video_cursor_x = pos >> 8; - params->video_cursor_y = pos & 0xff; - params->video_width = grub_term_width (term); - params->video_height = grub_term_height (term); - found = 1; - break; - } - if (!found) - { - params->video_cursor_x = 0; - params->video_cursor_y = 0; - params->video_width = 80; - params->video_height = 25; - } - } - - params->font_size = 16; - - params->ofw_signature = GRUB_LINUX_OFW_SIGNATURE; - params->ofw_num_items = 1; - params->ofw_cif_handler = (grub_uint32_t) grub_ieee1275_entry_fn; - params->ofw_idt = 0; - - if (initrd_size) - { - lh->type_of_loader = 1; - lh->ramdisk_image = GRUB_OFW_LINUX_INITRD_ADDR; - lh->ramdisk_size = initrd_size; - } - - if (kernel_cmdline) - grub_strcpy (lh->cmd_line_ptr, kernel_cmdline); - - prot_code = (char *) GRUB_OFW_LINUX_KERNEL_ADDR; - grub_memcpy (prot_code, kernel_addr, kernel_size); - - asm volatile ("movl %0, %%esi" : : "m" (params)); - asm volatile ("movl %%esi, %%esp" : : ); - asm volatile ("movl %0, %%ecx" : : "m" (prot_code)); - asm volatile ("xorl %%ebx, %%ebx" : : ); - asm volatile ("jmp *%%ecx" : : ); - - return GRUB_ERR_NONE; -} - -static grub_err_t -grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), - int argc, char *argv[]) -{ - grub_file_t file = 0; - struct linux_kernel_header lh; - grub_uint8_t setup_sects; - grub_size_t real_size, prot_size; - int i; - char *dest; - - grub_dl_ref (my_mod); - - if (argc == 0) - { - grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified"); - goto fail; - } - - file = grub_file_open (argv[0]); - if (! file) - goto fail; - - if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh)) - { - grub_error (GRUB_ERR_READ_ERROR, "cannot read the Linux header"); - goto fail; - } - - if ((lh.boot_flag != grub_cpu_to_le16 (0xaa55)) || - (lh.header != grub_cpu_to_le32 (GRUB_LINUX_MAGIC_SIGNATURE))) - { - grub_error (GRUB_ERR_BAD_OS, "invalid magic number"); - goto fail; - } - - setup_sects = lh.setup_sects; - if (! setup_sects) - setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS; - - real_size = setup_sects << GRUB_DISK_SECTOR_BITS; - prot_size = grub_file_size (file) - real_size - GRUB_DISK_SECTOR_SIZE; - - grub_printf (" [Linux-%s, setup=0x%x, size=0x%x]\n", - "bzImage", real_size, prot_size); - - grub_file_seek (file, real_size + GRUB_DISK_SECTOR_SIZE); - if (grub_errno) - goto fail; - - kernel_cmdline = grub_malloc (GRUB_OFW_LINUX_CL_LENGTH); - if (! kernel_cmdline) - goto fail; - - dest = kernel_cmdline; - for (i = 1; - i < argc - && dest + grub_strlen (argv[i]) + 1 < (kernel_cmdline - + GRUB_OFW_LINUX_CL_LENGTH); - i++) - { - *dest++ = ' '; - dest = grub_stpcpy (dest, argv[i]); - } - - kernel_addr = grub_malloc (prot_size); - if (! kernel_addr) - goto fail; - - kernel_size = prot_size; - if (grub_file_read (file, kernel_addr, prot_size) != (int) prot_size) - grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file"); - - if (grub_errno == GRUB_ERR_NONE) - grub_loader_set (grub_linux_boot, grub_linux_unload, 1); - -fail: - - if (file) - grub_file_close (file); - - if (grub_errno != GRUB_ERR_NONE) - { - grub_free (kernel_cmdline); - grub_free (kernel_addr); - kernel_cmdline = 0; - kernel_addr = 0; - - grub_dl_unref (my_mod); - } - - return grub_errno; -} - -static grub_err_t -grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), - int argc, char *argv[]) -{ - grub_file_t file = 0; - - if (argc == 0) - { - grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified"); - goto fail; - } - - if (! kernel_addr) - { - grub_error (GRUB_ERR_BAD_ARGUMENT, "you need to load the kernel first"); - goto fail; - } - - file = grub_file_open (argv[0]); - if (! file) - goto fail; - - initrd_size = grub_file_size (file); - if (grub_file_read (file, (void *) GRUB_OFW_LINUX_INITRD_ADDR, - initrd_size) != (int) initrd_size) - { - grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file"); - goto fail; - } - -fail: - if (file) - grub_file_close (file); - - return grub_errno; -} - -static grub_command_t cmd_linux, cmd_initrd; - -GRUB_MOD_INIT(linux) -{ - cmd_linux = grub_register_command ("linux", grub_cmd_linux, - 0, N_("Load Linux.")); - cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd, - 0, N_("Load initrd.")); - my_mod = mod; -} - -GRUB_MOD_FINI(linux) -{ - grub_unregister_command (cmd_linux); - grub_unregister_command (cmd_initrd); -} From 0599ad1507a99820019811320961c6e1e53e293d Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 13 Jan 2010 17:28:20 +0100 Subject: [PATCH 034/178] Fix compilation on i386-ieee127 --- kern/i386/ieee1275/startup.S | 4 ---- 1 file changed, 4 deletions(-) diff --git a/kern/i386/ieee1275/startup.S b/kern/i386/ieee1275/startup.S index 35258adb6..c8e68215e 100644 --- a/kern/i386/ieee1275/startup.S +++ b/kern/i386/ieee1275/startup.S @@ -64,7 +64,3 @@ codestart: */ #include "../realmode.S" -/* - * Routines needed by Linux and Multiboot loaders. - */ -#include "../loader.S" From ad184204b25d0eae80911fe909e232acbf8a032f Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 13 Jan 2010 18:41:49 +0100 Subject: [PATCH 035/178] Remove leftover multiboot elpers --- include/grub/i386/multiboot.h | 8 -------- 1 file changed, 8 deletions(-) diff --git a/include/grub/i386/multiboot.h b/include/grub/i386/multiboot.h index a3ff3f4da..02402e93d 100644 --- a/include/grub/i386/multiboot.h +++ b/include/grub/i386/multiboot.h @@ -19,14 +19,6 @@ #ifndef GRUB_MULTIBOOT_CPU_HEADER #define GRUB_MULTIBOOT_CPU_HEADER 1 -/* The asm part of the multiboot loader. */ -void grub_multiboot_real_boot (grub_addr_t entry, - struct multiboot_info *mbi) - __attribute__ ((noreturn)); -void grub_multiboot2_real_boot (grub_addr_t entry, - struct multiboot_info *mbi) - __attribute__ ((noreturn)); - extern struct grub_relocator *grub_multiboot_relocator; extern grub_uint32_t grub_multiboot_payload_eip; From 319fc3d2138fc54b2fd9941a4746b9cd27892c84 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 13 Jan 2010 18:44:12 +0100 Subject: [PATCH 036/178] Remove became useless i386/multiboot.h --- include/grub/i386/multiboot.h | 25 ------------------------- 1 file changed, 25 deletions(-) delete mode 100644 include/grub/i386/multiboot.h diff --git a/include/grub/i386/multiboot.h b/include/grub/i386/multiboot.h deleted file mode 100644 index 02402e93d..000000000 --- a/include/grub/i386/multiboot.h +++ /dev/null @@ -1,25 +0,0 @@ -/* - * GRUB -- GRand Unified Bootloader - * Copyright (C) 2002,2003,2004,2007,2008,2009 Free Software Foundation, Inc. - * - * GRUB 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. - * - * GRUB 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 GRUB. If not, see . - */ - -#ifndef GRUB_MULTIBOOT_CPU_HEADER -#define GRUB_MULTIBOOT_CPU_HEADER 1 - -extern struct grub_relocator *grub_multiboot_relocator; -extern grub_uint32_t grub_multiboot_payload_eip; - -#endif /* ! GRUB_MULTIBOOT_CPU_HEADER */ From 4a86b371b4619abd3172adc8f2c333a04b669768 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 13 Jan 2010 20:25:08 +0100 Subject: [PATCH 037/178] Fix coreboot compilation error --- kern/i386/coreboot/startup.S | 4 ---- 1 file changed, 4 deletions(-) diff --git a/kern/i386/coreboot/startup.S b/kern/i386/coreboot/startup.S index e94950aae..93921f8ba 100644 --- a/kern/i386/coreboot/startup.S +++ b/kern/i386/coreboot/startup.S @@ -83,7 +83,3 @@ codestart: */ #include "../realmode.S" -/* - * Routines needed by Linux and Multiboot loaders. - */ -#include "../loader.S" From 5ffb1b84948d70c1a62ef0ffb7611d604de0f331 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 13 Jan 2010 20:47:05 +0100 Subject: [PATCH 038/178] Remove leftover grub_unix_real_boot --- include/grub/i386/bsd.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/include/grub/i386/bsd.h b/include/grub/i386/bsd.h index 4f5d5d1ee..b431658fe 100644 --- a/include/grub/i386/bsd.h +++ b/include/grub/i386/bsd.h @@ -254,8 +254,6 @@ struct grub_netbsd_btinfo_bootdisk int partition; }; -void grub_unix_real_boot (grub_addr_t entry, ...) - __attribute__ ((cdecl,noreturn)); grub_err_t grub_freebsd_load_elfmodule32 (struct grub_relocator *relocator, grub_file_t file, int argc, char *argv[], grub_addr_t *kern_end); From 3f995850a4b794a4462f79322e3317cb3aa1161f Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 13 Jan 2010 20:49:13 +0100 Subject: [PATCH 039/178] declare grub_multiboot_relocator in multiboot.h. --- include/grub/multiboot.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/grub/multiboot.h b/include/grub/multiboot.h index 75f39daa9..1df152056 100644 --- a/include/grub/multiboot.h +++ b/include/grub/multiboot.h @@ -32,6 +32,8 @@ #include #include +extern struct grub_relocator *grub_multiboot_relocator; + void grub_multiboot (int argc, char *argv[]); void grub_module (int argc, char *argv[]); From d3fbca98e126661bb95be7b35928d1d21cbf0395 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 13 Jan 2010 20:50:04 +0100 Subject: [PATCH 040/178] Fix type problem --- loader/i386/linux.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/loader/i386/linux.c b/loader/i386/linux.c index f7abcf094..a70798f8b 100644 --- a/loader/i386/linux.c +++ b/loader/i386/linux.c @@ -68,7 +68,11 @@ static grub_uint32_t prot_mode_pages; static grub_uint32_t initrd_pages; static struct grub_relocator *relocator = NULL; static void *efi_mmap_buf; -static grub_size_t efi_mmap_size; +#ifdef GRUB_MACHINE_EFI +static grub_efi_uintn_t efi_mmap_size; +#else +static const grub_size_t efi_mmap_size = 0; +#endif /* FIXME */ #if 0 @@ -352,8 +356,6 @@ allocate_pages (grub_size_t prot_size) #ifdef GRUB_MACHINE_EFI efi_mmap_size = find_efi_mmap_size (); -#else - efi_mmap_size = 0; #endif grub_dprintf ("linux", "real_size = %x, prot_size = %x, mmap_size = %x\n", From 3a871558844fa14a1e5c6336b989ae06757fb2fc Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 13 Jan 2010 20:51:00 +0100 Subject: [PATCH 041/178] Propagate removing of cpu/multiboot.h --- loader/i386/multiboot_mbi.c | 1 - 1 file changed, 1 deletion(-) diff --git a/loader/i386/multiboot_mbi.c b/loader/i386/multiboot_mbi.c index 4fc9a7ac1..675d4c283 100644 --- a/loader/i386/multiboot_mbi.c +++ b/loader/i386/multiboot_mbi.c @@ -22,7 +22,6 @@ #include #endif #include -#include #include #include #include From c34a120a01730144d616636266b0da634e9a65b6 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 13 Jan 2010 20:55:34 +0100 Subject: [PATCH 042/178] Move linux.mod to conf/i386.rmk --- conf/i386-coreboot.rmk | 8 +------- conf/i386-efi.rmk | 7 +------ conf/i386-ieee1275.rmk | 7 +------ conf/i386-pc.rmk | 5 ----- conf/i386.rmk | 6 ++++++ conf/x86_64-efi.rmk | 8 +------- 6 files changed, 10 insertions(+), 31 deletions(-) diff --git a/conf/i386-coreboot.rmk b/conf/i386-coreboot.rmk index 6b3d32847..ec67bb30f 100644 --- a/conf/i386-coreboot.rmk +++ b/conf/i386-coreboot.rmk @@ -102,8 +102,7 @@ bin_SCRIPTS += grub-mkrescue grub_mkrescue_SOURCES = util/grub-mkrescue.in # Modules. -pkglib_MODULES = linux.mod \ - aout.mod play.mod serial.mod \ +pkglib_MODULES = aout.mod play.mod serial.mod \ memdisk.mod pci.mod lspci.mod reboot.mod \ halt.mod datetime.mod date.mod datehook.mod \ lsmmap.mod mmap.mod @@ -120,11 +119,6 @@ mmap_mod_CFLAGS = $(COMMON_CFLAGS) mmap_mod_LDFLAGS = $(COMMON_LDFLAGS) mmap_mod_ASFLAGS = $(COMMON_ASFLAGS) -# For linux.mod. -linux_mod_SOURCES = loader/i386/linux.c -linux_mod_CFLAGS = $(COMMON_CFLAGS) -linux_mod_LDFLAGS = $(COMMON_LDFLAGS) - # For reboot.mod. reboot_mod_SOURCES = commands/reboot.c reboot_mod_CFLAGS = $(COMMON_CFLAGS) diff --git a/conf/i386-efi.rmk b/conf/i386-efi.rmk index 3846fa9f2..959cc2355 100644 --- a/conf/i386-efi.rmk +++ b/conf/i386-efi.rmk @@ -31,7 +31,7 @@ grub_install_SOURCES = util/i386/efi/grub-install.in # Modules. pkglib_MODULES = kernel.img chain.mod appleldr.mod \ - linux.mod halt.mod reboot.mod pci.mod lspci.mod \ + halt.mod reboot.mod pci.mod lspci.mod \ datetime.mod date.mod datehook.mod loadbios.mod \ fixvideo.mod mmap.mod acpi.mod @@ -93,11 +93,6 @@ appleldr_mod_SOURCES = loader/efi/appleloader.c appleldr_mod_CFLAGS = $(COMMON_CFLAGS) appleldr_mod_LDFLAGS = $(COMMON_LDFLAGS) -# For linux.mod. -linux_mod_SOURCES = loader/i386/linux.c -linux_mod_CFLAGS = $(COMMON_CFLAGS) -linux_mod_LDFLAGS = $(COMMON_LDFLAGS) - # For halt.mod. halt_mod_SOURCES = commands/halt.c halt_mod_CFLAGS = $(COMMON_CFLAGS) diff --git a/conf/i386-ieee1275.rmk b/conf/i386-ieee1275.rmk index 28d454ffd..c58547f49 100644 --- a/conf/i386-ieee1275.rmk +++ b/conf/i386-ieee1275.rmk @@ -55,7 +55,7 @@ grub_install_SOURCES = util/ieee1275/grub-install.in # Modules. pkglib_MODULES = halt.mod reboot.mod suspend.mod \ - aout.mod serial.mod linux.mod \ + aout.mod serial.mod \ nand.mod memdisk.mod pci.mod lspci.mod datetime.mod \ date.mod datehook.mod lsmmap.mod mmap.mod @@ -96,11 +96,6 @@ serial_mod_SOURCES = term/i386/pc/serial.c serial_mod_CFLAGS = $(COMMON_CFLAGS) serial_mod_LDFLAGS = $(COMMON_LDFLAGS) -# For linux.mod. -linux_mod_SOURCES = loader/i386/linux.c -linux_mod_CFLAGS = $(COMMON_CFLAGS) -linux_mod_LDFLAGS = $(COMMON_LDFLAGS) - # For nand.mod. nand_mod_SOURCES = disk/ieee1275/nand.c nand_mod_CFLAGS = $(COMMON_CFLAGS) diff --git a/conf/i386-pc.rmk b/conf/i386-pc.rmk index 6c4f64f91..dbec54398 100644 --- a/conf/i386-pc.rmk +++ b/conf/i386-pc.rmk @@ -174,11 +174,6 @@ linux16_mod_SOURCES = loader/i386/pc/linux.c linux16_mod_CFLAGS = $(COMMON_CFLAGS) linux16_mod_LDFLAGS = $(COMMON_LDFLAGS) -pkglib_MODULES += linux.mod -linux_mod_SOURCES = loader/i386/linux.c -linux_mod_CFLAGS = $(COMMON_CFLAGS) -linux_mod_LDFLAGS = $(COMMON_LDFLAGS) - pkglib_MODULES += xnu.mod xnu_mod_SOURCES = loader/xnu_resume.c loader/i386/xnu.c \ loader/macho32.c loader/macho64.c loader/macho.c loader/xnu.c diff --git a/conf/i386.rmk b/conf/i386.rmk index d7417f444..bf6d7425b 100644 --- a/conf/i386.rmk +++ b/conf/i386.rmk @@ -27,3 +27,9 @@ pkglib_MODULES += ata.mod ata_mod_SOURCES = disk/ata.c ata_mod_CFLAGS = $(COMMON_CFLAGS) ata_mod_LDFLAGS = $(COMMON_LDFLAGS) + +# For linux.mod. +pkglib_MODULES += linux.mod +linux_mod_SOURCES = loader/i386/linux.c +linux_mod_CFLAGS = $(COMMON_CFLAGS) +linux_mod_LDFLAGS = $(COMMON_LDFLAGS) diff --git a/conf/x86_64-efi.rmk b/conf/x86_64-efi.rmk index 0ee09655f..6db4c9569 100644 --- a/conf/x86_64-efi.rmk +++ b/conf/x86_64-efi.rmk @@ -30,7 +30,7 @@ grub_install_SOURCES = util/i386/efi/grub-install.in # Modules. pkglib_MODULES = kernel.img chain.mod appleldr.mod \ - halt.mod reboot.mod linux.mod pci.mod lspci.mod \ + halt.mod reboot.mod pci.mod lspci.mod \ datetime.mod date.mod datehook.mod loadbios.mod \ fixvideo.mod mmap.mod acpi.mod ata.mod @@ -93,12 +93,6 @@ appleldr_mod_SOURCES = loader/efi/appleloader.c appleldr_mod_CFLAGS = $(COMMON_CFLAGS) appleldr_mod_LDFLAGS = $(COMMON_LDFLAGS) -# For linux.mod. -linux_mod_SOURCES = loader/i386/linux.c -linux_mod_CFLAGS = $(COMMON_CFLAGS) -linux_mod_ASFLAGS = $(COMMON_ASFLAGS) -linux_mod_LDFLAGS = $(COMMON_LDFLAGS) - # For halt.mod. halt_mod_SOURCES = commands/halt.c halt_mod_CFLAGS = $(COMMON_CFLAGS) From 021f5a22157cf9773f570802bd0ab1975e914a41 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 13 Jan 2010 20:56:57 +0100 Subject: [PATCH 043/178] Remove leftovers in multiboot.c --- loader/i386/multiboot.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/loader/i386/multiboot.c b/loader/i386/multiboot.c index be11fe20b..f7bf2a774 100644 --- a/loader/i386/multiboot.c +++ b/loader/i386/multiboot.c @@ -32,9 +32,7 @@ #include #include -#include #include -#include #include #include #include @@ -48,7 +46,7 @@ extern grub_dl_t my_mod; struct grub_relocator *grub_multiboot_relocator = NULL; -grub_uint32_t grub_multiboot_payload_eip; +static grub_uint32_t grub_multiboot_payload_eip; static grub_err_t grub_multiboot_boot (void) From c30074e344b6a2e91a9d24eb2afe976a462779ad Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 13 Jan 2010 21:00:46 +0100 Subject: [PATCH 044/178] Cleanup in bsd loaders --- loader/i386/bsd.c | 16 ++++++++-------- loader/i386/bsdXX.c | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/loader/i386/bsd.c b/loader/i386/bsd.c index 9c42f6a5c..f2ac4043d 100644 --- a/loader/i386/bsd.c +++ b/loader/i386/bsd.c @@ -17,9 +17,8 @@ */ #include -#include +#include #include -#include #include #include #include @@ -42,7 +41,7 @@ #include #include #include -#include +#include #define ALIGN_DWORD(a) ALIGN_UP (a, 4) #define ALIGN_QWORD(a) ALIGN_UP (a, 8) @@ -313,7 +312,7 @@ grub_freebsd_add_mmap (void) (unsigned long long) mmap_buf[i].addr, (unsigned long long) mmap_buf[i].size); - grub_dprintf ("bsd", "%d entries in smap\n", mmap - mmap_buf); + grub_dprintf ("bsd", "%ld entries in smap\n", (long) (mmap - mmap_buf)); grub_freebsd_add_meta (FREEBSD_MODINFO_METADATA | FREEBSD_MODINFOMD_SMAP, mmap_buf, len); @@ -413,9 +412,9 @@ grub_freebsd_list_modules (void) break; case FREEBSD_MODINFO_ADDR: { - grub_addr_t addr; + grub_uint32_t addr; - addr = *((grub_addr_t *) (mod_buf + pos)); + addr = *((grub_uint32_t *) (mod_buf + pos)); grub_printf (" 0x%08x", addr); break; } @@ -579,6 +578,7 @@ grub_freebsd_boot (void) GRUB_RELOCATOR_PREFERENCE_NONE); if (err) return err; + grub_memcpy (&stack[8], &bi, sizeof (bi)); state.eip = entry; state.esp = stack_target; @@ -1032,8 +1032,8 @@ grub_bsd_load_elf (grub_elf_t elf) if (err) return err; - grub_dprintf ("bsd", "kern_start = %x, kern_end = %x\n", kern_start, - kern_end); + grub_dprintf ("bsd", "kern_start = %lx, kern_end = %lx\n", + (unsigned long) kern_start, (unsigned long) kern_end); err = grub_relocator_alloc_chunk_addr (relocator, &kern_chunk_src, kern_start, kern_end - kern_start); if (err) diff --git a/loader/i386/bsdXX.c b/loader/i386/bsdXX.c index ad6c1bc75..41dfe89e4 100644 --- a/loader/i386/bsdXX.c +++ b/loader/i386/bsdXX.c @@ -1,9 +1,9 @@ #include -#include +#include #include #include #include -#include +#include #define ALIGN_PAGE(a) ALIGN_UP (a, 4096) From 8874cbbded80526db0e0049b1bd0f7a9a252e06a Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 13 Jan 2010 21:04:19 +0100 Subject: [PATCH 045/178] Initial multi-i386 support for *BSD --- conf/i386-coreboot.rmk | 14 +------------- conf/i386-pc.rmk | 13 +------------ conf/i386.rmk | 13 +++++++++++++ loader/i386/bsd.c | 15 +++++++++++++++ 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/conf/i386-coreboot.rmk b/conf/i386-coreboot.rmk index ec67bb30f..6ed6c54ac 100644 --- a/conf/i386-coreboot.rmk +++ b/conf/i386-coreboot.rmk @@ -102,7 +102,7 @@ bin_SCRIPTS += grub-mkrescue grub_mkrescue_SOURCES = util/grub-mkrescue.in # Modules. -pkglib_MODULES = aout.mod play.mod serial.mod \ +pkglib_MODULES = play.mod serial.mod \ memdisk.mod pci.mod lspci.mod reboot.mod \ halt.mod datetime.mod date.mod datehook.mod \ lsmmap.mod mmap.mod @@ -150,18 +150,6 @@ multiboot2_mod_CFLAGS = $(COMMON_CFLAGS) -DGRUB_USE_MULTIBOOT2 multiboot2_mod_LDFLAGS = $(COMMON_LDFLAGS) multiboot2_mod_ASFLAGS = $(COMMON_ASFLAGS) -# For aout.mod. -aout_mod_SOURCES = loader/aout.c -aout_mod_CFLAGS = $(COMMON_CFLAGS) -aout_mod_LDFLAGS = $(COMMON_LDFLAGS) - -# For bsd.mod -pkglib_MODULES += bsd.mod -bsd_mod_SOURCES = loader/i386/bsd.c loader/i386/bsd32.c loader/i386/bsd64.c -bsd_mod_CFLAGS = $(COMMON_CFLAGS) -bsd_mod_LDFLAGS = $(COMMON_LDFLAGS) -bsd_mod_ASFLAGS = $(COMMON_ASFLAGS) - # For play.mod. play_mod_SOURCES = commands/i386/pc/play.c play_mod_CFLAGS = $(COMMON_CFLAGS) diff --git a/conf/i386-pc.rmk b/conf/i386-pc.rmk index dbec54398..0a90bd746 100644 --- a/conf/i386-pc.rmk +++ b/conf/i386-pc.rmk @@ -119,7 +119,7 @@ pkglib_MODULES = biosdisk.mod chain.mod \ reboot.mod halt.mod \ vbe.mod vbetest.mod vbeinfo.mod play.mod serial.mod \ vga.mod memdisk.mod pci.mod lspci.mod \ - aout.mod bsd.mod pxe.mod pxecmd.mod datetime.mod date.mod \ + pxe.mod pxecmd.mod datetime.mod date.mod \ datehook.mod lsmmap.mod ata_pthru.mod hdparm.mod \ usb.mod uhci.mod ohci.mod usbtest.mod usbms.mod usb_keyboard.mod \ efiemu.mod mmap.mod acpi.mod drivemap.mod @@ -253,17 +253,6 @@ lspci_mod_SOURCES = commands/lspci.c lspci_mod_CFLAGS = $(COMMON_CFLAGS) lspci_mod_LDFLAGS = $(COMMON_LDFLAGS) -# For aout.mod -aout_mod_SOURCES = loader/aout.c -aout_mod_CFLAGS = $(COMMON_CFLAGS) -aout_mod_LDFLAGS = $(COMMON_LDFLAGS) - -# For bsd.mod -bsd_mod_SOURCES = loader/i386/bsd.c loader/i386/bsd32.c loader/i386/bsd64.c -bsd_mod_CFLAGS = $(COMMON_CFLAGS) -bsd_mod_LDFLAGS = $(COMMON_LDFLAGS) -bsd_mod_ASFLAGS = $(COMMON_ASFLAGS) - # For usb.mod usb_mod_SOURCES = bus/usb/usb.c bus/usb/usbtrans.c bus/usb/usbhub.c usb_mod_CFLAGS = $(COMMON_CFLAGS) diff --git a/conf/i386.rmk b/conf/i386.rmk index bf6d7425b..cebe67eaf 100644 --- a/conf/i386.rmk +++ b/conf/i386.rmk @@ -33,3 +33,16 @@ pkglib_MODULES += linux.mod linux_mod_SOURCES = loader/i386/linux.c linux_mod_CFLAGS = $(COMMON_CFLAGS) linux_mod_LDFLAGS = $(COMMON_LDFLAGS) + +# For aout.mod +pkglib_MODULES += aout.mod +aout_mod_SOURCES = loader/aout.c +aout_mod_CFLAGS = $(COMMON_CFLAGS) +aout_mod_LDFLAGS = $(COMMON_LDFLAGS) + +# For bsd.mod +pkglib_MODULES += bsd.mod +bsd_mod_SOURCES = loader/i386/bsd.c loader/i386/bsd32.c loader/i386/bsd64.c +bsd_mod_CFLAGS = $(COMMON_CFLAGS) +bsd_mod_LDFLAGS = $(COMMON_LDFLAGS) +bsd_mod_ASFLAGS = $(COMMON_ASFLAGS) diff --git a/loader/i386/bsd.c b/loader/i386/bsd.c index f2ac4043d..801eb9dfd 100644 --- a/loader/i386/bsd.c +++ b/loader/i386/bsd.c @@ -37,6 +37,9 @@ #ifdef GRUB_MACHINE_PCBIOS #include #endif +#ifdef GRUB_MACHINE_EFI +#include +#endif #include #include #include @@ -553,6 +556,9 @@ grub_freebsd_boot (void) if (err) return err; + if (! grub_efi_finish_boot_services ()) + grub_fatal ("cannot exit boot services"); + pagetable = p - (4096 * 3); fill_bsd64_pagetable (pagetable, (pagetable - p0) + p_target); @@ -579,6 +585,9 @@ grub_freebsd_boot (void) if (err) return err; + if (! grub_efi_finish_boot_services ()) + grub_fatal ("cannot exit boot services"); + grub_memcpy (&stack[8], &bi, sizeof (bi)); state.eip = entry; state.esp = stack_target; @@ -684,6 +693,9 @@ grub_openbsd_boot (void) buf = (grub_uint8_t *) pa; argbuf_target_end = buf - buf0 + buf_target; + if (! grub_efi_finish_boot_services ()) + grub_fatal ("cannot exit boot services"); + state.eip = entry; state.esp = ((grub_uint8_t *) stack - buf0) + buf_target; stack[0] = entry; @@ -804,6 +816,9 @@ grub_netbsd_boot (void) if (err) return err; + if (! grub_efi_finish_boot_services ()) + grub_fatal ("cannot exit boot services"); + state.eip = entry; state.esp = stack_target; stack[0] = entry; From 0c31c22bda4662340cbaf46c66a89213319d84e4 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 13 Jan 2010 21:06:49 +0100 Subject: [PATCH 046/178] Remove leftover aout.mod --- conf/i386-ieee1275.rmk | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/conf/i386-ieee1275.rmk b/conf/i386-ieee1275.rmk index c58547f49..4d652a504 100644 --- a/conf/i386-ieee1275.rmk +++ b/conf/i386-ieee1275.rmk @@ -54,8 +54,7 @@ sbin_SCRIPTS = grub-install grub_install_SOURCES = util/ieee1275/grub-install.in # Modules. -pkglib_MODULES = halt.mod reboot.mod suspend.mod \ - aout.mod serial.mod \ +pkglib_MODULES = halt.mod reboot.mod suspend.mod serial.mod \ nand.mod memdisk.mod pci.mod lspci.mod datetime.mod \ date.mod datehook.mod lsmmap.mod mmap.mod @@ -71,11 +70,6 @@ mmap_mod_CFLAGS = $(COMMON_CFLAGS) mmap_mod_LDFLAGS = $(COMMON_LDFLAGS) mmap_mod_ASFLAGS = $(COMMON_ASFLAGS) -# For aout.mod. -aout_mod_SOURCES = loader/aout.c -aout_mod_CFLAGS = $(COMMON_CFLAGS) -aout_mod_LDFLAGS = $(COMMON_LDFLAGS) - # For suspend.mod suspend_mod_SOURCES = commands/ieee1275/suspend.c suspend_mod_CFLAGS = $(COMMON_CFLAGS) From 6d8ebf76c412080434c47258a50475249f9f4faf Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 13 Jan 2010 23:17:12 +0100 Subject: [PATCH 047/178] support relocator64 from x86_64 mode --- lib/i386/relocator64.S | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/lib/i386/relocator64.S b/lib/i386/relocator64.S index 05627fb90..37a77b3b5 100644 --- a/lib/i386/relocator64.S +++ b/lib/i386/relocator64.S @@ -43,13 +43,13 @@ LOCAL(base): add $(LOCAL(cont0) - LOCAL(base)), RAX jmp *RAX LOCAL(cont0): +#ifndef __x86_64__ lea (LOCAL(cont1) - LOCAL(base)) (RSI, 1), RAX mov RAX, (LOCAL(jump_vector) - LOCAL(base)) (RSI, 1) lea (LOCAL(gdt) - LOCAL(base)) (RSI, 1), RAX mov RAX, (LOCAL(gdt_addr) - LOCAL(base)) (RSI, 1) -#ifndef __x86_64__ /* Disable paging. */ movl %cr0, %eax andl $(~GRUB_MEMORY_CPU_CR0_PAGING_ON), %eax @@ -76,6 +76,12 @@ VARIABLE(grub_relocator64_cr3) movl %cr0, %eax orl $GRUB_MEMORY_CPU_CR0_PAGING_ON, %eax movl %eax, %cr0 + + /* Load GDT. */ + lgdt (LOCAL(gdtdesc) - LOCAL(base)) (RSI, 1) + + /* Update %cs. */ + ljmp *(LOCAL(jump_vector) - LOCAL(base)) (RSI, 1) #else /* mov imm64, %rax */ .byte 0x48 @@ -84,11 +90,6 @@ VARIABLE(grub_relocator64_cr3) .quad 0 movq %rax, %cr3 #endif - /* Load GDT. */ - lgdt (LOCAL(gdtdesc) - LOCAL(base)) (RSI, 1) - - /* Update %cs. */ - ljmp *(LOCAL(jump_vector) - LOCAL(base)) (RSI, 1) LOCAL(cont1): .code64 @@ -143,6 +144,7 @@ LOCAL(jump_addr): VARIABLE(grub_relocator64_rip) .quad 0 +#ifndef __x86_64__ .p2align 4 LOCAL(gdt): /* NULL. */ @@ -185,22 +187,14 @@ LOCAL(gdt): LOCAL(gdtdesc): .word 0x20 LOCAL(gdt_addr): -#ifdef __x86_64__ - /* Filled by the code. */ - .quad 0 -#else /* Filled by the code. */ .long 0 -#endif .p2align 4 LOCAL(jump_vector): /* Jump location. Is filled by the code */ -#ifdef __x86_64__ - .quad 0 -#else .long 0 -#endif .long CODE64_SEGMENT +#endif VARIABLE(grub_relocator64_end) From ea96d34596f92abfe7f52f4c24f74c0cea043aa1 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Thu, 14 Jan 2010 13:40:17 +0100 Subject: [PATCH 048/178] Clarify type of bi_kernelname and bi_nfs_diskless --- include/grub/i386/bsd.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/grub/i386/bsd.h b/include/grub/i386/bsd.h index b431658fe..f68539a3c 100644 --- a/include/grub/i386/bsd.h +++ b/include/grub/i386/bsd.h @@ -97,8 +97,8 @@ enum bsd_kernel_types struct grub_freebsd_bootinfo { grub_uint32_t bi_version; - grub_uint8_t *bi_kernelname; - struct nfs_diskless *bi_nfs_diskless; + grub_uint32_t bi_kernelname; + grub_uint32_t bi_nfs_diskless; grub_uint32_t bi_n_bios_used; grub_uint32_t bi_bios_geom[FREEBSD_N_BIOS_GEOM]; grub_uint32_t bi_size; From 6a42fe54db23e792738a931f5d4960ecab46a478 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Thu, 14 Jan 2010 19:12:24 +0100 Subject: [PATCH 049/178] Add missing #ifdef --- loader/i386/bsd.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/loader/i386/bsd.c b/loader/i386/bsd.c index 801eb9dfd..58af4eb7d 100644 --- a/loader/i386/bsd.c +++ b/loader/i386/bsd.c @@ -556,8 +556,10 @@ grub_freebsd_boot (void) if (err) return err; +#ifdef GRUB_MACHINE_EFI if (! grub_efi_finish_boot_services ()) grub_fatal ("cannot exit boot services"); +#endif pagetable = p - (4096 * 3); fill_bsd64_pagetable (pagetable, (pagetable - p0) + p_target); @@ -585,8 +587,10 @@ grub_freebsd_boot (void) if (err) return err; +#ifdef GRUB_MACHINE_EFI if (! grub_efi_finish_boot_services ()) grub_fatal ("cannot exit boot services"); +#endif grub_memcpy (&stack[8], &bi, sizeof (bi)); state.eip = entry; @@ -693,8 +697,10 @@ grub_openbsd_boot (void) buf = (grub_uint8_t *) pa; argbuf_target_end = buf - buf0 + buf_target; +#ifdef GRUB_MACHINE_EFI if (! grub_efi_finish_boot_services ()) grub_fatal ("cannot exit boot services"); +#endif state.eip = entry; state.esp = ((grub_uint8_t *) stack - buf0) + buf_target; @@ -816,8 +822,10 @@ grub_netbsd_boot (void) if (err) return err; +#ifdef GRUB_MACHINE_EFI if (! grub_efi_finish_boot_services ()) grub_fatal ("cannot exit boot services"); +#endif state.eip = entry; state.esp = stack_target; From 4a04699258764201f3f5dffbd974740c4e4e5603 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Thu, 14 Jan 2010 19:14:04 +0100 Subject: [PATCH 050/178] Fix a mistake with size calculation --- lib/relocator.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/relocator.c b/lib/relocator.c index 090a6c7e3..b7fe404c3 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -308,12 +308,12 @@ malloc_in_range (struct grub_relocator *rel, if (best_addr - (grub_addr_t) hb >= sizeof (*hb)) { - hb->size = (best_addr - (grub_addr_t) hb) >> GRUB_MM_ALIGN_LOG2; + hb->size = ((best_addr - (grub_addr_t) hb) >> GRUB_MM_ALIGN_LOG2) - 1; if (foll) { foll->next = hb; hbp->next = foll; - if (rb->first == hbp) + if (rb->first == hb) rb->first = foll; } } From 865a0f8aa7dfbc98d40cc3bda9bc816e2d1f5e3f Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Thu, 14 Jan 2010 19:14:24 +0100 Subject: [PATCH 051/178] elf symbols --- include/grub/multiboot.h | 3 ++ loader/i386/multiboot_elfxx.c | 61 +++++++++++++++++++++++++++++++++++ loader/i386/multiboot_mbi.c | 32 +++++++++++++++++- 3 files changed, 95 insertions(+), 1 deletion(-) diff --git a/include/grub/multiboot.h b/include/grub/multiboot.h index 1df152056..830490170 100644 --- a/include/grub/multiboot.h +++ b/include/grub/multiboot.h @@ -43,6 +43,9 @@ grub_err_t grub_multiboot_init_mbi (int argc, char *argv[]); grub_err_t grub_multiboot_add_module (grub_addr_t start, grub_size_t size, int argc, char *argv[]); void grub_multiboot_set_bootdev (void); +void +grub_multiboot_add_elfsyms (grub_size_t num, grub_size_t entsize, + unsigned shndx, void *data); #endif /* ! GRUB_MULTIBOOT_HEADER */ diff --git a/loader/i386/multiboot_elfxx.c b/loader/i386/multiboot_elfxx.c index 155de5801..d996835a5 100644 --- a/loader/i386/multiboot_elfxx.c +++ b/loader/i386/multiboot_elfxx.c @@ -129,6 +129,67 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, void *buffer) if (i == ehdr->e_phnum) return grub_error (GRUB_ERR_BAD_OS, "entry point isn't in a segment"); + if (ehdr->e_shnum) + { + grub_uint8_t *shdr, *shdrptr; + + shdr = grub_malloc (ehdr->e_shnum * ehdr->e_shentsize); + if (!shdr) + return grub_errno; + + if (grub_file_seek (file, ehdr->e_shoff) == (grub_off_t) -1) + return grub_error (GRUB_ERR_BAD_OS, + "invalid offset to section headers"); + + if (grub_file_read (file, shdr, ehdr->e_shnum * ehdr->e_shentsize) + != (grub_ssize_t) ehdr->e_shnum * ehdr->e_shentsize) + return grub_error (GRUB_ERR_BAD_OS, + "couldn't read sections headers from file"); + + for (shdrptr = shdr, i = 0; i < ehdr->e_shnum; + shdrptr += ehdr->e_shentsize, i++) + { + Elf_Shdr *sh = (Elf_Shdr *) shdrptr; + void *src; + grub_addr_t target; + grub_err_t err; + + /* This section is a loaded section, + so we don't care. */ + if (sh->sh_addr != 0) + continue; + + /* This section is empty, so we don't care. */ + if (sh->sh_size == 0) + continue; + + err + = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, + &src, &target, 0, + (0xffffffff - sh->sh_size) + 1, + sh->sh_size, + sh->sh_addralign, + GRUB_RELOCATOR_PREFERENCE_NONE); + if (err) + { + grub_dprintf ("multiboot_loader", "Error loading shdr %d\n", i); + return err; + } + + if (grub_file_seek (file, sh->sh_offset) == (grub_off_t) -1) + return grub_error (GRUB_ERR_BAD_OS, + "invalid offset in section header"); + + if (grub_file_read (file, src, sh->sh_size) + != (grub_ssize_t) sh->sh_size) + return grub_error (GRUB_ERR_BAD_OS, + "couldn't read segment from file"); + sh->sh_addr = target; + } + grub_multiboot_add_elfsyms (ehdr->e_shnum, ehdr->e_shentsize, + ehdr->e_shstrndx, shdr); + } + #undef phdr return grub_errno; diff --git a/loader/i386/multiboot_mbi.c b/loader/i386/multiboot_mbi.c index 675d4c283..dbc8dcdfd 100644 --- a/loader/i386/multiboot_mbi.c +++ b/loader/i386/multiboot_mbi.c @@ -46,6 +46,9 @@ static unsigned modcnt; static char *cmdline = NULL; static grub_uint32_t bootdev; static int bootdev_set; +static grub_size_t elf_sec_num, elf_sec_entsize; +static unsigned elf_sec_shstrndx; +static void *elf_sections; /* Return the length of the Multiboot mmap that will be needed to allocate our platform's map. */ @@ -73,7 +76,8 @@ grub_multiboot_get_mbi_size (void) { return sizeof (struct multiboot_info) + ALIGN_UP (cmdline_size, 4) + modcnt * sizeof (struct multiboot_mod_list) + total_modcmd - + ALIGN_UP (sizeof(PACKAGE_STRING), 4) + grub_get_multiboot_mmap_len (); + + ALIGN_UP (sizeof(PACKAGE_STRING), 4) + grub_get_multiboot_mmap_len () + + elf_sec_entsize * elf_sec_num; } /* Fill previously allocated Multiboot mmap. */ @@ -192,9 +196,30 @@ grub_multiboot_make_mbi (grub_uint32_t *target) mbi->flags |= MULTIBOOT_INFO_BOOTDEV; } + if (elf_sec_num) + { + mbi->u.elf_sec.addr = ptrdest; + grub_memcpy (ptrorig, elf_sections, elf_sec_entsize * elf_sec_num); + mbi->u.elf_sec.num = elf_sec_num; + mbi->u.elf_sec.size = elf_sec_entsize; + mbi->u.elf_sec.shndx = elf_sec_shstrndx; + + mbi->flags |= MULTIBOOT_INFO_ELF_SHDR; + } + return GRUB_ERR_NONE; } +void +grub_multiboot_add_elfsyms (grub_size_t num, grub_size_t entsize, + unsigned shndx, void *data) +{ + elf_sec_num = num; + elf_sec_shstrndx = shndx; + elf_sec_entsize = entsize; + elf_sections = data; +} + void grub_multiboot_free_mbi (void) { @@ -215,6 +240,11 @@ grub_multiboot_free_mbi (void) } modules = NULL; modules_last = NULL; + + grub_free (elf_sections); + elf_sections = NULL; + elf_sec_entsize = 0; + elf_sec_num = 0; } grub_err_t From 9205ac07e3a1a8c4214298f4a6507c6a5a8f549d Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Thu, 14 Jan 2010 22:06:26 +0100 Subject: [PATCH 052/178] Fix off-by-one error --- lib/relocator.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/relocator.c b/lib/relocator.c index b7fe404c3..3e93abbaa 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -161,7 +161,7 @@ get_best_header (struct grub_relocator *rel, { grub_addr_t allowable_start, allowable_end; allowable_start = (grub_addr_t) h; - allowable_end = (grub_addr_t) (h + 1 + h->size); + allowable_end = (grub_addr_t) (h + h->size); try_addr (allowable_start, allowable_end); @@ -308,7 +308,7 @@ malloc_in_range (struct grub_relocator *rel, if (best_addr - (grub_addr_t) hb >= sizeof (*hb)) { - hb->size = ((best_addr - (grub_addr_t) hb) >> GRUB_MM_ALIGN_LOG2) - 1; + hb->size = ((best_addr - (grub_addr_t) hb) >> GRUB_MM_ALIGN_LOG2); if (foll) { foll->next = hb; From def6307401695e742fdf6eaf7b894c696684cd32 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Fri, 15 Jan 2010 11:34:22 +0100 Subject: [PATCH 053/178] Be paranoid in relocator allocations --- lib/relocator.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/relocator.c b/lib/relocator.c index 3e93abbaa..0064824a4 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -163,6 +163,9 @@ get_best_header (struct grub_relocator *rel, allowable_start = (grub_addr_t) h; allowable_end = (grub_addr_t) (h + h->size); + if (h->magic != GRUB_MM_FREE_MAGIC) + grub_fatal ("free magic is broken at %p: 0x%x", h, h->magic); + try_addr (allowable_start, allowable_end); if ((grub_addr_t) h == (grub_addr_t) (rb + 1)) @@ -299,7 +302,8 @@ malloc_in_range (struct grub_relocator *rel, { struct grub_mm_header *foll = NULL; - if (best_addr + size <= (grub_addr_t) (hb + hb->size)) + if (ALIGN_UP (best_addr + size, GRUB_MM_ALIGN) + GRUB_MM_ALIGN + <= (grub_addr_t) (hb + hb->size)) { foll = (void *) ALIGN_UP (best_addr + size, GRUB_MM_ALIGN); foll->magic = GRUB_MM_FREE_MAGIC; @@ -324,11 +328,11 @@ malloc_in_range (struct grub_relocator *rel, else foll = hb->next; + hbp->next = foll; if (rb->first == hb) rb->first = foll; if (rb->first == hb) rb->first = (void *) (rb + 1); - hbp->next = foll; } *res = best_addr; return 1; From 2386d586b95df9553a64a1cd7a99cc0638bc0a52 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Fri, 15 Jan 2010 11:39:05 +0100 Subject: [PATCH 054/178] Merge some knetbsdcode into kfreebsd one. Serial supoort for knetbsd --- include/grub/i386/bsd.h | 54 ++-- loader/i386/bsd.c | 527 ++++++++++++++++++++++------------------ loader/i386/bsdXX.c | 38 +-- 3 files changed, 330 insertions(+), 289 deletions(-) diff --git a/include/grub/i386/bsd.h b/include/grub/i386/bsd.h index f68539a3c..aa19b2338 100644 --- a/include/grub/i386/bsd.h +++ b/include/grub/i386/bsd.h @@ -114,6 +114,12 @@ struct grub_freebsd_bootinfo grub_uint32_t bi_modulep; } __attribute__ ((packed)); +struct freebsd_tag_header +{ + grub_uint32_t type; + grub_uint32_t len; +}; + #define OPENBSD_RB_ASKNAME (1 << 0) /* ask for file name to reboot from */ #define OPENBSD_RB_SINGLE (1 << 1) /* reboot to single user only */ #define OPENBSD_RB_NOSYNC (1 << 2) /* dont sync before reboot */ @@ -198,48 +204,21 @@ struct grub_openbsd_bootargs struct grub_netbsd_bootinfo { grub_uint32_t bi_count; - grub_addr_t bi_data[1]; + grub_addr_t bi_data[0]; }; #define NETBSD_BTINFO_BOOTPATH 0 #define NETBSD_BTINFO_ROOTDEVICE 1 -#define NETBSD_BTINFO_BOOTDISK 3 +#define NETBSD_BTINFO_CONSOLE 6 #define NETBSD_BTINFO_MEMMAP 9 struct grub_netbsd_btinfo_common { - int len; - int type; -}; - -struct grub_netbsd_btinfo_mmap_header -{ - struct grub_netbsd_btinfo_common common; - grub_uint32_t count; -}; - -struct grub_netbsd_btinfo_mmap_entry -{ - grub_uint64_t addr; - grub_uint64_t len; -#define NETBSD_MMAP_AVAILABLE 1 -#define NETBSD_MMAP_RESERVED 2 -#define NETBSD_MMAP_ACPI 3 -#define NETBSD_MMAP_NVS 4 + grub_uint32_t len; grub_uint32_t type; }; -struct grub_netbsd_btinfo_bootpath -{ - struct grub_netbsd_btinfo_common common; - char bootpath[80]; -}; - -struct grub_netbsd_btinfo_rootdevice -{ - struct grub_netbsd_btinfo_common common; - char devname[16]; -}; +#define GRUB_NETBSD_MAX_BOOTPATH_LEN 80 struct grub_netbsd_btinfo_bootdisk { @@ -254,6 +233,15 @@ struct grub_netbsd_btinfo_bootdisk int partition; }; +struct grub_netbsd_btinfo_serial +{ + char devname[16]; + grub_uint32_t addr; + grub_uint32_t speed; +}; + +#define GRUB_NETBSD_MAX_ROOTDEVICE_LEN 16 + grub_err_t grub_freebsd_load_elfmodule32 (struct grub_relocator *relocator, grub_file_t file, int argc, char *argv[], grub_addr_t *kern_end); @@ -268,8 +256,8 @@ grub_err_t grub_freebsd_load_elf_meta64 (struct grub_relocator *relocator, grub_file_t file, grub_addr_t *kern_end); -grub_err_t grub_freebsd_add_meta (grub_uint32_t type, void *data, - grub_uint32_t len); +grub_err_t grub_bsd_add_meta (grub_uint32_t type, + void *data, grub_uint32_t len); grub_err_t grub_freebsd_add_meta_module (char *filename, char *type, int argc, char **argv, grub_addr_t addr, grub_uint32_t size); diff --git a/loader/i386/bsd.c b/loader/i386/bsd.c index 58af4eb7d..3565884d5 100644 --- a/loader/i386/bsd.c +++ b/loader/i386/bsd.c @@ -51,20 +51,29 @@ #define ALIGN_VAR(a) ((is_64bit) ? (ALIGN_QWORD(a)) : (ALIGN_DWORD(a))) #define ALIGN_PAGE(a) ALIGN_UP (a, 4096) -#define MOD_BUF_ALLOC_UNIT 4096 - static int kernel_type = KERNEL_TYPE_NONE; static grub_dl_t my_mod; static grub_addr_t entry, entry_hi, kern_start, kern_end; static void *kern_chunk_src; static grub_uint32_t bootflags; -static char *mod_buf; -static grub_uint32_t mod_buf_len, mod_buf_max, kern_end_mdofs; static int is_elf_kernel, is_64bit; -static char *netbsd_root = NULL; static grub_uint32_t openbsd_root; struct grub_relocator *relocator = NULL; +struct bsd_tag +{ + struct bsd_tag *next; + grub_size_t len; + grub_uint32_t type; + union { + grub_uint8_t a; + grub_uint16_t b; + grub_uint32_t c; + } data[0]; +}; + +struct bsd_tag *tags, *tags_last; + static const struct grub_arg_option freebsd_opts[] = { {"dual", 'D', 0, N_("Display output on all consoles."), 0, 0}, @@ -127,6 +136,8 @@ static const struct grub_arg_option netbsd_opts[] = {"debug", 'x', 0, N_("Boot with debug messages."), 0, 0}, {"silent", 'z', 0, N_("Supress normal output (warnings remain)."), 0, 0}, {"root", 'r', 0, N_("Set root device."), N_("DEVICE"), ARG_TYPE_STRING}, + {"serial", 'h', GRUB_ARG_OPTION_OPTIONAL, + N_("Use serial console."), N_("ADDR,SPEED"), ARG_TYPE_STRING}, {0, 0, 0, 0, 0, 0} }; @@ -139,6 +150,7 @@ static const grub_uint32_t netbsd_flags[] = }; #define NETBSD_ROOT_ARG (ARRAY_SIZE (netbsd_flags) - 1) +#define NETBSD_SERIAL_ARG (ARRAY_SIZE (netbsd_flags)) static void grub_bsd_get_device (grub_uint32_t * biosdev, @@ -180,36 +192,23 @@ grub_bsd_get_device (grub_uint32_t * biosdev, } grub_err_t -grub_freebsd_add_meta (grub_uint32_t type, void *data, grub_uint32_t len) +grub_bsd_add_meta (grub_uint32_t type, void *data, grub_uint32_t len) { - if (mod_buf_max < mod_buf_len + len + 8) - { - char *new_buf; - - do - { - mod_buf_max += MOD_BUF_ALLOC_UNIT; - } - while (mod_buf_max < mod_buf_len + len + 8); - - new_buf = grub_malloc (mod_buf_max); - if (!new_buf) - return grub_errno; - - grub_memcpy (new_buf, mod_buf, mod_buf_len); - grub_free (mod_buf); - - mod_buf = new_buf; - } - - *((grub_uint32_t *) (mod_buf + mod_buf_len)) = type; - *((grub_uint32_t *) (mod_buf + mod_buf_len + 4)) = len; - mod_buf_len += 8; + struct bsd_tag *newtag; + newtag = grub_malloc (len + sizeof (struct bsd_tag)); + if (!newtag) + return grub_errno; + newtag->len = len; + newtag->type = type; + newtag->next = NULL; if (len) - grub_memcpy (mod_buf + mod_buf_len, data, len); - - mod_buf_len = ALIGN_VAR (mod_buf_len + len); + grub_memcpy (newtag->data, data, len); + if (tags_last) + tags_last->next = newtag; + else + tags = newtag; + tags_last = newtag; return GRUB_ERR_NONE; } @@ -226,13 +225,13 @@ struct grub_e820_mmap #define GRUB_E820_NVS 4 #define GRUB_E820_EXEC_CODE 5 -static grub_err_t -grub_freebsd_add_mmap (void) +static void +generate_e820_mmap (grub_size_t *len, grub_size_t *cnt, void *buf) { - grub_size_t len = 0; - struct grub_e820_mmap *mmap_buf = 0; - struct grub_e820_mmap *mmap = 0; + int count = 0; int isfirstrun = 1; + struct grub_e820_mmap *mmap = buf; + struct grub_e820_mmap prev, cur; auto int NESTED_FUNC_ATTR hook (grub_uint64_t, grub_uint64_t, grub_uint32_t); int NESTED_FUNC_ATTR hook (grub_uint64_t addr, grub_uint64_t size, @@ -240,86 +239,110 @@ grub_freebsd_add_mmap (void) { /* FreeBSD assumes that first 64KiB are available. Not always true but try to prevent panic somehow. */ - if (isfirstrun && addr != 0) + if (kernel_type == KERNEL_TYPE_FREEBSD && isfirstrun && addr != 0) { + cur.addr = 0; + cur.size = (addr < 0x10000) ? addr : 0x10000; + cur.type = GRUB_E820_RAM; if (mmap) - { - mmap->addr = 0; - mmap->size = (addr < 0x10000) ? addr : 0x10000; - mmap->type = GRUB_E820_RAM; - mmap++; - } - else - len += sizeof (struct grub_e820_mmap); + *mmap++ = cur; + + prev = cur; + count++; } isfirstrun = 0; - if (mmap) + + cur.addr = addr; + cur.size = size; + switch (type) { - mmap->addr = addr; - mmap->size = size; - switch (type) - { - case GRUB_MACHINE_MEMORY_AVAILABLE: - mmap->type = GRUB_E820_RAM; - break; + case GRUB_MACHINE_MEMORY_AVAILABLE: + cur.type = GRUB_E820_RAM; + break; #ifdef GRUB_MACHINE_MEMORY_ACPI - case GRUB_MACHINE_MEMORY_ACPI: - mmap->type = GRUB_E820_ACPI; - break; + case GRUB_MACHINE_MEMORY_ACPI: + cur.type = GRUB_E820_ACPI; + break; #endif #ifdef GRUB_MACHINE_MEMORY_NVS - case GRUB_MACHINE_MEMORY_NVS: - mmap->type = GRUB_E820_NVS; - break; + case GRUB_MACHINE_MEMORY_NVS: + cur.type = GRUB_E820_NVS; + break; #endif - default: + default: #ifdef GRUB_MACHINE_MEMORY_CODE - case GRUB_MACHINE_MEMORY_CODE: + case GRUB_MACHINE_MEMORY_CODE: #endif #ifdef GRUB_MACHINE_MEMORY_RESERVED - case GRUB_MACHINE_MEMORY_RESERVED: + case GRUB_MACHINE_MEMORY_RESERVED: #endif - mmap->type = GRUB_E820_RESERVED; - break; - } + cur.type = GRUB_E820_RESERVED; + break; + } - /* Merge regions if possible. */ - if (mmap != mmap_buf && mmap->type == mmap[-1].type && - mmap->addr == mmap[-1].addr + mmap[-1].size) - mmap[-1].size += mmap->size; - else - mmap++; + /* Merge regions if possible. */ + if (count && cur.type == prev.type && cur.addr == prev.addr + prev.size) + { + prev.size += cur.size; + if (mmap) + mmap[-1] = cur; } else - len += sizeof (struct grub_e820_mmap); + { + if (mmap) + *mmap++ = cur; + prev = cur; + count++; + } return 0; } - grub_mmap_iterate (hook); - mmap_buf = mmap = grub_malloc (len); - if (! mmap) - return grub_errno; - isfirstrun = 1; grub_mmap_iterate (hook); - len = (mmap - mmap_buf) * sizeof (struct grub_e820_mmap); - int i; - for (i = 0; i < mmap - mmap_buf; i++) - grub_dprintf ("bsd", "smap %d, %d:%llx - %llx\n", i, - mmap_buf[i].type, - (unsigned long long) mmap_buf[i].addr, - (unsigned long long) mmap_buf[i].size); + if (len) + *len = count * sizeof (struct grub_e820_mmap); + *cnt = count; - grub_dprintf ("bsd", "%ld entries in smap\n", (long) (mmap - mmap_buf)); - grub_freebsd_add_meta (FREEBSD_MODINFO_METADATA | - FREEBSD_MODINFOMD_SMAP, mmap_buf, len); + return; +} - grub_free (mmap_buf); +static grub_err_t +grub_bsd_add_mmap (void) +{ + grub_size_t len, cnt; + void *buf = NULL, *buf0; + + generate_e820_mmap (&len, &cnt, buf); + + if (kernel_type == KERNEL_TYPE_NETBSD) + len += sizeof (grub_uint32_t); + + buf = grub_malloc (len); + if (!buf) + return grub_errno; + + buf0 = buf; + if (kernel_type == KERNEL_TYPE_NETBSD) + { + *(grub_uint32_t *) buf = cnt; + buf = ((grub_uint32_t *) buf + 1); + } + + generate_e820_mmap (NULL, &cnt, buf); + + grub_dprintf ("bsd", "%u entries in smap\n", cnt); + if (kernel_type == KERNEL_TYPE_NETBSD) + grub_bsd_add_meta (NETBSD_BTINFO_MEMMAP, buf0, len); + else + grub_bsd_add_meta (FREEBSD_MODINFO_METADATA | + FREEBSD_MODINFOMD_SMAP, buf0, len); + + grub_free (buf0); return grub_errno; } @@ -337,29 +360,22 @@ grub_freebsd_add_meta_module (char *filename, char *type, int argc, char **argv, if (grub_strcmp (type, "/boot/zfs/zpool.cache") == 0) name = "/boot/zfs/zpool.cache"; - if (grub_freebsd_add_meta (FREEBSD_MODINFO_NAME, name, - grub_strlen (name) + 1)) + if (grub_bsd_add_meta (FREEBSD_MODINFO_NAME, name, grub_strlen (name) + 1)) return grub_errno; if (is_64bit) { grub_uint64_t addr64 = addr, size64 = size; - if ((grub_freebsd_add_meta (FREEBSD_MODINFO_TYPE, type, - grub_strlen (type) + 1)) || - (grub_freebsd_add_meta (FREEBSD_MODINFO_ADDR, &addr64, - sizeof (addr64))) || - (grub_freebsd_add_meta (FREEBSD_MODINFO_SIZE, &size64, - sizeof (size64)))) + if (grub_bsd_add_meta (FREEBSD_MODINFO_TYPE, type, grub_strlen (type) + 1) + || grub_bsd_add_meta (FREEBSD_MODINFO_ADDR, &addr64, sizeof (addr64)) + || grub_bsd_add_meta (FREEBSD_MODINFO_SIZE, &size64, sizeof (size64))) return grub_errno; } else { - if ((grub_freebsd_add_meta (FREEBSD_MODINFO_TYPE, type, - grub_strlen (type) + 1)) || - (grub_freebsd_add_meta (FREEBSD_MODINFO_ADDR, &addr, - sizeof (addr))) || - (grub_freebsd_add_meta (FREEBSD_MODINFO_SIZE, &size, - sizeof (size)))) + if (grub_bsd_add_meta (FREEBSD_MODINFO_TYPE, type, grub_strlen (type) + 1) + || grub_bsd_add_meta (FREEBSD_MODINFO_ADDR, &addr, sizeof (addr)) + || grub_bsd_add_meta (FREEBSD_MODINFO_SIZE, &size, sizeof (size))) return grub_errno; } @@ -386,7 +402,7 @@ grub_freebsd_add_meta_module (char *filename, char *type, int argc, char **argv, } *p = 0; - if (grub_freebsd_add_meta (FREEBSD_MODINFO_ARGS, cmdline, n)) + if (grub_bsd_add_meta (FREEBSD_MODINFO_ARGS, cmdline, n)) return grub_errno; } } @@ -397,27 +413,23 @@ grub_freebsd_add_meta_module (char *filename, char *type, int argc, char **argv, static void grub_freebsd_list_modules (void) { - grub_uint32_t pos = 0; + struct bsd_tag *tag; grub_printf (" %-18s %-18s%14s%14s\n", "name", "type", "addr", "size"); - while (pos < mod_buf_len) - { - grub_uint32_t type, size; - type = *((grub_uint32_t *) (mod_buf + pos)); - size = *((grub_uint32_t *) (mod_buf + pos + 4)); - pos += 8; - switch (type) + for (tag = tags; tag; tag = tag->next) + { + switch (tag->type) { case FREEBSD_MODINFO_NAME: case FREEBSD_MODINFO_TYPE: - grub_printf (" %-18s", mod_buf + pos); + grub_printf (" %-18s", (char *) tag->data); break; case FREEBSD_MODINFO_ADDR: { grub_uint32_t addr; - addr = *((grub_uint32_t *) (mod_buf + pos)); + addr = *((grub_uint32_t *) tag->data); grub_printf (" 0x%08x", addr); break; } @@ -425,12 +437,10 @@ grub_freebsd_list_modules (void) { grub_uint32_t len; - len = *((grub_uint32_t *) (mod_buf + pos)); + len = *((grub_uint32_t *) tag->data); grub_printf (" 0x%08x\n", len); } } - - pos = ALIGN_VAR (pos + size); } } @@ -446,6 +456,7 @@ grub_freebsd_boot (void) grub_size_t p_size = 0; grub_uint32_t bootdev, biosdev, unit, slice, part; grub_err_t err; + grub_size_t tag_buf_len = 0; auto int iterate_env (struct grub_env_var *var); int iterate_env (struct grub_env_var *var) @@ -475,7 +486,6 @@ grub_freebsd_boot (void) return 0; } - grub_memset (&bi, 0, sizeof (bi)); bi.bi_version = FREEBSD_BOOTINFO_VERSION; bi.bi_size = sizeof (bi); @@ -488,10 +498,29 @@ grub_freebsd_boot (void) p_size = 0; grub_env_iterate (iterate_env_count); + if (p_size) p_size = ALIGN_PAGE (kern_end + p_size + 1) - kern_end; + if (is_elf_kernel) - p_size = ALIGN_PAGE (kern_end + p_size + mod_buf_len) - kern_end; + { + struct bsd_tag *tag; + + err = grub_bsd_add_mmap (); + if (err) + return err; + + err = grub_bsd_add_meta (FREEBSD_MODINFO_END, 0, 0); + if (err) + return err; + + tag_buf_len = 0; + for (tag = tags; tag; tag = tag->next) + tag_buf_len = ALIGN_VAR (tag_buf_len + + sizeof (struct freebsd_tag_header) + + tag->len); + p_size = ALIGN_PAGE (kern_end + p_size + tag_buf_len) - kern_end; + } if (is_64bit) p_size += 4096 * 3; @@ -515,27 +544,50 @@ grub_freebsd_boot (void) if (is_elf_kernel) { - grub_uint8_t *md_ofs; - int ofs; + grub_uint8_t *p_tag = p; + struct bsd_tag *tag; + + for (tag = tags; tag; tag = tag->next) + { + struct freebsd_tag_header *head + = (struct freebsd_tag_header *) p_tag; + head->type = tag->type; + head->len = tag->len; + p_tag += sizeof (struct freebsd_tag_header); + switch (tag->type) + { + case FREEBSD_MODINFO_METADATA | FREEBSD_MODINFOMD_HOWTO: + if (is_64bit) + *(grub_uint64_t *) p_tag = bootflags; + else + *(grub_uint32_t *) p_tag = bootflags; + break; - if (grub_freebsd_add_meta (FREEBSD_MODINFO_END, 0, 0)) - return grub_errno; + case FREEBSD_MODINFO_METADATA | FREEBSD_MODINFOMD_ENVP: + if (is_64bit) + *(grub_uint64_t *) p_tag = bi.bi_envp; + else + *(grub_uint32_t *) p_tag = bi.bi_envp; + break; + + case FREEBSD_MODINFO_METADATA | FREEBSD_MODINFOMD_KERNEND: + if (is_64bit) + *(grub_uint64_t *) p_tag = kern_end; + else + *(grub_uint32_t *) p_tag = kern_end; + break; + + default: + grub_memcpy (p_tag, tag->data, tag->len); + break; + } + p_tag += tag->len; + p_tag = ALIGN_VAR (p_tag - p) + p; + } - grub_memcpy (p, mod_buf, mod_buf_len); bi.bi_modulep = (p - p0) + p_target; - md_ofs = p + kern_end_mdofs; - p = (ALIGN_PAGE ((p - p0) + p_target) - p_target) + p0; - - if (is_64bit) - p += 4096 * 4; - - ofs = (is_64bit) ? 16 : 12; - *((grub_uint32_t *) md_ofs) = kern_end; - md_ofs -= ofs; - *((grub_uint32_t *) md_ofs) = bi.bi_envp; - md_ofs -= ofs; - *((grub_uint32_t *) md_ofs) = bootflags; + p = (ALIGN_PAGE ((p_tag - p0) + p_target) - p_target) + p0; } bi.bi_kernend = kern_end; @@ -561,7 +613,7 @@ grub_freebsd_boot (void) grub_fatal ("cannot exit boot services"); #endif - pagetable = p - (4096 * 3); + pagetable = p; fill_bsd64_pagetable (pagetable, (pagetable - p0) + p_target); state.cr3 = (pagetable - p0) + p_target; @@ -721,99 +773,57 @@ static grub_err_t grub_netbsd_boot (void) { struct grub_netbsd_bootinfo *bootinfo; - int count = 0; - struct grub_netbsd_btinfo_mmap_header *mmap; - struct grub_netbsd_btinfo_mmap_entry *pm; void *curarg, *arg0; grub_addr_t arg_target, stack_target; grub_uint32_t *stack; grub_err_t err; struct grub_relocator32_state state; + grub_size_t tag_buf_len = 0; + int tag_count = 0; + + err = grub_bsd_add_mmap (); + if (err) + return err; - auto int NESTED_FUNC_ATTR count_hook (grub_uint64_t, grub_uint64_t, grub_uint32_t); - int NESTED_FUNC_ATTR count_hook (grub_uint64_t addr __attribute__ ((unused)), - grub_uint64_t size __attribute__ ((unused)), - grub_uint32_t type __attribute__ ((unused))) { - count++; - return 0; - } - - auto int NESTED_FUNC_ATTR fill_hook (grub_uint64_t, grub_uint64_t, grub_uint32_t); - int NESTED_FUNC_ATTR fill_hook (grub_uint64_t addr, grub_uint64_t size, grub_uint32_t type) - { - pm->addr = addr; - pm->len = size; - - switch (type) + struct bsd_tag *tag; + tag_buf_len = 0; + for (tag = tags; tag; tag = tag->next) { - case GRUB_MACHINE_MEMORY_AVAILABLE: - pm->type = NETBSD_MMAP_AVAILABLE; - break; - - case GRUB_MACHINE_MEMORY_ACPI: - pm->type = NETBSD_MMAP_ACPI; - break; - - case GRUB_MACHINE_MEMORY_NVS: - pm->type = NETBSD_MMAP_NVS; - break; - - default: - pm->type = NETBSD_MMAP_RESERVED; - break; + tag_buf_len = ALIGN_VAR (tag_buf_len + 2 * sizeof (grub_uint32_t) + + tag->len); + tag_count++; } - pm++; - - return 0; } - grub_mmap_iterate (count_hook); - arg_target = kern_end; - err = grub_relocator_alloc_chunk_addr - (relocator, &curarg, arg_target, - sizeof (struct grub_netbsd_btinfo_rootdevice) - + sizeof (struct grub_netbsd_bootinfo) - + sizeof (struct grub_netbsd_btinfo_mmap_header) - + count * sizeof (struct grub_netbsd_btinfo_mmap_entry)); + err = grub_relocator_alloc_chunk_addr (relocator, &curarg, + arg_target, tag_buf_len + + sizeof (struct grub_netbsd_bootinfo) + + tag_count * sizeof (grub_addr_t)); if (err) return err; arg0 = curarg; - mmap = curarg; - pm = (struct grub_netbsd_btinfo_mmap_entry *) (mmap + 1); + bootinfo = (void *) ((grub_uint8_t *) arg0 + tag_buf_len); - grub_mmap_iterate (fill_hook); - mmap->common.type = NETBSD_BTINFO_MEMMAP; - mmap->common.len = (char *) pm - (char *) mmap; - mmap->count = count; - curarg = pm; + { + struct bsd_tag *tag; + unsigned i; - if (netbsd_root) - { - struct grub_netbsd_btinfo_rootdevice *rootdev; - - rootdev = (struct grub_netbsd_btinfo_rootdevice *) curarg; - - rootdev->common.len = sizeof (struct grub_netbsd_btinfo_rootdevice); - rootdev->common.type = NETBSD_BTINFO_ROOTDEVICE; - grub_strncpy (rootdev->devname, netbsd_root, sizeof (rootdev->devname)); - - bootinfo = (struct grub_netbsd_bootinfo *) (rootdev + 1); - bootinfo->bi_count = 2; - bootinfo->bi_data[0] = ((grub_uint8_t *) mmap - (grub_uint8_t *) arg0) - + arg_target; - bootinfo->bi_data[1] = ((grub_uint8_t *) rootdev - (grub_uint8_t *) arg0) - + arg_target; - } - else - { - bootinfo = (struct grub_netbsd_bootinfo *) curarg; - bootinfo->bi_count = 1; - bootinfo->bi_data[0] = ((grub_uint8_t *) mmap - (grub_uint8_t *) arg0) - + arg_target; - } + bootinfo->bi_count = tag_count; + for (tag = tags, i = 0; tag; i++, tag = tag->next) + { + struct grub_netbsd_btinfo_common *head = curarg; + bootinfo->bi_data[i] = ((grub_uint8_t *) curarg - (grub_uint8_t *) arg0) + + arg_target; + head->type = tag->type; + head->len = tag->len + sizeof (*head); + curarg = head + 1; + grub_memcpy (curarg, tag->data, tag->len); + curarg = (grub_uint8_t *) curarg + tag->len; + } + } err = grub_relocator_alloc_chunk_align (relocator, (void **) &stack, &stack_target, 0x10000, 0x90000, @@ -843,19 +853,18 @@ grub_netbsd_boot (void) static grub_err_t grub_bsd_unload (void) { - if (mod_buf) + struct bsd_tag *tag, *next; + for (tag = tags; tag; tag = next) { - grub_free (mod_buf); - mod_buf = 0; - mod_buf_max = 0; + next = tag->next; + grub_free (tag); } + tags = NULL; + tags_last = NULL; kernel_type = KERNEL_TYPE_NONE; grub_dl_unref (my_mod); - grub_free (netbsd_root); - netbsd_root = NULL; - grub_relocator_unload (relocator); relocator = NULL; @@ -1163,28 +1172,22 @@ grub_cmd_freebsd (grub_extcmd_t cmd, int argc, char *argv[]) if (err) return err; - err = grub_freebsd_add_meta (FREEBSD_MODINFO_METADATA | - FREEBSD_MODINFOMD_HOWTO, &data, 4); + err = grub_bsd_add_meta (FREEBSD_MODINFO_METADATA | + FREEBSD_MODINFOMD_HOWTO, &data, 4); if (err) return err; - err = grub_freebsd_add_meta (FREEBSD_MODINFO_METADATA | + err = grub_bsd_add_meta (FREEBSD_MODINFO_METADATA | FREEBSD_MODINFOMD_ENVP, &data, len); if (err) return err; - err = grub_freebsd_add_meta (FREEBSD_MODINFO_METADATA | - FREEBSD_MODINFOMD_KERNEND, &data, len); - if (err) - return err; - - kern_end_mdofs = mod_buf_len - len; - - err = grub_freebsd_add_mmap (); + err = grub_bsd_add_meta (FREEBSD_MODINFO_METADATA | + FREEBSD_MODINFOMD_KERNEND, &data, len); if (err) return err; } - grub_loader_set (grub_freebsd_boot, grub_bsd_unload, 1); + grub_loader_set (grub_freebsd_boot, grub_bsd_unload, 0); } return grub_errno; @@ -1239,8 +1242,61 @@ grub_cmd_netbsd (grub_extcmd_t cmd, int argc, char *argv[]) if (grub_bsd_load (argc, argv) == GRUB_ERR_NONE) { grub_loader_set (grub_netbsd_boot, grub_bsd_unload, 0); + + { + char bootpath[GRUB_NETBSD_MAX_BOOTPATH_LEN]; + char *name; + name = grub_strrchr (argv[0], '/'); + if (name) + name++; + else + name = argv[0]; + grub_memset (bootpath, 0, sizeof (bootpath)); + grub_strncpy (bootpath, name, sizeof (bootpath) - 1); + grub_bsd_add_meta (NETBSD_BTINFO_BOOTPATH, bootpath, sizeof (bootpath)); + } + if (cmd->state[NETBSD_ROOT_ARG].set) - netbsd_root = grub_strdup (cmd->state[NETBSD_ROOT_ARG].arg); + { + char root[GRUB_NETBSD_MAX_ROOTDEVICE_LEN]; + grub_memset (root, 0, sizeof (root)); + grub_strncpy (root, cmd->state[NETBSD_ROOT_ARG].arg, + sizeof (root) - 1); + grub_bsd_add_meta (NETBSD_BTINFO_ROOTDEVICE, root, sizeof (root)); + } + if (cmd->state[NETBSD_SERIAL_ARG].set) + { + struct grub_netbsd_btinfo_serial serial; + char *ptr; + + grub_memset (&serial, 0, sizeof (serial)); + grub_strcpy (serial.devname, "com"); + + if (cmd->state[NETBSD_SERIAL_ARG].arg) + { + ptr = cmd->state[NETBSD_SERIAL_ARG].arg; + serial.addr = grub_strtoul (ptr, &ptr, 0); + if (grub_errno) + return grub_errno; + if (*ptr != ',') + return grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid format"); + ptr++; + serial.speed = grub_strtoul (ptr, &ptr, 0); + if (grub_errno) + return grub_errno; + } + + grub_bsd_add_meta (NETBSD_BTINFO_CONSOLE, &serial, sizeof (serial)); + } + else + { + struct grub_netbsd_btinfo_serial cons; + + grub_memset (&cons, 0, sizeof (cons)); + grub_strcpy (cons.devname, "pc"); + + grub_bsd_add_meta (NETBSD_BTINFO_CONSOLE, &cons, sizeof (cons)); + } } return grub_errno; @@ -1495,10 +1551,5 @@ GRUB_MOD_FINI (bsd) grub_unregister_command (cmd_freebsd_module); grub_unregister_command (cmd_freebsd_module_elf); - if (mod_buf) - { - grub_free (mod_buf); - mod_buf = 0; - mod_buf_max = 0; - } + grub_bsd_unload (); } diff --git a/loader/i386/bsdXX.c b/loader/i386/bsdXX.c index 41dfe89e4..b76093ccf 100644 --- a/loader/i386/bsdXX.c +++ b/loader/i386/bsdXX.c @@ -146,13 +146,13 @@ SUFFIX (grub_freebsd_load_elfmodule_obj) (struct grub_relocator *relocator, argc - 1, argv + 1, module, curload - module); if (! err) - err = grub_freebsd_add_meta (FREEBSD_MODINFO_METADATA - | FREEBSD_MODINFOMD_ELFHDR, - &e, sizeof (e)); + err = grub_bsd_add_meta (FREEBSD_MODINFO_METADATA + | FREEBSD_MODINFOMD_ELFHDR, + &e, sizeof (e)); if (! err) - err = grub_freebsd_add_meta (FREEBSD_MODINFO_METADATA - | FREEBSD_MODINFOMD_SHDR, - shdr, e.e_shnum * e.e_shentsize); + err = grub_bsd_add_meta (FREEBSD_MODINFO_METADATA + | FREEBSD_MODINFOMD_SHDR, + shdr, e.e_shnum * e.e_shentsize); return err; } @@ -275,9 +275,9 @@ SUFFIX (grub_freebsd_load_elf_meta) (struct grub_relocator *relocator, if (err) return err; - err = grub_freebsd_add_meta (FREEBSD_MODINFO_METADATA | - FREEBSD_MODINFOMD_ELFHDR, &e, - sizeof (e)); + err = grub_bsd_add_meta (FREEBSD_MODINFO_METADATA | + FREEBSD_MODINFOMD_ELFHDR, &e, + sizeof (e)); if (err) return err; @@ -346,22 +346,22 @@ SUFFIX (grub_freebsd_load_elf_meta) (struct grub_relocator *relocator, { dynamic = sym->st_value; grub_dprintf ("bsd", "dynamic = %llx\n", (unsigned long long) dynamic); - err = grub_freebsd_add_meta (FREEBSD_MODINFO_METADATA | - FREEBSD_MODINFOMD_DYNAMIC, &dynamic, - sizeof (dynamic)); + err = grub_bsd_add_meta (FREEBSD_MODINFO_METADATA | + FREEBSD_MODINFOMD_DYNAMIC, &dynamic, + sizeof (dynamic)); if (err) return err; } - err = grub_freebsd_add_meta (FREEBSD_MODINFO_METADATA | - FREEBSD_MODINFOMD_SSYM, &symstart, - sizeof (symstart)); + err = grub_bsd_add_meta (FREEBSD_MODINFO_METADATA | + FREEBSD_MODINFOMD_SSYM, &symstart, + sizeof (symstart)); if (err) return err; - err = grub_freebsd_add_meta (FREEBSD_MODINFO_METADATA | - FREEBSD_MODINFOMD_ESYM, &symend, - sizeof (symend)); + err = grub_bsd_add_meta (FREEBSD_MODINFO_METADATA | + FREEBSD_MODINFOMD_ESYM, &symend, + sizeof (symend)); if (err) return err; @@ -369,3 +369,5 @@ SUFFIX (grub_freebsd_load_elf_meta) (struct grub_relocator *relocator, return GRUB_ERR_NONE; } + + From 5fb5182f8ad3e2a02bd0f01134c26302a51bc702 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Fri, 15 Jan 2010 11:48:15 +0100 Subject: [PATCH 055/178] comX notation support --- include/grub/i386/pc/serial.h | 3 +++ loader/i386/bsd.c | 26 ++++++++++++++++++-------- term/i386/pc/serial.c | 8 ++++---- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/include/grub/i386/pc/serial.h b/include/grub/i386/pc/serial.h index 0632ff79d..4038f50fb 100644 --- a/include/grub/i386/pc/serial.h +++ b/include/grub/i386/pc/serial.h @@ -64,4 +64,7 @@ /* Turn on DTR, RTS, and OUT2. */ #define UART_ENABLE_MODEM 0x0B +unsigned short +grub_serial_hw_get_port (const unsigned int unit); + #endif /* ! GRUB_SERIAL_MACHINE_HEADER */ diff --git a/loader/i386/bsd.c b/loader/i386/bsd.c index 3565884d5..49c846c31 100644 --- a/loader/i386/bsd.c +++ b/loader/i386/bsd.c @@ -33,6 +33,7 @@ #include #include #include +#include #ifdef GRUB_MACHINE_PCBIOS #include @@ -137,7 +138,7 @@ static const struct grub_arg_option netbsd_opts[] = {"silent", 'z', 0, N_("Supress normal output (warnings remain)."), 0, 0}, {"root", 'r', 0, N_("Set root device."), N_("DEVICE"), ARG_TYPE_STRING}, {"serial", 'h', GRUB_ARG_OPTION_OPTIONAL, - N_("Use serial console."), N_("ADDR,SPEED"), ARG_TYPE_STRING}, + N_("Use serial console."), N_("[ADDR|comUNIT][,SPEED]"), ARG_TYPE_STRING}, {0, 0, 0, 0, 0, 0} }; @@ -1275,15 +1276,24 @@ grub_cmd_netbsd (grub_extcmd_t cmd, int argc, char *argv[]) if (cmd->state[NETBSD_SERIAL_ARG].arg) { ptr = cmd->state[NETBSD_SERIAL_ARG].arg; - serial.addr = grub_strtoul (ptr, &ptr, 0); - if (grub_errno) - return grub_errno; - if (*ptr != ',') - return grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid format"); - ptr++; - serial.speed = grub_strtoul (ptr, &ptr, 0); + if (grub_memcmp (ptr, "com", sizeof ("com") - 1) == 0) + { + ptr += sizeof ("com") - 1; + serial.addr + = grub_serial_hw_get_port (grub_strtoul (ptr, &ptr, 0)); + } + else + serial.addr = grub_strtoul (ptr, &ptr, 0); if (grub_errno) return grub_errno; + + if (*ptr == ',') + { + ptr++; + serial.speed = grub_strtoul (ptr, &ptr, 0); + if (grub_errno) + return grub_errno; + } } grub_bsd_add_meta (NETBSD_BTINFO_CONSOLE, &serial, sizeof (serial)); diff --git a/term/i386/pc/serial.c b/term/i386/pc/serial.c index 8d09f6211..b94b09553 100644 --- a/term/i386/pc/serial.c +++ b/term/i386/pc/serial.c @@ -74,8 +74,8 @@ static const unsigned short serial_hw_io_addr[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 } #endif /* Return the port number for the UNITth serial device. */ -static inline unsigned short -serial_hw_get_port (const unsigned int unit) +unsigned short +grub_serial_hw_get_port (const unsigned int unit) { if (unit < GRUB_SERIAL_PORT_NUM) return serial_hw_io_addr[unit]; @@ -498,7 +498,7 @@ grub_cmd_serial (grub_extcmd_t cmd, unsigned int unit; unit = grub_strtoul (state[0].arg, 0, 0); - serial_settings.port = serial_hw_get_port (unit); + serial_settings.port = grub_serial_hw_get_port (unit); if (!serial_settings.port) return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad unit number"); } @@ -608,7 +608,7 @@ GRUB_MOD_INIT(serial) N_("Configure serial port."), options); /* Set default settings. */ - serial_settings.port = serial_hw_get_port (0); + serial_settings.port = grub_serial_hw_get_port (0); serial_settings.divisor = serial_get_divisor (9600); serial_settings.word_len = UART_8BITS_WORD; serial_settings.parity = UART_NO_PARITY; From 9766dafa74d9d8a8116e623a07ff4d7fe846bf08 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Fri, 15 Jan 2010 12:31:06 +0100 Subject: [PATCH 056/178] symtab support for knetbsd --- include/grub/i386/bsd.h | 17 ++++- loader/i386/bsd.c | 19 +++++- loader/i386/bsdXX.c | 133 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 164 insertions(+), 5 deletions(-) diff --git a/include/grub/i386/bsd.h b/include/grub/i386/bsd.h index aa19b2338..f73dd7a31 100644 --- a/include/grub/i386/bsd.h +++ b/include/grub/i386/bsd.h @@ -210,6 +210,7 @@ struct grub_netbsd_bootinfo #define NETBSD_BTINFO_BOOTPATH 0 #define NETBSD_BTINFO_ROOTDEVICE 1 #define NETBSD_BTINFO_CONSOLE 6 +#define NETBSD_BTINFO_SYMTAB 8 #define NETBSD_BTINFO_MEMMAP 9 struct grub_netbsd_btinfo_common @@ -222,7 +223,6 @@ struct grub_netbsd_btinfo_common struct grub_netbsd_btinfo_bootdisk { - struct grub_netbsd_btinfo_common common; int labelsector; /* label valid if != -1 */ struct { @@ -233,6 +233,14 @@ struct grub_netbsd_btinfo_bootdisk int partition; }; +struct grub_netbsd_btinfo_symtab +{ + grub_uint32_t nsyms; + grub_uint32_t ssyms; + grub_uint32_t esyms; +}; + + struct grub_netbsd_btinfo_serial { char devname[16]; @@ -256,6 +264,13 @@ grub_err_t grub_freebsd_load_elf_meta64 (struct grub_relocator *relocator, grub_file_t file, grub_addr_t *kern_end); +grub_err_t grub_netbsd_load_elf_meta32 (struct grub_relocator *relocator, + grub_file_t file, + grub_addr_t *kern_end); +grub_err_t grub_netbsd_load_elf_meta64 (struct grub_relocator *relocator, + grub_file_t file, + grub_addr_t *kern_end); + grub_err_t grub_bsd_add_meta (grub_uint32_t type, void *data, grub_uint32_t len); grub_err_t grub_freebsd_add_meta_module (char *filename, char *type, diff --git a/loader/i386/bsd.c b/loader/i386/bsd.c index 49c846c31..9bdefbba9 100644 --- a/loader/i386/bsd.c +++ b/loader/i386/bsd.c @@ -1237,12 +1237,27 @@ grub_cmd_openbsd (grub_extcmd_t cmd, int argc, char *argv[]) static grub_err_t grub_cmd_netbsd (grub_extcmd_t cmd, int argc, char *argv[]) { + grub_err_t err; kernel_type = KERNEL_TYPE_NETBSD; bootflags = grub_bsd_parse_flags (cmd->state, netbsd_flags); if (grub_bsd_load (argc, argv) == GRUB_ERR_NONE) { - grub_loader_set (grub_netbsd_boot, grub_bsd_unload, 0); + if (is_elf_kernel) + { + grub_file_t file; + + file = grub_gzfile_open (argv[0], 1); + if (! file) + return grub_errno; + + if (is_64bit) + err = grub_netbsd_load_elf_meta64 (relocator, file, &kern_end); + else + err = grub_netbsd_load_elf_meta32 (relocator, file, &kern_end); + if (err) + return err; + } { char bootpath[GRUB_NETBSD_MAX_BOOTPATH_LEN]; @@ -1307,6 +1322,8 @@ grub_cmd_netbsd (grub_extcmd_t cmd, int argc, char *argv[]) grub_bsd_add_meta (NETBSD_BTINFO_CONSOLE, &cons, sizeof (cons)); } + + grub_loader_set (grub_netbsd_boot, grub_bsd_unload, 0); } return grub_errno; diff --git a/loader/i386/bsdXX.c b/loader/i386/bsdXX.c index b76093ccf..77d059921 100644 --- a/loader/i386/bsdXX.c +++ b/loader/i386/bsdXX.c @@ -254,7 +254,7 @@ SUFFIX (grub_freebsd_load_elfmodule) (struct grub_relocator *relocator, #endif grub_err_t -SUFFIX (grub_freebsd_load_elf_meta) (struct grub_relocator *relocator, +SUFFIX (grub_freebsd_load_elf_meta) (struct grub_relocator *relocator, grub_file_t file, grub_addr_t *kern_end) { grub_err_t err; @@ -296,8 +296,9 @@ SUFFIX (grub_freebsd_load_elf_meta) (struct grub_relocator *relocator, stroff = s->sh_offset; strsize = s->sh_size; - chunk_size = 2 * sizeof (grub_freebsd_addr_t) - + ALIGN_UP (symsize + strsize, sizeof (grub_freebsd_addr_t)); + chunk_size = ALIGN_UP (symsize + strsize, sizeof (grub_freebsd_addr_t)) + + 2 * sizeof (grub_freebsd_addr_t); + symtarget = ALIGN_UP (*kern_end, sizeof (grub_freebsd_addr_t)); err = grub_relocator_alloc_chunk_addr (relocator, &sym_chunk, symtarget, chunk_size); @@ -310,6 +311,7 @@ SUFFIX (grub_freebsd_load_elf_meta) (struct grub_relocator *relocator, curload = sym_chunk; *((grub_freebsd_addr_t *) curload) = symsize; curload += sizeof (grub_freebsd_addr_t); + if (grub_file_seek (file, symoff) == (grub_off_t) -1) return grub_errno; sym = (Elf_Sym *) curload; @@ -370,4 +372,129 @@ SUFFIX (grub_freebsd_load_elf_meta) (struct grub_relocator *relocator, return GRUB_ERR_NONE; } +grub_err_t +SUFFIX (grub_netbsd_load_elf_meta) (struct grub_relocator *relocator, + grub_file_t file, grub_addr_t *kern_end) +{ + grub_err_t err; + Elf_Ehdr e; + Elf_Shdr *s, *symsh, *strsh; + char *shdr; + unsigned symsize, strsize; + Elf_Sym *sym; + void *sym_chunk; + grub_uint8_t *curload; + const char *str; + grub_size_t chunk_size; + Elf_Ehdr *e2; + struct grub_netbsd_btinfo_symtab symtab; + grub_addr_t symtarget; + + err = read_headers (file, &e, &shdr); + if (err) + return err; + + for (s = (Elf_Shdr *) shdr; s < (Elf_Shdr *) (shdr + + e.e_shnum * e.e_shentsize); + s = (Elf_Shdr *) ((char *) s + e.e_shentsize)) + if (s->sh_type == SHT_SYMTAB) + break; + if (s >= (Elf_Shdr *) ((char *) shdr + + e.e_shnum * e.e_shentsize)) + return grub_error (GRUB_ERR_BAD_OS, "no symbol table"); + symsize = s->sh_size; + symsh = s; + s = (Elf_Shdr *) (shdr + e.e_shentsize * s->sh_link); + strsize = s->sh_size; + strsh = s; + + chunk_size = ALIGN_UP (symsize, sizeof (grub_freebsd_addr_t)) + + ALIGN_UP (strsize, sizeof (grub_freebsd_addr_t)) + + sizeof (e) + e.e_phnum * e.e_phentsize + + e.e_shnum * e.e_shentsize; + + symtarget = ALIGN_UP (*kern_end, sizeof (grub_freebsd_addr_t)); + err = grub_relocator_alloc_chunk_addr (relocator, &sym_chunk, + symtarget, chunk_size); + if (err) + return err; + + symtab.nsyms = chunk_size; + symtab.ssyms = symtarget; + symtab.esyms = symtarget + chunk_size; + + curload = sym_chunk; + + e2 = (Elf_Ehdr *) curload; + grub_memcpy (curload, &e, sizeof (e)); + e2->e_phoff = sizeof (e); + e2->e_shoff = sizeof (e) + e.e_phnum * e.e_phentsize; + + curload += sizeof (e); + if (grub_file_seek (file, e.e_phoff) == (grub_off_t) -1) + return grub_errno; + if (grub_file_read (file, curload, e.e_phnum * e.e_phentsize) + != (grub_ssize_t) (e.e_phnum * e.e_phentsize)) + { + if (! grub_errno) + return grub_error (GRUB_ERR_BAD_OS, "invalid ELF"); + return grub_errno; + } + curload += e.e_phnum * e.e_phentsize; + + for (s = (Elf_Shdr *) shdr; s < (Elf_Shdr *) (shdr + + e.e_shnum * e.e_shentsize); + s = (Elf_Shdr *) ((char *) s + e.e_shentsize)) + { + Elf_Shdr *s2; + s2 = (Elf_Shdr *) curload; + grub_memcpy (curload, s, e.e_shentsize); + if (s == symsh) + { + s2->sh_offset = sizeof (e) + e.e_phnum * e.e_phentsize + + e.e_shnum * e.e_shentsize; + } + else if (s == strsh) + { + s2->sh_offset = ALIGN_UP (symsize, sizeof (grub_freebsd_addr_t)) + + sizeof (e) + e.e_phnum * e.e_phentsize + + e.e_shnum * e.e_shentsize; + } + else + s2->sh_offset = 0; + s2->sh_addr = s2->sh_offset; + } + + if (grub_file_seek (file, symsh->sh_offset) == (grub_off_t) -1) + return grub_errno; + sym = (Elf_Sym *) curload; + if (grub_file_read (file, curload, symsize) != (grub_ssize_t) symsize) + { + if (! grub_errno) + return grub_error (GRUB_ERR_BAD_OS, "invalid ELF"); + return grub_errno; + } + curload += ALIGN_UP (symsize, sizeof (grub_freebsd_addr_t)); + + if (grub_file_seek (file, strsh->sh_offset) == (grub_off_t) -1) + return grub_errno; + str = (char *) curload; + if (grub_file_read (file, curload, strsize) != (grub_ssize_t) strsize) + { + if (! grub_errno) + return grub_error (GRUB_ERR_BAD_OS, "invalid ELF"); + return grub_errno; + } + + err = grub_bsd_add_meta (NETBSD_BTINFO_SYMTAB, + &symtab, + sizeof (symtab)); + if (err) + return err; + + *kern_end = ALIGN_PAGE (symtarget + chunk_size); + + return GRUB_ERR_NONE; +} + From 53fbae94a6d21f8f5938e5896f46e22f746553e7 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Fri, 15 Jan 2010 13:40:37 +0100 Subject: [PATCH 057/178] netbsd framebuffer support --- include/grub/i386/bsd.h | 22 ++++++++++ loader/i386/bsd.c | 95 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 116 insertions(+), 1 deletion(-) diff --git a/include/grub/i386/bsd.h b/include/grub/i386/bsd.h index f73dd7a31..62c374c03 100644 --- a/include/grub/i386/bsd.h +++ b/include/grub/i386/bsd.h @@ -212,6 +212,7 @@ struct grub_netbsd_bootinfo #define NETBSD_BTINFO_CONSOLE 6 #define NETBSD_BTINFO_SYMTAB 8 #define NETBSD_BTINFO_MEMMAP 9 +#define NETBSD_BTINFO_FRAMEBUF 12 struct grub_netbsd_btinfo_common { @@ -248,6 +249,27 @@ struct grub_netbsd_btinfo_serial grub_uint32_t speed; }; +struct grub_netbsd_btinfo_framebuf +{ + grub_uint64_t fbaddr; + grub_uint32_t flags; + grub_uint32_t width; + grub_uint32_t height; + grub_uint16_t pitch; + grub_uint8_t bpp; + + grub_uint8_t red_mask_size; + grub_uint8_t green_mask_size; + grub_uint8_t blue_mask_size; + + grub_uint8_t red_field_pos; + grub_uint8_t green_field_pos; + grub_uint8_t blue_field_pos; + + grub_uint8_t reserved[16]; +}; + + #define GRUB_NETBSD_MAX_ROOTDEVICE_LEN 16 grub_err_t grub_freebsd_load_elfmodule32 (struct grub_relocator *relocator, diff --git a/loader/i386/bsd.c b/loader/i386/bsd.c index 9bdefbba9..1d66b08d1 100644 --- a/loader/i386/bsd.c +++ b/loader/i386/bsd.c @@ -40,7 +40,13 @@ #endif #ifdef GRUB_MACHINE_EFI #include +#define NETBSD_DEFAULT_VIDEO_MODE "800x600" +#else +#define NETBSD_DEFAULT_VIDEO_MODE "text" +#include #endif +#include + #include #include #include @@ -770,6 +776,85 @@ grub_openbsd_boot (void) return grub_relocator32_boot (relocator, state); } +static grub_err_t +grub_netbsd_setup_video (void) +{ + struct grub_video_mode_info mode_info; + void *framebuffer; + const char *modevar; + struct grub_netbsd_btinfo_framebuf params; + grub_err_t err; + + modevar = grub_env_get ("gfxpayload"); + + /* Now all graphical modes are acceptable. + May change in future if we have modes without framebuffer. */ + if (modevar && *modevar != 0) + { + char *tmp; + tmp = grub_malloc (grub_strlen (modevar) + + sizeof (";" NETBSD_DEFAULT_VIDEO_MODE)); + if (! tmp) + return grub_errno; + grub_sprintf (tmp, "%s;" NETBSD_DEFAULT_VIDEO_MODE, modevar); + err = grub_video_set_mode (tmp, 0, 0); + grub_free (tmp); + } + else + err = grub_video_set_mode (NETBSD_DEFAULT_VIDEO_MODE, 0, 0); + + if (err) + return err; + + err = grub_video_get_info_and_fini (&mode_info, &framebuffer); + + if (err) + return err; + + params.width = mode_info.width; + params.height = mode_info.height; + params.bpp = mode_info.bpp; + params.pitch = mode_info.pitch; + params.flags = 0; + + params.fbaddr = (grub_addr_t) framebuffer; + + params.red_mask_size = mode_info.red_mask_size; + params.red_field_pos = mode_info.red_field_pos; + params.green_mask_size = mode_info.green_mask_size; + params.green_field_pos = mode_info.green_field_pos; + params.blue_mask_size = mode_info.blue_mask_size; + params.blue_field_pos = mode_info.blue_field_pos; + +#ifdef GRUB_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 (mode_info.bpp <= 8) + { + struct grub_vbe_info_block controller_info; + int status; + int width = 8; + + status = grub_vbe_bios_get_controller_info (&controller_info); + + if (status == GRUB_VBE_STATUS_OK && + (controller_info.capabilities & GRUB_VBE_CAPABILITY_DACWIDTH)) + status = grub_vbe_bios_set_dac_palette_width (&width); + + if (status != GRUB_VBE_STATUS_OK) + /* 6 is default after mode reset. */ + width = 6; + + params.red_mask_size = params.green_mask_size + = params.blue_mask_size = width; + } +#endif + + err = grub_bsd_add_meta (NETBSD_BTINFO_FRAMEBUF, ¶ms, sizeof (params)); + return err; +} + static grub_err_t grub_netbsd_boot (void) { @@ -786,6 +871,14 @@ grub_netbsd_boot (void) if (err) return err; + err = grub_netbsd_setup_video (); + if (err) + { + grub_print_error (); + grub_printf ("Booting however\n"); + grub_errno = GRUB_ERR_NONE; + } + { struct bsd_tag *tag; tag_buf_len = 0; @@ -1320,7 +1413,7 @@ grub_cmd_netbsd (grub_extcmd_t cmd, int argc, char *argv[]) grub_memset (&cons, 0, sizeof (cons)); grub_strcpy (cons.devname, "pc"); - grub_bsd_add_meta (NETBSD_BTINFO_CONSOLE, &cons, sizeof (cons)); + grub_bsd_add_meta (NETBSD_BTINFO_CONSOLE, &cons, sizeof (cons)); } grub_loader_set (grub_netbsd_boot, grub_bsd_unload, 0); From 820e8e55fd31f719c951ce9bcf56b15dfd578b82 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 16 Jan 2010 00:31:24 +0100 Subject: [PATCH 058/178] Avoid retrieving video info when no video is active --- loader/i386/bsd.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/loader/i386/bsd.c b/loader/i386/bsd.c index e6c5273cd..de4a256de 100644 --- a/loader/i386/bsd.c +++ b/loader/i386/bsd.c @@ -789,6 +789,7 @@ grub_netbsd_setup_video (void) const char *modevar; struct grub_netbsd_btinfo_framebuf params; grub_err_t err; + grub_video_driver_id_t driv_id; modevar = grub_env_get ("gfxpayload"); @@ -811,6 +812,10 @@ grub_netbsd_setup_video (void) if (err) return err; + driv_id = grub_video_get_driver_id (); + if (driv_id == GRUB_VIDEO_DRIVER_NONE) + return GRUB_ERR_NONE; + err = grub_video_get_info_and_fini (&mode_info, &framebuffer); if (err) @@ -835,7 +840,7 @@ grub_netbsd_setup_video (void) /* 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 (mode_info.bpp <= 8) + if (mode_info.bpp <= 8 && driv_id == GRUB_VIDEO_DRIVER_VBE) { struct grub_vbe_info_block controller_info; int status; From cefe39c94ba4e921c325fd275996426257e94d36 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 16 Jan 2010 11:06:55 +0100 Subject: [PATCH 059/178] Fix knetbsd symbols --- loader/i386/bsdXX.c | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/loader/i386/bsdXX.c b/loader/i386/bsdXX.c index 77d059921..ccf386440 100644 --- a/loader/i386/bsdXX.c +++ b/loader/i386/bsdXX.c @@ -410,8 +410,7 @@ SUFFIX (grub_netbsd_load_elf_meta) (struct grub_relocator *relocator, chunk_size = ALIGN_UP (symsize, sizeof (grub_freebsd_addr_t)) + ALIGN_UP (strsize, sizeof (grub_freebsd_addr_t)) - + sizeof (e) + e.e_phnum * e.e_phentsize - + e.e_shnum * e.e_shentsize; + + sizeof (e) + e.e_shnum * e.e_shentsize; symtarget = ALIGN_UP (*kern_end, sizeof (grub_freebsd_addr_t)); err = grub_relocator_alloc_chunk_addr (relocator, &sym_chunk, @@ -419,7 +418,7 @@ SUFFIX (grub_netbsd_load_elf_meta) (struct grub_relocator *relocator, if (err) return err; - symtab.nsyms = chunk_size; + symtab.nsyms = 1; symtab.ssyms = symtarget; symtab.esyms = symtarget + chunk_size; @@ -427,20 +426,13 @@ SUFFIX (grub_netbsd_load_elf_meta) (struct grub_relocator *relocator, e2 = (Elf_Ehdr *) curload; grub_memcpy (curload, &e, sizeof (e)); - e2->e_phoff = sizeof (e); - e2->e_shoff = sizeof (e) + e.e_phnum * e.e_phentsize; + e2->e_phoff = 0; + e2->e_phnum = 0; + e2->e_phentsize = 0; + e2->e_shstrndx = 0; + e2->e_shoff = sizeof (e); curload += sizeof (e); - if (grub_file_seek (file, e.e_phoff) == (grub_off_t) -1) - return grub_errno; - if (grub_file_read (file, curload, e.e_phnum * e.e_phentsize) - != (grub_ssize_t) (e.e_phnum * e.e_phentsize)) - { - if (! grub_errno) - return grub_error (GRUB_ERR_BAD_OS, "invalid ELF"); - return grub_errno; - } - curload += e.e_phnum * e.e_phentsize; for (s = (Elf_Shdr *) shdr; s < (Elf_Shdr *) (shdr + e.e_shnum * e.e_shentsize); @@ -450,19 +442,14 @@ SUFFIX (grub_netbsd_load_elf_meta) (struct grub_relocator *relocator, s2 = (Elf_Shdr *) curload; grub_memcpy (curload, s, e.e_shentsize); if (s == symsh) - { - s2->sh_offset = sizeof (e) + e.e_phnum * e.e_phentsize - + e.e_shnum * e.e_shentsize; - } + s2->sh_offset = sizeof (e) + e.e_shnum * e.e_shentsize; else if (s == strsh) - { - s2->sh_offset = ALIGN_UP (symsize, sizeof (grub_freebsd_addr_t)) - + sizeof (e) + e.e_phnum * e.e_phentsize - + e.e_shnum * e.e_shentsize; - } + s2->sh_offset = ALIGN_UP (symsize, sizeof (grub_freebsd_addr_t)) + + sizeof (e) + e.e_shnum * e.e_shentsize; else s2->sh_offset = 0; s2->sh_addr = s2->sh_offset; + curload += e.e_shentsize; } if (grub_file_seek (file, symsh->sh_offset) == (grub_off_t) -1) From ae9eb98c7d91aaef28362008dc2fe92043b6199f Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 16 Jan 2010 11:47:12 +0100 Subject: [PATCH 060/178] NetBSD module support --- include/grub/i386/bsd.h | 16 ++++ loader/i386/bsd.c | 167 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 176 insertions(+), 7 deletions(-) diff --git a/include/grub/i386/bsd.h b/include/grub/i386/bsd.h index 62c374c03..870287371 100644 --- a/include/grub/i386/bsd.h +++ b/include/grub/i386/bsd.h @@ -212,6 +212,7 @@ struct grub_netbsd_bootinfo #define NETBSD_BTINFO_CONSOLE 6 #define NETBSD_BTINFO_SYMTAB 8 #define NETBSD_BTINFO_MEMMAP 9 +#define NETBSD_BTINFO_MODULES 11 #define NETBSD_BTINFO_FRAMEBUF 12 struct grub_netbsd_btinfo_common @@ -249,6 +250,21 @@ struct grub_netbsd_btinfo_serial grub_uint32_t speed; }; +struct grub_netbsd_btinfo_modules +{ + grub_uint32_t num; + grub_uint32_t last_addr; + struct grub_netbsd_btinfo_module + { + char name[80]; +#define GRUB_NETBSD_MODULE_RAW 0 +#define GRUB_NETBSD_MODULE_ELF 1 + grub_uint32_t type; + grub_uint32_t size; + grub_uint32_t addr; + } mods[0]; +}; + struct grub_netbsd_btinfo_framebuf { grub_uint64_t fbaddr; diff --git a/loader/i386/bsd.c b/loader/i386/bsd.c index de4a256de..f93c87cf0 100644 --- a/loader/i386/bsd.c +++ b/loader/i386/bsd.c @@ -82,6 +82,14 @@ struct bsd_tag struct bsd_tag *tags, *tags_last; +struct netbsd_module +{ + struct netbsd_module *next; + struct grub_netbsd_btinfo_module mod; +}; + +struct netbsd_module *netbsd_mods, *netbsd_mods_last; + static const struct grub_arg_option freebsd_opts[] = { {"dual", 'D', 0, N_("Display output on all consoles."), 0, 0}, @@ -452,6 +460,49 @@ grub_freebsd_list_modules (void) } } +static grub_err_t +grub_netbsd_add_meta_module (char *filename, grub_uint32_t type, + grub_addr_t addr, grub_uint32_t size) +{ + char *name; + struct netbsd_module *mod; + name = grub_strrchr (filename, '/'); + + if (name) + name++; + else + name = filename; + + mod = grub_zalloc (sizeof (*mod)); + if (!mod) + return grub_errno; + + grub_strncpy (mod->mod.name, name, sizeof (mod->mod.name) - 1); + mod->mod.addr = addr; + mod->mod.type = type; + mod->mod.size = size; + + if (netbsd_mods_last) + netbsd_mods_last->next = mod; + else + netbsd_mods = mod; + netbsd_mods_last = mod; + + return GRUB_ERR_NONE; +} + +static void +grub_netbsd_list_modules (void) +{ + struct netbsd_module *mod; + + grub_printf (" %-18s%14s%14s%14s\n", "name", "type", "addr", "size"); + + for (mod = netbsd_mods; mod; mod = mod->next) + grub_printf (" %-18s 0x%08x 0x%08x 0x%08x", mod->mod.name, + mod->mod.type, mod->mod.addr, mod->mod.size); +} + /* This function would be here but it's under different license. */ #include "bsd_pagetable.c" @@ -865,6 +916,38 @@ grub_netbsd_setup_video (void) return err; } +static grub_err_t +grub_netbsd_add_modules (void) +{ + struct netbsd_module *mod; + unsigned modcnt = 0; + struct grub_netbsd_btinfo_modules *mods; + grub_addr_t last_addr = 0; + unsigned i; + grub_err_t err; + + for (mod = netbsd_mods; mod; mod = mod->next) + { + if (mod->mod.addr + mod->mod.size > last_addr) + last_addr = mod->mod.addr + mod->mod.size; + modcnt++; + } + + mods = grub_malloc (sizeof (*mods) + sizeof (mods->mods[0]) * modcnt); + if (!mods) + return grub_errno; + + mods->num = modcnt; + mods->last_addr = last_addr; + for (mod = netbsd_mods, i = 0; mod; i++, mod = mod->next) + mods->mods[i] = mod->mod; + + err = grub_bsd_add_meta (NETBSD_BTINFO_MODULES, mods, + sizeof (*mods) + sizeof (mods->mods[0]) * modcnt); + grub_free (mods); + return err; +} + static grub_err_t grub_netbsd_boot (void) { @@ -889,6 +972,10 @@ grub_netbsd_boot (void) grub_errno = GRUB_ERR_NONE; } + err = grub_netbsd_add_modules (); + if (err) + return err; + { struct bsd_tag *tag; tag_buf_len = 0; @@ -1535,13 +1622,8 @@ grub_cmd_freebsd_module (grub_command_t cmd __attribute__ ((unused)), grub_err_t err; void *src; - if (kernel_type == KERNEL_TYPE_NONE) - return grub_error (GRUB_ERR_BAD_ARGUMENT, - "you need to load the kernel first"); - if (kernel_type != KERNEL_TYPE_FREEBSD) - return grub_error (GRUB_ERR_BAD_ARGUMENT, - "only FreeBSD supports module"); + return grub_error (GRUB_ERR_BAD_ARGUMENT, "no FreeBSD loaded"); if (!is_elf_kernel) return grub_error (GRUB_ERR_BAD_ARGUMENT, @@ -1593,6 +1675,68 @@ fail: return grub_errno; } +static grub_err_t +grub_netbsd_module_load (char *filename, grub_uint32_t type) +{ + grub_file_t file = 0; + void *src; + grub_err_t err; + + file = grub_gzfile_open (filename, 1); + if ((!file) || (!file->size)) + goto fail; + + err = grub_relocator_alloc_chunk_addr (relocator, &src, kern_end, + file->size); + if (err) + goto fail; + + grub_file_read (file, src, file->size); + if (grub_errno) + goto fail; + + err = grub_netbsd_add_meta_module (filename, type, kern_end, file->size); + + if (err) + goto fail; + + kern_end = ALIGN_PAGE (kern_end + file->size); + +fail: + if (file) + grub_file_close (file); + + return grub_errno; +} + +static grub_err_t +grub_cmd_netbsd_module (grub_command_t cmd, + int argc, char *argv[]) +{ + grub_uint32_t type; + + if (kernel_type != KERNEL_TYPE_NETBSD) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "no NetBSD loaded"); + + if (!is_elf_kernel) + return grub_error (GRUB_ERR_BAD_ARGUMENT, + "only ELF kernel supports module"); + + /* List the current modules if no parameter. */ + if (!argc) + { + grub_netbsd_list_modules (); + return 0; + } + + if (grub_strcmp (cmd->name, "knetbsd_module_elf") == 0) + type = GRUB_NETBSD_MODULE_ELF; + else + type = GRUB_NETBSD_MODULE_RAW; + + return grub_netbsd_module_load (argv[0], type); +} + static grub_err_t grub_cmd_freebsd_module_elf (grub_command_t cmd __attribute__ ((unused)), int argc, char *argv[]) @@ -1642,7 +1786,8 @@ grub_cmd_freebsd_module_elf (grub_command_t cmd __attribute__ ((unused)), static grub_extcmd_t cmd_freebsd, cmd_openbsd, cmd_netbsd; static grub_command_t cmd_freebsd_loadenv, cmd_freebsd_module; -static grub_command_t cmd_freebsd_module_elf; +static grub_command_t cmd_netbsd_module, cmd_freebsd_module_elf; +static grub_command_t cmd_netbsd_module_elf; GRUB_MOD_INIT (bsd) { @@ -1664,6 +1809,12 @@ GRUB_MOD_INIT (bsd) cmd_freebsd_module = grub_register_command ("kfreebsd_module", grub_cmd_freebsd_module, 0, N_("Load FreeBSD kernel module.")); + cmd_netbsd_module = + grub_register_command ("knetbsd_module", grub_cmd_netbsd_module, + 0, N_("Load NetBSD kernel module.")); + cmd_netbsd_module_elf = + grub_register_command ("knetbsd_module_elf", grub_cmd_netbsd_module, + 0, N_("Load NetBSD kernel module (ELF).")); cmd_freebsd_module_elf = grub_register_command ("kfreebsd_module_elf", grub_cmd_freebsd_module_elf, 0, N_("Load FreeBSD kernel module (ELF).")); @@ -1679,7 +1830,9 @@ GRUB_MOD_FINI (bsd) grub_unregister_command (cmd_freebsd_loadenv); grub_unregister_command (cmd_freebsd_module); + grub_unregister_command (cmd_netbsd_module); grub_unregister_command (cmd_freebsd_module_elf); + grub_unregister_command (cmd_netbsd_module_elf); grub_bsd_unload (); } From d92b0c01d42236af68839e0c6360b3a44478b0a0 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 16 Jan 2010 12:55:52 +0100 Subject: [PATCH 061/178] Fixed knetbsd misbehaviour when no module is loaded --- loader/i386/bsd.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/loader/i386/bsd.c b/loader/i386/bsd.c index f93c87cf0..43c2c129b 100644 --- a/loader/i386/bsd.c +++ b/loader/i386/bsd.c @@ -922,23 +922,18 @@ grub_netbsd_add_modules (void) struct netbsd_module *mod; unsigned modcnt = 0; struct grub_netbsd_btinfo_modules *mods; - grub_addr_t last_addr = 0; unsigned i; grub_err_t err; for (mod = netbsd_mods; mod; mod = mod->next) - { - if (mod->mod.addr + mod->mod.size > last_addr) - last_addr = mod->mod.addr + mod->mod.size; - modcnt++; - } + modcnt++; mods = grub_malloc (sizeof (*mods) + sizeof (mods->mods[0]) * modcnt); if (!mods) return grub_errno; mods->num = modcnt; - mods->last_addr = last_addr; + mods->last_addr = kern_end; for (mod = netbsd_mods, i = 0; mod; i++, mod = mod->next) mods->mods[i] = mod->mod; From 72ebf8b87cd4bbfa9a18563bfcbe218b4943662c Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 17 Jan 2010 12:42:28 +0100 Subject: [PATCH 062/178] Sort chunks --- include/grub/relocator_private.h | 17 ------ lib/relocator.c | 99 +++++++++++++++++++++++++++----- 2 files changed, 86 insertions(+), 30 deletions(-) diff --git a/include/grub/relocator_private.h b/include/grub/relocator_private.h index cc68305c8..7a3ef2bd7 100644 --- a/include/grub/relocator_private.h +++ b/include/grub/relocator_private.h @@ -27,23 +27,6 @@ extern grub_size_t grub_relocator_forward_size; extern grub_size_t grub_relocator_backward_size; extern grub_size_t grub_relocator_jumper_size; -struct grub_relocator -{ - struct grub_relocator_chunk *chunks; - grub_addr_t postchunks; - grub_addr_t highestaddr; - grub_addr_t highestnonpostaddr; - grub_size_t relocators_size; -}; - -struct grub_relocator_chunk -{ - struct grub_relocator_chunk *next; - grub_addr_t src; - grub_addr_t target; - grub_size_t size; -}; - void grub_cpu_relocator_init (void); grub_err_t diff --git a/lib/relocator.c b/lib/relocator.c index 0064824a4..a02bca332 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -25,7 +25,23 @@ /* FIXME: implement unload. */ /* FIXME: check memory map. */ /* FIXME: try to request memory from firmware. */ -/* FIXME: sort chunk when programming relocators. */ + +struct grub_relocator +{ + struct grub_relocator_chunk *chunks; + grub_addr_t postchunks; + grub_addr_t highestaddr; + grub_addr_t highestnonpostaddr; + grub_size_t relocators_size; +}; + +struct grub_relocator_chunk +{ + struct grub_relocator_chunk *next; + grub_addr_t src; + grub_addr_t target; + grub_size_t size; +}; struct grub_relocator * grub_relocator_new (void) @@ -591,9 +607,11 @@ grub_err_t grub_relocator_prepare_relocs (struct grub_relocator *rel, grub_addr_t addr, grub_addr_t *relstart) { - struct grub_relocator_chunk *chunk; grub_addr_t rels; grub_addr_t rels0; + struct grub_relocator_chunk *sorted; + grub_size_t nchunks = 0; + unsigned j; grub_dprintf ("relocator", "Preparing relocs (size=%ld)\n", (unsigned long) rel->relocators_size); @@ -605,29 +623,84 @@ grub_relocator_prepare_relocs (struct grub_relocator *rel, grub_addr_t addr, rels = rels0; grub_dprintf ("relocator", "Relocs allocated\n"); + + { + unsigned i; + grub_size_t count[257]; + struct grub_relocator_chunk *from, *to, *tmp; + + grub_memset (count, 0, sizeof (count)); - for (chunk = rel->chunks; chunk; chunk = chunk->next) { - grub_dprintf ("relocator", "chunk %p->%p\n", - (void *) chunk->src, (void *) chunk->target); - if (chunk->src < chunk->target) + struct grub_relocator_chunk *chunk; + for (chunk = rel->chunks; chunk; chunk = chunk->next) + { + grub_dprintf ("relocator", "chunk %p->%p, 0x%x\n", + (void *) chunk->src, (void *) chunk->target, + chunk->size); + nchunks++; + count[(chunk->src & 0xff) + 1]++; + } + } + from = grub_malloc (nchunks * sizeof (sorted[0])); + to = grub_malloc (nchunks * sizeof (sorted[0])); + if (!from || !to) + { + grub_free (from); + grub_free (to); + return grub_errno; + } + + for (j = 0; j < 256; j++) + count[j+1] += count[j]; + + { + struct grub_relocator_chunk *chunk; + for (chunk = rel->chunks; chunk; chunk = chunk->next) + from[count[chunk->src & 0xff]++] = *chunk; + } + + for (i = 1; i < GRUB_CPU_SIZEOF_VOID_P; i++) + { + grub_memset (count, 0, sizeof (count)); + for (j = 0; j < nchunks; j++) + count[((from[j].src >> (8 * i)) & 0xff) + 1]++; + for (j = 0; j < 256; j++) + count[j+1] += count[j]; + for (j = 0; j < nchunks; j++) + to[count[(from[j].src >> (8 * i)) & 0xff]++] = from[j]; + tmp = to; + to = from; + from = tmp; + } + sorted = from; + grub_free (to); + } + + for (j = 0; j < nchunks; j++) + { + grub_dprintf ("relocator", "sorted chunk %p->%p, 0x%x\n", + (void *) sorted[j].src, (void *) sorted[j].target, + sorted[j].size); + if (sorted[j].src < sorted[j].target) { grub_cpu_relocator_backward ((void *) rels, - (void *) chunk->src, - (void *) chunk->target, - chunk->size); + (void *) sorted[j].src, + (void *) sorted[j].target, + sorted[j].size); rels += grub_relocator_backward_size; } - if (chunk->src > chunk->target) + if (sorted[j].src > sorted[j].target) { grub_cpu_relocator_forward ((void *) rels, - (void *) chunk->src, - (void *) chunk->target, - chunk->size); + (void *) sorted[j].src, + (void *) sorted[j].target, + sorted[j].size); rels += grub_relocator_forward_size; } } grub_cpu_relocator_jumper ((void *) rels, addr); *relstart = rels0; + grub_free (sorted); return GRUB_ERR_NONE; } From b14620812f8af7a2f9e5f740e2ae047b321d3197 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 17 Jan 2010 19:22:36 +0100 Subject: [PATCH 063/178] Enable serial on all i386.rmk --- conf/i386-coreboot.rmk | 8 +------- conf/i386-pc.rmk | 7 +------ conf/i386.rmk | 6 ++++++ term/i386/pc/serial.c | 3 +-- 4 files changed, 9 insertions(+), 15 deletions(-) diff --git a/conf/i386-coreboot.rmk b/conf/i386-coreboot.rmk index 2c1174459..2e835e814 100644 --- a/conf/i386-coreboot.rmk +++ b/conf/i386-coreboot.rmk @@ -102,7 +102,7 @@ bin_SCRIPTS += grub-mkrescue grub_mkrescue_SOURCES = util/grub-mkrescue.in # Modules. -pkglib_MODULES = play.mod serial.mod \ +pkglib_MODULES = play.mod \ memdisk.mod pci.mod lspci.mod reboot.mod \ halt.mod datetime.mod date.mod datehook.mod \ lsmmap.mod mmap.mod @@ -129,12 +129,6 @@ halt_mod_SOURCES = commands/halt.c halt_mod_CFLAGS = $(COMMON_CFLAGS) halt_mod_LDFLAGS = $(COMMON_LDFLAGS) -# For serial.mod. -serial_mod_SOURCES = term/i386/pc/serial.c -serial_mod_CFLAGS = $(COMMON_CFLAGS) -serial_mod_LDFLAGS = $(COMMON_LDFLAGS) - - # For play.mod. play_mod_SOURCES = commands/i386/pc/play.c play_mod_CFLAGS = $(COMMON_CFLAGS) diff --git a/conf/i386-pc.rmk b/conf/i386-pc.rmk index 8de04e283..24d7a7cf2 100644 --- a/conf/i386-pc.rmk +++ b/conf/i386-pc.rmk @@ -117,7 +117,7 @@ grub_mkrescue_SOURCES = util/grub-mkrescue.in pkglib_MODULES = biosdisk.mod chain.mod \ reboot.mod halt.mod \ - vbe.mod vbetest.mod vbeinfo.mod play.mod serial.mod \ + vbe.mod vbetest.mod vbeinfo.mod play.mod \ vga.mod memdisk.mod pci.mod lspci.mod \ pxe.mod pxecmd.mod datetime.mod date.mod \ datehook.mod lsmmap.mod ata_pthru.mod hdparm.mod \ @@ -191,11 +191,6 @@ halt_mod_SOURCES = commands/i386/pc/halt.c halt_mod_CFLAGS = $(COMMON_CFLAGS) halt_mod_LDFLAGS = $(COMMON_LDFLAGS) -# For serial.mod. -serial_mod_SOURCES = term/i386/pc/serial.c -serial_mod_CFLAGS = $(COMMON_CFLAGS) -serial_mod_LDFLAGS = $(COMMON_LDFLAGS) - # For vbe.mod. vbe_mod_SOURCES = video/i386/pc/vbe.c vbe_mod_CFLAGS = $(COMMON_CFLAGS) diff --git a/conf/i386.rmk b/conf/i386.rmk index 73ca95764..110a40ef4 100644 --- a/conf/i386.rmk +++ b/conf/i386.rmk @@ -62,3 +62,9 @@ multiboot2_mod_SOURCES = loader/i386/multiboot.c \ multiboot2_mod_CFLAGS = $(COMMON_CFLAGS) -DGRUB_USE_MULTIBOOT2 multiboot2_mod_LDFLAGS = $(COMMON_LDFLAGS) multiboot2_mod_ASFLAGS = $(COMMON_ASFLAGS) + +# For serial.mod. +pkglib_MODULES += serial.mod +serial_mod_SOURCES = term/i386/pc/serial.c +serial_mod_CFLAGS = $(COMMON_CFLAGS) +serial_mod_LDFLAGS = $(COMMON_LDFLAGS) diff --git a/term/i386/pc/serial.c b/term/i386/pc/serial.c index b94b09553..135bb6cc5 100644 --- a/term/i386/pc/serial.c +++ b/term/i386/pc/serial.c @@ -17,8 +17,7 @@ */ #include -#include -#include +#include #include #include #include From ad8e99ec20d8bcddbdb772751b08e8758a1be587 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 17 Jan 2010 23:36:45 +0100 Subject: [PATCH 064/178] bootcheck support --- conf/common.rmk | 5 +++ conf/i386-pc.rmk | 10 ++++++ conf/i386.rmk | 21 +++++++++++++ tests/boot/linux.cfg | 3 ++ tests/boot/linux16.cfg | 3 ++ tests/boot/linuxinit-i386.S | 55 +++++++++++++++++++++++++++++++++ tests/boot/linuxinit-x86_64.S | 54 +++++++++++++++++++++++++++++++++ tests/util/grub-shell.in | 57 +++++++++++++++++++++-------------- 8 files changed, 185 insertions(+), 23 deletions(-) create mode 100644 tests/boot/linux.cfg create mode 100644 tests/boot/linux16.cfg create mode 100644 tests/boot/linuxinit-i386.S create mode 100644 tests/boot/linuxinit-x86_64.S diff --git a/conf/common.rmk b/conf/common.rmk index 31b62892b..905024a55 100644 --- a/conf/common.rmk +++ b/conf/common.rmk @@ -695,4 +695,9 @@ bin_UTILITIES += grub-mkpasswd-pbkdf2 grub_mkpasswd_pbkdf2_SOURCES = gnulib/progname.c util/grub-mkpasswd-pbkdf2.c lib/crypto.c lib/libgcrypt-grub/cipher/sha512.c lib/pbkdf2.c util/misc.c kern/err.c grub_mkpasswd_pbkdf2_CFLAGS += -Wno-missing-field-initializers -Wno-error -I$(srcdir)/lib/libgcrypt_wrap -DGRUB_MKPASSWD=1 +# Randomly generated +SUCCESSFUL_BOOT_STRING=3e49994fd5d82b7c9298d672d774080d + +bootcheck: $(BOOTCHECKS) + include $(srcdir)/conf/gcry.mk diff --git a/conf/i386-pc.rmk b/conf/i386-pc.rmk index a89203dea..33575a87c 100644 --- a/conf/i386-pc.rmk +++ b/conf/i386-pc.rmk @@ -368,5 +368,15 @@ pkglib_DATA += efiemu32.o efiemu64.o endif +BOOTTARGET=bios-cd + +bootcheck-linux16-i386: linux-initramfs.i386 $(GRUB_PAYLOADS_DIR)/linux.i386 $(srcdir)/tests/boot/linux.cfg grub-shell + timeout -s KILL 30s ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-i386 --files=/initrd=linux-initramfs.i386 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.i386 $(srcdir)/tests/boot/linux16.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + +bootcheck-linux16-x86_64: linux-initramfs.x86_64 $(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/tests/boot/linux.cfg grub-shell + timeout -s KILL 30s ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/initrd=linux-initramfs.x86_64 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/tests/boot/linux16.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + +BOOTCHECKS+=bootcheck-linux16-i386 bootcheck-linux16-x86_64 + include $(srcdir)/conf/i386.mk include $(srcdir)/conf/common.mk diff --git a/conf/i386.rmk b/conf/i386.rmk index 7ef337c61..9305bf09c 100644 --- a/conf/i386.rmk +++ b/conf/i386.rmk @@ -41,3 +41,24 @@ multiboot2_mod_SOURCES = loader/i386/multiboot.c \ multiboot2_mod_CFLAGS = $(COMMON_CFLAGS) -DGRUB_USE_MULTIBOOT2 multiboot2_mod_LDFLAGS = $(COMMON_LDFLAGS) multiboot2_mod_ASFLAGS = $(COMMON_ASFLAGS) + +linuxinit.x86_64: $(srcdir)/tests/boot/linuxinit-x86_64.S + $(TARGET_CC) -o $@ $< -m64 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" + +linuxinit.i386: $(srcdir)/tests/boot/linuxinit-i386.S + $(TARGET_CC) -o $@ $< -m32 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" + +linux-initramfs.%: linuxinit.% Makefile + TDIR=`mktemp -d`; (cp $< $$TDIR/init; cd $$TDIR; echo ./init | cpio --quiet --dereference -o -H newc) | gzip > $@; rm -rf $$TDIR + +CLEANFILES += linuxinit.i386 linuxinit.x86_64 linux-initramfs.i386 linux-initramfs.x86_64 + +bootcheck-linux-i386: linux-initramfs.i386 $(GRUB_PAYLOADS_DIR)/linux.i386 $(srcdir)/tests/boot/linux.cfg grub-shell + timeout -s KILL 30s ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-i386 --files=/initrd=linux-initramfs.i386 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.i386 $(srcdir)/tests/boot/linux.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + +bootcheck-linux-x86_64: linux-initramfs.x86_64 $(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/tests/boot/linux.cfg grub-shell + timeout -s KILL 30s ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/initrd=linux-initramfs.x86_64 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/tests/boot/linux.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + +BOOTCHECKS+=bootcheck-linux-i386 bootcheck-linux-x86_64 + +.PHONY: bootcheck-linux-i386 bootcheck-linux-x86_64 diff --git a/tests/boot/linux.cfg b/tests/boot/linux.cfg new file mode 100644 index 000000000..201de99fd --- /dev/null +++ b/tests/boot/linux.cfg @@ -0,0 +1,3 @@ +linux /linux console=ttyS0 root=/dev/ram0 +initrd /initrd +boot diff --git a/tests/boot/linux16.cfg b/tests/boot/linux16.cfg new file mode 100644 index 000000000..ed0a50872 --- /dev/null +++ b/tests/boot/linux16.cfg @@ -0,0 +1,3 @@ +linux16 /linux console=ttyS0 root=/dev/ram0 +initrd16 /initrd +boot diff --git a/tests/boot/linuxinit-i386.S b/tests/boot/linuxinit-i386.S new file mode 100644 index 000000000..a79a5787e --- /dev/null +++ b/tests/boot/linuxinit-i386.S @@ -0,0 +1,55 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2010 Free Software Foundation, Inc. + * + * GRUB 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. + * + * GRUB 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 GRUB. If not, see . + */ + +#define SYSCALL_WRITE 4 +#define SYSCALL_RESET 88 +#define SYSCALL_EXIT 1 +#define SYSCALL_INT 0x80 + +#define STDOUT 1 +#define SHUTDOWN_MAGIC1 0xfee1dead +#define SHUTDOWN_MAGIC2 0x28121969 +#define SHUTDOWN_MAGIC3 0x4321fedc + + .text + .global start, _start +_start: +start: + /* write. */ + movl $SYSCALL_WRITE, %eax + movl $STDOUT, %ebx + leal message, %ecx + movl $(messageend-message), %edx + int $SYSCALL_INT + + /* shutdown. */ + movl $SYSCALL_RESET, %eax + movl $SHUTDOWN_MAGIC1, %ebx + movl $SHUTDOWN_MAGIC2, %ecx + movl $SHUTDOWN_MAGIC3, %edx + int $SYSCALL_INT + + /* exit (1). Shouldn't be reached. */ + movl $SYSCALL_EXIT, %eax + movl $1, %ebx + int $SYSCALL_INT + .data +message: + .ascii "Boot Test Passed Successfully\n" SUCCESSFUL_BOOT_STRING "\n" +messageend: + \ No newline at end of file diff --git a/tests/boot/linuxinit-x86_64.S b/tests/boot/linuxinit-x86_64.S new file mode 100644 index 000000000..17ba8040c --- /dev/null +++ b/tests/boot/linuxinit-x86_64.S @@ -0,0 +1,54 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2010 Free Software Foundation, Inc. + * + * GRUB 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. + * + * GRUB 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 GRUB. If not, see . + */ + +#define SYSCALL_WRITE 1 +#define SYSCALL_RESET 169 +#define SYSCALL_EXIT 60 + +#define STDOUT 1 +#define SHUTDOWN_MAGIC1 0xfee1dead +#define SHUTDOWN_MAGIC2 0x28121969 +#define SHUTDOWN_MAGIC3 0x4321fedc + + .text + .global start, _start +_start: +start: + /* write. */ + movq $SYSCALL_WRITE, %rax + movq $STDOUT, %rdi + leaq message, %rsi + movq $(messageend-message), %rdx + syscall + + /* shutdown. */ + movq $SYSCALL_RESET, %rax + movq $SHUTDOWN_MAGIC1, %rdi + movq $SHUTDOWN_MAGIC2, %rsi + movq $SHUTDOWN_MAGIC3, %rdx + syscall + + /* exit(1). Shouldn't be reached. */ + movq $SYSCALL_EXIT, %rax + movq $1, %rdi + syscall + .data +message: + .ascii "Boot Test Passed Successfully\n" SUCCESSFUL_BOOT_STRING "\n" +messageend: + \ No newline at end of file diff --git a/tests/util/grub-shell.in b/tests/util/grub-shell.in index e6fef8313..ee0cded55 100644 --- a/tests/util/grub-shell.in +++ b/tests/util/grub-shell.in @@ -44,7 +44,9 @@ Run GRUB script in a Qemu instance. -v, --version print the version information and exit --boot=[fd|hd|cd] boot method for Qemu instance --modules=MODULES pre-load specified modules MODULES + --qemu=FILE Name of qemu binary --qemu-opts=OPTIONS extra options to pass to Qemu instance + --files=FILES add files to the image $0 runs input GRUB script or SOURCE file in a Qemu instance and prints its output. @@ -53,6 +55,9 @@ Report bugs to . EOF } +boot=bios-hd +qemu=qemu-system-i386 + # Check the arguments. for option in "$@"; do case "$option" in @@ -65,14 +70,19 @@ for option in "$@"; do --modules=*) ms=`echo "$option" | sed -e 's/--modules=//' -e 's/,/ /g'` modules="$modules $ms" ;; + --files=*) + fls=`echo "$option" | sed -e 's/--files=//' -e 's/,/ /g'` + files="$files $fls" ;; + --qemu=*) + qemu=`echo "$option" | sed -e 's/--qemu=//' -e 's/,/ /g'`;; --qemu-opts=*) qs=`echo "$option" | sed -e 's/--qemu-opts=//'` qemuopts="$qemuopts $qs" ;; --boot=*) dev=`echo "$option" | sed -e 's/--boot=//'` - if [ "$dev" = "fd" ] ; then bootdev=a; - elif [ "$dev" = "hd" ] ; then bootdev=c; - elif [ "$dev" = "cd" ] ; then bootdev=d; + if [ "$dev" = "bios-fd" ] ; then boot=bios-fd; + elif [ "$dev" = "bios-hd" ] ; then boot=bios-hd; + elif [ "$dev" = "bios-cd" ] ; then boot=bios-cd; else echo "Unrecognized boot method \`$dev'" 1>&2 usage @@ -100,10 +110,6 @@ if [ "x${source}" = x ] ; then source=${tmpfile} fi -if [ "x${bootdev}" = x ] ; then - bootdev=c # default is boot as disk image -fi - cfgfile=`mktemp` cat <${cfgfile} grubshell=yes @@ -123,23 +129,28 @@ source /boot/grub/testcase.cfg halt EOF -isofile=`mktemp` -grub-mkrescue --output=${isofile} --override-directory=${builddir} \ - /boot/grub/grub.cfg=${cfgfile} /boot/grub/testcase.cfg=${source} \ - >/dev/null 2>&1 +if [ x$boot = xbios-hd ] || [ x$boot = xbios-fd ] || [ x$boot = xbios-cd ]; then + isofile=`mktemp` + grub-mkrescue --output=${isofile} --override-directory=${builddir} \ + /boot/grub/grub.cfg=${cfgfile} /boot/grub/testcase.cfg=${source} \ + ${files} >/dev/null 2>&1 + if [ x$boot = xbios-hd ]; then + device=hda + bootdev=c + fi + if [ x$boot = xbios-cd ]; then + device=cdrom + bootdev=d + fi + if [ x$boot = xbios-fd ]; then + device=fda + bootdev=a + fi + ${qemu} ${qemuopts} -nographic -serial file:/dev/stdout -monitor file:/dev/null -${device} ${isofile} -boot ${bootdev} | tr -d "\r" + rm -f ${isofile} +fi -hdafile=`mktemp` -cp ${isofile} ${hdafile} - -fdafile=`mktemp` -cp ${isofile} ${fdafile} - -outfile=`mktemp` -qemu-system-i386 ${qemuopts} -nographic -serial stdio -hda ${hdafile} -fda ${fdafile} -cdrom ${isofile} -boot ${bootdev} | tr -d "\r" >${outfile} - -cat $outfile - -rm -f ${tmpfile} ${outfile} ${cfgfile} ${isofile} ${hdafile} ${fdafile} +rm -f ${tmpfile} ${cfgfile} exit 0 From 5d615a77ce6af16ce4ee36f1ff54992df311d853 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Mon, 18 Jan 2010 11:14:04 +0100 Subject: [PATCH 065/178] kfreebsd-i386 boot test support --- conf/common.rmk | 1 + conf/i386-pc.rmk | 4 +- conf/i386.rmk | 26 +++++-- tests/boot/kfreebsd.cfg | 6 ++ tests/boot/kfreebsd.init-i386.S | 74 +++++++++++++++++++ tests/boot/linux.cfg | 2 + .../{linuxinit-i386.S => linux.init-i386.S} | 0 tests/boot/linux16.cfg | 2 + 8 files changed, 105 insertions(+), 10 deletions(-) create mode 100644 tests/boot/kfreebsd.cfg create mode 100644 tests/boot/kfreebsd.init-i386.S rename tests/boot/{linuxinit-i386.S => linux.init-i386.S} (100%) diff --git a/conf/common.rmk b/conf/common.rmk index 905024a55..14fe54200 100644 --- a/conf/common.rmk +++ b/conf/common.rmk @@ -697,6 +697,7 @@ grub_mkpasswd_pbkdf2_CFLAGS += -Wno-missing-field-initializers -Wno-error -I$(sr # Randomly generated SUCCESSFUL_BOOT_STRING=3e49994fd5d82b7c9298d672d774080d +BOOTCHECK_TIMEOUT=60 bootcheck: $(BOOTCHECKS) diff --git a/conf/i386-pc.rmk b/conf/i386-pc.rmk index 33575a87c..f01cba620 100644 --- a/conf/i386-pc.rmk +++ b/conf/i386-pc.rmk @@ -371,10 +371,10 @@ endif BOOTTARGET=bios-cd bootcheck-linux16-i386: linux-initramfs.i386 $(GRUB_PAYLOADS_DIR)/linux.i386 $(srcdir)/tests/boot/linux.cfg grub-shell - timeout -s KILL 30s ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-i386 --files=/initrd=linux-initramfs.i386 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.i386 $(srcdir)/tests/boot/linux16.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-i386 --files=/initrd=linux-initramfs.i386 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.i386 $(srcdir)/tests/boot/linux16.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null bootcheck-linux16-x86_64: linux-initramfs.x86_64 $(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/tests/boot/linux.cfg grub-shell - timeout -s KILL 30s ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/initrd=linux-initramfs.x86_64 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/tests/boot/linux16.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/initrd=linux-initramfs.x86_64 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/tests/boot/linux16.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null BOOTCHECKS+=bootcheck-linux16-i386 bootcheck-linux16-x86_64 diff --git a/conf/i386.rmk b/conf/i386.rmk index 9305bf09c..3743191be 100644 --- a/conf/i386.rmk +++ b/conf/i386.rmk @@ -45,20 +45,30 @@ multiboot2_mod_ASFLAGS = $(COMMON_ASFLAGS) linuxinit.x86_64: $(srcdir)/tests/boot/linuxinit-x86_64.S $(TARGET_CC) -o $@ $< -m64 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" -linuxinit.i386: $(srcdir)/tests/boot/linuxinit-i386.S +linux.init.i386: $(srcdir)/tests/boot/linux.init-i386.S $(TARGET_CC) -o $@ $< -m32 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" -linux-initramfs.%: linuxinit.% Makefile - TDIR=`mktemp -d`; (cp $< $$TDIR/init; cd $$TDIR; echo ./init | cpio --quiet --dereference -o -H newc) | gzip > $@; rm -rf $$TDIR +kfreebsd.init.i386: $(srcdir)/tests/boot/kfreebsd.init-i386.S + $(TARGET_CC) -o $@ $< -m32 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" && freebsd-brandelf -t FreeBSD $@ + +linux-initramfs.%: linux.init.% Makefile + TDIR=`mktemp -d`; cp $< $$TDIR/init; (cd $$TDIR; echo ./init | cpio --quiet --dereference -o -H newc) | gzip > $@; rm -rf $$TDIR + +kfreebsd-mfsroot.%: kfreebsd.init.% Makefile + TDIR=`mktemp -d`; mkdir $$TDIR/dev; mkdir $$TDIR/sbin; cp $< $$TDIR/sbin/init; makefs -t ffs -s 30m -f 1000 -o minfree=0,version=1 $@ $$TDIR; rm -rf $$TDIR + +CLEANFILES += linux.init.i386 kfreebsd.init.i386 linux.init.x86_64 linux-initramfs.i386 linux-initramfs.x86_64 + +bootcheck-kfreebsd-i386: kfreebsd-mfsroot.i386 $(GRUB_PAYLOADS_DIR)/kfreebsd.i386 $(GRUB_PAYLOADS_DIR)/kfreebsd_env.i386 $(srcdir)/tests/boot/kfreebsd.cfg grub-shell + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-i386 --files=/mfsroot=kfreebsd-mfsroot.i386 --files=/kfreebsd=$(GRUB_PAYLOADS_DIR)/kfreebsd.i386 --files=/kfreebsd_env=$(GRUB_PAYLOADS_DIR)/kfreebsd_env.i386 $(srcdir)/tests/boot/kfreebsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null -CLEANFILES += linuxinit.i386 linuxinit.x86_64 linux-initramfs.i386 linux-initramfs.x86_64 bootcheck-linux-i386: linux-initramfs.i386 $(GRUB_PAYLOADS_DIR)/linux.i386 $(srcdir)/tests/boot/linux.cfg grub-shell - timeout -s KILL 30s ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-i386 --files=/initrd=linux-initramfs.i386 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.i386 $(srcdir)/tests/boot/linux.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-i386 --files=/initrd=linux-initramfs.i386 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.i386 $(srcdir)/tests/boot/linux.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null bootcheck-linux-x86_64: linux-initramfs.x86_64 $(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/tests/boot/linux.cfg grub-shell - timeout -s KILL 30s ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/initrd=linux-initramfs.x86_64 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/tests/boot/linux.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/initrd=linux-initramfs.x86_64 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/tests/boot/linux.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null -BOOTCHECKS+=bootcheck-linux-i386 bootcheck-linux-x86_64 +BOOTCHECKS+=bootcheck-linux-i386 bootcheck-linux-x86_64 bootcheck-kfreebsd-i386 -.PHONY: bootcheck-linux-i386 bootcheck-linux-x86_64 +.PHONY: bootcheck-linux-i386 bootcheck-linux-x86_64 bootcheck-kfreebsd-i386 diff --git a/tests/boot/kfreebsd.cfg b/tests/boot/kfreebsd.cfg new file mode 100644 index 000000000..71b97b67e --- /dev/null +++ b/tests/boot/kfreebsd.cfg @@ -0,0 +1,6 @@ +kfreebsd /kfreebsd -h +kfreebsd_loadenv /kfreebsd_env +kfreebsd_module /mfsroot type=mfs_root +boot +# Shouln't happen +halt diff --git a/tests/boot/kfreebsd.init-i386.S b/tests/boot/kfreebsd.init-i386.S new file mode 100644 index 000000000..8812b650b --- /dev/null +++ b/tests/boot/kfreebsd.init-i386.S @@ -0,0 +1,74 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2010 Free Software Foundation, Inc. + * + * GRUB 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. + * + * GRUB 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 GRUB. If not, see . + */ + +#define MODE_RDRW 2 +#define FLAGS_NONE 0 +#define SYSCALL_OPEN 5 +#define SYSCALL_WRITE 4 +#define SYSCALL_RESET 55 +#define SYSCALL_EXIT 1 +#define SYSCALL_INT 0x80 + +#define RESET_NOSYNC 0x4 +#define RESET_HALT 0x8 +#define RESET_POWEROFF 0x4000 + + .section ".init", "ax" + .global start,_start +start: +_start: + /* open. */ + movl $SYSCALL_OPEN, %eax + pushl $FLAGS_NONE + pushl $MODE_RDRW + leal device, %ebx + pushl %ebx + pushl $0 + int $SYSCALL_INT + addl $16, %esp + movl %eax, %ecx + + /* write. */ + movl $SYSCALL_WRITE, %eax + pushl $(messageend-message) + leal message, %ebx + pushl %ebx + pushl %ecx + pushl $0 + int $SYSCALL_INT + addl $16, %esp + + /* shutdown. */ + movl $SYSCALL_RESET, %eax + pushl $(RESET_POWEROFF|RESET_HALT|RESET_NOSYNC) + pushl $0 + int $SYSCALL_INT + addl $8, %esp + + /* exit (1). Shouldn't be reached. */ + movl $SYSCALL_EXIT, %eax + pushl $1 + pushl $0 + int $SYSCALL_INT +device: + .ascii "/dev/console" + .byte 0 +message: + .ascii "Boot Test Passed Successfully\n" SUCCESSFUL_BOOT_STRING "\n" +messageend: + \ No newline at end of file diff --git a/tests/boot/linux.cfg b/tests/boot/linux.cfg index 201de99fd..f5bf6ac7d 100644 --- a/tests/boot/linux.cfg +++ b/tests/boot/linux.cfg @@ -1,3 +1,5 @@ linux /linux console=ttyS0 root=/dev/ram0 initrd /initrd boot +# Shouln't happen +halt diff --git a/tests/boot/linuxinit-i386.S b/tests/boot/linux.init-i386.S similarity index 100% rename from tests/boot/linuxinit-i386.S rename to tests/boot/linux.init-i386.S diff --git a/tests/boot/linux16.cfg b/tests/boot/linux16.cfg index ed0a50872..d7fbf961c 100644 --- a/tests/boot/linux16.cfg +++ b/tests/boot/linux16.cfg @@ -1,3 +1,5 @@ linux16 /linux console=ttyS0 root=/dev/ram0 initrd16 /initrd boot +# Shouln't happen +halt From 0db3ae3ce643451359c42515149c576be11177e9 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Mon, 18 Jan 2010 11:38:28 +0100 Subject: [PATCH 066/178] Add bootcheck for kfreebsd-x86_64 --- conf/i386.rmk | 11 +++- tests/boot/kfreebsd.init-x86_64.S | 62 +++++++++++++++++++ ...linuxinit-x86_64.S => linux.init-x86_64.S} | 0 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 tests/boot/kfreebsd.init-x86_64.S rename tests/boot/{linuxinit-x86_64.S => linux.init-x86_64.S} (100%) diff --git a/conf/i386.rmk b/conf/i386.rmk index 3743191be..919337e2a 100644 --- a/conf/i386.rmk +++ b/conf/i386.rmk @@ -48,6 +48,9 @@ linuxinit.x86_64: $(srcdir)/tests/boot/linuxinit-x86_64.S linux.init.i386: $(srcdir)/tests/boot/linux.init-i386.S $(TARGET_CC) -o $@ $< -m32 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" +kfreebsd.init.x86_64: $(srcdir)/tests/boot/kfreebsd.init-x86_64.S + $(TARGET_CC) -o $@ $< -m64 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" && freebsd-brandelf -t FreeBSD $@ + kfreebsd.init.i386: $(srcdir)/tests/boot/kfreebsd.init-i386.S $(TARGET_CC) -o $@ $< -m32 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" && freebsd-brandelf -t FreeBSD $@ @@ -62,6 +65,8 @@ CLEANFILES += linux.init.i386 kfreebsd.init.i386 linux.init.x86_64 linux-initram bootcheck-kfreebsd-i386: kfreebsd-mfsroot.i386 $(GRUB_PAYLOADS_DIR)/kfreebsd.i386 $(GRUB_PAYLOADS_DIR)/kfreebsd_env.i386 $(srcdir)/tests/boot/kfreebsd.cfg grub-shell timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-i386 --files=/mfsroot=kfreebsd-mfsroot.i386 --files=/kfreebsd=$(GRUB_PAYLOADS_DIR)/kfreebsd.i386 --files=/kfreebsd_env=$(GRUB_PAYLOADS_DIR)/kfreebsd_env.i386 $(srcdir)/tests/boot/kfreebsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null +bootcheck-kfreebsd-x86_64: kfreebsd-mfsroot.x86_64 $(GRUB_PAYLOADS_DIR)/kfreebsd.x86_64 $(GRUB_PAYLOADS_DIR)/kfreebsd_env.x86_64 $(srcdir)/tests/boot/kfreebsd.cfg grub-shell + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/mfsroot=kfreebsd-mfsroot.x86_64 --files=/kfreebsd=$(GRUB_PAYLOADS_DIR)/kfreebsd.x86_64 --files=/kfreebsd_env=$(GRUB_PAYLOADS_DIR)/kfreebsd_env.x86_64 $(srcdir)/tests/boot/kfreebsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null bootcheck-linux-i386: linux-initramfs.i386 $(GRUB_PAYLOADS_DIR)/linux.i386 $(srcdir)/tests/boot/linux.cfg grub-shell timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-i386 --files=/initrd=linux-initramfs.i386 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.i386 $(srcdir)/tests/boot/linux.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null @@ -69,6 +74,8 @@ bootcheck-linux-i386: linux-initramfs.i386 $(GRUB_PAYLOADS_DIR)/linux.i386 $(src bootcheck-linux-x86_64: linux-initramfs.x86_64 $(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/tests/boot/linux.cfg grub-shell timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/initrd=linux-initramfs.x86_64 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/tests/boot/linux.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null -BOOTCHECKS+=bootcheck-linux-i386 bootcheck-linux-x86_64 bootcheck-kfreebsd-i386 +BOOTCHECKS+=bootcheck-linux-i386 bootcheck-linux-x86_64 \ + bootcheck-kfreebsd-i386 bootcheck-kfreebsd-x86_64 -.PHONY: bootcheck-linux-i386 bootcheck-linux-x86_64 bootcheck-kfreebsd-i386 +.PHONY: bootcheck-linux-i386 bootcheck-linux-x86_64 \ + bootcheck-kfreebsd-i386 bootcheck-kfreebsd-x86_64 diff --git a/tests/boot/kfreebsd.init-x86_64.S b/tests/boot/kfreebsd.init-x86_64.S new file mode 100644 index 000000000..edff0d782 --- /dev/null +++ b/tests/boot/kfreebsd.init-x86_64.S @@ -0,0 +1,62 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2010 Free Software Foundation, Inc. + * + * GRUB 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. + * + * GRUB 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 GRUB. If not, see . + */ + +#define MODE_RDRW 2 +#define FLAGS_NONE 0 +#define SYSCALL_OPEN 5 +#define SYSCALL_WRITE 4 +#define SYSCALL_RESET 55 +#define SYSCALL_EXIT 1 + +#define RESET_NOSYNC 0x4 +#define RESET_HALT 0x8 +#define RESET_POWEROFF 0x4000 + + .section ".init", "ax" + .global start,_start +start: +_start: + /* open. */ + movq $SYSCALL_OPEN, %rax + leaq device, %rdi + movq $MODE_RDRW, %rsi + movq $FLAGS_NONE, %rdx + syscall + movq %rax, %rdi + + /* write. */ + movq $SYSCALL_WRITE, %rax + leaq message, %rsi + movq $(messageend-message), %rdx + syscall + + /* shutdown. */ + movq $SYSCALL_RESET, %rax + movq $(RESET_POWEROFF|RESET_HALT|RESET_NOSYNC), %rdi + syscall + + /* exit (1). Shouldn't be reached. */ + movq $SYSCALL_EXIT, %rax + movq $1, %rdi + syscall +device: + .ascii "/dev/console" + .byte 0 +message: + .ascii "Boot Test Passed Successfully\n" SUCCESSFUL_BOOT_STRING "\n" +messageend: diff --git a/tests/boot/linuxinit-x86_64.S b/tests/boot/linux.init-x86_64.S similarity index 100% rename from tests/boot/linuxinit-x86_64.S rename to tests/boot/linux.init-x86_64.S From c5545cf8baa2220d20fe6b861bf3dd47e08b5c9a Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Mon, 18 Jan 2010 12:00:00 +0100 Subject: [PATCH 067/178] Fix linux-x86_64 bootchecks --- conf/i386.rmk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conf/i386.rmk b/conf/i386.rmk index 919337e2a..515c50fc6 100644 --- a/conf/i386.rmk +++ b/conf/i386.rmk @@ -42,7 +42,7 @@ multiboot2_mod_CFLAGS = $(COMMON_CFLAGS) -DGRUB_USE_MULTIBOOT2 multiboot2_mod_LDFLAGS = $(COMMON_LDFLAGS) multiboot2_mod_ASFLAGS = $(COMMON_ASFLAGS) -linuxinit.x86_64: $(srcdir)/tests/boot/linuxinit-x86_64.S +linux.init.x86_64: $(srcdir)/tests/boot/linux.init-x86_64.S $(TARGET_CC) -o $@ $< -m64 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" linux.init.i386: $(srcdir)/tests/boot/linux.init-i386.S From 3de254033ca9d010fbaedb0ea12f4e84e049e46b Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Mon, 18 Jan 2010 12:01:13 +0100 Subject: [PATCH 068/178] Fix x86_64-efi compilation --- lib/relocator.c | 8 ++++---- loader/i386/bsd.c | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/relocator.c b/lib/relocator.c index a02bca332..0644691cd 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -635,9 +635,9 @@ grub_relocator_prepare_relocs (struct grub_relocator *rel, grub_addr_t addr, struct grub_relocator_chunk *chunk; for (chunk = rel->chunks; chunk; chunk = chunk->next) { - grub_dprintf ("relocator", "chunk %p->%p, 0x%x\n", + grub_dprintf ("relocator", "chunk %p->%p, 0x%lx\n", (void *) chunk->src, (void *) chunk->target, - chunk->size); + (unsigned long) chunk->size); nchunks++; count[(chunk->src & 0xff) + 1]++; } @@ -679,9 +679,9 @@ grub_relocator_prepare_relocs (struct grub_relocator *rel, grub_addr_t addr, for (j = 0; j < nchunks; j++) { - grub_dprintf ("relocator", "sorted chunk %p->%p, 0x%x\n", + grub_dprintf ("relocator", "sorted chunk %p->%p, 0x%lx\n", (void *) sorted[j].src, (void *) sorted[j].target, - sorted[j].size); + (unsigned long) sorted[j].size); if (sorted[j].src < sorted[j].target) { grub_cpu_relocator_backward ((void *) rels, diff --git a/loader/i386/bsd.c b/loader/i386/bsd.c index 28f1c685a..d019f21db 100644 --- a/loader/i386/bsd.c +++ b/loader/i386/bsd.c @@ -351,7 +351,7 @@ grub_bsd_add_mmap (void) generate_e820_mmap (NULL, &cnt, buf); - grub_dprintf ("bsd", "%u entries in smap\n", cnt); + grub_dprintf ("bsd", "%u entries in smap\n", (unsigned) cnt); if (kernel_type == KERNEL_TYPE_NETBSD) grub_bsd_add_meta (NETBSD_BTINFO_MEMMAP, buf0, len); else From 5756bfe137343d93af6d1bad4f065be725808a63 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Mon, 18 Jan 2010 12:45:28 +0100 Subject: [PATCH 069/178] Fix regression in kfreebsd-i386 --- loader/i386/bsd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/loader/i386/bsd.c b/loader/i386/bsd.c index d019f21db..73fe8cc77 100644 --- a/loader/i386/bsd.c +++ b/loader/i386/bsd.c @@ -705,7 +705,7 @@ grub_freebsd_boot (void) grub_fatal ("cannot exit boot services"); #endif - grub_memcpy (&stack[8], &bi, sizeof (bi)); + grub_memcpy (&stack[9], &bi, sizeof (bi)); state.eip = entry; state.esp = stack_target; stack[0] = entry; /* "Return" address. */ From 935842dd5666058010967e85b7522c256ace0234 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Mon, 18 Jan 2010 13:32:56 +0100 Subject: [PATCH 070/178] Always put smap after kern_end for freebsd --- loader/i386/bsd.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/loader/i386/bsd.c b/loader/i386/bsd.c index 73fe8cc77..7f9617543 100644 --- a/loader/i386/bsd.c +++ b/loader/i386/bsd.c @@ -220,6 +220,25 @@ grub_bsd_add_meta (grub_uint32_t type, void *data, grub_uint32_t len) newtag->next = NULL; if (len) grub_memcpy (newtag->data, data, len); + + if (kernel_type == KERNEL_TYPE_FREEBSD + && type == (FREEBSD_MODINFO_METADATA | FREEBSD_MODINFOMD_SMAP)) + { + struct bsd_tag *p; + for (p = tags; + p->type != (FREEBSD_MODINFO_METADATA | FREEBSD_MODINFOMD_KERNEND); + p = p->next); + + if (p) + { + newtag->next = p->next; + p->next = newtag; + if (newtag->next == NULL) + tags_last = newtag; + return GRUB_ERR_NONE; + } + } + if (tags_last) tags_last->next = newtag; else From 1ef7e2992dd55808333ad1acc8ebe77bd962e5a7 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Mon, 18 Jan 2010 16:03:57 +0100 Subject: [PATCH 071/178] =?UTF-8?q?Fix=20mismerge=20resulting=20in=20ghost?= =?UTF-8?q?=20multiboot=20module.=20Reported=20by:=20Gr=C3=A9goire=20Sutre?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- loader/i386/multiboot.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/loader/i386/multiboot.c b/loader/i386/multiboot.c index 8186e8cca..3c743c85f 100644 --- a/loader/i386/multiboot.c +++ b/loader/i386/multiboot.c @@ -303,11 +303,6 @@ grub_module (int argc, char *argv[]) if (err) goto fail; - err = grub_multiboot_add_module ((grub_addr_t) module, size, - argc - 1, argv + 1); - if (err) - goto fail; - if (grub_file_read (file, module, size) != size) { grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file"); From 5a6ff7ad4aa4c5d8637d1b5cb549758b620ede2c Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Mon, 18 Jan 2010 22:29:25 +0100 Subject: [PATCH 072/178] kfreebsd-i386 bootcheck --- conf/i386.rmk | 20 ++++- tests/boot/knetbsd.cfg | 5 ++ tests/boot/knetbsd.init-i386.S | 134 +++++++++++++++++++++++++++++++++ 3 files changed, 156 insertions(+), 3 deletions(-) create mode 100644 tests/boot/knetbsd.cfg create mode 100644 tests/boot/knetbsd.init-i386.S diff --git a/conf/i386.rmk b/conf/i386.rmk index b1adb13d9..c9ba18b11 100644 --- a/conf/i386.rmk +++ b/conf/i386.rmk @@ -81,14 +81,26 @@ kfreebsd.init.x86_64: $(srcdir)/tests/boot/kfreebsd.init-x86_64.S kfreebsd.init.i386: $(srcdir)/tests/boot/kfreebsd.init-i386.S $(TARGET_CC) -o $@ $< -m32 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" && freebsd-brandelf -t FreeBSD $@ -linux-initramfs.%: linux.init.% Makefile +knetbsd.init.i386: $(srcdir)/tests/boot/knetbsd.init-i386.S + $(TARGET_CC) -o $@ $< -m32 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" + +linux-initramfs.%: linux.init.% TDIR=`mktemp -d`; cp $< $$TDIR/init; (cd $$TDIR; echo ./init | cpio --quiet --dereference -o -H newc) | gzip > $@; rm -rf $$TDIR -kfreebsd-mfsroot.%: kfreebsd.init.% Makefile +kfreebsd-mfsroot.%: kfreebsd.init.% TDIR=`mktemp -d`; mkdir $$TDIR/dev; mkdir $$TDIR/sbin; cp $< $$TDIR/sbin/init; makefs -t ffs -s 30m -f 1000 -o minfree=0,version=1 $@ $$TDIR; rm -rf $$TDIR +knetbsd.image.%: knetbsd.init.% + TDIR=`mktemp -d` && mkdir $$TDIR/dev && mkdir $$TDIR/sbin && cp $< $$TDIR/sbin/init && makefs -t ffs -s 30m -f 1000 -o minfree=0,version=1 $@ $$TDIR && rm -rf $$TDIR + +knetbsd.miniroot-image.i386: knetbsd.image.i386 $(GRUB_PAYLOADS_DIR)/knetbsd.miniroot.i386 + $(OBJCOPY) --add-section=miniroot=$< $(GRUB_PAYLOADS_DIR)/knetbsd.miniroot.i386 $@ + CLEANFILES += linux.init.i386 kfreebsd.init.i386 linux.init.x86_64 linux-initramfs.i386 linux-initramfs.x86_64 +bootcheck-knetbsd-i386: knetbsd.miniroot-image.i386 $(GRUB_PAYLOADS_DIR)/knetbsd.i386 $(srcdir)/tests/boot/knetbsd.cfg knetbsd.miniroot-image.i386 grub-shell + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-i386 --files=/miniroot=knetbsd.miniroot-image.i386 --files=/knetbsd=$(GRUB_PAYLOADS_DIR)/knetbsd.i386 $(srcdir)/tests/boot/knetbsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + bootcheck-kfreebsd-i386: kfreebsd-mfsroot.i386 $(GRUB_PAYLOADS_DIR)/kfreebsd.i386 $(GRUB_PAYLOADS_DIR)/kfreebsd_env.i386 $(srcdir)/tests/boot/kfreebsd.cfg grub-shell timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-i386 --files=/mfsroot=kfreebsd-mfsroot.i386 --files=/kfreebsd=$(GRUB_PAYLOADS_DIR)/kfreebsd.i386 --files=/kfreebsd_env=$(GRUB_PAYLOADS_DIR)/kfreebsd_env.i386 $(srcdir)/tests/boot/kfreebsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null @@ -102,7 +114,9 @@ bootcheck-linux-x86_64: linux-initramfs.x86_64 $(GRUB_PAYLOADS_DIR)/linux.x86_64 timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/initrd=linux-initramfs.x86_64 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/tests/boot/linux.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null BOOTCHECKS+=bootcheck-linux-i386 bootcheck-linux-x86_64 \ - bootcheck-kfreebsd-i386 bootcheck-kfreebsd-x86_64 + bootcheck-kfreebsd-i386 bootcheck-kfreebsd-x86_64 \ + bootcheck-knetbsd-i386 .PHONY: bootcheck-linux-i386 bootcheck-linux-x86_64 \ bootcheck-kfreebsd-i386 bootcheck-kfreebsd-x86_64 + bootcheck-knetbsd-i386 \ No newline at end of file diff --git a/tests/boot/knetbsd.cfg b/tests/boot/knetbsd.cfg new file mode 100644 index 000000000..ad2258dce --- /dev/null +++ b/tests/boot/knetbsd.cfg @@ -0,0 +1,5 @@ +knetbsd /knetbsd -h +knetbsd_module_elf /miniroot +boot +# Shouln't happen +halt diff --git a/tests/boot/knetbsd.init-i386.S b/tests/boot/knetbsd.init-i386.S new file mode 100644 index 000000000..200aed01f --- /dev/null +++ b/tests/boot/knetbsd.init-i386.S @@ -0,0 +1,134 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2010 Free Software Foundation, Inc. + * + * GRUB 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. + * + * GRUB 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 GRUB. If not, see . + */ + +#define MODE_RDRW 2 +#define FLAGS_NONE 0 +#define SYSCALL_OPEN 5 +#define SYSCALL_WRITE 4 +#define SYSCALL_RESET 208 +#define SYSCALL_EXIT 1 +#define SYSCALL_MKNOD 14 +#define SYSCALL_MOUNT 410 +#define SYSCALL_INT 0x80 + +#define RESET_NOSYNC 0x4 +#define RESET_HALT 0x8 +#define RESET_POWEROFF 0x800 + + .section ".init", "ax" + .global start,_start +start: +_start: + /* mount. */ + movl $SYSCALL_MOUNT, %eax + push $(tmpfs_args_end - tmpfs_args) + push $tmpfs_args + push $0 + push $devfsdir + push $devfstype + pushl $0 + int $SYSCALL_INT + addl $20, %esp + + /* mknod. */ + movl $SYSCALL_MKNOD, %eax + pushl $0 + pushl $0x2140 + leal device, %ebx + pushl %ebx + pushl $0 + int $SYSCALL_INT + addl $16, %esp + + /* open. */ + movl $SYSCALL_OPEN, %eax + pushl $FLAGS_NONE + pushl $MODE_RDRW + leal device, %ebx + pushl %ebx + pushl $0 + int $SYSCALL_INT + addl $16, %esp + movl %eax, %ecx + + /* write. */ + movl $SYSCALL_WRITE, %eax + pushl $(messageend-message) + leal message, %ebx + pushl %ebx + pushl %ecx + pushl $0 + int $SYSCALL_INT + addl $16, %esp + + /* shutdown. */ + movl $SYSCALL_RESET, %eax + pushl $haltmsg + pushl $(RESET_POWEROFF|RESET_HALT|RESET_NOSYNC) + pushl $0 + int $SYSCALL_INT + addl $8, %esp + + /* exit (1). Shouldn't be reached. */ + movl $SYSCALL_EXIT, %eax + pushl $1 + pushl $0 + int $SYSCALL_INT + .section ".fini", "ax" +1: jmp 1b + .section ".text", "ax" +1: jmp 1b + /* This section is needed for NetBSD to identify the binary. */ + .section ".note.netbsd.ident", "a" + .long 0x7 + .long 0x4 + .long 0x1 + .ascii "NetBSD" + .byte 0 + .data +device: + .ascii "/dev/console" + .byte 0 +message: + .ascii "Boot Test Passed Successfully\n" SUCCESSFUL_BOOT_STRING "\n" +messageend: +haltmsg: + .ascii "Machine halted" + .byte 0 +devfstype: + .ascii "tmpfs" + .byte 0 +devfsdir: + .ascii "/dev" + .byte 0 +tmpfs_args: + /* Version. */ + .long 1 + + /* Maximum inodes. */ + .quad 0 + /* Maximum size. */ + .quad 0 + + /* UID */ + .long 0 + /* GID */ + .long 0 + /* Mode */ + .long 0777 +tmpfs_args_end: \ No newline at end of file From 96c713b69da081ad76f1c6cca2bcd9123b54e956 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Mon, 18 Jan 2010 23:37:11 +0100 Subject: [PATCH 073/178] Eliminate variable-length types in parameters --- include/grub/i386/netbsd_bootinfo.h | 8 ++++---- include/grub/i386/openbsd_bootarg.h | 6 +++--- loader/i386/bsd.c | 6 ++++-- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/include/grub/i386/netbsd_bootinfo.h b/include/grub/i386/netbsd_bootinfo.h index 5cda9d570..fd429a251 100644 --- a/include/grub/i386/netbsd_bootinfo.h +++ b/include/grub/i386/netbsd_bootinfo.h @@ -60,7 +60,7 @@ struct grub_netbsd_bootinfo { grub_uint32_t bi_count; - grub_addr_t bi_data[0]; + grub_uint32_t bi_data[0]; }; struct grub_netbsd_btinfo_common @@ -73,14 +73,14 @@ struct grub_netbsd_btinfo_common struct grub_netbsd_btinfo_bootdisk { - int labelsector; /* label valid if != -1 */ + grub_uint32_t labelsector; /* label valid if != 0xffffffff */ struct { grub_uint16_t type, checksum; char packname[16]; } label; - int biosdev; - int partition; + grub_uint32_t biosdev; + grub_uint32_t partition; }; struct grub_netbsd_btinfo_symtab diff --git a/include/grub/i386/openbsd_bootarg.h b/include/grub/i386/openbsd_bootarg.h index ccbe1ca12..cd99b14f2 100644 --- a/include/grub/i386/openbsd_bootarg.h +++ b/include/grub/i386/openbsd_bootarg.h @@ -64,9 +64,9 @@ struct grub_openbsd_bootargs { - int ba_type; - int ba_size; - struct grub_openbsd_bootargs *ba_next; + grub_uint32_t ba_type; + grub_uint32_t ba_size; + grub_uint32_t ba_next; } __attribute__ ((packed)); #endif diff --git a/loader/i386/bsd.c b/loader/i386/bsd.c index 7f9617543..ed9a1dbae 100644 --- a/loader/i386/bsd.c +++ b/loader/i386/bsd.c @@ -822,9 +822,11 @@ grub_openbsd_boot (void) buf = (grub_uint8_t *) pm; pa->ba_size = (char *) pm - (char *) pa; - pa->ba_next = (struct grub_openbsd_bootargs *) (buf - buf0 + buf_target); - pa = pa->ba_next; + pa->ba_next = buf - buf0 + buf_target; + pa = (struct grub_openbsd_bootargs *) buf; pa->ba_type = OPENBSD_BOOTARG_END; + pa->ba_size = 0; + pa->ba_next = 0; pa++; buf = (grub_uint8_t *) pa; From 839aec66c9ea917276bb533e41a315c9a2e0e74e Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Mon, 18 Jan 2010 23:57:10 +0100 Subject: [PATCH 074/178] make netbsd kernel symbols non-mandatory (not present on netbsd64) --- loader/i386/bsdXX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/loader/i386/bsdXX.c b/loader/i386/bsdXX.c index ccf386440..734633704 100644 --- a/loader/i386/bsdXX.c +++ b/loader/i386/bsdXX.c @@ -401,7 +401,7 @@ SUFFIX (grub_netbsd_load_elf_meta) (struct grub_relocator *relocator, break; if (s >= (Elf_Shdr *) ((char *) shdr + e.e_shnum * e.e_shentsize)) - return grub_error (GRUB_ERR_BAD_OS, "no symbol table"); + return GRUB_ERR_NONE; symsize = s->sh_size; symsh = s; s = (Elf_Shdr *) (shdr + e.e_shentsize * s->sh_link); From a3e99e1a45ff211b6d2ec220ef477372672cd961 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 19 Jan 2010 09:16:26 +0100 Subject: [PATCH 075/178] Align kern_end on page boundary as a precaution --- loader/i386/bsd.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/loader/i386/bsd.c b/loader/i386/bsd.c index ed9a1dbae..1d616c7b2 100644 --- a/loader/i386/bsd.c +++ b/loader/i386/bsd.c @@ -1008,7 +1008,7 @@ grub_netbsd_boot (void) err = grub_relocator_alloc_chunk_addr (relocator, &curarg, arg_target, tag_buf_len + sizeof (struct grub_netbsd_bootinfo) - + tag_count * sizeof (grub_addr_t)); + + tag_count * sizeof (grub_uint32_t)); if (err) return err; @@ -1322,6 +1322,8 @@ grub_bsd_load (int argc, char *argv[]) grub_file_close (file); } + kern_end = ALIGN_PAGE (kern_end); + fail: if (grub_errno != GRUB_ERR_NONE) From 2b9885e16bb2889baa42a9a782b8a10c4e735f4b Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 19 Jan 2010 09:17:23 +0100 Subject: [PATCH 076/178] bootcheck-kfreebsd-x86_64 --- conf/i386.rmk | 19 +++-- tests/boot/knetbsd.init-x86_64.S | 124 +++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 5 deletions(-) create mode 100644 tests/boot/knetbsd.init-x86_64.S diff --git a/conf/i386.rmk b/conf/i386.rmk index c9ba18b11..690194261 100644 --- a/conf/i386.rmk +++ b/conf/i386.rmk @@ -84,23 +84,32 @@ kfreebsd.init.i386: $(srcdir)/tests/boot/kfreebsd.init-i386.S knetbsd.init.i386: $(srcdir)/tests/boot/knetbsd.init-i386.S $(TARGET_CC) -o $@ $< -m32 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" +knetbsd.init.x86_64: $(srcdir)/tests/boot/knetbsd.init-x86_64.S + $(TARGET_CC) -o $@ $< -m64 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" + linux-initramfs.%: linux.init.% TDIR=`mktemp -d`; cp $< $$TDIR/init; (cd $$TDIR; echo ./init | cpio --quiet --dereference -o -H newc) | gzip > $@; rm -rf $$TDIR kfreebsd-mfsroot.%: kfreebsd.init.% - TDIR=`mktemp -d`; mkdir $$TDIR/dev; mkdir $$TDIR/sbin; cp $< $$TDIR/sbin/init; makefs -t ffs -s 30m -f 1000 -o minfree=0,version=1 $@ $$TDIR; rm -rf $$TDIR + TDIR=`mktemp -d`; mkdir $$TDIR/dev; mkdir $$TDIR/sbin; cp $< $$TDIR/sbin/init; makefs -t ffs -s 64k -f 10 -o minfree=0,version=1 $@ $$TDIR; rm -rf $$TDIR knetbsd.image.%: knetbsd.init.% - TDIR=`mktemp -d` && mkdir $$TDIR/dev && mkdir $$TDIR/sbin && cp $< $$TDIR/sbin/init && makefs -t ffs -s 30m -f 1000 -o minfree=0,version=1 $@ $$TDIR && rm -rf $$TDIR + TDIR=`mktemp -d` && mkdir $$TDIR/dev && mkdir $$TDIR/sbin && cp $< $$TDIR/sbin/init && makefs -t ffs -s 64k -f 10 -o minfree=0,version=1 $@ $$TDIR && rm -rf $$TDIR knetbsd.miniroot-image.i386: knetbsd.image.i386 $(GRUB_PAYLOADS_DIR)/knetbsd.miniroot.i386 $(OBJCOPY) --add-section=miniroot=$< $(GRUB_PAYLOADS_DIR)/knetbsd.miniroot.i386 $@ +knetbsd.miniroot-image.x86_64: knetbsd.image.x86_64 $(GRUB_PAYLOADS_DIR)/knetbsd.miniroot.x86_64 + $(OBJCOPY) --add-section=miniroot=$< $(GRUB_PAYLOADS_DIR)/knetbsd.miniroot.x86_64 $@ + CLEANFILES += linux.init.i386 kfreebsd.init.i386 linux.init.x86_64 linux-initramfs.i386 linux-initramfs.x86_64 bootcheck-knetbsd-i386: knetbsd.miniroot-image.i386 $(GRUB_PAYLOADS_DIR)/knetbsd.i386 $(srcdir)/tests/boot/knetbsd.cfg knetbsd.miniroot-image.i386 grub-shell timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-i386 --files=/miniroot=knetbsd.miniroot-image.i386 --files=/knetbsd=$(GRUB_PAYLOADS_DIR)/knetbsd.i386 $(srcdir)/tests/boot/knetbsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null +bootcheck-knetbsd-x86_64: knetbsd.miniroot-image.x86_64 $(GRUB_PAYLOADS_DIR)/knetbsd.x86_64 $(srcdir)/tests/boot/knetbsd.cfg knetbsd.miniroot-image.x86_64 grub-shell + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/miniroot=knetbsd.miniroot-image.x86_64 --files=/knetbsd=$(GRUB_PAYLOADS_DIR)/knetbsd.x86_64 $(srcdir)/tests/boot/knetbsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + bootcheck-kfreebsd-i386: kfreebsd-mfsroot.i386 $(GRUB_PAYLOADS_DIR)/kfreebsd.i386 $(GRUB_PAYLOADS_DIR)/kfreebsd_env.i386 $(srcdir)/tests/boot/kfreebsd.cfg grub-shell timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-i386 --files=/mfsroot=kfreebsd-mfsroot.i386 --files=/kfreebsd=$(GRUB_PAYLOADS_DIR)/kfreebsd.i386 --files=/kfreebsd_env=$(GRUB_PAYLOADS_DIR)/kfreebsd_env.i386 $(srcdir)/tests/boot/kfreebsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null @@ -115,8 +124,8 @@ bootcheck-linux-x86_64: linux-initramfs.x86_64 $(GRUB_PAYLOADS_DIR)/linux.x86_64 BOOTCHECKS+=bootcheck-linux-i386 bootcheck-linux-x86_64 \ bootcheck-kfreebsd-i386 bootcheck-kfreebsd-x86_64 \ - bootcheck-knetbsd-i386 + bootcheck-knetbsd-i386 bootcheck-knetbsd-x86_64 .PHONY: bootcheck-linux-i386 bootcheck-linux-x86_64 \ - bootcheck-kfreebsd-i386 bootcheck-kfreebsd-x86_64 - bootcheck-knetbsd-i386 \ No newline at end of file + bootcheck-kfreebsd-i386 bootcheck-kfreebsd-x86_64 \ + bootcheck-knetbsd-i386 bootcheck-knetbsd-x86_64 diff --git a/tests/boot/knetbsd.init-x86_64.S b/tests/boot/knetbsd.init-x86_64.S new file mode 100644 index 000000000..e56174f9c --- /dev/null +++ b/tests/boot/knetbsd.init-x86_64.S @@ -0,0 +1,124 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2010 Free Software Foundation, Inc. + * + * GRUB 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. + * + * GRUB 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 GRUB. If not, see . + */ + +#define MODE_RDRW 2 +#define FLAGS_NONE 0 +#define SYSCALL_OPEN 5 +#define SYSCALL_WRITE 4 +#define SYSCALL_RESET 208 +#define SYSCALL_EXIT 1 +#define SYSCALL_MKNOD 14 +#define SYSCALL_MOUNT 410 +#define SYSCALL_INT 0x80 + +#define RESET_NOSYNC 0x4 +#define RESET_HALT 0x8 +#define RESET_POWEROFF 0x800 + + .section ".init", "ax" + .global start,_start +start: +_start: + /* mount. */ + movq $SYSCALL_MOUNT, %rax + movq $devfstype, %rdi + movq $devfsdir, %rsi + movq $0, %rdx + movq $tmpfs_args, %r10 + movq $(tmpfs_args_end - tmpfs_args), %r8 + syscall + + /* mknod. */ + movq $SYSCALL_MKNOD, %rax + leaq device, %rdi + movq $0x2140, %rsi + movq $0, %rdx + syscall + + /* open. */ + movq $SYSCALL_OPEN, %rax + leaq device, %rdi + movq $MODE_RDRW, %rsi + movq $FLAGS_NONE, %rdx + syscall + movq %rax, %rdi + + /* write. */ + movq $SYSCALL_WRITE, %rax + movq $(messageend-message), %rdx + leaq message, %rsi + syscall + + /* shutdown. */ + movq $SYSCALL_RESET, %rax + movq $(RESET_POWEROFF|RESET_HALT|RESET_NOSYNC), %rdi + movq $haltmsg, %rsi + syscall + + /* exit (1). Shouldn't be reached. */ + movq $SYSCALL_EXIT, %rax + movq $1, %rdi + syscall + .section ".fini", "ax" +1: jmp 1b + .section ".text", "ax" +1: jmp 1b + /* This section is needed for NetBSD to identify the binary. */ + .section ".note.netbsd.ident", "a" + .long 0x7 + .long 0x4 + .long 0x1 + .ascii "NetBSD" + .byte 0 + .data +device: + .ascii "/dev/console" + .byte 0 +message: + .ascii "Boot Test Passed Successfully\n" SUCCESSFUL_BOOT_STRING "\n" +messageend: +haltmsg: + .ascii "Machine halted" + .byte 0 +devfstype: + .ascii "tmpfs" + .byte 0 +devfsdir: + .ascii "/dev" + .byte 0 +tmpfs_args: + /* Version. */ + .long 1 + + /* Alignment long. */ + .long 0 + + /* Maximum inodes. */ + .quad 0 + /* Maximum size. */ + .quad 0 + + /* UID */ + .long 0 + /* GID */ + .long 0 + /* Mode */ + .long 0777 + /* Alignment long. */ + .long 0 +tmpfs_args_end: \ No newline at end of file From 5b512173a30af58caff6a07940072b33188039d1 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 19 Jan 2010 10:32:57 +0100 Subject: [PATCH 077/178] Tags for OpenBSD --- include/grub/i386/bsd.h | 13 ---- loader/i386/bsd.c | 134 +++++++++++++++++----------------------- 2 files changed, 55 insertions(+), 92 deletions(-) diff --git a/include/grub/i386/bsd.h b/include/grub/i386/bsd.h index 8fd284bfc..f0cce840e 100644 --- a/include/grub/i386/bsd.h +++ b/include/grub/i386/bsd.h @@ -80,19 +80,6 @@ struct freebsd_tag_header grub_uint32_t len; }; -struct grub_openbsd_bios_mmap -{ - grub_uint64_t addr; - grub_uint64_t len; -#define OPENBSD_MMAP_AVAILABLE 1 -#define OPENBSD_MMAP_RESERVED 2 -#define OPENBSD_MMAP_ACPI 3 -#define OPENBSD_MMAP_NVS 4 - grub_uint32_t type; -}; - - - grub_err_t grub_freebsd_load_elfmodule32 (struct grub_relocator *relocator, grub_file_t file, int argc, char *argv[], grub_addr_t *kern_end); diff --git a/loader/i386/bsd.c b/loader/i386/bsd.c index 1d616c7b2..6643513b1 100644 --- a/loader/i386/bsd.c +++ b/loader/i386/bsd.c @@ -357,6 +357,9 @@ grub_bsd_add_mmap (void) if (kernel_type == KERNEL_TYPE_NETBSD) len += sizeof (grub_uint32_t); + if (kernel_type == KERNEL_TYPE_OPENBSD) + len += sizeof (struct grub_e820_mmap); + buf = grub_malloc (len); if (!buf) return grub_errno; @@ -370,9 +373,15 @@ grub_bsd_add_mmap (void) generate_e820_mmap (NULL, &cnt, buf); + if (kernel_type == KERNEL_TYPE_OPENBSD) + grub_memset ((grub_uint8_t *) buf + len - sizeof (struct grub_e820_mmap), 0, + sizeof (struct grub_e820_mmap)); + grub_dprintf ("bsd", "%u entries in smap\n", (unsigned) cnt); if (kernel_type == KERNEL_TYPE_NETBSD) grub_bsd_add_meta (NETBSD_BTINFO_MEMMAP, buf0, len); + else if (kernel_type == KERNEL_TYPE_OPENBSD) + grub_bsd_add_meta (OPENBSD_BOOTARG_MMAP, buf0, len); else grub_bsd_add_meta (FREEBSD_MODINFO_METADATA | FREEBSD_MODINFOMD_SMAP, buf0, len); @@ -746,91 +755,59 @@ grub_freebsd_boot (void) static grub_err_t grub_openbsd_boot (void) { - grub_uint8_t *buf, *buf0; grub_uint32_t *stack; - grub_addr_t buf_target, argbuf_target_start, argbuf_target_end; - grub_size_t buf_size; - struct grub_openbsd_bios_mmap *pm; - struct grub_openbsd_bootargs *pa; struct grub_relocator32_state state; + void *curarg, *buf0, *arg0; + grub_addr_t buf_target; grub_err_t err; + grub_size_t tag_buf_len; - auto int NESTED_FUNC_ATTR count_hook (grub_uint64_t, grub_uint64_t, grub_uint32_t); - int NESTED_FUNC_ATTR count_hook (grub_uint64_t addr __attribute__ ((unused)), - grub_uint64_t size __attribute__ ((unused)), - grub_uint32_t type __attribute__ ((unused))) - { - buf_size += sizeof (struct grub_openbsd_bios_mmap); - return 1; - } - - auto int NESTED_FUNC_ATTR hook (grub_uint64_t, grub_uint64_t, grub_uint32_t); - int NESTED_FUNC_ATTR hook (grub_uint64_t addr, grub_uint64_t size, grub_uint32_t type) - { - pm->addr = addr; - pm->len = size; - - switch (type) - { - case GRUB_MACHINE_MEMORY_AVAILABLE: - pm->type = OPENBSD_MMAP_AVAILABLE; - break; - - case GRUB_MACHINE_MEMORY_ACPI: - pm->type = OPENBSD_MMAP_ACPI; - break; - - case GRUB_MACHINE_MEMORY_NVS: - pm->type = OPENBSD_MMAP_NVS; - break; - - default: - pm->type = OPENBSD_MMAP_RESERVED; - break; - } - pm++; - - return 0; - } - - buf_target = GRUB_BSD_TEMP_BUFFER; - buf_size = sizeof (struct grub_openbsd_bootargs) + 9 * sizeof (grub_uint32_t); - grub_mmap_iterate (count_hook); - buf_size += sizeof (struct grub_openbsd_bootargs); - - err = grub_relocator_alloc_chunk_addr (relocator, (void **) &buf, - buf_target, buf_size); + err = grub_bsd_add_mmap (); if (err) return err; - buf0 = buf; - stack = (grub_uint32_t *) buf; - buf = (grub_uint8_t *) (stack + 9); - argbuf_target_start = buf - buf0 + buf_target; + { + struct bsd_tag *tag; + tag_buf_len = 0; + for (tag = tags; tag; tag = tag->next) + tag_buf_len = ALIGN_VAR (tag_buf_len + + sizeof (struct grub_openbsd_bootargs) + + tag->len); + } - pa = (struct grub_openbsd_bootargs *) buf; + buf_target = GRUB_BSD_TEMP_BUFFER - 9 * sizeof (grub_uint32_t); + err = grub_relocator_alloc_chunk_addr (relocator, &buf0, + buf_target, tag_buf_len + + sizeof (struct grub_openbsd_bootargs) + + 9 * sizeof (grub_uint32_t)); + if (err) + return err; - pa->ba_type = OPENBSD_BOOTARG_MMAP; - pm = (struct grub_openbsd_bios_mmap *) (pa + 1); - grub_mmap_iterate (hook); + stack = (grub_uint32_t *) buf0; + arg0 = curarg = stack + 9; - /* Memory map terminator. */ - pm->addr = 0; - pm->len = 0; - pm->type = 0; - pm++; - buf = (grub_uint8_t *) pm; + { + struct bsd_tag *tag; + struct grub_openbsd_bootargs *head; - pa->ba_size = (char *) pm - (char *) pa; - pa->ba_next = buf - buf0 + buf_target; - pa = (struct grub_openbsd_bootargs *) buf; - pa->ba_type = OPENBSD_BOOTARG_END; - pa->ba_size = 0; - pa->ba_next = 0; - pa++; + for (tag = tags; tag; tag = tag->next) + { + head = curarg; + head->ba_type = tag->type; + head->ba_size = tag->len + sizeof (*head); + curarg = head + 1; + grub_memcpy (curarg, tag->data, tag->len); + curarg = (grub_uint8_t *) curarg + tag->len; + head->ba_next = (grub_uint8_t *) curarg - (grub_uint8_t *) buf0 + + buf_target; + } + head = curarg; + head->ba_type = OPENBSD_BOOTARG_END; + head->ba_size = 0; + head->ba_next = 0; + } - buf = (grub_uint8_t *) pa; - argbuf_target_end = buf - buf0 + buf_target; + grub_video_set_mode ("text", 0, 0); #ifdef GRUB_MACHINE_EFI if (! grub_efi_finish_boot_services ()) @@ -838,7 +815,7 @@ grub_openbsd_boot (void) #endif state.eip = entry; - state.esp = ((grub_uint8_t *) stack - buf0) + buf_target; + state.esp = ((grub_uint8_t *) stack - (grub_uint8_t *) buf0) + buf_target; stack[0] = entry; stack[1] = bootflags; stack[2] = openbsd_root; @@ -846,10 +823,8 @@ grub_openbsd_boot (void) stack[4] = 0; stack[5] = grub_mmap_get_upper () >> 10; stack[6] = grub_mmap_get_lower () >> 10; - stack[7] = argbuf_target_end - argbuf_target_start; - stack[8] = argbuf_target_start; - - grub_video_set_mode ("text", 0, 0); + stack[7] = (grub_uint8_t *) curarg - (grub_uint8_t *) arg0; + stack[8] = ((grub_uint8_t *) arg0 - (grub_uint8_t *) buf0) + buf_target; return grub_relocator32_boot (relocator, state); } @@ -998,7 +973,8 @@ grub_netbsd_boot (void) tag_buf_len = 0; for (tag = tags; tag; tag = tag->next) { - tag_buf_len = ALIGN_VAR (tag_buf_len + 2 * sizeof (grub_uint32_t) + tag_buf_len = ALIGN_VAR (tag_buf_len + + sizeof (struct grub_netbsd_btinfo_common) + tag->len); tag_count++; } From b5b6745c874f3e8f6fb0546af72eb64f98b83f1a Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 19 Jan 2010 14:29:02 +0100 Subject: [PATCH 078/178] kopenbsd serial support --- include/grub/i386/openbsd_bootarg.h | 10 ++++++ loader/i386/bsd.c | 48 +++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/include/grub/i386/openbsd_bootarg.h b/include/grub/i386/openbsd_bootarg.h index cd99b14f2..935dfc0c8 100644 --- a/include/grub/i386/openbsd_bootarg.h +++ b/include/grub/i386/openbsd_bootarg.h @@ -61,6 +61,7 @@ #define OPENBSD_BOOTARG_END -1 #define OPENBSD_BOOTARG_MMAP 0 +#define OPENBSD_BOOTARG_CONSOLE 5 struct grub_openbsd_bootargs { @@ -69,4 +70,13 @@ struct grub_openbsd_bootargs grub_uint32_t ba_next; } __attribute__ ((packed)); +struct grub_openbsd_bootarg_console +{ + grub_uint32_t device; + grub_uint32_t speed; +}; + +#define GRUB_OPENBSD_COM_MAJOR 8 +#define GRUB_OPENBSD_VGA_MAJOR 12 + #endif diff --git a/loader/i386/bsd.c b/loader/i386/bsd.c index 6643513b1..7a8ddb89a 100644 --- a/loader/i386/bsd.c +++ b/loader/i386/bsd.c @@ -126,6 +126,8 @@ static const struct grub_arg_option openbsd_opts[] = {"single", 's', 0, N_("Boot into single mode."), 0, 0}, {"kdb", 'd', 0, N_("Enter in KDB on boot."), 0, 0}, {"root", 'r', 0, N_("Set root device."), "wdXY", ARG_TYPE_STRING}, + {"serial", 'h', GRUB_ARG_OPTION_OPTIONAL, + N_("Use serial console."), N_("comUNIT[,SPEED]"), ARG_TYPE_STRING}, {0, 0, 0, 0, 0, 0} }; @@ -136,6 +138,7 @@ static const grub_uint32_t openbsd_flags[] = }; #define OPENBSD_ROOT_ARG (ARRAY_SIZE (openbsd_flags) - 1) +#define OPENBSD_SERIAL_ARG (ARRAY_SIZE (openbsd_flags)) static const struct grub_arg_option netbsd_opts[] = { @@ -1410,6 +1413,51 @@ grub_cmd_openbsd (grub_extcmd_t cmd, int argc, char *argv[]) else bootdev = 0; + if (cmd->state[OPENBSD_SERIAL_ARG].set) + { + struct grub_openbsd_bootarg_console serial; + char *ptr; + unsigned port = 0; + unsigned speed = 9600; + + grub_memset (&serial, 0, sizeof (serial)); + + if (cmd->state[OPENBSD_SERIAL_ARG].arg) + { + ptr = cmd->state[OPENBSD_SERIAL_ARG].arg; + if (grub_memcmp (ptr, "com", sizeof ("com") - 1) != 0) + return grub_error (GRUB_ERR_BAD_ARGUMENT, + "only com0-com3 are supported"); + ptr += sizeof ("com") - 1; + port = grub_strtoul (ptr, &ptr, 0); + if (port >= 4) + return grub_error (GRUB_ERR_BAD_ARGUMENT, + "only com0-com3 are supported"); + if (*ptr == ',') + { + ptr++; + speed = grub_strtoul (ptr, &ptr, 0); + if (grub_errno) + return grub_errno; + } + } + + serial.device = (GRUB_OPENBSD_COM_MAJOR << 8) | port; + serial.speed = speed; + + grub_bsd_add_meta (OPENBSD_BOOTARG_CONSOLE, &serial, sizeof (serial)); + bootflags |= OPENBSD_RB_SERCONS; + } + else + { + struct grub_openbsd_bootarg_console serial; + + grub_memset (&serial, 0, sizeof (serial)); + serial.device = (GRUB_OPENBSD_VGA_MAJOR << 8); + grub_bsd_add_meta (OPENBSD_BOOTARG_CONSOLE, &serial, sizeof (serial)); + bootflags &= ~OPENBSD_RB_SERCONS; + } + if (grub_bsd_load (argc, argv) == GRUB_ERR_NONE) { grub_loader_set (grub_openbsd_boot, grub_bsd_unload, 1); From 69a30a6e8e01247536ababf08c57ec92969f2a57 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 20 Jan 2010 11:12:45 +0100 Subject: [PATCH 079/178] Pass NULL as second argument to netbsd reboot syscall --- tests/boot/knetbsd.init-i386.S | 5 +---- tests/boot/knetbsd.init-x86_64.S | 5 +---- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/tests/boot/knetbsd.init-i386.S b/tests/boot/knetbsd.init-i386.S index 200aed01f..c751421ba 100644 --- a/tests/boot/knetbsd.init-i386.S +++ b/tests/boot/knetbsd.init-i386.S @@ -78,7 +78,7 @@ _start: /* shutdown. */ movl $SYSCALL_RESET, %eax - pushl $haltmsg + pushl $0 pushl $(RESET_POWEROFF|RESET_HALT|RESET_NOSYNC) pushl $0 int $SYSCALL_INT @@ -107,9 +107,6 @@ device: message: .ascii "Boot Test Passed Successfully\n" SUCCESSFUL_BOOT_STRING "\n" messageend: -haltmsg: - .ascii "Machine halted" - .byte 0 devfstype: .ascii "tmpfs" .byte 0 diff --git a/tests/boot/knetbsd.init-x86_64.S b/tests/boot/knetbsd.init-x86_64.S index e56174f9c..dfc64e99d 100644 --- a/tests/boot/knetbsd.init-x86_64.S +++ b/tests/boot/knetbsd.init-x86_64.S @@ -67,7 +67,7 @@ _start: /* shutdown. */ movq $SYSCALL_RESET, %rax movq $(RESET_POWEROFF|RESET_HALT|RESET_NOSYNC), %rdi - movq $haltmsg, %rsi + movq $0, %rsi syscall /* exit (1). Shouldn't be reached. */ @@ -92,9 +92,6 @@ device: message: .ascii "Boot Test Passed Successfully\n" SUCCESSFUL_BOOT_STRING "\n" messageend: -haltmsg: - .ascii "Machine halted" - .byte 0 devfstype: .ascii "tmpfs" .byte 0 From 7d8c9ec63df5396df0bdc9843675ed56876525fd Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 23 Jan 2010 13:30:24 +0100 Subject: [PATCH 080/178] newreloc on yeeloong --- conf/mips.rmk | 4 +- include/grub/mips/relocator.h | 9 ++- include/grub/relocator_private.h | 2 +- lib/i386/relocator.c | 6 +- lib/mips/relocator.c | 111 ++++++++++++++++++++----------- lib/mips/relocator_asm.S | 8 +-- lib/relocator.c | 8 ++- loader/mips/linux.c | 60 +++++++++-------- 8 files changed, 126 insertions(+), 82 deletions(-) diff --git a/conf/mips.rmk b/conf/mips.rmk index d0b1c484c..0e96caeb4 100644 --- a/conf/mips.rmk +++ b/conf/mips.rmk @@ -17,7 +17,7 @@ kernel_img_HEADERS = boot.h cache.h device.h disk.h dl.h elf.h elfload.h \ env.h err.h file.h fs.h kernel.h misc.h mm.h net.h parser.h reader.h \ symbol.h term.h time.h types.h loader.h partition.h \ msdos_partition.h machine/kernel.h handler.h list.h \ - command.h machine/memory.h cpu/libgcc.h cpu/cache.h i18n.h + command.h machine/memory.h cpu/libgcc.h cpu/cache.h i18n.h mm_private.h ifeq ($(platform), yeeloong) kernel_img_HEADERS += pci.h @@ -68,7 +68,7 @@ serial_mod_LDFLAGS = $(COMMON_LDFLAGS) # For relocator.mod. pkglib_MODULES += relocator.mod -relocator_mod_SOURCES = lib/$(target_cpu)/relocator.c lib/$(target_cpu)/relocator_asm.S +relocator_mod_SOURCES = lib/$(target_cpu)/relocator.c lib/relocator.c lib/$(target_cpu)/relocator_asm.S relocator_mod_CFLAGS = $(COMMON_CFLAGS) relocator_mod_ASFLAGS = $(COMMON_ASFLAGS) relocator_mod_LDFLAGS = $(COMMON_LDFLAGS) diff --git a/include/grub/mips/relocator.h b/include/grub/mips/relocator.h index 838ef832f..67b0a4c43 100644 --- a/include/grub/mips/relocator.h +++ b/include/grub/mips/relocator.h @@ -21,6 +21,7 @@ #include #include +#include struct grub_relocator32_state { @@ -30,10 +31,8 @@ struct grub_relocator32_state int jumpreg; }; -void *grub_relocator32_alloc (grub_size_t size); -grub_err_t grub_relocator32_boot (void *relocator, grub_uint32_t dest, - struct grub_relocator32_state state); -void *grub_relocator32_realloc (void *relocator, grub_size_t size); -void grub_relocator32_free (void *relocator); +grub_err_t +grub_relocator32_boot (struct grub_relocator *rel, + struct grub_relocator32_state state); #endif /* ! GRUB_RELOCATOR_CPU_HEADER */ diff --git a/include/grub/relocator_private.h b/include/grub/relocator_private.h index 7a3ef2bd7..f9e76468e 100644 --- a/include/grub/relocator_private.h +++ b/include/grub/relocator_private.h @@ -31,7 +31,7 @@ void grub_cpu_relocator_init (void); grub_err_t grub_relocator_prepare_relocs (struct grub_relocator *rel, grub_addr_t addr, - grub_addr_t *relstart); + grub_addr_t *relstart, grub_size_t *relsize); void grub_cpu_relocator_forward (void *rels, void *src, void *tgt, grub_size_t size); void grub_cpu_relocator_backward (void *rels, void *src, void *tgt, diff --git a/lib/i386/relocator.c b/lib/i386/relocator.c index d040c80ab..4eaa66890 100644 --- a/lib/i386/relocator.c +++ b/lib/i386/relocator.c @@ -169,7 +169,7 @@ grub_relocator32_boot (struct grub_relocator *rel, grub_memmove (src, &grub_relocator32_start, RELOCATOR_SIZEOF (32)); - err = grub_relocator_prepare_relocs (rel, target, &relst); + err = grub_relocator_prepare_relocs (rel, target, &relst, NULL); if (err) return err; @@ -209,7 +209,7 @@ grub_relocator16_boot (struct grub_relocator *rel, grub_memmove (src, &grub_relocator16_start, RELOCATOR_SIZEOF (16)); - err = grub_relocator_prepare_relocs (rel, target, &relst); + err = grub_relocator_prepare_relocs (rel, target, &relst, NULL); if (err) return err; @@ -248,7 +248,7 @@ grub_relocator64_boot (struct grub_relocator *rel, grub_memmove (src, &grub_relocator64_start, RELOCATOR_SIZEOF (64)); - err = grub_relocator_prepare_relocs (rel, target, &relst); + err = grub_relocator_prepare_relocs (rel, target, &relst, NULL); if (err) return err; diff --git a/lib/mips/relocator.c b/lib/mips/relocator.c index 118ddbd6f..410b68b8b 100644 --- a/lib/mips/relocator.c +++ b/lib/mips/relocator.c @@ -25,26 +25,33 @@ #include #include +#include -/* Remark: doesn't work with source outside of 4G. - Use relocator64 in this case. - */ +/* Do we need mips64? */ -extern grub_uint8_t grub_relocator32_forward_start; -extern grub_uint8_t grub_relocator32_forward_end; -extern grub_uint8_t grub_relocator32_backward_start; -extern grub_uint8_t grub_relocator32_backward_end; +extern grub_uint8_t grub_relocator_forward_start; +extern grub_uint8_t grub_relocator_forward_end; +extern grub_uint8_t grub_relocator_backward_start; +extern grub_uint8_t grub_relocator_backward_end; #define REGW_SIZEOF (2 * sizeof (grub_uint32_t)) #define JUMP_SIZEOF (2 * sizeof (grub_uint32_t)) -#define RELOCATOR_SRC_SIZEOF(x) (&grub_relocator32_##x##_end \ - - &grub_relocator32_##x##_start) +#define RELOCATOR_SRC_SIZEOF(x) (&grub_relocator_##x##_end \ + - &grub_relocator_##x##_start) #define RELOCATOR_SIZEOF(x) (RELOCATOR_SRC_SIZEOF(x) \ - + REGW_SIZEOF * (31 + 3) + JUMP_SIZEOF) -#define RELOCATOR_ALIGN 16 + + REGW_SIZEOF * 3) +grub_size_t grub_relocator_align = sizeof (grub_uint32_t); +grub_size_t grub_relocator_forward_size; +grub_size_t grub_relocator_backward_size; +grub_size_t grub_relocator_jumper_size = JUMP_SIZEOF + REGW_SIZEOF; -#define PREFIX(x) grub_relocator32_ ## x +void +grub_cpu_relocator_init (void) +{ + grub_relocator_forward_size = RELOCATOR_SIZEOF(forward); + grub_relocator_backward_size = RELOCATOR_SIZEOF(backward); +} static void write_reg (int regn, grub_uint32_t val, void **target) @@ -69,44 +76,70 @@ write_jump (int regn, void **target) *target = ((grub_uint32_t *) *target) + 1; } -static void -write_call_relocator_bw (void *ptr0, void *src, grub_uint32_t dest, - grub_size_t size, struct grub_relocator32_state state) +void +grub_cpu_relocator_jumper (void *rels, grub_addr_t addr) +{ + write_reg (1, addr, &rels); + write_jump (1, &rels); +} + +void +grub_cpu_relocator_backward (void *ptr0, void *src, void *dest, + grub_size_t size) { void *ptr = ptr0; - int i; write_reg (8, (grub_uint32_t) src, &ptr); - write_reg (9, dest, &ptr); - write_reg (10, size, &ptr); - grub_memcpy (ptr, &grub_relocator32_backward_start, + write_reg (9, (grub_uint32_t) dest, &ptr); + write_reg (10, (grub_uint32_t) size, &ptr); + grub_memcpy (ptr, &grub_relocator_backward_start, RELOCATOR_SRC_SIZEOF (backward)); - ptr = (grub_uint8_t *) ptr + RELOCATOR_SRC_SIZEOF (backward); - for (i = 1; i < 32; i++) - write_reg (i, state.gpr[i], &ptr); - write_jump (state.jumpreg, &ptr); - grub_arch_sync_caches (ptr0, (grub_uint8_t *) ptr - (grub_uint8_t *) ptr0); - grub_dprintf ("relocator", "Backward relocator: about to jump to %p\n", ptr0); - ((void (*) (void)) ptr0) (); } -static void -write_call_relocator_fw (void *ptr0, void *src, grub_uint32_t dest, - grub_size_t size, struct grub_relocator32_state state) +void +grub_cpu_relocator_forward (void *ptr0, void *src, void *dest, + grub_size_t size) { void *ptr = ptr0; - int i; write_reg (8, (grub_uint32_t) src, &ptr); - write_reg (9, dest, &ptr); - write_reg (10, size, &ptr); - grub_memcpy (ptr, &grub_relocator32_forward_start, + write_reg (9, (grub_uint32_t) dest, &ptr); + write_reg (10, (grub_uint32_t) size, &ptr); + grub_memcpy (ptr, &grub_relocator_forward_start, RELOCATOR_SRC_SIZEOF (forward)); - ptr = (grub_uint8_t *) ptr + RELOCATOR_SRC_SIZEOF (forward); +} + +grub_err_t +grub_relocator32_boot (struct grub_relocator *rel, + struct grub_relocator32_state state) +{ + grub_addr_t target; + void *src, *ptr; + grub_err_t err; + grub_addr_t relst; + grub_size_t relsize; + grub_size_t stateset_size = 31 * REGW_SIZEOF + JUMP_SIZEOF; + unsigned i; + + err = grub_relocator_alloc_chunk_align (rel, &src, &target, 0, + (0xffffffff - stateset_size) + + 1, stateset_size, + sizeof (grub_uint32_t), + GRUB_RELOCATOR_PREFERENCE_NONE); + if (err) + return err; + + ptr = src; for (i = 1; i < 32; i++) write_reg (i, state.gpr[i], &ptr); write_jump (state.jumpreg, &ptr); - grub_arch_sync_caches (ptr0, (grub_uint8_t *) ptr - (grub_uint8_t *) ptr0); - grub_dprintf ("relocator", "Forward relocator: about to jump to %p\n", ptr0); - ((void (*) (void)) ptr0) (); -} -#include "../relocator.c" + err = grub_relocator_prepare_relocs (rel, target, &relst, &relsize); + if (err) + return err; + + grub_arch_sync_caches ((void *) relst, relsize); + + ((void (*) (void)) relst) (); + + /* Not reached. */ + return GRUB_ERR_NONE; +} diff --git a/lib/mips/relocator_asm.S b/lib/mips/relocator_asm.S index ff4fa31e0..8ffab99b7 100644 --- a/lib/mips/relocator_asm.S +++ b/lib/mips/relocator_asm.S @@ -20,7 +20,7 @@ .p2align 4 /* force 16-byte alignment */ -VARIABLE (grub_relocator32_forward_start) +VARIABLE (grub_relocator_forward_start) move $a0, $9 move $a1, $10 @@ -34,9 +34,9 @@ copycont1: #include "../../kern/mips/cache_flush.S" -VARIABLE (grub_relocator32_forward_end) +VARIABLE (grub_relocator_forward_end) -VARIABLE (grub_relocator32_backward_start) +VARIABLE (grub_relocator_backward_start) move $a0, $9 move $a1, $10 @@ -55,4 +55,4 @@ copycont2: #include "../../kern/mips/cache_flush.S" -VARIABLE (grub_relocator32_backward_end) +VARIABLE (grub_relocator_backward_end) diff --git a/lib/relocator.c b/lib/relocator.c index 0644691cd..dad0ec70c 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -20,6 +20,7 @@ #include #include #include +#include /* TODO: use more efficient data structures if necessary. */ /* FIXME: implement unload. */ @@ -605,7 +606,7 @@ grub_relocator_unload (struct grub_relocator *rel) grub_err_t grub_relocator_prepare_relocs (struct grub_relocator *rel, grub_addr_t addr, - grub_addr_t *relstart) + grub_addr_t *relstart, grub_size_t *relsize) { grub_addr_t rels; grub_addr_t rels0; @@ -622,6 +623,9 @@ grub_relocator_prepare_relocs (struct grub_relocator *rel, grub_addr_t addr, return grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory"); rels = rels0; + if (relsize) + *relsize = rel->relocators_size; + grub_dprintf ("relocator", "Relocs allocated\n"); { @@ -698,6 +702,8 @@ grub_relocator_prepare_relocs (struct grub_relocator *rel, grub_addr_t addr, sorted[j].size); rels += grub_relocator_forward_size; } + if (sorted[j].src == sorted[j].target) + grub_arch_sync_caches ((void *) sorted[j].src, sorted[j].size); } grub_cpu_relocator_jumper ((void *) rels, addr); *relstart = rels0; diff --git a/loader/mips/linux.c b/loader/mips/linux.c index 51060c4fb..09c7f1275 100644 --- a/loader/mips/linux.c +++ b/loader/mips/linux.c @@ -42,6 +42,7 @@ static int loaded; static grub_size_t linux_size; +static struct grub_relocator *relocator; static grub_uint8_t *playground; static grub_addr_t target_addr, entry_addr; static int linux_argc; @@ -60,15 +61,7 @@ grub_linux_boot (void) state.gpr[5] = target_addr + argv_off; state.gpr[6] = target_addr + envp_off; state.jumpreg = 1; - grub_relocator32_boot (playground, target_addr, state); - - return GRUB_ERR_NONE; -} - -static grub_err_t -grub_linux_release_mem (void) -{ - grub_relocator32_free (playground); + grub_relocator32_boot (relocator, state); return GRUB_ERR_NONE; } @@ -76,14 +69,12 @@ grub_linux_release_mem (void) static grub_err_t grub_linux_unload (void) { - grub_err_t err; - - err = grub_linux_release_mem (); + grub_relocator_unload (relocator); grub_dl_unref (my_mod); loaded = 0; - return err; + return GRUB_ERR_NONE; } static grub_err_t @@ -91,6 +82,7 @@ grub_linux_load32 (grub_elf_t elf, void **extra_mem, grub_size_t extra_size) { Elf32_Addr base; int extraoff; + grub_err_t err; /* Linux's entry point incorrectly contains a virtual address. */ entry_addr = elf->ehdr.ehdr32.e_entry & ~ELF32_LOADMASK; @@ -105,10 +97,15 @@ grub_linux_load32 (grub_elf_t elf, void **extra_mem, grub_size_t extra_size) extraoff = linux_size; linux_size += extra_size; - playground = grub_relocator32_alloc (linux_size); - if (!playground) + relocator = grub_relocator_new (); + if (!relocator) return grub_errno; + err = grub_relocator_alloc_chunk_addr (relocator, (void **) &playground, + target_addr, linux_size); + if (err) + return err; + *extra_mem = playground + extraoff; /* Now load the segments into the area we claimed. */ @@ -135,6 +132,7 @@ grub_linux_load64 (grub_elf_t elf, void **extra_mem, grub_size_t extra_size) { Elf64_Addr base; int extraoff; + grub_err_t err; /* Linux's entry point incorrectly contains a virtual address. */ entry_addr = elf->ehdr.ehdr64.e_entry & ~ELF64_LOADMASK; @@ -149,10 +147,15 @@ grub_linux_load64 (grub_elf_t elf, void **extra_mem, grub_size_t extra_size) extraoff = linux_size; linux_size += extra_size; - playground = grub_relocator32_alloc (linux_size); - if (!playground) + relocator = grub_relocator_new (); + if (!relocator) return grub_errno; + err = grub_relocator_alloc_chunk_addr (relocator, (void **) &playground, + target_addr, linux_size); + if (err) + return err; + *extra_mem = playground + extraoff; /* Now load the segments into the area we claimed. */ @@ -322,7 +325,9 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), { grub_file_t file = 0; grub_ssize_t size; - grub_size_t overhead; + void *initrd_src; + grub_addr_t initrd_dest; + grub_err_t err; if (argc == 0) return grub_error (GRUB_ERR_BAD_ARGUMENT, "No initrd specified"); @@ -339,19 +344,20 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), size = grub_file_size (file); - overhead = ALIGN_UP (target_addr + linux_size + 0x10000, 0x10000) - - (target_addr + linux_size); + err = grub_relocator_alloc_chunk_align (relocator, &initrd_src, + &initrd_dest, + target_addr + linux_size + 0x10000, + (0xffffffff - size) + 1, + size, 0x10000, + GRUB_RELOCATOR_PREFERENCE_NONE); - playground = grub_relocator32_realloc (playground, - linux_size + overhead + size); - - if (!playground) + if (err) { grub_file_close (file); - return grub_errno; + return err; } - if (grub_file_read (file, playground + linux_size + overhead, size) != size) + if (grub_file_read (file, initrd_src, size) != size) { grub_error (GRUB_ERR_FILE_READ_ERROR, "Couldn't read file"); grub_file_close (file); @@ -361,7 +367,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), grub_snprintf ((char *) playground + rd_addr_arg_off, sizeof ("rd_start=0xXXXXXXXXXXXXXXXX"), "rd_start=0x%llx", - (unsigned long long) target_addr + linux_size + overhead); + (unsigned long long) initrd_dest); ((grub_uint32_t *) (playground + argv_off))[linux_argc] = target_addr + rd_addr_arg_off; linux_argc++; From ddf23b9d81ea8cb0c2c06542caf7efb2d8f6e818 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 23 Jan 2010 14:30:06 +0100 Subject: [PATCH 081/178] relocator unloading support --- lib/relocator.c | 128 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 86 insertions(+), 42 deletions(-) diff --git a/lib/relocator.c b/lib/relocator.c index dad0ec70c..298c13d06 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -23,7 +23,6 @@ #include /* TODO: use more efficient data structures if necessary. */ -/* FIXME: implement unload. */ /* FIXME: check memory map. */ /* FIXME: try to request memory from firmware. */ @@ -42,6 +41,8 @@ struct grub_relocator_chunk grub_addr_t src; grub_addr_t target; grub_size_t size; + enum {CHUNK_TYPE_IN_REGION, CHUNK_TYPE_REGION_START} type; + grub_addr_t host_start; }; struct grub_relocator * @@ -204,8 +205,8 @@ get_best_header (struct grub_relocator *rel, static int malloc_in_range (struct grub_relocator *rel, grub_addr_t start, grub_addr_t end, grub_addr_t align, - grub_size_t size, grub_addr_t *res, int from_low_priv, - int collisioncheck) + grub_size_t size, struct grub_relocator_chunk *res, + int from_low_priv, int collisioncheck) { grub_mm_region_t rb, rbp; grub_mm_header_t hb = NULL, hbp = NULL; @@ -262,6 +263,11 @@ malloc_in_range (struct grub_relocator *rel, grub_addr_t newreg_start, newreg_raw_start = best_addr + size; grub_addr_t newreg_size, newreg_presize; grub_mm_header_t new_header; + + res->src = best_addr; + res->type = CHUNK_TYPE_REGION_START; + res->host_start = (grub_addr_t) rb - rb->pre_size; + newreg_start = ALIGN_UP (newreg_raw_start, GRUB_MM_ALIGN); newreg_presize = newreg_start - newreg_raw_start; newreg_size = rb->size - (newreg_start - (grub_addr_t) rb); @@ -313,11 +319,13 @@ malloc_in_range (struct grub_relocator *rel, while (h != newreg->first); } } - *res = best_addr; return 1; } { struct grub_mm_header *foll = NULL; + + res->src = best_addr; + res->type = CHUNK_TYPE_IN_REGION; if (ALIGN_UP (best_addr + size, GRUB_MM_ALIGN) + GRUB_MM_ALIGN <= (grub_addr_t) (hb + hb->size)) @@ -351,7 +359,6 @@ malloc_in_range (struct grub_relocator *rel, if (rb->first == hb) rb->first = (void *) (rb + 1); } - *res = best_addr; return 1; } } @@ -384,7 +391,6 @@ grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, void **src, grub_addr_t target, grub_size_t size) { struct grub_relocator_chunk *chunk; - grub_addr_t start; grub_addr_t min_addr = 0, max_addr; if (target > ~size) @@ -413,24 +419,24 @@ grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, void **src, #if defined (__i386__) || defined (__x86_64__) if (target < 0x100000) if (malloc_in_range (rel, rel->highestnonpostaddr, ~(grub_addr_t)0, 1, - size, &start, 0, 1)) + size, chunk, 0, 1)) { - if (rel->postchunks > start) - rel->postchunks = start; + if (rel->postchunks > chunk->src) + rel->postchunks = chunk->src; break; } #endif - if (malloc_in_range (rel, target, max_addr, 1, size, &start, 1, 0)) + if (malloc_in_range (rel, target, max_addr, 1, size, chunk, 1, 0)) break; - if (malloc_in_range (rel, min_addr, target, 1, size, &start, 0, 0)) + if (malloc_in_range (rel, min_addr, target, 1, size, chunk, 0, 0)) break; if (malloc_in_range (rel, rel->highestnonpostaddr, ~(grub_addr_t)0, 1, - size, &start, 0, 1)) + size, chunk, 0, 1)) { - if (rel->postchunks > start) - rel->postchunks = start; + if (rel->postchunks > chunk->src) + rel->postchunks = chunk->src; break; } @@ -441,35 +447,34 @@ grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, void **src, while (0); grub_dprintf ("relocator", "allocated 0x%llx/0x%llx\n", - (unsigned long long) start, (unsigned long long) target); + (unsigned long long) chunk->src, (unsigned long long) target); if (rel->highestaddr < target + size) rel->highestaddr = target + size; - if (rel->highestaddr < start + size) - rel->highestaddr = start + size; + if (rel->highestaddr < chunk->src + size) + rel->highestaddr = chunk->src + size; - if (start < rel->postchunks) + if (chunk->src < rel->postchunks) { if (rel->highestnonpostaddr < target + size) rel->highestnonpostaddr = target + size; - if (rel->highestnonpostaddr < start + size) - rel->highestnonpostaddr = start + size; + if (rel->highestnonpostaddr < chunk->src + size) + rel->highestnonpostaddr = chunk->src + size; } grub_dprintf ("relocator", "relocators_size=%ld\n", (unsigned long) rel->relocators_size); - if (start < target) + if (chunk->src < target) rel->relocators_size += grub_relocator_backward_size; - if (start > target) + if (chunk->src > target) rel->relocators_size += grub_relocator_forward_size; grub_dprintf ("relocator", "relocators_size=%ld\n", (unsigned long) rel->relocators_size); - chunk->src = start; chunk->target = target; chunk->size = size; chunk->next = rel->chunks; @@ -477,7 +482,7 @@ grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, void **src, grub_dprintf ("relocator", "cur = %p, next = %p\n", rel->chunks, rel->chunks->next); - *src = (void *) start; + *src = (void *) chunk->src; return GRUB_ERR_NONE; } @@ -490,7 +495,6 @@ grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, { grub_addr_t min_addr2 = 0, max_addr2; struct grub_relocator_chunk *chunk; - grub_addr_t start; if (max_addr > ~size) max_addr = ~size; @@ -507,19 +511,19 @@ grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, return grub_errno; if (malloc_in_range (rel, min_addr, max_addr, align, - size, &start, + size, chunk, preference != GRUB_RELOCATOR_PREFERENCE_HIGH, 1)) { grub_dprintf ("relocator", "allocated 0x%llx/0x%llx\n", - (unsigned long long) start, (unsigned long long) start); + (unsigned long long) chunk->src, + (unsigned long long) chunk->src); grub_dprintf ("relocator", "chunks = %p\n", rel->chunks); - chunk->src = start; - chunk->target = start; + chunk->target = chunk->src; chunk->size = size; chunk->next = rel->chunks; rel->chunks = chunk; - *src = (void *) start; - *target = start; + *src = (void *) chunk->src; + *target = chunk->target; return GRUB_ERR_NONE; } @@ -531,14 +535,14 @@ grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, do { if (malloc_in_range (rel, min_addr2, max_addr2, align, - size, &start, 1, 1)) + size, chunk, 1, 1)) break; if (malloc_in_range (rel, rel->highestnonpostaddr, ~(grub_addr_t)0, 1, - size, &start, 0, 1)) + size, chunk, 0, 1)) { - if (rel->postchunks > start) - rel->postchunks = start; + if (rel->postchunks > chunk->src) + rel->postchunks = chunk->src; break; } @@ -574,18 +578,17 @@ grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, break; } - if (start < chunk->target) + if (chunk->src < chunk->target) rel->relocators_size += grub_relocator_backward_size; - if (start > chunk->target) + if (chunk->src > chunk->target) rel->relocators_size += grub_relocator_forward_size; - chunk->src = start; chunk->size = size; chunk->next = rel->chunks; rel->chunks = chunk; grub_dprintf ("relocator", "cur = %p, next = %p\n", rel->chunks, rel->chunks->next); - *src = (void *) start; + *src = (void *) chunk->src; *target = chunk->target; return GRUB_ERR_NONE; } @@ -598,7 +601,47 @@ grub_relocator_unload (struct grub_relocator *rel) return; for (chunk = rel->chunks; chunk; chunk = next) { - grub_fatal ("Relocator unloading isn't implemented yet"); + switch (chunk->type) + { + case CHUNK_TYPE_REGION_START: + { + grub_mm_region_t r1, r2, *rp; + grub_mm_header_t h; + grub_size_t pre_size; + r1 = (grub_mm_region_t) ALIGN_UP (chunk->src + chunk->size, + GRUB_MM_ALIGN); + r2 = (grub_mm_region_t) ALIGN_UP (chunk->host_start, GRUB_MM_ALIGN); + for (rp = &grub_mm_base; *rp && *rp != r2; rp = &((*rp)->next)); + if (!*rp) + grub_fatal ("Anomaly in region alocations detected. " + "Simultaneous relocators?"); + pre_size = ALIGN_UP (chunk->host_start, GRUB_MM_ALIGN) + - chunk->host_start; + r2->first = r1->first; + r2->next = r1->next; + r2->pre_size = pre_size; + r2->size = r1->size + (r2 - r1) * sizeof (*r2); + *rp = r1; + h = (grub_mm_header_t) (r1 + 1); + h->next = h; + h->magic = GRUB_MM_ALLOC_MAGIC; + h->size = (r2 - r1); + grub_free (h + 1); + break; + } + case CHUNK_TYPE_IN_REGION: + { + grub_mm_header_t h = (grub_mm_header_t) ALIGN_DOWN (chunk->src, + GRUB_MM_ALIGN); + h->size = (chunk->src / GRUB_MM_ALIGN) + - ((chunk->src + chunk->size + GRUB_MM_ALIGN - 1) + / GRUB_MM_ALIGN); + h->next = h; + h->magic = GRUB_MM_ALLOC_MAGIC; + grub_free (h + 1); + break; + } + } next = chunk->next; grub_free (chunk); } @@ -613,15 +656,16 @@ grub_relocator_prepare_relocs (struct grub_relocator *rel, grub_addr_t addr, struct grub_relocator_chunk *sorted; grub_size_t nchunks = 0; unsigned j; + struct grub_relocator_chunk movers_chunk; grub_dprintf ("relocator", "Preparing relocs (size=%ld)\n", (unsigned long) rel->relocators_size); if (!malloc_in_range (rel, 0, ~(grub_addr_t)0 - rel->relocators_size + 1, grub_relocator_align, - rel->relocators_size, &rels0, 1, 1)) + rel->relocators_size, &movers_chunk, 1, 1)) return grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory"); - rels = rels0; + rels = rels0 = movers_chunk.src; if (relsize) *relsize = rel->relocators_size; From 473fc1a0623169dba7025ad5dbeccab4d02d28ac Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Fri, 5 Feb 2010 21:02:24 +0100 Subject: [PATCH 082/178] Make mips/relocator_asm.S more readable --- lib/mips/relocator_asm.S | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/mips/relocator_asm.S b/lib/mips/relocator_asm.S index 8ffab99b7..3408b59e1 100644 --- a/lib/mips/relocator_asm.S +++ b/lib/mips/relocator_asm.S @@ -27,9 +27,9 @@ VARIABLE (grub_relocator_forward_start) copycont1: lb $11,0($8) sb $11,0($9) - addiu $8, $8, 0x1 - addiu $9, $9, 0x1 - addiu $10, $10, 0xffff + addiu $8, $8, 1 + addiu $9, $9, 1 + addiu $10, $10, -1 bne $10, $0, copycont1 #include "../../kern/mips/cache_flush.S" @@ -43,14 +43,14 @@ VARIABLE (grub_relocator_backward_start) addu $9, $9, $10 addu $8, $8, $10 /* Backward movsl is implicitly off-by-one. compensate that. */ - addiu $9, $9, 0xffff - addiu $8, $8, 0xffff + addiu $9, $9, -1 + addiu $8, $8, -1 copycont2: lb $11,0($8) sb $11,0($9) - addiu $8, $8, 0xffff - addiu $9, $9, 0xffff - addiu $10, 0xffff + addiu $8, $8, -1 + addiu $9, $9, -1 + addiu $10, $10, -1 bne $10, $0, copycont2 #include "../../kern/mips/cache_flush.S" From d1de6ed1dc4eb9783e3dfd2f08d0cb0c2d40b82d Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 6 Feb 2010 00:33:20 +0100 Subject: [PATCH 083/178] First compiling newreloc for ppc (not yet tested) --- conf/powerpc-ieee1275.rmk | 7 ++ include/grub/powerpc/relocator.h | 37 ++++++++ kern/powerpc/cache.S | 26 +----- kern/powerpc/cache_flush.S | 43 ++++++++++ lib/powerpc/relocator.c | 141 +++++++++++++++++++++++++++++++ lib/powerpc/relocator_asm.S | 60 +++++++++++++ 6 files changed, 290 insertions(+), 24 deletions(-) create mode 100644 include/grub/powerpc/relocator.h create mode 100644 kern/powerpc/cache_flush.S create mode 100644 lib/powerpc/relocator.c create mode 100644 lib/powerpc/relocator_asm.S diff --git a/conf/powerpc-ieee1275.rmk b/conf/powerpc-ieee1275.rmk index 2a3334a4f..91f705bb4 100644 --- a/conf/powerpc-ieee1275.rmk +++ b/conf/powerpc-ieee1275.rmk @@ -100,4 +100,11 @@ lsmmap_mod_SOURCES = commands/lsmmap.c lsmmap_mod_CFLAGS = $(COMMON_CFLAGS) lsmmap_mod_LDFLAGS = $(COMMON_LDFLAGS) +# For relocator.mod. +pkglib_MODULES += relocator.mod +relocator_mod_SOURCES = lib/$(target_cpu)/relocator.c lib/relocator.c lib/$(target_cpu)/relocator_asm.S +relocator_mod_CFLAGS = $(COMMON_CFLAGS) +relocator_mod_ASFLAGS = $(COMMON_ASFLAGS) +relocator_mod_LDFLAGS = $(COMMON_LDFLAGS) + include $(srcdir)/conf/common.mk diff --git a/include/grub/powerpc/relocator.h b/include/grub/powerpc/relocator.h new file mode 100644 index 000000000..c2780bbca --- /dev/null +++ b/include/grub/powerpc/relocator.h @@ -0,0 +1,37 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2009,2010 Free Software Foundation, Inc. + * + * GRUB 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. + * + * GRUB 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 GRUB. If not, see . + */ + +#ifndef GRUB_RELOCATOR_CPU_HEADER +#define GRUB_RELOCATOR_CPU_HEADER 1 + +#include +#include +#include + +#define GRUB_PPC_JUMP_REGISTER 31 + +struct grub_relocator32_state +{ + grub_uint32_t gpr[32]; +}; + +grub_err_t +grub_relocator32_boot (struct grub_relocator *rel, + struct grub_relocator32_state state); + +#endif /* ! GRUB_RELOCATOR_CPU_HEADER */ diff --git a/kern/powerpc/cache.S b/kern/powerpc/cache.S index da982afa0..d85e68d42 100644 --- a/kern/powerpc/cache.S +++ b/kern/powerpc/cache.S @@ -1,7 +1,7 @@ /* cache.S - Flush the processor cache for a specific region. */ /* * GRUB -- GRand Unified Bootloader - * Copyright (C) 2004,2007 Free Software Foundation, Inc. + * Copyright (C) 2004,2007,2010 Free Software Foundation, Inc. * * GRUB is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,32 +17,10 @@ * along with GRUB. If not, see . */ -#define CACHE_LINE_BYTES 32 - .text .align 2 .globl grub_arch_sync_caches grub_arch_sync_caches: - /* `address' may not be CACHE_LINE_BYTES-aligned. */ - andi. 6, 3, CACHE_LINE_BYTES - 1 /* Find the misalignment. */ - add 4, 4, 6 /* Adjust `size' to compensate. */ - - /* Force the dcache lines to memory. */ - li 5, 0 -1: dcbst 5, 3 - addi 5, 5, CACHE_LINE_BYTES - cmpw 5, 4 - blt 1b - sync /* Force all dcbsts to complete. */ - - /* Invalidate the icache lines. */ - li 5, 0 -1: icbi 5, 3 - addi 5, 5, CACHE_LINE_BYTES - cmpw 5, 4 - blt 1b - sync /* Force all icbis to complete. */ - isync /* Discard partially executed instructions that were - loaded from the invalid icache. */ +#include "cache_flush.S" blr diff --git a/kern/powerpc/cache_flush.S b/kern/powerpc/cache_flush.S new file mode 100644 index 000000000..1410f78b1 --- /dev/null +++ b/kern/powerpc/cache_flush.S @@ -0,0 +1,43 @@ +/* cache.S - Flush the processor cache for a specific region. */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2004,2007,2010 Free Software Foundation, Inc. + * + * GRUB 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. + * + * GRUB 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 GRUB. If not, see . + */ + +#undef CACHE_LINE_BYTES +#define CACHE_LINE_BYTES 32 + + /* `address' may not be CACHE_LINE_BYTES-aligned. */ + andi. 6, 3, CACHE_LINE_BYTES - 1 /* Find the misalignment. */ + add 4, 4, 6 /* Adjust `size' to compensate. */ + + /* Force the dcache lines to memory. */ + li 5, 0 +1: dcbst 5, 3 + addi 5, 5, CACHE_LINE_BYTES + cmpw 5, 4 + blt 1b + sync /* Force all dcbsts to complete. */ + + /* Invalidate the icache lines. */ + li 5, 0 +1: icbi 5, 3 + addi 5, 5, CACHE_LINE_BYTES + cmpw 5, 4 + blt 1b + sync /* Force all icbis to complete. */ + isync /* Discard partially executed instructions that were + loaded from the invalid icache. */ diff --git a/lib/powerpc/relocator.c b/lib/powerpc/relocator.c new file mode 100644 index 000000000..9f5fc1c7f --- /dev/null +++ b/lib/powerpc/relocator.c @@ -0,0 +1,141 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2009,2010 Free Software Foundation, Inc. + * + * GRUB 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. + * + * GRUB 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 GRUB. If not, see . + */ + +#include +#include + +#include +#include +#include +#include + +#include +#include + +extern grub_uint8_t grub_relocator_forward_start; +extern grub_uint8_t grub_relocator_forward_end; +extern grub_uint8_t grub_relocator_backward_start; +extern grub_uint8_t grub_relocator_backward_end; + +#define REGW_SIZEOF (2 * sizeof (grub_uint32_t)) +#define JUMP_SIZEOF (sizeof (grub_uint32_t)) + +#define RELOCATOR_SRC_SIZEOF(x) (&grub_relocator_##x##_end \ + - &grub_relocator_##x##_start) +#define RELOCATOR_SIZEOF(x) (RELOCATOR_SRC_SIZEOF(x) \ + + REGW_SIZEOF * 3) +grub_size_t grub_relocator_align = sizeof (grub_uint32_t); +grub_size_t grub_relocator_forward_size; +grub_size_t grub_relocator_backward_size; +grub_size_t grub_relocator_jumper_size = JUMP_SIZEOF + REGW_SIZEOF; + +void +grub_cpu_relocator_init (void) +{ + grub_relocator_forward_size = RELOCATOR_SIZEOF(forward); + grub_relocator_backward_size = RELOCATOR_SIZEOF(backward); +} + +static void +write_reg (int regn, grub_uint32_t val, void **target) +{ + /* lis r, val >> 16 */ + *(grub_uint32_t *) *target = + ((0x3c00 | (regn << 5)) << 16) | (val >> 16); + *target = ((grub_uint32_t *) *target) + 1; + /* ori r, r, val & 0xffff. */ + *(grub_uint32_t *) *target = (((0x6000 | regn << 5 | regn) << 16) + | (val & 0xffff)); + *target = ((grub_uint32_t *) *target) + 1; +} + +static void +write_jump (void **target) +{ + /* blr. */ + *(grub_uint32_t *) *target = 0x4e800020; + *target = ((grub_uint32_t *) *target) + 1; +} + +void +grub_cpu_relocator_jumper (void *rels, grub_addr_t addr) +{ + write_reg (GRUB_PPC_JUMP_REGISTER, addr, &rels); + write_jump (&rels); +} + +void +grub_cpu_relocator_backward (void *ptr0, void *src, void *dest, + grub_size_t size) +{ + void *ptr = ptr0; + write_reg (8, (grub_uint32_t) src, &ptr); + write_reg (9, (grub_uint32_t) dest, &ptr); + write_reg (10, (grub_uint32_t) size, &ptr); + grub_memcpy (ptr, &grub_relocator_backward_start, + RELOCATOR_SRC_SIZEOF (backward)); +} + +void +grub_cpu_relocator_forward (void *ptr0, void *src, void *dest, + grub_size_t size) +{ + void *ptr = ptr0; + write_reg (8, (grub_uint32_t) src, &ptr); + write_reg (9, (grub_uint32_t) dest, &ptr); + write_reg (10, (grub_uint32_t) size, &ptr); + grub_memcpy (ptr, &grub_relocator_forward_start, + RELOCATOR_SRC_SIZEOF (forward)); +} + +grub_err_t +grub_relocator32_boot (struct grub_relocator *rel, + struct grub_relocator32_state state) +{ + grub_addr_t target; + void *src, *ptr; + grub_err_t err; + grub_addr_t relst; + grub_size_t relsize; + grub_size_t stateset_size = 32 * REGW_SIZEOF + JUMP_SIZEOF; + unsigned i; + + err = grub_relocator_alloc_chunk_align (rel, &src, &target, 0, + (0xffffffff - stateset_size) + + 1, stateset_size, + sizeof (grub_uint32_t), + GRUB_RELOCATOR_PREFERENCE_NONE); + if (err) + return err; + + ptr = src; + for (i = 0; i < 32; i++) + write_reg (i, state.gpr[i], &ptr); + write_jump (&ptr); + + err = grub_relocator_prepare_relocs (rel, target, &relst, &relsize); + if (err) + return err; + + grub_arch_sync_caches ((void *) relst, relsize); + + ((void (*) (void)) relst) (); + + /* Not reached. */ + return GRUB_ERR_NONE; +} diff --git a/lib/powerpc/relocator_asm.S b/lib/powerpc/relocator_asm.S new file mode 100644 index 000000000..355e9c8b4 --- /dev/null +++ b/lib/powerpc/relocator_asm.S @@ -0,0 +1,60 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2009,2010 Free Software Foundation, Inc. + * + * GRUB 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. + * + * GRUB 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 GRUB. If not, see . + */ + +#include + + .p2align 4 /* force 16-byte alignment */ + +VARIABLE (grub_relocator_forward_start) + mr 3, 9 + mr 4, 10 + +copycont1: + lbz 11,0(8) + stb 11,0(9) + addi 8, 8, 0x1 + addi 9, 9, 0x1 + addi 10, 10, -1 + cmpwi 10, 0 + bne copycont1 + +#include "../../kern/powerpc/cache_flush.S" + +VARIABLE (grub_relocator_forward_end) + +VARIABLE (grub_relocator_backward_start) + mr 3, 9 + mr 4, 10 + + add 9, 9, 10 + add 8, 8, 10 + /* Backward movsl is implicitly off-by-one. compensate that. */ + addi 9, 9, -1 + addi 8, 8, -1 +copycont2: + lbz 11,0(8) + stb 11,0(9) + addi 8, 8, -1 + addi 9, 9, -1 + addi 10, 10, -1 + cmpwi 10, 0 + bne copycont2 + +#include "../../kern/powerpc/cache_flush.S" + +VARIABLE (grub_relocator_backward_end) From accbdc88a5fa0917d0c061913e76f729903cd216 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Fri, 2 Apr 2010 18:43:52 +0200 Subject: [PATCH 084/178] Use scanline for relocator to allow multiple memory sources --- lib/relocator.c | 832 ++++++++++++++++++++++++++++++------------------ 1 file changed, 524 insertions(+), 308 deletions(-) diff --git a/lib/relocator.c b/lib/relocator.c index 298c13d06..38aa67502 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -22,7 +22,6 @@ #include #include -/* TODO: use more efficient data structures if necessary. */ /* FIXME: check memory map. */ /* FIXME: try to request memory from firmware. */ @@ -35,14 +34,22 @@ struct grub_relocator grub_size_t relocators_size; }; +struct grub_relocator_subchunk +{ + enum {CHUNK_TYPE_IN_REGION, CHUNK_TYPE_REGION_START} type; + grub_addr_t host_start; + grub_addr_t start; + grub_size_t size; +}; + struct grub_relocator_chunk { struct grub_relocator_chunk *next; grub_addr_t src; grub_addr_t target; grub_size_t size; - enum {CHUNK_TYPE_IN_REGION, CHUNK_TYPE_REGION_START} type; - grub_addr_t host_start; + struct grub_relocator_subchunk *subchunks; + unsigned nsubchunks; }; struct grub_relocator * @@ -63,143 +70,148 @@ grub_relocator_new (void) return ret; } -static grub_mm_header_t -get_best_header (struct grub_relocator *rel, - grub_addr_t start, grub_addr_t end, grub_addr_t align, - grub_size_t size, - grub_mm_region_t rb, grub_mm_header_t *prev, - grub_addr_t *best_addr, int from_low_priv, int collisioncheck) +struct event { - grub_mm_header_t h, hp; - grub_mm_header_t hb = NULL, hbp = NULL; - - auto void try_addr (grub_addr_t allowable_start, grub_addr_t allowable_end); - void try_addr (grub_addr_t allowable_start, grub_addr_t allowable_end) + enum { + IN_REG_START = 0, + IN_REG_END = 1, + REG_BEG_START = 2, + REG_BEG_END = REG_BEG_START | 1, + COLLISION_START = 4, + COLLISION_END = COLLISION_START | 1 + } type; + grub_addr_t pos; + union { - if (from_low_priv) - { - grub_addr_t addr; - - addr = ALIGN_UP (allowable_start, align); - - if (addr < start) - addr = ALIGN_UP (start, align); - - if (collisioncheck) - while (1) - { - struct grub_relocator_chunk *chunk; - for (chunk = rel->chunks; chunk; chunk = chunk->next) - if ((chunk->target <= addr - && addr < chunk->target + chunk->size) - || (chunk->target < addr + size - && addr + size < chunk->target + chunk->size) - || (addr <= chunk->target && chunk->target < addr + size) - || (addr < chunk->target + chunk->size - && chunk->target + chunk->size < addr + size)) - { - grub_dprintf ("relocator", - "collision 0x%llx+0x%llx, 0x%llx+0x%llx\n", - (unsigned long long) chunk->target, - (unsigned long long) chunk->size, - (unsigned long long) addr, - (unsigned long long) size); - addr = ALIGN_UP (chunk->target + chunk->size, align); - break; - } - if (!chunk) - break; - } - - if (allowable_end <= addr + size) - return; - - if (addr > end) - return; - - if (hb == NULL || *best_addr > addr) - { - hb = h; - hbp = hp; - *best_addr = addr; - grub_dprintf ("relocator", "picked %p/%lx\n", hb, - (unsigned long) addr); - } - } - else - { - grub_addr_t addr; - - addr = ALIGN_DOWN (allowable_end - size, align); - - if (addr > end) - addr = ALIGN_DOWN (end, align); - - if (collisioncheck) - while (1) - { - struct grub_relocator_chunk *chunk; - for (chunk = rel->chunks; chunk; chunk = chunk->next) - if ((chunk->target <= addr - && addr < chunk->target + chunk->size) - || (chunk->target < addr + size - && addr + size < chunk->target + chunk->size) - || (addr <= chunk->target && chunk->target < addr + size) - || (addr < chunk->target + chunk->size - && chunk->target + chunk->size < addr + size)) - { - addr = ALIGN_DOWN (chunk->target - size, align); - break; - } - if (!chunk) - break; - } - - if (allowable_start > addr) - return; - - if (addr < start) - return; - - if (hb == NULL || *best_addr < addr) - { - hb = h; - hbp = hp; - *best_addr = addr; - grub_dprintf ("relocator", "picked %p/%lx\n", hb, - (unsigned long) addr); - } - } - } - - hp = rb->first; - h = hp->next; - grub_dprintf ("relocator", "alive\n"); - do + struct { - grub_addr_t allowable_start, allowable_end; - allowable_start = (grub_addr_t) h; - allowable_end = (grub_addr_t) (h + h->size); + grub_mm_region_t reg; + grub_mm_header_t hancestor; + grub_mm_region_t *regancestor; + grub_mm_header_t head; + }; + }; +}; - if (h->magic != GRUB_MM_FREE_MAGIC) - grub_fatal ("free magic is broken at %p: 0x%x", h, h->magic); +#define DIGITSORT_BITS 8 +#define DIGITSORT_MASK ((1 << DIGITSORT_BITS) - 1) +#define BITS_IN_BYTE 8 - try_addr (allowable_start, allowable_end); +#define max(a, b) (((a) > (b)) ? (a) : (b)) +#define min(a, b) (((a) < (b)) ? (a) : (b)) - if ((grub_addr_t) h == (grub_addr_t) (rb + 1)) - { - grub_dprintf ("relocator", "Trying region start 0x%llx\n", - (unsigned long long) (allowable_start - - sizeof (*rb) - rb->pre_size)); - try_addr (allowable_start - sizeof (*rb) - rb->pre_size, - allowable_end - sizeof (*rb)); - } - hp = h; - h = hp->next; +static inline int +is_start (int type) +{ + return !(type & 1) && (type != COLLISION_START); +} + +static void +allocate_regstart (grub_addr_t addr, grub_size_t size, grub_mm_region_t rb, + grub_mm_region_t *regancestor, grub_mm_header_t hancestor) +{ + grub_addr_t newreg_start, newreg_raw_start = addr + size; + grub_addr_t newreg_size, newreg_presize; + grub_mm_header_t new_header; + grub_mm_header_t hb = (grub_mm_header_t) (rb + 1); + + grub_dprintf ("relocator", "ra = %p, rb = %p\n", regancestor, rb); + + newreg_start = ALIGN_UP (newreg_raw_start, GRUB_MM_ALIGN); + newreg_presize = newreg_start - newreg_raw_start; + newreg_size = rb->size - (newreg_start - (grub_addr_t) rb); + if ((hb->size << GRUB_MM_ALIGN_LOG2) >= newreg_start + - (grub_addr_t) rb) + { + grub_mm_header_t newhnext = hb->next; + grub_size_t newhsize = ((hb->size << GRUB_MM_ALIGN_LOG2) + - (newreg_start + - (grub_addr_t) rb)) >> GRUB_MM_ALIGN_LOG2; + new_header = (void *) (newreg_start + sizeof (*rb)); + if (newhnext == hb) + newhnext = new_header; + new_header->next = newhnext; + new_header->size = newhsize; + new_header->magic = GRUB_MM_FREE_MAGIC; + } + else + { + new_header = hb->next; + if (new_header == hb) + new_header = (void *) (newreg_start + sizeof (*rb)); + } + { + struct grub_mm_header *newregfirst = rb->first; + struct grub_mm_region *newregnext = rb->next; + struct grub_mm_region *newreg = (void *) newreg_start; + hancestor->next = new_header; + if (newregfirst == hb) + newregfirst = new_header; + newreg->first = newregfirst; + newreg->next = newregnext; + newreg->pre_size = newreg_presize; + newreg->size = newreg_size; + *regancestor = newreg; + { + grub_mm_header_t h = newreg->first, hp = NULL; + do + { + if ((void *) h < (void *) (newreg + 1)) + grub_fatal ("Failed to adjust memory region: %p, %p, %p, %p, %p", + newreg, newreg->first, h, hp, hb); + if ((void *) h == (void *) (newreg + 1)) + grub_dprintf ("relocator", + "Free start memory region: %p, %p, %p, %p, %p", + newreg, newreg->first, h, hp, hb); + + hp = h; + h = h->next; + } + while (h != newreg->first); + } + } + +} + +static void +allocate_inreg (grub_addr_t addr, grub_size_t size, + grub_mm_header_t hb, grub_mm_header_t hbp, + grub_mm_region_t rb) +{ + struct grub_mm_header *foll = NULL; + + if (ALIGN_UP (addr + size, GRUB_MM_ALIGN) + GRUB_MM_ALIGN + <= (grub_addr_t) (hb + hb->size)) + { + foll = (void *) ALIGN_UP (addr + size, GRUB_MM_ALIGN); + foll->magic = GRUB_MM_FREE_MAGIC; + foll->size = hb->size - (foll - hb); + } + + if (addr - (grub_addr_t) hb >= sizeof (*hb)) + { + hb->size = ((addr - (grub_addr_t) hb) >> GRUB_MM_ALIGN_LOG2); + if (foll) + { + foll->next = hb; + hbp->next = foll; + if (rb->first == hb) + rb->first = foll; + } + } + else + { + if (foll) + foll->next = hb->next; + else + foll = hb->next; + + hbp->next = foll; + if (rb->first == hb) + rb->first = foll; + if (rb->first == hb) + rb->first = (void *) (rb + 1); } - while (hp && hp != rb->first); - *prev = hbp; - return hb; } static int @@ -208,159 +220,355 @@ malloc_in_range (struct grub_relocator *rel, grub_size_t size, struct grub_relocator_chunk *res, int from_low_priv, int collisioncheck) { - grub_mm_region_t rb, rbp; - grub_mm_header_t hb = NULL, hbp = NULL; - grub_addr_t best_addr; + grub_mm_region_t r, *ra, base_saved; + struct event *events = NULL, *eventt = NULL, *t; + unsigned maxevents = 2; + grub_mm_header_t p, pa; + unsigned *counter; + int nallocs = 0; + unsigned i, j, N = 0; + grub_addr_t target = 0; - again: + grub_dprintf ("relocator", + "trying to allocate in %x-%x aligned %x size %x\n", + start, end, align, size); - rb = NULL, rbp = NULL; - - { - grub_mm_region_t r, rp; - for (rp = NULL, r = grub_mm_base; r; rp = r, r = r->next) - { - grub_dprintf ("relocator", "region %p. %d %d %d %d\n", r, - (grub_addr_t) r + r->size + sizeof (*r) >= start, - (grub_addr_t) r < end, r->size + sizeof (*r) >= size, - (rb == NULL || (from_low_priv ? rb > r : rb < r))); - if ((grub_addr_t) r + r->size + sizeof (*r) >= start - && (grub_addr_t) r < end && r->size + sizeof (*r) >= size - && (rb == NULL || (from_low_priv ? rb > r : rb < r))) - { - rb = r; - rbp = rp; - } - } - } + start = ALIGN_UP (start, align); + end = ALIGN_DOWN (end - size, align) + size; + grub_dprintf ("relocator", + "trying to allocate in %x-%x aligned %x size %x\n", + start, end, align, size); - if (!rb) + if (end < start + size) + return 0; + + /* We have to avoid any allocations when filling scanline events. + Hence 2-stages. + */ + for (r = grub_mm_base; r; r = r->next) { - grub_dprintf ("relocator", "no suitable region found\n"); + p = r->first; + do + { + maxevents += 2; + p = p->next; + } + while (p != r->first); + maxevents += 2; + } + if (collisioncheck && rel) + { + struct grub_relocator_chunk *chunk; + for (chunk = rel->chunks; chunk; chunk = chunk->next) + maxevents += 2; + } + + events = grub_malloc (maxevents * sizeof (events[0])); + eventt = grub_malloc (maxevents * sizeof (events[0])); + counter = grub_malloc ((DIGITSORT_MASK + 2) * sizeof (counter[0])); + if (!events || !eventt || !counter) + { + grub_dprintf ("relocator", "events or counter allocation failed %d\n", + maxevents); + grub_free (events); + grub_free (eventt); + grub_free (counter); return 0; } - grub_dprintf ("relocator", "trying region %p - %p\n", rb, rb + rb->size + 1); - - hb = get_best_header (rel, start, end, align, size, rb, &hbp, &best_addr, - from_low_priv, collisioncheck); - - grub_dprintf ("relocator", "best header %p/%p/%lx\n", hb, hbp, - (unsigned long) best_addr); - - if (!hb) + if (collisioncheck && rel) { - if (from_low_priv) - start = (grub_addr_t) (rb + rb->size + sizeof (*rb)); - else - end = (grub_addr_t) rb - 1; - goto again; + struct grub_relocator_chunk *chunk; + for (chunk = rel->chunks; chunk; chunk = chunk->next) + { + events[N].type = COLLISION_START; + events[N].pos = chunk->target; + N++; + events[N].type = COLLISION_END; + events[N].pos = chunk->target + chunk->size; + N++; + } } - /* Special case: relocating region start. */ - if (best_addr < (grub_addr_t) hb) + /* No malloc from this point. */ + base_saved = grub_mm_base; + grub_mm_base = NULL; + + for (ra = &base_saved, r = *ra; r; ra = &(r->next), r = *ra) { - grub_addr_t newreg_start, newreg_raw_start = best_addr + size; - grub_addr_t newreg_size, newreg_presize; - grub_mm_header_t new_header; - - res->src = best_addr; - res->type = CHUNK_TYPE_REGION_START; - res->host_start = (grub_addr_t) rb - rb->pre_size; - - newreg_start = ALIGN_UP (newreg_raw_start, GRUB_MM_ALIGN); - newreg_presize = newreg_start - newreg_raw_start; - newreg_size = rb->size - (newreg_start - (grub_addr_t) rb); - if ((hb->size << GRUB_MM_ALIGN_LOG2) >= newreg_start - - (grub_addr_t) rb) - { - grub_mm_header_t newhnext = hb->next; - grub_size_t newhsize = ((hb->size << GRUB_MM_ALIGN_LOG2) - - (newreg_start - - (grub_addr_t) rb)) >> GRUB_MM_ALIGN_LOG2; - new_header = (void *) (newreg_start + sizeof (*rb)); - if (newhnext == hb) - newhnext = new_header; - new_header->next = newhnext; - new_header->size = newhsize; - new_header->magic = GRUB_MM_FREE_MAGIC; - } - else - { - new_header = hb->next; - if (new_header == hb) - new_header = (void *) (newreg_start + sizeof (*rb)); - } - { - struct grub_mm_header *newregfirst = rb->first; - struct grub_mm_region *newregnext = rb->next; - struct grub_mm_region *newreg = (void *) newreg_start; - hbp->next = new_header; - if (newregfirst == hb) - newregfirst = new_header; - newreg->first = newregfirst; - newreg->next = newregnext; - newreg->pre_size = newreg_presize; - newreg->size = newreg_size; - if (rbp) - rbp->next = newreg; - else - grub_mm_base = newreg; - { - grub_mm_header_t h = newreg->first, hp = NULL; - do + int pre_added = 0; + pa = r->first; + p = pa->next; + do + { + if (p == (grub_mm_header_t) (r + 1)) { - if ((void *) h < (void *) (newreg + 1)) - grub_fatal ("Failed to adjust memory region: %p, %p, %p, %p, %p", - newreg, newreg->first, h, hp, hb); - hp = h; - h = h->next; + pre_added = 1; + events[N].type = REG_BEG_START; + events[N].pos = (grub_addr_t) r - r->pre_size; + events[N].reg = r; + events[N].regancestor = ra; + events[N].head = p; + events[N].hancestor = pa; + N++; + events[N].type = REG_BEG_END; + events[N].pos = (grub_addr_t) (p + p->size) - sizeof (*r); + N++; } - while (h != newreg->first); + else + { + events[N].type = IN_REG_START; + events[N].pos = (grub_addr_t) p; + events[N].head = p; + events[N].hancestor = pa; + events[N].reg = r; + N++; + events[N].type = IN_REG_END; + events[N].pos = (grub_addr_t) (p + p->size); + N++; + } + pa = p; + p = pa->next; + } + while (pa != r->first); + /* FIXME */ + if (0)//if (!pre_added) + { + events[N].type = REG_BEG_START; + events[N].pos = (grub_addr_t) r - r->pre_size; + events[N].reg = r; + N++; + events[N].type = REG_BEG_END; + events[N].pos = (grub_addr_t) r; + N++; } - } - return 1; } + + /* Put ending events after starting events. */ { - struct grub_mm_header *foll = NULL; - - res->src = best_addr; - res->type = CHUNK_TYPE_IN_REGION; - - if (ALIGN_UP (best_addr + size, GRUB_MM_ALIGN) + GRUB_MM_ALIGN - <= (grub_addr_t) (hb + hb->size)) - { - foll = (void *) ALIGN_UP (best_addr + size, GRUB_MM_ALIGN); - foll->magic = GRUB_MM_FREE_MAGIC; - foll->size = hb->size - (foll - hb); - } - - if (best_addr - (grub_addr_t) hb >= sizeof (*hb)) - { - hb->size = ((best_addr - (grub_addr_t) hb) >> GRUB_MM_ALIGN_LOG2); - if (foll) - { - foll->next = hb; - hbp->next = foll; - if (rb->first == hb) - rb->first = foll; - } - } - else - { - if (foll) - foll->next = hb->next; - else - foll = hb->next; - - hbp->next = foll; - if (rb->first == hb) - rb->first = foll; - if (rb->first == hb) - rb->first = (void *) (rb + 1); - } - return 1; + int st = 0, e = N / 2; + for (j = 0; j < N; j++) + if (is_start (events[j].type) || events[j].type == COLLISION_START) + eventt[st++] = events[j]; + else + eventt[e++] = events[j]; + t = eventt; + eventt = events; + events = t; } + for (i = 0; i < (BITS_IN_BYTE * sizeof (grub_addr_t) / DIGITSORT_BITS); + i++) + { + memset (counter, 0, (1 + (1 << DIGITSORT_BITS)) * sizeof (counter[0])); + for (j = 0; j < N; j++) + counter[((events[j].pos >> (DIGITSORT_BITS * i)) + & DIGITSORT_MASK) + 1]++; + for (j = 0; j <= DIGITSORT_MASK; j++) + counter[j+1] += counter[j]; + for (j = 0; j < N; j++) + eventt[counter[((events[j].pos >> (DIGITSORT_BITS * i)) + & DIGITSORT_MASK)]++] = events[j]; + t = eventt; + eventt = events; + events = t; + } + + grub_dprintf ("relocator", "scanline events:\n"); + for (j = 0; j < N; j++) + grub_dprintf ("relocator", "event %x, type %d\n", events[j].pos, + events[j].type); + + /* Now events are nicely sorted. */ + if (from_low_priv) + { + int nstarted = 0, ncollisions = 0; + grub_addr_t starta = 0; + int numstarted; + for (j = 0; j < N; j++) + { + switch (events[j].type) + { + case COLLISION_END: + ncollisions--; + case IN_REG_START: + case REG_BEG_START: + if ((events[j].type == COLLISION_END ? nstarted != 0 + : nstarted == 0) + && ncollisions == 0) + { + starta = ALIGN_UP (events[j].pos, align); + numstarted = j; + } + if (events[j].type != COLLISION_END) + nstarted++; + break; + + case IN_REG_END: + case REG_BEG_END: + nstarted--; + case COLLISION_START: + if (((events[j].type == COLLISION_START) + ? nstarted != 0 : nstarted == 0) + && ncollisions == 0) + { + target = starta; + if (target < start) + target = start; + grub_dprintf ("relocator", "%x, %x, %x\n", target, start, + events[j].pos); + if (target + size <= end && target + size <= events[j].pos) + /* Found an usable address. */ + goto found; + } + if (events[j].type == COLLISION_START) + ncollisions++; + break; + } + } + } + else + { + int nstarted = 0, ncollisions = 0; + grub_addr_t enda = 0; + int numend; + for (j = N - 1; j != (unsigned) -1; j--) + { + switch (events[j].type) + { + case COLLISION_START: + ncollisions--; + case IN_REG_END: + case REG_BEG_END: + if ((events[j].type == COLLISION_END ? nstarted != 0 + : nstarted == 0) + && ncollisions == 0) + { + enda = ALIGN_DOWN (events[j].pos - size, align) + size; + numend = j; + } + nstarted++; + break; + + case IN_REG_START: + case REG_BEG_START: + nstarted--; + case COLLISION_END: + if ((events[j].type == COLLISION_START ? nstarted != 0 + : nstarted == 0) + && ncollisions == 0) + { + target = enda - size; + if (target > end - size) + target = end - size; + grub_dprintf ("relocator", "%x, %x, %x\n", target, start, + events[j].pos); + if (target >= start && target >= events[j].pos) + goto found; + } + if (events[j].type == COLLISION_START) + ncollisions++; + break; + } + } + } + + grub_mm_base = base_saved; + grub_free (events); + grub_free (eventt); + grub_free (counter); + return 0; + + found: + { + int last_start = 0; + for (j = 0; j < N; j++) + { + if (j != 0 && events[j - 1].pos != events[j].pos) + { + grub_addr_t alloc_start, alloc_end; + alloc_start = max (events[j - 1].pos, target); + alloc_end = min (events[j].pos, target + size); + if (alloc_end > alloc_start) + { + grub_dprintf ("relocator", "%d\n", last_start); + + if (events[last_start].type == REG_BEG_START + || events[last_start].type == IN_REG_START) + { + if (events[last_start].type == REG_BEG_START && + (grub_addr_t) (events[last_start].reg + 1) > target) + allocate_regstart (alloc_start, alloc_end - alloc_start, + events[last_start].reg, + events[last_start].regancestor, + events[last_start].hancestor); + else + allocate_inreg (alloc_start, alloc_end - alloc_start, + events[last_start].head, + events[last_start].hancestor, + events[last_start].reg); + } + nallocs++; + } + } + if (is_start (events[j].type)) + last_start = j; + } + } + + grub_memset ((void *) target, 0, size); + grub_dprintf ("relocator", "baseptr = %p\n", &base_saved); + for (r = base_saved; r; r = r->next) + { + p = r->first; + do + { + if (!p) + grub_fatal ("null in the ring %p %p\n", r, p); + p = p->next; + } + while (p != r->first); + } + + /* Malloc is available again. */ + grub_mm_base = base_saved; + + /* FIXME: react on out of memory. */ + res->subchunks = grub_malloc (sizeof (res->subchunks[0]) * nallocs); + res->nsubchunks = nallocs; + + { + int last_start = 0; + int cural = 0; + for (j = 0; j < N; j++) + { + if (j != 0 && events[j - 1].pos != events[j].pos) + { + grub_addr_t alloc_start, alloc_end; + alloc_start = max (events[j - 1].pos, target); + alloc_end = min (events[j].pos, target + size); + if (alloc_end > alloc_start) + { + res->subchunks[cural].start = alloc_start; + res->subchunks[cural].size = alloc_end - alloc_start; + if (res->subchunks[last_start].type == IN_REG_START) + res->subchunks[cural].type = CHUNK_TYPE_IN_REGION; + else if (res->subchunks[last_start].type == REG_BEG_START) + { + res->subchunks[cural].type = CHUNK_TYPE_REGION_START; + res->subchunks[cural].host_start + = (grub_addr_t) events[last_start].reg; + } + cural++; + } + } + if (is_start (events[j].type)) + last_start = j; + } + } + res->src = target; + res->size = size; + grub_dprintf ("relocator", "allocated: %x %x\n", target, size); + return 1; } static void @@ -601,48 +809,56 @@ grub_relocator_unload (struct grub_relocator *rel) return; for (chunk = rel->chunks; chunk; chunk = next) { - switch (chunk->type) - { - case CHUNK_TYPE_REGION_START: + unsigned i; + for (i = 0; i < chunk->nsubchunks; i++) + switch (chunk->subchunks[i].type) { - grub_mm_region_t r1, r2, *rp; - grub_mm_header_t h; - grub_size_t pre_size; - r1 = (grub_mm_region_t) ALIGN_UP (chunk->src + chunk->size, - GRUB_MM_ALIGN); - r2 = (grub_mm_region_t) ALIGN_UP (chunk->host_start, GRUB_MM_ALIGN); - for (rp = &grub_mm_base; *rp && *rp != r2; rp = &((*rp)->next)); - if (!*rp) - grub_fatal ("Anomaly in region alocations detected. " - "Simultaneous relocators?"); - pre_size = ALIGN_UP (chunk->host_start, GRUB_MM_ALIGN) - - chunk->host_start; - r2->first = r1->first; - r2->next = r1->next; - r2->pre_size = pre_size; - r2->size = r1->size + (r2 - r1) * sizeof (*r2); - *rp = r1; - h = (grub_mm_header_t) (r1 + 1); - h->next = h; - h->magic = GRUB_MM_ALLOC_MAGIC; - h->size = (r2 - r1); - grub_free (h + 1); - break; + case CHUNK_TYPE_REGION_START: + { + grub_mm_region_t r1, r2, *rp; + grub_mm_header_t h; + grub_size_t pre_size; + r1 = (grub_mm_region_t) ALIGN_UP (chunk->subchunks[i].start + + chunk->subchunks[i].size, + GRUB_MM_ALIGN); + r2 = (grub_mm_region_t) ALIGN_UP (chunk->subchunks[i].host_start, + GRUB_MM_ALIGN); + for (rp = &grub_mm_base; *rp && *rp != r2; rp = &((*rp)->next)); + /* FIXME */ + if (!*rp) + grub_fatal ("Anomaly in region alocations detected. " + "Simultaneous relocators?"); + pre_size = ALIGN_UP (chunk->subchunks[i].host_start, + GRUB_MM_ALIGN) + - chunk->subchunks[i].host_start; + r2->first = r1->first; + r2->next = r1->next; + r2->pre_size = pre_size; + r2->size = r1->size + (r2 - r1) * sizeof (*r2); + *rp = r1; + h = (grub_mm_header_t) (r1 + 1); + h->next = h; + h->magic = GRUB_MM_ALLOC_MAGIC; + h->size = (r2 - r1); + grub_free (h + 1); + break; + } + case CHUNK_TYPE_IN_REGION: + { + grub_mm_header_t h + = (grub_mm_header_t) ALIGN_DOWN (chunk->subchunks[i].start, + GRUB_MM_ALIGN); + h->size = (chunk->subchunks[i].start / GRUB_MM_ALIGN) + - ((chunk->subchunks[i].start + chunk->subchunks[i].size + + GRUB_MM_ALIGN - 1) / GRUB_MM_ALIGN); + h->next = h; + h->magic = GRUB_MM_ALLOC_MAGIC; + grub_free (h + 1); + break; + } } - case CHUNK_TYPE_IN_REGION: - { - grub_mm_header_t h = (grub_mm_header_t) ALIGN_DOWN (chunk->src, - GRUB_MM_ALIGN); - h->size = (chunk->src / GRUB_MM_ALIGN) - - ((chunk->src + chunk->size + GRUB_MM_ALIGN - 1) - / GRUB_MM_ALIGN); - h->next = h; - h->magic = GRUB_MM_ALLOC_MAGIC; - grub_free (h + 1); - break; - } - } next = chunk->next; + grub_free (chunk->subchunks); grub_free (chunk); } } From 3a5768645c0594efc8fdd41a07661854e8709ccd Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 3 Apr 2010 11:53:29 +0200 Subject: [PATCH 085/178] First version of allocation from firmware --- conf/i386.rmk | 7 + include/grub/relocator_private.h | 46 ++++ lib/i386/relocator.c | 1 + lib/ieee1275/relocator.c | 83 ++++++ lib/relocator.c | 450 +++++++++++++++++++++---------- 5 files changed, 440 insertions(+), 147 deletions(-) create mode 100644 lib/ieee1275/relocator.c diff --git a/conf/i386.rmk b/conf/i386.rmk index 87514cb1f..868a767b1 100644 --- a/conf/i386.rmk +++ b/conf/i386.rmk @@ -16,9 +16,16 @@ vga_text_mod_CFLAGS = $(COMMON_CFLAGS) vga_text_mod_LDFLAGS = $(COMMON_LDFLAGS) pkglib_MODULES += relocator.mod +ifeq ($(platform), ieee1275) +relocator_mod_SOURCES = lib/relocator.c lib/i386/relocator32.S \ + lib/i386/relocator64.S lib/i386/relocator16.S \ + lib/$(target_cpu)/relocator_asm.S lib/i386/relocator.c \ + lib/ieee1275/relocator.c +else relocator_mod_SOURCES = lib/relocator.c lib/i386/relocator32.S \ lib/i386/relocator64.S lib/i386/relocator16.S \ lib/$(target_cpu)/relocator_asm.S lib/i386/relocator.c +endif relocator_mod_CFLAGS = $(COMMON_CFLAGS) relocator_mod_ASFLAGS = $(COMMON_ASFLAGS) relocator_mod_LDFLAGS = $(COMMON_LDFLAGS) diff --git a/include/grub/relocator_private.h b/include/grub/relocator_private.h index f9e76468e..c526b0b0c 100644 --- a/include/grub/relocator_private.h +++ b/include/grub/relocator_private.h @@ -21,6 +21,7 @@ #include #include +#include extern grub_size_t grub_relocator_align; extern grub_size_t grub_relocator_forward_size; @@ -38,4 +39,49 @@ void grub_cpu_relocator_backward (void *rels, void *src, void *tgt, grub_size_t size); void grub_cpu_relocator_jumper (void *rels, grub_addr_t addr); +#ifdef GRUB_MACHINE_IEEE1275 +#define GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS 1 +#else +#define GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS 0 +#endif + +struct grub_relocator_mmap_event +{ + enum { + IN_REG_START = 0, + IN_REG_END = 1, + REG_BEG_START = 2, + REG_BEG_END = REG_BEG_START | 1, +#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS + REG_FIRMWARE_START = 4, + REG_FIRMWARE_END = REG_FIRMWARE_START | 1, + /* To track the regions already in heap. */ + FIRMWARE_BLOCK_START = 6, + FIRMWARE_BLOCK_END = FIRMWARE_BLOCK_START | 1, +#endif + COLLISION_START = 8, + COLLISION_END = COLLISION_START | 1 + } type; + grub_addr_t pos; + union + { + struct + { + grub_mm_region_t reg; + grub_mm_header_t hancestor; + grub_mm_region_t *regancestor; + grub_mm_header_t head; + }; + }; +}; + +/* Return 0 on failure, 1 on success. The failure here + can be very time-expensive, so please make sure fill events is accurate. */ +#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS +int grub_relocator_firmware_alloc_region (grub_addr_t start, grub_size_t size); +unsigned grub_relocator_firmware_fill_events (struct grub_relocator_mmap_event *events); +unsigned grub_relocator_firmware_get_max_events (void); +void grub_relocator_firmware_free_region (grub_addr_t start, grub_size_t size); +#endif + #endif diff --git a/lib/i386/relocator.c b/lib/i386/relocator.c index 4eaa66890..a4038d75f 100644 --- a/lib/i386/relocator.c +++ b/lib/i386/relocator.c @@ -253,6 +253,7 @@ grub_relocator64_boot (struct grub_relocator *rel, return err; asm volatile ("cli"); + grub_printf ("%x\n", relst); ((void (*) (void)) relst) (); /* Not reached. */ diff --git a/lib/ieee1275/relocator.c b/lib/ieee1275/relocator.c new file mode 100644 index 000000000..bf7f4a821 --- /dev/null +++ b/lib/ieee1275/relocator.c @@ -0,0 +1,83 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2010 Free Software Foundation, Inc. + * + * GRUB 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. + * + * GRUB 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 GRUB. If not, see . + */ + +#include +#include +#include +#include + +unsigned +grub_relocator_firmware_get_max_events (void) +{ + int counter = 0; + auto int NESTED_FUNC_ATTR count (grub_uint64_t addr __attribute__ ((unused)), + grub_uint64_t len __attribute__ ((unused)), + grub_uint32_t type __attribute__ ((unused))); + int NESTED_FUNC_ATTR count (grub_uint64_t addr __attribute__ ((unused)), + grub_uint64_t len __attribute__ ((unused)), + grub_uint32_t type __attribute__ ((unused))) + { + counter++; + return 0; + } + + if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CANNOT_INTERPRET)) + return 0; + grub_machine_mmap_iterate (count); + return 2 * counter; +} + +unsigned +grub_relocator_firmware_fill_events (struct grub_relocator_mmap_event *events) +{ + int counter = 0; + auto int NESTED_FUNC_ATTR fill (grub_uint64_t addr, grub_uint64_t len, + grub_uint32_t type); + int NESTED_FUNC_ATTR fill (grub_uint64_t addr, grub_uint64_t len, + grub_uint32_t type) + { + if (type != GRUB_MACHINE_MEMORY_AVAILABLE) + return 0; + + events[counter].type = REG_FIRMWARE_START; + events[counter].pos = addr; + counter++; + events[counter].type = REG_FIRMWARE_END; + events[counter].pos = addr + len; + counter++; + + return 0; + } + + if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CANNOT_INTERPRET)) + return 0; + grub_machine_mmap_iterate (fill); + return counter; +} + +int +grub_relocator_firmware_alloc_region (grub_addr_t start, grub_size_t size) +{ + return (grub_claimmap (start, size) >= 0); +} + +void +grub_relocator_firmware_free_region (grub_addr_t start, grub_size_t size) +{ + grub_ieee1275_release (start, size); +} diff --git a/lib/relocator.c b/lib/relocator.c index 38aa67502..bf8fa3718 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -1,6 +1,6 @@ /* * GRUB -- GRand Unified Bootloader - * Copyright (C) 2009 Free Software Foundation, Inc. + * Copyright (C) 2009, 2010 Free Software Foundation, Inc. * * GRUB is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -23,7 +23,6 @@ #include /* FIXME: check memory map. */ -/* FIXME: try to request memory from firmware. */ struct grub_relocator { @@ -36,7 +35,11 @@ struct grub_relocator struct grub_relocator_subchunk { - enum {CHUNK_TYPE_IN_REGION, CHUNK_TYPE_REGION_START} type; + enum {CHUNK_TYPE_IN_REGION, CHUNK_TYPE_REGION_START, +#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS + CHUNK_TYPE_FIRMWARE +#endif + } type; grub_addr_t host_start; grub_addr_t start; grub_size_t size; @@ -52,6 +55,15 @@ struct grub_relocator_chunk unsigned nsubchunks; }; +struct grub_relocator_extra_block +{ + struct grub_relocator_extra_block *next; + grub_addr_t start; + grub_addr_t end; +}; + +struct grub_relocator_extra_block *extra_blocks; + struct grub_relocator * grub_relocator_new (void) { @@ -70,29 +82,6 @@ grub_relocator_new (void) return ret; } -struct event -{ - enum { - IN_REG_START = 0, - IN_REG_END = 1, - REG_BEG_START = 2, - REG_BEG_END = REG_BEG_START | 1, - COLLISION_START = 4, - COLLISION_END = COLLISION_START | 1 - } type; - grub_addr_t pos; - union - { - struct - { - grub_mm_region_t reg; - grub_mm_header_t hancestor; - grub_mm_region_t *regancestor; - grub_mm_header_t head; - }; - }; -}; - #define DIGITSORT_BITS 8 #define DIGITSORT_MASK ((1 << DIGITSORT_BITS) - 1) #define BITS_IN_BYTE 8 @@ -221,7 +210,7 @@ malloc_in_range (struct grub_relocator *rel, int from_low_priv, int collisioncheck) { grub_mm_region_t r, *ra, base_saved; - struct event *events = NULL, *eventt = NULL, *t; + struct grub_relocator_mmap_event *events = NULL, *eventt = NULL, *t; unsigned maxevents = 2; grub_mm_header_t p, pa; unsigned *counter; @@ -254,7 +243,7 @@ malloc_in_range (struct grub_relocator *rel, p = p->next; } while (p != r->first); - maxevents += 2; + maxevents += 4; } if (collisioncheck && rel) { @@ -263,6 +252,16 @@ malloc_in_range (struct grub_relocator *rel, maxevents += 2; } +#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS + { + struct grub_relocator_extra_block *cur; + for (cur = extra_blocks; cur; cur = cur->next) + maxevents += 2; + } +#endif + + maxevents += grub_relocator_firmware_get_max_events (); + events = grub_malloc (maxevents * sizeof (events[0])); eventt = grub_malloc (maxevents * sizeof (events[0])); counter = grub_malloc ((DIGITSORT_MASK + 2) * sizeof (counter[0])); @@ -290,6 +289,35 @@ malloc_in_range (struct grub_relocator *rel, } } +#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS + for (r = grub_mm_base; r; r = r->next) + { + grub_dprintf ("relocator", "Blocking at 0x%x-0x%x\n", + (grub_addr_t) r - r->pre_size, + (grub_addr_t) (r + 1) + r->size); + events[N].type = FIRMWARE_BLOCK_START; + events[N].pos = (grub_addr_t) r - r->pre_size; + N++; + events[N].type = FIRMWARE_BLOCK_END; + events[N].pos = (grub_addr_t) (r + 1) + r->size; + N++; + } + { + struct grub_relocator_extra_block *cur; + for (cur = extra_blocks; cur; cur = cur->next) + { + grub_dprintf ("relocator", "Blocking at 0x%x-0x%x\n", + cur->start, cur->end); + events[N].type = FIRMWARE_BLOCK_START; + events[N].pos = cur->start; + N++; + events[N].type = FIRMWARE_BLOCK_END; + events[N].pos = cur->end; + N++; + } + } +#endif + /* No malloc from this point. */ base_saved = grub_mm_base; grub_mm_base = NULL; @@ -344,6 +372,8 @@ malloc_in_range (struct grub_relocator *rel, } } + N += grub_relocator_firmware_fill_events (events + N); + /* Put ending events after starting events. */ { int st = 0, e = N / 2; @@ -373,104 +403,86 @@ malloc_in_range (struct grub_relocator *rel, events = t; } - grub_dprintf ("relocator", "scanline events:\n"); - for (j = 0; j < N; j++) - grub_dprintf ("relocator", "event %x, type %d\n", events[j].pos, - events[j].type); +#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS + retry: +#endif /* Now events are nicely sorted. */ - if (from_low_priv) - { - int nstarted = 0, ncollisions = 0; - grub_addr_t starta = 0; - int numstarted; - for (j = 0; j < N; j++) - { - switch (events[j].type) - { - case COLLISION_END: - ncollisions--; - case IN_REG_START: - case REG_BEG_START: - if ((events[j].type == COLLISION_END ? nstarted != 0 - : nstarted == 0) - && ncollisions == 0) - { - starta = ALIGN_UP (events[j].pos, align); - numstarted = j; - } - if (events[j].type != COLLISION_END) - nstarted++; - break; + { + int nstarted = 0, ncollisions = 0, nstartedfw = 0, nblockfw = 0; + grub_addr_t starta = 0; + int numstarted; + for (j = from_low_priv ? 0 : N - 1; from_low_priv ? j < N : (j + 1); + from_low_priv ? j++ : j--) + { + int isinsidebefore, isinsideafter; + isinsidebefore = (!ncollisions + && (nstarted || (nstartedfw && !nblockfw))); + switch (events[j].type) + { +#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS + case REG_FIRMWARE_START: + nstartedfw++; + break; - case IN_REG_END: - case REG_BEG_END: - nstarted--; - case COLLISION_START: - if (((events[j].type == COLLISION_START) - ? nstarted != 0 : nstarted == 0) - && ncollisions == 0) - { - target = starta; - if (target < start) - target = start; - grub_dprintf ("relocator", "%x, %x, %x\n", target, start, - events[j].pos); - if (target + size <= end && target + size <= events[j].pos) - /* Found an usable address. */ - goto found; - } - if (events[j].type == COLLISION_START) - ncollisions++; - break; - } - } - } - else - { - int nstarted = 0, ncollisions = 0; - grub_addr_t enda = 0; - int numend; - for (j = N - 1; j != (unsigned) -1; j--) - { - switch (events[j].type) - { - case COLLISION_START: - ncollisions--; - case IN_REG_END: - case REG_BEG_END: - if ((events[j].type == COLLISION_END ? nstarted != 0 - : nstarted == 0) - && ncollisions == 0) - { - enda = ALIGN_DOWN (events[j].pos - size, align) + size; - numend = j; - } - nstarted++; - break; + case REG_FIRMWARE_END: + nstartedfw--; + break; - case IN_REG_START: - case REG_BEG_START: - nstarted--; - case COLLISION_END: - if ((events[j].type == COLLISION_START ? nstarted != 0 - : nstarted == 0) - && ncollisions == 0) - { - target = enda - size; - if (target > end - size) - target = end - size; - grub_dprintf ("relocator", "%x, %x, %x\n", target, start, - events[j].pos); - if (target >= start && target >= events[j].pos) - goto found; - } - if (events[j].type == COLLISION_START) - ncollisions++; - break; - } - } - } + case FIRMWARE_BLOCK_START: + nblockfw++; + break; + + case FIRMWARE_BLOCK_END: + nblockfw--; + break; +#endif + + case COLLISION_START: + ncollisions++; + break; + + case COLLISION_END: + ncollisions--; + break; + + case IN_REG_START: + case REG_BEG_START: + nstarted++; + break; + + case IN_REG_END: + case REG_BEG_END: + nstarted--; + break; + } + isinsideafter = (!ncollisions + && (nstarted || (nstartedfw && !nblockfw))); + if (!isinsidebefore && isinsideafter) + { + starta = from_low_priv ? ALIGN_UP (events[j].pos, align) + : ALIGN_DOWN (events[j].pos - size, align) + size; + numstarted = j; + } + if (isinsidebefore && !isinsideafter && from_low_priv) + { + target = starta; + if (target < start) + target = start; + if (target + size <= end && target + size <= events[j].pos) + /* Found an usable address. */ + goto found; + } + if (isinsidebefore && !isinsideafter && !from_low_priv) + { + target = starta - size; + if (target > end - size) + target = end - size; + if (target >= start && target >= events[j].pos) + goto found; + } + } + } grub_mm_base = base_saved; grub_free (events); @@ -480,9 +492,24 @@ malloc_in_range (struct grub_relocator *rel, found: { + int inreg = 0, regbeg = 0, fwin = 0, fwb = 0, ncol = 0; int last_start = 0; for (j = 0; j < N; j++) { + int typepre; + if (ncol) + typepre = -1; + else if (regbeg) + typepre = CHUNK_TYPE_REGION_START; + else if (inreg) + typepre = CHUNK_TYPE_IN_REGION; +#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS + else if (fwin && !fwb) + typepre = CHUNK_TYPE_FIRMWARE; +#endif + else + typepre = -1; + if (j != 0 && events[j - 1].pos != events[j].pos) { grub_addr_t alloc_start, alloc_end; @@ -490,28 +517,84 @@ malloc_in_range (struct grub_relocator *rel, alloc_end = min (events[j].pos, target + size); if (alloc_end > alloc_start) { - grub_dprintf ("relocator", "%d\n", last_start); - - if (events[last_start].type == REG_BEG_START - || events[last_start].type == IN_REG_START) + switch (typepre) { - if (events[last_start].type == REG_BEG_START && - (grub_addr_t) (events[last_start].reg + 1) > target) - allocate_regstart (alloc_start, alloc_end - alloc_start, - events[last_start].reg, - events[last_start].regancestor, - events[last_start].hancestor); - else - allocate_inreg (alloc_start, alloc_end - alloc_start, - events[last_start].head, - events[last_start].hancestor, - events[last_start].reg); + case CHUNK_TYPE_REGION_START: + allocate_regstart (alloc_start, alloc_end - alloc_start, + events[last_start].reg, + events[last_start].regancestor, + events[last_start].hancestor); + break; + case CHUNK_TYPE_IN_REGION: + allocate_inreg (alloc_start, alloc_end - alloc_start, + events[last_start].head, + events[last_start].hancestor, + events[last_start].reg); + break; +#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS + case CHUNK_TYPE_FIRMWARE: + /* The failure here can be very expensive. */ + if (!grub_relocator_firmware_alloc_region (alloc_start, + alloc_end - alloc_start)) + { + grub_dprintf ("relocator", + "firmware allocation 0x%x-0x%x failed.\n", + alloc_start, alloc_end); + if (from_low_priv) + start = alloc_end; + else + end = alloc_start; + goto retry; + } + break; +#endif } nallocs++; } } - if (is_start (events[j].type)) - last_start = j; + + switch (events[j].type) + { + case REG_BEG_START: + case IN_REG_START: + if (events[j].type == REG_BEG_START && + (grub_addr_t) (events[j].reg + 1) > target) + regbeg++; + else + inreg++; + last_start = j; + break; + + case REG_BEG_END: + case IN_REG_END: + inreg = regbeg = 0; + break; + +#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS + case REG_FIRMWARE_START: + fwin++; + break; + + case REG_FIRMWARE_END: + fwin--; + break; + + case FIRMWARE_BLOCK_START: + fwb++; + break; + + case FIRMWARE_BLOCK_END: + fwb--; + break; +#endif + case COLLISION_START: + ncol++; + break; + case COLLISION_END: + ncol--; + break; + } + } } @@ -538,9 +621,24 @@ malloc_in_range (struct grub_relocator *rel, { int last_start = 0; + int inreg = 0, regbeg = 0, fwin = 0, fwb = 0, ncol = 0; int cural = 0; for (j = 0; j < N; j++) { + int typepre; + if (ncol) + typepre = -1; + else if (regbeg) + typepre = CHUNK_TYPE_REGION_START; + else if (inreg) + typepre = CHUNK_TYPE_IN_REGION; +#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS + else if (fwin && !fwb) + typepre = CHUNK_TYPE_FIRMWARE; +#endif + else + typepre = -1; + if (j != 0 && events[j - 1].pos != events[j].pos) { grub_addr_t alloc_start, alloc_end; @@ -548,23 +646,76 @@ malloc_in_range (struct grub_relocator *rel, alloc_end = min (events[j].pos, target + size); if (alloc_end > alloc_start) { - res->subchunks[cural].start = alloc_start; - res->subchunks[cural].size = alloc_end - alloc_start; - if (res->subchunks[last_start].type == IN_REG_START) - res->subchunks[cural].type = CHUNK_TYPE_IN_REGION; - else if (res->subchunks[last_start].type == REG_BEG_START) + grub_dprintf ("relocator", "subchunk 0x%x-0x%x, %d\n", + alloc_start, alloc_end, typepre); + res->subchunks[cural].type = typepre; + if (typepre == CHUNK_TYPE_REGION_START) { - res->subchunks[cural].type = CHUNK_TYPE_REGION_START; res->subchunks[cural].host_start = (grub_addr_t) events[last_start].reg; } +#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS + if (typepre == CHUNK_TYPE_REGION_START + || typepre == CHUNK_TYPE_FIRMWARE) + { + /* FIXME: react on out of memory. */ + struct grub_relocator_extra_block *ne; + ne = grub_malloc (sizeof (*ne)); + ne->start = alloc_start; + ne->end = alloc_end; + ne->next = extra_blocks; + extra_blocks = ne; + } +#endif cural++; } } - if (is_start (events[j].type)) - last_start = j; + + switch (events[j].type) + { + case REG_BEG_START: + case IN_REG_START: + if (events[j].type == REG_BEG_START && + (grub_addr_t) (events[j].reg + 1) > target) + regbeg++; + else + inreg++; + last_start = j; + break; + + case REG_BEG_END: + case IN_REG_END: + inreg = regbeg = 0; + break; + +#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS + case REG_FIRMWARE_START: + fwin++; + break; + + case REG_FIRMWARE_END: + fwin--; + break; + + case FIRMWARE_BLOCK_START: + fwb++; + break; + + case FIRMWARE_BLOCK_END: + fwb--; + break; +#endif + case COLLISION_START: + ncol++; + break; + case COLLISION_END: + ncol--; + break; + } + } } + res->src = target; res->size = size; grub_dprintf ("relocator", "allocated: %x %x\n", target, size); @@ -801,6 +952,7 @@ grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, return GRUB_ERR_NONE; } +/* FIXME: remove extra blocks. */ void grub_relocator_unload (struct grub_relocator *rel) { @@ -856,6 +1008,10 @@ grub_relocator_unload (struct grub_relocator *rel) grub_free (h + 1); break; } + case CHUNK_TYPE_FIRMWARE: + grub_relocator_firmware_free_region (chunk->subchunks[i].start, + chunk->subchunks[i].size); + break; } next = chunk->next; grub_free (chunk->subchunks); From 1c7a1bab8c86cbcfd8a3d493f741b1066eb5522f Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 3 Apr 2010 19:41:36 +0200 Subject: [PATCH 086/178] Definitively remove allocation from region start if no free header is present at the begining (at most 15 bytes loss) --- lib/relocator.c | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/lib/relocator.c b/lib/relocator.c index 11d2cff6b..b3a7801c5 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -359,17 +359,6 @@ malloc_in_range (struct grub_relocator *rel, p = pa->next; } while (pa != r->first); - /* FIXME */ - if (0)//if (!pre_added) - { - events[N].type = REG_BEG_START; - events[N].pos = (grub_addr_t) r - r->pre_size; - events[N].reg = r; - N++; - events[N].type = REG_BEG_END; - events[N].pos = (grub_addr_t) r; - N++; - } } #if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS From 88ba41253ea607829cd62dfe6cd37ae20c963dd3 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 3 Apr 2010 22:55:57 +0200 Subject: [PATCH 087/178] Fix x86_64-efi compilation. --- conf/x86-efi.rmk | 14 ++------------ lib/i386/relocator.c | 1 - lib/relocator.c | 16 ++++++++-------- 3 files changed, 10 insertions(+), 21 deletions(-) diff --git a/conf/x86-efi.rmk b/conf/x86-efi.rmk index 5cb472168..b97accac6 100644 --- a/conf/x86-efi.rmk +++ b/conf/x86-efi.rmk @@ -16,8 +16,7 @@ grub_install_SOURCES = util/i386/efi/grub-install.in # Modules. pkglib_PROGRAMS = kernel.img -pkglib_MODULES = chain.mod appleldr.mod \ - linux.mod halt.mod \ +pkglib_MODULES = chain.mod appleldr.mod halt.mod \ datetime.mod loadbios.mod \ fixvideo.mod mmap.mod acpi.mod @@ -63,15 +62,6 @@ appleldr_mod_SOURCES = loader/efi/appleloader.c appleldr_mod_CFLAGS = $(COMMON_CFLAGS) appleldr_mod_LDFLAGS = $(COMMON_LDFLAGS) -# For linux.mod. -linux_mod_SOURCES = loader/i386/efi/linux.c -ifeq ($(target_cpu), x86_64) -linux_mod_SOURCES += loader/i386/linux_trampoline.S -endif -linux_mod_CFLAGS = $(COMMON_CFLAGS) -linux_mod_ASFLAGS = $(COMMON_ASFLAGS) -linux_mod_LDFLAGS = $(COMMON_LDFLAGS) - # For halt.mod. halt_mod_SOURCES = commands/halt.c halt_mod_CFLAGS = $(COMMON_CFLAGS) @@ -103,7 +93,7 @@ efi_gop_mod_CFLAGS = $(COMMON_CFLAGS) efi_gop_mod_LDFLAGS = $(COMMON_LDFLAGS) pkglib_MODULES += xnu.mod -xnu_mod_SOURCES = loader/xnu_resume.c loader/i386/xnu.c loader/i386/efi/xnu.c \ +xnu_mod_SOURCES = loader/xnu_resume.c loader/i386/xnu.c \ loader/macho32.c loader/macho64.c loader/macho.c loader/xnu.c xnu_mod_CFLAGS = $(COMMON_CFLAGS) xnu_mod_LDFLAGS = $(COMMON_LDFLAGS) diff --git a/lib/i386/relocator.c b/lib/i386/relocator.c index a4038d75f..4eaa66890 100644 --- a/lib/i386/relocator.c +++ b/lib/i386/relocator.c @@ -253,7 +253,6 @@ grub_relocator64_boot (struct grub_relocator *rel, return err; asm volatile ("cli"); - grub_printf ("%x\n", relst); ((void (*) (void)) relst) (); /* Not reached. */ diff --git a/lib/relocator.c b/lib/relocator.c index b3a7801c5..79a98e851 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -219,14 +219,12 @@ malloc_in_range (struct grub_relocator *rel, grub_addr_t target = 0; grub_dprintf ("relocator", - "trying to allocate in %x-%x aligned %x size %x\n", - start, end, align, size); + "trying to allocate in 0x%lx-0x%lx aligned 0x%lx size 0x%lx\n", + (unsigned long) start, (unsigned long) end, + (unsigned long) align, (unsigned long) size); start = ALIGN_UP (start, align); end = ALIGN_DOWN (end - size, align) + size; - grub_dprintf ("relocator", - "trying to allocate in %x-%x aligned %x size %x\n", - start, end, align, size); if (end < start + size) return 0; @@ -643,8 +641,9 @@ malloc_in_range (struct grub_relocator *rel, alloc_end = min (events[j].pos, target + size); if (alloc_end > alloc_start) { - grub_dprintf ("relocator", "subchunk 0x%x-0x%x, %d\n", - alloc_start, alloc_end, typepre); + grub_dprintf ("relocator", "subchunk 0x%lx-0x%lx, %d\n", + (unsigned long) alloc_start, + (unsigned long) alloc_end, typepre); res->subchunks[cural].type = typepre; if (typepre == CHUNK_TYPE_REGION_START) { @@ -715,7 +714,8 @@ malloc_in_range (struct grub_relocator *rel, res->src = target; res->size = size; - grub_dprintf ("relocator", "allocated: %x %x\n", target, size); + grub_dprintf ("relocator", "allocated: 0x%lx+0x%lx\n", (unsigned long) target, + (unsigned long) size); return 1; } From e16c2c65b9e79559eef968b82f384d9fa4afdcfc Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Mon, 12 Apr 2010 10:32:45 +0200 Subject: [PATCH 088/178] React glacefully to in-middle out-of-memory. Fix few bugs. --- lib/relocator.c | 203 +++++++++++++++++++++++++++++------------------- 1 file changed, 121 insertions(+), 82 deletions(-) diff --git a/lib/relocator.c b/lib/relocator.c index 79a98e851..486b66700 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -22,8 +22,6 @@ #include #include -/* FIXME: check memory map. */ - struct grub_relocator { struct grub_relocator_chunk *chunks; @@ -185,21 +183,81 @@ allocate_inreg (grub_addr_t addr, grub_size_t size, foll->next = hb; hbp->next = foll; if (rb->first == hb) - rb->first = foll; + { + rb->first = foll; + } } } else { if (foll) - foll->next = hb->next; + { + foll->next = hb->next; + } else foll = hb->next; hbp->next = foll; if (rb->first == hb) - rb->first = foll; + { + rb->first = foll; + } if (rb->first == hb) - rb->first = (void *) (rb + 1); + { + rb->first = (void *) (rb + 1); + } + } +} + +/* FIXME: remove extra blocks. */ +static void +free_subchunk (const struct grub_relocator_subchunk *subchu) +{ + switch (subchu->type) + { + case CHUNK_TYPE_REGION_START: + { + grub_mm_region_t r1, r2, *rp; + grub_mm_header_t h; + grub_size_t pre_size; + r1 = (grub_mm_region_t) ALIGN_UP (subchu->start + subchu->size, + GRUB_MM_ALIGN); + r2 = (grub_mm_region_t) ALIGN_UP (subchu->host_start, GRUB_MM_ALIGN); + for (rp = &grub_mm_base; *rp && *rp != r2; rp = &((*rp)->next)); + /* FIXME */ + if (!*rp) + grub_fatal ("Anomaly in region alocations detected. " + "Simultaneous relocators?"); + pre_size = ALIGN_UP (subchu->host_start, GRUB_MM_ALIGN) + - subchu->host_start; + r2->first = r1->first; + r2->next = r1->next; + r2->pre_size = pre_size; + r2->size = r1->size + (r2 - r1) * sizeof (*r2); + *rp = r1; + h = (grub_mm_header_t) (r1 + 1); + h->next = h; + h->magic = GRUB_MM_ALLOC_MAGIC; + h->size = (r2 - r1); + grub_free (h + 1); + break; + } + case CHUNK_TYPE_IN_REGION: + { + grub_mm_header_t h = (grub_mm_header_t) ALIGN_DOWN (subchu->start, + GRUB_MM_ALIGN); + h->size = (subchu->start / GRUB_MM_ALIGN) + - ((subchu->start + subchu->size + GRUB_MM_ALIGN - 1) / GRUB_MM_ALIGN); + h->next = h; + h->magic = GRUB_MM_ALLOC_MAGIC; + grub_free (h + 1); + break; + } +#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS + case CHUNK_TYPE_FIRMWARE: + grub_relocator_firmware_free_region (subchu->start, subchu->size); + break; +#endif } } @@ -516,12 +574,24 @@ malloc_in_range (struct grub_relocator *rel, events[last_start].reg, events[last_start].regancestor, events[last_start].hancestor); + { + unsigned k; + for (k = 0; k < N; k++) + if (events[k].hancestor == events[last_start].head) + events[k].hancestor = events[last_start].hancestor; + } break; case CHUNK_TYPE_IN_REGION: allocate_inreg (alloc_start, alloc_end - alloc_start, events[last_start].head, events[last_start].hancestor, events[last_start].reg); + { + unsigned k; + for (k = 0; k < N; k++) + if (events[k].hancestor == events[last_start].head) + events[k].hancestor = events[last_start].hancestor; + } break; #if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS case CHUNK_TYPE_FIRMWARE: @@ -529,9 +599,6 @@ malloc_in_range (struct grub_relocator *rel, if (!grub_relocator_firmware_alloc_region (alloc_start, alloc_end - alloc_start)) { - grub_dprintf ("relocator", - "firmware allocation 0x%x-0x%x failed.\n", - alloc_start, alloc_end); if (from_low_priv) start = alloc_end; else @@ -559,7 +626,10 @@ malloc_in_range (struct grub_relocator *rel, case REG_BEG_END: case IN_REG_END: - inreg = regbeg = 0; + if (regbeg) + regbeg--; + else + inreg--; break; #if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS @@ -598,7 +668,7 @@ malloc_in_range (struct grub_relocator *rel, do { if (!p) - grub_fatal ("null in the ring %p %p\n", r, p); + grub_fatal ("null in the ring %p\n", r); p = p->next; } while (p != r->first); @@ -607,9 +677,6 @@ malloc_in_range (struct grub_relocator *rel, /* Malloc is available again. */ grub_mm_base = base_saved; - /* FIXME: react on out of memory. */ - res->subchunks = grub_malloc (sizeof (res->subchunks[0]) * nallocs); - res->nsubchunks = nallocs; { int last_start = 0; @@ -617,7 +684,13 @@ malloc_in_range (struct grub_relocator *rel, #if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS int fwin = 0, fwb = 0; #endif - int cural = 0; + unsigned cural = 0; + int oom = 0; + res->subchunks = grub_malloc (sizeof (res->subchunks[0]) * nallocs); + if (!res->subchunks) + oom = 1; + res->nsubchunks = nallocs; + for (j = 0; j < N; j++) { int typepre; @@ -637,6 +710,10 @@ malloc_in_range (struct grub_relocator *rel, if (j != 0 && events[j - 1].pos != events[j].pos) { grub_addr_t alloc_start, alloc_end; + struct grub_relocator_subchunk tofree; + struct grub_relocator_subchunk *curschu = &tofree; + if (!oom) + curschu = &res->subchunks[cural]; alloc_start = max (events[j - 1].pos, target); alloc_end = min (events[j].pos, target + size); if (alloc_end > alloc_start) @@ -644,26 +721,33 @@ malloc_in_range (struct grub_relocator *rel, grub_dprintf ("relocator", "subchunk 0x%lx-0x%lx, %d\n", (unsigned long) alloc_start, (unsigned long) alloc_end, typepre); - res->subchunks[cural].type = typepre; + curschu->type = typepre; if (typepre == CHUNK_TYPE_REGION_START) - { - res->subchunks[cural].host_start - = (grub_addr_t) events[last_start].reg; - } + curschu->host_start = (grub_addr_t) events[last_start].reg; #if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS - if (typepre == CHUNK_TYPE_REGION_START - || typepre == CHUNK_TYPE_FIRMWARE) + if (!oom && (typepre == CHUNK_TYPE_REGION_START + || typepre == CHUNK_TYPE_FIRMWARE)) { - /* FIXME: react on out of memory. */ struct grub_relocator_extra_block *ne; ne = grub_malloc (sizeof (*ne)); - ne->start = alloc_start; - ne->end = alloc_end; - ne->next = extra_blocks; - extra_blocks = ne; + if (!ne) + { + oom = 1; + grub_memcpy (&tofree, curschu, sizeof (tofree)); + } + else + { + ne->start = alloc_start; + ne->end = alloc_end; + ne->next = extra_blocks; + extra_blocks = ne; + } } #endif - cural++; + if (!oom) + cural++; + else + free_subchunk (&tofree); } } @@ -708,7 +792,14 @@ malloc_in_range (struct grub_relocator *rel, ncol--; break; } - + } + if (oom) + { + for (i = 0; i < cural; i++) + free_subchunk (&res->subchunks[i]); + grub_free (res->subchunks); + grub_dprintf ("relocator", "allocation failed with out-of-memory\n"); + return 0; } } @@ -949,7 +1040,6 @@ grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, return GRUB_ERR_NONE; } -/* FIXME: remove extra blocks. */ void grub_relocator_unload (struct grub_relocator *rel) { @@ -960,58 +1050,7 @@ grub_relocator_unload (struct grub_relocator *rel) { unsigned i; for (i = 0; i < chunk->nsubchunks; i++) - switch (chunk->subchunks[i].type) - { - case CHUNK_TYPE_REGION_START: - { - grub_mm_region_t r1, r2, *rp; - grub_mm_header_t h; - grub_size_t pre_size; - r1 = (grub_mm_region_t) ALIGN_UP (chunk->subchunks[i].start - + chunk->subchunks[i].size, - GRUB_MM_ALIGN); - r2 = (grub_mm_region_t) ALIGN_UP (chunk->subchunks[i].host_start, - GRUB_MM_ALIGN); - for (rp = &grub_mm_base; *rp && *rp != r2; rp = &((*rp)->next)); - /* FIXME */ - if (!*rp) - grub_fatal ("Anomaly in region alocations detected. " - "Simultaneous relocators?"); - pre_size = ALIGN_UP (chunk->subchunks[i].host_start, - GRUB_MM_ALIGN) - - chunk->subchunks[i].host_start; - r2->first = r1->first; - r2->next = r1->next; - r2->pre_size = pre_size; - r2->size = r1->size + (r2 - r1) * sizeof (*r2); - *rp = r1; - h = (grub_mm_header_t) (r1 + 1); - h->next = h; - h->magic = GRUB_MM_ALLOC_MAGIC; - h->size = (r2 - r1); - grub_free (h + 1); - break; - } - case CHUNK_TYPE_IN_REGION: - { - grub_mm_header_t h - = (grub_mm_header_t) ALIGN_DOWN (chunk->subchunks[i].start, - GRUB_MM_ALIGN); - h->size = (chunk->subchunks[i].start / GRUB_MM_ALIGN) - - ((chunk->subchunks[i].start + chunk->subchunks[i].size - + GRUB_MM_ALIGN - 1) / GRUB_MM_ALIGN); - h->next = h; - h->magic = GRUB_MM_ALLOC_MAGIC; - grub_free (h + 1); - break; - } -#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS - case CHUNK_TYPE_FIRMWARE: - grub_relocator_firmware_free_region (chunk->subchunks[i].start, - chunk->subchunks[i].size); - break; -#endif - } + free_subchunk (&chunk->subchunks[i]); next = chunk->next; grub_free (chunk->subchunks); grub_free (chunk); From 68eb58e95597f99da4819972a1527865800d70f4 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Mon, 12 Apr 2010 12:40:09 +0200 Subject: [PATCH 089/178] Add a TODO comment --- lib/relocator.c | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/relocator.c b/lib/relocator.c index 486b66700..93a88b889 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -574,6 +574,7 @@ malloc_in_range (struct grub_relocator *rel, events[last_start].reg, events[last_start].regancestor, events[last_start].hancestor); + /* TODO: maintain a reverse lookup tree for hancestor. */ { unsigned k; for (k = 0; k < N; k++) From d371a6313d881c0f18415c4d750fcabaee7a6268 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 13 Apr 2010 21:43:17 +0200 Subject: [PATCH 090/178] Fixed unloading payload --- lib/relocator.c | 104 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 84 insertions(+), 20 deletions(-) diff --git a/lib/relocator.c b/lib/relocator.c index 93a88b889..4f37fb435 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -41,6 +41,7 @@ struct grub_relocator_subchunk grub_addr_t host_start; grub_addr_t start; grub_size_t size; + struct grub_relocator_extra_block *extra; }; struct grub_relocator_chunk @@ -56,6 +57,7 @@ struct grub_relocator_chunk struct grub_relocator_extra_block { struct grub_relocator_extra_block *next; + struct grub_relocator_extra_block **prev; grub_addr_t start; grub_addr_t end; }; @@ -209,7 +211,6 @@ allocate_inreg (grub_addr_t addr, grub_size_t size, } } -/* FIXME: remove extra blocks. */ static void free_subchunk (const struct grub_relocator_subchunk *subchu) { @@ -220,34 +221,89 @@ free_subchunk (const struct grub_relocator_subchunk *subchu) grub_mm_region_t r1, r2, *rp; grub_mm_header_t h; grub_size_t pre_size; - r1 = (grub_mm_region_t) ALIGN_UP (subchu->start + subchu->size, + r1 = (grub_mm_region_t) ALIGN_UP (subchu->host_start, GRUB_MM_ALIGN); + r2 = (grub_mm_region_t) ALIGN_UP (subchu->start + subchu->size, GRUB_MM_ALIGN); - r2 = (grub_mm_region_t) ALIGN_UP (subchu->host_start, GRUB_MM_ALIGN); for (rp = &grub_mm_base; *rp && *rp != r2; rp = &((*rp)->next)); - /* FIXME */ - if (!*rp) - grub_fatal ("Anomaly in region alocations detected. " - "Simultaneous relocators?"); pre_size = ALIGN_UP (subchu->host_start, GRUB_MM_ALIGN) - subchu->host_start; - r2->first = r1->first; - r2->next = r1->next; - r2->pre_size = pre_size; - r2->size = r1->size + (r2 - r1) * sizeof (*r2); - *rp = r1; - h = (grub_mm_header_t) (r1 + 1); - h->next = h; - h->magic = GRUB_MM_ALLOC_MAGIC; - h->size = (r2 - r1); - grub_free (h + 1); + + if (*rp) + { + grub_mm_header_t h2, *hp; + r1->first = r2->first; + r1->next = r2->next; + r1->pre_size = pre_size; + r1->size = r2->size + (r2 - r1) * sizeof (*r2); + *rp = r1; + h = (grub_mm_header_t) (r1 + 1); + h->next = r2->first; + h->magic = GRUB_MM_FREE_MAGIC; + h->size = (r2 - r1 - 1); + for (hp = &r2->first, h2 = *hp; h2->next != r2->first; + hp = &(h2->next), h2 = *hp) + if (h2 == (grub_mm_header_t) (r2 + 1)) + break; + if (h2 == (grub_mm_header_t) (r2 + 1)) + { + h->size = h2->size + (h2 - h); + h->next = h2->next; + *hp = h; + if (hp == &r2->first) + { + for (h2 = r2->first; h2->next != r2->first; h2 = h2->next); + h2->next = h; + } + } + else + { + h2->next = h; + } + } + else + { + r1->pre_size = pre_size; + r1->size = (r2 - r1) * sizeof (*r2); + /* Find where to insert this region. + Put a smaller one before bigger ones, + to prevent fragmentation. */ + for (rp = &grub_mm_base; *rp; rp = &((*rp)->next)) + if ((*rp)->size > r1->size) + break; + r1->next = *rp; + *rp = r1->next; + h = (grub_mm_header_t) (r1 + 1); + r1->first = h; + h->next = h; + h->magic = GRUB_MM_FREE_MAGIC; + h->size = (r2 - r1 - 1); + } + for (r2 = grub_mm_base; r2; r2 = r2->next) + if ((grub_addr_t) r2 + r2->size == (grub_addr_t) r1) + break; + if (r2) + { + grub_mm_header_t hl2, hl, g; + g = (grub_mm_header_t) ((grub_addr_t) r2 + r2->size); + g->size = (grub_mm_header_t) r1 - g; + r2->size += r1->size; + for (hl = r2->first; hl->next != r2->first; hl = hl->next); + for (hl2 = r1->first; hl2->next != r1->first; hl2 = hl2->next); + hl2->next = r2->first; + r2->first = r1->first; + hl->next = r2->first; + *rp = (*rp)->next; + grub_free (g + 1); + } break; } case CHUNK_TYPE_IN_REGION: { grub_mm_header_t h = (grub_mm_header_t) ALIGN_DOWN (subchu->start, GRUB_MM_ALIGN); - h->size = (subchu->start / GRUB_MM_ALIGN) - - ((subchu->start + subchu->size + GRUB_MM_ALIGN - 1) / GRUB_MM_ALIGN); + h->size + = ((subchu->start + subchu->size + GRUB_MM_ALIGN - 1) / GRUB_MM_ALIGN) + - (subchu->start / GRUB_MM_ALIGN); h->next = h; h->magic = GRUB_MM_ALLOC_MAGIC; grub_free (h + 1); @@ -256,9 +312,11 @@ free_subchunk (const struct grub_relocator_subchunk *subchu) #if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS case CHUNK_TYPE_FIRMWARE: grub_relocator_firmware_free_region (subchu->start, subchu->size); + *curschu->extra->prev = curschu->extra->next; + grub_free (curschu->extra); break; #endif - } + } } static int @@ -723,6 +781,8 @@ malloc_in_range (struct grub_relocator *rel, (unsigned long) alloc_start, (unsigned long) alloc_end, typepre); curschu->type = typepre; + curschu->start = alloc_start; + curschu->size = alloc_end - alloc_start; if (typepre == CHUNK_TYPE_REGION_START) curschu->host_start = (grub_addr_t) events[last_start].reg; #if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS @@ -741,7 +801,10 @@ malloc_in_range (struct grub_relocator *rel, ne->start = alloc_start; ne->end = alloc_end; ne->next = extra_blocks; + ne->prev = &extra_blocks; + extra_blocks->prev = &(ne->next); extra_blocks = ne; + curschu->extra = ne; } } #endif @@ -1056,6 +1119,7 @@ grub_relocator_unload (struct grub_relocator *rel) grub_free (chunk->subchunks); grub_free (chunk); } + grub_free (rel); } grub_err_t From b883356cf6cfb07ee36eaf14ea590e2318efc4ae Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 14 Apr 2010 18:46:02 +0200 Subject: [PATCH 091/178] ntldr support. (based on information from nyu but no code from him) --- conf/i386-pc.rmk | 6 ++ loader/i386/pc/ntldr.c | 140 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 loader/i386/pc/ntldr.c diff --git a/conf/i386-pc.rmk b/conf/i386-pc.rmk index 1b6221f4d..c138188ed 100644 --- a/conf/i386-pc.rmk +++ b/conf/i386-pc.rmk @@ -143,6 +143,12 @@ chain_mod_SOURCES = loader/i386/pc/chainloader.c chain_mod_CFLAGS = $(COMMON_CFLAGS) chain_mod_LDFLAGS = $(COMMON_LDFLAGS) +# For ntldr.mod. +pkglib_MODULES += ntldr.mod +ntldr_mod_SOURCES = loader/i386/pc/ntldr.c +ntldr_mod_CFLAGS = $(COMMON_CFLAGS) +ntldr_mod_LDFLAGS = $(COMMON_LDFLAGS) + pkglib_MODULES += linux16.mod linux16_mod_SOURCES = loader/i386/pc/linux.c linux16_mod_CFLAGS = $(COMMON_CFLAGS) diff --git a/loader/i386/pc/ntldr.c b/loader/i386/pc/ntldr.c new file mode 100644 index 000000000..9e461ff49 --- /dev/null +++ b/loader/i386/pc/ntldr.c @@ -0,0 +1,140 @@ +/* chainloader.c - boot another boot loader */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2004,2007,2009,2010 Free Software Foundation, Inc. + * + * GRUB 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. + * + * GRUB 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 GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static grub_dl_t my_mod; +static struct grub_relocator *rel; + +#define GRUB_NTLDR_SEGMENT 0x2000 + +static grub_err_t +grub_ntldr_boot (void) +{ + struct grub_relocator16_state state = { + .cs = GRUB_NTLDR_SEGMENT, + .ip = 0 + }; + grub_video_set_mode ("text", 0, 0); + + return grub_relocator16_boot (rel, state); +} + +static grub_err_t +grub_ntldr_unload (void) +{ + grub_relocator_unload (rel); + rel = NULL; + grub_dl_unref (my_mod); + return GRUB_ERR_NONE; +} + +static grub_err_t +grub_cmd_ntldr (grub_command_t cmd __attribute__ ((unused)), + int argc, char *argv[]) +{ + grub_file_t file = 0; + grub_err_t err; + void *bs, *ntldr; + grub_size_t ntldrsize; + grub_device_t dev; + + if (argc == 0) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "no file specified"); + + grub_dl_ref (my_mod); + + rel = grub_relocator_new (); + if (!rel) + goto fail; + + file = grub_file_open (argv[0]); + if (! file) + goto fail; + + err = grub_relocator_alloc_chunk_addr (rel, &bs, 0x7C00, + GRUB_DISK_SECTOR_SIZE); + if (err) + goto fail; + + dev = grub_device_open (0); + + if (dev && dev->disk) + { + err = grub_disk_read (dev->disk, 0, 0, GRUB_DISK_SECTOR_SIZE, bs); + if (err) + { + grub_device_close (dev); + goto fail; + } + } + + if (dev) + grub_device_close (dev); + + ntldrsize = grub_file_size (file); + err = grub_relocator_alloc_chunk_addr (rel, &ntldr, GRUB_NTLDR_SEGMENT << 4, + ntldrsize); + if (err) + goto fail; + + if (grub_file_read (file, ntldr, ntldrsize) + != (grub_ssize_t) ntldrsize) + goto fail; + + grub_loader_set (grub_ntldr_boot, grub_ntldr_unload, 1); + return GRUB_ERR_NONE; + + fail: + + if (file) + grub_file_close (file); + + grub_ntldr_unload (); + + return grub_errno; +} + +static grub_command_t cmd; + +GRUB_MOD_INIT(ntldr) +{ + cmd = grub_register_command ("ntldr", grub_cmd_ntldr, + 0, N_("Load NTLDR or BootMGR.")); + my_mod = mod; +} + +GRUB_MOD_FINI(ntldr) +{ + grub_unregister_command (cmd); +} From dae84898b268bf733e8a6c2cc31be814ee0e1694 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Thu, 15 Apr 2010 02:11:26 +0200 Subject: [PATCH 092/178] Pass %dl to ntldr. Clear other registers. --- include/grub/i386/relocator.h | 1 + lib/i386/relocator.c | 3 +++ lib/i386/relocator16.S | 5 +++++ loader/i386/pc/ntldr.c | 11 ++++++++++- 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/include/grub/i386/relocator.h b/include/grub/i386/relocator.h index f32413a1b..891235f9b 100644 --- a/include/grub/i386/relocator.h +++ b/include/grub/i386/relocator.h @@ -44,6 +44,7 @@ struct grub_relocator16_state grub_uint16_t ss; grub_uint16_t sp; grub_uint16_t ip; + grub_uint32_t edx; }; struct grub_relocator64_state diff --git a/lib/i386/relocator.c b/lib/i386/relocator.c index 4eaa66890..5985fac7a 100644 --- a/lib/i386/relocator.c +++ b/lib/i386/relocator.c @@ -49,6 +49,7 @@ extern grub_uint16_t grub_relocator16_fs; extern grub_uint16_t grub_relocator16_gs; extern grub_uint16_t grub_relocator16_ss; extern grub_uint16_t grub_relocator16_sp; +extern grub_uint32_t grub_relocator16_edx; extern grub_uint8_t grub_relocator32_start; extern grub_uint8_t grub_relocator32_end; @@ -207,6 +208,8 @@ grub_relocator16_boot (struct grub_relocator *rel, grub_relocator16_ss = state.ss; grub_relocator16_sp = state.sp; + grub_relocator16_edx = state.edx; + grub_memmove (src, &grub_relocator16_start, RELOCATOR_SIZEOF (16)); err = grub_relocator_prepare_relocs (rel, target, &relst, NULL); diff --git a/lib/i386/relocator16.S b/lib/i386/relocator16.S index 7d65e4dbe..510d3a1ed 100644 --- a/lib/i386/relocator16.S +++ b/lib/i386/relocator16.S @@ -151,6 +151,11 @@ VARIABLE(grub_relocator16_ss) VARIABLE(grub_relocator16_sp) .word 0 movw %ax, %ss + + /* movw imm32, %edx. */ + .byte 0x66, 0xba +VARIABLE(grub_relocator16_edx) + .long 0 /* Cleared direction flag is of no problem with any current payload and makes this implementation easier. */ diff --git a/loader/i386/pc/ntldr.c b/loader/i386/pc/ntldr.c index 9e461ff49..1368694fb 100644 --- a/loader/i386/pc/ntldr.c +++ b/loader/i386/pc/ntldr.c @@ -35,6 +35,7 @@ static grub_dl_t my_mod; static struct grub_relocator *rel; +static grub_uint32_t edx = 0xffffffff; #define GRUB_NTLDR_SEGMENT 0x2000 @@ -43,7 +44,14 @@ grub_ntldr_boot (void) { struct grub_relocator16_state state = { .cs = GRUB_NTLDR_SEGMENT, - .ip = 0 + .ip = 0, + .ds = 0, + .es = 0, + .fs = 0, + .gs = 0, + .ss = 0, + .sp = 0x7c00, + .edx = edx }; grub_video_set_mode ("text", 0, 0); @@ -87,6 +95,7 @@ grub_cmd_ntldr (grub_command_t cmd __attribute__ ((unused)), if (err) goto fail; + edx = grub_get_root_biosnumber (); dev = grub_device_open (0); if (dev && dev->disk) From 91b58e6b747a4f3a7533090bf1e2e05ade05d038 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 20 Apr 2010 18:08:26 +0200 Subject: [PATCH 093/178] EFI requests support for newreloc --- conf/i386.rmk | 7 ++ include/grub/efi/efi.h | 8 +- include/grub/relocator_private.h | 8 ++ kern/efi/efi.c | 34 ------ kern/efi/mm.c | 74 +++++++++++++ lib/efi/relocator.c | 104 ++++++++++++++++++ lib/relocator.c | 174 ++++++++++++++++++++++++++----- loader/i386/bsd.c | 20 ++-- loader/i386/linux.c | 12 +-- loader/i386/xnu.c | 12 +-- loader/multiboot.c | 5 +- term/efi/console.c | 27 ++++- 12 files changed, 399 insertions(+), 86 deletions(-) create mode 100644 lib/efi/relocator.c diff --git a/conf/i386.rmk b/conf/i386.rmk index e223694df..e04d73d61 100644 --- a/conf/i386.rmk +++ b/conf/i386.rmk @@ -22,10 +22,17 @@ relocator_mod_SOURCES = lib/relocator.c lib/i386/relocator32.S \ lib/$(target_cpu)/relocator_asm.S lib/i386/relocator.c \ lib/ieee1275/relocator.c else +ifeq ($(platform), efi) +relocator_mod_SOURCES = lib/relocator.c lib/i386/relocator32.S \ + lib/i386/relocator64.S lib/i386/relocator16.S \ + lib/$(target_cpu)/relocator_asm.S lib/i386/relocator.c \ + lib/efi/relocator.c +else relocator_mod_SOURCES = lib/relocator.c lib/i386/relocator32.S \ lib/i386/relocator64.S lib/i386/relocator16.S \ lib/$(target_cpu)/relocator_asm.S lib/i386/relocator.c endif +endif relocator_mod_CFLAGS = $(COMMON_CFLAGS) relocator_mod_ASFLAGS = $(COMMON_ASFLAGS) relocator_mod_LDFLAGS = $(COMMON_LDFLAGS) diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h index 5852a476f..a56b3f56b 100644 --- a/include/grub/efi/efi.h +++ b/include/grub/efi/efi.h @@ -53,8 +53,10 @@ void EXPORT_FUNC(grub_efi_print_device_path) (grub_efi_device_path_t *dp); char *EXPORT_FUNC(grub_efi_get_filename) (grub_efi_device_path_t *dp); grub_efi_device_path_t * EXPORT_FUNC(grub_efi_get_device_path) (grub_efi_handle_t handle); -int EXPORT_FUNC(grub_efi_exit_boot_services) (grub_efi_uintn_t map_key); -int EXPORT_FUNC (grub_efi_finish_boot_services) (void); +grub_err_t EXPORT_FUNC (grub_efi_finish_boot_services) (grub_size_t *outbuf_size, void *outbuf, + grub_efi_uintn_t *map_key, + grub_efi_uintn_t *efi_desc_size, + grub_efi_uint32_t *efi_desc_version); grub_err_t EXPORT_FUNC (grub_efi_set_virtual_address_map) (grub_efi_uintn_t memory_map_size, grub_efi_uintn_t descriptor_size, grub_efi_uint32_t descriptor_version, @@ -70,4 +72,6 @@ void grub_efi_set_prefix (void); extern grub_efi_system_table_t *EXPORT_VAR(grub_efi_system_table); extern grub_efi_handle_t EXPORT_VAR(grub_efi_image_handle); +extern int EXPORT_VAR(grub_efi_is_finished); + #endif /* ! GRUB_EFI_EFI_HEADER */ diff --git a/include/grub/relocator_private.h b/include/grub/relocator_private.h index c526b0b0c..8398defbd 100644 --- a/include/grub/relocator_private.h +++ b/include/grub/relocator_private.h @@ -41,10 +41,18 @@ void grub_cpu_relocator_jumper (void *rels, grub_addr_t addr); #ifdef GRUB_MACHINE_IEEE1275 #define GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS 1 +#define GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT_LOG 0 +#elif defined (GRUB_MACHINE_EFI) +#define GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS 1 +#define GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT_LOG 12 #else #define GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS 0 #endif +#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS +#define GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT (1 << GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT_LOG) +#endif + struct grub_relocator_mmap_event { enum { diff --git a/kern/efi/efi.c b/kern/efi/efi.c index d8b225535..4916a0d18 100644 --- a/kern/efi/efi.c +++ b/kern/efi/efi.c @@ -181,17 +181,6 @@ grub_halt (void) GRUB_EFI_RESET_SHUTDOWN, GRUB_EFI_SUCCESS, 0, NULL); } -int -grub_efi_exit_boot_services (grub_efi_uintn_t map_key) -{ - grub_efi_boot_services_t *b; - grub_efi_status_t status; - - b = grub_efi_system_table->boot_services; - status = efi_call_2 (b->exit_boot_services, grub_efi_image_handle, map_key); - return status == GRUB_EFI_SUCCESS; -} - grub_err_t grub_efi_set_virtual_address_map (grub_efi_uintn_t memory_map_size, grub_efi_uintn_t descriptor_size, @@ -758,26 +747,3 @@ grub_efi_print_device_path (grub_efi_device_path_t *dp) dp = (grub_efi_device_path_t *) ((char *) dp + len); } } - -int -grub_efi_finish_boot_services (void) -{ - grub_efi_uintn_t mmap_size = 0; - grub_efi_uintn_t map_key; - grub_efi_uintn_t desc_size; - grub_efi_uint32_t desc_version; - void *mmap_buf = 0; - - if (grub_efi_get_memory_map (&mmap_size, mmap_buf, &map_key, - &desc_size, &desc_version) < 0) - return 0; - - mmap_buf = grub_malloc (mmap_size); - - if (grub_efi_get_memory_map (&mmap_size, mmap_buf, &map_key, - &desc_size, &desc_version) <= 0) - return 0; - - return grub_efi_exit_boot_services (map_key); -} - diff --git a/kern/efi/mm.c b/kern/efi/mm.c index ceb8fc9ba..4db0e7e92 100644 --- a/kern/efi/mm.c +++ b/kern/efi/mm.c @@ -49,6 +49,12 @@ static struct allocated_page *allocated_pages = 0; #define MIN_HEAP_SIZE 0x100000 #define MAX_HEAP_SIZE (1600 * 0x100000) +static void *finish_mmap_buf = 0; +static grub_efi_uintn_t finish_mmap_size = 0; +static grub_efi_uintn_t finish_key = 0; +static grub_efi_uintn_t finish_desc_size; +static grub_efi_uint32_t finish_desc_version; +int grub_efi_is_finished = 0; /* Allocate pages. Return the pointer to the first of allocated pages. */ void * @@ -140,6 +146,51 @@ grub_efi_free_pages (grub_efi_physical_address_t address, efi_call_2 (b->free_pages, address, pages); } +grub_err_t +grub_efi_finish_boot_services (grub_size_t *outbuf_size, void *outbuf, + grub_efi_uintn_t *map_key, + grub_efi_uintn_t *efi_desc_size, + grub_efi_uint32_t *efi_desc_version) +{ + grub_efi_boot_services_t *b; + grub_efi_status_t status; + + if (grub_efi_get_memory_map (&finish_mmap_size, finish_mmap_buf, &finish_key, + &finish_desc_size, &finish_desc_version) < 0) + return grub_error (GRUB_ERR_IO, "couldn't retrieve memory map"); + + if (outbuf && *outbuf_size < finish_mmap_size) + return grub_error (GRUB_ERR_IO, "memory map buffer is too small"); + + finish_mmap_buf = grub_malloc (finish_mmap_size); + if (!finish_mmap_buf) + return grub_errno; + + if (grub_efi_get_memory_map (&finish_mmap_size, finish_mmap_buf, &finish_key, + &finish_desc_size, &finish_desc_version) <= 0) + return grub_error (GRUB_ERR_IO, "couldn't retrieve memory map"); + + b = grub_efi_system_table->boot_services; + status = efi_call_2 (b->exit_boot_services, grub_efi_image_handle, + finish_key); + if (status != GRUB_EFI_SUCCESS) + return grub_error (GRUB_ERR_IO, "couldn't terminate EFI services"); + + grub_efi_is_finished = 1; + if (outbuf_size) + *outbuf_size = finish_mmap_size; + if (outbuf) + grub_memcpy (outbuf, finish_mmap_buf, finish_mmap_size); + if (map_key) + *map_key = finish_key; + if (efi_desc_size) + *efi_desc_size = finish_desc_size; + if (efi_desc_version) + *efi_desc_version = finish_desc_version; + + return GRUB_ERR_NONE; +} + /* Get the memory map as defined in the EFI spec. Return 1 if successful, return 0 if partial, or return -1 if an error occurs. */ int @@ -154,6 +205,29 @@ grub_efi_get_memory_map (grub_efi_uintn_t *memory_map_size, grub_efi_uintn_t key; grub_efi_uint32_t version; + if (grub_efi_is_finished) + { + int ret = 1; + if (*memory_map_size < finish_mmap_size) + { + grub_memcpy (memory_map, finish_mmap_buf, *memory_map_size); + ret = 0; + } + else + { + grub_memcpy (memory_map, finish_mmap_buf, finish_mmap_size); + ret = 1; + } + *memory_map_size = finish_mmap_size; + if (map_key) + *map_key = finish_key; + if (descriptor_size) + *descriptor_size = finish_desc_size; + if (descriptor_version) + *descriptor_version = finish_desc_version; + return ret; + } + /* Allow some parameters to be missing. */ if (! map_key) map_key = &key; diff --git a/lib/efi/relocator.c b/lib/efi/relocator.c new file mode 100644 index 000000000..fc4de834b --- /dev/null +++ b/lib/efi/relocator.c @@ -0,0 +1,104 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2010 Free Software Foundation, Inc. + * + * GRUB 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. + * + * GRUB 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 GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +#define NEXT_MEMORY_DESCRIPTOR(desc, size) \ + ((grub_efi_memory_descriptor_t *) ((char *) (desc) + (size))) + +unsigned +grub_relocator_firmware_get_max_events (void) +{ + grub_efi_uintn_t mmapsize = 0, descriptor_size = 0; + grub_efi_uint32_t descriptor_version = 0; + grub_efi_uintn_t key; + grub_efi_get_memory_map (&mmapsize, NULL, &key, &descriptor_size, + &descriptor_version); + /* Since grub_relocator_firmware_fill_events uses malloc + we need some reserve. Hence +10. */ + return 2 * (mmapsize / descriptor_size + 10); +} + +unsigned +grub_relocator_firmware_fill_events (struct grub_relocator_mmap_event *events) +{ + grub_efi_uintn_t mmapsize = 0, desc_size = 0; + grub_efi_uint32_t descriptor_version = 0; + grub_efi_memory_descriptor_t *descs = NULL; + grub_efi_uintn_t key; + int counter = 0; + grub_efi_memory_descriptor_t *desc; + + grub_efi_get_memory_map (&mmapsize, NULL, &key, &desc_size, + &descriptor_version); + descs = grub_malloc (mmapsize); + if (!descs) + return 0; + + grub_efi_get_memory_map (&mmapsize, descs, &key, &desc_size, + &descriptor_version); + + for (desc = descs; + (char *) desc < ((char *) descs + mmapsize); + desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size)) + { + if (desc->type != GRUB_EFI_CONVENTIONAL_MEMORY) + continue; + events[counter].type = REG_FIRMWARE_START; + events[counter].pos = desc->physical_start; + counter++; + events[counter].type = REG_FIRMWARE_END; + events[counter].pos = desc->physical_start + (desc->num_pages << 12); + counter++; + } + + return counter; +} + +int +grub_relocator_firmware_alloc_region (grub_addr_t start, grub_size_t size) +{ + grub_efi_boot_services_t *b; + grub_efi_physical_address_t address = start; + grub_efi_status_t status; + + if (grub_efi_is_finished) + return 1; + + b = grub_efi_system_table->boot_services; + status = efi_call_4 (b->allocate_pages, GRUB_EFI_ALLOCATE_ADDRESS, + GRUB_EFI_LOADER_DATA, size >> 12, &address); + return (status == GRUB_EFI_SUCCESS); +} + +void +grub_relocator_firmware_free_region (grub_addr_t start, grub_size_t size) +{ + grub_efi_boot_services_t *b; + + if (grub_efi_is_finished) + return; + + b = grub_efi_system_table->boot_services; + efi_call_2 (b->free_pages, start, size >> 12); +} diff --git a/lib/relocator.c b/lib/relocator.c index 4f37fb435..de4cca626 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -42,6 +42,7 @@ struct grub_relocator_subchunk grub_addr_t start; grub_size_t size; struct grub_relocator_extra_block *extra; + struct grub_relocator_fw_leftover *pre, *post; }; struct grub_relocator_chunk @@ -62,6 +63,15 @@ struct grub_relocator_extra_block grub_addr_t end; }; +struct grub_relocator_fw_leftover +{ + struct grub_relocator_fw_leftover *next; + struct grub_relocator_fw_leftover **prev; + grub_addr_t quantstart; + grub_uint8_t freebytes[GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT / 8]; +}; + +struct grub_relocator_fw_leftover *leftovers; struct grub_relocator_extra_block *extra_blocks; struct grub_relocator * @@ -159,7 +169,6 @@ allocate_regstart (grub_addr_t addr, grub_size_t size, grub_mm_region_t rb, while (h != newreg->first); } } - } static void @@ -211,6 +220,20 @@ allocate_inreg (grub_addr_t addr, grub_size_t size, } } +static void +check_leftover (struct grub_relocator_fw_leftover *lo) +{ + unsigned i; + for (i = 0; i < sizeof (lo->freebytes); i++) + if (lo->freebytes[i] != 0xff) + return; + grub_relocator_firmware_free_region (lo->quantstart, + GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT); + *lo->prev = lo->next; + if (lo->next) + lo->next->prev = lo->prev; +} + static void free_subchunk (const struct grub_relocator_subchunk *subchu) { @@ -311,9 +334,34 @@ free_subchunk (const struct grub_relocator_subchunk *subchu) } #if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS case CHUNK_TYPE_FIRMWARE: - grub_relocator_firmware_free_region (subchu->start, subchu->size); - *curschu->extra->prev = curschu->extra->next; - grub_free (curschu->extra); + { + grub_addr_t fstart, fend; + fstart = ALIGN_UP (subchu->start, + GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT); + fend = ALIGN_DOWN (subchu->start + subchu->size, + GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT); + if (fstart < fend) + grub_relocator_firmware_free_region (fstart, fend - fstart); + if (subchu->pre) + { + int off = subchu->start - fstart + - GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT; + grub_memset (subchu->pre->freebytes + off / 8 + 1, + 0xff, sizeof (subchu->pre->freebytes) - off / 8 - 1); + subchu->pre->freebytes[off / 8] |= ~((1 << (off % 8)) - 1); + check_leftover (subchu->pre); + } + if (subchu->post) + { + int off = subchu->start + subchu->size - fend; + grub_memset (subchu->pre->freebytes, + 0xff, sizeof (subchu->pre->freebytes) - off / 8); + subchu->pre->freebytes[off / 8] |= ((1 << (8 - (off % 8))) - 1); + check_leftover (subchu->post); + } + *subchu->extra->prev = subchu->extra->next; + grub_free (subchu->extra); + } break; #endif } @@ -406,7 +454,7 @@ malloc_in_range (struct grub_relocator *rel, #if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS for (r = grub_mm_base; r; r = r->next) { - grub_dprintf ("relocator", "Blocking at 0x%x-0x%x\n", + grub_dprintf ("relocator", "Blocking at 0x%lx-0x%lx\n", (grub_addr_t) r - r->pre_size, (grub_addr_t) (r + 1) + r->size); events[N].type = FIRMWARE_BLOCK_START; @@ -420,7 +468,7 @@ malloc_in_range (struct grub_relocator *rel, struct grub_relocator_extra_block *cur; for (cur = extra_blocks; cur; cur = cur->next) { - grub_dprintf ("relocator", "Blocking at 0x%x-0x%x\n", + grub_dprintf ("relocator", "Blocking at 0x%lx-0x%lx\n", cur->start, cur->end); events[N].type = FIRMWARE_BLOCK_START; events[N].pos = cur->start; @@ -432,6 +480,10 @@ malloc_in_range (struct grub_relocator *rel, } #endif +#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS + N += grub_relocator_firmware_fill_events (events + N); +#endif + /* No malloc from this point. */ base_saved = grub_mm_base; grub_mm_base = NULL; @@ -475,10 +527,6 @@ malloc_in_range (struct grub_relocator *rel, while (pa != r->first); } -#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS - N += grub_relocator_firmware_fill_events (events + N); -#endif - /* Put ending events after starting events. */ { int st = 0, e = N / 2; @@ -654,17 +702,26 @@ malloc_in_range (struct grub_relocator *rel, break; #if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS case CHUNK_TYPE_FIRMWARE: - /* The failure here can be very expensive. */ - if (!grub_relocator_firmware_alloc_region (alloc_start, - alloc_end - alloc_start)) - { - if (from_low_priv) - start = alloc_end; - else - end = alloc_start; - goto retry; - } - break; + { + grub_addr_t fstart, fend; + fstart + = ALIGN_DOWN (alloc_start, + GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT); + fend + = ALIGN_UP (alloc_end, + GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT); + /* The failure here can be very expensive. */ + if (!grub_relocator_firmware_alloc_region (fstart, + fend - fstart)) + { + if (from_low_priv) + start = fend; + else + end = fstart; + goto retry; + } + break; + } #endif } nallocs++; @@ -736,7 +793,6 @@ malloc_in_range (struct grub_relocator *rel, /* Malloc is available again. */ grub_mm_base = base_saved; - { int last_start = 0; int inreg = 0, regbeg = 0, ncol = 0; @@ -784,8 +840,6 @@ malloc_in_range (struct grub_relocator *rel, curschu->start = alloc_start; curschu->size = alloc_end - alloc_start; if (typepre == CHUNK_TYPE_REGION_START) - curschu->host_start = (grub_addr_t) events[last_start].reg; -#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS if (!oom && (typepre == CHUNK_TYPE_REGION_START || typepre == CHUNK_TYPE_FIRMWARE)) { @@ -807,6 +861,76 @@ malloc_in_range (struct grub_relocator *rel, curschu->extra = ne; } } +#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS + if (!oom && typepre == CHUNK_TYPE_FIRMWARE) + { + grub_addr_t fstart, fend; + struct grub_relocator_fw_leftover *lo1 = NULL; + struct grub_relocator_fw_leftover *lo2 = NULL; + + fstart + = ALIGN_DOWN (alloc_start, + GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT); + fend + = ALIGN_UP (alloc_end, + GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT); + + if (fstart != alloc_start) + lo1 = grub_malloc (sizeof (*lo1)); + if (fend != alloc_end) + lo2 = grub_malloc (sizeof (*lo2)); + if ((!lo1 && fstart != alloc_start) + || (!lo2 && fend != alloc_end)) + { + struct grub_relocator_extra_block *ne; + grub_free (lo1); + grub_free (lo2); + lo1 = NULL; + lo2 = NULL; + oom = 1; + grub_memcpy (&tofree, curschu, sizeof (tofree)); + ne = extra_blocks; + extra_blocks = extra_blocks->next; + grub_free (ne); + } + if (lo1) + { + lo1->quantstart = fstart; + grub_memset (lo1->freebytes, 0xff, + (alloc_start - fstart) / 8); + lo1->freebytes[(alloc_start - fstart) / 8] + = (1 << ((alloc_start - fstart) % 8)) - 1; + grub_memset (lo1->freebytes + + ((alloc_start - fstart) / 8) + 1, 0, + sizeof (lo1->freebytes) + - (alloc_start - fstart) / 8 - 1); + lo1->next = leftovers; + lo1->prev = &leftovers; + if (leftovers) + leftovers->prev = &lo1->next; + leftovers = lo1; + } + if (lo2) + { + lo2->quantstart + = fend - GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT; + grub_memset (lo2->freebytes, 0, + (alloc_end - lo2->quantstart) / 8); + lo2->freebytes[(alloc_end - lo2->quantstart) / 8] + = ~((1 << ((alloc_end - lo2->quantstart) % 8)) - 1); + grub_memset (lo2->freebytes + + ((alloc_end - lo2->quantstart) / 8) + + 1, 0, sizeof (lo2->freebytes) + - (alloc_end - lo2->quantstart) / 8 - 1); + lo2->prev = &leftovers; + if (leftovers) + leftovers->prev = &lo2->next; + lo2->next = leftovers; + leftovers = lo2; + } + curschu->pre = lo1; + curschu->post = lo2; + } #endif if (!oom) cural++; @@ -814,7 +938,7 @@ malloc_in_range (struct grub_relocator *rel, free_subchunk (&tofree); } } - + switch (events[j].type) { case REG_BEG_START: diff --git a/loader/i386/bsd.c b/loader/i386/bsd.c index f50e94f98..cfd10713d 100644 --- a/loader/i386/bsd.c +++ b/loader/i386/bsd.c @@ -692,8 +692,9 @@ grub_freebsd_boot (void) return err; #ifdef GRUB_MACHINE_EFI - if (! grub_efi_finish_boot_services ()) - grub_fatal ("cannot exit boot services"); + err = grub_efi_finish_boot_services (NULL, NULL, NULL, NULL, NULL); + if (err) + return err; #endif pagetable = p; @@ -723,8 +724,9 @@ grub_freebsd_boot (void) return err; #ifdef GRUB_MACHINE_EFI - if (! grub_efi_finish_boot_services ()) - grub_fatal ("cannot exit boot services"); + err = grub_efi_finish_boot_services (NULL, NULL, NULL, NULL, NULL); + if (err) + return err; #endif grub_memcpy (&stack[9], &bi, sizeof (bi)); @@ -804,8 +806,9 @@ grub_openbsd_boot (void) grub_video_set_mode ("text", 0, 0); #ifdef GRUB_MACHINE_EFI - if (! grub_efi_finish_boot_services ()) - grub_fatal ("cannot exit boot services"); + err = grub_efi_finish_boot_services (NULL, NULL, NULL, NULL, NULL); + if (err) + return err; #endif state.eip = entry; @@ -1009,8 +1012,9 @@ grub_netbsd_boot (void) return err; #ifdef GRUB_MACHINE_EFI - if (! grub_efi_finish_boot_services ()) - grub_fatal ("cannot exit boot services"); + err = grub_efi_finish_boot_services (NULL, NULL, NULL, NULL, NULL); + if (err) + return err; #endif state.eip = entry; diff --git a/loader/i386/linux.c b/loader/i386/linux.c index 4a1068070..ef1b8309e 100644 --- a/loader/i386/linux.c +++ b/loader/i386/linux.c @@ -681,15 +681,13 @@ grub_linux_boot (void) #ifdef GRUB_MACHINE_EFI { - grub_efi_uintn_t efi_map_key, efi_desc_size; + grub_efi_uintn_t efi_desc_size; grub_efi_uint32_t efi_desc_version; - if (grub_efi_get_memory_map (&efi_mmap_size, efi_mmap_buf, &efi_map_key, - &efi_desc_size, &efi_desc_version) <= 0) - grub_fatal ("cannot get memory map"); + err = grub_efi_finish_boot_services (&efi_mmap_size, efi_mmap_buf, NULL, + &efi_desc_size, &efi_desc_version); + if (err) + return err; - if (! grub_efi_exit_boot_services (efi_map_key)) - grub_fatal ("cannot exit boot services"); - /* Note that no boot services are available from here. */ /* Pass EFI parameters. */ diff --git a/loader/i386/xnu.c b/loader/i386/xnu.c index bdcd383c5..dcec3554f 100644 --- a/loader/i386/xnu.c +++ b/loader/i386/xnu.c @@ -1038,10 +1038,11 @@ grub_xnu_boot (void) bootparams->devtree = devtree_target; bootparams->devtreelen = devtreelen; - if (grub_autoefi_get_memory_map (&memory_map_size, memory_map, - &map_key, &descriptor_size, - &descriptor_version) <= 0) - return grub_errno; + err = grub_efi_finish_boot_services (&memory_map_size, memory_map, + &map_key, &descriptor_size, + &descriptor_version); + if (err) + return err; bootparams->efi_system_table = PTR_TO_UINT32 (grub_autoefi_system_table); @@ -1096,9 +1097,6 @@ grub_xnu_boot (void) + bootparams->heap_size + GRUB_XNU_PAGESIZE; grub_xnu_arg1 = bootparams_target; - if (! grub_autoefi_exit_boot_services (map_key)) - return grub_error (GRUB_ERR_IO, "can't exit boot services"); - grub_autoefi_set_virtual_address_map (memory_map_size, descriptor_size, descriptor_version,memory_map); diff --git a/loader/multiboot.c b/loader/multiboot.c index 53dc42abb..a3ca6266f 100644 --- a/loader/multiboot.c +++ b/loader/multiboot.c @@ -126,8 +126,9 @@ grub_multiboot_boot (void) return err; #ifdef GRUB_MACHINE_EFI - if (! grub_efi_finish_boot_services ()) - grub_fatal ("cannot exit boot services"); + err = grub_efi_finish_boot_services (NULL, NULL, NULL, NULL, NULL); + if (err) + return err; #endif grub_relocator32_boot (grub_multiboot_relocator, state); diff --git a/term/efi/console.c b/term/efi/console.c index 664861398..eac227561 100644 --- a/term/efi/console.c +++ b/term/efi/console.c @@ -90,6 +90,9 @@ grub_console_putchar (grub_uint32_t c) grub_efi_char16_t str[2]; grub_efi_simple_text_output_interface_t *o; + if (grub_efi_is_finished) + return; + o = grub_efi_system_table->con_out; /* For now, do not try to use a surrogate pair. */ @@ -120,6 +123,9 @@ grub_console_checkkey (void) grub_efi_input_key_t key; grub_efi_status_t status; + if (grub_efi_is_finished) + return 0; + if (read_key >= 0) return 1; @@ -217,6 +223,9 @@ grub_console_getkey (void) grub_efi_status_t status; int key; + if (grub_efi_is_finished) + return 0; + if (read_key >= 0) { key = read_key; @@ -249,7 +258,8 @@ grub_console_getwh (void) grub_efi_uintn_t columns, rows; o = grub_efi_system_table->con_out; - if (efi_call_4 (o->query_mode, o, o->mode->mode, &columns, &rows) != GRUB_EFI_SUCCESS) + if (grub_efi_is_finished || efi_call_4 (o->query_mode, o, o->mode->mode, + &columns, &rows) != GRUB_EFI_SUCCESS) { /* Why does this fail? */ columns = 80; @@ -264,6 +274,9 @@ grub_console_getxy (void) { grub_efi_simple_text_output_interface_t *o; + if (grub_efi_is_finished) + return 0; + o = grub_efi_system_table->con_out; return ((o->mode->cursor_column << 8) | o->mode->cursor_row); } @@ -273,6 +286,9 @@ grub_console_gotoxy (grub_uint8_t x, grub_uint8_t y) { grub_efi_simple_text_output_interface_t *o; + if (grub_efi_is_finished) + return; + o = grub_efi_system_table->con_out; efi_call_3 (o->set_cursor_position, o, x, y); } @@ -283,6 +299,9 @@ grub_console_cls (void) grub_efi_simple_text_output_interface_t *o; grub_efi_int32_t orig_attr; + if (grub_efi_is_finished) + return; + o = grub_efi_system_table->con_out; orig_attr = o->mode->attribute; efi_call_2 (o->set_attributes, o, GRUB_EFI_BACKGROUND_BLACK); @@ -295,6 +314,9 @@ grub_console_setcolorstate (grub_term_color_state state) { grub_efi_simple_text_output_interface_t *o; + if (grub_efi_is_finished) + return; + o = grub_efi_system_table->con_out; switch (state) { @@ -331,6 +353,9 @@ grub_console_setcursor (int on) { grub_efi_simple_text_output_interface_t *o; + if (grub_efi_is_finished) + return; + o = grub_efi_system_table->con_out; efi_call_2 (o->enable_cursor, o, on); } From 6d6f55c557871efa852e53c3f5a34f36241ed284 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 21 Apr 2010 09:27:57 +0200 Subject: [PATCH 094/178] Use leftovers --- include/grub/relocator_private.h | 7 +- lib/relocator.c | 281 +++++++++++++++++++++---------- util/grub-mkrescue.in | 45 ++++- 3 files changed, 237 insertions(+), 96 deletions(-) diff --git a/include/grub/relocator_private.h b/include/grub/relocator_private.h index 8398defbd..a859ec9fd 100644 --- a/include/grub/relocator_private.h +++ b/include/grub/relocator_private.h @@ -66,8 +66,10 @@ struct grub_relocator_mmap_event /* To track the regions already in heap. */ FIRMWARE_BLOCK_START = 6, FIRMWARE_BLOCK_END = FIRMWARE_BLOCK_START | 1, + REG_LEFTOVER_START = 8, + REG_LEFTOVER_END = REG_LEFTOVER_START | 1, #endif - COLLISION_START = 8, + COLLISION_START = 10, COLLISION_END = COLLISION_START | 1 } type; grub_addr_t pos; @@ -80,6 +82,9 @@ struct grub_relocator_mmap_event grub_mm_region_t *regancestor; grub_mm_header_t head; }; +#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS + struct grub_relocator_fw_leftover *leftover; +#endif }; }; diff --git a/lib/relocator.c b/lib/relocator.c index de4cca626..1fe5a94e5 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -35,7 +35,7 @@ struct grub_relocator_subchunk { enum {CHUNK_TYPE_IN_REGION, CHUNK_TYPE_REGION_START, #if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS - CHUNK_TYPE_FIRMWARE + CHUNK_TYPE_FIRMWARE, CHUNK_TYPE_LEFTOVER #endif } type; grub_addr_t host_start; @@ -334,6 +334,7 @@ free_subchunk (const struct grub_relocator_subchunk *subchu) } #if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS case CHUNK_TYPE_FIRMWARE: + case CHUNK_TYPE_LEFTOVER: { grub_addr_t fstart, fend; fstart = ALIGN_UP (subchu->start, @@ -375,11 +376,12 @@ malloc_in_range (struct grub_relocator *rel, { grub_mm_region_t r, *ra, base_saved; struct grub_relocator_mmap_event *events = NULL, *eventt = NULL, *t; - unsigned maxevents = 2; + /* 128 is just in case of additional malloc (shouldn't happen). */ + unsigned maxevents = 2 + 128; grub_mm_header_t p, pa; unsigned *counter; int nallocs = 0; - unsigned i, j, N = 0; + unsigned j, N = 0; grub_addr_t target = 0; grub_dprintf ("relocator", @@ -422,6 +424,23 @@ malloc_in_range (struct grub_relocator *rel, } maxevents += grub_relocator_firmware_get_max_events (); + + { + struct grub_relocator_fw_leftover *cur; + for (cur = leftovers; cur; cur = cur->next) + { + int l = 0; + unsigned i; + for (i = 0; i < GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT; i++) + { + if (l != ((cur->freebytes[i / 8] >> (i % 8)) & 1)) + maxevents++; + l = ((cur->freebytes[i / 8] >> (i % 8)) & 1); + } + if (l) + maxevents++; + } + } #endif events = grub_malloc (maxevents * sizeof (events[0])); @@ -478,10 +497,35 @@ malloc_in_range (struct grub_relocator *rel, N++; } } -#endif -#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS N += grub_relocator_firmware_fill_events (events + N); + + { + struct grub_relocator_fw_leftover *cur; + for (cur = leftovers; cur; cur = cur->next) + { + unsigned i; + int l = 0; + for (i = 0; i < GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT; i++) + { + if (l != ((cur->freebytes[i / 8] >> (i % 8)) & 1)) + { + events[N].type = l ? REG_LEFTOVER_END : REG_LEFTOVER_START; + events[N].pos = cur->quantstart + i; + events[N].leftover = cur; + N++; + } + l = ((cur->freebytes[i / 8] >> (i % 8)) & 1); + } + if (l) + { + events[N].type = REG_LEFTOVER_END; + events[N].pos = cur->quantstart + i; + events[N].leftover = cur; + N++; + } + } + } #endif /* No malloc from this point. */ @@ -539,22 +583,25 @@ malloc_in_range (struct grub_relocator *rel, eventt = events; events = t; } - for (i = 0; i < (BITS_IN_BYTE * sizeof (grub_addr_t) / DIGITSORT_BITS); - i++) - { - memset (counter, 0, (1 + (1 << DIGITSORT_BITS)) * sizeof (counter[0])); - for (j = 0; j < N; j++) - counter[((events[j].pos >> (DIGITSORT_BITS * i)) - & DIGITSORT_MASK) + 1]++; - for (j = 0; j <= DIGITSORT_MASK; j++) - counter[j+1] += counter[j]; - for (j = 0; j < N; j++) - eventt[counter[((events[j].pos >> (DIGITSORT_BITS * i)) - & DIGITSORT_MASK)]++] = events[j]; - t = eventt; - eventt = events; - events = t; - } + { + unsigned i; + for (i = 0; i < (BITS_IN_BYTE * sizeof (grub_addr_t) / DIGITSORT_BITS); + i++) + { + memset (counter, 0, (1 + (1 << DIGITSORT_BITS)) * sizeof (counter[0])); + for (j = 0; j < N; j++) + counter[((events[j].pos >> (DIGITSORT_BITS * i)) + & DIGITSORT_MASK) + 1]++; + for (j = 0; j <= DIGITSORT_MASK; j++) + counter[j+1] += counter[j]; + for (j = 0; j < N; j++) + eventt[counter[((events[j].pos >> (DIGITSORT_BITS * i)) + & DIGITSORT_MASK)]++] = events[j]; + t = eventt; + eventt = events; + events = t; + } + } #if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS retry: @@ -563,14 +610,15 @@ malloc_in_range (struct grub_relocator *rel, /* Now events are nicely sorted. */ { int nstarted = 0, ncollisions = 0, nstartedfw = 0, nblockfw = 0; + int nlefto = 0; grub_addr_t starta = 0; int numstarted; for (j = from_low_priv ? 0 : N - 1; from_low_priv ? j < N : (j + 1); from_low_priv ? j++ : j--) { int isinsidebefore, isinsideafter; - isinsidebefore = (!ncollisions - && (nstarted || (nstartedfw && !nblockfw))); + isinsidebefore = (!ncollisions && (nstarted || (((nlefto || nstartedfw) + && !nblockfw)))); switch (events[j].type) { #if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS @@ -589,6 +637,13 @@ malloc_in_range (struct grub_relocator *rel, case FIRMWARE_BLOCK_END: nblockfw--; break; + case REG_LEFTOVER_START: + nlefto++; + break; + + case REG_LEFTOVER_END: + nlefto--; + break; #endif case COLLISION_START: @@ -609,8 +664,8 @@ malloc_in_range (struct grub_relocator *rel, nstarted--; break; } - isinsideafter = (!ncollisions - && (nstarted || (nstartedfw && !nblockfw))); + isinsideafter = (!ncollisions && (nstarted || ((nlefto || nstartedfw) + && !nblockfw))); if (!isinsidebefore && isinsideafter) { starta = from_low_priv ? ALIGN_UP (events[j].pos, align) @@ -647,7 +702,7 @@ malloc_in_range (struct grub_relocator *rel, { int inreg = 0, regbeg = 0, ncol = 0; #if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS - int fwin = 0, fwb = 0; + int fwin = 0, fwb = 0, fwlefto = 0; #endif int last_start = 0; for (j = 0; j < N; j++) @@ -662,6 +717,8 @@ malloc_in_range (struct grub_relocator *rel, #if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS else if (fwin && !fwb) typepre = CHUNK_TYPE_FIRMWARE; + else if (fwlefto && !fwb) + typepre = CHUNK_TYPE_LEFTOVER; #endif else typepre = -1; @@ -722,6 +779,21 @@ malloc_in_range (struct grub_relocator *rel, } break; } + case CHUNK_TYPE_LEFTOVER: + { + unsigned offstart = alloc_start + % GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT; + unsigned offend = alloc_end + % GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT; + struct grub_relocator_fw_leftover *lo + = events[last_start].leftover; + lo->freebytes[offstart / 8] + &= ((1 << (8 - (start % 8))) - 1); + grub_memset (lo->freebytes + (offstart + 7) / 8, 0, + offend / 8 - (offstart + 7) / 8); + lo->freebytes[offend / 8] &= ~((1 << (offend % 8)) - 1); + } + break; #endif } nallocs++; @@ -757,6 +829,14 @@ malloc_in_range (struct grub_relocator *rel, fwin--; break; + case REG_LEFTOVER_START: + fwlefto++; + break; + + case REG_LEFTOVER_END: + fwlefto--; + break; + case FIRMWARE_BLOCK_START: fwb++; break; @@ -797,7 +877,7 @@ malloc_in_range (struct grub_relocator *rel, int last_start = 0; int inreg = 0, regbeg = 0, ncol = 0; #if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS - int fwin = 0, fwb = 0; + int fwin = 0, fwlefto = 0, fwb = 0; #endif unsigned cural = 0; int oom = 0; @@ -818,6 +898,8 @@ malloc_in_range (struct grub_relocator *rel, #if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS else if (fwin && !fwb) typepre = CHUNK_TYPE_FIRMWARE; + else if (fwlefto && !fwb) + typepre = CHUNK_TYPE_LEFTOVER; #endif else typepre = -1; @@ -863,74 +945,80 @@ malloc_in_range (struct grub_relocator *rel, } #if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS if (!oom && typepre == CHUNK_TYPE_FIRMWARE) - { - grub_addr_t fstart, fend; - struct grub_relocator_fw_leftover *lo1 = NULL; - struct grub_relocator_fw_leftover *lo2 = NULL; + { + grub_addr_t fstart, fend; + struct grub_relocator_fw_leftover *lo1 = NULL; + struct grub_relocator_fw_leftover *lo2 = NULL; - fstart - = ALIGN_DOWN (alloc_start, - GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT); - fend - = ALIGN_UP (alloc_end, + fstart + = ALIGN_DOWN (alloc_start, GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT); + fend + = ALIGN_UP (alloc_end, + GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT); - if (fstart != alloc_start) - lo1 = grub_malloc (sizeof (*lo1)); - if (fend != alloc_end) - lo2 = grub_malloc (sizeof (*lo2)); - if ((!lo1 && fstart != alloc_start) - || (!lo2 && fend != alloc_end)) - { - struct grub_relocator_extra_block *ne; - grub_free (lo1); - grub_free (lo2); - lo1 = NULL; - lo2 = NULL; - oom = 1; - grub_memcpy (&tofree, curschu, sizeof (tofree)); - ne = extra_blocks; - extra_blocks = extra_blocks->next; - grub_free (ne); - } - if (lo1) - { - lo1->quantstart = fstart; - grub_memset (lo1->freebytes, 0xff, - (alloc_start - fstart) / 8); - lo1->freebytes[(alloc_start - fstart) / 8] - = (1 << ((alloc_start - fstart) % 8)) - 1; - grub_memset (lo1->freebytes - + ((alloc_start - fstart) / 8) + 1, 0, - sizeof (lo1->freebytes) - - (alloc_start - fstart) / 8 - 1); - lo1->next = leftovers; - lo1->prev = &leftovers; - if (leftovers) - leftovers->prev = &lo1->next; - leftovers = lo1; - } - if (lo2) - { - lo2->quantstart - = fend - GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT; - grub_memset (lo2->freebytes, 0, - (alloc_end - lo2->quantstart) / 8); - lo2->freebytes[(alloc_end - lo2->quantstart) / 8] - = ~((1 << ((alloc_end - lo2->quantstart) % 8)) - 1); - grub_memset (lo2->freebytes - + ((alloc_end - lo2->quantstart) / 8) - + 1, 0, sizeof (lo2->freebytes) - - (alloc_end - lo2->quantstart) / 8 - 1); - lo2->prev = &leftovers; - if (leftovers) - leftovers->prev = &lo2->next; - lo2->next = leftovers; - leftovers = lo2; - } - curschu->pre = lo1; - curschu->post = lo2; - } + if (fstart != alloc_start) + lo1 = grub_malloc (sizeof (*lo1)); + if (fend != alloc_end) + lo2 = grub_malloc (sizeof (*lo2)); + if ((!lo1 && fstart != alloc_start) + || (!lo2 && fend != alloc_end)) + { + struct grub_relocator_extra_block *ne; + grub_free (lo1); + grub_free (lo2); + lo1 = NULL; + lo2 = NULL; + oom = 1; + grub_memcpy (&tofree, curschu, sizeof (tofree)); + ne = extra_blocks; + extra_blocks = extra_blocks->next; + grub_free (ne); + } + if (lo1) + { + lo1->quantstart = fstart; + grub_memset (lo1->freebytes, 0xff, + (alloc_start - fstart) / 8); + lo1->freebytes[(alloc_start - fstart) / 8] + = (1 << ((alloc_start - fstart) % 8)) - 1; + grub_memset (lo1->freebytes + + ((alloc_start - fstart) / 8) + 1, 0, + sizeof (lo1->freebytes) + - (alloc_start - fstart) / 8 - 1); + lo1->next = leftovers; + lo1->prev = &leftovers; + if (leftovers) + leftovers->prev = &lo1->next; + leftovers = lo1; + } + if (lo2) + { + lo2->quantstart + = fend - GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT; + grub_memset (lo2->freebytes, 0, + (alloc_end - lo2->quantstart) / 8); + lo2->freebytes[(alloc_end - lo2->quantstart) / 8] + = ~((1 << ((alloc_end - lo2->quantstart) % 8)) - 1); + grub_memset (lo2->freebytes + + ((alloc_end - lo2->quantstart) / 8) + + 1, 0, sizeof (lo2->freebytes) + - (alloc_end - lo2->quantstart) / 8 - 1); + lo2->prev = &leftovers; + if (leftovers) + leftovers->prev = &lo2->next; + lo2->next = leftovers; + leftovers = lo2; + } + curschu->pre = lo1; + curschu->post = lo2; + } + + if (typepre == CHUNK_TYPE_LEFTOVER) + { + curschu->pre = events[last_start].leftover; + curschu->post = events[last_start].leftover; + } #endif if (!oom) cural++; @@ -965,6 +1053,14 @@ malloc_in_range (struct grub_relocator *rel, fwin--; break; + case REG_LEFTOVER_START: + fwlefto++; + break; + + case REG_LEFTOVER_END: + fwlefto--; + break; + case FIRMWARE_BLOCK_START: fwb++; break; @@ -983,6 +1079,7 @@ malloc_in_range (struct grub_relocator *rel, } if (oom) { + unsigned i; for (i = 0; i < cural; i++) free_subchunk (&res->subchunks[i]); grub_free (res->subchunks); diff --git a/util/grub-mkrescue.in b/util/grub-mkrescue.in index aafdfb059..e7dbb45ff 100644 --- a/util/grub-mkrescue.in +++ b/util/grub-mkrescue.in @@ -32,6 +32,8 @@ pkglib_DATA="@pkglib_DATA@" coreboot_dir=${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/${target_cpu}-coreboot pc_dir=${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/${target_cpu}-pc +efi32_dir=${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/i386-efi +efi64_dir=${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/x86_64-efi # Usage: usage # Print the usage. @@ -128,13 +130,23 @@ if [ "${override_dir}" = "" ] ; then if test -e "${pc_dir}" ; then process_input_dir ${pc_dir} pc fi + if test -e "${efi32_dir}" ; then + process_input_dir ${efi32_dir} efi32 + fi + if test -e "${efi64_dir}" ; then + process_input_dir ${efi64_dir} efi64 + fi else process_input_dir ${override_dir} ${native_platform} coreboot_dir= pc_dir= - case "${native_platform}" in - coreboot) coreboot_dir=${override_dir} ;; - pc) pc_dir=${override_dir} ;; + efi32_dir= + efi64_dir= + case "${target_cpu}-${native_platform}" in + i386-coreboot) coreboot_dir=${override_dir} ;; + i386-pc) pc_dir=${override_dir} ;; + i386-efi) efi32_dir=${override_dir} ;; + x86_64-efi) efi64_dir=${override_dir} ;; esac fi @@ -191,6 +203,33 @@ if test -e "${pc_dir}" ; then --embedded-boot ${embed_img}" fi +if test -e "${efi64_dir}" || test -e "${efi32_dir}"; then + efi_dir=`mktemp -d "$MKTEMP_TEMPLATE"` + mkdir -p "${efi_dir}/efi/boot" +else + efi_dir= +fi + +# build bootx64.efi +if test -e "${efi64_dir}" ; then + echo "Generates bootx64.efi" + grub-mkimage -d "${efi64_dir}" -o "${efi_dir}"/efi/boot/bootx64.efi --prefix=/boot/grub/x86_64-efi \ + search iso9660 configfile sh + + modules="$(cat "${efi64_dir}"/partmap.lst) ${modules}" + (for i in ${modules} ; do + echo "insmod $i" + done ; \ + echo "source /boot/grub/grub.cfg") \ + > "${iso9660_dir}"/boot/grub/x86_64-efi/grub.cfg +fi + +if test x"${efi_dir}" != x; then + mformat -C -f 2880 -L 16 -i "${iso9660_dir}"/efi.img :: + mcopy -s -i "${iso9660_dir}"/efi.img ${efi_dir}/efi ::/ + grub_mkisofs_arguments="${grub_mkisofs_arguments} --efi-boot efi.img" +fi + # build iso image grub-mkisofs ${grub_mkisofs_arguments} --protective-msdos-label -o ${output_image} -r ${iso9660_dir} ${source} rm -rf ${iso9660_dir} From 5490ddc18bdcf27c1eeb88ffd90aab2708a2c016 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 21 Apr 2010 10:01:41 +0200 Subject: [PATCH 095/178] Fix compilation on i386-pc --- efiemu/main.c | 16 ---------------- include/grub/autoefi.h | 2 ++ include/grub/efiemu/efiemu.h | 3 +-- loader/i386/xnu.c | 6 +++--- 4 files changed, 6 insertions(+), 21 deletions(-) diff --git a/efiemu/main.c b/efiemu/main.c index 8a8a508fa..7ebbae946 100644 --- a/efiemu/main.c +++ b/efiemu/main.c @@ -182,22 +182,6 @@ grub_cmd_efiemu_prepare (grub_command_t cmd __attribute__ ((unused)), - -int -grub_efiemu_exit_boot_services (grub_efi_uintn_t map_key - __attribute__ ((unused))) -{ - /* Nothing to do here yet */ - return 1; -} - -int -grub_efiemu_finish_boot_services (void) -{ - /* Nothing to do here yet */ - return 1; -} - /* Load the runtime from the file FILENAME. */ static grub_err_t grub_efiemu_load_file (const char *filename) diff --git a/include/grub/autoefi.h b/include/grub/autoefi.h index 5ae4b3a21..740be3249 100644 --- a/include/grub/autoefi.h +++ b/include/grub/autoefi.h @@ -50,6 +50,7 @@ static inline grub_err_t grub_autoefi_prepare (void) # define SYSTEM_TABLE_PTR(x) ((void *)(grub_efi_system_table->x)) # define SIZEOF_OF_UINTN sizeof (grub_efi_uintn_t) # define SYSTEM_TABLE(x) (grub_efi_system_table->x) +# define grub_autoefi_finish_boot_services grub_efi_finish_boot_services # define EFI_PRESENT 1 #else # include @@ -72,6 +73,7 @@ static inline grub_err_t grub_autoefi_prepare (void) # define SYSTEM_TABLE GRUB_EFIEMU_SYSTEM_TABLE # define grub_efi_allocate_pages(x,y) (x) # define grub_efi_free_pages(x,y) GRUB_EFI_SUCCESS +# define grub_autoefi_finish_boot_services grub_efiemu_finish_boot_services # define EFI_PRESENT 1 #endif diff --git a/include/grub/efiemu/efiemu.h b/include/grub/efiemu/efiemu.h index 3980d32cd..1cddbca7c 100644 --- a/include/grub/efiemu/efiemu.h +++ b/include/grub/efiemu/efiemu.h @@ -217,13 +217,12 @@ int grub_efiemu_get_memory_map (grub_efi_uintn_t *memory_map_size, grub_efi_uintn_t *map_key, grub_efi_uintn_t *descriptor_size, grub_efi_uint32_t *descriptor_version); +#define grub_efiemu_finish_boot_services grub_efiemu_get_memory_map grub_err_t grub_efiemu_mmap_iterate (int NESTED_FUNC_ATTR (*hook) (grub_uint64_t, grub_uint64_t, grub_uint32_t)); int grub_efiemu_sizeof_uintn_t (void); -int grub_efiemu_exit_boot_services (grub_efi_uintn_t map_key); -int grub_efiemu_finish_boot_services (void); grub_err_t grub_efiemu_get_lower_upper_memory (grub_uint64_t *lower, grub_uint64_t *upper); #define GRUB_EFIEMU_MEMORY_AVAILABLE 1 diff --git a/loader/i386/xnu.c b/loader/i386/xnu.c index dcec3554f..2aec590fb 100644 --- a/loader/i386/xnu.c +++ b/loader/i386/xnu.c @@ -1038,9 +1038,9 @@ grub_xnu_boot (void) bootparams->devtree = devtree_target; bootparams->devtreelen = devtreelen; - err = grub_efi_finish_boot_services (&memory_map_size, memory_map, - &map_key, &descriptor_size, - &descriptor_version); + err = grub_autoefi_finish_boot_services (&memory_map_size, memory_map, + &map_key, &descriptor_size, + &descriptor_version); if (err) return err; From ba2f141cb580b243ef2defdb68b3137b7199499b Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 21 Apr 2010 10:02:05 +0200 Subject: [PATCH 096/178] Check memory map when choosing address --- lib/relocator.c | 46 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/lib/relocator.c b/lib/relocator.c index 1fe5a94e5..127b80533 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -21,6 +21,7 @@ #include #include #include +#include struct grub_relocator { @@ -63,6 +64,7 @@ struct grub_relocator_extra_block grub_addr_t end; }; +#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS struct grub_relocator_fw_leftover { struct grub_relocator_fw_leftover *next; @@ -72,6 +74,8 @@ struct grub_relocator_fw_leftover }; struct grub_relocator_fw_leftover *leftovers; +#endif + struct grub_relocator_extra_block *extra_blocks; struct grub_relocator * @@ -220,6 +224,7 @@ allocate_inreg (grub_addr_t addr, grub_size_t size, } } +#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS static void check_leftover (struct grub_relocator_fw_leftover *lo) { @@ -233,6 +238,7 @@ check_leftover (struct grub_relocator_fw_leftover *lo) if (lo->next) lo->next->prev = lo->prev; } +#endif static void free_subchunk (const struct grub_relocator_subchunk *subchu) @@ -923,7 +929,10 @@ malloc_in_range (struct grub_relocator *rel, curschu->size = alloc_end - alloc_start; if (typepre == CHUNK_TYPE_REGION_START) if (!oom && (typepre == CHUNK_TYPE_REGION_START - || typepre == CHUNK_TYPE_FIRMWARE)) +#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS + || typepre == CHUNK_TYPE_FIRMWARE +#endif + )) { struct grub_relocator_extra_block *ne; ne = grub_malloc (sizeof (*ne)); @@ -1282,11 +1291,36 @@ grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, } while (0); - /* FIXME: check memory map. */ - if (preference == GRUB_RELOCATOR_PREFERENCE_HIGH) - chunk->target = ALIGN_DOWN (max_addr, align); - else - chunk->target = ALIGN_UP (min_addr, align); + { + int found = 0; + auto int NESTED_FUNC_ATTR hook (grub_uint64_t, grub_uint64_t, grub_uint32_t); + int NESTED_FUNC_ATTR hook (grub_uint64_t addr, grub_uint64_t sz, grub_uint32_t type) + { + grub_uint64_t candidate; + if (type != GRUB_MACHINE_MEMORY_AVAILABLE) + return 0; + candidate = ALIGN_UP (addr, align); + if (candidate < min_addr) + candidate = min_addr; + if (candidate + size >= addr + sz + || candidate > ALIGN_DOWN (max_addr, align)) + return 0; + if (preference == GRUB_RELOCATOR_PREFERENCE_HIGH) + candidate = ALIGN_DOWN (addr + sz - size, align); + if (!found || (preference == GRUB_RELOCATOR_PREFERENCE_HIGH + && candidate > chunk->target)) + chunk->target = candidate; + if (!found || (preference == GRUB_RELOCATOR_PREFERENCE_LOW + && candidate < chunk->target)) + chunk->target = candidate; + found = 1; + return 0; + } + + grub_machine_mmap_iterate (hook); + if (!found) + return grub_error (GRUB_ERR_BAD_OS, "couldn't find suitable memory target"); + } while (1) { struct grub_relocator_chunk *chunk2; From 368c17f85d89ee8b03dc3c77b4df59ce2e5abf2c Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 21 Apr 2010 15:25:49 +0200 Subject: [PATCH 097/178] First part of virtual addr support in relocator --- include/grub/i386/memory.h | 24 ++++++ include/grub/relocator_private.h | 13 ++-- lib/i386/relocator.c | 24 +++--- lib/relocator.c | 124 ++++++++++++++++--------------- 4 files changed, 110 insertions(+), 75 deletions(-) diff --git a/include/grub/i386/memory.h b/include/grub/i386/memory.h index fe2f6e4e1..4f9a3c916 100644 --- a/include/grub/i386/memory.h +++ b/include/grub/i386/memory.h @@ -28,4 +28,28 @@ #define GRUB_MEMORY_CPU_AMD64_MSR 0xc0000080 #define GRUB_MEMORY_CPU_AMD64_MSR_ON 0x00000100 +#ifndef ASM_FILE + +typedef grub_addr_t grub_phys_addr_t; + +static inline grub_phys_addr_t +grub_vtop (void *a) +{ + return (grub_phys_addr_t) a; +} + +static inline void * +grub_map_memory (grub_phys_addr_t a, grub_size_t size __attribute__ ((unused))) +{ + return (void *) a; +} + +static inline void +grub_unmap_memory (void *a __attribute__ ((unused)), + grub_size_t size __attribute__ ((unused))) +{ +} + +#endif + #endif /* ! GRUB_MEMORY_CPU_HEADER */ diff --git a/include/grub/relocator_private.h b/include/grub/relocator_private.h index a859ec9fd..d12a0f05a 100644 --- a/include/grub/relocator_private.h +++ b/include/grub/relocator_private.h @@ -31,8 +31,9 @@ extern grub_size_t grub_relocator_jumper_size; void grub_cpu_relocator_init (void); grub_err_t -grub_relocator_prepare_relocs (struct grub_relocator *rel, grub_addr_t addr, - grub_addr_t *relstart, grub_size_t *relsize); +grub_relocator_prepare_relocs (struct grub_relocator *rel, + void *addr, + void **relstart, grub_size_t *relsize); void grub_cpu_relocator_forward (void *rels, void *src, void *tgt, grub_size_t size); void grub_cpu_relocator_backward (void *rels, void *src, void *tgt, @@ -72,7 +73,7 @@ struct grub_relocator_mmap_event COLLISION_START = 10, COLLISION_END = COLLISION_START | 1 } type; - grub_addr_t pos; + grub_phys_addr_t pos; union { struct @@ -91,10 +92,12 @@ struct grub_relocator_mmap_event /* Return 0 on failure, 1 on success. The failure here can be very time-expensive, so please make sure fill events is accurate. */ #if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS -int grub_relocator_firmware_alloc_region (grub_addr_t start, grub_size_t size); +int grub_relocator_firmware_alloc_region (grub_phys_addr_t start, + grub_size_t size); unsigned grub_relocator_firmware_fill_events (struct grub_relocator_mmap_event *events); unsigned grub_relocator_firmware_get_max_events (void); -void grub_relocator_firmware_free_region (grub_addr_t start, grub_size_t size); +void grub_relocator_firmware_free_region (grub_phys_addr_t start, + grub_size_t size); #endif #endif diff --git a/lib/i386/relocator.c b/lib/i386/relocator.c index 5985fac7a..5dd4cde75 100644 --- a/lib/i386/relocator.c +++ b/lib/i386/relocator.c @@ -84,6 +84,12 @@ grub_size_t grub_relocator_jumper_size = 12; grub_size_t grub_relocator_jumper_size = 7; #endif +static inline void * +ptov (grub_addr_t a) +{ + return (void *) a; +} + void grub_cpu_relocator_init (void) { @@ -148,10 +154,10 @@ grub_err_t grub_relocator32_boot (struct grub_relocator *rel, struct grub_relocator32_state state) { - grub_addr_t target; + grub_phys_addr_t target; void *src; grub_err_t err; - grub_addr_t relst; + void *relst; err = grub_relocator_alloc_chunk_align (rel, &src, &target, 0, (0xffffffff - RELOCATOR_SIZEOF (32)) @@ -170,7 +176,7 @@ grub_relocator32_boot (struct grub_relocator *rel, grub_memmove (src, &grub_relocator32_start, RELOCATOR_SIZEOF (32)); - err = grub_relocator_prepare_relocs (rel, target, &relst, NULL); + err = grub_relocator_prepare_relocs (rel, ptov (target), &relst, NULL); if (err) return err; @@ -185,10 +191,10 @@ grub_err_t grub_relocator16_boot (struct grub_relocator *rel, struct grub_relocator16_state state) { - grub_addr_t target; + grub_phys_addr_t target; void *src; grub_err_t err; - grub_addr_t relst; + void *relst; err = grub_relocator_alloc_chunk_align (rel, &src, &target, 0, 0xa0000 - RELOCATOR_SIZEOF (16), @@ -212,7 +218,7 @@ grub_relocator16_boot (struct grub_relocator *rel, grub_memmove (src, &grub_relocator16_start, RELOCATOR_SIZEOF (16)); - err = grub_relocator_prepare_relocs (rel, target, &relst, NULL); + err = grub_relocator_prepare_relocs (rel, ptov (target), &relst, NULL); if (err) return err; @@ -228,10 +234,10 @@ grub_relocator64_boot (struct grub_relocator *rel, struct grub_relocator64_state state, grub_addr_t min_addr, grub_addr_t max_addr) { - grub_addr_t target; + grub_phys_addr_t target; void *src; grub_err_t err; - grub_addr_t relst; + void *relst; err = grub_relocator_alloc_chunk_align (rel, &src, &target, min_addr, max_addr - RELOCATOR_SIZEOF (64), @@ -251,7 +257,7 @@ grub_relocator64_boot (struct grub_relocator *rel, grub_memmove (src, &grub_relocator64_start, RELOCATOR_SIZEOF (64)); - err = grub_relocator_prepare_relocs (rel, target, &relst, NULL); + err = grub_relocator_prepare_relocs (rel, ptov (target), &relst, NULL); if (err) return err; diff --git a/lib/relocator.c b/lib/relocator.c index 127b80533..b559d12ea 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -26,9 +26,9 @@ struct grub_relocator { struct grub_relocator_chunk *chunks; - grub_addr_t postchunks; - grub_addr_t highestaddr; - grub_addr_t highestnonpostaddr; + grub_phys_addr_t postchunks; + grub_phys_addr_t highestaddr; + grub_phys_addr_t highestnonpostaddr; grub_size_t relocators_size; }; @@ -39,9 +39,11 @@ struct grub_relocator_subchunk CHUNK_TYPE_FIRMWARE, CHUNK_TYPE_LEFTOVER #endif } type; - grub_addr_t host_start; - grub_addr_t start; + grub_mm_region_t reg; + grub_mm_header_t head; + grub_phys_addr_t start; grub_size_t size; + grub_size_t pre_size; struct grub_relocator_extra_block *extra; struct grub_relocator_fw_leftover *pre, *post; }; @@ -49,8 +51,9 @@ struct grub_relocator_subchunk struct grub_relocator_chunk { struct grub_relocator_chunk *next; - grub_addr_t src; - grub_addr_t target; + grub_phys_addr_t src; + void *srcv; + grub_phys_addr_t target; grub_size_t size; struct grub_relocator_subchunk *subchunks; unsigned nsubchunks; @@ -60,8 +63,8 @@ struct grub_relocator_extra_block { struct grub_relocator_extra_block *next; struct grub_relocator_extra_block **prev; - grub_addr_t start; - grub_addr_t end; + grub_phys_addr_t start; + grub_phys_addr_t end; }; #if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS @@ -69,7 +72,7 @@ struct grub_relocator_fw_leftover { struct grub_relocator_fw_leftover *next; struct grub_relocator_fw_leftover **prev; - grub_addr_t quantstart; + grub_phys_addr_t quantstart; grub_uint8_t freebytes[GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT / 8]; }; @@ -89,7 +92,7 @@ grub_relocator_new (void) if (!ret) return NULL; - ret->postchunks = ~(grub_addr_t) 0; + ret->postchunks = ~(grub_phys_addr_t) 0; ret->relocators_size = grub_relocator_jumper_size; grub_dprintf ("relocator", "relocators_size=%lu\n", (unsigned long) ret->relocators_size); @@ -110,10 +113,11 @@ is_start (int type) } static void -allocate_regstart (grub_addr_t addr, grub_size_t size, grub_mm_region_t rb, +allocate_regstart (grub_phys_addr_t addr, grub_size_t size, grub_mm_region_t rb, grub_mm_region_t *regancestor, grub_mm_header_t hancestor) { - grub_addr_t newreg_start, newreg_raw_start = addr + size; + grub_addr_t newreg_start, newreg_raw_start + = (grub_addr_t) rb + (addr - grub_vtop (rb)) + size; grub_addr_t newreg_size, newreg_presize; grub_mm_header_t new_header; grub_mm_header_t hb = (grub_mm_header_t) (rb + 1); @@ -176,23 +180,24 @@ allocate_regstart (grub_addr_t addr, grub_size_t size, grub_mm_region_t rb, } static void -allocate_inreg (grub_addr_t addr, grub_size_t size, +allocate_inreg (grub_phys_addr_t paddr, grub_size_t size, grub_mm_header_t hb, grub_mm_header_t hbp, grub_mm_region_t rb) { struct grub_mm_header *foll = NULL; + grub_addr_t vaddr = (grub_addr_t) hb + (paddr - grub_vtop (hb)); - if (ALIGN_UP (addr + size, GRUB_MM_ALIGN) + GRUB_MM_ALIGN + if (ALIGN_UP (vaddr + size, GRUB_MM_ALIGN) + GRUB_MM_ALIGN <= (grub_addr_t) (hb + hb->size)) { - foll = (void *) ALIGN_UP (addr + size, GRUB_MM_ALIGN); + foll = (void *) ALIGN_UP (vaddr + size, GRUB_MM_ALIGN); foll->magic = GRUB_MM_FREE_MAGIC; foll->size = hb->size - (foll - hb); } - if (addr - (grub_addr_t) hb >= sizeof (*hb)) + if (vaddr - (grub_addr_t) hb >= sizeof (*hb)) { - hb->size = ((addr - (grub_addr_t) hb) >> GRUB_MM_ALIGN_LOG2); + hb->size = ((vaddr - (grub_addr_t) hb) >> GRUB_MM_ALIGN_LOG2); if (foll) { foll->next = hb; @@ -250,12 +255,13 @@ free_subchunk (const struct grub_relocator_subchunk *subchu) grub_mm_region_t r1, r2, *rp; grub_mm_header_t h; grub_size_t pre_size; - r1 = (grub_mm_region_t) ALIGN_UP (subchu->host_start, GRUB_MM_ALIGN); - r2 = (grub_mm_region_t) ALIGN_UP (subchu->start + subchu->size, + r1 = subchu->reg; + r2 = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) subchu->reg + + (grub_vtop (subchu->reg) + - subchu->start) + subchu->size, GRUB_MM_ALIGN); for (rp = &grub_mm_base; *rp && *rp != r2; rp = &((*rp)->next)); - pre_size = ALIGN_UP (subchu->host_start, GRUB_MM_ALIGN) - - subchu->host_start; + pre_size = subchu->pre_size; if (*rp) { @@ -328,7 +334,7 @@ free_subchunk (const struct grub_relocator_subchunk *subchu) } case CHUNK_TYPE_IN_REGION: { - grub_mm_header_t h = (grub_mm_header_t) ALIGN_DOWN (subchu->start, + grub_mm_header_t h = (grub_mm_header_t) ALIGN_DOWN ((grub_addr_t) subchu->head, GRUB_MM_ALIGN); h->size = ((subchu->start + subchu->size + GRUB_MM_ALIGN - 1) / GRUB_MM_ALIGN) @@ -549,26 +555,26 @@ malloc_in_range (struct grub_relocator *rel, { pre_added = 1; events[N].type = REG_BEG_START; - events[N].pos = (grub_addr_t) r - r->pre_size; + events[N].pos = grub_vtop (r) - r->pre_size; events[N].reg = r; events[N].regancestor = ra; events[N].head = p; events[N].hancestor = pa; N++; events[N].type = REG_BEG_END; - events[N].pos = (grub_addr_t) (p + p->size) - sizeof (*r); + events[N].pos = grub_vtop (p + p->size) - sizeof (*r); N++; } else { events[N].type = IN_REG_START; - events[N].pos = (grub_addr_t) p; + events[N].pos = grub_vtop (p); events[N].head = p; events[N].hancestor = pa; events[N].reg = r; N++; events[N].type = IN_REG_END; - events[N].pos = (grub_addr_t) (p + p->size); + events[N].pos = grub_vtop (p + p->size); N++; } pa = p; @@ -862,20 +868,6 @@ malloc_in_range (struct grub_relocator *rel, } } - grub_memset ((void *) target, 0, size); - grub_dprintf ("relocator", "baseptr = %p\n", &base_saved); - for (r = base_saved; r; r = r->next) - { - p = r->first; - do - { - if (!p) - grub_fatal ("null in the ring %p\n", r); - p = p->next; - } - while (p != r->first); - } - /* Malloc is available again. */ grub_mm_base = base_saved; @@ -927,7 +919,13 @@ malloc_in_range (struct grub_relocator *rel, curschu->type = typepre; curschu->start = alloc_start; curschu->size = alloc_end - alloc_start; - if (typepre == CHUNK_TYPE_REGION_START) + if (typepre == CHUNK_TYPE_REGION_START + || typepre == CHUNK_TYPE_IN_REGION) + { + curschu->reg = events[last_start].reg; + curschu->head = events[last_start].head; + curschu->pre_size = alloc_start - events[j - 1].pos; + } if (!oom && (typepre == CHUNK_TYPE_REGION_START #if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS || typepre == CHUNK_TYPE_FIRMWARE @@ -1106,8 +1104,8 @@ malloc_in_range (struct grub_relocator *rel, static void adjust_limits (struct grub_relocator *rel, - grub_addr_t *min_addr, grub_addr_t *max_addr, - grub_addr_t in_min, grub_addr_t in_max) + grub_phys_addr_t *min_addr, grub_phys_addr_t *max_addr, + grub_phys_addr_t in_min, grub_phys_addr_t in_max) { struct grub_relocator_chunk *chunk; @@ -1129,10 +1127,10 @@ adjust_limits (struct grub_relocator *rel, grub_err_t grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, void **src, - grub_addr_t target, grub_size_t size) + grub_phys_addr_t target, grub_size_t size) { struct grub_relocator_chunk *chunk; - grub_addr_t min_addr = 0, max_addr; + grub_phys_addr_t min_addr = 0, max_addr; if (target > ~size) return grub_error (GRUB_ERR_OUT_OF_RANGE, "address is out of range"); @@ -1223,14 +1221,15 @@ grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, void **src, grub_dprintf ("relocator", "cur = %p, next = %p\n", rel->chunks, rel->chunks->next); - *src = (void *) chunk->src; + *src = chunk->srcv = grub_map_memory (chunk->src, chunk->size); return GRUB_ERR_NONE; } grub_err_t grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, - grub_addr_t *target, - grub_addr_t min_addr, grub_addr_t max_addr, + grub_phys_addr_t *target, + grub_phys_addr_t min_addr, + grub_phys_addr_t max_addr, grub_size_t size, grub_size_t align, int preference) { @@ -1354,7 +1353,7 @@ grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, rel->chunks = chunk; grub_dprintf ("relocator", "cur = %p, next = %p\n", rel->chunks, rel->chunks->next); - *src = (void *) chunk->src; + *src = chunk->srcv = grub_map_memory (chunk->src, chunk->size); *target = chunk->target; return GRUB_ERR_NONE; } @@ -1370,6 +1369,7 @@ grub_relocator_unload (struct grub_relocator *rel) unsigned i; for (i = 0; i < chunk->nsubchunks; i++) free_subchunk (&chunk->subchunks[i]); + grub_unmap_memory (chunk->srcv, chunk->size); next = chunk->next; grub_free (chunk->subchunks); grub_free (chunk); @@ -1378,11 +1378,11 @@ grub_relocator_unload (struct grub_relocator *rel) } grub_err_t -grub_relocator_prepare_relocs (struct grub_relocator *rel, grub_addr_t addr, - grub_addr_t *relstart, grub_size_t *relsize) +grub_relocator_prepare_relocs (struct grub_relocator *rel, void *addr, + void **relstart, grub_size_t *relsize) { - grub_addr_t rels; - grub_addr_t rels0; + grub_uint8_t *rels; + grub_uint8_t *rels0; struct grub_relocator_chunk *sorted; grub_size_t nchunks = 0; unsigned j; @@ -1395,7 +1395,7 @@ grub_relocator_prepare_relocs (struct grub_relocator *rel, grub_addr_t addr, grub_relocator_align, rel->relocators_size, &movers_chunk, 1, 1)) return grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory"); - rels = rels0 = movers_chunk.src; + rels = rels0 = movers_chunk.srcv; if (relsize) *relsize = rel->relocators_size; @@ -1463,23 +1463,25 @@ grub_relocator_prepare_relocs (struct grub_relocator *rel, grub_addr_t addr, if (sorted[j].src < sorted[j].target) { grub_cpu_relocator_backward ((void *) rels, - (void *) sorted[j].src, - (void *) sorted[j].target, + sorted[j].srcv, + grub_map_memory (sorted[j].target, + sorted[j].size), sorted[j].size); rels += grub_relocator_backward_size; } if (sorted[j].src > sorted[j].target) { grub_cpu_relocator_forward ((void *) rels, - (void *) sorted[j].src, - (void *) sorted[j].target, + sorted[j].srcv, + grub_map_memory (sorted[j].target, + sorted[j].size), sorted[j].size); rels += grub_relocator_forward_size; } if (sorted[j].src == sorted[j].target) - grub_arch_sync_caches ((void *) sorted[j].src, sorted[j].size); + grub_arch_sync_caches (sorted[j].srcv, sorted[j].size); } - grub_cpu_relocator_jumper ((void *) rels, addr); + grub_cpu_relocator_jumper ((void *) rels, (grub_addr_t) addr); *relstart = rels0; grub_free (sorted); return GRUB_ERR_NONE; From 4b2ec20b411f9829eafc639963a2f0812d8e046d Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 21 Apr 2010 19:13:45 +0200 Subject: [PATCH 098/178] Second part of p2v support --- include/grub/relocator.h | 20 +++- include/grub/relocator_private.h | 2 +- lib/i386/relocator.c | 41 ++++---- lib/relocator.c | 42 +++++--- loader/i386/bsd.c | 159 ++++++++++++++++++++----------- loader/i386/bsdXX.c | 51 ++++++---- loader/i386/linux.c | 44 +++++---- loader/i386/multiboot_mbi.c | 10 +- loader/i386/pc/linux.c | 48 ++++++---- loader/i386/pc/ntldr.c | 24 +++-- loader/multiboot.c | 24 +++-- loader/multiboot_elfxx.c | 48 ++++++---- loader/multiboot_mbi2.c | 12 ++- loader/xnu.c | 4 +- loader/xnu_resume.c | 44 +++++---- 15 files changed, 361 insertions(+), 212 deletions(-) diff --git a/include/grub/relocator.h b/include/grub/relocator.h index 32bab7053..89e746088 100644 --- a/include/grub/relocator.h +++ b/include/grub/relocator.h @@ -21,19 +21,29 @@ #include #include +#include struct grub_relocator; +struct grub_relocator_chunk; +typedef const struct grub_relocator_chunk *grub_relocator_chunk_t; struct grub_relocator *grub_relocator_new (void); grub_err_t -grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, void **src, - grub_addr_t target, grub_size_t size); +grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, + grub_relocator_chunk_t *out, + grub_phys_addr_t target, grub_size_t size); + +void * +get_virtual_current_address (grub_relocator_chunk_t in); +grub_phys_addr_t +get_physical_target_address (grub_relocator_chunk_t in); grub_err_t -grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, - grub_addr_t *target, - grub_addr_t min_addr, grub_addr_t max_addr, +grub_relocator_alloc_chunk_align (struct grub_relocator *rel, + grub_relocator_chunk_t *out, + grub_phys_addr_t min_addr, + grub_phys_addr_t max_addr, grub_size_t size, grub_size_t align, int preference); diff --git a/include/grub/relocator_private.h b/include/grub/relocator_private.h index d12a0f05a..10e445bfe 100644 --- a/include/grub/relocator_private.h +++ b/include/grub/relocator_private.h @@ -32,7 +32,7 @@ void grub_cpu_relocator_init (void); grub_err_t grub_relocator_prepare_relocs (struct grub_relocator *rel, - void *addr, + grub_addr_t addr, void **relstart, grub_size_t *relsize); void grub_cpu_relocator_forward (void *rels, void *src, void *tgt, grub_size_t size); diff --git a/lib/i386/relocator.c b/lib/i386/relocator.c index 5dd4cde75..55e6b5578 100644 --- a/lib/i386/relocator.c +++ b/lib/i386/relocator.c @@ -84,12 +84,6 @@ grub_size_t grub_relocator_jumper_size = 12; grub_size_t grub_relocator_jumper_size = 7; #endif -static inline void * -ptov (grub_addr_t a) -{ - return (void *) a; -} - void grub_cpu_relocator_init (void) { @@ -154,12 +148,11 @@ grub_err_t grub_relocator32_boot (struct grub_relocator *rel, struct grub_relocator32_state state) { - grub_phys_addr_t target; - void *src; grub_err_t err; void *relst; + grub_relocator_chunk_t ch; - err = grub_relocator_alloc_chunk_align (rel, &src, &target, 0, + err = grub_relocator_alloc_chunk_align (rel, &ch, 0, (0xffffffff - RELOCATOR_SIZEOF (32)) + 1, RELOCATOR_SIZEOF (32), 16, GRUB_RELOCATOR_PREFERENCE_NONE); @@ -174,9 +167,11 @@ grub_relocator32_boot (struct grub_relocator *rel, grub_relocator32_esp = state.esp; grub_relocator32_esi = state.esi; - grub_memmove (src, &grub_relocator32_start, RELOCATOR_SIZEOF (32)); + grub_memmove (get_virtual_current_address (ch), &grub_relocator32_start, + RELOCATOR_SIZEOF (32)); - err = grub_relocator_prepare_relocs (rel, ptov (target), &relst, NULL); + err = grub_relocator_prepare_relocs (rel, get_physical_target_address (ch), + &relst, NULL); if (err) return err; @@ -191,12 +186,11 @@ grub_err_t grub_relocator16_boot (struct grub_relocator *rel, struct grub_relocator16_state state) { - grub_phys_addr_t target; - void *src; grub_err_t err; void *relst; + grub_relocator_chunk_t ch; - err = grub_relocator_alloc_chunk_align (rel, &src, &target, 0, + err = grub_relocator_alloc_chunk_align (rel, &ch, 0, 0xa0000 - RELOCATOR_SIZEOF (16), RELOCATOR_SIZEOF (16), 16, GRUB_RELOCATOR_PREFERENCE_NONE); @@ -216,12 +210,16 @@ grub_relocator16_boot (struct grub_relocator *rel, grub_relocator16_edx = state.edx; - grub_memmove (src, &grub_relocator16_start, RELOCATOR_SIZEOF (16)); + grub_memmove (get_virtual_current_address (ch), &grub_relocator16_start, + RELOCATOR_SIZEOF (16)); - err = grub_relocator_prepare_relocs (rel, ptov (target), &relst, NULL); + err = grub_relocator_prepare_relocs (rel, get_physical_target_address (ch), + &relst, NULL); if (err) return err; + grub_printf ("%p\n", relst); + asm volatile ("cli"); ((void (*) (void)) relst) (); @@ -234,12 +232,11 @@ grub_relocator64_boot (struct grub_relocator *rel, struct grub_relocator64_state state, grub_addr_t min_addr, grub_addr_t max_addr) { - grub_phys_addr_t target; - void *src; grub_err_t err; void *relst; + grub_relocator_chunk_t ch; - err = grub_relocator_alloc_chunk_align (rel, &src, &target, min_addr, + err = grub_relocator_alloc_chunk_align (rel, &ch, min_addr, max_addr - RELOCATOR_SIZEOF (64), RELOCATOR_SIZEOF (64), 16, GRUB_RELOCATOR_PREFERENCE_NONE); @@ -255,9 +252,11 @@ grub_relocator64_boot (struct grub_relocator *rel, grub_relocator64_rsi = state.rsi; grub_relocator64_cr3 = state.cr3; - grub_memmove (src, &grub_relocator64_start, RELOCATOR_SIZEOF (64)); + grub_memmove (get_virtual_current_address (ch), &grub_relocator64_start, + RELOCATOR_SIZEOF (64)); - err = grub_relocator_prepare_relocs (rel, ptov (target), &relst, NULL); + err = grub_relocator_prepare_relocs (rel, get_physical_target_address (ch), + &relst, NULL); if (err) return err; diff --git a/lib/relocator.c b/lib/relocator.c index b559d12ea..e43463e4d 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -81,6 +81,18 @@ struct grub_relocator_fw_leftover *leftovers; struct grub_relocator_extra_block *extra_blocks; +void * +get_virtual_current_address (grub_relocator_chunk_t in) +{ + return in->srcv; +} + +grub_phys_addr_t +get_physical_target_address (grub_relocator_chunk_t in) +{ + return in->target; +} + struct grub_relocator * grub_relocator_new (void) { @@ -1126,7 +1138,8 @@ adjust_limits (struct grub_relocator *rel, } grub_err_t -grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, void **src, +grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, + grub_relocator_chunk_t *out, grub_phys_addr_t target, grub_size_t size) { struct grub_relocator_chunk *chunk; @@ -1221,13 +1234,14 @@ grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, void **src, grub_dprintf ("relocator", "cur = %p, next = %p\n", rel->chunks, rel->chunks->next); - *src = chunk->srcv = grub_map_memory (chunk->src, chunk->size); + chunk->srcv = grub_map_memory (chunk->src, chunk->size); + *out = chunk; return GRUB_ERR_NONE; } grub_err_t -grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, - grub_phys_addr_t *target, +grub_relocator_alloc_chunk_align (struct grub_relocator *rel, + grub_relocator_chunk_t *out, grub_phys_addr_t min_addr, grub_phys_addr_t max_addr, grub_size_t size, grub_size_t align, @@ -1262,8 +1276,8 @@ grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, chunk->size = size; chunk->next = rel->chunks; rel->chunks = chunk; - *src = (void *) chunk->src; - *target = chunk->target; + chunk->srcv = grub_map_memory (chunk->src, chunk->size); + *out = chunk; return GRUB_ERR_NONE; } @@ -1300,12 +1314,12 @@ grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, return 0; candidate = ALIGN_UP (addr, align); if (candidate < min_addr) - candidate = min_addr; - if (candidate + size >= addr + sz + candidate = ALIGN_UP (min_addr, align); + if (candidate + size > addr + sz || candidate > ALIGN_DOWN (max_addr, align)) return 0; if (preference == GRUB_RELOCATOR_PREFERENCE_HIGH) - candidate = ALIGN_DOWN (addr + sz - size, align); + candidate = ALIGN_DOWN (min (addr + sz - size, max_addr), align); if (!found || (preference == GRUB_RELOCATOR_PREFERENCE_HIGH && candidate > chunk->target)) chunk->target = candidate; @@ -1353,8 +1367,8 @@ grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src, rel->chunks = chunk; grub_dprintf ("relocator", "cur = %p, next = %p\n", rel->chunks, rel->chunks->next); - *src = chunk->srcv = grub_map_memory (chunk->src, chunk->size); - *target = chunk->target; + chunk->srcv = grub_map_memory (chunk->src, chunk->size); + *out = chunk; return GRUB_ERR_NONE; } @@ -1378,7 +1392,7 @@ grub_relocator_unload (struct grub_relocator *rel) } grub_err_t -grub_relocator_prepare_relocs (struct grub_relocator *rel, void *addr, +grub_relocator_prepare_relocs (struct grub_relocator *rel, grub_addr_t addr, void **relstart, grub_size_t *relsize) { grub_uint8_t *rels; @@ -1395,12 +1409,12 @@ grub_relocator_prepare_relocs (struct grub_relocator *rel, void *addr, grub_relocator_align, rel->relocators_size, &movers_chunk, 1, 1)) return grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory"); - rels = rels0 = movers_chunk.srcv; + rels = rels0 = grub_map_memory (movers_chunk.src, movers_chunk.size); if (relsize) *relsize = rel->relocators_size; - grub_dprintf ("relocator", "Relocs allocated\n"); + grub_dprintf ("relocator", "Relocs allocated at %p\n", movers_chunk.srcv); { unsigned i; diff --git a/loader/i386/bsd.c b/loader/i386/bsd.c index cfd10713d..1b2fadd80 100644 --- a/loader/i386/bsd.c +++ b/loader/i386/bsd.c @@ -606,10 +606,14 @@ grub_freebsd_boot (void) if (is_64bit) p_size += 4096 * 3; - err = grub_relocator_alloc_chunk_addr (relocator, (void **) &p, - kern_end, p_size); - if (err) - return err; + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_addr (relocator, &ch, + kern_end, p_size); + if (err) + return err; + p = get_virtual_current_address (ch); + } p_target = kern_end; p0 = p; kern_end += p_size; @@ -682,14 +686,18 @@ grub_freebsd_boot (void) grub_uint32_t *stack; grub_addr_t stack_target; - err = grub_relocator_alloc_chunk_align (relocator, (void **) &stack, - &stack_target, - 0x10000, 0x90000, - 3 * sizeof (grub_uint32_t) - + sizeof (bi), 4, - GRUB_RELOCATOR_PREFERENCE_NONE); - if (err) - return err; + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_align (relocator, &ch, + 0x10000, 0x90000, + 3 * sizeof (grub_uint32_t) + + sizeof (bi), 4, + GRUB_RELOCATOR_PREFERENCE_NONE); + if (err) + return err; + stack = get_virtual_current_address (ch); + stack_target = get_physical_target_address (ch); + } #ifdef GRUB_MACHINE_EFI err = grub_efi_finish_boot_services (NULL, NULL, NULL, NULL, NULL); @@ -714,14 +722,19 @@ grub_freebsd_boot (void) struct grub_relocator32_state state; grub_uint32_t *stack; grub_addr_t stack_target; - err = grub_relocator_alloc_chunk_align (relocator, (void **) &stack, - &stack_target, - 0x10000, 0x90000, - 9 * sizeof (grub_uint32_t) - + sizeof (bi), 4, - GRUB_RELOCATOR_PREFERENCE_NONE); - if (err) - return err; + + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_align (relocator, &ch, + 0x10000, 0x90000, + 9 * sizeof (grub_uint32_t) + + sizeof (bi), 4, + GRUB_RELOCATOR_PREFERENCE_NONE); + if (err) + return err; + stack = get_virtual_current_address (ch); + stack_target = get_physical_target_address (ch); + } #ifdef GRUB_MACHINE_EFI err = grub_efi_finish_boot_services (NULL, NULL, NULL, NULL, NULL); @@ -772,12 +785,16 @@ grub_openbsd_boot (void) } buf_target = GRUB_BSD_TEMP_BUFFER - 9 * sizeof (grub_uint32_t); - err = grub_relocator_alloc_chunk_addr (relocator, &buf0, - buf_target, tag_buf_len - + sizeof (struct grub_openbsd_bootargs) - + 9 * sizeof (grub_uint32_t)); - if (err) - return err; + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_addr (relocator, &ch, buf_target, + tag_buf_len + + sizeof (struct grub_openbsd_bootargs) + + 9 * sizeof (grub_uint32_t)); + if (err) + return err; + buf0 = get_virtual_current_address (ch); + } stack = (grub_uint32_t *) buf0; arg0 = curarg = stack + 9; @@ -976,12 +993,16 @@ grub_netbsd_boot (void) } arg_target = kern_end; - err = grub_relocator_alloc_chunk_addr (relocator, &curarg, - arg_target, tag_buf_len - + sizeof (struct grub_netbsd_bootinfo) - + tag_count * sizeof (grub_uint32_t)); - if (err) - return err; + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_addr (relocator, &ch, + arg_target, tag_buf_len + + sizeof (struct grub_netbsd_bootinfo) + + tag_count * sizeof (grub_uint32_t)); + if (err) + return err; + curarg = get_virtual_current_address (ch); + } arg0 = curarg; bootinfo = (void *) ((grub_uint8_t *) arg0 + tag_buf_len); @@ -1004,12 +1025,16 @@ grub_netbsd_boot (void) } } - err = grub_relocator_alloc_chunk_align (relocator, (void **) &stack, - &stack_target, 0x10000, 0x90000, - 7 * sizeof (grub_uint32_t), 4, - GRUB_RELOCATOR_PREFERENCE_NONE); - if (err) - return err; + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_align (relocator, &ch, 0x10000, 0x90000, + 7 * sizeof (grub_uint32_t), 4, + GRUB_RELOCATOR_PREFERENCE_NONE); + if (err) + return err; + stack = get_virtual_current_address (ch); + stack_target = get_physical_target_address (ch); + } #ifdef GRUB_MACHINE_EFI err = grub_efi_finish_boot_services (NULL, NULL, NULL, NULL, NULL); @@ -1107,10 +1132,15 @@ grub_bsd_load_aout (grub_file_t file) if (!relocator) return grub_errno; - err = grub_relocator_alloc_chunk_addr (relocator, &kern_chunk_src, - kern_start, kern_end - kern_start); - if (err) - return err; + { + grub_relocator_chunk_t ch; + + err = grub_relocator_alloc_chunk_addr (relocator, &ch, + kern_start, kern_end - kern_start); + if (err) + return err; + kern_chunk_src = get_virtual_current_address (ch); + } return grub_aout_load (file, ofs, kern_chunk_src, ah.aout32.a_text + ah.aout32.a_data, @@ -1210,15 +1240,19 @@ grub_bsd_load_elf (grub_elf_t elf) if (grub_elf_is_elf32 (elf)) { + grub_relocator_chunk_t ch; + entry = elf->ehdr.ehdr32.e_entry & 0xFFFFFF; err = grub_elf32_phdr_iterate (elf, grub_bsd_elf32_size_hook, NULL); if (err) return err; - err = grub_relocator_alloc_chunk_addr (relocator, &kern_chunk_src, + err = grub_relocator_alloc_chunk_addr (relocator, &ch, kern_start, kern_end - kern_start); if (err) return err; + kern_chunk_src = get_virtual_current_address (ch); + return grub_elf32_load (elf, grub_bsd_elf32_hook, 0, 0); } else if (grub_elf_is_elf64 (elf)) @@ -1246,10 +1280,15 @@ grub_bsd_load_elf (grub_elf_t elf) grub_dprintf ("bsd", "kern_start = %lx, kern_end = %lx\n", (unsigned long) kern_start, (unsigned long) kern_end); - err = grub_relocator_alloc_chunk_addr (relocator, &kern_chunk_src, - kern_start, kern_end - kern_start); - if (err) - return err; + { + grub_relocator_chunk_t ch; + + err = grub_relocator_alloc_chunk_addr (relocator, &ch, kern_start, + kern_end - kern_start); + if (err) + return err; + kern_chunk_src = get_virtual_current_address (ch); + } return grub_elf64_load (elf, grub_bsd_elf64_hook, 0, 0); } @@ -1682,10 +1721,15 @@ grub_cmd_freebsd_module (grub_command_t cmd __attribute__ ((unused)), if ((!file) || (!file->size)) goto fail; - err = grub_relocator_alloc_chunk_addr (relocator, &src, kern_end, - file->size); - if (err) - goto fail; + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_addr (relocator, &ch, kern_end, + file->size); + if (err) + goto fail; + src = get_virtual_current_address (ch); + } + grub_file_read (file, src, file->size); if (grub_errno) @@ -1728,10 +1772,15 @@ grub_netbsd_module_load (char *filename, grub_uint32_t type) if ((!file) || (!file->size)) goto fail; - err = grub_relocator_alloc_chunk_addr (relocator, &src, kern_end, - file->size); - if (err) - goto fail; + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_addr (relocator, &ch, kern_end, + file->size); + if (err) + goto fail; + + src = get_virtual_current_address (ch); + } grub_file_read (file, src, file->size); if (grub_errno) diff --git a/loader/i386/bsdXX.c b/loader/i386/bsdXX.c index 734633704..85f4c6236 100644 --- a/loader/i386/bsdXX.c +++ b/loader/i386/bsdXX.c @@ -103,10 +103,14 @@ SUFFIX (grub_freebsd_load_elfmodule_obj) (struct grub_relocator *relocator, chunk_size += s->sh_size; } - err = grub_relocator_alloc_chunk_addr (relocator, &chunk_src, - module, chunk_size); - if (err) - return err; + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_addr (relocator, &ch, + module, chunk_size); + if (err) + return err; + chunk_src = get_virtual_current_address (ch); + } for (s = (Elf_Shdr *) shdr; s < (Elf_Shdr *) ((char *) shdr + e.e_shnum * e.e_shentsize); @@ -191,10 +195,16 @@ SUFFIX (grub_freebsd_load_elfmodule) (struct grub_relocator *relocator, chunk_size = s->sh_addr + s->sh_size; } - err = grub_relocator_alloc_chunk_addr (relocator, &chunk_src, - module, chunk_size); - if (err) - return err; + { + grub_relocator_chunk_t ch; + + err = grub_relocator_alloc_chunk_addr (relocator, &ch, + module, chunk_size); + if (err) + return err; + + chunk_src = get_virtual_current_address (ch); + } for (s = (Elf_Shdr *) shdr; s < (Elf_Shdr *) ((char *) shdr + e.e_shnum * e.e_shentsize); @@ -300,10 +310,15 @@ SUFFIX (grub_freebsd_load_elf_meta) (struct grub_relocator *relocator, + 2 * sizeof (grub_freebsd_addr_t); symtarget = ALIGN_UP (*kern_end, sizeof (grub_freebsd_addr_t)); - err = grub_relocator_alloc_chunk_addr (relocator, &sym_chunk, - symtarget, chunk_size); - if (err) - return err; + + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_addr (relocator, &ch, + symtarget, chunk_size); + if (err) + return err; + sym_chunk = get_virtual_current_address (ch); + } symstart = symtarget; symend = symstart + chunk_size; @@ -413,10 +428,14 @@ SUFFIX (grub_netbsd_load_elf_meta) (struct grub_relocator *relocator, + sizeof (e) + e.e_shnum * e.e_shentsize; symtarget = ALIGN_UP (*kern_end, sizeof (grub_freebsd_addr_t)); - err = grub_relocator_alloc_chunk_addr (relocator, &sym_chunk, - symtarget, chunk_size); - if (err) - return err; + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_addr (relocator, &ch, + symtarget, chunk_size); + if (err) + return err; + sym_chunk = get_virtual_current_address (ch); + } symtab.nsyms = 1; symtab.ssyms = symtarget; diff --git a/loader/i386/linux.c b/loader/i386/linux.c index ef1b8309e..e05225f25 100644 --- a/loader/i386/linux.c +++ b/loader/i386/linux.c @@ -412,20 +412,28 @@ allocate_pages (grub_size_t prot_size) goto fail; } - err = grub_relocator_alloc_chunk_addr (relocator, &real_mode_mem, - real_mode_target, - (real_size + mmap_size - + efi_mmap_size)); - if (err) - goto fail; + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_addr (relocator, &ch, + real_mode_target, + (real_size + mmap_size + + efi_mmap_size)); + if (err) + goto fail; + real_mode_mem = get_virtual_current_address (ch); + } efi_mmap_buf = (grub_uint8_t *) real_mode_mem + real_size + mmap_size; prot_mode_target = GRUB_LINUX_BZIMAGE_ADDR; - err = grub_relocator_alloc_chunk_addr (relocator, &prot_mode_mem, - prot_mode_target, prot_size); - if (err) - goto fail; + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_addr (relocator, &ch, + prot_mode_target, prot_size); + if (err) + goto fail; + prot_mode_mem = get_virtual_current_address (ch); + } grub_dprintf ("linux", "real_mode_mem = %lx, real_mode_pages = %x, " "prot_mode_mem = %lx, prot_mode_pages = %x\n", @@ -1111,12 +1119,16 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), goto fail; } - err = grub_relocator_alloc_chunk_align (relocator, &initrd_mem, - &initrd_mem_target, - addr_min, addr, size, 0x1000, - GRUB_RELOCATOR_PREFERENCE_HIGH); - if (err) - return err; + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_align (relocator, &ch, + addr_min, addr, size, 0x1000, + GRUB_RELOCATOR_PREFERENCE_HIGH); + if (err) + return err; + initrd_mem = get_virtual_current_address (ch); + initrd_mem_target = get_physical_target_address (ch); + } if (grub_file_read (file, initrd_mem, size) != size) { diff --git a/loader/i386/multiboot_mbi.c b/loader/i386/multiboot_mbi.c index 2f32c3f4e..ea1385be7 100644 --- a/loader/i386/multiboot_mbi.c +++ b/loader/i386/multiboot_mbi.c @@ -108,6 +108,7 @@ grub_multiboot_load (grub_file_t file) header->load_end_addr - header->load_addr); grub_size_t code_size; void *source; + grub_relocator_chunk_t ch; if (header->bss_end_addr) code_size = (header->bss_end_addr - header->load_addr); @@ -115,7 +116,7 @@ grub_multiboot_load (grub_file_t file) code_size = load_size; err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator, - &source, header->load_addr, + &ch, header->load_addr, code_size); if (err) { @@ -123,6 +124,7 @@ grub_multiboot_load (grub_file_t file) grub_free (buffer); return err; } + source = get_virtual_current_address (ch); if ((grub_file_seek (file, offset)) == (grub_off_t) -1) { @@ -322,16 +324,18 @@ grub_multiboot_make_mbi (grub_uint32_t *target) grub_err_t err; grub_size_t bufsize; + grub_relocator_chunk_t ch; bufsize = grub_multiboot_get_mbi_size (); - err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, - (void **) &ptrorig, &ptrdest, + err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, &ch, 0, 0xffffffff - bufsize, bufsize, 4, GRUB_RELOCATOR_PREFERENCE_NONE); if (err) return err; + ptrorig = get_virtual_current_address (ch); + ptrdest = (grub_addr_t) get_virtual_current_address (ch); *target = ptrdest; diff --git a/loader/i386/pc/linux.c b/loader/i386/pc/linux.c index 176220a2b..82640d77d 100644 --- a/loader/i386/pc/linux.c +++ b/loader/i386/pc/linux.c @@ -255,12 +255,15 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), } } - err = grub_relocator_alloc_chunk_addr (relocator, (void **) - &grub_linux_real_chunk, - grub_linux_real_target, - GRUB_LINUX_SETUP_MOVE_SIZE); - if (err) - return err; + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_addr (relocator, &ch, + grub_linux_real_target, + GRUB_LINUX_SETUP_MOVE_SIZE); + if (err) + return err; + grub_linux_real_chunk = get_virtual_current_address (ch); + } /* Put the real mode code at the temporary address. */ grub_memmove (grub_linux_real_chunk, &lh, sizeof (lh)); @@ -301,12 +304,15 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), grub_linux_prot_target = GRUB_LINUX_BZIMAGE_ADDR; else grub_linux_prot_target = GRUB_LINUX_ZIMAGE_ADDR; - err = grub_relocator_alloc_chunk_addr (relocator, - (void **) &grub_linux_prot_chunk, - grub_linux_prot_target, - grub_linux16_prot_size); - if (err) - return err; + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_addr (relocator, &ch, + grub_linux_prot_target, + grub_linux16_prot_size); + if (err) + return err; + grub_linux_prot_chunk = get_virtual_current_address (ch); + } len = grub_linux16_prot_size; if (grub_file_read (file, grub_linux_prot_chunk, grub_linux16_prot_size) @@ -398,13 +404,17 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), size = grub_file_size (file); - err = grub_relocator_alloc_chunk_align (relocator, (void **) &initrd_chunk, - &initrd_addr, - addr_min, addr_max - size, - size, 0x1000, - GRUB_RELOCATOR_PREFERENCE_HIGH); - if (err) - return err; + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_align (relocator, &ch, + addr_min, addr_max - size, + size, 0x1000, + GRUB_RELOCATOR_PREFERENCE_HIGH); + if (err) + return err; + initrd_chunk = get_virtual_current_address (ch); + initrd_addr = get_physical_target_address (ch); + } if (grub_file_read (file, initrd_chunk, size) != size) { diff --git a/loader/i386/pc/ntldr.c b/loader/i386/pc/ntldr.c index 1368694fb..0c33a0680 100644 --- a/loader/i386/pc/ntldr.c +++ b/loader/i386/pc/ntldr.c @@ -90,10 +90,14 @@ grub_cmd_ntldr (grub_command_t cmd __attribute__ ((unused)), if (! file) goto fail; - err = grub_relocator_alloc_chunk_addr (rel, &bs, 0x7C00, - GRUB_DISK_SECTOR_SIZE); - if (err) - goto fail; + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_addr (rel, &ch, 0x7C00, + GRUB_DISK_SECTOR_SIZE); + if (err) + goto fail; + bs = get_virtual_current_address (ch); + } edx = grub_get_root_biosnumber (); dev = grub_device_open (0); @@ -112,10 +116,14 @@ grub_cmd_ntldr (grub_command_t cmd __attribute__ ((unused)), grub_device_close (dev); ntldrsize = grub_file_size (file); - err = grub_relocator_alloc_chunk_addr (rel, &ntldr, GRUB_NTLDR_SEGMENT << 4, - ntldrsize); - if (err) - goto fail; + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_addr (rel, &ch, GRUB_NTLDR_SEGMENT << 4, + ntldrsize); + if (err) + goto fail; + ntldr = get_virtual_current_address (ch); + } if (grub_file_read (file, ntldr, ntldrsize) != (grub_ssize_t) ntldrsize) diff --git a/loader/multiboot.c b/loader/multiboot.c index a3ca6266f..6c6afcee9 100644 --- a/loader/multiboot.c +++ b/loader/multiboot.c @@ -284,16 +284,20 @@ grub_cmd_module (grub_command_t cmd __attribute__ ((unused)), return grub_errno; size = grub_file_size (file); - err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, &module, - &target, - 0, (0xffffffff - size) + 1, - size, MULTIBOOT_MOD_ALIGN, - GRUB_RELOCATOR_PREFERENCE_NONE); - if (err) - { - grub_file_close (file); - return err; - } + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, &ch, + 0, (0xffffffff - size) + 1, + size, MULTIBOOT_MOD_ALIGN, + GRUB_RELOCATOR_PREFERENCE_NONE); + if (err) + { + grub_file_close (file); + return err; + } + module = get_virtual_current_address (ch); + target = (grub_addr_t) get_virtual_current_address (ch); + } err = grub_multiboot_add_module (target, size, argc - 1, argv + 1); if (err) diff --git a/loader/multiboot_elfxx.c b/loader/multiboot_elfxx.c index 561c7572c..880e93ce5 100644 --- a/loader/multiboot_elfxx.c +++ b/loader/multiboot_elfxx.c @@ -92,14 +92,18 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, void *buffer) grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx, memsz=0x%lx, vaddr=0x%lx\n", i, (long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz, (long) phdr(i)->p_vaddr); - err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator, - &source, phdr(i)->p_paddr, - phdr(i)->p_memsz); - if (err) - { - grub_dprintf ("multiboot_loader", "Error loading phdr %d\n", i); - return err; - } + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator, + &ch, phdr(i)->p_paddr, + phdr(i)->p_memsz); + if (err) + { + grub_dprintf ("multiboot_loader", "Error loading phdr %d\n", i); + return err; + } + source = get_virtual_current_address (ch); + } if (grub_file_seek (file, (grub_off_t) phdr(i)->p_offset) == (grub_off_t) -1) @@ -163,18 +167,22 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, void *buffer) if (sh->sh_size == 0) continue; - err - = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, - &src, &target, 0, - (0xffffffff - sh->sh_size) + 1, - sh->sh_size, - sh->sh_addralign, - GRUB_RELOCATOR_PREFERENCE_NONE); - if (err) - { - grub_dprintf ("multiboot_loader", "Error loading shdr %d\n", i); - return err; - } + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, + &ch, 0, + (0xffffffff - sh->sh_size) + + 1, sh->sh_size, + sh->sh_addralign, + GRUB_RELOCATOR_PREFERENCE_NONE); + if (err) + { + grub_dprintf ("multiboot_loader", "Error loading shdr %d\n", i); + return err; + } + src = get_virtual_current_address (ch); + target = get_physical_target_address (ch); + } if (grub_file_seek (file, sh->sh_offset) == (grub_off_t) -1) return grub_error (GRUB_ERR_BAD_OS, diff --git a/loader/multiboot_mbi2.c b/loader/multiboot_mbi2.c index c5929f10f..5f4a52800 100644 --- a/loader/multiboot_mbi2.c +++ b/loader/multiboot_mbi2.c @@ -191,6 +191,7 @@ grub_multiboot_load (grub_file_t file) addr_tag->load_end_addr - addr_tag->load_addr); grub_size_t code_size; void *source; + grub_relocator_chunk_t ch; if (addr_tag->bss_end_addr) code_size = (addr_tag->bss_end_addr - addr_tag->load_addr); @@ -198,7 +199,7 @@ grub_multiboot_load (grub_file_t file) code_size = load_size; err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator, - &source, addr_tag->load_addr, + &ch, addr_tag->load_addr, code_size); if (err) { @@ -206,6 +207,7 @@ grub_multiboot_load (grub_file_t file) grub_free (buffer); return err; } + source = get_virtual_current_address (ch); if ((grub_file_seek (file, offset)) == (grub_off_t) -1) { @@ -460,19 +462,19 @@ grub_multiboot_make_mbi (grub_uint32_t *target) grub_uint8_t *mbistart; grub_err_t err; grub_size_t bufsize; - grub_addr_t ptrdest; + grub_relocator_chunk_t ch; bufsize = grub_multiboot_get_mbi_size (); - err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, - (void **) &ptrorig, &ptrdest, + err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, &ch, 0, 0xffffffff - bufsize, bufsize, 4, GRUB_RELOCATOR_PREFERENCE_NONE); if (err) return err; - *target = ptrdest; + ptrorig = get_virtual_current_address (ch); + *target = get_physical_target_address (ch); mbistart = ptrorig; ptrorig += 2 * sizeof (grub_uint32_t); diff --git a/loader/xnu.c b/loader/xnu.c index f1d372b73..17f850018 100644 --- a/loader/xnu.c +++ b/loader/xnu.c @@ -51,13 +51,15 @@ grub_err_t grub_xnu_heap_malloc (int size, void **src, grub_addr_t *target) { grub_err_t err; + grub_relocator_chunk_t ch; - err = grub_relocator_alloc_chunk_addr (grub_xnu_relocator, src, + err = grub_relocator_alloc_chunk_addr (grub_xnu_relocator, &ch, grub_xnu_heap_target_start + grub_xnu_heap_size, size); if (err) return err; + *src = get_virtual_current_address (ch); *target = grub_xnu_heap_target_start + grub_xnu_heap_size; grub_xnu_heap_size += size; grub_dprintf ("xnu", "val=%p\n", *src); diff --git a/loader/xnu_resume.c b/loader/xnu_resume.c index 2d47df601..6aebc1f34 100644 --- a/loader/xnu_resume.c +++ b/loader/xnu_resume.c @@ -103,25 +103,33 @@ grub_xnu_resume (char *imagename) return grub_errno; } - err = grub_relocator_alloc_chunk_addr (grub_xnu_relocator, &code, - codedest, codesize + GRUB_XNU_PAGESIZE); - if (err) - { - grub_file_close (file); - return err; - } + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_addr (grub_xnu_relocator, &ch, codedest, + codesize + GRUB_XNU_PAGESIZE); + if (err) + { + grub_file_close (file); + return err; + } + code = get_virtual_current_address (ch); + } - err = grub_relocator_alloc_chunk_align (grub_xnu_relocator, &image, - &target_image, 0, - (0xffffffff - hibhead.image_size) + 1, - hibhead.image_size, - GRUB_XNU_PAGESIZE, - GRUB_RELOCATOR_PREFERENCE_NONE); - if (err) - { - grub_file_close (file); - return err; - } + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_align (grub_xnu_relocator, &ch, 0, + (0xffffffff - hibhead.image_size) + 1, + hibhead.image_size, + GRUB_XNU_PAGESIZE, + GRUB_RELOCATOR_PREFERENCE_NONE); + if (err) + { + grub_file_close (file); + return err; + } + image = get_virtual_current_address (ch); + target_image = get_physical_target_address (ch); + } /* Read code part. */ if (grub_file_seek (file, total_header_size) == (grub_off_t) -1 From 97b2dc70b47501e00f0b6ff3f49d606277d3b8c3 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Thu, 22 Apr 2010 02:43:24 +0200 Subject: [PATCH 099/178] Fix segv in reloc.c --- lib/relocator.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/relocator.c b/lib/relocator.c index e43463e4d..eac290c97 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -957,7 +957,8 @@ malloc_in_range (struct grub_relocator *rel, ne->end = alloc_end; ne->next = extra_blocks; ne->prev = &extra_blocks; - extra_blocks->prev = &(ne->next); + if (extra_blocks) + extra_blocks->prev = &(ne->next); extra_blocks = ne; curschu->extra = ne; } From a51df0a1b4dcc32a2a023c504352458a7d292b14 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Thu, 22 Apr 2010 02:44:45 +0200 Subject: [PATCH 100/178] Adjust mips relocator --- include/grub/mips/yeeloong/memory.h | 22 +++++++++++++++++++++- lib/mips/relocator.c | 16 ++++++++++------ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/include/grub/mips/yeeloong/memory.h b/include/grub/mips/yeeloong/memory.h index 922db2404..e7e995283 100644 --- a/include/grub/mips/yeeloong/memory.h +++ b/include/grub/mips/yeeloong/memory.h @@ -31,7 +31,6 @@ #define GRUB_ARCH_LOWMEMMAXSIZE 0x10000000 #define GRUB_ARCH_HIGHMEMPSTART 0x10000000 - #define GRUB_MACHINE_MEMORY_AVAILABLE 1 #define GRUB_MACHINE_MEMORY_MAX_TYPE 1 /* This one is special: it's used internally but is never reported @@ -40,6 +39,27 @@ #define GRUB_MACHINE_MEMORY_RESERVED GRUB_MACHINE_MEMORY_HOLE #ifndef ASM_FILE + +typedef grub_addr_t grub_phys_addr_t; + +static inline grub_phys_addr_t +grub_vtop (void *a) +{ + return ((grub_phys_addr_t) a) & 0x1fffffff; +} + +static inline void * +grub_map_memory (grub_phys_addr_t a, grub_size_t size __attribute__ ((unused))) +{ + return (void *) (a | 0x80000000); +} + +static inline void +grub_unmap_memory (void *a __attribute__ ((unused)), + grub_size_t size __attribute__ ((unused))) +{ +} + grub_err_t EXPORT_FUNC (grub_machine_mmap_iterate) (int NESTED_FUNC_ATTR (*hook) (grub_uint64_t, grub_uint64_t, grub_uint32_t)); diff --git a/lib/mips/relocator.c b/lib/mips/relocator.c index 410b68b8b..537b0af2c 100644 --- a/lib/mips/relocator.c +++ b/lib/mips/relocator.c @@ -111,15 +111,16 @@ grub_err_t grub_relocator32_boot (struct grub_relocator *rel, struct grub_relocator32_state state) { - grub_addr_t target; - void *src, *ptr; + grub_relocator_chunk_t ch; + void *ptr; grub_err_t err; - grub_addr_t relst; + void *relst; grub_size_t relsize; grub_size_t stateset_size = 31 * REGW_SIZEOF + JUMP_SIZEOF; unsigned i; + grub_addr_t vtarget; - err = grub_relocator_alloc_chunk_align (rel, &src, &target, 0, + err = grub_relocator_alloc_chunk_align (rel, &ch, 0, (0xffffffff - stateset_size) + 1, stateset_size, sizeof (grub_uint32_t), @@ -127,12 +128,15 @@ grub_relocator32_boot (struct grub_relocator *rel, if (err) return err; - ptr = src; + ptr = get_virtual_current_address (ch); for (i = 1; i < 32; i++) write_reg (i, state.gpr[i], &ptr); write_jump (state.jumpreg, &ptr); - err = grub_relocator_prepare_relocs (rel, target, &relst, &relsize); + vtarget = (grub_addr_t) grub_map_memory (get_physical_target_address (ch), + stateset_size); + + err = grub_relocator_prepare_relocs (rel, vtarget, &relst, &relsize); if (err) return err; From 530a4814cc0b7ac0ceca90c81ddfcd5eb590c5d3 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Thu, 22 Apr 2010 02:45:06 +0200 Subject: [PATCH 101/178] Adjust mips/linux.c --- loader/mips/linux.c | 60 +++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/loader/mips/linux.c b/loader/mips/linux.c index 4c59e31d0..a3569c34c 100644 --- a/loader/mips/linux.c +++ b/loader/mips/linux.c @@ -33,9 +33,6 @@ #include #include -#define ELF32_LOADMASK (0x00000000UL) -#define ELF64_LOADMASK (0x0000000000000000ULL) - static grub_dl_t my_mod; static int loaded; @@ -85,7 +82,7 @@ grub_linux_load32 (grub_elf_t elf, void **extra_mem, grub_size_t extra_size) grub_err_t err; /* Linux's entry point incorrectly contains a virtual address. */ - entry_addr = elf->ehdr.ehdr32.e_entry & ~ELF32_LOADMASK; + entry_addr = elf->ehdr.ehdr32.e_entry; linux_size = grub_elf32_size (elf, &base); if (linux_size == 0) @@ -101,10 +98,15 @@ grub_linux_load32 (grub_elf_t elf, void **extra_mem, grub_size_t extra_size) if (!relocator) return grub_errno; - err = grub_relocator_alloc_chunk_addr (relocator, (void **) &playground, - target_addr, linux_size); - if (err) - return err; + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_addr (relocator, &ch, + target_addr & 0x1fffffff, + linux_size); + if (err) + return err; + playground = get_virtual_current_address (ch); + } *extra_mem = playground + extraoff; @@ -135,7 +137,7 @@ grub_linux_load64 (grub_elf_t elf, void **extra_mem, grub_size_t extra_size) grub_err_t err; /* Linux's entry point incorrectly contains a virtual address. */ - entry_addr = elf->ehdr.ehdr64.e_entry & ~ELF64_LOADMASK; + entry_addr = elf->ehdr.ehdr64.e_entry; linux_size = grub_elf64_size (elf, &base); if (linux_size == 0) @@ -151,10 +153,15 @@ grub_linux_load64 (grub_elf_t elf, void **extra_mem, grub_size_t extra_size) if (!relocator) return grub_errno; - err = grub_relocator_alloc_chunk_addr (relocator, (void **) &playground, - target_addr, linux_size); - if (err) - return err; + { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_addr (relocator, &ch, + target_addr & 0x1fffffff, + linux_size); + if (err) + return err; + playground = get_virtual_current_address (ch); + } *extra_mem = playground + extraoff; @@ -344,18 +351,23 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), size = grub_file_size (file); - err = grub_relocator_alloc_chunk_align (relocator, &initrd_src, - &initrd_dest, - target_addr + linux_size + 0x10000, - (0xffffffff - size) + 1, - size, 0x10000, - GRUB_RELOCATOR_PREFERENCE_NONE); + { + grub_relocator_chunk_t ch; - if (err) - { - grub_file_close (file); - return err; - } + err = grub_relocator_alloc_chunk_align (relocator, &ch, + target_addr + linux_size + 0x10000, + (0xffffffff - size) + 1, + size, 0x10000, + GRUB_RELOCATOR_PREFERENCE_NONE); + + if (err) + { + grub_file_close (file); + return err; + } + initrd_src = get_virtual_current_address (ch); + initrd_dest = get_physical_target_address (ch) | 0x80000000; + } if (grub_file_read (file, initrd_src, size) != size) { From 6adde6749e034c0d718b974e6274f5a175330853 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 27 Apr 2010 13:23:11 +0200 Subject: [PATCH 102/178] efi boottests --- conf/i386-efi.rmk | 2 ++ conf/i386-pc.rmk | 5 +++-- conf/i386.rmk | 8 ++++---- conf/x86-efi.rmk | 2 ++ conf/x86_64-efi.rmk | 2 ++ tests/util/grub-shell.in | 6 ++++-- 6 files changed, 17 insertions(+), 8 deletions(-) diff --git a/conf/i386-efi.rmk b/conf/i386-efi.rmk index e826cb333..b50c77d3c 100644 --- a/conf/i386-efi.rmk +++ b/conf/i386-efi.rmk @@ -2,4 +2,6 @@ COMMON_LDFLAGS = -melf_i386 +QEMU32=qemu-system-i386 + include $(srcdir)/conf/x86-efi.mk diff --git a/conf/i386-pc.rmk b/conf/i386-pc.rmk index 801caba28..1888c2afb 100644 --- a/conf/i386-pc.rmk +++ b/conf/i386-pc.rmk @@ -292,12 +292,13 @@ pkglib_DATA += efiemu32.o efiemu64.o endif BOOTTARGET=cd +QEMU32=qemu-system-i386 bootcheck-linux16-i386: linux-initramfs.i386 $(GRUB_PAYLOADS_DIR)/linux.i386 $(srcdir)/tests/boot/linux.cfg grub-shell - timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-i386 --files=/initrd=linux-initramfs.i386 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.i386 $(srcdir)/tests/boot/linux16.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=$(QEMU32) --files=/initrd=linux-initramfs.i386 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.i386 $(srcdir)/tests/boot/linux16.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null bootcheck-linux16-x86_64: linux-initramfs.x86_64 $(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/tests/boot/linux.cfg grub-shell - timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/initrd=linux-initramfs.x86_64 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/tests/boot/linux16.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/initrd=linux-initramfs.x86_64 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/tests/boot/linux16.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null BOOTCHECKS+=bootcheck-linux16-i386 bootcheck-linux16-x86_64 diff --git a/conf/i386.rmk b/conf/i386.rmk index a3f79dd43..9cec10a80 100644 --- a/conf/i386.rmk +++ b/conf/i386.rmk @@ -89,16 +89,16 @@ kfreebsd-mfsroot.%: kfreebsd.init.% Makefile CLEANFILES += linux.init.i386 kfreebsd.init.i386 linux.init.x86_64 linux-initramfs.i386 linux-initramfs.x86_64 bootcheck-kfreebsd-i386: kfreebsd-mfsroot.i386 $(GRUB_PAYLOADS_DIR)/kfreebsd.i386 $(GRUB_PAYLOADS_DIR)/kfreebsd_env.i386 $(srcdir)/tests/boot/kfreebsd.cfg grub-shell - timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-i386 --files=/mfsroot=kfreebsd-mfsroot.i386 --files=/kfreebsd=$(GRUB_PAYLOADS_DIR)/kfreebsd.i386 --files=/kfreebsd_env=$(GRUB_PAYLOADS_DIR)/kfreebsd_env.i386 $(srcdir)/tests/boot/kfreebsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=$(QEMU32) --files=/mfsroot=kfreebsd-mfsroot.i386 --files=/kfreebsd=$(GRUB_PAYLOADS_DIR)/kfreebsd.i386 --files=/kfreebsd_env=$(GRUB_PAYLOADS_DIR)/kfreebsd_env.i386 $(srcdir)/tests/boot/kfreebsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null bootcheck-kfreebsd-x86_64: kfreebsd-mfsroot.x86_64 $(GRUB_PAYLOADS_DIR)/kfreebsd.x86_64 $(GRUB_PAYLOADS_DIR)/kfreebsd_env.x86_64 $(srcdir)/tests/boot/kfreebsd.cfg grub-shell - timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/mfsroot=kfreebsd-mfsroot.x86_64 --files=/kfreebsd=$(GRUB_PAYLOADS_DIR)/kfreebsd.x86_64 --files=/kfreebsd_env=$(GRUB_PAYLOADS_DIR)/kfreebsd_env.x86_64 $(srcdir)/tests/boot/kfreebsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/mfsroot=kfreebsd-mfsroot.x86_64 --files=/kfreebsd=$(GRUB_PAYLOADS_DIR)/kfreebsd.x86_64 --files=/kfreebsd_env=$(GRUB_PAYLOADS_DIR)/kfreebsd_env.x86_64 $(srcdir)/tests/boot/kfreebsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null bootcheck-linux-i386: linux-initramfs.i386 $(GRUB_PAYLOADS_DIR)/linux.i386 $(srcdir)/tests/boot/linux.cfg grub-shell - timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-i386 --files=/initrd=linux-initramfs.i386 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.i386 $(srcdir)/tests/boot/linux.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=$(QEMU32) --files=/initrd=linux-initramfs.i386 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.i386 $(srcdir)/tests/boot/linux.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null bootcheck-linux-x86_64: linux-initramfs.x86_64 $(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/tests/boot/linux.cfg grub-shell - timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/initrd=linux-initramfs.x86_64 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/tests/boot/linux.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/initrd=linux-initramfs.x86_64 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/tests/boot/linux.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null BOOTCHECKS += bootcheck-linux-i386 bootcheck-linux-x86_64 \ bootcheck-kfreebsd-i386 bootcheck-kfreebsd-x86_64 diff --git a/conf/x86-efi.rmk b/conf/x86-efi.rmk index 286ba988e..61d225bc7 100644 --- a/conf/x86-efi.rmk +++ b/conf/x86-efi.rmk @@ -104,5 +104,7 @@ xnu_mod_CFLAGS = $(COMMON_CFLAGS) xnu_mod_LDFLAGS = $(COMMON_LDFLAGS) xnu_mod_ASFLAGS = $(COMMON_ASFLAGS) +BOOTTARGET=cd + include $(srcdir)/conf/i386.mk include $(srcdir)/conf/common.mk diff --git a/conf/x86_64-efi.rmk b/conf/x86_64-efi.rmk index 200621280..5ef3cc434 100644 --- a/conf/x86_64-efi.rmk +++ b/conf/x86_64-efi.rmk @@ -2,4 +2,6 @@ COMMON_LDFLAGS = -melf_x86_64 +QEMU32=qemu-system-x86_64 + include $(srcdir)/conf/x86-efi.mk diff --git a/tests/util/grub-shell.in b/tests/util/grub-shell.in index 5726ec0d8..1ff562096 100644 --- a/tests/util/grub-shell.in +++ b/tests/util/grub-shell.in @@ -145,8 +145,10 @@ if [ x$boot = xfd ]; then device=fda bootdev=a fi -${qemu} ${qemuopts} -nographic -serial file:/dev/stdout -monitor file:/dev/null -${device} ${isofile} -boot ${bootdev} | tr -d "\r" -rm -f ${isofile} + +echo ${qemu} ${qemuopts} -nographic -serial file:/dev/stdout -monitor file:/dev/null -${device} ${isofile} -boot ${bootdev} +#${qemu} ${qemuopts} -nographic -serial file:/dev/stdout -monitor file:/dev/null -${device} ${isofile} -boot ${bootdev} | tr -d "\r" +#rm -f ${isofile} rm -f ${tmpfile} ${cfgfile} exit 0 From d534028780bc8aa39231a3d10ef811322feda2b8 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 27 Apr 2010 13:45:08 +0200 Subject: [PATCH 103/178] Remove debugging leftovers --- tests/util/grub-shell.in | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/util/grub-shell.in b/tests/util/grub-shell.in index 1ff562096..d8ea588a7 100644 --- a/tests/util/grub-shell.in +++ b/tests/util/grub-shell.in @@ -146,9 +146,8 @@ if [ x$boot = xfd ]; then bootdev=a fi -echo ${qemu} ${qemuopts} -nographic -serial file:/dev/stdout -monitor file:/dev/null -${device} ${isofile} -boot ${bootdev} -#${qemu} ${qemuopts} -nographic -serial file:/dev/stdout -monitor file:/dev/null -${device} ${isofile} -boot ${bootdev} | tr -d "\r" -#rm -f ${isofile} +${qemu} ${qemuopts} -nographic -serial file:/dev/stdout -monitor file:/dev/null -${device} ${isofile} -boot ${bootdev} | tr -d "\r" +rm -f ${isofile} rm -f ${tmpfile} ${cfgfile} exit 0 From 95327fc92dbdb567fd4980f9144e324e425d32e2 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 27 Apr 2010 17:25:32 +0200 Subject: [PATCH 104/178] Fix blocker counter --- lib/relocator.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/relocator.c b/lib/relocator.c index eac290c97..4c180e72a 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -446,6 +446,8 @@ malloc_in_range (struct grub_relocator *rel, for (cur = extra_blocks; cur; cur = cur->next) maxevents += 2; } + for (r = grub_mm_base; r; r = r->next) + maxevents += 2; maxevents += grub_relocator_firmware_get_max_events (); @@ -791,6 +793,8 @@ malloc_in_range (struct grub_relocator *rel, fend = ALIGN_UP (alloc_end, GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT); + grub_dprintf ("relocator", "requesting %lx-%lx\n", + fstart, fend); /* The failure here can be very expensive. */ if (!grub_relocator_firmware_alloc_region (fstart, fend - fstart)) From dc106194612b2a60e8759aaa3a5cc887ae145a3d Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 27 Apr 2010 21:41:22 +0200 Subject: [PATCH 105/178] Add midding qemu-opts --- conf/i386.rmk | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conf/i386.rmk b/conf/i386.rmk index c18ce9699..9007b2889 100644 --- a/conf/i386.rmk +++ b/conf/i386.rmk @@ -145,10 +145,10 @@ bootcheck-kfreebsd-x86_64: kfreebsd-mfsroot.x86_64 $(GRUB_PAYLOADS_DIR)/kfreebsd timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/mfsroot=kfreebsd-mfsroot.x86_64 --files=/kfreebsd=$(GRUB_PAYLOADS_DIR)/kfreebsd.x86_64 --files=/kfreebsd_env=$(GRUB_PAYLOADS_DIR)/kfreebsd_env.x86_64 $(srcdir)/tests/boot/kfreebsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null bootcheck-knetbsd-i386: knetbsd.miniroot-image.i386 $(GRUB_PAYLOADS_DIR)/knetbsd.i386 $(srcdir)/tests/boot/knetbsd.cfg knetbsd.miniroot-image.i386 grub-shell - timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-i386 --files=/miniroot=knetbsd.miniroot-image.i386 --files=/knetbsd=$(GRUB_PAYLOADS_DIR)/knetbsd.i386 $(srcdir)/tests/boot/knetbsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-i386 --files=/miniroot=knetbsd.miniroot-image.i386 --files=/knetbsd=$(GRUB_PAYLOADS_DIR)/knetbsd.i386 $(srcdir)/tests/boot/knetbsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null bootcheck-knetbsd-x86_64: knetbsd.miniroot-image.x86_64 $(GRUB_PAYLOADS_DIR)/knetbsd.x86_64 $(srcdir)/tests/boot/knetbsd.cfg knetbsd.miniroot-image.x86_64 grub-shell - timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/miniroot=knetbsd.miniroot-image.x86_64 --files=/knetbsd=$(GRUB_PAYLOADS_DIR)/knetbsd.x86_64 $(srcdir)/tests/boot/knetbsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/miniroot=knetbsd.miniroot-image.x86_64 --files=/knetbsd=$(GRUB_PAYLOADS_DIR)/knetbsd.x86_64 $(srcdir)/tests/boot/knetbsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null bootcheck-linux-i386: linux-initramfs.i386 $(GRUB_PAYLOADS_DIR)/linux.i386 $(srcdir)/tests/boot/linux.cfg grub-shell timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=$(QEMU32) --files=/initrd=linux-initramfs.i386 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.i386 $(srcdir)/tests/boot/linux.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null From 708745c8a74cbb7aa004fa400cb710e578cf0b8e Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 27 Apr 2010 21:42:15 +0200 Subject: [PATCH 106/178] Shutdown manually --- tests/boot/kfreebsd.init-i386.S | 42 ++++++++++++++++++++++++++++++- tests/boot/kfreebsd.init-x86_64.S | 40 +++++++++++++++++++++++++++-- tests/boot/knetbsd.init-x86_64.S | 31 ++++++++++++++++++++++- tests/boot/linux.init-i386.S | 25 ++++++++++++++++++ 4 files changed, 134 insertions(+), 4 deletions(-) diff --git a/tests/boot/kfreebsd.init-i386.S b/tests/boot/kfreebsd.init-i386.S index 8812b650b..12c94a036 100644 --- a/tests/boot/kfreebsd.init-i386.S +++ b/tests/boot/kfreebsd.init-i386.S @@ -21,12 +21,16 @@ #define SYSCALL_OPEN 5 #define SYSCALL_WRITE 4 #define SYSCALL_RESET 55 +#define SYSCALL_FSYNC 95 +#define SYSCALL_ARCH 165 #define SYSCALL_EXIT 1 +#define SYSCALL_ARCH_IOPL 4 #define SYSCALL_INT 0x80 #define RESET_NOSYNC 0x4 #define RESET_HALT 0x8 #define RESET_POWEROFF 0x4000 +#define SHUTDOWN_PORT 0x8900 .section ".init", "ax" .global start,_start @@ -52,6 +56,39 @@ _start: pushl $0 int $SYSCALL_INT addl $16, %esp + + /* fsync. */ + movl $SYSCALL_FSYNC, %eax + pushl %ecx + pushl $0 + int $SYSCALL_INT + addl $8, %esp + + /* IOPL. */ + movl $SYSCALL_ARCH, %eax + pushl $iopl_arg + pushl $SYSCALL_ARCH_IOPL + pushl $0 + int $SYSCALL_INT + addl $12, %esp + + movw $SHUTDOWN_PORT, %dx + movb $'S', %al + outb %al, %dx + movb $'h', %al + outb %al, %dx + movb $'u', %al + outb %al, %dx + movb $'t', %al + outb %al, %dx + movb $'d', %al + outb %al, %dx + movb $'o', %al + outb %al, %dx + movb $'w', %al + outb %al, %dx + movb $'n', %al + outb %al, %dx /* shutdown. */ movl $SYSCALL_RESET, %eax @@ -71,4 +108,7 @@ device: message: .ascii "Boot Test Passed Successfully\n" SUCCESSFUL_BOOT_STRING "\n" messageend: - \ No newline at end of file +iopl_arg: + .long SHUTDOWN_PORT + .long 1 + .long 1 diff --git a/tests/boot/kfreebsd.init-x86_64.S b/tests/boot/kfreebsd.init-x86_64.S index edff0d782..d2907b3a8 100644 --- a/tests/boot/kfreebsd.init-x86_64.S +++ b/tests/boot/kfreebsd.init-x86_64.S @@ -18,14 +18,18 @@ #define MODE_RDRW 2 #define FLAGS_NONE 0 +#define SYSCALL_ARCH 165 #define SYSCALL_OPEN 5 #define SYSCALL_WRITE 4 #define SYSCALL_RESET 55 #define SYSCALL_EXIT 1 +#define SYSCALL_ARCH_IOPL 4 +#define SYSCALL_FSYNC 95 #define RESET_NOSYNC 0x4 #define RESET_HALT 0x8 #define RESET_POWEROFF 0x4000 +#define SHUTDOWN_PORT 0x8900 .section ".init", "ax" .global start,_start @@ -40,10 +44,38 @@ _start: movq %rax, %rdi /* write. */ - movq $SYSCALL_WRITE, %rax leaq message, %rsi - movq $(messageend-message), %rdx + movq $SYSCALL_WRITE, %rax + movq $(messageend - message), %rdx syscall + + /* fsync. */ + movq $SYSCALL_FSYNC, %rax + syscall + + /* IOPL. */ + movq $SYSCALL_ARCH, %rax + movq $SYSCALL_ARCH_IOPL, %rdi + leaq iopl_arg, %rsi + syscall + + movw $SHUTDOWN_PORT, %dx + movb $'S', %al + outb %al, %dx + movb $'h', %al + outb %al, %dx + movb $'u', %al + outb %al, %dx + movb $'t', %al + outb %al, %dx + movb $'d', %al + outb %al, %dx + movb $'o', %al + outb %al, %dx + movb $'w', %al + outb %al, %dx + movb $'n', %al + outb %al, %dx /* shutdown. */ movq $SYSCALL_RESET, %rax @@ -60,3 +92,7 @@ device: message: .ascii "Boot Test Passed Successfully\n" SUCCESSFUL_BOOT_STRING "\n" messageend: +iopl_arg: + .long SHUTDOWN_PORT + .long 1 + .long 1 \ No newline at end of file diff --git a/tests/boot/knetbsd.init-x86_64.S b/tests/boot/knetbsd.init-x86_64.S index dfc64e99d..05a494594 100644 --- a/tests/boot/knetbsd.init-x86_64.S +++ b/tests/boot/knetbsd.init-x86_64.S @@ -23,12 +23,15 @@ #define SYSCALL_RESET 208 #define SYSCALL_EXIT 1 #define SYSCALL_MKNOD 14 +#define SYSCALL_ARCH 165 #define SYSCALL_MOUNT 410 #define SYSCALL_INT 0x80 +#define SYSCALL_ARCH_IOPL 2 #define RESET_NOSYNC 0x4 #define RESET_HALT 0x8 #define RESET_POWEROFF 0x800 +#define SHUTDOWN_PORT 0x8900 .section ".init", "ax" .global start,_start @@ -64,6 +67,30 @@ _start: leaq message, %rsi syscall + /* IOPL. */ + movq $SYSCALL_ARCH, %rax + movq $SYSCALL_ARCH_IOPL, %rdi + leaq iopl_arg, %rsi + syscall + + movw $SHUTDOWN_PORT, %dx + movb $'S', %al + outb %al, %dx + movb $'h', %al + outb %al, %dx + movb $'u', %al + outb %al, %dx + movb $'t', %al + outb %al, %dx + movb $'d', %al + outb %al, %dx + movb $'o', %al + outb %al, %dx + movb $'w', %al + outb %al, %dx + movb $'n', %al + outb %al, %dx + /* shutdown. */ movq $SYSCALL_RESET, %rax movq $(RESET_POWEROFF|RESET_HALT|RESET_NOSYNC), %rdi @@ -118,4 +145,6 @@ tmpfs_args: .long 0777 /* Alignment long. */ .long 0 -tmpfs_args_end: \ No newline at end of file +tmpfs_args_end: +iopl_arg: + .long 3 \ No newline at end of file diff --git a/tests/boot/linux.init-i386.S b/tests/boot/linux.init-i386.S index a79a5787e..f3eca6d88 100644 --- a/tests/boot/linux.init-i386.S +++ b/tests/boot/linux.init-i386.S @@ -18,6 +18,7 @@ #define SYSCALL_WRITE 4 #define SYSCALL_RESET 88 +#define SYSCALL_IOPL 110 #define SYSCALL_EXIT 1 #define SYSCALL_INT 0x80 @@ -26,6 +27,8 @@ #define SHUTDOWN_MAGIC2 0x28121969 #define SHUTDOWN_MAGIC3 0x4321fedc +#define SHUTDOWN_PORT 0x8900 + .text .global start, _start _start: @@ -37,6 +40,28 @@ start: movl $(messageend-message), %edx int $SYSCALL_INT + movl $SYSCALL_IOPL, %eax + movl $3, %ebx + int $SYSCALL_INT + + movw $SHUTDOWN_PORT, %dx + movb $'S', %al + outb %al, %dx + movb $'h', %al + outb %al, %dx + movb $'u', %al + outb %al, %dx + movb $'t', %al + outb %al, %dx + movb $'d', %al + outb %al, %dx + movb $'o', %al + outb %al, %dx + movb $'w', %al + outb %al, %dx + movb $'n', %al + outb %al, %dx + /* shutdown. */ movl $SYSCALL_RESET, %eax movl $SHUTDOWN_MAGIC1, %ebx From e7b43a6550fa6d46f9189e6758ec51d56b48ddf2 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 27 Apr 2010 21:42:45 +0200 Subject: [PATCH 107/178] Use hasbrokenint12 --- tests/boot/kfreebsd.cfg | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/boot/kfreebsd.cfg b/tests/boot/kfreebsd.cfg index 71b97b67e..8f339cd7f 100644 --- a/tests/boot/kfreebsd.cfg +++ b/tests/boot/kfreebsd.cfg @@ -1,6 +1,7 @@ kfreebsd /kfreebsd -h kfreebsd_loadenv /kfreebsd_env kfreebsd_module /mfsroot type=mfs_root +set kFreeBSD.hw.hasbrokenint12=1 boot # Shouln't happen halt From dce9e78a4f65fa009465cba9b9bceee8a85f720b Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 28 Apr 2010 09:40:50 +0200 Subject: [PATCH 108/178] Increase bootcheck timeout --- conf/common.rmk | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/conf/common.rmk b/conf/common.rmk index 0a622ca04..e1d148987 100644 --- a/conf/common.rmk +++ b/conf/common.rmk @@ -865,7 +865,8 @@ grub_mkpasswd_pbkdf2_CFLAGS += -Wno-missing-field-initializers -Wno-error -I$(sr # Randomly generated SUCCESSFUL_BOOT_STRING=3e49994fd5d82b7c9298d672d774080d -BOOTCHECK_TIMEOUT=60 +# tianocore cd access is very slow +BOOTCHECK_TIMEOUT=600 bootcheck: $(BOOTCHECKS) From 3af6010ff71a3c9f1b9a11bb67991dc55fba9a12 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 28 Apr 2010 09:41:34 +0200 Subject: [PATCH 109/178] Disable kfreebsd-x86_64 and knetbsd-i386 bootchecks on non-pc --- conf/i386-pc.rmk | 6 +++++- conf/i386.rmk | 3 +-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/conf/i386-pc.rmk b/conf/i386-pc.rmk index 95a996c75..66d2447ad 100644 --- a/conf/i386-pc.rmk +++ b/conf/i386-pc.rmk @@ -286,7 +286,11 @@ bootcheck-linux16-i386: linux-initramfs.i386 $(GRUB_PAYLOADS_DIR)/linux.i386 $(s bootcheck-linux16-x86_64: linux-initramfs.x86_64 $(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/tests/boot/linux.cfg grub-shell timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/initrd=linux-initramfs.x86_64 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/tests/boot/linux16.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null -BOOTCHECKS+=bootcheck-linux16-i386 bootcheck-linux16-x86_64 +BOOTCHECKS += bootcheck-linux16-i386 bootcheck-linux16-x86_64 +# It is defined in i386.rmk but requires ACPI +BOOTCHECKS += bootcheck-kfreebsd-x86_64 +# It is defined in i386.rmk but crashes early on non-BIOS +BOOTCHECKS += bootcheck-knetbsd-i386 include $(srcdir)/conf/i386.mk include $(srcdir)/conf/common.mk diff --git a/conf/i386.rmk b/conf/i386.rmk index 9007b2889..97becfc7b 100644 --- a/conf/i386.rmk +++ b/conf/i386.rmk @@ -157,8 +157,7 @@ bootcheck-linux-x86_64: linux-initramfs.x86_64 $(GRUB_PAYLOADS_DIR)/linux.x86_64 timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/initrd=linux-initramfs.x86_64 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/tests/boot/linux.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null BOOTCHECKS += bootcheck-linux-i386 bootcheck-linux-x86_64 \ - bootcheck-kfreebsd-i386 bootcheck-kfreebsd-x86_64 \ - bootcheck-knetbsd-i386 bootcheck-knetbsd-x86_64 + bootcheck-kfreebsd-i386 bootcheck-knetbsd-x86_64 .PHONY: bootcheck-linux-i386 bootcheck-linux-x86_64 \ bootcheck-kfreebsd-i386 bootcheck-kfreebsd-x86_64 \ From ae3c4cd010ff4aba666415a9804169b7ad0aeb84 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Thu, 29 Apr 2010 13:26:38 +0200 Subject: [PATCH 110/178] Remove loader.h --- conf/common.rmk | 2 +- conf/i386-pc.rmk | 2 +- include/grub/i386/coreboot/loader.h | 1 - include/grub/i386/efi/loader.h | 22 ------------------ include/grub/i386/ieee1275/loader.h | 29 ----------------------- include/grub/mips/yeeloong/loader.h | 0 include/grub/powerpc/ieee1275/loader.h | 32 -------------------------- include/grub/sparc64/ieee1275/loader.h | 27 ---------------------- 8 files changed, 2 insertions(+), 113 deletions(-) delete mode 100644 include/grub/i386/coreboot/loader.h delete mode 100644 include/grub/i386/efi/loader.h delete mode 100644 include/grub/i386/ieee1275/loader.h delete mode 100644 include/grub/mips/yeeloong/loader.h delete mode 100644 include/grub/powerpc/ieee1275/loader.h delete mode 100644 include/grub/sparc64/ieee1275/loader.h diff --git a/conf/common.rmk b/conf/common.rmk index e1d148987..a8c881276 100644 --- a/conf/common.rmk +++ b/conf/common.rmk @@ -118,7 +118,7 @@ kernel_img_HEADERS += boot.h cache.h device.h disk.h dl.h elf.h elfload.h \ list.h handler.h command.h i18n.h env_private.h libgcc.h mm_private.h ifneq ($(platform), emu) -kernel_img_HEADERS += machine/memory.h machine/loader.h +kernel_img_HEADERS += machine/memory.h endif symlist.c: $(addprefix include/grub/,$(kernel_img_HEADERS)) config.h gensymlist.sh diff --git a/conf/i386-pc.rmk b/conf/i386-pc.rmk index 66d2447ad..d403d19f1 100644 --- a/conf/i386-pc.rmk +++ b/conf/i386-pc.rmk @@ -53,7 +53,7 @@ kernel_img_SOURCES = kern/i386/pc/startup.S \ term/i386/pc/console.c term/i386/vga_common.c \ symlist.c kernel_img_HEADERS += machine/biosdisk.h machine/vga.h machine/vbe.h \ - machine/pxe.h i386/pit.h machine/kernel.h + machine/pxe.h i386/pit.h machine/kernel.h machine/loader.h kernel_img_CFLAGS = $(COMMON_CFLAGS) $(TARGET_IMG_CFLAGS) kernel_img_ASFLAGS = $(COMMON_ASFLAGS) kernel_img_LDFLAGS += $(COMMON_LDFLAGS) $(TARGET_IMG_LDFLAGS)0x8200 $(COMMON_CFLAGS) diff --git a/include/grub/i386/coreboot/loader.h b/include/grub/i386/coreboot/loader.h deleted file mode 100644 index d3f36bba5..000000000 --- a/include/grub/i386/coreboot/loader.h +++ /dev/null @@ -1 +0,0 @@ -#include diff --git a/include/grub/i386/efi/loader.h b/include/grub/i386/efi/loader.h deleted file mode 100644 index 222dae82d..000000000 --- a/include/grub/i386/efi/loader.h +++ /dev/null @@ -1,22 +0,0 @@ -/* - * GRUB -- GRand Unified Bootloader - * Copyright (C) 2002,2003,2004,2006,2007 Free Software Foundation, Inc. - * - * GRUB 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. - * - * GRUB 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 GRUB. If not, see . - */ - -#ifndef GRUB_LOADER_MACHINE_HEADER -#define GRUB_LOADER_MACHINE_HEADER 1 - -#endif /* ! GRUB_LOADER_MACHINE_HEADER */ diff --git a/include/grub/i386/ieee1275/loader.h b/include/grub/i386/ieee1275/loader.h deleted file mode 100644 index 20df2e1d6..000000000 --- a/include/grub/i386/ieee1275/loader.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * GRUB -- GRand Unified Bootloader - * Copyright (C) 2002,2003,2004,2007,2008 Free Software Foundation, Inc. - * - * GRUB 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. - * - * GRUB 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 GRUB. If not, see . - */ - -#ifndef GRUB_LOADER_MACHINE_HEADER -#define GRUB_LOADER_MACHINE_HEADER 1 - -#include -#include -#include - -void grub_rescue_cmd_linux (int argc, char *argv[]); -void grub_rescue_cmd_initrd (int argc, char *argv[]); - -#endif /* ! GRUB_LOADER_MACHINE_HEADER */ diff --git a/include/grub/mips/yeeloong/loader.h b/include/grub/mips/yeeloong/loader.h deleted file mode 100644 index e69de29bb..000000000 diff --git a/include/grub/powerpc/ieee1275/loader.h b/include/grub/powerpc/ieee1275/loader.h deleted file mode 100644 index 606bfcd0b..000000000 --- a/include/grub/powerpc/ieee1275/loader.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * GRUB -- GRand Unified Bootloader - * Copyright (C) 2004,2007 Free Software Foundation, Inc. - * - * GRUB 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. - * - * GRUB 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 GRUB. If not, see . - */ - -#ifndef GRUB_LOADER_MACHINE_HEADER -#define GRUB_LOADER_MACHINE_HEADER 1 - -/* The symbol shared between the normal mode and rescue mode - loader. */ -void grub_rescue_cmd_linux (int argc, char *argv[]); -void grub_rescue_cmd_initrd (int argc, char *argv[]); - -void grub_linux_init (void); -void grub_linux_fini (void); -void grub_linux_normal_init (void); -void grub_linux_normal_fini (void); - -#endif /* ! GRUB_LOADER_MACHINE_HEADER */ diff --git a/include/grub/sparc64/ieee1275/loader.h b/include/grub/sparc64/ieee1275/loader.h deleted file mode 100644 index 12bb2a69b..000000000 --- a/include/grub/sparc64/ieee1275/loader.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - * GRUB -- GRand Unified Bootloader - * Copyright (C) 2009 Free Software Foundation, Inc. - * - * GRUB 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. - * - * GRUB 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 GRUB. If not, see . - */ - -#ifndef GRUB_LOADER_MACHINE_HEADER -#define GRUB_LOADER_MACHINE_HEADER 1 - -/* The symbol shared between the normal mode and rescue mode - loader. */ -void grub_rescue_cmd_linux (int argc, char *argv[]); -void grub_rescue_cmd_initrd (int argc, char *argv[]); - -#endif /* ! GRUB_LOADER_MACHINE_HEADER */ From 3bd6968e566d70d763dd4b0623849bab12a01120 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Thu, 29 Apr 2010 13:28:46 +0200 Subject: [PATCH 111/178] fix i386-qemu building problems --- conf/i386-qemu.rmk | 19 +------------------ conf/i386.rmk | 2 ++ 2 files changed, 3 insertions(+), 18 deletions(-) diff --git a/conf/i386-qemu.rmk b/conf/i386-qemu.rmk index ff263245d..6d387a6f6 100644 --- a/conf/i386-qemu.rmk +++ b/conf/i386-qemu.rmk @@ -43,7 +43,7 @@ bin_SCRIPTS += grub-mkrescue grub_mkrescue_SOURCES = util/grub-mkrescue.in # Modules. -pkglib_MODULES = linux.mod aout.mod halt.mod datetime.mod mmap.mod +pkglib_MODULES = halt.mod datetime.mod mmap.mod # For mmap.mod. mmap_mod_SOURCES = mmap/mmap.c mmap/i386/uppermem.c mmap/i386/mmap.c @@ -51,28 +51,11 @@ mmap_mod_CFLAGS = $(COMMON_CFLAGS) mmap_mod_LDFLAGS = $(COMMON_LDFLAGS) mmap_mod_ASFLAGS = $(COMMON_ASFLAGS) -# For linux.mod. -linux_mod_SOURCES = loader/i386/linux.c -linux_mod_CFLAGS = $(COMMON_CFLAGS) -linux_mod_LDFLAGS = $(COMMON_LDFLAGS) - # For halt.mod. halt_mod_SOURCES = commands/halt.c halt_mod_CFLAGS = $(COMMON_CFLAGS) halt_mod_LDFLAGS = $(COMMON_LDFLAGS) -# For aout.mod. -aout_mod_SOURCES = loader/aout.c -aout_mod_CFLAGS = $(COMMON_CFLAGS) -aout_mod_LDFLAGS = $(COMMON_LDFLAGS) - -# For bsd.mod -pkglib_MODULES += bsd.mod -bsd_mod_SOURCES = loader/i386/bsd.c loader/i386/bsd32.c loader/i386/bsd64.c loader/i386/bsd_helper.S loader/i386/bsd_trampoline.S -bsd_mod_CFLAGS = $(COMMON_CFLAGS) -bsd_mod_LDFLAGS = $(COMMON_LDFLAGS) -bsd_mod_ASFLAGS = $(COMMON_ASFLAGS) - # For datetime.mod datetime_mod_SOURCES = lib/cmos_datetime.c datetime_mod_CFLAGS = $(COMMON_CFLAGS) diff --git a/conf/i386.rmk b/conf/i386.rmk index 97becfc7b..004dab7f3 100644 --- a/conf/i386.rmk +++ b/conf/i386.rmk @@ -10,10 +10,12 @@ at_keyboard_mod_SOURCES = term/at_keyboard.c at_keyboard_mod_CFLAGS = $(COMMON_CFLAGS) at_keyboard_mod_LDFLAGS = $(COMMON_LDFLAGS) +ifneq ($(platform), qemu) pkglib_MODULES += vga_text.mod vga_text_mod_SOURCES = term/i386/pc/vga_text.c term/i386/vga_common.c vga_text_mod_CFLAGS = $(COMMON_CFLAGS) vga_text_mod_LDFLAGS = $(COMMON_LDFLAGS) +endif pkglib_MODULES += relocator.mod ifeq ($(platform), ieee1275) From 6406a79dffb55949deb854ef449d5e70c7f632c5 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Thu, 29 Apr 2010 13:36:53 +0200 Subject: [PATCH 112/178] switch off manually on linux x86_64 --- tests/boot/linux.init-x86_64.S | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/boot/linux.init-x86_64.S b/tests/boot/linux.init-x86_64.S index 17ba8040c..63dca5e20 100644 --- a/tests/boot/linux.init-x86_64.S +++ b/tests/boot/linux.init-x86_64.S @@ -18,6 +18,7 @@ #define SYSCALL_WRITE 1 #define SYSCALL_RESET 169 +#define SYSCALL_IOPL 172 #define SYSCALL_EXIT 60 #define STDOUT 1 @@ -25,6 +26,8 @@ #define SHUTDOWN_MAGIC2 0x28121969 #define SHUTDOWN_MAGIC3 0x4321fedc +#define SHUTDOWN_PORT 0x8900 + .text .global start, _start _start: @@ -36,6 +39,28 @@ start: movq $(messageend-message), %rdx syscall + movq $SYSCALL_IOPL, %rax + movq $3, %rdi + syscall + + movw $SHUTDOWN_PORT, %dx + movb $'S', %al + outb %al, %dx + movb $'h', %al + outb %al, %dx + movb $'u', %al + outb %al, %dx + movb $'t', %al + outb %al, %dx + movb $'d', %al + outb %al, %dx + movb $'o', %al + outb %al, %dx + movb $'w', %al + outb %al, %dx + movb $'n', %al + outb %al, %dx + /* shutdown. */ movq $SYSCALL_RESET, %rax movq $SHUTDOWN_MAGIC1, %rdi From 421e8a55915429de1388c01467923ab2a3efeab3 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 1 May 2010 13:23:19 +0200 Subject: [PATCH 113/178] Fix ppc compilation problems --- conf/powerpc-ieee1275.rmk | 2 +- conf/tests.rmk | 2 +- include/grub/powerpc/memory.h | 47 ++++++++ include/grub/relocator_private.h | 10 ++ lib/powerpc/relocator.c | 13 ++- lib/relocator.c | 188 ++++++++++++++++++------------- loader/powerpc/ieee1275/linux.c | 1 - 7 files changed, 175 insertions(+), 88 deletions(-) create mode 100644 include/grub/powerpc/memory.h diff --git a/conf/powerpc-ieee1275.rmk b/conf/powerpc-ieee1275.rmk index 933aefe77..86ec85bfc 100644 --- a/conf/powerpc-ieee1275.rmk +++ b/conf/powerpc-ieee1275.rmk @@ -67,7 +67,7 @@ datetime_mod_LDFLAGS = $(COMMON_LDFLAGS) # For relocator.mod. pkglib_MODULES += relocator.mod -relocator_mod_SOURCES = lib/$(target_cpu)/relocator.c lib/relocator.c lib/$(target_cpu)/relocator_asm.S +relocator_mod_SOURCES = lib/$(target_cpu)/relocator.c lib/relocator.c lib/$(target_cpu)/relocator_asm.S lib/ieee1275/relocator.c relocator_mod_CFLAGS = $(COMMON_CFLAGS) relocator_mod_ASFLAGS = $(COMMON_ASFLAGS) relocator_mod_LDFLAGS = $(COMMON_LDFLAGS) diff --git a/conf/tests.rmk b/conf/tests.rmk index d48bc3dd9..e86c315d3 100644 --- a/conf/tests.rmk +++ b/conf/tests.rmk @@ -2,7 +2,7 @@ # For grub-shell grub-shell: tests/util/grub-shell.in config.status - ./config.status --file=$@:$< + ./config.status --file=-:$< | sed -e 's,@pkglib_DATA@,$(pkglib_DATA),g' > $@ chmod +x $@ check_SCRIPTS += grub-shell CLEANFILES += grub-shell diff --git a/include/grub/powerpc/memory.h b/include/grub/powerpc/memory.h new file mode 100644 index 000000000..b748f33c5 --- /dev/null +++ b/include/grub/powerpc/memory.h @@ -0,0 +1,47 @@ +/* memory.h - describe the memory map */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2007,2008,2009 Free Software Foundation, Inc. + * + * GRUB 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. + * + * GRUB 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 GRUB. If not, see . + */ + +#ifndef GRUB_MEMORY_CPU_HEADER +#define GRUB_MEMORY_CPU_HEADER 1 + +#ifndef ASM_FILE + +typedef grub_addr_t grub_phys_addr_t; + +static inline grub_phys_addr_t +grub_vtop (void *a) +{ + return (grub_phys_addr_t) a; +} + +static inline void * +grub_map_memory (grub_phys_addr_t a, grub_size_t size __attribute__ ((unused))) +{ + return (void *) a; +} + +static inline void +grub_unmap_memory (void *a __attribute__ ((unused)), + grub_size_t size __attribute__ ((unused))) +{ +} + +#endif + +#endif /* ! GRUB_MEMORY_CPU_HEADER */ diff --git a/include/grub/relocator_private.h b/include/grub/relocator_private.h index 10e445bfe..1c563cb64 100644 --- a/include/grub/relocator_private.h +++ b/include/grub/relocator_private.h @@ -40,6 +40,8 @@ void grub_cpu_relocator_backward (void *rels, void *src, void *tgt, grub_size_t size); void grub_cpu_relocator_jumper (void *rels, grub_addr_t addr); +/* Remark: GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT_LOG = 1 or 2 + aren't supported. */ #ifdef GRUB_MACHINE_IEEE1275 #define GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS 1 #define GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT_LOG 0 @@ -50,6 +52,12 @@ void grub_cpu_relocator_jumper (void *rels, grub_addr_t addr); #define GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS 0 #endif +#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS && GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT_LOG != 0 +#define GRUB_RELOCATOR_HAVE_LEFTOVERS 1 +#else +#define GRUB_RELOCATOR_HAVE_LEFTOVERS 0 +#endif + #if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS #define GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT (1 << GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT_LOG) #endif @@ -67,6 +75,8 @@ struct grub_relocator_mmap_event /* To track the regions already in heap. */ FIRMWARE_BLOCK_START = 6, FIRMWARE_BLOCK_END = FIRMWARE_BLOCK_START | 1, +#endif +#if GRUB_RELOCATOR_HAVE_LEFTOVERS REG_LEFTOVER_START = 8, REG_LEFTOVER_END = REG_LEFTOVER_START | 1, #endif diff --git a/lib/powerpc/relocator.c b/lib/powerpc/relocator.c index 9f5fc1c7f..85dfbeaf3 100644 --- a/lib/powerpc/relocator.c +++ b/lib/powerpc/relocator.c @@ -107,15 +107,15 @@ grub_err_t grub_relocator32_boot (struct grub_relocator *rel, struct grub_relocator32_state state) { - grub_addr_t target; - void *src, *ptr; + void *ptr; grub_err_t err; - grub_addr_t relst; + void *relst; grub_size_t relsize; grub_size_t stateset_size = 32 * REGW_SIZEOF + JUMP_SIZEOF; unsigned i; + grub_relocator_chunk_t ch; - err = grub_relocator_alloc_chunk_align (rel, &src, &target, 0, + err = grub_relocator_alloc_chunk_align (rel, &ch, 0, (0xffffffff - stateset_size) + 1, stateset_size, sizeof (grub_uint32_t), @@ -123,12 +123,13 @@ grub_relocator32_boot (struct grub_relocator *rel, if (err) return err; - ptr = src; + ptr = get_virtual_current_address (ch); for (i = 0; i < 32; i++) write_reg (i, state.gpr[i], &ptr); write_jump (&ptr); - err = grub_relocator_prepare_relocs (rel, target, &relst, &relsize); + err = grub_relocator_prepare_relocs (rel, get_physical_target_address (ch), + &relst, &relsize); if (err) return err; diff --git a/lib/relocator.c b/lib/relocator.c index 4c180e72a..6fbdb71d7 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -45,7 +45,9 @@ struct grub_relocator_subchunk grub_size_t size; grub_size_t pre_size; struct grub_relocator_extra_block *extra; +#if GRUB_RELOCATOR_HAVE_LEFTOVERS struct grub_relocator_fw_leftover *pre, *post; +#endif }; struct grub_relocator_chunk @@ -67,7 +69,7 @@ struct grub_relocator_extra_block grub_phys_addr_t end; }; -#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS +#if GRUB_RELOCATOR_HAVE_LEFTOVERS struct grub_relocator_fw_leftover { struct grub_relocator_fw_leftover *next; @@ -241,7 +243,7 @@ allocate_inreg (grub_phys_addr_t paddr, grub_size_t size, } } -#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS +#if GRUB_RELOCATOR_HAVE_LEFTOVERS static void check_leftover (struct grub_relocator_fw_leftover *lo) { @@ -367,6 +369,7 @@ free_subchunk (const struct grub_relocator_subchunk *subchu) GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT); if (fstart < fend) grub_relocator_firmware_free_region (fstart, fend - fstart); +#if GRUB_RELOCATOR_HAVE_LEFTOVERS if (subchu->pre) { int off = subchu->start - fstart @@ -384,6 +387,7 @@ free_subchunk (const struct grub_relocator_subchunk *subchu) subchu->pre->freebytes[off / 8] |= ((1 << (8 - (off % 8))) - 1); check_leftover (subchu->post); } +#endif *subchu->extra->prev = subchu->extra->next; grub_free (subchu->extra); } @@ -450,7 +454,9 @@ malloc_in_range (struct grub_relocator *rel, maxevents += 2; maxevents += grub_relocator_firmware_get_max_events (); +#endif +#if GRUB_RELOCATOR_HAVE_LEFTOVERS { struct grub_relocator_fw_leftover *cur; for (cur = leftovers; cur; cur = cur->next) @@ -500,8 +506,8 @@ malloc_in_range (struct grub_relocator *rel, for (r = grub_mm_base; r; r = r->next) { grub_dprintf ("relocator", "Blocking at 0x%lx-0x%lx\n", - (grub_addr_t) r - r->pre_size, - (grub_addr_t) (r + 1) + r->size); + (unsigned long) r - r->pre_size, + (unsigned long) (r + 1) + r->size); events[N].type = FIRMWARE_BLOCK_START; events[N].pos = (grub_addr_t) r - r->pre_size; N++; @@ -514,7 +520,7 @@ malloc_in_range (struct grub_relocator *rel, for (cur = extra_blocks; cur; cur = cur->next) { grub_dprintf ("relocator", "Blocking at 0x%lx-0x%lx\n", - cur->start, cur->end); + (unsigned long) cur->start, (unsigned long) cur->end); events[N].type = FIRMWARE_BLOCK_START; events[N].pos = cur->start; N++; @@ -526,6 +532,7 @@ malloc_in_range (struct grub_relocator *rel, N += grub_relocator_firmware_fill_events (events + N); +#if GRUB_RELOCATOR_HAVE_LEFTOVERS { struct grub_relocator_fw_leftover *cur; for (cur = leftovers; cur; cur = cur->next) @@ -552,6 +559,7 @@ malloc_in_range (struct grub_relocator *rel, } } } +#endif #endif /* No malloc from this point. */ @@ -636,7 +644,11 @@ malloc_in_range (struct grub_relocator *rel, /* Now events are nicely sorted. */ { int nstarted = 0, ncollisions = 0, nstartedfw = 0, nblockfw = 0; +#if GRUB_RELOCATOR_HAVE_LEFTOVERS int nlefto = 0; +#else + const int nlefto = 0; +#endif grub_addr_t starta = 0; int numstarted; for (j = from_low_priv ? 0 : N - 1; from_low_priv ? j < N : (j + 1); @@ -663,6 +675,9 @@ malloc_in_range (struct grub_relocator *rel, case FIRMWARE_BLOCK_END: nblockfw--; break; +#endif + +#if GRUB_RELOCATOR_HAVE_LEFTOVERS case REG_LEFTOVER_START: nlefto++; break; @@ -794,7 +809,8 @@ malloc_in_range (struct grub_relocator *rel, = ALIGN_UP (alloc_end, GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT); grub_dprintf ("relocator", "requesting %lx-%lx\n", - fstart, fend); + (unsigned long) fstart, + (unsigned long) fend); /* The failure here can be very expensive. */ if (!grub_relocator_firmware_alloc_region (fstart, fend - fstart)) @@ -807,6 +823,9 @@ malloc_in_range (struct grub_relocator *rel, } break; } +#endif + +#if GRUB_RELOCATOR_HAVE_LEFTOVERS case CHUNK_TYPE_LEFTOVER: { unsigned offstart = alloc_start @@ -857,14 +876,6 @@ malloc_in_range (struct grub_relocator *rel, fwin--; break; - case REG_LEFTOVER_START: - fwlefto++; - break; - - case REG_LEFTOVER_END: - fwlefto--; - break; - case FIRMWARE_BLOCK_START: fwb++; break; @@ -873,6 +884,16 @@ malloc_in_range (struct grub_relocator *rel, fwb--; break; #endif + +#if GRUB_RELOCATOR_HAVE_LEFTOVERS + case REG_LEFTOVER_START: + fwlefto++; + break; + + case REG_LEFTOVER_END: + fwlefto--; + break; +#endif case COLLISION_START: ncol++; break; @@ -971,8 +992,6 @@ malloc_in_range (struct grub_relocator *rel, if (!oom && typepre == CHUNK_TYPE_FIRMWARE) { grub_addr_t fstart, fend; - struct grub_relocator_fw_leftover *lo1 = NULL; - struct grub_relocator_fw_leftover *lo2 = NULL; fstart = ALIGN_DOWN (alloc_start, @@ -981,68 +1000,77 @@ malloc_in_range (struct grub_relocator *rel, = ALIGN_UP (alloc_end, GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT); - if (fstart != alloc_start) - lo1 = grub_malloc (sizeof (*lo1)); - if (fend != alloc_end) - lo2 = grub_malloc (sizeof (*lo2)); - if ((!lo1 && fstart != alloc_start) - || (!lo2 && fend != alloc_end)) - { - struct grub_relocator_extra_block *ne; - grub_free (lo1); - grub_free (lo2); - lo1 = NULL; - lo2 = NULL; - oom = 1; - grub_memcpy (&tofree, curschu, sizeof (tofree)); - ne = extra_blocks; - extra_blocks = extra_blocks->next; - grub_free (ne); - } - if (lo1) - { - lo1->quantstart = fstart; - grub_memset (lo1->freebytes, 0xff, - (alloc_start - fstart) / 8); - lo1->freebytes[(alloc_start - fstart) / 8] - = (1 << ((alloc_start - fstart) % 8)) - 1; - grub_memset (lo1->freebytes - + ((alloc_start - fstart) / 8) + 1, 0, - sizeof (lo1->freebytes) - - (alloc_start - fstart) / 8 - 1); - lo1->next = leftovers; - lo1->prev = &leftovers; - if (leftovers) - leftovers->prev = &lo1->next; - leftovers = lo1; - } - if (lo2) - { - lo2->quantstart - = fend - GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT; - grub_memset (lo2->freebytes, 0, - (alloc_end - lo2->quantstart) / 8); - lo2->freebytes[(alloc_end - lo2->quantstart) / 8] - = ~((1 << ((alloc_end - lo2->quantstart) % 8)) - 1); - grub_memset (lo2->freebytes - + ((alloc_end - lo2->quantstart) / 8) - + 1, 0, sizeof (lo2->freebytes) - - (alloc_end - lo2->quantstart) / 8 - 1); - lo2->prev = &leftovers; - if (leftovers) - leftovers->prev = &lo2->next; - lo2->next = leftovers; - leftovers = lo2; - } - curschu->pre = lo1; - curschu->post = lo2; +#if GRUB_RELOCATOR_HAVE_LEFTOVERS + { + struct grub_relocator_fw_leftover *lo1 = NULL; + struct grub_relocator_fw_leftover *lo2 = NULL; + if (fstart != alloc_start) + lo1 = grub_malloc (sizeof (*lo1)); + if (fend != alloc_end) + lo2 = grub_malloc (sizeof (*lo2)); + if ((!lo1 && fstart != alloc_start) + || (!lo2 && fend != alloc_end)) + { + struct grub_relocator_extra_block *ne; + grub_free (lo1); + grub_free (lo2); + lo1 = NULL; + lo2 = NULL; + oom = 1; + grub_memcpy (&tofree, curschu, sizeof (tofree)); + ne = extra_blocks; + extra_blocks = extra_blocks->next; + grub_free (ne); + } + if (lo1) + { + lo1->quantstart = fstart; + grub_memset (lo1->freebytes, 0xff, + (alloc_start - fstart) / 8); + lo1->freebytes[(alloc_start - fstart) / 8] + = (1 << ((alloc_start - fstart) % 8)) - 1; + grub_memset (lo1->freebytes + + ((alloc_start - fstart) / 8) + 1, 0, + sizeof (lo1->freebytes) + - (alloc_start - fstart) / 8 - 1); + lo1->next = leftovers; + lo1->prev = &leftovers; + if (leftovers) + leftovers->prev = &lo1->next; + leftovers = lo1; + } + if (lo2) + { + lo2->quantstart + = fend - GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT; + grub_memset (lo2->freebytes, 0, + (alloc_end - lo2->quantstart) / 8); + lo2->freebytes[(alloc_end - lo2->quantstart) / 8] + = ~((1 << ((alloc_end - lo2->quantstart) % 8)) - 1); + grub_memset (lo2->freebytes + + ((alloc_end - lo2->quantstart) / 8) + + 1, 0, sizeof (lo2->freebytes) + - (alloc_end - lo2->quantstart) / 8 - 1); + lo2->prev = &leftovers; + if (leftovers) + leftovers->prev = &lo2->next; + lo2->next = leftovers; + leftovers = lo2; + } + curschu->pre = lo1; + curschu->post = lo2; + } +#endif } +#if GRUB_RELOCATOR_HAVE_LEFTOVERS if (typepre == CHUNK_TYPE_LEFTOVER) { curschu->pre = events[last_start].leftover; curschu->post = events[last_start].leftover; } +#endif + #endif if (!oom) cural++; @@ -1077,14 +1105,6 @@ malloc_in_range (struct grub_relocator *rel, fwin--; break; - case REG_LEFTOVER_START: - fwlefto++; - break; - - case REG_LEFTOVER_END: - fwlefto--; - break; - case FIRMWARE_BLOCK_START: fwb++; break; @@ -1093,6 +1113,16 @@ malloc_in_range (struct grub_relocator *rel, fwb--; break; #endif + +#if GRUB_RELOCATOR_HAVE_LEFTOVERS + case REG_LEFTOVER_START: + fwlefto++; + break; + + case REG_LEFTOVER_END: + fwlefto--; + break; +#endif case COLLISION_START: ncol++; break; diff --git a/loader/powerpc/ieee1275/linux.c b/loader/powerpc/ieee1275/linux.c index 930c0cb41..c9feec975 100644 --- a/loader/powerpc/ieee1275/linux.c +++ b/loader/powerpc/ieee1275/linux.c @@ -24,7 +24,6 @@ #include #include #include -#include #include #include From ef4ffedd46e4fddb9ac73f26f30ba4add1238cb1 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 1 May 2010 15:15:38 +0200 Subject: [PATCH 114/178] remove loader.h leftovers --- include/grub/i386/multiboot/loader.h | 1 - loader/sparc64/ieee1275/linux.c | 1 - 2 files changed, 2 deletions(-) delete mode 100644 include/grub/i386/multiboot/loader.h diff --git a/include/grub/i386/multiboot/loader.h b/include/grub/i386/multiboot/loader.h deleted file mode 100644 index 1c725be19..000000000 --- a/include/grub/i386/multiboot/loader.h +++ /dev/null @@ -1 +0,0 @@ -#include diff --git a/loader/sparc64/ieee1275/linux.c b/loader/sparc64/ieee1275/linux.c index f55b4fa2a..948729a5d 100644 --- a/loader/sparc64/ieee1275/linux.c +++ b/loader/sparc64/ieee1275/linux.c @@ -24,7 +24,6 @@ #include #include #include -#include #include #include #include From aa8d7b264775ac81ab81470f649313eb13d7a180 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 1 May 2010 16:33:22 +0200 Subject: [PATCH 115/178] coreboot and qemu rescue disks and bootchecks --- conf/i386-coreboot.rmk | 3 + conf/i386-qemu.rmk | 3 + tests/util/grub-shell.in | 34 +++++++-- util/grub-mkrescue.in | 145 ++++++++++++++++++++------------------- 4 files changed, 110 insertions(+), 75 deletions(-) diff --git a/conf/i386-coreboot.rmk b/conf/i386-coreboot.rmk index ce54b80b1..805cc40b1 100644 --- a/conf/i386-coreboot.rmk +++ b/conf/i386-coreboot.rmk @@ -71,5 +71,8 @@ datetime_mod_SOURCES = lib/cmos_datetime.c datetime_mod_CFLAGS = $(COMMON_CFLAGS) datetime_mod_LDFLAGS = $(COMMON_LDFLAGS) +BOOTTARGET=coreboot +QEMU32=qemu-system-i386 + include $(srcdir)/conf/i386.mk include $(srcdir)/conf/common.mk diff --git a/conf/i386-qemu.rmk b/conf/i386-qemu.rmk index 6d387a6f6..416fbc0f3 100644 --- a/conf/i386-qemu.rmk +++ b/conf/i386-qemu.rmk @@ -61,5 +61,8 @@ datetime_mod_SOURCES = lib/cmos_datetime.c datetime_mod_CFLAGS = $(COMMON_CFLAGS) datetime_mod_LDFLAGS = $(COMMON_LDFLAGS) +BOOTTARGET=qemu +QEMU32=qemu-system-i386 + include $(srcdir)/conf/i386.mk include $(srcdir)/conf/common.mk diff --git a/tests/util/grub-shell.in b/tests/util/grub-shell.in index d8ea588a7..86b46cf77 100644 --- a/tests/util/grub-shell.in +++ b/tests/util/grub-shell.in @@ -83,6 +83,8 @@ for option in "$@"; do if [ "$dev" = "fd" ] ; then boot=fd; elif [ "$dev" = "hd" ] ; then boot=hd; elif [ "$dev" = "cd" ] ; then boot=cd; + elif [ "$dev" = "qemu" ] ; then boot=qemu; + elif [ "$dev" = "coreboot" ] ; then boot=coreboot; else echo "Unrecognized boot method \`$dev'" 1>&2 usage @@ -119,6 +121,8 @@ terminal_input serial terminal_output serial EOF +rom_directory=`mktemp -d` + for mod in ${modules} do echo "insmod ${mod}" >> ${cfgfile} @@ -131,25 +135,43 @@ EOF isofile=`mktemp` grub-mkrescue --output=${isofile} --override-directory=${builddir} \ + --rom-directory="${rom_directory}" \ /boot/grub/grub.cfg=${cfgfile} /boot/grub/testcase.cfg=${source} \ ${files} >/dev/null 2>&1 if [ x$boot = xhd ]; then device=hda - bootdev=c + bootdev="-boot c" fi if [ x$boot = xcd ]; then device=cdrom - bootdev=d + bootdev="-boot d" fi if [ x$boot = xfd ]; then device=fda - bootdev=a + bootdev="-boot a" fi -${qemu} ${qemuopts} -nographic -serial file:/dev/stdout -monitor file:/dev/null -${device} ${isofile} -boot ${bootdev} | tr -d "\r" -rm -f ${isofile} +if [ x$boot = xqemu ]; then + bootdev="-bios ${rom_directory}/qemu.img" + device=cdrom +fi -rm -f ${tmpfile} ${cfgfile} +if [ x$boot = xcoreboot ]; then + imgfile=`mktemp` + cp "${GRUB_COREBOOT_ROM}" "${imgfile}" + "${GRUB_CBFSTOOL}" "${imgfile}" add-payload "${rom_directory}/coreboot.elf" fallback/payload + bootdev="-bios ${imgfile}" + device=cdrom +fi + +${qemu} ${qemuopts} -nographic -serial file:/dev/stdout -monitor file:/dev/null -${device} ${isofile} ${bootdev} | tr -d "\r" +rm -f "${isofile}" "${imgfile}" +rm -rf "${rom_directory}" +if [ x$boot = xcoreboot ]; then + rm -f "${imgfile}" +fi + +rm -f "${tmpfile}" "${cfgfile}" exit 0 diff --git a/util/grub-mkrescue.in b/util/grub-mkrescue.in index 365c48f29..56b27a6e9 100644 --- a/util/grub-mkrescue.in +++ b/util/grub-mkrescue.in @@ -30,10 +30,13 @@ target_cpu=@target_cpu@ native_platform=@platform@ pkglib_DATA="@pkglib_DATA@" -multiboot_dir=${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/${target_cpu}-multiboot -pc_dir=${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/${target_cpu}-pc +multiboot_dir=${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/i386-multiboot +coreboot_dir=${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/i386-coreboot +qemu_dir=${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/i386-qemu +pc_dir=${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/i386-pc efi32_dir=${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/i386-efi efi64_dir=${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/x86_64-efi +rom_directory= # Usage: usage # Print the usage. @@ -46,6 +49,7 @@ Make GRUB rescue image. -v, --version print the version information and exit --modules=MODULES pre-load specified modules MODULES --output=FILE save output in FILE [required] + --rom-directory=DIR save rom images in DIR [optional] $0 generates a bootable rescue image with specified source files or directories. @@ -66,6 +70,8 @@ for option in "$@"; do modules=`echo "$option" | sed 's/--modules=//'` ;; --output=*) output_image=`echo "$option" | sed 's/--output=//'` ;; + --rom-directory=*) + rom_directory=`echo "$option" | sed 's/--rom-directory=//'` ;; # Intentionally undocumented --override-directory=*) override_dir=`echo "${option}/" | sed 's/--override-directory=//'` @@ -103,15 +109,15 @@ process_input_dir () { input_dir="$1" platform="$2" - mkdir -p ${iso9660_dir}/boot/grub/${target_cpu}-${platform} + mkdir -p ${iso9660_dir}/boot/grub/${platform} for file in ${input_dir}/*.mod; do if test -f "$file"; then - cp -f "$file" ${iso9660_dir}/boot/grub/${target_cpu}-${platform}/ + cp -f "$file" ${iso9660_dir}/boot/grub/${platform}/ fi done for file in ${pkglib_DATA}; do if test -f "${input_dir}/${file}"; then - cp -f "${input_dir}/${file}" ${iso9660_dir}/boot/grub/${target_cpu}-${platform}/ + cp -f "${input_dir}/${file}" ${iso9660_dir}/boot/grub/${platform}/ fi done @@ -123,27 +129,70 @@ process_input_dir () done } +make_image () +{ + source_directory="$1" + platform=$2 + if ! test -e "${source_directory}"; then + return; + fi + + echo "Enabling $2 support ..." + + memdisk_img=`mktemp "$MKTEMP_TEMPLATE"` + memdisk_dir=`mktemp -d "$MKTEMP_TEMPLATE"` + mkdir -p ${memdisk_dir}/boot/grub + + modules="$(cat ${source_directory}/partmap.lst) ${modules}" + cat << EOF > ${memdisk_dir}/boot/grub/grub.cfg +search --fs-uuid --set ${iso_uuid} +set prefix=(\${root})/boot/grub/${platform} +source \$prefix/grub.cfg +EOF + (for i in ${modules} ; do + echo "insmod $i" + done ; \ + echo "source /boot/grub/grub.cfg") \ + > ${iso9660_dir}/boot/grub/${platform}/grub.cfg + + tar -C ${memdisk_dir} -cf ${memdisk_img} boot + rm -rf ${memdisk_dir} + grub-mkimage -O ${platform} -d "${source_directory}" -m "${memdisk_img}" -o "$3" --prefix='(memdisk)/boot/grub' \ + search iso9660 configfile sh memdisk tar $4 + rm -rf ${memdisk_img} +} + if [ "${override_dir}" = "" ] ; then if test -e "${multiboot_dir}" ; then - process_input_dir ${multiboot_dir} multiboot + process_input_dir ${multiboot_dir} i386-multiboot + fi + if test -e "${coreboot_dir}" ; then + process_input_dir ${coreboot_dir} i386-coreboot + fi + if test -e "${qemu_dir}" ; then + process_input_dir ${qemu_dir} i386-qemu fi if test -e "${pc_dir}" ; then - process_input_dir ${pc_dir} pc + process_input_dir ${pc_dir} i386-pc fi if test -e "${efi32_dir}" ; then - process_input_dir ${efi32_dir} efi32 + process_input_dir ${efi32_dir} i386-efi fi if test -e "${efi64_dir}" ; then - process_input_dir ${efi64_dir} efi64 + process_input_dir ${efi64_dir} x86_64-efi fi else - process_input_dir ${override_dir} ${native_platform} + process_input_dir ${override_dir} ${target_cpu}-${native_platform} multiboot_dir= pc_dir= efi32_dir= efi64_dir= - case "${native_platform}" in + coreboot_dir= + qemu_dir= + case "${target_cpu}-${native_platform}" in i386-multiboot) multiboot_dir=${override_dir} ;; + i386-coreboot) coreboot_dir=${override_dir} ;; + i386-qemu) qemu_dir=${override_dir} ;; i386-pc) pc_dir=${override_dir} ;; i386-efi) efi32_dir=${override_dir} ;; x86_64-efi) efi64_dir=${override_dir} ;; @@ -154,33 +203,6 @@ fi iso_uuid=$(date -u +%Y-%m-%d-%H-%M-%S-00) grub_mkisofs_arguments="${grub_mkisofs_arguments} --modification-date=$(echo ${iso_uuid} | sed -e s/-//g)" -# build multiboot core.img -if test -e "${multiboot_dir}" ; then - echo "Enabling multiboot support ..." - memdisk_img=`mktemp "$MKTEMP_TEMPLATE"` - memdisk_dir=`mktemp -d "$MKTEMP_TEMPLATE"` - mkdir -p ${memdisk_dir}/boot/grub - - modules="$(cat ${multiboot_dir}/partmap.lst) ${modules}" - cat << EOF > ${memdisk_dir}/boot/grub/grub.cfg -search --fs-uuid --set ${iso_uuid} -set prefix=(\${root})/boot/grub/${target_cpu}-multiboot -source \$prefix/grub.cfg -EOF - (for i in ${modules} ; do - echo "insmod $i" - done ; \ - echo "source /boot/grub/grub.cfg") \ - > ${iso9660_dir}/boot/grub/i386-multiboot/grub.cfg - - tar -C ${memdisk_dir} -cf ${memdisk_img} boot - rm -rf ${memdisk_dir} - grub-mkimage -O i386-multiboot -d ${multiboot_dir}/ -m ${memdisk_img} -o ${iso9660_dir}/boot/multiboot.img \ - memdisk tar search iso9660 configfile sh \ - ata at_keyboard - rm -f ${memdisk_img} -fi - # build BIOS core.img if test -e "${pc_dir}" ; then echo "Enabling BIOS support ..." @@ -205,47 +227,32 @@ if test -e "${pc_dir}" ; then --embedded-boot ${embed_img}" fi +# build multiboot core.img +make_image "${multiboot_dir}" i386-multiboot "${iso9660_dir}/boot/multiboot.img" "ata at_keyboard" + if test -e "${efi64_dir}" || test -e "${efi32_dir}"; then efi_dir=`mktemp -d "$MKTEMP_TEMPLATE"` mkdir -p "${efi_dir}/efi/boot" -else - efi_dir= -fi -# build bootx64.efi -if test -e "${efi64_dir}" ; then - echo "Enabling EFI64 support ..." - memdisk_img=`mktemp "$MKTEMP_TEMPLATE"` - memdisk_dir=`mktemp -d "$MKTEMP_TEMPLATE"` - mkdir -p ${memdisk_dir}/boot/grub + # build bootx64.efi + make_image "${efi64_dir}" x86_64-efi "${efi_dir}"/efi/boot/bootx64.efi "" + # build bootia32.efi + make_image "${efi32_dir}" i386-efi "${efi_dir}"/efi/boot/bootia32.efi "" - cat << EOF > ${memdisk_dir}/boot/grub/grub.cfg -search --fs-uuid --set ${iso_uuid} -set prefix=(\${root})/boot/grub/${target_cpu}-efi -source \$prefix/grub.cfg -EOF - - tar -C ${memdisk_dir} -cf ${memdisk_img} boot - rm -rf ${memdisk_dir} - - grub-mkimage -O x86_64-efi -d "${efi64_dir}" -m "${memdisk_img}" -o "${efi_dir}"/efi/boot/bootx64.efi --prefix='(memdisk)/boot/grub' \ - search iso9660 configfile sh memdisk tar - rm -f ${memdisk_img} - - modules="$(cat "${efi64_dir}"/partmap.lst) ${modules}" - (for i in ${modules} ; do - echo "insmod $i" - done ; \ - echo "source /boot/grub/grub.cfg") \ - > "${iso9660_dir}"/boot/grub/x86_64-efi/grub.cfg -fi - -if test x"${efi_dir}" != x; then mformat -C -f 2880 -L 16 -i "${iso9660_dir}"/efi.img :: mcopy -s -i "${iso9660_dir}"/efi.img ${efi_dir}/efi ::/ grub_mkisofs_arguments="${grub_mkisofs_arguments} --efi-boot efi.img" fi +make_image "${qemu_dir}" i386-qemu "${iso9660_dir}/boot/qemu.img" "ata at_keyboard" +if [ -e "${iso9660_dir}/boot/qemu.img" ] && [ -d "${rom_directory}" ]; then + cp "${iso9660_dir}/boot/qemu.img" "${rom_directory}/qemu.img" +fi +make_image "${coreboot_dir}" i386-coreboot "${iso9660_dir}/boot/coreboot.elf" "ata at_keyboard" +if [ -e "${iso9660_dir}/boot/coreboot.elf" ] && [ -d "${rom_directory}" ]; then + cp "${iso9660_dir}/boot/coreboot.elf" "${rom_directory}/coreboot.elf" +fi + # build iso image xorriso -pathspecs on -as mkisofs ${grub_mkisofs_arguments} --protective-msdos-label -o ${output_image} -r ${iso9660_dir} ${source} rm -rf ${iso9660_dir} From 6d971470fad15daf02eb270bdd969c3ba9b42a6b Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 1 May 2010 16:38:10 +0200 Subject: [PATCH 116/178] REmove memory map altering for FreeBSD --- loader/i386/bsd.c | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/loader/i386/bsd.c b/loader/i386/bsd.c index 1b2fadd80..c0c75204d 100644 --- a/loader/i386/bsd.c +++ b/loader/i386/bsd.c @@ -258,7 +258,6 @@ static void generate_e820_mmap (grub_size_t *len, grub_size_t *cnt, void *buf) { int count = 0; - int isfirstrun = 1; struct grub_e820_mmap *mmap = buf; struct grub_e820_mmap prev, cur; @@ -266,21 +265,6 @@ generate_e820_mmap (grub_size_t *len, grub_size_t *cnt, void *buf) int NESTED_FUNC_ATTR hook (grub_uint64_t addr, grub_uint64_t size, grub_uint32_t type) { - /* FreeBSD assumes that first 64KiB are available. - Not always true but try to prevent panic somehow. */ - if (kernel_type == KERNEL_TYPE_FREEBSD && isfirstrun && addr != 0) - { - cur.addr = 0; - cur.size = (addr < 0x10000) ? addr : 0x10000; - cur.type = GRUB_E820_RAM; - if (mmap) - *mmap++ = cur; - - prev = cur; - count++; - } - isfirstrun = 0; - cur.addr = addr; cur.size = size; switch (type) @@ -317,7 +301,7 @@ generate_e820_mmap (grub_size_t *len, grub_size_t *cnt, void *buf) { prev.size += cur.size; if (mmap) - mmap[-1] = cur; + mmap[-1] = prev; } else { @@ -330,7 +314,6 @@ generate_e820_mmap (grub_size_t *len, grub_size_t *cnt, void *buf) return 0; } - isfirstrun = 1; grub_mmap_iterate (hook); if (len) From 92517362b2e204ec83a6d2d7bef99913b081da21 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 2 May 2010 16:26:00 +0200 Subject: [PATCH 117/178] Split a memory chunk spanning accross 1MiB mark for openbsd --- loader/i386/bsd.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/loader/i386/bsd.c b/loader/i386/bsd.c index c0c75204d..26055d5ca 100644 --- a/loader/i386/bsd.c +++ b/loader/i386/bsd.c @@ -301,7 +301,7 @@ generate_e820_mmap (grub_size_t *len, grub_size_t *cnt, void *buf) { prev.size += cur.size; if (mmap) - mmap[-1] = prev; + mmap[-1] = prev; } else { @@ -311,6 +311,23 @@ generate_e820_mmap (grub_size_t *len, grub_size_t *cnt, void *buf) count++; } + if (kernel_type == KERNEL_TYPE_OPENBSD && prev.addr < 0x100000 + && prev.addr + prev.size > 0x100000) + { + cur.addr = 0x100000; + cur.size = prev.addr + prev.size - 0x100000; + cur.type = prev.type; + prev.size = 0x100000 - prev.addr; + if (mmap) + { + mmap[-1] = prev; + mmap[0] = cur; + mmap++; + } + prev = cur; + count++; + } + return 0; } @@ -1475,7 +1492,7 @@ grub_cmd_openbsd (grub_extcmd_t cmd, int argc, char *argv[]) if (grub_bsd_load (argc, argv) == GRUB_ERR_NONE) { - grub_loader_set (grub_openbsd_boot, grub_bsd_unload, 1); + grub_loader_set (grub_openbsd_boot, grub_bsd_unload, 0); openbsd_root = bootdev; } From 4fc5ff74f2e5a1497cbdc482e0d8d2cd88fe34d8 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 2 May 2010 16:27:22 +0200 Subject: [PATCH 118/178] USe more low memory on i386-qemu --- kern/i386/qemu/mmap.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/kern/i386/qemu/mmap.c b/kern/i386/qemu/mmap.c index c7fc4f45e..dfdb99b74 100644 --- a/kern/i386/qemu/mmap.c +++ b/kern/i386/qemu/mmap.c @@ -52,6 +52,11 @@ grub_machine_mmap_iterate (int NESTED_FUNC_ATTR (*hook) (grub_uint64_t, grub_uin GRUB_MACHINE_MEMORY_AVAILABLE)) return 1; + if (hook ((grub_addr_t) _end, + 0xa0000 - (grub_addr_t) _end, + GRUB_MACHINE_MEMORY_AVAILABLE)) + return 1; + if (hook (GRUB_MEMORY_MACHINE_UPPER, 0x100000 - GRUB_MEMORY_MACHINE_UPPER, GRUB_MACHINE_MEMORY_RESERVED)) From 32bf9244dc9150500be489e43d2efc1c2a04245b Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 2 May 2010 19:39:07 +0200 Subject: [PATCH 119/178] Remove reference to loader.h --- loader/mips/linux.c | 1 - 1 file changed, 1 deletion(-) diff --git a/loader/mips/linux.c b/loader/mips/linux.c index a3569c34c..018cfdcc5 100644 --- a/loader/mips/linux.c +++ b/loader/mips/linux.c @@ -23,7 +23,6 @@ #include #include #include -#include #include #include #include From 6f030865dade7134dffee51bf27c3cc569aa2bab Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 2 May 2010 19:39:35 +0200 Subject: [PATCH 120/178] Add missing memory.h --- include/grub/mips/memory.h | 1 + 1 file changed, 1 insertion(+) create mode 100644 include/grub/mips/memory.h diff --git a/include/grub/mips/memory.h b/include/grub/mips/memory.h new file mode 100644 index 000000000..e51bcc1f2 --- /dev/null +++ b/include/grub/mips/memory.h @@ -0,0 +1 @@ +#include From db292d391f53e20454b1bf61c674399dc30105e9 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 2 May 2010 19:39:46 +0200 Subject: [PATCH 121/178] Support elfsyms on mb2 --- loader/multiboot_mbi2.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/loader/multiboot_mbi2.c b/loader/multiboot_mbi2.c index 78c280fbf..8cade2f2f 100644 --- a/loader/multiboot_mbi2.c +++ b/loader/multiboot_mbi2.c @@ -55,6 +55,19 @@ static unsigned modcnt; static char *cmdline = NULL; static int bootdev_set; static grub_uint32_t biosdev, slice, part; +static grub_size_t elf_sec_num, elf_sec_entsize; +static unsigned elf_sec_shstrndx; +static void *elf_sections; + +void +grub_multiboot_add_elfsyms (grub_size_t num, grub_size_t entsize, + unsigned shndx, void *data) +{ + elf_sec_num = num; + elf_sec_shstrndx = shndx; + elf_sec_entsize = entsize; + elf_sections = data; +} grub_err_t grub_multiboot_load (grub_file_t file) @@ -262,6 +275,8 @@ grub_multiboot_get_mbi_size (void) + (modcnt * sizeof (struct multiboot_tag_module) + total_modcmd) + sizeof (struct multiboot_tag_basic_meminfo) + ALIGN_UP (sizeof (struct multiboot_tag_bootdev), MULTIBOOT_TAG_ALIGN) + + sizeof (struct multiboot_tag_elf_sections) + + elf_sec_entsize * elf_sec_num + (sizeof (struct multiboot_tag_mmap) + grub_get_multiboot_mmap_count () * sizeof (struct multiboot_mmap_entry)) + sizeof (struct multiboot_tag_vbe) + MULTIBOOT_TAG_ALIGN - 1; @@ -518,6 +533,19 @@ grub_multiboot_make_mbi (grub_uint32_t *target) ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN); } + { + struct multiboot_tag_elf_sections *tag + = (struct multiboot_tag_elf_sections *) ptrorig; + tag->type = MULTIBOOT_TAG_TYPE_ELF_SECTIONS; + tag->size = sizeof (struct multiboot_tag_elf_sections) + + elf_sec_entsize * elf_sec_num; + grub_memcpy (tag->sections, elf_sections, elf_sec_entsize * elf_sec_num); + tag->num = elf_sec_num; + tag->entsize = elf_sec_entsize; + tag->shndx = elf_sec_shstrndx; + ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN); + } + { struct multiboot_tag_basic_meminfo *tag = (struct multiboot_tag_basic_meminfo *) ptrorig; From 05f602fc5168dbc53b05dc62d4fdc92d5bab4ed8 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 2 May 2010 22:06:44 +0200 Subject: [PATCH 122/178] enable xnu on all platforms --- conf/i386-coreboot.rmk | 17 +++++ conf/i386-ieee1275.rmk | 10 +++ conf/i386-multiboot.rmk | 27 ++++--- conf/i386-pc.rmk | 74 +++---------------- conf/i386-qemu.rmk | 11 +++ conf/i386.rmk | 57 ++++++++++++++ conf/x86-efi.rmk | 7 -- configure.ac | 6 ++ efiemu/i386/coredetect.c | 1 - .../pc/efiemu.h => efiemu/i386/nocfgtables.c | 16 ++-- efiemu/i386/pc/cfgtables.c | 1 - efiemu/loadcore.c | 1 - efiemu/main.c | 1 - include/grub/efiemu/efiemu.h | 2 + loader/i386/xnu.c | 4 + 15 files changed, 145 insertions(+), 90 deletions(-) rename include/grub/i386/pc/efiemu.h => efiemu/i386/nocfgtables.c (75%) diff --git a/conf/i386-coreboot.rmk b/conf/i386-coreboot.rmk index 805cc40b1..c9eea6470 100644 --- a/conf/i386-coreboot.rmk +++ b/conf/i386-coreboot.rmk @@ -22,6 +22,7 @@ kernel_img_SOURCES = kern/i386/coreboot/startup.S \ kern/env.c \ term/i386/pc/vga_text.c term/i386/vga_common.c \ symlist.c +kernel_img_HEADERS += i386/pit.h kernel_img_CFLAGS = $(COMMON_CFLAGS) kernel_img_ASFLAGS = $(COMMON_ASFLAGS) kernel_img_LDFLAGS += $(COMMON_LDFLAGS) -Wl,-N,-S,-Ttext,0x8200,-Bstatic @@ -71,6 +72,22 @@ datetime_mod_SOURCES = lib/cmos_datetime.c datetime_mod_CFLAGS = $(COMMON_CFLAGS) datetime_mod_LDFLAGS = $(COMMON_LDFLAGS) +pkglib_MODULES += efiemu.mod +efiemu_mod_SOURCES = efiemu/main.c efiemu/i386/loadcore32.c \ + efiemu/i386/loadcore64.c efiemu/i386/pc/cfgtables.c \ + efiemu/mm.c efiemu/loadcore_common.c efiemu/symbols.c \ + efiemu/loadcore32.c efiemu/loadcore64.c \ + efiemu/prepare32.c efiemu/prepare64.c efiemu/pnvram.c \ + efiemu/i386/coredetect.c +efiemu_mod_CFLAGS = $(COMMON_CFLAGS) +efiemu_mod_LDFLAGS = $(COMMON_LDFLAGS) + +# For acpi.mod. +pkglib_MODULES += acpi.mod +acpi_mod_SOURCES = commands/acpi.c commands/i386/pc/acpi.c +acpi_mod_CFLAGS = $(COMMON_CFLAGS) +acpi_mod_LDFLAGS = $(COMMON_LDFLAGS) + BOOTTARGET=coreboot QEMU32=qemu-system-i386 diff --git a/conf/i386-ieee1275.rmk b/conf/i386-ieee1275.rmk index d9469c6e6..1b7460dc3 100644 --- a/conf/i386-ieee1275.rmk +++ b/conf/i386-ieee1275.rmk @@ -64,5 +64,15 @@ datetime_mod_SOURCES = lib/cmos_datetime.c datetime_mod_CFLAGS = $(COMMON_CFLAGS) datetime_mod_LDFLAGS = $(COMMON_LDFLAGS) +pkglib_MODULES += efiemu.mod +efiemu_mod_SOURCES = efiemu/main.c efiemu/i386/loadcore32.c \ + efiemu/i386/loadcore64.c efiemu/i386/nocfgtables.c \ + efiemu/mm.c efiemu/loadcore_common.c efiemu/symbols.c \ + efiemu/loadcore32.c efiemu/loadcore64.c \ + efiemu/prepare32.c efiemu/prepare64.c efiemu/pnvram.c \ + efiemu/i386/coredetect.c +efiemu_mod_CFLAGS = $(COMMON_CFLAGS) +efiemu_mod_LDFLAGS = $(COMMON_LDFLAGS) + include $(srcdir)/conf/i386.mk include $(srcdir)/conf/common.mk diff --git a/conf/i386-multiboot.rmk b/conf/i386-multiboot.rmk index 69b8e9a48..ddfb7e283 100644 --- a/conf/i386-multiboot.rmk +++ b/conf/i386-multiboot.rmk @@ -24,6 +24,7 @@ kernel_img_SOURCES = kern/i386/coreboot/startup.S \ kern/env.c \ term/i386/pc/vga_text.c term/i386/vga_common.c \ symlist.c +kernel_img_HEADERS += i386/pit.h kernel_img_CFLAGS = $(COMMON_CFLAGS) kernel_img_ASFLAGS = $(COMMON_ASFLAGS) kernel_img_LDFLAGS += $(COMMON_LDFLAGS) -Wl,-N,-S,-Ttext,$(GRUB_KERNEL_MACHINE_LINK_ADDR),-Bstatic @@ -35,7 +36,7 @@ bin_SCRIPTS += grub-mkrescue grub_mkrescue_SOURCES = util/grub-mkrescue.in # Modules. -pkglib_MODULES = linux.mod aout.mod halt.mod datetime.mod mmap.mod +pkglib_MODULES = linux.mod halt.mod datetime.mod mmap.mod # For mmap.mod. mmap_mod_SOURCES = mmap/mmap.c mmap/i386/uppermem.c mmap/i386/mmap.c @@ -53,17 +54,21 @@ halt_mod_SOURCES = commands/halt.c halt_mod_CFLAGS = $(COMMON_CFLAGS) halt_mod_LDFLAGS = $(COMMON_LDFLAGS) -# For aout.mod. -aout_mod_SOURCES = loader/aout.c -aout_mod_CFLAGS = $(COMMON_CFLAGS) -aout_mod_LDFLAGS = $(COMMON_LDFLAGS) +pkglib_MODULES += efiemu.mod +efiemu_mod_SOURCES = efiemu/main.c efiemu/i386/loadcore32.c \ + efiemu/i386/loadcore64.c efiemu/i386/pc/cfgtables.c \ + efiemu/mm.c efiemu/loadcore_common.c efiemu/symbols.c \ + efiemu/loadcore32.c efiemu/loadcore64.c \ + efiemu/prepare32.c efiemu/prepare64.c efiemu/pnvram.c \ + efiemu/i386/coredetect.c +efiemu_mod_CFLAGS = $(COMMON_CFLAGS) +efiemu_mod_LDFLAGS = $(COMMON_LDFLAGS) -# For bsd.mod -pkglib_MODULES += bsd.mod -bsd_mod_SOURCES = loader/i386/bsd.c loader/i386/bsd32.c loader/i386/bsd64.c loader/i386/bsd_helper.S loader/i386/bsd_trampoline.S -bsd_mod_CFLAGS = $(COMMON_CFLAGS) -bsd_mod_LDFLAGS = $(COMMON_LDFLAGS) -bsd_mod_ASFLAGS = $(COMMON_ASFLAGS) +# For acpi.mod. +pkglib_MODULES += acpi.mod +acpi_mod_SOURCES = commands/acpi.c commands/i386/pc/acpi.c +acpi_mod_CFLAGS = $(COMMON_CFLAGS) +acpi_mod_LDFLAGS = $(COMMON_LDFLAGS) # For datetime.mod datetime_mod_SOURCES = lib/cmos_datetime.c diff --git a/conf/i386-pc.rmk b/conf/i386-pc.rmk index d403d19f1..3f0eeb611 100644 --- a/conf/i386-pc.rmk +++ b/conf/i386-pc.rmk @@ -92,7 +92,7 @@ grub_mkrescue_SOURCES = util/grub-mkrescue.in pkglib_MODULES = biosdisk.mod chain.mod halt.mod vbe.mod vbetest.mod \ vbeinfo.mod vga.mod pxe.mod pxecmd.mod datetime.mod ata_pthru.mod \ hdparm.mod usb.mod uhci.mod ohci.mod usbtest.mod usbms.mod \ - usb_keyboard.mod efiemu.mod mmap.mod acpi.mod drivemap.mod + usb_keyboard.mod mmap.mod drivemap.mod # For drivemap.mod. drivemap_mod_SOURCES = commands/i386/pc/drivemap.c \ @@ -101,17 +101,8 @@ drivemap_mod_ASFLAGS = $(COMMON_ASFLAGS) drivemap_mod_CFLAGS = $(COMMON_CFLAGS) drivemap_mod_LDFLAGS = $(COMMON_LDFLAGS) -# For efiemu.mod. -efiemu_mod_SOURCES = efiemu/main.c efiemu/i386/loadcore32.c \ - efiemu/i386/loadcore64.c efiemu/i386/pc/cfgtables.c \ - efiemu/mm.c efiemu/loadcore_common.c efiemu/symbols.c \ - efiemu/loadcore32.c efiemu/loadcore64.c \ - efiemu/prepare32.c efiemu/prepare64.c efiemu/pnvram.c \ - efiemu/i386/coredetect.c -efiemu_mod_CFLAGS = $(COMMON_CFLAGS) -efiemu_mod_LDFLAGS = $(COMMON_LDFLAGS) - # For acpi.mod. +pkglib_MODULES += acpi.mod acpi_mod_SOURCES = commands/acpi.c commands/i386/pc/acpi.c acpi_mod_CFLAGS = $(COMMON_CFLAGS) acpi_mod_LDFLAGS = $(COMMON_LDFLAGS) @@ -144,12 +135,15 @@ linux16_mod_SOURCES = loader/i386/pc/linux.c linux16_mod_CFLAGS = $(COMMON_CFLAGS) linux16_mod_LDFLAGS = $(COMMON_LDFLAGS) -pkglib_MODULES += xnu.mod -xnu_mod_SOURCES = loader/xnu_resume.c loader/i386/xnu.c \ - loader/macho32.c loader/macho64.c loader/macho.c loader/xnu.c -xnu_mod_CFLAGS = $(COMMON_CFLAGS) -xnu_mod_LDFLAGS = $(COMMON_LDFLAGS) -xnu_mod_ASFLAGS = $(COMMON_ASFLAGS) +pkglib_MODULES += efiemu.mod +efiemu_mod_SOURCES = efiemu/main.c efiemu/i386/loadcore32.c \ + efiemu/i386/loadcore64.c efiemu/i386/pc/cfgtables.c \ + efiemu/mm.c efiemu/loadcore_common.c efiemu/symbols.c \ + efiemu/loadcore32.c efiemu/loadcore64.c \ + efiemu/prepare32.c efiemu/prepare64.c efiemu/pnvram.c \ + efiemu/i386/coredetect.c +efiemu_mod_CFLAGS = $(COMMON_CFLAGS) +efiemu_mod_LDFLAGS = $(COMMON_LDFLAGS) # For halt.mod. halt_mod_SOURCES = commands/i386/pc/halt.c @@ -231,52 +225,6 @@ hdparm_mod_SOURCES = commands/hdparm.c lib/hexdump.c hdparm_mod_CFLAGS = $(COMMON_CFLAGS) hdparm_mod_LDFLAGS = $(COMMON_LDFLAGS) -ifeq ($(enable_efiemu), yes) - -efiemu32.o: efiemu/runtime/efiemu.c $(TARGET_OBJ2ELF) - -rm -f $@ -ifeq ($(TARGET_APPLE_CC), 1) - -rm -f $@.bin - $(TARGET_CC) -c -m32 -DELF32 -DAPPLE_CC -o $@.bin -Wall -Werror $< -nostdlib -O2 -I$(srcdir)/efiemu/runtime -I$(srcdir)/include -Iinclude - $(OBJCONV) -felf32 -nu -nd $@.bin $@ - -rm -f $@.bin -else - $(TARGET_CC) -c -m32 -DELF32 -o $@ -Wall -Werror $< -nostdlib -O2 -I$(srcdir)/efiemu/runtime -I$(srcdir)/include -Iinclude - if test ! -z $(TARGET_OBJ2ELF); then ./$(TARGET_OBJ2ELF) $@ || (rm -f $@; exit 1); fi -endif - -efiemu64_c.o: efiemu/runtime/efiemu.c -ifeq ($(TARGET_APPLE_CC), 1) - $(TARGET_CC) -c -m64 -DAPPLE_CC=1 -DELF64 -o $@ -Wall -Werror $< -nostdlib -mno-red-zone -O2 -I$(srcdir)/efiemu/runtime -I$(srcdir)/include -Iinclude -else - $(TARGET_CC) -c -m64 -DELF64 -o $@ -Wall -Werror $< -nostdlib -mcmodel=large -mno-red-zone -O2 -I$(srcdir)/efiemu/runtime -I$(srcdir)/include -Iinclude -endif - -efiemu64_s.o: efiemu/runtime/efiemu.S - -rm -f $@ -ifeq ($(TARGET_APPLE_CC), 1) - $(TARGET_CC) -c -m64 -DAPPLE_CC=1 -DELF64 -o $@ -Wall -Werror $< -nostdlib -mno-red-zone -O2 -I$(srcdir)/efiemu/runtime -I$(srcdir)/include -Iinclude -else - $(TARGET_CC) -c -m64 -DELF64 -o $@ -Wall -Werror $< -nostdlib -mcmodel=large -mno-red-zone -O2 -I$(srcdir)/efiemu/runtime -I$(srcdir)/include -Iinclude -endif - -efiemu64.o: efiemu64_c.o efiemu64_s.o $(TARGET_OBJ2ELF) - -rm -f $@ -ifeq ($(TARGET_APPLE_CC), 1) - -rm -f $@.bin - $(TARGET_CC) -m64 -o $@.bin -Wl,-r $^ -nostdlib - $(OBJCONV) -felf64 -nu -nd $@.bin $@ - -rm -f $@.bin -else - $(TARGET_CC) -m64 -o $@ -Wl,-r $^ -nostdlib - if test ! -z $(TARGET_OBJ2ELF); then ./$(TARGET_OBJ2ELF) $@ || (rm -f $@; exit 1); fi -endif - -CLEANFILES += efiemu32.o efiemu64.o efiemu64_c.o efiemu64_s.o -pkglib_DATA += efiemu32.o efiemu64.o - -endif - BOOTTARGET=cd QEMU32=qemu-system-i386 diff --git a/conf/i386-qemu.rmk b/conf/i386-qemu.rmk index 416fbc0f3..5ec3c7eea 100644 --- a/conf/i386-qemu.rmk +++ b/conf/i386-qemu.rmk @@ -31,6 +31,7 @@ kernel_img_SOURCES = kern/i386/qemu/startup.S \ kern/env.c \ term/i386/pc/vga_text.c term/i386/vga_common.c \ symlist.c +kernel_img_HEADERS += i386/pit.h kernel_img_CFLAGS = $(COMMON_CFLAGS) -DGRUB_BOOT_MACHINE_LINK_ADDR=$(GRUB_BOOT_MACHINE_LINK_ADDR) kernel_img_ASFLAGS = $(COMMON_ASFLAGS) -DGRUB_KERNEL_MACHINE_LINK_ADDR=$(GRUB_KERNEL_MACHINE_LINK_ADDR) kernel_img_LDFLAGS += $(COMMON_LDFLAGS) $(TARGET_IMG_LDFLAGS)$(GRUB_KERNEL_MACHINE_LINK_ADDR) @@ -61,6 +62,16 @@ datetime_mod_SOURCES = lib/cmos_datetime.c datetime_mod_CFLAGS = $(COMMON_CFLAGS) datetime_mod_LDFLAGS = $(COMMON_LDFLAGS) +pkglib_MODULES += efiemu.mod +efiemu_mod_SOURCES = efiemu/main.c efiemu/i386/loadcore32.c \ + efiemu/i386/loadcore64.c efiemu/i386/nocfgtables.c \ + efiemu/mm.c efiemu/loadcore_common.c efiemu/symbols.c \ + efiemu/loadcore32.c efiemu/loadcore64.c \ + efiemu/prepare32.c efiemu/prepare64.c efiemu/pnvram.c \ + efiemu/i386/coredetect.c +efiemu_mod_CFLAGS = $(COMMON_CFLAGS) +efiemu_mod_LDFLAGS = $(COMMON_LDFLAGS) + BOOTTARGET=qemu QEMU32=qemu-system-i386 diff --git a/conf/i386.rmk b/conf/i386.rmk index 004dab7f3..fb23e879b 100644 --- a/conf/i386.rmk +++ b/conf/i386.rmk @@ -105,6 +105,59 @@ play_mod_SOURCES = commands/i386/pc/play.c play_mod_CFLAGS = $(COMMON_CFLAGS) play_mod_LDFLAGS = $(COMMON_LDFLAGS) +pkglib_MODULES += xnu.mod +xnu_mod_SOURCES = loader/xnu_resume.c loader/i386/xnu.c \ + loader/macho32.c loader/macho64.c loader/macho.c loader/xnu.c +xnu_mod_CFLAGS = $(COMMON_CFLAGS) +xnu_mod_LDFLAGS = $(COMMON_LDFLAGS) +xnu_mod_ASFLAGS = $(COMMON_ASFLAGS) + +ifeq ($(enable_efiemu), yes) + +efiemu32.o: efiemu/runtime/efiemu.c $(TARGET_OBJ2ELF) + -rm -f $@ +ifeq ($(TARGET_APPLE_CC), 1) + -rm -f $@.bin + $(TARGET_CC) -c -m32 -DELF32 -DAPPLE_CC -o $@.bin -Wall -Werror $< -nostdlib -O2 -I$(srcdir)/efiemu/runtime -I$(srcdir)/include -Iinclude + $(OBJCONV) -felf32 -nu -nd $@.bin $@ + -rm -f $@.bin +else + $(TARGET_CC) -c -m32 -DELF32 -o $@ -Wall -Werror $< -nostdlib -O2 -I$(srcdir)/efiemu/runtime -I$(srcdir)/include -Iinclude + if test ! -z $(TARGET_OBJ2ELF); then ./$(TARGET_OBJ2ELF) $@ || (rm -f $@; exit 1); fi +endif + +efiemu64_c.o: efiemu/runtime/efiemu.c +ifeq ($(TARGET_APPLE_CC), 1) + $(TARGET_CC) -c -m64 -DAPPLE_CC=1 -DELF64 -o $@ -Wall -Werror $< -nostdlib -mno-red-zone -O2 -I$(srcdir)/efiemu/runtime -I$(srcdir)/include -Iinclude +else + $(TARGET_CC) -c -m64 -DELF64 -o $@ -Wall -Werror $< -nostdlib -mcmodel=large -mno-red-zone -O2 -I$(srcdir)/efiemu/runtime -I$(srcdir)/include -Iinclude +endif + +efiemu64_s.o: efiemu/runtime/efiemu.S + -rm -f $@ +ifeq ($(TARGET_APPLE_CC), 1) + $(TARGET_CC) -c -m64 -DAPPLE_CC=1 -DELF64 -o $@ -Wall -Werror $< -nostdlib -mno-red-zone -O2 -I$(srcdir)/efiemu/runtime -I$(srcdir)/include -Iinclude +else + $(TARGET_CC) -c -m64 -DELF64 -o $@ -Wall -Werror $< -nostdlib -mcmodel=large -mno-red-zone -O2 -I$(srcdir)/efiemu/runtime -I$(srcdir)/include -Iinclude +endif + +efiemu64.o: efiemu64_c.o efiemu64_s.o $(TARGET_OBJ2ELF) + -rm -f $@ +ifeq ($(TARGET_APPLE_CC), 1) + -rm -f $@.bin + $(TARGET_CC) -m64 -o $@.bin -Wl,-r $^ -nostdlib + $(OBJCONV) -felf64 -nu -nd $@.bin $@ + -rm -f $@.bin +else + $(TARGET_CC) -m64 -o $@ -Wl,-r $^ -nostdlib + if test ! -z $(TARGET_OBJ2ELF); then ./$(TARGET_OBJ2ELF) $@ || (rm -f $@; exit 1); fi +endif + +CLEANFILES += efiemu32.o efiemu64.o efiemu64_c.o efiemu64_s.o +pkglib_DATA += efiemu32.o efiemu64.o + +endif + linux.init.x86_64: $(srcdir)/tests/boot/linux.init-x86_64.S $(TARGET_CC) -o $@ $< -m64 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" @@ -161,6 +214,10 @@ bootcheck-linux-x86_64: linux-initramfs.x86_64 $(GRUB_PAYLOADS_DIR)/linux.x86_64 BOOTCHECKS += bootcheck-linux-i386 bootcheck-linux-x86_64 \ bootcheck-kfreebsd-i386 bootcheck-knetbsd-x86_64 +ifneq ($(platform), coreboot) +BOOTCHECKS += bootcheck-kfreebsd-i386 +endif + .PHONY: bootcheck-linux-i386 bootcheck-linux-x86_64 \ bootcheck-kfreebsd-i386 bootcheck-kfreebsd-x86_64 \ bootcheck-knetbsd-i386 bootcheck-knetbsd-x86_64 diff --git a/conf/x86-efi.rmk b/conf/x86-efi.rmk index 68e1b3c7c..7bc0eeea0 100644 --- a/conf/x86-efi.rmk +++ b/conf/x86-efi.rmk @@ -87,13 +87,6 @@ efi_gop_mod_SOURCES = video/efi_gop.c efi_gop_mod_CFLAGS = $(COMMON_CFLAGS) efi_gop_mod_LDFLAGS = $(COMMON_LDFLAGS) -pkglib_MODULES += xnu.mod -xnu_mod_SOURCES = loader/xnu_resume.c loader/i386/xnu.c \ - loader/macho32.c loader/macho64.c loader/macho.c loader/xnu.c -xnu_mod_CFLAGS = $(COMMON_CFLAGS) -xnu_mod_LDFLAGS = $(COMMON_LDFLAGS) -xnu_mod_ASFLAGS = $(COMMON_ASFLAGS) - BOOTTARGET=cd include $(srcdir)/conf/i386.mk diff --git a/configure.ac b/configure.ac index 1f7efd78a..810b8e78d 100644 --- a/configure.ac +++ b/configure.ac @@ -542,6 +542,12 @@ AC_ARG_ENABLE([efiemu], if test x"$enable_efiemu" = xno ; then efiemu_excuse="explicitly disabled" fi +if test x"$target_cpu" != xi386 ; then + efiemu_excuse="only available on i386" +fi +if test x"$platform" != xefi ; then + efiemu_excuse="not available on efi" +fi if test x"$efiemu_excuse" = x ; then AC_CACHE_CHECK([whether options required for efiemu work], grub_cv_cc_efiemu, [ CFLAGS="$CFLAGS -m64 -mcmodel=large -mno-red-zone -nostdlib" diff --git a/efiemu/i386/coredetect.c b/efiemu/i386/coredetect.c index 828508dee..975c4aa5d 100644 --- a/efiemu/i386/coredetect.c +++ b/efiemu/i386/coredetect.c @@ -17,7 +17,6 @@ */ #include -#include #include #define cpuid(num,a,b,c,d) \ diff --git a/include/grub/i386/pc/efiemu.h b/efiemu/i386/nocfgtables.c similarity index 75% rename from include/grub/i386/pc/efiemu.h rename to efiemu/i386/nocfgtables.c index f269dd085..775f1d03a 100644 --- a/include/grub/i386/pc/efiemu.h +++ b/efiemu/i386/nocfgtables.c @@ -1,3 +1,4 @@ +/* Register SMBIOS and ACPI tables. */ /* * GRUB -- GRand Unified Bootloader * Copyright (C) 2009 Free Software Foundation, Inc. @@ -16,9 +17,14 @@ * along with GRUB. If not, see . */ -#ifndef GRUB_MACHINE_EFI_EMU_HEADER -#define GRUB_MACHINE_EFI_EMU_HEADER 1 +#include +#include +#include +#include +#include -grub_err_t grub_machine_efiemu_init_tables (void); - -#endif +grub_err_t +grub_machine_efiemu_init_tables (void) +{ + return GRUB_ERR_NONE; +} diff --git a/efiemu/i386/pc/cfgtables.c b/efiemu/i386/pc/cfgtables.c index 9c6b4e55e..9287d3a94 100644 --- a/efiemu/i386/pc/cfgtables.c +++ b/efiemu/i386/pc/cfgtables.c @@ -19,7 +19,6 @@ #include #include -#include #include #include #include diff --git a/efiemu/loadcore.c b/efiemu/loadcore.c index 4bf26ee44..edadd4580 100644 --- a/efiemu/loadcore.c +++ b/efiemu/loadcore.c @@ -22,7 +22,6 @@ #include #include #include -#include #include /* ELF symbols and their values */ diff --git a/efiemu/main.c b/efiemu/main.c index 7ebbae946..3f5fb9435 100644 --- a/efiemu/main.c +++ b/efiemu/main.c @@ -29,7 +29,6 @@ #include #include #include -#include #include /* System table. Two version depending on mode */ diff --git a/include/grub/efiemu/efiemu.h b/include/grub/efiemu/efiemu.h index 1cddbca7c..fb1b69751 100644 --- a/include/grub/efiemu/efiemu.h +++ b/include/grub/efiemu/efiemu.h @@ -282,4 +282,6 @@ grub_efiemu_set_virtual_address_map (grub_efi_uintn_t memory_map_size, __attribute__ ((unused)), grub_efi_memory_descriptor_t *virtual_map); +grub_err_t grub_machine_efiemu_init_tables (void); + #endif /* ! GRUB_EFI_EMU_HEADER */ diff --git a/loader/i386/xnu.c b/loader/i386/xnu.c index 2aec590fb..d110293e0 100644 --- a/loader/i386/xnu.c +++ b/loader/i386/xnu.c @@ -123,6 +123,7 @@ static grub_uint64_t guessfsb (void) { const grub_uint64_t sane_value = 100000000; +#ifndef GRUB_MACHINE_IEEE1275 grub_uint32_t manufacturer[3], max_cpuid, capabilities, msrlow; grub_uint64_t start_tsc; grub_uint64_t end_tsc; @@ -208,6 +209,9 @@ guessfsb (void) return grub_divmod64 (2000 * tsc_ticks_per_ms, ((msrlow >> 7) & 0x3e) + ((msrlow >> 14) & 1), 0); +#else + return sane_value; +#endif } struct property_descriptor From 7f5320b3194675c5c56e179aea01ca4bec5fd2fd Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 2 May 2010 23:09:45 +0200 Subject: [PATCH 123/178] Compress miniroot and decrease timeout --- conf/common.rmk | 2 +- conf/i386.rmk | 26 ++++++++++++++------------ tests/boot/kfreebsd.cfg | 2 +- tests/boot/knetbsd.cfg | 2 +- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/conf/common.rmk b/conf/common.rmk index a8c881276..29d69f27a 100644 --- a/conf/common.rmk +++ b/conf/common.rmk @@ -866,7 +866,7 @@ grub_mkpasswd_pbkdf2_CFLAGS += -Wno-missing-field-initializers -Wno-error -I$(sr # Randomly generated SUCCESSFUL_BOOT_STRING=3e49994fd5d82b7c9298d672d774080d # tianocore cd access is very slow -BOOTCHECK_TIMEOUT=600 +BOOTCHECK_TIMEOUT=180 bootcheck: $(BOOTCHECKS) diff --git a/conf/i386.rmk b/conf/i386.rmk index fb23e879b..b7c9b293d 100644 --- a/conf/i386.rmk +++ b/conf/i386.rmk @@ -182,6 +182,9 @@ linux-initramfs.%: linux.init.% Makefile kfreebsd-mfsroot.%: kfreebsd.init.% Makefile TDIR=`mktemp -d`; mkdir $$TDIR/dev; mkdir $$TDIR/sbin; cp $< $$TDIR/sbin/init; makefs -t ffs -s 30m -f 1000 -o minfree=0,version=1 $@ $$TDIR; rm -rf $$TDIR +%.gz: % + gzip < $< > $@ + knetbsd.image.%: knetbsd.init.% TDIR=`mktemp -d` && mkdir $$TDIR/dev && mkdir $$TDIR/sbin && cp $< $$TDIR/sbin/init && makefs -t ffs -s 64k -f 10 -o minfree=0,version=1 $@ $$TDIR && rm -rf $$TDIR @@ -193,17 +196,17 @@ knetbsd.miniroot-image.x86_64: knetbsd.image.x86_64 $(GRUB_PAYLOADS_DIR)/knetbsd CLEANFILES += linux.init.i386 kfreebsd.init.i386 linux.init.x86_64 linux-initramfs.i386 linux-initramfs.x86_64 -bootcheck-kfreebsd-i386: kfreebsd-mfsroot.i386 $(GRUB_PAYLOADS_DIR)/kfreebsd.i386 $(GRUB_PAYLOADS_DIR)/kfreebsd_env.i386 $(srcdir)/tests/boot/kfreebsd.cfg grub-shell - timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=$(QEMU32) --files=/mfsroot=kfreebsd-mfsroot.i386 --files=/kfreebsd=$(GRUB_PAYLOADS_DIR)/kfreebsd.i386 --files=/kfreebsd_env=$(GRUB_PAYLOADS_DIR)/kfreebsd_env.i386 $(srcdir)/tests/boot/kfreebsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null +bootcheck-kfreebsd-i386: kfreebsd-mfsroot.i386.gz $(GRUB_PAYLOADS_DIR)/kfreebsd.i386 $(GRUB_PAYLOADS_DIR)/kfreebsd_env.i386 $(srcdir)/tests/boot/kfreebsd.cfg grub-shell + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=$(QEMU32) --files=/mfsroot.gz=kfreebsd-mfsroot.i386.gz --files=/kfreebsd=$(GRUB_PAYLOADS_DIR)/kfreebsd.i386 --files=/kfreebsd_env=$(GRUB_PAYLOADS_DIR)/kfreebsd_env.i386 $(srcdir)/tests/boot/kfreebsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null -bootcheck-kfreebsd-x86_64: kfreebsd-mfsroot.x86_64 $(GRUB_PAYLOADS_DIR)/kfreebsd.x86_64 $(GRUB_PAYLOADS_DIR)/kfreebsd_env.x86_64 $(srcdir)/tests/boot/kfreebsd.cfg grub-shell - timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/mfsroot=kfreebsd-mfsroot.x86_64 --files=/kfreebsd=$(GRUB_PAYLOADS_DIR)/kfreebsd.x86_64 --files=/kfreebsd_env=$(GRUB_PAYLOADS_DIR)/kfreebsd_env.x86_64 $(srcdir)/tests/boot/kfreebsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null +bootcheck-kfreebsd-x86_64: kfreebsd-mfsroot.x86_64.gz $(GRUB_PAYLOADS_DIR)/kfreebsd.x86_64 $(GRUB_PAYLOADS_DIR)/kfreebsd_env.x86_64 $(srcdir)/tests/boot/kfreebsd.cfg grub-shell + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/mfsroot=kfreebsd-mfsroot.x86_64.gz --files=/kfreebsd=$(GRUB_PAYLOADS_DIR)/kfreebsd.x86_64 --files=/kfreebsd_env=$(GRUB_PAYLOADS_DIR)/kfreebsd_env.x86_64 $(srcdir)/tests/boot/kfreebsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null -bootcheck-knetbsd-i386: knetbsd.miniroot-image.i386 $(GRUB_PAYLOADS_DIR)/knetbsd.i386 $(srcdir)/tests/boot/knetbsd.cfg knetbsd.miniroot-image.i386 grub-shell - timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-i386 --files=/miniroot=knetbsd.miniroot-image.i386 --files=/knetbsd=$(GRUB_PAYLOADS_DIR)/knetbsd.i386 $(srcdir)/tests/boot/knetbsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null +bootcheck-knetbsd-i386: knetbsd.miniroot-image.i386.gz $(GRUB_PAYLOADS_DIR)/knetbsd.i386 $(srcdir)/tests/boot/knetbsd.cfg knetbsd.miniroot-image.i386 grub-shell + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-i386 --files=/miniroot.gz=knetbsd.miniroot-image.i386.gz --files=/knetbsd=$(GRUB_PAYLOADS_DIR)/knetbsd.i386 $(srcdir)/tests/boot/knetbsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null -bootcheck-knetbsd-x86_64: knetbsd.miniroot-image.x86_64 $(GRUB_PAYLOADS_DIR)/knetbsd.x86_64 $(srcdir)/tests/boot/knetbsd.cfg knetbsd.miniroot-image.x86_64 grub-shell - timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/miniroot=knetbsd.miniroot-image.x86_64 --files=/knetbsd=$(GRUB_PAYLOADS_DIR)/knetbsd.x86_64 $(srcdir)/tests/boot/knetbsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null +bootcheck-knetbsd-x86_64: knetbsd.miniroot-image.x86_64.gz $(GRUB_PAYLOADS_DIR)/knetbsd.x86_64 $(srcdir)/tests/boot/knetbsd.cfg knetbsd.miniroot-image.x86_64 grub-shell + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/miniroot.gz=knetbsd.miniroot-image.x86_64.gz --files=/knetbsd=$(GRUB_PAYLOADS_DIR)/knetbsd.x86_64 $(srcdir)/tests/boot/knetbsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null bootcheck-linux-i386: linux-initramfs.i386 $(GRUB_PAYLOADS_DIR)/linux.i386 $(srcdir)/tests/boot/linux.cfg grub-shell timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=$(QEMU32) --files=/initrd=linux-initramfs.i386 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.i386 $(srcdir)/tests/boot/linux.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null @@ -211,11 +214,10 @@ bootcheck-linux-i386: linux-initramfs.i386 $(GRUB_PAYLOADS_DIR)/linux.i386 $(src bootcheck-linux-x86_64: linux-initramfs.x86_64 $(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/tests/boot/linux.cfg grub-shell timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/initrd=linux-initramfs.x86_64 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/tests/boot/linux.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null -BOOTCHECKS += bootcheck-linux-i386 bootcheck-linux-x86_64 \ - bootcheck-kfreebsd-i386 bootcheck-knetbsd-x86_64 +BOOTCHECKS += bootcheck-linux-i386 bootcheck-linux-x86_64 -ifneq ($(platform), coreboot) -BOOTCHECKS += bootcheck-kfreebsd-i386 +ifneq ($(platform), coreboot) +BOOTCHECKS += bootcheck-kfreebsd-i386 bootcheck-knetbsd-x86_64 endif .PHONY: bootcheck-linux-i386 bootcheck-linux-x86_64 \ diff --git a/tests/boot/kfreebsd.cfg b/tests/boot/kfreebsd.cfg index 8f339cd7f..5534f3c03 100644 --- a/tests/boot/kfreebsd.cfg +++ b/tests/boot/kfreebsd.cfg @@ -1,6 +1,6 @@ kfreebsd /kfreebsd -h kfreebsd_loadenv /kfreebsd_env -kfreebsd_module /mfsroot type=mfs_root +kfreebsd_module /mfsroot.gz type=mfs_root set kFreeBSD.hw.hasbrokenint12=1 boot # Shouln't happen diff --git a/tests/boot/knetbsd.cfg b/tests/boot/knetbsd.cfg index ad2258dce..f88a846e1 100644 --- a/tests/boot/knetbsd.cfg +++ b/tests/boot/knetbsd.cfg @@ -1,5 +1,5 @@ knetbsd /knetbsd -h -knetbsd_module_elf /miniroot +knetbsd_module_elf /miniroot.gz boot # Shouln't happen halt From 86fbf9798a8079d3863ed21aadafb17365d77611 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Mon, 3 May 2010 01:46:43 +0200 Subject: [PATCH 124/178] Fix makefile problem due to compression --- conf/i386.rmk | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/conf/i386.rmk b/conf/i386.rmk index b7c9b293d..0b707ca6a 100644 --- a/conf/i386.rmk +++ b/conf/i386.rmk @@ -179,19 +179,19 @@ knetbsd.init.x86_64: $(srcdir)/tests/boot/knetbsd.init-x86_64.S linux-initramfs.%: linux.init.% Makefile TDIR=`mktemp -d`; cp $< $$TDIR/init; (cd $$TDIR; echo ./init | cpio --quiet --dereference -o -H newc) | gzip > $@; rm -rf $$TDIR -kfreebsd-mfsroot.%: kfreebsd.init.% Makefile - TDIR=`mktemp -d`; mkdir $$TDIR/dev; mkdir $$TDIR/sbin; cp $< $$TDIR/sbin/init; makefs -t ffs -s 30m -f 1000 -o minfree=0,version=1 $@ $$TDIR; rm -rf $$TDIR - -%.gz: % +%.gz: %.img gzip < $< > $@ +kfreebsd-mfsroot.%.img: kfreebsd.init.% Makefile + TDIR=`mktemp -d`; mkdir $$TDIR/dev; mkdir $$TDIR/sbin; cp $< $$TDIR/sbin/init; makefs -t ffs -s 30m -f 1000 -o minfree=0,version=1 $@ $$TDIR; rm -rf $$TDIR + knetbsd.image.%: knetbsd.init.% TDIR=`mktemp -d` && mkdir $$TDIR/dev && mkdir $$TDIR/sbin && cp $< $$TDIR/sbin/init && makefs -t ffs -s 64k -f 10 -o minfree=0,version=1 $@ $$TDIR && rm -rf $$TDIR -knetbsd.miniroot-image.i386: knetbsd.image.i386 $(GRUB_PAYLOADS_DIR)/knetbsd.miniroot.i386 +knetbsd.miniroot-image.i386.img: knetbsd.image.i386 $(GRUB_PAYLOADS_DIR)/knetbsd.miniroot.i386 $(OBJCOPY) --add-section=miniroot=$< $(GRUB_PAYLOADS_DIR)/knetbsd.miniroot.i386 $@ -knetbsd.miniroot-image.x86_64: knetbsd.image.x86_64 $(GRUB_PAYLOADS_DIR)/knetbsd.miniroot.x86_64 +knetbsd.miniroot-image.x86_64.img: knetbsd.image.x86_64 $(GRUB_PAYLOADS_DIR)/knetbsd.miniroot.x86_64 $(OBJCOPY) --add-section=miniroot=$< $(GRUB_PAYLOADS_DIR)/knetbsd.miniroot.x86_64 $@ CLEANFILES += linux.init.i386 kfreebsd.init.i386 linux.init.x86_64 linux-initramfs.i386 linux-initramfs.x86_64 @@ -200,7 +200,7 @@ bootcheck-kfreebsd-i386: kfreebsd-mfsroot.i386.gz $(GRUB_PAYLOADS_DIR)/kfreebsd. timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=$(QEMU32) --files=/mfsroot.gz=kfreebsd-mfsroot.i386.gz --files=/kfreebsd=$(GRUB_PAYLOADS_DIR)/kfreebsd.i386 --files=/kfreebsd_env=$(GRUB_PAYLOADS_DIR)/kfreebsd_env.i386 $(srcdir)/tests/boot/kfreebsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null bootcheck-kfreebsd-x86_64: kfreebsd-mfsroot.x86_64.gz $(GRUB_PAYLOADS_DIR)/kfreebsd.x86_64 $(GRUB_PAYLOADS_DIR)/kfreebsd_env.x86_64 $(srcdir)/tests/boot/kfreebsd.cfg grub-shell - timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/mfsroot=kfreebsd-mfsroot.x86_64.gz --files=/kfreebsd=$(GRUB_PAYLOADS_DIR)/kfreebsd.x86_64 --files=/kfreebsd_env=$(GRUB_PAYLOADS_DIR)/kfreebsd_env.x86_64 $(srcdir)/tests/boot/kfreebsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/mfsroot.gz=kfreebsd-mfsroot.x86_64.gz --files=/kfreebsd=$(GRUB_PAYLOADS_DIR)/kfreebsd.x86_64 --files=/kfreebsd_env=$(GRUB_PAYLOADS_DIR)/kfreebsd_env.x86_64 $(srcdir)/tests/boot/kfreebsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null bootcheck-knetbsd-i386: knetbsd.miniroot-image.i386.gz $(GRUB_PAYLOADS_DIR)/knetbsd.i386 $(srcdir)/tests/boot/knetbsd.cfg knetbsd.miniroot-image.i386 grub-shell timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-i386 --files=/miniroot.gz=knetbsd.miniroot-image.i386.gz --files=/knetbsd=$(GRUB_PAYLOADS_DIR)/knetbsd.i386 $(srcdir)/tests/boot/knetbsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null From a57c242287d9e8d7cd97020a0b41cc196b5ed931 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Mon, 3 May 2010 22:53:51 +0200 Subject: [PATCH 125/178] Add missing token --- util/grub-install.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/grub-install.in b/util/grub-install.in index 8a93ace8c..0db216fd5 100644 --- a/util/grub-install.in +++ b/util/grub-install.in @@ -342,7 +342,7 @@ else prefix_drive=`$grub_probe --target=drive --device ${grub_device}` || exit 1 fi -case "${target_cpu}-${platform}" +case "${target_cpu}-${platform}" in i386-pc) mkimage_target=i386-pc ;; sparc64-ieee1275) mkimage_target=sparc64-ieee1275-raw ;; mips-yeeloong) mkimage_target=mipsel-yeeloong-elf ;; From a1a5c869850fe45fe0dba4000d522045ba40e5ca Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Mon, 3 May 2010 22:54:46 +0200 Subject: [PATCH 126/178] Fix default mkimage path determination --- Makefile.in | 2 +- util/grub-mkimage.c | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Makefile.in b/Makefile.in index 3df8b1b72..54f17f27f 100644 --- a/Makefile.in +++ b/Makefile.in @@ -90,7 +90,7 @@ GNULIB_CFLAGS = $(GNULIB_UTIL_CFLAGS) $(POSIX_CFLAGS) ASFLAGS = @ASFLAGS@ LDFLAGS = @LDFLAGS@ $(LIBS) CPPFLAGS = @CPPFLAGS@ -I$(builddir) -I$(builddir)/include -I$(srcdir)/gnulib -I$(srcdir)/include -Wall -W \ - -DGRUB_LIBDIR=\"$(libdir)\" -DLOCALEDIR=\"$(localedir)\" + -DGRUB_PKGLIBROOTDIR=\"$(libdir)/`echo @PACKAGE_TARNAME@ | sed '$(transform)'`\" -DLOCALEDIR=\"$(localedir)\" TARGET_CC = @TARGET_CC@ TARGET_CFLAGS = -ffreestanding @TARGET_CFLAGS@ TARGET_ASFLAGS = -nostdinc -fno-builtin @TARGET_ASFLAGS@ diff --git a/util/grub-mkimage.c b/util/grub-mkimage.c index cc4a225d4..5602b3a96 100644 --- a/util/grub-mkimage.c +++ b/util/grub-mkimage.c @@ -1182,7 +1182,7 @@ Make a bootable image of GRUB.\n\ \n\ Report bugs to <%s>.\n\ "), - program_name, GRUB_LIBDIR, DEFAULT_DIRECTORY, + program_name, GRUB_PKGLIBROOTDIR, DEFAULT_DIRECTORY, formats, PACKAGE_BUGREPORT); free (formats); @@ -1320,14 +1320,15 @@ main (int argc, char *argv[]) last = strchr (last + 1, '-'); if (!last) last = image_target->name + strlen (image_target->name); - dir = xmalloc (sizeof (GRUB_LIBDIR) + (last - image_target->name)); - memcpy (dir, GRUB_LIBDIR, sizeof (GRUB_LIBDIR) - 1); - memcpy (dir + sizeof (GRUB_LIBDIR) - 1, image_target->name, + dir = xmalloc (sizeof (GRUB_PKGLIBROOTDIR) + (last - image_target->name)); + memcpy (dir, GRUB_PKGLIBROOTDIR, sizeof (GRUB_PKGLIBROOTDIR) - 1); + *(dir + sizeof (GRUB_PKGLIBROOTDIR) - 1) = '/'; + memcpy (dir + sizeof (GRUB_PKGLIBROOTDIR), image_target->name, last - image_target->name); - *(dir + sizeof (GRUB_LIBDIR) - 1 + (last - image_target->name)) = 0; + *(dir + sizeof (GRUB_PKGLIBROOTDIR) + (last - image_target->name)) = 0; } - generate_image (dir ? : GRUB_LIBDIR, prefix ? : DEFAULT_DIRECTORY, fp, + generate_image (dir, prefix ? : DEFAULT_DIRECTORY, fp, argv + optind, memdisk, font, config, image_target, note); From ee0b981c202bf39e9c8bca2b4369776cbe026985 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Mon, 3 May 2010 22:58:27 +0200 Subject: [PATCH 127/178] Fix BSD tests. Move BSD bootchecks to i386.rmk in hope to enable them one day everywhere --- conf/i386-pc.rmk | 4 ---- conf/i386.rmk | 12 ++++++++++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/conf/i386-pc.rmk b/conf/i386-pc.rmk index 3f0eeb611..662b398e5 100644 --- a/conf/i386-pc.rmk +++ b/conf/i386-pc.rmk @@ -235,10 +235,6 @@ bootcheck-linux16-x86_64: linux-initramfs.x86_64 $(GRUB_PAYLOADS_DIR)/linux.x86_ timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/initrd=linux-initramfs.x86_64 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/tests/boot/linux16.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null BOOTCHECKS += bootcheck-linux16-i386 bootcheck-linux16-x86_64 -# It is defined in i386.rmk but requires ACPI -BOOTCHECKS += bootcheck-kfreebsd-x86_64 -# It is defined in i386.rmk but crashes early on non-BIOS -BOOTCHECKS += bootcheck-knetbsd-i386 include $(srcdir)/conf/i386.mk include $(srcdir)/conf/common.mk diff --git a/conf/i386.rmk b/conf/i386.rmk index 0b707ca6a..44a5cb185 100644 --- a/conf/i386.rmk +++ b/conf/i386.rmk @@ -202,10 +202,10 @@ bootcheck-kfreebsd-i386: kfreebsd-mfsroot.i386.gz $(GRUB_PAYLOADS_DIR)/kfreebsd. bootcheck-kfreebsd-x86_64: kfreebsd-mfsroot.x86_64.gz $(GRUB_PAYLOADS_DIR)/kfreebsd.x86_64 $(GRUB_PAYLOADS_DIR)/kfreebsd_env.x86_64 $(srcdir)/tests/boot/kfreebsd.cfg grub-shell timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/mfsroot.gz=kfreebsd-mfsroot.x86_64.gz --files=/kfreebsd=$(GRUB_PAYLOADS_DIR)/kfreebsd.x86_64 --files=/kfreebsd_env=$(GRUB_PAYLOADS_DIR)/kfreebsd_env.x86_64 $(srcdir)/tests/boot/kfreebsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null -bootcheck-knetbsd-i386: knetbsd.miniroot-image.i386.gz $(GRUB_PAYLOADS_DIR)/knetbsd.i386 $(srcdir)/tests/boot/knetbsd.cfg knetbsd.miniroot-image.i386 grub-shell +bootcheck-knetbsd-i386: knetbsd.miniroot-image.i386.gz $(GRUB_PAYLOADS_DIR)/knetbsd.i386 $(srcdir)/tests/boot/knetbsd.cfg grub-shell timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-i386 --files=/miniroot.gz=knetbsd.miniroot-image.i386.gz --files=/knetbsd=$(GRUB_PAYLOADS_DIR)/knetbsd.i386 $(srcdir)/tests/boot/knetbsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null -bootcheck-knetbsd-x86_64: knetbsd.miniroot-image.x86_64.gz $(GRUB_PAYLOADS_DIR)/knetbsd.x86_64 $(srcdir)/tests/boot/knetbsd.cfg knetbsd.miniroot-image.x86_64 grub-shell +bootcheck-knetbsd-x86_64: knetbsd.miniroot-image.x86_64.gz $(GRUB_PAYLOADS_DIR)/knetbsd.x86_64 $(srcdir)/tests/boot/knetbsd.cfg grub-shell timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/miniroot.gz=knetbsd.miniroot-image.x86_64.gz --files=/knetbsd=$(GRUB_PAYLOADS_DIR)/knetbsd.x86_64 $(srcdir)/tests/boot/knetbsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null bootcheck-linux-i386: linux-initramfs.i386 $(GRUB_PAYLOADS_DIR)/linux.i386 $(srcdir)/tests/boot/linux.cfg grub-shell @@ -217,9 +217,17 @@ bootcheck-linux-x86_64: linux-initramfs.x86_64 $(GRUB_PAYLOADS_DIR)/linux.x86_64 BOOTCHECKS += bootcheck-linux-i386 bootcheck-linux-x86_64 ifneq ($(platform), coreboot) +# Crashes because memory at 0-0x1000 is occupied BOOTCHECKS += bootcheck-kfreebsd-i386 bootcheck-knetbsd-x86_64 endif +ifeq ($(platform), pc) +# Requires ACPI +BOOTCHECKS += bootcheck-kfreebsd-x86_64 +# Crashes early on non-BIOS +BOOTCHECKS += bootcheck-knetbsd-i386 +endif + .PHONY: bootcheck-linux-i386 bootcheck-linux-x86_64 \ bootcheck-kfreebsd-i386 bootcheck-kfreebsd-x86_64 \ bootcheck-knetbsd-i386 bootcheck-knetbsd-x86_64 From cfdcef121fa3b4fe6782498a3bf483472bb1a244 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Mon, 3 May 2010 23:00:49 +0200 Subject: [PATCH 128/178] Fix efiemu compilation condition --- configure.ac | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 810b8e78d..60c989828 100644 --- a/configure.ac +++ b/configure.ac @@ -545,7 +545,7 @@ fi if test x"$target_cpu" != xi386 ; then efiemu_excuse="only available on i386" fi -if test x"$platform" != xefi ; then +if test x"$platform" = xefi ; then efiemu_excuse="not available on efi" fi if test x"$efiemu_excuse" = x ; then From 0cb0344d11b73b1e23e858fe344760651e9ea292 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Mon, 3 May 2010 23:02:18 +0200 Subject: [PATCH 129/178] Fix overflow and add more dprintfs --- lib/relocator.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/relocator.c b/lib/relocator.c index 6fbdb71d7..5772ccc7d 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -200,15 +200,21 @@ allocate_inreg (grub_phys_addr_t paddr, grub_size_t size, { struct grub_mm_header *foll = NULL; grub_addr_t vaddr = (grub_addr_t) hb + (paddr - grub_vtop (hb)); + + grub_dprintf ("relocator", + "inreg paddr = 0x%x, size = %d, hb = %p, hbp = %p, rb = %p, vaddr = 0x%x\n", + paddr, size, hb, hbp, rb, vaddr); if (ALIGN_UP (vaddr + size, GRUB_MM_ALIGN) + GRUB_MM_ALIGN <= (grub_addr_t) (hb + hb->size)) { foll = (void *) ALIGN_UP (vaddr + size, GRUB_MM_ALIGN); foll->magic = GRUB_MM_FREE_MAGIC; - foll->size = hb->size - (foll - hb); + foll->size = hb + hb->size - foll; } + grub_dprintf ("relocator", "foll = %p, foll->size = %d\n", foll, foll->size); + if (vaddr - (grub_addr_t) hb >= sizeof (*hb)) { hb->size = ((vaddr - (grub_addr_t) hb) >> GRUB_MM_ALIGN_LOG2); @@ -431,12 +437,16 @@ malloc_in_range (struct grub_relocator *rel, p = r->first; do { + if ((grub_addr_t) p < (grub_addr_t) (r + 1) + || (grub_addr_t) p >= (grub_addr_t) (r + 1) + r->size) + grub_fatal ("%d: out of range pointer: %p\n", __LINE__, p); maxevents += 2; p = p->next; } while (p != r->first); maxevents += 4; } + if (collisioncheck && rel) { struct grub_relocator_chunk *chunk; @@ -617,6 +627,7 @@ malloc_in_range (struct grub_relocator *rel, eventt = events; events = t; } + { unsigned i; for (i = 0; i < (BITS_IN_BYTE * sizeof (grub_addr_t) / DIGITSORT_BITS); @@ -1146,6 +1157,7 @@ malloc_in_range (struct grub_relocator *rel, res->size = size; grub_dprintf ("relocator", "allocated: 0x%lx+0x%lx\n", (unsigned long) target, (unsigned long) size); + return 1; } @@ -1190,7 +1202,6 @@ grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, || (target <= chunk->target && chunk->target < target + size)) return grub_error (GRUB_ERR_BAD_ARGUMENT, "overlap detected"); - chunk = grub_malloc (sizeof (struct grub_relocator_chunk)); if (!chunk) return grub_errno; From c3a4565068d248737de7a5cc0d159cef8181732f Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 4 May 2010 11:32:10 +0200 Subject: [PATCH 130/178] Don't access NULL in dprintf --- lib/relocator.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/relocator.c b/lib/relocator.c index 5772ccc7d..c30177591 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -211,10 +211,10 @@ allocate_inreg (grub_phys_addr_t paddr, grub_size_t size, foll = (void *) ALIGN_UP (vaddr + size, GRUB_MM_ALIGN); foll->magic = GRUB_MM_FREE_MAGIC; foll->size = hb + hb->size - foll; + grub_dprintf ("relocator", "foll = %p, foll->size = %d\n", foll, + foll->size); } - grub_dprintf ("relocator", "foll = %p, foll->size = %d\n", foll, foll->size); - if (vaddr - (grub_addr_t) hb >= sizeof (*hb)) { hb->size = ((vaddr - (grub_addr_t) hb) >> GRUB_MM_ALIGN_LOG2); From 4e75dd1212e66914f83e360bf4e9056da8aaf776 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 4 May 2010 17:25:29 +0200 Subject: [PATCH 131/178] fix warnings on x86_64 --- lib/relocator.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/relocator.c b/lib/relocator.c index c30177591..9f9927929 100644 --- a/lib/relocator.c +++ b/lib/relocator.c @@ -201,9 +201,10 @@ allocate_inreg (grub_phys_addr_t paddr, grub_size_t size, struct grub_mm_header *foll = NULL; grub_addr_t vaddr = (grub_addr_t) hb + (paddr - grub_vtop (hb)); - grub_dprintf ("relocator", - "inreg paddr = 0x%x, size = %d, hb = %p, hbp = %p, rb = %p, vaddr = 0x%x\n", - paddr, size, hb, hbp, rb, vaddr); + grub_dprintf ("relocator", "inreg paddr = 0x%lx, size = %lu," + " hb = %p, hbp = %p, rb = %p, vaddr = 0x%lx\n", + (unsigned long) paddr, (unsigned long) size, hb, hbp, + rb, (unsigned long) vaddr); if (ALIGN_UP (vaddr + size, GRUB_MM_ALIGN) + GRUB_MM_ALIGN <= (grub_addr_t) (hb + hb->size)) @@ -211,8 +212,8 @@ allocate_inreg (grub_phys_addr_t paddr, grub_size_t size, foll = (void *) ALIGN_UP (vaddr + size, GRUB_MM_ALIGN); foll->magic = GRUB_MM_FREE_MAGIC; foll->size = hb + hb->size - foll; - grub_dprintf ("relocator", "foll = %p, foll->size = %d\n", foll, - foll->size); + grub_dprintf ("relocator", "foll = %p, foll->size = %lu\n", foll, + (unsigned long) foll->size); } if (vaddr - (grub_addr_t) hb >= sizeof (*hb)) From 0cd8e62cfa74e68a03f016286ce1937f6906f352 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 5 May 2010 21:41:25 +0200 Subject: [PATCH 132/178] remove leftover modules --- conf/i386-ieee1275.rmk | 2 +- conf/i386-multiboot.rmk | 7 +------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/conf/i386-ieee1275.rmk b/conf/i386-ieee1275.rmk index 1b7460dc3..b12022780 100644 --- a/conf/i386-ieee1275.rmk +++ b/conf/i386-ieee1275.rmk @@ -36,7 +36,7 @@ sbin_SCRIPTS = grub-install grub_install_SOURCES = util/ieee1275/grub-install.in # Modules. -pkglib_MODULES = halt.mod suspend.mod aout.mod nand.mod datetime.mod mmap.mod +pkglib_MODULES = halt.mod suspend.mod nand.mod datetime.mod mmap.mod # For mmap.mod. mmap_mod_SOURCES = mmap/mmap.c mmap/i386/uppermem.c mmap/i386/mmap.c diff --git a/conf/i386-multiboot.rmk b/conf/i386-multiboot.rmk index ddfb7e283..6475e6763 100644 --- a/conf/i386-multiboot.rmk +++ b/conf/i386-multiboot.rmk @@ -36,7 +36,7 @@ bin_SCRIPTS += grub-mkrescue grub_mkrescue_SOURCES = util/grub-mkrescue.in # Modules. -pkglib_MODULES = linux.mod halt.mod datetime.mod mmap.mod +pkglib_MODULES = halt.mod datetime.mod mmap.mod # For mmap.mod. mmap_mod_SOURCES = mmap/mmap.c mmap/i386/uppermem.c mmap/i386/mmap.c @@ -44,11 +44,6 @@ mmap_mod_CFLAGS = $(COMMON_CFLAGS) mmap_mod_LDFLAGS = $(COMMON_LDFLAGS) mmap_mod_ASFLAGS = $(COMMON_ASFLAGS) -# For linux.mod. -linux_mod_SOURCES = loader/i386/linux.c -linux_mod_CFLAGS = $(COMMON_CFLAGS) -linux_mod_LDFLAGS = $(COMMON_LDFLAGS) - # For halt.mod. halt_mod_SOURCES = commands/halt.c halt_mod_CFLAGS = $(COMMON_CFLAGS) From f948a3ffab9076be1db1f9146e8319c4aea0db81 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 5 May 2010 21:42:39 +0200 Subject: [PATCH 133/178] respect GRUB_IEEE1275_FLAG_NO_PRE1_5M_CLAIM --- lib/ieee1275/relocator.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/ieee1275/relocator.c b/lib/ieee1275/relocator.c index bf7f4a821..947346d46 100644 --- a/lib/ieee1275/relocator.c +++ b/lib/ieee1275/relocator.c @@ -54,6 +54,18 @@ grub_relocator_firmware_fill_events (struct grub_relocator_mmap_event *events) if (type != GRUB_MACHINE_MEMORY_AVAILABLE) return 0; + if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_NO_PRE1_5M_CLAIM)) + { + if (addr + len <= 0x180000) + return 0; + + if (addr < 0x180000) + { + len = addr + len - 0x180000; + addr = 0x180000; + } + } + events[counter].type = REG_FIRMWARE_START; events[counter].pos = addr; counter++; From 65ce0931d0a8ef08395b2174f2e16914b2528b7e Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Thu, 13 May 2010 14:42:22 +0200 Subject: [PATCH 134/178] Fix inconsistent grub_efiemu_finish_boot_services return type --- efiemu/mm.c | 19 +++++++++++++++++++ include/grub/efiemu/efiemu.h | 10 +++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/efiemu/mm.c b/efiemu/mm.c index 4b293606f..de7d309be 100644 --- a/efiemu/mm.c +++ b/efiemu/mm.c @@ -323,6 +323,25 @@ grub_efiemu_get_memory_map (grub_efi_uintn_t *memory_map_size, return 1; } +grub_err_t +grub_efiemu_finish_boot_services (grub_efi_uintn_t *memory_map_size, + grub_efi_memory_descriptor_t *memory_map, + grub_efi_uintn_t *map_key, + grub_efi_uintn_t *descriptor_size, + grub_efi_uint32_t *descriptor_version) +{ + int val = grub_efiemu_get_memory_map (memory_map_size, + memory_map, map_key, + descriptor_size, + descriptor_version); + if (val == 1) + return GRUB_ERR_NONE; + if (val == -1) + return grub_errno; + return grub_error (GRUB_ERR_IO, "memory map buffer is too small"); +} + + /* Free everything */ grub_err_t grub_efiemu_mm_unload (void) diff --git a/include/grub/efiemu/efiemu.h b/include/grub/efiemu/efiemu.h index fb1b69751..56d4ea8ee 100644 --- a/include/grub/efiemu/efiemu.h +++ b/include/grub/efiemu/efiemu.h @@ -217,7 +217,15 @@ int grub_efiemu_get_memory_map (grub_efi_uintn_t *memory_map_size, grub_efi_uintn_t *map_key, grub_efi_uintn_t *descriptor_size, grub_efi_uint32_t *descriptor_version); -#define grub_efiemu_finish_boot_services grub_efiemu_get_memory_map + + +grub_err_t +grub_efiemu_finish_boot_services (grub_efi_uintn_t *memory_map_size, + grub_efi_memory_descriptor_t *memory_map, + grub_efi_uintn_t *map_key, + grub_efi_uintn_t *descriptor_size, + grub_efi_uint32_t *descriptor_version); + grub_err_t grub_efiemu_mmap_iterate (int NESTED_FUNC_ATTR (*hook) (grub_uint64_t, grub_uint64_t, From fff60aba9e0f1d8a6d4800bd6fc3a7b14bb36984 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 18 May 2010 14:29:33 +0200 Subject: [PATCH 135/178] Remove leftovers from i386-coreboot.rmk --- conf/i386-coreboot.rmk | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/conf/i386-coreboot.rmk b/conf/i386-coreboot.rmk index c9eea6470..821431cbb 100644 --- a/conf/i386-coreboot.rmk +++ b/conf/i386-coreboot.rmk @@ -34,7 +34,7 @@ bin_SCRIPTS += grub-mkrescue grub_mkrescue_SOURCES = util/grub-mkrescue.in # Modules. -pkglib_MODULES = aout.mod halt.mod datetime.mod mmap.mod +pkglib_MODULES = halt.mod datetime.mod mmap.mod # For mmap.mod. mmap_mod_SOURCES = mmap/mmap.c mmap/i386/uppermem.c mmap/i386/mmap.c @@ -47,26 +47,6 @@ halt_mod_SOURCES = commands/halt.c halt_mod_CFLAGS = $(COMMON_CFLAGS) halt_mod_LDFLAGS = $(COMMON_LDFLAGS) -# For play.mod. -play_mod_SOURCES = commands/i386/pc/play.c -play_mod_CFLAGS = $(COMMON_CFLAGS) -play_mod_LDFLAGS = $(COMMON_LDFLAGS) - -# For memdisk.mod. -memdisk_mod_SOURCES = disk/memdisk.c -memdisk_mod_CFLAGS = $(COMMON_CFLAGS) -memdisk_mod_LDFLAGS = $(COMMON_LDFLAGS) - -# For pci.mod -pci_mod_SOURCES = bus/pci.c -pci_mod_CFLAGS = $(COMMON_CFLAGS) -pci_mod_LDFLAGS = $(COMMON_LDFLAGS) - -# For lspci.mod -lspci_mod_SOURCES = commands/lspci.c -lspci_mod_CFLAGS = $(COMMON_CFLAGS) -lspci_mod_LDFLAGS = $(COMMON_LDFLAGS) - # For datetime.mod datetime_mod_SOURCES = lib/cmos_datetime.c datetime_mod_CFLAGS = $(COMMON_CFLAGS) From a797a26ee832a12c384da7c1a4404fa6ac626665 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 24 Aug 2010 08:57:18 +0200 Subject: [PATCH 136/178] Unify and macroify some code in x86 relocators --- lib/i386/relocator16.S | 71 ++++++++------------------------ lib/i386/relocator32.S | 61 ++++----------------------- lib/i386/relocator64.S | 54 ++++-------------------- lib/i386/relocator_common.S | 82 +++++++++++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+), 154 deletions(-) create mode 100644 lib/i386/relocator_common.S diff --git a/lib/i386/relocator16.S b/lib/i386/relocator16.S index 510d3a1ed..c3768f4eb 100644 --- a/lib/i386/relocator16.S +++ b/lib/i386/relocator16.S @@ -15,18 +15,7 @@ * You should have received a copy of the GNU General Public License * along with GRUB. If not, see . */ - -#include -#include - -#ifdef __x86_64__ -#define RAX %rax -#define RSI %rsi -#else -#define RAX %eax -#define RSI %esi -#endif - + /* The code segment of the protected mode. */ #define CODE_SEGMENT 0x08 @@ -37,50 +26,41 @@ #define PSEUDO_REAL_DSEG 0x20 +#include "relocator_common.S" + .p2align 4 /* force 16-byte alignment */ VARIABLE(grub_relocator16_start) -LOCAL(base): - /* %rax contains now our new 'base'. */ - mov RAX, RSI - add $(LOCAL(cont0) - LOCAL(base)), RAX - jmp *RAX -LOCAL(cont0): - lea (LOCAL(cont1) - LOCAL(base)) (RSI, 1), RAX - movl %eax, (LOCAL(jump_vector) - LOCAL(base)) (RSI, 1) - - lea (LOCAL(gdt) - LOCAL(base)) (RSI, 1), RAX - mov RAX, (LOCAL(gdt_addr) - LOCAL(base)) (RSI, 1) + PREAMBLE movl %esi, %eax movw %ax, (LOCAL (cs_base_bytes12) - LOCAL (base)) (RSI, 1) shrl $16, %eax movb %al, (LOCAL (cs_base_byte3) - LOCAL (base)) (RSI, 1) - /* Switch to compatibility mode. */ - - lgdt (LOCAL(gdtdesc) - LOCAL(base)) (RSI, 1) - - /* Update %cs. */ - ljmp *(LOCAL(jump_vector) - LOCAL(base)) (RSI, 1) - -LOCAL(cont1): + RELOAD_GDT .code32 + /* Update other registers. */ + movl $DATA_SEGMENT, %eax + movl %eax, %ds + movl %eax, %es + movl %eax, %fs + movl %eax, %gs + movl %eax, %ss - /* Disable paging. */ - movl %cr0, %eax - andl $(~GRUB_MEMORY_CPU_CR0_PAGING_ON), %eax - movl %eax, %cr0 + DISABLE_PAGING +#ifdef __x86_64__ /* Disable amd64. */ movl $GRUB_MEMORY_CPU_AMD64_MSR, %ecx rdmsr andl $(~GRUB_MEMORY_CPU_AMD64_MSR_ON), %eax wrmsr +#endif /* Turn off PAE. */ movl %cr4, %eax - andl $GRUB_MEMORY_CPU_CR4_PAE_ON, %eax + andl $(~GRUB_MEMORY_CPU_CR4_PAE_ON), %eax movl %eax, %cr4 /* Update other registers. */ @@ -208,23 +188,6 @@ LOCAL(cs_base_byte3): */ .word 0xFFFF, 0 .byte 0, 0x92, 0, 0 - - .p2align 4 -LOCAL(gdtdesc): - .word 0x27 -LOCAL(gdt_addr): -#ifdef __x86_64__ - /* Filled by the code. */ - .quad 0 -#else - /* Filled by the code. */ - .long 0 -#endif - - .p2align 4 -LOCAL(jump_vector): - /* Jump location. Is filled by the code */ - .long 0 - .long CODE_SEGMENT +LOCAL(gdt_end): VARIABLE(grub_relocator16_end) diff --git a/lib/i386/relocator32.S b/lib/i386/relocator32.S index 4f79151e2..b581305a5 100644 --- a/lib/i386/relocator32.S +++ b/lib/i386/relocator32.S @@ -16,48 +16,21 @@ * along with GRUB. If not, see . */ -#include -#include - -#ifdef __x86_64__ -#define RAX %rax -#define RSI %rsi -#else -#define RAX %eax -#define RSI %esi -#endif - /* The code segment of the protected mode. */ #define CODE_SEGMENT 0x10 /* The data segment of the protected mode. */ #define DATA_SEGMENT 0x18 +#include "relocator_common.S" + .p2align 4 /* force 16-byte alignment */ VARIABLE(grub_relocator32_start) -LOCAL(base): - /* %rax contains now our new 'base'. */ - mov RAX, RSI - add $(LOCAL(cont0) - LOCAL(base)), RAX - jmp *RAX -LOCAL(cont0): - lea (LOCAL(cont1) - LOCAL(base)) (RSI, 1), RAX - movl %eax, (LOCAL(jump_vector) - LOCAL(base)) (RSI, 1) + PREAMBLE - lea (LOCAL(gdt) - LOCAL(base)) (RSI, 1), RAX - mov RAX, (LOCAL(gdt_addr) - LOCAL(base)) (RSI, 1) - - /* Switch to compatibility mode. */ - - lgdt (LOCAL(gdtdesc) - LOCAL(base)) (RSI, 1) - - /* Update %cs. */ - ljmp *(LOCAL(jump_vector) - LOCAL(base)) (RSI, 1) - -LOCAL(cont1): + RELOAD_GDT .code32 - /* Update other registers. */ movl $DATA_SEGMENT, %eax movl %eax, %ds @@ -66,16 +39,15 @@ LOCAL(cont1): movl %eax, %gs movl %eax, %ss - /* Disable paging. */ - movl %cr0, %eax - andl $(~GRUB_MEMORY_CPU_CR0_PAGING_ON), %eax - movl %eax, %cr0 + DISABLE_PAGING +#ifdef __x86_64__ /* Disable amd64. */ movl $GRUB_MEMORY_CPU_AMD64_MSR, %ecx rdmsr andl $(~GRUB_MEMORY_CPU_AMD64_MSR_ON), %eax wrmsr +#endif /* Turn off PAE. */ movl %cr4, %eax @@ -143,23 +115,6 @@ LOCAL(gdt): /* Data segment. */ .byte 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x92, 0xCF, 0x00 - - .p2align 4 -LOCAL(gdtdesc): - .word 0x27 -LOCAL(gdt_addr): -#ifdef __x86_64__ - /* Filled by the code. */ - .quad 0 -#else - /* Filled by the code. */ - .long 0 -#endif - - .p2align 4 -LOCAL(jump_vector): - /* Jump location. Is filled by the code */ - .long 0 - .long CODE_SEGMENT +LOCAL(gdt_end): VARIABLE(grub_relocator32_end) diff --git a/lib/i386/relocator64.S b/lib/i386/relocator64.S index 37a77b3b5..bb086418c 100644 --- a/lib/i386/relocator64.S +++ b/lib/i386/relocator64.S @@ -16,44 +16,20 @@ * along with GRUB. If not, see . */ -#include -#include - -#ifdef __x86_64__ -#define RAX %rax -#define RSI %rsi -#else -#define RAX %eax -#define RSI %esi -#endif - #define CODE32_SEGMENT 0x18 -#define CODE64_SEGMENT 0x08 +#define CODE_SEGMENT 0x08 /* The data segment of the protected mode. */ #define DATA_SEGMENT 0x10 +#include "relocator_common.S" + .p2align 4 /* force 16-byte alignment */ VARIABLE(grub_relocator64_start) -LOCAL(base): - /* %rax contains now our new 'base'. */ - mov RAX, RSI - - add $(LOCAL(cont0) - LOCAL(base)), RAX - jmp *RAX -LOCAL(cont0): + PREAMBLE #ifndef __x86_64__ - lea (LOCAL(cont1) - LOCAL(base)) (RSI, 1), RAX - mov RAX, (LOCAL(jump_vector) - LOCAL(base)) (RSI, 1) - - lea (LOCAL(gdt) - LOCAL(base)) (RSI, 1), RAX - mov RAX, (LOCAL(gdt_addr) - LOCAL(base)) (RSI, 1) - - /* Disable paging. */ - movl %cr0, %eax - andl $(~GRUB_MEMORY_CPU_CR0_PAGING_ON), %eax - movl %eax, %cr0 + DISABLE_PAGING /* Turn on PAE. */ movl %cr4, %eax @@ -77,11 +53,7 @@ VARIABLE(grub_relocator64_cr3) orl $GRUB_MEMORY_CPU_CR0_PAGING_ON, %eax movl %eax, %cr0 - /* Load GDT. */ - lgdt (LOCAL(gdtdesc) - LOCAL(base)) (RSI, 1) - - /* Update %cs. */ - ljmp *(LOCAL(jump_vector) - LOCAL(base)) (RSI, 1) + RELOAD_GDT #else /* mov imm64, %rax */ .byte 0x48 @@ -91,7 +63,6 @@ VARIABLE(grub_relocator64_cr3) movq %rax, %cr3 #endif -LOCAL(cont1): .code64 /* mov imm64, %rax */ @@ -183,18 +154,7 @@ LOCAL(gdt): | (1 << 7) /* 4K granular. */) .byte 0x00 /* Base 00xxxxxx. */ - .p2align 4 -LOCAL(gdtdesc): - .word 0x20 -LOCAL(gdt_addr): - /* Filled by the code. */ - .long 0 - - .p2align 4 -LOCAL(jump_vector): - /* Jump location. Is filled by the code */ - .long 0 - .long CODE64_SEGMENT +LOCAL(gdt_end): #endif VARIABLE(grub_relocator64_end) diff --git a/lib/i386/relocator_common.S b/lib/i386/relocator_common.S new file mode 100644 index 000000000..bd5b53f95 --- /dev/null +++ b/lib/i386/relocator_common.S @@ -0,0 +1,82 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2009,2010 Free Software Foundation, Inc. + * + * GRUB 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. + * + * GRUB 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 GRUB. If not, see . + */ + + +#include +#include + +#ifdef __x86_64__ +#define RAX %rax +#define RSI %rsi +#else +#define RAX %eax +#define RSI %esi +#endif + + .macro DISABLE_PAGING +#ifdef GRUB_MACHINE_IEEE1275 +#endif + + movl %cr0, %eax + andl $(~GRUB_MEMORY_CPU_CR0_PAGING_ON), %eax + movl %eax, %cr0 + .endm + + .macro PREAMBLE +LOCAL(base): + /* %rax contains now our new 'base'. */ + mov RAX, RSI + + add $(LOCAL(cont0) - LOCAL(base)), RAX + jmp *RAX +LOCAL(cont0): + .endm + + .macro RELOAD_GDT + lea (LOCAL(cont1) - LOCAL(base)) (RSI, 1), RAX + movl %eax, (LOCAL(jump_vector) - LOCAL(base)) (RSI, 1) + + lea (LOCAL(gdt) - LOCAL(base)) (RSI, 1), RAX + mov RAX, (LOCAL(gdt_addr) - LOCAL(base)) (RSI, 1) + + /* Switch to compatibility mode. */ + lgdt (LOCAL(gdtdesc) - LOCAL(base)) (RSI, 1) + + /* Update %cs. */ + ljmp *(LOCAL(jump_vector) - LOCAL(base)) (RSI, 1) + + .p2align 4 +LOCAL(gdtdesc): + .word LOCAL(gdt_end) - LOCAL(gdt) +LOCAL(gdt_addr): +#ifdef __x86_64__ + /* Filled by the code. */ + .quad 0 +#else + /* Filled by the code. */ + .long 0 +#endif + + .p2align 4 +LOCAL(jump_vector): + /* Jump location. Is filled by the code */ + .long 0 + .long CODE_SEGMENT + +LOCAL(cont1): + .endm From 79f8b757ce4c5733341d00c9c9e5a489fadda3b5 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 24 Aug 2010 08:57:53 +0200 Subject: [PATCH 137/178] fix multiboot compilation --- conf/i386-multiboot.rmk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conf/i386-multiboot.rmk b/conf/i386-multiboot.rmk index 6475e6763..0fcfa9c66 100644 --- a/conf/i386-multiboot.rmk +++ b/conf/i386-multiboot.rmk @@ -16,7 +16,7 @@ kernel_img_SOURCES = kern/i386/coreboot/startup.S \ kern/disk.c kern/dl.c kern/file.c kern/fs.c kern/err.c \ kern/misc.c kern/mm.c kern/term.c \ kern/rescue_parser.c kern/rescue_reader.c \ - kern/time.c kern/list.c kern/handler.c kern/command.c kern/corecmd.c \ + kern/time.c kern/list.c kern/command.c kern/corecmd.c \ kern/$(target_cpu)/dl.c kern/parser.c kern/partition.c \ kern/i386/tsc.c kern/i386/pit.c \ kern/generic/rtc_get_time_ms.c \ From ffadea42bbabab54882210b7cd740f08370d311b Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Tue, 24 Aug 2010 19:33:08 +0200 Subject: [PATCH 138/178] Fix non-loading of BSS --- loader/multiboot_elfxx.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/loader/multiboot_elfxx.c b/loader/multiboot_elfxx.c index 7d08eda2a..024b44747 100644 --- a/loader/multiboot_elfxx.c +++ b/loader/multiboot_elfxx.c @@ -88,7 +88,7 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, void *buffer) /* Load every loadable segment in memory. */ for (i = 0; i < ehdr->e_phnum; i++) { - if (phdr(i)->p_type == PT_LOAD && phdr(i)->p_filesz != 0) + if (phdr(i)->p_type == PT_LOAD) { grub_err_t err; void *source; @@ -109,15 +109,18 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, void *buffer) source = get_virtual_current_address (ch); } - if (grub_file_seek (file, (grub_off_t) phdr(i)->p_offset) - == (grub_off_t) -1) - return grub_error (GRUB_ERR_BAD_OS, - "invalid offset in program header"); + if (phdr(i)->p_filesz != 0) + { + if (grub_file_seek (file, (grub_off_t) phdr(i)->p_offset) + == (grub_off_t) -1) + return grub_error (GRUB_ERR_BAD_OS, + "invalid offset in program header"); - if (grub_file_read (file, source, phdr(i)->p_filesz) - != (grub_ssize_t) phdr(i)->p_filesz) - return grub_error (GRUB_ERR_BAD_OS, - "couldn't read segment from file"); + if (grub_file_read (file, source, phdr(i)->p_filesz) + != (grub_ssize_t) phdr(i)->p_filesz) + return grub_error (GRUB_ERR_BAD_OS, + "couldn't read segment from file"); + } if (phdr(i)->p_filesz < phdr(i)->p_memsz) grub_memset ((grub_uint8_t *) source + phdr(i)->p_filesz, 0, From 262d4a94a0e6dacdb3d7f47d640c672f82cff2fc Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 25 Aug 2010 02:15:21 +0200 Subject: [PATCH 139/178] Add mips multiboot2 mbi address calculation --- loader/multiboot_mbi2.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/loader/multiboot_mbi2.c b/loader/multiboot_mbi2.c index 8cade2f2f..d1b306bbf 100644 --- a/loader/multiboot_mbi2.c +++ b/loader/multiboot_mbi2.c @@ -489,7 +489,13 @@ grub_multiboot_make_mbi (grub_uint32_t *target) return err; ptrorig = get_virtual_current_address (ch); +#if defined (__i386__) || defined (__x86_64__) *target = get_physical_target_address (ch); +#elif defined (__mips) + *target = get_physical_target_address (ch) | 0x80000000; +#else +#error Please complete this +#endif mbistart = ptrorig; ptrorig += 2 * sizeof (grub_uint32_t); From 5ad6967b19306efe28b047b97b21b839b74f7077 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 25 Aug 2010 16:59:11 +0200 Subject: [PATCH 140/178] Enable boottests --- Makefile.am | 144 +++++++++++++++++- {tests => grub-core/tests}/boot/kfreebsd.cfg | 0 .../tests}/boot/kfreebsd.init-i386.S | 0 .../tests}/boot/kfreebsd.init-x86_64.S | 0 {tests => grub-core/tests}/boot/knetbsd.cfg | 0 .../tests}/boot/knetbsd.init-i386.S | 0 .../tests}/boot/knetbsd.init-x86_64.S | 0 {tests => grub-core/tests}/boot/linux.cfg | 0 .../tests}/boot/linux.init-i386.S | 0 .../tests}/boot/linux.init-x86_64.S | 0 {tests => grub-core/tests}/boot/linux16.cfg | 0 tests/util/grub-shell.in | 2 +- 12 files changed, 144 insertions(+), 2 deletions(-) rename {tests => grub-core/tests}/boot/kfreebsd.cfg (100%) rename {tests => grub-core/tests}/boot/kfreebsd.init-i386.S (100%) rename {tests => grub-core/tests}/boot/kfreebsd.init-x86_64.S (100%) rename {tests => grub-core/tests}/boot/knetbsd.cfg (100%) rename {tests => grub-core/tests}/boot/knetbsd.init-i386.S (100%) rename {tests => grub-core/tests}/boot/knetbsd.init-x86_64.S (100%) rename {tests => grub-core/tests}/boot/linux.cfg (100%) rename {tests => grub-core/tests}/boot/linux.init-i386.S (100%) rename {tests => grub-core/tests}/boot/linux.init-x86_64.S (100%) rename {tests => grub-core/tests}/boot/linux16.cfg (100%) diff --git a/Makefile.am b/Makefile.am index f49a92ead..9988c4099 100644 --- a/Makefile.am +++ b/Makefile.am @@ -82,4 +82,146 @@ CLEANFILES += widthspec.h platform_HEADERS = config.h pkglib_DATA += grub-mkconfig_lib -pkglib_DATA += update-grub_lib \ No newline at end of file +pkglib_DATA += update-grub_lib + + +if COND_i386_coreboot +BOOTTARGET=coreboot +QEMU32=qemu-system-i386 +endif + +if COND_i386_multiboot +BOOTTARGET=cd +QEMU32=qemu-system-i386 +endif + +if COND_i386_ieee1275 +BOOTTARGET=cd +QEMU32=qemu-system-i386 +endif + +if COND_i386_qemu +BOOTTARGET=qemu +QEMU32=qemu-system-i386 +endif + +if COND_i386_pc +BOOTTARGET=cd +QEMU32=qemu-system-i386 +endif + +if COND_i386_efi +QEMU32=qemu-system-i386 +BOOTTARGET=cd +endif + +if COND_x86_64_efi +QEMU32=qemu-system-x86_64 +BOOTTARGET=cd +endif + +linux.init.x86_64: $(srcdir)/grub-core/tests/boot/linux.init-x86_64.S + $(TARGET_CC) -o $@ $< -m64 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" + +linux.init.i386: $(srcdir)/grub-core/tests/boot/linux.init-i386.S + $(TARGET_CC) -o $@ $< -m32 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" + +kfreebsd.init.x86_64: $(srcdir)/grub-core/tests/boot/kfreebsd.init-x86_64.S + $(TARGET_CC) -o $@ $< -m64 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" && freebsd-brandelf -t FreeBSD $@ + +kfreebsd.init.i386: $(srcdir)/grub-core/tests/boot/kfreebsd.init-i386.S + $(TARGET_CC) -o $@ $< -m32 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" && freebsd-brandelf -t FreeBSD $@ + +knetbsd.init.i386: $(srcdir)/grub-core/tests/boot/knetbsd.init-i386.S + $(TARGET_CC) -o $@ $< -m32 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" + +knetbsd.init.x86_64: $(srcdir)/grub-core/tests/boot/knetbsd.init-x86_64.S + $(TARGET_CC) -o $@ $< -m64 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" + +linux-initramfs.i386: linux.init.i386 Makefile + TDIR=`mktemp -d`; cp $< $$TDIR/init; (cd $$TDIR; echo ./init | cpio --quiet --dereference -o -H newc) | gzip > $@; rm -rf $$TDIR + +linux-initramfs.x86_64: linux.init.x86_64 Makefile + TDIR=`mktemp -d`; cp $< $$TDIR/init; (cd $$TDIR; echo ./init | cpio --quiet --dereference -o -H newc) | gzip > $@; rm -rf $$TDIR + +kfreebsd-mfsroot.i386.img: kfreebsd.init.i386 Makefile + TDIR=`mktemp -d`; mkdir $$TDIR/dev; mkdir $$TDIR/sbin; cp $< $$TDIR/sbin/init; makefs -t ffs -s 30m -f 1000 -o minfree=0,version=1 $@ $$TDIR; rm -rf $$TDIR + +knetbsd.image.i386: knetbsd.init.i386 + TDIR=`mktemp -d` && mkdir $$TDIR/dev && mkdir $$TDIR/sbin && cp $< $$TDIR/sbin/init && makefs -t ffs -s 64k -f 10 -o minfree=0,version=1 $@ $$TDIR && rm -rf $$TDIR + +knetbsd.miniroot-image.i386.img: knetbsd.image.i386 $(GRUB_PAYLOADS_DIR)/knetbsd.miniroot.i386 + $(OBJCOPY) --add-section=miniroot=$< $(GRUB_PAYLOADS_DIR)/knetbsd.miniroot.i386 $@ + +kfreebsd-mfsroot.x86_64.img: kfreebsd.init.x86_64 Makefile + TDIR=`mktemp -d`; mkdir $$TDIR/dev; mkdir $$TDIR/sbin; cp $< $$TDIR/sbin/init; makefs -t ffs -s 30m -f 1000 -o minfree=0,version=1 $@ $$TDIR; rm -rf $$TDIR + +knetbsd.image.x86_64: knetbsd.init.x86_64 + TDIR=`mktemp -d` && mkdir $$TDIR/dev && mkdir $$TDIR/sbin && cp $< $$TDIR/sbin/init && makefs -t ffs -s 64k -f 10 -o minfree=0,version=1 $@ $$TDIR && rm -rf $$TDIR + +knetbsd.miniroot-image.x86_64.img: knetbsd.image.x86_64 $(GRUB_PAYLOADS_DIR)/knetbsd.miniroot.x86_64 + $(OBJCOPY) --add-section=miniroot=$< $(GRUB_PAYLOADS_DIR)/knetbsd.miniroot.x86_64 $@ + +CLEANFILES += linux.init.i386 kfreebsd.init.i386 linux.init.x86_64 linux-initramfs.i386 linux-initramfs.x86_64 + +kfreebsd-mfsroot.i386.gz: kfreebsd-mfsroot.i386.img + gzip < $< > $@ + +bootcheck-kfreebsd-i386: kfreebsd-mfsroot.i386.gz $(GRUB_PAYLOADS_DIR)/kfreebsd.i386 $(GRUB_PAYLOADS_DIR)/kfreebsd_env.i386 $(srcdir)/grub-core/tests/boot/kfreebsd.cfg grub-shell + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=$(QEMU32) --files=/mfsroot.gz=kfreebsd-mfsroot.i386.gz --files=/kfreebsd=$(GRUB_PAYLOADS_DIR)/kfreebsd.i386 --files=/kfreebsd_env=$(GRUB_PAYLOADS_DIR)/kfreebsd_env.i386 $(srcdir)/grub-core/tests/boot/kfreebsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + +kfreebsd-mfsroot.x86_64.gz: kfreebsd-mfsroot.x86_64.img + gzip < $< > $@ + +bootcheck-kfreebsd-x86_64: kfreebsd-mfsroot.x86_64.gz $(GRUB_PAYLOADS_DIR)/kfreebsd.x86_64 $(GRUB_PAYLOADS_DIR)/kfreebsd_env.x86_64 $(srcdir)/grub-core/tests/boot/kfreebsd.cfg grub-shell + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/mfsroot.gz=kfreebsd-mfsroot.x86_64.gz --files=/kfreebsd=$(GRUB_PAYLOADS_DIR)/kfreebsd.x86_64 --files=/kfreebsd_env=$(GRUB_PAYLOADS_DIR)/kfreebsd_env.x86_64 $(srcdir)/grub-core/tests/boot/kfreebsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + +knetbsd.miniroot-image.i386.gz: knetbsd.miniroot-image.i386.img + gzip < $< > $@ + +bootcheck-knetbsd-i386: knetbsd.miniroot-image.i386.gz $(GRUB_PAYLOADS_DIR)/knetbsd.i386 $(srcdir)/grub-core/tests/boot/knetbsd.cfg grub-shell + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-i386 --files=/miniroot.gz=knetbsd.miniroot-image.i386.gz --files=/knetbsd=$(GRUB_PAYLOADS_DIR)/knetbsd.i386 $(srcdir)/grub-core/tests/boot/knetbsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + +knetbsd.miniroot-image.x86_64.gz: knetbsd.miniroot-image.x86_64.img + gzip < $< > $@ + +bootcheck-knetbsd-x86_64: knetbsd.miniroot-image.x86_64.gz $(GRUB_PAYLOADS_DIR)/knetbsd.x86_64 $(srcdir)/grub-core/tests/boot/knetbsd.cfg grub-shell + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/miniroot.gz=knetbsd.miniroot-image.x86_64.gz --files=/knetbsd=$(GRUB_PAYLOADS_DIR)/knetbsd.x86_64 $(srcdir)/grub-core/tests/boot/knetbsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + +bootcheck-linux-i386: linux-initramfs.i386 $(GRUB_PAYLOADS_DIR)/linux.i386 $(srcdir)/grub-core/tests/boot/linux.cfg grub-shell + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=$(QEMU32) --files=/initrd=linux-initramfs.i386 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.i386 $(srcdir)/grub-core/tests/boot/linux.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + +bootcheck-linux-x86_64: linux-initramfs.x86_64 $(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/grub-core/tests/boot/linux.cfg grub-shell + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/initrd=linux-initramfs.x86_64 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/grub-core/tests/boot/linux.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + +bootcheck-linux16-i386: linux-initramfs.i386 $(GRUB_PAYLOADS_DIR)/linux.i386 $(srcdir)/grub-core/tests/boot/linux.cfg grub-shell + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=$(QEMU32) --files=/initrd=linux-initramfs.i386 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.i386 $(srcdir)/grub-core/tests/boot/linux16.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + +bootcheck-linux16-x86_64: linux-initramfs.x86_64 $(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/grub-core/tests/boot/linux.cfg grub-shell + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/initrd=linux-initramfs.x86_64 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/grub-core/tests/boot/linux16.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + +BOOTCHECKS= + +BOOTCHECKS += bootcheck-linux16-i386 bootcheck-linux16-x86_64 + +BOOTCHECKS += bootcheck-linux-i386 bootcheck-linux-x86_64 + +# Crashes because memory at 0-0x1000 is occupied +BOOTCHECKS += bootcheck-kfreebsd-i386 bootcheck-knetbsd-x86_64 + +# Requires ACPI +BOOTCHECKS += bootcheck-kfreebsd-x86_64 +# Crashes early on non-BIOS +BOOTCHECKS += bootcheck-knetbsd-i386 + + +.PHONY: bootcheck-linux-i386 bootcheck-linux-x86_64 \ + bootcheck-kfreebsd-i386 bootcheck-kfreebsd-x86_64 \ + bootcheck-knetbsd-i386 bootcheck-knetbsd-x86_64 + +# Randomly generated +SUCCESSFUL_BOOT_STRING=3e49994fd5d82b7c9298d672d774080d +# tianocore cd access is very slow +BOOTCHECK_TIMEOUT=180 + +bootcheck: $(BOOTCHECKS) diff --git a/tests/boot/kfreebsd.cfg b/grub-core/tests/boot/kfreebsd.cfg similarity index 100% rename from tests/boot/kfreebsd.cfg rename to grub-core/tests/boot/kfreebsd.cfg diff --git a/tests/boot/kfreebsd.init-i386.S b/grub-core/tests/boot/kfreebsd.init-i386.S similarity index 100% rename from tests/boot/kfreebsd.init-i386.S rename to grub-core/tests/boot/kfreebsd.init-i386.S diff --git a/tests/boot/kfreebsd.init-x86_64.S b/grub-core/tests/boot/kfreebsd.init-x86_64.S similarity index 100% rename from tests/boot/kfreebsd.init-x86_64.S rename to grub-core/tests/boot/kfreebsd.init-x86_64.S diff --git a/tests/boot/knetbsd.cfg b/grub-core/tests/boot/knetbsd.cfg similarity index 100% rename from tests/boot/knetbsd.cfg rename to grub-core/tests/boot/knetbsd.cfg diff --git a/tests/boot/knetbsd.init-i386.S b/grub-core/tests/boot/knetbsd.init-i386.S similarity index 100% rename from tests/boot/knetbsd.init-i386.S rename to grub-core/tests/boot/knetbsd.init-i386.S diff --git a/tests/boot/knetbsd.init-x86_64.S b/grub-core/tests/boot/knetbsd.init-x86_64.S similarity index 100% rename from tests/boot/knetbsd.init-x86_64.S rename to grub-core/tests/boot/knetbsd.init-x86_64.S diff --git a/tests/boot/linux.cfg b/grub-core/tests/boot/linux.cfg similarity index 100% rename from tests/boot/linux.cfg rename to grub-core/tests/boot/linux.cfg diff --git a/tests/boot/linux.init-i386.S b/grub-core/tests/boot/linux.init-i386.S similarity index 100% rename from tests/boot/linux.init-i386.S rename to grub-core/tests/boot/linux.init-i386.S diff --git a/tests/boot/linux.init-x86_64.S b/grub-core/tests/boot/linux.init-x86_64.S similarity index 100% rename from tests/boot/linux.init-x86_64.S rename to grub-core/tests/boot/linux.init-x86_64.S diff --git a/tests/boot/linux16.cfg b/grub-core/tests/boot/linux16.cfg similarity index 100% rename from tests/boot/linux16.cfg rename to grub-core/tests/boot/linux16.cfg diff --git a/tests/util/grub-shell.in b/tests/util/grub-shell.in index 09847db2b..e21cc95f4 100644 --- a/tests/util/grub-shell.in +++ b/tests/util/grub-shell.in @@ -164,7 +164,7 @@ if [ x$boot = xcoreboot ]; then device=cdrom fi -${qemu} ${qemuopts} -nographic -serial file:/dev/stdout -monitor file:/dev/null -${device} ${isofile} ${bootdev} | tr -d "\r" +${qemu} ${qemuopts} -nographic -serial file:/dev/stdout -monitor file:/dev/null -${device} ${isofile} ${bootdev} | cat | tr -d "\r" rm -f "${isofile}" "${imgfile}" rm -rf "${rom_directory}" if [ x$boot = xcoreboot ]; then From 1e82303f1d8aed2e00920e09f725c2fa3c38c89e Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 25 Aug 2010 20:34:07 +0200 Subject: [PATCH 141/178] multiboot and multiboot2 bootchecks --- Makefile.am | 16 +++++- grub-core/tests/boot/multiboot.S | 89 +++++++++++++++++++++++++++++ grub-core/tests/boot/multiboot.cfg | 4 ++ grub-core/tests/boot/multiboot2.cfg | 4 ++ 4 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 grub-core/tests/boot/multiboot.S create mode 100644 grub-core/tests/boot/multiboot.cfg create mode 100644 grub-core/tests/boot/multiboot2.cfg diff --git a/Makefile.am b/Makefile.am index 9988c4099..f948a7a2f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -126,6 +126,12 @@ linux.init.x86_64: $(srcdir)/grub-core/tests/boot/linux.init-x86_64.S linux.init.i386: $(srcdir)/grub-core/tests/boot/linux.init-i386.S $(TARGET_CC) -o $@ $< -m32 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" +multiboot.elf: $(srcdir)/grub-core/tests/boot/multiboot.S + $(TARGET_CC) -o $@ $< -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" -ffreestanding -nostdlib -nostdinc -Wl,-N -Wl,-Ttext,0x100000 -m32 -I$(srcdir)/include + +multiboot2.elf: $(srcdir)/grub-core/tests/boot/multiboot.S + $(TARGET_CC) -o $@ $< -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" -ffreestanding -nostdlib -nostdinc -Wl,-N -Wl,-Ttext,0x100000 -m32 -I$(srcdir)/include -DMULTIBOOT2=1 + kfreebsd.init.x86_64: $(srcdir)/grub-core/tests/boot/kfreebsd.init-x86_64.S $(TARGET_CC) -o $@ $< -m64 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" && freebsd-brandelf -t FreeBSD $@ @@ -200,11 +206,19 @@ bootcheck-linux16-i386: linux-initramfs.i386 $(GRUB_PAYLOADS_DIR)/linux.i386 $(s bootcheck-linux16-x86_64: linux-initramfs.x86_64 $(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/grub-core/tests/boot/linux.cfg grub-shell timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/initrd=linux-initramfs.x86_64 --files=/linux=$(GRUB_PAYLOADS_DIR)/linux.x86_64 $(srcdir)/grub-core/tests/boot/linux16.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null +bootcheck-multiboot: multiboot.elf $(srcdir)/grub-core/tests/boot/multiboot.cfg grub-shell + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=$(QEMU32) --files=/multiboot.elf=multiboot.elf $(srcdir)/grub-core/tests/boot/multiboot.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + +bootcheck-multiboot2: multiboot2.elf $(srcdir)/grub-core/tests/boot/multiboot2.cfg grub-shell + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=$(QEMU32) --files=/multiboot2.elf=multiboot2.elf $(srcdir)/grub-core/tests/boot/multiboot2.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + BOOTCHECKS= +BOOTCHECKS += bootcheck-multiboot bootcheck-multiboot2 + BOOTCHECKS += bootcheck-linux16-i386 bootcheck-linux16-x86_64 -BOOTCHECKS += bootcheck-linux-i386 bootcheck-linux-x86_64 +BOOTCHECKS += bootcheck-linux-i386 bootcheck-linux-x86_64 # Crashes because memory at 0-0x1000 is occupied BOOTCHECKS += bootcheck-kfreebsd-i386 bootcheck-knetbsd-x86_64 diff --git a/grub-core/tests/boot/multiboot.S b/grub-core/tests/boot/multiboot.S new file mode 100644 index 000000000..b9c0059c0 --- /dev/null +++ b/grub-core/tests/boot/multiboot.S @@ -0,0 +1,89 @@ +#define ASM_FILE 1 +#ifdef MULTIBOOT2 +#include +#else +#include +#endif + +#define SHUTDOWN_PORT 0x8900 + + .text + /* Align 32 bits boundary. */ + .align 8 + +#ifdef MULTIBOOT2 + /* Multiboot header. */ +multiboot_header: + /* magic */ + .long MULTIBOOT2_HEADER_MAGIC + /* ISA: i386 */ + .long MULTIBOOT_ARCHITECTURE_I386 + /* Header length. */ + .long multiboot_header_end - multiboot_header + /* checksum */ + .long -(MULTIBOOT2_HEADER_MAGIC + MULTIBOOT_ARCHITECTURE_I386 + (multiboot_header_end - multiboot_header)) + .short MULTIBOOT_HEADER_TAG_END + .short 0 + .long 8 +multiboot_header_end: +#else + /* Multiboot header. */ +multiboot_header: + /* magic */ + .long MULTIBOOT_HEADER_MAGIC + /* flags */ + .long 0 + /* checksum */ + .long -MULTIBOOT_HEADER_MAGIC +#endif + + .global start +portmsg: + xorl %eax, %eax +1: + movb 0(%esi), %al + test %eax, %eax + jz 1f + outb %al, %dx + incl %esi + jmp 1b +1: + ret + +serialmsg: +1: + movb 0(%esi), %bl + testb %bl, %bl + jz 1f + movw $0x3fd, %dx +2: + inb %dx, %al + testb $0x20, %al + jz 2b + + movw $0x3f8, %dx + movb %bl, %al + outb %al, %dx + incl %esi + jmp 1b +1: + ret + + .globl _start +_start: + lea message, %esi + call serialmsg + lea shutdown, %esi + movw $SHUTDOWN_PORT, %dx + call portmsg + +1: + hlt + jmp 1b + +shutdown: + .ascii "Shutdown" + .byte 0 +message: + .ascii "Boot Test Passed Successfully\n" SUCCESSFUL_BOOT_STRING "\n" + .byte 0 diff --git a/grub-core/tests/boot/multiboot.cfg b/grub-core/tests/boot/multiboot.cfg new file mode 100644 index 000000000..0942ec67c --- /dev/null +++ b/grub-core/tests/boot/multiboot.cfg @@ -0,0 +1,4 @@ +multiboot /multiboot.elf +boot +# Shouln't happen +halt diff --git a/grub-core/tests/boot/multiboot2.cfg b/grub-core/tests/boot/multiboot2.cfg new file mode 100644 index 000000000..2bec9e6d1 --- /dev/null +++ b/grub-core/tests/boot/multiboot2.cfg @@ -0,0 +1,4 @@ +multiboot2 /multiboot2.elf +boot +# Shouln't happen +halt From 89e07694dc1716d7c296fa151f4251e5e619a9d8 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 25 Aug 2010 21:19:45 +0200 Subject: [PATCH 142/178] Remove grub_dl_unload_all. It's unnecessary and causes trouble --- grub-core/kern/dl.c | 17 ----------------- grub-core/kern/i386/pc/startup.S | 2 -- include/grub/dl.h | 1 - 3 files changed, 20 deletions(-) diff --git a/grub-core/kern/dl.c b/grub-core/kern/dl.c index 09849f174..02d785b9b 100644 --- a/grub-core/kern/dl.c +++ b/grub-core/kern/dl.c @@ -682,20 +682,3 @@ grub_dl_unload_unneeded (void) p = p->next; } } - -/* Unload all modules. */ -void -grub_dl_unload_all (void) -{ - while (grub_dl_head) - { - grub_dl_t p; - - grub_dl_unload_unneeded (); - - /* Force to decrement the ref count. This will purge pre-loaded - modules and manually inserted modules. */ - for (p = grub_dl_head; p; p = p->next) - p->ref_count--; - } -} diff --git a/grub-core/kern/i386/pc/startup.S b/grub-core/kern/i386/pc/startup.S index a73e27ad6..7ae901712 100644 --- a/grub-core/kern/i386/pc/startup.S +++ b/grub-core/kern/i386/pc/startup.S @@ -546,8 +546,6 @@ FUNCTION(grub_chainloader_real_boot) pushl %edx pushl %eax - call EXT_C(grub_dl_unload_all) - /* Turn off Gate A20 */ xorl %eax, %eax call EXT_C(grub_gate_a20) diff --git a/include/grub/dl.h b/include/grub/dl.h index 9db610467..afc4af41a 100644 --- a/include/grub/dl.h +++ b/include/grub/dl.h @@ -101,7 +101,6 @@ grub_dl_t EXPORT_FUNC(grub_dl_load) (const char *name); grub_dl_t grub_dl_load_core (void *addr, grub_size_t size); int EXPORT_FUNC(grub_dl_unload) (grub_dl_t mod); void grub_dl_unload_unneeded (void); -void grub_dl_unload_all (void); int EXPORT_FUNC(grub_dl_ref) (grub_dl_t mod); int EXPORT_FUNC(grub_dl_unref) (grub_dl_t mod); extern grub_dl_t EXPORT_VAR(grub_dl_head); From e8ea4b8424953e4b43044488556a433b8e10a9a2 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 25 Aug 2010 21:55:48 +0200 Subject: [PATCH 143/178] pc-chainloader bootcheck --- Makefile.am | 13 +++++ grub-core/tests/boot/pc-chainloader.S | 63 +++++++++++++++++++++++++ grub-core/tests/boot/pc-chainloader.cfg | 4 ++ 3 files changed, 80 insertions(+) create mode 100644 grub-core/tests/boot/pc-chainloader.S create mode 100644 grub-core/tests/boot/pc-chainloader.cfg diff --git a/Makefile.am b/Makefile.am index f948a7a2f..b97f012bc 100644 --- a/Makefile.am +++ b/Makefile.am @@ -129,6 +129,12 @@ linux.init.i386: $(srcdir)/grub-core/tests/boot/linux.init-i386.S multiboot.elf: $(srcdir)/grub-core/tests/boot/multiboot.S $(TARGET_CC) -o $@ $< -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" -ffreestanding -nostdlib -nostdinc -Wl,-N -Wl,-Ttext,0x100000 -m32 -I$(srcdir)/include +pc-chainloader.elf: $(srcdir)/grub-core/tests/boot/pc-chainloader.S + $(TARGET_CC) -o $@ $< -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" -ffreestanding -nostdlib -nostdinc -Wl,-N -Wl,-Ttext,0x7c00 -m32 + +pc-chainloader.bin: pc-chainloader.elf + $(OBJCOPY) -O binary --strip-unneeded -R .note -R .comment -R .note.gnu.build-id -R .reginfo -R .rel.dyn $< $@; + multiboot2.elf: $(srcdir)/grub-core/tests/boot/multiboot.S $(TARGET_CC) -o $@ $< -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" -ffreestanding -nostdlib -nostdinc -Wl,-N -Wl,-Ttext,0x100000 -m32 -I$(srcdir)/include -DMULTIBOOT2=1 @@ -212,8 +218,15 @@ bootcheck-multiboot: multiboot.elf $(srcdir)/grub-core/tests/boot/multiboot.cfg bootcheck-multiboot2: multiboot2.elf $(srcdir)/grub-core/tests/boot/multiboot2.cfg grub-shell timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=$(QEMU32) --files=/multiboot2.elf=multiboot2.elf $(srcdir)/grub-core/tests/boot/multiboot2.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null +bootcheck-pc-chainloader: pc-chainloader.bin $(srcdir)/grub-core/tests/boot/pc-chainloader.cfg grub-shell + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=$(QEMU32) --files=/pc-chainloader.bin=pc-chainloader.bin $(srcdir)/grub-core/tests/boot/pc-chainloader.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + BOOTCHECKS= +if COND_i386_pc +BOOTCHECKS += bootcheck-pc-chainloader +endif + BOOTCHECKS += bootcheck-multiboot bootcheck-multiboot2 BOOTCHECKS += bootcheck-linux16-i386 bootcheck-linux16-x86_64 diff --git a/grub-core/tests/boot/pc-chainloader.S b/grub-core/tests/boot/pc-chainloader.S new file mode 100644 index 000000000..fc7429940 --- /dev/null +++ b/grub-core/tests/boot/pc-chainloader.S @@ -0,0 +1,63 @@ + +#define SHUTDOWN_PORT 0x8900 + + .text + .globl _start +_start: +base: + .code16 + jmp cont + +portmsg: + xorw %ax, %ax +1: + movb 0(%si), %al + test %ax, %ax + jz 1f + outb %al, %dx + incw %si + jmp 1b +1: + ret + +serialmsg: +1: + movb 0(%si), %bl + testb %bl, %bl + jz 1f + movw $0x3fd, %dx +2: + inb %dx, %al + testb $0x20, %al + jz 2b + + movw $0x3f8, %dx + movb %bl, %al + outb %al, %dx + incw %si + jmp 1b +1: + ret + +cont: + xorw %ax, %ax + movw %ax, %ds + lea message, %si + call serialmsg + lea shutdown, %si + movw $SHUTDOWN_PORT, %dx + call portmsg + +1: + hlt + jmp 1b + +shutdown: + .ascii "Shutdown" + .byte 0 +message: + .ascii "Boot Test Passed Successfully\n" SUCCESSFUL_BOOT_STRING "\n" + .byte 0 + + . = base + 510 + .short 0xaa55 \ No newline at end of file diff --git a/grub-core/tests/boot/pc-chainloader.cfg b/grub-core/tests/boot/pc-chainloader.cfg new file mode 100644 index 000000000..1e80a5b96 --- /dev/null +++ b/grub-core/tests/boot/pc-chainloader.cfg @@ -0,0 +1,4 @@ +chainloader /pc-chainloader.bin +boot +# Shouln't happen +halt From 8218d8b6e804504b8815e4c1becf86e4c40050c8 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 25 Aug 2010 22:34:15 +0200 Subject: [PATCH 144/178] Fix efiemu compilation on ieee1275 --- grub-core/Makefile.core.def | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index b91609826..62c08928a 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -772,7 +772,10 @@ module = { common = efiemu/main.c; common = efiemu/i386/loadcore32.c; common = efiemu/i386/loadcore64.c; - common = efiemu/i386/pc/cfgtables.c; + i386_pc = efiemu/i386/pc/cfgtables.c; + i386_coreboot = efiemu/i386/pc/cfgtables.c; + i386_multiboot = efiemu/i386/pc/cfgtables.c; + i386_ieee1275 = efiemu/i386/nocfgtables.c; common = efiemu/mm.c; common = efiemu/loadcore_common.c; common = efiemu/symbols.c; @@ -789,7 +792,7 @@ module = { extra_dist = efiemu/runtime/efiemu.c; enable = i386_pc; - enable = i386_coeboot; + enable = i386_coreboot; enable = i386_ieee1275; enable = i386_multiboot; }; From ecde61b4909e2a4683bf17ee30232994df0b5712 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Thu, 26 Aug 2010 02:46:30 +0200 Subject: [PATCH 145/178] openbsd ramdisk support --- grub-core/loader/i386/bsd.c | 70 ++++++++++++++++++++- grub-core/loader/i386/bsdXX.c | 114 ++++++++++++++++++++++++++++++++++ include/grub/i386/bsd.h | 16 +++++ 3 files changed, 197 insertions(+), 3 deletions(-) diff --git a/grub-core/loader/i386/bsd.c b/grub-core/loader/i386/bsd.c index 6399cb1e3..770c6f278 100644 --- a/grub-core/loader/i386/bsd.c +++ b/grub-core/loader/i386/bsd.c @@ -67,6 +67,7 @@ static grub_uint32_t bootflags; static int is_elf_kernel, is_64bit; static grub_uint32_t openbsd_root; struct grub_relocator *relocator = NULL; +static struct grub_openbsd_ramdisk_descriptor openbsd_ramdisk; struct bsd_tag { @@ -1253,7 +1254,13 @@ grub_bsd_load_elf (grub_elf_t elf) kern_chunk_src = get_virtual_current_address (ch); - return grub_elf32_load (elf, grub_bsd_elf32_hook, 0, 0); + err = grub_elf32_load (elf, grub_bsd_elf32_hook, 0, 0); + if (err) + return err; + if (kernel_type != KERNEL_TYPE_OPENBSD) + return GRUB_ERR_NONE; + return grub_openbsd_find_ramdisk32 (elf->file, kern_start, + kern_chunk_src, &openbsd_ramdisk); } else if (grub_elf_is_elf64 (elf)) { @@ -1290,7 +1297,13 @@ grub_bsd_load_elf (grub_elf_t elf) kern_chunk_src = get_virtual_current_address (ch); } - return grub_elf64_load (elf, grub_bsd_elf64_hook, 0, 0); + err = grub_elf64_load (elf, grub_bsd_elf64_hook, 0, 0); + if (err) + return err; + if (kernel_type != KERNEL_TYPE_OPENBSD) + return GRUB_ERR_NONE; + return grub_openbsd_find_ramdisk64 (elf->file, kern_start, + kern_chunk_src, &openbsd_ramdisk); } else return grub_error (GRUB_ERR_BAD_OS, "invalid ELF"); @@ -1306,6 +1319,8 @@ grub_bsd_load (int argc, char *argv[]) grub_loader_unset (); + grub_memset (&openbsd_ramdisk, 0, sizeof (openbsd_ramdisk)); + if (argc == 0) { grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified"); @@ -1874,11 +1889,55 @@ grub_cmd_freebsd_module_elf (grub_command_t cmd __attribute__ ((unused)), return err; } +static grub_err_t +grub_cmd_openbsd_ramdisk (grub_command_t cmd __attribute__ ((unused)), + int argc, char *args[]) +{ + grub_file_t file; + grub_size_t size; + + if (argc != 1) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "file name required"); + + if (kernel_type != KERNEL_TYPE_OPENBSD) + return grub_error (GRUB_ERR_BAD_OS, "no kOpenBSD loaded"); + + if (!openbsd_ramdisk.max_size) + return grub_error (GRUB_ERR_BAD_OS, "your kOpenBSD doesn't support ramdisk"); + + file = grub_gzfile_open (args[0], 1); + if (! file) + return grub_error (GRUB_ERR_FILE_NOT_FOUND, + "couldn't load ramdisk"); + + size = grub_file_size (file); + + if (size > openbsd_ramdisk.max_size) + { + grub_file_close (file); + return grub_error (GRUB_ERR_BAD_OS, "your kOpenBSD supports ramdisk only" + " up to %u bytes, however you supplied a %u bytes one", + openbsd_ramdisk.max_size, size); + } + + if (grub_file_read (file, openbsd_ramdisk.target, size) + != (grub_ssize_t) (size)) + { + grub_file_close (file); + grub_error_push (); + return grub_error (GRUB_ERR_BAD_OS, "couldn't read file %s", args[0]); + } + grub_memset (openbsd_ramdisk.target + size, 0, + openbsd_ramdisk.max_size - size); + *openbsd_ramdisk.size = ALIGN_UP (size, 512); + + return GRUB_ERR_NONE; +} static grub_extcmd_t cmd_freebsd, cmd_openbsd, cmd_netbsd; static grub_command_t cmd_freebsd_loadenv, cmd_freebsd_module; static grub_command_t cmd_netbsd_module, cmd_freebsd_module_elf; -static grub_command_t cmd_netbsd_module_elf; +static grub_command_t cmd_netbsd_module_elf, cmd_openbsd_ramdisk; GRUB_MOD_INIT (bsd) { @@ -1910,6 +1969,10 @@ GRUB_MOD_INIT (bsd) grub_register_command ("kfreebsd_module_elf", grub_cmd_freebsd_module_elf, 0, N_("Load FreeBSD kernel module (ELF).")); + cmd_openbsd_ramdisk = grub_register_command ("kopenbsd_ramdisk", + grub_cmd_openbsd_ramdisk, 0, + "Load kOpenBSD ramdisk. "); + my_mod = mod; } @@ -1924,6 +1987,7 @@ GRUB_MOD_FINI (bsd) grub_unregister_command (cmd_netbsd_module); grub_unregister_command (cmd_freebsd_module_elf); grub_unregister_command (cmd_netbsd_module_elf); + grub_unregister_command (cmd_openbsd_ramdisk); grub_bsd_unload (); } diff --git a/grub-core/loader/i386/bsdXX.c b/grub-core/loader/i386/bsdXX.c index f053e7e13..073f01da2 100644 --- a/grub-core/loader/i386/bsdXX.c +++ b/grub-core/loader/i386/bsdXX.c @@ -503,4 +503,118 @@ SUFFIX (grub_netbsd_load_elf_meta) (struct grub_relocator *relocator, return GRUB_ERR_NONE; } +grub_err_t +SUFFIX(grub_openbsd_find_ramdisk) (grub_file_t file, + grub_addr_t kern_start, + void *kern_chunk_src, + struct grub_openbsd_ramdisk_descriptor *desc) +{ + unsigned symoff, stroff, symsize, strsize, symentsize; + { + grub_err_t err; + Elf_Ehdr e; + Elf_Shdr *s; + char *shdr; + + err = read_headers (file, &e, &shdr); + if (err) + return err; + + for (s = (Elf_Shdr *) shdr; s < (Elf_Shdr *) (shdr + + e.e_shnum * e.e_shentsize); + s = (Elf_Shdr *) ((char *) s + e.e_shentsize)) + if (s->sh_type == SHT_SYMTAB) + break; + if (s >= (Elf_Shdr *) ((char *) shdr + e.e_shnum * e.e_shentsize)) + { + grub_free (shdr); + return GRUB_ERR_NONE; + } + + symsize = s->sh_size; + symentsize = s->sh_entsize; + symoff = s->sh_offset; + + s = (Elf_Shdr *) (shdr + e.e_shentsize * s->sh_link); + stroff = s->sh_offset; + strsize = s->sh_size; + grub_free (shdr); + } + { + Elf_Sym *syms, *sym, *imagesym = NULL, *sizesym = NULL; + unsigned i; + char *strs; + + syms = grub_malloc (symsize); + if (!syms) + return grub_errno; + + if (grub_file_seek (file, symoff) == (grub_off_t) -1) + { + grub_free (syms); + return grub_errno; + } + if (grub_file_read (file, syms, symsize) != (grub_ssize_t) symsize) + { + grub_free (syms); + if (! grub_errno) + return grub_error (GRUB_ERR_BAD_OS, "invalid ELF"); + return grub_errno; + } + + strs = grub_malloc (strsize); + if (!strs) + { + grub_free (syms); + return grub_errno; + } + + if (grub_file_seek (file, stroff) == (grub_off_t) -1) + return grub_errno; + if (grub_file_read (file, strs, strsize) != (grub_ssize_t) strsize) + { + grub_free (syms); + grub_free (strs); + if (! grub_errno) + return grub_error (GRUB_ERR_BAD_OS, "invalid ELF"); + return grub_errno; + } + + for (i = 0, sym = syms; i < symsize / symentsize; + i++, sym = (Elf_Sym *) ((char *) sym + symentsize)) + { + if (ELF_ST_TYPE (sym->st_info) != STT_OBJECT) + continue; + if (!sym->st_name) + continue; + if (grub_strcmp (strs + sym->st_name, "rd_root_image") == 0) + imagesym = sym; + if (grub_strcmp (strs + sym->st_name, "rd_root_size") == 0) + sizesym = sym; + if (imagesym && sizesym) + break; + } + if (!imagesym || !sizesym) + { + grub_free (syms); + grub_free (strs); + return GRUB_ERR_NONE; + } + if (sizeof (*desc->size) != sizesym->st_size) + { + grub_free (syms); + grub_free (strs); + return grub_error (GRUB_ERR_BAD_OS, "unexpected size of rd_root_size"); + } + desc->max_size = imagesym->st_size; + desc->target = (imagesym->st_value & 0xFFFFFF) - kern_start + + (grub_uint8_t *) kern_chunk_src; + desc->size = (grub_uint32_t *) ((sizesym->st_value & 0xFFFFFF) - kern_start + + (grub_uint8_t *) kern_chunk_src); + grub_free (syms); + grub_free (strs); + + return GRUB_ERR_NONE; + } +} diff --git a/include/grub/i386/bsd.h b/include/grub/i386/bsd.h index a6abd7e90..53008cdaf 100644 --- a/include/grub/i386/bsd.h +++ b/include/grub/i386/bsd.h @@ -99,6 +99,22 @@ grub_err_t grub_freebsd_add_meta_module (char *filename, char *type, int argc, char **argv, grub_addr_t addr, grub_uint32_t size); +struct grub_openbsd_ramdisk_descriptor +{ + grub_size_t max_size; + grub_uint8_t *target; + grub_uint32_t *size; +}; + +grub_err_t grub_openbsd_find_ramdisk32 (grub_file_t file, + grub_addr_t kern_start, + void *kern_chunk_src, + struct grub_openbsd_ramdisk_descriptor *desc); +grub_err_t grub_openbsd_find_ramdisk64 (grub_file_t file, + grub_addr_t kern_start, + void *kern_chunk_src, + struct grub_openbsd_ramdisk_descriptor *desc); + extern grub_uint8_t grub_bsd64_trampoline_start, grub_bsd64_trampoline_end; extern grub_uint32_t grub_bsd64_trampoline_selfjump; extern grub_uint32_t grub_bsd64_trampoline_gdt; From e95616a173849b5c89faeb77c53628cccbc018b5 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 28 Aug 2010 03:17:07 +0200 Subject: [PATCH 146/178] REmove leftover declaration --- grub-core/loader/i386/linux.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/grub-core/loader/i386/linux.c b/grub-core/loader/i386/linux.c index e05225f25..debcdb71f 100644 --- a/grub-core/loader/i386/linux.c +++ b/grub-core/loader/i386/linux.c @@ -531,11 +531,6 @@ grub_linux_setup_video (struct linux_kernel_params *params) return GRUB_ERR_NONE; } -#ifdef __x86_64__ -extern grub_uint8_t grub_linux_trampoline_start[]; -extern grub_uint8_t grub_linux_trampoline_end[]; -#endif - static grub_err_t grub_linux_boot (void) { From dc1bff761fcb5dcb7690268878037b723b1c6136 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 28 Aug 2010 03:17:59 +0200 Subject: [PATCH 147/178] Simplify knetbsd bootcheck by using specfile --- Makefile.am | 9 +++-- grub-core/tests/boot/kbsd.spec.txt | 3 ++ grub-core/tests/boot/knetbsd.init-i386.S | 45 ---------------------- grub-core/tests/boot/knetbsd.init-x86_64.S | 45 ---------------------- 4 files changed, 9 insertions(+), 93 deletions(-) create mode 100644 grub-core/tests/boot/kbsd.spec.txt diff --git a/Makefile.am b/Makefile.am index b97f012bc..4b0ef7f4c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -159,7 +159,10 @@ linux-initramfs.x86_64: linux.init.x86_64 Makefile kfreebsd-mfsroot.i386.img: kfreebsd.init.i386 Makefile TDIR=`mktemp -d`; mkdir $$TDIR/dev; mkdir $$TDIR/sbin; cp $< $$TDIR/sbin/init; makefs -t ffs -s 30m -f 1000 -o minfree=0,version=1 $@ $$TDIR; rm -rf $$TDIR -knetbsd.image.i386: knetbsd.init.i386 +knetbsd.image.i386: knetbsd.init.i386 $(srcdir)/grub-core/tests/boot/kbsd.spec.txt + TDIR=`mktemp -d` && mkdir $$TDIR/dev && mkdir $$TDIR/sbin && cp $< $$TDIR/sbin/init && makefs -F $(srcdir)/grub-core/tests/boot/kbsd.spec.txt -t ffs -s 64k -f 10 -o minfree=0,version=1 $@ $$TDIR && rm -rf $$TDIR + +kopenbsd.image.i386: kopenbsd.init.i386 TDIR=`mktemp -d` && mkdir $$TDIR/dev && mkdir $$TDIR/sbin && cp $< $$TDIR/sbin/init && makefs -t ffs -s 64k -f 10 -o minfree=0,version=1 $@ $$TDIR && rm -rf $$TDIR knetbsd.miniroot-image.i386.img: knetbsd.image.i386 $(GRUB_PAYLOADS_DIR)/knetbsd.miniroot.i386 @@ -168,8 +171,8 @@ knetbsd.miniroot-image.i386.img: knetbsd.image.i386 $(GRUB_PAYLOADS_DIR)/knetbsd kfreebsd-mfsroot.x86_64.img: kfreebsd.init.x86_64 Makefile TDIR=`mktemp -d`; mkdir $$TDIR/dev; mkdir $$TDIR/sbin; cp $< $$TDIR/sbin/init; makefs -t ffs -s 30m -f 1000 -o minfree=0,version=1 $@ $$TDIR; rm -rf $$TDIR -knetbsd.image.x86_64: knetbsd.init.x86_64 - TDIR=`mktemp -d` && mkdir $$TDIR/dev && mkdir $$TDIR/sbin && cp $< $$TDIR/sbin/init && makefs -t ffs -s 64k -f 10 -o minfree=0,version=1 $@ $$TDIR && rm -rf $$TDIR +knetbsd.image.x86_64: knetbsd.init.x86_64 $(srcdir)/grub-core/tests/boot/kbsd.spec.txt + TDIR=`mktemp -d` && mkdir $$TDIR/dev && mkdir $$TDIR/sbin && cp $< $$TDIR/sbin/init && makefs -F $(srcdir)/grub-core/tests/boot/kbsd.spec.txt -t ffs -s 64k -f 10 -o minfree=0,version=1 $@ $$TDIR && rm -rf $$TDIR knetbsd.miniroot-image.x86_64.img: knetbsd.image.x86_64 $(GRUB_PAYLOADS_DIR)/knetbsd.miniroot.x86_64 $(OBJCOPY) --add-section=miniroot=$< $(GRUB_PAYLOADS_DIR)/knetbsd.miniroot.x86_64 $@ diff --git a/grub-core/tests/boot/kbsd.spec.txt b/grub-core/tests/boot/kbsd.spec.txt new file mode 100644 index 000000000..af171bc56 --- /dev/null +++ b/grub-core/tests/boot/kbsd.spec.txt @@ -0,0 +1,3 @@ +. type=dir + dev type=dir + console type=char device=0 mode=666 gid=0 uid=0 diff --git a/grub-core/tests/boot/knetbsd.init-i386.S b/grub-core/tests/boot/knetbsd.init-i386.S index c751421ba..587b4f41f 100644 --- a/grub-core/tests/boot/knetbsd.init-i386.S +++ b/grub-core/tests/boot/knetbsd.init-i386.S @@ -22,8 +22,6 @@ #define SYSCALL_WRITE 4 #define SYSCALL_RESET 208 #define SYSCALL_EXIT 1 -#define SYSCALL_MKNOD 14 -#define SYSCALL_MOUNT 410 #define SYSCALL_INT 0x80 #define RESET_NOSYNC 0x4 @@ -34,27 +32,6 @@ .global start,_start start: _start: - /* mount. */ - movl $SYSCALL_MOUNT, %eax - push $(tmpfs_args_end - tmpfs_args) - push $tmpfs_args - push $0 - push $devfsdir - push $devfstype - pushl $0 - int $SYSCALL_INT - addl $20, %esp - - /* mknod. */ - movl $SYSCALL_MKNOD, %eax - pushl $0 - pushl $0x2140 - leal device, %ebx - pushl %ebx - pushl $0 - int $SYSCALL_INT - addl $16, %esp - /* open. */ movl $SYSCALL_OPEN, %eax pushl $FLAGS_NONE @@ -107,25 +84,3 @@ device: message: .ascii "Boot Test Passed Successfully\n" SUCCESSFUL_BOOT_STRING "\n" messageend: -devfstype: - .ascii "tmpfs" - .byte 0 -devfsdir: - .ascii "/dev" - .byte 0 -tmpfs_args: - /* Version. */ - .long 1 - - /* Maximum inodes. */ - .quad 0 - /* Maximum size. */ - .quad 0 - - /* UID */ - .long 0 - /* GID */ - .long 0 - /* Mode */ - .long 0777 -tmpfs_args_end: \ No newline at end of file diff --git a/grub-core/tests/boot/knetbsd.init-x86_64.S b/grub-core/tests/boot/knetbsd.init-x86_64.S index 05a494594..1a19f3603 100644 --- a/grub-core/tests/boot/knetbsd.init-x86_64.S +++ b/grub-core/tests/boot/knetbsd.init-x86_64.S @@ -22,9 +22,7 @@ #define SYSCALL_WRITE 4 #define SYSCALL_RESET 208 #define SYSCALL_EXIT 1 -#define SYSCALL_MKNOD 14 #define SYSCALL_ARCH 165 -#define SYSCALL_MOUNT 410 #define SYSCALL_INT 0x80 #define SYSCALL_ARCH_IOPL 2 @@ -37,22 +35,6 @@ .global start,_start start: _start: - /* mount. */ - movq $SYSCALL_MOUNT, %rax - movq $devfstype, %rdi - movq $devfsdir, %rsi - movq $0, %rdx - movq $tmpfs_args, %r10 - movq $(tmpfs_args_end - tmpfs_args), %r8 - syscall - - /* mknod. */ - movq $SYSCALL_MKNOD, %rax - leaq device, %rdi - movq $0x2140, %rsi - movq $0, %rdx - syscall - /* open. */ movq $SYSCALL_OPEN, %rax leaq device, %rdi @@ -119,32 +101,5 @@ device: message: .ascii "Boot Test Passed Successfully\n" SUCCESSFUL_BOOT_STRING "\n" messageend: -devfstype: - .ascii "tmpfs" - .byte 0 -devfsdir: - .ascii "/dev" - .byte 0 -tmpfs_args: - /* Version. */ - .long 1 - - /* Alignment long. */ - .long 0 - - /* Maximum inodes. */ - .quad 0 - /* Maximum size. */ - .quad 0 - - /* UID */ - .long 0 - /* GID */ - .long 0 - /* Mode */ - .long 0777 - /* Alignment long. */ - .long 0 -tmpfs_args_end: iopl_arg: .long 3 \ No newline at end of file From 4619710a056e841fabd43e302347d5901e55e0e7 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 28 Aug 2010 11:32:49 +0200 Subject: [PATCH 148/178] Don't try to malloc if grub_mm_base is 0 --- grub-core/kern/mm.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c index fd4fde81c..dec07bc1b 100644 --- a/grub-core/kern/mm.c +++ b/grub-core/kern/mm.c @@ -284,6 +284,9 @@ grub_memalign (grub_size_t align, grub_size_t size) grub_size_t n = ((size + GRUB_MM_ALIGN - 1) >> GRUB_MM_ALIGN_LOG2) + 1; int count = 0; + if (!grub_mm_base) + goto fail; + align = (align >> GRUB_MM_ALIGN_LOG2); if (align == 0) align = 1; @@ -318,6 +321,7 @@ grub_memalign (grub_size_t align, grub_size_t size) break; } + fail: grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory"); return 0; } From 4aa5499064f6781699846b2272dd9ea67ee06d5a Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 28 Aug 2010 11:34:05 +0200 Subject: [PATCH 149/178] Prevent deadloop in term.c in case of out-of-memory --- grub-core/normal/charset.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/grub-core/normal/charset.c b/grub-core/normal/charset.c index fd377e1c6..b7f775c4f 100644 --- a/grub-core/normal/charset.c +++ b/grub-core/normal/charset.c @@ -297,13 +297,10 @@ grub_utf8_to_ucs4_alloc (const char *msg, grub_uint32_t **unicode_msg, { grub_size_t msg_len = grub_strlen (msg); - *unicode_msg = grub_malloc (grub_strlen (msg) * sizeof (grub_uint32_t)); + *unicode_msg = grub_malloc (msg_len * sizeof (grub_uint32_t)); if (!*unicode_msg) - { - grub_printf ("utf8_to_ucs4 ERROR1: %s", msg); - return -1; - } + return -1; msg_len = grub_utf8_to_ucs4 (*unicode_msg, msg_len, (grub_uint8_t *) msg, -1, 0); @@ -1215,6 +1212,8 @@ grub_bidi_logical_to_visual (const grub_uint32_t *logical, struct grub_unicode_glyph *visual_ptr; *visual_out = visual_ptr = grub_malloc (2 * sizeof (visual_ptr[0]) * logical_len); + if (!visual_ptr) + return -1; for (ptr = logical; ptr <= logical + logical_len; ptr++) { if (ptr == logical + logical_len || *ptr == '\n') From d1dce5d35684d23b4ce4af0b224c9a7005e3973e Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 28 Aug 2010 11:34:33 +0200 Subject: [PATCH 150/178] Add DEBUG_RELOCATOR parts --- grub-core/lib/relocator.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/grub-core/lib/relocator.c b/grub-core/lib/relocator.c index 9f9927929..0acd59b94 100644 --- a/grub-core/lib/relocator.c +++ b/grub-core/lib/relocator.c @@ -1283,6 +1283,25 @@ grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, chunk->srcv = grub_map_memory (chunk->src, chunk->size); *out = chunk; +#ifdef DEBUG_RELOCATOR + { + grub_mm_region_t r; + grub_mm_header_t p; + grub_memset (chunk->srcv, 0xfa, chunk->size); + for (r = grub_mm_base; r; r = r->next) + { + p = r->first; + do + { + if ((grub_addr_t) p < (grub_addr_t) (r + 1) + || (grub_addr_t) p >= (grub_addr_t) (r + 1) + r->size) + grub_fatal (__FILE__ ":%d: out of range pointer: %p\n", __LINE__, p); + p = p->next; + } + while (p != r->first); + } + } +#endif return GRUB_ERR_NONE; } @@ -1416,6 +1435,26 @@ grub_relocator_alloc_chunk_align (struct grub_relocator *rel, rel->chunks->next); chunk->srcv = grub_map_memory (chunk->src, chunk->size); *out = chunk; +#ifdef DEBUG_RELOCATOR + { + grub_mm_region_t r; + grub_mm_header_t p; + + grub_memset (chunk->srcv, 0xfa, chunk->size); + for (r = grub_mm_base; r; r = r->next) + { + p = r->first; + do + { + if ((grub_addr_t) p < (grub_addr_t) (r + 1) + || (grub_addr_t) p >= (grub_addr_t) (r + 1) + r->size) + grub_fatal (__FILE__ "%d: out of range pointer: %p\n", __LINE__, p); + p = p->next; + } + while (p != r->first); + } + } +#endif return GRUB_ERR_NONE; } From dcc953eecb077923049b6e6b50fbef3055122a2e Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 28 Aug 2010 11:35:02 +0200 Subject: [PATCH 151/178] Fallback to dumb printf if malloc failes --- grub-core/normal/term.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/grub-core/normal/term.c b/grub-core/normal/term.c index 4f5779abe..a5fdbe4e9 100644 --- a/grub-core/normal/term.c +++ b/grub-core/normal/term.c @@ -751,6 +751,34 @@ grub_xputs_normal (const char *str) if (!unicode_str) { grub_errno = GRUB_ERR_NONE; + for (; *str; str++) + { + grub_term_output_t term; + grub_uint32_t code = *str; + if (code > 0x7f) + code = '?'; + + FOR_ACTIVE_TERM_OUTPUTS(term) + { + struct grub_unicode_glyph c = + { + .base = code, + .variant = 0, + .attributes = 0, + .ncomb = 0, + .combining = 0, + .estimated_width = 1 + }; + + (term->putchar) (term, &c); + if (code == '\n') + { + c.base = '\r'; + (term->putchar) (term, &c); + } + } + } + return; } From 0f6a963e9b013eed61bc829dc5a49e6b5beeab99 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 28 Aug 2010 14:51:54 +0200 Subject: [PATCH 152/178] kOpenBSD bootcheck --- Makefile.am | 16 +++++--- .../{knetbsd.init-i386.S => kbsd.init-i386.S} | 41 ++++++++++++++++++- grub-core/tests/boot/kopenbsd.cfg | 5 +++ grub-core/tests/boot/kopenbsdlabel.txt | 3 ++ 4 files changed, 59 insertions(+), 6 deletions(-) rename grub-core/tests/boot/{knetbsd.init-i386.S => kbsd.init-i386.S} (76%) create mode 100644 grub-core/tests/boot/kopenbsd.cfg create mode 100644 grub-core/tests/boot/kopenbsdlabel.txt diff --git a/Makefile.am b/Makefile.am index 4b0ef7f4c..35f93bfb6 100644 --- a/Makefile.am +++ b/Makefile.am @@ -144,8 +144,11 @@ kfreebsd.init.x86_64: $(srcdir)/grub-core/tests/boot/kfreebsd.init-x86_64.S kfreebsd.init.i386: $(srcdir)/grub-core/tests/boot/kfreebsd.init-i386.S $(TARGET_CC) -o $@ $< -m32 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" && freebsd-brandelf -t FreeBSD $@ -knetbsd.init.i386: $(srcdir)/grub-core/tests/boot/knetbsd.init-i386.S - $(TARGET_CC) -o $@ $< -m32 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" +knetbsd.init.i386: $(srcdir)/grub-core/tests/boot/kbsd.init-i386.S + $(TARGET_CC) -o $@ $< -m32 -nostdlib -nostdinc -DTARGET_NETBSD=1 -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" + +kopenbsd.init.i386: $(srcdir)/grub-core/tests/boot/kbsd.init-i386.S + $(TARGET_CC) -o $@ $< -m32 -nostdlib -nostdinc -DTARGET_OPENBSD=1 -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" knetbsd.init.x86_64: $(srcdir)/grub-core/tests/boot/knetbsd.init-x86_64.S $(TARGET_CC) -o $@ $< -m64 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" @@ -162,8 +165,8 @@ kfreebsd-mfsroot.i386.img: kfreebsd.init.i386 Makefile knetbsd.image.i386: knetbsd.init.i386 $(srcdir)/grub-core/tests/boot/kbsd.spec.txt TDIR=`mktemp -d` && mkdir $$TDIR/dev && mkdir $$TDIR/sbin && cp $< $$TDIR/sbin/init && makefs -F $(srcdir)/grub-core/tests/boot/kbsd.spec.txt -t ffs -s 64k -f 10 -o minfree=0,version=1 $@ $$TDIR && rm -rf $$TDIR -kopenbsd.image.i386: kopenbsd.init.i386 - TDIR=`mktemp -d` && mkdir $$TDIR/dev && mkdir $$TDIR/sbin && cp $< $$TDIR/sbin/init && makefs -t ffs -s 64k -f 10 -o minfree=0,version=1 $@ $$TDIR && rm -rf $$TDIR +kopenbsd.image.i386: kopenbsd.init.i386 $(srcdir)/grub-core/tests/boot/kopenbsdlabel.txt + TDIR=`mktemp -d` && mkdir $$TDIR/dev && mkdir $$TDIR/sbin && cp $< $$TDIR/sbin/init && makefs -F $(srcdir)/grub-core/tests/boot/kbsd.spec.txt -t ffs -s 128k -f 10 -o minfree=0,version=1 $@ $$TDIR && bsdlabel -f -R $@ $(srcdir)/grub-core/tests/boot/kopenbsdlabel.txt && rm -rf $$TDIR || rm -f $@ knetbsd.miniroot-image.i386.img: knetbsd.image.i386 $(GRUB_PAYLOADS_DIR)/knetbsd.miniroot.i386 $(OBJCOPY) --add-section=miniroot=$< $(GRUB_PAYLOADS_DIR)/knetbsd.miniroot.i386 $@ @@ -195,7 +198,10 @@ knetbsd.miniroot-image.i386.gz: knetbsd.miniroot-image.i386.img gzip < $< > $@ bootcheck-knetbsd-i386: knetbsd.miniroot-image.i386.gz $(GRUB_PAYLOADS_DIR)/knetbsd.i386 $(srcdir)/grub-core/tests/boot/knetbsd.cfg grub-shell - timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-i386 --files=/miniroot.gz=knetbsd.miniroot-image.i386.gz --files=/knetbsd=$(GRUB_PAYLOADS_DIR)/knetbsd.i386 $(srcdir)/grub-core/tests/boot/knetbsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=$(QEMU32) --files=/miniroot.gz=knetbsd.miniroot-image.i386.gz --files=/knetbsd=$(GRUB_PAYLOADS_DIR)/knetbsd.i386 $(srcdir)/grub-core/tests/boot/knetbsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + +bootcheck-kopenbsd-i386: kopenbsd.image.i386 $(GRUB_PAYLOADS_DIR)/kopenbsd.i386 $(srcdir)/grub-core/tests/boot/kopenbsd.cfg grub-shell + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=$(QEMU32) --files=/ramdisk=kopenbsd.image.i386 --files=/kopenbsd=$(GRUB_PAYLOADS_DIR)/kopenbsd.i386 $(srcdir)/grub-core/tests/boot/kopenbsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null knetbsd.miniroot-image.x86_64.gz: knetbsd.miniroot-image.x86_64.img gzip < $< > $@ diff --git a/grub-core/tests/boot/knetbsd.init-i386.S b/grub-core/tests/boot/kbsd.init-i386.S similarity index 76% rename from grub-core/tests/boot/knetbsd.init-i386.S rename to grub-core/tests/boot/kbsd.init-i386.S index 587b4f41f..7011c79ff 100644 --- a/grub-core/tests/boot/knetbsd.init-i386.S +++ b/grub-core/tests/boot/kbsd.init-i386.S @@ -16,18 +16,29 @@ * along with GRUB. If not, see . */ +#ifdef TARGET_NETBSD +#define SYSCALL_RESET 208 +#elif defined (TARGET_OPENBSD) +#define SYSCALL_RESET 55 +#else +#error unknown target +#endif + #define MODE_RDRW 2 #define FLAGS_NONE 0 #define SYSCALL_OPEN 5 #define SYSCALL_WRITE 4 -#define SYSCALL_RESET 208 #define SYSCALL_EXIT 1 +#define SYSCALL_ARCH 165 #define SYSCALL_INT 0x80 +#define SYSCALL_ARCH_IOPL 2 #define RESET_NOSYNC 0x4 #define RESET_HALT 0x8 #define RESET_POWEROFF 0x800 +#define SHUTDOWN_PORT 0x8900 + .section ".init", "ax" .global start,_start start: @@ -53,6 +64,32 @@ _start: int $SYSCALL_INT addl $16, %esp + /* IOPL. */ + movl $SYSCALL_ARCH, %eax + pushl $iopl_arg + pushl $SYSCALL_ARCH_IOPL + pushl $0 + int $SYSCALL_INT + addl $12, %esp + + movw $SHUTDOWN_PORT, %dx + movb $'S', %al + outb %al, %dx + movb $'h', %al + outb %al, %dx + movb $'u', %al + outb %al, %dx + movb $'t', %al + outb %al, %dx + movb $'d', %al + outb %al, %dx + movb $'o', %al + outb %al, %dx + movb $'w', %al + outb %al, %dx + movb $'n', %al + outb %al, %dx + /* shutdown. */ movl $SYSCALL_RESET, %eax pushl $0 @@ -84,3 +121,5 @@ device: message: .ascii "Boot Test Passed Successfully\n" SUCCESSFUL_BOOT_STRING "\n" messageend: +iopl_arg: + .long 3 diff --git a/grub-core/tests/boot/kopenbsd.cfg b/grub-core/tests/boot/kopenbsd.cfg new file mode 100644 index 000000000..132bec4b6 --- /dev/null +++ b/grub-core/tests/boot/kopenbsd.cfg @@ -0,0 +1,5 @@ +kopenbsd /kopenbsd -h +kopenbsd_ramdisk /ramdisk +boot +# Shouln't happen +halt diff --git a/grub-core/tests/boot/kopenbsdlabel.txt b/grub-core/tests/boot/kopenbsdlabel.txt new file mode 100644 index 000000000..bb141133b --- /dev/null +++ b/grub-core/tests/boot/kopenbsdlabel.txt @@ -0,0 +1,3 @@ +# size offset fstype [fsize bsize bps/cpg] + a: 256 0 4.2BSD 0 0 1 + c: 256 0 unused 0 0 # "raw" part, don't edit From 4fc95be2e7206ca85a6b0ba1380b063f72c3f9cc Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 28 Aug 2010 14:52:25 +0200 Subject: [PATCH 153/178] fix grub-emu compilation --- include/grub/mm_private.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/grub/mm_private.h b/include/grub/mm_private.h index 2927f16c4..c2c4cb151 100644 --- a/include/grub/mm_private.h +++ b/include/grub/mm_private.h @@ -57,6 +57,8 @@ typedef struct grub_mm_region } *grub_mm_region_t; +#ifndef GRUB_MACHINE_EMU extern grub_mm_region_t EXPORT_VAR (grub_mm_base); +#endif #endif From 8e4ac346e86483619de8da601fb3fead85371f7f Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 28 Aug 2010 15:05:26 +0200 Subject: [PATCH 154/178] OpenBSD64 bootcheck --- Makefile.am | 15 +++++++++++++-- .../{knetbsd.init-x86_64.S => kbsd.init-x86_64.S} | 6 ++++++ 2 files changed, 19 insertions(+), 2 deletions(-) rename grub-core/tests/boot/{knetbsd.init-x86_64.S => kbsd.init-x86_64.S} (95%) diff --git a/Makefile.am b/Makefile.am index 35f93bfb6..53439fd42 100644 --- a/Makefile.am +++ b/Makefile.am @@ -150,8 +150,11 @@ knetbsd.init.i386: $(srcdir)/grub-core/tests/boot/kbsd.init-i386.S kopenbsd.init.i386: $(srcdir)/grub-core/tests/boot/kbsd.init-i386.S $(TARGET_CC) -o $@ $< -m32 -nostdlib -nostdinc -DTARGET_OPENBSD=1 -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" -knetbsd.init.x86_64: $(srcdir)/grub-core/tests/boot/knetbsd.init-x86_64.S - $(TARGET_CC) -o $@ $< -m64 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" +knetbsd.init.x86_64: $(srcdir)/grub-core/tests/boot/kbsd.init-x86_64.S + $(TARGET_CC) -o $@ $< -m64 -DTARGET_NETBSD=1 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" + +kopenbsd.init.x86_64: $(srcdir)/grub-core/tests/boot/kbsd.init-x86_64.S + $(TARGET_CC) -o $@ $< -m64 -DTARGET_OPENBSD=1 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" linux-initramfs.i386: linux.init.i386 Makefile TDIR=`mktemp -d`; cp $< $$TDIR/init; (cd $$TDIR; echo ./init | cpio --quiet --dereference -o -H newc) | gzip > $@; rm -rf $$TDIR @@ -168,6 +171,9 @@ knetbsd.image.i386: knetbsd.init.i386 $(srcdir)/grub-core/tests/boot/kbsd.spec.t kopenbsd.image.i386: kopenbsd.init.i386 $(srcdir)/grub-core/tests/boot/kopenbsdlabel.txt TDIR=`mktemp -d` && mkdir $$TDIR/dev && mkdir $$TDIR/sbin && cp $< $$TDIR/sbin/init && makefs -F $(srcdir)/grub-core/tests/boot/kbsd.spec.txt -t ffs -s 128k -f 10 -o minfree=0,version=1 $@ $$TDIR && bsdlabel -f -R $@ $(srcdir)/grub-core/tests/boot/kopenbsdlabel.txt && rm -rf $$TDIR || rm -f $@ +kopenbsd.image.x86_64: kopenbsd.init.x86_64 $(srcdir)/grub-core/tests/boot/kopenbsdlabel.txt + TDIR=`mktemp -d` && mkdir $$TDIR/dev && mkdir $$TDIR/sbin && cp $< $$TDIR/sbin/init && makefs -F $(srcdir)/grub-core/tests/boot/kbsd.spec.txt -t ffs -s 128k -f 10 -o minfree=0,version=1 $@ $$TDIR && bsdlabel -f -R $@ $(srcdir)/grub-core/tests/boot/kopenbsdlabel.txt && rm -rf $$TDIR || rm -f $@ + knetbsd.miniroot-image.i386.img: knetbsd.image.i386 $(GRUB_PAYLOADS_DIR)/knetbsd.miniroot.i386 $(OBJCOPY) --add-section=miniroot=$< $(GRUB_PAYLOADS_DIR)/knetbsd.miniroot.i386 $@ @@ -203,6 +209,9 @@ bootcheck-knetbsd-i386: knetbsd.miniroot-image.i386.gz $(GRUB_PAYLOADS_DIR)/knet bootcheck-kopenbsd-i386: kopenbsd.image.i386 $(GRUB_PAYLOADS_DIR)/kopenbsd.i386 $(srcdir)/grub-core/tests/boot/kopenbsd.cfg grub-shell timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=$(QEMU32) --files=/ramdisk=kopenbsd.image.i386 --files=/kopenbsd=$(GRUB_PAYLOADS_DIR)/kopenbsd.i386 $(srcdir)/grub-core/tests/boot/kopenbsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null +bootcheck-kopenbsd-x86_64: kopenbsd.image.x86_64 $(GRUB_PAYLOADS_DIR)/kopenbsd.x86_64 $(srcdir)/grub-core/tests/boot/kopenbsd.cfg grub-shell + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=qemu-system-x86_64 --files=/ramdisk=kopenbsd.image.x86_64 --files=/kopenbsd=$(GRUB_PAYLOADS_DIR)/kopenbsd.x86_64 $(srcdir)/grub-core/tests/boot/kopenbsd.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + knetbsd.miniroot-image.x86_64.gz: knetbsd.miniroot-image.x86_64.img gzip < $< > $@ @@ -236,6 +245,8 @@ if COND_i386_pc BOOTCHECKS += bootcheck-pc-chainloader endif +BOOTCHECKS += bootcheck-kopenbsd-i386 bootcheck-kopenbsd-x86_64 + BOOTCHECKS += bootcheck-multiboot bootcheck-multiboot2 BOOTCHECKS += bootcheck-linux16-i386 bootcheck-linux16-x86_64 diff --git a/grub-core/tests/boot/knetbsd.init-x86_64.S b/grub-core/tests/boot/kbsd.init-x86_64.S similarity index 95% rename from grub-core/tests/boot/knetbsd.init-x86_64.S rename to grub-core/tests/boot/kbsd.init-x86_64.S index 1a19f3603..58400db0d 100644 --- a/grub-core/tests/boot/knetbsd.init-x86_64.S +++ b/grub-core/tests/boot/kbsd.init-x86_64.S @@ -20,7 +20,13 @@ #define FLAGS_NONE 0 #define SYSCALL_OPEN 5 #define SYSCALL_WRITE 4 +#ifdef TARGET_NETBSD #define SYSCALL_RESET 208 +#elif defined (TARGET_OPENBSD) +#define SYSCALL_RESET 55 +#else +#error unknown target +#endif #define SYSCALL_EXIT 1 #define SYSCALL_ARCH 165 #define SYSCALL_INT 0x80 From 0101a723ce52d6962b2a32028425f915c8a8598c Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 28 Aug 2010 16:05:20 +0200 Subject: [PATCH 155/178] ntldr bootcheck --- Makefile.am | 13 +++++++++++-- grub-core/tests/boot/ntldr.cfg | 4 ++++ grub-core/tests/boot/pc-chainloader.S | 8 +++++++- 3 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 grub-core/tests/boot/ntldr.cfg diff --git a/Makefile.am b/Makefile.am index 53439fd42..64c9c7e0f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -130,11 +130,17 @@ multiboot.elf: $(srcdir)/grub-core/tests/boot/multiboot.S $(TARGET_CC) -o $@ $< -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" -ffreestanding -nostdlib -nostdinc -Wl,-N -Wl,-Ttext,0x100000 -m32 -I$(srcdir)/include pc-chainloader.elf: $(srcdir)/grub-core/tests/boot/pc-chainloader.S - $(TARGET_CC) -o $@ $< -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" -ffreestanding -nostdlib -nostdinc -Wl,-N -Wl,-Ttext,0x7c00 -m32 + $(TARGET_CC) -o $@ $< -DTARGET_CHAINLOADER=1 -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" -ffreestanding -nostdlib -nostdinc -Wl,-N -Wl,-Ttext,0x7c00 -m32 pc-chainloader.bin: pc-chainloader.elf $(OBJCOPY) -O binary --strip-unneeded -R .note -R .comment -R .note.gnu.build-id -R .reginfo -R .rel.dyn $< $@; +ntldr.elf: $(srcdir)/grub-core/tests/boot/pc-chainloader.S + $(TARGET_CC) -o $@ $< -DTARGET_NTLDR=1 -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" -ffreestanding -nostdlib -nostdinc -Wl,-N -Wl,-Ttext,0 -m32 + +ntldr.bin: ntldr.elf + $(OBJCOPY) -O binary --strip-unneeded -R .note -R .comment -R .note.gnu.build-id -R .reginfo -R .rel.dyn $< $@; + multiboot2.elf: $(srcdir)/grub-core/tests/boot/multiboot.S $(TARGET_CC) -o $@ $< -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" -ffreestanding -nostdlib -nostdinc -Wl,-N -Wl,-Ttext,0x100000 -m32 -I$(srcdir)/include -DMULTIBOOT2=1 @@ -239,10 +245,13 @@ bootcheck-multiboot2: multiboot2.elf $(srcdir)/grub-core/tests/boot/multiboot2.c bootcheck-pc-chainloader: pc-chainloader.bin $(srcdir)/grub-core/tests/boot/pc-chainloader.cfg grub-shell timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=$(QEMU32) --files=/pc-chainloader.bin=pc-chainloader.bin $(srcdir)/grub-core/tests/boot/pc-chainloader.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null +bootcheck-ntldr: ntldr.bin $(srcdir)/grub-core/tests/boot/ntldr.cfg grub-shell + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=$(QEMU32) --files=/ntldr.bin=ntldr.bin $(srcdir)/grub-core/tests/boot/ntldr.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + BOOTCHECKS= if COND_i386_pc -BOOTCHECKS += bootcheck-pc-chainloader +BOOTCHECKS += bootcheck-pc-chainloader bootcheck-ntldr endif BOOTCHECKS += bootcheck-kopenbsd-i386 bootcheck-kopenbsd-x86_64 diff --git a/grub-core/tests/boot/ntldr.cfg b/grub-core/tests/boot/ntldr.cfg new file mode 100644 index 000000000..cd438a4bc --- /dev/null +++ b/grub-core/tests/boot/ntldr.cfg @@ -0,0 +1,4 @@ +ntldr /ntldr.bin +boot +# Shouln't happen +halt diff --git a/grub-core/tests/boot/pc-chainloader.S b/grub-core/tests/boot/pc-chainloader.S index fc7429940..20040dabc 100644 --- a/grub-core/tests/boot/pc-chainloader.S +++ b/grub-core/tests/boot/pc-chainloader.S @@ -39,8 +39,14 @@ serialmsg: 1: ret -cont: +cont: +#ifdef TARGET_NTLDR + movw $0x2000, %ax +#elif defined (TARGET_CHAINLOADER) xorw %ax, %ax +#else +#error unsupported target +#endif movw %ax, %ds lea message, %si call serialmsg From 9bd44ab21ac53f307028a051c3c91121179c6da7 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 28 Aug 2010 16:21:45 +0200 Subject: [PATCH 156/178] kfreebsd-aout bootchecks --- Makefile.am | 13 ++++++++++++- grub-core/tests/boot/kfreebsd-aout.cfg | 4 ++++ grub-core/tests/boot/multiboot.S | 8 ++++---- 3 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 grub-core/tests/boot/kfreebsd-aout.cfg diff --git a/Makefile.am b/Makefile.am index 64c9c7e0f..ea9a64ce2 100644 --- a/Makefile.am +++ b/Makefile.am @@ -127,8 +127,14 @@ linux.init.i386: $(srcdir)/grub-core/tests/boot/linux.init-i386.S $(TARGET_CC) -o $@ $< -m32 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" multiboot.elf: $(srcdir)/grub-core/tests/boot/multiboot.S + $(TARGET_CC) -o $@ $< -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" -ffreestanding -nostdlib -nostdinc -DTARGET_MULTIBOOT=1 -Wl,-N -Wl,-Ttext,0x100000 -m32 -I$(srcdir)/include + +kfreebsd.elf: $(srcdir)/grub-core/tests/boot/multiboot.S $(TARGET_CC) -o $@ $< -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" -ffreestanding -nostdlib -nostdinc -Wl,-N -Wl,-Ttext,0x100000 -m32 -I$(srcdir)/include +kfreebsd.aout: kfreebsd.elf + $(OBJCOPY) -O a.out-i386-linux $< $@ -R .note.gnu.build-id + pc-chainloader.elf: $(srcdir)/grub-core/tests/boot/pc-chainloader.S $(TARGET_CC) -o $@ $< -DTARGET_CHAINLOADER=1 -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" -ffreestanding -nostdlib -nostdinc -Wl,-N -Wl,-Ttext,0x7c00 -m32 @@ -142,7 +148,7 @@ ntldr.bin: ntldr.elf $(OBJCOPY) -O binary --strip-unneeded -R .note -R .comment -R .note.gnu.build-id -R .reginfo -R .rel.dyn $< $@; multiboot2.elf: $(srcdir)/grub-core/tests/boot/multiboot.S - $(TARGET_CC) -o $@ $< -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" -ffreestanding -nostdlib -nostdinc -Wl,-N -Wl,-Ttext,0x100000 -m32 -I$(srcdir)/include -DMULTIBOOT2=1 + $(TARGET_CC) -o $@ $< -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" -ffreestanding -nostdlib -nostdinc -Wl,-N -Wl,-Ttext,0x100000 -m32 -I$(srcdir)/include -DTARGET_MULTIBOOT2=1 kfreebsd.init.x86_64: $(srcdir)/grub-core/tests/boot/kfreebsd.init-x86_64.S $(TARGET_CC) -o $@ $< -m64 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" && freebsd-brandelf -t FreeBSD $@ @@ -242,6 +248,9 @@ bootcheck-multiboot: multiboot.elf $(srcdir)/grub-core/tests/boot/multiboot.cfg bootcheck-multiboot2: multiboot2.elf $(srcdir)/grub-core/tests/boot/multiboot2.cfg grub-shell timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=$(QEMU32) --files=/multiboot2.elf=multiboot2.elf $(srcdir)/grub-core/tests/boot/multiboot2.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null +bootcheck-kfreebsd-aout: kfreebsd.aout $(srcdir)/grub-core/tests/boot/kfreebsd-aout.cfg grub-shell + timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=$(QEMU32) --files=/kfreebsd.aout=kfreebsd.aout $(srcdir)/grub-core/tests/boot/kfreebsd-aout.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null + bootcheck-pc-chainloader: pc-chainloader.bin $(srcdir)/grub-core/tests/boot/pc-chainloader.cfg grub-shell timeout -s KILL $(BOOTCHECK_TIMEOUT) ./grub-shell --qemu-opts="$(GRUB_QEMU_OPTS)" --boot=$(BOOTTARGET) --qemu=$(QEMU32) --files=/pc-chainloader.bin=pc-chainloader.bin $(srcdir)/grub-core/tests/boot/pc-chainloader.cfg | grep $(SUCCESSFUL_BOOT_STRING) > /dev/null @@ -254,6 +263,8 @@ if COND_i386_pc BOOTCHECKS += bootcheck-pc-chainloader bootcheck-ntldr endif +BOOTCHECKS += bootcheck-kfreebsd-aout + BOOTCHECKS += bootcheck-kopenbsd-i386 bootcheck-kopenbsd-x86_64 BOOTCHECKS += bootcheck-multiboot bootcheck-multiboot2 diff --git a/grub-core/tests/boot/kfreebsd-aout.cfg b/grub-core/tests/boot/kfreebsd-aout.cfg new file mode 100644 index 000000000..31e34b4ee --- /dev/null +++ b/grub-core/tests/boot/kfreebsd-aout.cfg @@ -0,0 +1,4 @@ +kfreebsd /kfreebsd.aout +boot +# Shouln't happen +halt diff --git a/grub-core/tests/boot/multiboot.S b/grub-core/tests/boot/multiboot.S index b9c0059c0..904b0d4c7 100644 --- a/grub-core/tests/boot/multiboot.S +++ b/grub-core/tests/boot/multiboot.S @@ -1,7 +1,7 @@ #define ASM_FILE 1 -#ifdef MULTIBOOT2 +#ifdef TARGET_MULTIBOOT2 #include -#else +#elif defined (TARGET_MULTIBOOT) #include #endif @@ -11,7 +11,7 @@ /* Align 32 bits boundary. */ .align 8 -#ifdef MULTIBOOT2 +#ifdef TARGET_MULTIBOOT2 /* Multiboot header. */ multiboot_header: /* magic */ @@ -26,7 +26,7 @@ multiboot_header: .short 0 .long 8 multiboot_header_end: -#else +#elif defined (TARGET_MULTIBOOT) /* Multiboot header. */ multiboot_header: /* magic */ From 9b1cb542dba38fb25b54c8c6e059b8bb7679f3b5 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 28 Aug 2010 16:51:36 +0200 Subject: [PATCH 157/178] Rename test kernels --- Makefile.am | 10 +++++----- .../tests/boot/{pc-chainloader.S => kernel-8086.S} | 0 grub-core/tests/boot/{multiboot.S => kernel-i386.S} | 0 3 files changed, 5 insertions(+), 5 deletions(-) rename grub-core/tests/boot/{pc-chainloader.S => kernel-8086.S} (100%) rename grub-core/tests/boot/{multiboot.S => kernel-i386.S} (100%) diff --git a/Makefile.am b/Makefile.am index ea9a64ce2..986d5234f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -126,28 +126,28 @@ linux.init.x86_64: $(srcdir)/grub-core/tests/boot/linux.init-x86_64.S linux.init.i386: $(srcdir)/grub-core/tests/boot/linux.init-i386.S $(TARGET_CC) -o $@ $< -m32 -nostdlib -nostdinc -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" -multiboot.elf: $(srcdir)/grub-core/tests/boot/multiboot.S +multiboot.elf: $(srcdir)/grub-core/tests/boot/kernel-i386.S $(TARGET_CC) -o $@ $< -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" -ffreestanding -nostdlib -nostdinc -DTARGET_MULTIBOOT=1 -Wl,-N -Wl,-Ttext,0x100000 -m32 -I$(srcdir)/include -kfreebsd.elf: $(srcdir)/grub-core/tests/boot/multiboot.S +kfreebsd.elf: $(srcdir)/grub-core/tests/boot/kernel-i386.S $(TARGET_CC) -o $@ $< -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" -ffreestanding -nostdlib -nostdinc -Wl,-N -Wl,-Ttext,0x100000 -m32 -I$(srcdir)/include kfreebsd.aout: kfreebsd.elf $(OBJCOPY) -O a.out-i386-linux $< $@ -R .note.gnu.build-id -pc-chainloader.elf: $(srcdir)/grub-core/tests/boot/pc-chainloader.S +pc-chainloader.elf: $(srcdir)/grub-core/tests/boot/kernel-8086.S $(TARGET_CC) -o $@ $< -DTARGET_CHAINLOADER=1 -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" -ffreestanding -nostdlib -nostdinc -Wl,-N -Wl,-Ttext,0x7c00 -m32 pc-chainloader.bin: pc-chainloader.elf $(OBJCOPY) -O binary --strip-unneeded -R .note -R .comment -R .note.gnu.build-id -R .reginfo -R .rel.dyn $< $@; -ntldr.elf: $(srcdir)/grub-core/tests/boot/pc-chainloader.S +ntldr.elf: $(srcdir)/grub-core/tests/boot/kernel-8086.S $(TARGET_CC) -o $@ $< -DTARGET_NTLDR=1 -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" -ffreestanding -nostdlib -nostdinc -Wl,-N -Wl,-Ttext,0 -m32 ntldr.bin: ntldr.elf $(OBJCOPY) -O binary --strip-unneeded -R .note -R .comment -R .note.gnu.build-id -R .reginfo -R .rel.dyn $< $@; -multiboot2.elf: $(srcdir)/grub-core/tests/boot/multiboot.S +multiboot2.elf: $(srcdir)/grub-core/tests/boot/kernel-i386.S $(TARGET_CC) -o $@ $< -DSUCCESSFUL_BOOT_STRING=\"$(SUCCESSFUL_BOOT_STRING)\" -ffreestanding -nostdlib -nostdinc -Wl,-N -Wl,-Ttext,0x100000 -m32 -I$(srcdir)/include -DTARGET_MULTIBOOT2=1 kfreebsd.init.x86_64: $(srcdir)/grub-core/tests/boot/kfreebsd.init-x86_64.S diff --git a/grub-core/tests/boot/pc-chainloader.S b/grub-core/tests/boot/kernel-8086.S similarity index 100% rename from grub-core/tests/boot/pc-chainloader.S rename to grub-core/tests/boot/kernel-8086.S diff --git a/grub-core/tests/boot/multiboot.S b/grub-core/tests/boot/kernel-i386.S similarity index 100% rename from grub-core/tests/boot/multiboot.S rename to grub-core/tests/boot/kernel-i386.S From 4a842991dbba38df15767a8def9f3e8363abdf37 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 28 Aug 2010 17:33:32 +0200 Subject: [PATCH 158/178] simplify normal/term.c and fix mismerge --- grub-core/normal/term.c | 66 ++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 18 deletions(-) diff --git a/grub-core/normal/term.c b/grub-core/normal/term.c index e6ef002d0..02850ef4e 100644 --- a/grub-core/normal/term.c +++ b/grub-core/normal/term.c @@ -206,19 +206,36 @@ void grub_puts_terminal (const char *str, struct grub_term_output *term) { grub_uint32_t *unicode_str, *unicode_last_position; + grub_error_push (); grub_utf8_to_ucs4_alloc (str, &unicode_str, &unicode_last_position); + grub_error_pop (); if (!unicode_str) { - for (; str; str++) + for (; *str; str++) { - grub_uint32_t code = *str; - if (code > 0x7f) - code = '?'; + struct grub_unicode_glyph c = + { + .variant = 0, + .attributes = 0, + .ncomb = 0, + .combining = 0, + .estimated_width = 1, + .base = *str + }; - putcode_real (term, code); - if (code == '\n') - putcode_real (term, '\r'); + FOR_ACTIVE_TERM_OUTPUTS(term) + { + (term->putchar) (term, &c); + } + if (*str == '\n') + { + c.base = '\r'; + FOR_ACTIVE_TERM_OUTPUTS(term) + { + (term->putchar) (term, &c); + } + } } return; } @@ -760,28 +777,41 @@ grub_print_ucs4 (const grub_uint32_t * str, void grub_xputs_normal (const char *str) { - grub_term_output_t term; - grub_uint32_t *unicode_str, *unicode_last_position; + grub_uint32_t *unicode_str = NULL, *unicode_last_position; int backlog = 0; + grub_term_output_t term; + + grub_error_push (); grub_utf8_to_ucs4_alloc (str, &unicode_str, - &unicode_last_position); + &unicode_last_position); + grub_error_pop (); if (!unicode_str) { - grub_errno = GRUB_ERR_NONE; for (; *str; str++) { - grub_term_output_t term; - grub_uint32_t code = *str; - if (code > 0x7f) - code = '?'; + struct grub_unicode_glyph c = + { + .variant = 0, + .attributes = 0, + .ncomb = 0, + .combining = 0, + .estimated_width = 1, + .base = *str + }; FOR_ACTIVE_TERM_OUTPUTS(term) { - putcode_real (term, code); - if (code == '\n') - putcode_real (term, '\r'); + (term->putchar) (term, &c); } + if (*str == '\n') + { + c.base = '\r'; + FOR_ACTIVE_TERM_OUTPUTS(term) + { + (term->putchar) (term, &c); + } + } } return; From 197eb519e5edee9ee5e8c6bdde3939440ae70041 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 28 Aug 2010 19:27:30 +0200 Subject: [PATCH 159/178] Remove leftover _printf --- grub-core/lib/i386/relocator.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/grub-core/lib/i386/relocator.c b/grub-core/lib/i386/relocator.c index 55e6b5578..f06a6ef86 100644 --- a/grub-core/lib/i386/relocator.c +++ b/grub-core/lib/i386/relocator.c @@ -218,8 +218,6 @@ grub_relocator16_boot (struct grub_relocator *rel, if (err) return err; - grub_printf ("%p\n", relst); - asm volatile ("cli"); ((void (*) (void)) relst) (); From c6785a2380185a960efeb16555c8610160831951 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 28 Aug 2010 21:25:35 +0200 Subject: [PATCH 160/178] Don't allocate relocator twice when loading aout --- grub-core/loader/i386/bsd.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/grub-core/loader/i386/bsd.c b/grub-core/loader/i386/bsd.c index 770c6f278..d6a22da5b 100644 --- a/grub-core/loader/i386/bsd.c +++ b/grub-core/loader/i386/bsd.c @@ -1129,10 +1129,6 @@ grub_bsd_load_aout (grub_file_t file) else bss_size = 0; - relocator = grub_relocator_new (); - if (!relocator) - return grub_errno; - { grub_relocator_chunk_t ch; From 328951ac24132cfef37f4c6a5558fe0e388a7f75 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 28 Aug 2010 21:27:10 +0200 Subject: [PATCH 161/178] Add safety checks in relocator and add a GRUB_MM_CHECK macro --- grub-core/lib/relocator.c | 36 +++++++++++++++++++++++++++++++++++- include/grub/mm.h | 3 +++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/grub-core/lib/relocator.c b/grub-core/lib/relocator.c index 0acd59b94..90b383301 100644 --- a/grub-core/lib/relocator.c +++ b/grub-core/lib/relocator.c @@ -582,8 +582,15 @@ malloc_in_range (struct grub_relocator *rel, int pre_added = 0; pa = r->first; p = pa->next; + if (p->magic == GRUB_MM_ALLOC_MAGIC) + continue; do - { + { + grub_dprintf ("relocator", "free block %p+0x%x\n", + p, p->size); + if (p->magic != GRUB_MM_FREE_MAGIC) + grub_fatal (__FILE__":%d free magic broken at %p (0x%x)\n", + __LINE__, p, p->magic); if (p == (grub_mm_header_t) (r + 1)) { pre_added = 1; @@ -1586,3 +1593,30 @@ grub_relocator_prepare_relocs (struct grub_relocator *rel, grub_addr_t addr, grub_free (sorted); return GRUB_ERR_NONE; } + +void +grub_mm_check_real (char *file, int line) +{ + grub_mm_region_t r; + grub_mm_header_t p, pa; + + for (r = grub_mm_base; r; r = r->next) + { + pa = r->first; + p = pa->next; + if (p->magic == GRUB_MM_ALLOC_MAGIC) + continue; + do + { + if ((grub_addr_t) p < (grub_addr_t) (r + 1) + || (grub_addr_t) p >= (grub_addr_t) (r + 1) + r->size) + grub_fatal ("%s:%d: out of range pointer: %p\n", file, line, p); + if (p->magic != GRUB_MM_FREE_MAGIC) + grub_fatal ("%s:%d free magic broken at %p (0x%x)\n", file, + line, p, p->magic); + pa = p; + p = pa->next; + } + while (pa != r->first); + } +} diff --git a/include/grub/mm.h b/include/grub/mm.h index 38dd39646..cc115907a 100644 --- a/include/grub/mm.h +++ b/include/grub/mm.h @@ -35,6 +35,9 @@ void EXPORT_FUNC(grub_free) (void *ptr); void *EXPORT_FUNC(grub_realloc) (void *ptr, grub_size_t size); void *EXPORT_FUNC(grub_memalign) (grub_size_t align, grub_size_t size); +void grub_mm_check_real (char *file, int line); +#define GRUB_MM_CHECK grub_mm_check_real (__FILE__, __LINE__); + /* For debugging. */ #if defined(MM_DEBUG) && !defined(GRUB_UTIL) && !defined (GRUB_MACHINE_EMU) /* Set this variable to 1 when you want to trace all memory function calls. */ From 04a0a4cdf4bfd778b7992491b73c78fd74163234 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 29 Aug 2010 00:19:13 +0200 Subject: [PATCH 162/178] Fix a bug in memory allocation --- grub-core/kern/mm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c index dec07bc1b..8d9b5db78 100644 --- a/grub-core/kern/mm.c +++ b/grub-core/kern/mm.c @@ -171,6 +171,7 @@ grub_real_malloc (grub_mm_header_t *first, grub_size_t n, grub_size_t align) if (p->size >= n + extra) { + extra += (p->size - extra - n) & (~(align - 1)); if (extra == 0 && p->size == n) { /* There is no special alignment requirement and memory block @@ -246,7 +247,6 @@ grub_real_malloc (grub_mm_header_t *first, grub_size_t n, grub_size_t align) */ grub_mm_header_t r; - extra += (p->size - extra - n) & (~(align - 1)); r = p + extra + n; r->magic = GRUB_MM_FREE_MAGIC; r->size = p->size - extra - n; From 5407820787a7bd8b60ba4dbc009001ad20071d28 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 29 Aug 2010 00:20:22 +0200 Subject: [PATCH 163/178] Adjust kfreebsd.cfg for EFI --- grub-core/tests/boot/kfreebsd.cfg | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/grub-core/tests/boot/kfreebsd.cfg b/grub-core/tests/boot/kfreebsd.cfg index 5534f3c03..f28ee7998 100644 --- a/grub-core/tests/boot/kfreebsd.cfg +++ b/grub-core/tests/boot/kfreebsd.cfg @@ -1,7 +1,8 @@ -kfreebsd /kfreebsd -h +kfreebsd /kfreebsd -hv kfreebsd_loadenv /kfreebsd_env kfreebsd_module /mfsroot.gz type=mfs_root set kFreeBSD.hw.hasbrokenint12=1 +fakebios boot # Shouln't happen halt From 02a16ba94c3c8659709281663a194e37d0270ad2 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 29 Aug 2010 00:21:02 +0200 Subject: [PATCH 164/178] Disable some bootcheck on some platforms --- Makefile.am | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/Makefile.am b/Makefile.am index 986d5234f..fc3d273f2 100644 --- a/Makefile.am +++ b/Makefile.am @@ -260,7 +260,23 @@ bootcheck-ntldr: ntldr.bin $(srcdir)/grub-core/tests/boot/ntldr.cfg grub-shell BOOTCHECKS= if COND_i386_pc -BOOTCHECKS += bootcheck-pc-chainloader bootcheck-ntldr +#pc chainloader by definition is only for i386-pc +BOOTCHECKS += bootcheck-pc-chainloader +#ntldr and bootmgr require BIOS. +BOOTCHECKS += bootcheck-ntldr +#legacy protocol makes early BIOS calls. +BOOTCHECKS += bootcheck-linux16-i386 bootcheck-linux16-x86_64 +# Crashes early on non-BIOS +BOOTCHECKS += bootcheck-knetbsd-i386 +endif + +if !COND_i386_coreboot +# Crashes because memory at 0-0x1000 is occupied +BOOTCHECKS += bootcheck-kfreebsd-x86_64 +# Likewise. +BOOTCHECKS += bootcheck-knetbsd-x86_64 + +BOOTCHECKS += bootcheck-kfreebsd-i386 endif BOOTCHECKS += bootcheck-kfreebsd-aout @@ -269,18 +285,8 @@ BOOTCHECKS += bootcheck-kopenbsd-i386 bootcheck-kopenbsd-x86_64 BOOTCHECKS += bootcheck-multiboot bootcheck-multiboot2 -BOOTCHECKS += bootcheck-linux16-i386 bootcheck-linux16-x86_64 - BOOTCHECKS += bootcheck-linux-i386 bootcheck-linux-x86_64 -# Crashes because memory at 0-0x1000 is occupied -BOOTCHECKS += bootcheck-kfreebsd-i386 bootcheck-knetbsd-x86_64 - -# Requires ACPI -BOOTCHECKS += bootcheck-kfreebsd-x86_64 -# Crashes early on non-BIOS -BOOTCHECKS += bootcheck-knetbsd-i386 - .PHONY: bootcheck-linux-i386 bootcheck-linux-x86_64 \ bootcheck-kfreebsd-i386 bootcheck-kfreebsd-x86_64 \ From 5dc598851fec1c0783fda53018e2a007835ba4e1 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 29 Aug 2010 00:21:16 +0200 Subject: [PATCH 165/178] Document newreloc --- docs/grub.texi | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/docs/grub.texi b/docs/grub.texi index f533a029c..85316fced 100644 --- a/docs/grub.texi +++ b/docs/grub.texi @@ -85,6 +85,7 @@ This edition documents version @value{VERSION}. * Interface:: The menu and the command-line * Commands:: The list of available builtin commands * Security:: Authentication and authorisation +* Supported kernels:: The list of supported kernels * Troubleshooting:: Error messages produced by GRUB * Invoking grub-install:: How to use the GRUB installer * Invoking grub-mkconfig:: Generate a GRUB configuration file @@ -2862,6 +2863,74 @@ adding @kbd{set superusers=} and @kbd{password} or @kbd{password_pbkdf2} commands. +@node Supported kernels +@chapter Supported boot targets + +X86 support is summarised in following table. ``Yes'' means that kernel works on the given platform, ``crashes'' means an early kernel crash which we hove will be fixed by concerned kernel developpers. ``no'' means GRUB doesn't load given kernel on a given platform. ``headless'' means that the kernel works but lacks console drivers (you can still use serial or network console). In case of ``no'' and ``crashes'' the reason is given in footnote. +@multitable @columnfractions .50 .15 .15 .15 +@item @tab BIOS @tab Coreboot @tab 32-bit EFI +@item BIOS chainloading @tab yes @tab no (1) @tab no (1) +@item NTLDR @tab yes @tab no (1) @tab no (1) +@item FreeBSD bootloader @tab yes @tab crashes (1)@tab crashes (1) +@item 32-bit kFreeBSD @tab yes @tab ? @tab headless +@item 64-bit kFreeBSD @tab yes @tab crashes (2)@tab headless +@item 32-bit kNetBSD @tab yes @tab crashes (1)@tab crashes (1) +@item 64-bit kNetBSD @tab yes @tab crashes (2)@tab yes +@item 32-bit kOpenBSD @tab yes @tab yes @tab headless +@item 64-bit kOpenBSD @tab yes @tab yes @tab headless +@item Multiboot @tab yes @tab yes @tab yes +@item Multiboot2 @tab yes @tab yes @tab yes +@item 32-bit Linux (legacy protocol) @tab yes @tab no (1) @tab no (1) +@item 64-bit Linux (legacy protocol) @tab yes @tab no (1) @tab no (1) +@item 32-bit Linux (modern protocol) @tab yes @tab yes @tab yes +@item 64-bit Linux (modern protocol) @tab yes @tab yes @tab yes +@item 32-bit XNU @tab yes @tab ? @tab yes +@item 64-bit XNU @tab yes @tab ? @tab yes (5) +@item 32-bit EFI chainloader @tab no (3) @tab no (3) @tab yes +@item 64-bit EFI chainloader @tab no (3) @tab no (3) @tab no (4) +@item Appleloader @tab no (3) @tab no (3) @tab yes +@end multitable + +@enumerate +@item Requires BIOS +@item Crashes because the memory at 0x0-0x1000 isn't available +@item EFI only +@item 32-bit and 64-bit EFI have different structures and work in different CPU modes so it's not possible to chainload 32-bit bootloader on 64-bit platform and vice-versa +@item Some modules may need to be disabled +@end enumerate + + +PowerPC and Sparc ports support only Linux. +MIPS port supports Linux and multiboot2. + +@chapter Boot tests + +As you have seen in previous chapter the support matrix is pretty big and some of the configurations are only rarely used. To ensure the quality bootchecks are available for all x86 targets except EFI chainloader, Appleloader and XNU. All x86 platforms have bootcheck facility except multiboot and ieee1275. Multiboot, multiboot2, BIOS chainloader, ntldr and freebsd-bootloader boot targets are tested only with a fake kernel images. Only Linux is tested among the payloads using Linux protocols. + +Following variables must be defined: + +@multitable @columnfractions .30 .65 +@item GRUB_PAYLOADS_DIR @tab directory containing the required kernels +@item GRUB_CBFSTOOL @tab cbfstoll from Coreboot package (for coreboot platform only) +@item GRUB_COREBOOT_ROM @tab empty Coreboot ROM +@item GRUB_QEMU_OPTS @tab additional options to be supplied to QEMU +@end multitable + +Required files are: + +@multitable @columnfractions .40 .55 +@item kfreebsd_env.i386 @tab 32-bit kFreeBSD device hints +@item kfreebsd.i386 @tab 32-bit FreeBSD kernel image +@item kfreebsd.x86_64, kfreebsd_env.x86_64 @tab same from 64-bit kFreeBSD +@item knetbsd.i386 @tab 32-bit NetBSD kernel image +@item knetbsd.miniroot.i386 @tab 32-bit kNetBSD miniroot.kmod. +@item knetbsd.x86_64, knetbsd.miniroot.x86_64 @tab same from 64-bit kNetBSD +@item kopenbsd.i386 @tab 32-bit OpenBSD kernel bsd.rd image +@item kopenbsd.x86_64 @tab same from 64-bit kOpenBSD +@item linux.i386 @tab 32-bit Linux +@item linux.x86_64 @tab 64-bit Linux +@end multitable + @node Troubleshooting @chapter Error messages produced by GRUB From f0b05761f49596a276794f8c3082a75b7510821d Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 29 Aug 2010 00:46:36 +0200 Subject: [PATCH 166/178] fix multiboot compilation --- grub-core/Makefile.core.def | 1 + include/grub/offsets.h | 4 ++++ util/grub-mkimage.c | 4 ++-- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index 62c08928a..0917e749c 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -361,6 +361,7 @@ module = { enable = x86_efi; enable = i386_ieee1275; enable = i386_coreboot; + enable = i386_multiboot; emu_condition = COND_GRUB_EMU_PCI; }; diff --git a/include/grub/offsets.h b/include/grub/offsets.h index ae0b2557e..7763e488c 100644 --- a/include/grub/offsets.h +++ b/include/grub/offsets.h @@ -123,12 +123,16 @@ #define GRUB_KERNEL_I386_COREBOOT_DATA_END 0x42 #define GRUB_KERNEL_I386_COREBOOT_LINK_ADDR 0x8200 +#define GRUB_KERNEL_I386_MULTIBOOT_PREFIX GRUB_KERNEL_I386_COREBOOT_PREFIX +#define GRUB_KERNEL_I386_MULTIBOOT_DATA_END GRUB_KERNEL_I386_COREBOOT_DATA_END + #define GRUB_KERNEL_I386_IEEE1275_PREFIX 0x2 #define GRUB_KERNEL_I386_IEEE1275_DATA_END 0x42 #define GRUB_KERNEL_I386_IEEE1275_LINK_ADDR 0x10000 #define GRUB_KERNEL_I386_IEEE1275_MOD_ALIGN 0x1000 #define GRUB_KERNEL_I386_COREBOOT_MOD_ALIGN 0x1 +#define GRUB_KERNEL_I386_MULTIBOOT_MOD_ALIGN GRUB_KERNEL_I386_COREBOOT_MOD_ALIGN /* Non-zero value is only needed for PowerMacs. */ #define GRUB_KERNEL_I386_IEEE1275_MOD_GAP 0x0 diff --git a/util/grub-mkimage.c b/util/grub-mkimage.c index c46f0700f..38c530b91 100644 --- a/util/grub-mkimage.c +++ b/util/grub-mkimage.c @@ -106,8 +106,8 @@ struct image_target_desc image_targets[] = .bigendian = 0, .id = IMAGE_COREBOOT, .flags = PLATFORM_FLAGS_NONE, - .prefix = GRUB_KERNEL_I386_COREBOOT_PREFIX, - .data_end = GRUB_KERNEL_I386_COREBOOT_DATA_END, + .prefix = GRUB_KERNEL_I386_MULTIBOOT_PREFIX, + .data_end = GRUB_KERNEL_I386_MULTIBOOT_DATA_END, .raw_size = 0, .total_module_size = TARGET_NO_FIELD, .kernel_image_size = TARGET_NO_FIELD, From 6f8157cb8974d36a9e505549d2ec740b4eab0207 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 29 Aug 2010 00:53:09 +0200 Subject: [PATCH 167/178] Fix qemu compilation --- grub-core/Makefile.core.def | 2 ++ 1 file changed, 2 insertions(+) diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index 0917e749c..709fd6cd7 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -777,6 +777,7 @@ module = { i386_coreboot = efiemu/i386/pc/cfgtables.c; i386_multiboot = efiemu/i386/pc/cfgtables.c; i386_ieee1275 = efiemu/i386/nocfgtables.c; + i386_qemu = efiemu/i386/nocfgtables.c; common = efiemu/mm.c; common = efiemu/loadcore_common.c; common = efiemu/symbols.c; @@ -796,6 +797,7 @@ module = { enable = i386_coreboot; enable = i386_ieee1275; enable = i386_multiboot; + enable = i386_qemu; }; module = { From f5c1e402d3ccaead39281145790eb2ce4d33de41 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 29 Aug 2010 01:26:48 +0200 Subject: [PATCH 168/178] enable grub-mkrescue on i386-multiboot --- Makefile.util.def | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile.util.def b/Makefile.util.def index fd3428e76..db3b6e5c6 100644 --- a/Makefile.util.def +++ b/Makefile.util.def @@ -334,6 +334,7 @@ script = { enable = i386_pc; enable = x86_efi; enable = i386_qemu; + enable = i386_multiboot; enable = i386_coreboot; enable = powerpc_ieee1275; }; From 5d9bdcf167596a3b55f2ac43e64f791dc3918978 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 29 Aug 2010 02:28:15 +0200 Subject: [PATCH 169/178] Fix x86_64-efi compilation error --- grub-core/lib/relocator.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/grub-core/lib/relocator.c b/grub-core/lib/relocator.c index 90b383301..53acda52f 100644 --- a/grub-core/lib/relocator.c +++ b/grub-core/lib/relocator.c @@ -586,8 +586,8 @@ malloc_in_range (struct grub_relocator *rel, continue; do { - grub_dprintf ("relocator", "free block %p+0x%x\n", - p, p->size); + grub_dprintf ("relocator", "free block %p+0x%lx\n", + p, (unsigned long) p->size); if (p->magic != GRUB_MM_FREE_MAGIC) grub_fatal (__FILE__":%d free magic broken at %p (0x%x)\n", __LINE__, p, p->magic); From 303f59958e9cb23056e8f35dc4263dc08e522d36 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 29 Aug 2010 02:29:01 +0200 Subject: [PATCH 170/178] Disable kfreebsd bootcheck on qemu and multiboot --- Makefile.am | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Makefile.am b/Makefile.am index fc3d273f2..1cf2297bd 100644 --- a/Makefile.am +++ b/Makefile.am @@ -272,12 +272,16 @@ endif if !COND_i386_coreboot # Crashes because memory at 0-0x1000 is occupied -BOOTCHECKS += bootcheck-kfreebsd-x86_64 -# Likewise. BOOTCHECKS += bootcheck-knetbsd-x86_64 +# Likewise and require ACPI. +if !COND_i386_multiboot +if !COND_i386_qemu +BOOTCHECKS += bootcheck-kfreebsd-x86_64 BOOTCHECKS += bootcheck-kfreebsd-i386 endif +endif +endif BOOTCHECKS += bootcheck-kfreebsd-aout From a30f510eac6f3ee39add695a6e3e49861988d43b Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 29 Aug 2010 02:29:14 +0200 Subject: [PATCH 171/178] newreloc documentation upgrade --- docs/grub.texi | 123 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 97 insertions(+), 26 deletions(-) diff --git a/docs/grub.texi b/docs/grub.texi index 85316fced..4c96f254f 100644 --- a/docs/grub.texi +++ b/docs/grub.texi @@ -2867,28 +2867,100 @@ commands. @chapter Supported boot targets X86 support is summarised in following table. ``Yes'' means that kernel works on the given platform, ``crashes'' means an early kernel crash which we hove will be fixed by concerned kernel developpers. ``no'' means GRUB doesn't load given kernel on a given platform. ``headless'' means that the kernel works but lacks console drivers (you can still use serial or network console). In case of ``no'' and ``crashes'' the reason is given in footnote. -@multitable @columnfractions .50 .15 .15 .15 -@item @tab BIOS @tab Coreboot @tab 32-bit EFI -@item BIOS chainloading @tab yes @tab no (1) @tab no (1) -@item NTLDR @tab yes @tab no (1) @tab no (1) -@item FreeBSD bootloader @tab yes @tab crashes (1)@tab crashes (1) -@item 32-bit kFreeBSD @tab yes @tab ? @tab headless -@item 64-bit kFreeBSD @tab yes @tab crashes (2)@tab headless -@item 32-bit kNetBSD @tab yes @tab crashes (1)@tab crashes (1) -@item 64-bit kNetBSD @tab yes @tab crashes (2)@tab yes -@item 32-bit kOpenBSD @tab yes @tab yes @tab headless -@item 64-bit kOpenBSD @tab yes @tab yes @tab headless -@item Multiboot @tab yes @tab yes @tab yes -@item Multiboot2 @tab yes @tab yes @tab yes -@item 32-bit Linux (legacy protocol) @tab yes @tab no (1) @tab no (1) -@item 64-bit Linux (legacy protocol) @tab yes @tab no (1) @tab no (1) -@item 32-bit Linux (modern protocol) @tab yes @tab yes @tab yes -@item 64-bit Linux (modern protocol) @tab yes @tab yes @tab yes -@item 32-bit XNU @tab yes @tab ? @tab yes -@item 64-bit XNU @tab yes @tab ? @tab yes (5) -@item 32-bit EFI chainloader @tab no (3) @tab no (3) @tab yes -@item 64-bit EFI chainloader @tab no (3) @tab no (3) @tab no (4) -@item Appleloader @tab no (3) @tab no (3) @tab yes +@multitable @columnfractions .50 .22 .22 +@item @tab BIOS @tab Coreboot +@item BIOS chainloading @tab yes @tab no (1) +@item NTLDR @tab yes @tab no (1) +@item FreeBSD bootloader @tab yes @tab crashes (1) +@item 32-bit kFreeBSD @tab yes @tab crashes (2,6) +@item 64-bit kFreeBSD @tab yes @tab crashes (2,6) +@item 32-bit kNetBSD @tab yes @tab crashes (1) +@item 64-bit kNetBSD @tab yes @tab crashes (2) +@item 32-bit kOpenBSD @tab yes @tab yes +@item 64-bit kOpenBSD @tab yes @tab yes +@item Multiboot @tab yes @tab yes +@item Multiboot2 @tab yes @tab yes +@item 32-bit Linux (legacy protocol) @tab yes @tab no (1) +@item 64-bit Linux (legacy protocol) @tab yes @tab no (1) +@item 32-bit Linux (modern protocol) @tab yes @tab yes +@item 64-bit Linux (modern protocol) @tab yes @tab yes +@item 32-bit XNU @tab yes @tab ? +@item 64-bit XNU @tab yes @tab ? +@item 32-bit EFI chainloader @tab no (3) @tab no (3) +@item 64-bit EFI chainloader @tab no (3) @tab no (3) +@item Appleloader @tab no (3) @tab no (3) +@end multitable + +@multitable @columnfractions .50 .22 .22 +@item @tab Multiboot @tab Qemu +@item BIOS chainloading @tab no (1) @tab no (1) +@item NTLDR @tab no (1) @tab no (1) +@item FreeBSD bootloader @tab crashes (1) @tab crashes (1) +@item 32-bit kFreeBSD @tab crashes (6) @tab crashes (6) +@item 64-bit kFreeBSD @tab crashes (6) @tab crashes (6) +@item 32-bit kNetBSD @tab crashes (1) @tab crashes (1) +@item 64-bit kNetBSD @tab yes @tab yes +@item 32-bit kOpenBSD @tab yes @tab yes +@item 64-bit kOpenBSD @tab yes @tab yes +@item Multiboot @tab yes @tab yes +@item Multiboot2 @tab yes @tab yes +@item 32-bit Linux (legacy protocol) @tab no (1) @tab no (1) +@item 64-bit Linux (legacy protocol) @tab no (1) @tab no (1) +@item 32-bit Linux (modern protocol) @tab yes @tab yes +@item 64-bit Linux (modern protocol) @tab yes @tab yes +@item 32-bit XNU @tab ? @tab ? +@item 64-bit XNU @tab ? @tab ? +@item 32-bit EFI chainloader @tab no (3) @tab no (3) +@item 64-bit EFI chainloader @tab no (3) @tab no (3) +@item Appleloader @tab no (3) @tab no (3) +@end multitable + +@multitable @columnfractions .50 .22 .22 +@item @tab 32-bit EFI @tab 64-bit EFI +@item BIOS chainloading @tab no (1) @tab no (1) +@item NTLDR @tab no (1) @tab no (1) +@item FreeBSD bootloader @tab crashes (1) @tab crashes (1) +@item 32-bit kFreeBSD @tab headless @tab headless +@item 64-bit kFreeBSD @tab headless @tab headless +@item 32-bit kNetBSD @tab crashes (1) @tab crashes (1) +@item 64-bit kNetBSD @tab yes @tab yes +@item 32-bit kOpenBSD @tab headless @tab headless +@item 64-bit kOpenBSD @tab headless @tab headless +@item Multiboot @tab yes @tab yes +@item Multiboot2 @tab yes @tab yes +@item 32-bit Linux (legacy protocol) @tab no (1) @tab no (1) +@item 64-bit Linux (legacy protocol) @tab no (1) @tab no (1) +@item 32-bit Linux (modern protocol) @tab yes @tab yes +@item 64-bit Linux (modern protocol) @tab yes @tab yes +@item 32-bit XNU @tab yes @tab yes +@item 64-bit XNU @tab yes (5) @tab yes +@item 32-bit EFI chainloader @tab yes @tab no (4) +@item 64-bit EFI chainloader @tab no (4) @tab yes +@item Appleloader @tab yes @tab yes +@end multitable + +@multitable @columnfractions .50 .22 .22 +@item @tab IEEE1275 +@item BIOS chainloading @tab no (1) +@item NTLDR @tab no (1) +@item FreeBSD bootloader @tab crashes (1) +@item 32-bit kFreeBSD @tab crashes (6) +@item 64-bit kFreeBSD @tab crashes (6) +@item 32-bit kNetBSD @tab crashes (1) +@item 64-bit kNetBSD @tab ? +@item 32-bit kOpenBSD @tab ? +@item 64-bit kOpenBSD @tab ? +@item Multiboot @tab ? +@item Multiboot2 @tab ? +@item 32-bit Linux (legacy protocol) @tab no (1) +@item 64-bit Linux (legacy protocol) @tab no (1) +@item 32-bit Linux (modern protocol) @tab ? +@item 64-bit Linux (modern protocol) @tab ? +@item 32-bit XNU @tab ? +@item 64-bit XNU @tab ? +@item 32-bit EFI chainloader @tab no (3) +@item 64-bit EFI chainloader @tab no (3) +@item Appleloader @tab no (3) @end multitable @enumerate @@ -2897,15 +2969,14 @@ X86 support is summarised in following table. ``Yes'' means that kernel works on @item EFI only @item 32-bit and 64-bit EFI have different structures and work in different CPU modes so it's not possible to chainload 32-bit bootloader on 64-bit platform and vice-versa @item Some modules may need to be disabled +@item Requires ACPI @end enumerate - -PowerPC and Sparc ports support only Linux. -MIPS port supports Linux and multiboot2. +PowerPC and Sparc ports support only Linux. MIPS port supports Linux and multiboot2. @chapter Boot tests -As you have seen in previous chapter the support matrix is pretty big and some of the configurations are only rarely used. To ensure the quality bootchecks are available for all x86 targets except EFI chainloader, Appleloader and XNU. All x86 platforms have bootcheck facility except multiboot and ieee1275. Multiboot, multiboot2, BIOS chainloader, ntldr and freebsd-bootloader boot targets are tested only with a fake kernel images. Only Linux is tested among the payloads using Linux protocols. +As you have seen in previous chapter the support matrix is pretty big and some of the configurations are only rarely used. To ensure the quality bootchecks are available for all x86 targets except EFI chainloader, Appleloader and XNU. All x86 platforms have bootcheck facility except ieee1275. Multiboot, multiboot2, BIOS chainloader, ntldr and freebsd-bootloader boot targets are tested only with a fake kernel images. Only Linux is tested among the payloads using Linux protocols. Following variables must be defined: From 6d387bafaf6149cd33845af4d68ca909e1cb43cf Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 29 Aug 2010 13:45:36 +0200 Subject: [PATCH 172/178] Fix compilation on yeeloong --- include/grub/ns8250.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/grub/ns8250.h b/include/grub/ns8250.h index 2dcd07f14..7947ba9c9 100644 --- a/include/grub/ns8250.h +++ b/include/grub/ns8250.h @@ -70,9 +70,11 @@ /* Turn on DTR, RTS, and OUT2. */ #define UART_ENABLE_OUT2 0x08 +#ifndef ASM_FILE #include grub_port_t grub_ns8250_hw_get_port (const unsigned int unit); +#endif #endif /* ! GRUB_SERIAL_MACHINE_HEADER */ From 506e4d1e7fdea605ba8fdb3ef00d905bfeac25e2 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 29 Aug 2010 15:45:21 +0200 Subject: [PATCH 173/178] Use kseg0 entry address on mips --- grub-core/loader/multiboot_elfxx.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/grub-core/loader/multiboot_elfxx.c b/grub-core/loader/multiboot_elfxx.c index 024b44747..0c29fe9c2 100644 --- a/grub-core/loader/multiboot_elfxx.c +++ b/grub-core/loader/multiboot_elfxx.c @@ -140,6 +140,14 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, void *buffer) if (i == ehdr->e_phnum) return grub_error (GRUB_ERR_BAD_OS, "entry point isn't in a segment"); +#if defined (__i386__) || defined (__x86_64__) + +#elif defined (__mips) + grub_multiboot_payload_eip |= 0x80000000; +#else +#error Please complete this +#endif + if (ehdr->e_shnum) { grub_uint8_t *shdr, *shdrptr; From cb601aad52eca95c4d1a40a5329f4786afa3ec40 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 29 Aug 2010 18:54:33 +0200 Subject: [PATCH 174/178] Fix failing make dist --- grub-core/Makefile.core.def | 1 - 1 file changed, 1 deletion(-) diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index 709fd6cd7..8533d3131 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -166,7 +166,6 @@ kernel = { emu = kern/emu/mm.c; emu = kern/emu/time.c; - extra_dist = kern/i386/loader.S; extra_dist = kern/i386/realmode.S; extra_dist = kern/i386/pc/lzma_decode.S; }; From c7fef4da8b3ef241e24aba8f6451f9491c2f10c5 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 29 Aug 2010 21:58:58 +0200 Subject: [PATCH 175/178] make tags variables statis as intended --- grub-core/loader/i386/bsd.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/grub-core/loader/i386/bsd.c b/grub-core/loader/i386/bsd.c index d6a22da5b..cfea3b6a1 100644 --- a/grub-core/loader/i386/bsd.c +++ b/grub-core/loader/i386/bsd.c @@ -81,7 +81,7 @@ struct bsd_tag } data[0]; }; -struct bsd_tag *tags, *tags_last; +static struct bsd_tag *tags, *tags_last; struct netbsd_module { @@ -89,7 +89,7 @@ struct netbsd_module struct grub_netbsd_btinfo_module mod; }; -struct netbsd_module *netbsd_mods, *netbsd_mods_last; +static struct netbsd_module *netbsd_mods, *netbsd_mods_last; static const struct grub_arg_option freebsd_opts[] = { From 0b986c402b9e98f3859997763487c3ab68b54518 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 29 Aug 2010 22:03:28 +0200 Subject: [PATCH 176/178] First part of ChangeLog --- ChangeLog | 280 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 280 insertions(+) diff --git a/ChangeLog b/ChangeLog index c2ebd6173..6162b2eca 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,283 @@ +2010-08-29 Vladimir Serbinenko + + New relocator. Allows for more kernel support and more straightforward + loader writing. + + * Makefile.am (BOOTTARGET): New variable. + (QEMU32): Likewise. + (linux.init.x86_64): New target. + (linux.init.i386): Likewise. + (multiboot.elf): Likewise. + (kfreebsd.elf): Likewise. + (kfreebsd.aout): Likewise. + (pc-chainloader.elf): Likewise. + (pc-chainloader.bin): Likewise. + (ntldr.elf): Likewise. + (ntldr.bin): Likewise. + (multiboot2.elf): Likewise. + (kfreebsd.init.x86_64): Likewise. + (kfreebsd.init.i386): Likewise. + (knetbsd.init.i386): Likewise. + (kopenbsd.init.i386): Likewise. + (knetbsd.init.x86_64): Likewise. + (kopenbsd.init.x86_64): Likewise. + (linux-initramfs.i386): Likewise. + (linux-initramfs.x86_64): Likewise. + (kfreebsd-mfsroot.i386.img): Likewise. + (knetbsd.image.i386): Likewise. + (kopenbsd.image.i386): Likewise. + (kopenbsd.image.x86_64): Likewise. + (knetbsd.miniroot-image.i386.img): Likewise. + (kfreebsd-mfsroot.x86_64.img): Likewise. + (knetbsd.image.x86_64): Likewise. + (knetbsd.miniroot-image.x86_64.img): Likewise. + (kfreebsd-mfsroot.i386.gz): Likewise. + (bootcheck-kfreebsd-i386): Likewise. + (kfreebsd-mfsroot.x86_64.gz): Likewise. + (bootcheck-kfreebsd-x86_64): Likewise. + (knetbsd.miniroot-image.i386.gz): Likewise. + (bootcheck-knetbsd-i386): Likewise. + (bootcheck-kopenbsd-i386): Likewise. + (bootcheck-kopenbsd-x86_64): Likewise. + (knetbsd.miniroot-image.x86_64.gz): Likewise. + (bootcheck-knetbsd-x86_64): Likewise. + (bootcheck-linux-i386): Likewise. + (bootcheck-linux-x86_64): Likewise. + (bootcheck-linux16-i386): Likewise. + (bootcheck-linux16-x86_64): Likewise. + (bootcheck-multiboot): Likewise. + (bootcheck-multiboot2): Likewise. + (bootcheck-kfreebsd-aout): Likewise. + (bootcheck-pc-chainloader): Likewise. + (bootcheck-ntldr): Likewise. + (CLEANFILES): Add new targets. + (BOOTCHECKS): New variable. + (.PHONY): Add bootchecks. + (SUCCESSFUL_BOOT_STRING): New variable. + (BOOTCHECK_TIMEOUT): Likewise. + (bootcheck): New target + * Makefile.util.def (grub-mkrescue): Enable on i386-multiboot. + * configure.ac: Correct efiemu excuse. + * docs/grub.texi (Supported kernels): New chapter. + * grub-core/Makefile.am (KERNEL_HEADER_FILES): Add + include/grub/mm_private.h. Simplify inclusion of + include/grub/boot.h, include/grub/loader.h + and include/grub/msdos_partition.h + (KERNEL_HEADER_FILES) [i386_coreboot]: + Remove include/grub/machine/loader.h. Add include/grub/i386/pit.h. + (KERNEL_HEADER_FILES) [i386_multiboot]: Likewise. + (KERNEL_HEADER_FILES) [i386_qemu]: Likewise. + (KERNEL_HEADER_FILES) [i386_ieee1275]: Remove + include/grub/machine/loader.h. + (KERNEL_HEADER_FILES) [x86_64-efi]: Likewise. + * grub-core/Makefile.core.def (kernel): Remove kern/i386/loader.S from + extra_dist. + (pci.mod): Enable on i386-multiboot. + (acpi.mod): Enable on i386-multiboot and i386-coreboot. + (efiemu.mod): Enable on i386-coreboot, i386-ieee1275, i386-multiboot and + i386-qemu. + (relocator.mod): Rewritten. + (aout.mod): Enable on all x86. + (bsd.mod): Likewise. + (ntldr.mod): New module. + (linux.mod): Use loader/i386/linux.c on all x86. + (xnu.mod): Enable on all x86. + (vga_text.mod): disable on EFI and QEMU. + * grub-core/efiemu/i386/coredetect.c: Remove useless include. + * grub-core/efiemu/i386/pc/cfgtables.c: Likewise. + * grub-core/efiemu/loadcore.c: Likewise. + * grub-core/efiemu/main.c: Likewise. + (grub_efiemu_exit_boot_services): Removed. + (grub_efiemu_finish_boot_services): Likewise. + * grub-core/efiemu/mm.c (grub_efiemu_finish_boot_services): New + function. + * grub-core/efiemu/i386/nocfgtables.c: New file. + * grub-core/kern/dl.c (grub_dl_unload_all): Removed. + * grub-core/kern/efi/efi.c (grub_efi_exit_boot_services): Removed. + (grub_efi_finish_boot_services): Moved from here ... + * grub-core/kern/efi/mm.c (grub_efi_finish_boot_services): ...here. + Fille finish memory map and related data. + (finish_mmap_buf): New variable. + (grub_efi_uintn_t finish_mmap_size): Likewise. + (grub_efi_uintn_t finish_key): Likewise. + (grub_efi_uintn_t finish_desc_size): Likewise. + (grub_efi_uint32_t finish_desc_version): Likewise. + (grub_efi_is_finished): Likewise. + (grub_efi_get_memory_map): Use saved memory map if EFI is already + finished. + * grub-core/kern/elf.c (grub_elf32_phdr_iterate): Make global. + (grub_elf64_phdr_iterate): Likewise. + * grub-core/kern/i386/coreboot/init.c (grub_os_area_addr): Removed. + (grub_os_area_size): Likewise. + (grub_machine_init): Don't reserve os area. + * grub-core/kern/i386/coreboot/startup.S: Don't include loader.S. + * grub-core/kern/i386/ieee1275/startup.S: Likewise. + * grub-core/kern/i386/loader.S: Removed. + * grub-core/kern/i386/pc/init.c (grub_os_area_addr): Removed. + (grub_os_area_size): Likewise. + (grub_machine_init): Don't reserve os area. + * grub-core/kern/i386/pc/startup.S (grub_chainloader_real_boot): + Don't call grub_dl_unload_all. + Don't include loader.S. + * grub-core/kern/i386/qemu/mmap.c (grub_machine_mmap_iterate): + Declare the memory after _end as available. + * grub-core/kern/mm.c (GRUB_MM_FREE_MAGIC): Moved from here... + (GRUB_MM_ALLOC_MAGIC): Moved from here... + (grub_mm_header) + (GRUB_MM_ALIGN) + * grub-core/kern/mm.c (grub_mm_region): Moved from here ... + (grub_mm_region): ..here. Removed addr. Added pre_size. + All users updated. + * grub-core/kern/mm.c (base): Renamed to ... + (grub_mm_base): ... this. Made global. + (grub_real_malloc): Alloc from end of region. + (grub_memalign): Don't attempt to malloc if grub_mm_base is NULL. + * grub-core/kern/powerpc/cache.S (grub_arch_sync_caches): Move to ... + * grub-core/kern/powerpc/cache_flush.S: ... here. + * grub-core/lib/efi/relocator.c: New file. + * grub-core/lib/i386/relocator.c: Rewritten. + * grub-core/lib/i386/relocator16.S: New file. + * grub-core/lib/i386/relocator32.S: Likewise. + * grub-core/lib/i386/relocator64.S: Likewise. + * grub-core/lib/i386/relocator_asm.S: Rewritten. + * grub-core/lib/i386/relocator_common.S: New file. + * grub-core/lib/ieee1275/relocator.c: Likewise. + * grub-core/lib/mips/relocator.c: Rewritten. + * grub-core/lib/mips/relocator_asm.S: Renamed variables and minor + stylistic adjustments. + * grub-core/lib/powerpc/relocator.c: New file. + * grub-core/lib/powerpc/relocator_asm.S: Likewise. + * grub-core/lib/relocator.c: Rewritten. + * grub-core/lib/x86_64/relocator_asm.S: New file. + * grub-core/loader/aout.c (grub_aout_load): Make load_addr a void *. + * grub-core/loader/i386/bsd.c (NETBSD_DEFAULT_VIDEO_MODE): New const. + (bsd_tag): New struct. + (tags): New variable. + (tags_last): Likewise. + (netbsd_module): New struct. + (netbsd_mods): New variable. + (netbsd_mods_last): Likewise. + (openbsd_opts): New parameter "serial". + (OPENBSD_SERIAL_ARG): New definition. + (netbsd_opts): New parameter "serial". + (NETBSD_SERIAL_ARG): New definition. + (grub_freebsd_add_meta): Reorganised into ... + (grub_bsd_add_meta): ...this. All users updated. + (grub_freebsd_add_mmap): Reorganised into ... + (generate_e820_mmap): ...this... + (grub_bsd_add_mmap): ...and this. All users updated. + (grub_freebsd_list_modules): Use tags. + (grub_netbsd_add_meta_module): New function. + (grub_netbsd_list_modules): Likewise. + (grub_freebsd_boot): Use relocator and finish EFI. + (grub_openbsd_boot): Likewise. + (grub_netbsd_setup_video): New function. + (grub_netbsd_add_modules): Likewise. + (grub_netbsd_boot): Use grub_netbsd_add_modules, relocator, netbsd_tags + and finish EFI. + (grub_bsd_unload): Unload tags. + (grub_bsd_load_aout): Use relocator. + (grub_bsd_elf32_size_hook): New function. + (grub_bsd_elf32_hook): Use relocator. + (grub_bsd_elf64_size_hook): New function. + (grub_bsd_elf64_hook): Use relocator. + (grub_bsd_load_elf): Use relocator and call grub_openbsd_find_ramdisk. + (grub_bsd_load): Zero-out openbsd_ramdisk. + (grub_bsd_load): Use relocator. + (grub_cmd_openbsd): Support serial. + (grub_cmd_netbsd): Support modules. + (grub_cmd_freebsd_module): Use relocator. + (grub_netbsd_module_load): New function. + (grub_cmd_netbsd_module): Likewise. + (grub_cmd_openbsd_ramdisk): Likewise. + (GRUB_MOD_INIT): Register knetbsd_module, knetbsd_module_elf and + kopenbsd_ramdisk. + (GRUB_MOD_FINI): Unregister new commands. + * grub-core/loader/i386/bsdXX.c (load): Remove useless checks. + (grub_freebsd_load_elfmodule_obj): Use relocator. + (grub_freebsd_load_elfmodule): Likewise. + (grub_freebsd_load_elf_meta): Likewise. + (grub_netbsd_load_elf_meta): New function. + (grub_openbsd_find_ramdisk): Likewise. + * grub-core/loader/i386/bsd_helper.S: Removed. + * grub-core/loader/i386/bsd_pagetable.c: Support relocator. + * grub-core/loader/i386/bsd_trampoline.S: Removed. + * grub-core/loader/i386/efi/linux.c: Likewise. + * grub-core/loader/i386/ieee1275/linux.c: Likewise. + * grub-core/loader/i386/linux.c (HAS_VGA_TEXT): New const. + (DEFAULT_VIDEO_MODE): Likewise. + (real_mode_target): New variable. + (prot_mode_target): Likewise. + (initrd_mem_target): Likewise. + (relocator): Likewise. + (efi_mmap_buf): Likewise. + (efi_mmap_size): Likewise. + (find_efi_mmap_size): Moved from grub-core/loader/i386/efi/linux.c. + (free_pages): Use relocator. + (allocate_pages): Account for efi_mmap and use relocator. Return error. + (grub_linux_setup_video): Return error. + (grub_linux_trampoline_start): Removed. + (grub_linux_trampoline_end): Likewise. + (grub_linux_boot): Use relocator and DEFAULT_VIDEO_MODE. Pass console + andd video parameters depending on firmware. + [GRUB_MACHINE_IEEE1275]: Pass OFW parameters. + [GRUB_MACHINE_EFI]: Pass EFI parameters. + (grub_cmd_linux) [GRUB_MACHINE_EFI]: Likewise. + (grub_cmd_initrd): Use relocator. + * grub-core/loader/i386/linux_trampoline.S: Removed. + * grub-core/loader/i386/multiboot_mbi.c (elf_sec_num): New variable. + (elf_sec_entsize): Likewise. + (elf_sec_shstrndx): Likewise. + (elf_sections): Likewise. + (grub_multiboot_load): Use relocator. + (grub_multiboot_get_mbi_size): Account for sections. + (grub_multiboot_make_mbi): Use relocator and support sections. + (grub_multiboot_add_elfsyms): New function. + (grub_multiboot_free_mbi): Free sections. + * grub-core/loader/i386/pc/linux.c (relocator): New variable. + (grub_linux_real_target): Likewise. + (grub_linux_real_chunk): Likewise. + (grub_linux16_prot_size): Likewise. + (grub_linux16_boot): Use relocator. + (grub_linux_unload): Unload relocator. + (grub_cmd_linux): Use relocator. + (grub_cmd_initrd): Likewise. + * grub-core/loader/i386/pc/ntldr.c: New file. + * grub-core/loader/i386/xnu.c (guessfsb) [GRUB_MACHINE_IEEE1275]: + Don't try to guess CPU frequency. + (grub_xnu_set_video): Stretch bitmap. + (grub_xnu_boot): Use relocator. + * grub-core/loader/mips/linux.c (grub_linux_boot): Use relocator. + (grub_linux_unload): Free relocator. + (grub_linux_load32): Use relocator. + (grub_linux_load64): Likewise. + (grub_cmd_initrd): Likewise. + * grub-core/loader/multiboot.c (grub_multiboot_boot): Use relocator. + (grub_multiboot_unload): Unload relocator. + (grub_cmd_multiboot): Use relocator. + (grub_cmd_module): Likewise. + * grub-core/loader/multiboot_elfxx.c (grub_multiboot_load_elfXX): + Use relocator and support sections. + * grub-core/loader/multiboot_mbi2.c(elf_sec_num): New variable. + (elf_sec_entsize): Likewise. + (elf_sec_shstrndx): Likewise. + (elf_sections): Likewise. + (grub_multiboot_load): Use relocator. + (grub_multiboot_get_mbi_size): Account for sections. + (grub_multiboot_make_mbi): Use relocator and support sections. + (grub_multiboot_add_elfsyms): New function. + * grub-core/loader/powerpc/ieee1275/linux.c: Remove useless include. + * grub-core/loader/sparc64/ieee1275/linux.c: Likewise. + * grub-core/loader/xnu.c (grub_xnu_heap_malloc): Use relocator. + Prototype changed. All users updated. + (grub_xnu_align_heap): Simplified. + (grub_xnu_writetree_toheap): Likewise. + (grub_xnu_unload): Unload relocator. + (grub_cmd_xnu_kernel): Use relocator. + (grub_cmd_xnu_kernel64): Likewise. + (grub_xnu_register_memory): Simplified. + * grub-core/loader/xnu_resume.c (grub_xnu_resume): Use relocator. + 2010-08-29 Vladimir Serbinenko * grub-core/Makefile.core.def (kernel): Add kern/mips/cache_flush.S to From 7542126ac2775ab63e8a41c387978f9755d9eb36 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 29 Aug 2010 23:07:44 +0200 Subject: [PATCH 177/178] Add missing newlines --- grub-core/tests/boot/kbsd.init-x86_64.S | 2 +- grub-core/tests/boot/kfreebsd.init-x86_64.S | 2 +- grub-core/tests/boot/linux.init-i386.S | 1 - grub-core/tests/boot/linux.init-x86_64.S | 1 - 4 files changed, 2 insertions(+), 4 deletions(-) diff --git a/grub-core/tests/boot/kbsd.init-x86_64.S b/grub-core/tests/boot/kbsd.init-x86_64.S index 58400db0d..81f810e2c 100644 --- a/grub-core/tests/boot/kbsd.init-x86_64.S +++ b/grub-core/tests/boot/kbsd.init-x86_64.S @@ -108,4 +108,4 @@ message: .ascii "Boot Test Passed Successfully\n" SUCCESSFUL_BOOT_STRING "\n" messageend: iopl_arg: - .long 3 \ No newline at end of file + .long 3 diff --git a/grub-core/tests/boot/kfreebsd.init-x86_64.S b/grub-core/tests/boot/kfreebsd.init-x86_64.S index d2907b3a8..0a9ff511e 100644 --- a/grub-core/tests/boot/kfreebsd.init-x86_64.S +++ b/grub-core/tests/boot/kfreebsd.init-x86_64.S @@ -95,4 +95,4 @@ messageend: iopl_arg: .long SHUTDOWN_PORT .long 1 - .long 1 \ No newline at end of file + .long 1 diff --git a/grub-core/tests/boot/linux.init-i386.S b/grub-core/tests/boot/linux.init-i386.S index f3eca6d88..5b0088e00 100644 --- a/grub-core/tests/boot/linux.init-i386.S +++ b/grub-core/tests/boot/linux.init-i386.S @@ -77,4 +77,3 @@ start: message: .ascii "Boot Test Passed Successfully\n" SUCCESSFUL_BOOT_STRING "\n" messageend: - \ No newline at end of file diff --git a/grub-core/tests/boot/linux.init-x86_64.S b/grub-core/tests/boot/linux.init-x86_64.S index 63dca5e20..fc32dfd91 100644 --- a/grub-core/tests/boot/linux.init-x86_64.S +++ b/grub-core/tests/boot/linux.init-x86_64.S @@ -76,4 +76,3 @@ start: message: .ascii "Boot Test Passed Successfully\n" SUCCESSFUL_BOOT_STRING "\n" messageend: - \ No newline at end of file From 1935c0773e0abe1d7878f1d361eff87a9c71aabc Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 29 Aug 2010 23:08:06 +0200 Subject: [PATCH 178/178] Finish Changelog --- ChangeLog | 145 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 143 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index ef81d87e9..4d5d6653a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -122,9 +122,13 @@ * grub-core/kern/i386/qemu/mmap.c (grub_machine_mmap_iterate): Declare the memory after _end as available. * grub-core/kern/mm.c (GRUB_MM_FREE_MAGIC): Moved from here... + * include/grub/mm_private.h (GRUB_MM_FREE_MAGIC): ... here. (GRUB_MM_ALLOC_MAGIC): Moved from here... - (grub_mm_header) - (GRUB_MM_ALIGN) + * include/grub/mm_private.h (GRUB_MM_ALLOC_MAGIC): ... here. + * grub-core/kern/mm.c (grub_mm_header): Moved from here... + * include/grub/mm_private.h (grub_mm_header): ... here. + * grub-core/kern/mm.c (GRUB_MM_ALIGN): Moved from here... + * include/grub/mm_private.h (GRUB_MM_ALIGN): ... here. * grub-core/kern/mm.c (grub_mm_region): Moved from here ... (grub_mm_region): ..here. Removed addr. Added pre_size. All users updated. @@ -277,6 +281,143 @@ (grub_cmd_xnu_kernel64): Likewise. (grub_xnu_register_memory): Simplified. * grub-core/loader/xnu_resume.c (grub_xnu_resume): Use relocator. + * grub-core/term/efi/console.c (grub_console_putchar): Abort if + EFI is finished. + (grub_console_checkkey): Likewise. + (grub_console_getkey): Likewise. + (grub_console_getwh): Likewise. + (grub_console_getxy): Likewise. + (grub_console_gotoxy): Likewise. + (grub_console_cls): Likewise. + (grub_console_setcolorstate): Likewise. + (grub_console_setcursor): Likewise. + * grub-core/term/ns8250.c (grub_ns8250_hw_get_port): New function. + * grub-core/tests/boot/kbsd.init-i386.S: New file. + * grub-core/tests/boot/kbsd.init-x86_64.S: Likewise. + * grub-core/tests/boot/kbsd.spec.txt: Likewise. + * grub-core/tests/boot/kernel-8086.S: Likewise. + * grub-core/tests/boot/kernel-i386.S: Likewise. + * grub-core/tests/boot/kfreebsd-aout.cfg: Likewise. + * grub-core/tests/boot/kfreebsd.cfg: Likewise. + * grub-core/tests/boot/kfreebsd.init-i386.S: Likewise. + * grub-core/tests/boot/kfreebsd.init-x86_64.S: Likewise. + * grub-core/tests/boot/knetbsd.cfg: Likewise. + * grub-core/tests/boot/kopenbsd.cfg: Likewise. + * grub-core/tests/boot/kopenbsdlabel.txt: Likewise. + * grub-core/tests/boot/linux.cfg: Likewise. + * grub-core/tests/boot/linux.init-i386.S: Likewise. + * grub-core/tests/boot/linux.init-x86_64.S: Likewise. + * grub-core/tests/boot/linux16.cfg: Likewise. + * grub-core/tests/boot/multiboot.cfg: Likewise. + * grub-core/tests/boot/multiboot2.cfg: Likewise. + * grub-core/tests/boot/ntldr.cfg: Likewise. + * grub-core/tests/boot/pc-chainloader.cfg: Likewise. + * include/grub/aout.h (grub_aout_load): Make load_addr a void *. + * include/grub/autoefi.h (grub_autoefi_finish_boot_services): + New definition. + * include/grub/dl.h (grub_dl_unload_all): Removed. + * include/grub/efi/efi.h (grub_efi_exit_boot_services): Likewise. + (grub_efi_finish_boot_services): Change prototype. + (grub_efi_is_finished): New variable. + * include/grub/efiemu/efiemu.h (grub_efiemu_finish_boot_services): + Changed prototype. + (grub_efiemu_finish_boot_services): Removed. + (grub_machine_efiemu_init_tables): New prototype. + * include/grub/elfload.h (grub_elf32_phdr_iterate): Likewise. + (grub_elf64_phdr_iterate): Likewise. + * include/grub/i386/bsd.h: Include relocator.h. + (freebsd_tag_header): New struct. + (grub_openbsd_bios_mmap): Removed. + (grub_unix_real_boot): Removed. + (grub_freebsd_load_elfmodule32): Changed prototype. + (grub_freebsd_load_elfmodule_obj64): Likewise. + (grub_freebsd_load_elf_meta32): Likewise. + (grub_freebsd_load_elf_meta64): Likewise. + (grub_freebsd_add_meta): Removed. + (grub_netbsd_load_elf_meta32): New prototype. + (grub_netbsd_load_elf_meta64): Likewise. + (grub_bsd_add_meta): Likewise. + (grub_openbsd_ramdisk_descriptor): New struct. + (grub_openbsd_find_ramdisk32): New prototype. + (grub_openbsd_find_ramdisk64): Likewise. + * include/grub/i386/coreboot/loader.h: Removed. + * include/grub/i386/efi/loader.h: Likewise. + * include/grub/i386/ieee1275/loader.h: Likewise. + * include/grub/i386/linux.h (linux_kernel_header): Change void * + to grub_uint32_t. + * include/grub/i386/loader.h: Removed. + * include/grub/i386/memory.h (GRUB_MEMORY_CPU_CR4_PAE_ON): Correct the + value. + (GRUB_MEMORY_CPU_CR4_PSE_ON): New definition. + (grub_phys_addr_t): New type. + (grub_vtop): New inline function. + (grub_map_memory): Likewise. + (grub_unmap_memory): Likewise. + * include/grub/i386/multiboot/loader.h: Removed. + * include/grub/i386/netbsd_bootinfo.h (NETBSD_BTINFO_BOOTDISK): Removed. + (NETBSD_BTINFO_CONSOLE): New definition. + (NETBSD_BTINFO_SYMTAB): Likewise. + (NETBSD_BTINFO_MODULES): Likewise. + (NETBSD_BTINFO_FRAMEBUF): Likewise. + (grub_netbsd_bootinfo): New struct. + (grub_netbsd_btinfo_common): Use explicit bitsize. + (grub_netbsd_btinfo_mmap_entry): Removed. + (GRUB_NETBSD_MAX_BOOTPATH_LEN): New definition. + (grub_netbsd_btinfo_bootdisk): New struct. + (grub_netbsd_btinfo_symtab): Likewise. + (grub_netbsd_btinfo_serial): Likewise. + (grub_netbsd_btinfo_modules): Likewise. + (grub_netbsd_btinfo_framebuf): Likewise. + (GRUB_NETBSD_MAX_ROOTDEVICE_LEN): New definition. + * include/grub/i386/openbsd_bootarg.h (OPENBSD_BOOTARG_CONSOLE): + Likewise. + (grub_openbsd_bootargs): Use explicit bitsize. + (grub_openbsd_bootarg_console): New struct. + (GRUB_OPENBSD_COM_MAJOR): New definition. + (GRUB_OPENBSD_VGA_MAJOR): Likewise. + * include/grub/i386/pc/efiemu.h: Removed. + * include/grub/i386/pc/loader.h: Don't include cpu/loader.h. + * include/grub/i386/qemu/loader.h: Removed. + * include/grub/i386/relocator.h: Rewritten. + * include/grub/i386/xnu.h (grub_xnu_heap_will_be_at): Removed. + * include/grub/mips/memory.h: New file. + * include/grub/mips/multiboot.h: Rewritten. + * include/grub/mips/relocator.h: Rewritten. + * include/grub/mips/yeeloong/memory.h (grub_phys_addr_t): New type. + (grub_vtop): New function. + (grub_map_memory): Likewise. + (grub_unmap_memory): Likewise. + * include/grub/misc.h (ALIGN_DOWN): New definition. + * include/grub/mm.h (grub_mm_check_real): New proto. + (GRUB_MM_CHECK): New definition. + * include/grub/mm_private.h: New file. + * include/grub/multiboot.h (grub_multiboot_relocator): New variable. + (grub_multiboot_get_mbi_size): Removed. + (grub_multiboot_make_mbi): Change prottype. + (grub_multiboot_set_accepts_video): New proto. + (grub_multiboot_add_elfsyms): Likewise. + (grub_multiboot_payload_eip): New variable. + * include/grub/ns8250.h (grub_ns8250_hw_get_port) [!ASM_FILE]: + New prototype. + * include/grub/offsets.h (GRUB_KERNEL_I386_MULTIBOOT_PREFIX): + New definition. + (GRUB_KERNEL_I386_MULTIBOOT_DATA_END): Likewise. + (GRUB_KERNEL_I386_MULTIBOOT_MOD_ALIGN): Likewise. + * include/grub/powerpc/ieee1275/loader.h: Removed. + * include/grub/powerpc/memory.h: New file. + * include/grub/powerpc/relocator.h: Likewise. + * include/grub/relocator.h: Likewise. + * include/grub/relocator_private.h: Likewise. + * include/grub/sparc64/ieee1275/loader.h: Removed. + * include/grub/x86_64/memory.h: New file. + * include/grub/xnu.h (grub_xnu_writetree_toheap): Changed prototype. + (grub_xnu_heap_malloc): Likewise. + (grub_xnu_heap_real_start): Removed. + (grub_xnu_heap_start): Likewise. + (grub_xnu_relocator): New variable. + (grub_xnu_heap_target_start): Likewise. + * tests/util/grub-shell.in: Support non-pc. + * util/grub-mkimage.c (image_targets): Fix multiboot target. 2010-08-29 Vladimir Serbinenko