1
1
/* grub-mkimage.c - make a bootable image */
3
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2002,2003,2004,2005,2006,2007,2008 Free Software Foundation, Inc.
4
* Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009 Free Software Foundation, Inc.
6
6
* GRUB is free software: you can redistribute it and/or modify
7
7
* it under the terms of the GNU General Public License as published by
36
36
#define _GNU_SOURCE 1
37
37
#include <getopt.h>
39
#if defined(ENABLE_LZO)
39
41
#if defined(HAVE_LZO_LZO1X_H)
40
42
# include <lzo/lzo1x.h>
41
43
#elif defined(HAVE_LZO1X_H)
42
44
# include <lzo1x.h>
47
#elif defined(ENABLE_LZMA)
49
#include <grub/lib/LzmaEnc.h>
53
#if defined(ENABLE_LZO)
46
56
compress_kernel (char *kernel_img, size_t kernel_size,
47
57
char **core_img, size_t *core_size)
52
grub_util_info ("kernel_img=%p, kernel_size=0x%x", kernel_img, kernel_size);
53
62
if (kernel_size < GRUB_KERNEL_MACHINE_RAW_SIZE)
54
63
grub_util_error ("the core image is too small");
56
65
if (lzo_init () != LZO_E_OK)
57
66
grub_util_error ("cannot initialize LZO");
76
85
*core_size = (size_t) size + GRUB_KERNEL_MACHINE_RAW_SIZE;
80
generate_image (const char *dir, char *prefix, FILE *out, char *mods[], char *memdisk_path)
82
grub_addr_t module_addr = 0;
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 };
95
compress_kernel (char *kernel_img, size_t kernel_size,
96
char **core_img, size_t *core_size)
99
unsigned char out_props[5];
100
size_t out_props_size = 5;
102
LzmaEncProps_Init(&props);
103
props.dictSize = 1 << 16;
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,
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;
130
compress_kernel (char *kernel_img, size_t kernel_size,
131
char **core_img, size_t *core_size)
133
*core_img = xmalloc (kernel_size);
134
memcpy (*core_img, kernel_img, kernel_size);
135
*core_size = kernel_size;
141
generate_image (const char *dir, char *prefix, FILE *out, char *mods[],
142
char *memdisk_path, char *config_path)
83
144
char *kernel_img, *boot_img, *core_img;
84
size_t kernel_size, boot_size, total_module_size, core_size, memdisk_size = 0;
145
size_t kernel_size, boot_size, total_module_size, core_size;
146
size_t memdisk_size = 0, config_size = 0;
85
147
char *kernel_path, *boot_path;
94
156
kernel_size = grub_util_get_image_size (kernel_path);
96
158
total_module_size = sizeof (struct grub_module_info);
162
memdisk_size = ALIGN_UP(grub_util_get_image_size (memdisk_path), 512);
163
grub_util_info ("the size of memory disk is 0x%x", memdisk_size);
164
total_module_size += memdisk_size + sizeof (struct grub_module_header);
169
config_size = grub_util_get_image_size (config_path) + 1;
170
grub_util_info ("the size of config file is 0x%x", config_size);
171
total_module_size += config_size + sizeof (struct grub_module_header);
97
174
for (p = path_list; p; p = p->next)
98
175
total_module_size += (grub_util_get_image_size (p->name)
99
176
+ sizeof (struct grub_module_header));
101
178
grub_util_info ("the total module size is 0x%x", total_module_size);
105
memdisk_size = ALIGN_UP(grub_util_get_image_size (memdisk_path), 512);
106
grub_util_info ("the size of memory disk is 0x%x", memdisk_size);
109
kernel_img = xmalloc (kernel_size + total_module_size + memdisk_size);
180
kernel_img = xmalloc (kernel_size + total_module_size);
110
181
grub_util_load_image (kernel_path, kernel_img);
112
183
if (GRUB_KERNEL_MACHINE_PREFIX + strlen (prefix) + 1 > GRUB_KERNEL_MACHINE_DATA_END)
128
200
mod_size = grub_util_get_image_size (p->name);
130
202
header = (struct grub_module_header *) (kernel_img + offset);
131
header->offset = grub_cpu_to_le32 (sizeof (*header));
203
memset (header, 0, sizeof (struct grub_module_header));
204
header->type = grub_cpu_to_le32 (OBJ_TYPE_ELF);
132
205
header->size = grub_cpu_to_le32 (mod_size + sizeof (*header));
133
206
offset += sizeof (*header);
139
212
if (memdisk_path)
214
struct grub_module_header *header;
216
header = (struct grub_module_header *) (kernel_img + offset);
217
memset (header, 0, sizeof (struct grub_module_header));
218
header->type = grub_cpu_to_le32 (OBJ_TYPE_MEMDISK);
219
header->size = grub_cpu_to_le32 (memdisk_size + sizeof (*header));
220
offset += sizeof (*header);
141
222
grub_util_load_image (memdisk_path, kernel_img + offset);
142
223
offset += memdisk_size;
145
compress_kernel (kernel_img, kernel_size + total_module_size + memdisk_size,
228
struct grub_module_header *header;
230
header = (struct grub_module_header *) (kernel_img + offset);
231
memset (header, 0, sizeof (struct grub_module_header));
232
header->type = grub_cpu_to_le32 (OBJ_TYPE_CONFIG);
233
header->size = grub_cpu_to_le32 (config_size + sizeof (*header));
234
offset += sizeof (*header);
236
grub_util_load_image (config_path, kernel_img + offset);
237
offset += config_size;
238
*(kernel_img + offset - 1) = 0;
241
grub_util_info ("kernel_img=%p, kernel_size=0x%x", kernel_img, kernel_size);
242
compress_kernel (kernel_img, kernel_size + total_module_size,
146
243
&core_img, &core_size);
148
245
grub_util_info ("the core size is 0x%x", core_size);
150
247
num = ((core_size + GRUB_DISK_SECTOR_SIZE - 1) >> GRUB_DISK_SECTOR_BITS);
151
248
if (num > 0xffff)
152
249
grub_util_error ("the core image is too big");
251
#if defined(GRUB_MACHINE_PCBIOS)
154
253
boot_path = grub_util_get_path (dir, "diskboot.img");
155
254
boot_size = grub_util_get_image_size (boot_path);
156
255
if (boot_size != GRUB_DISK_SECTOR_SIZE)
157
256
grub_util_error ("diskboot.img is not one sector size");
159
258
boot_img = grub_util_read_image (boot_path);
161
260
/* i386 is a little endian architecture. */
162
261
*((grub_uint16_t *) (boot_img + GRUB_DISK_SECTOR_SIZE
163
262
- GRUB_BOOT_MACHINE_LIST_SIZE + 8))
166
265
grub_util_write_image (boot_img, boot_size, out);
168
267
free (boot_path);
170
module_addr = (path_list
171
? (GRUB_BOOT_MACHINE_KERNEL_ADDR + GRUB_DISK_SECTOR_SIZE
175
grub_util_info ("the first module address is 0x%x", module_addr);
269
#elif defined(GRUB_MACHINE_QEMU)
275
boot_path = grub_util_get_path (dir, "boot.img");
276
boot_size = grub_util_get_image_size (boot_path);
277
boot_img = grub_util_read_image (boot_path);
279
/* Rom sizes must be 64k-aligned. */
280
rom_size = ALIGN_UP (core_size + boot_size, 64 * 1024);
282
rom_img = xmalloc (rom_size);
283
memset (rom_img, 0, rom_size);
285
*((grub_int32_t *) (core_img + GRUB_KERNEL_MACHINE_CORE_ENTRY_ADDR))
286
= grub_cpu_to_le32 ((grub_uint32_t) -rom_size);
288
memcpy (rom_img, core_img, core_size);
290
*((grub_int32_t *) (boot_img + GRUB_BOOT_MACHINE_CORE_ENTRY_ADDR))
291
= grub_cpu_to_le32 ((grub_uint32_t) -rom_size);
293
memcpy (rom_img + rom_size - boot_size, boot_img, boot_size);
297
core_size = rom_size;
305
#ifdef GRUB_KERNEL_MACHINE_TOTAL_MODULE_SIZE
176
306
*((grub_uint32_t *) (core_img + GRUB_KERNEL_MACHINE_TOTAL_MODULE_SIZE))
177
307
= grub_cpu_to_le32 (total_module_size);
178
309
*((grub_uint32_t *) (core_img + GRUB_KERNEL_MACHINE_KERNEL_IMAGE_SIZE))
179
310
= grub_cpu_to_le32 (kernel_size);
180
*((grub_uint32_t *) (core_img + GRUB_KERNEL_MACHINE_MEMDISK_IMAGE_SIZE))
181
= grub_cpu_to_le32 (memdisk_size);
311
#ifdef GRUB_KERNEL_MACHINE_COMPRESSED_SIZE
182
312
*((grub_uint32_t *) (core_img + GRUB_KERNEL_MACHINE_COMPRESSED_SIZE))
183
313
= grub_cpu_to_le32 (core_size - GRUB_KERNEL_MACHINE_RAW_SIZE);
185
if (core_size > GRUB_MEMORY_MACHINE_UPPER - GRUB_MEMORY_MACHINE_LINK_ADDR)
186
grub_util_error ("Core image is too big (%p > %p)\n", core_size,
187
GRUB_MEMORY_MACHINE_UPPER - GRUB_MEMORY_MACHINE_LINK_ADDR);
316
#if defined(GRUB_KERNEL_MACHINE_INSTALL_DOS_PART) && defined(GRUB_KERNEL_MACHINE_INSTALL_BSD_PART)
317
/* If we included a drive in our prefix, let GRUB know it doesn't have to
318
prepend the drive told by BIOS. */
319
if (prefix[0] == '(')
321
*((grub_int32_t *) (core_img + GRUB_KERNEL_MACHINE_INSTALL_DOS_PART))
322
= grub_cpu_to_le32 (-2);
323
*((grub_int32_t *) (core_img + GRUB_KERNEL_MACHINE_INSTALL_BSD_PART))
324
= grub_cpu_to_le32 (-2);
328
#ifdef GRUB_MACHINE_PCBIOS
329
if (GRUB_KERNEL_MACHINE_LINK_ADDR + core_size > GRUB_MEMORY_MACHINE_UPPER)
330
grub_util_error ("Core image is too big (%p > %p)\n",
331
GRUB_KERNEL_MACHINE_LINK_ADDR + core_size, GRUB_MEMORY_MACHINE_UPPER);
189
334
grub_util_write_image (core_img, core_size, out);
190
335
free (kernel_img);
207
352
{"directory", required_argument, 0, 'd'},
208
353
{"prefix", required_argument, 0, 'p'},
209
354
{"memdisk", required_argument, 0, 'm'},
355
{"config", required_argument, 0, 'c'},
210
356
{"output", required_argument, 0, 'o'},
211
357
{"help", no_argument, 0, 'h'},
212
358
{"version", no_argument, 0, 'V'},
228
374
-d, --directory=DIR use images and modules under DIR [default=%s]\n\
229
375
-p, --prefix=DIR set grub_prefix directory [default=%s]\n\
230
376
-m, --memdisk=FILE embed FILE as a memdisk image\n\
377
-c, --config=FILE embed FILE as boot config\n\
231
378
-o, --output=FILE output a generated image to FILE [default=stdout]\n\
232
379
-h, --help display this message and exit\n\
233
380
-V, --version print version information and exit\n\
310
470
fp = fopen (output, "wb");
312
472
grub_util_error ("cannot open %s", output);
315
generate_image (dir ? : GRUB_LIBDIR, prefix ? : DEFAULT_DIRECTORY, fp, argv + optind, memdisk);
476
generate_image (dir ? : GRUB_LIBDIR, prefix ? : DEFAULT_DIRECTORY, fp,
477
argv + optind, memdisk, config);