2009-06-22 Robert Millan <rmh.grub@aybabtu.com>
[grub2/phcoder/solaris.git] / util / i386 / pc / grub-mkimage.c
blobf9c74cd16704e2a616241c33d377f581f1d26f11
1 /* grub-mkimage.c - make a bootable image */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009 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 3 of the License, or
9 * (at your option) any later version.
11 * GRUB 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, see <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #include <grub/types.h>
22 #include <grub/machine/boot.h>
23 #include <grub/machine/kernel.h>
24 #include <grub/machine/memory.h>
25 #include <grub/kernel.h>
26 #include <grub/disk.h>
27 #include <grub/util/misc.h>
28 #include <grub/util/resolve.h>
29 #include <grub/misc.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <stdlib.h>
36 #define _GNU_SOURCE 1
37 #include <getopt.h>
39 #if defined(ENABLE_LZO)
41 #if defined(HAVE_LZO_LZO1X_H)
42 # include <lzo/lzo1x.h>
43 #elif defined(HAVE_LZO1X_H)
44 # include <lzo1x.h>
45 #endif
47 #elif defined(ENABLE_LZMA)
49 #include <grub/lib/LzmaEnc.h>
51 #endif
53 #if defined(ENABLE_LZO)
55 static void
56 compress_kernel (char *kernel_img, size_t kernel_size,
57 char **core_img, size_t *core_size)
59 lzo_uint size;
60 char *wrkmem;
62 if (kernel_size < GRUB_KERNEL_MACHINE_RAW_SIZE)
63 grub_util_error ("the core image is too small");
65 if (lzo_init () != LZO_E_OK)
66 grub_util_error ("cannot initialize LZO");
68 *core_img = xmalloc (kernel_size + kernel_size / 64 + 16 + 3);
69 wrkmem = xmalloc (LZO1X_999_MEM_COMPRESS);
71 memcpy (*core_img, kernel_img, GRUB_KERNEL_MACHINE_RAW_SIZE);
73 grub_util_info ("compressing the core image");
74 if (lzo1x_999_compress ((const lzo_byte *) (kernel_img
75 + GRUB_KERNEL_MACHINE_RAW_SIZE),
76 kernel_size - GRUB_KERNEL_MACHINE_RAW_SIZE,
77 (lzo_byte *) (*core_img
78 + GRUB_KERNEL_MACHINE_RAW_SIZE),
79 &size, wrkmem)
80 != LZO_E_OK)
81 grub_util_error ("cannot compress the kernel image");
83 free (wrkmem);
85 *core_size = (size_t) size + GRUB_KERNEL_MACHINE_RAW_SIZE;
88 #elif defined(ENABLE_LZMA)
90 static void *SzAlloc(void *p, size_t size) { p = p; return xmalloc(size); }
91 static void SzFree(void *p, void *address) { p = p; free(address); }
92 static ISzAlloc g_Alloc = { SzAlloc, SzFree };
94 static void
95 compress_kernel (char *kernel_img, size_t kernel_size,
96 char **core_img, size_t *core_size)
98 CLzmaEncProps props;
99 unsigned char out_props[5];
100 size_t out_props_size = 5;
102 LzmaEncProps_Init(&props);
103 props.dictSize = 1 << 16;
104 props.lc = 3;
105 props.lp = 0;
106 props.pb = 2;
107 props.numThreads = 1;
109 if (kernel_size < GRUB_KERNEL_MACHINE_RAW_SIZE)
110 grub_util_error ("the core image is too small");
112 *core_img = xmalloc (kernel_size);
113 memcpy (*core_img, kernel_img, GRUB_KERNEL_MACHINE_RAW_SIZE);
115 *core_size = kernel_size - GRUB_KERNEL_MACHINE_RAW_SIZE;
116 if (LzmaEncode((unsigned char *) *core_img + GRUB_KERNEL_MACHINE_RAW_SIZE,
117 core_size,
118 (unsigned char *) kernel_img + GRUB_KERNEL_MACHINE_RAW_SIZE,
119 kernel_size - GRUB_KERNEL_MACHINE_RAW_SIZE,
120 &props, out_props, &out_props_size,
121 0, NULL, &g_Alloc, &g_Alloc) != SZ_OK)
122 grub_util_error ("cannot compress the kernel image");
124 *core_size += GRUB_KERNEL_MACHINE_RAW_SIZE;
127 #endif
129 static void
130 generate_image (const char *dir, char *prefix, FILE *out, char *mods[],
131 char *memdisk_path, char *config_path)
133 char *kernel_img, *boot_img, *core_img;
134 size_t kernel_size, boot_size, total_module_size, core_size;
135 size_t memdisk_size = 0, config_size = 0;
136 char *kernel_path, *boot_path;
137 unsigned num;
138 size_t offset;
139 struct grub_util_path_list *path_list, *p, *next;
140 struct grub_module_info *modinfo;
142 path_list = grub_util_resolve_dependencies (dir, "moddep.lst", mods);
144 kernel_path = grub_util_get_path (dir, "kernel.img");
145 kernel_size = grub_util_get_image_size (kernel_path);
147 total_module_size = sizeof (struct grub_module_info);
149 if (memdisk_path)
151 memdisk_size = ALIGN_UP(grub_util_get_image_size (memdisk_path), 512);
152 grub_util_info ("the size of memory disk is 0x%x", memdisk_size);
153 total_module_size += memdisk_size + sizeof (struct grub_module_header);
156 if (config_path)
158 config_size = grub_util_get_image_size (config_path) + 1;
159 grub_util_info ("the size of config file is 0x%x", config_size);
160 total_module_size += config_size + sizeof (struct grub_module_header);
163 for (p = path_list; p; p = p->next)
164 total_module_size += (grub_util_get_image_size (p->name)
165 + sizeof (struct grub_module_header));
167 grub_util_info ("the total module size is 0x%x", total_module_size);
169 kernel_img = xmalloc (kernel_size + total_module_size);
170 grub_util_load_image (kernel_path, kernel_img);
172 if (GRUB_KERNEL_MACHINE_PREFIX + strlen (prefix) + 1 > GRUB_KERNEL_MACHINE_DATA_END)
173 grub_util_error ("prefix too long");
174 strcpy (kernel_img + GRUB_KERNEL_MACHINE_PREFIX, prefix);
176 /* Fill in the grub_module_info structure. */
177 modinfo = (struct grub_module_info *) (kernel_img + kernel_size);
178 memset (modinfo, 0, sizeof (struct grub_module_info));
179 modinfo->magic = GRUB_MODULE_MAGIC;
180 modinfo->offset = sizeof (struct grub_module_info);
181 modinfo->size = total_module_size;
183 offset = kernel_size + sizeof (struct grub_module_info);
184 for (p = path_list; p; p = p->next)
186 struct grub_module_header *header;
187 size_t mod_size;
189 mod_size = grub_util_get_image_size (p->name);
191 header = (struct grub_module_header *) (kernel_img + offset);
192 memset (header, 0, sizeof (struct grub_module_header));
193 header->type = grub_cpu_to_le32 (OBJ_TYPE_ELF);
194 header->size = grub_cpu_to_le32 (mod_size + sizeof (*header));
195 offset += sizeof (*header);
197 grub_util_load_image (p->name, kernel_img + offset);
198 offset += mod_size;
201 if (memdisk_path)
203 struct grub_module_header *header;
205 header = (struct grub_module_header *) (kernel_img + offset);
206 memset (header, 0, sizeof (struct grub_module_header));
207 header->type = grub_cpu_to_le32 (OBJ_TYPE_MEMDISK);
208 header->size = grub_cpu_to_le32 (memdisk_size + sizeof (*header));
209 offset += sizeof (*header);
211 grub_util_load_image (memdisk_path, kernel_img + offset);
212 offset += memdisk_size;
215 if (config_path)
217 struct grub_module_header *header;
219 header = (struct grub_module_header *) (kernel_img + offset);
220 memset (header, 0, sizeof (struct grub_module_header));
221 header->type = grub_cpu_to_le32 (OBJ_TYPE_CONFIG);
222 header->size = grub_cpu_to_le32 (config_size + sizeof (*header));
223 offset += sizeof (*header);
225 grub_util_load_image (config_path, kernel_img + offset);
226 offset += config_size;
227 *(kernel_img + offset - 1) = 0;
230 grub_util_info ("kernel_img=%p, kernel_size=0x%x", kernel_img, kernel_size);
231 compress_kernel (kernel_img, kernel_size + total_module_size,
232 &core_img, &core_size);
234 grub_util_info ("the core size is 0x%x", core_size);
236 num = ((core_size + GRUB_DISK_SECTOR_SIZE - 1) >> GRUB_DISK_SECTOR_BITS);
237 if (num > 0xffff)
238 grub_util_error ("the core image is too big");
240 boot_path = grub_util_get_path (dir, "diskboot.img");
241 boot_size = grub_util_get_image_size (boot_path);
242 if (boot_size != GRUB_DISK_SECTOR_SIZE)
243 grub_util_error ("diskboot.img is not one sector size");
245 boot_img = grub_util_read_image (boot_path);
247 /* i386 is a little endian architecture. */
248 *((grub_uint16_t *) (boot_img + GRUB_DISK_SECTOR_SIZE
249 - GRUB_BOOT_MACHINE_LIST_SIZE + 8))
250 = grub_cpu_to_le16 (num);
252 grub_util_write_image (boot_img, boot_size, out);
253 free (boot_img);
254 free (boot_path);
256 *((grub_uint32_t *) (core_img + GRUB_KERNEL_MACHINE_TOTAL_MODULE_SIZE))
257 = grub_cpu_to_le32 (total_module_size);
258 *((grub_uint32_t *) (core_img + GRUB_KERNEL_MACHINE_KERNEL_IMAGE_SIZE))
259 = grub_cpu_to_le32 (kernel_size);
260 *((grub_uint32_t *) (core_img + GRUB_KERNEL_MACHINE_COMPRESSED_SIZE))
261 = grub_cpu_to_le32 (core_size - GRUB_KERNEL_MACHINE_RAW_SIZE);
263 /* If we included a drive in our prefix, let GRUB know it doesn't have to
264 prepend the drive told by BIOS. */
265 if (prefix[0] == '(')
267 *((grub_int32_t *) (core_img + GRUB_KERNEL_MACHINE_INSTALL_DOS_PART))
268 = grub_cpu_to_le32 (-2);
269 *((grub_int32_t *) (core_img + GRUB_KERNEL_MACHINE_INSTALL_BSD_PART))
270 = grub_cpu_to_le32 (-2);
273 if (GRUB_KERNEL_MACHINE_LINK_ADDR + core_size > GRUB_MEMORY_MACHINE_UPPER)
274 grub_util_error ("Core image is too big (%p > %p)\n",
275 GRUB_KERNEL_MACHINE_LINK_ADDR + core_size, GRUB_MEMORY_MACHINE_UPPER);
277 grub_util_write_image (core_img, core_size, out);
278 free (kernel_img);
279 free (core_img);
280 free (kernel_path);
282 while (path_list)
284 next = path_list->next;
285 free ((void *) path_list->name);
286 free (path_list);
287 path_list = next;
293 static struct option options[] =
295 {"directory", required_argument, 0, 'd'},
296 {"prefix", required_argument, 0, 'p'},
297 {"memdisk", required_argument, 0, 'm'},
298 {"config", required_argument, 0, 'c'},
299 {"output", required_argument, 0, 'o'},
300 {"help", no_argument, 0, 'h'},
301 {"version", no_argument, 0, 'V'},
302 {"verbose", no_argument, 0, 'v'},
303 {0, 0, 0, 0}
306 static void
307 usage (int status)
309 if (status)
310 fprintf (stderr, "Try ``grub-mkimage --help'' for more information.\n");
311 else
312 printf ("\
313 Usage: grub-mkimage [OPTION]... [MODULES]\n\
315 Make a bootable image of GRUB.\n\
317 -d, --directory=DIR use images and modules under DIR [default=%s]\n\
318 -p, --prefix=DIR set grub_prefix directory [default=%s]\n\
319 -m, --memdisk=FILE embed FILE as a memdisk image\n\
320 -c, --config=FILE embed FILE as boot config\n\
321 -o, --output=FILE output a generated image to FILE [default=stdout]\n\
322 -h, --help display this message and exit\n\
323 -V, --version print version information and exit\n\
324 -v, --verbose print verbose messages\n\
326 Report bugs to <%s>.\n\
327 ", GRUB_LIBDIR, DEFAULT_DIRECTORY, PACKAGE_BUGREPORT);
329 exit (status);
333 main (int argc, char *argv[])
335 char *output = NULL;
336 char *dir = NULL;
337 char *prefix = NULL;
338 char *memdisk = NULL;
339 char *config = NULL;
340 FILE *fp = stdout;
342 progname = "grub-mkimage";
344 while (1)
346 int c = getopt_long (argc, argv, "d:p:m:c:o:hVv", options, 0);
348 if (c == -1)
349 break;
350 else
351 switch (c)
353 case 'o':
354 if (output)
355 free (output);
357 output = xstrdup (optarg);
358 break;
360 case 'd':
361 if (dir)
362 free (dir);
364 dir = xstrdup (optarg);
365 break;
367 case 'm':
368 if (memdisk)
369 free (memdisk);
371 memdisk = xstrdup (optarg);
373 if (prefix)
374 free (prefix);
376 prefix = xstrdup ("(memdisk)/boot/grub");
377 break;
379 case 'c':
380 if (config)
381 free (config);
383 config = xstrdup (optarg);
384 break;
386 case 'h':
387 usage (0);
388 break;
390 case 'p':
391 if (prefix)
392 free (prefix);
394 prefix = xstrdup (optarg);
395 break;
397 case 'V':
398 printf ("grub-mkimage (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
399 return 0;
401 case 'v':
402 verbosity++;
403 break;
405 default:
406 usage (1);
407 break;
411 if (output)
413 fp = fopen (output, "wb");
414 if (! fp)
415 grub_util_error ("cannot open %s", output);
416 free (output);
419 generate_image (dir ? : GRUB_LIBDIR, prefix ? : DEFAULT_DIRECTORY, fp,
420 argv + optind, memdisk, config);
422 fclose (fp);
424 if (dir)
425 free (dir);
427 return 0;