31004e6
From 4aaa34677317a2d9c574f368e8895a9a2971f002 Mon Sep 17 00:00:00 2001
a5bd9f6
From: Vladimir 'phcoder' Serbinenko <phcoder@gmail.com>
a5bd9f6
Date: Fri, 22 Mar 2013 21:01:28 +0100
f74b50e
Subject: [PATCH 224/482] 	Add ability to generate newc additions on
a5bd9f6
 runtime.
a5bd9f6
a5bd9f6
---
a5bd9f6
 ChangeLog                                 |   4 +
a5bd9f6
 grub-core/Makefile.core.def               |   2 +
a5bd9f6
 grub-core/loader/i386/linux.c             |  40 +---
a5bd9f6
 grub-core/loader/i386/pc/linux.c          |  40 +---
a5bd9f6
 grub-core/loader/ia64/efi/linux.c         |  42 +----
a5bd9f6
 grub-core/loader/linux.c                  | 294 ++++++++++++++++++++++++++++++
a5bd9f6
 grub-core/loader/mips/linux.c             |  40 +---
a5bd9f6
 grub-core/loader/powerpc/ieee1275/linux.c |  41 +----
a5bd9f6
 grub-core/loader/sparc64/ieee1275/linux.c |  40 +---
a5bd9f6
 include/grub/linux.h                      |  24 +++
a5bd9f6
 10 files changed, 366 insertions(+), 201 deletions(-)
a5bd9f6
 create mode 100644 grub-core/loader/linux.c
a5bd9f6
 create mode 100644 include/grub/linux.h
a5bd9f6
a5bd9f6
diff --git a/ChangeLog b/ChangeLog
a5bd9f6
index 4a5b683..5582e13 100644
a5bd9f6
--- a/ChangeLog
a5bd9f6
+++ b/ChangeLog
a5bd9f6
@@ -1,5 +1,9 @@
a5bd9f6
 2013-03-22  Vladimir Serbinenko  <phcoder@gmail.com>
a5bd9f6
 
a5bd9f6
+	Add ability to generate newc additions on runtime.
a5bd9f6
+
a5bd9f6
+2013-03-22  Vladimir Serbinenko  <phcoder@gmail.com>
a5bd9f6
+
a5bd9f6
 	* grub-core/commands/i386/coreboot/cbls.c: Fix typos and wrong
a5bd9f6
 	description.
a5bd9f6
 
a5bd9f6
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
a5bd9f6
index d851bc3..a614b22 100644
a5bd9f6
--- a/grub-core/Makefile.core.def
a5bd9f6
+++ b/grub-core/Makefile.core.def
a5bd9f6
@@ -1406,6 +1406,7 @@ module = {
a5bd9f6
 module = {
a5bd9f6
   name = linux16;
a5bd9f6
   common = loader/i386/pc/linux.c;
a5bd9f6
+  common = loader/linux.c;
a5bd9f6
   common = lib/cmdline.c;
a5bd9f6
   enable = x86;
a5bd9f6
 };
a5bd9f6
@@ -1454,6 +1455,7 @@ module = {
a5bd9f6
   powerpc_ieee1275 = loader/powerpc/ieee1275/linux.c;
a5bd9f6
   sparc64_ieee1275 = loader/sparc64/ieee1275/linux.c;
a5bd9f6
   ia64_efi = loader/ia64/efi/linux.c;
a5bd9f6
+  common = loader/linux.c;
a5bd9f6
   common = lib/cmdline.c;
a5bd9f6
   enable = noemu;
a5bd9f6
 };
a5bd9f6
diff --git a/grub-core/loader/i386/linux.c b/grub-core/loader/i386/linux.c
a5bd9f6
index 92cabfb..bdfe19a 100644
a5bd9f6
--- a/grub-core/loader/i386/linux.c
a5bd9f6
+++ b/grub-core/loader/i386/linux.c
a5bd9f6
@@ -34,6 +34,7 @@
a5bd9f6
 #include <grub/i386/relocator.h>
a5bd9f6
 #include <grub/i18n.h>
a5bd9f6
 #include <grub/lib/cmdline.h>
a5bd9f6
+#include <grub/linux.h>
a5bd9f6
 
a5bd9f6
 GRUB_MOD_LICENSE ("GPLv3+");
a5bd9f6
 
a5bd9f6
@@ -1055,14 +1056,11 @@ static grub_err_t
a5bd9f6
 grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
a5bd9f6
 		 int argc, char *argv[])
a5bd9f6
 {
a5bd9f6
-  grub_file_t *files = 0;
a5bd9f6
   grub_size_t size = 0;
a5bd9f6
   grub_addr_t addr_min, addr_max;
a5bd9f6
   grub_addr_t addr;
a5bd9f6
   grub_err_t err;
a5bd9f6
-  int i;
a5bd9f6
-  int nfiles = 0;
a5bd9f6
-  grub_uint8_t *ptr;
a5bd9f6
+  struct grub_linux_initrd_context initrd_ctx;
a5bd9f6
 
a5bd9f6
   if (argc == 0)
a5bd9f6
     {
a5bd9f6
@@ -1076,19 +1074,10 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
a5bd9f6
       goto fail;
a5bd9f6
     }
a5bd9f6
 
a5bd9f6
-  files = grub_zalloc (argc * sizeof (files[0]));
a5bd9f6
-  if (!files)
a5bd9f6
+  if (grub_initrd_init (argc, argv, &initrd_ctx))
a5bd9f6
     goto fail;
a5bd9f6
 
a5bd9f6
-  for (i = 0; i < argc; i++)
a5bd9f6
-    {
a5bd9f6
-      grub_file_filter_disable_compression ();
a5bd9f6
-      files[i] = grub_file_open (argv[i]);
a5bd9f6
-      if (! files[i])
a5bd9f6
-	goto fail;
a5bd9f6
-      nfiles++;
a5bd9f6
-      size += ALIGN_UP (grub_file_size (files[i]), 4);
a5bd9f6
-    }
a5bd9f6
+  size = grub_get_initrd_size (&initrd_ctx);
a5bd9f6
 
a5bd9f6
   initrd_pages = (page_align (size) >> 12);
a5bd9f6
 
a5bd9f6
@@ -1138,21 +1127,8 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
a5bd9f6
     initrd_mem_target = get_physical_target_address (ch);
a5bd9f6
   }
a5bd9f6
 
a5bd9f6
-  ptr = initrd_mem;
a5bd9f6
-  for (i = 0; i < nfiles; i++)
a5bd9f6
-    {
a5bd9f6
-      grub_ssize_t cursize = grub_file_size (files[i]);
a5bd9f6
-      if (grub_file_read (files[i], ptr, cursize) != cursize)
a5bd9f6
-	{
a5bd9f6
-	  if (!grub_errno)
a5bd9f6
-	    grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
a5bd9f6
-			argv[i]);
a5bd9f6
-	  goto fail;
a5bd9f6
-	}
a5bd9f6
-      ptr += cursize;
a5bd9f6
-      grub_memset (ptr, 0, ALIGN_UP_OVERHEAD (cursize, 4));
a5bd9f6
-      ptr += ALIGN_UP_OVERHEAD (cursize, 4);
a5bd9f6
-    }
a5bd9f6
+  if (grub_initrd_load (&initrd_ctx, argv, initrd_mem))
a5bd9f6
+    goto fail;
a5bd9f6
 
a5bd9f6
   grub_dprintf ("linux", "Initrd, addr=0x%x, size=0x%x\n",
a5bd9f6
 		(unsigned) addr, (unsigned) size);
a5bd9f6
@@ -1162,9 +1138,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
a5bd9f6
   linux_params.root_dev = 0x0100; /* XXX */
a5bd9f6
 
a5bd9f6
  fail:
a5bd9f6
-  for (i = 0; i < nfiles; i++)
a5bd9f6
-    grub_file_close (files[i]);
a5bd9f6
-  grub_free (files);
a5bd9f6
+  grub_initrd_close (&initrd_ctx);
a5bd9f6
 
a5bd9f6
   return grub_errno;
a5bd9f6
 }
a5bd9f6
diff --git a/grub-core/loader/i386/pc/linux.c b/grub-core/loader/i386/pc/linux.c
a5bd9f6
index 3ce21bc..672c013 100644
a5bd9f6
--- a/grub-core/loader/i386/pc/linux.c
a5bd9f6
+++ b/grub-core/loader/i386/pc/linux.c
a5bd9f6
@@ -34,6 +34,7 @@
a5bd9f6
 #include <grub/video.h>
a5bd9f6
 #include <grub/i386/floppy.h>
a5bd9f6
 #include <grub/lib/cmdline.h>
a5bd9f6
+#include <grub/linux.h>
a5bd9f6
 
a5bd9f6
 GRUB_MOD_LICENSE ("GPLv3+");
a5bd9f6
 
a5bd9f6
@@ -381,15 +382,13 @@ static grub_err_t
a5bd9f6
 grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
a5bd9f6
 		 int argc, char *argv[])
a5bd9f6
 {
a5bd9f6
-  grub_file_t *files = 0;
a5bd9f6
   grub_size_t size = 0;
a5bd9f6
   grub_addr_t addr_max, addr_min;
a5bd9f6
   struct linux_kernel_header *lh;
a5bd9f6
   grub_uint8_t *initrd_chunk;
a5bd9f6
   grub_addr_t initrd_addr;
a5bd9f6
   grub_err_t err;
a5bd9f6
-  int i, nfiles = 0;
a5bd9f6
-  grub_uint8_t *ptr;
a5bd9f6
+  struct grub_linux_initrd_context initrd_ctx;
a5bd9f6
 
a5bd9f6
   if (argc == 0)
a5bd9f6
     {
a5bd9f6
@@ -437,19 +436,10 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
a5bd9f6
 
a5bd9f6
   addr_min = GRUB_LINUX_BZIMAGE_ADDR + grub_linux16_prot_size;
a5bd9f6
 
a5bd9f6
-  files = grub_zalloc (argc * sizeof (files[0]));
a5bd9f6
-  if (!files)
a5bd9f6
+  if (grub_initrd_init (argc, argv, &initrd_ctx))
a5bd9f6
     goto fail;
a5bd9f6
 
a5bd9f6
-  for (i = 0; i < argc; i++)
a5bd9f6
-    {
a5bd9f6
-      grub_file_filter_disable_compression ();
a5bd9f6
-      files[i] = grub_file_open (argv[i]);
a5bd9f6
-      if (! files[i])
a5bd9f6
-	goto fail;
a5bd9f6
-      nfiles++;
a5bd9f6
-      size += ALIGN_UP (grub_file_size (files[i]), 4);
a5bd9f6
-    }
a5bd9f6
+  size = grub_get_initrd_size (&initrd_ctx);
a5bd9f6
 
a5bd9f6
   {
a5bd9f6
     grub_relocator_chunk_t ch;
a5bd9f6
@@ -463,30 +453,14 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
a5bd9f6
     initrd_addr = get_physical_target_address (ch);
a5bd9f6
   }
a5bd9f6
 
a5bd9f6
-  ptr = initrd_chunk;
a5bd9f6
-  
a5bd9f6
-  for (i = 0; i < nfiles; i++)
a5bd9f6
-    {
a5bd9f6
-      grub_ssize_t cursize = grub_file_size (files[i]);
a5bd9f6
-      if (grub_file_read (files[i], ptr, cursize) != cursize)
a5bd9f6
-	{
a5bd9f6
-	  if (!grub_errno)
a5bd9f6
-	    grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
a5bd9f6
-			argv[i]);
a5bd9f6
-	  goto fail;
a5bd9f6
-	}
a5bd9f6
-      ptr += cursize;
a5bd9f6
-      grub_memset (ptr, 0, ALIGN_UP_OVERHEAD (cursize, 4));
a5bd9f6
-      ptr += ALIGN_UP_OVERHEAD (cursize, 4);
a5bd9f6
-    }
a5bd9f6
+  if (grub_initrd_load (&initrd_ctx, argv, initrd_chunk))
a5bd9f6
+    goto fail;
a5bd9f6
 
a5bd9f6
   lh->ramdisk_image = initrd_addr;
a5bd9f6
   lh->ramdisk_size = size;
a5bd9f6
 
a5bd9f6
  fail:
a5bd9f6
-  for (i = 0; i < nfiles; i++)
a5bd9f6
-    grub_file_close (files[i]);
a5bd9f6
-  grub_free (files);
a5bd9f6
+  grub_initrd_close (&initrd_ctx);
a5bd9f6
 
a5bd9f6
   return grub_errno;
a5bd9f6
 }
a5bd9f6
diff --git a/grub-core/loader/ia64/efi/linux.c b/grub-core/loader/ia64/efi/linux.c
a5bd9f6
index 17843fd..87ac49f 100644
a5bd9f6
--- a/grub-core/loader/ia64/efi/linux.c
a5bd9f6
+++ b/grub-core/loader/ia64/efi/linux.c
a5bd9f6
@@ -32,6 +32,7 @@
a5bd9f6
 #include <grub/elf.h>
a5bd9f6
 #include <grub/i18n.h>
a5bd9f6
 #include <grub/env.h>
a5bd9f6
+#include <grub/linux.h>
a5bd9f6
 
a5bd9f6
 GRUB_MOD_LICENSE ("GPLv3+");
a5bd9f6
 
a5bd9f6
@@ -567,10 +568,7 @@ static grub_err_t
a5bd9f6
 grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
a5bd9f6
 		 int argc, char *argv[])
a5bd9f6
 {
a5bd9f6
-  grub_file_t *files = 0;
a5bd9f6
-  int i;
a5bd9f6
-  int nfiles = 0;
a5bd9f6
-  grub_uint8_t *ptr;
a5bd9f6
+  struct grub_linux_initrd_context initrd_ctx;
a5bd9f6
 
a5bd9f6
   if (argc == 0)
a5bd9f6
     {
a5bd9f6
@@ -584,22 +582,11 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
a5bd9f6
       goto fail;
a5bd9f6
     }
a5bd9f6
 
a5bd9f6
-  files = grub_zalloc (argc * sizeof (files[0]));
a5bd9f6
-  if (!files)
a5bd9f6
+  if (grub_initrd_init (argc, argv, &initrd_ctx))
a5bd9f6
     goto fail;
a5bd9f6
 
a5bd9f6
-  initrd_size = 0;
a5bd9f6
+  initrd_size = grub_get_initrd_size (&initrd_ctx);
a5bd9f6
   grub_dprintf ("linux", "Loading initrd\n");
a5bd9f6
-  for (i = 0; i < argc; i++)
a5bd9f6
-    {
a5bd9f6
-      grub_file_filter_disable_compression ();
a5bd9f6
-      files[i] = grub_file_open (argv[i]);
a5bd9f6
-      if (! files[i])
a5bd9f6
-	goto fail;
a5bd9f6
-      nfiles++;
a5bd9f6
-      initrd_size += ALIGN_UP (grub_file_size (files[i]), 4);
a5bd9f6
-      grub_dprintf ("linux", "File %d: %s\n", i, argv[i]);
a5bd9f6
-    }
a5bd9f6
 
a5bd9f6
   initrd_pages = (page_align (initrd_size) >> 12);
a5bd9f6
   initrd_mem = grub_efi_allocate_pages (0, initrd_pages);
a5bd9f6
@@ -612,25 +599,10 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
a5bd9f6
   grub_dprintf ("linux", "[addr=0x%lx, size=0x%lx]\n",
a5bd9f6
 		(grub_uint64_t) initrd_mem, initrd_size);
a5bd9f6
 
a5bd9f6
-  ptr = initrd_mem;
a5bd9f6
-  for (i = 0; i < nfiles; i++)
a5bd9f6
-    {
a5bd9f6
-      grub_ssize_t cursize = grub_file_size (files[i]);
a5bd9f6
-      if (grub_file_read (files[i], ptr, cursize) != cursize)
a5bd9f6
-	{
a5bd9f6
-	  if (!grub_errno)
a5bd9f6
-	    grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
a5bd9f6
-			argv[i]);
a5bd9f6
-	  goto fail;
a5bd9f6
-	}
a5bd9f6
-      ptr += cursize;
a5bd9f6
-      grub_memset (ptr, 0, ALIGN_UP_OVERHEAD (cursize, 4));
a5bd9f6
-      ptr += ALIGN_UP_OVERHEAD (cursize, 4);
a5bd9f6
-    }
a5bd9f6
+  if (grub_initrd_load (&initrd_ctx, argv, initrd_mem))
a5bd9f6
+    goto fail;
a5bd9f6
  fail:
a5bd9f6
-  for (i = 0; i < nfiles; i++)
a5bd9f6
-    grub_file_close (files[i]);
a5bd9f6
-  grub_free (files);
a5bd9f6
+  grub_initrd_close (&initrd_ctx);
a5bd9f6
   return grub_errno;
a5bd9f6
 }
a5bd9f6
 
a5bd9f6
diff --git a/grub-core/loader/linux.c b/grub-core/loader/linux.c
a5bd9f6
new file mode 100644
a5bd9f6
index 0000000..cbe3c53
a5bd9f6
--- /dev/null
a5bd9f6
+++ b/grub-core/loader/linux.c
a5bd9f6
@@ -0,0 +1,294 @@
a5bd9f6
+#include <grub/types.h>
a5bd9f6
+#include <grub/err.h>
a5bd9f6
+#include <grub/linux.h>
a5bd9f6
+#include <grub/misc.h>
a5bd9f6
+#include <grub/file.h>
a5bd9f6
+#include <grub/mm.h>
a5bd9f6
+
a5bd9f6
+struct newc_head
a5bd9f6
+{
a5bd9f6
+  char magic[6];
a5bd9f6
+  char ino[8];
a5bd9f6
+  char mode[8];
a5bd9f6
+  char uid[8];
a5bd9f6
+  char gid[8];
a5bd9f6
+  char nlink[8];
a5bd9f6
+  char mtime[8];
a5bd9f6
+  char filesize[8];
a5bd9f6
+  char devmajor[8];
a5bd9f6
+  char devminor[8];
a5bd9f6
+  char rdevmajor[8];
a5bd9f6
+  char rdevminor[8];
a5bd9f6
+  char namesize[8];
a5bd9f6
+  char check[8];
a5bd9f6
+} __attribute__ ((packed));
a5bd9f6
+
a5bd9f6
+struct grub_linux_initrd_component
a5bd9f6
+{
a5bd9f6
+  grub_file_t file;
a5bd9f6
+  char *newc_name;
a5bd9f6
+  grub_off_t size;
a5bd9f6
+};
a5bd9f6
+
a5bd9f6
+struct dir
a5bd9f6
+{
a5bd9f6
+  char *name;
a5bd9f6
+  struct dir *next;
a5bd9f6
+  struct dir *child;
a5bd9f6
+};
a5bd9f6
+
a5bd9f6
+static char
a5bd9f6
+hex (grub_uint8_t val)
a5bd9f6
+{
a5bd9f6
+  if (val < 10)
a5bd9f6
+    return '0' + val;
a5bd9f6
+  return 'a' + val - 10;
a5bd9f6
+}
a5bd9f6
+
a5bd9f6
+static void
a5bd9f6
+set_field (char *var, grub_uint32_t val)
a5bd9f6
+{
a5bd9f6
+  int i;
a5bd9f6
+  char *ptr = var;
a5bd9f6
+  for (i = 28; i >= 0; i -= 4)
a5bd9f6
+    *ptr++ = hex((val >> i) & 0xf);
a5bd9f6
+}
a5bd9f6
+
a5bd9f6
+static grub_uint8_t *
a5bd9f6
+make_header (grub_uint8_t *ptr,
a5bd9f6
+	     const char *name, grub_size_t len,
a5bd9f6
+	     grub_uint32_t mode,
a5bd9f6
+	     grub_off_t fsize)
a5bd9f6
+{
a5bd9f6
+  struct newc_head *head = (struct newc_head *) ptr;
a5bd9f6
+  grub_uint8_t *optr;
a5bd9f6
+  grub_size_t oh = 0;
a5bd9f6
+  grub_memcpy (head->magic, "070701", 6);
a5bd9f6
+  set_field (head->ino, 0);
a5bd9f6
+  set_field (head->mode, mode);
a5bd9f6
+  set_field (head->uid, 0);
a5bd9f6
+  set_field (head->gid, 0);
a5bd9f6
+  set_field (head->nlink, 1);
a5bd9f6
+  set_field (head->mtime, 0);
a5bd9f6
+  set_field (head->filesize, fsize);
a5bd9f6
+  set_field (head->devmajor, 0);
a5bd9f6
+  set_field (head->devminor, 0);
a5bd9f6
+  set_field (head->rdevmajor, 0);
a5bd9f6
+  set_field (head->rdevminor, 0);
a5bd9f6
+  set_field (head->namesize, len);
a5bd9f6
+  set_field (head->check, 0);
a5bd9f6
+  optr = ptr;
a5bd9f6
+  ptr += sizeof (struct newc_head);
a5bd9f6
+  grub_memcpy (ptr, name, len);
a5bd9f6
+  ptr += len;
a5bd9f6
+  oh = ALIGN_UP_OVERHEAD (ptr - optr, 4);
a5bd9f6
+  grub_memset (ptr, 0, oh);
a5bd9f6
+  ptr += oh;
a5bd9f6
+  return ptr;
a5bd9f6
+}
a5bd9f6
+
a5bd9f6
+static void
a5bd9f6
+free_dir (struct dir *root)
a5bd9f6
+{
a5bd9f6
+  if (!root)
a5bd9f6
+    return;
a5bd9f6
+  free_dir (root->next);
a5bd9f6
+  free_dir (root->child);
a5bd9f6
+  grub_free (root->name);
a5bd9f6
+  grub_free (root);
a5bd9f6
+}
a5bd9f6
+
a5bd9f6
+static grub_size_t
a5bd9f6
+insert_dir (const char *name, struct dir **root,
a5bd9f6
+	    grub_uint8_t *ptr)
a5bd9f6
+{
a5bd9f6
+  struct dir *cur, **head = root;
a5bd9f6
+  const char *cb, *ce = name;
a5bd9f6
+  grub_size_t size = 0;
a5bd9f6
+  while (1)
a5bd9f6
+    {
a5bd9f6
+      for (cb = ce; *cb == '/'; cb++);
a5bd9f6
+      for (ce = cb; *ce && *ce != '/'; ce++);
a5bd9f6
+      if (!*ce)
a5bd9f6
+	break;
a5bd9f6
+
a5bd9f6
+      for (cur = *root; cur; cur = cur->next)
a5bd9f6
+	if (grub_memcmp (cur->name, cb, ce - cb)
a5bd9f6
+	    && cur->name[ce - cb] == 0)
a5bd9f6
+	  break;
a5bd9f6
+      if (!cur)
a5bd9f6
+	{
a5bd9f6
+	  struct dir *n;
a5bd9f6
+	  n = grub_zalloc (sizeof (*n));
a5bd9f6
+	  if (!n)
a5bd9f6
+	    return 0;
a5bd9f6
+	  n->next = *head;
a5bd9f6
+	  n->name = grub_strndup (cb, ce - cb);
a5bd9f6
+	  if (ptr)
a5bd9f6
+	    {
a5bd9f6
+	      grub_printf ("Creating directory %s, %s\n", name, ce);
a5bd9f6
+	      ptr = make_header (ptr, name, ce - name,
a5bd9f6
+				 040777, 0);
a5bd9f6
+	    }
a5bd9f6
+	  size += ALIGN_UP ((ce - (char *) name)
a5bd9f6
+			    + sizeof (struct newc_head), 4);
a5bd9f6
+	  *head = n;
a5bd9f6
+	  cur = n;
a5bd9f6
+	}
a5bd9f6
+      root = &cur->next;
a5bd9f6
+    }
a5bd9f6
+  return size;
a5bd9f6
+}
a5bd9f6
+
a5bd9f6
+grub_err_t
a5bd9f6
+grub_initrd_init (int argc, char *argv[],
a5bd9f6
+		  struct grub_linux_initrd_context *initrd_ctx)
a5bd9f6
+{
a5bd9f6
+  int i;
a5bd9f6
+  int newc = 0;
a5bd9f6
+  struct dir *root = 0;
a5bd9f6
+
a5bd9f6
+  initrd_ctx->nfiles = 0;
a5bd9f6
+  initrd_ctx->components = 0;
a5bd9f6
+
a5bd9f6
+  initrd_ctx->components = grub_zalloc (argc
a5bd9f6
+					* sizeof (initrd_ctx->components[0]));
a5bd9f6
+  if (!initrd_ctx->components)
a5bd9f6
+    return grub_errno;
a5bd9f6
+
a5bd9f6
+  initrd_ctx->size = 0;
a5bd9f6
+
a5bd9f6
+  for (i = 0; i < argc; i++)
a5bd9f6
+    {
a5bd9f6
+      const char *fname = argv[i];
a5bd9f6
+      if (grub_memcmp (argv[i], "newc:", 5) == 0)
a5bd9f6
+	{
a5bd9f6
+	  const char *ptr, *eptr;
a5bd9f6
+	  ptr = argv[i] + 5;
a5bd9f6
+	  while (*ptr == '/')
a5bd9f6
+	    ptr++;
a5bd9f6
+	  eptr = grub_strchr (ptr, ':');
a5bd9f6
+	  if (eptr)
a5bd9f6
+	    {
a5bd9f6
+	      grub_file_filter_disable_compression ();
a5bd9f6
+	      initrd_ctx->components[i].newc_name = grub_strndup (ptr, eptr - ptr);
a5bd9f6
+	      if (!initrd_ctx->components[i].newc_name)
a5bd9f6
+		{
a5bd9f6
+		  grub_initrd_close (initrd_ctx);
a5bd9f6
+		  return grub_errno;
a5bd9f6
+		}
a5bd9f6
+	      initrd_ctx->size
a5bd9f6
+		+= ALIGN_UP (sizeof (struct newc_head)
a5bd9f6
+			    + grub_strlen (initrd_ctx->components[i].newc_name),
a5bd9f6
+			     4);
a5bd9f6
+	      initrd_ctx->size += insert_dir (initrd_ctx->components[i].newc_name,
a5bd9f6
+					      &root, 0);
a5bd9f6
+	      newc = 1;
a5bd9f6
+	      fname = eptr + 1;
a5bd9f6
+	    }
a5bd9f6
+	}
a5bd9f6
+      else if (newc)
a5bd9f6
+	{
a5bd9f6
+	  initrd_ctx->size += ALIGN_UP (sizeof (struct newc_head)
a5bd9f6
+					+ sizeof ("TRAILER!!!") - 1, 4);
a5bd9f6
+	  free_dir (root);
a5bd9f6
+	  root = 0;
a5bd9f6
+	  newc = 0;
a5bd9f6
+	}
a5bd9f6
+      grub_file_filter_disable_compression ();
a5bd9f6
+      initrd_ctx->components[i].file = grub_file_open (fname);
a5bd9f6
+      if (!initrd_ctx->components[i].file)
a5bd9f6
+	{
a5bd9f6
+	  grub_initrd_close (initrd_ctx);
a5bd9f6
+	  return grub_errno;
a5bd9f6
+	}
a5bd9f6
+      initrd_ctx->nfiles++;
a5bd9f6
+      initrd_ctx->components[i].size
a5bd9f6
+	= grub_file_size (initrd_ctx->components[i].file);
a5bd9f6
+      initrd_ctx->size += ALIGN_UP (initrd_ctx->components[i].size, 4);
a5bd9f6
+    }
a5bd9f6
+
a5bd9f6
+  if (newc)
a5bd9f6
+    {
a5bd9f6
+      initrd_ctx->size += ALIGN_UP (sizeof (struct newc_head)
a5bd9f6
+				    + sizeof ("TRAILER!!!") - 1, 4);
a5bd9f6
+      free_dir (root);
a5bd9f6
+      root = 0;
a5bd9f6
+    }
a5bd9f6
+  
a5bd9f6
+  return GRUB_ERR_NONE;
a5bd9f6
+}
a5bd9f6
+
a5bd9f6
+grub_size_t
a5bd9f6
+grub_get_initrd_size (struct grub_linux_initrd_context *initrd_ctx)
a5bd9f6
+{
a5bd9f6
+  return initrd_ctx->size;
a5bd9f6
+}
a5bd9f6
+
a5bd9f6
+void
a5bd9f6
+grub_initrd_close (struct grub_linux_initrd_context *initrd_ctx)
a5bd9f6
+{
a5bd9f6
+  int i;
a5bd9f6
+  if (!initrd_ctx->components)
a5bd9f6
+    return;
a5bd9f6
+  for (i = 0; i < initrd_ctx->nfiles; i++)
a5bd9f6
+    {
a5bd9f6
+      grub_free (initrd_ctx->components[i].newc_name);
a5bd9f6
+      grub_file_close (initrd_ctx->components[i].file);
a5bd9f6
+    }
a5bd9f6
+  grub_free (initrd_ctx->components);
a5bd9f6
+  initrd_ctx->components = 0;
a5bd9f6
+}
a5bd9f6
+
a5bd9f6
+grub_err_t
a5bd9f6
+grub_initrd_load (struct grub_linux_initrd_context *initrd_ctx,
a5bd9f6
+		  char *argv[], void *target)
a5bd9f6
+{
a5bd9f6
+  grub_uint8_t *ptr = target;
a5bd9f6
+  int i;
a5bd9f6
+  int newc = 0;
a5bd9f6
+  struct dir *root = 0;
a5bd9f6
+
a5bd9f6
+  for (i = 0; i < initrd_ctx->nfiles; i++)
a5bd9f6
+    {
a5bd9f6
+      grub_ssize_t cursize;
a5bd9f6
+
a5bd9f6
+      if (initrd_ctx->components[i].newc_name)
a5bd9f6
+	{
a5bd9f6
+	  ptr += insert_dir (initrd_ctx->components[i].newc_name,
a5bd9f6
+			     &root, ptr);
a5bd9f6
+	  ptr = make_header (ptr, initrd_ctx->components[i].newc_name,
a5bd9f6
+			     grub_strlen (initrd_ctx->components[i].newc_name),
a5bd9f6
+			     0100777,
a5bd9f6
+			     initrd_ctx->components[i].size);
a5bd9f6
+	  newc = 1;
a5bd9f6
+	}
a5bd9f6
+      else if (newc)
a5bd9f6
+	{
a5bd9f6
+	  ptr = make_header (ptr, "TRAILER!!!", sizeof ("TRAILER!!!") - 1,
a5bd9f6
+			     0, 0);
a5bd9f6
+	  free_dir (root);
a5bd9f6
+	  root = 0;
a5bd9f6
+	  newc = 0;
a5bd9f6
+	}
a5bd9f6
+
a5bd9f6
+      cursize = initrd_ctx->components[i].size;
a5bd9f6
+      if (grub_file_read (initrd_ctx->components[i].file, ptr, cursize)
a5bd9f6
+	  != cursize)
a5bd9f6
+	{
a5bd9f6
+	  if (!grub_errno)
a5bd9f6
+	    grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
a5bd9f6
+			argv[i]);
a5bd9f6
+	  grub_initrd_close (initrd_ctx);
a5bd9f6
+	  return grub_errno;
a5bd9f6
+	}
a5bd9f6
+      ptr += cursize;
a5bd9f6
+      grub_memset (ptr, 0, ALIGN_UP_OVERHEAD (cursize, 4));
a5bd9f6
+      ptr += ALIGN_UP_OVERHEAD (cursize, 4);
a5bd9f6
+    }
a5bd9f6
+  if (newc)
a5bd9f6
+    ptr = make_header (ptr, "TRAILER!!!", sizeof ("TRAILER!!!") - 1, 0, 0);
a5bd9f6
+  free_dir (root);
a5bd9f6
+  root = 0;
a5bd9f6
+  return GRUB_ERR_NONE;
a5bd9f6
+}
a5bd9f6
diff --git a/grub-core/loader/mips/linux.c b/grub-core/loader/mips/linux.c
a5bd9f6
index 653f8a2..ef64a5b 100644
a5bd9f6
--- a/grub-core/loader/mips/linux.c
a5bd9f6
+++ b/grub-core/loader/mips/linux.c
a5bd9f6
@@ -28,6 +28,7 @@
a5bd9f6
 #include <grub/memory.h>
a5bd9f6
 #include <grub/i18n.h>
a5bd9f6
 #include <grub/lib/cmdline.h>
a5bd9f6
+#include <grub/linux.h>
a5bd9f6
 
a5bd9f6
 GRUB_MOD_LICENSE ("GPLv3+");
a5bd9f6
 
a5bd9f6
@@ -410,14 +411,11 @@ static grub_err_t
a5bd9f6
 grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
a5bd9f6
 		 int argc, char *argv[])
a5bd9f6
 {
a5bd9f6
-  grub_file_t *files = 0;
a5bd9f6
   grub_size_t size = 0;
a5bd9f6
   void *initrd_src;
a5bd9f6
   grub_addr_t initrd_dest;
a5bd9f6
   grub_err_t err;
a5bd9f6
-  int i;
a5bd9f6
-  int nfiles = 0;
a5bd9f6
-  grub_uint8_t *ptr;
a5bd9f6
+  struct grub_linux_initrd_context initrd_ctx;
a5bd9f6
 
a5bd9f6
   if (argc == 0)
a5bd9f6
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
a5bd9f6
@@ -428,19 +426,10 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
a5bd9f6
   if (initrd_loaded)
a5bd9f6
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "only one initrd command can be issued.");
a5bd9f6
 
a5bd9f6
-  files = grub_zalloc (argc * sizeof (files[0]));
a5bd9f6
-  if (!files)
a5bd9f6
+  if (grub_initrd_init (argc, argv, &initrd_ctx))
a5bd9f6
     goto fail;
a5bd9f6
 
a5bd9f6
-  for (i = 0; i < argc; i++)
a5bd9f6
-    {
a5bd9f6
-      grub_file_filter_disable_compression ();
a5bd9f6
-      files[i] = grub_file_open (argv[i]);
a5bd9f6
-      if (! files[i])
a5bd9f6
-	goto fail;
a5bd9f6
-      nfiles++;
a5bd9f6
-      size += ALIGN_UP (grub_file_size (files[i]), 4);
a5bd9f6
-    }
a5bd9f6
+  size = grub_get_initrd_size (&initrd_ctx);
a5bd9f6
 
a5bd9f6
   {
a5bd9f6
     grub_relocator_chunk_t ch;
a5bd9f6
@@ -458,21 +447,8 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
a5bd9f6
     initrd_dest = get_physical_target_address (ch) | 0x80000000;
a5bd9f6
   }
a5bd9f6
 
a5bd9f6
-  ptr = initrd_src;
a5bd9f6
-  for (i = 0; i < nfiles; i++)
a5bd9f6
-    {
a5bd9f6
-      grub_ssize_t cursize = grub_file_size (files[i]);
a5bd9f6
-      if (grub_file_read (files[i], ptr, cursize) != cursize)
a5bd9f6
-	{
a5bd9f6
-	  if (!grub_errno)
a5bd9f6
-	    grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
a5bd9f6
-			argv[i]);
a5bd9f6
-	  goto fail;
a5bd9f6
-	}
a5bd9f6
-      ptr += cursize;
a5bd9f6
-      grub_memset (ptr, 0, ALIGN_UP_OVERHEAD (cursize, 4));
a5bd9f6
-      ptr += ALIGN_UP_OVERHEAD (cursize, 4);
a5bd9f6
-    }
a5bd9f6
+  if (grub_initrd_load (&initrd_ctx, argv, initrd_src))
a5bd9f6
+    goto fail;
a5bd9f6
 
a5bd9f6
 #ifdef GRUB_MACHINE_MIPS_QEMU_MIPS
a5bd9f6
   {
a5bd9f6
@@ -504,9 +480,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
a5bd9f6
   initrd_loaded = 1;
a5bd9f6
 
a5bd9f6
  fail:
a5bd9f6
-  for (i = 0; i < nfiles; i++)
a5bd9f6
-    grub_file_close (files[i]);
a5bd9f6
-  grub_free (files);
a5bd9f6
+  grub_initrd_close (&initrd_ctx);
a5bd9f6
 
a5bd9f6
   return grub_errno;
a5bd9f6
 }
a5bd9f6
diff --git a/grub-core/loader/powerpc/ieee1275/linux.c b/grub-core/loader/powerpc/ieee1275/linux.c
a5bd9f6
index 9055399..cff4fd1 100644
a5bd9f6
--- a/grub-core/loader/powerpc/ieee1275/linux.c
a5bd9f6
+++ b/grub-core/loader/powerpc/ieee1275/linux.c
a5bd9f6
@@ -29,6 +29,7 @@
a5bd9f6
 #include <grub/memory.h>
a5bd9f6
 #include <grub/lib/cmdline.h>
a5bd9f6
 #include <grub/cache.h>
a5bd9f6
+#include <grub/linux.h>
a5bd9f6
 
a5bd9f6
 GRUB_MOD_LICENSE ("GPLv3+");
a5bd9f6
 
a5bd9f6
@@ -315,13 +316,10 @@ static grub_err_t
a5bd9f6
 grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
a5bd9f6
 		 int argc, char *argv[])
a5bd9f6
 {
a5bd9f6
-  grub_file_t *files = 0;
a5bd9f6
   grub_size_t size = 0;
a5bd9f6
   grub_addr_t first_addr;
a5bd9f6
   grub_addr_t addr;
a5bd9f6
-  int i;
a5bd9f6
-  int nfiles = 0;
a5bd9f6
-  grub_uint8_t *ptr;
a5bd9f6
+  struct grub_linux_initrd_context initrd_ctx;
a5bd9f6
 
a5bd9f6
   if (argc == 0)
a5bd9f6
     {
a5bd9f6
@@ -335,19 +333,10 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
a5bd9f6
       goto fail;
a5bd9f6
     }
a5bd9f6
 
a5bd9f6
-  files = grub_zalloc (argc * sizeof (files[0]));
a5bd9f6
-  if (!files)
a5bd9f6
+  if (grub_initrd_init (argc, argv, &initrd_ctx))
a5bd9f6
     goto fail;
a5bd9f6
 
a5bd9f6
-  for (i = 0; i < argc; i++)
a5bd9f6
-    {
a5bd9f6
-      grub_file_filter_disable_compression ();
a5bd9f6
-      files[i] = grub_file_open (argv[i]);
a5bd9f6
-      if (! files[i])
a5bd9f6
-	goto fail;
a5bd9f6
-      nfiles++;
a5bd9f6
-      size += ALIGN_UP (grub_file_size (files[i]), 4);
a5bd9f6
-    }
a5bd9f6
+  size = grub_get_initrd_size (&initrd_ctx);
a5bd9f6
 
a5bd9f6
   first_addr = linux_addr + linux_size;
a5bd9f6
 
a5bd9f6
@@ -359,30 +348,14 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
a5bd9f6
 
a5bd9f6
   grub_dprintf ("loader", "Loading initrd at 0x%x, size 0x%x\n", addr, size);
a5bd9f6
 
a5bd9f6
-  ptr = (void *) addr;
a5bd9f6
-  for (i = 0; i < nfiles; i++)
a5bd9f6
-    {
a5bd9f6
-      grub_ssize_t cursize = grub_file_size (files[i]);
a5bd9f6
-      if (grub_file_read (files[i], ptr, cursize) != cursize)
a5bd9f6
-	{
a5bd9f6
-	  grub_ieee1275_release (addr, size);
a5bd9f6
-	  if (!grub_errno)
a5bd9f6
-	    grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
a5bd9f6
-			argv[i]);
a5bd9f6
-	  goto fail;
a5bd9f6
-	}
a5bd9f6
-      ptr += cursize;
a5bd9f6
-      grub_memset (ptr, 0, ALIGN_UP_OVERHEAD (cursize, 4));
a5bd9f6
-      ptr += ALIGN_UP_OVERHEAD (cursize, 4);
a5bd9f6
-    }
a5bd9f6
+  if (grub_initrd_load (&initrd_ctx, argv, (void *) addr))
a5bd9f6
+    goto fail;
a5bd9f6
 
a5bd9f6
   initrd_addr = addr;
a5bd9f6
   initrd_size = size;
a5bd9f6
 
a5bd9f6
  fail:
a5bd9f6
-  for (i = 0; i < nfiles; i++)
a5bd9f6
-    grub_file_close (files[i]);
a5bd9f6
-  grub_free (files);
a5bd9f6
+  grub_initrd_close (&initrd_ctx);
a5bd9f6
 
a5bd9f6
   return grub_errno;
a5bd9f6
 }
a5bd9f6
diff --git a/grub-core/loader/sparc64/ieee1275/linux.c b/grub-core/loader/sparc64/ieee1275/linux.c
a5bd9f6
index d203377..577ecff 100644
a5bd9f6
--- a/grub-core/loader/sparc64/ieee1275/linux.c
a5bd9f6
+++ b/grub-core/loader/sparc64/ieee1275/linux.c
a5bd9f6
@@ -28,6 +28,7 @@
a5bd9f6
 #include <grub/i18n.h>
a5bd9f6
 #include <grub/memory.h>
a5bd9f6
 #include <grub/lib/cmdline.h>
a5bd9f6
+#include <grub/linux.h>
a5bd9f6
 
a5bd9f6
 GRUB_MOD_LICENSE ("GPLv3+");
a5bd9f6
 
a5bd9f6
@@ -368,14 +369,11 @@ static grub_err_t
a5bd9f6
 grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
a5bd9f6
 		 int argc, char *argv[])
a5bd9f6
 {
a5bd9f6
-  grub_file_t *files = 0;
a5bd9f6
   grub_size_t size = 0;
a5bd9f6
   grub_addr_t paddr;
a5bd9f6
   grub_addr_t addr;
a5bd9f6
   int ret;
a5bd9f6
-  int i;
a5bd9f6
-  int nfiles = 0;
a5bd9f6
-  grub_uint8_t *ptr;
a5bd9f6
+  struct grub_linux_initrd_context initrd_ctx;
a5bd9f6
 
a5bd9f6
   if (argc == 0)
a5bd9f6
     {
a5bd9f6
@@ -389,19 +387,10 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
a5bd9f6
       goto fail;
a5bd9f6
     }
a5bd9f6
 
a5bd9f6
-  files = grub_zalloc (argc * sizeof (files[0]));
a5bd9f6
-  if (!files)
a5bd9f6
+  if (grub_initrd_init (argc, argv, &initrd_ctx))
a5bd9f6
     goto fail;
a5bd9f6
 
a5bd9f6
-  for (i = 0; i < argc; i++)
a5bd9f6
-    {
a5bd9f6
-      grub_file_filter_disable_compression ();
a5bd9f6
-      files[i] = grub_file_open (argv[i]);
a5bd9f6
-      if (! files[i])
a5bd9f6
-	goto fail;
a5bd9f6
-      nfiles++;
a5bd9f6
-      size += ALIGN_UP(grub_file_size (files[i]), 4);
a5bd9f6
-    }
a5bd9f6
+  size = grub_get_initrd_size (&initrd_ctx);
a5bd9f6
 
a5bd9f6
   addr = 0x60000000;
a5bd9f6
 
a5bd9f6
@@ -423,30 +412,15 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
a5bd9f6
   grub_dprintf ("loader", "Loading initrd at vaddr 0x%lx, paddr 0x%lx, size 0x%lx\n",
a5bd9f6
 		addr, paddr, size);
a5bd9f6
 
a5bd9f6
-  ptr = (void *) addr;
a5bd9f6
-  for (i = 0; i < nfiles; i++)
a5bd9f6
-    {
a5bd9f6
-      grub_ssize_t cursize = grub_file_size (files[i]);
a5bd9f6
-      if (grub_file_read (files[i], ptr, cursize) != cursize)
a5bd9f6
-	{
a5bd9f6
-	  if (!grub_errno)
a5bd9f6
-	    grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
a5bd9f6
-			argv[i]);
a5bd9f6
-	  goto fail;
a5bd9f6
-	}
a5bd9f6
-      ptr += cursize;
a5bd9f6
-      grub_memset (ptr, 0, ALIGN_UP_OVERHEAD (cursize, 4));
a5bd9f6
-      ptr += ALIGN_UP_OVERHEAD (cursize, 4);
a5bd9f6
-    }
a5bd9f6
+  if (grub_initrd_load (&initrd_ctx, argv, (void *) addr))
a5bd9f6
+    goto fail;
a5bd9f6
 
a5bd9f6
   initrd_addr = addr;
a5bd9f6
   initrd_paddr = paddr;
a5bd9f6
   initrd_size = size;
a5bd9f6
 
a5bd9f6
  fail:
a5bd9f6
-  for (i = 0; i < nfiles; i++)
a5bd9f6
-    grub_file_close (files[i]);
a5bd9f6
-  grub_free (files);
a5bd9f6
+  grub_initrd_close (&initrd_ctx);
a5bd9f6
 
a5bd9f6
   return grub_errno;
a5bd9f6
 }
a5bd9f6
diff --git a/include/grub/linux.h b/include/grub/linux.h
a5bd9f6
new file mode 100644
a5bd9f6
index 0000000..594a3f3
a5bd9f6
--- /dev/null
a5bd9f6
+++ b/include/grub/linux.h
a5bd9f6
@@ -0,0 +1,24 @@
a5bd9f6
+#include <grub/file.h>
a5bd9f6
+
a5bd9f6
+struct grub_linux_initrd_component;
a5bd9f6
+
a5bd9f6
+struct grub_linux_initrd_context
a5bd9f6
+{
a5bd9f6
+  int nfiles;
a5bd9f6
+  struct grub_linux_initrd_component *components;
a5bd9f6
+  grub_size_t size;
a5bd9f6
+};
a5bd9f6
+
a5bd9f6
+grub_err_t
a5bd9f6
+grub_initrd_init (int argc, char *argv[],
a5bd9f6
+		  struct grub_linux_initrd_context *ctx);
a5bd9f6
+
a5bd9f6
+grub_size_t
a5bd9f6
+grub_get_initrd_size (struct grub_linux_initrd_context *ctx);
a5bd9f6
+
a5bd9f6
+void
a5bd9f6
+grub_initrd_close (struct grub_linux_initrd_context *initrd_ctx);
a5bd9f6
+
a5bd9f6
+grub_err_t
a5bd9f6
+grub_initrd_load (struct grub_linux_initrd_context *initrd_ctx,
a5bd9f6
+		  char *argv[], void *target);
a5bd9f6
-- 
31004e6
1.8.2.1
a5bd9f6