a5bd9f6
From 68cd2e93537bee644f0595cb0b734fe53cfa0637 Mon Sep 17 00:00:00 2001
a5bd9f6
From: Vladimir 'phcoder' Serbinenko <phcoder@gmail.com>
a5bd9f6
Date: Thu, 7 Mar 2013 08:17:24 +0100
a5bd9f6
Subject: [PATCH 195/364] 	Lift up core size limits on some platforms.
a5bd9f6
 Fix potential memory 	corruption with big core on small memory systems.
a5bd9f6
 Document remaining 	limits.
a5bd9f6
a5bd9f6
---
a5bd9f6
 ChangeLog                           |  6 ++++
a5bd9f6
 docs/grub.texi                      | 38 +++++++++++++++++++++++++
a5bd9f6
 grub-core/kern/i386/coreboot/init.c | 38 ++++++++++++++-----------
a5bd9f6
 grub-core/kern/i386/pc/init.c       | 12 +++++++-
a5bd9f6
 grub-core/kern/main.c               | 56 ++++++++++++++++++++++++++++++++-----
a5bd9f6
 grub-core/kern/mm.c                 | 21 ++++++++++++++
a5bd9f6
 include/grub/kernel.h               | 25 +++++++++++++++++
a5bd9f6
 include/grub/offsets.h              |  1 +
a5bd9f6
 util/grub-mkimage.c                 | 47 +++++++++++++++++++++++++------
a5bd9f6
 9 files changed, 211 insertions(+), 33 deletions(-)
a5bd9f6
a5bd9f6
diff --git a/ChangeLog b/ChangeLog
a5bd9f6
index f1ab52a..96527dd 100644
a5bd9f6
--- a/ChangeLog
a5bd9f6
+++ b/ChangeLog
a5bd9f6
@@ -1,3 +1,9 @@
a5bd9f6
+2013-03-07  Vladimir Serbinenko  <phcoder@gmail.com>
a5bd9f6
+
a5bd9f6
+	Lift up core size limits on some platforms. Fix potential memory
a5bd9f6
+	corruption with big core on small memory systems. Document remaining
a5bd9f6
+	limits.
a5bd9f6
+
a5bd9f6
 2013-03-05  Vladimir Serbinenko  <phcoder@gmail.com>
a5bd9f6
 
a5bd9f6
 	* grub-core/term/terminfo.c (grub_terminfo_cls): Issue an explicit
a5bd9f6
diff --git a/docs/grub.texi b/docs/grub.texi
a5bd9f6
index 9941b47..0b66827 100644
a5bd9f6
--- a/docs/grub.texi
a5bd9f6
+++ b/docs/grub.texi
a5bd9f6
@@ -89,6 +89,7 @@ This edition documents version @value{VERSION}.
a5bd9f6
 * Serial terminal::             Using GRUB via a serial line
a5bd9f6
 * Vendor power-on keys::        Changing GRUB behaviour on vendor power-on keys
a5bd9f6
 * Images::                      GRUB image files
a5bd9f6
+* Core image size limitation::  GRUB image files size limitations
a5bd9f6
 * Filesystem::                  Filesystem syntax and semantics
a5bd9f6
 * Interface::                   The menu and the command-line
a5bd9f6
 * Environment::                 GRUB environment variables
a5bd9f6
@@ -2390,6 +2391,43 @@ In GRUB 2, images for PXE network booting are now constructed using
a5bd9f6
 contains the @samp{pxe} and @samp{pxecmd} modules.  @xref{Network}.
a5bd9f6
 @end table
a5bd9f6
 
a5bd9f6
+@node Core image size limitation
a5bd9f6
+@chapter Core image size limitation
a5bd9f6
+
a5bd9f6
+Heavily limited platforms:
a5bd9f6
+@itemize
a5bd9f6
+@item i386-pc (normal and PXE): the core image size (compressed) is limited by 458240 bytes.
a5bd9f6
+ kernel.img (.text + .data + .bss, uncompressed) is limited by 392704 bytes.
a5bd9f6
+ module size (uncompressed) + kernel.img (.text + .data, uncompressed) is limited by the size of contiguous chunk at 1M address.
a5bd9f6
+@item sparc64-ieee1275: kernel.img (.text + .data + .bss) + modules + 256K (stack) + 2M (heap) is limited by space available at 0x4400. On most platforms it's just 3 or 4M since ieee1275 maps only so much.
a5bd9f6
+@item i386-ieee1275: kernel.img  (.text + .data + .bss) + modules is limited by memory available at 0x10000, at most 596K
a5bd9f6
+@end itemize
a5bd9f6
+
a5bd9f6
+Lightly limited platforms:
a5bd9f6
+
a5bd9f6
+@itemize
a5bd9f6
+@item i386-qemu: kernel.img (.text + .data + .bss) is limited by 392704 bytes.
a5bd9f6
+                 (core.img would be limited by ROM size but it's unlimited on qemu
a5bd9f6
+@item All EFI platforms: limited by contiguous RAM size and possibly firmware bugs
a5bd9f6
+@item Coreboot and multiboot. kernel.img (.text + .data + .bss) is limited by 392704 bytes.
a5bd9f6
+      module size is limited by the size of contiguous chunk at 1M address.
a5bd9f6
+@item mipsel-loongson (ELF), mips(el)-qemu_mips (ELF): if uncompressed:
a5bd9f6
+                kernel.img (.text + .data) + modules is limited by the space from 80200000 forward
a5bd9f6
+                if compressed:
a5bd9f6
+                kernel.img (.text + .data, uncompressed) + modules (uncompressed)
a5bd9f6
+                + (modules + kernel.img (.text + .data)) (compressed)
a5bd9f6
+                + decompressor is limited by the space from 80200000 forward
a5bd9f6
+@item mipsel-loongson (Flash), mips(el)-qemu_mips (Flash): kernel.img (.text + .data) + modules is limited by the space from 80200000 forward
a5bd9f6
+                               core.img (final) is limited by flash size (512K on yeeloong and fulooong)
a5bd9f6
+@item mips-arc: if uncompressed:
a5bd9f6
+                kernel.img (.text + .data) is limited by the space from 8bd00000 forward
a5bd9f6
+                modules + dummy decompressor  is limited by the space from 8bd00000 backward
a5bd9f6
+                if compressed:
a5bd9f6
+                kernel.img (.text + .data, uncompressed) is limited by the space from 8bd00000 forward
a5bd9f6
+                modules (uncompressed) + (modules + kernel.img (.text + .data)) (compressed, aligned to 1M)
a5bd9f6
+                + 1M (decompressor + scratch space) is limited by the space from 8bd00000 backward
a5bd9f6
+@item powerpc-ieee1275: kernel.img (.text + .data + .bss) + modules is limited by space available at 0x200000
a5bd9f6
+@end itemize
a5bd9f6
 
a5bd9f6
 @node Filesystem
a5bd9f6
 @chapter Filesystem syntax and semantics
a5bd9f6
diff --git a/grub-core/kern/i386/coreboot/init.c b/grub-core/kern/i386/coreboot/init.c
a5bd9f6
index 48fd1a6..bfc8f3f 100644
a5bd9f6
--- a/grub-core/kern/i386/coreboot/init.c
a5bd9f6
+++ b/grub-core/kern/i386/coreboot/init.c
a5bd9f6
@@ -54,7 +54,8 @@ grub_exit (void)
a5bd9f6
 #ifdef GRUB_MACHINE_QEMU
a5bd9f6
 grub_addr_t grub_modbase;
a5bd9f6
 #else
a5bd9f6
-grub_addr_t grub_modbase = ALIGN_UP((grub_addr_t) _end, GRUB_KERNEL_MACHINE_MOD_ALIGN);
a5bd9f6
+grub_addr_t grub_modbase = GRUB_KERNEL_I386_COREBOOT_MODULES_ADDR;
a5bd9f6
+static grub_uint64_t modend;
a5bd9f6
 #endif
a5bd9f6
 
a5bd9f6
 /* Helper for grub_machine_init.  */
a5bd9f6
@@ -62,30 +63,32 @@ static int
a5bd9f6
 heap_init (grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type,
a5bd9f6
 	   void *data __attribute__ ((unused)))
a5bd9f6
 {
a5bd9f6
+  grub_uint64_t begin = addr, end = addr + size;
a5bd9f6
+
a5bd9f6
 #if GRUB_CPU_SIZEOF_VOID_P == 4
a5bd9f6
   /* Restrict ourselves to 32-bit memory space.  */
a5bd9f6
-  if (addr > GRUB_ULONG_MAX)
a5bd9f6
+  if (begin > GRUB_ULONG_MAX)
a5bd9f6
     return 0;
a5bd9f6
-  if (addr + size > GRUB_ULONG_MAX)
a5bd9f6
-    size = GRUB_ULONG_MAX - addr;
a5bd9f6
+  if (end > GRUB_ULONG_MAX)
a5bd9f6
+    end = GRUB_ULONG_MAX;
a5bd9f6
 #endif
a5bd9f6
 
a5bd9f6
   if (type != GRUB_MEMORY_AVAILABLE)
a5bd9f6
     return 0;
a5bd9f6
 
a5bd9f6
   /* Avoid the lower memory.  */
a5bd9f6
-  if (addr < GRUB_MEMORY_MACHINE_LOWER_SIZE)
a5bd9f6
-    {
a5bd9f6
-      if (addr + size <= GRUB_MEMORY_MACHINE_LOWER_SIZE)
a5bd9f6
-	return 0;
a5bd9f6
-      else
a5bd9f6
-	{
a5bd9f6
-	  size -= GRUB_MEMORY_MACHINE_LOWER_SIZE - addr;
a5bd9f6
-	  addr = GRUB_MEMORY_MACHINE_LOWER_SIZE;
a5bd9f6
-	}
a5bd9f6
-    }
a5bd9f6
-
a5bd9f6
-  grub_mm_init_region ((void *) (grub_addr_t) addr, (grub_size_t) size);
a5bd9f6
+  if (begin < GRUB_MEMORY_MACHINE_LOWER_SIZE)
a5bd9f6
+    begin = GRUB_MEMORY_MACHINE_LOWER_SIZE;
a5bd9f6
+
a5bd9f6
+#ifndef GRUB_MACHINE_QEMU
a5bd9f6
+  if (modend && begin < modend)
a5bd9f6
+    begin = modend;
a5bd9f6
+#endif
a5bd9f6
+  
a5bd9f6
+  if (end <= begin)
a5bd9f6
+    return 0;
a5bd9f6
+
a5bd9f6
+  grub_mm_init_region ((void *) (grub_addr_t) begin, (grub_size_t) (end - begin));
a5bd9f6
 
a5bd9f6
   return 0;
a5bd9f6
 }
a5bd9f6
@@ -98,6 +101,9 @@ grub_machine_init (void)
a5bd9f6
 
a5bd9f6
   grub_qemu_init_cirrus ();
a5bd9f6
 #endif
a5bd9f6
+#ifndef GRUB_MACHINE_QEMU
a5bd9f6
+  modend = grub_modules_get_end ();
a5bd9f6
+#endif
a5bd9f6
   /* Initialize the console as early as possible.  */
a5bd9f6
   grub_vga_text_init ();
a5bd9f6
 
a5bd9f6
diff --git a/grub-core/kern/i386/pc/init.c b/grub-core/kern/i386/pc/init.c
a5bd9f6
index 7d8b12c..730e04a 100644
a5bd9f6
--- a/grub-core/kern/i386/pc/init.c
a5bd9f6
+++ b/grub-core/kern/i386/pc/init.c
a5bd9f6
@@ -191,6 +191,7 @@ grub_machine_init (void)
a5bd9f6
 #if 0
a5bd9f6
   int grub_lower_mem;
a5bd9f6
 #endif
a5bd9f6
+  grub_addr_t modend;
a5bd9f6
 
a5bd9f6
   grub_modbase = GRUB_MEMORY_MACHINE_DECOMPRESSION_ADDR + (_edata - _start);
a5bd9f6
 
a5bd9f6
@@ -222,8 +223,17 @@ grub_machine_init (void)
a5bd9f6
 
a5bd9f6
   compact_mem_regions ();
a5bd9f6
 
a5bd9f6
+  modend = grub_modules_get_end ();
a5bd9f6
   for (i = 0; i < num_regions; i++)
a5bd9f6
-      grub_mm_init_region ((void *) mem_regions[i].addr, mem_regions[i].size);
a5bd9f6
+    {
a5bd9f6
+      grub_addr_t beg = mem_regions[i].addr;
a5bd9f6
+      grub_addr_t fin = mem_regions[i].addr + mem_regions[i].size;
a5bd9f6
+      if (modend && beg < modend)
a5bd9f6
+	beg = modend;
a5bd9f6
+      if (beg >= fin)
a5bd9f6
+	continue;
a5bd9f6
+      grub_mm_init_region ((void *) beg, fin - beg);
a5bd9f6
+    }
a5bd9f6
 
a5bd9f6
   grub_tsc_init ();
a5bd9f6
 }
a5bd9f6
diff --git a/grub-core/kern/main.c b/grub-core/kern/main.c
a5bd9f6
index c43ac6b..e1a2001 100644
a5bd9f6
--- a/grub-core/kern/main.c
a5bd9f6
+++ b/grub-core/kern/main.c
a5bd9f6
@@ -30,8 +30,10 @@
a5bd9f6
 #include <grub/reader.h>
a5bd9f6
 #include <grub/parser.h>
a5bd9f6
 
a5bd9f6
-/* This is actualy platform-independant but used only on loongson and sparc.  */
a5bd9f6
-#if defined (GRUB_MACHINE_MIPS_LOONGSON) || defined (GRUB_MACHINE_MIPS_QEMU_MIPS) || defined (GRUB_MACHINE_SPARC64)
a5bd9f6
+#ifdef GRUB_MACHINE_PCBIOS
a5bd9f6
+#include <grub/machine/memory.h>
a5bd9f6
+#endif
a5bd9f6
+
a5bd9f6
 grub_addr_t
a5bd9f6
 grub_modules_get_end (void)
a5bd9f6
 {
a5bd9f6
@@ -45,7 +47,6 @@ grub_modules_get_end (void)
a5bd9f6
 
a5bd9f6
   return grub_modbase + modinfo->size;
a5bd9f6
 }
a5bd9f6
-#endif
a5bd9f6
 
a5bd9f6
 /* Load all modules in core.  */
a5bd9f6
 static void
a5bd9f6
@@ -67,6 +68,8 @@ grub_load_modules (void)
a5bd9f6
   }
a5bd9f6
 }
a5bd9f6
 
a5bd9f6
+static char *load_config;
a5bd9f6
+
a5bd9f6
 static void
a5bd9f6
 grub_load_config (void)
a5bd9f6
 {
a5bd9f6
@@ -76,9 +79,17 @@ grub_load_config (void)
a5bd9f6
     /* Not an embedded config, skip.  */
a5bd9f6
     if (header->type != OBJ_TYPE_CONFIG)
a5bd9f6
       continue;
a5bd9f6
-    
a5bd9f6
-    grub_parser_execute ((char *) header +
a5bd9f6
-			 sizeof (struct grub_module_header));
a5bd9f6
+
a5bd9f6
+    load_config = grub_malloc (header->size - sizeof (struct grub_module_header) + 1);
a5bd9f6
+    if (!load_config)
a5bd9f6
+      {
a5bd9f6
+	grub_print_error ();
a5bd9f6
+	break;
a5bd9f6
+      }
a5bd9f6
+    grub_memcpy (load_config, (char *) header +
a5bd9f6
+		 sizeof (struct grub_module_header),
a5bd9f6
+		 header->size - sizeof (struct grub_module_header));
a5bd9f6
+    load_config[header->size - sizeof (struct grub_module_header)] = 0;
a5bd9f6
     break;
a5bd9f6
   }
a5bd9f6
 }
a5bd9f6
@@ -212,6 +223,30 @@ grub_load_normal_mode (void)
a5bd9f6
   grub_command_execute ("normal", 0, 0);
a5bd9f6
 }
a5bd9f6
 
a5bd9f6
+static void
a5bd9f6
+reclaim_module_space (void)
a5bd9f6
+{
a5bd9f6
+  grub_addr_t modstart, modend;
a5bd9f6
+
a5bd9f6
+  if (!grub_modbase)
a5bd9f6
+    return;
a5bd9f6
+
a5bd9f6
+#ifdef GRUB_MACHINE_PCBIOS
a5bd9f6
+  modstart = GRUB_MEMORY_MACHINE_DECOMPRESSION_ADDR;
a5bd9f6
+#else
a5bd9f6
+  modstart = grub_modbase;
a5bd9f6
+#endif
a5bd9f6
+  modend = grub_modules_get_end ();
a5bd9f6
+  grub_modbase = 0;
a5bd9f6
+
a5bd9f6
+#if GRUB_KERNEL_PRELOAD_SPACE_REUSABLE
a5bd9f6
+  grub_mm_init_region ((void *) modstart, modend - modstart);
a5bd9f6
+#else
a5bd9f6
+  (void) modstart;
a5bd9f6
+  (void) modend;
a5bd9f6
+#endif
a5bd9f6
+}
a5bd9f6
+
a5bd9f6
 /* The main routine.  */
a5bd9f6
 void __attribute__ ((noreturn))
a5bd9f6
 grub_main (void)
a5bd9f6
@@ -224,6 +259,8 @@ grub_main (void)
a5bd9f6
   grub_printf ("Welcome to GRUB!\n\n");
a5bd9f6
   grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
a5bd9f6
 
a5bd9f6
+  grub_load_config ();
a5bd9f6
+
a5bd9f6
   /* Load pre-loaded modules and free the space.  */
a5bd9f6
   grub_register_exported_symbols ();
a5bd9f6
 #ifdef GRUB_LINKER_HAVE_INIT
a5bd9f6
@@ -237,9 +274,14 @@ grub_main (void)
a5bd9f6
   grub_env_export ("root");
a5bd9f6
   grub_env_export ("prefix");
a5bd9f6
 
a5bd9f6
+  /* Reclaim space used for modules.  */
a5bd9f6
+  reclaim_module_space ();
a5bd9f6
+
a5bd9f6
   grub_register_core_commands ();
a5bd9f6
 
a5bd9f6
-  grub_load_config ();
a5bd9f6
+  if (load_config)
a5bd9f6
+    grub_parser_execute (load_config);
a5bd9f6
+
a5bd9f6
   grub_load_normal_mode ();
a5bd9f6
   grub_rescue_run ();
a5bd9f6
 }
a5bd9f6
diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c
a5bd9f6
index 9f08e05..d869091 100644
a5bd9f6
--- a/grub-core/kern/mm.c
a5bd9f6
+++ b/grub-core/kern/mm.c
a5bd9f6
@@ -117,6 +117,27 @@ grub_mm_init_region (void *addr, grub_size_t size)
a5bd9f6
   grub_printf ("Using memory for heap: start=%p, end=%p\n", addr, addr + (unsigned int) size);
a5bd9f6
 #endif
a5bd9f6
 
a5bd9f6
+  for (p = &grub_mm_base, q = *p; q; p = &(q->next), q = *p)
a5bd9f6
+    if ((grub_uint8_t *) addr + size + q->pre_size == (grub_uint8_t *) q)
a5bd9f6
+      {
a5bd9f6
+	r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN);
a5bd9f6
+	*r = *q;
a5bd9f6
+	r->pre_size += size;
a5bd9f6
+	
a5bd9f6
+	if (r->pre_size >> GRUB_MM_ALIGN_LOG2)
a5bd9f6
+	  {
a5bd9f6
+	    h = (grub_mm_header_t) (r + 1);
a5bd9f6
+	    h->size = (r->pre_size >> GRUB_MM_ALIGN_LOG2);
a5bd9f6
+	    h->magic = GRUB_MM_ALLOC_MAGIC;
a5bd9f6
+	    r->size += h->size << GRUB_MM_ALIGN_LOG2;
a5bd9f6
+	    r->pre_size &= (GRUB_MM_ALIGN - 1);
a5bd9f6
+	    *p = r;
a5bd9f6
+	    grub_free (h + 1);
a5bd9f6
+	  }
a5bd9f6
+	*p = r;
a5bd9f6
+	return;
a5bd9f6
+      }
a5bd9f6
+
a5bd9f6
   /* Allocate a region from the head.  */
a5bd9f6
   r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN);
a5bd9f6
   size -= (char *) r - (char *) addr + sizeof (*r);
a5bd9f6
diff --git a/include/grub/kernel.h b/include/grub/kernel.h
a5bd9f6
index 23e4f02..73ea416 100644
a5bd9f6
--- a/include/grub/kernel.h
a5bd9f6
+++ b/include/grub/kernel.h
a5bd9f6
@@ -64,6 +64,29 @@ struct grub_module_info64
a5bd9f6
   grub_uint64_t size;
a5bd9f6
 };
a5bd9f6
 
a5bd9f6
+#ifndef GRUB_UTIL
a5bd9f6
+/* Space isn't reusable on some platforms.  */
a5bd9f6
+/* On Qemu the preload space is readonly.  */
a5bd9f6
+/* On emu there is no preload space.  */
a5bd9f6
+/* On ieee1275 our code assumes that heap is p=v which isn't guaranteed for module space.  */
a5bd9f6
+#if defined (GRUB_MACHINE_QEMU) || defined (GRUB_MACHINE_EMU) \
a5bd9f6
+  || defined (GRUB_MACHINE_EFI) \
a5bd9f6
+  || (defined (GRUB_MACHINE_IEEE1275) && !defined (__sparc__))
a5bd9f6
+#define GRUB_KERNEL_PRELOAD_SPACE_REUSABLE 0
a5bd9f6
+#endif
a5bd9f6
+
a5bd9f6
+#if defined (GRUB_MACHINE_PCBIOS) || defined (GRUB_MACHINE_COREBOOT) \
a5bd9f6
+  || defined (GRUB_MACHINE_MULTIBOOT) || defined (GRUB_MACHINE_MIPS_QEMU_MIPS) \
a5bd9f6
+  || defined (GRUB_MACHINE_MIPS_LOONGSON) || defined (GRUB_MACHINE_ARC) \
a5bd9f6
+  || defined (__sparc__)
a5bd9f6
+/* FIXME: stack is between 2 heap regions. Move it.  */
a5bd9f6
+#define GRUB_KERNEL_PRELOAD_SPACE_REUSABLE 1
a5bd9f6
+#endif
a5bd9f6
+
a5bd9f6
+#ifndef GRUB_KERNEL_PRELOAD_SPACE_REUSABLE
a5bd9f6
+#error "Please check if preload space is reusable on this platform!"
a5bd9f6
+#endif
a5bd9f6
+
a5bd9f6
 #if GRUB_TARGET_SIZEOF_VOID_P == 8
a5bd9f6
 #define grub_module_info grub_module_info64
a5bd9f6
 #else
a5bd9f6
@@ -82,6 +105,8 @@ extern grub_addr_t EXPORT_VAR (grub_modbase);
a5bd9f6
 
a5bd9f6
 grub_addr_t grub_modules_get_end (void);
a5bd9f6
 
a5bd9f6
+#endif
a5bd9f6
+
a5bd9f6
 /* The start point of the C code.  */
a5bd9f6
 void grub_main (void) __attribute__ ((noreturn));
a5bd9f6
 
a5bd9f6
diff --git a/include/grub/offsets.h b/include/grub/offsets.h
a5bd9f6
index d55e308..bce755d 100644
a5bd9f6
--- a/include/grub/offsets.h
a5bd9f6
+++ b/include/grub/offsets.h
a5bd9f6
@@ -91,6 +91,7 @@
a5bd9f6
 #define GRUB_KERNEL_MIPS_ARC_TOTAL_MODULE_SIZE	0x08
a5bd9f6
 
a5bd9f6
 #define GRUB_KERNEL_I386_COREBOOT_LINK_ADDR	0x8200
a5bd9f6
+#define GRUB_KERNEL_I386_COREBOOT_MODULES_ADDR  0x100000
a5bd9f6
 
a5bd9f6
 #define GRUB_KERNEL_I386_IEEE1275_LINK_ADDR	0x10000
a5bd9f6
 
a5bd9f6
diff --git a/util/grub-mkimage.c b/util/grub-mkimage.c
a5bd9f6
index 29bda17..845abed 100644
a5bd9f6
--- a/util/grub-mkimage.c
a5bd9f6
+++ b/util/grub-mkimage.c
a5bd9f6
@@ -995,16 +995,42 @@ generate_image (const char *dir, const char *prefix,
a5bd9f6
     {
a5bd9f6
     case IMAGE_I386_PC:
a5bd9f6
     case IMAGE_I386_PC_PXE:
a5bd9f6
-      {
a5bd9f6
-	unsigned num;
a5bd9f6
-	char *boot_path, *boot_img;
a5bd9f6
-	size_t boot_size;
a5bd9f6
-
a5bd9f6
 	if (GRUB_KERNEL_I386_PC_LINK_ADDR + core_size > 0x78000
a5bd9f6
-	    || (core_size > (0xffff << GRUB_DISK_SECTOR_BITS)))
a5bd9f6
+	    || (core_size > (0xffff << GRUB_DISK_SECTOR_BITS))
a5bd9f6
+	    || (kernel_size + bss_size + GRUB_KERNEL_I386_PC_LINK_ADDR > 0x68000))
a5bd9f6
 	  grub_util_error (_("core image is too big (0x%x > 0x%x)"),
a5bd9f6
 			   GRUB_KERNEL_I386_PC_LINK_ADDR + core_size,
a5bd9f6
 			   0x78000);
a5bd9f6
+	/* fallthrough */
a5bd9f6
+    case IMAGE_COREBOOT:
a5bd9f6
+    case IMAGE_QEMU:
a5bd9f6
+	if (kernel_size + bss_size + GRUB_KERNEL_I386_PC_LINK_ADDR > 0x68000)
a5bd9f6
+	  grub_util_error (_("kernel image is too big (0x%x > 0x%x)"),
a5bd9f6
+			   kernel_size + bss_size + GRUB_KERNEL_I386_PC_LINK_ADDR,
a5bd9f6
+			   0x68000);
a5bd9f6
+	break;
a5bd9f6
+    case IMAGE_LOONGSON_ELF:
a5bd9f6
+    case IMAGE_YEELOONG_FLASH:
a5bd9f6
+    case IMAGE_FULOONG2F_FLASH:
a5bd9f6
+    case IMAGE_EFI:
a5bd9f6
+    case IMAGE_MIPS_ARC:
a5bd9f6
+    case IMAGE_QEMU_MIPS_FLASH:
a5bd9f6
+      break;
a5bd9f6
+    case IMAGE_SPARC64_AOUT:
a5bd9f6
+    case IMAGE_SPARC64_RAW:
a5bd9f6
+    case IMAGE_I386_IEEE1275:
a5bd9f6
+    case IMAGE_PPC:
a5bd9f6
+      break;
a5bd9f6
+    }
a5bd9f6
+
a5bd9f6
+  switch (image_target->id)
a5bd9f6
+    {
a5bd9f6
+    case IMAGE_I386_PC:
a5bd9f6
+    case IMAGE_I386_PC_PXE:
a5bd9f6
+      {
a5bd9f6
+	unsigned num;
a5bd9f6
+	char *boot_path, *boot_img;
a5bd9f6
+	size_t boot_size;
a5bd9f6
 
a5bd9f6
 	num = ((core_size + GRUB_DISK_SECTOR_SIZE - 1) >> GRUB_DISK_SECTOR_BITS);
a5bd9f6
 	if (image_target->id == IMAGE_I386_PC_PXE)
a5bd9f6
@@ -1615,9 +1641,12 @@ generate_image (const char *dir, const char *prefix,
a5bd9f6
 	    phdr->p_filesz = phdr->p_memsz
a5bd9f6
 	      = grub_host_to_target32 (core_size - kernel_size);
a5bd9f6
 
a5bd9f6
-	    target_addr_mods = ALIGN_UP (target_addr + kernel_size + bss_size
a5bd9f6
-					 + image_target->mod_gap,
a5bd9f6
-					 image_target->mod_align);
a5bd9f6
+	    if (image_target->id == IMAGE_COREBOOT)
a5bd9f6
+	      target_addr_mods = GRUB_KERNEL_I386_COREBOOT_MODULES_ADDR;
a5bd9f6
+	    else
a5bd9f6
+	      target_addr_mods = ALIGN_UP (target_addr + kernel_size + bss_size
a5bd9f6
+					   + image_target->mod_gap,
a5bd9f6
+					   image_target->mod_align);
a5bd9f6
 	    phdr->p_vaddr = grub_host_to_target32 (target_addr_mods);
a5bd9f6
 	    phdr->p_paddr = grub_host_to_target32 (target_addr_mods);
a5bd9f6
 	    phdr->p_align = grub_host_to_target32 (image_target->link_align);
a5bd9f6
-- 
a5bd9f6
1.8.1.4
a5bd9f6