summaryrefslogtreecommitdiffstats
path: root/grub-core/kern
diff options
context:
space:
mode:
Diffstat (limited to 'grub-core/kern')
-rw-r--r--grub-core/kern/acpi.c119
-rw-r--r--grub-core/kern/arm/cache.S123
-rw-r--r--grub-core/kern/arm/cache.c311
-rw-r--r--grub-core/kern/arm/cache_armv6.S72
-rw-r--r--grub-core/kern/arm/cache_armv7.S138
-rw-r--r--grub-core/kern/arm/compiler-rt.S86
-rw-r--r--grub-core/kern/arm/coreboot/cbtable.c40
-rw-r--r--grub-core/kern/arm/coreboot/coreboot.S44
-rw-r--r--grub-core/kern/arm/coreboot/dma.c59
-rw-r--r--grub-core/kern/arm/coreboot/init.c151
-rw-r--r--grub-core/kern/arm/coreboot/timer.c101
-rw-r--r--grub-core/kern/arm/dl.c280
-rw-r--r--grub-core/kern/arm/dl_helper.c245
-rw-r--r--grub-core/kern/arm/efi/init.c77
-rw-r--r--grub-core/kern/arm/efi/startup.S36
-rw-r--r--grub-core/kern/arm/startup.S177
-rw-r--r--grub-core/kern/arm/uboot/init.c70
-rw-r--r--grub-core/kern/arm/uboot/uboot.S73
-rw-r--r--grub-core/kern/arm64/cache.c63
-rw-r--r--grub-core/kern/arm64/cache_flush.S55
-rw-r--r--grub-core/kern/arm64/dl.c198
-rw-r--r--grub-core/kern/arm64/dl_helper.c134
-rw-r--r--grub-core/kern/arm64/efi/init.c63
-rw-r--r--grub-core/kern/arm64/efi/startup.S39
-rw-r--r--grub-core/kern/buffer.c117
-rw-r--r--grub-core/kern/command.c111
-rw-r--r--grub-core/kern/compiler-rt.c464
-rw-r--r--grub-core/kern/coreboot/cbtable.c72
-rw-r--r--grub-core/kern/coreboot/mmap.c100
-rw-r--r--grub-core/kern/corecmd.c189
-rw-r--r--grub-core/kern/device.c191
-rw-r--r--grub-core/kern/disk.c544
-rw-r--r--grub-core/kern/disk_common.c66
-rw-r--r--grub-core/kern/dl.c825
-rw-r--r--grub-core/kern/efi/acpi.c59
-rw-r--r--grub-core/kern/efi/efi.c1017
-rw-r--r--grub-core/kern/efi/fdt.c43
-rw-r--r--grub-core/kern/efi/init.c149
-rw-r--r--grub-core/kern/efi/mm.c691
-rw-r--r--grub-core/kern/efi/sb.c188
-rw-r--r--grub-core/kern/elf.c212
-rw-r--r--grub-core/kern/elfXX.c207
-rw-r--r--grub-core/kern/emu/argp_common.c41
-rw-r--r--grub-core/kern/emu/cache.c35
-rw-r--r--grub-core/kern/emu/cache_s.S15
-rw-r--r--grub-core/kern/emu/full.c69
-rw-r--r--grub-core/kern/emu/hostdisk.c686
-rw-r--r--grub-core/kern/emu/hostfs.c200
-rw-r--r--grub-core/kern/emu/lite.c47
-rw-r--r--grub-core/kern/emu/main.c286
-rw-r--r--grub-core/kern/emu/misc.c216
-rw-r--r--grub-core/kern/emu/mm.c75
-rw-r--r--grub-core/kern/emu/time.c46
-rw-r--r--grub-core/kern/env.c238
-rw-r--r--grub-core/kern/err.c122
-rw-r--r--grub-core/kern/file.c218
-rw-r--r--grub-core/kern/fs.c253
-rw-r--r--grub-core/kern/generic/millisleep.c39
-rw-r--r--grub-core/kern/generic/rtc_get_time_ms.c38
-rw-r--r--grub-core/kern/i386/coreboot/cbtable.c44
-rw-r--r--grub-core/kern/i386/coreboot/init.c143
-rw-r--r--grub-core/kern/i386/coreboot/startup.S62
-rw-r--r--grub-core/kern/i386/dl.c81
-rw-r--r--grub-core/kern/i386/efi/init.c48
-rw-r--r--grub-core/kern/i386/efi/startup.S36
-rw-r--r--grub-core/kern/i386/efi/tsc.c40
-rw-r--r--grub-core/kern/i386/ieee1275/startup.S40
-rw-r--r--grub-core/kern/i386/int.S134
-rw-r--r--grub-core/kern/i386/multiboot_mmap.c73
-rw-r--r--grub-core/kern/i386/pc/acpi.c83
-rw-r--r--grub-core/kern/i386/pc/init.c271
-rw-r--r--grub-core/kern/i386/pc/mmap.c193
-rw-r--r--grub-core/kern/i386/pc/startup.S217
-rw-r--r--grub-core/kern/i386/qemu/init.c276
-rw-r--r--grub-core/kern/i386/qemu/mmap.c107
-rw-r--r--grub-core/kern/i386/qemu/startup.S75
-rw-r--r--grub-core/kern/i386/realmode.S281
-rw-r--r--grub-core/kern/i386/tsc.c78
-rw-r--r--grub-core/kern/i386/tsc_pit.c84
-rw-r--r--grub-core/kern/i386/tsc_pmtimer.c88
-rw-r--r--grub-core/kern/i386/xen/hypercall.S43
-rw-r--r--grub-core/kern/i386/xen/pvh.c369
-rw-r--r--grub-core/kern/i386/xen/startup.S38
-rw-r--r--grub-core/kern/i386/xen/startup_pvh.S81
-rw-r--r--grub-core/kern/i386/xen/tsc.c40
-rw-r--r--grub-core/kern/ia64/cache.c35
-rw-r--r--grub-core/kern/ia64/dl.c150
-rw-r--r--grub-core/kern/ia64/dl_helper.c241
-rw-r--r--grub-core/kern/ia64/efi/init.c80
-rw-r--r--grub-core/kern/ia64/efi/startup.S44
-rw-r--r--grub-core/kern/ieee1275/cmain.c220
-rw-r--r--grub-core/kern/ieee1275/ieee1275.c809
-rw-r--r--grub-core/kern/ieee1275/init.c322
-rw-r--r--grub-core/kern/ieee1275/mmap.c83
-rw-r--r--grub-core/kern/ieee1275/openfw.c593
-rw-r--r--grub-core/kern/list.c55
-rw-r--r--grub-core/kern/lockdown.c84
-rw-r--r--grub-core/kern/main.c316
-rw-r--r--grub-core/kern/mips/arc/init.c463
-rw-r--r--grub-core/kern/mips/cache.S70
-rw-r--r--grub-core/kern/mips/cache_flush.S54
-rw-r--r--grub-core/kern/mips/dl.c274
-rw-r--r--grub-core/kern/mips/init.c38
-rw-r--r--grub-core/kern/mips/loongson/init.c320
-rw-r--r--grub-core/kern/mips/qemu_mips/init.c105
-rw-r--r--grub-core/kern/mips/startup.S126
-rw-r--r--grub-core/kern/misc.c1267
-rw-r--r--grub-core/kern/mm.c664
-rw-r--r--grub-core/kern/parser.c342
-rw-r--r--grub-core/kern/partition.c279
-rw-r--r--grub-core/kern/powerpc/cache.S26
-rw-r--r--grub-core/kern/powerpc/cache_flush.S43
-rw-r--r--grub-core/kern/powerpc/compiler-rt.S130
-rw-r--r--grub-core/kern/powerpc/dl.c169
-rw-r--r--grub-core/kern/powerpc/ieee1275/startup.S67
-rw-r--r--grub-core/kern/rescue_parser.c84
-rw-r--r--grub-core/kern/rescue_reader.c98
-rw-r--r--grub-core/kern/riscv/cache.c63
-rw-r--r--grub-core/kern/riscv/cache_flush.S44
-rw-r--r--grub-core/kern/riscv/dl.c345
-rw-r--r--grub-core/kern/riscv/efi/init.c79
-rw-r--r--grub-core/kern/riscv/efi/startup.S48
-rw-r--r--grub-core/kern/sparc64/cache.S41
-rw-r--r--grub-core/kern/sparc64/dl.c191
-rw-r--r--grub-core/kern/sparc64/ieee1275/crt0.S104
-rw-r--r--grub-core/kern/sparc64/ieee1275/ieee1275.c147
-rw-r--r--grub-core/kern/term.c169
-rw-r--r--grub-core/kern/time.c37
-rw-r--r--grub-core/kern/uboot/hw.c112
-rw-r--r--grub-core/kern/uboot/init.c172
-rw-r--r--grub-core/kern/uboot/uboot.c307
-rw-r--r--grub-core/kern/verifiers.c228
-rw-r--r--grub-core/kern/vga_init.c128
-rw-r--r--grub-core/kern/x86_64/dl.c121
-rw-r--r--grub-core/kern/x86_64/efi/callwrap.S129
-rw-r--r--grub-core/kern/x86_64/efi/startup.S35
-rw-r--r--grub-core/kern/x86_64/xen/hypercall.S53
-rw-r--r--grub-core/kern/x86_64/xen/startup.S39
-rw-r--r--grub-core/kern/xen/init.c601
139 files changed, 24492 insertions, 0 deletions
diff --git a/grub-core/kern/acpi.c b/grub-core/kern/acpi.c
new file mode 100644
index 0000000..5746ac0
--- /dev/null
+++ b/grub-core/kern/acpi.c
@@ -0,0 +1,119 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2012 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/types.h>
+#include <grub/time.h>
+#include <grub/misc.h>
+#include <grub/acpi.h>
+
+/* Simple checksum by summing all bytes. Used by ACPI and SMBIOS. */
+grub_uint8_t
+grub_byte_checksum (void *base, grub_size_t size)
+{
+ grub_uint8_t *ptr;
+ grub_uint8_t ret = 0;
+ for (ptr = (grub_uint8_t *) base; ptr < ((grub_uint8_t *) base) + size;
+ ptr++)
+ ret += *ptr;
+ return ret;
+}
+
+static void *
+grub_acpi_rsdt_find_table (struct grub_acpi_table_header *rsdt, const char *sig)
+{
+ grub_size_t s;
+ grub_unaligned_uint32_t *ptr;
+
+ if (!rsdt)
+ return 0;
+
+ if (grub_memcmp (rsdt->signature, "RSDT", 4) != 0)
+ return 0;
+
+ ptr = (grub_unaligned_uint32_t *) (rsdt + 1);
+ s = (rsdt->length - sizeof (*rsdt)) / sizeof (grub_uint32_t);
+ for (; s; s--, ptr++)
+ {
+ struct grub_acpi_table_header *tbl;
+ tbl = (struct grub_acpi_table_header *) (grub_addr_t) ptr->val;
+ if (grub_memcmp (tbl->signature, sig, 4) == 0)
+ return tbl;
+ }
+ return 0;
+}
+
+static void *
+grub_acpi_xsdt_find_table (struct grub_acpi_table_header *xsdt, const char *sig)
+{
+ grub_size_t s;
+ grub_unaligned_uint64_t *ptr;
+
+ if (!xsdt)
+ return 0;
+
+ if (grub_memcmp (xsdt->signature, "XSDT", 4) != 0)
+ return 0;
+
+ ptr = (grub_unaligned_uint64_t *) (xsdt + 1);
+ s = (xsdt->length - sizeof (*xsdt)) / sizeof (grub_uint32_t);
+ for (; s; s--, ptr++)
+ {
+ struct grub_acpi_table_header *tbl;
+#if GRUB_CPU_SIZEOF_VOID_P != 8
+ if (ptr->val >> 32)
+ continue;
+#endif
+ tbl = (struct grub_acpi_table_header *) (grub_addr_t) ptr->val;
+ if (grub_memcmp (tbl->signature, sig, 4) == 0)
+ return tbl;
+ }
+ return 0;
+}
+
+struct grub_acpi_fadt *
+grub_acpi_find_fadt (void)
+{
+ struct grub_acpi_fadt *fadt = 0;
+ struct grub_acpi_rsdp_v10 *rsdpv1;
+ struct grub_acpi_rsdp_v20 *rsdpv2;
+ rsdpv1 = grub_machine_acpi_get_rsdpv1 ();
+ if (rsdpv1)
+ fadt = grub_acpi_rsdt_find_table ((struct grub_acpi_table_header *)
+ (grub_addr_t) rsdpv1->rsdt_addr,
+ GRUB_ACPI_FADT_SIGNATURE);
+ if (fadt)
+ return fadt;
+ rsdpv2 = grub_machine_acpi_get_rsdpv2 ();
+ if (rsdpv2)
+ fadt = grub_acpi_rsdt_find_table ((struct grub_acpi_table_header *)
+ (grub_addr_t) rsdpv2->rsdpv1.rsdt_addr,
+ GRUB_ACPI_FADT_SIGNATURE);
+ if (fadt)
+ return fadt;
+ if (rsdpv2
+#if GRUB_CPU_SIZEOF_VOID_P != 8
+ && !(rsdpv2->xsdt_addr >> 32)
+#endif
+ )
+ fadt = grub_acpi_xsdt_find_table ((struct grub_acpi_table_header *)
+ (grub_addr_t) rsdpv2->xsdt_addr,
+ GRUB_ACPI_FADT_SIGNATURE);
+ if (fadt)
+ return fadt;
+ return 0;
+}
diff --git a/grub-core/kern/arm/cache.S b/grub-core/kern/arm/cache.S
new file mode 100644
index 0000000..354a069
--- /dev/null
+++ b/grub-core/kern/arm/cache.S
@@ -0,0 +1,123 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2013 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/symbol.h>
+
+ .file "cache.S"
+ .text
+ .syntax unified
+#if !defined (__thumb2__) || !defined (ARMV7)
+ .arm
+#else
+ .thumb
+#endif
+
+#if !defined (ARMV6) && !defined (ARMV7)
+# error Unsupported architecture version!
+#endif
+
+ .align 2
+
+/*
+ * Simple cache maintenance functions
+ */
+
+@ r0 - *beg (inclusive)
+@ r1 - *end (exclusive)
+@void grub_arm_clean_dcache_range (grub_addr_t start, grub_addr_t end, grub_addr_t dlinesz)
+#ifdef ARMV6
+FUNCTION(grub_arm_clean_dcache_range_armv6)
+#else
+FUNCTION(grub_arm_clean_dcache_range_armv7)
+#endif
+ DSB
+ @ Clean data cache for range to point-of-unification
+1: cmp r0, r1
+ bge 2f
+#ifdef ARMV6
+ mcr p15, 0, r0, c7, c10, 1 @ Clean data cache line by MVA
+#else
+ mcr p15, 0, r0, c7, c11, 1 @ DCCMVAU
+#endif
+ add r0, r0, r2 @ Next line
+ b 1b
+2: DSB
+ bx lr
+
+@ r0 - *beg (inclusive)
+@ r1 - *end (exclusive)
+#ifdef ARMV6
+FUNCTION(grub_arm_invalidate_icache_range_armv6)
+#else
+FUNCTION(grub_arm_invalidate_icache_range_armv7)
+#endif
+ @ Invalidate instruction cache for range to point-of-unification
+1: cmp r0, r1
+ bge 2f
+ mcr p15, 0, r0, c7, c5, 1 @ ICIMVAU
+ add r0, r0, r2 @ Next line
+ b 1b
+ @ Branch predictor invalidate all
+2: mcr p15, 0, r0, c7, c5, 6 @ BPIALL
+ DSB
+ ISB
+ bx lr
+
+#ifdef ARMV6
+FUNCTION(grub_arm_disable_caches_mmu_armv6)
+#else
+FUNCTION(grub_arm_disable_caches_mmu_armv7)
+#endif
+
+ push {r4, lr}
+
+ @ disable D-cache
+ mrc p15, 0, r0, c1, c0, 0
+ bic r0, r0, #(1 << 2)
+ mcr p15, 0, r0, c1, c0, 0
+ DSB
+ ISB
+
+ @ clean/invalidate D-cache
+ bl clean_invalidate_dcache
+
+ @ disable I-cache
+ mrc p15, 0, r0, c1, c0, 0
+ bic r0, r0, #(1 << 12)
+ mcr p15, 0, r0, c1, c0, 0
+ DSB
+ ISB
+
+ @ invalidate I-cache (also invalidates branch predictors)
+ mcr p15, 0, r0, c7, c5, 0
+ DSB
+ ISB
+
+ @ clear SCTLR M bit
+ mrc p15, 0, r0, c1, c0, 0
+ bic r0, r0, #(1 << 0)
+ mcr p15, 0, r0, c1, c0, 0
+
+ mcr p15, 0, r0, c8, c7, 0 @ invalidate TLB
+ mcr p15, 0, r0, c7, c5, 6 @ invalidate branch predictor
+ DSB
+ ISB
+
+ pop {r4, lr}
+ bx lr
+
diff --git a/grub-core/kern/arm/cache.c b/grub-core/kern/arm/cache.c
new file mode 100644
index 0000000..6c75193
--- /dev/null
+++ b/grub-core/kern/arm/cache.c
@@ -0,0 +1,311 @@
+#include <grub/dl.h>
+#include <grub/cache.h>
+#include <grub/arm/system.h>
+#ifdef GRUB_MACHINE_UBOOT
+#include <grub/uboot/uboot.h>
+#include <grub/uboot/api_public.h>
+#include <grub/mm.h>
+#endif
+
+/* This is only about cache architecture. It doesn't imply
+ the CPU architecture. */
+static enum
+ {
+ ARCH_UNKNOWN,
+ ARCH_ARMV5_WRITE_THROUGH,
+ ARCH_ARMV6,
+ ARCH_ARMV6_UNIFIED,
+ ARCH_ARMV7
+ } type = ARCH_UNKNOWN;
+
+static int is_v6_mmu;
+
+static grub_uint32_t grub_arch_cache_dlinesz;
+static grub_uint32_t grub_arch_cache_ilinesz;
+static grub_uint32_t grub_arch_cache_max_linesz;
+
+/* Prototypes for asm functions. */
+void grub_arm_clean_dcache_range_armv6 (grub_addr_t start, grub_addr_t end,
+ grub_addr_t dlinesz);
+void grub_arm_clean_dcache_range_armv7 (grub_addr_t start, grub_addr_t end,
+ grub_addr_t dlinesz);
+void grub_arm_clean_dcache_range_poc_armv7 (grub_addr_t start, grub_addr_t end,
+ grub_addr_t dlinesz);
+void grub_arm_invalidate_icache_range_armv6 (grub_addr_t start, grub_addr_t end,
+ grub_addr_t dlinesz);
+void grub_arm_invalidate_icache_range_armv7 (grub_addr_t start, grub_addr_t end,
+ grub_addr_t dlinesz);
+void grub_arm_disable_caches_mmu_armv6 (void);
+void grub_arm_disable_caches_mmu_armv7 (void);
+grub_uint32_t grub_arm_main_id (void);
+grub_uint32_t grub_arm_cache_type (void);
+
+static void
+probe_caches (void)
+{
+ grub_uint32_t main_id, cache_type;
+
+ /* Read main ID Register */
+ main_id = grub_arm_main_id ();
+
+ switch ((main_id >> 16) & 0xf)
+ {
+ case 0x3:
+ case 0x4:
+ case 0x5:
+ case 0x6:
+ is_v6_mmu = 0;
+ break;
+ case 0x7:
+ case 0xf:
+ is_v6_mmu = 1;
+ break;
+ default:
+ grub_fatal ("Unsupported ARM ID 0x%x", main_id);
+ }
+
+ /* Read Cache Type Register */
+ cache_type = grub_arm_cache_type ();
+
+ switch (cache_type >> 24)
+ {
+ case 0x00:
+ case 0x01:
+ grub_arch_cache_dlinesz = 8 << ((cache_type >> 12) & 3);
+ grub_arch_cache_ilinesz = 8 << (cache_type & 3);
+ type = ARCH_ARMV5_WRITE_THROUGH;
+ break;
+ case 0x04:
+ case 0x0a:
+ case 0x0c:
+ case 0x0e:
+ case 0x1c:
+ grub_arch_cache_dlinesz = 8 << ((cache_type >> 12) & 3);
+ grub_arch_cache_ilinesz = 8 << (cache_type & 3);
+ type = ARCH_ARMV6_UNIFIED;
+ break;
+ case 0x05:
+ case 0x0b:
+ case 0x0d:
+ case 0x0f:
+ case 0x1d:
+ grub_arch_cache_dlinesz = 8 << ((cache_type >> 12) & 3);
+ grub_arch_cache_ilinesz = 8 << (cache_type & 3);
+ type = ARCH_ARMV6;
+ break;
+ default:
+ /*
+ * The CTR register is pretty much unchanged from v7 onwards,
+ * and is guaranteed to be backward compatible (the IDC/DIC bits
+ * allow certain CMOs to be elided, but performing them is never
+ * wrong), hence handling it like its AArch64 equivalent.
+ */
+ grub_arch_cache_dlinesz = 4 << ((cache_type >> 16) & 0xf);
+ grub_arch_cache_ilinesz = 4 << (cache_type & 0xf);
+ type = ARCH_ARMV7;
+ }
+ if (grub_arch_cache_dlinesz > grub_arch_cache_ilinesz)
+ grub_arch_cache_max_linesz = grub_arch_cache_dlinesz;
+ else
+ grub_arch_cache_max_linesz = grub_arch_cache_ilinesz;
+}
+
+#ifdef GRUB_MACHINE_UBOOT
+
+static void subdivide (grub_uint32_t *table, grub_uint32_t *subtable,
+ grub_uint32_t addr)
+{
+ grub_uint32_t j;
+ addr = addr >> 20 << 20;
+ table[addr >> 20] = (grub_addr_t) subtable | 1;
+ for (j = 0; j < 256; j++)
+ subtable[j] = addr | (j << 12)
+ | (3 << 4) | (3 << 6) | (3 << 8) | (3 << 10)
+ | (0 << 3) | (1 << 2) | 2;
+}
+
+void
+grub_arm_enable_caches_mmu (void)
+{
+ grub_uint32_t *table;
+ grub_uint32_t i;
+ grub_uint32_t border_crossing = 0;
+ grub_uint32_t *subtable;
+ struct sys_info *si = grub_uboot_get_sys_info ();
+
+ if (!si || (si->mr_no == 0))
+ {
+ grub_printf ("couldn't get memory map, not enabling caches");
+ grub_errno = GRUB_ERR_NONE;
+ return;
+ }
+
+ if (type == ARCH_UNKNOWN)
+ probe_caches ();
+
+ for (i = 0; (signed) i < si->mr_no; i++)
+ {
+ if (si->mr[i].start & ((1 << 20) - 1))
+ border_crossing++;
+ if ((si->mr[i].start + si->mr[i].size) & ((1 << 20) - 1))
+ border_crossing++;
+ }
+
+ grub_printf ("%d crossers\n", border_crossing);
+
+ table = grub_memalign (1 << 14, (1 << 14) + (border_crossing << 10));
+ if (!table)
+ {
+ grub_printf ("couldn't allocate place for MMU table, not enabling caches");
+ grub_errno = GRUB_ERR_NONE;
+ return;
+ }
+
+ subtable = table + (1 << 12);
+ /* Map all unknown as device. */
+ for (i = 0; i < (1 << 12); i++)
+ table[i] = (i << 20) | (3 << 10) | (0 << 3) | (1 << 2) | 2;
+ /*
+ Device: TEX= 0, C=0, B=1
+ normal: TEX= 0, C=1, B=1
+ AP = 3
+ IMP = 0
+ Domain = 0
+*/
+
+ for (i = 0; (signed) i < si->mr_no; i++)
+ {
+ if (si->mr[i].start & ((1 << 20) - 1))
+ {
+ subdivide (table, subtable, si->mr[i].start);
+ subtable += (1 << 8);
+ }
+ if ((si->mr[i].start + si->mr[i].size) & ((1 << 20) - 1))
+ {
+ subdivide (table, subtable, si->mr[i].start + si->mr[i].size);
+ subtable += (1 << 8);
+ }
+ }
+
+ for (i = 0; (signed) i < si->mr_no; i++)
+ if ((si->mr[i].flags & MR_ATTR_MASK) == MR_ATTR_DRAM
+ || (si->mr[i].flags & MR_ATTR_MASK) == MR_ATTR_SRAM
+ || (si->mr[i].flags & MR_ATTR_MASK) == MR_ATTR_FLASH)
+ {
+ grub_uint32_t cur, end;
+ cur = si->mr[i].start;
+ end = si->mr[i].start + si->mr[i].size;
+ while (cur < end)
+ {
+ grub_uint32_t *st;
+ if ((table[cur >> 20] & 3) == 2)
+ {
+ cur = cur >> 20 << 20;
+ table[cur >> 20] = cur | (3 << 10) | (1 << 3) | (1 << 2) | 2;
+ cur += (1 << 20);
+ continue;
+ }
+ cur = cur >> 12 << 12;
+ st = (grub_uint32_t *) (table[cur >> 20] & ~0x3ff);
+ st[(cur >> 12) & 0xff] = cur | (3 << 4) | (3 << 6)
+ | (3 << 8) | (3 << 10)
+ | (1 << 3) | (1 << 2) | 2;
+ cur += (1 << 12);
+ }
+ }
+
+ grub_printf ("MMU tables generated\n");
+ if (is_v6_mmu)
+ grub_arm_clear_mmu_v6 ();
+
+ grub_printf ("enabling MMU\n");
+ grub_arm_enable_mmu (table);
+ grub_printf ("MMU enabled\n");
+}
+
+#endif
+
+void
+grub_arch_sync_caches (void *address, grub_size_t len)
+{
+ grub_addr_t start = (grub_addr_t) address;
+ grub_addr_t end = start + len;
+
+ if (type == ARCH_UNKNOWN)
+ probe_caches ();
+ start = ALIGN_DOWN (start, grub_arch_cache_max_linesz);
+ end = ALIGN_UP (end, grub_arch_cache_max_linesz);
+ switch (type)
+ {
+ case ARCH_ARMV6:
+ grub_arm_clean_dcache_range_armv6 (start, end, grub_arch_cache_dlinesz);
+ grub_arm_invalidate_icache_range_armv6 (start, end,
+ grub_arch_cache_ilinesz);
+ break;
+ case ARCH_ARMV7:
+ grub_arm_clean_dcache_range_armv7 (start, end, grub_arch_cache_dlinesz);
+ grub_arm_invalidate_icache_range_armv7 (start, end,
+ grub_arch_cache_ilinesz);
+ break;
+ /* Nothing to do. */
+ case ARCH_ARMV5_WRITE_THROUGH:
+ case ARCH_ARMV6_UNIFIED:
+ break;
+ /* Pacify GCC. */
+ case ARCH_UNKNOWN:
+ break;
+ }
+}
+
+void
+grub_arch_sync_dma_caches (volatile void *address, grub_size_t len)
+{
+ grub_addr_t start = (grub_addr_t) address;
+ grub_addr_t end = start + len;
+
+ if (type == ARCH_UNKNOWN)
+ probe_caches ();
+ start = ALIGN_DOWN (start, grub_arch_cache_max_linesz);
+ end = ALIGN_UP (end, grub_arch_cache_max_linesz);
+ switch (type)
+ {
+ case ARCH_ARMV6:
+ grub_arm_clean_dcache_range_armv6 (start, end, grub_arch_cache_dlinesz);
+ grub_arm_invalidate_icache_range_armv6 (start, end,
+ grub_arch_cache_ilinesz);
+ break;
+ case ARCH_ARMV5_WRITE_THROUGH:
+ case ARCH_ARMV6_UNIFIED:
+ grub_arm_clean_dcache_range_armv6 (start, end, grub_arch_cache_dlinesz);
+ break;
+ case ARCH_ARMV7:
+ grub_arm_clean_dcache_range_poc_armv7 (start, end, grub_arch_cache_dlinesz);
+ grub_arm_invalidate_icache_range_armv7 (start, end,
+ grub_arch_cache_ilinesz);
+ break;
+ /* Pacify GCC. */
+ case ARCH_UNKNOWN:
+ break;
+ }
+}
+
+void
+grub_arm_disable_caches_mmu (void)
+{
+ if (type == ARCH_UNKNOWN)
+ probe_caches ();
+ switch (type)
+ {
+ case ARCH_ARMV5_WRITE_THROUGH:
+ case ARCH_ARMV6_UNIFIED:
+ case ARCH_ARMV6:
+ grub_arm_disable_caches_mmu_armv6 ();
+ break;
+ case ARCH_ARMV7:
+ grub_arm_disable_caches_mmu_armv7 ();
+ break;
+ /* Pacify GCC. */
+ case ARCH_UNKNOWN:
+ break;
+ }
+}
diff --git a/grub-core/kern/arm/cache_armv6.S b/grub-core/kern/arm/cache_armv6.S
new file mode 100644
index 0000000..dfaded0
--- /dev/null
+++ b/grub-core/kern/arm/cache_armv6.S
@@ -0,0 +1,72 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2013 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/symbol.h>
+
+ .file "cache_armv6.S"
+ .text
+ .syntax unified
+ .arm
+
+# define DMB mcr p15, 0, r0, c7, c10, 5
+# define DSB mcr p15, 0, r0, c7, c10, 4
+# define ISB mcr p15, 0, r0, c7, c5, 4
+#define ARMV6 1
+
+clean_invalidate_dcache:
+ mcr p15, 0, r0, c7, c14, 0 @ Clean/Invalidate D-cache
+ bx lr
+
+#include "cache.S"
+
+FUNCTION(grub_arm_main_id)
+ mrc p15, 0, r0, c0, c0, 0
+ bx lr
+
+FUNCTION(grub_arm_cache_type)
+ mrc p15, 0, r0, c0, c0, 1
+ bx lr
+
+FUNCTION(grub_arm_clear_mmu_v6)
+ mov r0, #0
+ mcr p15, 0, r0, c2, c0, 2
+ bx lr
+
+FUNCTION(grub_arm_enable_mmu)
+ mcr p15, 0, r0, c2, c0, 0
+
+ mvn r0, #0
+ mcr p15, 0, r0, c3, c0, 0
+
+ mrc p15, 0, r0, c1, c0, 0
+ bic r0, r0, #(1 << 23)
+ mcr p15, 0, r0, c1, c0, 0
+
+ mrc p15, 0, r0, c1, c0, 0
+ orr r0, r0, #(1 << 0)
+ mcr p15, 0, r0, c1, c0, 0
+
+ mrc p15, 0, r0, c1, c0, 0
+ orr r0, r0, #(1 << 2)
+ mcr p15, 0, r0, c1, c0, 0
+
+ mrc p15, 0, r0, c1, c0, 0
+ orr r0, r0, #(1 << 12)
+ mcr p15, 0, r0, c1, c0, 0
+
+ bx lr
diff --git a/grub-core/kern/arm/cache_armv7.S b/grub-core/kern/arm/cache_armv7.S
new file mode 100644
index 0000000..5ae76a3
--- /dev/null
+++ b/grub-core/kern/arm/cache_armv7.S
@@ -0,0 +1,138 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2013 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/symbol.h>
+
+ .file "cache_armv7.S"
+ .text
+ .syntax unified
+#if !defined (__thumb2__)
+ .arch armv7a
+ .arm
+#else
+ .arch armv7
+ .thumb
+#endif
+# define DMB dmb
+# define DSB dsb
+# define ISB isb
+#define ARMV7 1
+
+FUNCTION(grub_arm_clean_dcache_range_poc_armv7)
+ DSB
+ @ Clean data cache for range to point-of-coherence
+1: cmp r0, r1
+ bge 2f
+ mcr p15, 0, r0, c7, c14, 1 @ DCCMVAC
+ add r0, r0, r2 @ Next line
+ b 1b
+2: DSB
+ bx lr
+
+
+ @ r0 - CLIDR
+ @ r1 - LoC
+ @ r2 - current level
+ @ r3 - num sets
+ @ r4 - num ways
+ @ r5 - current set
+ @ r6 - current way
+ @ r7 - line size
+ @ r8 - scratch
+ @ r9 - scratch
+ @ r10 - scratch
+ @ r11 - scratch
+clean_invalidate_dcache:
+ push {r4-r12, lr}
+ mrc p15, 1, r0, c0, c0, 1 @ Read CLIDR
+ lsr r1, r0, #24 @ Extract LoC
+ and r1, r1, #0x7
+
+ mov r2, #0 @ First level, L1
+2: and r8, r0, #7 @ cache type at current level
+ cmp r8, #2
+ blt 5f @ instruction only, or none, skip level
+
+ @ set current cache level/type (for CCSIDR read)
+ lsl r8, r2, #1
+ mcr p15, 2, r8, c0, c0, 0 @ Write CSSELR (level, type: data/uni)
+
+ @ read current cache information
+ mrc p15, 1, r8, c0, c0, 0 @ Read CCSIDR
+ lsr r3, r8, #13 @ Number of sets -1
+
+ @ Keep only 14 bits of r3
+ lsl r3, r3, #18
+ lsr r3, r3, #18
+
+ lsr r4, r8, #3 @ Number of ways -1
+
+ @ Keep only 9 bits of r4
+ lsl r4, r4, #23
+ lsr r4, r4, #23
+
+ and r7, r8, #7 @ log2(line size in words) - 2
+ add r7, r7, #2 @ adjust
+ mov r8, #1
+ lsl r7, r8, r7 @ -> line size in words
+ lsl r7, r7, #2 @ -> bytes
+
+ @ set loop
+ mov r5, #0 @ current set = 0
+3: lsl r8, r2, #1 @ insert level
+ clz r9, r7 @ calculate set field offset
+ mov r10, #31
+ sub r9, r10, r9
+ lsl r10, r5, r9
+ orr r8, r8, r10 @ insert set field
+
+ @ way loop
+ @ calculate way field offset
+ mov r6, #0 @ current way = 0
+ add r10, r4, #1
+ clz r9, r10 @ r9 = way field offset
+ add r9, r9, #1
+4: lsl r10, r6, r9
+ orr r11, r8, r10 @ insert way field
+
+ @ clean and invalidate line by set/way
+ mcr p15, 0, r11, c7, c14, 2 @ DCCISW
+
+ @ next way
+ add r6, r6, #1
+ cmp r6, r4
+ ble 4b
+
+ @ next set
+ add r5, r5, #1
+ cmp r5, r3
+ ble 3b
+
+ @ next level
+5: lsr r0, r0, #3 @ align next level CLIDR 'type' field
+ add r2, r2, #1 @ increment cache level counter
+ cmp r2, r1
+ blt 2b @ outer loop
+
+ @ return
+6: DSB
+ ISB
+ pop {r4-r12, lr}
+ bx lr
+
+#include "cache.S" \ No newline at end of file
diff --git a/grub-core/kern/arm/compiler-rt.S b/grub-core/kern/arm/compiler-rt.S
new file mode 100644
index 0000000..645b42f
--- /dev/null
+++ b/grub-core/kern/arm/compiler-rt.S
@@ -0,0 +1,86 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2013 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/symbol.h>
+#include <grub/dl.h>
+
+ .file "misc.S"
+ .text
+ .syntax unified
+#if !defined (__thumb2__)
+ .arm
+#else
+ .thumb
+#endif
+
+ .align 2
+
+FUNCTION(__muldi3)
+FUNCTION(__aeabi_lmul)
+ stmfd sp!, {r4, fp}
+ add fp, sp, #4
+ sub sp, sp, #16
+ str r0, [fp, #-12]
+ str r1, [fp, #-8]
+ str r2, [fp, #-20]
+ str r3, [fp, #-16]
+ ldr r3, [fp, #-8]
+ ldr r2, [fp, #-20]
+ mul r2, r3, r2
+ ldr r3, [fp, #-16]
+ ldr r1, [fp, #-12]
+ mul r3, r1, r3
+ add r2, r2, r3
+ ldr r0, [fp, #-12]
+ ldr r1, [fp, #-20]
+ umull r3, r4, r0, r1
+ add r2, r2, r4
+ mov r4, r2
+ mov r0, r3
+ mov r1, r4
+ mov sp, fp
+ sub sp, sp, #4
+ ldmfd sp!, {r4, fp}
+ bx lr
+
+ .macro division32 parent
+
+ sub sp, sp, #8 @ Allocate naturally aligned 64-bit space
+ stmfd sp!, {r3,lr} @ Dummy r3 to maintain stack alignment
+ add r2, sp, #8 @ Set r2 to address of 64-bit space
+ bl \parent
+ ldr r1, [sp, #8] @ Extract remainder
+ ldmfd sp!, {r3,lr} @ Pop into an unused arg/scratch register
+ add sp, sp, #8
+ bx lr
+ .endm
+
+FUNCTION(__aeabi_uidivmod)
+ division32 grub_divmod32
+FUNCTION(__aeabi_idivmod)
+ division32 grub_divmod32s
+
+/*
+ * Null divide-by-zero handler
+ */
+FUNCTION(__aeabi_unwind_cpp_pr0)
+FUNCTION(raise)
+ mov r0, #0
+ bx lr
+
+ END
diff --git a/grub-core/kern/arm/coreboot/cbtable.c b/grub-core/kern/arm/coreboot/cbtable.c
new file mode 100644
index 0000000..8a655bb
--- /dev/null
+++ b/grub-core/kern/arm/coreboot/cbtable.c
@@ -0,0 +1,40 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2007,2008,2013 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/coreboot/lbio.h>
+#include <grub/types.h>
+#include <grub/err.h>
+#include <grub/misc.h>
+#include <grub/dl.h>
+#include <grub/arm/startup.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+#pragma GCC diagnostic ignored "-Wcast-align"
+
+grub_linuxbios_table_header_t
+grub_linuxbios_get_tables (void)
+{
+ grub_linuxbios_table_header_t table_header
+ = (grub_linuxbios_table_header_t) grub_arm_saved_registers.r[0];
+
+ if (!grub_linuxbios_check_signature (table_header))
+ return 0;
+
+ return table_header;
+}
diff --git a/grub-core/kern/arm/coreboot/coreboot.S b/grub-core/kern/arm/coreboot/coreboot.S
new file mode 100644
index 0000000..a110452
--- /dev/null
+++ b/grub-core/kern/arm/coreboot/coreboot.S
@@ -0,0 +1,44 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/symbol.h>
+
+ .file "coreboot.S"
+ .text
+ .syntax unified
+#if !defined (__thumb2__)
+ .arch armv7a
+ .arm
+#else
+ .arch armv7
+ .thumb
+#endif
+
+FUNCTION(grub_arm_pfr1)
+ mrc p15, 0, r0, c0, c1, 1
+ bx lr
+
+FUNCTION(grub_armv7_get_timer_value)
+ isb
+ mrrc p15, 1, r0, r1, c14
+ bx lr
+
+FUNCTION(grub_armv7_get_timer_frequency)
+ mrc p15, 0, r0, c14, c0, 0
+ bx lr
+
diff --git a/grub-core/kern/arm/coreboot/dma.c b/grub-core/kern/arm/coreboot/dma.c
new file mode 100644
index 0000000..2c2a627
--- /dev/null
+++ b/grub-core/kern/arm/coreboot/dma.c
@@ -0,0 +1,59 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2007,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/dl.h>
+#include <grub/dma.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/mm_private.h>
+#include <grub/cache.h>
+
+struct grub_pci_dma_chunk *
+grub_memalign_dma32 (grub_size_t align, grub_size_t size)
+{
+ void *ret;
+ if (align < 64)
+ align = 64;
+ size = ALIGN_UP (size, align);
+ ret = grub_memalign (align, size);
+ if (!ret)
+ return 0;
+ grub_arch_sync_dma_caches (ret, size);
+ return ret;
+}
+
+void
+grub_dma_free (struct grub_pci_dma_chunk *ch)
+{
+ grub_size_t size = (((struct grub_mm_header *) ch) - 1)->size * GRUB_MM_ALIGN;
+ grub_arch_sync_dma_caches (ch, size);
+ grub_free (ch);
+}
+
+volatile void *
+grub_dma_get_virt (struct grub_pci_dma_chunk *ch)
+{
+ return (void *) ch;
+}
+
+grub_uint32_t
+grub_dma_get_phys (struct grub_pci_dma_chunk *ch)
+{
+ return (grub_uint32_t) (grub_addr_t) ch;
+}
+
diff --git a/grub-core/kern/arm/coreboot/init.c b/grub-core/kern/arm/coreboot/init.c
new file mode 100644
index 0000000..8d8c5b8
--- /dev/null
+++ b/grub-core/kern/arm/coreboot/init.c
@@ -0,0 +1,151 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2013 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/kernel.h>
+#include <grub/mm.h>
+#include <grub/memory.h>
+#include <grub/machine/console.h>
+#include <grub/machine/kernel.h>
+#include <grub/offsets.h>
+#include <grub/types.h>
+#include <grub/err.h>
+#include <grub/dl.h>
+#include <grub/misc.h>
+#include <grub/loader.h>
+#include <grub/env.h>
+#include <grub/cache.h>
+#include <grub/time.h>
+#include <grub/symbol.h>
+#include <grub/video.h>
+#include <grub/coreboot/lbio.h>
+#include <grub/fdtbus.h>
+
+extern grub_uint8_t _start[];
+extern grub_uint8_t _end[];
+extern grub_uint8_t _edata[];
+grub_addr_t start_of_ram = ~(grub_addr_t)0;
+
+void __attribute__ ((noreturn))
+grub_exit (void)
+{
+ /* We can't use grub_fatal() in this function. This would create an infinite
+ loop, since grub_fatal() calls grub_abort() which in turn calls grub_exit(). */
+ while (1)
+ grub_cpu_idle ();
+}
+
+static grub_uint64_t modend;
+static int have_memory = 0;
+
+/* Helper for grub_machine_init. */
+static int
+heap_init (grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type,
+ void *data __attribute__ ((unused)))
+{
+ grub_uint64_t begin = addr, end = addr + size;
+
+#if GRUB_CPU_SIZEOF_VOID_P == 4
+ /* Restrict ourselves to 32-bit memory space. */
+ if (begin > GRUB_ULONG_MAX)
+ return 0;
+ if (end > GRUB_ULONG_MAX)
+ end = GRUB_ULONG_MAX;
+#endif
+
+ if (start_of_ram > begin)
+ start_of_ram = begin;
+
+ if (type != GRUB_MEMORY_AVAILABLE)
+ return 0;
+
+ if (modend && begin < modend)
+ {
+ if (begin < (grub_addr_t)_start)
+ {
+ grub_mm_init_region ((void *) (grub_addr_t) begin, (grub_size_t) ((grub_addr_t)_start - begin));
+ have_memory = 1;
+ }
+ begin = modend;
+ }
+
+ /* Avoid DMA problems. */
+ if (end >= 0xfe000000)
+ end = 0xfe000000;
+
+ if (end <= begin)
+ return 0;
+
+ grub_mm_init_region ((void *) (grub_addr_t) begin, (grub_size_t) (end - begin));
+
+ have_memory = 1;
+
+ return 0;
+}
+
+void
+grub_machine_init (void)
+{
+ struct grub_module_header *header;
+ void *dtb = 0;
+ grub_size_t dtb_size = 0;
+
+ modend = grub_modules_get_end ();
+
+ grub_video_coreboot_fb_early_init ();
+
+ grub_machine_mmap_iterate (heap_init, NULL);
+ if (!have_memory)
+ grub_fatal ("No memory found");
+
+ grub_video_coreboot_fb_late_init ();
+
+ grub_font_init ();
+ grub_gfxterm_init ();
+
+ FOR_MODULES (header)
+ if (header->type == OBJ_TYPE_DTB)
+ {
+ char *dtb_orig_addr, *dtb_copy;
+ dtb_orig_addr = (char *) header + sizeof (struct grub_module_header);
+
+ dtb_size = header->size - sizeof (struct grub_module_header);
+ dtb = dtb_copy = grub_malloc (dtb_size);
+ grub_memmove (dtb_copy, dtb_orig_addr, dtb_size);
+ break;
+ }
+ if (!dtb)
+ grub_fatal ("No DTB found");
+ grub_fdtbus_init (dtb, dtb_size);
+
+ grub_rk3288_spi_init ();
+
+ grub_machine_timer_init ();
+ grub_cros_init ();
+ grub_pl050_init ();
+}
+
+void
+grub_machine_get_bootlocation (char **device __attribute__ ((unused)),
+ char **path __attribute__ ((unused)))
+{
+}
+
+void
+grub_machine_fini (int flags __attribute__ ((unused)))
+{
+}
diff --git a/grub-core/kern/arm/coreboot/timer.c b/grub-core/kern/arm/coreboot/timer.c
new file mode 100644
index 0000000..d97b844
--- /dev/null
+++ b/grub-core/kern/arm/coreboot/timer.c
@@ -0,0 +1,101 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/mm.h>
+#include <grub/machine/kernel.h>
+#include <grub/types.h>
+#include <grub/err.h>
+#include <grub/time.h>
+#include <grub/fdtbus.h>
+#include <grub/misc.h>
+
+grub_uint64_t
+grub_armv7_get_timer_value(void);
+
+grub_uint32_t
+grub_armv7_get_timer_frequency(void);
+
+grub_uint32_t
+grub_arm_pfr1(void);
+
+static int have_timer = 0;
+static volatile grub_uint32_t *sp804_regs;
+
+static grub_uint64_t
+sp804_get_time_ms (void)
+{
+ static grub_uint32_t high, last_low;
+ grub_uint32_t low = ~sp804_regs[1];
+ if (last_low > low)
+ high++;
+ last_low = low;
+ return grub_divmod64 ((((grub_uint64_t) high) << 32) | low,
+ 1000, 0);
+}
+
+static grub_err_t
+sp804_attach(const struct grub_fdtbus_dev *dev)
+{
+ if (have_timer)
+ return GRUB_ERR_NONE;
+ sp804_regs = grub_fdtbus_map_reg (dev, 0, 0);
+ if (!grub_fdtbus_is_mapping_valid (sp804_regs))
+ return grub_error (GRUB_ERR_IO, "could not map sp804: %p", sp804_regs);
+ grub_install_get_time_ms (sp804_get_time_ms);
+ have_timer = 1;
+ return GRUB_ERR_NONE;
+}
+
+struct grub_fdtbus_driver sp804 =
+{
+ .compatible = "arm,sp804",
+ .attach = sp804_attach
+};
+
+static grub_uint32_t timer_frequency_in_khz;
+
+static grub_uint64_t
+generic_get_time_ms (void)
+{
+ return grub_divmod64 (grub_armv7_get_timer_value(), timer_frequency_in_khz, 0);
+}
+
+static int
+try_generic_timer (void)
+{
+ if (((grub_arm_pfr1 () >> 16) & 0xf) != 1)
+ return 0;
+ grub_printf ("freq = %x\n", grub_armv7_get_timer_frequency());
+ timer_frequency_in_khz = 0x016e3600 / 1000; //grub_armv7_get_timer_frequency() / 1000;
+ if (timer_frequency_in_khz == 0)
+ return 0;
+ grub_install_get_time_ms (generic_get_time_ms);
+ have_timer = 1;
+ return 1;
+}
+
+void
+grub_machine_timer_init (void)
+{
+ grub_fdtbus_register (&sp804);
+
+ if (!have_timer)
+ try_generic_timer ();
+ if (!have_timer)
+ grub_fatal ("No timer found");
+}
diff --git a/grub-core/kern/arm/dl.c b/grub-core/kern/arm/dl.c
new file mode 100644
index 0000000..eab9d17
--- /dev/null
+++ b/grub-core/kern/arm/dl.c
@@ -0,0 +1,280 @@
+/* dl.c - arch-dependent part of loadable module support */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2013 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/dl.h>
+#include <grub/elf.h>
+#include <grub/misc.h>
+#include <grub/err.h>
+#include <grub/mm.h>
+#include <grub/i18n.h>
+#include <grub/arm/reloc.h>
+
+struct trampoline_arm
+{
+#define ARM_LOAD_IP 0xe59fc000
+#define ARM_BX 0xe12fff1c
+#define ARM_MOV_PC 0xe1a0f00c
+ grub_uint32_t load_ip; /* ldr ip, [pc] */
+ grub_uint32_t bx; /* bx ip or mov pc, ip*/
+ grub_uint32_t addr;
+};
+
+static grub_uint16_t thumb_template[8] =
+ {
+ 0x468c, /* mov ip, r1 */
+ 0x4903, /* ldr r1, [pc, #12] ; (10 <.text+0x10>) */
+ /* Exchange R1 and IP in limited Thumb instruction set.
+ IP gets negated but we compensate it by C code. */
+ /* R1 IP */
+ /* -A R1 */
+ 0x4461, /* add r1, ip */ /* R1-A R1 */
+ 0x4249, /* negs r1, r1 */ /* A-R1 R1 */
+ 0x448c, /* add ip, r1 */ /* A-R1 A */
+ 0x4249, /* negs r1, r1 */ /* R1-A A */
+ 0x4461, /* add r1, ip */ /* R1 A */
+ 0x4760 /* bx ip */
+ };
+
+struct trampoline_thumb
+{
+ grub_uint16_t template[8];
+ grub_uint32_t neg_addr;
+};
+
+#pragma GCC diagnostic ignored "-Wcast-align"
+
+grub_err_t
+grub_arch_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp,
+ grub_size_t *got)
+{
+ const Elf_Ehdr *e = ehdr;
+ const Elf_Shdr *s;
+ unsigned i;
+
+ *tramp = 0;
+ *got = 0;
+
+ for (i = 0, s = (const Elf_Shdr *) ((grub_addr_t) e + e->e_shoff);
+ i < e->e_shnum;
+ i++, s = (const Elf_Shdr *) ((grub_addr_t) s + e->e_shentsize))
+ if (s->sh_type == SHT_REL)
+ {
+ const Elf_Rel *rel, *max;
+
+ for (rel = (const Elf_Rel *) ((grub_addr_t) e + s->sh_offset),
+ max = (const Elf_Rel *) ((grub_addr_t) rel + s->sh_size);
+ rel + 1 <= max;
+ rel = (const Elf_Rel *) ((grub_addr_t) rel + s->sh_entsize))
+ switch (ELF_R_TYPE (rel->r_info))
+ {
+ case R_ARM_CALL:
+ case R_ARM_JUMP24:
+ {
+ *tramp += sizeof (struct trampoline_arm);
+ break;
+ }
+ case R_ARM_THM_CALL:
+ case R_ARM_THM_JUMP24:
+ case R_ARM_THM_JUMP19:
+ {
+ *tramp += sizeof (struct trampoline_thumb);
+ break;
+ }
+ }
+ }
+
+ grub_dprintf ("dl", "trampoline size %x\n", *tramp);
+
+ return GRUB_ERR_NONE;
+}
+
+/*************************************************
+ * Runtime dynamic linker with helper functions. *
+ *************************************************/
+grub_err_t
+grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
+ Elf_Shdr *s, grub_dl_segment_t seg)
+{
+ Elf_Rel *rel, *max;
+
+ for (rel = (Elf_Rel *) ((char *) ehdr + s->sh_offset),
+ max = (Elf_Rel *) ((char *) rel + s->sh_size);
+ rel < max;
+ rel = (Elf_Rel *) ((char *) rel + s->sh_entsize))
+ {
+ Elf_Addr *target, sym_addr;
+ grub_err_t retval;
+ Elf_Sym *sym;
+
+ if (seg->size < rel->r_offset)
+ return grub_error (GRUB_ERR_BAD_MODULE,
+ "reloc offset is out of the segment");
+ target = (void *) ((char *) seg->addr + rel->r_offset);
+ sym = (Elf_Sym *) ((char *) mod->symtab
+ + mod->symsize * ELF_R_SYM (rel->r_info));
+
+ sym_addr = sym->st_value;
+
+ switch (ELF_R_TYPE (rel->r_info))
+ {
+ case R_ARM_ABS32:
+ {
+ /* Data will be naturally aligned */
+ retval = grub_arm_reloc_abs32 (target, sym_addr);
+ if (retval != GRUB_ERR_NONE)
+ return retval;
+ }
+ break;
+ case R_ARM_CALL:
+ case R_ARM_JUMP24:
+ {
+ grub_int32_t offset;
+
+ sym_addr += grub_arm_jump24_get_offset (target);
+ offset = sym_addr - (grub_uint32_t) target;
+
+ if ((sym_addr & 1) || !grub_arm_jump24_check_offset (offset))
+ {
+ struct trampoline_arm *tp = mod->trampptr;
+ mod->trampptr = tp + 1;
+ tp->load_ip = ARM_LOAD_IP;
+ tp->bx = (sym_addr & 1) ? ARM_BX : ARM_MOV_PC;
+ tp->addr = sym_addr + 8;
+ offset = (grub_uint8_t *) tp - (grub_uint8_t *) target - 8;
+ }
+ if (!grub_arm_jump24_check_offset (offset))
+ return grub_error (GRUB_ERR_BAD_MODULE,
+ "trampoline out of range");
+ grub_arm_jump24_set_offset (target, offset);
+ }
+ break;
+ case R_ARM_THM_CALL:
+ case R_ARM_THM_JUMP24:
+ {
+ /* Thumb instructions can be 16-bit aligned */
+ grub_int32_t offset;
+
+ sym_addr += grub_arm_thm_call_get_offset ((grub_uint16_t *) target);
+
+ grub_dprintf ("dl", " sym_addr = 0x%08x\n", sym_addr);
+ if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
+ sym_addr |= 1;
+
+ offset = sym_addr - (grub_uint32_t) target;
+
+ grub_dprintf("dl", " BL*: target=%p, sym_addr=0x%08x, offset=%d\n",
+ target, sym_addr, offset);
+
+ if (!(sym_addr & 1) || (offset < -0x200000 || offset >= 0x200000))
+ {
+ struct trampoline_thumb *tp = mod->trampptr;
+ mod->trampptr = tp + 1;
+ grub_memcpy (tp->template, thumb_template, sizeof (tp->template));
+ tp->neg_addr = -sym_addr - 4;
+ offset = ((grub_uint8_t *) tp - (grub_uint8_t *) target - 4) | 1;
+ }
+
+ if (offset < -0x200000 || offset >= 0x200000)
+ return grub_error (GRUB_ERR_BAD_MODULE,
+ "trampoline out of range");
+
+ grub_dprintf ("dl", " relative destination = %p\n",
+ (char *) target + offset);
+
+ retval = grub_arm_thm_call_set_offset ((grub_uint16_t *) target, offset);
+ if (retval != GRUB_ERR_NONE)
+ return retval;
+ }
+ break;
+ /* Happens when compiled with -march=armv4. Since currently we need
+ at least armv5, keep bx as-is.
+ */
+ case R_ARM_V4BX:
+ break;
+ case R_ARM_THM_MOVW_ABS_NC:
+ case R_ARM_THM_MOVT_ABS:
+ {
+ grub_uint32_t offset;
+ offset = grub_arm_thm_movw_movt_get_value((grub_uint16_t *) target);
+ offset += sym_addr;
+
+ if (ELF_R_TYPE (rel->r_info) == R_ARM_THM_MOVT_ABS)
+ offset >>= 16;
+ else
+ offset &= 0xffff;
+
+ grub_arm_thm_movw_movt_set_value((grub_uint16_t *) target, offset);
+ }
+ break;
+ case R_ARM_THM_JUMP19:
+ {
+ /* Thumb instructions can be 16-bit aligned */
+ grub_int32_t offset;
+
+ sym_addr += grub_arm_thm_jump19_get_offset ((grub_uint16_t *) target);
+
+ if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
+ sym_addr |= 1;
+
+ offset = sym_addr - (grub_uint32_t) target;
+
+ if (!grub_arm_thm_jump19_check_offset (offset)
+ || !(sym_addr & 1))
+ {
+ struct trampoline_thumb *tp = mod->trampptr;
+ mod->trampptr = tp + 1;
+ grub_memcpy (tp->template, thumb_template, sizeof (tp->template));
+ tp->neg_addr = -sym_addr - 4;
+ offset = ((grub_uint8_t *) tp - (grub_uint8_t *) target - 4) | 1;
+ }
+
+ if (!grub_arm_thm_jump19_check_offset (offset))
+ return grub_error (GRUB_ERR_BAD_MODULE,
+ "trampoline out of range");
+
+ grub_arm_thm_jump19_set_offset ((grub_uint16_t *) target, offset);
+ }
+ break;
+ default:
+ return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
+ N_("relocation 0x%x is not implemented yet"),
+ ELF_R_TYPE (rel->r_info));
+ }
+ }
+
+ return GRUB_ERR_NONE;
+}
+
+
+/*
+ * Check if EHDR is a valid ELF header.
+ */
+grub_err_t
+grub_arch_dl_check_header (void *ehdr)
+{
+ Elf_Ehdr *e = ehdr;
+
+ /* Check the magic numbers. */
+ if (e->e_ident[EI_CLASS] != ELFCLASS32
+ || e->e_ident[EI_DATA] != ELFDATA2LSB || e->e_machine != EM_ARM)
+ return grub_error (GRUB_ERR_BAD_OS,
+ N_("invalid arch-dependent ELF magic"));
+
+ return GRUB_ERR_NONE;
+}
diff --git a/grub-core/kern/arm/dl_helper.c b/grub-core/kern/arm/dl_helper.c
new file mode 100644
index 0000000..21d77f7
--- /dev/null
+++ b/grub-core/kern/arm/dl_helper.c
@@ -0,0 +1,245 @@
+/* dl.c - arch-dependent part of loadable module support */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2013 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/dl.h>
+#include <grub/elf.h>
+#include <grub/misc.h>
+#include <grub/err.h>
+#include <grub/mm.h>
+#include <grub/i18n.h>
+#include <grub/arm/reloc.h>
+
+static inline grub_uint32_t
+thumb_get_instruction_word (grub_uint16_t *target)
+{
+ /* Extract instruction word in alignment-safe manner */
+ return grub_le_to_cpu16 ((*target)) << 16 | grub_le_to_cpu16 (*(target + 1));
+}
+
+static inline void
+thumb_set_instruction_word (grub_uint16_t *target, grub_uint32_t insword)
+{
+ *target = grub_cpu_to_le16 (insword >> 16);
+ *(target + 1) = grub_cpu_to_le16 (insword & 0xffff);
+}
+
+/*
+ * R_ARM_ABS32
+ *
+ * Simple relocation of 32-bit value (in literal pool)
+ */
+grub_err_t
+grub_arm_reloc_abs32 (Elf32_Word *target, Elf32_Addr sym_addr)
+{
+ Elf32_Addr tmp;
+
+ tmp = grub_le_to_cpu32 (*target);
+ tmp += sym_addr;
+ *target = grub_cpu_to_le32 (tmp);
+
+ return GRUB_ERR_NONE;
+}
+
+/********************************************************************
+ * Thumb (T32) relocations: *
+ * *
+ * 32-bit Thumb instructions can be 16-bit aligned, and are fetched *
+ * little-endian, requiring some additional fiddling. *
+ ********************************************************************/
+
+grub_int32_t
+grub_arm_thm_call_get_offset (grub_uint16_t *target)
+{
+ grub_uint32_t sign, j1, j2;
+ grub_uint32_t insword;
+ grub_int32_t offset;
+
+ insword = thumb_get_instruction_word (target);
+
+ /* Extract bitfields from instruction words */
+ sign = (insword >> 26) & 1;
+ j1 = (insword >> 13) & 1;
+ j2 = (insword >> 11) & 1;
+ offset = (sign << 24) | ((~(j1 ^ sign) & 1) << 23) |
+ ((~(j2 ^ sign) & 1) << 22) |
+ ((insword & 0x03ff0000) >> 4) | ((insword & 0x000007ff) << 1);
+
+ /* Sign adjust and calculate offset */
+ if (offset & (1 << 24))
+ offset -= (1 << 25);
+
+ return offset;
+}
+
+grub_err_t
+grub_arm_thm_call_set_offset (grub_uint16_t *target, grub_int32_t offset)
+{
+ grub_uint32_t sign, j1, j2;
+ const grub_uint32_t insmask = 0xf800d000;
+ grub_uint32_t insword;
+ int is_blx;
+
+ insword = thumb_get_instruction_word (target);
+
+ if (((insword >> 12) & 0xd) == 0xc)
+ is_blx = 1;
+ else
+ is_blx = 0;
+
+ if (!is_blx && !(offset & 1))
+ return grub_error (GRUB_ERR_BAD_MODULE, "bl/b.w targettting ARM");
+
+ /* Transform blx into bl if necessarry. */
+ if (is_blx && (offset & 1))
+ insword |= (1 << 12);
+
+ /* Reassemble instruction word */
+ sign = (offset >> 24) & 1;
+ j1 = sign ^ (~(offset >> 23) & 1);
+ j2 = sign ^ (~(offset >> 22) & 1);
+ insword = (insword & insmask) |
+ (sign << 26) |
+ (((offset >> 12) & 0x03ff) << 16) |
+ (j1 << 13) | (j2 << 11) | ((offset >> 1) & 0x07ff);
+
+ thumb_set_instruction_word (target, insword);
+
+ grub_dprintf ("dl", " *insword = 0x%08x", insword);
+
+ return GRUB_ERR_NONE;
+}
+
+grub_int32_t
+grub_arm_thm_jump19_get_offset (grub_uint16_t *target)
+{
+ grub_int32_t offset;
+ grub_uint32_t insword;
+
+ insword = thumb_get_instruction_word (target);
+
+ /* Extract and sign extend offset */
+ offset = ((insword >> 26) & 1) << 19
+ | ((insword >> 11) & 1) << 18
+ | ((insword >> 13) & 1) << 17
+ | ((insword >> 16) & 0x3f) << 11
+ | (insword & 0x7ff);
+ offset <<= 1;
+ if (offset & (1 << 20))
+ offset -= (1 << 21);
+
+ return offset;
+}
+
+void
+grub_arm_thm_jump19_set_offset (grub_uint16_t *target, grub_int32_t offset)
+{
+ grub_uint32_t insword;
+ const grub_uint32_t insmask = 0xfbc0d000;
+
+ offset >>= 1;
+ offset &= 0xfffff;
+
+ insword = thumb_get_instruction_word (target);
+
+ /* Reassemble instruction word and write back */
+ insword &= insmask;
+ insword |= ((offset >> 19) & 1) << 26
+ | ((offset >> 18) & 1) << 11
+ | ((offset >> 17) & 1) << 13
+ | ((offset >> 11) & 0x3f) << 16
+ | (offset & 0x7ff);
+ thumb_set_instruction_word (target, insword);
+}
+
+int
+grub_arm_thm_jump19_check_offset (grub_int32_t offset)
+{
+ if ((offset > 1048574) || (offset < -1048576))
+ return 0;
+ return 1;
+}
+
+grub_uint16_t
+grub_arm_thm_movw_movt_get_value (grub_uint16_t *target)
+{
+ grub_uint32_t insword;
+
+ insword = thumb_get_instruction_word (target);
+
+ return ((insword & 0xf0000) >> 4) | ((insword & 0x04000000) >> 15) | \
+ ((insword & 0x7000) >> 4) | (insword & 0xff);
+}
+
+void
+grub_arm_thm_movw_movt_set_value (grub_uint16_t *target, grub_uint16_t value)
+{
+ grub_uint32_t insword;
+ const grub_uint32_t insmask = 0xfbf08f00;
+
+ insword = thumb_get_instruction_word (target);
+ insword &= insmask;
+
+ insword |= ((value & 0xf000) << 4) | ((value & 0x0800) << 15) | \
+ ((value & 0x0700) << 4) | (value & 0xff);
+
+ thumb_set_instruction_word (target, insword);
+}
+
+
+/***********************************************************
+ * ARM (A32) relocations: *
+ * *
+ * ARM instructions are 32-bit in size and 32-bit aligned. *
+ ***********************************************************/
+
+grub_int32_t
+grub_arm_jump24_get_offset (grub_uint32_t *target)
+{
+ grub_int32_t offset;
+ grub_uint32_t insword;
+
+ insword = grub_le_to_cpu32 (*target);
+
+ offset = (insword & 0x00ffffff) << 2;
+ if (offset & 0x02000000)
+ offset -= 0x04000000;
+ return offset;
+}
+
+int
+grub_arm_jump24_check_offset (grub_int32_t offset)
+{
+ if (offset >= 0x02000000 || offset < -0x02000000)
+ return 0;
+ return 1;
+}
+
+void
+grub_arm_jump24_set_offset (grub_uint32_t *target,
+ grub_int32_t offset)
+{
+ grub_uint32_t insword;
+
+ insword = grub_le_to_cpu32 (*target);
+
+ insword &= 0xff000000;
+ insword |= (offset >> 2) & 0x00ffffff;
+
+ *target = grub_cpu_to_le32 (insword);
+}
diff --git a/grub-core/kern/arm/efi/init.c b/grub-core/kern/arm/efi/init.c
new file mode 100644
index 0000000..40c3b46
--- /dev/null
+++ b/grub-core/kern/arm/efi/init.c
@@ -0,0 +1,77 @@
+/* init.c - initialize an arm-based EFI system */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2013 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/env.h>
+#include <grub/kernel.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/time.h>
+#include <grub/efi/efi.h>
+#include <grub/loader.h>
+
+static grub_uint64_t tmr;
+static grub_efi_event_t tmr_evt;
+
+static grub_uint64_t
+grub_efi_get_time_ms (void)
+{
+ return tmr;
+}
+
+static void
+increment_timer (grub_efi_event_t event __attribute__ ((unused)),
+ void *context __attribute__ ((unused)))
+{
+ tmr += 10;
+}
+
+void
+grub_machine_init (void)
+{
+ grub_efi_boot_services_t *b;
+
+ grub_efi_init ();
+
+ b = grub_efi_system_table->boot_services;
+
+ efi_call_5 (b->create_event, GRUB_EFI_EVT_TIMER | GRUB_EFI_EVT_NOTIFY_SIGNAL,
+ GRUB_EFI_TPL_CALLBACK, increment_timer, NULL, &tmr_evt);
+ efi_call_3 (b->set_timer, tmr_evt, GRUB_EFI_TIMER_PERIODIC, 100000);
+
+ grub_install_get_time_ms (grub_efi_get_time_ms);
+}
+
+void
+grub_machine_fini (int flags)
+{
+ grub_efi_boot_services_t *b;
+
+ if (!(flags & GRUB_LOADER_FLAG_NORETURN))
+ return;
+
+ b = grub_efi_system_table->boot_services;
+
+ efi_call_3 (b->set_timer, tmr_evt, GRUB_EFI_TIMER_CANCEL, 0);
+ efi_call_1 (b->close_event, tmr_evt);
+
+ grub_efi_fini ();
+
+ if (!(flags & GRUB_LOADER_FLAG_EFI_KEEP_ALLOCATED_MEMORY))
+ grub_efi_memory_fini ();
+}
diff --git a/grub-core/kern/arm/efi/startup.S b/grub-core/kern/arm/efi/startup.S
new file mode 100644
index 0000000..9f82653
--- /dev/null
+++ b/grub-core/kern/arm/efi/startup.S
@@ -0,0 +1,36 @@
+/*
+ * (C) Copyright 2013 Free Software Foundation
+ *
+ * This program 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 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ *
+ */
+
+#include <grub/symbol.h>
+
+ .file "startup.S"
+ .text
+ .arm
+FUNCTION(_start)
+ /*
+ * EFI_SYSTEM_TABLE and EFI_HANDLE are passed in r1/r0.
+ */
+ ldr ip, =EXT_C(grub_efi_image_handle)
+ str r0, [ip]
+ ldr ip, =EXT_C(grub_efi_system_table)
+ str r1, [ip]
+ ldr ip, =EXT_C(grub_main)
+ bx ip
+ END
diff --git a/grub-core/kern/arm/startup.S b/grub-core/kern/arm/startup.S
new file mode 100644
index 0000000..3946fe8
--- /dev/null
+++ b/grub-core/kern/arm/startup.S
@@ -0,0 +1,177 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2013 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/offsets.h>
+#include <grub/symbol.h>
+#include <grub/machine/kernel.h>
+
+/*
+ * GRUB is called from U-Boot as a Linux Kernel type image, which
+ * means among other things that it always enters in ARM state.
+ *
+ * coreboot starts in ARM mode as well.
+ *
+ * Overview of GRUB image layout:
+ *
+ * _start:
+ * Entry point (1 ARM branch instruction, to "codestart")
+ * grub_total_module_size:
+ * Data field: Size of included module blob
+ * (when generated by grub-mkimage)
+ * codestart:
+ * Remainder of statically-linked executable code and data.
+ * __bss_start:
+ * Start of included module blob.
+ * Also where global/static variables are located.
+ * _end:
+ * End of bss region (but not necessarily module blob).
+ * <stack>:
+ * <modules>:
+ * Loadable modules, post relocation.
+ * <heap>:
+ */
+
+ .text
+ .arm
+FUNCTION(_start)
+ b codestart
+
+ @ Size of final image integrated module blob - set by grub-mkimage
+ .org _start + GRUB_KERNEL_MACHINE_TOTAL_MODULE_SIZE
+VARIABLE(grub_total_module_size)
+ .long 0
+
+VARIABLE(grub_modbase)
+ .long 0
+bss_start_ptr:
+ .long EXT_C(__bss_start)
+end_ptr:
+ .long EXT_C(_end)
+
+ @ Memory map at start:
+ @ * text+data
+ @ * list relocations
+ @ * modules
+ @ Before we enter C, we need to apply the relocations
+ @ and get following map:
+ @ * text+data
+ @ * BSS (cleared)
+ @ * stack
+ @ * modules
+ @
+ @ To make things easier we ensure
+ @ that BSS+stack is larger than list of relocations
+ @ by increasing stack if necessarry.
+ @ This allows us to always unconditionally copy backwards
+ @ Currently list of relocations is ~5K and stack is set
+ @ to be at least 256K
+
+FUNCTION(codestart)
+ @ Store context: Machine ID, atags/dtb, ...
+ @ U-Boot API signature is stored on the U-Boot heap
+ @ Stack pointer used as start address for signature probing
+ mov r12, sp
+ adr sp, entry_state
+ push {r0-r12,lr} @ store U-Boot context (sp in r12)
+
+ adr r1, _start
+ ldr r0, bss_start_ptr @ src
+ add r0, r0, r1
+
+ add r0, r0, #(GRUB_KERNEL_MACHINE_MOD_ALIGN - 1)
+ mvn r2, #(GRUB_KERNEL_MACHINE_MOD_ALIGN - 1)
+ and r0, r0, r2
+1:
+ ldr r3, [r0], #4 @load next offset
+ @ both -2 and -1 are treated the same as we have only one type of relocs
+ @ -2 means "end of this type of relocs" and -1 means "end of all relocs"
+ add r2, r3, #2
+ cmp r2, #1
+ bls reloc_done
+ @ Adjust next offset
+ ldr r2, [r3, r1]
+ add r2, r2, r1
+ str r2, [r3, r1]
+ b 1b
+
+reloc_done:
+
+ @ Modules have been stored as a blob
+ @ they need to be manually relocated to _end
+ add r0, r0, #(GRUB_KERNEL_MACHINE_MOD_ALIGN - 1)
+ mvn r1, #(GRUB_KERNEL_MACHINE_MOD_ALIGN - 1)
+ and r0, r0, r1 @ src = aligned end of relocations
+
+ ldr r1, end_ptr @ dst = End of BSS
+ ldr r2, grub_total_module_size @ blob size
+
+ add r1, r1, #GRUB_KERNEL_MACHINE_STACK_SIZE
+ and r1, r1, #~0x7 @ Ensure 8-byte alignment
+
+ sub sp, r1, #8
+ add r1, r1, #1024
+
+ str r1, EXT_C(grub_modbase)
+
+ /* Coreboot already places modules at right place. */
+#ifndef GRUB_MACHINE_COREBOOT
+ add r1, r1, r2
+ add r0, r0, r2
+ sub r1, r1, #4
+ sub r0, r0, #4
+
+1: ldr r3, [r0], #-4 @ r3 = *src--
+ str r3, [r1], #-4 @ *dst-- = r3
+ subs r2, #4 @ remaining -= 4
+ bne 1b @ while remaining != 0
+#endif
+
+ @ Since we _are_ the C run-time, we need to manually zero the BSS
+ @ region before continuing
+ ldr r0, bss_start_ptr @ zero from here
+ @ If unaligned, bytewise zero until base address aligned.
+ mov r2, #0
+1: tst r0, #3
+ beq 2f
+ strb r2, [r0], #1
+ b 1b
+2: ldr r1, end_ptr @ to here
+1: str r2, [r0], #4
+ cmp r0, r1
+ bne 1b
+
+ b EXT_C(grub_main)
+
+ .align 3
+@ U-boot/coreboot context stack space
+VARIABLE(grub_arm_saved_registers)
+ .long 0 @ r0
+ .long 0 @ r1
+ .long 0 @ r2
+ .long 0 @ r3
+ .long 0 @ r4
+ .long 0 @ r5
+ .long 0 @ r6
+ .long 0 @ r7
+ .long 0 @ r8
+ .long 0 @ r9
+ .long 0 @ r10
+ .long 0 @ r11
+ .long 0 @ sp
+ .long 0 @ lr
+entry_state:
diff --git a/grub-core/kern/arm/uboot/init.c b/grub-core/kern/arm/uboot/init.c
new file mode 100644
index 0000000..2a6aa3f
--- /dev/null
+++ b/grub-core/kern/arm/uboot/init.c
@@ -0,0 +1,70 @@
+/* init.c - generic U-Boot initialization and finalization */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/uboot/uboot.h>
+#include <grub/arm/startup.h>
+#include <grub/uboot/api_public.h>
+
+extern int (*grub_uboot_syscall_ptr) (int, int *, ...);
+
+grub_uint32_t
+grub_uboot_get_machine_type (void)
+{
+ return grub_arm_saved_registers.r[1];
+}
+
+grub_addr_t
+grub_uboot_get_boot_data (void)
+{
+ return grub_arm_saved_registers.r[2];
+}
+
+int
+grub_uboot_api_init (void)
+{
+ struct api_signature *start, *end;
+ struct api_signature *p;
+ grub_addr_t grub_uboot_search_hint = grub_arm_saved_registers.sp;
+ if (grub_uboot_search_hint)
+ {
+ /* Extended search range to work around Trim Slice U-Boot issue */
+ start = (struct api_signature *) ((grub_uboot_search_hint & ~0x000fffff)
+ - 0x00500000);
+ end =
+ (struct api_signature *) ((grub_addr_t) start + UBOOT_API_SEARCH_LEN -
+ API_SIG_MAGLEN + 0x00500000);
+ }
+ else
+ {
+ start = 0;
+ end = (struct api_signature *) (256 * 1024 * 1024);
+ }
+
+ /* Structure alignment is (at least) 8 bytes */
+ for (p = start; p < end; p = (void *) ((grub_addr_t) p + 8))
+ {
+ if (grub_memcmp (&(p->magic), API_SIG_MAGIC, API_SIG_MAGLEN) == 0)
+ {
+ grub_uboot_syscall_ptr = p->syscall;
+ return p->version;
+ }
+ }
+
+ return 0;
+}
diff --git a/grub-core/kern/arm/uboot/uboot.S b/grub-core/kern/arm/uboot/uboot.S
new file mode 100644
index 0000000..d128775
--- /dev/null
+++ b/grub-core/kern/arm/uboot/uboot.S
@@ -0,0 +1,73 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2013 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/offsets.h>
+#include <grub/symbol.h>
+#include <grub/machine/kernel.h>
+
+ /*
+ * uboot_syscall():
+ * This function is effectively a veneer, so it cannot
+ * modify the stack or corrupt any registers other than
+ * r12 (ip). Furthermore it needs to restore r8 for
+ * U-Boot (Global Data Pointer) and preserve it for Grub.
+ */
+FUNCTION(grub_uboot_syscall)
+ str r8, transition_space
+ str lr, transition_space + 4
+ str r9, transition_space + 8
+
+ ldr ip, saved_registers_ptr
+ ldr r8, [ip, #4 * 8]
+ ldr r9, [ip, #4 * 9]
+
+ bl do_syscall
+
+ ldr r8, transition_space
+ ldr lr, transition_space + 4
+ ldr r9, transition_space + 8
+
+ bx lr
+do_syscall:
+
+ ldr ip, grub_uboot_syscall_ptr
+ bx ip
+
+FUNCTION(grub_uboot_return)
+ ldr ip, saved_registers_ptr
+ ldr sp, [ip, #4 * 4]
+ pop {r4-r12, lr}
+ mov sp, r12
+ bx lr
+
+
+ .align 3
+
+@ GRUB context stack space
+transition_space:
+ .long 0 @ r8
+ .long 0 @ lr
+ .long 0 @ r9
+
+saved_registers_ptr:
+ .long EXT_C(grub_arm_saved_registers)
+
+VARIABLE(grub_uboot_syscall_ptr)
+ .long 0 @
+
+ END
diff --git a/grub-core/kern/arm64/cache.c b/grub-core/kern/arm64/cache.c
new file mode 100644
index 0000000..b84383d
--- /dev/null
+++ b/grub-core/kern/arm64/cache.c
@@ -0,0 +1,63 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2013 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/cache.h>
+#include <grub/misc.h>
+
+static grub_int64_t dlinesz;
+static grub_int64_t ilinesz;
+
+/* Prototypes for asm functions. */
+void grub_arch_clean_dcache_range (grub_addr_t beg, grub_addr_t end,
+ grub_uint64_t line_size);
+void grub_arch_invalidate_icache_range (grub_addr_t beg, grub_addr_t end,
+ grub_uint64_t line_size);
+
+static void
+probe_caches (void)
+{
+ grub_uint64_t cache_type;
+
+ /* Read Cache Type Register */
+ asm volatile ("mrs %0, ctr_el0": "=r"(cache_type));
+
+ dlinesz = 4 << ((cache_type >> 16) & 0xf);
+ ilinesz = 4 << (cache_type & 0xf);
+
+ grub_dprintf("cache", "D$ line size: %lld\n", (long long) dlinesz);
+ grub_dprintf("cache", "I$ line size: %lld\n", (long long) ilinesz);
+}
+
+void
+grub_arch_sync_caches (void *address, grub_size_t len)
+{
+ grub_uint64_t start, end, max_align;
+
+ if (dlinesz == 0)
+ probe_caches();
+ if (dlinesz == 0)
+ grub_fatal ("Unknown cache line size!");
+
+ max_align = dlinesz > ilinesz ? dlinesz : ilinesz;
+
+ start = ALIGN_DOWN ((grub_uint64_t) address, max_align);
+ end = ALIGN_UP ((grub_uint64_t) address + len, max_align);
+
+ grub_arch_clean_dcache_range (start, end, dlinesz);
+ grub_arch_invalidate_icache_range (start, end, ilinesz);
+}
diff --git a/grub-core/kern/arm64/cache_flush.S b/grub-core/kern/arm64/cache_flush.S
new file mode 100644
index 0000000..e064f7e
--- /dev/null
+++ b/grub-core/kern/arm64/cache_flush.S
@@ -0,0 +1,55 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2013 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/symbol.h>
+
+ .file "cache_flush.S"
+ .text
+
+/*
+ * Simple cache maintenance functions
+ */
+
+// x0 - *beg (inclusive)
+// x1 - *end (exclusive)
+// x2 - line size
+FUNCTION(grub_arch_clean_dcache_range)
+ // Clean data cache for range to point-of-unification
+1: cmp x0, x1
+ b.ge 2f
+ dc cvau, x0 // Clean Virtual Address to PoU
+ add x0, x0, x2 // Next line
+ b 1b
+2: dsb ish
+ isb
+ ret
+
+// x0 - *beg (inclusive)
+// x1 - *end (exclusive)
+// x2 - line size
+FUNCTION(grub_arch_invalidate_icache_range)
+ // Invalidate instruction cache for range to point-of-unification
+1: cmp x0, x1
+ b.ge 2f
+ ic ivau, x0 // Invalidate Virtual Address to PoU
+ add x0, x0, x2 // Next line
+ b 1b
+ // Branch predictor invalidation not needed on AArch64
+2: dsb ish
+ isb
+ ret
diff --git a/grub-core/kern/arm64/dl.c b/grub-core/kern/arm64/dl.c
new file mode 100644
index 0000000..512e5a8
--- /dev/null
+++ b/grub-core/kern/arm64/dl.c
@@ -0,0 +1,198 @@
+/* dl.c - arch-dependent part of loadable module support */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2013 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/dl.h>
+#include <grub/elf.h>
+#include <grub/misc.h>
+#include <grub/err.h>
+#include <grub/mm.h>
+#include <grub/i18n.h>
+#include <grub/cpu/reloc.h>
+
+#define LDR 0x58000050
+#define BR 0xd61f0200
+
+
+/*
+ * Check if EHDR is a valid ELF header.
+ */
+grub_err_t
+grub_arch_dl_check_header (void *ehdr)
+{
+ Elf_Ehdr *e = ehdr;
+
+ /* Check the magic numbers. */
+ if (e->e_ident[EI_CLASS] != ELFCLASS64
+ || e->e_ident[EI_DATA] != ELFDATA2LSB || e->e_machine != EM_AARCH64)
+ return grub_error (GRUB_ERR_BAD_OS,
+ N_("invalid arch-dependent ELF magic"));
+
+ return GRUB_ERR_NONE;
+}
+
+#pragma GCC diagnostic ignored "-Wcast-align"
+
+/*
+ * Unified function for both REL and RELA
+ */
+grub_err_t
+grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
+ Elf_Shdr *s, grub_dl_segment_t seg)
+{
+ Elf_Rel *rel, *max;
+ unsigned unmatched_adr_got_page = 0;
+
+ for (rel = (Elf_Rel *) ((char *) ehdr + s->sh_offset),
+ max = (Elf_Rel *) ((char *) rel + s->sh_size);
+ rel < max;
+ rel = (Elf_Rel *) ((char *) rel + s->sh_entsize))
+ {
+ Elf_Sym *sym;
+ void *place;
+ grub_uint64_t sym_addr;
+
+ if (rel->r_offset >= seg->size)
+ return grub_error (GRUB_ERR_BAD_MODULE,
+ "reloc offset is out of the segment");
+
+ sym = (Elf_Sym *) ((char *) mod->symtab
+ + mod->symsize * ELF_R_SYM (rel->r_info));
+
+ sym_addr = sym->st_value;
+ if (s->sh_type == SHT_RELA)
+ sym_addr += ((Elf_Rela *) rel)->r_addend;
+
+ place = (void *) ((grub_addr_t) seg->addr + rel->r_offset);
+
+ switch (ELF_R_TYPE (rel->r_info))
+ {
+ case R_AARCH64_ABS64:
+ {
+ grub_uint64_t *abs_place = place;
+
+ grub_dprintf ("dl", " reloc_abs64 %p => 0x%016llx\n",
+ place, (unsigned long long) sym_addr);
+
+ *abs_place = (grub_uint64_t) sym_addr;
+ }
+ break;
+ case R_AARCH64_ADD_ABS_LO12_NC:
+ grub_arm64_set_abs_lo12 (place, sym_addr);
+ break;
+ case R_AARCH64_LDST64_ABS_LO12_NC:
+ grub_arm64_set_abs_lo12_ldst64 (place, sym_addr);
+ break;
+ case R_AARCH64_CALL26:
+ case R_AARCH64_JUMP26:
+ {
+ grub_int64_t offset = sym_addr - (grub_uint64_t) place;
+
+ if (!grub_arm_64_check_xxxx26_offset (offset))
+ {
+ struct grub_arm64_trampoline *tp = mod->trampptr;
+ mod->trampptr = tp + 1;
+ tp->ldr = LDR;
+ tp->br = BR;
+ tp->addr = sym_addr;
+ offset = (grub_uint8_t *) tp - (grub_uint8_t *) place;
+ }
+
+ if (!grub_arm_64_check_xxxx26_offset (offset))
+ return grub_error (GRUB_ERR_BAD_MODULE,
+ "trampoline out of range");
+
+ grub_arm64_set_xxxx26_offset (place, offset);
+ }
+ break;
+ case R_AARCH64_PREL32:
+ {
+ grub_int64_t value;
+ Elf64_Word *addr32 = place;
+ value = ((grub_int32_t) *addr32) + sym_addr -
+ (Elf64_Xword) (grub_addr_t) seg->addr - rel->r_offset;
+ if (value != (grub_int32_t) value)
+ return grub_error (GRUB_ERR_BAD_MODULE, "relocation out of range");
+ grub_dprintf("dl", " reloc_prel32 %p => 0x%016llx\n",
+ place, (unsigned long long) sym_addr);
+ *addr32 = value;
+ }
+ break;
+ case R_AARCH64_ADR_GOT_PAGE:
+ {
+ grub_uint64_t *gp = mod->gotptr;
+ Elf_Rela *rel2;
+ grub_int64_t gpoffset = ((grub_uint64_t) gp & ~0xfffULL) - (((grub_uint64_t) place) & ~0xfffULL);
+ *gp = (grub_uint64_t) sym_addr;
+ mod->gotptr = gp + 1;
+ unmatched_adr_got_page++;
+ grub_dprintf("dl", " reloc_got %p => 0x%016llx (0x%016llx)\n",
+ place, (unsigned long long) sym_addr, (unsigned long long) gp);
+ if (!grub_arm64_check_hi21_signed (gpoffset))
+ return grub_error (GRUB_ERR_BAD_MODULE,
+ "HI21 out of range");
+ grub_arm64_set_hi21(place, gpoffset);
+ for (rel2 = (Elf_Rela *) ((char *) rel + s->sh_entsize);
+ rel2 < (Elf_Rela *) max;
+ rel2 = (Elf_Rela *) ((char *) rel2 + s->sh_entsize))
+ if (ELF_R_SYM (rel2->r_info)
+ == ELF_R_SYM (rel->r_info)
+ && ((Elf_Rela *) rel)->r_addend == rel2->r_addend
+ && ELF_R_TYPE (rel2->r_info) == R_AARCH64_LD64_GOT_LO12_NC)
+ {
+ grub_arm64_set_abs_lo12_ldst64 ((void *) ((grub_addr_t) seg->addr + rel2->r_offset),
+ (grub_uint64_t)gp);
+ break;
+ }
+ if (rel2 >= (Elf_Rela *) max)
+ return grub_error (GRUB_ERR_BAD_MODULE,
+ "ADR_GOT_PAGE without matching LD64_GOT_LO12_NC");
+ }
+ break;
+ case R_AARCH64_LD64_GOT_LO12_NC:
+ if (unmatched_adr_got_page == 0)
+ return grub_error (GRUB_ERR_BAD_MODULE,
+ "LD64_GOT_LO12_NC without matching ADR_GOT_PAGE");
+ unmatched_adr_got_page--;
+ break;
+ case R_AARCH64_ADR_PREL_PG_HI21:
+ {
+ grub_int64_t offset = (sym_addr & ~0xfffULL) - (((grub_uint64_t) place) & ~0xfffULL);
+
+ if (!grub_arm64_check_hi21_signed (offset))
+ return grub_error (GRUB_ERR_BAD_MODULE,
+ "HI21 out of range");
+
+ grub_arm64_set_hi21 (place, offset);
+ }
+ break;
+
+ default:
+ {
+ char rel_info[17]; /* log16(2^64) = 16, plus NUL. */
+
+ grub_snprintf (rel_info, sizeof (rel_info) - 1, "%" PRIxGRUB_UINT64_T,
+ ELF_R_TYPE (rel->r_info));
+ return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
+ N_("relocation 0x%s is not implemented yet"), rel_info);
+ }
+ }
+ }
+
+ return GRUB_ERR_NONE;
+}
diff --git a/grub-core/kern/arm64/dl_helper.c b/grub-core/kern/arm64/dl_helper.c
new file mode 100644
index 0000000..e00c198
--- /dev/null
+++ b/grub-core/kern/arm64/dl_helper.c
@@ -0,0 +1,134 @@
+/* dl_helper.c - relocation helper functions for modules and grub-mkimage */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2013 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/dl.h>
+#include <grub/elf.h>
+#include <grub/misc.h>
+#include <grub/err.h>
+#include <grub/mm.h>
+#include <grub/i18n.h>
+#include <grub/arm64/reloc.h>
+
+/*
+ * grub_arm64_reloc_xxxx26():
+ *
+ * JUMP26/CALL26 relocations for B and BL instructions.
+ */
+
+int
+grub_arm_64_check_xxxx26_offset (grub_int64_t offset)
+{
+ const grub_ssize_t offset_low = -(1 << 27), offset_high = (1 << 27) - 1;
+
+ if ((offset < offset_low) || (offset > offset_high))
+ return 0;
+ return 1;
+}
+
+void
+grub_arm64_set_xxxx26_offset (grub_uint32_t *place, grub_int64_t offset)
+{
+ const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xfc000000);
+
+ grub_dprintf ("dl", " reloc_xxxx64 %p %c= 0x%llx\n",
+ place, offset > 0 ? '+' : '-',
+ offset < 0 ? (long long) -(unsigned long long) offset : offset);
+
+ *place &= insmask;
+ *place |= grub_cpu_to_le32 (offset >> 2) & ~insmask;
+}
+
+int
+grub_arm64_check_hi21_signed (grub_int64_t offset)
+{
+ if (offset != (grub_int64_t)(grub_int32_t)offset)
+ return 0;
+ return 1;
+}
+
+void
+grub_arm64_set_hi21 (grub_uint32_t *place, grub_int64_t offset)
+{
+ const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0x9f00001f);
+ grub_uint32_t val;
+
+ offset >>= 12;
+
+ val = ((offset & 3) << 29) | (((offset >> 2) & 0x7ffff) << 5);
+
+ *place &= insmask;
+ *place |= grub_cpu_to_le32 (val) & ~insmask;
+}
+
+void
+grub_arm64_set_abs_lo12 (grub_uint32_t *place, grub_int64_t target)
+{
+ const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xffc003ff);
+
+ *place &= insmask;
+ *place |= grub_cpu_to_le32 (target << 10) & ~insmask;
+}
+
+void
+grub_arm64_set_abs_lo12_ldst64 (grub_uint32_t *place, grub_int64_t target)
+{
+ const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xfff803ff);
+
+ *place &= insmask;
+ *place |= grub_cpu_to_le32 (target << 7) & ~insmask;
+}
+
+#pragma GCC diagnostic ignored "-Wcast-align"
+
+grub_err_t
+grub_arm64_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp,
+ grub_size_t *got)
+{
+ const Elf64_Ehdr *e = ehdr;
+ const Elf64_Shdr *s;
+ unsigned i;
+
+ *tramp = 0;
+ *got = 0;
+
+ for (i = 0, s = (Elf64_Shdr *) ((char *) e + grub_le_to_cpu64 (e->e_shoff));
+ i < grub_le_to_cpu16 (e->e_shnum);
+ i++, s = (Elf64_Shdr *) ((char *) s + grub_le_to_cpu16 (e->e_shentsize)))
+ if (s->sh_type == grub_cpu_to_le32_compile_time (SHT_REL)
+ || s->sh_type == grub_cpu_to_le32_compile_time (SHT_RELA))
+ {
+ const Elf64_Rela *rel, *max;
+
+ for (rel = (Elf64_Rela *) ((char *) e + grub_le_to_cpu64 (s->sh_offset)),
+ max = (const Elf64_Rela *) ((char *) rel + grub_le_to_cpu64 (s->sh_size));
+ rel < max; rel = (const Elf64_Rela *) ((char *) rel + grub_le_to_cpu64 (s->sh_entsize)))
+ switch (ELF64_R_TYPE (rel->r_info))
+ {
+ case R_AARCH64_CALL26:
+ case R_AARCH64_JUMP26:
+ *tramp += sizeof (struct grub_arm64_trampoline);
+ break;
+ case R_AARCH64_ADR_GOT_PAGE:
+ *got += 8;
+ break;
+ }
+ }
+
+ return GRUB_ERR_NONE;
+}
diff --git a/grub-core/kern/arm64/efi/init.c b/grub-core/kern/arm64/efi/init.c
new file mode 100644
index 0000000..5010cae
--- /dev/null
+++ b/grub-core/kern/arm64/efi/init.c
@@ -0,0 +1,63 @@
+/* init.c - initialize an arm-based EFI system */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2013 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/env.h>
+#include <grub/kernel.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/time.h>
+#include <grub/efi/efi.h>
+#include <grub/loader.h>
+
+static grub_uint64_t timer_frequency_in_khz;
+
+static grub_uint64_t
+grub_efi_get_time_ms (void)
+{
+ grub_uint64_t tmr;
+ asm volatile("mrs %0, cntvct_el0" : "=r" (tmr));
+
+ return tmr / timer_frequency_in_khz;
+}
+
+
+void
+grub_machine_init (void)
+{
+ grub_uint64_t timer_frequency;
+
+ grub_efi_init ();
+
+ asm volatile("mrs %0, cntfrq_el0" : "=r" (timer_frequency));
+ timer_frequency_in_khz = timer_frequency / 1000;
+
+ grub_install_get_time_ms (grub_efi_get_time_ms);
+}
+
+void
+grub_machine_fini (int flags)
+{
+ if (!(flags & GRUB_LOADER_FLAG_NORETURN))
+ return;
+
+ grub_efi_fini ();
+
+ if (!(flags & GRUB_LOADER_FLAG_EFI_KEEP_ALLOCATED_MEMORY))
+ grub_efi_memory_fini ();
+}
diff --git a/grub-core/kern/arm64/efi/startup.S b/grub-core/kern/arm64/efi/startup.S
new file mode 100644
index 0000000..666a7ee
--- /dev/null
+++ b/grub-core/kern/arm64/efi/startup.S
@@ -0,0 +1,39 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2013 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/symbol.h>
+
+ .file "startup.S"
+ .text
+FUNCTION(_start)
+ /*
+ * EFI_SYSTEM_TABLE and EFI_HANDLE are passed in x1/x0.
+ */
+ ldr x2, efi_image_handle_val
+ str x0, [x2]
+ ldr x2, efi_system_table_val
+ str x1, [x2]
+ ldr x2, grub_main_val
+ br x2
+grub_main_val:
+ .quad EXT_C(grub_main)
+efi_system_table_val:
+ .quad EXT_C(grub_efi_system_table)
+efi_image_handle_val:
+ .quad EXT_C(grub_efi_image_handle)
+
diff --git a/grub-core/kern/buffer.c b/grub-core/kern/buffer.c
new file mode 100644
index 0000000..9f5f8b8
--- /dev/null
+++ b/grub-core/kern/buffer.c
@@ -0,0 +1,117 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2021 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/buffer.h>
+#include <grub/err.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/safemath.h>
+#include <grub/types.h>
+
+grub_buffer_t
+grub_buffer_new (grub_size_t sz)
+{
+ struct grub_buffer *ret;
+
+ ret = (struct grub_buffer *) grub_malloc (sizeof (*ret));
+ if (ret == NULL)
+ return NULL;
+
+ ret->data = (grub_uint8_t *) grub_malloc (sz);
+ if (ret->data == NULL)
+ {
+ grub_free (ret);
+ return NULL;
+ }
+
+ ret->sz = sz;
+ ret->pos = 0;
+ ret->used = 0;
+
+ return ret;
+}
+
+void
+grub_buffer_free (grub_buffer_t buf)
+{
+ grub_free (buf->data);
+ grub_free (buf);
+}
+
+grub_err_t
+grub_buffer_ensure_space (grub_buffer_t buf, grub_size_t req)
+{
+ grub_uint8_t *d;
+ grub_size_t newsz = 1;
+
+ /* Is the current buffer size adequate? */
+ if (buf->sz >= req)
+ return GRUB_ERR_NONE;
+
+ /* Find the smallest power-of-2 size that satisfies the request. */
+ while (newsz < req)
+ {
+ if (newsz == 0)
+ return grub_error (GRUB_ERR_OUT_OF_RANGE,
+ N_("requested buffer size is too large"));
+ newsz <<= 1;
+ }
+
+ d = (grub_uint8_t *) grub_realloc (buf->data, newsz);
+ if (d == NULL)
+ return grub_errno;
+
+ buf->data = d;
+ buf->sz = newsz;
+
+ return GRUB_ERR_NONE;
+}
+
+void *
+grub_buffer_take_data (grub_buffer_t buf)
+{
+ void *data = buf->data;
+
+ buf->data = NULL;
+ buf->sz = buf->pos = buf->used = 0;
+
+ return data;
+}
+
+void
+grub_buffer_reset (grub_buffer_t buf)
+{
+ buf->pos = buf->used = 0;
+}
+
+grub_err_t
+grub_buffer_advance_read_pos (grub_buffer_t buf, grub_size_t n)
+{
+ grub_size_t newpos;
+
+ if (grub_add (buf->pos, n, &newpos))
+ return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
+
+ if (newpos > buf->used)
+ return grub_error (GRUB_ERR_OUT_OF_RANGE,
+ N_("new read is position beyond the end of the written data"));
+
+ buf->pos = newpos;
+
+ return GRUB_ERR_NONE;
+}
diff --git a/grub-core/kern/command.c b/grub-core/kern/command.c
new file mode 100644
index 0000000..4aabcd4
--- /dev/null
+++ b/grub-core/kern/command.c
@@ -0,0 +1,111 @@
+/* command.c - support basic command */
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/lockdown.h>
+#include <grub/mm.h>
+#include <grub/command.h>
+
+grub_command_t grub_command_list;
+
+grub_command_t
+grub_register_command_prio (const char *name,
+ grub_command_func_t func,
+ const char *summary,
+ const char *description,
+ int prio)
+{
+ grub_command_t cmd;
+ int inactive = 0;
+
+ grub_command_t *p, q;
+
+ cmd = (grub_command_t) grub_zalloc (sizeof (*cmd));
+ if (! cmd)
+ return 0;
+
+ cmd->name = name;
+ cmd->func = func;
+ cmd->summary = (summary) ? summary : "";
+ cmd->description = description;
+
+ cmd->flags = 0;
+ cmd->prio = prio;
+
+ for (p = &grub_command_list, q = *p; q; p = &(q->next), q = q->next)
+ {
+ int r;
+
+ r = grub_strcmp (cmd->name, q->name);
+ if (r < 0)
+ break;
+ if (r > 0)
+ continue;
+
+ if (cmd->prio >= (q->prio & GRUB_COMMAND_PRIO_MASK))
+ {
+ q->prio &= ~GRUB_COMMAND_FLAG_ACTIVE;
+ break;
+ }
+
+ inactive = 1;
+ }
+
+ *p = cmd;
+ cmd->next = q;
+ if (q)
+ q->prev = &cmd->next;
+ cmd->prev = p;
+
+ if (! inactive)
+ cmd->prio |= GRUB_COMMAND_FLAG_ACTIVE;
+
+ return cmd;
+}
+
+static grub_err_t
+grub_cmd_lockdown (grub_command_t cmd __attribute__ ((unused)),
+ int argc __attribute__ ((unused)),
+ char **argv __attribute__ ((unused)))
+
+{
+ return grub_error (GRUB_ERR_ACCESS_DENIED,
+ N_("%s: the command is not allowed when lockdown is enforced"),
+ cmd->name);
+}
+
+grub_command_t
+grub_register_command_lockdown (const char *name,
+ grub_command_func_t func,
+ const char *summary,
+ const char *description)
+{
+ if (grub_is_lockdown () == GRUB_LOCKDOWN_ENABLED)
+ func = grub_cmd_lockdown;
+
+ return grub_register_command_prio (name, func, summary, description, 0);
+}
+
+void
+grub_unregister_command (grub_command_t cmd)
+{
+ if ((cmd->prio & GRUB_COMMAND_FLAG_ACTIVE) && (cmd->next))
+ cmd->next->prio |= GRUB_COMMAND_FLAG_ACTIVE;
+ grub_list_remove (GRUB_AS_LIST (cmd));
+ grub_free (cmd);
+}
diff --git a/grub-core/kern/compiler-rt.c b/grub-core/kern/compiler-rt.c
new file mode 100644
index 0000000..2057c2e
--- /dev/null
+++ b/grub-core/kern/compiler-rt.c
@@ -0,0 +1,464 @@
+/* compiler-rt.c - compiler helpers. */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010-2014 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/misc.h>
+#include <grub/compiler-rt.h>
+
+#ifndef GRUB_EMBED_DECOMPRESSOR
+void * GRUB_BUILTIN_ATTR
+memcpy (void *dest, const void *src, grub_size_t n)
+{
+ return grub_memmove (dest, src, n);
+}
+void * GRUB_BUILTIN_ATTR
+memmove (void *dest, const void *src, grub_size_t n)
+{
+ return grub_memmove (dest, src, n);
+}
+int GRUB_BUILTIN_ATTR
+memcmp (const void *s1, const void *s2, grub_size_t n)
+{
+ return grub_memcmp (s1, s2, n);
+}
+void * GRUB_BUILTIN_ATTR
+memset (void *s, int c, grub_size_t n)
+{
+ return grub_memset (s, c, n);
+}
+
+#ifdef __APPLE__
+
+void GRUB_BUILTIN_ATTR
+__bzero (void *s, grub_size_t n)
+{
+ grub_memset (s, 0, n);
+}
+
+#endif
+
+#if GRUB_DIVISION_IN_SOFTWARE
+
+grub_uint32_t
+__udivsi3 (grub_uint32_t a, grub_uint32_t b)
+{
+ return grub_divmod64 (a, b, 0);
+}
+
+grub_int32_t
+__divsi3 (grub_int32_t a, grub_int32_t b)
+{
+ return grub_divmod64s (a, b, 0);
+}
+
+grub_uint32_t
+__umodsi3 (grub_uint32_t a, grub_uint32_t b)
+{
+ grub_uint64_t ret;
+ grub_divmod64 (a, b, &ret);
+ return ret;
+}
+
+grub_int32_t
+__modsi3 (grub_int32_t a, grub_int32_t b)
+{
+ grub_int64_t ret;
+ grub_divmod64s (a, b, &ret);
+ return ret;
+}
+
+grub_uint64_t
+__udivdi3 (grub_uint64_t a, grub_uint64_t b)
+{
+ return grub_divmod64 (a, b, 0);
+}
+
+grub_uint64_t
+__umoddi3 (grub_uint64_t a, grub_uint64_t b)
+{
+ grub_uint64_t ret;
+ grub_divmod64 (a, b, &ret);
+ return ret;
+}
+
+grub_int64_t
+__divdi3 (grub_int64_t a, grub_int64_t b)
+{
+ return grub_divmod64s (a, b, 0);
+}
+
+grub_int64_t
+__moddi3 (grub_int64_t a, grub_int64_t b)
+{
+ grub_int64_t ret;
+ grub_divmod64s (a, b, &ret);
+ return ret;
+}
+
+#endif
+
+#endif
+
+#ifdef NEED_CTZDI2
+
+unsigned
+__ctzdi2 (grub_uint64_t x)
+{
+ unsigned ret = 0;
+ if (!x)
+ return 64;
+ if (!(x & 0xffffffff))
+ {
+ x >>= 32;
+ ret |= 32;
+ }
+ if (!(x & 0xffff))
+ {
+ x >>= 16;
+ ret |= 16;
+ }
+ if (!(x & 0xff))
+ {
+ x >>= 8;
+ ret |= 8;
+ }
+ if (!(x & 0xf))
+ {
+ x >>= 4;
+ ret |= 4;
+ }
+ if (!(x & 0x3))
+ {
+ x >>= 2;
+ ret |= 2;
+ }
+ if (!(x & 0x1))
+ {
+ x >>= 1;
+ ret |= 1;
+ }
+ return ret;
+}
+#endif
+
+#ifdef NEED_CTZSI2
+unsigned
+__ctzsi2 (grub_uint32_t x)
+{
+ unsigned ret = 0;
+ if (!x)
+ return 32;
+
+ if (!(x & 0xffff))
+ {
+ x >>= 16;
+ ret |= 16;
+ }
+ if (!(x & 0xff))
+ {
+ x >>= 8;
+ ret |= 8;
+ }
+ if (!(x & 0xf))
+ {
+ x >>= 4;
+ ret |= 4;
+ }
+ if (!(x & 0x3))
+ {
+ x >>= 2;
+ ret |= 2;
+ }
+ if (!(x & 0x1))
+ {
+ x >>= 1;
+ ret |= 1;
+ }
+ return ret;
+}
+
+#endif
+
+
+#if defined (__clang__) && !defined(GRUB_EMBED_DECOMPRESSOR)
+/* clang emits references to abort(). */
+void __attribute__ ((noreturn))
+abort (void)
+{
+ grub_fatal ("compiler abort");
+}
+#endif
+
+#if (defined (__MINGW32__) || defined (__CYGWIN__))
+void __register_frame_info (void)
+{
+}
+
+void __deregister_frame_info (void)
+{
+}
+
+void ___chkstk_ms (void)
+{
+}
+
+void __chkstk_ms (void)
+{
+}
+#endif
+
+union component64
+{
+ grub_uint64_t full;
+ struct
+ {
+#ifdef GRUB_CPU_WORDS_BIGENDIAN
+ grub_uint32_t high;
+ grub_uint32_t low;
+#else
+ grub_uint32_t low;
+ grub_uint32_t high;
+#endif
+ };
+};
+
+#if defined (__powerpc__) || defined (__arm__) || defined(__mips__) || \
+ (defined(__riscv) && (__riscv_xlen == 32))
+
+/* Based on libgcc2.c from gcc suite. */
+grub_uint64_t
+__lshrdi3 (grub_uint64_t u, int b)
+{
+ if (b == 0)
+ return u;
+
+ const union component64 uu = {.full = u};
+ const int bm = 32 - b;
+ union component64 w;
+
+ if (bm <= 0)
+ {
+ w.high = 0;
+ w.low = (grub_uint32_t) uu.high >> -bm;
+ }
+ else
+ {
+ const grub_uint32_t carries = (grub_uint32_t) uu.high << bm;
+
+ w.high = (grub_uint32_t) uu.high >> b;
+ w.low = ((grub_uint32_t) uu.low >> b) | carries;
+ }
+
+ return w.full;
+}
+
+/* Based on libgcc2.c from gcc suite. */
+grub_uint64_t
+__ashrdi3 (grub_uint64_t u, int b)
+{
+ if (b == 0)
+ return u;
+
+ const union component64 uu = {.full = u};
+ const int bm = 32 - b;
+ union component64 w;
+
+ if (bm <= 0)
+ {
+ /* w.high = 1..1 or 0..0 */
+ w.high = ((grub_int32_t) uu.high) >> (32 - 1);
+ w.low = ((grub_int32_t) uu.high) >> -bm;
+ }
+ else
+ {
+ const grub_uint32_t carries = ((grub_uint32_t) uu.high) << bm;
+
+ w.high = ((grub_int32_t) uu.high) >> b;
+ w.low = ((grub_uint32_t) uu.low >> b) | carries;
+ }
+
+ return w.full;
+}
+
+/* Based on libgcc2.c from gcc suite. */
+grub_uint64_t
+__ashldi3 (grub_uint64_t u, int b)
+{
+ if (b == 0)
+ return u;
+
+ const union component64 uu = {.full = u};
+ const int bm = 32 - b;
+ union component64 w;
+
+ if (bm <= 0)
+ {
+ w.low = 0;
+ w.high = (grub_uint32_t) uu.low << -bm;
+ }
+ else
+ {
+ const grub_uint32_t carries = (grub_uint32_t) uu.low >> bm;
+
+ w.low = (grub_uint32_t) uu.low << b;
+ w.high = ((grub_uint32_t) uu.high << b) | carries;
+ }
+
+ return w.full;
+}
+
+/* Based on libgcc2.c from gcc suite. */
+int
+__ucmpdi2 (grub_uint64_t a, grub_uint64_t b)
+{
+ union component64 ac, bc;
+ ac.full = a;
+ bc.full = b;
+
+ if (ac.high < bc.high)
+ return 0;
+ else if (ac.high > bc.high)
+ return 2;
+
+ if (ac.low < bc.low)
+ return 0;
+ else if (ac.low > bc.low)
+ return 2;
+ return 1;
+}
+
+#endif
+
+#if defined (__powerpc__) || defined(__mips__) || defined(__sparc__) || \
+ defined(__arm__) || defined(__riscv)
+
+/* Based on libgcc2.c from gcc suite. */
+grub_uint32_t
+__bswapsi2 (grub_uint32_t u)
+{
+ return ((((u) & 0xff000000) >> 24)
+ | (((u) & 0x00ff0000) >> 8)
+ | (((u) & 0x0000ff00) << 8)
+ | (((u) & 0x000000ff) << 24));
+}
+
+/* Based on libgcc2.c from gcc suite. */
+grub_uint64_t
+__bswapdi2 (grub_uint64_t u)
+{
+ return ((((u) & 0xff00000000000000ull) >> 56)
+ | (((u) & 0x00ff000000000000ull) >> 40)
+ | (((u) & 0x0000ff0000000000ull) >> 24)
+ | (((u) & 0x000000ff00000000ull) >> 8)
+ | (((u) & 0x00000000ff000000ull) << 8)
+ | (((u) & 0x0000000000ff0000ull) << 24)
+ | (((u) & 0x000000000000ff00ull) << 40)
+ | (((u) & 0x00000000000000ffull) << 56));
+}
+
+
+#endif
+
+#ifdef __arm__
+grub_uint32_t
+__aeabi_uidiv (grub_uint32_t a, grub_uint32_t b)
+ __attribute__ ((alias ("__udivsi3")));
+grub_int32_t
+__aeabi_idiv (grub_int32_t a, grub_int32_t b)
+ __attribute__ ((alias ("__divsi3")));
+void *__aeabi_memcpy (void *dest, const void *src, grub_size_t n)
+ __attribute__ ((alias ("grub_memcpy")));
+void *__aeabi_memcpy4 (void *dest, const void *src, grub_size_t n)
+ __attribute__ ((alias ("grub_memcpy")));
+void *__aeabi_memcpy8 (void *dest, const void *src, grub_size_t n)
+ __attribute__ ((alias ("grub_memcpy")));
+void *__aeabi_memset (void *s, int c, grub_size_t n)
+ __attribute__ ((alias ("memset")));
+
+void
+__aeabi_memclr (void *s, grub_size_t n)
+{
+ grub_memset (s, 0, n);
+}
+
+void __aeabi_memclr4 (void *s, grub_size_t n)
+ __attribute__ ((alias ("__aeabi_memclr")));
+void __aeabi_memclr8 (void *s, grub_size_t n)
+ __attribute__ ((alias ("__aeabi_memclr")));
+
+int
+__aeabi_ulcmp (grub_uint64_t a, grub_uint64_t b)
+{
+ return __ucmpdi2 (a, b) - 1;
+}
+
+grub_uint64_t
+__aeabi_lasr (grub_uint64_t u, int b)
+ __attribute__ ((alias ("__ashrdi3")));
+grub_uint64_t
+__aeabi_llsr (grub_uint64_t u, int b)
+ __attribute__ ((alias ("__lshrdi3")));
+
+grub_uint64_t
+__aeabi_llsl (grub_uint64_t u, int b)
+ __attribute__ ((alias ("__ashldi3")));
+
+#endif
+
+#if defined(__mips__) || defined(__riscv) || defined(__sparc__)
+/* Based on libgcc from gcc suite. */
+int
+__clzsi2 (grub_uint32_t val)
+{
+ int i = 32;
+ int j = 16;
+ int temp;
+
+ for (; j; j >>= 1)
+ {
+ if ((temp = val) >> j)
+ {
+ if (j == 1)
+ {
+ return (i - 2);
+ }
+ else
+ {
+ i -= j;
+ val = temp;
+ }
+ }
+ }
+ return (i - val);
+}
+#endif
+
+#if defined(__mips__) || defined(__riscv) || defined(__sparc__)
+int
+__clzdi2 (grub_uint64_t val)
+{
+ if (val >> 32)
+ {
+ return __clzsi2 (val >> 32);
+ }
+ else
+ {
+ return __clzsi2 (val) + 32;
+ }
+}
+#endif
diff --git a/grub-core/kern/coreboot/cbtable.c b/grub-core/kern/coreboot/cbtable.c
new file mode 100644
index 0000000..aec63db
--- /dev/null
+++ b/grub-core/kern/coreboot/cbtable.c
@@ -0,0 +1,72 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2007,2008,2013 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/i386/coreboot/memory.h>
+#include <grub/coreboot/lbio.h>
+#include <grub/types.h>
+#include <grub/err.h>
+#include <grub/misc.h>
+#include <grub/dl.h>
+
+#pragma GCC diagnostic ignored "-Wcast-align"
+
+/* Helper for grub_linuxbios_table_iterate. */
+int
+grub_linuxbios_check_signature (grub_linuxbios_table_header_t tbl_header)
+{
+ if (! grub_memcmp (tbl_header->signature, "LBIO", 4))
+ return 1;
+
+ return 0;
+}
+
+grub_err_t
+grub_linuxbios_table_iterate (int (*hook) (grub_linuxbios_table_item_t,
+ void *),
+ void *hook_data)
+{
+ grub_linuxbios_table_header_t table_header = grub_linuxbios_get_tables ();
+ grub_linuxbios_table_item_t table_item;
+
+ if (!table_header)
+ return 0;
+
+signature_found:
+
+ table_item =
+ (grub_linuxbios_table_item_t) ((char *) table_header +
+ table_header->header_size);
+ for (; table_item < (grub_linuxbios_table_item_t) ((char *) table_header
+ + table_header->header_size
+ + table_header->table_size);
+ table_item = (grub_linuxbios_table_item_t) ((char *) table_item + table_item->size))
+ {
+ if (table_item->tag == GRUB_LINUXBIOS_MEMBER_LINK
+ && grub_linuxbios_check_signature ((grub_linuxbios_table_header_t) (grub_addr_t)
+ *(grub_uint64_t *) (table_item + 1)))
+ {
+ table_header = (grub_linuxbios_table_header_t) (grub_addr_t)
+ *(grub_uint64_t *) (table_item + 1);
+ goto signature_found;
+ }
+ if (hook (table_item, hook_data))
+ return 1;
+ }
+
+ return 0;
+}
diff --git a/grub-core/kern/coreboot/mmap.c b/grub-core/kern/coreboot/mmap.c
new file mode 100644
index 0000000..caf8f7c
--- /dev/null
+++ b/grub-core/kern/coreboot/mmap.c
@@ -0,0 +1,100 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2007,2008,2013 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/memory.h>
+#include <grub/coreboot/lbio.h>
+#include <grub/types.h>
+#include <grub/err.h>
+#include <grub/misc.h>
+
+/* Context for grub_machine_mmap_iterate. */
+struct grub_machine_mmap_iterate_ctx
+{
+ grub_memory_hook_t hook;
+ void *hook_data;
+};
+
+#define GRUB_MACHINE_MEMORY_BADRAM 5
+
+/* Helper for grub_machine_mmap_iterate. */
+static int
+iterate_linuxbios_table (grub_linuxbios_table_item_t table_item, void *data)
+{
+ struct grub_machine_mmap_iterate_ctx *ctx = data;
+ mem_region_t mem_region;
+
+ if (table_item->tag != GRUB_LINUXBIOS_MEMBER_MEMORY)
+ return 0;
+
+ mem_region =
+ (mem_region_t) ((long) table_item +
+ sizeof (struct grub_linuxbios_table_item));
+ for (; (long) mem_region < (long) table_item + (long) table_item->size;
+ mem_region++)
+ {
+ grub_uint64_t start = mem_region->addr;
+ grub_uint64_t end = mem_region->addr + mem_region->size;
+#ifdef __i386__
+ /* Mark region 0xa0000 - 0x100000 as reserved. */
+ if (start < 0x100000 && end >= 0xa0000
+ && mem_region->type == GRUB_MACHINE_MEMORY_AVAILABLE)
+ {
+ if (start < 0xa0000
+ && ctx->hook (start, 0xa0000 - start,
+ /* Multiboot mmaps match with the coreboot mmap
+ definition. Therefore, we can just pass type
+ through. */
+ mem_region->type,
+ ctx->hook_data))
+ return 1;
+ if (start < 0xa0000)
+ start = 0xa0000;
+ if (start >= end)
+ continue;
+
+ if (ctx->hook (start, (end > 0x100000 ? 0x100000 : end) - start,
+ GRUB_MEMORY_RESERVED,
+ ctx->hook_data))
+ return 1;
+ start = 0x100000;
+
+ if (end <= start)
+ continue;
+ }
+#endif
+ if (ctx->hook (start, end - start,
+ /* Multiboot mmaps match with the coreboot mmap
+ definition. Therefore, we can just pass type
+ through. */
+ mem_region->type,
+ ctx->hook_data))
+ return 1;
+ }
+
+ return 0;
+}
+
+grub_err_t
+grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
+{
+ struct grub_machine_mmap_iterate_ctx ctx = { hook, hook_data };
+
+ grub_linuxbios_table_iterate (iterate_linuxbios_table, &ctx);
+
+ return 0;
+}
diff --git a/grub-core/kern/corecmd.c b/grub-core/kern/corecmd.c
new file mode 100644
index 0000000..fc54f43
--- /dev/null
+++ b/grub-core/kern/corecmd.c
@@ -0,0 +1,189 @@
+/* corecmd.c - critical commands which are registered in kernel */
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/mm.h>
+#include <grub/dl.h>
+#include <grub/err.h>
+#include <grub/env.h>
+#include <grub/misc.h>
+#include <grub/term.h>
+#include <grub/file.h>
+#include <grub/device.h>
+#include <grub/command.h>
+#include <grub/i18n.h>
+
+/* set ENVVAR=VALUE */
+static grub_err_t
+grub_core_cmd_set (struct grub_command *cmd __attribute__ ((unused)),
+ int argc, char *argv[])
+{
+ char *var;
+ char *val;
+
+ if (argc < 1)
+ {
+ struct grub_env_var *env;
+ FOR_SORTED_ENV (env)
+ grub_printf ("%s=%s\n", env->name, grub_env_get (env->name));
+ return 0;
+ }
+
+ var = argv[0];
+ val = grub_strchr (var, '=');
+ if (! val)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "not an assignment");
+
+ val[0] = 0;
+ grub_env_set (var, val + 1);
+ val[0] = '=';
+
+ return 0;
+}
+
+static grub_err_t
+grub_core_cmd_unset (struct grub_command *cmd __attribute__ ((unused)),
+ int argc, char *argv[])
+{
+ if (argc < 1)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT,
+ N_("one argument expected"));
+
+ grub_env_unset (argv[0]);
+ return 0;
+}
+
+/* insmod MODULE */
+static grub_err_t
+grub_core_cmd_insmod (struct grub_command *cmd __attribute__ ((unused)),
+ int argc, char *argv[])
+{
+ grub_dl_t mod;
+
+ if (argc == 0)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
+
+ if (argv[0][0] == '/' || argv[0][0] == '(' || argv[0][0] == '+')
+ mod = grub_dl_load_file (argv[0]);
+ else
+ mod = grub_dl_load (argv[0]);
+
+ if (mod)
+ grub_dl_ref (mod);
+
+ return 0;
+}
+
+static int
+grub_mini_print_devices (const char *name, void *data __attribute__ ((unused)))
+{
+ grub_printf ("(%s) ", name);
+
+ return 0;
+}
+
+static int
+grub_mini_print_files (const char *filename,
+ const struct grub_dirhook_info *info,
+ void *data __attribute__ ((unused)))
+{
+ grub_printf ("%s%s ", filename, info->dir ? "/" : "");
+
+ return 0;
+}
+
+/* ls [ARG] */
+static grub_err_t
+grub_core_cmd_ls (struct grub_command *cmd __attribute__ ((unused)),
+ int argc, char *argv[])
+{
+ if (argc < 1)
+ {
+ grub_device_iterate (grub_mini_print_devices, NULL);
+ grub_xputs ("\n");
+ grub_refresh ();
+ }
+ else
+ {
+ char *device_name;
+ grub_device_t dev = 0;
+ grub_fs_t fs;
+ char *path;
+
+ device_name = grub_file_get_device_name (argv[0]);
+ if (grub_errno)
+ goto fail;
+ dev = grub_device_open (device_name);
+ if (! dev)
+ goto fail;
+
+ fs = grub_fs_probe (dev);
+ path = grub_strchr (argv[0], ')');
+ if (! path)
+ path = argv[0];
+ else
+ path++;
+
+ if (! *path && ! device_name)
+ {
+ grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid argument");
+ goto fail;
+ }
+
+ if (! *path)
+ {
+ if (grub_errno == GRUB_ERR_UNKNOWN_FS)
+ grub_errno = GRUB_ERR_NONE;
+
+ grub_printf ("(%s): Filesystem is %s.\n",
+ device_name, fs ? fs->name : "unknown");
+ }
+ else if (fs)
+ {
+ (fs->fs_dir) (dev, path, grub_mini_print_files, NULL);
+ grub_xputs ("\n");
+ grub_refresh ();
+ }
+
+ fail:
+ if (dev)
+ grub_device_close (dev);
+
+ grub_free (device_name);
+ }
+
+ return grub_errno;
+}
+
+void
+grub_register_core_commands (void)
+{
+ grub_command_t cmd;
+ cmd = grub_register_command ("set", grub_core_cmd_set,
+ N_("[ENVVAR=VALUE]"),
+ N_("Set an environment variable."));
+ if (cmd)
+ cmd->flags |= GRUB_COMMAND_FLAG_EXTRACTOR;
+ grub_register_command ("unset", grub_core_cmd_unset,
+ N_("ENVVAR"),
+ N_("Remove an environment variable."));
+ grub_register_command ("ls", grub_core_cmd_ls,
+ N_("[ARG]"), N_("List devices or files."));
+ grub_register_command ("insmod", grub_core_cmd_insmod,
+ N_("MODULE"), N_("Insert a module."));
+}
diff --git a/grub-core/kern/device.c b/grub-core/kern/device.c
new file mode 100644
index 0000000..73b8ecc
--- /dev/null
+++ b/grub-core/kern/device.c
@@ -0,0 +1,191 @@
+/* device.c - device manager */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2002,2005,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/device.h>
+#include <grub/disk.h>
+#include <grub/net.h>
+#include <grub/fs.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/env.h>
+#include <grub/partition.h>
+#include <grub/i18n.h>
+
+grub_net_t (*grub_net_open) (const char *name) = NULL;
+
+grub_device_t
+grub_device_open (const char *name)
+{
+ grub_device_t dev = 0;
+
+ if (! name)
+ {
+ name = grub_env_get ("root");
+ if (name == NULL || *name == '\0')
+ {
+ grub_error (GRUB_ERR_BAD_DEVICE, N_("variable `%s' isn't set"), "root");
+ goto fail;
+ }
+ }
+
+ dev = grub_malloc (sizeof (*dev));
+ if (! dev)
+ goto fail;
+
+ dev->net = NULL;
+ /* Try to open a disk. */
+ dev->disk = grub_disk_open (name);
+ if (dev->disk)
+ return dev;
+ if (grub_net_open && grub_errno == GRUB_ERR_UNKNOWN_DEVICE)
+ {
+ grub_errno = GRUB_ERR_NONE;
+ dev->net = grub_net_open (name);
+ }
+
+ if (dev->net)
+ return dev;
+
+ fail:
+ grub_free (dev);
+
+ return 0;
+}
+
+grub_err_t
+grub_device_close (grub_device_t device)
+{
+ if (device->disk)
+ grub_disk_close (device->disk);
+
+ if (device->net)
+ {
+ grub_free (device->net->server);
+ grub_free (device->net);
+ }
+
+ grub_free (device);
+
+ return grub_errno;
+}
+
+struct part_ent
+{
+ struct part_ent *next;
+ char *name;
+};
+
+/* Context for grub_device_iterate. */
+struct grub_device_iterate_ctx
+{
+ grub_device_iterate_hook_t hook;
+ void *hook_data;
+ struct part_ent *ents;
+};
+
+/* Helper for grub_device_iterate. */
+static int
+iterate_partition (grub_disk_t disk, const grub_partition_t partition,
+ void *data)
+{
+ struct grub_device_iterate_ctx *ctx = data;
+ struct part_ent *p;
+ char *part_name;
+
+ p = grub_malloc (sizeof (*p));
+ if (!p)
+ {
+ return 1;
+ }
+
+ part_name = grub_partition_get_name (partition);
+ if (!part_name)
+ {
+ grub_free (p);
+ return 1;
+ }
+ p->name = grub_xasprintf ("%s,%s", disk->name, part_name);
+ grub_free (part_name);
+ if (!p->name)
+ {
+ grub_free (p);
+ return 1;
+ }
+
+ p->next = ctx->ents;
+ ctx->ents = p;
+
+ return 0;
+}
+
+/* Helper for grub_device_iterate. */
+static int
+iterate_disk (const char *disk_name, void *data)
+{
+ struct grub_device_iterate_ctx *ctx = data;
+ grub_device_t dev;
+
+ if (ctx->hook (disk_name, ctx->hook_data))
+ return 1;
+
+ dev = grub_device_open (disk_name);
+ if (! dev)
+ {
+ grub_errno = GRUB_ERR_NONE;
+ return 0;
+ }
+
+ if (dev->disk)
+ {
+ struct part_ent *p;
+ int ret = 0;
+
+ ctx->ents = NULL;
+ (void) grub_partition_iterate (dev->disk, iterate_partition, ctx);
+ grub_device_close (dev);
+
+ grub_errno = GRUB_ERR_NONE;
+
+ p = ctx->ents;
+ while (p != NULL)
+ {
+ struct part_ent *next = p->next;
+
+ if (!ret)
+ ret = ctx->hook (p->name, ctx->hook_data);
+ grub_free (p->name);
+ grub_free (p);
+ p = next;
+ }
+
+ return ret;
+ }
+
+ grub_device_close (dev);
+ return 0;
+}
+
+int
+grub_device_iterate (grub_device_iterate_hook_t hook, void *hook_data)
+{
+ struct grub_device_iterate_ctx ctx = { hook, hook_data, NULL };
+
+ /* Only disk devices are supported at the moment. */
+ return grub_disk_dev_iterate (iterate_disk, &ctx);
+}
diff --git a/grub-core/kern/disk.c b/grub-core/kern/disk.c
new file mode 100644
index 0000000..e1b0e07
--- /dev/null
+++ b/grub-core/kern/disk.c
@@ -0,0 +1,544 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2002,2003,2004,2006,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/disk.h>
+#include <grub/err.h>
+#include <grub/mm.h>
+#include <grub/types.h>
+#include <grub/partition.h>
+#include <grub/misc.h>
+#include <grub/time.h>
+#include <grub/file.h>
+#include <grub/i18n.h>
+
+#define GRUB_CACHE_TIMEOUT 2
+
+/* The last time the disk was used. */
+static grub_uint64_t grub_last_time = 0;
+
+struct grub_disk_cache grub_disk_cache_table[GRUB_DISK_CACHE_NUM];
+
+void (*grub_disk_firmware_fini) (void);
+int grub_disk_firmware_is_tainted;
+
+#if DISK_CACHE_STATS
+static unsigned long grub_disk_cache_hits;
+static unsigned long grub_disk_cache_misses;
+
+void
+grub_disk_cache_get_performance (unsigned long *hits, unsigned long *misses)
+{
+ *hits = grub_disk_cache_hits;
+ *misses = grub_disk_cache_misses;
+}
+#endif
+
+grub_err_t (*grub_disk_write_weak) (grub_disk_t disk,
+ grub_disk_addr_t sector,
+ grub_off_t offset,
+ grub_size_t size,
+ const void *buf);
+#include "disk_common.c"
+
+void
+grub_disk_cache_invalidate_all (void)
+{
+ unsigned i;
+
+ for (i = 0; i < GRUB_DISK_CACHE_NUM; i++)
+ {
+ struct grub_disk_cache *cache = grub_disk_cache_table + i;
+
+ if (cache->data && ! cache->lock)
+ {
+ grub_free (cache->data);
+ cache->data = 0;
+ }
+ }
+}
+
+static char *
+grub_disk_cache_fetch (unsigned long dev_id, unsigned long disk_id,
+ grub_disk_addr_t sector)
+{
+ struct grub_disk_cache *cache;
+ unsigned cache_index;
+
+ cache_index = grub_disk_cache_get_index (dev_id, disk_id, sector);
+ cache = grub_disk_cache_table + cache_index;
+
+ if (cache->dev_id == dev_id && cache->disk_id == disk_id
+ && cache->sector == sector)
+ {
+ cache->lock = 1;
+#if DISK_CACHE_STATS
+ grub_disk_cache_hits++;
+#endif
+ return cache->data;
+ }
+
+#if DISK_CACHE_STATS
+ grub_disk_cache_misses++;
+#endif
+
+ return 0;
+}
+
+static void
+grub_disk_cache_unlock (unsigned long dev_id, unsigned long disk_id,
+ grub_disk_addr_t sector)
+{
+ struct grub_disk_cache *cache;
+ unsigned cache_index;
+
+ cache_index = grub_disk_cache_get_index (dev_id, disk_id, sector);
+ cache = grub_disk_cache_table + cache_index;
+
+ if (cache->dev_id == dev_id && cache->disk_id == disk_id
+ && cache->sector == sector)
+ cache->lock = 0;
+}
+
+static grub_err_t
+grub_disk_cache_store (unsigned long dev_id, unsigned long disk_id,
+ grub_disk_addr_t sector, const char *data)
+{
+ unsigned cache_index;
+ struct grub_disk_cache *cache;
+
+ cache_index = grub_disk_cache_get_index (dev_id, disk_id, sector);
+ cache = grub_disk_cache_table + cache_index;
+
+ cache->lock = 1;
+ grub_free (cache->data);
+ cache->data = 0;
+ cache->lock = 0;
+
+ cache->data = grub_malloc (GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS);
+ if (! cache->data)
+ return grub_errno;
+
+ grub_memcpy (cache->data, data,
+ GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS);
+ cache->dev_id = dev_id;
+ cache->disk_id = disk_id;
+ cache->sector = sector;
+
+ return GRUB_ERR_NONE;
+}
+
+
+
+grub_disk_dev_t grub_disk_dev_list;
+
+void
+grub_disk_dev_register (grub_disk_dev_t dev)
+{
+ dev->next = grub_disk_dev_list;
+ grub_disk_dev_list = dev;
+}
+
+void
+grub_disk_dev_unregister (grub_disk_dev_t dev)
+{
+ grub_disk_dev_t *p, q;
+
+ for (p = &grub_disk_dev_list, q = *p; q; p = &(q->next), q = q->next)
+ if (q == dev)
+ {
+ *p = q->next;
+ break;
+ }
+}
+
+/* Return the location of the first ',', if any, which is not
+ escaped by a '\'. */
+static const char *
+find_part_sep (const char *name)
+{
+ const char *p = name;
+ char c;
+
+ while ((c = *p++) != '\0')
+ {
+ if (c == '\\' && *p == ',')
+ p++;
+ else if (c == ',')
+ return p - 1;
+ }
+ return NULL;
+}
+
+grub_disk_t
+grub_disk_open (const char *name)
+{
+ const char *p;
+ grub_disk_t disk;
+ grub_disk_dev_t dev;
+ char *raw = (char *) name;
+ grub_uint64_t current_time;
+
+ grub_dprintf ("disk", "Opening `%s'...\n", name);
+
+ disk = (grub_disk_t) grub_zalloc (sizeof (*disk));
+ if (! disk)
+ return 0;
+ disk->log_sector_size = GRUB_DISK_SECTOR_BITS;
+ /* Default 1MiB of maximum agglomerate. */
+ disk->max_agglomerate = 1048576 >> (GRUB_DISK_SECTOR_BITS
+ + GRUB_DISK_CACHE_BITS);
+
+ p = find_part_sep (name);
+ if (p)
+ {
+ grub_size_t len = p - name;
+
+ raw = grub_malloc (len + 1);
+ if (! raw)
+ goto fail;
+
+ grub_memcpy (raw, name, len);
+ raw[len] = '\0';
+ disk->name = grub_strdup (raw);
+ }
+ else
+ disk->name = grub_strdup (name);
+ if (! disk->name)
+ goto fail;
+
+ for (dev = grub_disk_dev_list; dev; dev = dev->next)
+ {
+ if ((dev->disk_open) (raw, disk) == GRUB_ERR_NONE)
+ break;
+ else if (grub_errno == GRUB_ERR_UNKNOWN_DEVICE)
+ grub_errno = GRUB_ERR_NONE;
+ else
+ goto fail;
+ }
+
+ if (! dev)
+ {
+ grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("disk `%s' not found"),
+ name);
+ goto fail;
+ }
+ if (disk->log_sector_size > GRUB_DISK_CACHE_BITS + GRUB_DISK_SECTOR_BITS
+ || disk->log_sector_size < GRUB_DISK_SECTOR_BITS)
+ {
+ grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
+ "sector sizes of %d bytes aren't supported yet",
+ (1 << disk->log_sector_size));
+ goto fail;
+ }
+
+ disk->dev = dev;
+
+ if (p)
+ {
+ disk->partition = grub_partition_probe (disk, p + 1);
+ if (! disk->partition)
+ {
+ /* TRANSLATORS: It means that the specified partition e.g.
+ hd0,msdos1=/dev/sda1 doesn't exist. */
+ grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("no such partition"));
+ goto fail;
+ }
+ }
+
+ /* The cache will be invalidated about 2 seconds after a device was
+ closed. */
+ current_time = grub_get_time_ms ();
+
+ if (current_time > (grub_last_time
+ + GRUB_CACHE_TIMEOUT * 1000))
+ grub_disk_cache_invalidate_all ();
+
+ grub_last_time = current_time;
+
+ fail:
+
+ if (raw && raw != name)
+ grub_free (raw);
+
+ if (grub_errno != GRUB_ERR_NONE)
+ {
+ grub_error_push ();
+ grub_dprintf ("disk", "Opening `%s' failed.\n", name);
+ grub_error_pop ();
+
+ grub_disk_close (disk);
+ return 0;
+ }
+
+ return disk;
+}
+
+void
+grub_disk_close (grub_disk_t disk)
+{
+ grub_partition_t part;
+ grub_dprintf ("disk", "Closing `%s'.\n", disk->name);
+
+ if (disk->dev && disk->dev->disk_close)
+ (disk->dev->disk_close) (disk);
+
+ /* Reset the timer. */
+ grub_last_time = grub_get_time_ms ();
+
+ while (disk->partition)
+ {
+ part = disk->partition->parent;
+ grub_free (disk->partition);
+ disk->partition = part;
+ }
+ grub_free ((void *) disk->name);
+ grub_free (disk);
+}
+
+/* Small read (less than cache size and not pass across cache unit boundaries).
+ sector is already adjusted and is divisible by cache unit size.
+ */
+static grub_err_t
+grub_disk_read_small_real (grub_disk_t disk, grub_disk_addr_t sector,
+ grub_off_t offset, grub_size_t size, void *buf)
+{
+ char *data;
+ char *tmp_buf;
+
+ /* Fetch the cache. */
+ data = grub_disk_cache_fetch (disk->dev->id, disk->id, sector);
+ if (data)
+ {
+ /* Just copy it! */
+ grub_memcpy (buf, data + offset, size);
+ grub_disk_cache_unlock (disk->dev->id, disk->id, sector);
+ return GRUB_ERR_NONE;
+ }
+
+ /* Allocate a temporary buffer. */
+ tmp_buf = grub_malloc (GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS);
+ if (! tmp_buf)
+ return grub_errno;
+
+ /* Otherwise read data from the disk actually. */
+ if (disk->total_sectors == GRUB_DISK_SIZE_UNKNOWN
+ || sector + GRUB_DISK_CACHE_SIZE
+ < (disk->total_sectors << (disk->log_sector_size - GRUB_DISK_SECTOR_BITS)))
+ {
+ grub_err_t err;
+ err = (disk->dev->disk_read) (disk, transform_sector (disk, sector),
+ 1U << (GRUB_DISK_CACHE_BITS
+ + GRUB_DISK_SECTOR_BITS
+ - disk->log_sector_size), tmp_buf);
+ if (!err)
+ {
+ /* Copy it and store it in the disk cache. */
+ grub_memcpy (buf, tmp_buf + offset, size);
+ grub_disk_cache_store (disk->dev->id, disk->id,
+ sector, tmp_buf);
+ grub_free (tmp_buf);
+ return GRUB_ERR_NONE;
+ }
+ }
+
+ grub_free (tmp_buf);
+ grub_errno = GRUB_ERR_NONE;
+
+ {
+ /* Uggh... Failed. Instead, just read necessary data. */
+ unsigned num;
+ grub_disk_addr_t aligned_sector;
+
+ sector += (offset >> GRUB_DISK_SECTOR_BITS);
+ offset &= ((1 << GRUB_DISK_SECTOR_BITS) - 1);
+ aligned_sector = (sector & ~((1ULL << (disk->log_sector_size
+ - GRUB_DISK_SECTOR_BITS))
+ - 1));
+ offset += ((sector - aligned_sector) << GRUB_DISK_SECTOR_BITS);
+ num = ((size + offset + (1ULL << (disk->log_sector_size))
+ - 1) >> (disk->log_sector_size));
+
+ tmp_buf = grub_malloc (num << disk->log_sector_size);
+ if (!tmp_buf)
+ return grub_errno;
+
+ if ((disk->dev->disk_read) (disk, transform_sector (disk, aligned_sector),
+ num, tmp_buf))
+ {
+ grub_error_push ();
+ grub_dprintf ("disk", "%s read failed\n", disk->name);
+ grub_error_pop ();
+ grub_free (tmp_buf);
+ return grub_errno;
+ }
+ grub_memcpy (buf, tmp_buf + offset, size);
+ grub_free (tmp_buf);
+ return GRUB_ERR_NONE;
+ }
+}
+
+static grub_err_t
+grub_disk_read_small (grub_disk_t disk, grub_disk_addr_t sector,
+ grub_off_t offset, grub_size_t size, void *buf)
+{
+ grub_err_t err;
+
+ err = grub_disk_read_small_real (disk, sector, offset, size, buf);
+ if (err)
+ return err;
+ if (disk->read_hook)
+ (disk->read_hook) (sector + (offset >> GRUB_DISK_SECTOR_BITS),
+ offset & (GRUB_DISK_SECTOR_SIZE - 1),
+ size, disk->read_hook_data);
+ return GRUB_ERR_NONE;
+}
+
+/* Read data from the disk. */
+grub_err_t
+grub_disk_read (grub_disk_t disk, grub_disk_addr_t sector,
+ grub_off_t offset, grub_size_t size, void *buf)
+{
+ /* First of all, check if the region is within the disk. */
+ if (grub_disk_adjust_range (disk, &sector, &offset, size) != GRUB_ERR_NONE)
+ {
+ grub_error_push ();
+ grub_dprintf ("disk", "Read out of range: sector 0x%llx (%s).\n",
+ (unsigned long long) sector, grub_errmsg);
+ grub_error_pop ();
+ return grub_errno;
+ }
+
+ /* First read until first cache boundary. */
+ if (offset || (sector & (GRUB_DISK_CACHE_SIZE - 1)))
+ {
+ grub_disk_addr_t start_sector;
+ grub_size_t pos;
+ grub_err_t err;
+ grub_size_t len;
+
+ start_sector = sector & ~((grub_disk_addr_t) GRUB_DISK_CACHE_SIZE - 1);
+ pos = (sector - start_sector) << GRUB_DISK_SECTOR_BITS;
+ len = ((GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS)
+ - pos - offset);
+ if (len > size)
+ len = size;
+ err = grub_disk_read_small (disk, start_sector,
+ offset + pos, len, buf);
+ if (err)
+ return err;
+ buf = (char *) buf + len;
+ size -= len;
+ offset += len;
+ sector += (offset >> GRUB_DISK_SECTOR_BITS);
+ offset &= ((1 << GRUB_DISK_SECTOR_BITS) - 1);
+ }
+
+ /* Until SIZE is zero... */
+ while (size >= (GRUB_DISK_CACHE_SIZE << GRUB_DISK_SECTOR_BITS))
+ {
+ char *data = NULL;
+ grub_disk_addr_t agglomerate;
+ grub_err_t err;
+
+ /* agglomerate read until we find a first cached entry. */
+ for (agglomerate = 0; agglomerate
+ < (size >> (GRUB_DISK_SECTOR_BITS + GRUB_DISK_CACHE_BITS))
+ && agglomerate < disk->max_agglomerate;
+ agglomerate++)
+ {
+ data = grub_disk_cache_fetch (disk->dev->id, disk->id,
+ sector + (agglomerate
+ << GRUB_DISK_CACHE_BITS));
+ if (data)
+ break;
+ }
+
+ if (data)
+ {
+ grub_memcpy ((char *) buf
+ + (agglomerate << (GRUB_DISK_CACHE_BITS
+ + GRUB_DISK_SECTOR_BITS)),
+ data, GRUB_DISK_CACHE_SIZE << GRUB_DISK_SECTOR_BITS);
+ grub_disk_cache_unlock (disk->dev->id, disk->id,
+ sector + (agglomerate
+ << GRUB_DISK_CACHE_BITS));
+ }
+
+ if (agglomerate)
+ {
+ grub_disk_addr_t i;
+
+ err = (disk->dev->disk_read) (disk, transform_sector (disk, sector),
+ agglomerate << (GRUB_DISK_CACHE_BITS
+ + GRUB_DISK_SECTOR_BITS
+ - disk->log_sector_size),
+ buf);
+ if (err)
+ return err;
+
+ for (i = 0; i < agglomerate; i ++)
+ grub_disk_cache_store (disk->dev->id, disk->id,
+ sector + (i << GRUB_DISK_CACHE_BITS),
+ (char *) buf
+ + (i << (GRUB_DISK_CACHE_BITS
+ + GRUB_DISK_SECTOR_BITS)));
+
+
+ if (disk->read_hook)
+ (disk->read_hook) (sector, 0, agglomerate << (GRUB_DISK_CACHE_BITS + GRUB_DISK_SECTOR_BITS),
+ disk->read_hook_data);
+
+ sector += agglomerate << GRUB_DISK_CACHE_BITS;
+ size -= agglomerate << (GRUB_DISK_CACHE_BITS + GRUB_DISK_SECTOR_BITS);
+ buf = (char *) buf
+ + (agglomerate << (GRUB_DISK_CACHE_BITS + GRUB_DISK_SECTOR_BITS));
+ }
+
+ if (data)
+ {
+ if (disk->read_hook)
+ (disk->read_hook) (sector, 0, (GRUB_DISK_CACHE_SIZE << GRUB_DISK_SECTOR_BITS),
+ disk->read_hook_data);
+ sector += GRUB_DISK_CACHE_SIZE;
+ buf = (char *) buf + (GRUB_DISK_CACHE_SIZE << GRUB_DISK_SECTOR_BITS);
+ size -= (GRUB_DISK_CACHE_SIZE << GRUB_DISK_SECTOR_BITS);
+ }
+ }
+
+ /* And now read the last part. */
+ if (size)
+ {
+ grub_err_t err;
+ err = grub_disk_read_small (disk, sector, 0, size, buf);
+ if (err)
+ return err;
+ }
+
+ return grub_errno;
+}
+
+grub_uint64_t
+grub_disk_native_sectors (grub_disk_t disk)
+{
+ if (disk->partition)
+ return grub_partition_get_len (disk->partition);
+ else if (disk->total_sectors != GRUB_DISK_SIZE_UNKNOWN)
+ return disk->total_sectors << (disk->log_sector_size - GRUB_DISK_SECTOR_BITS);
+ else
+ return GRUB_DISK_SIZE_UNKNOWN;
+}
diff --git a/grub-core/kern/disk_common.c b/grub-core/kern/disk_common.c
new file mode 100644
index 0000000..e09fba8
--- /dev/null
+++ b/grub-core/kern/disk_common.c
@@ -0,0 +1,66 @@
+/* This function performs three tasks:
+ - Make sectors disk relative from partition relative.
+ - Normalize offset to be less than the sector size.
+ - Verify that the range is inside the partition. */
+static grub_err_t
+grub_disk_adjust_range (grub_disk_t disk, grub_disk_addr_t *sector,
+ grub_off_t *offset, grub_size_t size)
+{
+ grub_partition_t part;
+ grub_disk_addr_t total_sectors;
+
+ *sector += *offset >> GRUB_DISK_SECTOR_BITS;
+ *offset &= GRUB_DISK_SECTOR_SIZE - 1;
+
+ for (part = disk->partition; part; part = part->parent)
+ {
+ grub_disk_addr_t start;
+ grub_uint64_t len;
+
+ start = part->start;
+ len = part->len;
+
+ if (*sector >= len
+ || len - *sector < ((*offset + size + GRUB_DISK_SECTOR_SIZE - 1)
+ >> GRUB_DISK_SECTOR_BITS))
+ return grub_error (GRUB_ERR_OUT_OF_RANGE,
+ N_("attempt to read or write outside of partition"));
+
+ *sector += start;
+ }
+
+ /* Transform total_sectors to number of 512B blocks. */
+ total_sectors = disk->total_sectors << (disk->log_sector_size - GRUB_DISK_SECTOR_BITS);
+
+ /*
+ * Some drivers have problems with disks above reasonable sizes.
+ * Clamp the size to GRUB_DISK_MAX_SECTORS. Just one condition is enough
+ * since GRUB_DISK_SIZE_UNKNOWN is always above GRUB_DISK_MAX_SECTORS,
+ * assuming a maximum 4 KiB sector size.
+ */
+ if (total_sectors > GRUB_DISK_MAX_SECTORS)
+ total_sectors = GRUB_DISK_MAX_SECTORS;
+
+ if ((total_sectors <= *sector
+ || ((*offset + size + GRUB_DISK_SECTOR_SIZE - 1)
+ >> GRUB_DISK_SECTOR_BITS) > total_sectors - *sector))
+ return grub_error (GRUB_ERR_OUT_OF_RANGE,
+ N_("attempt to read or write outside of disk `%s'"), disk->name);
+
+ return GRUB_ERR_NONE;
+}
+
+static inline grub_disk_addr_t
+transform_sector (grub_disk_t disk, grub_disk_addr_t sector)
+{
+ return sector >> (disk->log_sector_size - GRUB_DISK_SECTOR_BITS);
+}
+
+static unsigned
+grub_disk_cache_get_index (unsigned long dev_id, unsigned long disk_id,
+ grub_disk_addr_t sector)
+{
+ return ((dev_id * 524287UL + disk_id * 2606459UL
+ + ((unsigned) (sector >> GRUB_DISK_CACHE_BITS)))
+ % GRUB_DISK_CACHE_NUM);
+}
diff --git a/grub-core/kern/dl.c b/grub-core/kern/dl.c
new file mode 100644
index 0000000..48f8a79
--- /dev/null
+++ b/grub-core/kern/dl.c
@@ -0,0 +1,825 @@
+/* dl.c - loadable module support */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2002,2003,2004,2005,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 <http://www.gnu.org/licenses/>.
+ */
+
+/* Force native word size */
+#define GRUB_TARGET_WORDSIZE (8 * GRUB_CPU_SIZEOF_VOID_P)
+
+#include <config.h>
+#include <grub/elf.h>
+#include <grub/dl.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/err.h>
+#include <grub/types.h>
+#include <grub/symbol.h>
+#include <grub/file.h>
+#include <grub/env.h>
+#include <grub/cache.h>
+#include <grub/i18n.h>
+
+/* Platforms where modules are in a readonly area of memory. */
+#if defined(GRUB_MACHINE_QEMU)
+#define GRUB_MODULES_MACHINE_READONLY
+#endif
+
+
+
+#pragma GCC diagnostic ignored "-Wcast-align"
+
+grub_dl_t grub_dl_head = 0;
+
+grub_err_t
+grub_dl_add (grub_dl_t mod);
+
+/* Keep global so that GDB scripts work. */
+grub_err_t
+grub_dl_add (grub_dl_t mod)
+{
+ if (grub_dl_get (mod->name))
+ return grub_error (GRUB_ERR_BAD_MODULE,
+ "`%s' is already loaded", mod->name);
+
+ return GRUB_ERR_NONE;
+}
+
+static void
+grub_dl_remove (grub_dl_t mod)
+{
+ grub_dl_t *p, q;
+
+ for (p = &grub_dl_head, q = *p; q; p = &q->next, q = *p)
+ if (q == mod)
+ {
+ *p = q->next;
+ return;
+ }
+}
+
+
+
+struct grub_symbol
+{
+ struct grub_symbol *next;
+ const char *name;
+ void *addr;
+ int isfunc;
+ grub_dl_t mod; /* The module to which this symbol belongs. */
+};
+typedef struct grub_symbol *grub_symbol_t;
+
+/* The size of the symbol table. */
+#define GRUB_SYMTAB_SIZE 509
+
+/* The symbol table (using an open-hash). */
+static struct grub_symbol *grub_symtab[GRUB_SYMTAB_SIZE];
+
+/* Simple hash function. */
+static unsigned
+grub_symbol_hash (const char *s)
+{
+ unsigned key = 0;
+
+ while (*s)
+ key = key * 65599 + *s++;
+
+ return (key + (key >> 5)) % GRUB_SYMTAB_SIZE;
+}
+
+/* Resolve the symbol name NAME and return the address.
+ Return NULL, if not found. */
+static grub_symbol_t
+grub_dl_resolve_symbol (const char *name)
+{
+ grub_symbol_t sym;
+
+ for (sym = grub_symtab[grub_symbol_hash (name)]; sym; sym = sym->next)
+ if (grub_strcmp (sym->name, name) == 0)
+ return sym;
+
+ return 0;
+}
+
+/* Register a symbol with the name NAME and the address ADDR. */
+grub_err_t
+grub_dl_register_symbol (const char *name, void *addr, int isfunc,
+ grub_dl_t mod)
+{
+ grub_symbol_t sym;
+ unsigned k;
+
+ sym = (grub_symbol_t) grub_malloc (sizeof (*sym));
+ if (! sym)
+ return grub_errno;
+
+ if (mod)
+ {
+ sym->name = grub_strdup (name);
+ if (! sym->name)
+ {
+ grub_free (sym);
+ return grub_errno;
+ }
+ }
+ else
+ sym->name = name;
+
+ sym->addr = addr;
+ sym->mod = mod;
+ sym->isfunc = isfunc;
+
+ k = grub_symbol_hash (name);
+ sym->next = grub_symtab[k];
+ grub_symtab[k] = sym;
+
+ return GRUB_ERR_NONE;
+}
+
+/* Unregister all the symbols defined in the module MOD. */
+static void
+grub_dl_unregister_symbols (grub_dl_t mod)
+{
+ unsigned i;
+
+ if (! mod)
+ grub_fatal ("core symbols cannot be unregistered");
+
+ for (i = 0; i < GRUB_SYMTAB_SIZE; i++)
+ {
+ grub_symbol_t sym, *p, q;
+
+ for (p = &grub_symtab[i], sym = *p; sym; sym = q)
+ {
+ q = sym->next;
+ if (sym->mod == mod)
+ {
+ *p = q;
+ grub_free ((void *) sym->name);
+ grub_free (sym);
+ }
+ else
+ p = &sym->next;
+ }
+ }
+}
+
+/* Return the address of a section whose index is N. */
+static void *
+grub_dl_get_section_addr (grub_dl_t mod, unsigned n)
+{
+ grub_dl_segment_t seg;
+
+ for (seg = mod->segment; seg; seg = seg->next)
+ if (seg->section == n)
+ return seg->addr;
+
+ return 0;
+}
+
+/* Check if EHDR is a valid ELF header. */
+static grub_err_t
+grub_dl_check_header (void *ehdr, grub_size_t size)
+{
+ Elf_Ehdr *e = ehdr;
+ grub_err_t err;
+
+ /* Check the header size. */
+ if (size < sizeof (Elf_Ehdr))
+ return grub_error (GRUB_ERR_BAD_OS, "ELF header smaller than expected");
+
+ /* Check the magic numbers. */
+ if (e->e_ident[EI_MAG0] != ELFMAG0
+ || e->e_ident[EI_MAG1] != ELFMAG1
+ || e->e_ident[EI_MAG2] != ELFMAG2
+ || e->e_ident[EI_MAG3] != ELFMAG3
+ || e->e_ident[EI_VERSION] != EV_CURRENT
+ || e->e_version != EV_CURRENT)
+ return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-independent ELF magic"));
+
+ err = grub_arch_dl_check_header (ehdr);
+ if (err)
+ return err;
+
+ return GRUB_ERR_NONE;
+}
+
+/* Load all segments from memory specified by E. */
+static grub_err_t
+grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
+{
+ unsigned i;
+ const Elf_Shdr *s;
+ grub_size_t tsize = 0, talign = 1;
+#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv)
+ grub_size_t tramp;
+ grub_size_t got;
+ grub_err_t err;
+#endif
+ char *ptr;
+
+ for (i = 0, s = (const Elf_Shdr *)((const char *) e + e->e_shoff);
+ i < e->e_shnum;
+ i++, s = (const Elf_Shdr *)((const char *) s + e->e_shentsize))
+ {
+ tsize = ALIGN_UP (tsize, s->sh_addralign) + s->sh_size;
+ if (talign < s->sh_addralign)
+ talign = s->sh_addralign;
+ }
+
+#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv)
+ err = grub_arch_dl_get_tramp_got_size (e, &tramp, &got);
+ if (err)
+ return err;
+ tsize += ALIGN_UP (tramp, GRUB_ARCH_DL_TRAMP_ALIGN);
+ if (talign < GRUB_ARCH_DL_TRAMP_ALIGN)
+ talign = GRUB_ARCH_DL_TRAMP_ALIGN;
+ tsize += ALIGN_UP (got, GRUB_ARCH_DL_GOT_ALIGN);
+ if (talign < GRUB_ARCH_DL_GOT_ALIGN)
+ talign = GRUB_ARCH_DL_GOT_ALIGN;
+#endif
+
+#ifdef GRUB_MACHINE_EMU
+ mod->base = grub_osdep_dl_memalign (talign, tsize);
+#else
+ mod->base = grub_memalign (talign, tsize);
+#endif
+ if (!mod->base)
+ return grub_errno;
+ mod->sz = tsize;
+ ptr = mod->base;
+
+ for (i = 0, s = (Elf_Shdr *)((char *) e + e->e_shoff);
+ i < e->e_shnum;
+ i++, s = (Elf_Shdr *)((char *) s + e->e_shentsize))
+ {
+ if (s->sh_flags & SHF_ALLOC)
+ {
+ grub_dl_segment_t seg;
+
+ seg = (grub_dl_segment_t) grub_malloc (sizeof (*seg));
+ if (! seg)
+ return grub_errno;
+
+ if (s->sh_size)
+ {
+ void *addr;
+
+ ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, s->sh_addralign);
+ addr = ptr;
+ ptr += s->sh_size;
+
+ switch (s->sh_type)
+ {
+ case SHT_PROGBITS:
+ grub_memcpy (addr, (char *) e + s->sh_offset, s->sh_size);
+ break;
+ case SHT_NOBITS:
+ grub_memset (addr, 0, s->sh_size);
+ break;
+ }
+
+ seg->addr = addr;
+ }
+ else
+ seg->addr = 0;
+
+ seg->size = s->sh_size;
+ seg->section = i;
+ seg->next = mod->segment;
+ mod->segment = seg;
+ }
+ }
+#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv)
+ ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_TRAMP_ALIGN);
+ mod->tramp = ptr;
+ mod->trampptr = ptr;
+ ptr += tramp;
+ ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_GOT_ALIGN);
+ mod->got = ptr;
+ mod->gotptr = ptr;
+ ptr += got;
+#endif
+
+ return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+grub_dl_resolve_symbols (grub_dl_t mod, Elf_Ehdr *e)
+{
+ unsigned i;
+ Elf_Shdr *s;
+ Elf_Sym *sym;
+ const char *str;
+ Elf_Word size, entsize;
+
+ for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
+ i < e->e_shnum;
+ i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
+ if (s->sh_type == SHT_SYMTAB)
+ break;
+
+ /* Module without symbol table may still be used to pull in dependencies.
+ We verify at build time that such modules do not contain any relocations
+ that may reference symbol table. */
+ if (i == e->e_shnum)
+ return GRUB_ERR_NONE;
+
+#ifdef GRUB_MODULES_MACHINE_READONLY
+ mod->symtab = grub_malloc (s->sh_size);
+ if (!mod->symtab)
+ return grub_errno;
+ grub_memcpy (mod->symtab, (char *) e + s->sh_offset, s->sh_size);
+#else
+ mod->symtab = (Elf_Sym *) ((char *) e + s->sh_offset);
+#endif
+ mod->symsize = s->sh_entsize;
+ sym = mod->symtab;
+ size = s->sh_size;
+ entsize = s->sh_entsize;
+
+ s = (Elf_Shdr *) ((char *) e + e->e_shoff + e->e_shentsize * s->sh_link);
+ str = (char *) e + s->sh_offset;
+
+ for (i = 0;
+ i < size / entsize;
+ i++, sym = (Elf_Sym *) ((char *) sym + entsize))
+ {
+ unsigned char type = ELF_ST_TYPE (sym->st_info);
+ unsigned char bind = ELF_ST_BIND (sym->st_info);
+ const char *name = str + sym->st_name;
+
+ switch (type)
+ {
+ case STT_NOTYPE:
+ case STT_OBJECT:
+ /* Resolve a global symbol. */
+ if (sym->st_name != 0 && sym->st_shndx == 0)
+ {
+ grub_symbol_t nsym = grub_dl_resolve_symbol (name);
+ if (! nsym)
+ return grub_error (GRUB_ERR_BAD_MODULE,
+ N_("symbol `%s' not found"), name);
+ sym->st_value = (Elf_Addr) nsym->addr;
+ if (nsym->isfunc)
+ sym->st_info = ELF_ST_INFO (bind, STT_FUNC);
+ }
+ else
+ {
+ sym->st_value += (Elf_Addr) grub_dl_get_section_addr (mod,
+ sym->st_shndx);
+ if (bind != STB_LOCAL)
+ if (grub_dl_register_symbol (name, (void *) sym->st_value, 0, mod))
+ return grub_errno;
+ }
+ break;
+
+ case STT_FUNC:
+ sym->st_value += (Elf_Addr) grub_dl_get_section_addr (mod,
+ sym->st_shndx);
+#ifdef __ia64__
+ {
+ /* FIXME: free descriptor once it's not used anymore. */
+ char **desc;
+ desc = grub_malloc (2 * sizeof (char *));
+ if (!desc)
+ return grub_errno;
+ desc[0] = (void *) sym->st_value;
+ desc[1] = mod->base;
+ sym->st_value = (grub_addr_t) desc;
+ }
+#endif
+ if (bind != STB_LOCAL)
+ if (grub_dl_register_symbol (name, (void *) sym->st_value, 1, mod))
+ return grub_errno;
+ if (grub_strcmp (name, "grub_mod_init") == 0)
+ mod->init = (void (*) (grub_dl_t)) sym->st_value;
+ else if (grub_strcmp (name, "grub_mod_fini") == 0)
+ mod->fini = (void (*) (void)) sym->st_value;
+ break;
+
+ case STT_SECTION:
+ sym->st_value = (Elf_Addr) grub_dl_get_section_addr (mod,
+ sym->st_shndx);
+ break;
+
+ case STT_FILE:
+ sym->st_value = 0;
+ break;
+
+ default:
+ return grub_error (GRUB_ERR_BAD_MODULE,
+ "unknown symbol type `%d'", (int) type);
+ }
+ }
+
+ return GRUB_ERR_NONE;
+}
+
+static Elf_Shdr *
+grub_dl_find_section (Elf_Ehdr *e, const char *name)
+{
+ Elf_Shdr *s;
+ const char *str;
+ unsigned i;
+
+ s = (Elf_Shdr *) ((char *) e + e->e_shoff + e->e_shstrndx * e->e_shentsize);
+ str = (char *) e + s->sh_offset;
+
+ for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
+ i < e->e_shnum;
+ i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
+ if (grub_strcmp (str + s->sh_name, name) == 0)
+ return s;
+ return NULL;
+}
+
+/* Me, Vladimir Serbinenko, hereby I add this module check as per new
+ GNU module policy. Note that this license check is informative only.
+ Modules have to be licensed under GPLv3 or GPLv3+ (optionally
+ multi-licensed under other licences as well) independently of the
+ presence of this check and solely by linking (module loading in GRUB
+ constitutes linking) and GRUB core being licensed under GPLv3+.
+ Be sure to understand your license obligations.
+*/
+static grub_err_t
+grub_dl_check_license (Elf_Ehdr *e)
+{
+ Elf_Shdr *s = grub_dl_find_section (e, ".module_license");
+ if (s && (grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv3") == 0
+ || grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv3+") == 0
+ || grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv2+") == 0))
+ return GRUB_ERR_NONE;
+ return grub_error (GRUB_ERR_BAD_MODULE, "incompatible license");
+}
+
+static grub_err_t
+grub_dl_resolve_name (grub_dl_t mod, Elf_Ehdr *e)
+{
+ Elf_Shdr *s;
+
+ s = grub_dl_find_section (e, ".modname");
+ if (!s)
+ return grub_error (GRUB_ERR_BAD_MODULE, "no module name found");
+
+ mod->name = grub_strdup ((char *) e + s->sh_offset);
+ if (! mod->name)
+ return grub_errno;
+
+ return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+grub_dl_resolve_dependencies (grub_dl_t mod, Elf_Ehdr *e)
+{
+ Elf_Shdr *s;
+
+ s = grub_dl_find_section (e, ".moddeps");
+
+ if (!s)
+ return GRUB_ERR_NONE;
+
+ const char *name = (char *) e + s->sh_offset;
+ const char *max = name + s->sh_size;
+
+ while ((name < max) && (*name))
+ {
+ grub_dl_t m;
+ grub_dl_dep_t dep;
+
+ m = grub_dl_load (name);
+ if (! m)
+ return grub_errno;
+
+ grub_dl_ref (m);
+
+ dep = (grub_dl_dep_t) grub_malloc (sizeof (*dep));
+ if (! dep)
+ return grub_errno;
+
+ dep->mod = m;
+ dep->next = mod->dep;
+ mod->dep = dep;
+
+ name += grub_strlen (name) + 1;
+ }
+
+ return GRUB_ERR_NONE;
+}
+
+int
+grub_dl_ref (grub_dl_t mod)
+{
+ grub_dl_dep_t dep;
+
+ if (!mod)
+ return 0;
+
+ for (dep = mod->dep; dep; dep = dep->next)
+ grub_dl_ref (dep->mod);
+
+ return ++mod->ref_count;
+}
+
+int
+grub_dl_unref (grub_dl_t mod)
+{
+ grub_dl_dep_t dep;
+
+ if (!mod)
+ return 0;
+
+ for (dep = mod->dep; dep; dep = dep->next)
+ grub_dl_unref (dep->mod);
+
+ return --mod->ref_count;
+}
+
+int
+grub_dl_ref_count (grub_dl_t mod)
+{
+ if (mod == NULL)
+ return 0;
+
+ return mod->ref_count;
+}
+
+static void
+grub_dl_flush_cache (grub_dl_t mod)
+{
+ grub_dprintf ("modules", "flushing 0x%lx bytes at %p\n",
+ (unsigned long) mod->sz, mod->base);
+ grub_arch_sync_caches (mod->base, mod->sz);
+}
+
+static grub_err_t
+grub_dl_relocate_symbols (grub_dl_t mod, void *ehdr)
+{
+ Elf_Ehdr *e = ehdr;
+ Elf_Shdr *s;
+ unsigned i;
+
+ for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
+ i < e->e_shnum;
+ i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
+ if (s->sh_type == SHT_REL || s->sh_type == SHT_RELA)
+ {
+ grub_dl_segment_t seg;
+ grub_err_t err;
+
+ /* Find the target segment. */
+ for (seg = mod->segment; seg; seg = seg->next)
+ if (seg->section == s->sh_info)
+ break;
+
+ if (seg)
+ {
+ if (!mod->symtab)
+ return grub_error (GRUB_ERR_BAD_MODULE, "relocation without symbol table");
+
+ err = grub_arch_dl_relocate_symbols (mod, ehdr, s, seg);
+ if (err)
+ return err;
+ }
+ }
+
+ return GRUB_ERR_NONE;
+}
+
+/* Load a module from core memory. */
+grub_dl_t
+grub_dl_load_core_noinit (void *addr, grub_size_t size)
+{
+ Elf_Ehdr *e;
+ grub_dl_t mod;
+
+ grub_dprintf ("modules", "module at %p, size 0x%lx\n", addr,
+ (unsigned long) size);
+ e = addr;
+ if (grub_dl_check_header (e, size))
+ return 0;
+
+ if (e->e_type != ET_REL)
+ {
+ grub_error (GRUB_ERR_BAD_MODULE, N_("this ELF file is not of the right type"));
+ return 0;
+ }
+
+ /* Make sure that every section is within the core. */
+ if (size < e->e_shoff + (grub_uint32_t) e->e_shentsize * e->e_shnum)
+ {
+ grub_error (GRUB_ERR_BAD_OS, "ELF sections outside core");
+ return 0;
+ }
+
+ mod = (grub_dl_t) grub_zalloc (sizeof (*mod));
+ if (! mod)
+ return 0;
+
+ mod->ref_count = 1;
+
+ grub_dprintf ("modules", "relocating to %p\n", mod);
+ /* Me, Vladimir Serbinenko, hereby I add this module check as per new
+ GNU module policy. Note that this license check is informative only.
+ Modules have to be licensed under GPLv3 or GPLv3+ (optionally
+ multi-licensed under other licences as well) independently of the
+ presence of this check and solely by linking (module loading in GRUB
+ constitutes linking) and GRUB core being licensed under GPLv3+.
+ Be sure to understand your license obligations.
+ */
+ if (grub_dl_check_license (e)
+ || grub_dl_resolve_name (mod, e)
+ || grub_dl_resolve_dependencies (mod, e)
+ || grub_dl_load_segments (mod, e)
+ || grub_dl_resolve_symbols (mod, e)
+ || grub_dl_relocate_symbols (mod, e))
+ {
+ mod->fini = 0;
+ grub_dl_unload (mod);
+ return 0;
+ }
+
+ grub_dl_flush_cache (mod);
+
+ grub_dprintf ("modules", "module name: %s\n", mod->name);
+ grub_dprintf ("modules", "init function: %p\n", mod->init);
+
+ if (grub_dl_add (mod))
+ {
+ grub_dl_unload (mod);
+ return 0;
+ }
+
+ return mod;
+}
+
+grub_dl_t
+grub_dl_load_core (void *addr, grub_size_t size)
+{
+ grub_dl_t mod;
+
+ grub_boot_time ("Parsing module");
+
+ mod = grub_dl_load_core_noinit (addr, size);
+
+ if (!mod)
+ return NULL;
+
+ grub_boot_time ("Initing module %s", mod->name);
+ grub_dl_init (mod);
+ grub_boot_time ("Module %s inited", mod->name);
+
+ return mod;
+}
+
+/* Load a module from the file FILENAME. */
+grub_dl_t
+grub_dl_load_file (const char *filename)
+{
+ grub_file_t file = NULL;
+ grub_ssize_t size;
+ void *core = 0;
+ grub_dl_t mod = 0;
+
+ grub_boot_time ("Loading module %s", filename);
+
+ file = grub_file_open (filename, GRUB_FILE_TYPE_GRUB_MODULE);
+ if (! file)
+ return 0;
+
+ size = grub_file_size (file);
+ core = grub_malloc (size);
+ if (! core)
+ {
+ grub_file_close (file);
+ return 0;
+ }
+
+ if (grub_file_read (file, core, size) != (int) size)
+ {
+ grub_file_close (file);
+ grub_free (core);
+ return 0;
+ }
+
+ /* We must close this before we try to process dependencies.
+ Some disk backends do not handle gracefully multiple concurrent
+ opens of the same device. */
+ grub_file_close (file);
+
+ mod = grub_dl_load_core (core, size);
+ grub_free (core);
+ if (! mod)
+ return 0;
+
+ mod->ref_count--;
+ return mod;
+}
+
+/* Load a module using a symbolic name. */
+grub_dl_t
+grub_dl_load (const char *name)
+{
+ char *filename;
+ grub_dl_t mod;
+ const char *grub_dl_dir = grub_env_get ("prefix");
+
+ mod = grub_dl_get (name);
+ if (mod)
+ return mod;
+
+ if (grub_no_modules)
+ return 0;
+
+ if (! grub_dl_dir) {
+ grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("variable `%s' isn't set"), "prefix");
+ return 0;
+ }
+
+ filename = grub_xasprintf ("%s/" GRUB_TARGET_CPU "-" GRUB_PLATFORM "/%s.mod",
+ grub_dl_dir, name);
+ if (! filename)
+ return 0;
+
+ mod = grub_dl_load_file (filename);
+ grub_free (filename);
+
+ if (! mod)
+ return 0;
+
+ if (grub_strcmp (mod->name, name) != 0)
+ grub_error (GRUB_ERR_BAD_MODULE, "mismatched names");
+
+ return mod;
+}
+
+/* Unload the module MOD. */
+int
+grub_dl_unload (grub_dl_t mod)
+{
+ grub_dl_dep_t dep, depn;
+
+ if (mod->ref_count > 0)
+ return 0;
+
+ if (mod->fini)
+ (mod->fini) ();
+
+ grub_dl_remove (mod);
+ grub_dl_unregister_symbols (mod);
+
+ for (dep = mod->dep; dep; dep = depn)
+ {
+ depn = dep->next;
+
+ grub_dl_unload (dep->mod);
+
+ grub_free (dep);
+ }
+
+#ifdef GRUB_MACHINE_EMU
+ grub_dl_osdep_dl_free (mod->base);
+#else
+ grub_free (mod->base);
+#endif
+ grub_free (mod->name);
+#ifdef GRUB_MODULES_MACHINE_READONLY
+ grub_free (mod->symtab);
+#endif
+ grub_free (mod);
+ return 1;
+}
+
+/* Unload unneeded modules. */
+void
+grub_dl_unload_unneeded (void)
+{
+ /* Because grub_dl_remove modifies the list of modules, this
+ implementation is tricky. */
+ grub_dl_t p = grub_dl_head;
+
+ while (p)
+ {
+ if (grub_dl_unload (p))
+ {
+ p = grub_dl_head;
+ continue;
+ }
+
+ p = p->next;
+ }
+}
diff --git a/grub-core/kern/efi/acpi.c b/grub-core/kern/efi/acpi.c
new file mode 100644
index 0000000..74f8cd1
--- /dev/null
+++ b/grub-core/kern/efi/acpi.c
@@ -0,0 +1,59 @@
+/* acpi.c - get acpi tables. */
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/acpi.h>
+#include <grub/misc.h>
+#include <grub/efi/efi.h>
+#include <grub/efi/api.h>
+
+struct grub_acpi_rsdp_v10 *
+grub_machine_acpi_get_rsdpv1 (void)
+{
+ unsigned i;
+ static grub_efi_packed_guid_t acpi_guid = GRUB_EFI_ACPI_TABLE_GUID;
+
+ for (i = 0; i < grub_efi_system_table->num_table_entries; i++)
+ {
+ grub_efi_packed_guid_t *guid =
+ &grub_efi_system_table->configuration_table[i].vendor_guid;
+
+ if (! grub_memcmp (guid, &acpi_guid, sizeof (grub_efi_packed_guid_t)))
+ return (struct grub_acpi_rsdp_v10 *)
+ grub_efi_system_table->configuration_table[i].vendor_table;
+ }
+ return 0;
+}
+
+struct grub_acpi_rsdp_v20 *
+grub_machine_acpi_get_rsdpv2 (void)
+{
+ unsigned i;
+ static grub_efi_packed_guid_t acpi20_guid = GRUB_EFI_ACPI_20_TABLE_GUID;
+
+ for (i = 0; i < grub_efi_system_table->num_table_entries; i++)
+ {
+ grub_efi_packed_guid_t *guid =
+ &grub_efi_system_table->configuration_table[i].vendor_guid;
+
+ if (! grub_memcmp (guid, &acpi20_guid, sizeof (grub_efi_packed_guid_t)))
+ return (struct grub_acpi_rsdp_v20 *)
+ grub_efi_system_table->configuration_table[i].vendor_table;
+ }
+ return 0;
+}
diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c
new file mode 100644
index 0000000..8cff7be
--- /dev/null
+++ b/grub-core/kern/efi/efi.c
@@ -0,0 +1,1017 @@
+/* efi.c - generic EFI support */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2006,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/misc.h>
+#include <grub/charset.h>
+#include <grub/efi/api.h>
+#include <grub/efi/efi.h>
+#include <grub/efi/console_control.h>
+#include <grub/efi/pe32.h>
+#include <grub/time.h>
+#include <grub/term.h>
+#include <grub/kernel.h>
+#include <grub/mm.h>
+#include <grub/loader.h>
+
+/* The handle of GRUB itself. Filled in by the startup code. */
+grub_efi_handle_t grub_efi_image_handle;
+
+/* The pointer to a system table. Filled in by the startup code. */
+grub_efi_system_table_t *grub_efi_system_table;
+
+static grub_efi_guid_t console_control_guid = GRUB_EFI_CONSOLE_CONTROL_GUID;
+static grub_efi_guid_t loaded_image_guid = GRUB_EFI_LOADED_IMAGE_GUID;
+static grub_efi_guid_t device_path_guid = GRUB_EFI_DEVICE_PATH_GUID;
+
+void *
+grub_efi_locate_protocol (grub_efi_guid_t *protocol, void *registration)
+{
+ void *interface;
+ grub_efi_status_t status;
+
+ status = efi_call_3 (grub_efi_system_table->boot_services->locate_protocol,
+ protocol, registration, &interface);
+ if (status != GRUB_EFI_SUCCESS)
+ return 0;
+
+ return interface;
+}
+
+/* Return the array of handles which meet the requirement. If successful,
+ the number of handles is stored in NUM_HANDLES. The array is allocated
+ from the heap. */
+grub_efi_handle_t *
+grub_efi_locate_handle (grub_efi_locate_search_type_t search_type,
+ grub_efi_guid_t *protocol,
+ void *search_key,
+ grub_efi_uintn_t *num_handles)
+{
+ grub_efi_boot_services_t *b;
+ grub_efi_status_t status;
+ grub_efi_handle_t *buffer;
+ grub_efi_uintn_t buffer_size = 8 * sizeof (grub_efi_handle_t);
+
+ buffer = grub_malloc (buffer_size);
+ if (! buffer)
+ return 0;
+
+ b = grub_efi_system_table->boot_services;
+ status = efi_call_5 (b->locate_handle, search_type, protocol, search_key,
+ &buffer_size, buffer);
+ if (status == GRUB_EFI_BUFFER_TOO_SMALL)
+ {
+ grub_free (buffer);
+ buffer = grub_malloc (buffer_size);
+ if (! buffer)
+ return 0;
+
+ status = efi_call_5 (b->locate_handle, search_type, protocol, search_key,
+ &buffer_size, buffer);
+ }
+
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_free (buffer);
+ return 0;
+ }
+
+ *num_handles = buffer_size / sizeof (grub_efi_handle_t);
+ return buffer;
+}
+
+void *
+grub_efi_open_protocol (grub_efi_handle_t handle,
+ grub_efi_guid_t *protocol,
+ grub_efi_uint32_t attributes)
+{
+ grub_efi_boot_services_t *b;
+ grub_efi_status_t status;
+ void *interface;
+
+ b = grub_efi_system_table->boot_services;
+ status = efi_call_6 (b->open_protocol, handle,
+ protocol,
+ &interface,
+ grub_efi_image_handle,
+ 0,
+ attributes);
+ if (status != GRUB_EFI_SUCCESS)
+ return 0;
+
+ return interface;
+}
+
+int
+grub_efi_set_text_mode (int on)
+{
+ grub_efi_console_control_protocol_t *c;
+ grub_efi_screen_mode_t mode, new_mode;
+
+ c = grub_efi_locate_protocol (&console_control_guid, 0);
+ if (! c)
+ /* No console control protocol instance available, assume it is
+ already in text mode. */
+ return 1;
+
+ if (efi_call_4 (c->get_mode, c, &mode, 0, 0) != GRUB_EFI_SUCCESS)
+ return 0;
+
+ new_mode = on ? GRUB_EFI_SCREEN_TEXT : GRUB_EFI_SCREEN_GRAPHICS;
+ if (mode != new_mode)
+ if (efi_call_2 (c->set_mode, c, new_mode) != GRUB_EFI_SUCCESS)
+ return 0;
+
+ return 1;
+}
+
+void
+grub_efi_stall (grub_efi_uintn_t microseconds)
+{
+ efi_call_1 (grub_efi_system_table->boot_services->stall, microseconds);
+}
+
+grub_efi_loaded_image_t *
+grub_efi_get_loaded_image (grub_efi_handle_t image_handle)
+{
+ return grub_efi_open_protocol (image_handle,
+ &loaded_image_guid,
+ GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+}
+
+void
+grub_reboot (void)
+{
+ grub_machine_fini (GRUB_LOADER_FLAG_NORETURN |
+ GRUB_LOADER_FLAG_EFI_KEEP_ALLOCATED_MEMORY);
+ efi_call_4 (grub_efi_system_table->runtime_services->reset_system,
+ GRUB_EFI_RESET_COLD, GRUB_EFI_SUCCESS, 0, NULL);
+ for (;;) ;
+}
+
+void
+grub_exit (void)
+{
+ grub_machine_fini (GRUB_LOADER_FLAG_NORETURN);
+ efi_call_4 (grub_efi_system_table->boot_services->exit,
+ grub_efi_image_handle, GRUB_EFI_SUCCESS, 0, 0);
+ for (;;) ;
+}
+
+grub_err_t
+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,
+ grub_efi_memory_descriptor_t *virtual_map)
+{
+ grub_efi_runtime_services_t *r;
+ grub_efi_status_t status;
+
+ r = grub_efi_system_table->runtime_services;
+ status = efi_call_4 (r->set_virtual_address_map, memory_map_size,
+ descriptor_size, descriptor_version, virtual_map);
+
+ if (status == GRUB_EFI_SUCCESS)
+ return GRUB_ERR_NONE;
+
+ return grub_error (GRUB_ERR_IO, "set_virtual_address_map failed");
+}
+
+grub_err_t
+grub_efi_set_variable(const char *var, const grub_efi_guid_t *guid,
+ void *data, grub_size_t datasize)
+{
+ grub_efi_status_t status;
+ grub_efi_runtime_services_t *r;
+ grub_efi_char16_t *var16;
+ grub_size_t len, len16;
+
+ len = grub_strlen (var);
+ len16 = len * GRUB_MAX_UTF16_PER_UTF8;
+ var16 = grub_calloc (len16 + 1, sizeof (var16[0]));
+ if (!var16)
+ return grub_errno;
+ len16 = grub_utf8_to_utf16 (var16, len16, (grub_uint8_t *) var, len, NULL);
+ var16[len16] = 0;
+
+ r = grub_efi_system_table->runtime_services;
+
+ status = efi_call_5 (r->set_variable, var16, guid,
+ (GRUB_EFI_VARIABLE_NON_VOLATILE
+ | GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS
+ | GRUB_EFI_VARIABLE_RUNTIME_ACCESS),
+ datasize, data);
+ grub_free (var16);
+ if (status == GRUB_EFI_SUCCESS)
+ return GRUB_ERR_NONE;
+
+ return grub_error (GRUB_ERR_IO, "could not set EFI variable `%s'", var);
+}
+
+grub_efi_status_t
+grub_efi_get_variable_with_attributes (const char *var,
+ const grub_efi_guid_t *guid,
+ grub_size_t *datasize_out,
+ void **data_out,
+ grub_efi_uint32_t *attributes)
+{
+ grub_efi_status_t status;
+ grub_efi_uintn_t datasize = 0;
+ grub_efi_runtime_services_t *r;
+ grub_efi_char16_t *var16;
+ void *data;
+ grub_size_t len, len16;
+
+ *data_out = NULL;
+ *datasize_out = 0;
+
+ len = grub_strlen (var);
+ len16 = len * GRUB_MAX_UTF16_PER_UTF8;
+ var16 = grub_calloc (len16 + 1, sizeof (var16[0]));
+ if (!var16)
+ return GRUB_EFI_OUT_OF_RESOURCES;
+ len16 = grub_utf8_to_utf16 (var16, len16, (grub_uint8_t *) var, len, NULL);
+ var16[len16] = 0;
+
+ r = grub_efi_system_table->runtime_services;
+
+ status = efi_call_5 (r->get_variable, var16, guid, NULL, &datasize, NULL);
+
+ if (status != GRUB_EFI_BUFFER_TOO_SMALL || !datasize)
+ {
+ grub_free (var16);
+ return status;
+ }
+
+ data = grub_malloc (datasize);
+ if (!data)
+ {
+ grub_free (var16);
+ return GRUB_EFI_OUT_OF_RESOURCES;
+ }
+
+ status = efi_call_5 (r->get_variable, var16, guid, attributes, &datasize, data);
+ grub_free (var16);
+
+ if (status == GRUB_EFI_SUCCESS)
+ {
+ *data_out = data;
+ *datasize_out = datasize;
+ return status;
+ }
+
+ grub_free (data);
+ return status;
+}
+
+grub_efi_status_t
+grub_efi_get_variable (const char *var, const grub_efi_guid_t *guid,
+ grub_size_t *datasize_out, void **data_out)
+{
+ return grub_efi_get_variable_with_attributes (var, guid, datasize_out, data_out, NULL);
+}
+
+#pragma GCC diagnostic ignored "-Wcast-align"
+
+/* Search the mods section from the PE32/PE32+ image. This code uses
+ a PE32 header, but should work with PE32+ as well. */
+grub_addr_t
+grub_efi_modules_addr (void)
+{
+ grub_efi_loaded_image_t *image;
+ struct grub_pe32_header *header;
+ struct grub_pe32_coff_header *coff_header;
+ struct grub_pe32_section_table *sections;
+ struct grub_pe32_section_table *section;
+ struct grub_module_info *info;
+ grub_uint16_t i;
+
+ image = grub_efi_get_loaded_image (grub_efi_image_handle);
+ if (! image)
+ return 0;
+
+ header = image->image_base;
+ coff_header = &(header->coff_header);
+ sections
+ = (struct grub_pe32_section_table *) ((char *) coff_header
+ + sizeof (*coff_header)
+ + coff_header->optional_header_size);
+
+ for (i = 0, section = sections;
+ i < coff_header->num_sections;
+ i++, section++)
+ {
+ if (grub_strcmp (section->name, "mods") == 0)
+ break;
+ }
+
+ if (i == coff_header->num_sections)
+ {
+ grub_dprintf("sections", "section %d is last section; invalid.\n", i);
+ return 0;
+ }
+
+ info = (struct grub_module_info *) ((char *) image->image_base
+ + section->virtual_address);
+ if (section->name[0] != '.' && info->magic != GRUB_MODULE_MAGIC)
+ {
+ grub_dprintf("sections",
+ "section %d has bad magic %08x, should be %08x\n",
+ i, info->magic, GRUB_MODULE_MAGIC);
+ return 0;
+ }
+
+ grub_dprintf("sections", "returning section info for section %d: \"%s\"\n",
+ i, section->name);
+ return (grub_addr_t) info;
+}
+
+#pragma GCC diagnostic error "-Wcast-align"
+
+char *
+grub_efi_get_filename (grub_efi_device_path_t *dp0)
+{
+ char *name = 0, *p, *pi;
+ grub_size_t filesize = 0;
+ grub_efi_device_path_t *dp;
+
+ if (!dp0)
+ return NULL;
+
+ dp = dp0;
+
+ while (dp)
+ {
+ grub_efi_uint8_t type = GRUB_EFI_DEVICE_PATH_TYPE (dp);
+ grub_efi_uint8_t subtype = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp);
+
+ if (type == GRUB_EFI_END_DEVICE_PATH_TYPE)
+ break;
+ if (type == GRUB_EFI_MEDIA_DEVICE_PATH_TYPE
+ && subtype == GRUB_EFI_FILE_PATH_DEVICE_PATH_SUBTYPE)
+ {
+ grub_efi_uint16_t len = GRUB_EFI_DEVICE_PATH_LENGTH (dp);
+
+ if (len < 4)
+ {
+ grub_error (GRUB_ERR_OUT_OF_RANGE,
+ "malformed EFI Device Path node has length=%d", len);
+ return NULL;
+ }
+ len = (len - 4) / sizeof (grub_efi_char16_t);
+ filesize += GRUB_MAX_UTF8_PER_UTF16 * len + 2;
+ }
+
+ dp = GRUB_EFI_NEXT_DEVICE_PATH (dp);
+ }
+
+ if (!filesize)
+ return NULL;
+
+ dp = dp0;
+
+ p = name = grub_malloc (filesize);
+ if (!name)
+ return NULL;
+
+ while (dp)
+ {
+ grub_efi_uint8_t type = GRUB_EFI_DEVICE_PATH_TYPE (dp);
+ grub_efi_uint8_t subtype = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp);
+
+ if (type == GRUB_EFI_END_DEVICE_PATH_TYPE)
+ break;
+ else if (type == GRUB_EFI_MEDIA_DEVICE_PATH_TYPE
+ && subtype == GRUB_EFI_FILE_PATH_DEVICE_PATH_SUBTYPE)
+ {
+ grub_efi_file_path_device_path_t *fp;
+ grub_efi_uint16_t len;
+ grub_efi_char16_t *dup_name;
+
+ *p++ = '/';
+
+ len = GRUB_EFI_DEVICE_PATH_LENGTH (dp);
+ if (len < 4)
+ {
+ grub_error (GRUB_ERR_OUT_OF_RANGE,
+ "malformed EFI Device Path node has length=%d", len);
+ grub_free (name);
+ return NULL;
+ }
+
+ len = (len - 4) / sizeof (grub_efi_char16_t);
+ fp = (grub_efi_file_path_device_path_t *) dp;
+ /* According to EFI spec Path Name is NULL terminated */
+ while (len > 0 && fp->path_name[len - 1] == 0)
+ len--;
+
+ dup_name = grub_calloc (len, sizeof (*dup_name));
+ if (!dup_name)
+ {
+ grub_free (name);
+ return NULL;
+ }
+ p = (char *) grub_utf16_to_utf8 ((unsigned char *) p,
+ grub_memcpy (dup_name, fp->path_name, len * sizeof (*dup_name)),
+ len);
+ grub_free (dup_name);
+ }
+
+ dp = GRUB_EFI_NEXT_DEVICE_PATH (dp);
+ }
+
+ *p = '\0';
+
+ for (pi = name, p = name; *pi;)
+ {
+ /* EFI breaks paths with backslashes. */
+ if (*pi == '\\' || *pi == '/')
+ {
+ *p++ = '/';
+ while (*pi == '\\' || *pi == '/')
+ pi++;
+ continue;
+ }
+ *p++ = *pi++;
+ }
+ *p = '\0';
+
+ return name;
+}
+
+grub_efi_device_path_t *
+grub_efi_get_device_path (grub_efi_handle_t handle)
+{
+ return grub_efi_open_protocol (handle, &device_path_guid,
+ GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+}
+
+/* Return the device path node right before the end node. */
+grub_efi_device_path_t *
+grub_efi_find_last_device_path (const grub_efi_device_path_t *dp)
+{
+ grub_efi_device_path_t *next, *p;
+
+ if (GRUB_EFI_END_ENTIRE_DEVICE_PATH (dp))
+ return 0;
+
+ for (p = (grub_efi_device_path_t *) dp, next = GRUB_EFI_NEXT_DEVICE_PATH (p);
+ ! GRUB_EFI_END_ENTIRE_DEVICE_PATH (next);
+ p = next, next = GRUB_EFI_NEXT_DEVICE_PATH (next))
+ ;
+
+ return p;
+}
+
+/* Duplicate a device path. */
+grub_efi_device_path_t *
+grub_efi_duplicate_device_path (const grub_efi_device_path_t *dp)
+{
+ grub_efi_device_path_t *p;
+ grub_size_t total_size = 0;
+
+ for (p = (grub_efi_device_path_t *) dp;
+ ;
+ p = GRUB_EFI_NEXT_DEVICE_PATH (p))
+ {
+ grub_size_t len = GRUB_EFI_DEVICE_PATH_LENGTH (p);
+
+ /*
+ * In the event that we find a node that's completely garbage, for
+ * example if we get to 0x7f 0x01 0x02 0x00 ... (EndInstance with a size
+ * of 2), GRUB_EFI_END_ENTIRE_DEVICE_PATH() will be true and
+ * GRUB_EFI_NEXT_DEVICE_PATH() will return NULL, so we won't continue,
+ * and neither should our consumers, but there won't be any error raised
+ * even though the device path is junk.
+ *
+ * This keeps us from passing junk down back to our caller.
+ */
+ if (len < 4)
+ {
+ grub_error (GRUB_ERR_OUT_OF_RANGE,
+ "malformed EFI Device Path node has length=%" PRIuGRUB_SIZE, len);
+ return NULL;
+ }
+
+ total_size += len;
+ if (GRUB_EFI_END_ENTIRE_DEVICE_PATH (p))
+ break;
+ }
+
+ p = grub_malloc (total_size);
+ if (! p)
+ return 0;
+
+ grub_memcpy (p, dp, total_size);
+ return p;
+}
+
+static void
+dump_vendor_path (const char *type, grub_efi_vendor_device_path_t *vendor)
+{
+ grub_uint32_t vendor_data_len = vendor->header.length - sizeof (*vendor);
+ grub_printf ("/%sVendor(%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x)[%x: ",
+ type,
+ (unsigned) vendor->vendor_guid.data1,
+ (unsigned) vendor->vendor_guid.data2,
+ (unsigned) vendor->vendor_guid.data3,
+ (unsigned) vendor->vendor_guid.data4[0],
+ (unsigned) vendor->vendor_guid.data4[1],
+ (unsigned) vendor->vendor_guid.data4[2],
+ (unsigned) vendor->vendor_guid.data4[3],
+ (unsigned) vendor->vendor_guid.data4[4],
+ (unsigned) vendor->vendor_guid.data4[5],
+ (unsigned) vendor->vendor_guid.data4[6],
+ (unsigned) vendor->vendor_guid.data4[7],
+ vendor_data_len);
+ if (vendor->header.length > sizeof (*vendor))
+ {
+ grub_uint32_t i;
+ for (i = 0; i < vendor_data_len; i++)
+ grub_printf ("%02x ", vendor->vendor_defined_data[i]);
+ }
+ grub_printf ("]");
+}
+
+
+/* Print the chain of Device Path nodes. This is mainly for debugging. */
+void
+grub_efi_print_device_path (grub_efi_device_path_t *dp)
+{
+ while (GRUB_EFI_DEVICE_PATH_VALID (dp))
+ {
+ grub_efi_uint8_t type = GRUB_EFI_DEVICE_PATH_TYPE (dp);
+ grub_efi_uint8_t subtype = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp);
+ grub_efi_uint16_t len = GRUB_EFI_DEVICE_PATH_LENGTH (dp);
+
+ switch (type)
+ {
+ case GRUB_EFI_END_DEVICE_PATH_TYPE:
+ switch (subtype)
+ {
+ case GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE:
+ grub_printf ("/EndEntire\n");
+ //grub_putchar ('\n');
+ break;
+ case GRUB_EFI_END_THIS_DEVICE_PATH_SUBTYPE:
+ grub_printf ("/EndThis\n");
+ //grub_putchar ('\n');
+ break;
+ default:
+ grub_printf ("/EndUnknown(%x)\n", (unsigned) subtype);
+ break;
+ }
+ break;
+
+ case GRUB_EFI_HARDWARE_DEVICE_PATH_TYPE:
+ switch (subtype)
+ {
+ case GRUB_EFI_PCI_DEVICE_PATH_SUBTYPE:
+ {
+ grub_efi_pci_device_path_t *pci
+ = (grub_efi_pci_device_path_t *) dp;
+ grub_printf ("/PCI(%x,%x)",
+ (unsigned) pci->function, (unsigned) pci->device);
+ }
+ break;
+ case GRUB_EFI_PCCARD_DEVICE_PATH_SUBTYPE:
+ {
+ grub_efi_pccard_device_path_t *pccard
+ = (grub_efi_pccard_device_path_t *) dp;
+ grub_printf ("/PCCARD(%x)",
+ (unsigned) pccard->function);
+ }
+ break;
+ case GRUB_EFI_MEMORY_MAPPED_DEVICE_PATH_SUBTYPE:
+ {
+ grub_efi_memory_mapped_device_path_t *mmapped
+ = (grub_efi_memory_mapped_device_path_t *) dp;
+ grub_printf ("/MMap(%x,%llx,%llx)",
+ (unsigned) mmapped->memory_type,
+ (unsigned long long) mmapped->start_address,
+ (unsigned long long) mmapped->end_address);
+ }
+ break;
+ case GRUB_EFI_VENDOR_DEVICE_PATH_SUBTYPE:
+ dump_vendor_path ("Hardware",
+ (grub_efi_vendor_device_path_t *) dp);
+ break;
+ case GRUB_EFI_CONTROLLER_DEVICE_PATH_SUBTYPE:
+ {
+ grub_efi_controller_device_path_t *controller
+ = (grub_efi_controller_device_path_t *) dp;
+ grub_printf ("/Ctrl(%x)",
+ (unsigned) controller->controller_number);
+ }
+ break;
+ default:
+ grub_printf ("/UnknownHW(%x)", (unsigned) subtype);
+ break;
+ }
+ break;
+
+ case GRUB_EFI_ACPI_DEVICE_PATH_TYPE:
+ switch (subtype)
+ {
+ case GRUB_EFI_ACPI_DEVICE_PATH_SUBTYPE:
+ {
+ grub_efi_acpi_device_path_t *acpi
+ = (grub_efi_acpi_device_path_t *) dp;
+ grub_printf ("/ACPI(%x,%x)",
+ (unsigned) acpi->hid,
+ (unsigned) acpi->uid);
+ }
+ break;
+ case GRUB_EFI_EXPANDED_ACPI_DEVICE_PATH_SUBTYPE:
+ {
+ grub_efi_expanded_acpi_device_path_t *eacpi
+ = (grub_efi_expanded_acpi_device_path_t *) dp;
+ grub_printf ("/ACPI(");
+
+ if (GRUB_EFI_EXPANDED_ACPI_HIDSTR (dp)[0] == '\0')
+ grub_printf ("%x,", (unsigned) eacpi->hid);
+ else
+ grub_printf ("%s,", GRUB_EFI_EXPANDED_ACPI_HIDSTR (dp));
+
+ if (GRUB_EFI_EXPANDED_ACPI_UIDSTR (dp)[0] == '\0')
+ grub_printf ("%x,", (unsigned) eacpi->uid);
+ else
+ grub_printf ("%s,", GRUB_EFI_EXPANDED_ACPI_UIDSTR (dp));
+
+ if (GRUB_EFI_EXPANDED_ACPI_CIDSTR (dp)[0] == '\0')
+ grub_printf ("%x)", (unsigned) eacpi->cid);
+ else
+ grub_printf ("%s)", GRUB_EFI_EXPANDED_ACPI_CIDSTR (dp));
+ }
+ break;
+ default:
+ grub_printf ("/UnknownACPI(%x)", (unsigned) subtype);
+ break;
+ }
+ break;
+
+ case GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE:
+ switch (subtype)
+ {
+ case GRUB_EFI_ATAPI_DEVICE_PATH_SUBTYPE:
+ {
+ grub_efi_atapi_device_path_t *atapi
+ = (grub_efi_atapi_device_path_t *) dp;
+ grub_printf ("/ATAPI(%x,%x,%x)",
+ (unsigned) atapi->primary_secondary,
+ (unsigned) atapi->slave_master,
+ (unsigned) atapi->lun);
+ }
+ break;
+ case GRUB_EFI_SCSI_DEVICE_PATH_SUBTYPE:
+ {
+ grub_efi_scsi_device_path_t *scsi
+ = (grub_efi_scsi_device_path_t *) dp;
+ grub_printf ("/SCSI(%x,%x)",
+ (unsigned) scsi->pun,
+ (unsigned) scsi->lun);
+ }
+ break;
+ case GRUB_EFI_FIBRE_CHANNEL_DEVICE_PATH_SUBTYPE:
+ {
+ grub_efi_fibre_channel_device_path_t *fc
+ = (grub_efi_fibre_channel_device_path_t *) dp;
+ grub_printf ("/FibreChannel(%llx,%llx)",
+ (unsigned long long) fc->wwn,
+ (unsigned long long) fc->lun);
+ }
+ break;
+ case GRUB_EFI_1394_DEVICE_PATH_SUBTYPE:
+ {
+ grub_efi_1394_device_path_t *firewire
+ = (grub_efi_1394_device_path_t *) dp;
+ grub_printf ("/1394(%llx)",
+ (unsigned long long) firewire->guid);
+ }
+ break;
+ case GRUB_EFI_USB_DEVICE_PATH_SUBTYPE:
+ {
+ grub_efi_usb_device_path_t *usb
+ = (grub_efi_usb_device_path_t *) dp;
+ grub_printf ("/USB(%x,%x)",
+ (unsigned) usb->parent_port_number,
+ (unsigned) usb->usb_interface);
+ }
+ break;
+ case GRUB_EFI_USB_CLASS_DEVICE_PATH_SUBTYPE:
+ {
+ grub_efi_usb_class_device_path_t *usb_class
+ = (grub_efi_usb_class_device_path_t *) dp;
+ grub_printf ("/USBClass(%x,%x,%x,%x,%x)",
+ (unsigned) usb_class->vendor_id,
+ (unsigned) usb_class->product_id,
+ (unsigned) usb_class->device_class,
+ (unsigned) usb_class->device_subclass,
+ (unsigned) usb_class->device_protocol);
+ }
+ break;
+ case GRUB_EFI_I2O_DEVICE_PATH_SUBTYPE:
+ {
+ grub_efi_i2o_device_path_t *i2o
+ = (grub_efi_i2o_device_path_t *) dp;
+ grub_printf ("/I2O(%x)", (unsigned) i2o->tid);
+ }
+ break;
+ case GRUB_EFI_MAC_ADDRESS_DEVICE_PATH_SUBTYPE:
+ {
+ grub_efi_mac_address_device_path_t *mac
+ = (grub_efi_mac_address_device_path_t *) dp;
+ grub_printf ("/MacAddr(%02x:%02x:%02x:%02x:%02x:%02x,%x)",
+ (unsigned) mac->mac_address[0],
+ (unsigned) mac->mac_address[1],
+ (unsigned) mac->mac_address[2],
+ (unsigned) mac->mac_address[3],
+ (unsigned) mac->mac_address[4],
+ (unsigned) mac->mac_address[5],
+ (unsigned) mac->if_type);
+ }
+ break;
+ case GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE:
+ {
+ grub_efi_ipv4_device_path_t *ipv4
+ = (grub_efi_ipv4_device_path_t *) dp;
+ grub_printf ("/IPv4(%u.%u.%u.%u,%u.%u.%u.%u,%u,%u,%x,%x)",
+ (unsigned) ipv4->local_ip_address[0],
+ (unsigned) ipv4->local_ip_address[1],
+ (unsigned) ipv4->local_ip_address[2],
+ (unsigned) ipv4->local_ip_address[3],
+ (unsigned) ipv4->remote_ip_address[0],
+ (unsigned) ipv4->remote_ip_address[1],
+ (unsigned) ipv4->remote_ip_address[2],
+ (unsigned) ipv4->remote_ip_address[3],
+ (unsigned) ipv4->local_port,
+ (unsigned) ipv4->remote_port,
+ (unsigned) ipv4->protocol,
+ (unsigned) ipv4->static_ip_address);
+ }
+ break;
+ case GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE:
+ {
+ grub_efi_ipv6_device_path_t *ipv6
+ = (grub_efi_ipv6_device_path_t *) dp;
+ grub_printf ("/IPv6(%x:%x:%x:%x:%x:%x:%x:%x,%x:%x:%x:%x:%x:%x:%x:%x,%u,%u,%x,%x)",
+ (unsigned) ipv6->local_ip_address[0],
+ (unsigned) ipv6->local_ip_address[1],
+ (unsigned) ipv6->local_ip_address[2],
+ (unsigned) ipv6->local_ip_address[3],
+ (unsigned) ipv6->local_ip_address[4],
+ (unsigned) ipv6->local_ip_address[5],
+ (unsigned) ipv6->local_ip_address[6],
+ (unsigned) ipv6->local_ip_address[7],
+ (unsigned) ipv6->remote_ip_address[0],
+ (unsigned) ipv6->remote_ip_address[1],
+ (unsigned) ipv6->remote_ip_address[2],
+ (unsigned) ipv6->remote_ip_address[3],
+ (unsigned) ipv6->remote_ip_address[4],
+ (unsigned) ipv6->remote_ip_address[5],
+ (unsigned) ipv6->remote_ip_address[6],
+ (unsigned) ipv6->remote_ip_address[7],
+ (unsigned) ipv6->local_port,
+ (unsigned) ipv6->remote_port,
+ (unsigned) ipv6->protocol,
+ (unsigned) ipv6->static_ip_address);
+ }
+ break;
+ case GRUB_EFI_INFINIBAND_DEVICE_PATH_SUBTYPE:
+ {
+ grub_efi_infiniband_device_path_t *ib
+ = (grub_efi_infiniband_device_path_t *) dp;
+ grub_printf ("/InfiniBand(%x,%llx,%llx,%llx)",
+ (unsigned) ib->port_gid[0], /* XXX */
+ (unsigned long long) ib->remote_id,
+ (unsigned long long) ib->target_port_id,
+ (unsigned long long) ib->device_id);
+ }
+ break;
+ case GRUB_EFI_UART_DEVICE_PATH_SUBTYPE:
+ {
+ grub_efi_uart_device_path_t *uart
+ = (grub_efi_uart_device_path_t *) dp;
+ grub_printf ("/UART(%llu,%u,%x,%x)",
+ (unsigned long long) uart->baud_rate,
+ uart->data_bits,
+ uart->parity,
+ uart->stop_bits);
+ }
+ break;
+ case GRUB_EFI_SATA_DEVICE_PATH_SUBTYPE:
+ {
+ grub_efi_sata_device_path_t *sata;
+ sata = (grub_efi_sata_device_path_t *) dp;
+ grub_printf ("/Sata(%x,%x,%x)",
+ sata->hba_port,
+ sata->multiplier_port,
+ sata->lun);
+ }
+ break;
+
+ case GRUB_EFI_VENDOR_MESSAGING_DEVICE_PATH_SUBTYPE:
+ dump_vendor_path ("Messaging",
+ (grub_efi_vendor_device_path_t *) dp);
+ break;
+ default:
+ grub_printf ("/UnknownMessaging(%x)", (unsigned) subtype);
+ break;
+ }
+ break;
+
+ case GRUB_EFI_MEDIA_DEVICE_PATH_TYPE:
+ switch (subtype)
+ {
+ case GRUB_EFI_HARD_DRIVE_DEVICE_PATH_SUBTYPE:
+ {
+ grub_efi_hard_drive_device_path_t *hd = (grub_efi_hard_drive_device_path_t *) dp;
+ grub_printf ("/HD(%u,%llx,%llx,%02x%02x%02x%02x%02x%02x%02x%02x,%x,%x)",
+ hd->partition_number,
+ (unsigned long long) hd->partition_start,
+ (unsigned long long) hd->partition_size,
+ (unsigned) hd->partition_signature[0],
+ (unsigned) hd->partition_signature[1],
+ (unsigned) hd->partition_signature[2],
+ (unsigned) hd->partition_signature[3],
+ (unsigned) hd->partition_signature[4],
+ (unsigned) hd->partition_signature[5],
+ (unsigned) hd->partition_signature[6],
+ (unsigned) hd->partition_signature[7],
+ (unsigned) hd->partmap_type,
+ (unsigned) hd->signature_type);
+ }
+ break;
+ case GRUB_EFI_CDROM_DEVICE_PATH_SUBTYPE:
+ {
+ grub_efi_cdrom_device_path_t *cd
+ = (grub_efi_cdrom_device_path_t *) dp;
+ grub_printf ("/CD(%u,%llx,%llx)",
+ cd->boot_entry,
+ (unsigned long long) cd->partition_start,
+ (unsigned long long) cd->partition_size);
+ }
+ break;
+ case GRUB_EFI_VENDOR_MEDIA_DEVICE_PATH_SUBTYPE:
+ dump_vendor_path ("Media",
+ (grub_efi_vendor_device_path_t *) dp);
+ break;
+ case GRUB_EFI_FILE_PATH_DEVICE_PATH_SUBTYPE:
+ {
+ grub_efi_file_path_device_path_t *fp;
+ grub_uint8_t *buf;
+ fp = (grub_efi_file_path_device_path_t *) dp;
+ buf = grub_malloc ((len - 4) * 2 + 1);
+ if (buf)
+ {
+ grub_efi_char16_t *dup_name = grub_malloc (len - 4);
+ if (!dup_name)
+ {
+ grub_errno = GRUB_ERR_NONE;
+ grub_printf ("/File((null))");
+ grub_free (buf);
+ break;
+ }
+ *grub_utf16_to_utf8 (buf, grub_memcpy (dup_name, fp->path_name, len - 4),
+ (len - 4) / sizeof (grub_efi_char16_t))
+ = '\0';
+ grub_free (dup_name);
+ }
+ else
+ grub_errno = GRUB_ERR_NONE;
+ grub_printf ("/File(%s)", buf);
+ grub_free (buf);
+ }
+ break;
+ case GRUB_EFI_PROTOCOL_DEVICE_PATH_SUBTYPE:
+ {
+ grub_efi_protocol_device_path_t *proto
+ = (grub_efi_protocol_device_path_t *) dp;
+ grub_printf ("/Protocol(%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x)",
+ (unsigned) proto->guid.data1,
+ (unsigned) proto->guid.data2,
+ (unsigned) proto->guid.data3,
+ (unsigned) proto->guid.data4[0],
+ (unsigned) proto->guid.data4[1],
+ (unsigned) proto->guid.data4[2],
+ (unsigned) proto->guid.data4[3],
+ (unsigned) proto->guid.data4[4],
+ (unsigned) proto->guid.data4[5],
+ (unsigned) proto->guid.data4[6],
+ (unsigned) proto->guid.data4[7]);
+ }
+ break;
+ default:
+ grub_printf ("/UnknownMedia(%x)", (unsigned) subtype);
+ break;
+ }
+ break;
+
+ case GRUB_EFI_BIOS_DEVICE_PATH_TYPE:
+ switch (subtype)
+ {
+ case GRUB_EFI_BIOS_DEVICE_PATH_SUBTYPE:
+ {
+ grub_efi_bios_device_path_t *bios
+ = (grub_efi_bios_device_path_t *) dp;
+ grub_printf ("/BIOS(%x,%x,%s)",
+ (unsigned) bios->device_type,
+ (unsigned) bios->status_flags,
+ (char *) (dp + 1));
+ }
+ break;
+ default:
+ grub_printf ("/UnknownBIOS(%x)", (unsigned) subtype);
+ break;
+ }
+ break;
+
+ default:
+ grub_printf ("/UnknownType(%x,%x)\n",
+ (unsigned) type,
+ (unsigned) subtype);
+ return;
+ break;
+ }
+
+ if (GRUB_EFI_END_ENTIRE_DEVICE_PATH (dp))
+ break;
+
+ dp = (grub_efi_device_path_t *) ((char *) dp + len);
+ }
+}
+
+/* Compare device paths. */
+int
+grub_efi_compare_device_paths (const grub_efi_device_path_t *dp1,
+ const grub_efi_device_path_t *dp2)
+{
+ if (! dp1 || ! dp2)
+ /* Return non-zero. */
+ return 1;
+
+ if (dp1 == dp2)
+ return 0;
+
+ while (GRUB_EFI_DEVICE_PATH_VALID (dp1) && GRUB_EFI_DEVICE_PATH_VALID (dp2))
+ {
+ grub_efi_uint8_t type1, type2;
+ grub_efi_uint8_t subtype1, subtype2;
+ grub_efi_uint16_t len1, len2;
+ int ret;
+
+ type1 = GRUB_EFI_DEVICE_PATH_TYPE (dp1);
+ type2 = GRUB_EFI_DEVICE_PATH_TYPE (dp2);
+
+ if (type1 != type2)
+ return (int) type2 - (int) type1;
+
+ subtype1 = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp1);
+ subtype2 = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp2);
+
+ if (subtype1 != subtype2)
+ return (int) subtype1 - (int) subtype2;
+
+ len1 = GRUB_EFI_DEVICE_PATH_LENGTH (dp1);
+ len2 = GRUB_EFI_DEVICE_PATH_LENGTH (dp2);
+
+ if (len1 != len2)
+ return (int) len1 - (int) len2;
+
+ ret = grub_memcmp (dp1, dp2, len1);
+ if (ret != 0)
+ return ret;
+
+ if (GRUB_EFI_END_ENTIRE_DEVICE_PATH (dp1))
+ break;
+
+ dp1 = (grub_efi_device_path_t *) ((char *) dp1 + len1);
+ dp2 = (grub_efi_device_path_t *) ((char *) dp2 + len2);
+ }
+
+ /*
+ * There's no "right" answer here, but we probably don't want to call a valid
+ * dp and an invalid dp equal, so pick one way or the other.
+ */
+ if (GRUB_EFI_DEVICE_PATH_VALID (dp1) && !GRUB_EFI_DEVICE_PATH_VALID (dp2))
+ return 1;
+ else if (!GRUB_EFI_DEVICE_PATH_VALID (dp1) && GRUB_EFI_DEVICE_PATH_VALID (dp2))
+ return -1;
+
+ return 0;
+}
diff --git a/grub-core/kern/efi/fdt.c b/grub-core/kern/efi/fdt.c
new file mode 100644
index 0000000..30100c6
--- /dev/null
+++ b/grub-core/kern/efi/fdt.c
@@ -0,0 +1,43 @@
+/* fdt.c - EFI Flattened Device Tree interaction */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/efi/efi.h>
+#include <grub/mm.h>
+
+void *
+grub_efi_get_firmware_fdt (void)
+{
+ grub_efi_configuration_table_t *tables;
+ grub_efi_guid_t fdt_guid = GRUB_EFI_DEVICE_TREE_GUID;
+ void *firmware_fdt = NULL;
+ unsigned int i;
+
+ /* Look for FDT in UEFI config tables. */
+ tables = grub_efi_system_table->configuration_table;
+
+ for (i = 0; i < grub_efi_system_table->num_table_entries; i++)
+ if (grub_memcmp (&tables[i].vendor_guid, &fdt_guid, sizeof (fdt_guid)) == 0)
+ {
+ firmware_fdt = tables[i].vendor_table;
+ grub_dprintf ("linux", "found registered FDT @ %p\n", firmware_fdt);
+ break;
+ }
+
+ return firmware_fdt;
+}
diff --git a/grub-core/kern/efi/init.c b/grub-core/kern/efi/init.c
new file mode 100644
index 0000000..7facacf
--- /dev/null
+++ b/grub-core/kern/efi/init.c
@@ -0,0 +1,149 @@
+/* init.c - generic EFI initialization and finalization */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/efi/efi.h>
+#include <grub/efi/console.h>
+#include <grub/efi/disk.h>
+#include <grub/efi/sb.h>
+#include <grub/lockdown.h>
+#include <grub/term.h>
+#include <grub/misc.h>
+#include <grub/env.h>
+#include <grub/mm.h>
+#include <grub/kernel.h>
+#include <grub/stack_protector.h>
+
+#ifdef GRUB_STACK_PROTECTOR
+
+static grub_efi_guid_t rng_protocol_guid = GRUB_EFI_RNG_PROTOCOL_GUID;
+
+/*
+ * Don't put this on grub_efi_init()'s local stack to avoid it
+ * getting a stack check.
+ */
+static grub_efi_uint8_t stack_chk_guard_buf[32];
+
+grub_addr_t __stack_chk_guard;
+
+void __attribute__ ((noreturn))
+__stack_chk_fail (void)
+{
+ /*
+ * Assume it's not safe to call into EFI Boot Services. Sorry, that
+ * means no console message here.
+ */
+ do
+ {
+ /* Do not optimize out the loop. */
+ asm volatile ("");
+ }
+ while (1);
+}
+
+static void
+stack_protector_init (void)
+{
+ grub_efi_rng_protocol_t *rng;
+
+ /* Set up the stack canary. Make errors here non-fatal for now. */
+ rng = grub_efi_locate_protocol (&rng_protocol_guid, NULL);
+ if (rng != NULL)
+ {
+ grub_efi_status_t status;
+
+ status = efi_call_4 (rng->get_rng, rng, NULL, sizeof (stack_chk_guard_buf),
+ stack_chk_guard_buf);
+ if (status == GRUB_EFI_SUCCESS)
+ grub_memcpy (&__stack_chk_guard, stack_chk_guard_buf, sizeof (__stack_chk_guard));
+ }
+}
+#else
+static void
+stack_protector_init (void)
+{
+}
+#endif
+
+grub_addr_t grub_modbase;
+
+void
+grub_efi_init (void)
+{
+ grub_modbase = grub_efi_modules_addr ();
+ /* First of all, initialize the console so that GRUB can display
+ messages. */
+ grub_console_init ();
+
+ stack_protector_init ();
+
+ /* Initialize the memory management system. */
+ grub_efi_mm_init ();
+
+ /*
+ * Lockdown the GRUB and register the shim_lock verifier
+ * if the UEFI Secure Boot is enabled.
+ */
+ if (grub_efi_get_secureboot () == GRUB_EFI_SECUREBOOT_MODE_ENABLED)
+ {
+ grub_lockdown ();
+ grub_shim_lock_verifier_setup ();
+ }
+
+ efi_call_4 (grub_efi_system_table->boot_services->set_watchdog_timer,
+ 0, 0, 0, NULL);
+
+ grub_efidisk_init ();
+}
+
+void (*grub_efi_net_config) (grub_efi_handle_t hnd,
+ char **device,
+ char **path);
+
+void
+grub_machine_get_bootlocation (char **device, char **path)
+{
+ grub_efi_loaded_image_t *image = NULL;
+ char *p;
+
+ image = grub_efi_get_loaded_image (grub_efi_image_handle);
+ if (!image)
+ return;
+ *device = grub_efidisk_get_device_name (image->device_handle);
+ if (!*device && grub_efi_net_config)
+ {
+ grub_efi_net_config (image->device_handle, device, path);
+ return;
+ }
+
+ *path = grub_efi_get_filename (image->file_path);
+ if (*path)
+ {
+ /* Get the directory. */
+ p = grub_strrchr (*path, '/');
+ if (p)
+ *p = '\0';
+ }
+}
+
+void
+grub_efi_fini (void)
+{
+ grub_efidisk_fini ();
+ grub_console_fini ();
+}
diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c
new file mode 100644
index 0000000..9838fb2
--- /dev/null
+++ b/grub-core/kern/efi/mm.c
@@ -0,0 +1,691 @@
+/* mm.c - generic EFI memory management */
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/efi/api.h>
+#include <grub/efi/efi.h>
+#include <grub/cpu/efi/memory.h>
+
+#if defined (__i386__) || defined (__x86_64__)
+#include <grub/pci.h>
+#endif
+
+#define NEXT_MEMORY_DESCRIPTOR(desc, size) \
+ ((grub_efi_memory_descriptor_t *) ((char *) (desc) + (size)))
+
+#define BYTES_TO_PAGES(bytes) (((bytes) + 0xfff) >> 12)
+#define BYTES_TO_PAGES_DOWN(bytes) ((bytes) >> 12)
+#define PAGES_TO_BYTES(pages) ((pages) << 12)
+
+/* The size of a memory map obtained from the firmware. This must be
+ a multiplier of 4KB. */
+#define MEMORY_MAP_SIZE 0x3000
+
+/* The minimum and maximum heap size for GRUB itself. */
+#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;
+
+/*
+ * We need to roll back EFI allocations on exit. Remember allocations that
+ * we'll free on exit.
+ */
+struct efi_allocation;
+struct efi_allocation {
+ grub_efi_physical_address_t address;
+ grub_efi_uint64_t pages;
+ struct efi_allocation *next;
+};
+static struct efi_allocation *efi_allocated_memory;
+
+static void
+grub_efi_store_alloc (grub_efi_physical_address_t address,
+ grub_efi_uintn_t pages)
+{
+ grub_efi_boot_services_t *b;
+ struct efi_allocation *alloc;
+ grub_efi_status_t status;
+
+ b = grub_efi_system_table->boot_services;
+ status = efi_call_3 (b->allocate_pool, GRUB_EFI_LOADER_DATA,
+ sizeof(*alloc), (void**)&alloc);
+
+ if (status == GRUB_EFI_SUCCESS)
+ {
+ alloc->next = efi_allocated_memory;
+ alloc->address = address;
+ alloc->pages = pages;
+ efi_allocated_memory = alloc;
+ }
+ else
+ grub_printf ("Could not malloc memory to remember EFI allocation. "
+ "Exiting GRUB won't free all memory.\n");
+}
+
+static void
+grub_efi_drop_alloc (grub_efi_physical_address_t address,
+ grub_efi_uintn_t pages)
+{
+ struct efi_allocation *ea, *eap;
+ grub_efi_boot_services_t *b;
+
+ b = grub_efi_system_table->boot_services;
+
+ for (eap = NULL, ea = efi_allocated_memory; ea; eap = ea, ea = ea->next)
+ {
+ if (ea->address != address || ea->pages != pages)
+ continue;
+
+ /* Remove the current entry from the list. */
+ if (eap)
+ eap->next = ea->next;
+ else
+ efi_allocated_memory = ea->next;
+
+ /* Then free the memory backing it. */
+ efi_call_1 (b->free_pool, ea);
+
+ /* And leave, we're done. */
+ break;
+ }
+}
+
+/* Allocate pages. Return the pointer to the first of allocated pages. */
+void *
+grub_efi_allocate_pages_real (grub_efi_physical_address_t address,
+ grub_efi_uintn_t pages,
+ grub_efi_allocate_type_t alloctype,
+ grub_efi_memory_type_t memtype)
+{
+ grub_efi_status_t status;
+ grub_efi_boot_services_t *b;
+
+ /* Limit the memory access to less than 4GB for 32-bit platforms. */
+ if (address > GRUB_EFI_MAX_USABLE_ADDRESS)
+ {
+ char inv_addr[17], max_addr[17]; /* log16(2^64) = 16, plus NUL. */
+
+ grub_snprintf (inv_addr, sizeof (inv_addr) - 1, "%" PRIxGRUB_UINT64_T,
+ address);
+ grub_snprintf (max_addr, sizeof (max_addr) - 1, "%" PRIxGRUB_UINT64_T,
+ (grub_efi_uint64_t) GRUB_EFI_MAX_USABLE_ADDRESS);
+ grub_error (GRUB_ERR_BAD_ARGUMENT,
+ N_("invalid memory address (0x%s > 0x%s)"), inv_addr, max_addr);
+ return NULL;
+ }
+
+ b = grub_efi_system_table->boot_services;
+ status = efi_call_4 (b->allocate_pages, alloctype, memtype, pages, &address);
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
+ return NULL;
+ }
+
+ if (address == 0)
+ {
+ /* Uggh, the address 0 was allocated... This is too annoying,
+ so reallocate another one. */
+ address = GRUB_EFI_MAX_USABLE_ADDRESS;
+ status = efi_call_4 (b->allocate_pages, alloctype, memtype, pages, &address);
+ grub_efi_free_pages (0, pages);
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
+ return NULL;
+ }
+ }
+
+ grub_efi_store_alloc (address, pages);
+
+ return (void *) ((grub_addr_t) address);
+}
+
+void *
+grub_efi_allocate_any_pages (grub_efi_uintn_t pages)
+{
+ return grub_efi_allocate_pages_real (GRUB_EFI_MAX_USABLE_ADDRESS,
+ pages, GRUB_EFI_ALLOCATE_MAX_ADDRESS,
+ GRUB_EFI_LOADER_DATA);
+}
+
+void *
+grub_efi_allocate_fixed (grub_efi_physical_address_t address,
+ grub_efi_uintn_t pages)
+{
+ return grub_efi_allocate_pages_real (address, pages,
+ GRUB_EFI_ALLOCATE_ADDRESS,
+ GRUB_EFI_LOADER_DATA);
+}
+
+/* Free pages starting from ADDRESS. */
+void
+grub_efi_free_pages (grub_efi_physical_address_t address,
+ grub_efi_uintn_t pages)
+{
+ grub_efi_boot_services_t *b;
+
+ b = grub_efi_system_table->boot_services;
+ efi_call_2 (b->free_pages, address, pages);
+
+ grub_efi_drop_alloc (address, pages);
+}
+
+#if defined (__i386__) || defined (__x86_64__)
+
+/* Helper for stop_broadcom. */
+static int
+find_card (grub_pci_device_t dev, grub_pci_id_t pciid,
+ void *data __attribute__ ((unused)))
+{
+ grub_pci_address_t addr;
+ grub_uint8_t cap;
+ grub_uint16_t pm_state;
+
+ if ((pciid & 0xffff) != GRUB_PCI_VENDOR_BROADCOM)
+ return 0;
+
+ addr = grub_pci_make_address (dev, GRUB_PCI_REG_CLASS);
+ if (grub_pci_read (addr) >> 24 != GRUB_PCI_CLASS_NETWORK)
+ return 0;
+ cap = grub_pci_find_capability (dev, GRUB_PCI_CAP_POWER_MANAGEMENT);
+ if (!cap)
+ return 0;
+ addr = grub_pci_make_address (dev, cap + 4);
+ pm_state = grub_pci_read_word (addr);
+ pm_state = pm_state | 0x03;
+ grub_pci_write_word (addr, pm_state);
+ grub_pci_read_word (addr);
+ return 0;
+}
+
+static void
+stop_broadcom (void)
+{
+ grub_pci_iterate (find_card, NULL);
+}
+
+#endif
+
+grub_err_t
+grub_efi_finish_boot_services (grub_efi_uintn_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 defined (__i386__) || defined (__x86_64__)
+ const grub_uint16_t apple[] = { 'A', 'p', 'p', 'l', 'e' };
+ int is_apple;
+
+ is_apple = (grub_memcmp (grub_efi_system_table->firmware_vendor,
+ apple, sizeof (apple)) == 0);
+#endif
+
+ while (1)
+ {
+ 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)
+ {
+ grub_free (finish_mmap_buf);
+ 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)
+ break;
+
+ if (status != GRUB_EFI_INVALID_PARAMETER)
+ {
+ grub_free (finish_mmap_buf);
+ return grub_error (GRUB_ERR_IO, "couldn't terminate EFI services");
+ }
+
+ grub_free (finish_mmap_buf);
+ grub_printf ("Trying to terminate EFI services again\n");
+ }
+ 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;
+
+#if defined (__i386__) || defined (__x86_64__)
+ if (is_apple)
+ stop_broadcom ();
+#endif
+
+ return GRUB_ERR_NONE;
+}
+
+/*
+ * To obtain the UEFI memory map, we must pass a buffer of sufficient size
+ * to hold the entire map. This function returns a sane start value for
+ * buffer size.
+ */
+grub_efi_uintn_t
+grub_efi_find_mmap_size (void)
+{
+ grub_efi_uintn_t mmap_size = 0;
+ grub_efi_uintn_t desc_size;
+
+ if (grub_efi_get_memory_map (&mmap_size, NULL, NULL, &desc_size, 0) < 0)
+ {
+ grub_error (GRUB_ERR_IO, "cannot get EFI memory map size");
+ return 0;
+ }
+
+ /*
+ * Add an extra page, since UEFI can alter the memory map itself on
+ * callbacks or explicit calls, including console output.
+ */
+ return ALIGN_UP (mmap_size + GRUB_EFI_PAGE_SIZE, GRUB_EFI_PAGE_SIZE);
+}
+
+/* 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
+grub_efi_get_memory_map (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_efi_status_t status;
+ grub_efi_boot_services_t *b;
+ grub_efi_uintn_t key;
+ grub_efi_uint32_t version;
+ grub_efi_uintn_t size;
+
+ if (grub_efi_is_finished)
+ {
+ int ret = 1;
+
+ if (memory_map != NULL)
+ {
+ 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);
+ }
+ else
+ {
+ /*
+ * Incomplete, no buffer to copy into, same as
+ * GRUB_EFI_BUFFER_TOO_SMALL below.
+ */
+ ret = 0;
+ }
+ *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;
+ if (! descriptor_version)
+ descriptor_version = &version;
+ if (! descriptor_size)
+ descriptor_size = &size;
+
+ b = grub_efi_system_table->boot_services;
+ status = efi_call_5 (b->get_memory_map, memory_map_size, memory_map, map_key,
+ descriptor_size, descriptor_version);
+ if (*descriptor_size == 0)
+ *descriptor_size = sizeof (grub_efi_memory_descriptor_t);
+ if (status == GRUB_EFI_SUCCESS)
+ return 1;
+ else if (status == GRUB_EFI_BUFFER_TOO_SMALL)
+ return 0;
+ else
+ return -1;
+}
+
+/* Sort the memory map in place. */
+static void
+sort_memory_map (grub_efi_memory_descriptor_t *memory_map,
+ grub_efi_uintn_t desc_size,
+ grub_efi_memory_descriptor_t *memory_map_end)
+{
+ grub_efi_memory_descriptor_t *d1;
+ grub_efi_memory_descriptor_t *d2;
+
+ for (d1 = memory_map;
+ d1 < memory_map_end;
+ d1 = NEXT_MEMORY_DESCRIPTOR (d1, desc_size))
+ {
+ grub_efi_memory_descriptor_t *max_desc = d1;
+
+ for (d2 = NEXT_MEMORY_DESCRIPTOR (d1, desc_size);
+ d2 < memory_map_end;
+ d2 = NEXT_MEMORY_DESCRIPTOR (d2, desc_size))
+ {
+ if (max_desc->num_pages < d2->num_pages)
+ max_desc = d2;
+ }
+
+ if (max_desc != d1)
+ {
+ grub_efi_memory_descriptor_t tmp;
+
+ tmp = *d1;
+ *d1 = *max_desc;
+ *max_desc = tmp;
+ }
+ }
+}
+
+/* Filter the descriptors. GRUB needs only available memory. */
+static grub_efi_memory_descriptor_t *
+filter_memory_map (grub_efi_memory_descriptor_t *memory_map,
+ grub_efi_memory_descriptor_t *filtered_memory_map,
+ grub_efi_uintn_t desc_size,
+ grub_efi_memory_descriptor_t *memory_map_end)
+{
+ grub_efi_memory_descriptor_t *desc;
+ grub_efi_memory_descriptor_t *filtered_desc;
+
+ for (desc = memory_map, filtered_desc = filtered_memory_map;
+ desc < memory_map_end;
+ desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))
+ {
+ if (desc->type == GRUB_EFI_CONVENTIONAL_MEMORY
+#if 1
+ && desc->physical_start <= GRUB_EFI_MAX_USABLE_ADDRESS
+#endif
+ && desc->physical_start + PAGES_TO_BYTES (desc->num_pages) > 0x100000
+ && desc->num_pages != 0)
+ {
+ grub_memcpy (filtered_desc, desc, desc_size);
+
+ /* Avoid less than 1MB, because some loaders seem to be confused. */
+ if (desc->physical_start < 0x100000)
+ {
+ desc->num_pages -= BYTES_TO_PAGES (0x100000
+ - desc->physical_start);
+ desc->physical_start = 0x100000;
+ }
+
+#if 1
+ if (BYTES_TO_PAGES (filtered_desc->physical_start)
+ + filtered_desc->num_pages
+ > BYTES_TO_PAGES_DOWN (GRUB_EFI_MAX_USABLE_ADDRESS))
+ filtered_desc->num_pages
+ = (BYTES_TO_PAGES_DOWN (GRUB_EFI_MAX_USABLE_ADDRESS)
+ - BYTES_TO_PAGES (filtered_desc->physical_start));
+#endif
+
+ if (filtered_desc->num_pages == 0)
+ continue;
+
+ filtered_desc = NEXT_MEMORY_DESCRIPTOR (filtered_desc, desc_size);
+ }
+ }
+
+ return filtered_desc;
+}
+
+/* Return the total number of pages. */
+static grub_efi_uint64_t
+get_total_pages (grub_efi_memory_descriptor_t *memory_map,
+ grub_efi_uintn_t desc_size,
+ grub_efi_memory_descriptor_t *memory_map_end)
+{
+ grub_efi_memory_descriptor_t *desc;
+ grub_efi_uint64_t total = 0;
+
+ for (desc = memory_map;
+ desc < memory_map_end;
+ desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))
+ total += desc->num_pages;
+
+ return total;
+}
+
+/* Add memory regions. */
+static void
+add_memory_regions (grub_efi_memory_descriptor_t *memory_map,
+ grub_efi_uintn_t desc_size,
+ grub_efi_memory_descriptor_t *memory_map_end,
+ grub_efi_uint64_t required_pages)
+{
+ grub_efi_memory_descriptor_t *desc;
+
+ for (desc = memory_map;
+ desc < memory_map_end;
+ desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))
+ {
+ grub_efi_uint64_t pages;
+ grub_efi_physical_address_t start;
+ void *addr;
+
+ start = desc->physical_start;
+ pages = desc->num_pages;
+ if (pages > required_pages)
+ {
+ start += PAGES_TO_BYTES (pages - required_pages);
+ pages = required_pages;
+ }
+
+ addr = grub_efi_allocate_pages_real (start, pages,
+ GRUB_EFI_ALLOCATE_ADDRESS,
+ GRUB_EFI_LOADER_CODE);
+ if (! addr)
+ grub_fatal ("cannot allocate conventional memory %p with %u pages",
+ (void *) ((grub_addr_t) start),
+ (unsigned) pages);
+
+ grub_mm_init_region (addr, PAGES_TO_BYTES (pages));
+
+ required_pages -= pages;
+ if (required_pages == 0)
+ break;
+ }
+
+ if (required_pages > 0)
+ grub_fatal ("too little memory");
+}
+
+void
+grub_efi_memory_fini (void)
+{
+ /*
+ * Free all stale allocations. grub_efi_free_pages() will remove
+ * the found entry from the list and it will always find the first
+ * list entry (efi_allocated_memory is the list start). Hence we
+ * remove all entries from the list until none is left altogether.
+ */
+ while (efi_allocated_memory)
+ grub_efi_free_pages (efi_allocated_memory->address,
+ efi_allocated_memory->pages);
+}
+
+#if 0
+/* Print the memory map. */
+static void
+print_memory_map (grub_efi_memory_descriptor_t *memory_map,
+ grub_efi_uintn_t desc_size,
+ grub_efi_memory_descriptor_t *memory_map_end)
+{
+ grub_efi_memory_descriptor_t *desc;
+ int i;
+
+ for (desc = memory_map, i = 0;
+ desc < memory_map_end;
+ desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size), i++)
+ {
+ grub_printf ("MD: t=%x, p=%llx, v=%llx, n=%llx, a=%llx\n",
+ desc->type, desc->physical_start, desc->virtual_start,
+ desc->num_pages, desc->attribute);
+ }
+}
+#endif
+
+void
+grub_efi_mm_init (void)
+{
+ grub_efi_memory_descriptor_t *memory_map;
+ grub_efi_memory_descriptor_t *memory_map_end;
+ grub_efi_memory_descriptor_t *filtered_memory_map;
+ grub_efi_memory_descriptor_t *filtered_memory_map_end;
+ grub_efi_uintn_t map_size;
+ grub_efi_uintn_t desc_size;
+ grub_efi_uint64_t total_pages;
+ grub_efi_uint64_t required_pages;
+ int mm_status;
+
+ /* Prepare a memory region to store two memory maps. */
+ memory_map = grub_efi_allocate_any_pages (2 * BYTES_TO_PAGES (MEMORY_MAP_SIZE));
+ if (! memory_map)
+ grub_fatal ("cannot allocate memory");
+
+ /* Obtain descriptors for available memory. */
+ map_size = MEMORY_MAP_SIZE;
+
+ mm_status = grub_efi_get_memory_map (&map_size, memory_map, 0, &desc_size, 0);
+
+ if (mm_status == 0)
+ {
+ grub_efi_free_pages
+ ((grub_efi_physical_address_t) ((grub_addr_t) memory_map),
+ 2 * BYTES_TO_PAGES (MEMORY_MAP_SIZE));
+
+ /* Freeing/allocating operations may increase memory map size. */
+ map_size += desc_size * 32;
+
+ memory_map = grub_efi_allocate_any_pages (2 * BYTES_TO_PAGES (map_size));
+ if (! memory_map)
+ grub_fatal ("cannot allocate memory");
+
+ mm_status = grub_efi_get_memory_map (&map_size, memory_map, 0,
+ &desc_size, 0);
+ }
+
+ if (mm_status < 0)
+ grub_fatal ("cannot get memory map");
+
+ memory_map_end = NEXT_MEMORY_DESCRIPTOR (memory_map, map_size);
+
+ filtered_memory_map = memory_map_end;
+
+ filtered_memory_map_end = filter_memory_map (memory_map, filtered_memory_map,
+ desc_size, memory_map_end);
+
+ /* By default, request a quarter of the available memory. */
+ total_pages = get_total_pages (filtered_memory_map, desc_size,
+ filtered_memory_map_end);
+ required_pages = (total_pages >> 2);
+ if (required_pages < BYTES_TO_PAGES (MIN_HEAP_SIZE))
+ required_pages = BYTES_TO_PAGES (MIN_HEAP_SIZE);
+ else if (required_pages > BYTES_TO_PAGES (MAX_HEAP_SIZE))
+ required_pages = BYTES_TO_PAGES (MAX_HEAP_SIZE);
+
+ /* Sort the filtered descriptors, so that GRUB can allocate pages
+ from smaller regions. */
+ sort_memory_map (filtered_memory_map, desc_size, filtered_memory_map_end);
+
+ /* Allocate memory regions for GRUB's memory management. */
+ add_memory_regions (filtered_memory_map, desc_size,
+ filtered_memory_map_end, required_pages);
+
+#if 0
+ /* For debug. */
+ map_size = MEMORY_MAP_SIZE;
+
+ if (grub_efi_get_memory_map (&map_size, memory_map, 0, &desc_size, 0) < 0)
+ grub_fatal ("cannot get memory map");
+
+ grub_printf ("printing memory map\n");
+ print_memory_map (memory_map, desc_size,
+ NEXT_MEMORY_DESCRIPTOR (memory_map, map_size));
+ grub_fatal ("Debug. ");
+#endif
+
+ /* Release the memory maps. */
+ grub_efi_free_pages ((grub_addr_t) memory_map,
+ 2 * BYTES_TO_PAGES (MEMORY_MAP_SIZE));
+}
+
+#if defined (__aarch64__) || defined (__arm__) || defined (__riscv)
+grub_err_t
+grub_efi_get_ram_base(grub_addr_t *base_addr)
+{
+ grub_efi_memory_descriptor_t *memory_map, *desc;
+ grub_efi_uintn_t memory_map_size, desc_size;
+ int ret;
+
+ memory_map_size = grub_efi_find_mmap_size();
+
+ memory_map = grub_malloc (memory_map_size);
+ if (! memory_map)
+ return GRUB_ERR_OUT_OF_MEMORY;
+ ret = grub_efi_get_memory_map (&memory_map_size, memory_map, NULL,
+ &desc_size, NULL);
+
+ if (ret < 1)
+ return GRUB_ERR_BUG;
+
+ for (desc = memory_map, *base_addr = GRUB_EFI_MAX_USABLE_ADDRESS;
+ (grub_addr_t) desc < ((grub_addr_t) memory_map + memory_map_size);
+ desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))
+ if (desc->attribute & GRUB_EFI_MEMORY_WB)
+ *base_addr = grub_min (*base_addr, desc->physical_start);
+
+ grub_free(memory_map);
+
+ return GRUB_ERR_NONE;
+}
+#endif
diff --git a/grub-core/kern/efi/sb.c b/grub-core/kern/efi/sb.c
new file mode 100644
index 0000000..c52ec62
--- /dev/null
+++ b/grub-core/kern/efi/sb.c
@@ -0,0 +1,188 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2020 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 <http://www.gnu.org/licenses/>.
+ *
+ * UEFI Secure Boot related checkings.
+ */
+
+#include <grub/efi/efi.h>
+#include <grub/efi/pe32.h>
+#include <grub/efi/sb.h>
+#include <grub/env.h>
+#include <grub/err.h>
+#include <grub/file.h>
+#include <grub/i386/linux.h>
+#include <grub/kernel.h>
+#include <grub/mm.h>
+#include <grub/types.h>
+#include <grub/verify.h>
+
+static grub_efi_guid_t shim_lock_guid = GRUB_EFI_SHIM_LOCK_GUID;
+
+/*
+ * Determine whether we're in secure boot mode.
+ *
+ * Please keep the logic in sync with the Linux kernel,
+ * drivers/firmware/efi/libstub/secureboot.c:efi_get_secureboot().
+ */
+grub_uint8_t
+grub_efi_get_secureboot (void)
+{
+ static grub_efi_guid_t efi_variable_guid = GRUB_EFI_GLOBAL_VARIABLE_GUID;
+ grub_efi_status_t status;
+ grub_efi_uint32_t attr = 0;
+ grub_size_t size = 0;
+ grub_uint8_t *secboot = NULL;
+ grub_uint8_t *setupmode = NULL;
+ grub_uint8_t *moksbstate = NULL;
+ grub_uint8_t secureboot = GRUB_EFI_SECUREBOOT_MODE_UNKNOWN;
+ const char *secureboot_str = "UNKNOWN";
+
+ status = grub_efi_get_variable ("SecureBoot", &efi_variable_guid,
+ &size, (void **) &secboot);
+
+ if (status == GRUB_EFI_NOT_FOUND)
+ {
+ secureboot = GRUB_EFI_SECUREBOOT_MODE_DISABLED;
+ goto out;
+ }
+
+ if (status != GRUB_EFI_SUCCESS)
+ goto out;
+
+ status = grub_efi_get_variable ("SetupMode", &efi_variable_guid,
+ &size, (void **) &setupmode);
+
+ if (status != GRUB_EFI_SUCCESS)
+ goto out;
+
+ if ((*secboot == 0) || (*setupmode == 1))
+ {
+ secureboot = GRUB_EFI_SECUREBOOT_MODE_DISABLED;
+ goto out;
+ }
+
+ /*
+ * See if a user has put the shim into insecure mode. If so, and if the
+ * variable doesn't have the runtime attribute set, we might as well
+ * honor that.
+ */
+ status = grub_efi_get_variable_with_attributes ("MokSBState", &shim_lock_guid,
+ &size, (void **) &moksbstate, &attr);
+
+ /* If it fails, we don't care why. Default to secure. */
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ secureboot = GRUB_EFI_SECUREBOOT_MODE_ENABLED;
+ goto out;
+ }
+
+ if (!(attr & GRUB_EFI_VARIABLE_RUNTIME_ACCESS) && *moksbstate == 1)
+ {
+ secureboot = GRUB_EFI_SECUREBOOT_MODE_DISABLED;
+ goto out;
+ }
+
+ secureboot = GRUB_EFI_SECUREBOOT_MODE_ENABLED;
+
+ out:
+ grub_free (moksbstate);
+ grub_free (setupmode);
+ grub_free (secboot);
+
+ if (secureboot == GRUB_EFI_SECUREBOOT_MODE_DISABLED)
+ secureboot_str = "Disabled";
+ else if (secureboot == GRUB_EFI_SECUREBOOT_MODE_ENABLED)
+ secureboot_str = "Enabled";
+
+ grub_dprintf ("efi", "UEFI Secure Boot state: %s\n", secureboot_str);
+
+ return secureboot;
+}
+
+static grub_err_t
+shim_lock_verifier_init (grub_file_t io __attribute__ ((unused)),
+ enum grub_file_type type,
+ void **context __attribute__ ((unused)),
+ enum grub_verify_flags *flags)
+{
+ *flags = GRUB_VERIFY_FLAGS_SKIP_VERIFICATION;
+
+ switch (type & GRUB_FILE_TYPE_MASK)
+ {
+ case GRUB_FILE_TYPE_LINUX_KERNEL:
+ case GRUB_FILE_TYPE_MULTIBOOT_KERNEL:
+ case GRUB_FILE_TYPE_BSD_KERNEL:
+ case GRUB_FILE_TYPE_XNU_KERNEL:
+ case GRUB_FILE_TYPE_PLAN9_KERNEL:
+ case GRUB_FILE_TYPE_EFI_CHAINLOADED_IMAGE:
+ *flags = GRUB_VERIFY_FLAGS_SINGLE_CHUNK;
+
+ /* Fall through. */
+
+ default:
+ return GRUB_ERR_NONE;
+ }
+}
+
+static grub_err_t
+shim_lock_verifier_write (void *context __attribute__ ((unused)), void *buf, grub_size_t size)
+{
+ grub_efi_shim_lock_protocol_t *sl = grub_efi_locate_protocol (&shim_lock_guid, 0);
+
+ if (!sl)
+ return grub_error (GRUB_ERR_ACCESS_DENIED, N_("shim_lock protocol not found"));
+
+ if (sl->verify (buf, size) != GRUB_EFI_SUCCESS)
+ return grub_error (GRUB_ERR_BAD_SIGNATURE, N_("bad shim signature"));
+
+ return GRUB_ERR_NONE;
+}
+
+struct grub_file_verifier shim_lock_verifier =
+ {
+ .name = "shim_lock_verifier",
+ .init = shim_lock_verifier_init,
+ .write = shim_lock_verifier_write
+ };
+
+void
+grub_shim_lock_verifier_setup (void)
+{
+ struct grub_module_header *header;
+ grub_efi_shim_lock_protocol_t *sl =
+ grub_efi_locate_protocol (&shim_lock_guid, 0);
+
+ /* shim_lock is missing, check if GRUB image is built with --disable-shim-lock. */
+ if (!sl)
+ {
+ FOR_MODULES (header)
+ {
+ if (header->type == OBJ_TYPE_DISABLE_SHIM_LOCK)
+ return;
+ }
+ }
+
+ /* Secure Boot is off. Do not load shim_lock. */
+ if (grub_efi_get_secureboot () != GRUB_EFI_SECUREBOOT_MODE_ENABLED)
+ return;
+
+ /* Enforce shim_lock_verifier. */
+ grub_verifier_register (&shim_lock_verifier);
+
+ grub_env_set ("shim_lock", "y");
+ grub_env_export ("shim_lock");
+}
diff --git a/grub-core/kern/elf.c b/grub-core/kern/elf.c
new file mode 100644
index 0000000..9d7149b
--- /dev/null
+++ b/grub-core/kern/elf.c
@@ -0,0 +1,212 @@
+/* elf.c - load ELF files */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2003,2004,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/err.h>
+#include <grub/elf.h>
+#include <grub/elfload.h>
+#include <grub/file.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/dl.h>
+#include <grub/i18n.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+#pragma GCC diagnostic ignored "-Wcast-align"
+
+#if defined(__powerpc__) && defined(GRUB_MACHINE_IEEE1275)
+#define GRUB_ELF_ENABLE_BI_ENDIAN 1
+#else
+#define GRUB_ELF_ENABLE_BI_ENDIAN 0
+#endif
+
+#if defined(GRUB_CPU_WORDS_BIGENDIAN)
+#define GRUB_ELF_NATIVE_ENDIANNESS ELFDATA2MSB
+#define GRUB_ELF_OPPOSITE_ENDIANNESS ELFDATA2LSB
+#else
+#define GRUB_ELF_NATIVE_ENDIANNESS ELFDATA2LSB
+#define GRUB_ELF_OPPOSITE_ENDIANNESS ELFDATA2MSB
+#endif
+
+static int grub_elf32_check_endianess_and_bswap_ehdr (grub_elf_t elf);
+static int grub_elf64_check_endianess_and_bswap_ehdr (grub_elf_t elf);
+
+/* Check if EHDR is a valid ELF header. */
+static grub_err_t
+grub_elf_check_header (grub_elf_t elf)
+{
+ Elf32_Ehdr *e = &elf->ehdr.ehdr32;
+
+ if (e->e_ident[EI_MAG0] != ELFMAG0
+ || e->e_ident[EI_MAG1] != ELFMAG1
+ || e->e_ident[EI_MAG2] != ELFMAG2
+ || e->e_ident[EI_MAG3] != ELFMAG3
+ || e->e_ident[EI_VERSION] != EV_CURRENT)
+ return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-independent ELF magic"));
+
+ if (grub_elf_is_elf32 (elf))
+ {
+ if (!grub_elf32_check_endianess_and_bswap_ehdr (elf)) {
+ return grub_error (GRUB_ERR_BAD_OS, "invalid ELF endianness magic");
+ }
+ }
+ else if (grub_elf_is_elf64 (elf))
+ {
+ if (!grub_elf64_check_endianess_and_bswap_ehdr (elf)) {
+ return grub_error (GRUB_ERR_BAD_OS, "invalid ELF endianness magic");
+ }
+ }
+ else
+ return grub_error (GRUB_ERR_BAD_OS, "unknown ELF class");
+
+ if (e->e_version != EV_CURRENT)
+ return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-independent ELF magic"));
+
+ return GRUB_ERR_NONE;
+}
+
+grub_err_t
+grub_elf_close (grub_elf_t elf)
+{
+ grub_file_t file = elf->file;
+
+ grub_free (elf->phdrs);
+ grub_free (elf->filename);
+ grub_free (elf);
+
+ if (file)
+ grub_file_close (file);
+
+ return grub_errno;
+}
+
+grub_elf_t
+grub_elf_file (grub_file_t file, const char *filename)
+{
+ grub_elf_t elf;
+
+ elf = grub_zalloc (sizeof (*elf));
+ if (! elf)
+ return 0;
+
+ elf->file = file;
+
+ if (grub_file_seek (elf->file, 0) == (grub_off_t) -1)
+ goto fail;
+
+ if (grub_file_read (elf->file, &elf->ehdr, sizeof (elf->ehdr))
+ != sizeof (elf->ehdr))
+ {
+ if (!grub_errno)
+ grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
+ filename);
+ goto fail;
+ }
+
+ if (grub_elf_check_header (elf))
+ goto fail;
+
+ elf->filename = grub_strdup (filename);
+ if (!elf->filename)
+ goto fail;
+
+ return elf;
+
+fail:
+ grub_free (elf->filename);
+ grub_free (elf->phdrs);
+ grub_free (elf);
+ return 0;
+}
+
+grub_elf_t
+grub_elf_open (const char *name, enum grub_file_type type)
+{
+ grub_file_t file;
+ grub_elf_t elf;
+
+ file = grub_file_open (name, type);
+ if (! file)
+ return 0;
+
+ elf = grub_elf_file (file, name);
+ if (! elf)
+ grub_file_close (file);
+
+ return elf;
+}
+
+
+#define grub_swap_bytes_halfXX grub_swap_bytes16
+#define grub_swap_bytes_wordXX grub_swap_bytes32
+
+/* 32-bit */
+#define ehdrXX ehdr32
+#define ELFCLASSXX ELFCLASS32
+#define ElfXX_Addr Elf32_Addr
+#define grub_elfXX_size grub_elf32_size
+#define grub_elfXX_load grub_elf32_load
+#define FOR_ELFXX_PHDRS FOR_ELF32_PHDRS
+#define grub_elf_is_elfXX grub_elf_is_elf32
+#define grub_elfXX_load_phdrs grub_elf32_load_phdrs
+#define ElfXX_Phdr Elf32_Phdr
+#define ElfXX_Ehdr Elf32_Ehdr
+#define grub_uintXX_t grub_uint32_t
+#define grub_swap_bytes_addrXX grub_swap_bytes32
+#define grub_swap_bytes_offXX grub_swap_bytes32
+#define grub_swap_bytes_XwordXX grub_swap_bytes32
+#define grub_elfXX_check_endianess_and_bswap_ehdr grub_elf32_check_endianess_and_bswap_ehdr
+
+#include "elfXX.c"
+
+#undef ehdrXX
+#undef ELFCLASSXX
+#undef ElfXX_Addr
+#undef grub_elfXX_size
+#undef grub_elfXX_load
+#undef FOR_ELFXX_PHDRS
+#undef grub_elf_is_elfXX
+#undef grub_elfXX_load_phdrs
+#undef ElfXX_Phdr
+#undef ElfXX_Ehdr
+#undef grub_uintXX_t
+#undef grub_swap_bytes_addrXX
+#undef grub_swap_bytes_offXX
+#undef grub_swap_bytes_XwordXX
+#undef grub_elfXX_check_endianess_and_bswap_ehdr
+
+
+/* 64-bit */
+#define ehdrXX ehdr64
+#define ELFCLASSXX ELFCLASS64
+#define ElfXX_Addr Elf64_Addr
+#define grub_elfXX_size grub_elf64_size
+#define grub_elfXX_load grub_elf64_load
+#define FOR_ELFXX_PHDRS FOR_ELF64_PHDRS
+#define grub_elf_is_elfXX grub_elf_is_elf64
+#define grub_elfXX_load_phdrs grub_elf64_load_phdrs
+#define ElfXX_Phdr Elf64_Phdr
+#define ElfXX_Ehdr Elf64_Ehdr
+#define grub_uintXX_t grub_uint64_t
+#define grub_swap_bytes_addrXX grub_swap_bytes64
+#define grub_swap_bytes_offXX grub_swap_bytes64
+#define grub_swap_bytes_XwordXX grub_swap_bytes64
+#define grub_elfXX_check_endianess_and_bswap_ehdr grub_elf64_check_endianess_and_bswap_ehdr
+
+#include "elfXX.c"
diff --git a/grub-core/kern/elfXX.c b/grub-core/kern/elfXX.c
new file mode 100644
index 0000000..1859d18
--- /dev/null
+++ b/grub-core/kern/elfXX.c
@@ -0,0 +1,207 @@
+int
+grub_elf_is_elfXX (grub_elf_t elf)
+{
+ return elf->ehdr.ehdrXX.e_ident[EI_CLASS] == ELFCLASSXX;
+}
+
+grub_err_t
+grub_elfXX_load_phdrs (grub_elf_t elf)
+{
+ grub_ssize_t phdrs_size;
+
+ if (elf->phdrs)
+ return GRUB_ERR_NONE;
+
+ phdrs_size = (grub_uint32_t) elf->ehdr.ehdrXX.e_phnum * elf->ehdr.ehdrXX.e_phentsize;
+
+ grub_dprintf ("elf", "Loading program headers at 0x%llx, size 0x%lx.\n",
+ (unsigned long long) elf->ehdr.ehdrXX.e_phoff,
+ (unsigned long) phdrs_size);
+
+ elf->phdrs = grub_malloc (phdrs_size);
+ if (! elf->phdrs)
+ return grub_errno;
+
+ if ((grub_file_seek (elf->file, elf->ehdr.ehdrXX.e_phoff) == (grub_off_t) -1)
+ || (grub_file_read (elf->file, elf->phdrs, phdrs_size) != phdrs_size))
+ {
+ if (!grub_errno)
+ grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
+ elf->filename);
+ return grub_errno;
+ }
+
+#if GRUB_ELF_ENABLE_BI_ENDIAN
+ if (elf->ehdr.ehdrXX.e_ident[EI_DATA] == GRUB_ELF_OPPOSITE_ENDIANNESS)
+ {
+ ElfXX_Phdr *phdr;
+ for (phdr = elf->phdrs; (char *) phdr < (char *) elf->phdrs + phdrs_size;
+ phdr = (ElfXX_Phdr *) ((char *) phdr + elf->ehdr.ehdrXX.e_phentsize))
+ {
+ phdr->p_type = grub_swap_bytes_wordXX (phdr->p_type);
+ phdr->p_flags = grub_swap_bytes_wordXX (phdr->p_flags);
+ phdr->p_offset = grub_swap_bytes_offXX (phdr->p_offset);
+ phdr->p_vaddr = grub_swap_bytes_addrXX (phdr->p_vaddr);
+ phdr->p_paddr = grub_swap_bytes_addrXX (phdr->p_paddr);
+ phdr->p_filesz = grub_swap_bytes_XwordXX (phdr->p_filesz);
+ phdr->p_memsz = grub_swap_bytes_XwordXX (phdr->p_memsz);
+ phdr->p_align = grub_swap_bytes_XwordXX (phdr->p_align);
+ }
+ }
+#endif /* GRUB_ELF_ENABLE_BI_ENDIAN */
+
+ return GRUB_ERR_NONE;
+}
+
+/* Calculate the amount of memory spanned by the segments. */
+grub_size_t
+grub_elfXX_size (grub_elf_t elf,
+ ElfXX_Addr *base, grub_uintXX_t *max_align)
+{
+ ElfXX_Addr segments_start = (ElfXX_Addr) -1;
+ ElfXX_Addr segments_end = 0;
+ int nr_phdrs = 0;
+ grub_uint32_t curr_align = 1;
+ ElfXX_Phdr *phdr;
+
+ /* Run through the program headers to calculate the total memory size we
+ * should claim. */
+ FOR_ELFXX_PHDRS (elf, phdr)
+ {
+ /* Only consider loadable segments. */
+ if (phdr->p_type != PT_LOAD)
+ continue;
+ nr_phdrs++;
+ if (phdr->p_paddr < segments_start)
+ segments_start = phdr->p_paddr;
+ if (phdr->p_paddr + phdr->p_memsz > segments_end)
+ segments_end = phdr->p_paddr + phdr->p_memsz;
+ if (curr_align < phdr->p_align)
+ curr_align = phdr->p_align;
+ }
+
+ if (base)
+ *base = 0;
+
+ if (nr_phdrs == 0)
+ {
+ grub_error (GRUB_ERR_BAD_OS, "no program headers present");
+ return 0;
+ }
+
+ if (segments_end < segments_start)
+ {
+ /* Very bad addresses. */
+ grub_error (GRUB_ERR_BAD_OS, "bad program header load addresses");
+ return 0;
+ }
+
+ if (base)
+ *base = segments_start;
+ if (max_align)
+ *max_align = curr_align;
+ return segments_end - segments_start;
+}
+
+grub_err_t
+grub_elfXX_load (grub_elf_t elf, const char *filename,
+ void *load_offset, enum grub_elf_load_flags load_flags,
+ grub_addr_t *base, grub_size_t *size)
+{
+ grub_addr_t load_base = (grub_addr_t) -1ULL;
+ grub_size_t load_size = 0;
+ ElfXX_Phdr *phdr;
+
+ FOR_ELFXX_PHDRS(elf, phdr)
+ {
+ grub_addr_t load_addr;
+
+ if (phdr->p_type != PT_LOAD && !((load_flags & GRUB_ELF_LOAD_FLAGS_LOAD_PT_DYNAMIC) && phdr->p_type == PT_DYNAMIC))
+ continue;
+
+ load_addr = (grub_addr_t) phdr->p_paddr;
+ switch (load_flags & GRUB_ELF_LOAD_FLAGS_BITS)
+ {
+ case GRUB_ELF_LOAD_FLAGS_ALL_BITS:
+ break;
+ case GRUB_ELF_LOAD_FLAGS_28BITS:
+ load_addr &= 0xFFFFFFF;
+ break;
+ case GRUB_ELF_LOAD_FLAGS_30BITS:
+ load_addr &= 0x3FFFFFFF;
+ break;
+ case GRUB_ELF_LOAD_FLAGS_62BITS:
+ load_addr &= 0x3FFFFFFFFFFFFFFFULL;
+ break;
+ }
+ load_addr += (grub_addr_t) load_offset;
+
+ if (load_addr < load_base)
+ load_base = load_addr;
+
+ grub_dprintf ("elf", "Loading segment at 0x%llx, size 0x%llx\n",
+ (unsigned long long) load_addr,
+ (unsigned long long) phdr->p_memsz);
+
+ if (grub_file_seek (elf->file, phdr->p_offset) == (grub_off_t) -1)
+ return grub_errno;
+
+ if (phdr->p_filesz)
+ {
+ grub_ssize_t read;
+ read = grub_file_read (elf->file, (void *) load_addr, phdr->p_filesz);
+ if (read != (grub_ssize_t) phdr->p_filesz)
+ {
+ if (!grub_errno)
+ grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
+ filename);
+ return grub_errno;
+ }
+ }
+
+ if (phdr->p_filesz < phdr->p_memsz)
+ grub_memset ((void *) (grub_addr_t) (load_addr + phdr->p_filesz),
+ 0, phdr->p_memsz - phdr->p_filesz);
+
+ load_size += phdr->p_memsz;
+ }
+
+ if (base)
+ *base = load_base;
+ if (size)
+ *size = load_size;
+
+ return grub_errno;
+}
+
+static int
+grub_elfXX_check_endianess_and_bswap_ehdr (grub_elf_t elf)
+{
+ ElfXX_Ehdr *e = &(elf->ehdr.ehdrXX);
+ if (e->e_ident[EI_DATA] == GRUB_ELF_NATIVE_ENDIANNESS)
+ {
+ return 1;
+ }
+
+#if GRUB_ELF_ENABLE_BI_ENDIAN
+ if (e->e_ident[EI_DATA] == GRUB_ELF_OPPOSITE_ENDIANNESS)
+ {
+ e->e_type = grub_swap_bytes_halfXX (e->e_type);
+ e->e_machine = grub_swap_bytes_halfXX (e->e_machine);
+ e->e_version = grub_swap_bytes_wordXX (e->e_version);
+ e->e_entry = grub_swap_bytes_addrXX (e->e_entry);
+ e->e_phoff = grub_swap_bytes_offXX (e->e_phoff);
+ e->e_shoff = grub_swap_bytes_offXX (e->e_shoff);
+ e->e_flags = grub_swap_bytes_wordXX (e->e_flags);
+ e->e_ehsize = grub_swap_bytes_halfXX (e->e_ehsize);
+ e->e_phentsize = grub_swap_bytes_halfXX (e->e_phentsize);
+ e->e_phnum = grub_swap_bytes_halfXX (e->e_phnum);
+ e->e_shentsize = grub_swap_bytes_halfXX (e->e_shentsize);
+ e->e_shnum = grub_swap_bytes_halfXX (e->e_shnum);
+ e->e_shstrndx = grub_swap_bytes_halfXX (e->e_shstrndx);
+ return 1;
+ }
+#endif /* GRUB_ELF_ENABLE_BI_ENDIAN */
+
+ return 0;
+}
diff --git a/grub-core/kern/emu/argp_common.c b/grub-core/kern/emu/argp_common.c
new file mode 100644
index 0000000..1668858
--- /dev/null
+++ b/grub-core/kern/emu/argp_common.c
@@ -0,0 +1,41 @@
+/* grub-setup.c - make GRUB usable */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <config-util.h>
+
+#pragma GCC diagnostic ignored "-Wmissing-prototypes"
+#pragma GCC diagnostic ignored "-Wmissing-declarations"
+
+#define _GNU_SOURCE 1
+#include <stdlib.h>
+#include <unistd.h>
+#include <progname.h>
+#include <argp.h>
+
+/* Print the version information. */
+static void
+print_version (FILE *stream, struct argp_state *state)
+{
+ fprintf (stream, "%s (%s) %s\n", program_name, PACKAGE_NAME, PACKAGE_VERSION);
+}
+void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
+
+/* Set the bug report address */
+const char *argp_program_bug_address = "<"PACKAGE_BUGREPORT">";
diff --git a/grub-core/kern/emu/cache.c b/grub-core/kern/emu/cache.c
new file mode 100644
index 0000000..113682c
--- /dev/null
+++ b/grub-core/kern/emu/cache.c
@@ -0,0 +1,35 @@
+#ifndef GRUB_MACHINE_EMU
+#error "This source is only meant for grub-emu platform"
+#endif
+
+#include <grub/cache.h>
+
+#if defined(__ia64__)
+#include "../ia64/cache.c"
+#elif defined (__arm__) || defined (__aarch64__)
+
+void __clear_cache (void *beg, void *end);
+
+void
+grub_arch_sync_caches (void *address, grub_size_t len)
+{
+ __clear_cache (address, (char *) address + len);
+}
+
+#elif defined (__mips__)
+void _flush_cache (void *address, grub_size_t len, int type);
+
+void
+grub_arch_sync_caches (void *address, grub_size_t len)
+{
+ return _flush_cache (address, len, 0);
+}
+
+#elif defined(__riscv)
+void
+grub_arch_sync_caches (void *address, grub_size_t len)
+{
+}
+
+#endif
+
diff --git a/grub-core/kern/emu/cache_s.S b/grub-core/kern/emu/cache_s.S
new file mode 100644
index 0000000..2245cdd
--- /dev/null
+++ b/grub-core/kern/emu/cache_s.S
@@ -0,0 +1,15 @@
+#ifndef GRUB_MACHINE_EMU
+#error "This source is only meant for grub-emu platform"
+#endif
+
+#if defined(__i386__) || defined(__x86_64__)
+/* Nothing is necessary. */
+#elif defined(__sparc__)
+#include "../sparc64/cache.S"
+#elif defined(__powerpc__)
+#include "../powerpc/cache.S"
+#elif defined(__ia64__) || defined(__arm__) || defined(__aarch64__) || \
+ defined(__mips__) || defined(__riscv)
+#else
+#error "No target cpu type is defined"
+#endif
diff --git a/grub-core/kern/emu/full.c b/grub-core/kern/emu/full.c
new file mode 100644
index 0000000..e8d63b1
--- /dev/null
+++ b/grub-core/kern/emu/full.c
@@ -0,0 +1,69 @@
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <grub/dl.h>
+#include <grub/env.h>
+#include <grub/kernel.h>
+#include <grub/misc.h>
+#include <grub/emu/misc.h>
+#include <grub/disk.h>
+
+const int grub_no_modules = 1;
+
+void
+grub_register_exported_symbols (void)
+{
+}
+
+grub_err_t
+grub_arch_dl_check_header (void *ehdr)
+{
+ (void) ehdr;
+ return GRUB_ERR_BAD_MODULE;
+}
+
+grub_err_t
+grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
+ Elf_Shdr *s, grub_dl_segment_t seg)
+{
+ (void) mod;
+ (void) ehdr;
+ (void) s;
+ (void) seg;
+ return GRUB_ERR_BAD_MODULE;
+}
+
+#if !defined (__i386__) && !defined (__x86_64__)
+grub_err_t
+grub_arch_dl_get_tramp_got_size (const void *ehdr __attribute__ ((unused)),
+ grub_size_t *tramp, grub_size_t *got)
+{
+ *tramp = 0;
+ *got = 0;
+ return GRUB_ERR_BAD_MODULE;
+}
+#endif
+
+#ifdef GRUB_LINKER_HAVE_INIT
+void
+grub_arch_dl_init_linker (void)
+{
+}
+#endif
+
diff --git a/grub-core/kern/emu/hostdisk.c b/grub-core/kern/emu/hostdisk.c
new file mode 100644
index 0000000..d975265
--- /dev/null
+++ b/grub-core/kern/emu/hostdisk.c
@@ -0,0 +1,686 @@
+/* hostdisk.c - emulate biosdisk */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 1999,2000,2001,2002,2003,2004,2006,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <config-util.h>
+
+#include <grub/disk.h>
+#include <grub/partition.h>
+#include <grub/msdos_partition.h>
+#include <grub/types.h>
+#include <grub/err.h>
+#include <grub/emu/misc.h>
+#include <grub/emu/hostdisk.h>
+#include <grub/emu/getroot.h>
+#include <grub/misc.h>
+#include <grub/i18n.h>
+#include <grub/list.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <assert.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <limits.h>
+
+#ifdef __linux__
+# include <sys/ioctl.h> /* ioctl */
+# include <sys/mount.h>
+# ifndef BLKFLSBUF
+# define BLKFLSBUF _IO (0x12,97) /* flush buffer cache */
+# endif /* ! BLKFLSBUF */
+#endif /* __linux__ */
+
+static struct
+{
+ char *drive;
+ char *device;
+ int device_map;
+} map[256];
+
+static int
+unescape_cmp (const char *a, const char *b_escaped)
+{
+ while (*a || *b_escaped)
+ {
+ if (*b_escaped == '\\' && b_escaped[1] != 0)
+ b_escaped++;
+ if (*a < *b_escaped)
+ return -1;
+ if (*a > *b_escaped)
+ return +1;
+ a++;
+ b_escaped++;
+ }
+ if (*a)
+ return +1;
+ if (*b_escaped)
+ return -1;
+ return 0;
+}
+
+static int
+find_grub_drive (const char *name)
+{
+ unsigned int i;
+
+ if (name)
+ {
+ for (i = 0; i < ARRAY_SIZE (map); i++)
+ if (map[i].drive && unescape_cmp (map[i].drive, name) == 0)
+ return i;
+ }
+
+ return -1;
+}
+
+static int
+find_free_slot (void)
+{
+ unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE (map); i++)
+ if (! map[i].drive)
+ return i;
+
+ return -1;
+}
+
+static int
+grub_util_biosdisk_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
+ grub_disk_pull_t pull)
+{
+ unsigned i;
+
+ if (pull != GRUB_DISK_PULL_NONE)
+ return 0;
+
+ for (i = 0; i < ARRAY_SIZE (map); i++)
+ if (map[i].drive && hook (map[i].drive, hook_data))
+ return 1;
+
+ return 0;
+}
+
+static grub_err_t
+grub_util_biosdisk_open (const char *name, grub_disk_t disk)
+{
+ int drive;
+ struct grub_util_hostdisk_data *data;
+
+ drive = find_grub_drive (name);
+ grub_util_info ("drive = %d", drive);
+ if (drive < 0)
+ return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
+ "no mapping exists for `%s'", name);
+
+ disk->id = drive;
+ disk->data = data = xmalloc (sizeof (struct grub_util_hostdisk_data));
+ data->dev = NULL;
+ data->access_mode = 0;
+ data->fd = GRUB_UTIL_FD_INVALID;
+ data->is_disk = 0;
+ data->device_map = map[drive].device_map;
+
+ /* Get the size. */
+ {
+ grub_util_fd_t fd;
+
+ fd = grub_util_fd_open (map[drive].device, GRUB_UTIL_FD_O_RDONLY);
+
+ if (!GRUB_UTIL_FD_IS_VALID(fd))
+ return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("cannot open `%s': %s"),
+ map[drive].device, grub_util_fd_strerror ());
+
+ disk->total_sectors = grub_util_get_fd_size (fd, map[drive].device,
+ &disk->log_sector_size);
+ disk->total_sectors >>= disk->log_sector_size;
+ disk->max_agglomerate = GRUB_DISK_MAX_MAX_AGGLOMERATE;
+
+#if GRUB_UTIL_FD_STAT_IS_FUNCTIONAL
+ {
+ struct stat st;
+# if GRUB_DISK_DEVS_ARE_CHAR
+ if (fstat (fd, &st) >= 0 && S_ISCHR (st.st_mode))
+# else
+ if (fstat (fd, &st) >= 0 && S_ISBLK (st.st_mode))
+# endif
+ data->is_disk = 1;
+ }
+#endif
+
+ grub_util_fd_close (fd);
+
+ grub_util_info ("the size of %s is %" GRUB_HOST_PRIuLONG_LONG,
+ name, (unsigned long long) disk->total_sectors);
+
+ return GRUB_ERR_NONE;
+ }
+}
+
+const char *
+grub_hostdisk_os_dev_to_grub_drive (const char *os_disk, int add)
+{
+ unsigned int i;
+ char *canon;
+
+ canon = grub_canonicalize_file_name (os_disk);
+ if (!canon)
+ canon = xstrdup (os_disk);
+
+ for (i = 0; i < ARRAY_SIZE (map); i++)
+ if (! map[i].device)
+ break;
+ else if (strcmp (map[i].device, canon) == 0)
+ {
+ free (canon);
+ return map[i].drive;
+ }
+
+ if (!add)
+ {
+ free (canon);
+ return NULL;
+ }
+
+ if (i == ARRAY_SIZE (map))
+ /* TRANSLATORS: it refers to the lack of free slots. */
+ grub_util_error ("%s", _("device count exceeds limit"));
+
+ map[i].device = canon;
+ map[i].drive = xmalloc (sizeof ("hostdisk/") + strlen (os_disk));
+ strcpy (map[i].drive, "hostdisk/");
+ strcpy (map[i].drive + sizeof ("hostdisk/") - 1, os_disk);
+ map[i].device_map = 0;
+
+ grub_hostdisk_flush_initial_buffer (os_disk);
+
+ return map[i].drive;
+}
+
+#ifndef __linux__
+grub_util_fd_t
+grub_util_fd_open_device (const grub_disk_t disk, grub_disk_addr_t sector, int flags,
+ grub_disk_addr_t *max)
+{
+ grub_util_fd_t fd;
+ struct grub_util_hostdisk_data *data = disk->data;
+
+ *max = ~0ULL;
+
+ flags |= GRUB_UTIL_FD_O_SYNC;
+
+ if (data->dev && strcmp (data->dev, map[disk->id].device) == 0 &&
+ data->access_mode == (flags & O_ACCMODE))
+ {
+ grub_dprintf ("hostdisk", "reusing open device `%s'\n", data->dev);
+ fd = data->fd;
+ }
+ else
+ {
+ free (data->dev);
+ data->dev = 0;
+ if (GRUB_UTIL_FD_IS_VALID(data->fd))
+ {
+ if (data->access_mode == O_RDWR || data->access_mode == O_WRONLY)
+ grub_util_fd_sync (data->fd);
+ grub_util_fd_close (data->fd);
+ data->fd = GRUB_UTIL_FD_INVALID;
+ }
+
+ fd = grub_util_fd_open (map[disk->id].device, flags);
+ if (GRUB_UTIL_FD_IS_VALID(fd))
+ {
+ data->dev = xstrdup (map[disk->id].device);
+ data->access_mode = (flags & O_ACCMODE);
+ data->fd = fd;
+ }
+ }
+
+ if (!GRUB_UTIL_FD_IS_VALID(data->fd))
+ {
+ grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot open `%s': %s"),
+ map[disk->id].device, grub_util_fd_strerror ());
+ return GRUB_UTIL_FD_INVALID;
+ }
+
+ if (grub_util_fd_seek (fd, sector << disk->log_sector_size))
+ {
+ grub_util_fd_close (fd);
+ grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot seek `%s': %s"),
+ map[disk->id].device, grub_util_fd_strerror ());
+
+ return GRUB_UTIL_FD_INVALID;
+ }
+
+ return fd;
+}
+#endif
+
+
+static grub_err_t
+grub_util_biosdisk_read (grub_disk_t disk, grub_disk_addr_t sector,
+ grub_size_t size, char *buf)
+{
+ while (size)
+ {
+ grub_util_fd_t fd;
+ grub_disk_addr_t max = ~0ULL;
+ fd = grub_util_fd_open_device (disk, sector, GRUB_UTIL_FD_O_RDONLY, &max);
+ if (!GRUB_UTIL_FD_IS_VALID (fd))
+ return grub_errno;
+
+#ifdef __linux__
+ if (sector == 0)
+ /* Work around a bug in Linux ez remapping. Linux remaps all
+ sectors that are read together with the MBR in one read. It
+ should only remap the MBR, so we split the read in two
+ parts. -jochen */
+ max = 1;
+#endif /* __linux__ */
+
+ if (max > size)
+ max = size;
+
+ if (grub_util_fd_read (fd, buf, max << disk->log_sector_size)
+ != (ssize_t) (max << disk->log_sector_size))
+ return grub_error (GRUB_ERR_READ_ERROR, N_("cannot read `%s': %s"),
+ map[disk->id].device, grub_util_fd_strerror ());
+ size -= max;
+ buf += (max << disk->log_sector_size);
+ sector += max;
+ }
+ return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+grub_util_biosdisk_write (grub_disk_t disk, grub_disk_addr_t sector,
+ grub_size_t size, const char *buf)
+{
+ while (size)
+ {
+ grub_util_fd_t fd;
+ grub_disk_addr_t max = ~0ULL;
+ fd = grub_util_fd_open_device (disk, sector, GRUB_UTIL_FD_O_WRONLY, &max);
+ if (!GRUB_UTIL_FD_IS_VALID (fd))
+ return grub_errno;
+
+#ifdef __linux__
+ if (sector == 0)
+ /* Work around a bug in Linux ez remapping. Linux remaps all
+ sectors that are write together with the MBR in one write. It
+ should only remap the MBR, so we split the write in two
+ parts. -jochen */
+ max = 1;
+#endif /* __linux__ */
+
+ if (max > size)
+ max = size;
+
+ if (grub_util_fd_write (fd, buf, max << disk->log_sector_size)
+ != (ssize_t) (max << disk->log_sector_size))
+ return grub_error (GRUB_ERR_WRITE_ERROR, N_("cannot write to `%s': %s"),
+ map[disk->id].device, grub_util_fd_strerror ());
+ size -= max;
+ buf += (max << disk->log_sector_size);
+ }
+ return GRUB_ERR_NONE;
+}
+
+grub_err_t
+grub_util_biosdisk_flush (struct grub_disk *disk)
+{
+ struct grub_util_hostdisk_data *data = disk->data;
+
+ if (disk->dev->id != GRUB_DISK_DEVICE_BIOSDISK_ID)
+ return GRUB_ERR_NONE;
+ if (!GRUB_UTIL_FD_IS_VALID (data->fd))
+ {
+ grub_disk_addr_t max;
+ data->fd = grub_util_fd_open_device (disk, 0, GRUB_UTIL_FD_O_RDONLY, &max);
+ if (!GRUB_UTIL_FD_IS_VALID (data->fd))
+ return grub_errno;
+ }
+ grub_util_fd_sync (data->fd);
+#ifdef __linux__
+ if (data->is_disk)
+ ioctl (data->fd, BLKFLSBUF, 0);
+#endif
+ return GRUB_ERR_NONE;
+}
+
+static void
+grub_util_biosdisk_close (struct grub_disk *disk)
+{
+ struct grub_util_hostdisk_data *data = disk->data;
+
+ free (data->dev);
+ if (GRUB_UTIL_FD_IS_VALID (data->fd))
+ {
+ if (data->access_mode == O_RDWR || data->access_mode == O_WRONLY)
+ grub_util_biosdisk_flush (disk);
+ grub_util_fd_close (data->fd);
+ }
+ free (data);
+}
+
+static struct grub_disk_dev grub_util_biosdisk_dev =
+ {
+ .name = "hostdisk",
+ .id = GRUB_DISK_DEVICE_HOSTDISK_ID,
+ .disk_iterate = grub_util_biosdisk_iterate,
+ .disk_open = grub_util_biosdisk_open,
+ .disk_close = grub_util_biosdisk_close,
+ .disk_read = grub_util_biosdisk_read,
+ .disk_write = grub_util_biosdisk_write,
+ .next = 0
+ };
+
+static int
+grub_util_check_file_presence (const char *p)
+{
+#if !GRUB_UTIL_FD_STAT_IS_FUNCTIONAL
+ grub_util_fd_t h;
+ h = grub_util_fd_open (p, GRUB_UTIL_FD_O_RDONLY);
+ if (!GRUB_UTIL_FD_IS_VALID(h))
+ return 0;
+ grub_util_fd_close (h);
+ return 1;
+#else
+ struct stat st;
+
+ if (stat (p, &st) == -1)
+ return 0;
+ return 1;
+#endif
+}
+
+static void
+read_device_map (const char *dev_map)
+{
+ FILE *fp;
+ char buf[1024]; /* XXX */
+ int lineno = 0;
+
+ if (!dev_map || dev_map[0] == '\0')
+ {
+ grub_util_info ("no device.map");
+ return;
+ }
+
+ fp = grub_util_fopen (dev_map, "r");
+ if (! fp)
+ {
+ grub_util_info (_("cannot open `%s': %s"), dev_map, strerror (errno));
+ return;
+ }
+
+ while (fgets (buf, sizeof (buf), fp))
+ {
+ char *p = buf;
+ char *e;
+ char *drive_e, *drive_p;
+ int drive;
+
+ lineno++;
+
+ /* Skip leading spaces. */
+ while (*p && grub_isspace (*p))
+ p++;
+
+ /* If the first character is `#' or NUL, skip this line. */
+ if (*p == '\0' || *p == '#')
+ continue;
+
+ if (*p != '(')
+ {
+ char *tmp;
+ tmp = xasprintf (_("missing `%c' symbol"), '(');
+ grub_util_error ("%s:%d: %s", dev_map, lineno, tmp);
+ }
+
+ p++;
+ /* Find a free slot. */
+ drive = find_free_slot ();
+ if (drive < 0)
+ grub_util_error ("%s:%d: %s", dev_map, lineno, _("device count exceeds limit"));
+
+ e = p;
+ p = strchr (p, ')');
+ if (! p)
+ {
+ char *tmp;
+ tmp = xasprintf (_("missing `%c' symbol"), ')');
+ grub_util_error ("%s:%d: %s", dev_map, lineno, tmp);
+ }
+
+ map[drive].drive = 0;
+ if ((e[0] == 'f' || e[0] == 'h' || e[0] == 'c') && e[1] == 'd')
+ {
+ char *ptr;
+ for (ptr = e + 2; ptr < p; ptr++)
+ if (!grub_isdigit (*ptr))
+ break;
+ if (ptr == p)
+ {
+ map[drive].drive = xmalloc (p - e + sizeof ('\0'));
+ strncpy (map[drive].drive, e, p - e + sizeof ('\0'));
+ map[drive].drive[p - e] = '\0';
+ }
+ if (*ptr == ',')
+ {
+ *p = 0;
+
+ /* TRANSLATORS: Only one entry is ignored. However the suggestion
+ is to correct/delete the whole file.
+ device.map is a file indicating which
+ devices are available at boot time. Fedora populated it with
+ entries like (hd0,1) /dev/sda1 which would mean that every
+ partition is a separate disk for BIOS. Such entries were
+ inactive in GRUB due to its bug which is now gone. Without
+ this additional check these entries would be harmful now.
+ */
+ grub_util_warn (_("the device.map entry `%s' is invalid. "
+ "Ignoring it. Please correct or "
+ "delete your device.map"), e);
+ continue;
+ }
+ }
+ drive_e = e;
+ drive_p = p;
+ map[drive].device_map = 1;
+
+ p++;
+ /* Skip leading spaces. */
+ while (*p && grub_isspace (*p))
+ p++;
+
+ if (*p == '\0')
+ grub_util_error ("%s:%d: %s", dev_map, lineno, _("filename expected"));
+
+ /* NUL-terminate the filename. */
+ e = p;
+ while (*e && ! grub_isspace (*e))
+ e++;
+ *e = '\0';
+
+ if (!grub_util_check_file_presence (p))
+ {
+ free (map[drive].drive);
+ map[drive].drive = NULL;
+ grub_util_info ("Cannot stat `%s', skipping", p);
+ continue;
+ }
+
+ /* On Linux, the devfs uses symbolic links horribly, and that
+ confuses the interface very much, so use realpath to expand
+ symbolic links. */
+ map[drive].device = grub_canonicalize_file_name (p);
+ if (! map[drive].device)
+ map[drive].device = xstrdup (p);
+
+ if (!map[drive].drive)
+ {
+ char c;
+ map[drive].drive = xmalloc (sizeof ("hostdisk/") + strlen (p));
+ memcpy (map[drive].drive, "hostdisk/", sizeof ("hostdisk/") - 1);
+ strcpy (map[drive].drive + sizeof ("hostdisk/") - 1, p);
+ c = *drive_p;
+ *drive_p = 0;
+ /* TRANSLATORS: device.map is a filename. Not to be translated.
+ device.map specifies disk correspondance overrides. Previously
+ one could create any kind of device name with this. Due to
+ some problems we decided to limit it to just a handful
+ possibilities. */
+ grub_util_warn (_("the drive name `%s' in device.map is incorrect. "
+ "Using %s instead. "
+ "Please use the form [hfc]d[0-9]* "
+ "(E.g. `hd0' or `cd')"),
+ drive_e, map[drive].drive);
+ *drive_p = c;
+ }
+
+ grub_util_info ("adding `%s' -> `%s' from device.map", map[drive].drive,
+ map[drive].device);
+
+ grub_hostdisk_flush_initial_buffer (map[drive].device);
+ }
+
+ fclose (fp);
+}
+
+void
+grub_util_biosdisk_init (const char *dev_map)
+{
+ read_device_map (dev_map);
+ grub_disk_dev_register (&grub_util_biosdisk_dev);
+}
+
+void
+grub_util_biosdisk_fini (void)
+{
+ unsigned i;
+
+ for (i = 0; i < ARRAY_SIZE(map); i++)
+ {
+ if (map[i].drive)
+ free (map[i].drive);
+ if (map[i].device)
+ free (map[i].device);
+ map[i].drive = map[i].device = NULL;
+ }
+
+ grub_disk_dev_unregister (&grub_util_biosdisk_dev);
+}
+
+const char *
+grub_util_biosdisk_get_compatibility_hint (grub_disk_t disk)
+{
+ if (disk->dev != &grub_util_biosdisk_dev || map[disk->id].device_map)
+ return disk->name;
+ return 0;
+}
+
+const char *
+grub_util_biosdisk_get_osdev (grub_disk_t disk)
+{
+ if (disk->dev != &grub_util_biosdisk_dev)
+ return 0;
+
+ return map[disk->id].device;
+}
+
+
+static char *
+grub_util_path_concat_real (size_t n, int ext, va_list ap)
+{
+ size_t totlen = 0;
+ char **l = xcalloc (n + ext, sizeof (l[0]));
+ char *r, *p, *pi;
+ size_t i;
+ int first = 1;
+
+ for (i = 0; i < n + ext; i++)
+ {
+ l[i] = va_arg (ap, char *);
+ if (l[i])
+ totlen += strlen (l[i]) + 1;
+ }
+
+ r = xmalloc (totlen + 10);
+
+ p = r;
+ for (i = 0; i < n; i++)
+ {
+ pi = l[i];
+ if (!pi)
+ continue;
+ while (*pi == '/')
+ pi++;
+ if ((p != r || (pi != l[i] && first)) && (p == r || *(p - 1) != '/'))
+ *p++ = '/';
+ first = 0;
+ p = grub_stpcpy (p, pi);
+ while (p != r && p != r + 1 && *(p - 1) == '/')
+ p--;
+ }
+
+ if (ext && l[i])
+ p = grub_stpcpy (p, l[i]);
+
+ *p = '\0';
+
+ free (l);
+
+ return r;
+}
+
+char *
+grub_util_path_concat (size_t n, ...)
+{
+ va_list ap;
+ char *r;
+
+ va_start (ap, n);
+
+ r = grub_util_path_concat_real (n, 0, ap);
+
+ va_end (ap);
+
+ return r;
+}
+
+char *
+grub_util_path_concat_ext (size_t n, ...)
+{
+ va_list ap;
+ char *r;
+
+ va_start (ap, n);
+
+ r = grub_util_path_concat_real (n, 1, ap);
+
+ va_end (ap);
+
+ return r;
+}
diff --git a/grub-core/kern/emu/hostfs.c b/grub-core/kern/emu/hostfs.c
new file mode 100644
index 0000000..cb53210
--- /dev/null
+++ b/grub-core/kern/emu/hostfs.c
@@ -0,0 +1,200 @@
+/* hostfs.c - Dummy filesystem to provide access to the hosts filesystem */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <config-util.h>
+
+#include <grub/fs.h>
+#include <grub/file.h>
+#include <grub/disk.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/dl.h>
+#include <grub/util/misc.h>
+#include <grub/emu/hostdisk.h>
+#include <grub/i18n.h>
+
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+
+static int
+is_dir (const char *path, const char *name)
+{
+ int len1 = strlen(path);
+ int len2 = strlen(name);
+ int ret;
+
+ char *pathname = xmalloc (len1 + 1 + len2 + 1 + 13);
+ strcpy (pathname, path);
+
+ /* Avoid UNC-path "//name" on Cygwin. */
+ if (len1 > 0 && pathname[len1 - 1] != '/')
+ strcat (pathname, "/");
+
+ strcat (pathname, name);
+
+ ret = grub_util_is_directory (pathname);
+ free (pathname);
+ return ret;
+}
+
+struct grub_hostfs_data
+{
+ char *filename;
+ grub_util_fd_t f;
+};
+
+static grub_err_t
+grub_hostfs_dir (grub_device_t device, const char *path,
+ grub_fs_dir_hook_t hook, void *hook_data)
+{
+ grub_util_fd_dir_t dir;
+
+ /* Check if the disk is our dummy disk. */
+ if (grub_strcmp (device->disk->name, "host"))
+ return grub_error (GRUB_ERR_BAD_FS, "not a hostfs");
+
+ dir = grub_util_fd_opendir (path);
+ if (! dir)
+ return grub_error (GRUB_ERR_BAD_FILENAME,
+ N_("can't open `%s': %s"), path,
+ grub_util_fd_strerror ());
+
+ while (1)
+ {
+ grub_util_fd_dirent_t de;
+ struct grub_dirhook_info info;
+ grub_memset (&info, 0, sizeof (info));
+
+ de = grub_util_fd_readdir (dir);
+ if (! de)
+ break;
+
+ info.dir = !! is_dir (path, de->d_name);
+ hook (de->d_name, &info, hook_data);
+
+ }
+
+ grub_util_fd_closedir (dir);
+
+ return GRUB_ERR_NONE;
+}
+
+/* Open a file named NAME and initialize FILE. */
+static grub_err_t
+grub_hostfs_open (struct grub_file *file, const char *name)
+{
+ grub_util_fd_t f;
+ struct grub_hostfs_data *data;
+
+ f = grub_util_fd_open (name, GRUB_UTIL_FD_O_RDONLY);
+ if (! GRUB_UTIL_FD_IS_VALID (f))
+ return grub_error (GRUB_ERR_BAD_FILENAME,
+ N_("can't open `%s': %s"), name,
+ strerror (errno));
+ data = grub_malloc (sizeof (*data));
+ if (!data)
+ {
+ grub_util_fd_close (f);
+ return grub_errno;
+ }
+ data->filename = grub_strdup (name);
+ if (!data->filename)
+ {
+ grub_free (data);
+ grub_util_fd_close (f);
+ return grub_errno;
+ }
+
+ data->f = f;
+
+ file->data = data;
+
+ file->size = grub_util_get_fd_size (f, name, NULL);
+
+ return GRUB_ERR_NONE;
+}
+
+static grub_ssize_t
+grub_hostfs_read (grub_file_t file, char *buf, grub_size_t len)
+{
+ struct grub_hostfs_data *data;
+
+ data = file->data;
+ if (grub_util_fd_seek (data->f, file->offset) != 0)
+ {
+ grub_error (GRUB_ERR_OUT_OF_RANGE, N_("cannot seek `%s': %s"),
+ data->filename, grub_util_fd_strerror ());
+ return -1;
+ }
+
+ unsigned int s = grub_util_fd_read (data->f, buf, len);
+ if (s != len)
+ grub_error (GRUB_ERR_FILE_READ_ERROR, N_("cannot read `%s': %s"),
+ data->filename, grub_util_fd_strerror ());
+
+ return (signed) s;
+}
+
+static grub_err_t
+grub_hostfs_close (grub_file_t file)
+{
+ struct grub_hostfs_data *data;
+
+ data = file->data;
+ grub_util_fd_close (data->f);
+ grub_free (data->filename);
+ grub_free (data);
+
+ return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+grub_hostfs_label (grub_device_t device __attribute ((unused)),
+ char **label __attribute ((unused)))
+{
+ *label = 0;
+ return GRUB_ERR_NONE;
+}
+
+#undef open
+#undef close
+
+static struct grub_fs grub_hostfs_fs =
+ {
+ .name = "hostfs",
+ .fs_dir = grub_hostfs_dir,
+ .fs_open = grub_hostfs_open,
+ .fs_read = grub_hostfs_read,
+ .fs_close = grub_hostfs_close,
+ .fs_label = grub_hostfs_label,
+ .next = 0
+ };
+
+
+
+GRUB_MOD_INIT(hostfs)
+{
+ grub_fs_register (&grub_hostfs_fs);
+}
+
+GRUB_MOD_FINI(hostfs)
+{
+ grub_fs_unregister (&grub_hostfs_fs);
+}
diff --git a/grub-core/kern/emu/lite.c b/grub-core/kern/emu/lite.c
new file mode 100644
index 0000000..b327d4e
--- /dev/null
+++ b/grub-core/kern/emu/lite.c
@@ -0,0 +1,47 @@
+#include <config.h>
+#include <grub/emu/misc.h>
+
+#ifndef GRUB_MACHINE_EMU
+#error "This source is only meant for grub-emu platform"
+#endif
+
+#if defined(__i386__)
+#include "../i386/dl.c"
+#elif defined(__x86_64__)
+#include "../x86_64/dl.c"
+#elif defined(__sparc__)
+#include "../sparc64/dl.c"
+#elif defined(__mips__)
+#include "../mips/dl.c"
+#elif defined(__powerpc__)
+#include "../powerpc/dl.c"
+#elif defined(__ia64__)
+#include "../ia64/dl_helper.c"
+#include "../ia64/dl.c"
+#elif defined(__arm__)
+#include "../arm/dl_helper.c"
+#include "../arm/dl.c"
+#elif defined(__aarch64__)
+#include "../arm64/dl_helper.c"
+#include "../arm64/dl.c"
+#elif defined(__riscv)
+#include "../riscv/dl.c"
+#else
+#error "No target cpu type is defined"
+#endif
+
+const int grub_no_modules = 0;
+
+/* grub-emu-lite supports dynamic module loading, so it won't have any
+ embedded modules. */
+void
+grub_init_all (void)
+{
+ return;
+}
+
+void
+grub_fini_all (void)
+{
+ return;
+}
diff --git a/grub-core/kern/emu/main.c b/grub-core/kern/emu/main.c
new file mode 100644
index 0000000..425bb96
--- /dev/null
+++ b/grub-core/kern/emu/main.c
@@ -0,0 +1,286 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2003,2004,2005,2006,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <config-util.h>
+
+#include <time.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <setjmp.h>
+#include <string.h>
+#include <signal.h>
+#include <sys/types.h>
+
+#include <grub/dl.h>
+#include <grub/mm.h>
+#include <grub/setjmp.h>
+#include <grub/fs.h>
+#include <grub/emu/hostdisk.h>
+#include <grub/time.h>
+#include <grub/emu/console.h>
+#include <grub/emu/misc.h>
+#include <grub/kernel.h>
+#include <grub/normal.h>
+#include <grub/emu/getroot.h>
+#include <grub/env.h>
+#include <grub/partition.h>
+#include <grub/i18n.h>
+#include <grub/loader.h>
+#include <grub/util/misc.h>
+
+#pragma GCC diagnostic ignored "-Wmissing-prototypes"
+
+#include "progname.h"
+#include <argp.h>
+
+#define ENABLE_RELOCATABLE 0
+
+/* Used for going back to the main function. */
+static jmp_buf main_env;
+
+/* Store the prefix specified by an argument. */
+static char *root_dev = NULL, *dir = NULL;
+
+grub_addr_t grub_modbase = 0;
+
+void
+grub_reboot (void)
+{
+ longjmp (main_env, 1);
+ grub_fatal ("longjmp failed");
+}
+
+void
+grub_exit (void)
+{
+ grub_reboot ();
+}
+
+void
+grub_machine_init (void)
+{
+}
+
+void
+grub_machine_get_bootlocation (char **device, char **path)
+{
+ *device = root_dev;
+ *path = dir;
+}
+
+void
+grub_machine_fini (int flags)
+{
+ if (flags & GRUB_LOADER_FLAG_NORETURN)
+ grub_console_fini ();
+}
+
+
+
+#define OPT_MEMDISK 257
+
+static struct argp_option options[] = {
+ {"root", 'r', N_("DEVICE_NAME"), 0, N_("Set root device."), 2},
+ {"device-map", 'm', N_("FILE"), 0,
+ /* TRANSLATORS: There are many devices in device map. */
+ N_("use FILE as the device map [default=%s]"), 0},
+ {"memdisk", OPT_MEMDISK, N_("FILE"), 0,
+ /* TRANSLATORS: There are many devices in device map. */
+ N_("use FILE as memdisk"), 0},
+ {"directory", 'd', N_("DIR"), 0,
+ N_("use GRUB files in the directory DIR [default=%s]"), 0},
+ {"verbose", 'v', 0, 0, N_("print verbose messages."), 0},
+ {"hold", 'H', N_("SECS"), OPTION_ARG_OPTIONAL, N_("wait until a debugger will attach"), 0},
+ { 0, 0, 0, 0, 0, 0 }
+};
+
+#pragma GCC diagnostic ignored "-Wformat-nonliteral"
+
+static char *
+help_filter (int key, const char *text, void *input __attribute__ ((unused)))
+{
+ switch (key)
+ {
+ case 'd':
+ return xasprintf (text, DEFAULT_DIRECTORY);
+ case 'm':
+ return xasprintf (text, DEFAULT_DEVICE_MAP);
+ default:
+ return (char *) text;
+ }
+}
+
+#pragma GCC diagnostic error "-Wformat-nonliteral"
+
+struct arguments
+{
+ const char *dev_map;
+ const char *mem_disk;
+ int hold;
+};
+
+static error_t
+argp_parser (int key, char *arg, struct argp_state *state)
+{
+ /* Get the input argument from argp_parse, which we
+ know is a pointer to our arguments structure. */
+ struct arguments *arguments = state->input;
+
+ switch (key)
+ {
+ case OPT_MEMDISK:
+ arguments->mem_disk = arg;
+ break;
+ case 'r':
+ free (root_dev);
+ root_dev = xstrdup (arg);
+ break;
+ case 'd':
+ free (dir);
+ dir = xstrdup (arg);
+ break;
+ case 'm':
+ arguments->dev_map = arg;
+ break;
+ case 'H':
+ arguments->hold = (arg ? atoi (arg) : -1);
+ break;
+ case 'v':
+ verbosity++;
+ break;
+
+ case ARGP_KEY_ARG:
+ {
+ /* Too many arguments. */
+ fprintf (stderr, _("Unknown extra argument `%s'."), arg);
+ fprintf (stderr, "\n");
+ argp_usage (state);
+ }
+ break;
+
+ default:
+ return ARGP_ERR_UNKNOWN;
+ }
+ return 0;
+}
+
+static struct argp argp = {
+ options, argp_parser, NULL,
+ N_("GRUB emulator."),
+ NULL, help_filter, NULL
+};
+
+
+
+#pragma GCC diagnostic ignored "-Wmissing-prototypes"
+
+int
+main (int argc, char *argv[])
+{
+ struct arguments arguments =
+ {
+ .dev_map = DEFAULT_DEVICE_MAP,
+ .hold = 0,
+ .mem_disk = 0,
+ };
+ volatile int hold = 0;
+ size_t total_module_size = sizeof (struct grub_module_info), memdisk_size = 0;
+ struct grub_module_info *modinfo;
+ void *mods;
+
+ grub_util_host_init (&argc, &argv);
+
+ dir = xstrdup (DEFAULT_DIRECTORY);
+
+ if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
+ {
+ fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
+ exit(1);
+ }
+
+ if (arguments.mem_disk)
+ {
+ memdisk_size = ALIGN_UP(grub_util_get_image_size (arguments.mem_disk), 512);
+ total_module_size += memdisk_size + sizeof (struct grub_module_header);
+ }
+
+ mods = xmalloc (total_module_size);
+ modinfo = grub_memset (mods, 0, total_module_size);
+ mods = (char *) (modinfo + 1);
+
+ modinfo->magic = GRUB_MODULE_MAGIC;
+ modinfo->offset = sizeof (struct grub_module_info);
+ modinfo->size = total_module_size;
+
+ if (arguments.mem_disk)
+ {
+ struct grub_module_header *header = (struct grub_module_header *) mods;
+ header->type = OBJ_TYPE_MEMDISK;
+ header->size = memdisk_size + sizeof (*header);
+ mods = header + 1;
+
+ grub_util_load_image (arguments.mem_disk, mods);
+ mods = (char *) mods + memdisk_size;
+ }
+
+ grub_modbase = (grub_addr_t) modinfo;
+
+ hold = arguments.hold;
+ /* Wait until the ARGS.HOLD variable is cleared by an attached debugger. */
+ if (hold && verbosity > 0)
+ /* TRANSLATORS: In this case GRUB tells user what he has to do. */
+ printf (_("Run `gdb %s %d', and set ARGS.HOLD to zero.\n"),
+ program_name, (int) getpid ());
+ while (hold)
+ {
+ if (hold > 0)
+ hold--;
+
+ sleep (1);
+ }
+
+ signal (SIGINT, SIG_IGN);
+ grub_console_init ();
+ grub_host_init ();
+
+ /* XXX: This is a bit unportable. */
+ grub_util_biosdisk_init (arguments.dev_map);
+
+ grub_init_all ();
+
+ grub_hostfs_init ();
+
+ /* Make sure that there is a root device. */
+ if (! root_dev)
+ root_dev = grub_strdup ("host");
+
+ dir = xstrdup (dir);
+
+ /* Start GRUB! */
+ if (setjmp (main_env) == 0)
+ grub_main ();
+
+ grub_fini_all ();
+ grub_hostfs_fini ();
+ grub_host_fini ();
+
+ grub_machine_fini (GRUB_LOADER_FLAG_NORETURN);
+
+ return 0;
+}
diff --git a/grub-core/kern/emu/misc.c b/grub-core/kern/emu/misc.c
new file mode 100644
index 0000000..dfd8a8e
--- /dev/null
+++ b/grub-core/kern/emu/misc.c
@@ -0,0 +1,216 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2002,2003,2005,2006,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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GRUB_BUILD
+#include <config-util.h>
+#endif
+#include <config.h>
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <sys/time.h>
+#include <sys/types.h>
+
+#include <grub/mm.h>
+#include <grub/err.h>
+#include <grub/env.h>
+#include <grub/types.h>
+#include <grub/misc.h>
+#include <grub/i18n.h>
+#include <grub/time.h>
+#include <grub/emu/misc.h>
+
+int verbosity;
+
+void
+grub_util_warn (const char *fmt, ...)
+{
+ va_list ap;
+
+ fprintf (stderr, _("%s: warning:"), program_name);
+ fprintf (stderr, " ");
+ va_start (ap, fmt);
+ vfprintf (stderr, fmt, ap);
+ va_end (ap);
+ fprintf (stderr, ".\n");
+ fflush (stderr);
+}
+
+void
+grub_util_info (const char *fmt, ...)
+{
+ if (verbosity > 0)
+ {
+ va_list ap;
+
+ fprintf (stderr, _("%s: info:"), program_name);
+ fprintf (stderr, " ");
+ va_start (ap, fmt);
+ vfprintf (stderr, fmt, ap);
+ va_end (ap);
+ fprintf (stderr, ".\n");
+ fflush (stderr);
+ }
+}
+
+void
+grub_util_error (const char *fmt, ...)
+{
+ va_list ap;
+
+ fprintf (stderr, _("%s: error:"), program_name);
+ fprintf (stderr, " ");
+ va_start (ap, fmt);
+ vfprintf (stderr, fmt, ap);
+ va_end (ap);
+ fprintf (stderr, ".\n");
+ exit (1);
+}
+
+void *
+xcalloc (grub_size_t nmemb, grub_size_t size)
+{
+ void *p;
+
+ p = calloc (nmemb, size);
+ if (!p)
+ grub_util_error ("%s", _("out of memory"));
+
+ return p;
+}
+
+void *
+xmalloc (grub_size_t size)
+{
+ void *p;
+
+ p = malloc (size);
+ if (! p)
+ grub_util_error ("%s", _("out of memory"));
+
+ return p;
+}
+
+void *
+xrealloc (void *ptr, grub_size_t size)
+{
+ ptr = realloc (ptr, size);
+ if (! ptr)
+ grub_util_error ("%s", _("out of memory"));
+
+ return ptr;
+}
+
+char *
+xstrdup (const char *str)
+{
+ size_t len;
+ char *newstr;
+
+ len = strlen (str);
+ newstr = (char *) xmalloc (len + 1);
+ memcpy (newstr, str, len + 1);
+
+ return newstr;
+}
+
+#if !defined (GRUB_MKFONT) && !defined (GRUB_BUILD)
+char *
+xasprintf (const char *fmt, ...)
+{
+ va_list ap;
+ char *result;
+
+ va_start (ap, fmt);
+ result = grub_xvasprintf (fmt, ap);
+ va_end (ap);
+ if (!result)
+ grub_util_error ("%s", _("out of memory"));
+
+ return result;
+}
+#endif
+
+#if !defined (GRUB_MACHINE_EMU) || defined (GRUB_UTIL)
+void
+grub_exit (void)
+{
+ exit (1);
+}
+#endif
+
+grub_uint64_t
+grub_get_time_ms (void)
+{
+ struct timeval tv;
+
+ gettimeofday (&tv, 0);
+
+ return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
+}
+
+size_t
+grub_util_get_image_size (const char *path)
+{
+ FILE *f;
+ size_t ret;
+ off_t sz;
+
+ f = grub_util_fopen (path, "rb");
+
+ if (!f)
+ grub_util_error (_("cannot open `%s': %s"), path, strerror (errno));
+
+ fseeko (f, 0, SEEK_END);
+
+ sz = ftello (f);
+ if (sz < 0)
+ grub_util_error (_("cannot open `%s': %s"), path, strerror (errno));
+ if (sz != (size_t) sz)
+ grub_util_error (_("file `%s' is too big"), path);
+ ret = (size_t) sz;
+
+ fclose (f);
+
+ return ret;
+}
+
+void
+grub_util_load_image (const char *path, char *buf)
+{
+ FILE *fp;
+ size_t size;
+
+ grub_util_info ("reading %s", path);
+
+ size = grub_util_get_image_size (path);
+
+ fp = grub_util_fopen (path, "rb");
+ if (! fp)
+ grub_util_error (_("cannot open `%s': %s"), path,
+ strerror (errno));
+
+ if (fread (buf, 1, size, fp) != size)
+ grub_util_error (_("cannot read `%s': %s"), path,
+ strerror (errno));
+
+ fclose (fp);
+}
diff --git a/grub-core/kern/emu/mm.c b/grub-core/kern/emu/mm.c
new file mode 100644
index 0000000..4d1046a
--- /dev/null
+++ b/grub-core/kern/emu/mm.c
@@ -0,0 +1,75 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2002,2003,2005,2006,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <config-util.h>
+
+#include <grub/types.h>
+#include <grub/err.h>
+#include <grub/mm.h>
+#include <stdlib.h>
+#include <string.h>
+#include <grub/i18n.h>
+
+void *
+grub_calloc (grub_size_t nmemb, grub_size_t size)
+{
+ void *ret;
+ ret = calloc (nmemb, size);
+ if (!ret)
+ grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
+ return ret;
+}
+
+void *
+grub_malloc (grub_size_t size)
+{
+ void *ret;
+ ret = malloc (size);
+ if (!ret)
+ grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
+ return ret;
+}
+
+void *
+grub_zalloc (grub_size_t size)
+{
+ void *ret;
+
+ ret = grub_malloc (size);
+ if (!ret)
+ return NULL;
+ memset (ret, 0, size);
+ return ret;
+}
+
+void
+grub_free (void *ptr)
+{
+ if (ptr)
+ free (ptr);
+}
+
+void *
+grub_realloc (void *ptr, grub_size_t size)
+{
+ void *ret;
+ ret = realloc (ptr, size);
+ if (!ret)
+ grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
+ return ret;
+}
diff --git a/grub-core/kern/emu/time.c b/grub-core/kern/emu/time.c
new file mode 100644
index 0000000..5da8092
--- /dev/null
+++ b/grub-core/kern/emu/time.c
@@ -0,0 +1,46 @@
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/datetime.h>
+#include <time.h>
+
+grub_err_t
+grub_get_datetime (struct grub_datetime *datetime)
+{
+ struct tm *mytm;
+ time_t mytime;
+
+ mytime = time (&mytime);
+ mytm = gmtime (&mytime);
+
+ datetime->year = mytm->tm_year + 1900;
+ datetime->month = mytm->tm_mon + 1;
+ datetime->day = mytm->tm_mday;
+ datetime->hour = mytm->tm_hour;
+ datetime->minute = mytm->tm_min;
+ datetime->second = mytm->tm_sec;
+
+ return GRUB_ERR_NONE;
+}
+
+grub_err_t
+grub_set_datetime (struct grub_datetime *datetime __attribute__ ((unused)))
+{
+ return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
+ "no clock setting routine available");
+}
diff --git a/grub-core/kern/env.c b/grub-core/kern/env.c
new file mode 100644
index 0000000..c408626
--- /dev/null
+++ b/grub-core/kern/env.c
@@ -0,0 +1,238 @@
+/* env.c - Environment variables */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/env.h>
+#include <grub/env_private.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+
+/* The initial context. */
+static struct grub_env_context initial_context;
+
+/* The current context. */
+struct grub_env_context *grub_current_context = &initial_context;
+
+/* Return the hash representation of the string S. */
+static unsigned int
+grub_env_hashval (const char *s)
+{
+ unsigned int i = 0;
+
+ /* XXX: This can be done much more efficiently. */
+ while (*s)
+ i += 5 * *(s++);
+
+ return i % HASHSZ;
+}
+
+static struct grub_env_var *
+grub_env_find (const char *name)
+{
+ struct grub_env_var *var;
+ int idx = grub_env_hashval (name);
+
+ /* Look for the variable in the current context. */
+ for (var = grub_current_context->vars[idx]; var; var = var->next)
+ if (grub_strcmp (var->name, name) == 0)
+ return var;
+
+ return 0;
+}
+
+static void
+grub_env_insert (struct grub_env_context *context,
+ struct grub_env_var *var)
+{
+ int idx = grub_env_hashval (var->name);
+
+ /* Insert the variable into the hashtable. */
+ var->prevp = &context->vars[idx];
+ var->next = context->vars[idx];
+ if (var->next)
+ var->next->prevp = &(var->next);
+ context->vars[idx] = var;
+}
+
+static void
+grub_env_remove (struct grub_env_var *var)
+{
+ /* Remove the entry from the variable table. */
+ *var->prevp = var->next;
+ if (var->next)
+ var->next->prevp = var->prevp;
+}
+
+grub_err_t
+grub_env_set (const char *name, const char *val)
+{
+ struct grub_env_var *var;
+
+ /* If the variable does already exist, just update the variable. */
+ var = grub_env_find (name);
+ if (var)
+ {
+ char *old = var->value;
+
+ if (var->write_hook)
+ var->value = var->write_hook (var, val);
+ else
+ var->value = grub_strdup (val);
+
+ if (! var->value)
+ {
+ var->value = old;
+ return grub_errno;
+ }
+
+ grub_free (old);
+ return GRUB_ERR_NONE;
+ }
+
+ /* The variable does not exist, so create a new one. */
+ var = grub_zalloc (sizeof (*var));
+ if (! var)
+ return grub_errno;
+
+ var->name = grub_strdup (name);
+ if (! var->name)
+ goto fail;
+
+ var->value = grub_strdup (val);
+ if (! var->value)
+ goto fail;
+
+ grub_env_insert (grub_current_context, var);
+
+ return GRUB_ERR_NONE;
+
+ fail:
+ grub_free (var->name);
+ grub_free (var->value);
+ grub_free (var);
+
+ return grub_errno;
+}
+
+const char *
+grub_env_get (const char *name)
+{
+ struct grub_env_var *var;
+
+ var = grub_env_find (name);
+ if (! var)
+ return 0;
+
+ if (var->read_hook)
+ return var->read_hook (var, var->value);
+
+ return var->value;
+}
+
+void
+grub_env_unset (const char *name)
+{
+ struct grub_env_var *var;
+
+ var = grub_env_find (name);
+ if (! var)
+ return;
+
+ if (var->read_hook || var->write_hook)
+ {
+ grub_env_set (name, "");
+ return;
+ }
+
+ grub_env_remove (var);
+
+ grub_free (var->name);
+ grub_free (var->value);
+ grub_free (var);
+}
+
+struct grub_env_var *
+grub_env_update_get_sorted (void)
+{
+ struct grub_env_var *sorted_list = 0;
+ int i;
+
+ /* Add variables associated with this context into a sorted list. */
+ for (i = 0; i < HASHSZ; i++)
+ {
+ struct grub_env_var *var;
+
+ for (var = grub_current_context->vars[i]; var; var = var->next)
+ {
+ struct grub_env_var *p, **q;
+
+ for (q = &sorted_list, p = *q; p; q = &((*q)->sorted_next), p = *q)
+ {
+ if (grub_strcmp (p->name, var->name) > 0)
+ break;
+ }
+
+ var->sorted_next = *q;
+ *q = var;
+ }
+ }
+
+ return sorted_list;
+}
+
+grub_err_t
+grub_register_variable_hook (const char *name,
+ grub_env_read_hook_t read_hook,
+ grub_env_write_hook_t write_hook)
+{
+ struct grub_env_var *var = grub_env_find (name);
+
+ if (! var)
+ {
+ if (grub_env_set (name, "") != GRUB_ERR_NONE)
+ return grub_errno;
+
+ var = grub_env_find (name);
+ /* XXX Insert an assertion? */
+ }
+
+ var->read_hook = read_hook;
+ var->write_hook = write_hook;
+
+ return GRUB_ERR_NONE;
+}
+
+grub_err_t
+grub_env_export (const char *name)
+{
+ struct grub_env_var *var;
+
+ var = grub_env_find (name);
+ if (! var)
+ {
+ grub_err_t err;
+
+ err = grub_env_set (name, "");
+ if (err)
+ return err;
+ var = grub_env_find (name);
+ }
+ var->global = 1;
+
+ return GRUB_ERR_NONE;
+}
diff --git a/grub-core/kern/err.c b/grub-core/kern/err.c
new file mode 100644
index 0000000..53c734d
--- /dev/null
+++ b/grub-core/kern/err.c
@@ -0,0 +1,122 @@
+/* err.c - error handling routines */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2002,2005,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/err.h>
+#include <grub/misc.h>
+#include <stdarg.h>
+#include <grub/i18n.h>
+
+#define GRUB_ERROR_STACK_SIZE 10
+
+grub_err_t grub_errno;
+char grub_errmsg[GRUB_MAX_ERRMSG];
+int grub_err_printed_errors;
+
+static struct grub_error_saved grub_error_stack_items[GRUB_ERROR_STACK_SIZE];
+
+static int grub_error_stack_pos;
+static int grub_error_stack_assert;
+
+grub_err_t
+grub_error (grub_err_t n, const char *fmt, ...)
+{
+ va_list ap;
+
+ grub_errno = n;
+
+ va_start (ap, fmt);
+ grub_vsnprintf (grub_errmsg, sizeof (grub_errmsg), _(fmt), ap);
+ va_end (ap);
+
+ return n;
+}
+
+void
+grub_error_push (void)
+{
+ /* Only add items to stack, if there is enough room. */
+ if (grub_error_stack_pos < GRUB_ERROR_STACK_SIZE)
+ {
+ /* Copy active error message to stack. */
+ grub_error_stack_items[grub_error_stack_pos].grub_errno = grub_errno;
+ grub_memcpy (grub_error_stack_items[grub_error_stack_pos].errmsg,
+ grub_errmsg,
+ sizeof (grub_errmsg));
+
+ /* Advance to next error stack position. */
+ grub_error_stack_pos++;
+ }
+ else
+ {
+ /* There is no room for new error message. Discard new error message
+ and mark error stack assertion flag. */
+ grub_error_stack_assert = 1;
+ }
+
+ /* Allow further operation of other components by resetting
+ active errno to GRUB_ERR_NONE. */
+ grub_errno = GRUB_ERR_NONE;
+}
+
+int
+grub_error_pop (void)
+{
+ if (grub_error_stack_pos > 0)
+ {
+ /* Pop error message from error stack to current active error. */
+ grub_error_stack_pos--;
+
+ grub_errno = grub_error_stack_items[grub_error_stack_pos].grub_errno;
+ grub_memcpy (grub_errmsg,
+ grub_error_stack_items[grub_error_stack_pos].errmsg,
+ sizeof (grub_errmsg));
+
+ return 1;
+ }
+ else
+ {
+ /* There is no more items on error stack, reset to no error state. */
+ grub_errno = GRUB_ERR_NONE;
+
+ return 0;
+ }
+}
+
+void
+grub_print_error (void)
+{
+ /* Print error messages in reverse order. First print active error message
+ and then empty error stack. */
+ do
+ {
+ if (grub_errno != GRUB_ERR_NONE)
+ {
+ grub_err_printf (_("error: %s.\n"), grub_errmsg);
+ grub_err_printed_errors++;
+ }
+ }
+ while (grub_error_pop ());
+
+ /* If there was an assert while using error stack, report about it. */
+ if (grub_error_stack_assert)
+ {
+ grub_err_printf ("assert: error stack overflow detected!\n");
+ grub_error_stack_assert = 0;
+ }
+}
diff --git a/grub-core/kern/file.c b/grub-core/kern/file.c
new file mode 100644
index 0000000..5845445
--- /dev/null
+++ b/grub-core/kern/file.c
@@ -0,0 +1,218 @@
+/* file.c - file I/O functions */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2002,2006,2007,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/misc.h>
+#include <grub/err.h>
+#include <grub/file.h>
+#include <grub/net.h>
+#include <grub/mm.h>
+#include <grub/fs.h>
+#include <grub/device.h>
+#include <grub/i18n.h>
+
+void (*EXPORT_VAR (grub_grubnet_fini)) (void);
+
+grub_file_filter_t grub_file_filters[GRUB_FILE_FILTER_MAX];
+
+/* Get the device part of the filename NAME. It is enclosed by parentheses. */
+char *
+grub_file_get_device_name (const char *name)
+{
+ if (name[0] == '(')
+ {
+ char *p = grub_strchr (name, ')');
+ char *ret;
+
+ if (! p)
+ {
+ grub_error (GRUB_ERR_BAD_FILENAME, N_("missing `%c' symbol"), ')');
+ return 0;
+ }
+
+ ret = (char *) grub_malloc (p - name);
+ if (! ret)
+ return 0;
+
+ grub_memcpy (ret, name + 1, p - name - 1);
+ ret[p - name - 1] = '\0';
+ return ret;
+ }
+
+ return 0;
+}
+
+grub_file_t
+grub_file_open (const char *name, enum grub_file_type type)
+{
+ grub_device_t device = 0;
+ grub_file_t file = 0, last_file = 0;
+ char *device_name;
+ const char *file_name;
+ grub_file_filter_id_t filter;
+
+ device_name = grub_file_get_device_name (name);
+ if (grub_errno)
+ goto fail;
+
+ /* Get the file part of NAME. */
+ file_name = (name[0] == '(') ? grub_strchr (name, ')') : NULL;
+ if (file_name)
+ file_name++;
+ else
+ file_name = name;
+
+ device = grub_device_open (device_name);
+ grub_free (device_name);
+ if (! device)
+ goto fail;
+
+ file = (grub_file_t) grub_zalloc (sizeof (*file));
+ if (! file)
+ goto fail;
+
+ file->device = device;
+
+ /* In case of relative pathnames and non-Unix systems (like Windows)
+ * name of host files may not start with `/'. Blocklists for host files
+ * are meaningless as well (for a start, host disk does not allow any direct
+ * access - it is just a marker). So skip host disk in this case.
+ */
+ if (device->disk && file_name[0] != '/'
+#if defined(GRUB_UTIL) || defined(GRUB_MACHINE_EMU)
+ && grub_strcmp (device->disk->name, "host")
+#endif
+ )
+ /* This is a block list. */
+ file->fs = &grub_fs_blocklist;
+ else
+ {
+ file->fs = grub_fs_probe (device);
+ if (! file->fs)
+ goto fail;
+ }
+
+ if ((file->fs->fs_open) (file, file_name) != GRUB_ERR_NONE)
+ goto fail;
+
+ file->name = grub_strdup (name);
+ grub_errno = GRUB_ERR_NONE;
+
+ for (filter = 0; file && filter < ARRAY_SIZE (grub_file_filters);
+ filter++)
+ if (grub_file_filters[filter])
+ {
+ last_file = file;
+ file = grub_file_filters[filter] (file, type);
+ if (file && file != last_file)
+ {
+ file->name = grub_strdup (name);
+ grub_errno = GRUB_ERR_NONE;
+ }
+ }
+ if (!file)
+ grub_file_close (last_file);
+
+ return file;
+
+ fail:
+ if (device)
+ grub_device_close (device);
+
+ /* if (net) grub_net_close (net); */
+
+ grub_free (file);
+
+ return 0;
+}
+
+grub_disk_read_hook_t grub_file_progress_hook;
+
+grub_ssize_t
+grub_file_read (grub_file_t file, void *buf, grub_size_t len)
+{
+ grub_ssize_t res;
+ grub_disk_read_hook_t read_hook;
+ void *read_hook_data;
+
+ if (file->offset > file->size)
+ {
+ grub_error (GRUB_ERR_OUT_OF_RANGE,
+ N_("attempt to read past the end of file"));
+ return -1;
+ }
+
+ if (len == 0)
+ return 0;
+
+ if (len > file->size - file->offset)
+ len = file->size - file->offset;
+
+ /* Prevent an overflow. */
+ if ((grub_ssize_t) len < 0)
+ len >>= 1;
+
+ if (len == 0)
+ return 0;
+ read_hook = file->read_hook;
+ read_hook_data = file->read_hook_data;
+ if (!file->read_hook)
+ {
+ file->read_hook = grub_file_progress_hook;
+ file->read_hook_data = file;
+ file->progress_offset = file->offset;
+ }
+ res = (file->fs->fs_read) (file, buf, len);
+ file->read_hook = read_hook;
+ file->read_hook_data = read_hook_data;
+ if (res > 0)
+ file->offset += res;
+
+ return res;
+}
+
+grub_err_t
+grub_file_close (grub_file_t file)
+{
+ if (file->fs->fs_close)
+ (file->fs->fs_close) (file);
+
+ if (file->device)
+ grub_device_close (file->device);
+ grub_free (file->name);
+ grub_free (file);
+ return grub_errno;
+}
+
+grub_off_t
+grub_file_seek (grub_file_t file, grub_off_t offset)
+{
+ grub_off_t old;
+
+ if (offset > file->size)
+ {
+ grub_error (GRUB_ERR_OUT_OF_RANGE,
+ N_("attempt to seek outside of the file"));
+ return -1;
+ }
+
+ old = file->offset;
+ file->offset = offset;
+
+ return old;
+}
diff --git a/grub-core/kern/fs.c b/grub-core/kern/fs.c
new file mode 100644
index 0000000..c698295
--- /dev/null
+++ b/grub-core/kern/fs.c
@@ -0,0 +1,253 @@
+/* fs.c - filesystem manager */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2002,2005,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/disk.h>
+#include <grub/net.h>
+#include <grub/fs.h>
+#include <grub/file.h>
+#include <grub/err.h>
+#include <grub/misc.h>
+#include <grub/types.h>
+#include <grub/mm.h>
+#include <grub/term.h>
+#include <grub/i18n.h>
+
+grub_fs_t grub_fs_list = 0;
+
+grub_fs_autoload_hook_t grub_fs_autoload_hook = 0;
+
+/* Helper for grub_fs_probe. */
+static int
+probe_dummy_iter (const char *filename __attribute__ ((unused)),
+ const struct grub_dirhook_info *info __attribute__ ((unused)),
+ void *data __attribute__ ((unused)))
+{
+ return 1;
+}
+
+grub_fs_t
+grub_fs_probe (grub_device_t device)
+{
+ grub_fs_t p;
+
+ if (device->disk)
+ {
+ /* Make it sure not to have an infinite recursive calls. */
+ static int count = 0;
+
+ for (p = grub_fs_list; p; p = p->next)
+ {
+ grub_dprintf ("fs", "Detecting %s...\n", p->name);
+
+ /* This is evil: newly-created just mounted BtrFS after copying all
+ GRUB files has a very peculiar unrecoverable corruption which
+ will be fixed at sync but we'd rather not do a global sync and
+ syncing just files doesn't seem to help. Relax the check for
+ this time. */
+#ifdef GRUB_UTIL
+ if (grub_strcmp (p->name, "btrfs") == 0)
+ {
+ char *label = 0;
+ p->fs_uuid (device, &label);
+ if (label)
+ grub_free (label);
+ }
+ else
+#endif
+ (p->fs_dir) (device, "/", probe_dummy_iter, NULL);
+ if (grub_errno == GRUB_ERR_NONE)
+ return p;
+
+ grub_error_push ();
+ grub_dprintf ("fs", "%s detection failed.\n", p->name);
+ grub_error_pop ();
+
+ if (grub_errno != GRUB_ERR_BAD_FS
+ && grub_errno != GRUB_ERR_OUT_OF_RANGE)
+ return 0;
+
+ grub_errno = GRUB_ERR_NONE;
+ }
+
+ /* Let's load modules automatically. */
+ if (grub_fs_autoload_hook && count == 0)
+ {
+ count++;
+
+ while (grub_fs_autoload_hook ())
+ {
+ p = grub_fs_list;
+
+ (p->fs_dir) (device, "/", probe_dummy_iter, NULL);
+ if (grub_errno == GRUB_ERR_NONE)
+ {
+ count--;
+ return p;
+ }
+
+ if (grub_errno != GRUB_ERR_BAD_FS
+ && grub_errno != GRUB_ERR_OUT_OF_RANGE)
+ {
+ count--;
+ return 0;
+ }
+
+ grub_errno = GRUB_ERR_NONE;
+ }
+
+ count--;
+ }
+ }
+ else if (device->net && device->net->fs)
+ return device->net->fs;
+
+ grub_error (GRUB_ERR_UNKNOWN_FS, N_("unknown filesystem"));
+ return 0;
+}
+
+
+
+/* Block list support routines. */
+
+struct grub_fs_block
+{
+ grub_disk_addr_t offset;
+ unsigned long length;
+};
+
+static grub_err_t
+grub_fs_blocklist_open (grub_file_t file, const char *name)
+{
+ const char *p = name;
+ unsigned num = 0;
+ unsigned i;
+ grub_disk_t disk = file->device->disk;
+ struct grub_fs_block *blocks;
+ grub_size_t max_sectors;
+
+ /* First, count the number of blocks. */
+ do
+ {
+ num++;
+ p = grub_strchr (p, ',');
+ if (p)
+ p++;
+ }
+ while (p);
+
+ /* Allocate a block list. */
+ blocks = grub_calloc (num + 1, sizeof (struct grub_fs_block));
+ if (! blocks)
+ return 0;
+
+ file->size = 0;
+ max_sectors = grub_disk_from_native_sector (disk, disk->total_sectors);
+ p = (char *) name;
+ for (i = 0; i < num; i++)
+ {
+ if (*p != '+')
+ {
+ blocks[i].offset = grub_strtoull (p, &p, 0);
+ if (grub_errno != GRUB_ERR_NONE || *p != '+')
+ {
+ grub_error (GRUB_ERR_BAD_FILENAME,
+ N_("invalid file name `%s'"), name);
+ goto fail;
+ }
+ }
+
+ p++;
+ blocks[i].length = grub_strtoul (p, &p, 0);
+ if (grub_errno != GRUB_ERR_NONE
+ || blocks[i].length == 0
+ || (*p && *p != ',' && ! grub_isspace (*p)))
+ {
+ grub_error (GRUB_ERR_BAD_FILENAME,
+ N_("invalid file name `%s'"), name);
+ goto fail;
+ }
+
+ if (max_sectors < blocks[i].offset + blocks[i].length)
+ {
+ grub_error (GRUB_ERR_BAD_FILENAME, "beyond the total sectors");
+ goto fail;
+ }
+
+ file->size += (blocks[i].length << GRUB_DISK_SECTOR_BITS);
+ p++;
+ }
+
+ file->data = blocks;
+
+ return GRUB_ERR_NONE;
+
+ fail:
+ grub_free (blocks);
+ return grub_errno;
+}
+
+static grub_ssize_t
+grub_fs_blocklist_read (grub_file_t file, char *buf, grub_size_t len)
+{
+ struct grub_fs_block *p;
+ grub_disk_addr_t sector;
+ grub_off_t offset;
+ grub_ssize_t ret = 0;
+
+ if (len > file->size - file->offset)
+ len = file->size - file->offset;
+
+ sector = (file->offset >> GRUB_DISK_SECTOR_BITS);
+ offset = (file->offset & (GRUB_DISK_SECTOR_SIZE - 1));
+ for (p = file->data; p->length && len > 0; p++)
+ {
+ if (sector < p->length)
+ {
+ grub_size_t size;
+
+ size = len;
+ if (((size + offset + GRUB_DISK_SECTOR_SIZE - 1)
+ >> GRUB_DISK_SECTOR_BITS) > p->length - sector)
+ size = ((p->length - sector) << GRUB_DISK_SECTOR_BITS) - offset;
+
+ if (grub_disk_read (file->device->disk, p->offset + sector, offset,
+ size, buf) != GRUB_ERR_NONE)
+ return -1;
+
+ ret += size;
+ len -= size;
+ sector -= ((size + offset) >> GRUB_DISK_SECTOR_BITS);
+ offset = ((size + offset) & (GRUB_DISK_SECTOR_SIZE - 1));
+ }
+ else
+ sector -= p->length;
+ }
+
+ return ret;
+}
+
+struct grub_fs grub_fs_blocklist =
+ {
+ .name = "blocklist",
+ .fs_dir = 0,
+ .fs_open = grub_fs_blocklist_open,
+ .fs_read = grub_fs_blocklist_read,
+ .fs_close = 0,
+ .next = 0
+ };
diff --git a/grub-core/kern/generic/millisleep.c b/grub-core/kern/generic/millisleep.c
new file mode 100644
index 0000000..9d5971f
--- /dev/null
+++ b/grub-core/kern/generic/millisleep.c
@@ -0,0 +1,39 @@
+/* millisleep.c - generic millisleep function.
+ * The generic implementation of these functions can be used for architectures
+ * or platforms that do not have a more specialized implementation. */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 1999,2000,2001,2002,2003,2004,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/misc.h>
+#include <grub/time.h>
+
+void
+grub_millisleep (grub_uint32_t ms)
+{
+ grub_uint64_t start;
+
+ start = grub_get_time_ms ();
+
+ /* Instead of setting an end time and looping while the current time is
+ less than that, comparing the elapsed sleep time with the desired sleep
+ time handles the (unlikely!) case that the timer would wrap around
+ during the sleep. */
+
+ while (grub_get_time_ms () - start < ms)
+ grub_cpu_idle ();
+}
diff --git a/grub-core/kern/generic/rtc_get_time_ms.c b/grub-core/kern/generic/rtc_get_time_ms.c
new file mode 100644
index 0000000..3e39c8f
--- /dev/null
+++ b/grub-core/kern/generic/rtc_get_time_ms.c
@@ -0,0 +1,38 @@
+/* rtc_get_time_ms.c - get_time_ms implementation using platform RTC.
+ * The generic implementation of these functions can be used for architectures
+ * or platforms that do not have a more specialized implementation. */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/time.h>
+#include <grub/misc.h>
+#include <grub/machine/time.h>
+
+/* Calculate the time in milliseconds since the epoch based on the RTC. */
+grub_uint64_t
+grub_rtc_get_time_ms (void)
+{
+ /* By dimensional analysis:
+
+ 1000 ms N rtc ticks 1 s
+ ------- * ----------- * ----------- = 1000*N/T ms
+ 1 s 1 T rtc ticks
+ */
+ grub_uint64_t ticks_ms_per_sec = ((grub_uint64_t) 1000) * grub_get_rtc ();
+ return grub_divmod64 (ticks_ms_per_sec, GRUB_TICKS_PER_SECOND ? : 1000, 0);
+}
diff --git a/grub-core/kern/i386/coreboot/cbtable.c b/grub-core/kern/i386/coreboot/cbtable.c
new file mode 100644
index 0000000..34a2b59
--- /dev/null
+++ b/grub-core/kern/i386/coreboot/cbtable.c
@@ -0,0 +1,44 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2007,2008,2013 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/i386/coreboot/memory.h>
+#include <grub/coreboot/lbio.h>
+#include <grub/types.h>
+#include <grub/err.h>
+#include <grub/misc.h>
+#include <grub/dl.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+grub_linuxbios_table_header_t
+grub_linuxbios_get_tables (void)
+{
+ grub_linuxbios_table_header_t table_header;
+ /* Assuming table_header is aligned to its size (8 bytes). */
+ for (table_header = (grub_linuxbios_table_header_t) 0x500;
+ table_header < (grub_linuxbios_table_header_t) 0x1000; table_header++)
+ if (grub_linuxbios_check_signature (table_header))
+ return table_header;
+
+ for (table_header = (grub_linuxbios_table_header_t) 0xf0000;
+ table_header < (grub_linuxbios_table_header_t) 0x100000; table_header++)
+ if (grub_linuxbios_check_signature (table_header))
+ return table_header;
+
+ return 0;
+}
diff --git a/grub-core/kern/i386/coreboot/init.c b/grub-core/kern/i386/coreboot/init.c
new file mode 100644
index 0000000..3314f02
--- /dev/null
+++ b/grub-core/kern/i386/coreboot/init.c
@@ -0,0 +1,143 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2013 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/kernel.h>
+#include <grub/mm.h>
+#include <grub/machine/time.h>
+#include <grub/machine/memory.h>
+#include <grub/machine/console.h>
+#include <grub/offsets.h>
+#include <grub/types.h>
+#include <grub/err.h>
+#include <grub/dl.h>
+#include <grub/misc.h>
+#include <grub/loader.h>
+#include <grub/env.h>
+#include <grub/cache.h>
+#include <grub/time.h>
+#include <grub/symbol.h>
+#include <grub/cpu/io.h>
+#include <grub/cpu/floppy.h>
+#include <grub/cpu/tsc.h>
+#include <grub/video.h>
+
+extern grub_uint8_t _start[];
+extern grub_uint8_t _end[];
+extern grub_uint8_t _edata[];
+
+void __attribute__ ((noreturn))
+grub_exit (void)
+{
+ /* We can't use grub_fatal() in this function. This would create an infinite
+ loop, since grub_fatal() calls grub_abort() which in turn calls grub_exit(). */
+ while (1)
+ grub_cpu_idle ();
+}
+
+grub_addr_t grub_modbase = GRUB_KERNEL_I386_COREBOOT_MODULES_ADDR;
+static grub_uint64_t modend;
+static int have_memory = 0;
+
+/* Helper for grub_machine_init. */
+static int
+heap_init (grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type,
+ void *data __attribute__ ((unused)))
+{
+ grub_uint64_t begin = addr, end = addr + size;
+
+#if GRUB_CPU_SIZEOF_VOID_P == 4
+ /* Restrict ourselves to 32-bit memory space. */
+ if (begin > GRUB_ULONG_MAX)
+ return 0;
+ if (end > GRUB_ULONG_MAX)
+ end = GRUB_ULONG_MAX;
+#endif
+
+ if (type != GRUB_MEMORY_AVAILABLE)
+ return 0;
+
+ /* Avoid the lower memory. */
+ if (begin < GRUB_MEMORY_MACHINE_LOWER_SIZE)
+ begin = GRUB_MEMORY_MACHINE_LOWER_SIZE;
+
+ if (modend && begin < modend)
+ begin = modend;
+
+ if (end <= begin)
+ return 0;
+
+ grub_mm_init_region ((void *) (grub_addr_t) begin, (grub_size_t) (end - begin));
+
+ have_memory = 1;
+
+ return 0;
+}
+
+#ifndef GRUB_MACHINE_MULTIBOOT
+
+void
+grub_machine_init (void)
+{
+ modend = grub_modules_get_end ();
+
+ grub_video_coreboot_fb_early_init ();
+
+ grub_vga_text_init ();
+
+ grub_machine_mmap_iterate (heap_init, NULL);
+ if (!have_memory)
+ grub_fatal ("No memory found");
+
+ grub_video_coreboot_fb_late_init ();
+
+ grub_font_init ();
+ grub_gfxterm_init ();
+
+ grub_tsc_init ();
+}
+
+#else
+
+void
+grub_machine_init (void)
+{
+ modend = grub_modules_get_end ();
+
+ grub_vga_text_init ();
+
+ grub_machine_mmap_init ();
+ grub_machine_mmap_iterate (heap_init, NULL);
+
+ grub_tsc_init ();
+}
+
+#endif
+
+void
+grub_machine_get_bootlocation (char **device __attribute__ ((unused)),
+ char **path __attribute__ ((unused)))
+{
+}
+
+void
+grub_machine_fini (int flags)
+{
+ if (flags & GRUB_LOADER_FLAG_NORETURN)
+ grub_vga_text_fini ();
+ grub_stop_floppy ();
+}
diff --git a/grub-core/kern/i386/coreboot/startup.S b/grub-core/kern/i386/coreboot/startup.S
new file mode 100644
index 0000000..df6adba
--- /dev/null
+++ b/grub-core/kern/i386/coreboot/startup.S
@@ -0,0 +1,62 @@
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/symbol.h>
+#include <grub/machine/memory.h>
+#include <grub/offsets.h>
+#include <multiboot.h>
+#include <multiboot2.h>
+
+/*
+ * 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.
+ */
+
+ .file "startup.S"
+ .text
+ .globl start, _start
+start:
+_start:
+#ifdef GRUB_MACHINE_MULTIBOOT
+ cmpl $MULTIBOOT_BOOTLOADER_MAGIC, %eax
+ jne 0f
+ movl %ebx, EXT_C(startup_multiboot_info)
+0:
+#endif
+
+ /* initialize the stack */
+ movl $GRUB_MEMORY_MACHINE_PROT_STACK, %esp
+
+ /* jump to the main body of C code */
+ jmp EXT_C(grub_main)
+
+/*
+ * Support for booting GRUB from a Multiboot boot loader (e.g. GRUB itself).
+ */
+ .p2align 2 /* force 4-byte alignment */
+multiboot_header:
+ /* magic */
+ .long 0x1BADB002
+ /* flags */
+ .long MULTIBOOT_MEMORY_INFO
+ /* checksum */
+ .long -0x1BADB002 - MULTIBOOT_MEMORY_INFO
+
diff --git a/grub-core/kern/i386/dl.c b/grub-core/kern/i386/dl.c
new file mode 100644
index 0000000..1346da5
--- /dev/null
+++ b/grub-core/kern/i386/dl.c
@@ -0,0 +1,81 @@
+/* dl-386.c - arch-dependent part of loadable module support */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2002,2005,2007,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/dl.h>
+#include <grub/elf.h>
+#include <grub/misc.h>
+#include <grub/err.h>
+#include <grub/i18n.h>
+
+/* Check if EHDR is a valid ELF header. */
+grub_err_t
+grub_arch_dl_check_header (void *ehdr)
+{
+ Elf_Ehdr *e = ehdr;
+
+ /* Check the magic numbers. */
+ if (e->e_ident[EI_CLASS] != ELFCLASS32
+ || e->e_ident[EI_DATA] != ELFDATA2LSB
+ || e->e_machine != EM_386)
+ return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF magic"));
+
+ return GRUB_ERR_NONE;
+}
+
+/* Relocate symbols. */
+grub_err_t
+grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
+ Elf_Shdr *s, grub_dl_segment_t seg)
+{
+ Elf_Rel *rel, *max;
+
+ for (rel = (Elf_Rel *) ((char *) ehdr + s->sh_offset),
+ max = (Elf_Rel *) ((char *) rel + s->sh_size);
+ rel < max;
+ rel = (Elf_Rel *) ((char *) rel + s->sh_entsize))
+ {
+ Elf_Word *addr;
+ Elf_Sym *sym;
+
+ if (seg->size < rel->r_offset)
+ return grub_error (GRUB_ERR_BAD_MODULE,
+ "reloc offset is out of the segment");
+
+ addr = (Elf_Word *) ((char *) seg->addr + rel->r_offset);
+ sym = (Elf_Sym *) ((char *) mod->symtab
+ + mod->symsize * ELF_R_SYM (rel->r_info));
+
+ switch (ELF_R_TYPE (rel->r_info))
+ {
+ case R_386_32:
+ *addr += sym->st_value;
+ break;
+
+ case R_386_PC32:
+ *addr += (sym->st_value - (grub_addr_t) addr);
+ break;
+ default:
+ return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
+ N_("relocation 0x%x is not implemented yet"),
+ ELF_R_TYPE (rel->r_info));
+ }
+ }
+
+ return GRUB_ERR_NONE;
+}
diff --git a/grub-core/kern/i386/efi/init.c b/grub-core/kern/i386/efi/init.c
new file mode 100644
index 0000000..46476e2
--- /dev/null
+++ b/grub-core/kern/i386/efi/init.c
@@ -0,0 +1,48 @@
+/* init.c - initialize an x86-based EFI system */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/types.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/err.h>
+#include <grub/dl.h>
+#include <grub/cache.h>
+#include <grub/kernel.h>
+#include <grub/efi/efi.h>
+#include <grub/i386/tsc.h>
+#include <grub/loader.h>
+
+void
+grub_machine_init (void)
+{
+ grub_efi_init ();
+ grub_tsc_init ();
+}
+
+void
+grub_machine_fini (int flags)
+{
+ if (!(flags & GRUB_LOADER_FLAG_NORETURN))
+ return;
+
+ grub_efi_fini ();
+
+ if (!(flags & GRUB_LOADER_FLAG_EFI_KEEP_ALLOCATED_MEMORY))
+ grub_efi_memory_fini ();
+}
diff --git a/grub-core/kern/i386/efi/startup.S b/grub-core/kern/i386/efi/startup.S
new file mode 100644
index 0000000..fc5ea3d
--- /dev/null
+++ b/grub-core/kern/i386/efi/startup.S
@@ -0,0 +1,36 @@
+/* startup.S - bootstrap GRUB itself */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2006,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <grub/symbol.h>
+
+ .file "startup.S"
+ .text
+ .globl start, _start
+start:
+_start:
+ /*
+ * EFI_SYSTEM_TABLE * and EFI_HANDLE are passed on the stack.
+ */
+ movl 4(%esp), %eax
+ movl %eax, EXT_C(grub_efi_image_handle)
+ movl 8(%esp), %eax
+ movl %eax, EXT_C(grub_efi_system_table)
+ call EXT_C(grub_main)
+ ret
diff --git a/grub-core/kern/i386/efi/tsc.c b/grub-core/kern/i386/efi/tsc.c
new file mode 100644
index 0000000..4b93ba8
--- /dev/null
+++ b/grub-core/kern/i386/efi/tsc.c
@@ -0,0 +1,40 @@
+/* kern/i386/tsc.c - x86 TSC time source implementation
+ * Requires Pentium or better x86 CPU that supports the RDTSC instruction.
+ * This module uses the PIT to calibrate the TSC to
+ * real time.
+ *
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/types.h>
+#include <grub/time.h>
+#include <grub/misc.h>
+#include <grub/i386/tsc.h>
+#include <grub/efi/efi.h>
+#include <grub/efi/api.h>
+
+int
+grub_tsc_calibrate_from_efi (void)
+{
+ grub_uint64_t start_tsc, end_tsc;
+ /* Use EFI Time Service to calibrate TSC */
+ start_tsc = grub_get_tsc ();
+ efi_call_1 (grub_efi_system_table->boot_services->stall, 1000);
+ end_tsc = grub_get_tsc ();
+ grub_tsc_rate = grub_divmod64 ((1ULL << 32), end_tsc - start_tsc, 0);
+ return 1;
+}
diff --git a/grub-core/kern/i386/ieee1275/startup.S b/grub-core/kern/i386/ieee1275/startup.S
new file mode 100644
index 0000000..62cf348
--- /dev/null
+++ b/grub-core/kern/i386/ieee1275/startup.S
@@ -0,0 +1,40 @@
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/symbol.h>
+#include <grub/offsets.h>
+#include <multiboot.h>
+#include <multiboot2.h>
+
+/*
+ * 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.
+ */
+
+ .file "startup.S"
+ .text
+ .globl start, _start
+
+start:
+_start:
+ movl %eax, EXT_C(grub_ieee1275_entry_fn)
+ jmp EXT_C(grub_main)
+
diff --git a/grub-core/kern/i386/int.S b/grub-core/kern/i386/int.S
new file mode 100644
index 0000000..862a542
--- /dev/null
+++ b/grub-core/kern/i386/int.S
@@ -0,0 +1,134 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2010,2011 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 <http://www.gnu.org/licenses/>.
+ */
+
+FUNCTION(grub_bios_interrupt)
+ pushf
+ cli
+ popf
+ pushl %ebp
+ pushl %ecx
+ pushl %eax
+ pushl %ebx
+ pushl %esi
+ pushl %edi
+ pushl %edx
+
+ movb %al, intno
+ movl (%edx), %eax
+ movl %eax, LOCAL(bios_register_eax)
+ movw 4(%edx), %ax
+ movw %ax, LOCAL(bios_register_es)
+ movw 6(%edx), %ax
+ movw %ax, LOCAL(bios_register_ds)
+ movw 8(%edx), %ax
+ movw %ax, LOCAL(bios_register_flags)
+
+ movl 12(%edx), %ebx
+ movl 16(%edx), %ecx
+ movl 20(%edx), %edi
+ movl 24(%edx), %esi
+ movl 28(%edx), %edx
+
+ /*
+ Via C3 CPUs have cache coherence problems, so we need to call
+ wbinvd at these 2 points. As wbinvd slows down boot, don't do
+ it on non-VIA. 9090 is nop nop. */
+VARIABLE(grub_bios_via_workaround1)
+ .byte 0x90, 0x90
+
+ PROT_TO_REAL
+ .code16
+ pushf
+ cli
+
+ mov %ds, %ax
+ push %ax
+
+ /* movw imm16, %ax*/
+ .byte 0xb8
+LOCAL(bios_register_es):
+ .short 0
+ movw %ax, %es
+ /* movw imm16, %ax*/
+ .byte 0xb8
+LOCAL(bios_register_ds):
+ .short 0
+ movw %ax, %ds
+
+ /* movw imm16, %ax*/
+ .byte 0xb8
+LOCAL(bios_register_flags):
+ .short 0
+ push %ax
+ popf
+
+ /* movl imm32, %eax*/
+ .byte 0x66, 0xb8
+LOCAL(bios_register_eax):
+ .long 0
+
+ /* int imm8. */
+ .byte 0xcd
+intno:
+ .byte 0
+
+ movl %eax, %cs:LOCAL(bios_register_eax)
+ movw %ds, %ax
+ movw %ax, %cs:LOCAL(bios_register_ds)
+ pop %ax
+ mov %ax, %ds
+ pushf
+ pop %ax
+ movw %ax, LOCAL(bios_register_flags)
+ mov %es, %ax
+ movw %ax, LOCAL(bios_register_es)
+
+ popf
+
+VARIABLE(grub_bios_via_workaround2)
+ .byte 0x90, 0x90
+
+ REAL_TO_PROT
+ .code32
+
+ popl %eax
+
+ movl %ebx, 12(%eax)
+ movl %ecx, 16(%eax)
+ movl %edi, 20(%eax)
+ movl %esi, 24(%eax)
+ movl %edx, 28(%eax)
+
+ movl %eax, %edx
+
+ movl LOCAL(bios_register_eax), %eax
+ movl %eax, (%edx)
+ movw LOCAL(bios_register_es), %ax
+ movw %ax, 4(%edx)
+ movw LOCAL(bios_register_ds), %ax
+ movw %ax, 6(%edx)
+ movw LOCAL(bios_register_flags), %ax
+ movw %ax, 8(%edx)
+
+ popl %edi
+ popl %esi
+ popl %ebx
+ popl %eax
+ popl %ecx
+ popl %ebp
+ ret
diff --git a/grub-core/kern/i386/multiboot_mmap.c b/grub-core/kern/i386/multiboot_mmap.c
new file mode 100644
index 0000000..e8f4f34
--- /dev/null
+++ b/grub-core/kern/i386/multiboot_mmap.c
@@ -0,0 +1,73 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2002,2003,2004,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/machine/memory.h>
+#include <grub/types.h>
+#include <grub/multiboot.h>
+#include <grub/err.h>
+#include <grub/misc.h>
+
+/* A pointer to the MBI in its initial location. */
+struct multiboot_info *startup_multiboot_info;
+
+/* The MBI has to be copied to our BSS so that it won't be
+ overwritten. This is its final location. */
+static struct multiboot_info kern_multiboot_info;
+
+/* Unfortunately we can't use heap at this point. But 32 looks like a sane
+ limit (used by memtest86). */
+static grub_uint8_t mmap_entries[sizeof (struct multiboot_mmap_entry) * 32];
+
+void
+grub_machine_mmap_init (void)
+{
+ if (! startup_multiboot_info)
+ grub_fatal ("Unable to find Multiboot Information (is CONFIG_MULTIBOOT disabled in coreboot?)");
+
+ /* Move MBI to a safe place. */
+ grub_memmove (&kern_multiboot_info, startup_multiboot_info, sizeof (struct multiboot_info));
+
+ if ((kern_multiboot_info.flags & MULTIBOOT_INFO_MEM_MAP) == 0)
+ grub_fatal ("Missing Multiboot memory information");
+
+ /* Move the memory map to a safe place. */
+ if (kern_multiboot_info.mmap_length > sizeof (mmap_entries))
+ {
+ grub_printf ("WARNING: Memory map size exceeds limit (0x%x > 0x%x); it will be truncated\n",
+ kern_multiboot_info.mmap_length, sizeof (mmap_entries));
+ kern_multiboot_info.mmap_length = sizeof (mmap_entries);
+ }
+ grub_memmove (mmap_entries, (void *) kern_multiboot_info.mmap_addr, kern_multiboot_info.mmap_length);
+ kern_multiboot_info.mmap_addr = (grub_uint32_t) mmap_entries;
+}
+
+grub_err_t
+grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
+{
+ struct multiboot_mmap_entry *entry = (void *) kern_multiboot_info.mmap_addr;
+
+ while ((unsigned long) entry < kern_multiboot_info.mmap_addr + kern_multiboot_info.mmap_length)
+ {
+ if (hook (entry->addr, entry->len, entry->type, hook_data))
+ break;
+
+ entry = (void *) ((grub_addr_t) entry + entry->size + sizeof (entry->size));
+ }
+
+ return 0;
+}
diff --git a/grub-core/kern/i386/pc/acpi.c b/grub-core/kern/i386/pc/acpi.c
new file mode 100644
index 0000000..297f5d0
--- /dev/null
+++ b/grub-core/kern/i386/pc/acpi.c
@@ -0,0 +1,83 @@
+/* acpi.c - get acpi tables. */
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/acpi.h>
+#include <grub/misc.h>
+
+struct grub_acpi_rsdp_v10 *
+grub_machine_acpi_get_rsdpv1 (void)
+{
+ int ebda_len;
+ grub_uint8_t *ebda, *ptr;
+
+ grub_dprintf ("acpi", "Looking for RSDP. Scanning EBDA\n");
+ ebda = (grub_uint8_t *) ((* ((grub_uint16_t *) 0x40e)) << 4);
+ ebda_len = * (grub_uint16_t *) ebda;
+ if (! ebda_len) /* FIXME do we really need this check? */
+ goto scan_bios;
+ for (ptr = ebda; ptr < ebda + 0x400; ptr += 16)
+ if (grub_memcmp (ptr, GRUB_RSDP_SIGNATURE, GRUB_RSDP_SIGNATURE_SIZE) == 0
+ && grub_byte_checksum (ptr, sizeof (struct grub_acpi_rsdp_v10)) == 0
+ && ((struct grub_acpi_rsdp_v10 *) ptr)->revision == 0)
+ return (struct grub_acpi_rsdp_v10 *) ptr;
+
+scan_bios:
+ grub_dprintf ("acpi", "Looking for RSDP. Scanning BIOS\n");
+ for (ptr = (grub_uint8_t *) 0xe0000; ptr < (grub_uint8_t *) 0x100000;
+ ptr += 16)
+ if (grub_memcmp (ptr, GRUB_RSDP_SIGNATURE, GRUB_RSDP_SIGNATURE_SIZE) == 0
+ && grub_byte_checksum (ptr, sizeof (struct grub_acpi_rsdp_v10)) == 0
+ && ((struct grub_acpi_rsdp_v10 *) ptr)->revision == 0)
+ return (struct grub_acpi_rsdp_v10 *) ptr;
+ return 0;
+}
+
+struct grub_acpi_rsdp_v20 *
+grub_machine_acpi_get_rsdpv2 (void)
+{
+ int ebda_len;
+ grub_uint8_t *ebda, *ptr;
+
+ grub_dprintf ("acpi", "Looking for RSDP. Scanning EBDA\n");
+ ebda = (grub_uint8_t *) ((* ((grub_uint16_t *) 0x40e)) << 4);
+ ebda_len = * (grub_uint16_t *) ebda;
+ if (! ebda_len) /* FIXME do we really need this check? */
+ goto scan_bios;
+ for (ptr = ebda; ptr < ebda + 0x400; ptr += 16)
+ if (grub_memcmp (ptr, GRUB_RSDP_SIGNATURE, GRUB_RSDP_SIGNATURE_SIZE) == 0
+ && grub_byte_checksum (ptr, sizeof (struct grub_acpi_rsdp_v10)) == 0
+ && ((struct grub_acpi_rsdp_v10 *) ptr)->revision != 0
+ && ((struct grub_acpi_rsdp_v20 *) ptr)->length < 1024
+ && grub_byte_checksum (ptr, ((struct grub_acpi_rsdp_v20 *) ptr)->length)
+ == 0)
+ return (struct grub_acpi_rsdp_v20 *) ptr;
+
+scan_bios:
+ grub_dprintf ("acpi", "Looking for RSDP. Scanning BIOS\n");
+ for (ptr = (grub_uint8_t *) 0xe0000; ptr < (grub_uint8_t *) 0x100000;
+ ptr += 16)
+ if (grub_memcmp (ptr, GRUB_RSDP_SIGNATURE, GRUB_RSDP_SIGNATURE_SIZE) == 0
+ && grub_byte_checksum (ptr, sizeof (struct grub_acpi_rsdp_v10)) == 0
+ && ((struct grub_acpi_rsdp_v10 *) ptr)->revision != 0
+ && ((struct grub_acpi_rsdp_v20 *) ptr)->length < 1024
+ && grub_byte_checksum (ptr, ((struct grub_acpi_rsdp_v20 *) ptr)->length)
+ == 0)
+ return (struct grub_acpi_rsdp_v20 *) ptr;
+ return 0;
+}
diff --git a/grub-core/kern/i386/pc/init.c b/grub-core/kern/i386/pc/init.c
new file mode 100644
index 0000000..27bc68b
--- /dev/null
+++ b/grub-core/kern/i386/pc/init.c
@@ -0,0 +1,271 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2002,2003,2004,2005,2006,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/kernel.h>
+#include <grub/mm.h>
+#include <grub/machine/boot.h>
+#include <grub/i386/floppy.h>
+#include <grub/machine/memory.h>
+#include <grub/machine/console.h>
+#include <grub/machine/kernel.h>
+#include <grub/machine/int.h>
+#include <grub/types.h>
+#include <grub/err.h>
+#include <grub/dl.h>
+#include <grub/misc.h>
+#include <grub/loader.h>
+#include <grub/env.h>
+#include <grub/cache.h>
+#include <grub/time.h>
+#include <grub/cpu/cpuid.h>
+#include <grub/cpu/tsc.h>
+#include <grub/machine/time.h>
+
+struct mem_region
+{
+ grub_addr_t addr;
+ grub_size_t size;
+};
+
+#define MAX_REGIONS 32
+
+static struct mem_region mem_regions[MAX_REGIONS];
+static int num_regions;
+
+void (*grub_pc_net_config) (char **device, char **path);
+
+/*
+ * return the real time in ticks, of which there are about
+ * 18-20 per second
+ */
+grub_uint64_t
+grub_rtc_get_time_ms (void)
+{
+ struct grub_bios_int_registers regs;
+
+ regs.eax = 0;
+ regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
+ grub_bios_interrupt (0x1a, &regs);
+
+ return ((regs.ecx << 16) | (regs.edx & 0xffff)) * 55ULL;
+}
+
+void
+grub_machine_get_bootlocation (char **device, char **path)
+{
+ char *ptr;
+ grub_uint8_t boot_drive, dos_part, bsd_part;
+
+ boot_drive = (grub_boot_device >> 24);
+ dos_part = (grub_boot_device >> 16);
+ bsd_part = (grub_boot_device >> 8);
+
+ /* No hardcoded root partition - make it from the boot drive and the
+ partition number encoded at the install time. */
+ if (boot_drive == GRUB_BOOT_MACHINE_PXE_DL)
+ {
+ if (grub_pc_net_config)
+ grub_pc_net_config (device, path);
+ return;
+ }
+
+ /* XXX: This should be enough. */
+#define DEV_SIZE 100
+ *device = grub_malloc (DEV_SIZE);
+ ptr = *device;
+ grub_snprintf (*device, DEV_SIZE,
+ "%cd%u", (boot_drive & 0x80) ? 'h' : 'f',
+ boot_drive & 0x7f);
+ ptr += grub_strlen (ptr);
+
+ if (dos_part != 0xff)
+ grub_snprintf (ptr, DEV_SIZE - (ptr - *device),
+ ",%u", dos_part + 1);
+ ptr += grub_strlen (ptr);
+
+ if (bsd_part != 0xff)
+ grub_snprintf (ptr, DEV_SIZE - (ptr - *device), ",%u",
+ bsd_part + 1);
+ ptr += grub_strlen (ptr);
+ *ptr = 0;
+}
+
+/* Add a memory region. */
+static void
+add_mem_region (grub_addr_t addr, grub_size_t size)
+{
+ if (num_regions == MAX_REGIONS)
+ /* Ignore. */
+ return;
+
+ mem_regions[num_regions].addr = addr;
+ mem_regions[num_regions].size = size;
+ num_regions++;
+}
+
+/* Compact memory regions. */
+static void
+compact_mem_regions (void)
+{
+ int i, j;
+
+ /* Sort them. */
+ for (i = 0; i < num_regions - 1; i++)
+ for (j = i + 1; j < num_regions; j++)
+ if (mem_regions[i].addr > mem_regions[j].addr)
+ {
+ struct mem_region tmp = mem_regions[i];
+ mem_regions[i] = mem_regions[j];
+ mem_regions[j] = tmp;
+ }
+
+ /* Merge overlaps. */
+ for (i = 0; i < num_regions - 1; i++)
+ if (mem_regions[i].addr + mem_regions[i].size >= mem_regions[i + 1].addr)
+ {
+ j = i + 1;
+
+ if (mem_regions[i].addr + mem_regions[i].size
+ < mem_regions[j].addr + mem_regions[j].size)
+ mem_regions[i].size = (mem_regions[j].addr + mem_regions[j].size
+ - mem_regions[i].addr);
+
+ grub_memmove (mem_regions + j, mem_regions + j + 1,
+ (num_regions - j - 1) * sizeof (struct mem_region));
+ i--;
+ num_regions--;
+ }
+}
+
+grub_addr_t grub_modbase;
+extern grub_uint8_t _start[], _edata[];
+
+/* Helper for grub_machine_init. */
+static int
+mmap_iterate_hook (grub_uint64_t addr, grub_uint64_t size,
+ grub_memory_type_t type,
+ void *data __attribute__ ((unused)))
+{
+ /* Avoid the lower memory. */
+ if (addr < GRUB_MEMORY_MACHINE_UPPER_START)
+ {
+ if (size <= GRUB_MEMORY_MACHINE_UPPER_START - addr)
+ return 0;
+
+ size -= GRUB_MEMORY_MACHINE_UPPER_START - addr;
+ addr = GRUB_MEMORY_MACHINE_UPPER_START;
+ }
+
+ /* Ignore >4GB. */
+ if (addr <= 0xFFFFFFFF && type == GRUB_MEMORY_AVAILABLE)
+ {
+ grub_size_t len;
+
+ len = (grub_size_t) ((addr + size > 0xFFFFFFFF)
+ ? 0xFFFFFFFF - addr
+ : size);
+ add_mem_region (addr, len);
+ }
+
+ return 0;
+}
+
+extern grub_uint16_t grub_bios_via_workaround1, grub_bios_via_workaround2;
+
+/* Via needs additional wbinvd. */
+static void
+grub_via_workaround_init (void)
+{
+ grub_uint32_t manufacturer[3], max_cpuid;
+ if (! grub_cpu_is_cpuid_supported ())
+ return;
+
+ grub_cpuid (0, max_cpuid, manufacturer[0], manufacturer[2], manufacturer[1]);
+
+ if (grub_memcmp (manufacturer, "CentaurHauls", 12) != 0)
+ return;
+
+ grub_bios_via_workaround1 = 0x090f;
+ grub_bios_via_workaround2 = 0x090f;
+ asm volatile ("wbinvd");
+}
+
+void
+grub_machine_init (void)
+{
+ int i;
+#if 0
+ int grub_lower_mem;
+#endif
+ grub_addr_t modend;
+
+ /* This has to happen before any BIOS calls. */
+ grub_via_workaround_init ();
+
+ grub_modbase = GRUB_MEMORY_MACHINE_DECOMPRESSION_ADDR + (_edata - _start);
+
+ /* Initialize the console as early as possible. */
+ grub_console_init ();
+
+ /* This sanity check is useless since top of GRUB_MEMORY_MACHINE_RESERVED_END
+ is used for stack and if it's unavailable we wouldn't have gotten so far.
+ */
+#if 0
+ grub_lower_mem = grub_get_conv_memsize () << 10;
+
+ /* Sanity check. */
+ if (grub_lower_mem < GRUB_MEMORY_MACHINE_RESERVED_END)
+ grub_fatal ("too small memory");
+#endif
+
+/* FIXME: This prevents loader/i386/linux.c from using low memory. When our
+ heap implements support for requesting a chunk in low memory, this should
+ no longer be a problem. */
+#if 0
+ /* Add the lower memory into free memory. */
+ if (grub_lower_mem >= GRUB_MEMORY_MACHINE_RESERVED_END)
+ add_mem_region (GRUB_MEMORY_MACHINE_RESERVED_END,
+ grub_lower_mem - GRUB_MEMORY_MACHINE_RESERVED_END);
+#endif
+
+ grub_machine_mmap_iterate (mmap_iterate_hook, NULL);
+
+ compact_mem_regions ();
+
+ modend = grub_modules_get_end ();
+ for (i = 0; i < num_regions; i++)
+ {
+ grub_addr_t beg = mem_regions[i].addr;
+ grub_addr_t fin = mem_regions[i].addr + mem_regions[i].size;
+ if (modend && beg < modend)
+ beg = modend;
+ if (beg >= fin)
+ continue;
+ grub_mm_init_region ((void *) beg, fin - beg);
+ }
+
+ grub_tsc_init ();
+}
+
+void
+grub_machine_fini (int flags)
+{
+ if (flags & GRUB_LOADER_FLAG_NORETURN)
+ grub_console_fini ();
+ grub_stop_floppy ();
+}
diff --git a/grub-core/kern/i386/pc/mmap.c b/grub-core/kern/i386/pc/mmap.c
new file mode 100644
index 0000000..c0c3c35
--- /dev/null
+++ b/grub-core/kern/i386/pc/mmap.c
@@ -0,0 +1,193 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2002,2003,2004,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/machine/memory.h>
+#include <grub/machine/int.h>
+#include <grub/err.h>
+#include <grub/types.h>
+#include <grub/misc.h>
+
+struct grub_machine_mmap_entry
+{
+ grub_uint32_t size;
+ grub_uint64_t addr;
+ grub_uint64_t len;
+#define GRUB_MACHINE_MEMORY_AVAILABLE 1
+#define GRUB_MACHINE_MEMORY_RESERVED 2
+#define GRUB_MACHINE_MEMORY_ACPI 3
+#define GRUB_MACHINE_MEMORY_NVS 4
+#define GRUB_MACHINE_MEMORY_BADRAM 5
+ grub_uint32_t type;
+} GRUB_PACKED;
+
+
+/*
+ *
+ * grub_get_conv_memsize(i) : return the conventional memory size in KB.
+ * BIOS call "INT 12H" to get conventional memory size
+ * The return value in AX.
+ */
+static inline grub_uint16_t
+grub_get_conv_memsize (void)
+{
+ struct grub_bios_int_registers regs;
+
+ regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
+ grub_bios_interrupt (0x12, &regs);
+ return regs.eax & 0xffff;
+}
+
+/*
+ * grub_get_ext_memsize() : return the extended memory size in KB.
+ * BIOS call "INT 15H, AH=88H" to get extended memory size
+ * The return value in AX.
+ *
+ */
+static inline grub_uint16_t
+grub_get_ext_memsize (void)
+{
+ struct grub_bios_int_registers regs;
+
+ regs.eax = 0x8800;
+ regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
+ grub_bios_interrupt (0x15, &regs);
+ return regs.eax & 0xffff;
+}
+
+/* Get a packed EISA memory map. Lower 16 bits are between 1MB and 16MB
+ in 1KB parts, and upper 16 bits are above 16MB in 64KB parts. If error, return zero.
+ BIOS call "INT 15H, AH=E801H" to get EISA memory map,
+ AX = memory between 1M and 16M in 1K parts.
+ BX = memory above 16M in 64K parts.
+*/
+
+static inline grub_uint32_t
+grub_get_eisa_mmap (void)
+{
+ struct grub_bios_int_registers regs;
+
+ regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
+ regs.eax = 0xe801;
+ grub_bios_interrupt (0x15, &regs);
+
+ if ((regs.eax & 0xff00) == 0x8600)
+ return 0;
+
+ return (regs.eax & 0xffff) | (regs.ebx << 16);
+}
+
+/*
+ *
+ * grub_get_mmap_entry(addr, cont) : address and old continuation value (zero to
+ * start), for the Query System Address Map BIOS call.
+ *
+ * Sets the first 4-byte int value of "addr" to the size returned by
+ * the call. If the call fails, sets it to zero.
+ *
+ * Returns: new (non-zero) continuation value, 0 if done.
+ */
+/* Get a memory map entry. Return next continuation value. Zero means
+ the end. */
+static grub_uint32_t
+grub_get_mmap_entry (struct grub_machine_mmap_entry *entry,
+ grub_uint32_t cont)
+{
+ struct grub_bios_int_registers regs;
+
+ regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
+
+ /* place address (+4) in ES:DI */
+ regs.es = ((grub_addr_t) &entry->addr) >> 4;
+ regs.edi = ((grub_addr_t) &entry->addr) & 0xf;
+
+ /* set continuation value */
+ regs.ebx = cont;
+
+ /* set default maximum buffer size */
+ regs.ecx = sizeof (*entry) - sizeof (entry->size);
+
+ /* set EDX to 'SMAP' */
+ regs.edx = 0x534d4150;
+
+ regs.eax = 0xe820;
+ grub_bios_interrupt (0x15, &regs);
+
+ /* write length of buffer (zero if error) into ADDR */
+ if ((regs.flags & GRUB_CPU_INT_FLAGS_CARRY) || regs.eax != 0x534d4150
+ || regs.ecx < 0x14 || regs.ecx > 0x400)
+ entry->size = 0;
+ else
+ entry->size = regs.ecx;
+
+ /* return the continuation value */
+ return regs.ebx;
+}
+
+grub_err_t
+grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
+{
+ grub_uint32_t cont = 0;
+ struct grub_machine_mmap_entry *entry
+ = (struct grub_machine_mmap_entry *) GRUB_MEMORY_MACHINE_SCRATCH_ADDR;
+ int e820_works = 0;
+
+ while (1)
+ {
+ grub_memset (entry, 0, sizeof (*entry));
+
+ cont = grub_get_mmap_entry (entry, cont);
+
+ if (!entry->size)
+ break;
+
+ if (entry->len)
+ e820_works = 1;
+ if (entry->len
+ && hook (entry->addr, entry->len,
+ /* GRUB mmaps have been defined to match with
+ the E820 definition.
+ Therefore, we can just pass type through. */
+ entry->type, hook_data))
+ break;
+
+ if (! cont)
+ break;
+ }
+
+ if (!e820_works)
+ {
+ grub_uint32_t eisa_mmap = grub_get_eisa_mmap ();
+
+ if (hook (0x0, ((grub_uint32_t) grub_get_conv_memsize ()) << 10,
+ GRUB_MEMORY_AVAILABLE, hook_data))
+ return 0;
+
+ if (eisa_mmap)
+ {
+ if (hook (0x100000, (eisa_mmap & 0xFFFF) << 10,
+ GRUB_MEMORY_AVAILABLE, hook_data) == 0)
+ hook (0x1000000, eisa_mmap & ~0xFFFF, GRUB_MEMORY_AVAILABLE,
+ hook_data);
+ }
+ else
+ hook (0x100000, ((grub_uint32_t) grub_get_ext_memsize ()) << 10,
+ GRUB_MEMORY_AVAILABLE, hook_data);
+ }
+
+ return 0;
+}
diff --git a/grub-core/kern/i386/pc/startup.S b/grub-core/kern/i386/pc/startup.S
new file mode 100644
index 0000000..b8a9b33
--- /dev/null
+++ b/grub-core/kern/i386/pc/startup.S
@@ -0,0 +1,217 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 1999,2000,2001,2002,2003,2005,2006,2007,2008,2009,2011 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 <http://www.gnu.org/licenses/>.
+ */
+
+
+/*
+ * 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 is greater than three, the function must return
+ * with "ret $N" where N is ((the number of arguments) - 3) * 4.
+ */
+
+#include <config.h>
+#include <grub/symbol.h>
+#include <multiboot.h>
+#ifdef __APPLE__
+#include <grub/i386/pc/memory.h>
+#endif
+
+ .file "startup.S"
+
+ .text
+
+ .globl start, _start, __start
+start:
+_start:
+__start:
+#ifdef __APPLE__
+LOCAL(start):
+#endif
+ .code32
+
+ movl %ecx, (LOCAL(real_to_prot_addr) - _start) (%esi)
+ movl %edi, (LOCAL(prot_to_real_addr) - _start) (%esi)
+ movl %eax, (EXT_C(grub_realidt) - _start) (%esi)
+
+ /* copy back the decompressed part (except the modules) */
+#ifdef __APPLE__
+ movl $EXT_C(_edata), %ecx
+ subl $LOCAL(start), %ecx
+#else
+ movl $(_edata - _start), %ecx
+#endif
+ movl $(_start), %edi
+ rep
+ movsb
+
+ movl $LOCAL (cont), %esi
+ jmp *%esi
+LOCAL(cont):
+
+#if 0
+ /* copy modules before cleaning out the bss */
+ movl EXT_C(grub_total_module_size), %ecx
+ movl EXT_C(grub_kernel_image_size), %esi
+ addl %ecx, %esi
+ addl $_start, %esi
+ decl %esi
+ movl $END_SYMBOL, %edi
+ addl %ecx, %edi
+ decl %edi
+ std
+ rep
+ movsb
+#endif
+
+#ifdef __APPLE__
+ /* clean out the bss */
+ movl $EXT_C(_edata), %edi
+
+ /* compute the bss length */
+ movl $GRUB_MEMORY_MACHINE_SCRATCH_ADDR, %ecx
+#else
+ /* clean out the bss */
+ movl $BSS_START_SYMBOL, %edi
+
+ /* compute the bss length */
+ movl $END_SYMBOL, %ecx
+#endif
+ subl %edi, %ecx
+
+ /* clean out */
+ xorl %eax, %eax
+ cld
+ rep
+ stosb
+
+ movl %edx, EXT_C(grub_boot_device)
+
+ /*
+ * Call the start of main body of C code.
+ */
+ call EXT_C(grub_main)
+
+LOCAL(real_to_prot_addr):
+ .long 0
+LOCAL(prot_to_real_addr):
+ .long 0
+
+ .macro PROT_TO_REAL
+ movl LOCAL(prot_to_real_addr), %eax
+ call *%eax
+ .endm
+
+ .macro REAL_TO_PROT
+ movl LOCAL(real_to_prot_addr), %eax
+ calll *%eax
+ .endm
+
+/*
+ * grub_exit()
+ *
+ * Exit the system.
+ */
+FUNCTION(grub_exit)
+ PROT_TO_REAL
+ .code16
+ /* Tell the BIOS a boot failure. If this does not work, reboot. */
+ int $0x18
+ /* set 0x472 to 0x0000 for cold boot (0x1234 for warm boot) */
+ xorw %ax, %ax
+ movw $0x0472, %di
+ movw %ax, (%di)
+ ljmp $0xf000, $0xfff0
+ .code32
+
+/*
+ * int grub_pxe_call (int func, void* data, grub_uint32_t pxe_rm_entry);
+ */
+FUNCTION(grub_pxe_call)
+ pushl %ebp
+ movl %esp, %ebp
+ pushl %esi
+ pushl %edi
+ pushl %ebx
+
+ movl %ecx, %ebx
+ movl %eax, %ecx
+ movl %edx, %eax
+ andl $0xF, %eax
+ shrl $4, %edx
+ shll $16, %edx
+ addl %eax, %edx
+
+ PROT_TO_REAL
+ .code16
+
+ pushl %ebx
+ pushl %edx
+ pushw %cx
+ movw %sp, %bx
+ lcall *%ss:6(%bx)
+ cld
+ addw $10, %sp
+ movw %ax, %cx
+
+ REAL_TO_PROT
+ .code32
+
+ movzwl %cx, %eax
+
+ popl %ebx
+ popl %edi
+ popl %esi
+ popl %ebp
+ ret
+
+#include "../int.S"
+
+VARIABLE(grub_realidt)
+ .long 0
+
+#ifdef __APPLE__
+ /* Older versions of objconv assume that there is the same number
+ of text and data sections. Hence this dummy. */
+ .section __TEXT, __zz_dummy
+ .byte 0
+ .globl EXT_C(_edata)
+ .globl EXT_C(grub_boot_device)
+ .zerofill __DATA, __aa_before_bss, EXT_C(_edata), 1, 0
+ .zerofill __DATA, __bss, EXT_C(grub_boot_device), 4, 2
+#else
+ .bss
+VARIABLE(grub_boot_device)
+ .long 0
+#endif
diff --git a/grub-core/kern/i386/qemu/init.c b/grub-core/kern/i386/qemu/init.c
new file mode 100644
index 0000000..271b6fb
--- /dev/null
+++ b/grub-core/kern/i386/qemu/init.c
@@ -0,0 +1,276 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2013 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/kernel.h>
+#include <grub/mm.h>
+#include <grub/machine/time.h>
+#include <grub/machine/memory.h>
+#include <grub/machine/console.h>
+#include <grub/offsets.h>
+#include <grub/types.h>
+#include <grub/err.h>
+#include <grub/dl.h>
+#include <grub/misc.h>
+#include <grub/loader.h>
+#include <grub/env.h>
+#include <grub/cache.h>
+#include <grub/time.h>
+#include <grub/symbol.h>
+#include <grub/cpu/io.h>
+#include <grub/cpu/floppy.h>
+#include <grub/cpu/tsc.h>
+#include <grub/machine/kernel.h>
+#include <grub/pci.h>
+
+extern grub_uint8_t _start[];
+extern grub_uint8_t _end[];
+extern grub_uint8_t _edata[];
+
+void __attribute__ ((noreturn))
+grub_exit (void)
+{
+ /* We can't use grub_fatal() in this function. This would create an infinite
+ loop, since grub_fatal() calls grub_abort() which in turn calls grub_exit(). */
+ while (1)
+ grub_cpu_idle ();
+}
+
+grub_addr_t grub_modbase;
+
+/* Helper for grub_machine_init. */
+static int
+heap_init (grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type,
+ void *data __attribute__ ((unused)))
+{
+ grub_uint64_t begin = addr, end = addr + size;
+
+#if GRUB_CPU_SIZEOF_VOID_P == 4
+ /* Restrict ourselves to 32-bit memory space. */
+ if (begin > GRUB_ULONG_MAX)
+ return 0;
+ if (end > GRUB_ULONG_MAX)
+ end = GRUB_ULONG_MAX;
+#endif
+
+ if (type != GRUB_MEMORY_AVAILABLE)
+ return 0;
+
+ /* Avoid the lower memory. */
+ if (begin < GRUB_MEMORY_MACHINE_LOWER_SIZE)
+ begin = GRUB_MEMORY_MACHINE_LOWER_SIZE;
+
+ if (end <= begin)
+ return 0;
+
+ grub_mm_init_region ((void *) (grub_addr_t) begin, (grub_size_t) (end - begin));
+
+ return 0;
+}
+
+struct resource
+{
+ grub_pci_device_t dev;
+ grub_size_t size;
+ unsigned type:4;
+ unsigned bar:3;
+};
+
+struct iterator_ctx
+{
+ struct resource *resources;
+ grub_size_t nresources;
+};
+
+/* We don't support bridges, so can't have more than 32 devices. */
+#define MAX_DEVICES 32
+
+static int
+find_resources (grub_pci_device_t dev,
+ grub_pci_id_t pciid __attribute__ ((unused)),
+ void *data)
+{
+ struct iterator_ctx *ctx = data;
+ int bar;
+
+ if (ctx->nresources >= MAX_DEVICES * 6)
+ return 1;
+
+ for (bar = 0; bar < 6; bar++)
+ {
+ grub_pci_address_t addr;
+ grub_uint32_t ones, zeros, mask;
+ struct resource *res;
+ addr = grub_pci_make_address (dev, GRUB_PCI_REG_ADDRESS_REG0
+ + 4 * bar);
+ grub_pci_write (addr, 0xffffffff);
+ grub_pci_read (addr);
+ ones = grub_pci_read (addr);
+ grub_pci_write (addr, 0);
+ grub_pci_read (addr);
+ zeros = grub_pci_read (addr);
+ if (ones == zeros)
+ continue;
+ res = &ctx->resources[ctx->nresources++];
+ if ((zeros & GRUB_PCI_ADDR_SPACE_MASK) == GRUB_PCI_ADDR_SPACE_IO)
+ mask = GRUB_PCI_ADDR_SPACE_MASK;
+ else
+ mask = (GRUB_PCI_ADDR_MEM_TYPE_MASK | GRUB_PCI_ADDR_SPACE_MASK | GRUB_PCI_ADDR_MEM_PREFETCH);
+
+ res->type = ones & mask;
+ res->dev = dev;
+ res->bar = bar;
+ res->size = (~((zeros ^ ones)) | mask) + 1;
+ if ((zeros & (GRUB_PCI_ADDR_MEM_TYPE_MASK | GRUB_PCI_ADDR_SPACE_MASK))
+ == (GRUB_PCI_ADDR_SPACE_MEMORY | GRUB_PCI_ADDR_MEM_TYPE_64))
+ bar++;
+ }
+ return 0;
+}
+
+static int
+enable_cards (grub_pci_device_t dev,
+ grub_pci_id_t pciid __attribute__ ((unused)),
+ void *data __attribute__ ((unused)))
+{
+ grub_uint16_t cmd = 0;
+ grub_pci_address_t addr;
+ grub_uint32_t class;
+ int bar;
+
+ for (bar = 0; bar < 6; bar++)
+ {
+ grub_uint32_t val;
+ addr = grub_pci_make_address (dev, GRUB_PCI_REG_ADDRESS_REG0
+ + 4 * bar);
+ val = grub_pci_read (addr);
+ if (!val)
+ continue;
+ if ((val & GRUB_PCI_ADDR_SPACE_MASK) == GRUB_PCI_ADDR_SPACE_IO)
+ cmd |= GRUB_PCI_COMMAND_IO_ENABLED;
+ else
+ cmd |= GRUB_PCI_COMMAND_MEM_ENABLED;
+ }
+
+ class = (grub_pci_read (addr) >> 16) & 0xffff;
+
+ if (class == GRUB_PCI_CLASS_SUBCLASS_VGA)
+ cmd |= GRUB_PCI_COMMAND_IO_ENABLED
+ | GRUB_PCI_COMMAND_MEM_ENABLED;
+
+ if (class == GRUB_PCI_CLASS_SUBCLASS_USB)
+ return 0;
+
+ addr = grub_pci_make_address (dev, GRUB_PCI_REG_COMMAND);
+ grub_pci_write (addr, cmd);
+
+ return 0;
+}
+
+static void
+grub_pci_assign_addresses (void)
+{
+ struct iterator_ctx ctx;
+
+ {
+ struct resource resources[MAX_DEVICES * 6];
+ int done;
+ unsigned i;
+ ctx.nresources = 0;
+ ctx.resources = resources;
+ grub_uint32_t memptr = 0xf0000000;
+ grub_uint16_t ioptr = 0x1000;
+
+ grub_pci_iterate (find_resources, &ctx);
+ /* FIXME: do we need a better sort here? */
+ do
+ {
+ done = 0;
+ for (i = 0; i + 1 < ctx.nresources; i++)
+ if (resources[i].size < resources[i+1].size)
+ {
+ struct resource t;
+ t = resources[i];
+ resources[i] = resources[i+1];
+ resources[i+1] = t;
+ done = 1;
+ }
+ }
+ while (done);
+
+ for (i = 0; i < ctx.nresources; i++)
+ {
+ grub_pci_address_t addr;
+ addr = grub_pci_make_address (resources[i].dev,
+ GRUB_PCI_REG_ADDRESS_REG0
+ + 4 * resources[i].bar);
+ if ((resources[i].type & GRUB_PCI_ADDR_SPACE_MASK)
+ == GRUB_PCI_ADDR_SPACE_IO)
+ {
+ grub_pci_write (addr, ioptr | resources[i].type);
+ ioptr += resources[i].size;
+ }
+ else
+ {
+ grub_pci_write (addr, memptr | resources[i].type);
+ memptr += resources[i].size;
+ if ((resources[i].type & (GRUB_PCI_ADDR_MEM_TYPE_MASK
+ | GRUB_PCI_ADDR_SPACE_MASK))
+ == (GRUB_PCI_ADDR_SPACE_MEMORY | GRUB_PCI_ADDR_MEM_TYPE_64))
+ {
+ addr = grub_pci_make_address (resources[i].dev,
+ GRUB_PCI_REG_ADDRESS_REG0
+ + 4 * resources[i].bar + 4);
+ grub_pci_write (addr, 0);
+ }
+ }
+ }
+ grub_pci_iterate (enable_cards, NULL);
+ }
+}
+
+void
+grub_machine_init (void)
+{
+ grub_modbase = grub_core_entry_addr + (_edata - _start);
+
+ grub_pci_assign_addresses ();
+
+ grub_qemu_init_cirrus ();
+
+ grub_vga_text_init ();
+
+ grub_machine_mmap_init ();
+ grub_machine_mmap_iterate (heap_init, NULL);
+
+
+ grub_tsc_init ();
+}
+
+void
+grub_machine_get_bootlocation (char **device __attribute__ ((unused)),
+ char **path __attribute__ ((unused)))
+{
+}
+
+void
+grub_machine_fini (int flags)
+{
+ if (flags & GRUB_LOADER_FLAG_NORETURN)
+ grub_vga_text_fini ();
+ grub_stop_floppy ();
+}
diff --git a/grub-core/kern/i386/qemu/mmap.c b/grub-core/kern/i386/qemu/mmap.c
new file mode 100644
index 0000000..f449637
--- /dev/null
+++ b/grub-core/kern/i386/qemu/mmap.c
@@ -0,0 +1,107 @@
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/i386/memory.h>
+#include <grub/machine/memory.h>
+#include <grub/machine/boot.h>
+#include <grub/types.h>
+#include <grub/err.h>
+#include <grub/misc.h>
+#include <grub/cmos.h>
+#include <grub/offsets.h>
+
+#define QEMU_CMOS_MEMSIZE_HIGH 0x35
+#define QEMU_CMOS_MEMSIZE_LOW 0x34
+
+#define QEMU_CMOS_MEMSIZE2_HIGH 0x31
+#define QEMU_CMOS_MEMSIZE2_LOW 0x30
+
+#define min(a,b) ((a) > (b) ? (b) : (a))
+
+extern char _start[];
+extern char _end[];
+
+static grub_uint64_t mem_size, above_4g;
+
+void
+grub_machine_mmap_init (void)
+{
+ grub_uint8_t high, low, b, c, d;
+ grub_cmos_read (QEMU_CMOS_MEMSIZE_HIGH, &high);
+ grub_cmos_read (QEMU_CMOS_MEMSIZE_LOW, &low);
+ mem_size = ((grub_uint64_t) high) << 24
+ | ((grub_uint64_t) low) << 16;
+ if (mem_size > 0)
+ {
+ /* Don't ask... */
+ mem_size += (16 * 1024 * 1024);
+ }
+ else
+ {
+ grub_cmos_read (QEMU_CMOS_MEMSIZE2_HIGH, &high);
+ grub_cmos_read (QEMU_CMOS_MEMSIZE2_LOW, &low);
+ mem_size
+ = ((((grub_uint64_t) high) << 18) | (((grub_uint64_t) low) << 10))
+ + 1024 * 1024;
+ }
+
+ grub_cmos_read (0x5b, &b);
+ grub_cmos_read (0x5c, &c);
+ grub_cmos_read (0x5d, &d);
+ above_4g = (((grub_uint64_t) b) << 16)
+ | (((grub_uint64_t) c) << 24)
+ | (((grub_uint64_t) d) << 32);
+}
+
+grub_err_t
+grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
+{
+ if (hook (0x0,
+ (grub_addr_t) _start,
+ GRUB_MEMORY_AVAILABLE, hook_data))
+ return 1;
+
+ if (hook ((grub_addr_t) _end,
+ 0xa0000 - (grub_addr_t) _end,
+ GRUB_MEMORY_AVAILABLE, hook_data))
+ return 1;
+
+ if (hook (GRUB_MEMORY_MACHINE_UPPER,
+ 0x100000 - GRUB_MEMORY_MACHINE_UPPER,
+ GRUB_MEMORY_RESERVED, hook_data))
+ return 1;
+
+ /* Everything else is free. */
+ if (hook (0x100000,
+ min (mem_size, (grub_uint32_t) -GRUB_BOOT_MACHINE_SIZE) - 0x100000,
+ GRUB_MEMORY_AVAILABLE, hook_data))
+ return 1;
+
+ /* Protect boot.img, which contains the gdt. It is mapped at the top of memory
+ (it is also mapped below 0x100000, but we already reserved that area). */
+ if (hook ((grub_uint32_t) -GRUB_BOOT_MACHINE_SIZE,
+ GRUB_BOOT_MACHINE_SIZE,
+ GRUB_MEMORY_RESERVED, hook_data))
+ return 1;
+
+ if (above_4g != 0 && hook (0x100000000ULL, above_4g,
+ GRUB_MEMORY_AVAILABLE, hook_data))
+ return 1;
+
+ return 0;
+}
diff --git a/grub-core/kern/i386/qemu/startup.S b/grub-core/kern/i386/qemu/startup.S
new file mode 100644
index 0000000..0d89858
--- /dev/null
+++ b/grub-core/kern/i386/qemu/startup.S
@@ -0,0 +1,75 @@
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <grub/symbol.h>
+
+#include <grub/machine/memory.h>
+#include <grub/machine/kernel.h>
+
+ .text
+ .code32
+ .globl _start
+_start:
+ jmp codestart
+
+ .org GRUB_KERNEL_I386_QEMU_CORE_ENTRY_ADDR
+VARIABLE(grub_core_entry_addr)
+ .long 0
+
+codestart:
+ /* Relocate to low memory. First we figure out our location.
+ We will derive the rom start address from it. */
+ call 1f
+1: popl %esi
+
+ /* Rom size is a multiple of 64 kiB. With this we get the
+ value of `grub_core_entry_addr' in %esi. */
+ xorw %si, %si
+
+ movl $(_edata - _start), %ecx
+ movl $_start, %edi
+ cld
+ rep
+ movsb
+ ljmp $GRUB_MEMORY_MACHINE_PROT_MODE_CSEG, $1f
+1:
+
+ /* clean out the bss */
+ movl $BSS_START_SYMBOL, %edi
+
+ /* compute the bss length */
+ movl $END_SYMBOL, %ecx
+ subl %edi, %ecx
+
+ /* clean out */
+ xorl %eax, %eax
+ cld
+ rep
+ stosb
+
+ /*
+ * Call the start of main body of C code.
+ */
+ call EXT_C(grub_main)
+
+ /* This should never happen. */
+ cli
+1:
+ hlt
+ jmp 1b
diff --git a/grub-core/kern/i386/realmode.S b/grub-core/kern/i386/realmode.S
new file mode 100644
index 0000000..265cdcb
--- /dev/null
+++ b/grub-core/kern/i386/realmode.S
@@ -0,0 +1,281 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 1999,2000,2001,2002,2003,2005,2006,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/machine/memory.h>
+
+/*
+ * 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.
+ */
+
+protstack:
+ .long GRUB_MEMORY_MACHINE_PROT_STACK
+
+ .macro PROT_TO_REAL
+ call prot_to_real
+ .endm
+
+ .macro REAL_TO_PROT
+ calll real_to_prot
+ .endm
+
+/*
+ * This is the Global Descriptor Table
+ *
+ * An entry, a "Segment Descriptor", looks like this:
+ *
+ * 31 24 19 16 7 0
+ * ------------------------------------------------------------
+ * | | |B| |A| | | |1|0|E|W|A| |
+ * | BASE 31..24 |G|/|L|V| LIMIT |P|DPL| TYPE | BASE 23:16 | 4
+ * | | |D| |L| 19..16| | |1|1|C|R|A| |
+ * ------------------------------------------------------------
+ * | | |
+ * | BASE 15..0 | LIMIT 15..0 | 0
+ * | | |
+ * ------------------------------------------------------------
+ *
+ * Note the ordering of the data items is reversed from the above
+ * description.
+ */
+
+ .p2align 5 /* force 32-byte alignment */
+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, 0
+ .byte 0, 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 5
+/* this is the GDT descriptor */
+gdtdesc:
+ .word 0x27 /* limit */
+ .long gdt /* addr */
+LOCAL(realidt):
+ .word 0x400
+ .long 0
+protidt:
+ .word 0
+ .long 0
+
+/*
+ * These next two routines, "real_to_prot" and "prot_to_real" are structured
+ * in a very specific way. Be very careful when changing them.
+ *
+ * NOTE: Use of either one messes up %eax and %ebp.
+ */
+
+real_to_prot:
+ .code16
+ cli
+
+ /* load the GDT register */
+ xorw %ax, %ax
+ movw %ax, %ds
+#ifdef GRUB_MACHINE_QEMU
+ /*
+ qemu is special: gdtdesc is in ROM.
+ %cs = 0xf000
+ _start + GRUB_BOOT_MACHINE_SIZE = 0x100000
+ So
+ _start + GRUB_BOOT_MACHINE_SIZE - 0x10000 points to the same point
+ as %cs.
+ gdtdesc - (_start + GRUB_BOOT_MACHINE_SIZE - 0x10000)
+ = gdtdesc - _start - GRUB_BOOT_MACHINE_SIZE + 0x10000
+ but the later can be computed by assembly.
+ */
+ lgdtl %cs:(gdtdesc - _start - GRUB_BOOT_MACHINE_SIZE + 0x10000)
+#else
+ lgdtl gdtdesc
+#endif
+
+ /* turn on protected mode */
+ movl %cr0, %eax
+ orl $GRUB_MEMORY_CPU_CR0_PE_ON, %eax
+ movl %eax, %cr0
+
+ /* jump to relocation, flush prefetch queue, and reload %cs */
+ ljmpl $GRUB_MEMORY_MACHINE_PROT_MODE_CSEG, $protcseg
+
+ .code32
+protcseg:
+ /* reload other segment registers */
+ movw $GRUB_MEMORY_MACHINE_PROT_MODE_DSEG, %ax
+ movw %ax, %ds
+ movw %ax, %es
+ movw %ax, %fs
+ movw %ax, %gs
+ movw %ax, %ss
+
+ /* put the return address in a known safe location */
+ movl (%esp), %eax
+ movl %eax, GRUB_MEMORY_MACHINE_REAL_STACK
+
+ /* get protected mode stack */
+ movl protstack, %eax
+ movl %eax, %esp
+ movl %eax, %ebp
+
+ /* get return address onto the right stack */
+ movl GRUB_MEMORY_MACHINE_REAL_STACK, %eax
+ movl %eax, (%esp)
+
+ /* zero %eax */
+ xorl %eax, %eax
+
+ sidt LOCAL(realidt)
+ lidt protidt
+
+ /* return on the old (or initialized) stack! */
+ ret
+ /* prot_to_real assumes that this code is under 64K which is not
+ true for qemu. */
+#ifndef GRUB_MACHINE_QEMU
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 1999,2000,2001,2002,2003,2005,2006,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 <http://www.gnu.org/licenses/>.
+ */
+
+prot_to_real:
+ /* just in case, set GDT */
+ lgdt gdtdesc
+
+ sidt protidt
+ lidt LOCAL(realidt)
+
+ /* save the protected mode stack */
+ movl %esp, %eax
+ movl %eax, protstack
+
+ /* get the return address */
+ movl (%esp), %eax
+ movl %eax, GRUB_MEMORY_MACHINE_REAL_STACK
+
+ /* set up new stack */
+ movl $GRUB_MEMORY_MACHINE_REAL_STACK, %eax
+ movl %eax, %esp
+ movl %eax, %ebp
+
+ /* set up segment limits */
+ movw $GRUB_MEMORY_MACHINE_PSEUDO_REAL_DSEG, %ax
+ movw %ax, %ds
+ movw %ax, %es
+ movw %ax, %fs
+ movw %ax, %gs
+ movw %ax, %ss
+
+ /* this might be an extra step */
+ /* jump to a 16 bit segment */
+ ljmp $GRUB_MEMORY_MACHINE_PSEUDO_REAL_CSEG, $tmpcseg
+
+tmpcseg:
+ .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 */
+ ljmpl $0, $realcseg
+
+realcseg:
+ /* we are in real mode now
+ * set up the real mode segment registers : DS, SS, ES
+ */
+ /* zero %eax */
+ xorl %eax, %eax
+
+ movw %ax, %ds
+ movw %ax, %es
+ movw %ax, %fs
+ movw %ax, %gs
+ movw %ax, %ss
+
+#ifdef GRUB_MACHINE_PCBIOS
+ /* restore interrupts */
+ sti
+#endif
+
+ /* return on new stack! */
+ retl
+#endif
+ .code32
diff --git a/grub-core/kern/i386/tsc.c b/grub-core/kern/i386/tsc.c
new file mode 100644
index 0000000..9293b16
--- /dev/null
+++ b/grub-core/kern/i386/tsc.c
@@ -0,0 +1,78 @@
+/* kern/i386/tsc.c - x86 TSC time source implementation
+ * Requires Pentium or better x86 CPU that supports the RDTSC instruction.
+ * This module calibrates the TSC to real time.
+ *
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/types.h>
+#include <grub/time.h>
+#include <grub/misc.h>
+#include <grub/i386/tsc.h>
+#include <grub/i386/cpuid.h>
+
+/* This defines the value TSC had at the epoch (that is, when we calibrated it). */
+static grub_uint64_t tsc_boot_time;
+
+/* Calibrated TSC rate. (In ms per 2^32 ticks) */
+/* We assume that the tick is less than 1 ms and hence this value fits
+ in 32-bit. */
+grub_uint32_t grub_tsc_rate;
+
+static grub_uint64_t
+grub_tsc_get_time_ms (void)
+{
+ grub_uint64_t a = grub_get_tsc () - tsc_boot_time;
+ grub_uint64_t ah = a >> 32;
+ grub_uint64_t al = a & 0xffffffff;
+
+ return ((al * grub_tsc_rate) >> 32) + ah * grub_tsc_rate;
+}
+
+static int
+calibrate_tsc_hardcode (void)
+{
+ grub_tsc_rate = 5368;/* 800 MHz */
+ return 1;
+}
+
+void
+grub_tsc_init (void)
+{
+ if (!grub_cpu_is_tsc_supported ())
+ {
+#if defined (GRUB_MACHINE_PCBIOS) || defined (GRUB_MACHINE_IEEE1275)
+ grub_install_get_time_ms (grub_rtc_get_time_ms);
+#else
+ grub_fatal ("no TSC found");
+#endif
+ return;
+ }
+
+ tsc_boot_time = grub_get_tsc ();
+
+#if defined (GRUB_MACHINE_XEN) || defined (GRUB_MACHINE_XEN_PVH)
+ (void) (grub_tsc_calibrate_from_xen () || calibrate_tsc_hardcode());
+#elif defined (GRUB_MACHINE_EFI)
+ (void) (grub_tsc_calibrate_from_pmtimer () || grub_tsc_calibrate_from_pit () || grub_tsc_calibrate_from_efi() || calibrate_tsc_hardcode());
+#elif defined (GRUB_MACHINE_COREBOOT)
+ (void) (grub_tsc_calibrate_from_pmtimer () || grub_tsc_calibrate_from_pit () || calibrate_tsc_hardcode());
+#else
+ (void) (grub_tsc_calibrate_from_pit () || calibrate_tsc_hardcode());
+#endif
+ grub_install_get_time_ms (grub_tsc_get_time_ms);
+}
diff --git a/grub-core/kern/i386/tsc_pit.c b/grub-core/kern/i386/tsc_pit.c
new file mode 100644
index 0000000..67990bf
--- /dev/null
+++ b/grub-core/kern/i386/tsc_pit.c
@@ -0,0 +1,84 @@
+/* kern/i386/tsc.c - x86 TSC time source implementation
+ * Requires Pentium or better x86 CPU that supports the RDTSC instruction.
+ * This module uses the PIT to calibrate the TSC to
+ * real time.
+ *
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/types.h>
+#include <grub/time.h>
+#include <grub/misc.h>
+#include <grub/i386/tsc.h>
+#include <grub/i386/pit.h>
+#include <grub/cpu/io.h>
+
+static int
+grub_pit_wait (void)
+{
+ int ret = 0;
+
+ /* Disable timer2 gate and speaker. */
+ grub_outb (grub_inb (GRUB_PIT_SPEAKER_PORT)
+ & ~ (GRUB_PIT_SPK_DATA | GRUB_PIT_SPK_TMR2),
+ GRUB_PIT_SPEAKER_PORT);
+
+ /* Set tics. */
+ grub_outb (GRUB_PIT_CTRL_SELECT_2 | GRUB_PIT_CTRL_READLOAD_WORD,
+ GRUB_PIT_CTRL);
+ /* 0xffff ticks: 55ms. */
+ grub_outb (0xff, GRUB_PIT_COUNTER_2);
+ grub_outb (0xff, GRUB_PIT_COUNTER_2);
+
+ /* Enable timer2 gate, keep speaker disabled. */
+ grub_outb ((grub_inb (GRUB_PIT_SPEAKER_PORT) & ~ GRUB_PIT_SPK_DATA)
+ | GRUB_PIT_SPK_TMR2,
+ GRUB_PIT_SPEAKER_PORT);
+
+ if ((grub_inb (GRUB_PIT_SPEAKER_PORT) & GRUB_PIT_SPK_TMR2_LATCH) == 0x00) {
+ ret = 1;
+ /* Wait. */
+ while ((grub_inb (GRUB_PIT_SPEAKER_PORT) & GRUB_PIT_SPK_TMR2_LATCH) == 0x00);
+ }
+
+ /* Disable timer2 gate and speaker. */
+ grub_outb (grub_inb (GRUB_PIT_SPEAKER_PORT)
+ & ~ (GRUB_PIT_SPK_DATA | GRUB_PIT_SPK_TMR2),
+ GRUB_PIT_SPEAKER_PORT);
+
+ return ret;
+}
+
+/* Calibrate the TSC based on the RTC. */
+int
+grub_tsc_calibrate_from_pit (void)
+{
+ /* First calibrate the TSC rate (relative, not absolute time). */
+ grub_uint64_t start_tsc, end_tsc;
+
+ start_tsc = grub_get_tsc ();
+ if (!grub_pit_wait ())
+ return 0;
+ end_tsc = grub_get_tsc ();
+
+ grub_tsc_rate = 0;
+ if (end_tsc > start_tsc)
+ grub_tsc_rate = grub_divmod64 ((55ULL << 32), end_tsc - start_tsc, 0);
+ if (grub_tsc_rate == 0)
+ return 0;
+ return 1;
+}
diff --git a/grub-core/kern/i386/tsc_pmtimer.c b/grub-core/kern/i386/tsc_pmtimer.c
new file mode 100644
index 0000000..c9c3616
--- /dev/null
+++ b/grub-core/kern/i386/tsc_pmtimer.c
@@ -0,0 +1,88 @@
+/* kern/i386/tsc.c - x86 TSC time source implementation
+ * Requires Pentium or better x86 CPU that supports the RDTSC instruction.
+ * This module uses the PIT to calibrate the TSC to
+ * real time.
+ *
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/types.h>
+#include <grub/time.h>
+#include <grub/misc.h>
+#include <grub/i386/tsc.h>
+#include <grub/i386/pmtimer.h>
+#include <grub/acpi.h>
+#include <grub/cpu/io.h>
+
+grub_uint64_t
+grub_pmtimer_wait_count_tsc (grub_port_t pmtimer,
+ grub_uint16_t num_pm_ticks)
+{
+ grub_uint32_t start;
+ grub_uint32_t last;
+ grub_uint32_t cur, end;
+ grub_uint64_t start_tsc;
+ grub_uint64_t end_tsc;
+ int num_iter = 0;
+
+ start = grub_inl (pmtimer) & 0xffffff;
+ last = start;
+ end = start + num_pm_ticks;
+ start_tsc = grub_get_tsc ();
+ while (1)
+ {
+ cur = grub_inl (pmtimer) & 0xffffff;
+ if (cur < last)
+ cur |= 0x1000000;
+ num_iter++;
+ if (cur >= end)
+ {
+ end_tsc = grub_get_tsc ();
+ return end_tsc - start_tsc;
+ }
+ /* Check for broken PM timer.
+ 50000000 TSCs is between 5 ms (10GHz) and 200 ms (250 MHz)
+ if after this time we still don't have 1 ms on pmtimer, then
+ pmtimer is broken.
+ */
+ if ((num_iter & 0xffffff) == 0 && grub_get_tsc () - start_tsc > 5000000) {
+ return 0;
+ }
+ }
+}
+
+int
+grub_tsc_calibrate_from_pmtimer (void)
+{
+ struct grub_acpi_fadt *fadt;
+ grub_port_t pmtimer;
+ grub_uint64_t tsc_diff;
+
+ fadt = grub_acpi_find_fadt ();
+ if (!fadt)
+ return 0;
+ pmtimer = fadt->pmtimer;
+ if (!pmtimer)
+ return 0;
+
+ /* It's 3.579545 MHz clock. Wait 1 ms. */
+ tsc_diff = grub_pmtimer_wait_count_tsc (pmtimer, 3580);
+ if (tsc_diff == 0)
+ return 0;
+ grub_tsc_rate = grub_divmod64 ((1ULL << 32), tsc_diff, 0);
+ return 1;
+}
diff --git a/grub-core/kern/i386/xen/hypercall.S b/grub-core/kern/i386/xen/hypercall.S
new file mode 100644
index 0000000..09d75c3
--- /dev/null
+++ b/grub-core/kern/i386/xen/hypercall.S
@@ -0,0 +1,43 @@
+/* hypercall.S - wrappers for Xen hypercalls */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2013 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/symbol.h>
+#include <grub/xen.h>
+
+FUNCTION(grub_xen_hypercall)
+ pushl %ebp
+ movl %esp, %ebp
+ pushl %esi
+ pushl %edi
+ pushl %ebx
+
+ /* call number already in %eax. */
+ /* %edx -> %ebx*/
+ /* %ecx -> %ecx*/
+ movl %edx, %ebx
+ movl 8(%ebp), %edx
+ movl 12(%ebp), %esi
+ movl 16(%ebp), %edi
+ movl 20(%ebp), %ebp
+ int $0x82
+ popl %ebx
+ popl %edi
+ popl %esi
+ popl %ebp
+ ret
diff --git a/grub-core/kern/i386/xen/pvh.c b/grub-core/kern/i386/xen/pvh.c
new file mode 100644
index 0000000..91fbca8
--- /dev/null
+++ b/grub-core/kern/i386/xen/pvh.c
@@ -0,0 +1,369 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2018 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/kernel.h>
+#include <grub/misc.h>
+#include <grub/memory.h>
+#include <grub/mm.h>
+#include <grub/i386/cpuid.h>
+#include <grub/i386/io.h>
+#include <grub/xen.h>
+#include <xen/hvm/start_info.h>
+#include <grub/i386/linux.h>
+#include <grub/machine/kernel.h>
+#include <grub/machine/memory.h>
+#include <xen/hvm/params.h>
+#include <xen/memory.h>
+
+#define XEN_MEMORY_MAP_SIZE 128
+
+grub_uint64_t grub_rsdp_addr;
+
+static char hypercall_page[GRUB_XEN_PAGE_SIZE]
+ __attribute__ ((aligned (GRUB_XEN_PAGE_SIZE)));
+
+static grub_uint32_t xen_cpuid_base;
+static struct start_info grub_xen_start_page;
+static struct grub_e820_mmap_entry map[XEN_MEMORY_MAP_SIZE];
+static unsigned int nr_map_entries;
+
+static void
+grub_xen_cons_msg (const char *msg)
+{
+ const char *c;
+
+ for (c = msg; *c; c++)
+ grub_outb (*c, XEN_HVM_DEBUGCONS_IOPORT);
+}
+
+static void
+grub_xen_panic (const char *msg)
+{
+ grub_xen_cons_msg (msg);
+ grub_xen_cons_msg ("System halted!\n");
+
+ asm volatile ("cli");
+
+ while (1)
+ {
+ asm volatile ("hlt");
+ }
+}
+
+static void
+grub_xen_cpuid_base (void)
+{
+ grub_uint32_t base, eax, signature[3];
+
+ for (base = 0x40000000; base < 0x40010000; base += 0x100)
+ {
+ grub_cpuid (base, eax, signature[0], signature[1], signature[2]);
+ if (!grub_memcmp ("XenVMMXenVMM", signature, 12) && (eax - base) >= 2)
+ {
+ xen_cpuid_base = base;
+ return;
+ }
+ }
+
+ grub_xen_panic ("Found no Xen signature!\n");
+}
+
+static void
+grub_xen_setup_hypercall_page (void)
+{
+ grub_uint32_t msr, addr, eax, ebx, ecx, edx;
+
+ /* Get base address of Xen-specific MSRs. */
+ grub_cpuid (xen_cpuid_base + 2, eax, ebx, ecx, edx);
+ msr = ebx;
+ addr = (grub_uint32_t) (&hypercall_page);
+
+ /* Specify hypercall page address for Xen. */
+ asm volatile ("wrmsr" : : "c" (msr), "a" (addr), "d" (0) : "memory");
+}
+
+int
+grub_xen_hypercall (grub_uint32_t callno, grub_uint32_t a0,
+ grub_uint32_t a1, grub_uint32_t a2,
+ grub_uint32_t a3, grub_uint32_t a4,
+ grub_uint32_t a5 __attribute__ ((unused)))
+{
+ grub_uint32_t res;
+
+ asm volatile ("call *%[callno]"
+ : "=a" (res), "+b" (a0), "+c" (a1), "+d" (a2),
+ "+S" (a3), "+D" (a4)
+ : [callno] "a" (&hypercall_page[callno * 32])
+ : "memory");
+ return res;
+}
+
+static grub_uint32_t
+grub_xen_get_param (int idx)
+{
+ struct xen_hvm_param xhv;
+ int r;
+
+ xhv.domid = DOMID_SELF;
+ xhv.index = idx;
+ r = grub_xen_hypercall (__HYPERVISOR_hvm_op, HVMOP_get_param,
+ (grub_uint32_t) (&xhv), 0, 0, 0, 0);
+ if (r < 0)
+ grub_xen_panic ("Could not get parameter from Xen!\n");
+ return xhv.value;
+}
+
+static void *
+grub_xen_add_physmap (unsigned int space, void *addr)
+{
+ struct xen_add_to_physmap xatp;
+
+ xatp.domid = DOMID_SELF;
+ xatp.idx = 0;
+ xatp.space = space;
+ xatp.gpfn = (grub_addr_t) addr >> GRUB_XEN_LOG_PAGE_SIZE;
+ if (grub_xen_hypercall (__HYPERVISOR_memory_op, XENMEM_add_to_physmap,
+ (grub_uint32_t) (&xatp), 0, 0, 0, 0))
+ grub_xen_panic ("Memory_op hypercall failed!\n");
+ return addr;
+}
+
+static void
+grub_xen_sort_mmap (void)
+{
+ grub_uint64_t from, to;
+ unsigned int i;
+ struct grub_e820_mmap_entry tmp;
+
+ /* Align map entries to page boundaries. */
+ for (i = 0; i < nr_map_entries; i++)
+ {
+ from = map[i].addr;
+ to = from + map[i].len;
+ if (map[i].type == GRUB_MEMORY_AVAILABLE)
+ {
+ from = ALIGN_UP (from, GRUB_XEN_PAGE_SIZE);
+ to = ALIGN_DOWN (to, GRUB_XEN_PAGE_SIZE);
+ }
+ else
+ {
+ from = ALIGN_DOWN (from, GRUB_XEN_PAGE_SIZE);
+ to = ALIGN_UP (to, GRUB_XEN_PAGE_SIZE);
+ }
+ map[i].addr = from;
+ map[i].len = to - from;
+ }
+
+ again:
+ /* Sort entries by start address. */
+ for (i = 1; i < nr_map_entries; i++)
+ {
+ if (map[i].addr >= map[i - 1].addr)
+ continue;
+ tmp = map[i];
+ map[i] = map[i - 1];
+ map[i - 1] = tmp;
+ i = 0;
+ }
+
+ /* Detect overlapping areas. */
+ for (i = 1; i < nr_map_entries; i++)
+ {
+ if (map[i].addr >= map[i - 1].addr + map[i - 1].len)
+ continue;
+ tmp = map[i - 1];
+ map[i - 1].len = map[i].addr - map[i - 1].addr;
+ if (map[i].addr + map[i].len >= tmp.addr + tmp.len)
+ continue;
+ if (nr_map_entries < ARRAY_SIZE (map))
+ {
+ map[nr_map_entries].addr = map[i].addr + map[i].len;
+ map[nr_map_entries].len = tmp.addr + tmp.len - map[nr_map_entries].addr;
+ map[nr_map_entries].type = tmp.type;
+ nr_map_entries++;
+ goto again;
+ }
+ }
+
+ /* Merge adjacent entries. */
+ for (i = 1; i < nr_map_entries; i++)
+ {
+ if (map[i].type == map[i - 1].type &&
+ map[i].addr == map[i - 1].addr + map[i - 1].len)
+ {
+ map[i - 1].len += map[i].len;
+ map[i] = map[nr_map_entries - 1];
+ nr_map_entries--;
+ goto again;
+ }
+ }
+}
+
+static void
+grub_xen_get_mmap (void)
+{
+ struct xen_memory_map memmap;
+
+ memmap.nr_entries = ARRAY_SIZE (map);
+ set_xen_guest_handle (memmap.buffer, map);
+ if (grub_xen_hypercall (__HYPERVISOR_memory_op, XENMEM_memory_map,
+ (grub_uint32_t) (&memmap), 0, 0, 0, 0))
+ grub_xen_panic ("Could not get memory map from Xen!\n");
+ nr_map_entries = memmap.nr_entries;
+
+ grub_xen_sort_mmap ();
+}
+
+static void
+grub_xen_set_mmap (void)
+{
+ struct xen_foreign_memory_map memmap;
+
+ memmap.domid = DOMID_SELF;
+ memmap.map.nr_entries = nr_map_entries;
+ set_xen_guest_handle (memmap.map.buffer, map);
+ grub_xen_hypercall (__HYPERVISOR_memory_op, XENMEM_set_memory_map,
+ (grub_uint32_t) (&memmap), 0, 0, 0, 0);
+}
+
+static void
+grub_xen_mm_init_regions (void)
+{
+ grub_uint64_t modend, from, to;
+ unsigned int i;
+
+ modend = grub_modules_get_end ();
+
+ for (i = 0; i < nr_map_entries; i++)
+ {
+ if (map[i].type != GRUB_MEMORY_AVAILABLE)
+ continue;
+ from = map[i].addr;
+ to = from + map[i].len;
+ if (from < modend)
+ from = modend;
+ if (from >= to || from >= (1ULL << 32))
+ continue;
+ if (to > (1ULL << 32))
+ to = 1ULL << 32;
+ grub_mm_init_region ((void *) (grub_addr_t) from, to - from);
+ }
+}
+
+static grub_uint64_t
+grub_xen_find_page (grub_uint64_t start)
+{
+ unsigned int i, j;
+ grub_uint64_t last = start;
+
+ /*
+ * Try to find a e820 map hole below 4G.
+ * Relies on page-aligned entries (addr and len) and input (start).
+ */
+
+ for (i = 0; i < nr_map_entries; i++)
+ {
+ if (last > map[i].addr + map[i].len)
+ continue;
+ if (last < map[i].addr)
+ return last;
+ if ((map[i].addr >> 32) || ((map[i].addr + map[i].len) >> 32))
+ break;
+ last = map[i].addr + map[i].len;
+ }
+ if (i == nr_map_entries)
+ return last;
+
+ /* No hole found, use the highest RAM page below 4G and reserve it. */
+ if (nr_map_entries == ARRAY_SIZE (map))
+ grub_xen_panic ("Memory map size limit reached!\n");
+ for (i = 0, j = 0; i < nr_map_entries; i++)
+ {
+ if (map[i].type != GRUB_MEMORY_AVAILABLE)
+ continue;
+ if (map[i].addr >> 32)
+ break;
+ j = i;
+ if ((map[i].addr + map[i].len) >> 32)
+ break;
+ }
+ if (map[j].type != GRUB_MEMORY_AVAILABLE)
+ grub_xen_panic ("No free memory page found!\n");
+ if ((map[j].addr + map[j].len) >> 32)
+ last = (1ULL << 32) - GRUB_XEN_PAGE_SIZE;
+ else
+ last = map[j].addr + map[j].len - GRUB_XEN_PAGE_SIZE;
+ map[nr_map_entries].addr = last;
+ map[nr_map_entries].len = GRUB_XEN_PAGE_SIZE;
+ map[nr_map_entries].type = GRUB_MEMORY_RESERVED;
+ nr_map_entries++;
+ grub_xen_sort_mmap ();
+
+ return last;
+}
+
+void
+grub_xen_setup_pvh (void)
+{
+ grub_addr_t par;
+
+ grub_xen_cpuid_base ();
+ grub_xen_setup_hypercall_page ();
+ grub_xen_get_mmap ();
+
+ /* Setup Xen data. */
+ grub_xen_start_page_addr = &grub_xen_start_page;
+
+ par = grub_xen_get_param (HVM_PARAM_CONSOLE_PFN);
+ grub_xen_start_page_addr->console.domU.mfn = par;
+ grub_xen_xcons = (void *) (grub_addr_t) (par << GRUB_XEN_LOG_PAGE_SIZE);
+ par = grub_xen_get_param (HVM_PARAM_CONSOLE_EVTCHN);
+ grub_xen_start_page_addr->console.domU.evtchn = par;
+
+ par = grub_xen_get_param (HVM_PARAM_STORE_PFN);
+ grub_xen_start_page_addr->store_mfn = par;
+ grub_xen_xenstore = (void *) (grub_addr_t) (par << GRUB_XEN_LOG_PAGE_SIZE);
+ par = grub_xen_get_param (HVM_PARAM_STORE_EVTCHN);
+ grub_xen_start_page_addr->store_evtchn = par;
+
+ par = grub_xen_find_page (0);
+ grub_xen_grant_table = grub_xen_add_physmap (XENMAPSPACE_grant_table,
+ (void *) par);
+ par = grub_xen_find_page (par + GRUB_XEN_PAGE_SIZE);
+ grub_xen_shared_info = grub_xen_add_physmap (XENMAPSPACE_shared_info,
+ (void *) par);
+ grub_xen_set_mmap ();
+
+ grub_xen_mm_init_regions ();
+
+ grub_rsdp_addr = pvh_start_info->rsdp_paddr;
+}
+
+grub_err_t
+grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
+{
+ unsigned int i;
+
+ for (i = 0; i < nr_map_entries; i++)
+ {
+ if (map[i].len && hook (map[i].addr, map[i].len, map[i].type, hook_data))
+ break;
+ }
+
+ return GRUB_ERR_NONE;
+}
diff --git a/grub-core/kern/i386/xen/startup.S b/grub-core/kern/i386/xen/startup.S
new file mode 100644
index 0000000..fbe8300
--- /dev/null
+++ b/grub-core/kern/i386/xen/startup.S
@@ -0,0 +1,38 @@
+/* startup.S - bootstrap GRUB itself */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2013 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <grub/symbol.h>
+
+ .file "startup.S"
+ .text
+ .globl start, _start
+ .code32
+
+start:
+_start:
+ leal LOCAL(stack_end), %esp
+ movl %esi, EXT_C(grub_xen_start_page_addr)
+
+ call EXT_C(grub_main)
+ /* Doesn't return. */
+
+ .bss
+ .space (1 << 22)
+LOCAL(stack_end):
diff --git a/grub-core/kern/i386/xen/startup_pvh.S b/grub-core/kern/i386/xen/startup_pvh.S
new file mode 100644
index 0000000..363c318
--- /dev/null
+++ b/grub-core/kern/i386/xen/startup_pvh.S
@@ -0,0 +1,81 @@
+/* startup.S - bootstrap GRUB itself */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2018 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <grub/symbol.h>
+#include <grub/machine/memory.h>
+
+ .file "startup_pvh.S"
+ .text
+ .globl start, _start
+ .code32
+
+start:
+_start:
+ cld
+ lgdt gdtdesc
+ ljmp $GRUB_MEMORY_MACHINE_PROT_MODE_CSEG, $1f
+1:
+ movl $GRUB_MEMORY_MACHINE_PROT_MODE_DSEG, %eax
+ mov %eax, %ds
+ mov %eax, %es
+ mov %eax, %fs
+ mov %eax, %gs
+ mov %eax, %ss
+ leal LOCAL(stack_end), %esp
+
+ /* Save address of start info structure. */
+ mov %ebx, pvh_start_info
+ call EXT_C(grub_main)
+ /* Doesn't return. */
+
+ .p2align 3
+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
+
+ .p2align 3
+/* this is the GDT descriptor */
+gdtdesc:
+ .word 0x17 /* limit */
+ .long gdt /* addr */
+
+ .p2align 2
+/* Saved pointer to start info structure. */
+ .globl pvh_start_info
+pvh_start_info:
+ .long 0
+
+ .bss
+ .space GRUB_MEMORY_MACHINE_PROT_STACK_SIZE
+LOCAL(stack_end):
diff --git a/grub-core/kern/i386/xen/tsc.c b/grub-core/kern/i386/xen/tsc.c
new file mode 100644
index 0000000..8792b30
--- /dev/null
+++ b/grub-core/kern/i386/xen/tsc.c
@@ -0,0 +1,40 @@
+/* kern/i386/tsc.c - x86 TSC time source implementation
+ * Requires Pentium or better x86 CPU that supports the RDTSC instruction.
+ * This module uses the PIT to calibrate the TSC to
+ * real time.
+ *
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/types.h>
+#include <grub/time.h>
+#include <grub/misc.h>
+#include <grub/i386/tsc.h>
+#include <grub/xen.h>
+
+int
+grub_tsc_calibrate_from_xen (void)
+{
+ grub_uint64_t t;
+ t = grub_xen_shared_info->vcpu_info[0].time.tsc_to_system_mul;
+ if (grub_xen_shared_info->vcpu_info[0].time.tsc_shift > 0)
+ t <<= grub_xen_shared_info->vcpu_info[0].time.tsc_shift;
+ else
+ t >>= -grub_xen_shared_info->vcpu_info[0].time.tsc_shift;
+ grub_tsc_rate = grub_divmod64 (t, 1000000, 0);
+ return 1;
+}
diff --git a/grub-core/kern/ia64/cache.c b/grub-core/kern/ia64/cache.c
new file mode 100644
index 0000000..13efd30
--- /dev/null
+++ b/grub-core/kern/ia64/cache.c
@@ -0,0 +1,35 @@
+/* init.c - initialize an ia64-based EFI system */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/types.h>
+#include <grub/cache.h>
+
+void
+grub_arch_sync_caches (void *address, grub_size_t len)
+{
+ /* Cache line length is at least 32. */
+ len += (grub_uint64_t)address & 0x1f;
+ grub_uint64_t a = (grub_uint64_t)address & ~0x1f;
+
+ /* Flush data. */
+ for (len = (len + 31) & ~0x1f; len > 0; len -= 0x20, a += 0x20)
+ asm volatile ("fc.i %0" : : "r" (a));
+ /* Sync and serialize. Maybe extra. */
+ asm volatile (";; sync.i;; srlz.i;;");
+}
diff --git a/grub-core/kern/ia64/dl.c b/grub-core/kern/ia64/dl.c
new file mode 100644
index 0000000..db59300
--- /dev/null
+++ b/grub-core/kern/ia64/dl.c
@@ -0,0 +1,150 @@
+/* dl.c - arch-dependent part of loadable module support */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2002,2004,2005,2007,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/dl.h>
+#include <grub/elf.h>
+#include <grub/misc.h>
+#include <grub/err.h>
+#include <grub/mm.h>
+#include <grub/i18n.h>
+#include <grub/ia64/reloc.h>
+
+#define MASK19 ((1 << 19) - 1)
+#define MASK20 ((1 << 20) - 1)
+
+/* Check if EHDR is a valid ELF header. */
+grub_err_t
+grub_arch_dl_check_header (void *ehdr)
+{
+ Elf_Ehdr *e = ehdr;
+
+ /* Check the magic numbers. */
+ if (e->e_ident[EI_CLASS] != ELFCLASS64
+ || e->e_ident[EI_DATA] != ELFDATA2LSB
+ || e->e_machine != EM_IA_64)
+ return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF magic"));
+
+ return GRUB_ERR_NONE;
+}
+
+#pragma GCC diagnostic ignored "-Wcast-align"
+
+/* Relocate symbols. */
+grub_err_t
+grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
+ Elf_Shdr *s, grub_dl_segment_t seg)
+{
+ Elf_Rela *rel, *max;
+
+ for (rel = (Elf_Rela *) ((char *) ehdr + s->sh_offset),
+ max = (Elf_Rela *) ((char *) rel + s->sh_size);
+ rel < max;
+ rel = (Elf_Rela *) ((char *) rel + s->sh_entsize))
+ {
+ grub_addr_t addr;
+ Elf_Sym *sym;
+ grub_uint64_t value;
+
+ if (seg->size < (rel->r_offset & ~3))
+ return grub_error (GRUB_ERR_BAD_MODULE,
+ "reloc offset is out of the segment");
+
+ addr = (grub_addr_t) seg->addr + rel->r_offset;
+ sym = (Elf_Sym *) ((char *) mod->symtab
+ + mod->symsize * ELF_R_SYM (rel->r_info));
+
+ /* On the PPC the value does not have an explicit
+ addend, add it. */
+ value = sym->st_value + rel->r_addend;
+
+ switch (ELF_R_TYPE (rel->r_info))
+ {
+ case R_IA64_PCREL21B:
+ {
+ grub_int64_t noff;
+ if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
+ {
+ struct grub_ia64_trampoline *tr = mod->trampptr;
+ grub_ia64_make_trampoline (tr, value);
+ noff = ((char *) tr - (char *) (addr & ~3)) >> 4;
+ mod->trampptr = tr + 1;
+ }
+ else
+ noff = ((char *) value - (char *) (addr & ~3)) >> 4;
+
+ if ((noff & ~MASK19) && ((-noff) & ~MASK19))
+ return grub_error (GRUB_ERR_BAD_MODULE,
+ "jump offset too big (%lx)", noff);
+ grub_ia64_add_value_to_slot_20b (addr, noff);
+ }
+ break;
+ case R_IA64_SEGREL64LSB:
+ *(grub_uint64_t *) addr += value - (grub_addr_t) seg->addr;
+ break;
+ case R_IA64_FPTR64LSB:
+ case R_IA64_DIR64LSB:
+ *(grub_uint64_t *) addr += value;
+ break;
+ case R_IA64_PCREL64LSB:
+ *(grub_uint64_t *) addr += value - addr;
+ break;
+ case R_IA64_GPREL64I:
+ grub_ia64_set_immu64 (addr, value - (grub_addr_t) mod->base);
+ break;
+ case R_IA64_GPREL22:
+ if ((value - (grub_addr_t) mod->base) & ~MASK20)
+ return grub_error (GRUB_ERR_BAD_MODULE,
+ "gprel offset too big (%lx)",
+ value - (grub_addr_t) mod->base);
+ grub_ia64_add_value_to_slot_21 (addr, value - (grub_addr_t) mod->base);
+ break;
+
+ case R_IA64_LTOFF22X:
+ case R_IA64_LTOFF22:
+ if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
+ value = *(grub_uint64_t *) sym->st_value + rel->r_addend;
+ /* Fallthrough. */
+ case R_IA64_LTOFF_FPTR22:
+ {
+ grub_uint64_t *gpptr = mod->gotptr;
+ *gpptr = value;
+ if (((grub_addr_t) gpptr - (grub_addr_t) mod->base) & ~MASK20)
+ return grub_error (GRUB_ERR_BAD_MODULE,
+ "gprel offset too big (%lx)",
+ (grub_addr_t) gpptr - (grub_addr_t) mod->base);
+ grub_ia64_add_value_to_slot_21 (addr, (grub_addr_t) gpptr - (grub_addr_t) mod->base);
+ mod->gotptr = gpptr + 1;
+ break;
+ }
+ /* We treat LTOFF22X as LTOFF22, so we can ignore LDXMOV. */
+ case R_IA64_LDXMOV:
+ break;
+ default:
+ {
+ char rel_info[17]; /* log16(2^64) = 16, plus NUL. */
+
+ grub_snprintf (rel_info, sizeof (rel_info) - 1, "%" PRIxGRUB_UINT64_T,
+ ELF_R_TYPE (rel->r_info));
+ return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
+ N_("relocation 0x%s is not implemented yet"), rel_info);
+ }
+ }
+ }
+ return GRUB_ERR_NONE;
+}
diff --git a/grub-core/kern/ia64/dl_helper.c b/grub-core/kern/ia64/dl_helper.c
new file mode 100644
index 0000000..c601595
--- /dev/null
+++ b/grub-core/kern/ia64/dl_helper.c
@@ -0,0 +1,241 @@
+/* dl.c - arch-dependent part of loadable module support */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2002,2004,2005,2007,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/dl.h>
+#include <grub/elf.h>
+#include <grub/misc.h>
+#include <grub/err.h>
+#include <grub/mm.h>
+#include <grub/i18n.h>
+#include <grub/ia64/reloc.h>
+
+#pragma GCC diagnostic ignored "-Wcast-align"
+
+#define MASK20 ((1 << 20) - 1)
+#define MASK3 (~(grub_addr_t) 3)
+
+void
+grub_ia64_set_immu64 (grub_addr_t addr, grub_uint64_t val)
+{
+ /* Copied from binutils. */
+ grub_uint64_t *ptr = ((grub_uint64_t *) (addr & MASK3));
+ grub_uint64_t t0, t1;
+
+ t0 = grub_le_to_cpu64 (ptr[0]);
+ t1 = grub_le_to_cpu64 (ptr[1]);
+
+ /* tmpl/s: bits 0.. 5 in t0
+ slot 0: bits 5..45 in t0
+ slot 1: bits 46..63 in t0, bits 0..22 in t1
+ slot 2: bits 23..63 in t1 */
+
+ /* First, clear the bits that form the 64 bit constant. */
+ t0 &= ~(0x3ffffLL << 46);
+ t1 &= ~(0x7fffffLL
+ | (( (0x07fLL << 13) | (0x1ffLL << 27)
+ | (0x01fLL << 22) | (0x001LL << 21)
+ | (0x001LL << 36)) << 23));
+
+ t0 |= ((val >> 22) & 0x03ffffLL) << 46; /* 18 lsbs of imm41 */
+ t1 |= ((val >> 40) & 0x7fffffLL) << 0; /* 23 msbs of imm41 */
+ t1 |= ( (((val >> 0) & 0x07f) << 13) /* imm7b */
+ | (((val >> 7) & 0x1ff) << 27) /* imm9d */
+ | (((val >> 16) & 0x01f) << 22) /* imm5c */
+ | (((val >> 21) & 0x001) << 21) /* ic */
+ | (((val >> 63) & 0x001) << 36)) << 23; /* i */
+
+ ptr[0] = t0;
+ ptr[1] = t1;
+}
+
+void
+grub_ia64_add_value_to_slot_20b (grub_addr_t addr, grub_uint32_t value)
+{
+ grub_uint32_t val;
+ switch (addr & 3)
+ {
+ case 0:
+ val = grub_le_to_cpu32 (grub_get_unaligned32 (((grub_uint8_t *)
+ (addr & MASK3) + 2)));
+ val = (((((val & MASK20) + value) & MASK20) << 2)
+ | (val & ~(MASK20 << 2)));
+ grub_set_unaligned32 (((grub_uint8_t *) (addr & MASK3) + 2),
+ grub_cpu_to_le32 (val));
+ break;
+ case 1:
+ val = grub_le_to_cpu32 (grub_get_unaligned32 (((grub_uint8_t *)
+ (addr & MASK3) + 7)));
+ val = ((((((val >> 3) & MASK20) + value) & MASK20) << 3)
+ | (val & ~(MASK20 << 3)));
+ grub_set_unaligned32 (((grub_uint8_t *) (addr & MASK3) + 7),
+ grub_cpu_to_le32 (val));
+ break;
+ case 2:
+ val = grub_le_to_cpu32 (grub_get_unaligned32 (((grub_uint8_t *)
+ (addr & MASK3) + 12)));
+ val = ((((((val >> 4) & MASK20) + value) & MASK20) << 4)
+ | (val & ~(MASK20 << 4)));
+ grub_set_unaligned32 (((grub_uint8_t *) (addr & MASK3) + 12),
+ grub_cpu_to_le32 (val));
+ break;
+ }
+}
+
+#define MASKF21 ( ((1 << 23) - 1) & ~((1 << 7) | (1 << 8)) )
+
+static grub_uint32_t
+add_value_to_slot_21_real (grub_uint32_t a, grub_uint32_t value)
+{
+ grub_uint32_t high, mid, low, c;
+ low = (a & 0x00007f);
+ mid = (a & 0x7fc000) >> 7;
+ high = (a & 0x003e00) << 7;
+ c = (low | mid | high) + value;
+ return (c & 0x7f) | ((c << 7) & 0x7fc000) | ((c >> 7) & 0x0003e00); //0x003e00
+}
+
+void
+grub_ia64_add_value_to_slot_21 (grub_addr_t addr, grub_uint32_t value)
+{
+ grub_uint32_t val;
+ switch (addr & 3)
+ {
+ case 0:
+ val = grub_le_to_cpu32 (grub_get_unaligned32 (((grub_uint8_t *)
+ (addr & MASK3) + 2)));
+ val = ((add_value_to_slot_21_real (((val >> 2) & MASKF21), value)
+ & MASKF21) << 2) | (val & ~(MASKF21 << 2));
+ grub_set_unaligned32 (((grub_uint8_t *) (addr & MASK3) + 2),
+ grub_cpu_to_le32 (val));
+ break;
+ case 1:
+ val = grub_le_to_cpu32 (grub_get_unaligned32 (((grub_uint8_t *)
+ (addr & MASK3) + 7)));
+ val = ((add_value_to_slot_21_real (((val >> 3) & MASKF21), value)
+ & MASKF21) << 3) | (val & ~(MASKF21 << 3));
+ grub_set_unaligned32 (((grub_uint8_t *) (addr & MASK3) + 7),
+ grub_cpu_to_le32 (val));
+ break;
+ case 2:
+ val = grub_le_to_cpu32 (grub_get_unaligned32 (((grub_uint8_t *)
+ (addr & MASK3) + 12)));
+ val = ((add_value_to_slot_21_real (((val >> 4) & MASKF21), value)
+ & MASKF21) << 4) | (val & ~(MASKF21 << 4));
+ grub_set_unaligned32 (((grub_uint8_t *) (addr & MASK3) + 12),
+ grub_cpu_to_le32 (val));
+ break;
+ }
+}
+
+static const grub_uint8_t nopm[5] =
+ {
+ /* [MLX] nop.m 0x0 */
+ 0x05, 0x00, 0x00, 0x00, 0x01
+ };
+
+#ifdef GRUB_UTIL
+static grub_uint8_t jump[0x20] =
+ {
+ /* [MMI] add r15=r15,r1;; */
+ 0x0b, 0x78, 0x3c, 0x02, 0x00, 0x20,
+ /* ld8 r16=[r15],8 */
+ 0x00, 0x41, 0x3c, 0x30, 0x28, 0xc0,
+ /* mov r14=r1;; */
+ 0x01, 0x08, 0x00, 0x84,
+ /* [MIB] ld8 r1=[r15] */
+ 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10,
+ /* mov b6=r16 */
+ 0x60, 0x80, 0x04, 0x80, 0x03, 0x00,
+ /* br.few b6;; */
+ 0x60, 0x00, 0x80, 0x00
+ };
+#else
+static const grub_uint8_t jump[0x20] =
+ {
+ /* ld8 r16=[r15],8 */
+ 0x02, 0x80, 0x20, 0x1e, 0x18, 0x14,
+ /* mov r14=r1;; */
+ 0xe0, 0x00, 0x04, 0x00, 0x42, 0x00,
+ /* nop.i 0x0 */
+ 0x00, 0x00, 0x04, 0x00,
+ /* ld8 r1=[r15] */
+ 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10,
+ /* mov b6=r16 */
+ 0x60, 0x80, 0x04, 0x80, 0x03, 0x00,
+ /* br.few b6;; */
+ 0x60, 0x00, 0x80, 0x00
+ };
+#endif
+
+void
+grub_ia64_make_trampoline (struct grub_ia64_trampoline *tr, grub_uint64_t addr)
+{
+ grub_memcpy (tr->nop, nopm, sizeof (tr->nop));
+ tr->addr_hi[0] = ((addr & 0xc00000) >> 16);
+ tr->addr_hi[1] = (addr >> 24) & 0xff;
+ tr->addr_hi[2] = (addr >> 32) & 0xff;
+ tr->addr_hi[3] = (addr >> 40) & 0xff;
+ tr->addr_hi[4] = (addr >> 48) & 0xff;
+ tr->addr_hi[5] = (addr >> 56) & 0xff;
+ tr->e0 = 0xe0;
+ tr->addr_lo[0] = ((addr & 0x000f) << 4) | 0x01;
+ tr->addr_lo[1] = (((addr & 0x0070) >> 4) | ((addr & 0x070000) >> 11)
+ | ((addr & 0x200000) >> 17));
+ tr->addr_lo[2] = ((addr & 0x1f80) >> 5) | ((addr & 0x180000) >> 19);
+ tr->addr_lo[3] = ((addr & 0xe000) >> 13) | 0x60;
+ grub_memcpy (tr->jump, jump, sizeof (tr->jump));
+}
+
+grub_err_t
+grub_ia64_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp,
+ grub_size_t *got)
+{
+ const Elf64_Ehdr *e = ehdr;
+ grub_size_t cntt = 0, cntg = 0;
+ const Elf64_Shdr *s;
+ unsigned i;
+
+ for (i = 0, s = (Elf64_Shdr *) ((char *) e + grub_le_to_cpu64 (e->e_shoff));
+ i < grub_le_to_cpu16 (e->e_shnum);
+ i++, s = (Elf64_Shdr *) ((char *) s + grub_le_to_cpu16 (e->e_shentsize)))
+ if (s->sh_type == grub_cpu_to_le32_compile_time (SHT_RELA))
+ {
+ const Elf64_Rela *rel, *max;
+
+ for (rel = (Elf64_Rela *) ((char *) e + grub_le_to_cpu64 (s->sh_offset)),
+ max = (const Elf64_Rela *) ((char *) rel + grub_le_to_cpu64 (s->sh_size));
+ rel < max; rel = (const Elf64_Rela *) ((char *) rel + grub_le_to_cpu64 (s->sh_entsize)))
+ switch (ELF64_R_TYPE (grub_le_to_cpu64 (rel->r_info)))
+ {
+ case R_IA64_PCREL21B:
+ cntt++;
+ break;
+ case R_IA64_LTOFF_FPTR22:
+ case R_IA64_LTOFF22X:
+ case R_IA64_LTOFF22:
+ cntg++;
+ break;
+ }
+ }
+ *tramp = cntt * sizeof (struct grub_ia64_trampoline);
+ *got = cntg * sizeof (grub_uint64_t);
+
+ return GRUB_ERR_NONE;
+}
+
diff --git a/grub-core/kern/ia64/efi/init.c b/grub-core/kern/ia64/efi/init.c
new file mode 100644
index 0000000..f196557
--- /dev/null
+++ b/grub-core/kern/ia64/efi/init.c
@@ -0,0 +1,80 @@
+/* init.c - initialize an ia64-based EFI system */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/types.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/time.h>
+#include <grub/err.h>
+#include <grub/dl.h>
+#include <grub/kernel.h>
+#include <grub/efi/efi.h>
+#include <grub/loader.h>
+
+static grub_uint64_t divisor = 1;
+
+static grub_uint64_t
+get_itc (void)
+{
+ grub_uint64_t ret;
+ asm volatile ("mov %0=ar.itc" : "=r" (ret));
+ return ret;
+}
+
+grub_uint64_t
+grub_rtc_get_time_ms (void)
+{
+ return get_itc () / divisor;
+}
+
+void
+grub_machine_init (void)
+{
+ grub_efi_event_t event;
+ grub_uint64_t before, after;
+ grub_efi_uintn_t idx;
+ grub_efi_init ();
+
+ efi_call_5 (grub_efi_system_table->boot_services->create_event,
+ GRUB_EFI_EVT_TIMER, GRUB_EFI_TPL_CALLBACK, 0, 0, &event);
+
+ before = get_itc ();
+ efi_call_3 (grub_efi_system_table->boot_services->set_timer, event,
+ GRUB_EFI_TIMER_RELATIVE, 200000);
+ efi_call_3 (grub_efi_system_table->boot_services->wait_for_event, 1,
+ &event, &idx);
+ after = get_itc ();
+ efi_call_1 (grub_efi_system_table->boot_services->close_event, event);
+ divisor = (after - before + 5) / 20;
+ if (divisor == 0)
+ divisor = 800000;
+ grub_install_get_time_ms (grub_rtc_get_time_ms);
+}
+
+void
+grub_machine_fini (int flags)
+{
+ if (!(flags & GRUB_LOADER_FLAG_NORETURN))
+ return;
+
+ grub_efi_fini ();
+
+ if (!(flags & GRUB_LOADER_FLAG_EFI_KEEP_ALLOCATED_MEMORY))
+ grub_efi_memory_fini ();
+}
diff --git a/grub-core/kern/ia64/efi/startup.S b/grub-core/kern/ia64/efi/startup.S
new file mode 100644
index 0000000..d75c6d7
--- /dev/null
+++ b/grub-core/kern/ia64/efi/startup.S
@@ -0,0 +1,44 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 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 <http://www.gnu.org/licenses/>.
+ */
+#include <config.h>
+#include <grub/symbol.h>
+#include <grub/offsets.h>
+
+ .text
+ .psr abi64
+ .psr lsb
+ .lsb
+
+ .global _start
+ .proc _start
+_start:
+ alloc loc0=ar.pfs,2,4,0,0
+ mov loc1=rp
+ addl loc2=@gprel(grub_efi_image_handle),gp
+ addl loc3=@gprel(grub_efi_system_table),gp
+ ;;
+ st8 [loc2]=in0
+ st8 [loc3]=in1
+ br.call.sptk.few rp=grub_main
+ ;;
+ mov ar.pfs=loc0
+ mov rp=loc1
+ ;;
+ br.ret.sptk.few rp
+
+ .endp _start
diff --git a/grub-core/kern/ieee1275/cmain.c b/grub-core/kern/ieee1275/cmain.c
new file mode 100644
index 0000000..20cbbd7
--- /dev/null
+++ b/grub-core/kern/ieee1275/cmain.c
@@ -0,0 +1,220 @@
+/* cmain.c - Startup code for the PowerPC. */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2003,2004,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/kernel.h>
+#include <grub/misc.h>
+#include <grub/types.h>
+#include <grub/ieee1275/ieee1275.h>
+
+int (*grub_ieee1275_entry_fn) (void *) GRUB_IEEE1275_ENTRY_FN_ATTRIBUTE;
+
+grub_ieee1275_phandle_t grub_ieee1275_chosen;
+grub_ieee1275_ihandle_t grub_ieee1275_mmu;
+
+static grub_uint32_t grub_ieee1275_flags;
+
+
+
+int
+grub_ieee1275_test_flag (enum grub_ieee1275_flag flag)
+{
+ return (grub_ieee1275_flags & (1 << flag));
+}
+
+void
+grub_ieee1275_set_flag (enum grub_ieee1275_flag flag)
+{
+ grub_ieee1275_flags |= (1 << flag);
+}
+
+static void
+grub_ieee1275_find_options (void)
+{
+ grub_ieee1275_phandle_t root;
+ grub_ieee1275_phandle_t options;
+ grub_ieee1275_phandle_t openprom;
+ grub_ieee1275_phandle_t bootrom;
+ int rc;
+ grub_uint32_t realmode = 0;
+ char tmp[256];
+ int is_smartfirmware = 0;
+ int is_olpc = 0;
+ int is_qemu = 0;
+ grub_ssize_t actual;
+
+#ifdef __sparc__
+ grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_NO_PARTITION_0);
+#endif
+
+ grub_ieee1275_finddevice ("/", &root);
+ grub_ieee1275_finddevice ("/options", &options);
+ grub_ieee1275_finddevice ("/openprom", &openprom);
+
+ rc = grub_ieee1275_get_integer_property (options, "real-mode?", &realmode,
+ sizeof realmode, 0);
+ if (((rc >= 0) && realmode) || (grub_ieee1275_mmu == 0))
+ grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_REAL_MODE);
+
+ rc = grub_ieee1275_get_property (openprom, "CodeGen-copyright",
+ tmp, sizeof (tmp), 0);
+ if (rc >= 0 && !grub_strncmp (tmp, "SmartFirmware(tm)",
+ sizeof ("SmartFirmware(tm)") - 1))
+ is_smartfirmware = 1;
+
+ rc = grub_ieee1275_get_property (root, "architecture",
+ tmp, sizeof (tmp), 0);
+ if (rc >= 0 && !grub_strcmp (tmp, "OLPC"))
+ is_olpc = 1;
+
+ rc = grub_ieee1275_get_property (root, "model",
+ tmp, sizeof (tmp), 0);
+ if (rc >= 0 && (!grub_strcmp (tmp, "Emulated PC")
+ || !grub_strcmp (tmp, "IBM pSeries (emulated by qemu)"))) {
+ is_qemu = 1;
+ }
+
+ if (rc >= 0 && grub_strncmp (tmp, "IBM", 3) == 0)
+ grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_NO_TREE_SCANNING_FOR_DISKS);
+
+ /* Old Macs have no key repeat, newer ones have fully working one.
+ The ones inbetween when repeated key generates an escaoe sequence
+ only the escape is repeated. With this workaround however a fast
+ e.g. down arrow-ESC is perceived as down arrow-down arrow which is
+ also annoying but is less so than the original bug of exiting from
+ the current window on arrow repeat. To avoid unaffected users suffering
+ from this workaround match only exact models known to have this bug.
+ */
+ if (rc >= 0 && grub_strcmp (tmp, "PowerBook3,3") == 0)
+ grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_BROKEN_REPEAT);
+
+ rc = grub_ieee1275_get_property (root, "compatible",
+ tmp, sizeof (tmp), &actual);
+ if (rc >= 0)
+ {
+ char *ptr;
+
+ if (grub_strncmp (tmp, "sun4v", 5) == 0)
+ grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_RAW_DEVNAMES);
+ for (ptr = tmp; ptr - tmp < actual; ptr += grub_strlen (ptr) + 1)
+ {
+ if (grub_memcmp (ptr, "MacRISC", sizeof ("MacRISC") - 1) == 0
+ && (ptr[sizeof ("MacRISC") - 1] == 0
+ || grub_isdigit (ptr[sizeof ("MacRISC") - 1])))
+ {
+ grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_BROKEN_ADDRESS_CELLS);
+ grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_NO_OFNET_SUFFIX);
+ grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN);
+ grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_CURSORONOFF_ANSI_BROKEN);
+ break;
+ }
+ }
+ }
+
+ if (is_smartfirmware)
+ {
+ /* Broken in all versions */
+ grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_BROKEN_OUTPUT);
+
+ /* There are two incompatible ways of checking the version number. Try
+ both. */
+ rc = grub_ieee1275_get_property (openprom, "SmartFirmware-version",
+ tmp, sizeof (tmp), 0);
+ if (rc < 0)
+ rc = grub_ieee1275_get_property (openprom, "firmware-version",
+ tmp, sizeof (tmp), 0);
+ if (rc >= 0)
+ {
+ /* It is tempting to implement a version parser to set the flags for
+ e.g. 1.3 and below. However, there's a special situation here.
+ 3rd party updates which fix the partition bugs are common, and for
+ some reason their fixes aren't being merged into trunk. So for
+ example we know that 1.2 and 1.3 are broken, but there's 1.2.99
+ and 1.3.99 which are known good (and applying this workaround
+ would cause breakage). */
+ if (!grub_strcmp (tmp, "1.0")
+ || !grub_strcmp (tmp, "1.1")
+ || !grub_strcmp (tmp, "1.2")
+ || !grub_strcmp (tmp, "1.3"))
+ {
+ grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_NO_PARTITION_0);
+ grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_0_BASED_PARTITIONS);
+ }
+ }
+ }
+
+ if (is_olpc)
+ {
+ /* OLPC / XO laptops have three kinds of storage devices:
+
+ - NAND flash. These are accessible via OFW callbacks, but:
+ - Follow strange semantics, imposed by hardware constraints.
+ - Its ABI is undocumented, and not stable.
+ They lack "device_type" property, which conveniently makes GRUB
+ skip them.
+
+ - USB drives. Not accessible, because OFW shuts down the controller
+ in order to prevent collisions with applications accessing it
+ directly. Even worse, attempts to access it will NOT return
+ control to the caller, so we have to avoid probing them.
+
+ - SD cards. These work fine.
+
+ To avoid breakage, we only need to skip USB probing. However,
+ since detecting SD cards is more reliable, we do that instead.
+ */
+
+ grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_OFDISK_SDCARD_ONLY);
+ grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_HAS_CURSORONOFF);
+ }
+
+ if (is_qemu)
+ {
+ /* OpenFirmware hangs on qemu if one requests any memory below 1.5 MiB. */
+ grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_NO_PRE1_5M_CLAIM);
+
+ grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_HAS_CURSORONOFF);
+ }
+
+ if (! grub_ieee1275_finddevice ("/rom/boot-rom", &bootrom)
+ || ! grub_ieee1275_finddevice ("/boot-rom", &bootrom))
+ {
+ rc = grub_ieee1275_get_property (bootrom, "model", tmp, sizeof (tmp), 0);
+ if (rc >= 0 && !grub_strncmp (tmp, "PPC Open Hack'Ware",
+ sizeof ("PPC Open Hack'Ware") - 1))
+ {
+ grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_BROKEN_OUTPUT);
+ grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_CANNOT_SET_COLORS);
+ grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_CANNOT_INTERPRET);
+ grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_FORCE_CLAIM);
+ grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_NO_ANSI);
+ }
+ }
+}
+
+void
+grub_ieee1275_init (void)
+{
+ grub_ieee1275_finddevice ("/chosen", &grub_ieee1275_chosen);
+
+ if (grub_ieee1275_get_integer_property (grub_ieee1275_chosen, "mmu", &grub_ieee1275_mmu,
+ sizeof grub_ieee1275_mmu, 0) < 0)
+ grub_ieee1275_mmu = 0;
+
+ grub_ieee1275_find_options ();
+}
diff --git a/grub-core/kern/ieee1275/ieee1275.c b/grub-core/kern/ieee1275/ieee1275.c
new file mode 100644
index 0000000..86f81a3
--- /dev/null
+++ b/grub-core/kern/ieee1275/ieee1275.c
@@ -0,0 +1,809 @@
+/* of.c - Access the Open Firmware client interface. */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2003,2004,2005,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/ieee1275/ieee1275.h>
+#include <grub/types.h>
+#include <grub/misc.h>
+
+#define IEEE1275_PHANDLE_INVALID ((grub_ieee1275_cell_t) -1)
+#define IEEE1275_IHANDLE_INVALID ((grub_ieee1275_cell_t) 0)
+#define IEEE1275_CELL_INVALID ((grub_ieee1275_cell_t) -1)
+
+
+
+int
+grub_ieee1275_finddevice (const char *name, grub_ieee1275_phandle_t *phandlep)
+{
+ struct find_device_args
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t device;
+ grub_ieee1275_cell_t phandle;
+ }
+ args;
+
+ INIT_IEEE1275_COMMON (&args.common, "finddevice", 1, 1);
+ args.device = (grub_ieee1275_cell_t) name;
+
+ if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
+ return -1;
+ *phandlep = args.phandle;
+ if (args.phandle == IEEE1275_PHANDLE_INVALID)
+ return -1;
+ return 0;
+}
+
+int
+grub_ieee1275_get_property (grub_ieee1275_phandle_t phandle,
+ const char *property, void *buf,
+ grub_size_t size, grub_ssize_t *actual)
+{
+ struct get_property_args
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t phandle;
+ grub_ieee1275_cell_t prop;
+ grub_ieee1275_cell_t buf;
+ grub_ieee1275_cell_t buflen;
+ grub_ieee1275_cell_t size;
+ }
+ args;
+
+ INIT_IEEE1275_COMMON (&args.common, "getprop", 4, 1);
+ args.phandle = phandle;
+ args.prop = (grub_ieee1275_cell_t) property;
+ args.buf = (grub_ieee1275_cell_t) buf;
+ args.buflen = (grub_ieee1275_cell_t) size;
+
+ if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
+ return -1;
+ if (actual)
+ *actual = (grub_ssize_t) args.size;
+ if (args.size == IEEE1275_CELL_INVALID)
+ return -1;
+ return 0;
+}
+
+int
+grub_ieee1275_get_integer_property (grub_ieee1275_phandle_t phandle,
+ const char *property, grub_uint32_t *buf,
+ grub_size_t size, grub_ssize_t *actual)
+{
+ int ret;
+ ret = grub_ieee1275_get_property (phandle, property, (void *) buf, size, actual);
+#ifndef GRUB_CPU_WORDS_BIGENDIAN
+ /* Integer properties are always in big endian. */
+ if (ret == 0)
+ {
+ unsigned int i;
+ size /= sizeof (grub_uint32_t);
+ for (i = 0; i < size; i++)
+ buf[i] = grub_be_to_cpu32 (buf[i]);
+ }
+#endif
+ return ret;
+}
+
+int
+grub_ieee1275_next_property (grub_ieee1275_phandle_t phandle, char *prev_prop,
+ char *prop)
+{
+ struct get_property_args
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t phandle;
+ grub_ieee1275_cell_t prev_prop;
+ grub_ieee1275_cell_t next_prop;
+ grub_ieee1275_cell_t flags;
+ }
+ args;
+
+ INIT_IEEE1275_COMMON (&args.common, "nextprop", 3, 1);
+ args.phandle = phandle;
+ args.prev_prop = (grub_ieee1275_cell_t) prev_prop;
+ args.next_prop = (grub_ieee1275_cell_t) prop;
+ args.flags = (grub_ieee1275_cell_t) -1;
+
+ if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
+ return -1;
+ return (int) args.flags;
+}
+
+int
+grub_ieee1275_get_property_length (grub_ieee1275_phandle_t phandle,
+ const char *prop, grub_ssize_t *length)
+{
+ struct get_property_args
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t phandle;
+ grub_ieee1275_cell_t prop;
+ grub_ieee1275_cell_t length;
+ }
+ args;
+
+ INIT_IEEE1275_COMMON (&args.common, "getproplen", 2, 1);
+ args.phandle = phandle;
+ args.prop = (grub_ieee1275_cell_t) prop;
+ args.length = (grub_ieee1275_cell_t) -1;
+
+ if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
+ return -1;
+ *length = args.length;
+ if (args.length == IEEE1275_CELL_INVALID)
+ return -1;
+ return 0;
+}
+
+int
+grub_ieee1275_instance_to_package (grub_ieee1275_ihandle_t ihandle,
+ grub_ieee1275_phandle_t *phandlep)
+{
+ struct instance_to_package_args
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t ihandle;
+ grub_ieee1275_cell_t phandle;
+ }
+ args;
+
+ INIT_IEEE1275_COMMON (&args.common, "instance-to-package", 1, 1);
+ args.ihandle = ihandle;
+
+ if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
+ return -1;
+ *phandlep = args.phandle;
+ if (args.phandle == IEEE1275_PHANDLE_INVALID)
+ return -1;
+ return 0;
+}
+
+int
+grub_ieee1275_package_to_path (grub_ieee1275_phandle_t phandle,
+ char *path, grub_size_t len,
+ grub_ssize_t *actual)
+{
+ struct instance_to_package_args
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t phandle;
+ grub_ieee1275_cell_t buf;
+ grub_ieee1275_cell_t buflen;
+ grub_ieee1275_cell_t actual;
+ }
+ args;
+
+ INIT_IEEE1275_COMMON (&args.common, "package-to-path", 3, 1);
+ args.phandle = phandle;
+ args.buf = (grub_ieee1275_cell_t) path;
+ args.buflen = (grub_ieee1275_cell_t) len;
+
+ if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
+ return -1;
+ if (actual)
+ *actual = args.actual;
+ if (args.actual == IEEE1275_CELL_INVALID)
+ return -1;
+ return 0;
+}
+
+int
+grub_ieee1275_instance_to_path (grub_ieee1275_ihandle_t ihandle,
+ char *path, grub_size_t len,
+ grub_ssize_t *actual)
+{
+ struct instance_to_path_args
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t ihandle;
+ grub_ieee1275_cell_t buf;
+ grub_ieee1275_cell_t buflen;
+ grub_ieee1275_cell_t actual;
+ }
+ args;
+
+ INIT_IEEE1275_COMMON (&args.common, "instance-to-path", 3, 1);
+ args.ihandle = ihandle;
+ args.buf = (grub_ieee1275_cell_t) path;
+ args.buflen = (grub_ieee1275_cell_t) len;
+
+ if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
+ return -1;
+ if (actual)
+ *actual = args.actual;
+ if (args.actual == IEEE1275_CELL_INVALID)
+ return -1;
+ return 0;
+}
+
+int
+grub_ieee1275_write (grub_ieee1275_ihandle_t ihandle, const void *buffer,
+ grub_size_t len, grub_ssize_t *actualp)
+{
+ struct write_args
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t ihandle;
+ grub_ieee1275_cell_t buf;
+ grub_ieee1275_cell_t len;
+ grub_ieee1275_cell_t actual;
+ }
+ args;
+
+ INIT_IEEE1275_COMMON (&args.common, "write", 3, 1);
+ args.ihandle = ihandle;
+ args.buf = (grub_ieee1275_cell_t) buffer;
+ args.len = (grub_ieee1275_cell_t) len;
+
+ if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
+ return -1;
+ if (actualp)
+ *actualp = args.actual;
+ return 0;
+}
+
+int
+grub_ieee1275_read (grub_ieee1275_ihandle_t ihandle, void *buffer,
+ grub_size_t len, grub_ssize_t *actualp)
+{
+ struct write_args
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t ihandle;
+ grub_ieee1275_cell_t buf;
+ grub_ieee1275_cell_t len;
+ grub_ieee1275_cell_t actual;
+ }
+ args;
+
+ INIT_IEEE1275_COMMON (&args.common, "read", 3, 1);
+ args.ihandle = ihandle;
+ args.buf = (grub_ieee1275_cell_t) buffer;
+ args.len = (grub_ieee1275_cell_t) len;
+
+ if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
+ return -1;
+ if (actualp)
+ *actualp = args.actual;
+ return 0;
+}
+
+int
+grub_ieee1275_seek (grub_ieee1275_ihandle_t ihandle, grub_disk_addr_t pos,
+ grub_ssize_t *result)
+{
+ struct write_args
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t ihandle;
+ grub_ieee1275_cell_t pos_hi;
+ grub_ieee1275_cell_t pos_lo;
+ grub_ieee1275_cell_t result;
+ }
+ args;
+
+ INIT_IEEE1275_COMMON (&args.common, "seek", 3, 1);
+ args.ihandle = ihandle;
+ /* To prevent stupid gcc warning. */
+#if GRUB_IEEE1275_CELL_SIZEOF >= 8
+ args.pos_hi = 0;
+ args.pos_lo = pos;
+#else
+ args.pos_hi = (grub_ieee1275_cell_t) (pos >> (8 * GRUB_IEEE1275_CELL_SIZEOF));
+ args.pos_lo = (grub_ieee1275_cell_t)
+ (pos & ((1ULL << (8 * GRUB_IEEE1275_CELL_SIZEOF)) - 1));
+#endif
+
+ if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
+ return -1;
+
+ if (result)
+ *result = args.result;
+ return 0;
+}
+
+int
+grub_ieee1275_peer (grub_ieee1275_phandle_t node,
+ grub_ieee1275_phandle_t *result)
+{
+ struct peer_args
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t node;
+ grub_ieee1275_cell_t result;
+ }
+ args;
+
+ INIT_IEEE1275_COMMON (&args.common, "peer", 1, 1);
+ args.node = node;
+
+ if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
+ return -1;
+ *result = args.result;
+ if (args.result == 0)
+ return -1;
+ return 0;
+}
+
+int
+grub_ieee1275_child (grub_ieee1275_phandle_t node,
+ grub_ieee1275_phandle_t *result)
+{
+ struct child_args
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t node;
+ grub_ieee1275_cell_t result;
+ }
+ args;
+
+ INIT_IEEE1275_COMMON (&args.common, "child", 1, 1);
+ args.node = node;
+ args.result = IEEE1275_PHANDLE_INVALID;
+
+ if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
+ return -1;
+ *result = args.result;
+ if (args.result == 0)
+ return -1;
+ return 0;
+}
+
+int
+grub_ieee1275_parent (grub_ieee1275_phandle_t node,
+ grub_ieee1275_phandle_t *result)
+{
+ struct parent_args
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t node;
+ grub_ieee1275_cell_t result;
+ }
+ args;
+
+ INIT_IEEE1275_COMMON (&args.common, "parent", 1, 1);
+ args.node = node;
+ args.result = IEEE1275_PHANDLE_INVALID;
+
+ if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
+ return -1;
+ *result = args.result;
+ return 0;
+}
+
+int
+grub_ieee1275_interpret (const char *command, grub_ieee1275_cell_t *catch)
+{
+ struct enter_args
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t command;
+ grub_ieee1275_cell_t catch;
+ }
+ args;
+
+ if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CANNOT_INTERPRET))
+ return -1;
+
+ INIT_IEEE1275_COMMON (&args.common, "interpret", 1, 1);
+ args.command = (grub_ieee1275_cell_t) command;
+
+ if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
+ return -1;
+ if (catch)
+ *catch = args.catch;
+ return 0;
+}
+
+int
+grub_ieee1275_enter (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;
+}
+
+void
+grub_ieee1275_exit (void)
+{
+ struct exit_args
+ {
+ struct grub_ieee1275_common_hdr common;
+ }
+ args;
+
+ INIT_IEEE1275_COMMON (&args.common, "exit", 0, 0);
+
+ IEEE1275_CALL_ENTRY_FN (&args);
+ for (;;) ;
+}
+
+int
+grub_ieee1275_open (const char *path, grub_ieee1275_ihandle_t *result)
+{
+ struct open_args
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t path;
+ grub_ieee1275_cell_t result;
+ }
+ args;
+
+ INIT_IEEE1275_COMMON (&args.common, "open", 1, 1);
+ args.path = (grub_ieee1275_cell_t) path;
+
+ if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
+ return -1;
+ *result = args.result;
+ if (args.result == IEEE1275_IHANDLE_INVALID)
+ return -1;
+ return 0;
+}
+
+int
+grub_ieee1275_close (grub_ieee1275_ihandle_t ihandle)
+{
+ struct close_args
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t ihandle;
+ }
+ args;
+
+ INIT_IEEE1275_COMMON (&args.common, "close", 1, 0);
+ args.ihandle = ihandle;
+
+ if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
+ return -1;
+
+ return 0;
+}
+
+int
+grub_ieee1275_decode_unit4 (grub_ieee1275_ihandle_t ihandle,
+ void *addr, grub_size_t size,
+ grub_uint32_t *phy_lo, grub_uint32_t *phy_hi,
+ grub_uint32_t *lun_lo, grub_uint32_t *lun_hi)
+{
+ struct decode_args
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t method;
+ grub_ieee1275_cell_t ihandle;
+ grub_ieee1275_cell_t size;
+ grub_ieee1275_cell_t addr;
+ grub_ieee1275_cell_t catch_result;
+ grub_ieee1275_cell_t tgt_h;
+ grub_ieee1275_cell_t tgt_l;
+ grub_ieee1275_cell_t lun_h;
+ grub_ieee1275_cell_t lun_l;
+ }
+ args;
+
+ INIT_IEEE1275_COMMON (&args.common, "call-method", 4, 5);
+ args.method = (grub_ieee1275_cell_t) "decode-unit";
+ args.ihandle = ihandle;
+ args.size = size;
+ args.addr = (grub_ieee1275_cell_t) addr;
+ args.catch_result = 1;
+
+ if ((IEEE1275_CALL_ENTRY_FN (&args) == -1) || (args.catch_result))
+ {
+ grub_error (GRUB_ERR_OUT_OF_RANGE, "decode-unit failed\n");
+ return -1;
+ }
+
+ *phy_lo = args.tgt_l;
+ *phy_hi = args.tgt_h;
+ *lun_lo = args.lun_l;
+ *lun_hi = args.lun_h;
+ return 0;
+}
+
+char *
+grub_ieee1275_encode_uint4 (grub_ieee1275_ihandle_t ihandle,
+ grub_uint32_t phy_lo, grub_uint32_t phy_hi,
+ grub_uint32_t lun_lo, grub_uint32_t lun_hi,
+ grub_size_t *size)
+{
+ char *addr;
+ struct encode_args
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t method;
+ grub_ieee1275_cell_t ihandle;
+ grub_ieee1275_cell_t tgt_h;
+ grub_ieee1275_cell_t tgt_l;
+ grub_ieee1275_cell_t lun_h;
+ grub_ieee1275_cell_t lun_l;
+ grub_ieee1275_cell_t catch_result;
+ grub_ieee1275_cell_t size;
+ grub_ieee1275_cell_t addr;
+ }
+ args;
+
+ INIT_IEEE1275_COMMON (&args.common, "call-method", 6, 3);
+ args.method = (grub_ieee1275_cell_t) "encode-unit";
+ args.ihandle = ihandle;
+
+ args.tgt_l = phy_lo;
+ args.tgt_h = phy_hi;
+ args.lun_l = lun_lo;
+ args.lun_h = lun_hi;
+ args.catch_result = 1;
+
+ if ((IEEE1275_CALL_ENTRY_FN (&args) == -1) || (args.catch_result))
+ {
+ grub_error (GRUB_ERR_OUT_OF_RANGE, "encode-unit failed\n");
+ return 0;
+ }
+
+ addr = (void *)args.addr;
+ *size = args.size;
+ addr = grub_strdup ((char *)args.addr);
+ return addr;
+}
+
+int
+grub_ieee1275_claim (grub_addr_t addr, grub_size_t size, unsigned int align,
+ grub_addr_t *result)
+{
+ struct claim_args
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t addr;
+ grub_ieee1275_cell_t size;
+ grub_ieee1275_cell_t align;
+ grub_ieee1275_cell_t base;
+ }
+ args;
+
+ INIT_IEEE1275_COMMON (&args.common, "claim", 3, 1);
+ args.addr = (grub_ieee1275_cell_t) addr;
+ args.size = (grub_ieee1275_cell_t) size;
+ args.align = (grub_ieee1275_cell_t) align;
+
+ if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
+ return -1;
+ if (result)
+ *result = args.base;
+ if (args.base == IEEE1275_CELL_INVALID)
+ return -1;
+ return 0;
+}
+
+int
+grub_ieee1275_release (grub_addr_t addr, grub_size_t size)
+{
+ struct release_args
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t addr;
+ grub_ieee1275_cell_t size;
+ }
+ args;
+
+ INIT_IEEE1275_COMMON (&args.common, "release", 2, 0);
+ args.addr = addr;
+ args.size = size;
+
+ if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
+ return -1;
+ return 0;
+}
+
+int
+grub_ieee1275_set_property (grub_ieee1275_phandle_t phandle,
+ const char *propname, const void *buf,
+ grub_size_t size, grub_ssize_t *actual)
+{
+ struct set_property_args
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t phandle;
+ grub_ieee1275_cell_t propname;
+ grub_ieee1275_cell_t buf;
+ grub_ieee1275_cell_t size;
+ grub_ieee1275_cell_t actual;
+ }
+ args;
+
+ INIT_IEEE1275_COMMON (&args.common, "setprop", 4, 1);
+ args.size = (grub_ieee1275_cell_t) size;
+ args.buf = (grub_ieee1275_cell_t) buf;
+ args.propname = (grub_ieee1275_cell_t) propname;
+ args.phandle = (grub_ieee1275_cell_t) phandle;
+
+ if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
+ return -1;
+ *actual = args.actual;
+ if ((args.actual == IEEE1275_CELL_INVALID) || (args.actual != args.size))
+ return -1;
+ return 0;
+}
+
+int
+grub_ieee1275_set_color (grub_ieee1275_ihandle_t ihandle,
+ int index, int r, int g, int b)
+{
+ struct set_color_args
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t method;
+ grub_ieee1275_cell_t ihandle;
+ grub_ieee1275_cell_t index;
+ grub_ieee1275_cell_t b;
+ grub_ieee1275_cell_t g;
+ grub_ieee1275_cell_t r;
+ grub_ieee1275_cell_t catch_result;
+ }
+ args;
+
+ INIT_IEEE1275_COMMON (&args.common, "call-method", 6, 1);
+ args.method = (grub_ieee1275_cell_t) "color!";
+ args.ihandle = ihandle;
+ args.index = index;
+ args.r = r;
+ args.g = g;
+ args.b = b;
+
+ if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
+ return -1;
+ return args.catch_result;
+}
+
+int
+grub_ieee1275_milliseconds (grub_uint32_t *msecs)
+{
+ struct milliseconds_args
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t msecs;
+ }
+ args;
+
+ INIT_IEEE1275_COMMON (&args.common, "milliseconds", 0, 1);
+
+ if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
+ return -1;
+ *msecs = args.msecs;
+ return 0;
+}
+
+int
+grub_ieee1275_set_address (grub_ieee1275_ihandle_t ihandle,
+ grub_uint32_t target, grub_uint32_t lun)
+{
+ struct set_address
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t method;
+ grub_ieee1275_cell_t ihandle;
+ grub_ieee1275_cell_t tgt;
+ grub_ieee1275_cell_t lun;
+ grub_ieee1275_cell_t catch_result;
+ }
+ args;
+
+ INIT_IEEE1275_COMMON (&args.common, "call-method", 4, 1);
+
+ /*
+ * IEEE 1275-1994 Standard for Boot (Initialization Configuration)
+ * Firmware: Core Requirements and Practices
+ * E.3.2.2 Bus-specific methods for bus nodes
+ *
+ * A package implementing the scsi-2 device type shall implement the
+ * following bus-specific method:
+ *
+ * set-address ( unit# target# -- )
+ * Sets the SCSI target number (0x0..0xf) and unit number (0..7) to which
+ * subsequent commands apply.
+ */
+ args.method = (grub_ieee1275_cell_t) "set-address";
+ args.ihandle = ihandle;
+ args.tgt = target;
+ args.lun = lun;
+
+ if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
+ return -1;
+
+ return args.catch_result;
+}
+
+int
+grub_ieee1275_no_data_command (grub_ieee1275_ihandle_t ihandle,
+ const void *cmd_addr, grub_ssize_t *result)
+{
+ struct set_address
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t method;
+ grub_ieee1275_cell_t ihandle;
+ grub_ieee1275_cell_t cmd_addr;
+ grub_ieee1275_cell_t error;
+ grub_ieee1275_cell_t catch_result;
+ }
+ args;
+
+ INIT_IEEE1275_COMMON (&args.common, "call-method", 3, 2);
+
+ /*
+ * IEEE 1275-1994 Standard for Boot (Initialization Configuration)
+ * Firmware: Core Requirements and Practices
+ *
+ * E.3.2.2 Bus-specific methods for bus nodes
+ *
+ * A package implementing the scsi-2 device type shall implement the
+ * following bus-specific method:
+ *
+ * no-data-command ( cmd-addr -- error? )
+ * Executes a simple SCSI command, automatically retrying under
+ * certain conditions. cmd-addr is the address of a 6-byte command buffer
+ * containing an SCSI command that does not have a data transfer phase.
+ * Executes the command, retrying indefinitely with the same retry criteria
+ * as retry-command.
+ *
+ * error? is nonzero if an error occurred, zero otherwise.
+ * NOTE no-data-command is a convenience function. It provides
+ * no capabilities that are not present in retry-command, but for
+ * those commands that meet its restrictions, it is easier to use.
+ */
+ args.method = (grub_ieee1275_cell_t) "no-data-command";
+ args.ihandle = ihandle;
+ args.cmd_addr = (grub_ieee1275_cell_t) cmd_addr;
+
+ if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
+ return -1;
+
+ if (result)
+ *result = args.error;
+
+ return args.catch_result;
+}
+
+int
+grub_ieee1275_get_block_size (grub_ieee1275_ihandle_t ihandle)
+{
+ struct size_args_ieee1275
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t method;
+ grub_ieee1275_cell_t ihandle;
+ grub_ieee1275_cell_t result;
+ grub_ieee1275_cell_t size;
+ } args;
+
+ INIT_IEEE1275_COMMON (&args.common, "call-method", 2, 2);
+ args.method = (grub_ieee1275_cell_t) "block-size";
+ args.ihandle = ihandle;
+ args.result = 1;
+
+ if ((IEEE1275_CALL_ENTRY_FN (&args) == -1) || (args.result))
+ return 0;
+
+ return args.size;
+}
diff --git a/grub-core/kern/ieee1275/init.c b/grub-core/kern/ieee1275/init.c
new file mode 100644
index 0000000..d483e35
--- /dev/null
+++ b/grub-core/kern/ieee1275/init.c
@@ -0,0 +1,322 @@
+/* init.c -- Initialize GRUB on the newworld mac (PPC). */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2003,2004,2005,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/kernel.h>
+#include <grub/dl.h>
+#include <grub/disk.h>
+#include <grub/mm.h>
+#include <grub/partition.h>
+#include <grub/normal.h>
+#include <grub/fs.h>
+#include <grub/setjmp.h>
+#include <grub/env.h>
+#include <grub/misc.h>
+#include <grub/time.h>
+#include <grub/ieee1275/console.h>
+#include <grub/ieee1275/ofdisk.h>
+#ifdef __sparc__
+#include <grub/ieee1275/obdisk.h>
+#endif
+#include <grub/ieee1275/ieee1275.h>
+#include <grub/net.h>
+#include <grub/offsets.h>
+#include <grub/memory.h>
+#include <grub/loader.h>
+#ifdef __i386__
+#include <grub/cpu/tsc.h>
+#endif
+#ifdef __sparc__
+#include <grub/machine/kernel.h>
+#endif
+
+/* The minimal heap size we can live with. */
+#define HEAP_MIN_SIZE (unsigned long) (2 * 1024 * 1024)
+
+/* The maximum heap size we're going to claim */
+#ifdef __i386__
+#define HEAP_MAX_SIZE (unsigned long) (64 * 1024 * 1024)
+#else
+#define HEAP_MAX_SIZE (unsigned long) (32 * 1024 * 1024)
+#endif
+
+/* If possible, we will avoid claiming heap above this address, because it
+ seems to cause relocation problems with OSes that link at 4 MiB */
+#ifdef __i386__
+#define HEAP_MAX_ADDR (unsigned long) (64 * 1024 * 1024)
+#else
+#define HEAP_MAX_ADDR (unsigned long) (32 * 1024 * 1024)
+#endif
+
+extern char _start[];
+extern char _end[];
+
+#ifdef __sparc__
+grub_addr_t grub_ieee1275_original_stack;
+#endif
+
+void
+grub_exit (void)
+{
+ grub_ieee1275_exit ();
+}
+
+/* Translate an OF filesystem path (separated by backslashes), into a GRUB
+ path (separated by forward slashes). */
+static void
+grub_translate_ieee1275_path (char *filepath)
+{
+ char *backslash;
+
+ backslash = grub_strchr (filepath, '\\');
+ while (backslash != 0)
+ {
+ *backslash = '/';
+ backslash = grub_strchr (filepath, '\\');
+ }
+}
+
+void (*grub_ieee1275_net_config) (const char *dev, char **device, char **path,
+ char *bootpath);
+void
+grub_machine_get_bootlocation (char **device, char **path)
+{
+ char *bootpath;
+ char *filename;
+ char *type;
+
+ bootpath = grub_ieee1275_get_boot_dev ();
+ if (! bootpath)
+ return;
+
+ /* Transform an OF device path to a GRUB path. */
+
+ type = grub_ieee1275_get_device_type (bootpath);
+ if (type && grub_strcmp (type, "network") == 0)
+ {
+ char *dev, *canon;
+ char *ptr;
+ dev = grub_ieee1275_get_aliasdevname (bootpath);
+ canon = grub_ieee1275_canonicalise_devname (dev);
+ if (! canon)
+ return;
+ ptr = canon + grub_strlen (canon) - 1;
+ while (ptr > canon && (*ptr == ',' || *ptr == ':'))
+ ptr--;
+ ptr++;
+ *ptr = 0;
+
+ if (grub_ieee1275_net_config)
+ grub_ieee1275_net_config (canon, device, path, bootpath);
+ grub_free (dev);
+ grub_free (canon);
+ }
+ else
+ *device = grub_ieee1275_encode_devname (bootpath);
+ grub_free (type);
+
+ filename = grub_ieee1275_get_filename (bootpath);
+ if (filename)
+ {
+ char *lastslash = grub_strrchr (filename, '\\');
+
+ /* Truncate at last directory. */
+ if (lastslash)
+ {
+ *lastslash = '\0';
+ grub_translate_ieee1275_path (filename);
+
+ *path = filename;
+ }
+ }
+ grub_free (bootpath);
+}
+
+/* Claim some available memory in the first /memory node. */
+#ifdef __sparc__
+static void
+grub_claim_heap (void)
+{
+ grub_mm_init_region ((void *) (grub_modules_get_end ()
+ + GRUB_KERNEL_MACHINE_STACK_SIZE), 0x200000);
+}
+#else
+/* Helper for grub_claim_heap. */
+static int
+heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
+ void *data)
+{
+ unsigned long *total = data;
+
+ if (type != GRUB_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;
+ }
+ }
+ len -= 1; /* Required for some firmware. */
+
+ /* Never exceed HEAP_MAX_SIZE */
+ if (*total + len > HEAP_MAX_SIZE)
+ len = HEAP_MAX_SIZE - *total;
+
+ /* Avoid claiming anything above HEAP_MAX_ADDR, if possible. */
+ if ((addr < HEAP_MAX_ADDR) && /* if it's too late, don't bother */
+ (addr + len > HEAP_MAX_ADDR) && /* if it wasn't available anyway, don't bother */
+ (*total + (HEAP_MAX_ADDR - addr) > HEAP_MIN_SIZE)) /* only limit ourselves when we can afford to */
+ len = HEAP_MAX_ADDR - addr;
+
+ /* In theory, firmware should already prevent this from happening by not
+ listing our own image in /memory/available. The check below is intended
+ as a safeguard in case that doesn't happen. However, it doesn't protect
+ us from corrupting our module area, which extends up to a
+ yet-undetermined region above _end. */
+ if ((addr < (grub_addr_t) _end) && ((addr + len) > (grub_addr_t) _start))
+ {
+ grub_printf ("Warning: attempt to claim over our own code!\n");
+ len = 0;
+ }
+
+ if (len)
+ {
+ grub_err_t err;
+ /* Claim and use it. */
+ err = grub_claimmap (addr, len);
+ if (err)
+ return err;
+ grub_mm_init_region ((void *) (grub_addr_t) addr, len);
+ }
+
+ *total += len;
+ if (*total >= HEAP_MAX_SIZE)
+ return 1;
+
+ return 0;
+}
+
+static void
+grub_claim_heap (void)
+{
+ unsigned long total = 0;
+
+ if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_FORCE_CLAIM))
+ heap_init (GRUB_IEEE1275_STATIC_HEAP_START, GRUB_IEEE1275_STATIC_HEAP_LEN,
+ 1, &total);
+ else
+ grub_machine_mmap_iterate (heap_init, &total);
+}
+#endif
+
+static void
+grub_parse_cmdline (void)
+{
+ grub_ssize_t actual;
+ char args[256];
+
+ if (grub_ieee1275_get_property (grub_ieee1275_chosen, "bootargs", &args,
+ sizeof args, &actual) == 0
+ && actual > 1)
+ {
+ int i = 0;
+
+ while (i < actual)
+ {
+ char *command = &args[i];
+ char *end;
+ char *val;
+
+ end = grub_strchr (command, ';');
+ if (end == 0)
+ i = actual; /* No more commands after this one. */
+ else
+ {
+ *end = '\0';
+ i += end - command + 1;
+ while (grub_isspace(args[i]))
+ i++;
+ }
+
+ /* Process command. */
+ val = grub_strchr (command, '=');
+ if (val)
+ {
+ *val = '\0';
+ grub_env_set (command, val + 1);
+ }
+ }
+ }
+}
+
+grub_addr_t grub_modbase;
+
+void
+grub_machine_init (void)
+{
+ grub_modbase = ALIGN_UP((grub_addr_t) _end
+ + GRUB_KERNEL_MACHINE_MOD_GAP,
+ GRUB_KERNEL_MACHINE_MOD_ALIGN);
+ grub_ieee1275_init ();
+
+ grub_console_init_early ();
+ grub_claim_heap ();
+ grub_console_init_lately ();
+#ifdef __sparc__
+ grub_obdisk_init ();
+#else
+ grub_ofdisk_init ();
+#endif
+ grub_parse_cmdline ();
+
+#ifdef __i386__
+ grub_tsc_init ();
+#else
+ grub_install_get_time_ms (grub_rtc_get_time_ms);
+#endif
+}
+
+void
+grub_machine_fini (int flags)
+{
+ if (flags & GRUB_LOADER_FLAG_NORETURN)
+ {
+#ifdef __sparc__
+ grub_obdisk_fini ();
+#else
+ grub_ofdisk_fini ();
+#endif
+ grub_console_fini ();
+ }
+}
+
+grub_uint64_t
+grub_rtc_get_time_ms (void)
+{
+ grub_uint32_t msecs = 0;
+
+ grub_ieee1275_milliseconds (&msecs);
+
+ return msecs;
+}
diff --git a/grub-core/kern/ieee1275/mmap.c b/grub-core/kern/ieee1275/mmap.c
new file mode 100644
index 0000000..bf325ea
--- /dev/null
+++ b/grub-core/kern/ieee1275/mmap.c
@@ -0,0 +1,83 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2003,2004,2005,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/memory.h>
+#include <grub/ieee1275/ieee1275.h>
+#include <grub/types.h>
+
+grub_err_t
+grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
+{
+ grub_ieee1275_phandle_t root;
+ grub_ieee1275_phandle_t memory;
+ grub_uint32_t available[128];
+ grub_ssize_t available_size;
+ grub_uint32_t address_cells = 1;
+ grub_uint32_t size_cells = 1;
+ int i;
+
+ /* Determine the format of each entry in `available'. */
+ grub_ieee1275_finddevice ("/", &root);
+ grub_ieee1275_get_integer_property (root, "#address-cells", &address_cells,
+ sizeof address_cells, 0);
+ grub_ieee1275_get_integer_property (root, "#size-cells", &size_cells,
+ sizeof size_cells, 0);
+
+ if (size_cells > address_cells)
+ address_cells = size_cells;
+
+ /* Load `/memory/available'. */
+ if (grub_ieee1275_finddevice ("/memory", &memory))
+ return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
+ "couldn't find /memory node");
+ if (grub_ieee1275_get_integer_property (memory, "available", available,
+ sizeof available, &available_size))
+ return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
+ "couldn't examine /memory/available property");
+ if (available_size < 0 || (grub_size_t) available_size > sizeof (available))
+ return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
+ "/memory response buffer exceeded");
+
+ if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_BROKEN_ADDRESS_CELLS))
+ {
+ address_cells = 1;
+ size_cells = 1;
+ }
+
+ /* Decode each entry and call `hook'. */
+ i = 0;
+ available_size /= sizeof (grub_uint32_t);
+ while (i < available_size)
+ {
+ grub_uint64_t address;
+ grub_uint64_t size;
+
+ address = available[i++];
+ if (address_cells == 2)
+ address = (address << 32) | available[i++];
+
+ size = available[i++];
+ if (size_cells == 2)
+ size = (size << 32) | available[i++];
+
+ if (hook (address, size, GRUB_MEMORY_AVAILABLE, hook_data))
+ break;
+ }
+
+ return grub_errno;
+}
diff --git a/grub-core/kern/ieee1275/openfw.c b/grub-core/kern/ieee1275/openfw.c
new file mode 100644
index 0000000..4d493ab
--- /dev/null
+++ b/grub-core/kern/ieee1275/openfw.c
@@ -0,0 +1,593 @@
+/* openfw.c -- Open firmware support functions. */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2003,2004,2005,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/types.h>
+#include <grub/err.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/ieee1275/ieee1275.h>
+#include <grub/net.h>
+
+enum grub_ieee1275_parse_type
+{
+ GRUB_PARSE_FILENAME,
+ GRUB_PARSE_PARTITION,
+ GRUB_PARSE_DEVICE,
+ GRUB_PARSE_DEVICE_TYPE
+};
+
+static int
+fill_alias (struct grub_ieee1275_devalias *alias)
+{
+ grub_ssize_t actual;
+
+ if (grub_ieee1275_get_property (alias->phandle, "device_type", alias->type,
+ IEEE1275_MAX_PROP_LEN, &actual))
+ alias->type[0] = 0;
+
+ if (alias->parent_dev == alias->phandle)
+ return 0;
+
+ if (grub_ieee1275_package_to_path (alias->phandle, alias->path,
+ IEEE1275_MAX_PATH_LEN, &actual))
+ return 0;
+
+ if (grub_strcmp (alias->parent_path, alias->path) == 0)
+ return 0;
+
+ if (grub_ieee1275_get_property (alias->phandle, "name", alias->name,
+ IEEE1275_MAX_PROP_LEN, &actual))
+ return 0;
+ grub_dprintf ("devalias", "device path=%s\n", alias->path);
+ return 1;
+}
+
+void
+grub_ieee1275_devalias_free (struct grub_ieee1275_devalias *alias)
+{
+ grub_free (alias->name);
+ grub_free (alias->type);
+ grub_free (alias->path);
+ grub_free (alias->parent_path);
+ alias->name = 0;
+ alias->type = 0;
+ alias->path = 0;
+ alias->parent_path = 0;
+ alias->phandle = GRUB_IEEE1275_PHANDLE_INVALID;
+}
+
+void
+grub_ieee1275_children_peer (struct grub_ieee1275_devalias *alias)
+{
+ while (grub_ieee1275_peer (alias->phandle, &alias->phandle) != -1)
+ if (fill_alias (alias))
+ return;
+ grub_ieee1275_devalias_free (alias);
+}
+
+void
+grub_ieee1275_children_first (const char *devpath,
+ struct grub_ieee1275_devalias *alias)
+{
+ grub_ieee1275_phandle_t dev;
+
+ grub_dprintf ("devalias", "iterating children of %s\n",
+ devpath);
+
+ alias->name = 0;
+ alias->path = 0;
+ alias->parent_path = 0;
+ alias->type = 0;
+
+ if (grub_ieee1275_finddevice (devpath, &dev))
+ return;
+
+ if (grub_ieee1275_child (dev, &alias->phandle))
+ return;
+
+ alias->type = grub_malloc (IEEE1275_MAX_PROP_LEN);
+ if (!alias->type)
+ return;
+ alias->path = grub_malloc (IEEE1275_MAX_PATH_LEN);
+ if (!alias->path)
+ {
+ grub_free (alias->type);
+ return;
+ }
+ alias->parent_path = grub_strdup (devpath);
+ if (!alias->parent_path)
+ {
+ grub_free (alias->path);
+ grub_free (alias->type);
+ return;
+ }
+
+ alias->name = grub_malloc (IEEE1275_MAX_PROP_LEN);
+ if (!alias->name)
+ {
+ grub_free (alias->path);
+ grub_free (alias->type);
+ grub_free (alias->parent_path);
+ return;
+ }
+ if (!fill_alias (alias))
+ grub_ieee1275_children_peer (alias);
+}
+
+static int
+iterate_recursively (const char *path,
+ int (*hook) (struct grub_ieee1275_devalias *alias))
+{
+ struct grub_ieee1275_devalias alias;
+ int ret = 0;
+
+ FOR_IEEE1275_DEVCHILDREN(path, alias)
+ {
+ ret = hook (&alias);
+ if (ret)
+ break;
+ ret = iterate_recursively (alias.path, hook);
+ if (ret)
+ break;
+ }
+ grub_ieee1275_devalias_free (&alias);
+ return ret;
+}
+
+int
+grub_ieee1275_devices_iterate (int (*hook) (struct grub_ieee1275_devalias *alias))
+{
+ return iterate_recursively ("/", hook);
+}
+
+void
+grub_ieee1275_devalias_init_iterator (struct grub_ieee1275_devalias *alias)
+{
+ alias->name = 0;
+ alias->path = 0;
+ alias->parent_path = 0;
+ alias->type = 0;
+
+ grub_dprintf ("devalias", "iterating aliases\n");
+
+ if (grub_ieee1275_finddevice ("/aliases", &alias->parent_dev))
+ return;
+
+ alias->name = grub_malloc (IEEE1275_MAX_PROP_LEN);
+ if (!alias->name)
+ return;
+
+ alias->type = grub_malloc (IEEE1275_MAX_PROP_LEN);
+ if (!alias->type)
+ {
+ grub_free (alias->name);
+ alias->name = 0;
+ return;
+ }
+
+ alias->name[0] = '\0';
+}
+
+int
+grub_ieee1275_devalias_next (struct grub_ieee1275_devalias *alias)
+{
+ if (!alias->name)
+ return 0;
+ while (1)
+ {
+ grub_ssize_t pathlen;
+ grub_ssize_t actual;
+ char *tmp;
+
+ if (alias->path)
+ {
+ grub_free (alias->path);
+ alias->path = 0;
+ }
+ tmp = grub_strdup (alias->name);
+ if (grub_ieee1275_next_property (alias->parent_dev, tmp,
+ alias->name) <= 0)
+ {
+ grub_free (tmp);
+ grub_ieee1275_devalias_free (alias);
+ return 0;
+ }
+ grub_free (tmp);
+
+ grub_dprintf ("devalias", "devalias name = %s\n", alias->name);
+
+ grub_ieee1275_get_property_length (alias->parent_dev, alias->name, &pathlen);
+
+ /* The property `name' is a special case we should skip. */
+ if (grub_strcmp (alias->name, "name") == 0)
+ continue;
+
+ /* Sun's OpenBoot often doesn't zero terminate the device alias
+ strings, so we will add a NULL byte at the end explicitly. */
+ pathlen += 1;
+
+ alias->path = grub_malloc (pathlen + 1);
+ if (! alias->path)
+ {
+ grub_ieee1275_devalias_free (alias);
+ return 0;
+ }
+
+ if (grub_ieee1275_get_property (alias->parent_dev, alias->name, alias->path,
+ pathlen, &actual) || actual < 0)
+ {
+ grub_dprintf ("devalias", "get_property (%s) failed\n", alias->name);
+ grub_free (alias->path);
+ continue;
+ }
+ if (actual > pathlen)
+ actual = pathlen;
+ alias->path[actual] = '\0';
+ alias->path[pathlen] = '\0';
+
+ if (grub_ieee1275_finddevice (alias->path, &alias->phandle))
+ {
+ grub_dprintf ("devalias", "finddevice (%s) failed\n", alias->path);
+ grub_free (alias->path);
+ alias->path = 0;
+ continue;
+ }
+
+ if (grub_ieee1275_get_property (alias->phandle, "device_type", alias->type,
+ IEEE1275_MAX_PROP_LEN, &actual))
+ {
+ /* NAND device don't have device_type property. */
+ alias->type[0] = 0;
+ }
+ return 1;
+ }
+}
+
+/* Call the "map" method of /chosen/mmu. */
+int
+grub_ieee1275_map (grub_addr_t phys, grub_addr_t virt, grub_size_t size,
+ grub_uint32_t mode)
+{
+ struct map_args {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t method;
+ grub_ieee1275_cell_t ihandle;
+ grub_ieee1275_cell_t mode;
+ grub_ieee1275_cell_t size;
+ grub_ieee1275_cell_t virt;
+#ifdef __sparc__
+ grub_ieee1275_cell_t phys_high;
+#endif
+ grub_ieee1275_cell_t phys_low;
+ grub_ieee1275_cell_t catch_result;
+ } args;
+
+ INIT_IEEE1275_COMMON (&args.common, "call-method",
+#ifdef __sparc__
+ 7,
+#else
+ 6,
+#endif
+ 1);
+ args.method = (grub_ieee1275_cell_t) "map";
+ args.ihandle = grub_ieee1275_mmu;
+#ifdef __sparc__
+ args.phys_high = 0;
+#endif
+ args.phys_low = phys;
+ args.virt = virt;
+ args.size = size;
+ args.mode = mode; /* Format is WIMG0PP. */
+ args.catch_result = (grub_ieee1275_cell_t) -1;
+
+ if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
+ return -1;
+
+ return args.catch_result;
+}
+
+grub_err_t
+grub_claimmap (grub_addr_t addr, grub_size_t size)
+{
+ if (grub_ieee1275_claim (addr, size, 0, 0))
+ return -1;
+
+ if (! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_REAL_MODE)
+ && grub_ieee1275_map (addr, addr, size, 0x00))
+ {
+ grub_error (GRUB_ERR_OUT_OF_MEMORY, "map failed: address 0x%llx, size 0x%llx\n",
+ (long long) addr, (long long) size);
+ grub_ieee1275_release (addr, size);
+ return grub_errno;
+ }
+
+ return GRUB_ERR_NONE;
+}
+
+/* Get the device arguments of the Open Firmware node name `path'. */
+static char *
+grub_ieee1275_get_devargs (const char *path)
+{
+ char *colon = grub_strchr (path, ':');
+
+ if (! colon)
+ return 0;
+
+ return grub_strdup (colon + 1);
+}
+
+/* Get the device path of the Open Firmware node name `path'. */
+char *
+grub_ieee1275_get_devname (const char *path)
+{
+ char *colon = grub_strchr (path, ':');
+ int pathlen = grub_strlen (path);
+ struct grub_ieee1275_devalias curalias;
+ if (colon)
+ pathlen = (int)(colon - path);
+
+ /* Try to find an alias for this device. */
+ FOR_IEEE1275_DEVALIASES (curalias)
+ /* briQ firmware can change capitalization in /chosen/bootpath. */
+ if (grub_strncasecmp (curalias.path, path, pathlen) == 0
+ && curalias.path[pathlen] == 0)
+ {
+ char *newpath;
+ newpath = grub_strdup (curalias.name);
+ grub_ieee1275_devalias_free (&curalias);
+ return newpath;
+ }
+
+ return grub_strndup (path, pathlen);
+}
+
+static char *
+grub_ieee1275_parse_args (const char *path, enum grub_ieee1275_parse_type ptype)
+{
+ char type[64]; /* XXX check size. */
+ char *device = grub_ieee1275_get_devname (path);
+ char *ret = 0;
+ grub_ieee1275_phandle_t dev;
+
+ /* We need to know what type of device it is in order to parse the full
+ file path properly. */
+ if (grub_ieee1275_finddevice (device, &dev))
+ {
+ grub_error (GRUB_ERR_UNKNOWN_DEVICE, "device %s not found", device);
+ goto fail;
+ }
+ if (grub_ieee1275_get_property (dev, "device_type", &type, sizeof type, 0))
+ {
+ grub_error (GRUB_ERR_UNKNOWN_DEVICE,
+ "device %s lacks a device_type property", device);
+ goto fail;
+ }
+
+ switch (ptype)
+ {
+ case GRUB_PARSE_DEVICE:
+ ret = grub_strdup (device);
+ break;
+ case GRUB_PARSE_DEVICE_TYPE:
+ ret = grub_strdup (type);
+ break;
+ case GRUB_PARSE_FILENAME:
+ {
+ char *comma;
+ char *args;
+
+ args = grub_ieee1275_get_devargs (path);
+ if (!args)
+ /* Shouldn't happen. */
+ return 0;
+
+ /* The syntax of the device arguments is defined in the CHRP and PReP
+ IEEE1275 bindings: "[partition][,[filename]]". */
+ comma = grub_strchr (args, ',');
+
+ if (comma)
+ {
+ char *filepath = comma + 1;
+
+ /* Make sure filepath has leading backslash. */
+ if (filepath[0] != '\\')
+ ret = grub_xasprintf ("\\%s", filepath);
+ else
+ ret = grub_strdup (filepath);
+ }
+ grub_free (args);
+ }
+ break;
+ case GRUB_PARSE_PARTITION:
+ {
+ char *comma;
+ char *args;
+
+ if (grub_strcmp ("block", type) != 0)
+ goto unknown;
+
+ args = grub_ieee1275_get_devargs (path);
+ if (!args)
+ /* Shouldn't happen. */
+ return 0;
+
+ comma = grub_strchr (args, ',');
+ if (!comma)
+ ret = grub_strdup (args);
+ else
+ ret = grub_strndup (args, (grub_size_t)(comma - args));
+ /* Consistently provide numbered partitions to GRUB.
+ OpenBOOT traditionally uses alphabetical partition
+ specifiers. */
+ if (ret[0] >= 'a' && ret[0] <= 'z')
+ ret[0] = '1' + (ret[0] - 'a');
+ grub_free (args);
+ }
+ break;
+ default:
+ unknown:
+ grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
+ "unsupported type %s for device %s", type, device);
+ }
+
+fail:
+ grub_free (device);
+ return ret;
+}
+
+char *
+grub_ieee1275_get_device_type (const char *path)
+{
+ return grub_ieee1275_parse_args (path, GRUB_PARSE_DEVICE_TYPE);
+}
+
+char *
+grub_ieee1275_get_aliasdevname (const char *path)
+{
+ return grub_ieee1275_parse_args (path, GRUB_PARSE_DEVICE);
+}
+
+char *
+grub_ieee1275_get_filename (const char *path)
+{
+ return grub_ieee1275_parse_args (path, GRUB_PARSE_FILENAME);
+}
+
+/* Convert a device name from IEEE1275 syntax to GRUB syntax. */
+char *
+grub_ieee1275_encode_devname (const char *path)
+{
+ char *device = grub_ieee1275_get_devname (path);
+ char *partition;
+ char *encoding;
+ char *optr;
+ const char *iptr;
+
+ if (! device)
+ return 0;
+
+ encoding = grub_malloc (sizeof ("ieee1275/") + 2 * grub_strlen (device)
+ + sizeof (",XXXXXXXXXXXX"));
+ if (!encoding)
+ {
+ grub_free (device);
+ return 0;
+ }
+
+ partition = grub_ieee1275_parse_args (path, GRUB_PARSE_PARTITION);
+
+ optr = grub_stpcpy (encoding, "ieee1275/");
+ for (iptr = device; *iptr; )
+ {
+ if (*iptr == ',')
+ *optr++ ='\\';
+ *optr++ = *iptr++;
+ }
+ if (partition && partition[0])
+ {
+ unsigned int partno = grub_strtoul (partition, 0, 0);
+
+ *optr++ = ',';
+
+ if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_0_BASED_PARTITIONS))
+ /* GRUB partition 1 is OF partition 0. */
+ partno++;
+
+ grub_snprintf (optr, sizeof ("XXXXXXXXXXXX"), "%d", partno);
+ }
+ else
+ *optr = '\0';
+
+ grub_free (partition);
+ grub_free (device);
+
+ return encoding;
+}
+
+/* Resolve aliases. */
+char *
+grub_ieee1275_canonicalise_devname (const char *path)
+{
+ struct canon_args
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t path;
+ grub_ieee1275_cell_t buf;
+ grub_ieee1275_cell_t inlen;
+ grub_ieee1275_cell_t outlen;
+ }
+ args;
+ char *buf = NULL;
+ grub_size_t bufsize = 64;
+ int i;
+
+ for (i = 0; i < 2; i++)
+ {
+ grub_free (buf);
+
+ buf = grub_malloc (bufsize);
+ if (!buf)
+ return NULL;
+
+ INIT_IEEE1275_COMMON (&args.common, "canon", 3, 1);
+ args.path = (grub_ieee1275_cell_t) path;
+ args.buf = (grub_ieee1275_cell_t) buf;
+ args.inlen = (grub_ieee1275_cell_t) (bufsize - 1);
+
+ if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
+ return 0;
+ if (args.outlen > bufsize - 1)
+ {
+ bufsize = args.outlen + 2;
+ continue;
+ }
+ return buf;
+ }
+ /* Shouldn't reach here. */
+ grub_free (buf);
+ return NULL;
+}
+
+char *
+grub_ieee1275_get_boot_dev (void)
+{
+ char *bootpath;
+ grub_ssize_t bootpath_size;
+
+ if (grub_ieee1275_get_property_length (grub_ieee1275_chosen, "bootpath",
+ &bootpath_size)
+ || bootpath_size <= 0)
+ {
+ /* Should never happen. */
+ grub_printf ("/chosen/bootpath property missing!\n");
+ return NULL;
+ }
+
+ bootpath = (char *) grub_malloc ((grub_size_t) bootpath_size + 64);
+ if (! bootpath)
+ {
+ grub_print_error ();
+ return NULL;
+ }
+ grub_ieee1275_get_property (grub_ieee1275_chosen, "bootpath", bootpath,
+ (grub_size_t) bootpath_size + 1, 0);
+ bootpath[bootpath_size] = '\0';
+
+ return bootpath;
+}
diff --git a/grub-core/kern/list.c b/grub-core/kern/list.c
new file mode 100644
index 0000000..a256bb3
--- /dev/null
+++ b/grub-core/kern/list.c
@@ -0,0 +1,55 @@
+/* list.c - grub list function */
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/list.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+
+void *
+grub_named_list_find (grub_named_list_t head, const char *name)
+{
+ grub_named_list_t item;
+
+ FOR_LIST_ELEMENTS (item, head)
+ if (grub_strcmp (item->name, name) == 0)
+ return item;
+
+ return NULL;
+}
+
+void
+grub_list_push (grub_list_t *head, grub_list_t item)
+{
+ item->prev = head;
+ if (*head)
+ (*head)->prev = &item->next;
+ item->next = *head;
+ *head = item;
+}
+
+void
+grub_list_remove (grub_list_t item)
+{
+ if (item->prev)
+ *item->prev = item->next;
+ if (item->next)
+ item->next->prev = item->prev;
+ item->next = 0;
+ item->prev = 0;
+}
diff --git a/grub-core/kern/lockdown.c b/grub-core/kern/lockdown.c
new file mode 100644
index 0000000..0bc70fd
--- /dev/null
+++ b/grub-core/kern/lockdown.c
@@ -0,0 +1,84 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2020 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 <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <grub/dl.h>
+#include <grub/env.h>
+#include <grub/file.h>
+#include <grub/lockdown.h>
+#include <grub/verify.h>
+
+static int lockdown = GRUB_LOCKDOWN_DISABLED;
+
+static grub_err_t
+lockdown_verifier_init (grub_file_t io __attribute__ ((unused)),
+ enum grub_file_type type,
+ void **context __attribute__ ((unused)),
+ enum grub_verify_flags *flags)
+{
+ *flags = GRUB_VERIFY_FLAGS_SKIP_VERIFICATION;
+
+ switch (type & GRUB_FILE_TYPE_MASK)
+ {
+ case GRUB_FILE_TYPE_GRUB_MODULE:
+ case GRUB_FILE_TYPE_LINUX_KERNEL:
+ case GRUB_FILE_TYPE_MULTIBOOT_KERNEL:
+ case GRUB_FILE_TYPE_XEN_HYPERVISOR:
+ case GRUB_FILE_TYPE_BSD_KERNEL:
+ case GRUB_FILE_TYPE_XNU_KERNEL:
+ case GRUB_FILE_TYPE_PLAN9_KERNEL:
+ case GRUB_FILE_TYPE_NTLDR:
+ case GRUB_FILE_TYPE_TRUECRYPT:
+ case GRUB_FILE_TYPE_FREEDOS:
+ case GRUB_FILE_TYPE_PXECHAINLOADER:
+ case GRUB_FILE_TYPE_PCCHAINLOADER:
+ case GRUB_FILE_TYPE_COREBOOT_CHAINLOADER:
+ case GRUB_FILE_TYPE_EFI_CHAINLOADED_IMAGE:
+ case GRUB_FILE_TYPE_ACPI_TABLE:
+ case GRUB_FILE_TYPE_DEVICE_TREE_IMAGE:
+ *flags = GRUB_VERIFY_FLAGS_DEFER_AUTH;
+
+ /* Fall through. */
+
+ default:
+ return GRUB_ERR_NONE;
+ }
+}
+
+struct grub_file_verifier lockdown_verifier =
+ {
+ .name = "lockdown_verifier",
+ .init = lockdown_verifier_init,
+ };
+
+void
+grub_lockdown (void)
+{
+ lockdown = GRUB_LOCKDOWN_ENABLED;
+
+ grub_verifier_register (&lockdown_verifier);
+
+ grub_env_set ("lockdown", "y");
+ grub_env_export ("lockdown");
+}
+
+int
+grub_is_lockdown (void)
+{
+ return lockdown;
+}
diff --git a/grub-core/kern/main.c b/grub-core/kern/main.c
new file mode 100644
index 0000000..73967e2
--- /dev/null
+++ b/grub-core/kern/main.c
@@ -0,0 +1,316 @@
+/* main.c - the kernel main routine */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2002,2003,2005,2006,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/kernel.h>
+#include <grub/misc.h>
+#include <grub/symbol.h>
+#include <grub/dl.h>
+#include <grub/term.h>
+#include <grub/file.h>
+#include <grub/device.h>
+#include <grub/env.h>
+#include <grub/mm.h>
+#include <grub/command.h>
+#include <grub/reader.h>
+#include <grub/parser.h>
+#include <grub/verify.h>
+
+#ifdef GRUB_MACHINE_PCBIOS
+#include <grub/machine/memory.h>
+#endif
+
+grub_addr_t
+grub_modules_get_end (void)
+{
+ struct grub_module_info *modinfo;
+
+ modinfo = (struct grub_module_info *) grub_modbase;
+
+ /* Check if there are any modules. */
+ if ((modinfo == 0) || modinfo->magic != GRUB_MODULE_MAGIC)
+ return grub_modbase;
+
+ return grub_modbase + modinfo->size;
+}
+
+/* Load all modules in core. */
+static void
+grub_load_modules (void)
+{
+ struct grub_module_header *header;
+ FOR_MODULES (header)
+ {
+ /* Not an ELF module, skip. */
+ if (header->type != OBJ_TYPE_ELF)
+ continue;
+
+ if (! grub_dl_load_core ((char *) header + sizeof (struct grub_module_header),
+ (header->size - sizeof (struct grub_module_header))))
+ grub_fatal ("%s", grub_errmsg);
+
+ if (grub_errno)
+ grub_print_error ();
+ }
+}
+
+static char *load_config;
+
+static void
+grub_load_config (void)
+{
+ struct grub_module_header *header;
+ FOR_MODULES (header)
+ {
+ /* Not an embedded config, skip. */
+ if (header->type != OBJ_TYPE_CONFIG)
+ continue;
+
+ load_config = grub_malloc (header->size - sizeof (struct grub_module_header) + 1);
+ if (!load_config)
+ {
+ grub_print_error ();
+ break;
+ }
+ grub_memcpy (load_config, (char *) header +
+ sizeof (struct grub_module_header),
+ header->size - sizeof (struct grub_module_header));
+ load_config[header->size - sizeof (struct grub_module_header)] = 0;
+ break;
+ }
+}
+
+/* Write hook for the environment variables of root. Remove surrounding
+ parentheses, if any. */
+static char *
+grub_env_write_root (struct grub_env_var *var __attribute__ ((unused)),
+ const char *val)
+{
+ /* XXX Is it better to check the existence of the device? */
+ grub_size_t len = grub_strlen (val);
+
+ if (val[0] == '(' && val[len - 1] == ')')
+ return grub_strndup (val + 1, len - 2);
+
+ return grub_strdup (val);
+}
+
+static void
+grub_set_prefix_and_root (void)
+{
+ char *device = NULL;
+ char *path = NULL;
+ char *fwdevice = NULL;
+ char *fwpath = NULL;
+ char *prefix = NULL;
+ struct grub_module_header *header;
+
+ FOR_MODULES (header)
+ if (header->type == OBJ_TYPE_PREFIX)
+ prefix = (char *) header + sizeof (struct grub_module_header);
+
+ grub_register_variable_hook ("root", 0, grub_env_write_root);
+
+ grub_machine_get_bootlocation (&fwdevice, &fwpath);
+
+ if (fwdevice)
+ {
+ char *cmdpath;
+
+ cmdpath = grub_xasprintf ("(%s)%s", fwdevice, fwpath ? : "");
+ if (cmdpath)
+ {
+ grub_env_set ("cmdpath", cmdpath);
+ grub_env_export ("cmdpath");
+ grub_free (cmdpath);
+ }
+ }
+
+ if (prefix)
+ {
+ char *pptr = NULL;
+ if (prefix[0] == '(')
+ {
+ pptr = grub_strrchr (prefix, ')');
+ if (pptr)
+ {
+ device = grub_strndup (prefix + 1, pptr - prefix - 1);
+ pptr++;
+ }
+ }
+ if (!pptr)
+ pptr = prefix;
+ if (pptr[0])
+ path = grub_strdup (pptr);
+ }
+
+ if (!device && fwdevice)
+ device = fwdevice;
+ else if (fwdevice && (device[0] == ',' || !device[0]))
+ {
+ /* We have a partition, but still need to fill in the drive. */
+ char *comma, *new_device;
+
+ for (comma = fwdevice; *comma; )
+ {
+ if (comma[0] == '\\' && comma[1] == ',')
+ {
+ comma += 2;
+ continue;
+ }
+ if (*comma == ',')
+ break;
+ comma++;
+ }
+ if (*comma)
+ {
+ char *drive = grub_strndup (fwdevice, comma - fwdevice);
+ new_device = grub_xasprintf ("%s%s", drive, device);
+ grub_free (drive);
+ }
+ else
+ new_device = grub_xasprintf ("%s%s", fwdevice, device);
+
+ grub_free (fwdevice);
+ grub_free (device);
+ device = new_device;
+ }
+ else
+ grub_free (fwdevice);
+ if (fwpath && !path)
+ {
+ grub_size_t len = grub_strlen (fwpath);
+ while (len > 1 && fwpath[len - 1] == '/')
+ fwpath[--len] = 0;
+ if (len >= sizeof (GRUB_TARGET_CPU "-" GRUB_PLATFORM) - 1
+ && grub_memcmp (fwpath + len - (sizeof (GRUB_TARGET_CPU "-" GRUB_PLATFORM) - 1), GRUB_TARGET_CPU "-" GRUB_PLATFORM,
+ sizeof (GRUB_TARGET_CPU "-" GRUB_PLATFORM) - 1) == 0)
+ fwpath[len - (sizeof (GRUB_TARGET_CPU "-" GRUB_PLATFORM) - 1)] = 0;
+ path = fwpath;
+ }
+ else
+ grub_free (fwpath);
+ if (device)
+ {
+ char *prefix_set;
+
+ prefix_set = grub_xasprintf ("(%s)%s", device, path ? : "");
+ if (prefix_set)
+ {
+ grub_env_set ("prefix", prefix_set);
+ grub_free (prefix_set);
+ }
+ grub_env_set ("root", device);
+ }
+
+ grub_free (device);
+ grub_free (path);
+ grub_print_error ();
+}
+
+/* Load the normal mode module and execute the normal mode if possible. */
+static void
+grub_load_normal_mode (void)
+{
+ /* Load the module. */
+ grub_dl_load ("normal");
+
+ /* Print errors if any. */
+ grub_print_error ();
+ grub_errno = 0;
+
+ grub_command_execute ("normal", 0, 0);
+}
+
+static void
+reclaim_module_space (void)
+{
+ grub_addr_t modstart, modend;
+
+ if (!grub_modbase)
+ return;
+
+#ifdef GRUB_MACHINE_PCBIOS
+ modstart = GRUB_MEMORY_MACHINE_DECOMPRESSION_ADDR;
+#else
+ modstart = grub_modbase;
+#endif
+ modend = grub_modules_get_end ();
+ grub_modbase = 0;
+
+#if GRUB_KERNEL_PRELOAD_SPACE_REUSABLE
+ grub_mm_init_region ((void *) modstart, modend - modstart);
+#else
+ (void) modstart;
+ (void) modend;
+#endif
+}
+
+/* The main routine. */
+void __attribute__ ((noreturn))
+grub_main (void)
+{
+ /* First of all, initialize the machine. */
+ grub_machine_init ();
+
+ grub_boot_time ("After machine init.");
+
+ /* Hello. */
+ grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
+ grub_printf ("Welcome to GRUB!\n\n");
+ grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
+
+ /* Init verifiers API. */
+ grub_verifiers_init ();
+
+ grub_load_config ();
+
+ grub_boot_time ("Before loading embedded modules.");
+
+ /* Load pre-loaded modules and free the space. */
+ grub_register_exported_symbols ();
+#ifdef GRUB_LINKER_HAVE_INIT
+ grub_arch_dl_init_linker ();
+#endif
+ grub_load_modules ();
+
+ grub_boot_time ("After loading embedded modules.");
+
+ /* It is better to set the root device as soon as possible,
+ for convenience. */
+ grub_set_prefix_and_root ();
+ grub_env_export ("root");
+ grub_env_export ("prefix");
+
+ /* Reclaim space used for modules. */
+ reclaim_module_space ();
+
+ grub_boot_time ("After reclaiming module space.");
+
+ grub_register_core_commands ();
+
+ grub_boot_time ("Before execution of embedded config.");
+
+ if (load_config)
+ grub_parser_execute (load_config);
+
+ grub_boot_time ("After execution of embedded config. Attempt to go to normal mode");
+
+ grub_load_normal_mode ();
+ grub_rescue_run ();
+}
diff --git a/grub-core/kern/mips/arc/init.c b/grub-core/kern/mips/arc/init.c
new file mode 100644
index 0000000..2ed3ff3
--- /dev/null
+++ b/grub-core/kern/mips/arc/init.c
@@ -0,0 +1,463 @@
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/kernel.h>
+#include <grub/misc.h>
+#include <grub/env.h>
+#include <grub/time.h>
+#include <grub/types.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/time.h>
+#include <grub/machine/kernel.h>
+#include <grub/machine/memory.h>
+#include <grub/arc/console.h>
+#include <grub/cpu/memory.h>
+#include <grub/cpu/time.h>
+#include <grub/memory.h>
+#include <grub/term.h>
+#include <grub/arc/arc.h>
+#include <grub/offsets.h>
+#include <grub/i18n.h>
+#include <grub/disk.h>
+#include <grub/partition.h>
+
+const char *type_names[] = {
+#ifdef GRUB_CPU_WORDS_BIGENDIAN
+ NULL,
+#endif
+ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
+ "eisa", "tc", "scsi", "dti", "multi", "disk", "tape", "cdrom", "worm",
+ "serial", "net", "video", "par", "point", "key", "audio", "other",
+ "rdisk", "fdisk", "tape", "modem", "monitor", "print", "pointer",
+ "keyboard", "term",
+#ifndef GRUB_CPU_WORDS_BIGENDIAN
+ "other",
+#endif
+ "line", "network", NULL
+};
+
+static int
+iterate_rec (const char *prefix, const struct grub_arc_component *parent,
+ grub_arc_iterate_devs_hook_t hook, void *hook_data,
+ int alt_names)
+{
+ const struct grub_arc_component *comp;
+ FOR_ARC_CHILDREN(comp, parent)
+ {
+ char *name;
+ const char *cname = NULL;
+ if (comp->type < ARRAY_SIZE (type_names))
+ cname = type_names[comp->type];
+ if (!cname)
+ cname = "unknown";
+ if (alt_names)
+ name = grub_xasprintf ("%s/%s%lu", prefix, cname, comp->key);
+ else
+ name = grub_xasprintf ("%s%s(%lu)", prefix, cname, comp->key);
+ if (!name)
+ return 1;
+ if (hook (name, comp, hook_data))
+ {
+ grub_free (name);
+ return 1;
+ }
+ if (iterate_rec ((parent ? name : prefix), comp, hook, hook_data,
+ alt_names))
+ {
+ grub_free (name);
+ return 1;
+ }
+ grub_free (name);
+ }
+ return 0;
+}
+
+int
+grub_arc_iterate_devs (grub_arc_iterate_devs_hook_t hook, void *hook_data,
+ int alt_names)
+{
+ return iterate_rec ((alt_names ? "arc" : ""), NULL, hook, hook_data,
+ alt_names);
+}
+
+grub_err_t
+grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
+{
+ struct grub_arc_memory_descriptor *cur = NULL;
+ while (1)
+ {
+ grub_memory_type_t type;
+ cur = GRUB_ARC_FIRMWARE_VECTOR->getmemorydescriptor (cur);
+ if (!cur)
+ return GRUB_ERR_NONE;
+ switch (cur->type)
+ {
+ case GRUB_ARC_MEMORY_EXCEPTION_BLOCK:
+ case GRUB_ARC_MEMORY_SYSTEM_PARAMETER_BLOCK:
+ case GRUB_ARC_MEMORY_FW_PERMANENT:
+ default:
+ type = GRUB_MEMORY_RESERVED;
+ break;
+
+ case GRUB_ARC_MEMORY_FW_TEMPORARY:
+ case GRUB_ARC_MEMORY_FREE:
+ case GRUB_ARC_MEMORY_LOADED:
+ case GRUB_ARC_MEMORY_FREE_CONTIGUOUS:
+ type = GRUB_MEMORY_AVAILABLE;
+ break;
+ case GRUB_ARC_MEMORY_BADRAM:
+ type = GRUB_MEMORY_BADRAM;
+ break;
+ }
+ if (hook (((grub_uint64_t) cur->start_page) << 12,
+ ((grub_uint64_t) cur->num_pages) << 12, type, hook_data))
+ return GRUB_ERR_NONE;
+ }
+}
+
+char *
+grub_arc_alt_name_to_norm (const char *name, const char *suffix)
+{
+ char *optr;
+ const char *iptr;
+ char * ret = grub_malloc (2 * grub_strlen (name) + grub_strlen (suffix));
+ int state = 0;
+
+ if (!ret)
+ return NULL;
+ optr = ret;
+ for (iptr = name + 4; *iptr; iptr++)
+ if (state == 0)
+ {
+ if (!grub_isdigit (*iptr))
+ *optr++ = *iptr;
+ else
+ {
+ *optr++ = '(';
+ *optr++ = *iptr;
+ state = 1;
+ }
+ }
+ else
+ {
+ if (grub_isdigit (*iptr))
+ *optr++ = *iptr;
+ else
+ {
+ *optr++ = ')';
+ state = 0;
+ }
+ }
+ if (state)
+ *optr++ = ')';
+ grub_strcpy (optr, suffix);
+ return ret;
+}
+
+static char *
+norm_name_to_alt (const char *name)
+{
+ char *optr;
+ const char *iptr;
+ int state = 0;
+ char * ret = grub_malloc (grub_strlen (name) + sizeof ("arc/"));
+
+ if (!ret)
+ return NULL;
+ optr = grub_stpcpy (ret, "arc/");
+ for (iptr = name; *iptr; iptr++)
+ {
+ if (state == 3)
+ {
+ *optr++ = '/';
+ state = 0;
+ }
+ if (*iptr == '(')
+ {
+ state = 1;
+ continue;
+ }
+ if (*iptr == ')')
+ {
+ if (state == 1)
+ *optr++ = '0';
+ state = 3;
+ continue;
+ }
+ *optr++ = *iptr;
+ if (state == 1)
+ state = 2;
+ }
+ *optr = '\0';
+ return ret;
+}
+
+extern grub_uint32_t grub_total_modules_size __attribute__ ((section(".text")));
+grub_addr_t grub_modbase;
+
+extern char _end[];
+static char boot_location[256];
+
+void
+grub_machine_init (void)
+{
+ struct grub_arc_memory_descriptor *cur = NULL;
+ grub_addr_t modend;
+
+ grub_memcpy (boot_location,
+ (char *) (GRUB_DECOMPRESSOR_LINK_ADDR - 256), 256);
+
+ grub_modbase = ALIGN_UP ((grub_addr_t) _end, GRUB_KERNEL_MACHINE_MOD_ALIGN);
+ modend = grub_modbase + grub_total_modules_size;
+ grub_console_init_early ();
+
+ /* FIXME: measure this. */
+ grub_arch_cpuclock = 150000000;
+ grub_install_get_time_ms (grub_rtc_get_time_ms);
+
+ while (1)
+ {
+ grub_uint64_t start, end;
+ cur = GRUB_ARC_FIRMWARE_VECTOR->getmemorydescriptor (cur);
+ if (!cur)
+ break;
+ if (cur->type != GRUB_ARC_MEMORY_FREE
+ && cur->type != GRUB_ARC_MEMORY_LOADED
+ && cur->type != GRUB_ARC_MEMORY_FREE_CONTIGUOUS)
+ continue;
+ start = ((grub_uint64_t) cur->start_page) << 12;
+ end = ((grub_uint64_t) cur->num_pages) << 12;
+ end += start;
+ if ((grub_uint64_t) start < (modend & 0x1fffffff))
+ start = (modend & 0x1fffffff);
+ if ((grub_uint64_t) end > 0x20000000)
+ end = 0x20000000;
+ if (end > start)
+ grub_mm_init_region ((void *) (grub_addr_t) (start | 0x80000000),
+ end - start);
+ }
+
+ grub_console_init_lately ();
+
+ grub_arcdisk_init ();
+}
+
+void
+grub_machine_fini (int flags __attribute__ ((unused)))
+{
+}
+
+void
+grub_halt (void)
+{
+ GRUB_ARC_FIRMWARE_VECTOR->powerdown ();
+
+ grub_millisleep (1500);
+
+ grub_puts_ (N_("Shutdown failed"));
+ grub_refresh ();
+ while (1);
+}
+
+void
+grub_exit (void)
+{
+ GRUB_ARC_FIRMWARE_VECTOR->exit ();
+
+ grub_millisleep (1500);
+
+ grub_puts_ (N_("Exit failed"));
+ grub_refresh ();
+ while (1);
+}
+
+static char *
+get_part (char *dev)
+{
+ char *ptr;
+ if (!*dev)
+ return 0;
+ ptr = dev + grub_strlen (dev) - 1;
+ if (ptr == dev || *ptr != ')')
+ return 0;
+ ptr--;
+ while (grub_isdigit (*ptr) && ptr > dev)
+ ptr--;
+ if (*ptr != '(' || ptr == dev)
+ return 0;
+ ptr--;
+ if (ptr - dev < (int) sizeof ("partition") - 2)
+ return 0;
+ ptr -= sizeof ("partition") - 2;
+ if (grub_memcmp (ptr, "partition", sizeof ("partition") - 1) != 0)
+ return 0;
+ return ptr;
+}
+
+static grub_disk_addr_t
+get_partition_offset (char *part, grub_disk_addr_t *en)
+{
+ grub_arc_fileno_t handle;
+ grub_disk_addr_t ret = -1;
+ struct grub_arc_fileinfo info;
+ grub_arc_err_t r;
+
+ if (GRUB_ARC_FIRMWARE_VECTOR->open (part, GRUB_ARC_FILE_ACCESS_OPEN_RO,
+ &handle))
+ return -1;
+
+ r = GRUB_ARC_FIRMWARE_VECTOR->getfileinformation (handle, &info);
+ if (!r)
+ {
+ ret = (info.start >> 9);
+ *en = (info.end >> 9);
+ }
+ GRUB_ARC_FIRMWARE_VECTOR->close (handle);
+ return ret;
+}
+
+struct get_device_name_ctx
+{
+ char *partition_name;
+ grub_disk_addr_t poff, pend;
+};
+
+static int
+get_device_name_iter (grub_disk_t disk __attribute__ ((unused)),
+ const grub_partition_t part, void *data)
+{
+ struct get_device_name_ctx *ctx = data;
+
+ if (grub_partition_get_start (part) == ctx->poff
+ && grub_partition_get_len (part) == ctx->pend)
+ {
+ ctx->partition_name = grub_partition_get_name (part);
+ return 1;
+ }
+
+ return 0;
+}
+
+void
+grub_machine_get_bootlocation (char **device, char **path)
+{
+ char *loaddev = boot_location;
+ char *pptr, *partptr;
+ char *dname;
+ grub_disk_addr_t poff = -1, pend = -1;
+ struct get_device_name_ctx ctx;
+ grub_disk_t parent = 0;
+ unsigned i;
+
+ for (i = 0; i < ARRAY_SIZE (type_names); i++)
+ if (type_names[i]
+ && grub_memcmp (loaddev, type_names[i], grub_strlen (type_names[i])) == 0
+ && loaddev[grub_strlen (type_names[i])] == '(')
+ break;
+ if (i == ARRAY_SIZE (type_names))
+ pptr = loaddev;
+ else
+ for (pptr = loaddev; *pptr && *pptr != '/' && *pptr != '\\'; pptr++);
+ if (*pptr)
+ {
+ char *iptr, *optr;
+ char sep = *pptr;
+ *path = grub_malloc (grub_strlen (pptr) + 1);
+ if (!*path)
+ return;
+ for (iptr = pptr, optr = *path; *iptr; iptr++, optr++)
+ if (*iptr == sep)
+ *optr = '/';
+ else
+ *optr = *iptr;
+ *optr = '\0';
+ *path = grub_strdup (pptr);
+ *pptr = '\0';
+ }
+
+ if (*loaddev == '\0')
+ {
+ const char *syspart = 0;
+
+ if (GRUB_ARC_SYSTEM_PARAMETER_BLOCK->firmware_vector_length
+ >= (unsigned) ((char *) (&GRUB_ARC_FIRMWARE_VECTOR->getenvironmentvariable + 1)
+ - (char *) GRUB_ARC_FIRMWARE_VECTOR)
+ && GRUB_ARC_FIRMWARE_VECTOR->getenvironmentvariable)
+ syspart = GRUB_ARC_FIRMWARE_VECTOR->getenvironmentvariable ("SystemPartition");
+ if (!syspart)
+ return;
+ loaddev = grub_strdup (syspart);
+ }
+
+ partptr = get_part (loaddev);
+ if (partptr)
+ {
+ poff = get_partition_offset (loaddev, &pend);
+ *partptr = '\0';
+ }
+ dname = norm_name_to_alt (loaddev);
+ if (poff == (grub_addr_t) -1)
+ {
+ *device = dname;
+ if (loaddev != boot_location)
+ grub_free (loaddev);
+ return;
+ }
+
+ parent = grub_disk_open (dname);
+ if (!parent)
+ {
+ *device = dname;
+ if (loaddev != boot_location)
+ grub_free (loaddev);
+ return;
+ }
+
+ if (poff == 0
+ && pend == grub_disk_native_sectors (parent))
+ {
+ grub_disk_close (parent);
+ *device = dname;
+ if (loaddev != boot_location)
+ grub_free (loaddev);
+ return;
+ }
+
+ ctx.partition_name = NULL;
+ ctx.poff = poff;
+ ctx.pend = pend;
+
+ grub_partition_iterate (parent, get_device_name_iter, &ctx);
+ grub_disk_close (parent);
+
+ if (! ctx.partition_name)
+ {
+ *device = dname;
+ if (loaddev != boot_location)
+ grub_free (loaddev);
+ return;
+ }
+
+ *device = grub_xasprintf ("%s,%s", dname,
+ ctx.partition_name);
+ grub_free (ctx.partition_name);
+ grub_free (dname);
+ if (loaddev != boot_location)
+ grub_free (loaddev);
+}
diff --git a/grub-core/kern/mips/cache.S b/grub-core/kern/mips/cache.S
new file mode 100644
index 0000000..fa331ec
--- /dev/null
+++ b/grub-core/kern/mips/cache.S
@@ -0,0 +1,70 @@
+
+#include <grub/symbol.h>
+
+ .set noreorder
+ .set nomacro
+
+FUNCTION (grub_arch_sync_caches)
+#include "cache_flush.S"
+ j $ra
+ nop
+
+FUNCTION (grub_arch_sync_dma_caches)
+ move $t2, $a0
+ addu $t3, $a0, $a1
+ srl $t2, $t2, 5
+ sll $t2, $t2, 5
+ addu $t3, $t3, 0x1f
+ srl $t3, $t3, 5
+ sll $t3, $t3, 5
+ move $t0, $t2
+ subu $t1, $t3, $t2
+1:
+ cache_op 1, 0($t0)
+#ifdef GRUB_MACHINE_MIPS_LOONGSON
+ cache_op 1, 1($t0)
+ cache_op 1, 2($t0)
+ cache_op 1, 3($t0)
+
+ addiu $t1, $t1, -0x20
+ bne $t1, $zero, 1b
+ addiu $t0, $t0, 0x20
+#else
+ addiu $t1, $t1, -4
+ bne $t1, $zero, 1b
+ addiu $t0, $t0, 0x4
+#endif
+ sync_op
+ move $t0, $t2
+ subu $t1, $t3, $t2
+2:
+#ifdef GRUB_MACHINE_MIPS_LOONGSON
+ cache_op 0, 0($t0)
+ addiu $t1, $t1, -0x20
+ bne $t1, $zero, 2b
+ addiu $t0, $t0, 0x20
+#else
+ cache_op 0, 0($t0)
+ addiu $t1, $t1, -4
+ bne $t1, $zero, 2b
+ addiu $t0, $t0, 0x4
+#endif
+ sync_op
+ move $t0, $t2
+ subu $t1, $t3, $t2
+2:
+#ifdef GRUB_MACHINE_MIPS_LOONGSON
+ cache_op 23, 0($t0)
+ addiu $t1, $t1, -0x20
+ bne $t1, $zero, 2b
+ addiu $t0, $t0, 0x20
+#else
+ cache_op 23, 0($t0)
+ addiu $t1, $t1, -0x4
+ bne $t1, $zero, 2b
+ addiu $t0, $t0, 0x4
+#endif
+ sync_op
+
+ jr $ra
+ nop
diff --git a/grub-core/kern/mips/cache_flush.S b/grub-core/kern/mips/cache_flush.S
new file mode 100644
index 0000000..89961a0
--- /dev/null
+++ b/grub-core/kern/mips/cache_flush.S
@@ -0,0 +1,54 @@
+#ifndef CACHE_OP_DEFINED
+#define CACHE_OP_DEFINED 1
+ .macro cache_op op addr
+ .set mips3
+ cache \op, \addr
+ .set mips1
+ .endm
+ .macro sync_op
+ .set mips3
+ sync
+ .set mips1
+ .endm
+#endif
+
+ move $t2, $a0
+ addu $t3, $a0, $a1
+ srl $t2, $t2, 5
+ sll $t2, $t2, 5
+ addu $t3, $t3, 0x1f
+ srl $t3, $t3, 5
+ sll $t3, $t3, 5
+ move $t0, $t2
+ subu $t1, $t3, $t2
+1:
+ cache_op 1, 0($t0)
+ /* All four ways. */
+#ifdef GRUB_MACHINE_MIPS_LOONGSON
+ cache_op 1, 1($t0)
+ cache_op 1, 2($t0)
+ cache_op 1, 3($t0)
+ addiu $t1, $t1, -0x20
+ bne $t1, $zero, 1b
+ addiu $t0, $t0, 0x20
+
+#else
+ addiu $t1, $t1, -0x4
+ bne $t1, $zero, 1b
+ addiu $t0, $t0, 0x4
+#endif
+ sync_op
+ move $t0, $t2
+ subu $t1, $t3, $t2
+2:
+ cache_op 0, 0($t0)
+#ifdef GRUB_MACHINE_MIPS_LOONGSON
+ addiu $t1, $t1, -0x20
+ bne $t1, $zero, 2b
+ addiu $t0, $t0, 0x20
+#else
+ addiu $t1, $t1, -0x4
+ bne $t1, $zero, 2b
+ addiu $t0, $t0, 0x4
+#endif
+ sync_op
diff --git a/grub-core/kern/mips/dl.c b/grub-core/kern/mips/dl.c
new file mode 100644
index 0000000..5d7d299
--- /dev/null
+++ b/grub-core/kern/mips/dl.c
@@ -0,0 +1,274 @@
+/* dl-386.c - arch-dependent part of loadable module support */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2002,2005,2007,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/dl.h>
+#include <grub/elf.h>
+#include <grub/misc.h>
+#include <grub/err.h>
+#include <grub/cpu/types.h>
+#include <grub/mm.h>
+#include <grub/i18n.h>
+
+/* Dummy __gnu_local_gp. Resolved by linker. */
+static char __gnu_local_gp_dummy;
+static char _gp_disp_dummy;
+
+/* Check if EHDR is a valid ELF header. */
+grub_err_t
+grub_arch_dl_check_header (void *ehdr)
+{
+ Elf_Ehdr *e = ehdr;
+
+ /* Check the magic numbers. */
+#ifdef GRUB_CPU_WORDS_BIGENDIAN
+ if (e->e_ident[EI_CLASS] != ELFCLASS32
+ || e->e_ident[EI_DATA] != ELFDATA2MSB
+ || e->e_machine != EM_MIPS)
+#else
+ if (e->e_ident[EI_CLASS] != ELFCLASS32
+ || e->e_ident[EI_DATA] != ELFDATA2LSB
+ || e->e_machine != EM_MIPS)
+#endif
+ return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF magic"));
+
+ return GRUB_ERR_NONE;
+}
+
+#pragma GCC diagnostic ignored "-Wcast-align"
+
+grub_err_t
+grub_arch_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp,
+ grub_size_t *got)
+{
+ const Elf_Ehdr *e = ehdr;
+ const Elf_Shdr *s;
+ /* FIXME: suboptimal. */
+ grub_size_t gp_size = 0;
+ unsigned i;
+
+ *tramp = 0;
+ *got = 0;
+
+ for (i = 0, s = (const Elf_Shdr *) ((const char *) e + e->e_shoff);
+ i < e->e_shnum;
+ i++, s = (const Elf_Shdr *) ((const char *) s + e->e_shentsize))
+ if (s->sh_type == SHT_REL)
+ {
+ const Elf_Rel *rel, *max;
+
+ for (rel = (const Elf_Rel *) ((const char *) e + s->sh_offset),
+ max = rel + s->sh_size / s->sh_entsize;
+ rel < max;
+ rel++)
+ switch (ELF_R_TYPE (rel->r_info))
+ {
+ case R_MIPS_GOT16:
+ case R_MIPS_CALL16:
+ case R_MIPS_GPREL32:
+ gp_size += 4;
+ break;
+ }
+ }
+
+ if (gp_size > 0x08000)
+ return grub_error (GRUB_ERR_OUT_OF_RANGE, "__gnu_local_gp is too big\n");
+
+ *got = gp_size;
+
+ return GRUB_ERR_NONE;
+}
+
+/* Relocate symbols. */
+grub_err_t
+grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
+ Elf_Shdr *s, grub_dl_segment_t seg)
+{
+ grub_uint32_t gp0;
+ Elf_Ehdr *e = ehdr;
+
+ if (!mod->reginfo)
+ {
+ unsigned i;
+ Elf_Shdr *ri;
+
+ /* Find reginfo. */
+ for (i = 0, ri = (Elf_Shdr *) ((char *) ehdr + e->e_shoff);
+ i < e->e_shnum;
+ i++, ri = (Elf_Shdr *) ((char *) ri + e->e_shentsize))
+ if (ri->sh_type == SHT_MIPS_REGINFO)
+ break;
+ if (i == e->e_shnum)
+ return grub_error (GRUB_ERR_BAD_MODULE, "no reginfo found");
+ mod->reginfo = (grub_uint32_t *)((char *) ehdr + ri->sh_offset);
+ }
+
+ gp0 = mod->reginfo[5];
+ Elf_Rel *rel, *max;
+
+ for (rel = (Elf_Rel *) ((char *) e + s->sh_offset),
+ max = (Elf_Rel *) ((char *) rel + s->sh_size);
+ rel < max;
+ rel = (Elf_Rel *) ((char *) rel + s->sh_entsize))
+ {
+ grub_uint8_t *addr;
+ Elf_Sym *sym;
+ grub_uint32_t sym_value;
+
+ if (seg->size < rel->r_offset)
+ return grub_error (GRUB_ERR_BAD_MODULE,
+ "reloc offset is out of the segment");
+
+ addr = (grub_uint8_t *) ((char *) seg->addr + rel->r_offset);
+ sym = (Elf_Sym *) ((char *) mod->symtab
+ + mod->symsize * ELF_R_SYM (rel->r_info));
+ sym_value = sym->st_value;
+ if (s->sh_type == SHT_RELA)
+ {
+ sym_value += ((Elf_Rela *) rel)->r_addend;
+ }
+ if (sym_value == (grub_addr_t) &__gnu_local_gp_dummy)
+ sym_value = (grub_addr_t) mod->got;
+ else if (sym_value == (grub_addr_t) &_gp_disp_dummy)
+ {
+ sym_value = (grub_addr_t) mod->got - (grub_addr_t) addr;
+ if (ELF_R_TYPE (rel->r_info) == R_MIPS_LO16)
+ /* ABI mandates +4 even if partner lui doesn't
+ immediately precede addiu. */
+ sym_value += 4;
+ }
+ switch (ELF_R_TYPE (rel->r_info))
+ {
+ case R_MIPS_HI16:
+ {
+ grub_uint32_t value;
+ Elf_Rel *rel2;
+
+#ifdef GRUB_CPU_WORDS_BIGENDIAN
+ addr += 2;
+#endif
+
+ /* Handle partner lo16 relocation. Lower part is
+ treated as signed. Hence add 0x8000 to compensate.
+ */
+ value = (*(grub_uint16_t *) addr << 16)
+ + sym_value + 0x8000;
+ for (rel2 = rel + 1; rel2 < max; rel2++)
+ if (ELF_R_SYM (rel2->r_info)
+ == ELF_R_SYM (rel->r_info)
+ && ELF_R_TYPE (rel2->r_info) == R_MIPS_LO16)
+ {
+ value += *(grub_int16_t *)
+ ((char *) seg->addr + rel2->r_offset
+#ifdef GRUB_CPU_WORDS_BIGENDIAN
+ + 2
+#endif
+ );
+ break;
+ }
+ *(grub_uint16_t *) addr = (value >> 16) & 0xffff;
+ }
+ break;
+ case R_MIPS_LO16:
+#ifdef GRUB_CPU_WORDS_BIGENDIAN
+ addr += 2;
+#endif
+ *(grub_uint16_t *) addr += sym_value & 0xffff;
+ break;
+ case R_MIPS_32:
+ *(grub_uint32_t *) addr += sym_value;
+ break;
+ case R_MIPS_GPREL32:
+ *(grub_uint32_t *) addr = sym_value
+ + *(grub_uint32_t *) addr + gp0 - (grub_uint32_t)mod->got;
+ break;
+
+ case R_MIPS_26:
+ {
+ grub_uint32_t value;
+ grub_uint32_t raw;
+ raw = (*(grub_uint32_t *) addr) & 0x3ffffff;
+ value = raw << 2;
+ value += sym_value;
+ raw = (value >> 2) & 0x3ffffff;
+
+ *(grub_uint32_t *) addr =
+ raw | ((*(grub_uint32_t *) addr) & 0xfc000000);
+ }
+ break;
+ case R_MIPS_GOT16:
+ if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
+ {
+ Elf_Rel *rel2;
+ /* Handle partner lo16 relocation. Lower part is
+ treated as signed. Hence add 0x8000 to compensate.
+ */
+ sym_value += (*(grub_uint16_t *) addr << 16)
+ + 0x8000;
+ for (rel2 = rel + 1; rel2 < max; rel2++)
+ if (ELF_R_SYM (rel2->r_info)
+ == ELF_R_SYM (rel->r_info)
+ && ELF_R_TYPE (rel2->r_info) == R_MIPS_LO16)
+ {
+ sym_value += *(grub_int16_t *)
+ ((char *) seg->addr + rel2->r_offset
+#ifdef GRUB_CPU_WORDS_BIGENDIAN
+ + 2
+#endif
+ );
+ break;
+ }
+ sym_value &= 0xffff0000;
+ *(grub_uint16_t *) addr = 0;
+ }
+ /* Fallthrough. */
+ case R_MIPS_CALL16:
+ {
+ grub_uint32_t *gpptr = mod->gotptr;
+ /* FIXME: reuse*/
+#ifdef GRUB_CPU_WORDS_BIGENDIAN
+ addr += 2;
+#endif
+ *gpptr = sym_value + *(grub_uint16_t *) addr;
+ *(grub_uint16_t *) addr
+ = sizeof (grub_uint32_t) * (gpptr - (grub_uint32_t *) mod->got);
+ mod->gotptr = gpptr + 1;
+ break;
+ }
+ case R_MIPS_JALR:
+ break;
+ default:
+ {
+ return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
+ N_("relocation 0x%x is not implemented yet"),
+ ELF_R_TYPE (rel->r_info));
+ }
+ break;
+ }
+ }
+
+ return GRUB_ERR_NONE;
+}
+
+void
+grub_arch_dl_init_linker (void)
+{
+ grub_dl_register_symbol ("__gnu_local_gp", &__gnu_local_gp_dummy, 0, 0);
+ grub_dl_register_symbol ("_gp_disp", &_gp_disp_dummy, 0, 0);
+}
+
diff --git a/grub-core/kern/mips/init.c b/grub-core/kern/mips/init.c
new file mode 100644
index 0000000..14b8752
--- /dev/null
+++ b/grub-core/kern/mips/init.c
@@ -0,0 +1,38 @@
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/kernel.h>
+#include <grub/env.h>
+#include <grub/cpu/time.h>
+#include <grub/cpu/mips.h>
+
+/* FIXME: use interrupt to count high. */
+grub_uint64_t
+grub_get_rtc (void)
+{
+ static grub_uint32_t high = 0;
+ static grub_uint32_t last = 0;
+ grub_uint32_t low;
+
+ asm volatile ("mfc0 %0, " GRUB_CPU_MIPS_COP0_TIMER_COUNT : "=r" (low));
+ if (low < last)
+ high++;
+ last = low;
+
+ return (((grub_uint64_t) high) << 32) | low;
+}
diff --git a/grub-core/kern/mips/loongson/init.c b/grub-core/kern/mips/loongson/init.c
new file mode 100644
index 0000000..7b96531
--- /dev/null
+++ b/grub-core/kern/mips/loongson/init.c
@@ -0,0 +1,320 @@
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/kernel.h>
+#include <grub/misc.h>
+#include <grub/env.h>
+#include <grub/time.h>
+#include <grub/types.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/machine/time.h>
+#include <grub/machine/kernel.h>
+#include <grub/machine/memory.h>
+#include <grub/memory.h>
+#include <grub/mips/loongson.h>
+#include <grub/cs5536.h>
+#include <grub/term.h>
+#include <grub/cpu/memory.h>
+#include <grub/i18n.h>
+#include <grub/video.h>
+#include <grub/terminfo.h>
+#include <grub/keyboard_layouts.h>
+#include <grub/serial.h>
+#include <grub/loader.h>
+#include <grub/at_keyboard.h>
+
+grub_err_t
+grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
+{
+ hook (GRUB_ARCH_LOWMEMPSTART, grub_arch_memsize << 20,
+ GRUB_MEMORY_AVAILABLE, hook_data);
+ hook (GRUB_ARCH_HIGHMEMPSTART, grub_arch_highmemsize << 20,
+ GRUB_MEMORY_AVAILABLE, hook_data);
+ return GRUB_ERR_NONE;
+}
+
+/* Helper for init_pci. */
+static int
+set_card (grub_pci_device_t dev, grub_pci_id_t pciid,
+ void *data __attribute__ ((unused)))
+{
+ grub_pci_address_t addr;
+ /* We could use grub_pci_assign_addresses for this but we prefer to
+ have exactly same memory map as on pmon. */
+ switch (pciid)
+ {
+ case GRUB_LOONGSON_OHCI_PCIID:
+ addr = grub_pci_make_address (dev, GRUB_PCI_REG_ADDRESS_REG0);
+ grub_pci_write (addr, 0x5025000);
+ addr = grub_pci_make_address (dev, GRUB_PCI_REG_COMMAND);
+ grub_pci_write_word (addr, GRUB_PCI_COMMAND_SERR_ENABLE
+ | GRUB_PCI_COMMAND_PARITY_ERROR
+ | GRUB_PCI_COMMAND_BUS_MASTER
+ | GRUB_PCI_COMMAND_MEM_ENABLED);
+
+ addr = grub_pci_make_address (dev, GRUB_PCI_REG_STATUS);
+ grub_pci_write_word (addr, 0x0200 | GRUB_PCI_STATUS_CAPABILITIES);
+ break;
+ case GRUB_LOONGSON_EHCI_PCIID:
+ addr = grub_pci_make_address (dev, GRUB_PCI_REG_ADDRESS_REG0);
+ grub_pci_write (addr, 0x5026000);
+ addr = grub_pci_make_address (dev, GRUB_PCI_REG_COMMAND);
+ grub_pci_write_word (addr, GRUB_PCI_COMMAND_SERR_ENABLE
+ | GRUB_PCI_COMMAND_PARITY_ERROR
+ | GRUB_PCI_COMMAND_BUS_MASTER
+ | GRUB_PCI_COMMAND_MEM_ENABLED);
+
+ addr = grub_pci_make_address (dev, GRUB_PCI_REG_STATUS);
+ grub_pci_write_word (addr, (1 << GRUB_PCI_STATUS_DEVSEL_TIMING_SHIFT)
+ | GRUB_PCI_STATUS_CAPABILITIES);
+ break;
+ }
+ return 0;
+}
+
+static void
+init_pci (void)
+{
+ *((volatile grub_uint32_t *) GRUB_CPU_LOONGSON_PCI_HIT1_SEL_LO) = 0x8000000c;
+ *((volatile grub_uint32_t *) GRUB_CPU_LOONGSON_PCI_HIT1_SEL_HI) = 0xffffffff;
+
+ /* Setup PCI controller. */
+ *((volatile grub_uint16_t *) (GRUB_MACHINE_PCI_CONTROLLER_HEADER
+ + GRUB_PCI_REG_COMMAND))
+ = GRUB_PCI_COMMAND_PARITY_ERROR | GRUB_PCI_COMMAND_BUS_MASTER
+ | GRUB_PCI_COMMAND_MEM_ENABLED;
+ *((volatile grub_uint16_t *) (GRUB_MACHINE_PCI_CONTROLLER_HEADER
+ + GRUB_PCI_REG_STATUS))
+ = (1 << GRUB_PCI_STATUS_DEVSEL_TIMING_SHIFT)
+ | GRUB_PCI_STATUS_FAST_B2B_CAPABLE | GRUB_PCI_STATUS_66MHZ_CAPABLE
+ | GRUB_PCI_STATUS_CAPABILITIES;
+
+ *((volatile grub_uint32_t *) (GRUB_MACHINE_PCI_CONTROLLER_HEADER
+ + GRUB_PCI_REG_CACHELINE)) = 0xff;
+ *((volatile grub_uint32_t *) (GRUB_MACHINE_PCI_CONTROLLER_HEADER
+ + GRUB_PCI_REG_ADDRESS_REG0))
+ = 0x80000000 | GRUB_PCI_ADDR_MEM_TYPE_64 | GRUB_PCI_ADDR_MEM_PREFETCH;
+ *((volatile grub_uint32_t *) (GRUB_MACHINE_PCI_CONTROLLER_HEADER
+ + GRUB_PCI_REG_ADDRESS_REG1)) = 0;
+
+ grub_pci_iterate (set_card, NULL);
+}
+
+void
+grub_machine_init (void)
+{
+ grub_addr_t modend;
+ grub_uint32_t prid;
+
+ asm volatile ("mfc0 %0, " GRUB_CPU_LOONGSON_COP0_PRID : "=r" (prid));
+
+ switch (prid)
+ {
+ /* Loongson 2E. */
+ case 0x6302:
+ grub_arch_machine = GRUB_ARCH_MACHINE_FULOONG2E;
+ grub_bonito_type = GRUB_BONITO_2F;
+ break;
+ /* Loongson 2F. */
+ case 0x6303:
+ if (grub_arch_machine != GRUB_ARCH_MACHINE_FULOONG2F
+ && grub_arch_machine != GRUB_ARCH_MACHINE_YEELOONG)
+ grub_arch_machine = GRUB_ARCH_MACHINE_YEELOONG;
+ grub_bonito_type = GRUB_BONITO_2F;
+ break;
+ /* Loongson 3A. */
+ case 0x6305:
+ grub_arch_machine = GRUB_ARCH_MACHINE_YEELOONG_3A;
+ grub_bonito_type = GRUB_BONITO_3A;
+ break;
+ }
+
+ /* FIXME: measure this. */
+ if (grub_arch_busclock == 0)
+ {
+ grub_arch_busclock = 66000000;
+ grub_arch_cpuclock = 797000000;
+ }
+
+ grub_install_get_time_ms (grub_rtc_get_time_ms);
+
+ if (grub_arch_memsize == 0)
+ {
+ grub_port_t smbbase;
+ grub_err_t err;
+ grub_pci_device_t dev;
+ struct grub_smbus_spd spd;
+ unsigned totalmem;
+ int i;
+
+ if (!grub_cs5536_find (&dev))
+ grub_fatal ("No CS5536 found\n");
+
+ err = grub_cs5536_init_smbus (dev, 0x7ff, &smbbase);
+ if (err)
+ grub_fatal ("Couldn't init SMBus: %s\n", grub_errmsg);
+
+ /* Yeeloong and Fuloong have only one memory slot. */
+ err = grub_cs5536_read_spd (smbbase, GRUB_SMB_RAM_START_ADDR, &spd);
+ if (err)
+ grub_fatal ("Couldn't read SPD: %s\n", grub_errmsg);
+ for (i = 5; i < 13; i++)
+ if (spd.ddr2.rank_capacity & (1 << (i & 7)))
+ break;
+ /* Something is wrong. */
+ if (i == 13)
+ totalmem = 256;
+ else
+ totalmem = ((spd.ddr2.num_of_ranks
+ & GRUB_SMBUS_SPD_MEMORY_NUM_OF_RANKS_MASK) + 1) << (i + 2);
+
+ if (totalmem >= 256)
+ {
+ grub_arch_memsize = 256;
+ grub_arch_highmemsize = totalmem - 256;
+ }
+ else
+ {
+ grub_arch_memsize = totalmem;
+ grub_arch_highmemsize = 0;
+ }
+
+ grub_cs5536_init_geode (dev);
+
+ init_pci ();
+ }
+
+ modend = grub_modules_get_end ();
+ grub_mm_init_region ((void *) modend, (grub_arch_memsize << 20)
+ - (modend - GRUB_ARCH_LOWMEMVSTART));
+ /* FIXME: use upper memory as well. */
+
+ /* Initialize output terminal (can't be done earlier, as gfxterm
+ relies on a working heap. */
+ grub_video_sm712_init ();
+ grub_video_sis315pro_init ();
+ grub_video_radeon_fuloong2e_init ();
+ grub_video_radeon_yeeloong3a_init ();
+ grub_font_init ();
+ grub_gfxterm_init ();
+
+ grub_keylayouts_init ();
+ if (grub_arch_machine == GRUB_ARCH_MACHINE_YEELOONG
+ || grub_arch_machine == GRUB_ARCH_MACHINE_YEELOONG_3A)
+ grub_at_keyboard_init ();
+
+ grub_terminfo_init ();
+ grub_serial_init ();
+
+ grub_boot_init ();
+}
+
+void
+grub_machine_fini (int flags __attribute__ ((unused)))
+{
+}
+
+static int
+halt_via (grub_pci_device_t dev, grub_pci_id_t pciid,
+ void *data __attribute__ ((unused)))
+{
+ grub_uint16_t pm;
+ grub_pci_address_t addr;
+
+ if (pciid != 0x30571106)
+ return 0;
+
+ addr = grub_pci_make_address (dev, 0x40);
+ pm = grub_pci_read (addr) & ~1;
+
+ if (pm == 0)
+ {
+ grub_pci_write (addr, 0x1801);
+ pm = 0x1800;
+ }
+
+ addr = grub_pci_make_address (dev, 0x80);
+ grub_pci_write_byte (addr, 0xff);
+
+ addr = grub_pci_make_address (dev, GRUB_PCI_REG_COMMAND);
+ grub_pci_write_word (addr, grub_pci_read_word (addr) | GRUB_PCI_COMMAND_IO_ENABLED);
+
+ /* FIXME: This one is derived from qemu. Check on real hardware. */
+ grub_outw (0x2000, pm + 4 + GRUB_MACHINE_PCI_IO_BASE);
+ grub_millisleep (5000);
+
+ return 0;
+}
+
+void
+grub_halt (void)
+{
+ switch (grub_arch_machine)
+ {
+ case GRUB_ARCH_MACHINE_FULOONG2E:
+ grub_pci_iterate (halt_via, NULL);
+ break;
+ case GRUB_ARCH_MACHINE_FULOONG2F:
+ {
+ grub_pci_device_t dev;
+ grub_port_t p;
+ if (grub_cs5536_find (&dev))
+ {
+ p = (grub_cs5536_read_msr (dev, GRUB_CS5536_MSR_GPIO_BAR)
+ & GRUB_CS5536_LBAR_ADDR_MASK) + GRUB_MACHINE_PCI_IO_BASE;
+ grub_outl ((1 << 13), p + 4);
+ grub_outl ((1 << 29), p);
+ grub_millisleep (5000);
+ }
+ }
+ break;
+ case GRUB_ARCH_MACHINE_YEELOONG:
+ grub_outb (grub_inb (GRUB_CPU_LOONGSON_GPIOCFG)
+ & ~GRUB_CPU_YEELOONG_SHUTDOWN_GPIO, GRUB_CPU_LOONGSON_GPIOCFG);
+ grub_millisleep (1500);
+ break;
+ case GRUB_ARCH_MACHINE_YEELOONG_3A:
+ grub_millisleep (1);
+ grub_outb (0x4e, GRUB_MACHINE_PCI_IO_BASE_3A | 0x66);
+ grub_millisleep (1);
+ grub_outb (2, GRUB_MACHINE_PCI_IO_BASE_3A | 0x62);
+ grub_millisleep (5000);
+ break;
+ }
+
+ grub_puts_ (N_("Shutdown failed"));
+ grub_refresh ();
+ while (1);
+}
+
+void
+grub_exit (void)
+{
+ grub_halt ();
+}
+
+void
+grub_machine_get_bootlocation (char **device __attribute__ ((unused)),
+ char **path __attribute__ ((unused)))
+{
+}
+
+extern char _end[];
+grub_addr_t grub_modbase = (grub_addr_t) _end;
+
diff --git a/grub-core/kern/mips/qemu_mips/init.c b/grub-core/kern/mips/qemu_mips/init.c
new file mode 100644
index 0000000..be88b77
--- /dev/null
+++ b/grub-core/kern/mips/qemu_mips/init.c
@@ -0,0 +1,105 @@
+#include <grub/kernel.h>
+#include <grub/misc.h>
+#include <grub/env.h>
+#include <grub/time.h>
+#include <grub/types.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/time.h>
+#include <grub/machine/memory.h>
+#include <grub/machine/kernel.h>
+#include <grub/machine/console.h>
+#include <grub/cpu/memory.h>
+#include <grub/memory.h>
+#include <grub/video.h>
+#include <grub/terminfo.h>
+#include <grub/keyboard_layouts.h>
+#include <grub/serial.h>
+#include <grub/loader.h>
+#include <grub/at_keyboard.h>
+
+static inline int
+probe_mem (grub_addr_t addr)
+{
+ volatile grub_uint8_t *ptr = (grub_uint8_t *) (0xa0000000 | addr);
+ grub_uint8_t c = *ptr;
+ *ptr = 0xAA;
+ if (*ptr != 0xAA)
+ return 0;
+ *ptr = 0x55;
+ if (*ptr != 0x55)
+ return 0;
+ *ptr = c;
+ return 1;
+}
+
+void
+grub_machine_init (void)
+{
+ grub_addr_t modend;
+
+ if (grub_arch_memsize == 0)
+ {
+ int i;
+
+ for (i = 27; i >= 0; i--)
+ if (probe_mem (grub_arch_memsize | (1 << i)))
+ grub_arch_memsize |= (1 << i);
+ grub_arch_memsize++;
+ }
+
+ /* FIXME: measure this. */
+ grub_arch_cpuclock = 200000000;
+
+ modend = grub_modules_get_end ();
+ grub_mm_init_region ((void *) modend, grub_arch_memsize
+ - (modend - GRUB_ARCH_LOWMEMVSTART));
+
+ grub_install_get_time_ms (grub_rtc_get_time_ms);
+
+ grub_keylayouts_init ();
+ grub_at_keyboard_init ();
+
+ grub_qemu_init_cirrus ();
+ grub_vga_text_init ();
+
+ grub_terminfo_init ();
+ grub_serial_init ();
+
+ grub_boot_init ();
+}
+
+void
+grub_machine_fini (int flags __attribute__ ((unused)))
+{
+}
+
+void
+grub_exit (void)
+{
+ grub_halt ();
+}
+
+void
+grub_halt (void)
+{
+ grub_outl (42, 0xbfbf0004);
+ while (1);
+}
+
+grub_err_t
+grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
+{
+ hook (0, grub_arch_memsize, GRUB_MEMORY_AVAILABLE, hook_data);
+ return GRUB_ERR_NONE;
+}
+
+void
+grub_machine_get_bootlocation (char **device __attribute__ ((unused)),
+ char **path __attribute__ ((unused)))
+{
+}
+
+extern char _end[];
+grub_addr_t grub_modbase = (grub_addr_t) _end;
+
diff --git a/grub-core/kern/mips/startup.S b/grub-core/kern/mips/startup.S
new file mode 100644
index 0000000..1fdb58a
--- /dev/null
+++ b/grub-core/kern/mips/startup.S
@@ -0,0 +1,126 @@
+/* startup.S - Startup code for the MIPS. */
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/symbol.h>
+#include <grub/offsets.h>
+#include <grub/machine/memory.h>
+#include <grub/machine/kernel.h>
+#include <grub/offsets.h>
+#include <grub/mips/asm.h>
+
+#define BASE_ADDR 8
+
+ .globl __start, _start, start
+ .set noreorder
+ .set nomacro
+__start:
+_start:
+start:
+.extern __bss_start
+.extern _end
+ bal cont
+ nop
+
+ .org GRUB_KERNEL_MACHINE_TOTAL_MODULE_SIZE
+VARIABLE(grub_total_modules_size)
+ .long 0
+
+VARIABLE (grub_arch_busclock)
+ .long 0
+VARIABLE (grub_arch_cpuclock)
+ .long 0
+VARIABLE (grub_arch_memsize)
+ .long 0
+VARIABLE (grub_arch_highmemsize)
+ .long 0
+#ifdef GRUB_MACHINE_MIPS_LOONGSON
+VARIABLE (grub_arch_machine)
+ .long GRUB_ARCH_MACHINE_FULOONG2F
+#endif
+cont:
+ /* Save our base. */
+ move $s0, $ra
+
+#ifdef GRUB_MACHINE_MIPS_QEMU_MIPS
+ lui $t1, %hi(grub_arch_busclock)
+ addiu $t1, %lo(grub_arch_busclock)
+ sw $s4, 8($t1)
+#endif
+
+#ifdef GRUB_MACHINE_MIPS_LOONGSON
+ lui $t1, %hi(grub_arch_busclock)
+ addiu $t1, %lo(grub_arch_busclock)
+ sw $s2, 0($t1)
+ sw $s3, 4($t1)
+ sw $s4, 8($t1)
+ sw $s5, 12($t1)
+ sw $s7, 16($t1)
+#endif
+
+ /* Move the modules out of BSS. */
+ lui $t2, %hi(__bss_start)
+ addiu $t2, %lo(__bss_start)
+
+ lui $t1, %hi(_end)
+ addiu $t1, %lo(_end)
+ addiu $t1, (GRUB_KERNEL_MACHINE_MOD_ALIGN - 1)
+ li $t3, (GRUB_KERNEL_MACHINE_MOD_ALIGN - 1)
+ nor $t3, $t3, $0
+ and $t1, $t1, $t3
+
+ lw $t3, (GRUB_KERNEL_MACHINE_TOTAL_MODULE_SIZE - BASE_ADDR)($s0)
+
+ /* Backward copy. */
+ add $t1, $t1, $t3
+ add $t2, $t2, $t3
+ addiu $t1, $t1, -1
+ addiu $t2, $t2, -1
+
+ /* $t2 is source. $t1 is destination. $t3 is size. */
+modulesmovcont:
+ beq $t3, $0, modulesmovdone
+ nop
+ lb GRUB_ASM_T4, 0($t2)
+ sb GRUB_ASM_T4, 0($t1)
+ addiu $t2, $t2, -1
+ addiu $t1, $t1, -1
+ b modulesmovcont
+ addiu $t3, $t3, -1
+modulesmovdone:
+
+ /* Clean BSS. */
+
+ lui $t1, %hi(__bss_start)
+ addiu $t1, $t1, %lo(__bss_start)
+ lui $t2, %hi(_end)
+ addiu $t2, $t2, %lo(_end)
+bsscont:
+ sb $0,0($t1)
+ addiu $t1, $t1, 1
+ sltu $t3, $t1, $t2
+ bne $t3, $0, bsscont
+ nop
+
+ lui $t9, %hi(grub_main)
+ addiu $t9, %lo(grub_main)
+
+ lui $sp, %hi(GRUB_MACHINE_MEMORY_STACK_HIGH)
+ jr $t9
+ addiu $sp, $sp, %lo(GRUB_MACHINE_MEMORY_STACK_HIGH)
+
diff --git a/grub-core/kern/misc.c b/grub-core/kern/misc.c
new file mode 100644
index 0000000..3af336e
--- /dev/null
+++ b/grub-core/kern/misc.c
@@ -0,0 +1,1267 @@
+/* misc.c - definitions of misc functions */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/misc.h>
+#include <grub/err.h>
+#include <grub/mm.h>
+#include <stdarg.h>
+#include <grub/term.h>
+#include <grub/env.h>
+#include <grub/i18n.h>
+
+union printf_arg
+{
+ /* Yes, type is also part of union as the moment we fill the value
+ we don't need to store its type anymore (when we'll need it, we'll
+ have format spec again. So save some space. */
+ enum
+ {
+ INT, LONG, LONGLONG,
+ UNSIGNED_INT = 3, UNSIGNED_LONG, UNSIGNED_LONGLONG,
+ STRING
+ } type;
+ long long ll;
+};
+
+struct printf_args
+{
+ union printf_arg prealloc[32];
+ union printf_arg *ptr;
+ grub_size_t count;
+};
+
+static void
+parse_printf_args (const char *fmt0, struct printf_args *args,
+ va_list args_in);
+static int
+grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt0,
+ struct printf_args *args);
+
+static void
+free_printf_args (struct printf_args *args)
+{
+ if (args->ptr != args->prealloc)
+ grub_free (args->ptr);
+}
+
+static int
+grub_iswordseparator (int c)
+{
+ return (grub_isspace (c) || c == ',' || c == ';' || c == '|' || c == '&');
+}
+
+/* grub_gettext_dummy is not translating anything. */
+static const char *
+grub_gettext_dummy (const char *s)
+{
+ return s;
+}
+
+const char* (*grub_gettext) (const char *s) = grub_gettext_dummy;
+
+void *
+grub_memmove (void *dest, const void *src, grub_size_t n)
+{
+ char *d = (char *) dest;
+ const char *s = (const char *) src;
+
+ if (d < s)
+ while (n--)
+ *d++ = *s++;
+ else
+ {
+ d += n;
+ s += n;
+
+ while (n--)
+ *--d = *--s;
+ }
+
+ return dest;
+}
+
+char *
+grub_strcpy (char *dest, const char *src)
+{
+ char *p = dest;
+
+ while ((*p++ = *src++) != '\0')
+ ;
+
+ return dest;
+}
+
+int
+grub_printf (const char *fmt, ...)
+{
+ va_list ap;
+ int ret;
+
+ va_start (ap, fmt);
+ ret = grub_vprintf (fmt, ap);
+ va_end (ap);
+
+ return ret;
+}
+
+int
+grub_printf_ (const char *fmt, ...)
+{
+ va_list ap;
+ int ret;
+
+ va_start (ap, fmt);
+ ret = grub_vprintf (_(fmt), ap);
+ va_end (ap);
+
+ return ret;
+}
+
+int
+grub_puts_ (const char *s)
+{
+ return grub_puts (_(s));
+}
+
+#if defined (__APPLE__) && ! defined (GRUB_UTIL)
+int
+grub_err_printf (const char *fmt, ...)
+{
+ va_list ap;
+ int ret;
+
+ va_start (ap, fmt);
+ ret = grub_vprintf (fmt, ap);
+ va_end (ap);
+
+ return ret;
+}
+#endif
+
+#if ! defined (__APPLE__) && ! defined (GRUB_UTIL)
+int grub_err_printf (const char *fmt, ...)
+__attribute__ ((alias("grub_printf")));
+#endif
+
+int
+grub_debug_enabled (const char * condition)
+{
+ const char *debug;
+
+ debug = grub_env_get ("debug");
+ if (!debug)
+ return 0;
+
+ if (grub_strword (debug, "all") || grub_strword (debug, condition))
+ return 1;
+
+ return 0;
+}
+
+void
+grub_real_dprintf (const char *file, const int line, const char *condition,
+ const char *fmt, ...)
+{
+ va_list args;
+
+ if (grub_debug_enabled (condition))
+ {
+ grub_printf ("%s:%d: ", file, line);
+ va_start (args, fmt);
+ grub_vprintf (fmt, args);
+ va_end (args);
+ grub_refresh ();
+ }
+}
+
+#define PREALLOC_SIZE 255
+
+int
+grub_vprintf (const char *fmt, va_list ap)
+{
+ grub_size_t s;
+ static char buf[PREALLOC_SIZE + 1];
+ char *curbuf = buf;
+ struct printf_args args;
+
+ parse_printf_args (fmt, &args, ap);
+
+ s = grub_vsnprintf_real (buf, PREALLOC_SIZE, fmt, &args);
+ if (s > PREALLOC_SIZE)
+ {
+ curbuf = grub_malloc (s + 1);
+ if (!curbuf)
+ {
+ grub_errno = GRUB_ERR_NONE;
+ buf[PREALLOC_SIZE - 3] = '.';
+ buf[PREALLOC_SIZE - 2] = '.';
+ buf[PREALLOC_SIZE - 1] = '.';
+ buf[PREALLOC_SIZE] = 0;
+ curbuf = buf;
+ }
+ else
+ s = grub_vsnprintf_real (curbuf, s, fmt, &args);
+ }
+
+ free_printf_args (&args);
+
+ grub_xputs (curbuf);
+
+ if (curbuf != buf)
+ grub_free (curbuf);
+
+ return s;
+}
+
+int
+grub_memcmp (const void *s1, const void *s2, grub_size_t n)
+{
+ const grub_uint8_t *t1 = s1;
+ const grub_uint8_t *t2 = s2;
+
+ while (n--)
+ {
+ if (*t1 != *t2)
+ return (int) *t1 - (int) *t2;
+
+ t1++;
+ t2++;
+ }
+
+ return 0;
+}
+
+int
+grub_strcmp (const char *s1, const char *s2)
+{
+ while (*s1 && *s2)
+ {
+ if (*s1 != *s2)
+ break;
+
+ s1++;
+ s2++;
+ }
+
+ return (int) (grub_uint8_t) *s1 - (int) (grub_uint8_t) *s2;
+}
+
+int
+grub_strncmp (const char *s1, const char *s2, grub_size_t n)
+{
+ if (n == 0)
+ return 0;
+
+ while (*s1 && *s2 && --n)
+ {
+ if (*s1 != *s2)
+ break;
+
+ s1++;
+ s2++;
+ }
+
+ return (int) (grub_uint8_t) *s1 - (int) (grub_uint8_t) *s2;
+}
+
+char *
+grub_strchr (const char *s, int c)
+{
+ do
+ {
+ if (*s == c)
+ return (char *) s;
+ }
+ while (*s++);
+
+ return 0;
+}
+
+char *
+grub_strrchr (const char *s, int c)
+{
+ char *p = NULL;
+
+ do
+ {
+ if (*s == c)
+ p = (char *) s;
+ }
+ while (*s++);
+
+ return p;
+}
+
+int
+grub_strword (const char *haystack, const char *needle)
+{
+ const char *n_pos = needle;
+
+ while (grub_iswordseparator (*haystack))
+ haystack++;
+
+ while (*haystack)
+ {
+ /* Crawl both the needle and the haystack word we're on. */
+ while(*haystack && !grub_iswordseparator (*haystack)
+ && *haystack == *n_pos)
+ {
+ haystack++;
+ n_pos++;
+ }
+
+ /* If we reached the end of both words at the same time, the word
+ is found. If not, eat everything in the haystack that isn't the
+ next word (or the end of string) and "reset" the needle. */
+ if ( (!*haystack || grub_iswordseparator (*haystack))
+ && (!*n_pos || grub_iswordseparator (*n_pos)))
+ return 1;
+ else
+ {
+ n_pos = needle;
+ while (*haystack && !grub_iswordseparator (*haystack))
+ haystack++;
+ while (grub_iswordseparator (*haystack))
+ haystack++;
+ }
+ }
+
+ return 0;
+}
+
+int
+grub_isspace (int c)
+{
+ return (c == '\n' || c == '\r' || c == ' ' || c == '\t');
+}
+
+unsigned long
+grub_strtoul (const char * restrict str, const char ** const restrict end,
+ int base)
+{
+ unsigned long long num;
+
+ num = grub_strtoull (str, end, base);
+#if GRUB_CPU_SIZEOF_LONG != 8
+ if (num > ~0UL)
+ {
+ grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
+ return ~0UL;
+ }
+#endif
+
+ return (unsigned long) num;
+}
+
+unsigned long long
+grub_strtoull (const char * restrict str, const char ** const restrict end,
+ int base)
+{
+ unsigned long long num = 0;
+ int found = 0;
+
+ /* Skip white spaces. */
+ /* grub_isspace checks that *str != '\0'. */
+ while (grub_isspace (*str))
+ str++;
+
+ /* Guess the base, if not specified. The prefix `0x' means 16, and
+ the prefix `0' means 8. */
+ if (str[0] == '0')
+ {
+ if (str[1] == 'x')
+ {
+ if (base == 0 || base == 16)
+ {
+ base = 16;
+ str += 2;
+ }
+ }
+ else if (base == 0 && str[1] >= '0' && str[1] <= '7')
+ base = 8;
+ }
+
+ if (base == 0)
+ base = 10;
+
+ while (*str)
+ {
+ unsigned long digit;
+
+ digit = grub_tolower (*str) - '0';
+ if (digit >= 'a' - '0')
+ digit += '0' - 'a' + 10;
+ else if (digit > 9)
+ break;
+
+ if (digit >= (unsigned long) base)
+ break;
+
+ found = 1;
+
+ /* NUM * BASE + DIGIT > ~0ULL */
+ if (num > grub_divmod64 (~0ULL - digit, base, 0))
+ {
+ grub_error (GRUB_ERR_OUT_OF_RANGE,
+ N_("overflow is detected"));
+
+ if (end)
+ *end = (char *) str;
+
+ return ~0ULL;
+ }
+
+ num = num * base + digit;
+ str++;
+ }
+
+ if (! found)
+ {
+ grub_error (GRUB_ERR_BAD_NUMBER,
+ N_("unrecognized number"));
+
+ if (end)
+ *end = (char *) str;
+
+ return 0;
+ }
+
+ if (end)
+ *end = (char *) str;
+
+ return num;
+}
+
+char *
+grub_strdup (const char *s)
+{
+ grub_size_t len;
+ char *p;
+
+ len = grub_strlen (s) + 1;
+ p = (char *) grub_malloc (len);
+ if (! p)
+ return 0;
+
+ return grub_memcpy (p, s, len);
+}
+
+char *
+grub_strndup (const char *s, grub_size_t n)
+{
+ grub_size_t len;
+ char *p;
+
+ len = grub_strlen (s);
+ if (len > n)
+ len = n;
+ p = (char *) grub_malloc (len + 1);
+ if (! p)
+ return 0;
+
+ grub_memcpy (p, s, len);
+ p[len] = '\0';
+ return p;
+}
+
+/* clang detects that we're implementing here a memset so it decides to
+ optimise and calls memset resulting in infinite recursion. With volatile
+ we make it not optimise in this way. */
+#ifdef __clang__
+#define VOLATILE_CLANG volatile
+#else
+#define VOLATILE_CLANG
+#endif
+
+void *
+grub_memset (void *s, int c, grub_size_t len)
+{
+ void *p = s;
+ grub_uint8_t pattern8 = c;
+
+ if (len >= 3 * sizeof (unsigned long))
+ {
+ unsigned long patternl = 0;
+ grub_size_t i;
+
+ for (i = 0; i < sizeof (unsigned long); i++)
+ patternl |= ((unsigned long) pattern8) << (8 * i);
+
+ while (len > 0 && (((grub_addr_t) p) & (sizeof (unsigned long) - 1)))
+ {
+ *(VOLATILE_CLANG grub_uint8_t *) p = pattern8;
+ p = (grub_uint8_t *) p + 1;
+ len--;
+ }
+ while (len >= sizeof (unsigned long))
+ {
+ *(VOLATILE_CLANG unsigned long *) p = patternl;
+ p = (unsigned long *) p + 1;
+ len -= sizeof (unsigned long);
+ }
+ }
+
+ while (len > 0)
+ {
+ *(VOLATILE_CLANG grub_uint8_t *) p = pattern8;
+ p = (grub_uint8_t *) p + 1;
+ len--;
+ }
+
+ return s;
+}
+
+grub_size_t
+grub_strlen (const char *s)
+{
+ const char *p = s;
+
+ while (*p)
+ p++;
+
+ return p - s;
+}
+
+static inline void
+grub_reverse (char *str)
+{
+ char *p = str + grub_strlen (str) - 1;
+
+ while (str < p)
+ {
+ char tmp;
+
+ tmp = *str;
+ *str = *p;
+ *p = tmp;
+ str++;
+ p--;
+ }
+}
+
+/* Divide N by D, return the quotient, and store the remainder in *R. */
+grub_uint64_t
+grub_divmod64 (grub_uint64_t n, grub_uint64_t d, grub_uint64_t *r)
+{
+ /* This algorithm is typically implemented by hardware. The idea
+ is to get the highest bit in N, 64 times, by keeping
+ upper(N * 2^i) = (Q * D + M), where upper
+ represents the high 64 bits in 128-bits space. */
+ unsigned bits = 64;
+ grub_uint64_t q = 0;
+ grub_uint64_t m = 0;
+
+ /* ARM and IA64 don't have a fast 32-bit division.
+ Using that code would just make us use software division routines, calling
+ ourselves indirectly and hence getting infinite recursion.
+ */
+#if !GRUB_DIVISION_IN_SOFTWARE
+ /* Skip the slow computation if 32-bit arithmetic is possible. */
+ if (n < 0xffffffff && d < 0xffffffff)
+ {
+ if (r)
+ *r = ((grub_uint32_t) n) % (grub_uint32_t) d;
+
+ return ((grub_uint32_t) n) / (grub_uint32_t) d;
+ }
+#endif
+
+ while (bits--)
+ {
+ m <<= 1;
+
+ if (n & (1ULL << 63))
+ m |= 1;
+
+ q <<= 1;
+ n <<= 1;
+
+ if (m >= d)
+ {
+ q |= 1;
+ m -= d;
+ }
+ }
+
+ if (r)
+ *r = m;
+
+ return q;
+}
+
+/* Convert a long long value to a string. This function avoids 64-bit
+ modular arithmetic or divisions. */
+static inline char *
+grub_lltoa (char *str, int c, unsigned long long n)
+{
+ unsigned base = ((c == 'x') || (c == 'X')) ? 16 : 10;
+ char *p;
+
+ if ((long long) n < 0 && c == 'd')
+ {
+ n = (unsigned long long) (-((long long) n));
+ *str++ = '-';
+ }
+
+ p = str;
+
+ if (base == 16)
+ do
+ {
+ unsigned d = (unsigned) (n & 0xf);
+ *p++ = (d > 9) ? d + ((c == 'x') ? 'a' : 'A') - 10 : d + '0';
+ }
+ while (n >>= 4);
+ else
+ /* BASE == 10 */
+ do
+ {
+ grub_uint64_t m;
+
+ n = grub_divmod64 (n, 10, &m);
+ *p++ = m + '0';
+ }
+ while (n);
+
+ *p = 0;
+
+ grub_reverse (str);
+ return p;
+}
+
+/*
+ * Parse printf() fmt0 string into args arguments.
+ *
+ * The parsed arguments are either used by a printf() function to format the fmt0
+ * string or they are used to compare a format string from an untrusted source
+ * against a format string with expected arguments.
+ *
+ * When the fmt_check is set to !0, e.g. 1, then this function is executed in
+ * printf() format check mode. This enforces stricter rules for parsing the
+ * fmt0 to limit exposure to possible errors in printf() handling. It also
+ * disables positional parameters, "$", because some formats, e.g "%s%1$d",
+ * cannot be validated with the current implementation.
+ *
+ * The max_args allows to set a maximum number of accepted arguments. If the fmt0
+ * string defines more arguments than the max_args then the parse_printf_arg_fmt()
+ * function returns an error. This is currently used for format check only.
+ */
+static grub_err_t
+parse_printf_arg_fmt (const char *fmt0, struct printf_args *args,
+ int fmt_check, grub_size_t max_args)
+{
+ const char *fmt;
+ char c;
+ grub_size_t n = 0;
+
+ args->count = 0;
+
+ COMPILE_TIME_ASSERT (sizeof (int) == sizeof (grub_uint32_t));
+ COMPILE_TIME_ASSERT (sizeof (int) <= sizeof (long long));
+ COMPILE_TIME_ASSERT (sizeof (long) <= sizeof (long long));
+ COMPILE_TIME_ASSERT (sizeof (long long) == sizeof (void *)
+ || sizeof (int) == sizeof (void *));
+
+ fmt = fmt0;
+ while ((c = *fmt++) != 0)
+ {
+ if (c != '%')
+ continue;
+
+ if (*fmt =='-')
+ fmt++;
+
+ while (grub_isdigit (*fmt))
+ fmt++;
+
+ if (*fmt == '$')
+ {
+ if (fmt_check)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT,
+ "positional arguments are not supported");
+ fmt++;
+ }
+
+ if (*fmt =='-')
+ fmt++;
+
+ while (grub_isdigit (*fmt))
+ fmt++;
+
+ if (*fmt =='.')
+ fmt++;
+
+ while (grub_isdigit (*fmt))
+ fmt++;
+
+ c = *fmt++;
+ if (c == 'l')
+ c = *fmt++;
+ if (c == 'l')
+ c = *fmt++;
+
+ switch (c)
+ {
+ case 'p':
+ case 'x':
+ case 'X':
+ case 'u':
+ case 'd':
+ case 'c':
+ case 'C':
+ case 's':
+ args->count++;
+ break;
+ case '%':
+ /* "%%" is the escape sequence to output "%". */
+ break;
+ default:
+ if (fmt_check)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "unexpected format");
+ break;
+ }
+ }
+
+ if (fmt_check && args->count > max_args)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "too many arguments");
+
+ if (args->count <= ARRAY_SIZE (args->prealloc))
+ args->ptr = args->prealloc;
+ else
+ {
+ args->ptr = grub_calloc (args->count, sizeof (args->ptr[0]));
+ if (!args->ptr)
+ {
+ if (fmt_check)
+ return grub_errno;
+
+ grub_errno = GRUB_ERR_NONE;
+ args->ptr = args->prealloc;
+ args->count = ARRAY_SIZE (args->prealloc);
+ }
+ }
+
+ grub_memset (args->ptr, 0, args->count * sizeof (args->ptr[0]));
+
+ fmt = fmt0;
+ n = 0;
+ while ((c = *fmt++) != 0)
+ {
+ int longfmt = 0;
+ grub_size_t curn;
+ const char *p;
+
+ if (c != '%')
+ continue;
+
+ curn = n++;
+
+ if (*fmt =='-')
+ fmt++;
+
+ p = fmt;
+
+ while (grub_isdigit (*fmt))
+ fmt++;
+
+ if (*fmt == '$')
+ {
+ curn = grub_strtoull (p, 0, 10) - 1;
+ fmt++;
+ }
+
+ if (*fmt =='-')
+ fmt++;
+
+ while (grub_isdigit (*fmt))
+ fmt++;
+
+ if (*fmt =='.')
+ fmt++;
+
+ while (grub_isdigit (*fmt))
+ fmt++;
+
+ c = *fmt++;
+ if (c == '%')
+ {
+ n--;
+ continue;
+ }
+
+ if (c == 'l')
+ {
+ c = *fmt++;
+ longfmt = 1;
+ }
+ if (c == 'l')
+ {
+ c = *fmt++;
+ longfmt = 2;
+ }
+ if (curn >= args->count)
+ continue;
+ switch (c)
+ {
+ case 'x':
+ case 'X':
+ case 'u':
+ args->ptr[curn].type = UNSIGNED_INT + longfmt;
+ break;
+ case 'd':
+ args->ptr[curn].type = INT + longfmt;
+ break;
+ case 'p':
+ if (sizeof (void *) == sizeof (long long))
+ args->ptr[curn].type = UNSIGNED_LONGLONG;
+ else
+ args->ptr[curn].type = UNSIGNED_INT;
+ break;
+ case 's':
+ args->ptr[curn].type = STRING;
+ break;
+ case 'C':
+ case 'c':
+ args->ptr[curn].type = INT;
+ break;
+ }
+ }
+
+ return GRUB_ERR_NONE;
+}
+
+static void
+parse_printf_args (const char *fmt0, struct printf_args *args, va_list args_in)
+{
+ grub_size_t n;
+
+ parse_printf_arg_fmt (fmt0, args, 0, 0);
+
+ for (n = 0; n < args->count; n++)
+ switch (args->ptr[n].type)
+ {
+ case INT:
+ args->ptr[n].ll = va_arg (args_in, int);
+ break;
+ case LONG:
+ args->ptr[n].ll = va_arg (args_in, long);
+ break;
+ case UNSIGNED_INT:
+ args->ptr[n].ll = va_arg (args_in, unsigned int);
+ break;
+ case UNSIGNED_LONG:
+ args->ptr[n].ll = va_arg (args_in, unsigned long);
+ break;
+ case LONGLONG:
+ case UNSIGNED_LONGLONG:
+ args->ptr[n].ll = va_arg (args_in, long long);
+ break;
+ case STRING:
+ if (sizeof (void *) == sizeof (long long))
+ args->ptr[n].ll = va_arg (args_in, long long);
+ else
+ args->ptr[n].ll = va_arg (args_in, unsigned int);
+ break;
+ }
+}
+
+static inline void __attribute__ ((always_inline))
+write_char (char *str, grub_size_t *count, grub_size_t max_len, unsigned char ch)
+{
+ if (*count < max_len)
+ str[*count] = ch;
+
+ (*count)++;
+}
+
+static int
+grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt0,
+ struct printf_args *args)
+{
+ char c;
+ grub_size_t n = 0;
+ grub_size_t count = 0;
+ const char *fmt;
+
+ fmt = fmt0;
+
+ while ((c = *fmt++) != 0)
+ {
+ unsigned int format1 = 0;
+ unsigned int format2 = ~ 0U;
+ char zerofill = ' ';
+ char rightfill = 0;
+ grub_size_t curn;
+
+ if (c != '%')
+ {
+ write_char (str, &count, max_len,c);
+ continue;
+ }
+
+ curn = n++;
+
+ rescan:;
+
+ if (*fmt =='-')
+ {
+ rightfill = 1;
+ fmt++;
+ }
+
+ /* Read formatting parameters. */
+ if (grub_isdigit (*fmt))
+ {
+ if (fmt[0] == '0')
+ zerofill = '0';
+ format1 = grub_strtoul (fmt, &fmt, 10);
+ }
+
+ if (*fmt == '.')
+ fmt++;
+
+ if (grub_isdigit (*fmt))
+ format2 = grub_strtoul (fmt, &fmt, 10);
+
+ if (*fmt == '$')
+ {
+ curn = format1 - 1;
+ fmt++;
+ format1 = 0;
+ format2 = ~ 0U;
+ zerofill = ' ';
+ rightfill = 0;
+
+ goto rescan;
+ }
+
+ c = *fmt++;
+ if (c == 'l')
+ c = *fmt++;
+ if (c == 'l')
+ c = *fmt++;
+
+ if (c == '%')
+ {
+ write_char (str, &count, max_len,c);
+ n--;
+ continue;
+ }
+
+ if (curn >= args->count)
+ continue;
+
+ long long curarg = args->ptr[curn].ll;
+
+ switch (c)
+ {
+ case 'p':
+ write_char (str, &count, max_len, '0');
+ write_char (str, &count, max_len, 'x');
+ c = 'x';
+ /* Fall through. */
+ case 'x':
+ case 'X':
+ case 'u':
+ case 'd':
+ {
+ char tmp[32];
+ const char *p = tmp;
+ grub_size_t len;
+ grub_size_t fill;
+
+ len = grub_lltoa (tmp, c, curarg) - tmp;
+ fill = len < format1 ? format1 - len : 0;
+ if (! rightfill)
+ while (fill--)
+ write_char (str, &count, max_len, zerofill);
+ while (*p)
+ write_char (str, &count, max_len, *p++);
+ if (rightfill)
+ while (fill--)
+ write_char (str, &count, max_len, zerofill);
+ }
+ break;
+
+ case 'c':
+ write_char (str, &count, max_len,curarg & 0xff);
+ break;
+
+ case 'C':
+ {
+ grub_uint32_t code = curarg;
+ int shift;
+ unsigned mask;
+
+ if (code <= 0x7f)
+ {
+ shift = 0;
+ mask = 0;
+ }
+ else if (code <= 0x7ff)
+ {
+ shift = 6;
+ mask = 0xc0;
+ }
+ else if (code <= 0xffff)
+ {
+ shift = 12;
+ mask = 0xe0;
+ }
+ else if (code <= 0x10ffff)
+ {
+ shift = 18;
+ mask = 0xf0;
+ }
+ else
+ {
+ code = '?';
+ shift = 0;
+ mask = 0;
+ }
+
+ write_char (str, &count, max_len,mask | (code >> shift));
+
+ for (shift -= 6; shift >= 0; shift -= 6)
+ write_char (str, &count, max_len,0x80 | (0x3f & (code >> shift)));
+ }
+ break;
+
+ case 's':
+ {
+ grub_size_t len = 0;
+ grub_size_t fill;
+ const char *p = ((char *) (grub_addr_t) curarg) ? : "(null)";
+ grub_size_t i;
+
+ while (len < format2 && p[len])
+ len++;
+
+ fill = len < format1 ? format1 - len : 0;
+
+ if (!rightfill)
+ while (fill--)
+ write_char (str, &count, max_len, zerofill);
+
+ for (i = 0; i < len; i++)
+ write_char (str, &count, max_len,*p++);
+
+ if (rightfill)
+ while (fill--)
+ write_char (str, &count, max_len, zerofill);
+ }
+
+ break;
+
+ default:
+ write_char (str, &count, max_len,c);
+ break;
+ }
+ }
+
+ if (count < max_len)
+ str[count] = '\0';
+ else
+ str[max_len] = '\0';
+ return count;
+}
+
+int
+grub_vsnprintf (char *str, grub_size_t n, const char *fmt, va_list ap)
+{
+ grub_size_t ret;
+ struct printf_args args;
+
+ if (!n)
+ return 0;
+
+ n--;
+
+ parse_printf_args (fmt, &args, ap);
+
+ ret = grub_vsnprintf_real (str, n, fmt, &args);
+
+ free_printf_args (&args);
+
+ return ret < n ? ret : n;
+}
+
+int
+grub_snprintf (char *str, grub_size_t n, const char *fmt, ...)
+{
+ va_list ap;
+ int ret;
+
+ va_start (ap, fmt);
+ ret = grub_vsnprintf (str, n, fmt, ap);
+ va_end (ap);
+
+ return ret;
+}
+
+char *
+grub_xvasprintf (const char *fmt, va_list ap)
+{
+ grub_size_t s, as = PREALLOC_SIZE;
+ char *ret;
+ struct printf_args args;
+
+ parse_printf_args (fmt, &args, ap);
+
+ while (1)
+ {
+ ret = grub_malloc (as + 1);
+ if (!ret)
+ {
+ free_printf_args (&args);
+ return NULL;
+ }
+
+ s = grub_vsnprintf_real (ret, as, fmt, &args);
+
+ if (s <= as)
+ {
+ free_printf_args (&args);
+ return ret;
+ }
+
+ grub_free (ret);
+ as = s;
+ }
+}
+
+char *
+grub_xasprintf (const char *fmt, ...)
+{
+ va_list ap;
+ char *ret;
+
+ va_start (ap, fmt);
+ ret = grub_xvasprintf (fmt, ap);
+ va_end (ap);
+
+ return ret;
+}
+
+grub_err_t
+grub_printf_fmt_check (const char *fmt, const char *fmt_expected)
+{
+ struct printf_args args_expected, args_fmt;
+ grub_err_t ret;
+ grub_size_t n;
+
+ if (fmt == NULL || fmt_expected == NULL)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid format");
+
+ ret = parse_printf_arg_fmt (fmt_expected, &args_expected, 1, GRUB_SIZE_MAX);
+ if (ret != GRUB_ERR_NONE)
+ return ret;
+
+ /* Limit parsing to the number of expected arguments. */
+ ret = parse_printf_arg_fmt (fmt, &args_fmt, 1, args_expected.count);
+ if (ret != GRUB_ERR_NONE)
+ {
+ free_printf_args (&args_expected);
+ return ret;
+ }
+
+ for (n = 0; n < args_fmt.count; n++)
+ if (args_fmt.ptr[n].type != args_expected.ptr[n].type)
+ {
+ ret = grub_error (GRUB_ERR_BAD_ARGUMENT, "arguments types do not match");
+ break;
+ }
+
+ free_printf_args (&args_expected);
+ free_printf_args (&args_fmt);
+
+ return ret;
+}
+
+
+/* Abort GRUB. This function does not return. */
+static void __attribute__ ((noreturn))
+grub_abort (void)
+{
+ grub_printf ("\nAborted.");
+
+#ifndef GRUB_UTIL
+ if (grub_term_inputs)
+#endif
+ {
+ grub_printf (" Press any key to exit.");
+ grub_getkey ();
+ }
+
+ grub_exit ();
+}
+
+void
+grub_fatal (const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start (ap, fmt);
+ grub_vprintf (_(fmt), ap);
+ va_end (ap);
+
+ grub_refresh ();
+
+ grub_abort ();
+}
+
+#if BOOT_TIME_STATS
+
+#include <grub/time.h>
+
+struct grub_boot_time *grub_boot_time_head;
+static struct grub_boot_time **boot_time_last = &grub_boot_time_head;
+
+void
+grub_real_boot_time (const char *file,
+ const int line,
+ const char *fmt, ...)
+{
+ struct grub_boot_time *n;
+ va_list args;
+
+ grub_error_push ();
+ n = grub_malloc (sizeof (*n));
+ if (!n)
+ {
+ grub_errno = 0;
+ grub_error_pop ();
+ return;
+ }
+ n->file = file;
+ n->line = line;
+ n->tp = grub_get_time_ms ();
+ n->next = 0;
+
+ va_start (args, fmt);
+ n->msg = grub_xvasprintf (fmt, args);
+ va_end (args);
+
+ *boot_time_last = n;
+ boot_time_last = &n->next;
+
+ grub_errno = 0;
+ grub_error_pop ();
+}
+#endif
diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c
new file mode 100644
index 0000000..c070afc
--- /dev/null
+++ b/grub-core/kern/mm.c
@@ -0,0 +1,664 @@
+/* mm.c - functions for memory manager */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2002,2005,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 <http://www.gnu.org/licenses/>.
+ */
+
+/*
+ The design of this memory manager.
+
+ This is a simple implementation of malloc with a few extensions. These are
+ the extensions:
+
+ - memalign is implemented efficiently.
+
+ - multiple regions may be used as free space. They may not be
+ contiguous.
+
+ Regions are managed by a singly linked list, and the meta information is
+ stored in the beginning of each region. Space after the meta information
+ is used to allocate memory.
+
+ The memory space is used as cells instead of bytes for simplicity. This
+ is important for some CPUs which may not access multiple bytes at a time
+ when the first byte is not aligned at a certain boundary (typically,
+ 4-byte or 8-byte). The size of each cell is equal to the size of struct
+ grub_mm_header, so the header of each allocated/free block fits into one
+ cell precisely. One cell is 16 bytes on 32-bit platforms and 32 bytes
+ on 64-bit platforms.
+
+ There are two types of blocks: allocated blocks and free blocks.
+
+ In allocated blocks, the header of each block has only its size. Note that
+ this size is based on cells but not on bytes. The header is located right
+ before the returned pointer, that is, the header resides at the previous
+ cell.
+
+ Free blocks constitutes a ring, using a singly linked list. The first free
+ block is pointed to by the meta information of a region. The allocator
+ attempts to pick up the second block instead of the first one. This is
+ a typical optimization against defragmentation, and makes the
+ implementation a bit easier.
+
+ For safety, both allocated blocks and free ones are marked by magic
+ numbers. Whenever anything unexpected is detected, GRUB aborts the
+ operation.
+ */
+
+#include <config.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/err.h>
+#include <grub/types.h>
+#include <grub/disk.h>
+#include <grub/dl.h>
+#include <grub/i18n.h>
+#include <grub/mm_private.h>
+#include <grub/safemath.h>
+
+#ifdef MM_DEBUG
+# undef grub_calloc
+# undef grub_malloc
+# undef grub_zalloc
+# undef grub_realloc
+# undef grub_free
+# undef grub_memalign
+#endif
+
+
+
+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
+ be allocated. */
+static void
+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 = 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)
+ grub_fatal ("out of range pointer %p", ptr);
+
+ *p = (grub_mm_header_t) ptr - 1;
+ if ((*p)->magic == GRUB_MM_FREE_MAGIC)
+ grub_fatal ("double free at %p", *p);
+ if ((*p)->magic != GRUB_MM_ALLOC_MAGIC)
+ grub_fatal ("alloc magic is broken at %p: %lx", *p,
+ (unsigned long) (*p)->magic);
+}
+
+/* Initialize a region starting from ADDR and whose size is SIZE,
+ to use it as free space. */
+void
+grub_mm_init_region (void *addr, grub_size_t size)
+{
+ grub_mm_header_t h;
+ grub_mm_region_t r, *p, q;
+
+#if 0
+ grub_printf ("Using memory for heap: start=%p, end=%p\n", addr, addr + (unsigned int) size);
+#endif
+
+ /* Exclude last 4K to avoid overflows. */
+ /* If addr + 0x1000 overflows then whole region is in excluded zone. */
+ if ((grub_addr_t) addr > ~((grub_addr_t) 0x1000))
+ return;
+
+ /* If addr + 0x1000 + size overflows then decrease size. */
+ if (((grub_addr_t) addr + 0x1000) > ~(grub_addr_t) size)
+ size = ((grub_addr_t) -0x1000) - (grub_addr_t) addr;
+
+ for (p = &grub_mm_base, q = *p; q; p = &(q->next), q = *p)
+ if ((grub_uint8_t *) addr + size + q->pre_size == (grub_uint8_t *) q)
+ {
+ r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN);
+ *r = *q;
+ r->pre_size += size;
+
+ if (r->pre_size >> GRUB_MM_ALIGN_LOG2)
+ {
+ h = (grub_mm_header_t) (r + 1);
+ h->size = (r->pre_size >> GRUB_MM_ALIGN_LOG2);
+ h->magic = GRUB_MM_ALLOC_MAGIC;
+ r->size += h->size << GRUB_MM_ALIGN_LOG2;
+ r->pre_size &= (GRUB_MM_ALIGN - 1);
+ *p = r;
+ grub_free (h + 1);
+ }
+ *p = r;
+ return;
+ }
+
+ /* Allocate a region from the head. */
+ r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN);
+
+ /* If this region is too small, ignore it. */
+ if (size < GRUB_MM_ALIGN + (char *) r - (char *) addr + sizeof (*r))
+ return;
+
+ size -= (char *) r - (char *) addr + sizeof (*r);
+
+ 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->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 = &grub_mm_base, q = *p; q; p = &(q->next), q = *p)
+ if (q->size > r->size)
+ break;
+
+ *p = r;
+ r->next = q;
+}
+
+/* Allocate the number of units N with the alignment ALIGN from the ring
+ buffer starting from *FIRST. ALIGN must be a power of two. Both N and
+ ALIGN are in units of GRUB_MM_ALIGN. Return a non-NULL if successful,
+ otherwise return NULL. */
+static void *
+grub_real_malloc (grub_mm_header_t *first, grub_size_t n, grub_size_t align)
+{
+ grub_mm_header_t p, q;
+
+ /* When everything is allocated side effect is that *first will have alloc
+ magic marked, meaning that there is no room in this region. */
+ if ((*first)->magic == GRUB_MM_ALLOC_MAGIC)
+ return 0;
+
+ /* Try to search free slot for allocation in this memory region. */
+ for (q = *first, p = q->next; ; q = p, p = p->next)
+ {
+ grub_off_t extra;
+
+ extra = ((grub_addr_t) (p + 1) >> GRUB_MM_ALIGN_LOG2) & (align - 1);
+ if (extra)
+ extra = align - extra;
+
+ if (! p)
+ grub_fatal ("null in the ring");
+
+ if (p->magic != GRUB_MM_FREE_MAGIC)
+ grub_fatal ("free magic is broken at %p: 0x%x", p, p->magic);
+
+ 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
+ is complete match.
+
+ 1. Just mark memory block as allocated and remove it from
+ free list.
+
+ Result:
+ +---------------+ previous block's next
+ | alloc, size=n | |
+ +---------------+ v
+ */
+ q->next = p->next;
+ }
+ else if (align == 1 || p->size == n + extra)
+ {
+ /* There might be alignment requirement, when taking it into
+ account memory block fits in.
+
+ 1. Allocate new area at end of memory block.
+ 2. Reduce size of available blocks from original node.
+ 3. Mark new area as allocated and "remove" it from free
+ list.
+
+ Result:
+ +---------------+
+ | free, size-=n | next --+
+ +---------------+ |
+ | alloc, size=n | |
+ +---------------+ v
+ */
+
+ p->size -= n;
+ p += p->size;
+ }
+ else if (extra == 0)
+ {
+ grub_mm_header_t r;
+
+ r = p + extra + n;
+ r->magic = GRUB_MM_FREE_MAGIC;
+ r->size = p->size - extra - n;
+ r->next = p->next;
+ q->next = r;
+
+ if (q == p)
+ {
+ q = r;
+ r->next = r;
+ }
+ }
+ else
+ {
+ /* There is alignment requirement and there is room in memory
+ block. Split memory block to three pieces.
+
+ 1. Create new memory block right after section being
+ allocated. Mark it as free.
+ 2. Add new memory block to free chain.
+ 3. Mark current memory block having only extra blocks.
+ 4. Advance to aligned block and mark that as allocated and
+ "remove" it from free list.
+
+ Result:
+ +------------------------------+
+ | free, size=extra | next --+
+ +------------------------------+ |
+ | alloc, size=n | |
+ +------------------------------+ |
+ | free, size=orig.size-extra-n | <------+, next --+
+ +------------------------------+ v
+ */
+ grub_mm_header_t r;
+
+ r = p + extra + n;
+ r->magic = GRUB_MM_FREE_MAGIC;
+ r->size = p->size - extra - n;
+ r->next = p;
+
+ p->size = extra;
+ q->next = r;
+ p += extra;
+ }
+
+ p->magic = GRUB_MM_ALLOC_MAGIC;
+ p->size = n;
+
+ /* Mark find as a start marker for next allocation to fasten it.
+ This will have side effect of fragmenting memory as small
+ pieces before this will be un-used. */
+ /* So do it only for chunks under 64K. */
+ if (n < (0x8000 >> GRUB_MM_ALIGN_LOG2)
+ || *first == p)
+ *first = q;
+
+ return p + 1;
+ }
+
+ /* Search was completed without result. */
+ if (p == *first)
+ break;
+ }
+
+ return 0;
+}
+
+/* Allocate SIZE bytes with the alignment ALIGN and return the pointer. */
+void *
+grub_memalign (grub_size_t align, grub_size_t size)
+{
+ grub_mm_region_t r;
+ grub_size_t n = ((size + GRUB_MM_ALIGN - 1) >> GRUB_MM_ALIGN_LOG2) + 1;
+ int count = 0;
+
+ if (!grub_mm_base)
+ goto fail;
+
+ if (size > ~(grub_size_t) align)
+ goto fail;
+
+ /* We currently assume at least a 32-bit grub_size_t,
+ so limiting allocations to <adress space size> - 1MiB
+ in name of sanity is beneficial. */
+ if ((size + align) > ~(grub_size_t) 0x100000)
+ goto fail;
+
+ align = (align >> GRUB_MM_ALIGN_LOG2);
+ if (align == 0)
+ align = 1;
+
+ again:
+
+ for (r = grub_mm_base; r; r = r->next)
+ {
+ void *p;
+
+ p = grub_real_malloc (&(r->first), n, align);
+ if (p)
+ return p;
+ }
+
+ /* If failed, increase free memory somehow. */
+ switch (count)
+ {
+ case 0:
+ /* Invalidate disk caches. */
+ grub_disk_cache_invalidate_all ();
+ count++;
+ goto again;
+
+#if 0
+ case 1:
+ /* Unload unneeded modules. */
+ grub_dl_unload_unneeded ();
+ count++;
+ goto again;
+#endif
+
+ default:
+ break;
+ }
+
+ fail:
+ grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
+ return 0;
+}
+
+/*
+ * Allocate NMEMB instances of SIZE bytes and return the pointer, or error on
+ * integer overflow.
+ */
+void *
+grub_calloc (grub_size_t nmemb, grub_size_t size)
+{
+ void *ret;
+ grub_size_t sz = 0;
+
+ if (grub_mul (nmemb, size, &sz))
+ {
+ grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
+ return NULL;
+ }
+
+ ret = grub_memalign (0, sz);
+ if (!ret)
+ return NULL;
+
+ grub_memset (ret, 0, sz);
+ return ret;
+}
+
+/* Allocate SIZE bytes and return the pointer. */
+void *
+grub_malloc (grub_size_t size)
+{
+ return grub_memalign (0, size);
+}
+
+/* Allocate SIZE bytes, clear them and return the pointer. */
+void *
+grub_zalloc (grub_size_t size)
+{
+ void *ret;
+
+ ret = grub_memalign (0, size);
+ if (ret)
+ grub_memset (ret, 0, size);
+
+ return ret;
+}
+
+/* Deallocate the pointer PTR. */
+void
+grub_free (void *ptr)
+{
+ grub_mm_header_t p;
+ grub_mm_region_t r;
+
+ if (! ptr)
+ return;
+
+ get_header_from_pointer (ptr, &p, &r);
+
+ if (r->first->magic == GRUB_MM_ALLOC_MAGIC)
+ {
+ p->magic = GRUB_MM_FREE_MAGIC;
+ r->first = p->next = p;
+ }
+ else
+ {
+ grub_mm_header_t q, s;
+
+#if 0
+ q = r->first;
+ do
+ {
+ grub_printf ("%s:%d: q=%p, q->size=0x%x, q->magic=0x%x\n",
+ GRUB_FILE, __LINE__, q, q->size, q->magic);
+ q = q->next;
+ }
+ while (q != r->first);
+#endif
+
+ for (s = r->first, q = s->next; q <= p || q->next >= p; s = q, q = s->next)
+ {
+ if (q->magic != GRUB_MM_FREE_MAGIC)
+ grub_fatal ("free magic is broken at %p: 0x%x", q, q->magic);
+
+ if (q <= q->next && (q > p || q->next < p))
+ break;
+ }
+
+ p->magic = GRUB_MM_FREE_MAGIC;
+ p->next = q->next;
+ q->next = p;
+
+ if (p->next + p->next->size == p)
+ {
+ p->magic = 0;
+
+ p->next->size += p->size;
+ q->next = p->next;
+ p = p->next;
+ }
+
+ r->first = q;
+
+ if (q == p + p->size)
+ {
+ q->magic = 0;
+ p->size += q->size;
+ if (q == s)
+ s = p;
+ s->next = p;
+ q = s;
+ }
+
+ r->first = q;
+ }
+}
+
+/* Reallocate SIZE bytes and return the pointer. The contents will be
+ the same as that of PTR. */
+void *
+grub_realloc (void *ptr, grub_size_t size)
+{
+ grub_mm_header_t p;
+ grub_mm_region_t r;
+ void *q;
+ grub_size_t n;
+
+ if (! ptr)
+ return grub_malloc (size);
+
+ if (! size)
+ {
+ grub_free (ptr);
+ return 0;
+ }
+
+ /* FIXME: Not optimal. */
+ n = ((size + GRUB_MM_ALIGN - 1) >> GRUB_MM_ALIGN_LOG2) + 1;
+ get_header_from_pointer (ptr, &p, &r);
+
+ if (p->size >= n)
+ return ptr;
+
+ q = grub_malloc (size);
+ if (! q)
+ return q;
+
+ /* We've already checked that p->size < n. */
+ grub_memcpy (q, ptr, p->size << GRUB_MM_ALIGN_LOG2);
+ grub_free (ptr);
+ return q;
+}
+
+#ifdef MM_DEBUG
+int grub_mm_debug = 0;
+
+void
+grub_mm_dump_free (void)
+{
+ grub_mm_region_t r;
+
+ for (r = grub_mm_base; r; r = r->next)
+ {
+ grub_mm_header_t p;
+
+ /* Follow the free list. */
+ p = r->first;
+ do
+ {
+ if (p->magic != GRUB_MM_FREE_MAGIC)
+ grub_fatal ("free magic is broken at %p: 0x%x", p, p->magic);
+
+ grub_printf ("F:%p:%u:%p\n",
+ p, (unsigned int) p->size << GRUB_MM_ALIGN_LOG2, p->next);
+ p = p->next;
+ }
+ while (p != r->first);
+ }
+
+ grub_printf ("\n");
+}
+
+void
+grub_mm_dump (unsigned lineno)
+{
+ grub_mm_region_t r;
+
+ grub_printf ("called at line %u\n", lineno);
+ for (r = grub_mm_base; r; r = r->next)
+ {
+ grub_mm_header_t p;
+
+ 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)
+ {
+ case GRUB_MM_FREE_MAGIC:
+ grub_printf ("F:%p:%u:%p\n",
+ p, (unsigned int) p->size << GRUB_MM_ALIGN_LOG2, p->next);
+ break;
+ case GRUB_MM_ALLOC_MAGIC:
+ grub_printf ("A:%p:%u\n", p, (unsigned int) p->size << GRUB_MM_ALIGN_LOG2);
+ break;
+ }
+ }
+ }
+
+ grub_printf ("\n");
+}
+
+void *
+grub_debug_calloc (const char *file, int line, grub_size_t nmemb, grub_size_t size)
+{
+ void *ptr;
+
+ if (grub_mm_debug)
+ grub_printf ("%s:%d: calloc (0x%" PRIxGRUB_SIZE ", 0x%" PRIxGRUB_SIZE ") = ",
+ file, line, nmemb, size);
+ ptr = grub_calloc (nmemb, size);
+ if (grub_mm_debug)
+ grub_printf ("%p\n", ptr);
+ return ptr;
+}
+
+void *
+grub_debug_malloc (const char *file, int line, grub_size_t size)
+{
+ void *ptr;
+
+ if (grub_mm_debug)
+ grub_printf ("%s:%d: malloc (0x%" PRIxGRUB_SIZE ") = ", file, line, size);
+ ptr = grub_malloc (size);
+ if (grub_mm_debug)
+ grub_printf ("%p\n", ptr);
+ return ptr;
+}
+
+void *
+grub_debug_zalloc (const char *file, int line, grub_size_t size)
+{
+ void *ptr;
+
+ if (grub_mm_debug)
+ grub_printf ("%s:%d: zalloc (0x%" PRIxGRUB_SIZE ") = ", file, line, size);
+ ptr = grub_zalloc (size);
+ if (grub_mm_debug)
+ grub_printf ("%p\n", ptr);
+ return ptr;
+}
+
+void
+grub_debug_free (const char *file, int line, void *ptr)
+{
+ if (grub_mm_debug)
+ grub_printf ("%s:%d: free (%p)\n", file, line, ptr);
+ grub_free (ptr);
+}
+
+void *
+grub_debug_realloc (const char *file, int line, void *ptr, grub_size_t size)
+{
+ if (grub_mm_debug)
+ grub_printf ("%s:%d: realloc (%p, 0x%" PRIxGRUB_SIZE ") = ", file, line, ptr, size);
+ ptr = grub_realloc (ptr, size);
+ if (grub_mm_debug)
+ grub_printf ("%p\n", ptr);
+ return ptr;
+}
+
+void *
+grub_debug_memalign (const char *file, int line, grub_size_t align,
+ grub_size_t size)
+{
+ void *ptr;
+
+ if (grub_mm_debug)
+ grub_printf ("%s:%d: memalign (0x%" PRIxGRUB_SIZE ", 0x%" PRIxGRUB_SIZE
+ ") = ", file, line, align, size);
+ ptr = grub_memalign (align, size);
+ if (grub_mm_debug)
+ grub_printf ("%p\n", ptr);
+ return ptr;
+}
+
+#endif /* MM_DEBUG */
diff --git a/grub-core/kern/parser.c b/grub-core/kern/parser.c
new file mode 100644
index 0000000..6ab7aa4
--- /dev/null
+++ b/grub-core/kern/parser.c
@@ -0,0 +1,342 @@
+/* parser.c - the part of the parser that can return partial tokens */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2005,2007,2009,2021 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/parser.h>
+#include <grub/buffer.h>
+#include <grub/env.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+
+
+/* All the possible state transitions on the command line. If a
+ transition can not be found, it is assumed that there is no
+ transition and keep_value is assumed to be 1. */
+static struct grub_parser_state_transition state_transitions[] = {
+ {GRUB_PARSER_STATE_TEXT, GRUB_PARSER_STATE_QUOTE, '\'', 0},
+ {GRUB_PARSER_STATE_TEXT, GRUB_PARSER_STATE_DQUOTE, '\"', 0},
+ {GRUB_PARSER_STATE_TEXT, GRUB_PARSER_STATE_VAR, '$', 0},
+ {GRUB_PARSER_STATE_TEXT, GRUB_PARSER_STATE_ESC, '\\', 0},
+
+ {GRUB_PARSER_STATE_ESC, GRUB_PARSER_STATE_TEXT, 0, 1},
+
+ {GRUB_PARSER_STATE_QUOTE, GRUB_PARSER_STATE_TEXT, '\'', 0},
+
+ {GRUB_PARSER_STATE_DQUOTE, GRUB_PARSER_STATE_TEXT, '\"', 0},
+ {GRUB_PARSER_STATE_DQUOTE, GRUB_PARSER_STATE_QVAR, '$', 0},
+
+ {GRUB_PARSER_STATE_VAR, GRUB_PARSER_STATE_VARNAME2, '{', 0},
+ {GRUB_PARSER_STATE_VAR, GRUB_PARSER_STATE_VARNAME, 0, 1},
+ {GRUB_PARSER_STATE_VARNAME, GRUB_PARSER_STATE_TEXT, ' ', 1},
+ {GRUB_PARSER_STATE_VARNAME, GRUB_PARSER_STATE_TEXT, '\t', 1},
+ {GRUB_PARSER_STATE_VARNAME2, GRUB_PARSER_STATE_TEXT, '}', 0},
+
+ {GRUB_PARSER_STATE_QVAR, GRUB_PARSER_STATE_QVARNAME2, '{', 0},
+ {GRUB_PARSER_STATE_QVAR, GRUB_PARSER_STATE_QVARNAME, 0, 1},
+ {GRUB_PARSER_STATE_QVARNAME, GRUB_PARSER_STATE_TEXT, '\"', 0},
+ {GRUB_PARSER_STATE_QVARNAME, GRUB_PARSER_STATE_DQUOTE, ' ', 1},
+ {GRUB_PARSER_STATE_QVARNAME, GRUB_PARSER_STATE_DQUOTE, '\t', 1},
+ {GRUB_PARSER_STATE_QVARNAME2, GRUB_PARSER_STATE_DQUOTE, '}', 0},
+
+ {0, 0, 0, 0}
+};
+
+
+/* Determines the state following STATE, determined by C. */
+grub_parser_state_t
+grub_parser_cmdline_state (grub_parser_state_t state, char c, char *result)
+{
+ struct grub_parser_state_transition *transition;
+ struct grub_parser_state_transition default_transition;
+
+ default_transition.to_state = state;
+ default_transition.keep_value = 1;
+
+ /* Look for a good translation. */
+ for (transition = state_transitions; transition->from_state; transition++)
+ {
+ if (transition->from_state != state)
+ continue;
+ /* An exact match was found, use it. */
+ if (transition->input == c)
+ break;
+
+ if (grub_isspace (transition->input) && !grub_isalpha (c)
+ && !grub_isdigit (c) && c != '_')
+ break;
+
+ /* A less perfect match was found, use this one if no exact
+ match can be found. */
+ if (transition->input == 0)
+ break;
+ }
+
+ if (!transition->from_state)
+ transition = &default_transition;
+
+ if (transition->keep_value)
+ *result = c;
+ else
+ *result = 0;
+ return transition->to_state;
+}
+
+
+/* Helper for grub_parser_split_cmdline. */
+static inline int
+check_varstate (grub_parser_state_t s)
+{
+ return (s == GRUB_PARSER_STATE_VARNAME
+ || s == GRUB_PARSER_STATE_VARNAME2
+ || s == GRUB_PARSER_STATE_QVARNAME
+ || s == GRUB_PARSER_STATE_QVARNAME2);
+}
+
+
+static grub_err_t
+add_var (grub_buffer_t varname, grub_buffer_t buf,
+ grub_parser_state_t state, grub_parser_state_t newstate)
+{
+ const char *val;
+
+ /* Check if a variable was being read in and the end of the name
+ was reached. */
+ if (!(check_varstate (state) && !check_varstate (newstate)))
+ return GRUB_ERR_NONE;
+
+ if (grub_buffer_append_char (varname, '\0') != GRUB_ERR_NONE)
+ return grub_errno;
+
+ val = grub_env_get ((const char *) grub_buffer_peek_data (varname));
+ grub_buffer_reset (varname);
+ if (!val)
+ return GRUB_ERR_NONE;
+
+ /* Insert the contents of the variable in the buffer. */
+ return grub_buffer_append_data (buf, val, grub_strlen (val));
+}
+
+static grub_err_t
+terminate_arg (grub_buffer_t buffer, int *argc)
+{
+ grub_size_t unread = grub_buffer_get_unread_bytes (buffer);
+
+ if (unread == 0)
+ return GRUB_ERR_NONE;
+
+ if (*(const char *) grub_buffer_peek_data_at (buffer, unread - 1) == '\0')
+ return GRUB_ERR_NONE;
+
+ if (grub_buffer_append_char (buffer, '\0') != GRUB_ERR_NONE)
+ return grub_errno;
+
+ (*argc)++;
+
+ return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+process_char (char c, grub_buffer_t buffer, grub_buffer_t varname,
+ grub_parser_state_t state, int *argc,
+ grub_parser_state_t *newstate)
+{
+ char use;
+
+ *newstate = grub_parser_cmdline_state (state, c, &use);
+
+ /*
+ * If a variable was being processed and this character does
+ * not describe the variable anymore, write the variable to
+ * the buffer.
+ */
+ if (add_var (varname, buffer, state, *newstate) != GRUB_ERR_NONE)
+ return grub_errno;
+
+ if (check_varstate (*newstate))
+ {
+ if (use)
+ return grub_buffer_append_char (varname, use);
+ }
+ else if (*newstate == GRUB_PARSER_STATE_TEXT &&
+ state != GRUB_PARSER_STATE_ESC && grub_isspace (use))
+ {
+ /*
+ * Don't add more than one argument if multiple
+ * spaces are used.
+ */
+ return terminate_arg (buffer, argc);
+ }
+ else if (use)
+ return grub_buffer_append_char (buffer, use);
+
+ return GRUB_ERR_NONE;
+}
+
+grub_err_t
+grub_parser_split_cmdline (const char *cmdline,
+ grub_reader_getline_t getline, void *getline_data,
+ int *argc, char ***argv)
+{
+ grub_parser_state_t state = GRUB_PARSER_STATE_TEXT;
+ grub_buffer_t buffer, varname;
+ char *rd = (char *) cmdline;
+ char *rp = rd;
+ int i;
+
+ *argc = 0;
+ *argv = NULL;
+
+ buffer = grub_buffer_new (1024);
+ if (buffer == NULL)
+ return grub_errno;
+
+ varname = grub_buffer_new (200);
+ if (varname == NULL)
+ goto fail;
+
+ do
+ {
+ if (rp == NULL || *rp == '\0')
+ {
+ if (rd != cmdline)
+ {
+ grub_free (rd);
+ rd = rp = NULL;
+ }
+ if (getline)
+ {
+ getline (&rd, 1, getline_data);
+ rp = rd;
+ }
+ else
+ break;
+ }
+
+ if (!rd)
+ break;
+
+ for (; *rp != '\0'; rp++)
+ {
+ grub_parser_state_t newstate;
+
+ if (process_char (*rp, buffer, varname, state, argc,
+ &newstate) != GRUB_ERR_NONE)
+ goto fail;
+
+ state = newstate;
+ }
+ }
+ while (state != GRUB_PARSER_STATE_TEXT && !check_varstate (state));
+
+ /* A special case for when the last character was part of a
+ variable. */
+ if (add_var (varname, buffer, state, GRUB_PARSER_STATE_TEXT) != GRUB_ERR_NONE)
+ goto fail;
+
+ /* Ensure that the last argument is terminated. */
+ if (terminate_arg (buffer, argc) != GRUB_ERR_NONE)
+ goto fail;
+
+ /* If there are no args, then we're done. */
+ if (!*argc)
+ {
+ grub_errno = GRUB_ERR_NONE;
+ goto out;
+ }
+
+ *argv = grub_calloc (*argc + 1, sizeof (char *));
+ if (!*argv)
+ goto fail;
+
+ /* The arguments are separated with 0's, setup argv so it points to
+ the right values. */
+ for (i = 0; i < *argc; i++)
+ {
+ char *arg;
+
+ if (i > 0)
+ {
+ if (grub_buffer_advance_read_pos (buffer, 1) != GRUB_ERR_NONE)
+ goto fail;
+ }
+
+ arg = (char *) grub_buffer_peek_data (buffer);
+ if (arg == NULL ||
+ grub_buffer_advance_read_pos (buffer, grub_strlen (arg)) != GRUB_ERR_NONE)
+ goto fail;
+
+ (*argv)[i] = arg;
+ }
+
+ /* Keep memory for the return values. */
+ grub_buffer_take_data (buffer);
+
+ grub_errno = GRUB_ERR_NONE;
+
+ out:
+ if (rd != cmdline)
+ grub_free (rd);
+ grub_buffer_free (buffer);
+ grub_buffer_free (varname);
+
+ return grub_errno;
+
+ fail:
+ grub_free (*argv);
+ goto out;
+}
+
+/* Helper for grub_parser_execute. */
+static grub_err_t
+grub_parser_execute_getline (char **line, int cont __attribute__ ((unused)),
+ void *data)
+{
+ char **source = data;
+ char *p;
+
+ if (!*source)
+ {
+ *line = 0;
+ return 0;
+ }
+
+ p = grub_strchr (*source, '\n');
+
+ if (p)
+ *line = grub_strndup (*source, p - *source);
+ else
+ *line = grub_strdup (*source);
+ *source = p ? p + 1 : 0;
+ return 0;
+}
+
+grub_err_t
+grub_parser_execute (char *source)
+{
+ while (source)
+ {
+ char *line;
+
+ grub_parser_execute_getline (&line, 0, &source);
+ grub_rescue_parse_line (line, grub_parser_execute_getline, &source);
+ grub_free (line);
+ grub_print_error ();
+ }
+
+ return grub_errno;
+}
diff --git a/grub-core/kern/partition.c b/grub-core/kern/partition.c
new file mode 100644
index 0000000..3068c4d
--- /dev/null
+++ b/grub-core/kern/partition.c
@@ -0,0 +1,279 @@
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/partition.h>
+#include <grub/disk.h>
+#include <grub/i18n.h>
+
+#ifdef GRUB_UTIL
+#include <grub/util/misc.h>
+#endif
+
+grub_partition_map_t grub_partition_map_list;
+
+/*
+ * Checks that disk->partition contains part. This function assumes that the
+ * start of part is relative to the start of disk->partition. Returns 1 if
+ * disk->partition is null.
+ */
+static int
+grub_partition_check_containment (const grub_disk_t disk,
+ const grub_partition_t part)
+{
+ if (disk->partition == NULL)
+ return 1;
+
+ if (part->start + part->len > disk->partition->len)
+ {
+ char *partname;
+
+ partname = grub_partition_get_name (disk->partition);
+ grub_dprintf ("partition", "sub-partition %s%d of (%s,%s) ends after parent.\n",
+ part->partmap->name, part->number + 1, disk->name, partname);
+#ifdef GRUB_UTIL
+ grub_util_warn (_("Discarding improperly nested partition (%s,%s,%s%d)"),
+ disk->name, partname, part->partmap->name, part->number + 1);
+#endif
+ grub_free (partname);
+
+ return 0;
+ }
+
+ return 1;
+}
+
+/* Context for grub_partition_map_probe. */
+struct grub_partition_map_probe_ctx
+{
+ int partnum;
+ grub_partition_t p;
+};
+
+/* Helper for grub_partition_map_probe. */
+static int
+probe_iter (grub_disk_t dsk, const grub_partition_t partition, void *data)
+{
+ struct grub_partition_map_probe_ctx *ctx = data;
+
+ if (ctx->partnum != partition->number)
+ return 0;
+
+ if (!(grub_partition_check_containment (dsk, partition)))
+ return 0;
+
+ ctx->p = (grub_partition_t) grub_malloc (sizeof (*ctx->p));
+ if (! ctx->p)
+ return 1;
+
+ grub_memcpy (ctx->p, partition, sizeof (*ctx->p));
+ return 1;
+}
+
+static grub_partition_t
+grub_partition_map_probe (const grub_partition_map_t partmap,
+ grub_disk_t disk, int partnum)
+{
+ struct grub_partition_map_probe_ctx ctx = {
+ .partnum = partnum,
+ .p = 0
+ };
+
+ partmap->iterate (disk, probe_iter, &ctx);
+ if (grub_errno)
+ goto fail;
+
+ return ctx.p;
+
+ fail:
+ grub_free (ctx.p);
+ return 0;
+}
+
+grub_partition_t
+grub_partition_probe (struct grub_disk *disk, const char *str)
+{
+ grub_partition_t part;
+ grub_partition_t curpart = 0;
+ grub_partition_t tail;
+ const char *ptr;
+
+ if (str == NULL)
+ return 0;
+
+ part = tail = disk->partition;
+
+ for (ptr = str; *ptr;)
+ {
+ grub_partition_map_t partmap;
+ int num;
+ const char *partname, *partname_end;
+
+ partname = ptr;
+ while (*ptr && grub_isalpha (*ptr))
+ ptr++;
+ partname_end = ptr;
+ num = grub_strtoul (ptr, &ptr, 0) - 1;
+
+ curpart = 0;
+ /* Use the first partition map type found. */
+ FOR_PARTITION_MAPS(partmap)
+ {
+ if (partname_end != partname &&
+ (grub_strncmp (partmap->name, partname, partname_end - partname)
+ != 0 || partmap->name[partname_end - partname] != 0))
+ continue;
+
+ disk->partition = part;
+ curpart = grub_partition_map_probe (partmap, disk, num);
+ disk->partition = tail;
+ if (curpart)
+ break;
+
+ if (grub_errno == GRUB_ERR_BAD_PART_TABLE)
+ {
+ /* Continue to next partition map type. */
+ grub_errno = GRUB_ERR_NONE;
+ continue;
+ }
+
+ break;
+ }
+
+ if (! curpart)
+ {
+ while (part)
+ {
+ curpart = part->parent;
+ grub_free (part);
+ part = curpart;
+ }
+ return 0;
+ }
+ curpart->parent = part;
+ part = curpart;
+ if (! ptr || *ptr != ',')
+ break;
+ ptr++;
+ }
+
+ return part;
+}
+
+/* Context for grub_partition_iterate. */
+struct grub_partition_iterate_ctx
+{
+ int ret;
+ grub_partition_iterate_hook_t hook;
+ void *hook_data;
+};
+
+/* Helper for grub_partition_iterate. */
+static int
+part_iterate (grub_disk_t dsk, const grub_partition_t partition, void *data)
+{
+ struct grub_partition_iterate_ctx *ctx = data;
+ struct grub_partition p = *partition;
+
+ if (!(grub_partition_check_containment (dsk, partition)))
+ return 0;
+
+ p.parent = dsk->partition;
+ dsk->partition = 0;
+ if (ctx->hook (dsk, &p, ctx->hook_data))
+ {
+ ctx->ret = 1;
+ return 1;
+ }
+ if (p.start != 0)
+ {
+ const struct grub_partition_map *partmap;
+ dsk->partition = &p;
+ FOR_PARTITION_MAPS(partmap)
+ {
+ grub_err_t err;
+ err = partmap->iterate (dsk, part_iterate, ctx);
+ if (err)
+ grub_errno = GRUB_ERR_NONE;
+ if (ctx->ret)
+ break;
+ }
+ }
+ dsk->partition = p.parent;
+ return ctx->ret;
+}
+
+int
+grub_partition_iterate (struct grub_disk *disk,
+ grub_partition_iterate_hook_t hook, void *hook_data)
+{
+ struct grub_partition_iterate_ctx ctx = {
+ .ret = 0,
+ .hook = hook,
+ .hook_data = hook_data
+ };
+ const struct grub_partition_map *partmap;
+
+ FOR_PARTITION_MAPS(partmap)
+ {
+ grub_err_t err;
+ err = partmap->iterate (disk, part_iterate, &ctx);
+ if (err)
+ grub_errno = GRUB_ERR_NONE;
+ if (ctx.ret)
+ break;
+ }
+
+ return ctx.ret;
+}
+
+char *
+grub_partition_get_name (const grub_partition_t partition)
+{
+ char *out = 0, *ptr;
+ grub_size_t needlen = 0;
+ grub_partition_t part;
+ if (!partition)
+ return grub_strdup ("");
+ for (part = partition; part; part = part->parent)
+ /* Even on 64-bit machines this buffer is enough to hold
+ longest number. */
+ needlen += grub_strlen (part->partmap->name) + 1 + 27;
+ out = grub_malloc (needlen + 1);
+ if (!out)
+ return NULL;
+
+ ptr = out + needlen;
+ *ptr = 0;
+ for (part = partition; part; part = part->parent)
+ {
+ char buf[27];
+ grub_size_t len;
+ grub_snprintf (buf, sizeof (buf), "%d", part->number + 1);
+ len = grub_strlen (buf);
+ ptr -= len;
+ grub_memcpy (ptr, buf, len);
+ len = grub_strlen (part->partmap->name);
+ ptr -= len;
+ grub_memcpy (ptr, part->partmap->name, len);
+ *--ptr = ',';
+ }
+ grub_memmove (out, ptr + 1, out + needlen - ptr);
+ return out;
+}
diff --git a/grub-core/kern/powerpc/cache.S b/grub-core/kern/powerpc/cache.S
new file mode 100644
index 0000000..d85e68d
--- /dev/null
+++ b/grub-core/kern/powerpc/cache.S
@@ -0,0 +1,26 @@
+/* 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 <http://www.gnu.org/licenses/>.
+ */
+
+ .text
+
+ .align 2
+ .globl grub_arch_sync_caches
+grub_arch_sync_caches:
+#include "cache_flush.S"
+ blr
diff --git a/grub-core/kern/powerpc/cache_flush.S b/grub-core/kern/powerpc/cache_flush.S
new file mode 100644
index 0000000..1410f78
--- /dev/null
+++ b/grub-core/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 <http://www.gnu.org/licenses/>.
+ */
+
+#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/grub-core/kern/powerpc/compiler-rt.S b/grub-core/kern/powerpc/compiler-rt.S
new file mode 100644
index 0000000..b3b912d
--- /dev/null
+++ b/grub-core/kern/powerpc/compiler-rt.S
@@ -0,0 +1,130 @@
+/*
+ * Special support for eabi and SVR4
+ *
+ * Copyright (C) 1995-2014 Free Software Foundation, Inc.
+ * Written By Michael Meissner
+ * 64-bit support written by David Edelsohn
+ *
+ * This file 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, or (at your option) any
+ * later version.
+ *
+ * This file 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.
+ *
+ * Under Section 7 of GPL version 3, you are granted additional
+ * permissions described in the GCC Runtime Library Exception, version
+ * 3.1, as published by the Free Software Foundation.
+ *
+ * You should have received a copy of the GNU General Public License and
+ * a copy of the GCC Runtime Library Exception along with this program;
+ * see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+/* Do any initializations needed for the eabi environment */
+
+#include <grub/symbol.h>
+#include <grub/dl.h>
+
+ .section ".text"
+
+#define CFI_RESTORE(reg) .cfi_restore reg
+#define CFI_OFFSET(reg, off) .cfi_offset reg, off
+#define CFI_DEF_CFA_REGISTER(reg) .cfi_def_cfa_register reg
+#define CFI_STARTPROC .cfi_startproc
+#define CFI_ENDPROC .cfi_endproc
+
+/* Routines for restoring integer registers, called by the compiler. */
+/* Called with r11 pointing to the stack header word of the caller of the */
+/* function, just beyond the end of the integer restore area. */
+
+CFI_STARTPROC
+CFI_DEF_CFA_REGISTER (11)
+CFI_OFFSET (65, 4)
+CFI_OFFSET (14, -72)
+CFI_OFFSET (15, -68)
+CFI_OFFSET (16, -64)
+CFI_OFFSET (17, -60)
+CFI_OFFSET (18, -56)
+CFI_OFFSET (19, -52)
+CFI_OFFSET (20, -48)
+CFI_OFFSET (21, -44)
+CFI_OFFSET (22, -40)
+CFI_OFFSET (23, -36)
+CFI_OFFSET (24, -32)
+CFI_OFFSET (25, -28)
+CFI_OFFSET (26, -24)
+CFI_OFFSET (27, -20)
+CFI_OFFSET (28, -16)
+CFI_OFFSET (29, -12)
+CFI_OFFSET (30, -8)
+CFI_OFFSET (31, -4)
+FUNCTION(_restgpr_14_x) lwz 14,-72(11) /* restore gp registers */
+CFI_RESTORE (14)
+FUNCTION(_restgpr_15_x) lwz 15,-68(11)
+CFI_RESTORE (15)
+FUNCTION(_restgpr_16_x) lwz 16,-64(11)
+CFI_RESTORE (16)
+FUNCTION(_restgpr_17_x) lwz 17,-60(11)
+CFI_RESTORE (17)
+FUNCTION(_restgpr_18_x) lwz 18,-56(11)
+CFI_RESTORE (18)
+FUNCTION(_restgpr_19_x) lwz 19,-52(11)
+CFI_RESTORE (19)
+FUNCTION(_restgpr_20_x) lwz 20,-48(11)
+CFI_RESTORE (20)
+FUNCTION(_restgpr_21_x) lwz 21,-44(11)
+CFI_RESTORE (21)
+FUNCTION(_restgpr_22_x) lwz 22,-40(11)
+CFI_RESTORE (22)
+FUNCTION(_restgpr_23_x) lwz 23,-36(11)
+CFI_RESTORE (23)
+FUNCTION(_restgpr_24_x) lwz 24,-32(11)
+CFI_RESTORE (24)
+FUNCTION(_restgpr_25_x) lwz 25,-28(11)
+CFI_RESTORE (25)
+FUNCTION(_restgpr_26_x) lwz 26,-24(11)
+CFI_RESTORE (26)
+FUNCTION(_restgpr_27_x) lwz 27,-20(11)
+CFI_RESTORE (27)
+FUNCTION(_restgpr_28_x) lwz 28,-16(11)
+CFI_RESTORE (28)
+FUNCTION(_restgpr_29_x) lwz 29,-12(11)
+CFI_RESTORE (29)
+FUNCTION(_restgpr_30_x) lwz 30,-8(11)
+CFI_RESTORE (30)
+FUNCTION(_restgpr_31_x) lwz 0,4(11)
+ lwz 31,-4(11)
+CFI_RESTORE (31)
+ mtlr 0
+CFI_RESTORE (65)
+ mr 1,11
+CFI_DEF_CFA_REGISTER (1)
+ blr
+CFI_ENDPROC
+
+CFI_STARTPROC
+FUNCTION(_savegpr_14) stw 14,-72(11) /* save gp registers */
+FUNCTION(_savegpr_15) stw 15,-68(11)
+FUNCTION(_savegpr_16) stw 16,-64(11)
+FUNCTION(_savegpr_17) stw 17,-60(11)
+FUNCTION(_savegpr_18) stw 18,-56(11)
+FUNCTION(_savegpr_19) stw 19,-52(11)
+FUNCTION(_savegpr_20) stw 20,-48(11)
+FUNCTION(_savegpr_21) stw 21,-44(11)
+FUNCTION(_savegpr_22) stw 22,-40(11)
+FUNCTION(_savegpr_23) stw 23,-36(11)
+FUNCTION(_savegpr_24) stw 24,-32(11)
+FUNCTION(_savegpr_25) stw 25,-28(11)
+FUNCTION(_savegpr_26) stw 26,-24(11)
+FUNCTION(_savegpr_27) stw 27,-20(11)
+FUNCTION(_savegpr_28) stw 28,-16(11)
+FUNCTION(_savegpr_29) stw 29,-12(11)
+FUNCTION(_savegpr_30) stw 30,-8(11)
+FUNCTION(_savegpr_31) stw 31,-4(11)
+ blr
+CFI_ENDPROC
diff --git a/grub-core/kern/powerpc/dl.c b/grub-core/kern/powerpc/dl.c
new file mode 100644
index 0000000..cdd61b3
--- /dev/null
+++ b/grub-core/kern/powerpc/dl.c
@@ -0,0 +1,169 @@
+/* dl.c - arch-dependent part of loadable module support */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2002,2004,2005,2007,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/dl.h>
+#include <grub/elf.h>
+#include <grub/misc.h>
+#include <grub/err.h>
+#include <grub/i18n.h>
+
+/* Check if EHDR is a valid ELF header. */
+grub_err_t
+grub_arch_dl_check_header (void *ehdr)
+{
+ Elf_Ehdr *e = ehdr;
+
+ /* Check the magic numbers. */
+ if (e->e_ident[EI_CLASS] != ELFCLASS32
+ || e->e_ident[EI_DATA] != ELFDATA2MSB
+ || e->e_machine != EM_PPC)
+ return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF magic"));
+
+ return GRUB_ERR_NONE;
+}
+
+/* For low-endian reverse lis and addr_high as well as ori and addr_low. */
+struct trampoline
+{
+ grub_uint32_t lis;
+ grub_uint32_t ori;
+ grub_uint32_t mtctr;
+ grub_uint32_t bctr;
+};
+
+static const struct trampoline trampoline_template =
+ {
+ 0x3d800000,
+ 0x618c0000,
+ 0x7d8903a6,
+ 0x4e800420,
+ };
+
+#pragma GCC diagnostic ignored "-Wcast-align"
+
+grub_err_t
+grub_arch_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp,
+ grub_size_t *got)
+{
+ const Elf_Ehdr *e = ehdr;
+ const Elf_Shdr *s;
+ unsigned i;
+
+ *tramp = 0;
+ *got = 0;
+
+ for (i = 0, s = (const Elf_Shdr *) ((const char *) e + e->e_shoff);
+ i < e->e_shnum;
+ i++, s = (const Elf_Shdr *) ((const char *) s + e->e_shentsize))
+ if (s->sh_type == SHT_RELA)
+ {
+ const Elf_Rela *rel, *max;
+
+ for (rel = (const Elf_Rela *) ((const char *) e + s->sh_offset),
+ max = rel + s->sh_size / s->sh_entsize;
+ rel < max;
+ rel++)
+ if (ELF_R_TYPE (rel->r_info) == GRUB_ELF_R_PPC_REL24
+ || ELF_R_TYPE (rel->r_info) == GRUB_ELF_R_PPC_PLTREL24)
+ (*tramp)++;
+
+ }
+
+ *tramp *= sizeof (struct trampoline);
+
+ return GRUB_ERR_NONE;
+}
+
+/* Relocate symbols. */
+grub_err_t
+grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
+ Elf_Shdr *s, grub_dl_segment_t seg)
+{
+ Elf_Rela *rel, *max;
+
+ for (rel = (Elf_Rela *) ((char *) ehdr + s->sh_offset),
+ max = (Elf_Rela *) ((char *) rel + s->sh_size);
+ rel < max;
+ rel = (Elf_Rela *) ((char *) rel + s->sh_entsize))
+ {
+ Elf_Word *addr;
+ Elf_Sym *sym;
+ grub_uint32_t value;
+
+ if (seg->size < rel->r_offset)
+ return grub_error (GRUB_ERR_BAD_MODULE,
+ "reloc offset is out of the segment");
+
+ addr = (Elf_Word *) ((char *) seg->addr + rel->r_offset);
+ sym = (Elf_Sym *) ((char *) mod->symtab
+ + mod->symsize * ELF_R_SYM (rel->r_info));
+
+ /* On the PPC the value does not have an explicit
+ addend, add it. */
+ value = sym->st_value + rel->r_addend;
+ switch (ELF_R_TYPE (rel->r_info))
+ {
+ case GRUB_ELF_R_PPC_ADDR16_LO:
+ *(Elf_Half *) addr = value;
+ break;
+
+ case GRUB_ELF_R_PPC_PLTREL24:
+ case GRUB_ELF_R_PPC_REL24:
+ {
+ Elf_Sword delta = value - (Elf_Word) addr;
+
+ if (delta << 6 >> 6 != delta)
+ {
+ struct trampoline *tptr = mod->trampptr;
+ grub_memcpy (tptr, &trampoline_template,
+ sizeof (*tptr));
+ delta = (grub_uint8_t *) tptr - (grub_uint8_t *) addr;
+ tptr->lis |= (((value) >> 16) & 0xffff);
+ tptr->ori |= ((value) & 0xffff);
+ mod->trampptr = tptr + 1;
+ }
+
+ if (delta << 6 >> 6 != delta)
+ return grub_error (GRUB_ERR_BAD_MODULE,
+ "relocation overflow");
+ *addr = (*addr & 0xfc000003) | (delta & 0x3fffffc);
+ break;
+ }
+
+ case GRUB_ELF_R_PPC_ADDR16_HA:
+ *(Elf_Half *) addr = (value + 0x8000) >> 16;
+ break;
+
+ case GRUB_ELF_R_PPC_ADDR32:
+ *addr = value;
+ break;
+
+ case GRUB_ELF_R_PPC_REL32:
+ *addr = value - (Elf_Word) addr;
+ break;
+
+ default:
+ return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
+ N_("relocation 0x%x is not implemented yet"),
+ ELF_R_TYPE (rel->r_info));
+ }
+ }
+
+ return GRUB_ERR_NONE;
+}
diff --git a/grub-core/kern/powerpc/ieee1275/startup.S b/grub-core/kern/powerpc/ieee1275/startup.S
new file mode 100644
index 0000000..21c884b
--- /dev/null
+++ b/grub-core/kern/powerpc/ieee1275/startup.S
@@ -0,0 +1,67 @@
+/* startup.S - Startup code for the PowerPC. */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2003,2004,2005,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/symbol.h>
+#include <grub/offsets.h>
+
+.extern __bss_start
+.extern _end
+
+ .text
+ .align 2
+ .globl start, _start
+start:
+_start:
+ li 2, 0
+ li 13, 0
+
+ /* Stage1 won't zero BSS for us. In other cases, why not do it again? */
+ lis 6, (__bss_start - 4)@h
+ ori 6, 6, (__bss_start - 4)@l
+
+2: stb 2, 4(6)
+ addi 6, 6, 1
+ andi. 7, 6, 3
+ cmpi 0, 1, 7, 0
+ bne 2b
+
+ lis 7, (_end - 4)@h
+ ori 7, 7, (_end - 4)@l
+ subf 7, 6, 7
+ subi 8, 7, 1
+ andi. 8, 8, 3
+ addi 8, 8, 1
+ sub 7, 7, 8
+
+ srwi 7, 7, 2 /* We store 4 bytes at a time. */
+ mtctr 7
+2: stwu 2, 4(6) /* We know r2 is already 0 from above. */
+ bdnz 2b
+
+ mtctr 8
+2: stb 2, 4(6) /* We know r2 is already 0 from above. */
+ addi 6, 6, 1
+ bdnz 2b
+
+ /* Store r5 in grub_ieee1275_entry_fn. */
+ lis 9, grub_ieee1275_entry_fn@ha
+ stw 5, grub_ieee1275_entry_fn@l(9)
+
+ bl grub_main
+1: b 1b
diff --git a/grub-core/kern/rescue_parser.c b/grub-core/kern/rescue_parser.c
new file mode 100644
index 0000000..6338366
--- /dev/null
+++ b/grub-core/kern/rescue_parser.c
@@ -0,0 +1,84 @@
+/* rescue_parser.c - rescue mode parser */
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/types.h>
+#include <grub/mm.h>
+#include <grub/env.h>
+#include <grub/parser.h>
+#include <grub/misc.h>
+#include <grub/command.h>
+#include <grub/i18n.h>
+
+grub_err_t
+grub_rescue_parse_line (char *line,
+ grub_reader_getline_t getline, void *getline_data)
+{
+ char *name;
+ int n;
+ grub_command_t cmd;
+ char **args;
+
+ if (grub_parser_split_cmdline (line, getline, getline_data, &n, &args)
+ || n < 0)
+ return grub_errno;
+
+ if (n == 0)
+ return GRUB_ERR_NONE;
+
+ /* In case of an assignment set the environment accordingly
+ instead of calling a function. */
+ if (n == 1)
+ {
+ char *val = grub_strchr (args[0], '=');
+
+ if (val)
+ {
+ val[0] = 0;
+ grub_env_set (args[0], val + 1);
+ val[0] = '=';
+ goto quit;
+ }
+ }
+
+ /* Get the command name. */
+ name = args[0];
+
+ /* If nothing is specified, restart. */
+ if (*name == '\0')
+ goto quit;
+
+ cmd = grub_command_find (name);
+ if (cmd)
+ {
+ (cmd->func) (cmd, n - 1, &args[1]);
+ }
+ else
+ {
+ grub_printf_ (N_("Unknown command `%s'.\n"), name);
+ if (grub_command_find ("help"))
+ grub_printf ("Try `help' for usage\n");
+ }
+
+ quit:
+ /* Arguments are returned in single memory chunk separated by zeroes */
+ grub_free (args[0]);
+ grub_free (args);
+
+ return grub_errno;
+}
diff --git a/grub-core/kern/rescue_reader.c b/grub-core/kern/rescue_reader.c
new file mode 100644
index 0000000..dcd7d44
--- /dev/null
+++ b/grub-core/kern/rescue_reader.c
@@ -0,0 +1,98 @@
+/* rescue_reader.c - rescue mode reader */
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/types.h>
+#include <grub/reader.h>
+#include <grub/parser.h>
+#include <grub/misc.h>
+#include <grub/term.h>
+#include <grub/mm.h>
+
+#define GRUB_RESCUE_BUF_SIZE 256
+
+static char linebuf[GRUB_RESCUE_BUF_SIZE];
+
+/* Prompt to input a command and read the line. */
+static grub_err_t
+grub_rescue_read_line (char **line, int cont,
+ void *data __attribute__ ((unused)))
+{
+ int c;
+ int pos = 0;
+ char str[4];
+
+ grub_printf ((cont) ? "> " : "grub rescue> ");
+ grub_memset (linebuf, 0, GRUB_RESCUE_BUF_SIZE);
+
+ while ((c = grub_getkey ()) != '\n' && c != '\r')
+ {
+ if (grub_isprint (c))
+ {
+ if (pos < GRUB_RESCUE_BUF_SIZE - 1)
+ {
+ str[0] = c;
+ str[1] = 0;
+ linebuf[pos++] = c;
+ grub_xputs (str);
+ }
+ }
+ else if (c == '\b')
+ {
+ if (pos > 0)
+ {
+ str[0] = c;
+ str[1] = ' ';
+ str[2] = c;
+ str[3] = 0;
+ linebuf[--pos] = 0;
+ grub_xputs (str);
+ }
+ }
+ grub_refresh ();
+ }
+
+ grub_xputs ("\n");
+ grub_refresh ();
+
+ *line = grub_strdup (linebuf);
+
+ return 0;
+}
+
+void __attribute__ ((noreturn))
+grub_rescue_run (void)
+{
+ grub_printf ("Entering rescue mode...\n");
+
+ while (1)
+ {
+ char *line;
+
+ /* Print an error, if any. */
+ grub_print_error ();
+ grub_errno = GRUB_ERR_NONE;
+
+ grub_rescue_read_line (&line, 0, NULL);
+ if (! line || line[0] == '\0')
+ continue;
+
+ grub_rescue_parse_line (line, grub_rescue_read_line, NULL);
+ grub_free (line);
+ }
+}
diff --git a/grub-core/kern/riscv/cache.c b/grub-core/kern/riscv/cache.c
new file mode 100644
index 0000000..47777a0
--- /dev/null
+++ b/grub-core/kern/riscv/cache.c
@@ -0,0 +1,63 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2018 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/cache.h>
+#include <grub/misc.h>
+
+static grub_int64_t dlinesz;
+static grub_int64_t ilinesz;
+
+/* Prototypes for asm functions. */
+void grub_arch_clean_dcache_range (grub_addr_t beg, grub_addr_t end,
+ grub_size_t line_size);
+void grub_arch_invalidate_icache_range (grub_addr_t beg, grub_addr_t end,
+ grub_size_t line_size);
+
+static void
+probe_caches (void)
+{
+ /* TODO */
+ dlinesz = 32;
+ ilinesz = 32;
+}
+
+void
+grub_arch_sync_caches (void *address, grub_size_t len)
+{
+ grub_size_t start, end, max_align;
+
+ if (dlinesz == 0)
+ probe_caches();
+ if (dlinesz == 0)
+ grub_fatal ("Unknown cache line size!");
+
+ max_align = dlinesz > ilinesz ? dlinesz : ilinesz;
+
+ start = ALIGN_DOWN ((grub_size_t) address, max_align);
+ end = ALIGN_UP ((grub_size_t) address + len, max_align);
+
+ grub_arch_clean_dcache_range (start, end, dlinesz);
+ grub_arch_invalidate_icache_range (start, end, ilinesz);
+}
+
+void
+grub_arch_sync_dma_caches (volatile void *address __attribute__((unused)),
+ grub_size_t len __attribute__((unused)))
+{
+ /* DMA incoherent devices not supported yet */
+}
diff --git a/grub-core/kern/riscv/cache_flush.S b/grub-core/kern/riscv/cache_flush.S
new file mode 100644
index 0000000..41de6e4
--- /dev/null
+++ b/grub-core/kern/riscv/cache_flush.S
@@ -0,0 +1,44 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2018 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/symbol.h>
+
+ .file "cache_flush.S"
+ .text
+
+/*
+ * Simple cache maintenance functions
+ */
+
+/*
+ * a0 - *beg (inclusive)
+ * a1 - *end (exclusive)
+ * a2 - line size
+*/
+FUNCTION(grub_arch_clean_dcache_range)
+ /* TODO */
+ ret
+
+/*
+ * a0 - *beg (inclusive)
+ * a1 - *end (exclusive)
+ * a2 - line size
+ */
+FUNCTION(grub_arch_invalidate_icache_range)
+ fence.i
+ ret
diff --git a/grub-core/kern/riscv/dl.c b/grub-core/kern/riscv/dl.c
new file mode 100644
index 0000000..f26b12a
--- /dev/null
+++ b/grub-core/kern/riscv/dl.c
@@ -0,0 +1,345 @@
+/* dl.c - arch-dependent part of loadable module support */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2018 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/dl.h>
+#include <grub/elf.h>
+#include <grub/misc.h>
+#include <grub/err.h>
+#include <grub/mm.h>
+#include <grub/i18n.h>
+
+/*
+ * Instructions and instruction encoding are documented in the RISC-V
+ * specification. This file is based on version 2.2:
+ *
+ * https://github.com/riscv/riscv-isa-manual/blob/master/release/riscv-spec-v2.2.pdf
+ */
+#define LDR 0x58000050
+#define BR 0xd61f0200
+
+/*
+ * Check if EHDR is a valid ELF header.
+ */
+grub_err_t
+grub_arch_dl_check_header (void *ehdr)
+{
+ Elf_Ehdr *e = ehdr;
+
+ /* Check the magic numbers. */
+ if (e->e_ident[EI_DATA] != ELFDATA2LSB || e->e_machine != EM_RISCV)
+ return grub_error (GRUB_ERR_BAD_OS,
+ N_("invalid arch-dependent ELF magic"));
+
+ return GRUB_ERR_NONE;
+}
+
+#pragma GCC diagnostic ignored "-Wcast-align"
+
+/* Relocate symbols. */
+grub_err_t
+grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
+ Elf_Shdr *s, grub_dl_segment_t seg)
+{
+ Elf_Rel *rel, *max;
+
+ for (rel = (Elf_Rel *) ((char *) ehdr + s->sh_offset),
+ max = (Elf_Rel *) ((char *) rel + s->sh_size);
+ rel < max;
+ rel = (Elf_Rel *) ((char *) rel + s->sh_entsize))
+ {
+ Elf_Sym *sym;
+ void *place;
+ grub_size_t sym_addr;
+
+ if (rel->r_offset >= seg->size)
+ return grub_error (GRUB_ERR_BAD_MODULE,
+ "reloc offset is out of the segment");
+
+ sym = (Elf_Sym *) ((char *) mod->symtab
+ + mod->symsize * ELF_R_SYM (rel->r_info));
+
+ sym_addr = sym->st_value;
+ if (s->sh_type == SHT_RELA)
+ sym_addr += ((Elf_Rela *) rel)->r_addend;
+
+ place = (void *) ((grub_addr_t) seg->addr + rel->r_offset);
+
+ switch (ELF_R_TYPE (rel->r_info))
+ {
+ case R_RISCV_32:
+ {
+ grub_uint32_t *abs_place = place;
+
+ grub_dprintf ("dl", " reloc_abs32 %p => 0x%016llx\n",
+ place, (unsigned long long) sym_addr);
+
+ *abs_place = (grub_uint32_t) sym_addr;
+ }
+ break;
+ case R_RISCV_64:
+ {
+ grub_size_t *abs_place = place;
+
+ grub_dprintf ("dl", " reloc_abs64 %p => 0x%016llx\n",
+ place, (unsigned long long) sym_addr);
+
+ *abs_place = (grub_size_t) sym_addr;
+ }
+ break;
+
+ case R_RISCV_ADD8:
+ {
+ grub_uint8_t *abs_place = place;
+
+ *abs_place += (grub_uint8_t) sym_addr;
+ }
+ break;
+ case R_RISCV_ADD16:
+ {
+ grub_uint16_t *abs_place = place;
+
+ *abs_place += (grub_uint16_t) sym_addr;
+ }
+ break;
+ case R_RISCV_ADD32:
+ {
+ grub_uint32_t *abs_place = place;
+
+ *abs_place += (grub_uint32_t) sym_addr;
+ }
+ break;
+ case R_RISCV_ADD64:
+ {
+ grub_size_t *abs_place = place;
+
+ *abs_place += (grub_size_t) sym_addr;
+ }
+ break;
+
+ case R_RISCV_SUB8:
+ {
+ grub_uint8_t *abs_place = place;
+
+ *abs_place -= (grub_uint8_t) sym_addr;
+ }
+ break;
+ case R_RISCV_SUB16:
+ {
+ grub_uint16_t *abs_place = place;
+
+ *abs_place -= (grub_uint16_t) sym_addr;
+ }
+ break;
+ case R_RISCV_SUB32:
+ {
+ grub_uint32_t *abs_place = place;
+
+ *abs_place -= (grub_uint32_t) sym_addr;
+ }
+ break;
+ case R_RISCV_SUB64:
+ {
+ grub_size_t *abs_place = place;
+
+ *abs_place -= (grub_size_t) sym_addr;
+ }
+ break;
+
+ case R_RISCV_BRANCH:
+ {
+ grub_uint32_t *abs_place = place;
+ grub_ssize_t off = sym_addr - (grub_addr_t) place;
+ grub_uint32_t imm12 = (off & 0x1000) << (31 - 12);
+ grub_uint32_t imm11 = (off & 0x800) >> (11 - 7);
+ grub_uint32_t imm10_5 = (off & 0x7e0) << (30 - 10);
+ grub_uint32_t imm4_1 = (off & 0x1e) << (11 - 4);
+ *abs_place = (*abs_place & 0x1fff07f)
+ | imm12 | imm11 | imm10_5 | imm4_1;
+ }
+ break;
+
+ case R_RISCV_JAL:
+ {
+ grub_uint32_t *abs_place = place;
+ grub_ssize_t off = sym_addr - (grub_addr_t) place;
+ grub_uint32_t imm20 = (off & 0x100000) << (31 - 20);
+ grub_uint32_t imm19_12 = (off & 0xff000);
+ grub_uint32_t imm11 = (off & 0x800) << (20 - 11);
+ grub_uint32_t imm10_1 = (off & 0x7fe) << (30 - 10);
+ *abs_place = (*abs_place & 0xfff)
+ | imm20 | imm19_12 | imm11 | imm10_1;
+ }
+ break;
+
+ case R_RISCV_CALL:
+ {
+ grub_uint32_t *abs_place = place;
+ grub_ssize_t off = sym_addr - (grub_addr_t) place;
+ grub_uint32_t hi20, lo12;
+
+ if (off != (grub_int32_t) off)
+ return grub_error (GRUB_ERR_BAD_MODULE, "relocation overflow");
+
+ hi20 = (off + 0x800) & 0xfffff000;
+ lo12 = (off - hi20) & 0xfff;
+ abs_place[0] = (abs_place[0] & 0xfff) | hi20;
+ abs_place[1] = (abs_place[1] & 0xfffff) | (lo12 << 20);
+ }
+ break;
+
+ case R_RISCV_RVC_BRANCH:
+ {
+ grub_uint16_t *abs_place = place;
+ grub_ssize_t off = sym_addr - (grub_addr_t) place;
+ grub_uint16_t imm8 = (off & 0x100) << (12 - 8);
+ grub_uint16_t imm7_6 = (off & 0xc0) >> (6 - 5);
+ grub_uint16_t imm5 = (off & 0x20) >> (5 - 2);
+ grub_uint16_t imm4_3 = (off & 0x18) << (12 - 5);
+ grub_uint16_t imm2_1 = (off & 0x6) << (12 - 10);
+ *abs_place = (*abs_place & 0xe383)
+ | imm8 | imm7_6 | imm5 | imm4_3 | imm2_1;
+ }
+ break;
+
+ case R_RISCV_RVC_JUMP:
+ {
+ grub_uint16_t *abs_place = place;
+ grub_ssize_t off = sym_addr - (grub_addr_t) place;
+ grub_uint16_t imm11 = (off & 0x800) << (12 - 11);
+ grub_uint16_t imm10 = (off & 0x400) >> (10 - 8);
+ grub_uint16_t imm9_8 = (off & 0x300) << (12 - 11);
+ grub_uint16_t imm7 = (off & 0x80) >> (7 - 6);
+ grub_uint16_t imm6 = (off & 0x40) << (12 - 11);
+ grub_uint16_t imm5 = (off & 0x20) >> (5 - 2);
+ grub_uint16_t imm4 = (off & 0x10) << (12 - 5);
+ grub_uint16_t imm3_1 = (off & 0xe) << (12 - 10);
+ *abs_place = ((*abs_place & 0xe003)
+ | imm11 | imm10 | imm9_8 | imm7 | imm6
+ | imm5 | imm4 | imm3_1);
+ }
+ break;
+
+ case R_RISCV_PCREL_HI20:
+ {
+ grub_uint32_t *abs_place = place;
+ grub_ssize_t off = sym_addr - (grub_addr_t) place;
+ grub_int32_t hi20;
+
+ if (off != (grub_int32_t)off)
+ return grub_error (GRUB_ERR_BAD_MODULE, "relocation overflow");
+
+ hi20 = (off + 0x800) & 0xfffff000;
+ *abs_place = (*abs_place & 0xfff) | hi20;
+ }
+ break;
+
+ case R_RISCV_PCREL_LO12_I:
+ case R_RISCV_PCREL_LO12_S:
+ {
+ grub_uint32_t *t32 = place;
+ Elf_Rela *rel2;
+ /* Search backwards for matching HI20 reloc. */
+ for (rel2 = (Elf_Rela *) ((char *) rel - s->sh_entsize);
+ (unsigned long)rel2 >= ((unsigned long)ehdr + s->sh_offset);
+ rel2 = (Elf_Rela *) ((char *) rel2 - s->sh_entsize))
+ {
+ Elf_Addr rel2_info;
+ Elf_Addr rel2_offset;
+ Elf_Addr rel2_sym_addr;
+ Elf_Addr rel2_loc;
+ grub_ssize_t rel2_off;
+ grub_ssize_t off;
+ Elf_Sym *sym2;
+
+ rel2_offset = rel2->r_offset;
+ rel2_info = rel2->r_info;
+ rel2_loc = (grub_addr_t) seg->addr + rel2_offset;
+
+ if (ELF_R_TYPE (rel2_info) == R_RISCV_PCREL_HI20
+ && rel2_loc == sym_addr)
+ {
+ sym2 = (Elf_Sym *) ((char *) mod->symtab
+ + mod->symsize * ELF_R_SYM (rel2->r_info));
+ rel2_sym_addr = sym2->st_value;
+ if (s->sh_type == SHT_RELA)
+ rel2_sym_addr += ((Elf_Rela *) rel2)->r_addend;
+
+ rel2_off = rel2_sym_addr - rel2_loc;
+ off = rel2_off - ((rel2_off + 0x800) & 0xfffff000);
+
+ if (ELF_R_TYPE (rel->r_info) == R_RISCV_PCREL_LO12_I)
+ *t32 = (*t32 & 0xfffff) | (off & 0xfff) << 20;
+ else
+ {
+ grub_uint32_t imm11_5 = (off & 0xfe0) << (31 - 11);
+ grub_uint32_t imm4_0 = (off & 0x1f) << (11 - 4);
+ *t32 = (*t32 & 0x1fff07f) | imm11_5 | imm4_0;
+ }
+ break;
+ }
+ }
+ if ((unsigned long)rel2 < ((unsigned long)ehdr + s->sh_offset))
+ return grub_error (GRUB_ERR_BAD_MODULE, "cannot find matching HI20 relocation");
+ }
+ break;
+
+ case R_RISCV_HI20:
+ {
+ grub_uint32_t *abs_place = place;
+ *abs_place = (*abs_place & 0xfff) |
+ (((grub_int32_t) sym_addr + 0x800) & 0xfffff000);
+ }
+ break;
+
+ case R_RISCV_LO12_I:
+ {
+ grub_uint32_t *abs_place = place;
+ grub_int32_t lo12 = (grub_int32_t) sym_addr -
+ (((grub_int32_t) sym_addr + 0x800) & 0xfffff000);
+ *abs_place = (*abs_place & 0xfffff) | ((lo12 & 0xfff) << 20);
+ }
+ break;
+
+ case R_RISCV_LO12_S:
+ {
+ grub_uint32_t *abs_place = place;
+ grub_int32_t lo12 = (grub_int32_t) sym_addr -
+ (((grub_int32_t) sym_addr + 0x800) & 0xfffff000);
+ grub_uint32_t imm11_5 = (lo12 & 0xfe0) << (31 - 11);
+ grub_uint32_t imm4_0 = (lo12 & 0x1f) << (11 - 4);
+ *abs_place = (*abs_place & 0x1fff07f) | imm11_5 | imm4_0;
+ }
+ break;
+
+ case R_RISCV_RELAX:
+ break;
+ default:
+ {
+ char rel_info[17]; /* log16(2^64) = 16, plus NUL. */
+
+ grub_snprintf (rel_info, sizeof (rel_info) - 1, "%" PRIxGRUB_UINT64_T,
+ (grub_uint64_t) ELF_R_TYPE (rel->r_info));
+ return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
+ N_("relocation 0x%s is not implemented yet"), rel_info);
+ }
+ }
+ }
+
+ return GRUB_ERR_NONE;
+}
diff --git a/grub-core/kern/riscv/efi/init.c b/grub-core/kern/riscv/efi/init.c
new file mode 100644
index 0000000..38795fe
--- /dev/null
+++ b/grub-core/kern/riscv/efi/init.c
@@ -0,0 +1,79 @@
+/* init.c - initialize a riscv-based EFI system */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2018 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/env.h>
+#include <grub/kernel.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/time.h>
+#include <grub/efi/efi.h>
+#include <grub/loader.h>
+
+static grub_uint64_t timer_frequency_in_khz;
+
+static grub_uint64_t
+grub_efi_get_time_ms (void)
+{
+ grub_uint64_t tmr;
+
+#if __riscv_xlen == 64
+ asm volatile ("rdcycle %0" : "=r" (tmr));
+#else
+ grub_uint32_t lo, hi, tmp;
+ asm volatile (
+ "1:\n"
+ "rdcycleh %0\n"
+ "rdcycle %1\n"
+ "rdcycleh %2\n"
+ "bne %0, %2, 1b"
+ : "=&r" (hi), "=&r" (lo), "=&r" (tmp));
+ tmr = ((grub_uint64_t)hi << 32) | lo;
+#endif
+
+ return tmr / timer_frequency_in_khz;
+}
+
+void
+grub_machine_init (void)
+{
+ grub_uint64_t time_before, time_after;
+
+ grub_efi_init ();
+
+ /* Calculate timer frequency */
+ timer_frequency_in_khz = 1;
+ time_before = grub_efi_get_time_ms();
+ grub_efi_stall(1000);
+ time_after = grub_efi_get_time_ms();
+ timer_frequency_in_khz = time_after - time_before;
+
+ grub_install_get_time_ms (grub_efi_get_time_ms);
+}
+
+void
+grub_machine_fini (int flags)
+{
+ if (!(flags & GRUB_LOADER_FLAG_NORETURN))
+ return;
+
+ grub_efi_fini ();
+
+ if (!(flags & GRUB_LOADER_FLAG_EFI_KEEP_ALLOCATED_MEMORY))
+ grub_efi_memory_fini ();
+}
diff --git a/grub-core/kern/riscv/efi/startup.S b/grub-core/kern/riscv/efi/startup.S
new file mode 100644
index 0000000..f2a7b2b
--- /dev/null
+++ b/grub-core/kern/riscv/efi/startup.S
@@ -0,0 +1,48 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2018 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/symbol.h>
+
+#if __riscv_xlen == 64
+#define sl sd
+#define ll ld
+#else
+#define sl sw
+#define ll lw
+#endif
+
+
+ .file "startup.S"
+ .text
+FUNCTION(_start)
+ /*
+ * EFI_SYSTEM_TABLE and EFI_HANDLE are passed in a1/a0.
+ */
+
+ ll a2, efi_image_handle_val
+ sl a0, 0(a2)
+ ll a2, efi_system_table_val
+ sl a1, 0(a2)
+ ll a2, grub_main_val
+ jr a2
+grub_main_val:
+ .quad EXT_C(grub_main)
+efi_system_table_val:
+ .quad EXT_C(grub_efi_system_table)
+efi_image_handle_val:
+ .quad EXT_C(grub_efi_image_handle)
diff --git a/grub-core/kern/sparc64/cache.S b/grub-core/kern/sparc64/cache.S
new file mode 100644
index 0000000..1a16add
--- /dev/null
+++ b/grub-core/kern/sparc64/cache.S
@@ -0,0 +1,41 @@
+/* cache.S - Flush the processor cache for a specific region. */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2005,2007,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/symbol.h>
+
+ .file "cache.S"
+
+ .text
+
+/*
+ * void grub_arch_sync_caches (void *address, grub_size_t len)
+ */
+FUNCTION(grub_arch_sync_caches)
+ brz,pn %o1, 2f
+ add %o0, %o1, %o1
+ add %o1, 7, %o1
+ andn %o1, 7, %o1
+ andn %o0, 7, %o0
+ sub %o1, %o0, %o1
+1: subcc %o1, 8, %o1
+ bne,pt %icc, 1b
+ flush %o0 + %o1
+2: retl
+ nop
+
diff --git a/grub-core/kern/sparc64/dl.c b/grub-core/kern/sparc64/dl.c
new file mode 100644
index 0000000..f3d9601
--- /dev/null
+++ b/grub-core/kern/sparc64/dl.c
@@ -0,0 +1,191 @@
+/* dl.c - arch-dependent part of loadable module support */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2002,2004,2005,2007,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/dl.h>
+#include <grub/elf.h>
+#include <grub/misc.h>
+#include <grub/err.h>
+#include <grub/i18n.h>
+
+/* Check if EHDR is a valid ELF header. */
+grub_err_t
+grub_arch_dl_check_header (void *ehdr)
+{
+ Elf_Ehdr *e = ehdr;
+
+ /* Check the magic numbers. */
+ if (e->e_ident[EI_CLASS] != ELFCLASS64
+ || e->e_ident[EI_DATA] != ELFDATA2MSB
+ || e->e_machine != EM_SPARCV9)
+ return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF magic"));
+
+ return GRUB_ERR_NONE;
+}
+
+#pragma GCC diagnostic ignored "-Wcast-align"
+
+struct trampoline
+{
+ grub_uint8_t code[0x28];
+ grub_uint64_t addr;
+};
+
+static const grub_uint8_t trampoline_code[0x28] =
+{
+ /* 0: */ 0x82, 0x10, 0x00, 0x0f, /* mov %o7, %g1 */
+ /* 4: */ 0x40, 0x00, 0x00, 0x02, /* call 0xc */
+ /* 8: */ 0x01, 0x00, 0x00, 0x00, /* nop */
+ /* c: */ 0x9e, 0x1b, 0xc0, 0x01, /* xor %o7, %g1, %o7 */
+ /* 10: */ 0x82, 0x18, 0x40, 0x0f, /* xor %g1, %o7, %g1 */
+ /* 14: */ 0x9e, 0x1b, 0xc0, 0x01, /* xor %o7, %g1, %o7 */
+ /* 18: */ 0xc2, 0x58, 0x60, 0x24, /* ldx [ %g1 + 0x24 ], %g1 */
+ /* 1c: */ 0x81, 0xc0, 0x40, 0x00, /* jmp %g1 */
+ /* 20: */ 0x01, 0x00, 0x00, 0x00, /* nop */
+ /* 24: */ 0x01, 0x00, 0x00, 0x00, /* nop */
+};
+
+grub_err_t
+grub_arch_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp,
+ grub_size_t *got)
+{
+ const Elf_Ehdr *e = ehdr;
+ const Elf_Shdr *s;
+ unsigned i;
+
+ *tramp = 0;
+ *got = 0;
+
+ for (i = 0, s = (const Elf_Shdr *) ((grub_addr_t) e + e->e_shoff);
+ i < e->e_shnum;
+ i++, s = (const Elf_Shdr *) ((grub_addr_t) s + e->e_shentsize))
+ if (s->sh_type == SHT_REL || s->sh_type == SHT_RELA)
+ {
+ const Elf_Rel *rel, *max;
+
+ for (rel = (const Elf_Rel *) ((grub_addr_t) e + s->sh_offset),
+ max = rel + s->sh_size / s->sh_entsize;
+ rel < max;
+ rel = (const Elf_Rel *) ((grub_addr_t) rel + s->sh_entsize))
+ switch (ELF_R_TYPE (rel->r_info))
+ {
+ case R_SPARC_WDISP30:
+ {
+ *tramp += sizeof (struct trampoline);
+ break;
+ }
+ }
+ }
+
+ return GRUB_ERR_NONE;
+}
+
+/* Relocate symbols. */
+grub_err_t
+grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
+ Elf_Shdr *s, grub_dl_segment_t seg)
+{
+ Elf_Rela *rel, *max;
+
+ for (rel = (Elf_Rela *) ((char *) ehdr + s->sh_offset),
+ max = (Elf_Rela *) ((char *) rel + s->sh_size);
+ rel < max;
+ rel = (Elf_Rela *) ((char *) rel + s->sh_entsize))
+ {
+ Elf_Word *addr;
+ Elf_Sym *sym;
+ Elf_Addr value;
+
+ if (seg->size < rel->r_offset)
+ return grub_error (GRUB_ERR_BAD_MODULE,
+ "reloc offset is out of the segment");
+
+ addr = (Elf_Word *) ((char *) seg->addr + rel->r_offset);
+ sym = (Elf_Sym *) ((char *) mod->symtab
+ + mod->symsize * ELF_R_SYM (rel->r_info));
+
+ value = sym->st_value + rel->r_addend;
+ switch (ELF_R_TYPE (rel->r_info) & 0xff)
+ {
+ case R_SPARC_32: /* 3 V-word32 */
+ if (value & 0xFFFFFFFF00000000)
+ return grub_error (GRUB_ERR_BAD_MODULE,
+ "address out of 32 bits range");
+ *addr = value;
+ break;
+ case R_SPARC_WDISP30: /* 7 V-disp30 */
+ if (((value - (Elf_Addr) addr) & 0xFFFFFFFF00000000) &&
+ (((value - (Elf_Addr) addr) & 0xFFFFFFFF00000000)
+ != 0xFFFFFFFF00000000))
+ {
+ struct trampoline *tp = mod->trampptr;
+ mod->trampptr = tp + 1;
+ grub_memcpy (tp->code, trampoline_code, sizeof (tp->code));
+ tp->addr = value;
+ value = (Elf_Addr) tp;
+ }
+
+ if (((value - (Elf_Addr) addr) & 0xFFFFFFFF00000000) &&
+ (((value - (Elf_Addr) addr) & 0xFFFFFFFF00000000)
+ != 0xFFFFFFFF00000000))
+ return grub_error (GRUB_ERR_BAD_MODULE,
+ "displacement out of 30 bits range");
+ *addr = (*addr & 0xC0000000) |
+ (((grub_int32_t) ((value - (Elf_Addr) addr) >> 2)) &
+ 0x3FFFFFFF);
+ break;
+ case R_SPARC_HH22: /* 9 V-imm22 */
+ *addr = (*addr & 0xFFC00000) | ((value >> 42) & 0x3FFFFF);
+ break;
+ case R_SPARC_HM10: /* 12 T-simm13 */
+ *addr = (*addr & 0xFFFFFC00) | ((value >> 32) & 0x3FF);
+ break;
+ case R_SPARC_HI22: /* 9 V-imm22 */
+ if (value >> 32)
+ return grub_error (GRUB_ERR_BAD_MODULE,
+ "address out of 32 bits range");
+ /* Fallthrough. */
+ case R_SPARC_LM22:
+ *addr = (*addr & 0xFFC00000) | ((value >> 10) & 0x3FFFFF);
+ break;
+ case R_SPARC_LO10: /* 12 T-simm13 */
+ *addr = (*addr & 0xFFFFFC00) | (value & 0x3FF);
+ break;
+ case R_SPARC_64: /* 32 V-xwords64 */
+ *(Elf_Xword *) addr = value;
+ break;
+ case R_SPARC_OLO10:
+ *addr = (*addr & ~0x1fff)
+ | (((value & 0x3ff) +
+ (ELF_R_TYPE (rel->r_info) >> 8))
+ & 0x1fff);
+ break;
+ default:
+ {
+ char rel_info[17]; /* log16(2^64) = 16, plus NUL. */
+
+ grub_snprintf (rel_info, sizeof (rel_info) - 1, "%" PRIxGRUB_UINT64_T,
+ ELF_R_TYPE (rel->r_info));
+ return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
+ N_("relocation 0x%s is not implemented yet"), rel_info);
+ }
+ }
+ }
+
+ return GRUB_ERR_NONE;
+}
diff --git a/grub-core/kern/sparc64/ieee1275/crt0.S b/grub-core/kern/sparc64/ieee1275/crt0.S
new file mode 100644
index 0000000..03b916f
--- /dev/null
+++ b/grub-core/kern/sparc64/ieee1275/crt0.S
@@ -0,0 +1,104 @@
+/* crt0.S - Startup code for the Sparc64. */
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+#include <grub/symbol.h>
+#include <grub/machine/kernel.h>
+#include <grub/offsets.h>
+
+ .text
+ .align 4
+ .globl _start
+_start:
+ ba codestart
+ mov %o4, %o0
+
+ .org GRUB_KERNEL_MACHINE_TOTAL_MODULE_SIZE
+
+VARIABLE(grub_total_module_size)
+ .word 0
+
+codestart:
+ /* Copy the modules past the end of the kernel image.
+ * They are currently sitting in the BSS.
+ */
+ sethi %hi(__bss_start + GRUB_KERNEL_SPARC64_IEEE1275_MOD_ALIGN - 1), %o2
+ or %o2, %lo(__bss_start + GRUB_KERNEL_SPARC64_IEEE1275_MOD_ALIGN - 1), %o2
+ srl %o2, GRUB_KERNEL_SPARC64_IEEE1275_LOG_MOD_ALIGN, %o2
+ sll %o2, GRUB_KERNEL_SPARC64_IEEE1275_LOG_MOD_ALIGN, %o2
+ sethi %hi(_end + GRUB_KERNEL_SPARC64_IEEE1275_MOD_ALIGN - 1), %o3
+ or %o3, %lo(_end + GRUB_KERNEL_SPARC64_IEEE1275_MOD_ALIGN - 1), %o3
+ srl %o3, GRUB_KERNEL_SPARC64_IEEE1275_LOG_MOD_ALIGN, %o3
+ sll %o3, GRUB_KERNEL_SPARC64_IEEE1275_LOG_MOD_ALIGN, %o3
+ sethi %hi(grub_total_module_size), %o4
+ lduw [%o4 + %lo(grub_total_module_size)], %o4
+
+ add %o2, %o4, %o2
+ add %o3, %o4, %o3
+
+ /* Save ieee1275 stack for future use by booter. */
+ mov %o6, %o1
+ /* Our future stack. */
+ sethi %hi(GRUB_KERNEL_MACHINE_STACK_SIZE), %o5
+ or %o5, %lo(GRUB_KERNEL_MACHINE_STACK_SIZE), %o5
+ add %o3, %o5, %o6
+ and %o6, ~0xff, %o6
+ sub %o6, 2047, %o6
+
+ sub %o2, 4, %o2
+ sub %o3, 4, %o3
+1: lduw [%o2], %o5
+ stw %o5, [%o3]
+ subcc %o4, 4, %o4
+ sub %o2, 4, %o2
+ bne,pt %icc, 1b
+ sub %o3, 4, %o3
+
+ /* Now it's safe to clear out the BSS. */
+ sethi %hi(__bss_start), %o2
+ or %o2, %lo(__bss_start), %o2
+1: stb %g0, [%o2]
+ add %o2, 1, %o2
+ and %o2, 7, %o3
+ brnz %o3, 1b
+ nop
+
+ sethi %hi(_end - 1), %o3
+ or %o3, %lo(_end - 1), %o3
+ srl %o3, 3, %o3
+ sll %o3, 3, %o3
+1: stx %g0, [%o2]
+ add %o2, 8, %o2
+ cmp %o2, %o3
+ blt,pt %xcc, 1b
+ nop
+
+ sethi %hi(_end), %o3
+ or %o3, %lo(_end), %o3
+1: stb %g0, [%o2]
+ add %o2, 1, %o2
+ cmp %o2, %o3
+ blt,pt %xcc, 1b
+ nop
+
+ sethi %hi(grub_ieee1275_original_stack), %o2
+ stx %o1, [%o2 + %lo(grub_ieee1275_original_stack)]
+ sethi %hi(grub_ieee1275_entry_fn), %o2
+ call grub_main
+ stx %o0, [%o2 + %lo(grub_ieee1275_entry_fn)]
+1: ba,a 1b
+ nop
diff --git a/grub-core/kern/sparc64/ieee1275/ieee1275.c b/grub-core/kern/sparc64/ieee1275/ieee1275.c
new file mode 100644
index 0000000..5a59aaf
--- /dev/null
+++ b/grub-core/kern/sparc64/ieee1275/ieee1275.c
@@ -0,0 +1,147 @@
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/ieee1275/ieee1275.h>
+#include <grub/types.h>
+
+/* Sun specific ieee1275 interfaces used by GRUB. */
+
+int
+grub_ieee1275_claim_vaddr (grub_addr_t vaddr, grub_size_t size)
+{
+ struct claim_vaddr_args
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t method;
+ grub_ieee1275_cell_t ihandle;
+ grub_ieee1275_cell_t align;
+ grub_ieee1275_cell_t size;
+ grub_ieee1275_cell_t virt;
+ grub_ieee1275_cell_t catch_result;
+ }
+ args;
+
+ INIT_IEEE1275_COMMON (&args.common, "call-method", 5, 2);
+ args.method = (grub_ieee1275_cell_t) "claim";
+ args.ihandle = grub_ieee1275_mmu;
+ args.align = 0;
+ args.size = size;
+ args.virt = vaddr;
+ args.catch_result = (grub_ieee1275_cell_t) -1;
+
+ if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
+ return -1;
+ return args.catch_result;
+}
+
+int
+grub_ieee1275_alloc_physmem (grub_addr_t *paddr, grub_size_t size,
+ grub_uint32_t align)
+{
+ grub_uint32_t memory_ihandle;
+ struct alloc_physmem_args
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t method;
+ grub_ieee1275_cell_t ihandle;
+ grub_ieee1275_cell_t align;
+ grub_ieee1275_cell_t size;
+ grub_ieee1275_cell_t catch_result;
+ grub_ieee1275_cell_t phys_high;
+ grub_ieee1275_cell_t phys_low;
+ }
+ args;
+ grub_ssize_t actual = 0;
+
+ grub_ieee1275_get_property (grub_ieee1275_chosen, "memory",
+ &memory_ihandle, sizeof (memory_ihandle),
+ &actual);
+ if (actual != sizeof (memory_ihandle))
+ return -1;
+
+ if (!align)
+ align = 1;
+
+ INIT_IEEE1275_COMMON (&args.common, "call-method", 4, 3);
+ args.method = (grub_ieee1275_cell_t) "claim";
+ args.ihandle = memory_ihandle;
+ args.align = (align ? align : 1);
+ args.size = size;
+ if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
+ return -1;
+
+ *paddr = args.phys_low;
+
+ return args.catch_result;
+}
+
+grub_uint64_t
+grub_ieee1275_num_blocks (grub_ieee1275_ihandle_t ihandle)
+{
+ struct nblocks_args_ieee1275
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t method;
+ grub_ieee1275_cell_t ihandle;
+ grub_ieee1275_cell_t catch_result;
+ grub_ieee1275_cell_t blocks;
+ }
+ args;
+
+ INIT_IEEE1275_COMMON (&args.common, "call-method", 2, 2);
+ args.method = (grub_ieee1275_cell_t) "#blocks";
+ args.ihandle = ihandle;
+ args.catch_result = 1;
+
+ if ((IEEE1275_CALL_ENTRY_FN (&args) == -1) || (args.catch_result != 0))
+ return -1;
+
+ /*
+ * If the number of blocks exceeds the range of an unsigned number,
+ * return 0 to alert the caller to try the #blocks64 command.
+ */
+ if (args.blocks >= 0xffffffffULL)
+ return 0;
+
+ return args.blocks;
+}
+
+grub_uint64_t
+grub_ieee1275_num_blocks64 (grub_ieee1275_ihandle_t ihandle)
+{
+ struct nblocks_args_ieee1275
+ {
+ struct grub_ieee1275_common_hdr common;
+ grub_ieee1275_cell_t method;
+ grub_ieee1275_cell_t ihandle;
+ grub_ieee1275_cell_t catch_result;
+ grub_ieee1275_cell_t hi_blocks;
+ grub_ieee1275_cell_t lo_blocks;
+ }
+ args;
+
+ INIT_IEEE1275_COMMON (&args.common, "call-method", 2, 3);
+ args.method = (grub_ieee1275_cell_t) "#blocks64";
+ args.ihandle = ihandle;
+ args.catch_result = 1;
+
+ if ((IEEE1275_CALL_ENTRY_FN (&args) == -1) || (args.catch_result != 0))
+ return -1;
+
+ return ((args.hi_blocks << 32) | (args.lo_blocks));
+}
diff --git a/grub-core/kern/term.c b/grub-core/kern/term.c
new file mode 100644
index 0000000..14d5964
--- /dev/null
+++ b/grub-core/kern/term.c
@@ -0,0 +1,169 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2002,2003,2005,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/term.h>
+#include <grub/err.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/env.h>
+#include <grub/time.h>
+
+struct grub_term_output *grub_term_outputs_disabled;
+struct grub_term_input *grub_term_inputs_disabled;
+struct grub_term_output *grub_term_outputs;
+struct grub_term_input *grub_term_inputs;
+
+/* Current color state. */
+grub_uint8_t grub_term_normal_color = GRUB_TERM_DEFAULT_NORMAL_COLOR;
+grub_uint8_t grub_term_highlight_color = GRUB_TERM_DEFAULT_HIGHLIGHT_COLOR;
+
+void (*grub_term_poll_usb) (int wait_for_completion) = NULL;
+void (*grub_net_poll_cards_idle) (void) = NULL;
+
+/* Put a Unicode character. */
+static void
+grub_putcode_dumb (grub_uint32_t code,
+ struct grub_term_output *term)
+{
+ struct grub_unicode_glyph c =
+ {
+ .base = code,
+ .variant = 0,
+ .attributes = 0,
+ .ncomb = 0,
+ .estimated_width = 1
+ };
+
+ if (code == '\t' && term->getxy)
+ {
+ int n;
+
+ n = GRUB_TERM_TAB_WIDTH - ((term->getxy (term).x)
+ % GRUB_TERM_TAB_WIDTH);
+ while (n--)
+ grub_putcode_dumb (' ', term);
+
+ return;
+ }
+
+ (term->putchar) (term, &c);
+ if (code == '\n')
+ grub_putcode_dumb ('\r', term);
+}
+
+static void
+grub_xputs_dumb (const char *str)
+{
+ for (; *str; str++)
+ {
+ grub_term_output_t term;
+ grub_uint32_t code = *str;
+ if (code > 0x7f)
+ code = '?';
+
+ FOR_ACTIVE_TERM_OUTPUTS(term)
+ grub_putcode_dumb (code, term);
+ }
+}
+
+void (*grub_xputs) (const char *str) = grub_xputs_dumb;
+
+int
+grub_getkey_noblock (void)
+{
+ grub_term_input_t term;
+
+ if (grub_term_poll_usb)
+ grub_term_poll_usb (0);
+
+ if (grub_net_poll_cards_idle)
+ grub_net_poll_cards_idle ();
+
+ FOR_ACTIVE_TERM_INPUTS(term)
+ {
+ int key = term->getkey (term);
+ if (key != GRUB_TERM_NO_KEY)
+ return key;
+ }
+
+ return GRUB_TERM_NO_KEY;
+}
+
+int
+grub_getkey (void)
+{
+ int ret;
+
+ grub_refresh ();
+
+ while (1)
+ {
+ ret = grub_getkey_noblock ();
+ if (ret != GRUB_TERM_NO_KEY)
+ return ret;
+ grub_cpu_idle ();
+ }
+}
+
+int
+grub_getkeystatus (void)
+{
+ int status = 0;
+ grub_term_input_t term;
+
+ if (grub_term_poll_usb)
+ grub_term_poll_usb (0);
+
+ FOR_ACTIVE_TERM_INPUTS(term)
+ {
+ if (term->getkeystatus)
+ status |= term->getkeystatus (term);
+ }
+
+ return status;
+}
+
+int
+grub_key_is_interrupt (int key)
+{
+ /*
+ * ESC sometimes is the BIOS setup hotkey and may be hard to discover, also
+ * check F4, which was chosen because is not used as a hotkey to enter the
+ * BIOS setup by any vendor.
+ */
+ if (key == GRUB_TERM_ESC || key == GRUB_TERM_KEY_F4)
+ return 1;
+
+ /*
+ * Pressing keys at the right time during boot is hard to time, also allow
+ * interrupting sleeps / the menu countdown by keeping shift pressed.
+ */
+ if (grub_getkeystatus() & (GRUB_TERM_STATUS_LSHIFT | GRUB_TERM_STATUS_RSHIFT))
+ return 1;
+
+ return 0;
+}
+
+void
+grub_refresh (void)
+{
+ struct grub_term_output *term;
+
+ FOR_ACTIVE_TERM_OUTPUTS(term)
+ grub_term_refresh (term);
+}
diff --git a/grub-core/kern/time.c b/grub-core/kern/time.c
new file mode 100644
index 0000000..6521ec6
--- /dev/null
+++ b/grub-core/kern/time.c
@@ -0,0 +1,37 @@
+/* time.c - kernel time functions */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/time.h>
+
+typedef grub_uint64_t (*get_time_ms_func_t) (void);
+
+/* Function pointer to the implementation in use. */
+static get_time_ms_func_t get_time_ms_func;
+
+grub_uint64_t
+grub_get_time_ms (void)
+{
+ return get_time_ms_func ();
+}
+
+void
+grub_install_get_time_ms (get_time_ms_func_t func)
+{
+ get_time_ms_func = func;
+}
diff --git a/grub-core/kern/uboot/hw.c b/grub-core/kern/uboot/hw.c
new file mode 100644
index 0000000..272b83b
--- /dev/null
+++ b/grub-core/kern/uboot/hw.c
@@ -0,0 +1,112 @@
+/* hw.c - U-Boot hardware discovery */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2013 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/kernel.h>
+#include <grub/memory.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/offsets.h>
+#include <grub/machine/kernel.h>
+#include <grub/uboot/disk.h>
+#include <grub/uboot/uboot.h>
+#include <grub/uboot/api_public.h>
+
+grub_addr_t start_of_ram;
+
+/*
+ * grub_uboot_probe_memory():
+ * Queries U-Boot for available memory regions.
+ *
+ * Sets up heap near the image in memory and sets up "start_of_ram".
+ */
+void
+grub_uboot_mm_init (void)
+{
+ struct sys_info *si = grub_uboot_get_sys_info ();
+
+ grub_mm_init_region ((void *) grub_modules_get_end (),
+ GRUB_KERNEL_MACHINE_HEAP_SIZE);
+
+ if (si && (si->mr_no != 0))
+ {
+ int i;
+ start_of_ram = GRUB_UINT_MAX;
+
+ for (i = 0; i < si->mr_no; i++)
+ if ((si->mr[i].flags & MR_ATTR_MASK) == MR_ATTR_DRAM)
+ if (si->mr[i].start < start_of_ram)
+ start_of_ram = si->mr[i].start;
+ }
+}
+
+/*
+ * grub_uboot_probe_hardware():
+ */
+grub_err_t
+grub_uboot_probe_hardware (void)
+{
+ int devcount, i;
+
+ devcount = grub_uboot_dev_enum ();
+ grub_dprintf ("init", "%d devices found\n", devcount);
+
+ for (i = 0; i < devcount; i++)
+ {
+ struct device_info *devinfo = grub_uboot_dev_get (i);
+
+ grub_dprintf ("init", "device handle: %d\n", i);
+ grub_dprintf ("init", " cookie\t= 0x%08x\n",
+ (grub_uint32_t) devinfo->cookie);
+
+ if (devinfo->type & DEV_TYP_STOR)
+ {
+ grub_dprintf ("init", " type\t\t= DISK\n");
+ grub_ubootdisk_register (devinfo);
+ }
+ else if (devinfo->type & DEV_TYP_NET)
+ {
+ /* Dealt with in ubootnet module. */
+ grub_dprintf ("init", " type\t\t= NET (not supported yet)\n");
+ }
+ else
+ {
+ grub_dprintf ("init", "%s: unknown device type", __FUNCTION__);
+ }
+ }
+
+ return GRUB_ERR_NONE;
+}
+
+grub_err_t
+grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
+{
+ int i;
+ struct sys_info *si = grub_uboot_get_sys_info ();
+
+ if (!si || (si->mr_no < 1))
+ return GRUB_ERR_BUG;
+
+ /* Iterate and call `hook'. */
+ for (i = 0; i < si->mr_no; i++)
+ if ((si->mr[i].flags & MR_ATTR_MASK) == MR_ATTR_DRAM)
+ hook (si->mr[i].start, si->mr[i].size, GRUB_MEMORY_AVAILABLE,
+ hook_data);
+
+ return GRUB_ERR_NONE;
+}
diff --git a/grub-core/kern/uboot/init.c b/grub-core/kern/uboot/init.c
new file mode 100644
index 0000000..3e33864
--- /dev/null
+++ b/grub-core/kern/uboot/init.c
@@ -0,0 +1,172 @@
+/* init.c - generic U-Boot initialization and finalization */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2013 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 <http://www.gnu.org/licenses/>.
+ */
+
+
+#include <grub/env.h>
+#include <grub/kernel.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/offsets.h>
+#include <grub/term.h>
+#include <grub/time.h>
+#include <grub/machine/kernel.h>
+#include <grub/uboot/console.h>
+#include <grub/uboot/disk.h>
+#include <grub/uboot/uboot.h>
+#include <grub/uboot/api_public.h>
+#include <grub/cpu/system.h>
+#include <grub/cache.h>
+
+extern char __bss_start[];
+extern char _end[];
+extern grub_size_t grub_total_module_size;
+static unsigned long timer_start;
+
+void
+grub_exit (void)
+{
+ grub_uboot_return (0);
+}
+
+static grub_uint64_t
+uboot_timer_ms (void)
+{
+ static grub_uint32_t last = 0, high = 0;
+ grub_uint32_t cur = grub_uboot_get_timer (timer_start);
+ if (cur < last)
+ high++;
+ last = cur;
+ return (((grub_uint64_t) high) << 32) | cur;
+}
+
+#ifdef __arm__
+static grub_uint64_t
+rpi_timer_ms (void)
+{
+ static grub_uint32_t last = 0, high = 0;
+ grub_uint32_t cur = *(volatile grub_uint32_t *) 0x20003004;
+ if (cur < last)
+ high++;
+ last = cur;
+ return grub_divmod64 ((((grub_uint64_t) high) << 32) | cur, 1000, 0);
+}
+#endif
+
+void
+grub_machine_init (void)
+{
+ int ver;
+
+ /* First of all - establish connection with U-Boot */
+ ver = grub_uboot_api_init ();
+ if (!ver)
+ {
+ /* Don't even have a console to log errors to... */
+ grub_exit ();
+ }
+ else if (ver > API_SIG_VERSION)
+ {
+ /* Try to print an error message */
+ grub_uboot_puts ("invalid U-Boot API version\n");
+ }
+
+ /* Initialize the console so that GRUB can display messages. */
+ grub_console_init_early ();
+
+ /* Enumerate memory and initialize the memory management system. */
+ grub_uboot_mm_init ();
+
+ /* Should be earlier but it needs memalign. */
+#ifdef __arm__
+ grub_arm_enable_caches_mmu ();
+#endif
+
+ grub_dprintf ("init", "__bss_start: %p\n", __bss_start);
+ grub_dprintf ("init", "_end: %p\n", _end);
+ grub_dprintf ("init", "grub_modbase: %p\n", (void *) grub_modbase);
+ grub_dprintf ("init", "grub_modules_get_end(): %p\n",
+ (void *) grub_modules_get_end ());
+
+ /* Initialise full terminfo support */
+ grub_console_init_lately ();
+
+ /* Enumerate uboot devices */
+ grub_uboot_probe_hardware ();
+
+ /* Initialise timer */
+#ifdef __arm__
+ if (grub_uboot_get_machine_type () == GRUB_ARM_MACHINE_TYPE_RASPBERRY_PI)
+ {
+ grub_install_get_time_ms (rpi_timer_ms);
+ }
+ else
+#endif
+ {
+ timer_start = grub_uboot_get_timer (0);
+ grub_install_get_time_ms (uboot_timer_ms);
+ }
+
+ /* Initialize */
+ grub_ubootdisk_init ();
+}
+
+
+void
+grub_machine_fini (int flags __attribute__ ((unused)))
+{
+}
+
+/*
+ * grub_machine_get_bootlocation():
+ * Called from kern/main.c, which expects a device name (minus parentheses)
+ * and a filesystem path back, if any are known.
+ * Any returned values must be pointers to dynamically allocated strings.
+ */
+void
+grub_machine_get_bootlocation (char **device, char **path)
+{
+ char *tmp;
+
+ tmp = grub_uboot_env_get ("grub_bootdev");
+ if (tmp)
+ {
+ *device = grub_strdup (tmp);
+ if (*device == NULL)
+ return;
+ }
+ else
+ *device = NULL;
+
+ tmp = grub_uboot_env_get ("grub_bootpath");
+ if (tmp)
+ {
+ *path = grub_strdup (tmp);
+ if (*path == NULL)
+ return;
+ }
+ else
+ *path = NULL;
+}
+
+void
+grub_uboot_fini (void)
+{
+ grub_ubootdisk_fini ();
+ grub_console_fini ();
+}
diff --git a/grub-core/kern/uboot/uboot.c b/grub-core/kern/uboot/uboot.c
new file mode 100644
index 0000000..aac8f9a
--- /dev/null
+++ b/grub-core/kern/uboot/uboot.c
@@ -0,0 +1,307 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2013 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/uboot/api_public.h>
+#include <grub/uboot/uboot.h>
+
+/*
+ * The main syscall entry point is not reentrant, only one call is
+ * serviced until finished.
+ *
+ * int syscall(int call, int *retval, ...)
+ * e.g. syscall(1, int *, u_int32_t, u_int32_t, u_int32_t, u_int32_t);
+ *
+ * call: syscall number
+ *
+ * retval: points to the return value placeholder, this is the place the
+ * syscall puts its return value, if NULL the caller does not
+ * expect a return value
+ *
+ * ... syscall arguments (variable number)
+ *
+ * returns: 0 if the call not found, 1 if serviced
+ */
+
+extern int grub_uboot_syscall (int, int *, ...);
+
+static struct sys_info uboot_sys_info;
+static struct mem_region uboot_mem_info[5];
+static struct device_info * devices;
+static int num_devices;
+
+
+/*
+ * All functions below are wrappers around the grub_uboot_syscall() function
+ */
+
+int
+grub_uboot_getc (void)
+{
+ int c;
+ if (!grub_uboot_syscall (API_GETC, NULL, &c))
+ return -1;
+
+ return c;
+}
+
+int
+grub_uboot_tstc (void)
+{
+ int c;
+ if (!grub_uboot_syscall (API_TSTC, NULL, &c))
+ return -1;
+
+ return c;
+}
+
+void
+grub_uboot_putc (int c)
+{
+ grub_uboot_syscall (API_PUTC, NULL, &c);
+}
+
+void
+grub_uboot_puts (const char *s)
+{
+ grub_uboot_syscall (API_PUTS, NULL, s);
+}
+
+void
+grub_uboot_reset (void)
+{
+ grub_uboot_syscall (API_RESET, NULL, 0);
+}
+
+struct sys_info *
+grub_uboot_get_sys_info (void)
+{
+ int retval;
+
+ grub_memset (&uboot_sys_info, 0, sizeof (uboot_sys_info));
+ grub_memset (&uboot_mem_info, 0, sizeof (uboot_mem_info));
+ uboot_sys_info.mr = uboot_mem_info;
+ uboot_sys_info.mr_no = sizeof (uboot_mem_info) / sizeof (struct mem_region);
+
+ if (grub_uboot_syscall (API_GET_SYS_INFO, &retval, &uboot_sys_info))
+ if (retval == 0)
+ return &uboot_sys_info;
+
+ return NULL;
+}
+
+void
+grub_uboot_udelay (grub_uint32_t usec)
+{
+ grub_uboot_syscall (API_UDELAY, NULL, &usec);
+}
+
+grub_uint32_t
+grub_uboot_get_timer (grub_uint32_t base)
+{
+ grub_uint32_t current;
+
+ if (!grub_uboot_syscall (API_GET_TIMER, NULL, &current, &base))
+ return 0;
+
+ return current;
+}
+
+int
+grub_uboot_dev_enum (void)
+{
+ struct device_info * enum_devices;
+ int num_enum_devices, max_devices;
+
+ if (num_devices)
+ return num_devices;
+
+ max_devices = 2;
+ enum_devices = grub_calloc (max_devices, sizeof(struct device_info));
+ if (!enum_devices)
+ return 0;
+
+ /*
+ * The API_DEV_ENUM call starts a fresh enumeration when passed a
+ * struct device_info with a NULL cookie, and then depends on having
+ * the previously enumerated device cookie "seeded" into the target
+ * structure.
+ */
+
+ enum_devices[0].cookie = NULL;
+ num_enum_devices = 0;
+
+ if (grub_uboot_syscall (API_DEV_ENUM, NULL,
+ &enum_devices[num_enum_devices]) == 0)
+ goto error;
+
+ num_enum_devices++;
+
+ while (enum_devices[num_enum_devices - 1].cookie != NULL)
+ {
+ if (num_enum_devices == max_devices)
+ {
+ struct device_info *tmp;
+ int new_max;
+ new_max = max_devices * 2;
+ tmp = grub_realloc (enum_devices,
+ sizeof (struct device_info) * new_max);
+ if (!tmp)
+ {
+ /* Failed to realloc, so return what we have */
+ break;
+ }
+ enum_devices = tmp;
+ max_devices = new_max;
+ }
+
+ enum_devices[num_enum_devices].cookie =
+ enum_devices[num_enum_devices - 1].cookie;
+ if (grub_uboot_syscall (API_DEV_ENUM, NULL,
+ &enum_devices[num_enum_devices]) == 0)
+ goto error;
+
+ if (enum_devices[num_enum_devices].cookie == NULL)
+ break;
+
+ num_enum_devices++;
+ }
+
+ devices = enum_devices;
+ return num_devices = num_enum_devices;
+
+ error:
+ grub_free (enum_devices);
+ return 0;
+}
+
+#define VALID_DEV(x) (((x) < num_devices) && ((x) >= 0))
+#define OPEN_DEV(x) ((x->state == DEV_STA_OPEN))
+
+struct device_info *
+grub_uboot_dev_get (int index)
+{
+ if (VALID_DEV (index))
+ return &devices[index];
+
+ return NULL;
+}
+
+
+int
+grub_uboot_dev_open (struct device_info *dev)
+{
+ int retval;
+
+ if (!grub_uboot_syscall (API_DEV_OPEN, &retval, dev))
+ return -1;
+
+ return retval;
+}
+
+int
+grub_uboot_dev_close (struct device_info *dev)
+{
+ int retval;
+
+ if (!grub_uboot_syscall (API_DEV_CLOSE, &retval, dev))
+ return -1;
+
+ return retval;
+}
+
+
+int
+grub_uboot_dev_read (struct device_info *dev, void *buf, grub_size_t blocks,
+ grub_uint32_t start, grub_size_t * real_blocks)
+{
+ int retval;
+
+ if (!OPEN_DEV (dev))
+ return -1;
+
+ if (!grub_uboot_syscall (API_DEV_READ, &retval, dev, buf,
+ &blocks, &start, real_blocks))
+ return -1;
+
+ return retval;
+}
+
+int
+grub_uboot_dev_write (struct device_info *dev, const void *buf,
+ grub_size_t blocks, grub_uint32_t start)
+{
+ int retval;
+
+ if (!OPEN_DEV (dev))
+ return -1;
+
+ if (!grub_uboot_syscall (API_DEV_WRITE, &retval, dev, buf,
+ &blocks, &start))
+ return -1;
+
+ return retval;
+}
+
+int
+grub_uboot_dev_recv (struct device_info *dev, void *buf,
+ int size, int *real_size)
+{
+ int retval;
+
+ if (!OPEN_DEV (dev))
+ return -1;
+
+ if (!grub_uboot_syscall (API_DEV_READ, &retval, dev, buf, &size, real_size))
+ return -1;
+
+ return retval;
+
+}
+
+int
+grub_uboot_dev_send (struct device_info *dev, void *buf, int size)
+{
+ int retval;
+
+ if (!OPEN_DEV (dev))
+ return -1;
+
+ if (!grub_uboot_syscall (API_DEV_WRITE, &retval, dev, buf, &size))
+ return -1;
+
+ return retval;
+}
+
+char *
+grub_uboot_env_get (const char *name)
+{
+ char *value;
+
+ if (!grub_uboot_syscall (API_ENV_GET, NULL, name, &value))
+ return NULL;
+
+ return value;
+}
+
+void
+grub_uboot_env_set (const char *name, const char *value)
+{
+ grub_uboot_syscall (API_ENV_SET, NULL, name, value);
+}
diff --git a/grub-core/kern/verifiers.c b/grub-core/kern/verifiers.c
new file mode 100644
index 0000000..75d7994
--- /dev/null
+++ b/grub-core/kern/verifiers.c
@@ -0,0 +1,228 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2017 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 <http://www.gnu.org/licenses/>.
+ *
+ * Verifiers helper.
+ */
+
+#include <grub/file.h>
+#include <grub/verify.h>
+#include <grub/dl.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+struct grub_file_verifier *grub_file_verifiers;
+
+struct grub_verified
+{
+ grub_file_t file;
+ void *buf;
+};
+typedef struct grub_verified *grub_verified_t;
+
+static void
+verified_free (grub_verified_t verified)
+{
+ if (verified)
+ {
+ grub_free (verified->buf);
+ grub_free (verified);
+ }
+}
+
+static grub_ssize_t
+verified_read (struct grub_file *file, char *buf, grub_size_t len)
+{
+ grub_verified_t verified = file->data;
+
+ grub_memcpy (buf, (char *) verified->buf + file->offset, len);
+ return len;
+}
+
+static grub_err_t
+verified_close (struct grub_file *file)
+{
+ grub_verified_t verified = file->data;
+
+ grub_file_close (verified->file);
+ verified_free (verified);
+ file->data = 0;
+
+ /* Device and name are freed by parent. */
+ file->device = 0;
+ file->name = 0;
+
+ return grub_errno;
+}
+
+struct grub_fs verified_fs =
+{
+ .name = "verified_read",
+ .fs_read = verified_read,
+ .fs_close = verified_close
+};
+
+static grub_file_t
+grub_verifiers_open (grub_file_t io, enum grub_file_type type)
+{
+ grub_verified_t verified = NULL;
+ struct grub_file_verifier *ver;
+ void *context;
+ grub_file_t ret = 0;
+ grub_err_t err;
+ int defer = 0;
+
+ grub_dprintf ("verify", "file: %s type: %d\n", io->name, type);
+
+ if ((type & GRUB_FILE_TYPE_MASK) == GRUB_FILE_TYPE_SIGNATURE
+ || (type & GRUB_FILE_TYPE_MASK) == GRUB_FILE_TYPE_VERIFY_SIGNATURE
+ || (type & GRUB_FILE_TYPE_SKIP_SIGNATURE))
+ return io;
+
+ if (io->device->disk &&
+ (io->device->disk->dev->id == GRUB_DISK_DEVICE_MEMDISK_ID
+ || io->device->disk->dev->id == GRUB_DISK_DEVICE_PROCFS_ID))
+ return io;
+
+ FOR_LIST_ELEMENTS(ver, grub_file_verifiers)
+ {
+ enum grub_verify_flags flags = 0;
+ err = ver->init (io, type, &context, &flags);
+ if (err)
+ goto fail_noclose;
+ if (flags & GRUB_VERIFY_FLAGS_DEFER_AUTH)
+ {
+ defer = 1;
+ continue;
+ }
+ if (!(flags & GRUB_VERIFY_FLAGS_SKIP_VERIFICATION))
+ break;
+ }
+
+ if (!ver)
+ {
+ if (defer)
+ {
+ grub_error (GRUB_ERR_ACCESS_DENIED,
+ N_("verification requested but nobody cares: %s"), io->name);
+ goto fail_noclose;
+ }
+
+ /* No verifiers wanted to verify. Just return underlying file. */
+ return io;
+ }
+
+ ret = grub_malloc (sizeof (*ret));
+ if (!ret)
+ {
+ goto fail;
+ }
+ *ret = *io;
+
+ ret->fs = &verified_fs;
+ ret->not_easily_seekable = 0;
+ if (ret->size >> (sizeof (grub_size_t) * GRUB_CHAR_BIT - 1))
+ {
+ grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
+ N_("big file signature isn't implemented yet"));
+ goto fail;
+ }
+ verified = grub_malloc (sizeof (*verified));
+ if (!verified)
+ {
+ goto fail;
+ }
+ verified->buf = grub_malloc (ret->size);
+ if (!verified->buf)
+ {
+ goto fail;
+ }
+ if (grub_file_read (io, verified->buf, ret->size) != (grub_ssize_t) ret->size)
+ {
+ if (!grub_errno)
+ grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
+ io->name);
+ goto fail;
+ }
+
+ err = ver->write (context, verified->buf, ret->size);
+ if (err)
+ goto fail;
+
+ err = ver->fini ? ver->fini (context) : GRUB_ERR_NONE;
+ if (err)
+ goto fail;
+
+ if (ver->close)
+ ver->close (context);
+
+ FOR_LIST_ELEMENTS_NEXT(ver, grub_file_verifiers)
+ {
+ enum grub_verify_flags flags = 0;
+ err = ver->init (io, type, &context, &flags);
+ if (err)
+ goto fail_noclose;
+ if (flags & GRUB_VERIFY_FLAGS_SKIP_VERIFICATION ||
+ /* Verification done earlier. So, we are happy here. */
+ flags & GRUB_VERIFY_FLAGS_DEFER_AUTH)
+ continue;
+ err = ver->write (context, verified->buf, ret->size);
+ if (err)
+ goto fail;
+
+ err = ver->fini ? ver->fini (context) : GRUB_ERR_NONE;
+ if (err)
+ goto fail;
+
+ if (ver->close)
+ ver->close (context);
+ }
+
+ verified->file = io;
+ ret->data = verified;
+ return ret;
+
+ fail:
+ if (ver->close)
+ ver->close (context);
+ fail_noclose:
+ verified_free (verified);
+ grub_free (ret);
+ return NULL;
+}
+
+grub_err_t
+grub_verify_string (char *str, enum grub_verify_string_type type)
+{
+ struct grub_file_verifier *ver;
+
+ grub_dprintf ("verify", "string: %s, type: %d\n", str, type);
+
+ FOR_LIST_ELEMENTS(ver, grub_file_verifiers)
+ {
+ grub_err_t err;
+ err = ver->verify_string ? ver->verify_string (str, type) : GRUB_ERR_NONE;
+ if (err)
+ return err;
+ }
+ return GRUB_ERR_NONE;
+}
+
+void
+grub_verifiers_init (void)
+{
+ grub_file_filter_register (GRUB_FILE_FILTER_VERIFY, grub_verifiers_open);
+}
diff --git a/grub-core/kern/vga_init.c b/grub-core/kern/vga_init.c
new file mode 100644
index 0000000..3fe2f16
--- /dev/null
+++ b/grub-core/kern/vga_init.c
@@ -0,0 +1,128 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2010,2011 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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __mips__
+#include <grub/pci.h>
+#include <grub/mm.h>
+#endif
+#include <grub/machine/kernel.h>
+#include <grub/misc.h>
+#include <grub/vga.h>
+
+static struct {grub_uint8_t r, g, b, a; } colors[] =
+ {
+ // {R, G, B, A}
+ {0x00, 0x00, 0x00, 0xFF}, // 0 = black
+ {0x00, 0x00, 0xA8, 0xFF}, // 1 = blue
+ {0x00, 0xA8, 0x00, 0xFF}, // 2 = green
+ {0x00, 0xA8, 0xA8, 0xFF}, // 3 = cyan
+ {0xA8, 0x00, 0x00, 0xFF}, // 4 = red
+ {0xA8, 0x00, 0xA8, 0xFF}, // 5 = magenta
+ {0xA8, 0x54, 0x00, 0xFF}, // 6 = brown
+ {0xA8, 0xA8, 0xA8, 0xFF}, // 7 = light gray
+
+ {0x54, 0x54, 0x54, 0xFF}, // 8 = dark gray
+ {0x54, 0x54, 0xFE, 0xFF}, // 9 = bright blue
+ {0x54, 0xFE, 0x54, 0xFF}, // 10 = bright green
+ {0x54, 0xFE, 0xFE, 0xFF}, // 11 = bright cyan
+ {0xFE, 0x54, 0x54, 0xFF}, // 12 = bright red
+ {0xFE, 0x54, 0xFE, 0xFF}, // 13 = bright magenta
+ {0xFE, 0xFE, 0x54, 0xFF}, // 14 = yellow
+ {0xFE, 0xFE, 0xFE, 0xFF} // 15 = white
+ };
+
+#include <ascii.h>
+
+#ifdef __mips__
+#define VGA_ADDR 0xb00a0000
+#else
+#define VGA_ADDR 0xa0000
+#endif
+
+static void
+load_font (void)
+{
+ unsigned i;
+
+ grub_vga_gr_write (0 << 2, GRUB_VGA_GR_GR6);
+
+ grub_vga_sr_write (GRUB_VGA_SR_MEMORY_MODE_NORMAL, GRUB_VGA_SR_MEMORY_MODE);
+ grub_vga_sr_write (1 << GRUB_VGA_TEXT_FONT_PLANE,
+ GRUB_VGA_SR_MAP_MASK_REGISTER);
+
+ grub_vga_gr_write (0, GRUB_VGA_GR_DATA_ROTATE);
+ grub_vga_gr_write (0, GRUB_VGA_GR_MODE);
+ grub_vga_gr_write (0xff, GRUB_VGA_GR_BITMASK);
+
+ for (i = 0; i < 128; i++)
+ grub_memcpy ((void *) (VGA_ADDR + 32 * i), ascii_bitmaps + 16 * i, 16);
+}
+
+static void
+load_palette (void)
+{
+ unsigned i;
+ for (i = 0; i < 16; i++)
+ grub_vga_write_arx (i, i);
+
+ for (i = 0; i < ARRAY_SIZE (colors); i++)
+ grub_vga_palette_write (i, colors[i].r, colors[i].g, colors[i].b);
+}
+
+void
+grub_qemu_init_cirrus (void)
+{
+ grub_outb (GRUB_VGA_IO_MISC_COLOR,
+ GRUB_MACHINE_PCI_IO_BASE + GRUB_VGA_IO_MISC_WRITE);
+
+ load_font ();
+
+ grub_vga_gr_write (GRUB_VGA_GR_GR6_MMAP_CGA, GRUB_VGA_GR_GR6);
+ grub_vga_gr_write (GRUB_VGA_GR_MODE_ODD_EVEN, GRUB_VGA_GR_MODE);
+
+ grub_vga_sr_write (GRUB_VGA_SR_MEMORY_MODE_NORMAL, GRUB_VGA_SR_MEMORY_MODE);
+
+ grub_vga_sr_write ((1 << GRUB_VGA_TEXT_TEXT_PLANE)
+ | (1 << GRUB_VGA_TEXT_ATTR_PLANE),
+ GRUB_VGA_SR_MAP_MASK_REGISTER);
+
+ grub_vga_cr_write (15, GRUB_VGA_CR_CELL_HEIGHT);
+ grub_vga_cr_write (79, GRUB_VGA_CR_HORIZ_END);
+ grub_vga_cr_write (40, GRUB_VGA_CR_PITCH);
+
+ int vert = 25 * 16;
+ grub_vga_cr_write (vert & 0xff, GRUB_VGA_CR_VDISPLAY_END);
+ grub_vga_cr_write (((vert >> GRUB_VGA_CR_OVERFLOW_HEIGHT1_SHIFT)
+ & GRUB_VGA_CR_OVERFLOW_HEIGHT1_MASK)
+ | ((vert >> GRUB_VGA_CR_OVERFLOW_HEIGHT2_SHIFT)
+ & GRUB_VGA_CR_OVERFLOW_HEIGHT2_MASK),
+ GRUB_VGA_CR_OVERFLOW);
+
+ load_palette ();
+
+ grub_vga_write_arx (GRUB_VGA_ARX_MODE_TEXT, GRUB_VGA_ARX_MODE);
+ grub_vga_write_arx (0, GRUB_VGA_ARX_COLOR_SELECT);
+
+ grub_vga_sr_write (GRUB_VGA_SR_CLOCKING_MODE_8_DOT_CLOCK,
+ GRUB_VGA_SR_CLOCKING_MODE);
+
+ grub_vga_cr_write (14, GRUB_VGA_CR_CURSOR_START);
+ grub_vga_cr_write (15, GRUB_VGA_CR_CURSOR_END);
+
+ grub_outb (0x20, GRUB_MACHINE_PCI_IO_BASE + GRUB_VGA_IO_ARX);
+}
diff --git a/grub-core/kern/x86_64/dl.c b/grub-core/kern/x86_64/dl.c
new file mode 100644
index 0000000..e5a8bdc
--- /dev/null
+++ b/grub-core/kern/x86_64/dl.c
@@ -0,0 +1,121 @@
+/* dl-x86_64.c - arch-dependent part of loadable module support */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2002,2005,2007,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/dl.h>
+#include <grub/elf.h>
+#include <grub/misc.h>
+#include <grub/err.h>
+#include <grub/i18n.h>
+
+/* Check if EHDR is a valid ELF header. */
+grub_err_t
+grub_arch_dl_check_header (void *ehdr)
+{
+ Elf64_Ehdr *e = ehdr;
+
+ /* Check the magic numbers. */
+ if (e->e_ident[EI_CLASS] != ELFCLASS64
+ || e->e_ident[EI_DATA] != ELFDATA2LSB
+ || e->e_machine != EM_X86_64)
+ return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF magic"));
+
+ return GRUB_ERR_NONE;
+}
+
+/* Relocate symbols. */
+grub_err_t
+grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
+ Elf_Shdr *s, grub_dl_segment_t seg)
+{
+ Elf64_Rela *rel, *max;
+
+ for (rel = (Elf64_Rela *) ((char *) ehdr + s->sh_offset),
+ max = (Elf64_Rela *) ((char *) rel + s->sh_size);
+ rel < max;
+ rel = (Elf64_Rela *) ((char *) rel + s->sh_entsize))
+ {
+ Elf64_Word *addr32;
+ Elf64_Xword *addr64;
+ Elf64_Sym *sym;
+
+ if (seg->size < rel->r_offset)
+ return grub_error (GRUB_ERR_BAD_MODULE,
+ "reloc offset is out of the segment");
+
+ addr32 = (Elf64_Word *) ((char *) seg->addr + rel->r_offset);
+ addr64 = (Elf64_Xword *) addr32;
+ sym = (Elf64_Sym *) ((char *) mod->symtab
+ + mod->symsize * ELF_R_SYM (rel->r_info));
+
+ switch (ELF_R_TYPE (rel->r_info))
+ {
+ case R_X86_64_64:
+ *addr64 += rel->r_addend + sym->st_value;
+ break;
+
+ case R_X86_64_PC32:
+ case R_X86_64_PLT32:
+ {
+ grub_int64_t value;
+ value = ((grub_int32_t) *addr32) + rel->r_addend + sym->st_value -
+ (Elf64_Xword) (grub_addr_t) seg->addr - rel->r_offset;
+ if (value != (grub_int32_t) value)
+ return grub_error (GRUB_ERR_BAD_MODULE, "relocation out of range");
+ *addr32 = value;
+ }
+ break;
+
+ case R_X86_64_PC64:
+ {
+ *addr64 += rel->r_addend + sym->st_value -
+ (Elf64_Xword) (grub_addr_t) seg->addr - rel->r_offset;
+ }
+ break;
+
+ case R_X86_64_32:
+ {
+ grub_uint64_t value = *addr32 + rel->r_addend + sym->st_value;
+ if (value != (grub_uint32_t) value)
+ return grub_error (GRUB_ERR_BAD_MODULE, "relocation out of range");
+ *addr32 = value;
+ }
+ break;
+ case R_X86_64_32S:
+ {
+ grub_int64_t value = ((grub_int32_t) *addr32) + rel->r_addend + sym->st_value;
+ if (value != (grub_int32_t) value)
+ return grub_error (GRUB_ERR_BAD_MODULE, "relocation out of range");
+ *addr32 = value;
+ }
+ break;
+
+ default:
+ {
+ char rel_info[17]; /* log16(2^64) = 16, plus NUL. */
+
+ grub_snprintf (rel_info, sizeof (rel_info) - 1, "%" PRIxGRUB_UINT64_T,
+ ELF_R_TYPE (rel->r_info));
+ return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
+ N_("relocation 0x%s is not implemented yet"), rel_info);
+ }
+ }
+ }
+
+ return GRUB_ERR_NONE;
+}
diff --git a/grub-core/kern/x86_64/efi/callwrap.S b/grub-core/kern/x86_64/efi/callwrap.S
new file mode 100644
index 0000000..1337fd9
--- /dev/null
+++ b/grub-core/kern/x86_64/efi/callwrap.S
@@ -0,0 +1,129 @@
+/* callwrap.S - wrapper for x86_64 efi calls */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2006,2007,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <grub/symbol.h>
+
+/*
+ * x86_64 uses registry to pass parameters. Unfortunately, gcc and efi use
+ * different call conversion, so we need to do some conversion.
+ *
+ * gcc:
+ * %rdi, %rsi, %rdx, %rcx, %r8, %r9, 8(%rsp), 16(%rsp), ...
+ *
+ * efi:
+ * %rcx, %rdx, %r8, %r9, 32(%rsp), 40(%rsp), 48(%rsp), ...
+ *
+ */
+
+ .file "callwrap.S"
+ .text
+
+FUNCTION(efi_wrap_0)
+ subq $40, %rsp
+ call *%rdi
+ addq $40, %rsp
+ ret
+
+FUNCTION(efi_wrap_1)
+ subq $40, %rsp
+ mov %rsi, %rcx
+ call *%rdi
+ addq $40, %rsp
+ ret
+
+FUNCTION(efi_wrap_2)
+ subq $40, %rsp
+ mov %rsi, %rcx
+ call *%rdi
+ addq $40, %rsp
+ ret
+
+FUNCTION(efi_wrap_3)
+ subq $40, %rsp
+ mov %rcx, %r8
+ mov %rsi, %rcx
+ call *%rdi
+ addq $40, %rsp
+ ret
+
+FUNCTION(efi_wrap_4)
+ subq $40, %rsp
+ mov %r8, %r9
+ mov %rcx, %r8
+ mov %rsi, %rcx
+ call *%rdi
+ addq $40, %rsp
+ ret
+
+FUNCTION(efi_wrap_5)
+ subq $40, %rsp
+ mov %r9, 32(%rsp)
+ mov %r8, %r9
+ mov %rcx, %r8
+ mov %rsi, %rcx
+ call *%rdi
+ addq $40, %rsp
+ ret
+
+FUNCTION(efi_wrap_6)
+ subq $56, %rsp
+ mov 56+8(%rsp), %rax
+ mov %rax, 40(%rsp)
+ mov %r9, 32(%rsp)
+ mov %r8, %r9
+ mov %rcx, %r8
+ mov %rsi, %rcx
+ call *%rdi
+ addq $56, %rsp
+ ret
+
+FUNCTION(efi_wrap_7)
+ subq $88, %rsp
+ mov 88+16(%rsp), %rax
+ mov %rax, 48(%rsp)
+ mov 88+8(%rsp), %rax
+ mov %rax, 40(%rsp)
+ mov %r9, 32(%rsp)
+ mov %r8, %r9
+ mov %rcx, %r8
+ mov %rsi, %rcx
+ call *%rdi
+ addq $88, %rsp
+ ret
+
+FUNCTION(efi_wrap_10)
+ subq $88, %rsp
+ mov 88+40(%rsp), %rax
+ mov %rax, 72(%rsp)
+ mov 88+32(%rsp), %rax
+ mov %rax, 64(%rsp)
+ mov 88+24(%rsp), %rax
+ mov %rax, 56(%rsp)
+ mov 88+16(%rsp), %rax
+ mov %rax, 48(%rsp)
+ mov 88+8(%rsp), %rax
+ mov %rax, 40(%rsp)
+ mov %r9, 32(%rsp)
+ mov %r8, %r9
+ mov %rcx, %r8
+ mov %rsi, %rcx
+ call *%rdi
+ addq $88, %rsp
+ ret
diff --git a/grub-core/kern/x86_64/efi/startup.S b/grub-core/kern/x86_64/efi/startup.S
new file mode 100644
index 0000000..9357e5c
--- /dev/null
+++ b/grub-core/kern/x86_64/efi/startup.S
@@ -0,0 +1,35 @@
+/* startup.S - bootstrap GRUB itself */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2006,2007,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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <grub/symbol.h>
+
+ .file "startup.S"
+ .text
+ .globl start, _start
+ .code64
+
+start:
+_start:
+ movq %rcx, EXT_C(grub_efi_image_handle)(%rip)
+ movq %rdx, EXT_C(grub_efi_system_table)(%rip)
+
+ andq $~0xf, %rsp
+ call EXT_C(grub_main)
+ /* Doesn't return. */
diff --git a/grub-core/kern/x86_64/xen/hypercall.S b/grub-core/kern/x86_64/xen/hypercall.S
new file mode 100644
index 0000000..9b04db6
--- /dev/null
+++ b/grub-core/kern/x86_64/xen/hypercall.S
@@ -0,0 +1,53 @@
+/* hypercall.S - wrappers for Xen hypercalls */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2011 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/symbol.h>
+#include <grub/xen.h>
+
+FUNCTION(grub_xen_sched_op)
+ movq $__HYPERVISOR_sched_op, %rax
+ syscall
+ ret
+
+FUNCTION(grub_xen_event_channel_op)
+ movq $__HYPERVISOR_event_channel_op, %rax
+ syscall
+ ret
+
+FUNCTION(grub_xen_update_va_mapping)
+ movq $__HYPERVISOR_update_va_mapping, %rax
+ syscall
+ ret
+
+FUNCTION(grub_xen_mmuext_op)
+ movq %rcx, %r10
+ movq $__HYPERVISOR_mmuext_op, %rax
+ syscall
+ ret
+
+FUNCTION(grub_xen_grant_table_op)
+ movq $__HYPERVISOR_grant_table_op, %rax
+ syscall
+ ret
+
+FUNCTION(grub_xen_mmu_update)
+ movq %rcx, %r10
+ movq $__HYPERVISOR_mmu_update, %rax
+ syscall
+ ret
diff --git a/grub-core/kern/x86_64/xen/startup.S b/grub-core/kern/x86_64/xen/startup.S
new file mode 100644
index 0000000..21a139f
--- /dev/null
+++ b/grub-core/kern/x86_64/xen/startup.S
@@ -0,0 +1,39 @@
+/* startup.S - bootstrap GRUB itself */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2013 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <grub/symbol.h>
+
+ .file "startup.S"
+ .text
+ .globl start, _start
+ .code64
+
+start:
+_start:
+ leaq LOCAL(stack_end), %rsp
+ movq %rsi, EXT_C(grub_xen_start_page_addr)(%rip)
+
+ andq $~0xf, %rsp
+ call EXT_C(grub_main)
+ /* Doesn't return. */
+
+ .bss
+ .space (1 << 22)
+LOCAL(stack_end):
diff --git a/grub-core/kern/xen/init.c b/grub-core/kern/xen/init.c
new file mode 100644
index 0000000..782ca72
--- /dev/null
+++ b/grub-core/kern/xen/init.c
@@ -0,0 +1,601 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2011 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/xen.h>
+#include <grub/term.h>
+#include <grub/misc.h>
+#include <grub/env.h>
+#include <grub/mm.h>
+#include <grub/kernel.h>
+#include <grub/offsets.h>
+#include <grub/memory.h>
+#include <grub/i386/tsc.h>
+#include <grub/term.h>
+#include <grub/loader.h>
+
+grub_addr_t grub_modbase;
+struct start_info *grub_xen_start_page_addr;
+volatile struct xencons_interface *grub_xen_xcons;
+volatile struct shared_info *grub_xen_shared_info;
+volatile struct xenstore_domain_interface *grub_xen_xenstore;
+volatile grant_entry_v1_t *grub_xen_grant_table;
+static const grub_size_t total_grants =
+ GRUB_XEN_PAGE_SIZE / sizeof (grub_xen_grant_table[0]);
+grub_size_t grub_xen_n_allocated_shared_pages;
+
+static grub_xen_mfn_t
+grub_xen_ptr2mfn (void *ptr)
+{
+#ifdef GRUB_MACHINE_XEN
+ grub_xen_mfn_t *mfn_list =
+ (grub_xen_mfn_t *) grub_xen_start_page_addr->mfn_list;
+ return mfn_list[(grub_addr_t) ptr >> GRUB_XEN_LOG_PAGE_SIZE];
+#else
+ return (grub_addr_t) ptr >> GRUB_XEN_LOG_PAGE_SIZE;
+#endif
+}
+
+void *
+grub_xen_alloc_shared_page (domid_t dom, grub_xen_grant_t * grnum)
+{
+ void *ret;
+ grub_xen_mfn_t mfn;
+ volatile grant_entry_v1_t *entry;
+
+ /* Avoid 0. */
+ for (entry = grub_xen_grant_table;
+ entry < grub_xen_grant_table + total_grants; entry++)
+ if (!entry->flags)
+ break;
+
+ if (entry == grub_xen_grant_table + total_grants)
+ {
+ grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of grant entries");
+ return NULL;
+ }
+ ret = grub_memalign (GRUB_XEN_PAGE_SIZE, GRUB_XEN_PAGE_SIZE);
+ if (!ret)
+ return NULL;
+ mfn = grub_xen_ptr2mfn (ret);
+ entry->frame = mfn;
+ entry->domid = dom;
+ mb ();
+ entry->flags = GTF_permit_access;
+ mb ();
+ *grnum = entry - grub_xen_grant_table;
+ grub_xen_n_allocated_shared_pages++;
+ return ret;
+}
+
+void
+grub_xen_free_shared_page (void *ptr)
+{
+ grub_xen_mfn_t mfn;
+ volatile grant_entry_v1_t *entry;
+
+ mfn = grub_xen_ptr2mfn (ptr);
+ for (entry = grub_xen_grant_table + 1;
+ entry < grub_xen_grant_table + total_grants; entry++)
+ if (entry->flags && entry->frame == mfn)
+ {
+ mb ();
+ entry->flags = 0;
+ mb ();
+ entry->frame = 0;
+ mb ();
+ }
+ grub_xen_n_allocated_shared_pages--;
+}
+
+void
+grub_machine_get_bootlocation (char **device __attribute__ ((unused)),
+ char **path __attribute__ ((unused)))
+{
+}
+
+void
+grub_xen_store_send (const void *buf_, grub_size_t len)
+{
+ const grub_uint8_t *buf = buf_;
+ struct evtchn_send send;
+ int event_sent = 0;
+ while (len)
+ {
+ grub_size_t avail, inbuf;
+ grub_size_t prod, cons;
+ mb ();
+ prod = grub_xen_xenstore->req_prod;
+ cons = grub_xen_xenstore->req_cons;
+ if (prod >= cons + sizeof (grub_xen_xenstore->req))
+ {
+ if (!event_sent)
+ {
+ send.port = grub_xen_start_page_addr->store_evtchn;
+ grub_xen_event_channel_op (EVTCHNOP_send, &send);
+ event_sent = 1;
+ }
+ grub_xen_sched_op (SCHEDOP_yield, 0);
+ continue;
+ }
+ event_sent = 0;
+ avail = cons + sizeof (grub_xen_xenstore->req) - prod;
+ inbuf = (~prod & (sizeof (grub_xen_xenstore->req) - 1)) + 1;
+ if (avail > inbuf)
+ avail = inbuf;
+ if (avail > len)
+ avail = len;
+ grub_memcpy ((void *) &grub_xen_xenstore->req[prod & (sizeof (grub_xen_xenstore->req) - 1)],
+ buf, avail);
+ buf += avail;
+ len -= avail;
+ mb ();
+ grub_xen_xenstore->req_prod += avail;
+ mb ();
+ if (!event_sent)
+ {
+ send.port = grub_xen_start_page_addr->store_evtchn;
+ grub_xen_event_channel_op (EVTCHNOP_send, &send);
+ event_sent = 1;
+ }
+ grub_xen_sched_op (SCHEDOP_yield, 0);
+ }
+}
+
+void
+grub_xen_store_recv (void *buf_, grub_size_t len)
+{
+ grub_uint8_t *buf = buf_;
+ struct evtchn_send send;
+ int event_sent = 0;
+ while (len)
+ {
+ grub_size_t avail, inbuf;
+ grub_size_t prod, cons;
+ mb ();
+ prod = grub_xen_xenstore->rsp_prod;
+ cons = grub_xen_xenstore->rsp_cons;
+ if (prod <= cons)
+ {
+ if (!event_sent)
+ {
+ send.port = grub_xen_start_page_addr->store_evtchn;
+ grub_xen_event_channel_op (EVTCHNOP_send, &send);
+ event_sent = 1;
+ }
+ grub_xen_sched_op (SCHEDOP_yield, 0);
+ continue;
+ }
+ event_sent = 0;
+ avail = prod - cons;
+ inbuf = (~cons & (sizeof (grub_xen_xenstore->req) - 1)) + 1;
+ if (avail > inbuf)
+ avail = inbuf;
+ if (avail > len)
+ avail = len;
+ grub_memcpy (buf,
+ (void *) &grub_xen_xenstore->rsp[cons & (sizeof (grub_xen_xenstore->rsp) - 1)],
+ avail);
+ buf += avail;
+ len -= avail;
+ mb ();
+ grub_xen_xenstore->rsp_cons += avail;
+ mb ();
+ if (!event_sent)
+ {
+ send.port = grub_xen_start_page_addr->store_evtchn;
+ grub_xen_event_channel_op(EVTCHNOP_send, &send);
+ event_sent = 1;
+ }
+ grub_xen_sched_op(SCHEDOP_yield, 0);
+ }
+}
+
+void *
+grub_xenstore_get_file (const char *dir, grub_size_t *len)
+{
+ struct xsd_sockmsg msg;
+ char *buf;
+ grub_size_t dirlen = grub_strlen (dir) + 1;
+
+ if (len)
+ *len = 0;
+
+ grub_memset (&msg, 0, sizeof (msg));
+ msg.type = XS_READ;
+ msg.len = dirlen;
+ grub_xen_store_send (&msg, sizeof (msg));
+ grub_xen_store_send (dir, dirlen);
+ grub_xen_store_recv (&msg, sizeof (msg));
+ buf = grub_malloc (msg.len + 1);
+ if (!buf)
+ return NULL;
+ grub_dprintf ("xen", "msg type = %d, len = %d\n", msg.type, msg.len);
+ grub_xen_store_recv (buf, msg.len);
+ buf[msg.len] = '\0';
+ if (msg.type == XS_ERROR)
+ {
+ grub_error (GRUB_ERR_IO, "couldn't read xenstorage `%s': %s", dir, buf);
+ grub_free (buf);
+ return NULL;
+ }
+ if (len)
+ *len = msg.len;
+ return buf;
+}
+
+grub_err_t
+grub_xenstore_write_file (const char *dir, const void *buf, grub_size_t len)
+{
+ struct xsd_sockmsg msg;
+ grub_size_t dirlen = grub_strlen (dir) + 1;
+ char *resp;
+
+ grub_memset (&msg, 0, sizeof (msg));
+ msg.type = XS_WRITE;
+ msg.len = dirlen + len;
+ grub_xen_store_send (&msg, sizeof (msg));
+ grub_xen_store_send (dir, dirlen);
+ grub_xen_store_send (buf, len);
+ grub_xen_store_recv (&msg, sizeof (msg));
+ resp = grub_malloc (msg.len + 1);
+ if (!resp)
+ return grub_errno;
+ grub_dprintf ("xen", "msg type = %d, len = %d\n", msg.type, msg.len);
+ grub_xen_store_recv (resp, msg.len);
+ resp[msg.len] = '\0';
+ if (msg.type == XS_ERROR)
+ {
+ grub_dprintf ("xen", "error = %s\n", resp);
+ grub_error (GRUB_ERR_IO, "couldn't read xenstorage `%s': %s",
+ dir, resp);
+ grub_free (resp);
+ return grub_errno;
+ }
+ grub_free (resp);
+ return GRUB_ERR_NONE;
+}
+
+/* FIXME: error handling. */
+grub_err_t
+grub_xenstore_dir (const char *dir,
+ int (*hook) (const char *dir, void *hook_data),
+ void *hook_data)
+{
+ struct xsd_sockmsg msg;
+ char *buf;
+ char *ptr;
+ grub_size_t dirlen = grub_strlen (dir) + 1;
+
+ grub_memset (&msg, 0, sizeof (msg));
+ msg.type = XS_DIRECTORY;
+ msg.len = dirlen;
+ grub_xen_store_send (&msg, sizeof (msg));
+ grub_xen_store_send (dir, dirlen);
+ grub_xen_store_recv (&msg, sizeof (msg));
+ buf = grub_malloc (msg.len + 1);
+ if (!buf)
+ return grub_errno;
+ grub_dprintf ("xen", "msg type = %d, len = %d\n", msg.type, msg.len);
+ grub_xen_store_recv (buf, msg.len);
+ buf[msg.len] = '\0';
+ if (msg.type == XS_ERROR)
+ {
+ grub_err_t err;
+ err = grub_error (GRUB_ERR_IO, "couldn't read xenstorage `%s': %s",
+ dir, buf);
+ grub_free (buf);
+ return err;
+ }
+ for (ptr = buf; ptr < buf + msg.len; ptr += grub_strlen (ptr) + 1)
+ if (hook (ptr, hook_data))
+ break;
+ grub_free (buf);
+ return grub_errno;
+}
+
+unsigned long gntframe = 0;
+
+static void
+grub_xen_setup_gnttab (void)
+{
+ struct gnttab_set_version gnttab_setver;
+ struct gnttab_setup_table gnttab_setup;
+
+ grub_memset (&gnttab_setver, 0, sizeof (gnttab_setver));
+
+ gnttab_setver.version = 1;
+ grub_xen_grant_table_op (GNTTABOP_set_version, &gnttab_setver, 1);
+
+ grub_memset (&gnttab_setup, 0, sizeof (gnttab_setup));
+ gnttab_setup.dom = DOMID_SELF;
+ gnttab_setup.nr_frames = 1;
+ gnttab_setup.frame_list.p = &gntframe;
+
+ grub_xen_grant_table_op (GNTTABOP_setup_table, &gnttab_setup, 1);
+}
+
+#ifdef GRUB_MACHINE_XEN
+static grub_uint8_t window[GRUB_XEN_PAGE_SIZE]
+ __attribute__ ((aligned (GRUB_XEN_PAGE_SIZE)));
+
+#ifdef __x86_64__
+#define NUMBER_OF_LEVELS 4
+#else
+#define NUMBER_OF_LEVELS 3
+#endif
+
+#define LOG_POINTERS_PER_PAGE 9
+#define POINTERS_PER_PAGE (1 << LOG_POINTERS_PER_PAGE)
+
+#define MAX_N_UNUSABLE_PAGES 4
+
+static int
+grub_xen_is_page_usable (grub_xen_mfn_t mfn)
+{
+ if (mfn == grub_xen_start_page_addr->console.domU.mfn)
+ return 0;
+ if (mfn == grub_xen_start_page_addr->shared_info)
+ return 0;
+ if (mfn == grub_xen_start_page_addr->store_mfn)
+ return 0;
+ if (mfn == gntframe)
+ return 0;
+ return 1;
+}
+
+static grub_uint64_t
+page2offset (grub_uint64_t page)
+{
+ return page << 12;
+}
+
+#if defined (__x86_64__) && defined (__code_model_large__)
+#define MAX_TOTAL_PAGES (1LL << (64 - 12))
+#elif defined (__x86_64__)
+#define MAX_TOTAL_PAGES (1LL << (31 - 12))
+#else
+#define MAX_TOTAL_PAGES (1LL << (32 - 12))
+#endif
+
+static void
+map_all_pages (void)
+{
+ grub_uint64_t total_pages = grub_xen_start_page_addr->nr_pages;
+ grub_uint64_t i, j;
+ grub_xen_mfn_t *mfn_list =
+ (grub_xen_mfn_t *) grub_xen_start_page_addr->mfn_list;
+ grub_uint64_t *pg = (grub_uint64_t *) window;
+ grub_uint64_t oldpgstart, oldpgend;
+ grub_size_t n_unusable_pages = 0;
+ struct mmu_update m2p_updates[2 * MAX_N_UNUSABLE_PAGES];
+
+ if (total_pages > MAX_TOTAL_PAGES - 4)
+ total_pages = MAX_TOTAL_PAGES - 4;
+
+ for (j = 0; j < total_pages - n_unusable_pages; j++)
+ while (!grub_xen_is_page_usable (mfn_list[j]))
+ {
+ grub_xen_mfn_t t;
+ if (n_unusable_pages >= MAX_N_UNUSABLE_PAGES)
+ {
+ struct sched_shutdown arg;
+ arg.reason = SHUTDOWN_crash;
+ grub_xen_sched_op (SCHEDOP_shutdown, &arg);
+ while (1);
+ }
+ t = mfn_list[j];
+ mfn_list[j] = mfn_list[total_pages - n_unusable_pages - 1];
+ mfn_list[total_pages - n_unusable_pages - 1] = t;
+
+ m2p_updates[2 * n_unusable_pages].ptr
+ = page2offset (mfn_list[j]) | MMU_MACHPHYS_UPDATE;
+ m2p_updates[2 * n_unusable_pages].val = j;
+ m2p_updates[2 * n_unusable_pages + 1].ptr
+ = page2offset (mfn_list[total_pages - n_unusable_pages - 1])
+ | MMU_MACHPHYS_UPDATE;
+ m2p_updates[2 * n_unusable_pages + 1].val = total_pages
+ - n_unusable_pages - 1;
+
+ n_unusable_pages++;
+ }
+
+ grub_xen_mmu_update (m2p_updates, 2 * n_unusable_pages, NULL, DOMID_SELF);
+
+ total_pages += 4;
+
+ grub_uint64_t lx[NUMBER_OF_LEVELS], nlx;
+ grub_uint64_t paging_start = total_pages - 4 - n_unusable_pages, curpage;
+
+ for (nlx = total_pages, i = 0; i < (unsigned) NUMBER_OF_LEVELS; i++)
+ {
+ nlx = (nlx + POINTERS_PER_PAGE - 1) >> LOG_POINTERS_PER_PAGE;
+ /* PAE wants all 4 root directories present. */
+#ifdef __i386__
+ if (i == 1)
+ nlx = 4;
+#endif
+ lx[i] = nlx;
+ paging_start -= nlx;
+ }
+
+ oldpgstart = grub_xen_start_page_addr->pt_base >> 12;
+ oldpgend = oldpgstart + grub_xen_start_page_addr->nr_pt_frames;
+
+ curpage = paging_start;
+
+ int l;
+
+ for (l = NUMBER_OF_LEVELS - 1; l >= 1; l--)
+ {
+ for (i = 0; i < lx[l]; i++)
+ {
+ grub_xen_update_va_mapping (&window,
+ page2offset (mfn_list[curpage + i]) | 7,
+ UVMF_INVLPG);
+ grub_memset (&window, 0, sizeof (window));
+
+ for (j = i * POINTERS_PER_PAGE;
+ j < (i + 1) * POINTERS_PER_PAGE && j < lx[l - 1]; j++)
+ pg[j - i * POINTERS_PER_PAGE] =
+ page2offset (mfn_list[curpage + lx[l] + j])
+#ifdef __x86_64__
+ | 4
+#endif
+ | 3;
+ }
+ curpage += lx[l];
+ }
+
+ for (i = 0; i < lx[0]; i++)
+ {
+ grub_xen_update_va_mapping (&window,
+ page2offset (mfn_list[curpage + i]) | 7,
+ UVMF_INVLPG);
+ grub_memset (&window, 0, sizeof (window));
+
+ for (j = i * POINTERS_PER_PAGE;
+ j < (i + 1) * POINTERS_PER_PAGE && j < total_pages; j++)
+ if (j < paging_start && !(j >= oldpgstart && j < oldpgend))
+ pg[j - i * POINTERS_PER_PAGE] = page2offset (mfn_list[j]) | 0x7;
+ else if (j < grub_xen_start_page_addr->nr_pages)
+ pg[j - i * POINTERS_PER_PAGE] = page2offset (mfn_list[j]) | 5;
+ else if (j == grub_xen_start_page_addr->nr_pages)
+ {
+ pg[j - i * POINTERS_PER_PAGE] =
+ page2offset (grub_xen_start_page_addr->console.domU.mfn) | 7;
+ grub_xen_xcons = (void *) (grub_addr_t) page2offset (j);
+ }
+ else if (j == grub_xen_start_page_addr->nr_pages + 1)
+ {
+ pg[j - i * POINTERS_PER_PAGE] =
+ grub_xen_start_page_addr->shared_info | 7;
+ grub_xen_shared_info = (void *) (grub_addr_t) page2offset (j);
+ }
+ else if (j == grub_xen_start_page_addr->nr_pages + 2)
+ {
+ pg[j - i * POINTERS_PER_PAGE] =
+ page2offset (grub_xen_start_page_addr->store_mfn) | 7;
+ grub_xen_xenstore = (void *) (grub_addr_t) page2offset (j);
+ }
+ else if (j == grub_xen_start_page_addr->nr_pages + 3)
+ {
+ pg[j - i * POINTERS_PER_PAGE] = page2offset (gntframe) | 7;
+ grub_xen_grant_table = (void *) (grub_addr_t) page2offset (j);
+ }
+ }
+
+ grub_xen_update_va_mapping (&window, 0, UVMF_INVLPG);
+
+ mmuext_op_t op[3];
+
+ op[0].cmd = MMUEXT_PIN_L1_TABLE + (NUMBER_OF_LEVELS - 1);
+ op[0].arg1.mfn = mfn_list[paging_start];
+ op[1].cmd = MMUEXT_NEW_BASEPTR;
+ op[1].arg1.mfn = mfn_list[paging_start];
+ op[2].cmd = MMUEXT_UNPIN_TABLE;
+ op[2].arg1.mfn = mfn_list[oldpgstart];
+
+ grub_xen_mmuext_op (op, 3, NULL, DOMID_SELF);
+
+ for (i = oldpgstart; i < oldpgend; i++)
+ grub_xen_update_va_mapping ((void *) (grub_addr_t) page2offset (i),
+ page2offset (mfn_list[i]) | 7, UVMF_INVLPG);
+ void *new_start_page, *new_mfn_list;
+ new_start_page = (void *) (grub_addr_t) page2offset (paging_start - 1);
+ grub_memcpy (new_start_page, grub_xen_start_page_addr, 4096);
+ grub_xen_start_page_addr = new_start_page;
+ new_mfn_list = (void *) (grub_addr_t)
+ page2offset (paging_start - 1
+ - ((grub_xen_start_page_addr->nr_pages
+ * sizeof (grub_uint64_t) + 4095) / 4096));
+ grub_memcpy (new_mfn_list, mfn_list, grub_xen_start_page_addr->nr_pages
+ * sizeof (grub_uint64_t));
+ grub_xen_start_page_addr->pt_base = page2offset (paging_start);
+ grub_xen_start_page_addr->mfn_list = (grub_addr_t) new_mfn_list;
+
+ grub_addr_t heap_start = grub_modules_get_end ();
+ grub_addr_t heap_end = (grub_addr_t) new_mfn_list;
+
+ grub_mm_init_region ((void *) heap_start, heap_end - heap_start);
+}
+
+grub_err_t
+grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
+{
+ grub_uint64_t total_pages = grub_xen_start_page_addr->nr_pages;
+ grub_uint64_t usable_pages = grub_xen_start_page_addr->pt_base >> 12;
+ if (hook (0, page2offset (usable_pages), GRUB_MEMORY_AVAILABLE, hook_data))
+ return GRUB_ERR_NONE;
+
+ hook (page2offset (usable_pages), page2offset (total_pages - usable_pages),
+ GRUB_MEMORY_RESERVED, hook_data);
+
+ return GRUB_ERR_NONE;
+}
+#endif
+
+extern char _end[];
+
+void
+grub_machine_init (void)
+{
+#ifdef GRUB_MACHINE_XEN
+#ifdef __i386__
+ grub_xen_vm_assist (VMASST_CMD_enable, VMASST_TYPE_pae_extended_cr3);
+#endif
+#endif
+
+ grub_modbase = ALIGN_UP ((grub_addr_t) _end
+ + GRUB_KERNEL_MACHINE_MOD_GAP,
+ GRUB_KERNEL_MACHINE_MOD_ALIGN);
+
+#ifdef GRUB_MACHINE_XEN_PVH
+ grub_xen_setup_pvh ();
+#endif
+
+ grub_xen_setup_gnttab ();
+
+#ifdef GRUB_MACHINE_XEN
+ map_all_pages ();
+#endif
+
+ grub_console_init ();
+
+ grub_tsc_init ();
+
+ grub_xendisk_init ();
+
+ grub_boot_init ();
+}
+
+void
+grub_exit (void)
+{
+ struct sched_shutdown arg;
+
+ arg.reason = SHUTDOWN_poweroff;
+ grub_xen_sched_op (SCHEDOP_shutdown, &arg);
+ while (1);
+}
+
+void
+grub_machine_fini (int flags __attribute__ ((unused)))
+{
+ grub_xendisk_fini ();
+ grub_boot_fini ();
+}