Skip to contentSkip to conversation

XZ compressed memory disk

PATCH New Szymon Janc 1 participant 1 message Sep 16, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 XZ compressed memory disk

Hello,

Attached patches add support for XZ compressed memory disk. 01_xzmisc.diff moves some code from xzio to xzembed to reuse it in xzmemdisk 02_xzmemdisk.diff adds new module xzmemdisk.mod and new option for grub-mkimage

xzmemdisk.mod depends on gcry_crc.mod and crypto.mod (~22KiB)

To compress memdisk please use following command:
xz --lzma2=dict=2MiB --check=crc32 memdisk.tar
You can adjust dictionary size if needed (dictionary larger than memdisk.tar does not improve compression ratio).

I've noticed that on i386 memdisk.tar gets better compresion ratio without BCJ filter.

Example (all GRUB modules in tar) size profit: 1296 KiB vs 392 KiB.

Comments and testing(!) are welcome :)

Signature · 2 lines
Szymon K. Janc
[email protected] // GG: 1383435

=== modified file 'ChangeLog'

ChangeLog +13 -0
@@ -1,3 +1,16 @@
+2010-09-16  Szymon Janc <[email protected]>
+
+	Move some xz generic code from grub-core/io/xzio.c to
+	grub-core/lib/xzembed/xz_misc.{h|c}. It will be used in xzmemdisk.
+
+	* grub-core/lib/xzembed/xz_dec_stream.h: New defines 
+	XZ_STREAM_FOOTER_SIZE, XZ_CRC_SIZE, VLI_MAX_DIGITS.
+	* grub-core/lib/xzembed/xz_misc.h: New file.
+	* grub-core/lib/xzembed/xz_misc.c: New file.
+	* grub-core/io/xzio.c (test_footer): Rewritten to use helper functions
+	from grub-core/lib/xzembed/xz_misc.h.
+	* grub-core/Makefile.core.def (xzio): Include lib/xzembed/xz_misc.c.
+
 2010-09-15  Szymon Janc <[email protected]>
 
 	* grub-core/lib/xzembed/xz_dec_stream.c (dec_main): Fix index and block

=== modified file 'grub-core/Makefile.core.def'

grub-core/Makefile.core.def +1 -0
@@ -1424,6 +1424,7 @@
   common = lib/xzembed/xz_dec_bcj.c;
   common = lib/xzembed/xz_dec_lzma2.c;
   common = lib/xzembed/xz_dec_stream.c;
+  common = lib/xzembed/xz_misc.c;
   cppflags = '-I$(srcdir)/lib/posix_wrap -I$(srcdir)/lib/xzembed';
 };
 

=== modified file 'grub-core/io/xzio.c'

grub-core/io/xzio.c +26 -79

137 lines not on this page · raw message

=== added file 'grub-core/lib/xzembed/xz_misc.c'

grub-core/lib/xzembed/xz_misc.c +88 -0

88 lines not on this page · raw message

=== added file 'grub-core/lib/xzembed/xz_misc.h'

grub-core/lib/xzembed/xz_misc.h +42 -0

42 lines not on this page · raw message

=== modified file 'grub-core/lib/xzembed/xz_stream.h'

grub-core/lib/xzembed/xz_stream.h +6 -0
@@ -24,6 +24,8 @@
 #ifndef XZ_STREAM_H
 #define XZ_STREAM_H
 
+#include <stdint.h>
+
 /*
  * See the .xz file format specification at
  * http://tukaani.org/xz/xz-file-format.txt
@@ -38,6 +40,9 @@
 #define FOOTER_MAGIC "YZ"
 #define FOOTER_MAGIC_SIZE 2
 
+#define XZ_STREAM_FOOTER_SIZE 12
+#define XZ_CRC_SIZE 4
+
 /*
  * Variable-length integer can hold a 63-bit unsigned integer, or a special
  * value to indicate that the value is unknown.
@@ -49,5 +54,6 @@
 
 /* Maximum encoded size of a VLI */
 #define VLI_BYTES_MAX (sizeof(vli_type) * 8 / 7)
+#define VLI_MAX_DIGITS 9
 
 #endif

=== modified file 'ChangeLog'

ChangeLog +15 -0
@@ -1,5 +1,20 @@
 2010-09-16  Szymon Janc <[email protected]>
 
+	New module xzmemdisk.mod.
+	Support for XZ compressed memory disk in grub-mkimage.
+
+	* grub-core/disk/xzmemdisk.c: New file.
+	* grub-core/Makefile.core.def (xzmemdisk): New module.
+	* include/grub/disk.h (grub_disk_dev_id): Add
+	GRUB_DISK_DEVICE_XZMEMDISK_ID.
+	* include/grub/kernel.h (OBJ_TYPE_XZMEMDISK): New enum value.
+	* util/grub-mkimage.c: (generate_image): New parameter (int xz) to mark
+	memory disk as XZ compressed. Callers updated.
+	* util/grub-mkimage.c: New options (-M, --xzmemdisk) to include XZ
+	compressed memdisk into image.
+
+2010-09-16  Szymon Janc <[email protected]>
+
 	Move some xz generic code from grub-core/io/xzio.c to
 	grub-core/lib/xzembed/xz_misc.{h|c}. It will be used in xzmemdisk.
 

=== modified file 'grub-core/Makefile.core.def'

grub-core/Makefile.core.def +10 -0
@@ -759,6 +759,16 @@
 };
 
 module = {
+  name = xzmemdisk;
+  common = disk/xzmemdisk.c;
+  common = lib/xzembed/xz_dec_bcj.c;
+  common = lib/xzembed/xz_dec_lzma2.c;
+  common = lib/xzembed/xz_dec_stream.c;
+  common = lib/xzembed/xz_misc.c;
+  cppflags = '-I$(srcdir)/lib/posix_wrap -I$(srcdir)/lib/xzembed';
+};
+
+module = {
   name = ata;
   common = disk/ata.c;
   enable = pci;

=== added file 'grub-core/disk/xzmemdisk.c'

grub-core/disk/xzmemdisk.c +187 -0

187 lines not on this page · raw message

=== modified file 'include/grub/disk.h'

include/grub/disk.h +1 -0
@@ -37,6 +37,7 @@
     GRUB_DISK_DEVICE_HOST_ID,
     GRUB_DISK_DEVICE_ATA_ID,
     GRUB_DISK_DEVICE_MEMDISK_ID,
+    GRUB_DISK_DEVICE_XZMEMDISK_ID,
     GRUB_DISK_DEVICE_NAND_ID,
     GRUB_DISK_DEVICE_UUID_ID,
     GRUB_DISK_DEVICE_PXE_ID,

=== modified file 'include/grub/kernel.h'

include/grub/kernel.h +1 -0
@@ -26,6 +26,7 @@
 {
   OBJ_TYPE_ELF,
   OBJ_TYPE_MEMDISK,
+  OBJ_TYPE_XZMEMDISK,
   OBJ_TYPE_CONFIG
 };
 

=== modified file 'util/grub-mkimage.c'

util/grub-mkimage.c +10 -5
@@ -527,7 +527,7 @@
 static void
 generate_image (const char *dir, char *prefix, FILE *out, char *mods[],
 		char *memdisk_path, char *config_path,
-		struct image_target_desc *image_target, int note)
+		struct image_target_desc *image_target, int note, int xz)
 {
   char *kernel_img, *core_img;
   size_t kernel_size, total_module_size, core_size, exec_size;
@@ -630,7 +630,7 @@
 
       header = (struct grub_module_header *) (kernel_img + offset);
       memset (header, 0, sizeof (struct grub_module_header));
-      header->type = grub_host_to_target32 (OBJ_TYPE_MEMDISK);
+      header->type = grub_host_to_target32 (xz ? OBJ_TYPE_XZMEMDISK : OBJ_TYPE_MEMDISK);
       header->size = grub_host_to_target32 (memdisk_size + sizeof (*header));
       offset += sizeof (*header);
 
@@ -1213,6 +1213,7 @@
     {"directory", required_argument, 0, 'd'},
     {"prefix", required_argument, 0, 'p'},
     {"memdisk", required_argument, 0, 'm'},
+    {"xzmemdisk", required_argument, 0, 'M'},
     {"font", required_argument, 0, 'f'},
     {"config", required_argument, 0, 'c'},
     {"output", required_argument, 0, 'o'},
@@ -1255,6 +1256,7 @@
   -d, --directory=DIR     use images and modules under DIR [default=%s/@platform@]\n\
   -p, --prefix=DIR        set grub_prefix directory [default=%s]\n\
   -m, --memdisk=FILE      embed FILE as a memdisk image\n\
+  -M, --xzmemdisk=FILE    embed FILE as a xz compressed memdisk image\n\
   -c, --config=FILE       embed FILE as boot config\n\
   -n, --note              add NOTE segment for CHRP Open Firmware\n\
   -o, --output=FILE       output a generated image to FILE [default=stdout]\n\
@@ -1281,6 +1283,7 @@
   char *dir = NULL;
   char *prefix = NULL;
   char *memdisk = NULL;
+  int xz = 0;
   char *font = NULL;
   char *config = NULL;
   FILE *fp = stdout;
@@ -1293,7 +1296,7 @@
 
   while (1)
     {
-      int c = getopt_long (argc, argv, "d:p:m:c:o:O:f:hVvn", options, 0);
+      int c = getopt_long (argc, argv, "d:p:m:M:c:o:O:f:hVvn", options, 0);
 
       if (c == -1)
 	break;
@@ -1331,6 +1334,8 @@
 	    note = 1;
 	    break;
 
+	  case 'M':
+	    xz = 1;
 	  case 'm':
 	    if (memdisk)
 	      free (memdisk);
@@ -1340,7 +1345,7 @@
 	    if (prefix)
 	      free (prefix);
 
-	    prefix = xstrdup ("(memdisk)/boot/grub");
+	    prefix = xstrdup (xz ? "(xzmemdisk)/boot/grub" : "(memdisk)/boot/grub");
 	    break;
 
 	  case 'c':
@@ -1408,7 +1413,7 @@
 
   generate_image (dir, prefix ? : DEFAULT_DIRECTORY, fp,
 		  argv + optind, memdisk, config,
-		  image_target, note);
+		  image_target, note, xz);
 
   fclose (fp);