~darkmuggle-deactivatedaccount/ubuntu/quantal/grub2/fix-872244

« back to all changes in this revision

Viewing changes to util/grub-mkimage.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson, Colin Watson, Evan Broder, Mario Limonciello
  • Date: 2010-11-24 13:59:55 UTC
  • mfrom: (1.17.6 upstream) (17.6.15 experimental)
  • Revision ID: james.westby@ubuntu.com-20101124135955-r6ii5sepayr7jt53
Tags: 1.99~20101124-1ubuntu1
[ Colin Watson ]
* Resynchronise with Debian experimental.  Remaining changes:
  - Adjust for default Ubuntu boot options ("quiet splash").
  - Default to hiding the menu; holding down Shift at boot will show it.
  - Set a monochromatic theme for Ubuntu.
  - Apply Ubuntu GRUB Legacy changes to legacy update-grub script: title,
    recovery mode, quiet option, tweak how memtest86+ is displayed, and
    use UUIDs where appropriate.
  - Fix backslash-escaping in merge_debconf_into_conf.
  - Remove "GNU/Linux" from default distributor string.
  - Add crashkernel= options if kdump and makedumpfile are available.
  - If other operating systems are installed, then automatically unhide
    the menu.  Otherwise, if GRUB_HIDDEN_TIMEOUT is 0, then use keystatus
    if available to check whether Shift is pressed.  If it is, show the
    menu, otherwise boot immediately.  If keystatus is not available, then
    fall back to a short delay interruptible with Escape.
  - Allow Shift to interrupt 'sleep --interruptible'.
  - Don't display introductory message about line editing unless we're
    actually offering a shell prompt.  Don't clear the screen just before
    booting if we never drew the menu in the first place.
  - Remove some verbose messages printed before reading the configuration
    file.
  - Suppress progress messages as the kernel and initrd load for
    non-recovery kernel menu entries.
  - Change prepare_grub_to_access_device to handle filesystems
    loop-mounted on file images.
  - Ignore devices loop-mounted from files in 10_linux.
  - Show the boot menu if the previous boot failed, that is if it failed
    to get to the end of one of the normal runlevels.
  - Don't generate /boot/grub/device.map during grub-install or
    grub-mkconfig by default.
  - Adjust upgrade version checks for Ubuntu.
  - Don't display "GRUB loading" unless Shift is held down.
  - Adjust versions of grub-doc and grub-legacy-doc conflicts to tolerate
    our backport of the grub-doc split.
  - Fix LVM/RAID probing in the absence of /boot/grub/device.map.
  - Look for .mo files in /usr/share/locale-langpack as well, in
    preference.
  - Make sure GRUB_TIMEOUT isn't quoted unnecessarily.
  - Probe all devices in 'grub-probe --target=drive' if
    /boot/grub/device.map is missing.
  - Build-depend on qemu-kvm rather than qemu-system for grub-pc tests.
  - Use qemu rather than qemu-system-i386.
  - Program vesafb on BIOS systems rather than efifb.
  - Add a grub-rescue-efi-amd64 package containing a rescue CD-ROM image
    for EFI-AMD64.
  - On Wubi, don't ask for an install device, but just update wubildr
    using the diverted grub-install.
  - When embedding the core image in a post-MBR gap, check for and avoid
    sectors matching any of a list of known signatures.
  - Disable video_bochs and video_cirrus on PC BIOS systems, as probing
    PCI space seems to break on some systems.
* Downgrade "ACPI shutdown failed" error to a debug message, since it can
  cause spurious test failures.

[ Evan Broder ]
* Enable lua from grub-extras.
* Incorporate the bitop library into lua.
* Add enum_pci function to grub module in lua.
* Switch back to gfxpayload=keep by default, unless the video hardware
  is known to not support it.

[ Mario Limonciello ]
* Built part_msdos and vfat into bootx64.efi (LP: #677758)

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#include <grub/i18n.h>
25
25
#include <grub/kernel.h>
26
26
#include <grub/disk.h>
 
27
#include <grub/emu/misc.h>
27
28
#include <grub/util/misc.h>
28
29
#include <grub/util/resolve.h>
29
30
#include <grub/misc.h>
44
45
 
45
46
#define ALIGN_ADDR(x) (ALIGN_UP((x), image_target->voidp_sizeof))
46
47
 
 
48
#ifdef HAVE_LIBLZMA
 
49
#include <lzma.h>
 
50
#endif
 
51
 
47
52
#define TARGET_NO_FIELD 0xffffffff
 
53
 
 
54
typedef enum {
 
55
  COMPRESSION_AUTO, COMPRESSION_NONE, COMPRESSION_XZ
 
56
} grub_compression_t;
 
57
 
48
58
struct image_target_desc
49
59
{
50
60
  const char *name;
53
63
  enum {
54
64
    IMAGE_I386_PC, IMAGE_EFI, IMAGE_COREBOOT,
55
65
    IMAGE_SPARC64_AOUT, IMAGE_SPARC64_RAW, IMAGE_I386_IEEE1275,
56
 
    IMAGE_YEELOONG_ELF, IMAGE_QEMU, IMAGE_PPC, IMAGE_YEELOONG_FLASH
 
66
    IMAGE_YEELOONG_ELF, IMAGE_QEMU, IMAGE_PPC, IMAGE_YEELOONG_FLASH,
 
67
    IMAGE_I386_PC_PXE
57
68
  } id;
58
69
  enum
59
70
    {
60
71
      PLATFORM_FLAGS_NONE = 0,
61
 
      PLATFORM_FLAGS_LZMA = 1
 
72
      PLATFORM_FLAGS_LZMA = 1,
 
73
      PLATFORM_FLAGS_DECOMPRESSORS = 2
62
74
    } flags;
63
75
  unsigned prefix;
64
 
  unsigned data_end;
 
76
  unsigned prefix_end;
65
77
  unsigned raw_size;
66
78
  unsigned total_module_size;
67
79
  unsigned kernel_image_size;
73
85
  unsigned install_dos_part, install_bsd_part;
74
86
  grub_uint64_t link_addr;
75
87
  unsigned mod_gap, mod_align;
 
88
  grub_compression_t default_compression;
76
89
};
77
90
 
78
91
struct image_target_desc image_targets[] =
84
97
      .id = IMAGE_COREBOOT,
85
98
      .flags = PLATFORM_FLAGS_NONE,
86
99
      .prefix = GRUB_KERNEL_I386_COREBOOT_PREFIX,
87
 
      .data_end = GRUB_KERNEL_I386_COREBOOT_DATA_END,
 
100
      .prefix_end = GRUB_KERNEL_I386_COREBOOT_PREFIX_END,
88
101
      .raw_size = 0,
89
102
      .total_module_size = TARGET_NO_FIELD,
90
103
      .kernel_image_size = TARGET_NO_FIELD,
105
118
      .bigendian = 0,
106
119
      .id = IMAGE_COREBOOT,
107
120
      .flags = PLATFORM_FLAGS_NONE,
108
 
      .prefix = GRUB_KERNEL_I386_COREBOOT_PREFIX,
109
 
      .data_end = GRUB_KERNEL_I386_COREBOOT_DATA_END,
 
121
      .prefix = GRUB_KERNEL_I386_MULTIBOOT_PREFIX,
 
122
      .prefix_end = GRUB_KERNEL_I386_MULTIBOOT_PREFIX_END,
110
123
      .raw_size = 0,
111
124
      .total_module_size = TARGET_NO_FIELD,
112
125
      .kernel_image_size = TARGET_NO_FIELD,
128
141
      .id = IMAGE_I386_PC, 
129
142
      .flags = PLATFORM_FLAGS_LZMA,
130
143
      .prefix = GRUB_KERNEL_I386_PC_PREFIX,
131
 
      .data_end = GRUB_KERNEL_I386_PC_DATA_END,
 
144
      .prefix_end = GRUB_KERNEL_I386_PC_PREFIX_END,
 
145
      .raw_size = GRUB_KERNEL_I386_PC_RAW_SIZE,
 
146
      .total_module_size = GRUB_KERNEL_I386_PC_TOTAL_MODULE_SIZE,
 
147
      .kernel_image_size = GRUB_KERNEL_I386_PC_KERNEL_IMAGE_SIZE,
 
148
      .compressed_size = GRUB_KERNEL_I386_PC_COMPRESSED_SIZE,
 
149
      .section_align = 1,
 
150
      .vaddr_offset = 0,
 
151
      .install_dos_part = GRUB_KERNEL_I386_PC_INSTALL_DOS_PART,
 
152
      .install_bsd_part = GRUB_KERNEL_I386_PC_INSTALL_BSD_PART,
 
153
      .link_addr = GRUB_KERNEL_I386_PC_LINK_ADDR
 
154
    },
 
155
    {
 
156
      .name = "i386-pc-pxe",
 
157
      .voidp_sizeof = 4,
 
158
      .bigendian = 0,
 
159
      .id = IMAGE_I386_PC_PXE, 
 
160
      .flags = PLATFORM_FLAGS_LZMA,
 
161
      .prefix = GRUB_KERNEL_I386_PC_PREFIX,
 
162
      .prefix_end = GRUB_KERNEL_I386_PC_PREFIX_END,
132
163
      .raw_size = GRUB_KERNEL_I386_PC_RAW_SIZE,
133
164
      .total_module_size = GRUB_KERNEL_I386_PC_TOTAL_MODULE_SIZE,
134
165
      .kernel_image_size = GRUB_KERNEL_I386_PC_KERNEL_IMAGE_SIZE,
146
177
      .id = IMAGE_EFI,
147
178
      .flags = PLATFORM_FLAGS_NONE,
148
179
      .prefix = GRUB_KERNEL_I386_EFI_PREFIX,
149
 
      .data_end = GRUB_KERNEL_I386_EFI_DATA_END,
 
180
      .prefix_end = GRUB_KERNEL_I386_EFI_PREFIX_END,
150
181
      .raw_size = 0,
151
182
      .total_module_size = TARGET_NO_FIELD,
152
183
      .kernel_image_size = TARGET_NO_FIELD,
168
199
      .id = IMAGE_I386_IEEE1275, 
169
200
      .flags = PLATFORM_FLAGS_NONE,
170
201
      .prefix = GRUB_KERNEL_I386_IEEE1275_PREFIX,
171
 
      .data_end = GRUB_KERNEL_I386_IEEE1275_DATA_END,
 
202
      .prefix_end = GRUB_KERNEL_I386_IEEE1275_PREFIX_END,
172
203
      .raw_size = 0,
173
204
      .total_module_size = TARGET_NO_FIELD,
174
205
      .kernel_image_size = TARGET_NO_FIELD,
190
221
      .id = IMAGE_QEMU, 
191
222
      .flags = PLATFORM_FLAGS_NONE,
192
223
      .prefix = GRUB_KERNEL_I386_QEMU_PREFIX,
193
 
      .data_end = GRUB_KERNEL_I386_QEMU_DATA_END,
 
224
      .prefix_end = GRUB_KERNEL_I386_QEMU_PREFIX_END,
194
225
      .raw_size = 0,
195
226
      .total_module_size = TARGET_NO_FIELD,
196
227
      .compressed_size = TARGET_NO_FIELD,
208
239
      .id = IMAGE_EFI, 
209
240
      .flags = PLATFORM_FLAGS_NONE,
210
241
      .prefix = GRUB_KERNEL_X86_64_EFI_PREFIX,
211
 
      .data_end = GRUB_KERNEL_X86_64_EFI_DATA_END,
 
242
      .prefix_end = GRUB_KERNEL_X86_64_EFI_PREFIX_END,
212
243
      .raw_size = 0,
213
244
      .total_module_size = TARGET_NO_FIELD,
214
245
      .kernel_image_size = TARGET_NO_FIELD,
228
259
      .voidp_sizeof = 4,
229
260
      .bigendian = 0,
230
261
      .id = IMAGE_YEELOONG_FLASH, 
231
 
      .flags = PLATFORM_FLAGS_NONE,
 
262
      .flags = PLATFORM_FLAGS_DECOMPRESSORS,
232
263
      .prefix = GRUB_KERNEL_MIPS_YEELOONG_PREFIX,
233
 
      .data_end = GRUB_KERNEL_MIPS_YEELOONG_DATA_END,
234
 
      .raw_size = GRUB_KERNEL_MIPS_YEELOONG_RAW_SIZE,
 
264
      .prefix_end = GRUB_KERNEL_MIPS_YEELOONG_PREFIX_END,
 
265
      .raw_size = 0,
235
266
      .total_module_size = GRUB_KERNEL_MIPS_YEELOONG_TOTAL_MODULE_SIZE,
236
 
      .compressed_size = GRUB_KERNEL_MIPS_YEELOONG_COMPRESSED_SIZE,
237
 
      .kernel_image_size = GRUB_KERNEL_MIPS_YEELOONG_KERNEL_IMAGE_SIZE,
 
267
      .compressed_size = TARGET_NO_FIELD,
 
268
      .kernel_image_size = TARGET_NO_FIELD,
238
269
      .section_align = 1,
239
270
      .vaddr_offset = 0,
240
271
      .install_dos_part = TARGET_NO_FIELD,
241
272
      .install_bsd_part = TARGET_NO_FIELD,
242
273
      .link_addr = GRUB_KERNEL_MIPS_YEELOONG_LINK_ADDR,
243
274
      .elf_target = EM_MIPS,
244
 
      .link_align = GRUB_KERNEL_MIPS_YEELOONG_LINK_ALIGN
 
275
      .link_align = GRUB_KERNEL_MIPS_YEELOONG_LINK_ALIGN,
 
276
      .default_compression = COMPRESSION_NONE
245
277
    },
246
278
    {
247
279
      .name = "mipsel-yeeloong-elf",
248
280
      .voidp_sizeof = 4,
249
281
      .bigendian = 0,
250
282
      .id = IMAGE_YEELOONG_ELF, 
251
 
      .flags = PLATFORM_FLAGS_NONE,
 
283
      .flags = PLATFORM_FLAGS_DECOMPRESSORS,
252
284
      .prefix = GRUB_KERNEL_MIPS_YEELOONG_PREFIX,
253
 
      .data_end = GRUB_KERNEL_MIPS_YEELOONG_DATA_END,
254
 
      .raw_size = GRUB_KERNEL_MIPS_YEELOONG_RAW_SIZE,
 
285
      .prefix_end = GRUB_KERNEL_MIPS_YEELOONG_PREFIX_END,
 
286
      .raw_size = 0,
255
287
      .total_module_size = GRUB_KERNEL_MIPS_YEELOONG_TOTAL_MODULE_SIZE,
256
 
      .compressed_size = GRUB_KERNEL_MIPS_YEELOONG_COMPRESSED_SIZE,
257
 
      .kernel_image_size = GRUB_KERNEL_MIPS_YEELOONG_KERNEL_IMAGE_SIZE,
 
288
      .compressed_size = TARGET_NO_FIELD,
 
289
      .kernel_image_size = TARGET_NO_FIELD,
258
290
      .section_align = 1,
259
291
      .vaddr_offset = 0,
260
292
      .install_dos_part = TARGET_NO_FIELD,
261
293
      .install_bsd_part = TARGET_NO_FIELD,
262
294
      .link_addr = GRUB_KERNEL_MIPS_YEELOONG_LINK_ADDR,
263
295
      .elf_target = EM_MIPS,
264
 
      .link_align = GRUB_KERNEL_MIPS_YEELOONG_LINK_ALIGN
 
296
      .link_align = GRUB_KERNEL_MIPS_YEELOONG_LINK_ALIGN,
 
297
      .default_compression = COMPRESSION_NONE
265
298
    },
266
299
    {
267
300
      .name = "powerpc-ieee1275",
270
303
      .id = IMAGE_PPC, 
271
304
      .flags = PLATFORM_FLAGS_NONE,
272
305
      .prefix = GRUB_KERNEL_POWERPC_IEEE1275_PREFIX,
273
 
      .data_end = GRUB_KERNEL_POWERPC_IEEE1275_DATA_END,
 
306
      .prefix_end = GRUB_KERNEL_POWERPC_IEEE1275_PREFIX_END,
274
307
      .raw_size = 0,
275
308
      .total_module_size = TARGET_NO_FIELD,
276
309
      .kernel_image_size = TARGET_NO_FIELD,
292
325
      .id = IMAGE_SPARC64_RAW,
293
326
      .flags = PLATFORM_FLAGS_NONE,
294
327
      .prefix = GRUB_KERNEL_SPARC64_IEEE1275_PREFIX,
295
 
      .data_end = GRUB_KERNEL_SPARC64_IEEE1275_DATA_END,
 
328
      .prefix_end = GRUB_KERNEL_SPARC64_IEEE1275_PREFIX_END,
296
329
      .raw_size = GRUB_KERNEL_SPARC64_IEEE1275_RAW_SIZE,
297
330
      .total_module_size = GRUB_KERNEL_SPARC64_IEEE1275_TOTAL_MODULE_SIZE,
298
331
      .kernel_image_size = GRUB_KERNEL_SPARC64_IEEE1275_KERNEL_IMAGE_SIZE,
310
343
      .id = IMAGE_SPARC64_AOUT,
311
344
      .flags = PLATFORM_FLAGS_NONE,
312
345
      .prefix = GRUB_KERNEL_SPARC64_IEEE1275_PREFIX,
313
 
      .data_end = GRUB_KERNEL_SPARC64_IEEE1275_DATA_END,
 
346
      .prefix_end = GRUB_KERNEL_SPARC64_IEEE1275_PREFIX_END,
314
347
      .raw_size = GRUB_KERNEL_SPARC64_IEEE1275_RAW_SIZE,
315
348
      .total_module_size = GRUB_KERNEL_SPARC64_IEEE1275_TOTAL_MODULE_SIZE,
316
349
      .kernel_image_size = GRUB_KERNEL_SPARC64_IEEE1275_KERNEL_IMAGE_SIZE,
473
506
  *core_size += raw_size;
474
507
}
475
508
 
 
509
#ifdef HAVE_LIBLZMA
 
510
static void
 
511
compress_kernel_xz (char *kernel_img, size_t kernel_size,
 
512
                    char **core_img, size_t *core_size, size_t raw_size)
 
513
{
 
514
  lzma_stream strm = LZMA_STREAM_INIT;
 
515
  lzma_ret xzret;
 
516
  lzma_options_lzma lzopts = {
 
517
    .dict_size = 1 << 16,
 
518
    .preset_dict = NULL,
 
519
    .preset_dict_size = 0,
 
520
    .lc = 3,
 
521
    .lp = 0,
 
522
    .pb = 2,
 
523
    .mode = LZMA_MODE_NORMAL,
 
524
    .nice_len = 64,
 
525
    .mf = LZMA_MF_BT4,
 
526
    .depth = 0,
 
527
  };
 
528
  lzma_filter fltrs[] = {
 
529
    { .id = LZMA_FILTER_LZMA2, .options = &lzopts},
 
530
    { .id = LZMA_VLI_UNKNOWN, .options = NULL}
 
531
  };
 
532
 
 
533
  if (kernel_size < raw_size)
 
534
    grub_util_error (_("the core image is too small"));
 
535
 
 
536
  xzret = lzma_stream_encoder (&strm, fltrs, LZMA_CHECK_NONE);
 
537
  if (xzret != LZMA_OK)
 
538
    grub_util_error (_("cannot compress the kernel image"));
 
539
 
 
540
  *core_img = xmalloc (kernel_size);
 
541
  memcpy (*core_img, kernel_img, raw_size);
 
542
 
 
543
  *core_size = kernel_size - raw_size;
 
544
  strm.next_in = (unsigned char *) kernel_img + raw_size;
 
545
  strm.avail_in = kernel_size - raw_size;
 
546
  strm.next_out = (unsigned char *) *core_img + raw_size;
 
547
  strm.avail_out = *core_size;
 
548
 
 
549
  while (1)
 
550
    {
 
551
      xzret = lzma_code (&strm, LZMA_FINISH);
 
552
      if (xzret == LZMA_OK)
 
553
        continue;
 
554
      if (xzret == LZMA_STREAM_END)
 
555
        break;
 
556
      grub_util_error (_("cannot compress the kernel image"));
 
557
    }
 
558
 
 
559
  *core_size -= strm.avail_out;
 
560
 
 
561
  *core_size += raw_size;
 
562
}
 
563
#endif
 
564
 
476
565
static void
477
566
compress_kernel (struct image_target_desc *image_target, char *kernel_img,
478
 
                 size_t kernel_size, char **core_img, size_t *core_size)
 
567
                 size_t kernel_size, char **core_img, size_t *core_size,
 
568
                 grub_compression_t comp)
479
569
{
480
570
 if (image_target->flags & PLATFORM_FLAGS_LZMA)
481
571
   {
484
574
     return;
485
575
   }
486
576
 
 
577
#ifdef HAVE_LIBLZMA
 
578
 if (image_target->flags & PLATFORM_FLAGS_DECOMPRESSORS
 
579
     && (comp == COMPRESSION_XZ))
 
580
   {
 
581
     compress_kernel_xz (kernel_img, kernel_size, core_img,
 
582
                         core_size, image_target->raw_size);
 
583
     return;
 
584
   }
 
585
#endif
 
586
 
 
587
 if (image_target->flags & PLATFORM_FLAGS_DECOMPRESSORS
 
588
     && (comp != COMPRESSION_NONE))
 
589
   grub_util_error ("unknown compression %d\n", comp);
 
590
 
487
591
  *core_img = xmalloc (kernel_size);
488
592
  memcpy (*core_img, kernel_img, kernel_size);
489
593
  *core_size = kernel_size;
506
610
 
507
611
static void
508
612
generate_image (const char *dir, char *prefix, FILE *out, char *mods[],
509
 
                char *memdisk_path, char *font_path, char *config_path,
510
 
                struct image_target_desc *image_target, int note)
 
613
                char *memdisk_path, char *config_path,
 
614
                struct image_target_desc *image_target, int note,
 
615
                grub_compression_t comp)
511
616
{
512
617
  char *kernel_img, *core_img;
513
618
  size_t kernel_size, total_module_size, core_size, exec_size;
514
 
  size_t memdisk_size = 0, font_size = 0, config_size = 0, config_size_pure = 0;
 
619
  size_t memdisk_size = 0, config_size = 0, config_size_pure = 0;
515
620
  char *kernel_path;
516
621
  size_t offset;
517
622
  struct grub_util_path_list *path_list, *p, *next;
519
624
  grub_uint64_t start_address;
520
625
  void *rel_section;
521
626
  grub_size_t reloc_size, align;
 
627
 
 
628
  if (comp == COMPRESSION_AUTO)
 
629
    comp = image_target->default_compression;
 
630
 
522
631
  path_list = grub_util_resolve_dependencies (dir, "moddep.lst", mods);
523
632
 
524
633
  kernel_path = grub_util_get_path (dir, "kernel.img");
535
644
      total_module_size += memdisk_size + sizeof (struct grub_module_header);
536
645
    }
537
646
 
538
 
  if (font_path)
539
 
    {
540
 
      font_size = ALIGN_ADDR (grub_util_get_image_size (font_path));
541
 
      total_module_size += font_size + sizeof (struct grub_module_header);
542
 
    }
543
 
 
544
647
  if (config_path)
545
648
    {
546
649
      config_size_pure = grub_util_get_image_size (config_path) + 1;
564
667
                               total_module_size, &start_address, &rel_section,
565
668
                               &reloc_size, &align, image_target);
566
669
 
567
 
  if (image_target->prefix + strlen (prefix) + 1 > image_target->data_end)
 
670
  if (image_target->prefix + strlen (prefix) + 1 > image_target->prefix_end)
568
671
    grub_util_error (_("prefix is too long"));
569
672
  strcpy (kernel_img + image_target->prefix, prefix);
570
673
 
624
727
      offset += memdisk_size;
625
728
    }
626
729
 
627
 
  if (font_path)
628
 
    {
629
 
      struct grub_module_header *header;
630
 
 
631
 
      header = (struct grub_module_header *) (kernel_img + offset);
632
 
      memset (header, 0, sizeof (struct grub_module_header));
633
 
      header->type = grub_host_to_target32 (OBJ_TYPE_FONT);
634
 
      header->size = grub_host_to_target32 (font_size + sizeof (*header));
635
 
      offset += sizeof (*header);
636
 
 
637
 
      grub_util_load_image (font_path, kernel_img + offset);
638
 
      offset += font_size;
639
 
    }
640
 
 
641
730
  if (config_path)
642
731
    {
643
732
      struct grub_module_header *header;
653
742
      offset += config_size;
654
743
    }
655
744
 
 
745
  if ((image_target->flags & PLATFORM_FLAGS_DECOMPRESSORS)
 
746
      && (image_target->total_module_size != TARGET_NO_FIELD))
 
747
    *((grub_uint32_t *) (kernel_img + image_target->total_module_size))
 
748
      = grub_host_to_target32 (total_module_size);
 
749
 
656
750
  grub_util_info ("kernel_img=%p, kernel_size=0x%x", kernel_img, kernel_size);
657
751
  compress_kernel (image_target, kernel_img, kernel_size + total_module_size,
658
 
                   &core_img, &core_size);
 
752
                   &core_img, &core_size, comp);
659
753
 
660
754
  grub_util_info ("the core size is 0x%x", core_size);
661
755
 
662
 
  if (image_target->total_module_size != TARGET_NO_FIELD)
 
756
  if (!(image_target->flags & PLATFORM_FLAGS_DECOMPRESSORS) 
 
757
      && image_target->total_module_size != TARGET_NO_FIELD)
663
758
    *((grub_uint32_t *) (core_img + image_target->total_module_size))
664
759
      = grub_host_to_target32 (total_module_size);
665
760
  if (image_target->kernel_image_size != TARGET_NO_FIELD)
680
775
        = grub_host_to_target32 (-2);
681
776
    }
682
777
 
 
778
  if (image_target->flags & PLATFORM_FLAGS_DECOMPRESSORS)
 
779
    {
 
780
      char *full_img;
 
781
      size_t full_size;
 
782
      char *decompress_path, *decompress_img;
 
783
      size_t decompress_size;
 
784
      const char *name;
 
785
 
 
786
      switch (comp)
 
787
        {
 
788
        case COMPRESSION_XZ:
 
789
          name = "xz_decompress.img";
 
790
          break;
 
791
        case COMPRESSION_NONE:
 
792
          name = "none_decompress.img";
 
793
          break;
 
794
        default:
 
795
          grub_util_error ("unknown compression %d\n", comp);
 
796
        }
 
797
      
 
798
      decompress_path = grub_util_get_path (dir, name);
 
799
      decompress_size = grub_util_get_image_size (decompress_path);
 
800
      decompress_img = grub_util_read_image (decompress_path);
 
801
 
 
802
      *((grub_uint32_t *) (decompress_img + GRUB_KERNEL_MIPS_YEELOONG_COMPRESSED_SIZE))
 
803
        = grub_host_to_target32 (core_size);
 
804
 
 
805
      *((grub_uint32_t *) (decompress_img + GRUB_KERNEL_MIPS_YEELOONG_UNCOMPRESSED_SIZE))
 
806
        = grub_host_to_target32 (kernel_size + total_module_size);
 
807
 
 
808
      full_size = core_size + decompress_size;
 
809
 
 
810
      full_img = xmalloc (full_size);
 
811
      memset (full_img, 0, full_size); 
 
812
 
 
813
      memcpy (full_img, decompress_img, decompress_size);
 
814
 
 
815
      memcpy (full_img + decompress_size, core_img, core_size);
 
816
 
 
817
      memset (full_img + decompress_size + core_size, 0,
 
818
              full_size - (decompress_size + core_size));
 
819
 
 
820
      free (core_img);
 
821
      core_img = full_img;
 
822
      core_size = full_size;
 
823
    }
 
824
 
683
825
  switch (image_target->id)
684
826
    {
685
827
    case IMAGE_I386_PC:
 
828
    case IMAGE_I386_PC_PXE:
686
829
      {
687
830
        unsigned num;
688
831
        char *boot_path, *boot_img;
697
840
        if (num > 0xffff)
698
841
          grub_util_error (_("the core image is too big"));
699
842
 
 
843
        if (image_target->id == IMAGE_I386_PC_PXE)
 
844
          {
 
845
            char *pxeboot_path, *pxeboot_img;
 
846
            size_t pxeboot_size;
 
847
            
 
848
            pxeboot_path = grub_util_get_path (dir, "pxeboot.img");
 
849
            pxeboot_size = grub_util_get_image_size (pxeboot_path);
 
850
            pxeboot_img = grub_util_read_image (pxeboot_path);
 
851
            
 
852
            grub_util_write_image (pxeboot_img, pxeboot_size, out);
 
853
            free (pxeboot_img);
 
854
            free (pxeboot_path);
 
855
          }
 
856
 
700
857
        boot_path = grub_util_get_path (dir, "diskboot.img");
701
858
        boot_size = grub_util_get_image_size (boot_path);
702
859
        if (boot_size != GRUB_DISK_SECTOR_SIZE)
1203
1360
    {"output", required_argument, 0, 'o'},
1204
1361
    {"note", no_argument, 0, 'n'},
1205
1362
    {"format", required_argument, 0, 'O'},
 
1363
    {"compression", required_argument, 0, 'C'},
1206
1364
    {"help", no_argument, 0, 'h'},
1207
1365
    {"version", no_argument, 0, 'V'},
1208
1366
    {"verbose", no_argument, 0, 'v'},
1240
1398
  -d, --directory=DIR     use images and modules under DIR [default=%s/@platform@]\n\
1241
1399
  -p, --prefix=DIR        set grub_prefix directory [default=%s]\n\
1242
1400
  -m, --memdisk=FILE      embed FILE as a memdisk image\n\
1243
 
  -f, --font=FILE         embed FILE as a boot font\n\
1244
1401
  -c, --config=FILE       embed FILE as boot config\n\
1245
1402
  -n, --note              add NOTE segment for CHRP Open Firmware\n\
1246
1403
  -o, --output=FILE       output a generated image to FILE [default=stdout]\n\
1247
1404
  -O, --format=FORMAT     generate an image in format\n\
1248
1405
                          available formats: %s\n\
 
1406
  -C, --compression=(xz|none|auto)  choose the compression to use\n\
1249
1407
  -h, --help              display this message and exit\n\
1250
1408
  -V, --version           print version information and exit\n\
1251
1409
  -v, --verbose           print verbose messages\n\
1272
1430
  FILE *fp = stdout;
1273
1431
  int note = 0;
1274
1432
  struct image_target_desc *image_target = NULL;
 
1433
  grub_compression_t comp = COMPRESSION_AUTO;
1275
1434
 
1276
1435
  set_program_name (argv[0]);
1277
1436
 
1279
1438
 
1280
1439
  while (1)
1281
1440
    {
1282
 
      int c = getopt_long (argc, argv, "d:p:m:c:o:O:f:hVvn", options, 0);
 
1441
      int c = getopt_long (argc, argv, "d:p:m:c:o:O:f:C:hVvn", options, 0);
1283
1442
 
1284
1443
      if (c == -1)
1285
1444
        break;
1329
1488
            prefix = xstrdup ("(memdisk)/boot/grub");
1330
1489
            break;
1331
1490
 
1332
 
          case 'f':
1333
 
            if (font)
1334
 
              free (font);
1335
 
 
1336
 
            font = xstrdup (optarg);
1337
 
            break;
1338
 
 
1339
1491
          case 'c':
1340
1492
            if (config)
1341
1493
              free (config);
1343
1495
            config = xstrdup (optarg);
1344
1496
            break;
1345
1497
 
 
1498
          case 'C':
 
1499
            if (grub_strcmp (optarg, "xz") == 0)
 
1500
              {
 
1501
#ifdef HAVE_LIBLZMA
 
1502
                comp = COMPRESSION_XZ;
 
1503
#else
 
1504
                grub_util_error ("grub-mkimage is compiled without XZ support",
 
1505
                                 optarg);
 
1506
#endif
 
1507
              }
 
1508
            else if (grub_strcmp (optarg, "none") == 0)
 
1509
              comp = COMPRESSION_NONE;
 
1510
            else
 
1511
              grub_util_error ("Unknown compression format %s", optarg);
 
1512
            break;
 
1513
 
1346
1514
          case 'h':
1347
1515
            usage (0);
1348
1516
            break;
1400
1568
    }
1401
1569
 
1402
1570
  generate_image (dir, prefix ? : DEFAULT_DIRECTORY, fp,
1403
 
                  argv + optind, memdisk, font, config,
1404
 
                  image_target, note);
 
1571
                  argv + optind, memdisk, config,
 
1572
                  image_target, note, comp);
1405
1573
 
1406
1574
  fclose (fp);
1407
1575