/* grub-mkimage.c - make a bootable image */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
*
* 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 <time.h>
#include <multiboot.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <getopt.h>
#include "progname.h"
#ifdef HAVE_LIBLZMA
#include <lzma.h>
#endif
typedef enum {
struct image_target_desc
{
const char *dirname;
int bigendian;
enum {
} id;
enum
{
PLATFORM_FLAGS_NONE = 0,
} flags;
unsigned total_module_size;
unsigned decompressor_compressed_size;
unsigned decompressor_uncompressed_size;
unsigned decompressor_uncompressed_addr;
unsigned link_align;
unsigned section_align;
signed vaddr_offset;
};
+ sizeof (struct grub_pe32_coff_header) \
+ sizeof (struct grub_pe64_optional_header) \
+ 4 * sizeof (struct grub_pe32_section_table), \
{
{
.dirname = "i386-coreboot",
.voidp_sizeof = 4,
.bigendian = 0,
.id = IMAGE_COREBOOT,
.section_align = 1,
.vaddr_offset = 0,
.elf_target = EM_386,
.link_align = 4,
},
{
.dirname = "i386-multiboot",
.voidp_sizeof = 4,
.bigendian = 0,
.id = IMAGE_COREBOOT,
.section_align = 1,
.vaddr_offset = 0,
.elf_target = EM_386,
.link_align = 4,
},
{
.dirname = "i386-pc",
.voidp_sizeof = 4,
.bigendian = 0,
.id = IMAGE_I386_PC,
.section_align = 1,
.vaddr_offset = 0,
},
{
.dirname = "i386-pc",
.voidp_sizeof = 4,
.bigendian = 0,
.id = IMAGE_I386_PC_PXE,
.section_align = 1,
.vaddr_offset = 0,
},
{
.dirname = "i386-efi",
.voidp_sizeof = 4,
.bigendian = 0,
+ sizeof (struct grub_pe32_coff_header)
+ sizeof (struct grub_pe32_optional_header)
+ 4 * sizeof (struct grub_pe32_section_table),
.elf_target = EM_386,
},
{
.dirname = "i386-ieee1275",
.voidp_sizeof = 4,
.bigendian = 0,
.section_align = 1,
.vaddr_offset = 0,
.elf_target = EM_386,
.link_align = 4,
},
{
.dirname = "i386-qemu",
.voidp_sizeof = 4,
.bigendian = 0,
.id = IMAGE_QEMU,
.section_align = 1,
.vaddr_offset = 0,
},
{
.dirname = "x86_64-efi",
.voidp_sizeof = 8,
.bigendian = 0,
.elf_target = EM_X86_64,
},
{
.dirname = "mipsel-loongson",
.voidp_sizeof = 4,
.bigendian = 0,
.section_align = 1,
.vaddr_offset = 0,
.elf_target = EM_MIPS,
},
{
.dirname = "mipsel-loongson",
.voidp_sizeof = 4,
.bigendian = 0,
.section_align = 1,
.vaddr_offset = 0,
.elf_target = EM_MIPS,
},
{
.dirname = "mipsel-loongson",
"mipsel-fuloong2f-elf", "mipsel-fuloong2e-elf",
"mipsel-fuloong-elf", NULL },
.voidp_sizeof = 4,
.bigendian = 0,
.id = IMAGE_LOONGSON_ELF,
.section_align = 1,
.vaddr_offset = 0,
.elf_target = EM_MIPS,
},
{
.dirname = "powerpc-ieee1275",
.voidp_sizeof = 4,
.bigendian = 1,
.section_align = 1,
.vaddr_offset = 0,
.elf_target = EM_PPC,
.link_align = 4
},
{
.dirname = "sparc64-ieee1275",
.voidp_sizeof = 8,
.bigendian = 1,
.id = IMAGE_SPARC64_RAW,
.section_align = 1,
.vaddr_offset = 0,
},
{
.dirname = "sparc64-ieee1275",
.voidp_sizeof = 8,
.bigendian = 1,
.id = IMAGE_SPARC64_AOUT,
.section_align = 1,
.vaddr_offset = 0,
},
{
.dirname = "ia64-efi",
.voidp_sizeof = 8,
.bigendian = 0,
.elf_target = EM_IA_64,
},
{
.dirname = "mips-arc",
.voidp_sizeof = 4,
.bigendian = 1,
.id = IMAGE_MIPS_ARC,
.section_align = 1,
.vaddr_offset = 0,
.elf_target = EM_MIPS,
},
{
.dirname = "mipsel-qemu_mips",
.voidp_sizeof = 4,
.bigendian = 0,
.id = IMAGE_LOONGSON_ELF,
.section_align = 1,
.vaddr_offset = 0,
.elf_target = EM_MIPS,
},
{
.dirname = "mips-qemu_mips",
.voidp_sizeof = 4,
.bigendian = 1,
.section_align = 1,
.vaddr_offset = 0,
.elf_target = EM_MIPS,
},
{
.dirname = "mipsel-qemu_mips",
.voidp_sizeof = 4,
.bigendian = 0,
.section_align = 1,
.vaddr_offset = 0,
.elf_target = EM_MIPS,
},
{
.dirname = "mips-qemu_mips",
.voidp_sizeof = 4,
.bigendian = 1,
.id = IMAGE_LOONGSON_ELF,
.section_align = 1,
.vaddr_offset = 0,
.elf_target = EM_MIPS,
},
};
static inline grub_uint32_t
{
if (image_target->bigendian)
return grub_be_to_cpu32 (in);
else
return grub_le_to_cpu32 (in);
}
static inline grub_uint64_t
{
if (image_target->bigendian)
return grub_be_to_cpu64 (in);
else
return grub_le_to_cpu64 (in);
}
static inline grub_uint64_t
{
if (image_target->bigendian)
return grub_cpu_to_be64 (in);
else
return grub_cpu_to_le64 (in);
}
static inline grub_uint32_t
{
if (image_target->bigendian)
return grub_cpu_to_be32 (in);
else
return grub_cpu_to_le32 (in);
}
static inline grub_uint16_t
{
if (image_target->bigendian)
return grub_be_to_cpu16 (in);
else
return grub_le_to_cpu16 (in);
}
static inline grub_uint16_t
{
if (image_target->bigendian)
return grub_cpu_to_be16 (in);
else
return grub_cpu_to_le16 (in);
}
static inline grub_uint64_t
{
else
}
static inline grub_uint64_t
{
else
}
/* These structures are defined according to the CHRP binding to IEEE1275,
"Client Program Format" section. */
struct grub_ieee1275_note_hdr
{
};
struct grub_ieee1275_note_desc
{
};
struct grub_ieee1275_note
{
};
static void
{
*core_size = kernel_size;
(unsigned char *) kernel_img,
grub_util_error (_("cannot compress the kernel image"));
}
#ifdef HAVE_LIBLZMA
static void
{
.preset_dict = NULL,
.preset_dict_size = 0,
.lc = 3,
.lp = 0,
.pb = 2,
.mode = LZMA_MODE_NORMAL,
.nice_len = 64,
.mf = LZMA_MF_BT4,
.depth = 0,
};
};
grub_util_error (_("cannot compress the kernel image"));
*core_size = kernel_size;
while (1)
{
continue;
if (xzret == LZMA_STREAM_END)
break;
grub_util_error (_("cannot compress the kernel image"));
}
}
#endif
static void
{
&& (comp == COMPRESSION_LZMA))
{
return;
}
#ifdef HAVE_LIBLZMA
&& (comp == COMPRESSION_XZ))
{
return;
}
#endif
&& (comp != COMPRESSION_NONE))
*core_size = kernel_size;
}
struct fixup_block_list
{
int state;
struct grub_pe32_fixup_block b;
};
#include "grub-mkimagexx.c"
#include "grub-mkimagexx.c"
static void
char *memdisk_path, char *config_path,
{
char *kernel_path;
void *rel_section;
if (comp == COMPRESSION_AUTO)
total_module_size = sizeof (struct grub_module_info64);
else
total_module_size = sizeof (struct grub_module_info32);
if (memdisk_path)
{
}
if (config_path)
{
}
if (prefix)
{
}
+ sizeof (struct grub_module_header));
else
{
/* Fill in the grub_module_info structure. */
else
offset = sizeof (struct grub_module_info64);
else
}
else
{
/* Fill in the grub_module_info structure. */
else
offset = sizeof (struct grub_module_info32);
else
}
{
}
if (memdisk_path)
{
offset += memdisk_size;
}
if (config_path)
{
offset += config_size;
}
if (prefix)
{
offset += prefix_size;
}
free (kernel_img);
{
char *full_img;
const char *name;
switch (comp)
{
case COMPRESSION_XZ:
name = "xz_decompress.img";
break;
case COMPRESSION_LZMA:
name = "lzma_decompress.img";
break;
case COMPRESSION_NONE:
name = "none_decompress.img";
break;
default:
}
grub_util_error (_("Decompressor is too big"));
*((grub_uint32_t *) (decompress_img
*((grub_uint32_t *) (decompress_img
{
else
}
}
switch (image_target->id)
{
case IMAGE_I386_PC:
case IMAGE_I386_PC_PXE:
{
unsigned num;
grub_util_error (_("core image is too big (0x%x > 0x%x)"),
0x78000);
if (num > 0xffff)
grub_util_error (_("the core image is too big"));
{
free (pxeboot_img);
free (pxeboot_path);
/* Remove Multiboot header to avoid confusing ipxe. */
&& grub_target_to_host32 (ptr[0])
{
*ptr = 0;
break;
}
}
if (boot_size != GRUB_DISK_SECTOR_SIZE)
grub_util_error (_("diskboot.img size must be %u bytes"),
{
- sizeof (*block));
/* This is filled elsewhere. Verify it just in case. */
+ (GRUB_DISK_SECTOR_SIZE >> 4)));
}
}
break;
case IMAGE_EFI:
{
void *pe_img;
void *sections;
struct grub_pe32_coff_header *c;
int header_size;
int reloc_addr;
+ sizeof (struct grub_pe32_coff_header)
+ sizeof (struct grub_pe32_optional_header)
+ 4 * sizeof (struct grub_pe32_section_table),
else
/* The magic. */
/* The COFF file header. */
: 0)
/* The PE Optional header. */
{
struct grub_pe32_optional_header *o;
o = (struct grub_pe32_optional_header *)
+ sizeof (struct grub_pe32_coff_header));
- header_size);
o->image_base = 0;
/* Do these really matter? */
sections = o + 1;
}
else
{
struct grub_pe64_optional_header *o;
o = (struct grub_pe64_optional_header *)
+ sizeof (struct grub_pe32_coff_header));
- header_size);
o->image_base = 0;
/* Do these really matter? */
sections = o + 1;
}
/* The sections. */
#if 0
bss_section->raw_data_size = 0;
bss_section->raw_data_offset = 0;
| 0x80);
#endif
}
break;
case IMAGE_QEMU:
{
char *rom_img;
/* Rom sizes must be 64k-aligned. */
}
break;
case IMAGE_SPARC64_AOUT:
{
void *aout_img;
| AOUT32_OMAGIC);
}
break;
case IMAGE_SPARC64_RAW:
{
unsigned int num;
if (boot_size != GRUB_DISK_SECTOR_SIZE)
grub_util_error (_("diskboot.img is not one sector size"));
= grub_host_to_target32 (num);
}
break;
case IMAGE_YEELOONG_FLASH:
case IMAGE_FULOONG2F_FLASH:
{
char *rom_img;
/* fwstart.img is the only part which can't be tested by using *-elf
target. Check it against the checksum. */
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
/* None yet. */
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
{
}
else
{
}
GRUB_MD_SHA512->mdlen) != 0)
grub_util_warn (_("fwstart.img doesn't match the known good version. "
"proceed at your own risk"));
grub_util_error (_("firmware image is too big"));
}
break;
case IMAGE_QEMU_MIPS_FLASH:
{
char *rom_img;
grub_util_error (_("firmware image is too big"));
}
break;
case IMAGE_MIPS_ARC:
{
char *ecoff_img;
struct ecoff_header {
};
struct ecoff_section
{
};
if (comp == COMPRESSION_NONE)
else
- (1 << 20));
}
break;
case IMAGE_LOONGSON_ELF:
case IMAGE_PPC:
case IMAGE_COREBOOT:
case IMAGE_I386_IEEE1275:
{
char *elf_img;
phnum += 2;
if (note)
{
phnum++;
footer_size += sizeof (struct grub_ieee1275_note);
}
if (!image_target->bigendian)
else
/* No section headers. */
else
{
if (comp == COMPRESSION_NONE)
else
}
else
phdr->p_align = grub_host_to_target32 (align > image_target->link_align ? align : image_target->link_align);
| EF_MIPS_PIC | EF_MIPS_CPIC);
else
{
}
else
{
phdr++;
phdr++;
+ image_target->mod_gap,
}
if (note)
{
grub_util_info ("adding CHRP NOTE segment");
phdr++;
}
}
break;
}
free (kernel_path);
while (path_list)
{
}
}
{
{0, 0, 0, 0}
};
static void
{
if (status)
else
{
int format_len = 0;
char *formats;
char *ptr;
unsigned i;
for (i = 0; i < ARRAY_SIZE (image_targets); i++)
for (i = 0; i < ARRAY_SIZE (image_targets); i++)
{
*ptr++ = ',';
*ptr++ = ' ';
}
ptr[-2] = 0;
printf (_("\
Usage: %s [OPTION]... [MODULES]\n\
\n\
Make a bootable image of GRUB.\n\
\n\
-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\
-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\
-O, --format=FORMAT generate an image in format\n\
available formats: %s\n\
-C, --compression=(xz|none|auto) choose the compression to use\n\
-h, --help display this message and exit\n\
-V, --version print version information and exit\n\
-v, --verbose print verbose messages\n\
\n\
Report bugs to <%s>.\n\
"),
}
}
int
{
set_program_name (argv[0]);
while (1)
{
if (c == -1)
break;
else
switch (c)
{
case 'o':
if (output)
break;
case 'O':
{
unsigned i, j;
for (i = 0; i < ARRAY_SIZE (image_targets); i++)
image_targets[i].names[j]; j++)
image_target = &image_targets[i];
if (!image_target)
{
usage (1);
}
break;
}
case 'd':
if (dir)
break;
case 'n':
note = 1;
break;
case 'm':
if (memdisk)
if (prefix)
break;
case 'c':
if (config)
break;
case 'C':
{
#ifdef HAVE_LIBLZMA
#else
grub_util_error (_("grub-mkimage is compiled without XZ support"),
optarg);
#endif
}
else
break;
case 'h':
usage (0);
break;
case 'p':
if (prefix)
break;
case 'V':
return 0;
case 'v':
verbosity++;
break;
default:
usage (1);
break;
}
}
if (!image_target)
{
printf (_("Target format not specified (use the -O option).\n"));
usage (1);
}
if (!dir)
{
}
#ifdef __sun__
{
if (err > 0) /* Actual version check failed */
"modules in %s.\nRefusing to grub-mkimage with incompatible modules.\n"),
dir);
exit(5);
}
#endif
if (output)
{
if (! fp)
}
if (dir)
return 0;
}