1
/* grub-mkimage.c - make a bootable image */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2008,2009,2010 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 <grub/types.h>
21
#include <grub/machine/boot.h>
22
#include <grub/machine/kernel.h>
23
#include <grub/kernel.h>
24
#include <grub/i18n.h>
25
#include <grub/disk.h>
26
#include <grub/util/misc.h>
27
#include <grub/util/resolve.h>
28
#include <grub/misc.h>
41
compress_kernel (char *kernel_img, size_t kernel_size,
42
char **core_img, size_t *core_size)
44
/* No compression support yet. */
45
grub_util_info ("kernel_img=%p, kernel_size=0x%x", kernel_img, kernel_size);
46
*core_img = xmalloc (kernel_size);
47
memcpy (*core_img, kernel_img, kernel_size);
48
*core_size = kernel_size;
52
generate_image (const char *dir, const char *prefix, FILE *out, char *mods[], char *memdisk_path)
54
size_t kernel_size, total_module_size, memdisk_size, core_size, boot_size, offset;
55
char *kernel_path, *kernel_img, *core_img, *boot_path, *boot_img;
56
struct grub_util_path_list *path_list, *p;
57
struct grub_module_info *modinfo;
58
grub_addr_t module_addr;
61
path_list = grub_util_resolve_dependencies (dir, "moddep.lst", mods);
63
kernel_path = grub_util_get_path (dir, "kernel.img");
64
kernel_size = grub_util_get_image_size (kernel_path);
66
total_module_size = sizeof (struct grub_module_info);
67
for (p = path_list; p; p = p->next)
68
total_module_size += (grub_util_get_image_size (p->name)
69
+ sizeof (struct grub_module_header));
74
memdisk_size = ALIGN_UP(grub_util_get_image_size (memdisk_path), 512);
75
grub_util_info ("the size of memory disk is 0x%x", memdisk_size);
76
total_module_size += memdisk_size + sizeof (struct grub_module_header);
79
grub_util_info ("the total module size is 0x%x", total_module_size);
81
kernel_img = xmalloc (kernel_size + total_module_size);
82
grub_util_load_image (kernel_path, kernel_img);
84
if ((GRUB_KERNEL_MACHINE_PREFIX + strlen (prefix) + 1)
85
> GRUB_KERNEL_MACHINE_DATA_END)
86
grub_util_error ("prefix too long");
87
strcpy (kernel_img + GRUB_KERNEL_MACHINE_PREFIX, prefix);
89
/* Fill in the grub_module_info structure. */
90
modinfo = (struct grub_module_info *) (kernel_img + kernel_size);
91
modinfo->magic = GRUB_MODULE_MAGIC;
92
modinfo->offset = sizeof (struct grub_module_info);
93
modinfo->size = total_module_size;
95
offset = kernel_size + sizeof (struct grub_module_info);
96
for (p = path_list; p; p = p->next)
98
struct grub_module_header *header;
101
mod_size = grub_util_get_image_size (p->name);
103
header = (struct grub_module_header *) (kernel_img + offset);
104
header->type = OBJ_TYPE_ELF;
105
header->size = grub_host_to_target32 (mod_size + sizeof (*header));
106
offset += sizeof (*header);
108
grub_util_load_image (p->name, kernel_img + offset);
114
struct grub_module_header *header;
116
header = (struct grub_module_header *) (kernel_img + offset);
117
header->type = OBJ_TYPE_MEMDISK;
118
header->size = grub_host_to_target32 (memdisk_size + sizeof (*header));
119
offset += sizeof (*header);
121
grub_util_load_image (memdisk_path, kernel_img + offset);
122
offset += memdisk_size;
125
compress_kernel (kernel_img, kernel_size + total_module_size,
126
&core_img, &core_size);
128
grub_util_info ("the core size is 0x%x", core_size);
130
num = ((core_size + GRUB_DISK_SECTOR_SIZE - 1) >> GRUB_DISK_SECTOR_BITS);
131
num <<= GRUB_DISK_SECTOR_BITS;
133
boot_path = grub_util_get_path (dir, "diskboot.img");
134
boot_size = grub_util_get_image_size (boot_path);
135
if (boot_size != GRUB_DISK_SECTOR_SIZE)
136
grub_util_error ("diskboot.img is not one sector size");
138
boot_img = grub_util_read_image (boot_path);
140
/* sparc is a big endian architecture. */
141
*((grub_uint32_t *) (boot_img + GRUB_DISK_SECTOR_SIZE
142
- GRUB_BOOT_MACHINE_LIST_SIZE + 8))
143
= grub_cpu_to_be32 (num);
145
grub_util_write_image (boot_img, boot_size, out);
149
module_addr = (path_list
150
? (GRUB_BOOT_MACHINE_IMAGE_ADDRESS + kernel_size)
153
grub_util_info ("the first module address is 0x%x", module_addr);
155
*((grub_uint32_t *) (core_img + GRUB_KERNEL_MACHINE_TOTAL_MODULE_SIZE))
156
= grub_cpu_to_be32 (total_module_size);
157
*((grub_uint32_t *) (core_img + GRUB_KERNEL_MACHINE_KERNEL_IMAGE_SIZE))
158
= grub_cpu_to_be32 (kernel_size);
160
/* No compression support yet. */
161
*((grub_uint32_t *) (core_img + GRUB_KERNEL_MACHINE_COMPRESSED_SIZE))
162
= grub_cpu_to_be32 (0);
164
grub_util_write_image (core_img, core_size, out);
171
struct grub_util_path_list *next = path_list->next;
172
free ((void *) path_list->name);
178
static struct option options[] =
180
{"directory", required_argument, 0, 'd'},
181
{"prefix", required_argument, 0, 'p'},
182
{"memdisk", required_argument, 0, 'm'},
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 `%s --help' for more information.\n", program_name);
197
Usage: %s [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
-p, --prefix=DIR set grub_prefix directory [default=%s]\n\
203
-m, --memdisk=FILE embed FILE as a memdisk image\n\
204
-o, --output=FILE output a generated image to FILE [default=stdout]\n\
205
-h, --help display this message and exit\n\
206
-V, --version print version information and exit\n\
207
-v, --verbose print verbose messages\n\
209
Report bugs to <%s>.\n\
210
", program_name, GRUB_LIBDIR, DEFAULT_DIRECTORY, PACKAGE_BUGREPORT);
216
main (int argc, char *argv[])
221
char *memdisk = NULL;
224
set_program_name (argv[0]);
226
grub_util_init_nls ();
230
int c = getopt_long (argc, argv, "d:p:m:o:hVv", options, 0);
240
output = xstrdup (optarg);
246
dir = xstrdup (optarg);
252
memdisk = xstrdup (optarg);
256
prefix = xstrdup ("(memdisk)/boot/grub");
266
prefix = xstrdup (optarg);
270
printf ("grub-mkimage (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
285
fp = fopen (output, "wb");
287
grub_util_error ("cannot open %s", output);
290
generate_image (dir ? : GRUB_LIBDIR,
291
prefix ? : DEFAULT_DIRECTORY, fp,
292
argv + optind, memdisk);