1
/* grub-mkimage.c - make a bootable image */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 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 <grub/types.h>
21
#include <grub/machine/boot.h>
22
#include <grub/machine/kernel.h>
23
#include <grub/kernel.h>
24
#include <grub/disk.h>
25
#include <grub/util/misc.h>
26
#include <grub/util/resolve.h>
27
#include <grub/misc.h>
38
compress_kernel (char *kernel_img, size_t kernel_size,
39
char **core_img, size_t *core_size)
41
/* No compression support yet. */
42
grub_util_info ("kernel_img=%p, kernel_size=0x%x", kernel_img, kernel_size);
43
*core_img = xmalloc (kernel_size);
44
memcpy (*core_img, kernel_img, kernel_size);
45
*core_size = kernel_size;
49
generate_image (const char *dir, const char *prefix, FILE *out, char *mods[], char *memdisk_path)
51
size_t kernel_size, total_module_size, memdisk_size, core_size, boot_size, offset;
52
char *kernel_path, *kernel_img, *core_img, *boot_path, *boot_img;
53
struct grub_util_path_list *path_list, *p;
54
struct grub_module_info *modinfo;
55
grub_addr_t module_addr;
58
path_list = grub_util_resolve_dependencies (dir, "moddep.lst", mods);
60
kernel_path = grub_util_get_path (dir, "kernel.img");
61
kernel_size = grub_util_get_image_size (kernel_path);
63
total_module_size = sizeof (struct grub_module_info);
64
for (p = path_list; p; p = p->next)
65
total_module_size += (grub_util_get_image_size (p->name)
66
+ sizeof (struct grub_module_header));
71
memdisk_size = ALIGN_UP(grub_util_get_image_size (memdisk_path), 512);
72
grub_util_info ("the size of memory disk is 0x%x", memdisk_size);
73
total_module_size += memdisk_size + sizeof (struct grub_module_header);
76
grub_util_info ("the total module size is 0x%x", total_module_size);
78
kernel_img = xmalloc (kernel_size + total_module_size);
79
grub_util_load_image (kernel_path, kernel_img);
81
if ((GRUB_KERNEL_MACHINE_PREFIX + strlen (prefix) + 1)
82
> GRUB_KERNEL_MACHINE_DATA_END)
83
grub_util_error ("prefix too long");
84
strcpy (kernel_img + GRUB_KERNEL_MACHINE_PREFIX, prefix);
86
/* Fill in the grub_module_info structure. */
87
modinfo = (struct grub_module_info *) (kernel_img + kernel_size);
88
modinfo->magic = GRUB_MODULE_MAGIC;
89
modinfo->offset = sizeof (struct grub_module_info);
90
modinfo->size = total_module_size;
92
offset = kernel_size + sizeof (struct grub_module_info);
93
for (p = path_list; p; p = p->next)
95
struct grub_module_header *header;
98
mod_size = grub_util_get_image_size (p->name);
100
header = (struct grub_module_header *) (kernel_img + offset);
101
header->type = grub_cpu_to_be32 (OBJ_TYPE_ELF);
102
header->size = grub_cpu_to_be32 (mod_size + sizeof (*header));
103
offset += sizeof (*header);
105
grub_util_load_image (p->name, kernel_img + offset);
111
struct grub_module_header *header;
113
header = (struct grub_module_header *) (kernel_img + offset);
114
header->type = grub_cpu_to_be32 (OBJ_TYPE_MEMDISK);
115
header->size = grub_cpu_to_be32 (memdisk_size + sizeof (*header));
116
offset += sizeof (*header);
118
grub_util_load_image (memdisk_path, kernel_img + offset);
119
offset += memdisk_size;
122
compress_kernel (kernel_img, kernel_size + total_module_size,
123
&core_img, &core_size);
125
grub_util_info ("the core size is 0x%x", core_size);
127
num = ((core_size + GRUB_DISK_SECTOR_SIZE - 1) >> GRUB_DISK_SECTOR_BITS);
128
num <<= GRUB_DISK_SECTOR_BITS;
130
boot_path = grub_util_get_path (dir, "diskboot.img");
131
boot_size = grub_util_get_image_size (boot_path);
132
if (boot_size != GRUB_DISK_SECTOR_SIZE)
133
grub_util_error ("diskboot.img is not one sector size");
135
boot_img = grub_util_read_image (boot_path);
137
/* sparc is a big endian architecture. */
138
*((grub_uint32_t *) (boot_img + GRUB_DISK_SECTOR_SIZE
139
- GRUB_BOOT_MACHINE_LIST_SIZE + 8))
140
= grub_cpu_to_be32 (num);
142
grub_util_write_image (boot_img, boot_size, out);
146
module_addr = (path_list
147
? (GRUB_BOOT_MACHINE_IMAGE_ADDRESS + kernel_size)
150
grub_util_info ("the first module address is 0x%x", module_addr);
152
*((grub_uint32_t *) (core_img + GRUB_KERNEL_MACHINE_TOTAL_MODULE_SIZE))
153
= grub_cpu_to_be32 (total_module_size);
154
*((grub_uint32_t *) (core_img + GRUB_KERNEL_MACHINE_KERNEL_IMAGE_SIZE))
155
= grub_cpu_to_be32 (kernel_size);
157
/* No compression support yet. */
158
*((grub_uint32_t *) (core_img + GRUB_KERNEL_MACHINE_COMPRESSED_SIZE))
159
= grub_cpu_to_be32 (0);
161
grub_util_write_image (core_img, core_size, out);
168
struct grub_util_path_list *next = path_list->next;
169
free ((void *) path_list->name);
175
static struct option options[] =
177
{"directory", required_argument, 0, 'd'},
178
{"prefix", required_argument, 0, 'p'},
179
{"memdisk", required_argument, 0, 'm'},
180
{"output", required_argument, 0, 'o'},
181
{"help", no_argument, 0, 'h'},
182
{"version", no_argument, 0, 'V'},
183
{"verbose", no_argument, 0, 'v'},
191
fprintf (stderr, "Try ``grub-mkimage --help'' for more information.\n");
194
Usage: grub-mkimage [OPTION]... [MODULES]\n\
196
Make a bootable image of GRUB.\n\
198
-d, --directory=DIR use images and modules under DIR [default=%s]\n\
199
-p, --prefix=DIR set grub_prefix directory [default=%s]\n\
200
-m, --memdisk=FILE embed FILE as a memdisk image\n\
201
-o, --output=FILE output a generated image to FILE [default=stdout]\n\
202
-h, --help display this message and exit\n\
203
-V, --version print version information and exit\n\
204
-v, --verbose print verbose messages\n\
206
Report bugs to <%s>.\n\
207
", GRUB_LIBDIR, DEFAULT_DIRECTORY, PACKAGE_BUGREPORT);
213
main (int argc, char *argv[])
218
char *memdisk = NULL;
221
progname = "grub-mkimage";
224
int c = getopt_long (argc, argv, "d:p:m:o:hVv", options, 0);
234
output = xstrdup (optarg);
240
dir = xstrdup (optarg);
246
memdisk = xstrdup (optarg);
250
prefix = xstrdup ("(memdisk)/boot/grub");
260
prefix = xstrdup (optarg);
264
printf ("grub-mkimage (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
279
fp = fopen (output, "wb");
281
grub_util_error ("cannot open %s", output);
284
generate_image (dir ? : GRUB_LIBDIR,
285
prefix ? : DEFAULT_DIRECTORY, fp,
286
argv + optind, memdisk);