1
/* grub-mkimage.c - make a bootable image */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2002,2003,2004,2005 Free Software Foundation, Inc.
6
* GRUB is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with GRUB; if not, write to the Free Software
18
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
#include <grub/types.h>
23
#include <grub/machine/boot.h>
24
#include <grub/machine/kernel.h>
25
#include <grub/kernel.h>
26
#include <grub/disk.h>
27
#include <grub/util/misc.h>
28
#include <grub/util/resolve.h>
38
#if defined(HAVE_LZO_LZO1X_H)
39
# include <lzo/lzo1x.h>
40
#elif defined(HAVE_LZO1X_H)
45
compress_kernel (char *kernel_img, size_t kernel_size,
46
char **core_img, size_t *core_size)
51
grub_util_info ("kernel_img=%p, kernel_size=0x%x", kernel_img, kernel_size);
52
if (kernel_size < GRUB_KERNEL_MACHINE_RAW_SIZE)
53
grub_util_error ("the core image is too small");
55
if (lzo_init () != LZO_E_OK)
56
grub_util_error ("cannot initialize LZO");
58
*core_img = xmalloc (kernel_size + kernel_size / 64 + 16 + 3);
59
wrkmem = xmalloc (LZO1X_999_MEM_COMPRESS);
61
memcpy (*core_img, kernel_img, GRUB_KERNEL_MACHINE_RAW_SIZE);
63
grub_util_info ("compressing the core image");
64
if (lzo1x_999_compress (kernel_img + GRUB_KERNEL_MACHINE_RAW_SIZE,
65
kernel_size - GRUB_KERNEL_MACHINE_RAW_SIZE,
66
*core_img + GRUB_KERNEL_MACHINE_RAW_SIZE,
69
grub_util_error ("cannot compress the kernel image");
73
*core_size = (size_t) size + GRUB_KERNEL_MACHINE_RAW_SIZE;
77
generate_image (const char *dir, FILE *out, char *mods[])
79
grub_addr_t module_addr = 0;
80
char *kernel_img, *boot_img, *core_img;
81
size_t kernel_size, boot_size, total_module_size, core_size;
82
char *kernel_path, *boot_path;
85
struct grub_util_path_list *path_list, *p, *next;
86
struct grub_module_info *modinfo;
88
path_list = grub_util_resolve_dependencies (dir, "moddep.lst", mods);
90
kernel_path = grub_util_get_path (dir, "kernel.img");
91
kernel_size = grub_util_get_image_size (kernel_path);
93
total_module_size = sizeof (struct grub_module_info);
94
for (p = path_list; p; p = p->next)
95
total_module_size += (grub_util_get_image_size (p->name)
96
+ sizeof (struct grub_module_header));
98
grub_util_info ("the total module size is 0x%x", total_module_size);
100
kernel_img = xmalloc (kernel_size + total_module_size);
101
grub_util_load_image (kernel_path, kernel_img);
103
/* Fill in the grub_module_info structure. */
104
modinfo = (struct grub_module_info *) (kernel_img + kernel_size);
105
modinfo->magic = GRUB_MODULE_MAGIC;
106
modinfo->offset = sizeof (struct grub_module_info);
107
modinfo->size = total_module_size;
109
offset = kernel_size + sizeof (struct grub_module_info);
110
for (p = path_list; p; p = p->next)
112
struct grub_module_header *header;
115
mod_size = grub_util_get_image_size (p->name);
117
header = (struct grub_module_header *) (kernel_img + offset);
118
header->offset = grub_cpu_to_le32 (sizeof (*header));
119
header->size = grub_cpu_to_le32 (mod_size + sizeof (*header));
121
grub_util_load_image (p->name, kernel_img + offset + sizeof (*header));
123
offset += sizeof (*header) + mod_size;
126
compress_kernel (kernel_img, kernel_size + total_module_size,
127
&core_img, &core_size);
129
grub_util_info ("the core size is 0x%x", core_size);
131
num = ((core_size + GRUB_DISK_SECTOR_SIZE - 1) >> GRUB_DISK_SECTOR_BITS);
133
grub_util_error ("the core image is too big");
135
boot_path = grub_util_get_path (dir, "diskboot.img");
136
boot_size = grub_util_get_image_size (boot_path);
137
if (boot_size != GRUB_DISK_SECTOR_SIZE)
138
grub_util_error ("diskboot.img is not one sector size");
140
boot_img = grub_util_read_image (boot_path);
142
/* i386 is a little endian architecture. */
143
*((grub_uint16_t *) (boot_img + GRUB_DISK_SECTOR_SIZE
144
- GRUB_BOOT_MACHINE_LIST_SIZE + 4))
145
= grub_cpu_to_le16 (num);
147
grub_util_write_image (boot_img, boot_size, out);
151
module_addr = (path_list
152
? (GRUB_BOOT_MACHINE_KERNEL_ADDR + GRUB_DISK_SECTOR_SIZE
156
grub_util_info ("the first module address is 0x%x", module_addr);
157
*((grub_uint32_t *) (core_img + GRUB_KERNEL_MACHINE_TOTAL_MODULE_SIZE))
158
= grub_cpu_to_le32 (total_module_size);
159
*((grub_uint32_t *) (core_img + GRUB_KERNEL_MACHINE_KERNEL_IMAGE_SIZE))
160
= grub_cpu_to_le32 (kernel_size);
161
*((grub_uint32_t *) (core_img + GRUB_KERNEL_MACHINE_COMPRESSED_SIZE))
162
= grub_cpu_to_le32 (core_size - GRUB_KERNEL_MACHINE_RAW_SIZE);
164
grub_util_write_image (core_img, core_size, out);
171
next = path_list->next;
172
free ((void *) path_list->name);
180
static struct option options[] =
182
{"directory", required_argument, 0, 'd'},
183
{"output", required_argument, 0, 'o'},
184
{"help", no_argument, 0, 'h'},
185
{"version", no_argument, 0, 'V'},
186
{"verbose", no_argument, 0, 'v'},
194
fprintf (stderr, "Try ``grub-mkimage --help'' for more information.\n");
197
Usage: grub-mkimage [OPTION]... [MODULES]\n\
199
Make a bootable image of GRUB.\n\
201
-d, --directory=DIR use images and modules under DIR [default=%s]\n\
202
-o, --output=FILE output a generated image to FILE [default=stdout]\n\
203
-h, --help display this message and exit\n\
204
-V, --version print version information and exit\n\
205
-v, --verbose print verbose messages\n\
207
Report bugs to <%s>.\n\
208
", GRUB_DATADIR, PACKAGE_BUGREPORT);
214
main (int argc, char *argv[])
220
progname = "grub-mkimage";
224
int c = getopt_long (argc, argv, "d:o:hVv", options, 0);
235
output = xstrdup (optarg);
242
dir = xstrdup (optarg);
250
printf ("grub-mkimage (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
265
fp = fopen (output, "wb");
267
grub_util_error ("cannot open %s", output);
270
generate_image (dir ? : GRUB_DATADIR, fp, argv + optind);