2
* GRUB -- GRand Unified Bootloader
3
* Copyright (C) 2004, 2005 Free Software Foundation, Inc.
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28
#include <grub/util/misc.h>
29
#include <grub/util/resolve.h>
30
#include <grub/kernel.h>
31
#include <grub/machine/kernel.h>
33
#define ALIGN_UP(addr, align) ((long)((char *)addr + align - 1) & ~(align - 1))
35
#define GRUB_IEEE1275_NOTE_NAME "PowerPC"
36
#define GRUB_IEEE1275_NOTE_TYPE 0x1275
38
/* These structures are defined according to the CHRP binding to IEEE1275,
39
"Client Program Format" section. */
41
struct grub_ieee1275_note_hdr
46
char name[sizeof (GRUB_IEEE1275_NOTE_NAME)];
49
struct grub_ieee1275_note_desc
51
grub_uint32_t real_mode;
52
grub_uint32_t real_base;
53
grub_uint32_t real_size;
54
grub_uint32_t virt_base;
55
grub_uint32_t virt_size;
56
grub_uint32_t load_base;
59
struct grub_ieee1275_note
61
struct grub_ieee1275_note_hdr header;
62
struct grub_ieee1275_note_desc descriptor;
66
load_note (Elf32_Phdr *phdr, FILE *out)
68
struct grub_ieee1275_note note;
69
int note_size = sizeof (struct grub_ieee1275_note);
71
grub_util_info ("adding CHRP NOTE segment");
73
note.header.namesz = grub_cpu_to_be32 (sizeof (GRUB_IEEE1275_NOTE_NAME));
74
note.header.descsz = grub_cpu_to_be32 (note_size);
75
note.header.type = grub_cpu_to_be32 (GRUB_IEEE1275_NOTE_TYPE);
76
strcpy (note.header.name, GRUB_IEEE1275_NOTE_NAME);
77
note.descriptor.real_mode = grub_cpu_to_be32 (0xffffffff);
78
note.descriptor.real_base = grub_cpu_to_be32 (0x00c00000);
79
note.descriptor.real_size = grub_cpu_to_be32 (0xffffffff);
80
note.descriptor.virt_base = grub_cpu_to_be32 (0xffffffff);
81
note.descriptor.virt_size = grub_cpu_to_be32 (0xffffffff);
82
note.descriptor.load_base = grub_cpu_to_be32 (0x00004000);
84
/* Write the note data to the new segment. */
85
grub_util_write_image_at (¬e, note_size,
86
grub_be_to_cpu32 (phdr->p_offset), out);
88
/* Fill in the rest of the segment header. */
89
phdr->p_type = grub_cpu_to_be32 (PT_NOTE);
90
phdr->p_flags = grub_cpu_to_be32 (PF_R);
91
phdr->p_align = grub_cpu_to_be32 (sizeof (long));
94
phdr->p_filesz = grub_cpu_to_be32 (note_size);
99
load_modules (Elf32_Phdr *phdr, const char *dir, char *mods[], FILE *out)
102
struct grub_util_path_list *path_list;
103
struct grub_util_path_list *p;
104
struct grub_module_info *modinfo;
106
size_t total_module_size;
108
path_list = grub_util_resolve_dependencies (dir, "moddep.lst", mods);
110
offset = sizeof (struct grub_module_info);
111
total_module_size = sizeof (struct grub_module_info);
112
for (p = path_list; p; p = p->next)
114
total_module_size += (grub_util_get_image_size (p->name)
115
+ sizeof (struct grub_module_header));
118
grub_util_info ("the total module size is 0x%x", total_module_size);
120
module_img = xmalloc (total_module_size);
121
modinfo = (struct grub_module_info *) module_img;
122
modinfo->magic = grub_cpu_to_be32 (GRUB_MODULE_MAGIC);
123
modinfo->offset = grub_cpu_to_be32 (sizeof (struct grub_module_info));
124
modinfo->size = grub_cpu_to_be32 (total_module_size);
126
/* Load all the modules, with headers, into module_img. */
127
for (p = path_list; p; p = p->next)
129
struct grub_module_header *header;
132
grub_util_info ("adding module %s", p->name);
134
mod_size = grub_util_get_image_size (p->name);
136
header = (struct grub_module_header *) (module_img + offset);
137
header->offset = grub_cpu_to_be32 (sizeof (*header));
138
header->size = grub_cpu_to_be32 (mod_size + sizeof (*header));
140
grub_util_load_image (p->name, module_img + offset + sizeof (*header));
142
offset += sizeof (*header) + mod_size;
145
/* Write the module data to the new segment. */
146
grub_util_write_image_at (module_img, total_module_size,
147
grub_cpu_to_be32 (phdr->p_offset), out);
149
/* Fill in the rest of the segment header. */
150
phdr->p_type = grub_cpu_to_be32 (PT_LOAD);
151
phdr->p_flags = grub_cpu_to_be32 (PF_R | PF_W | PF_X);
152
phdr->p_align = grub_cpu_to_be32 (sizeof (long));
153
phdr->p_vaddr = grub_cpu_to_be32 (GRUB_IEEE1275_MODULE_BASE);
154
phdr->p_paddr = grub_cpu_to_be32 (GRUB_IEEE1275_MODULE_BASE);
155
phdr->p_filesz = grub_cpu_to_be32 (total_module_size);
156
phdr->p_memsz = grub_cpu_to_be32 (total_module_size);
160
add_segments (char *dir, FILE *out, int chrp, char *mods[])
163
Elf32_Phdr *phdrs = NULL;
170
/* Read ELF header. */
171
kernel_path = grub_util_get_path (dir, "grubof");
172
in = fopen (kernel_path, "rb");
174
grub_util_error ("cannot open %s", kernel_path);
176
grub_util_read_at (&ehdr, sizeof (ehdr), 0, in);
178
phdrs = xmalloc (grub_be_to_cpu16 (ehdr.e_phentsize)
179
* (grub_be_to_cpu16 (ehdr.e_phnum) + 2));
180
/* Copy all existing segments. */
181
for (i = 0; i < grub_be_to_cpu16 (ehdr.e_phnum); i++)
187
/* Read segment header. */
188
grub_util_read_at (phdr, sizeof (Elf32_Phdr),
189
(grub_be_to_cpu32 (ehdr.e_phoff)
190
+ (i * grub_be_to_cpu16 (ehdr.e_phentsize))),
192
grub_util_info ("copying segment %d, type %d", i,
193
grub_be_to_cpu32 (phdr->p_type));
195
/* Read segment data and write it to new file. */
196
segment_img = xmalloc (grub_be_to_cpu32 (phdr->p_filesz));
198
grub_util_read_at (segment_img, grub_be_to_cpu32 (phdr->p_filesz),
199
grub_be_to_cpu32 (phdr->p_offset), in);
200
grub_util_write_image_at (segment_img, grub_be_to_cpu32 (phdr->p_filesz),
201
grub_be_to_cpu32 (phdr->p_offset), out);
208
/* Construct new segment header for modules. */
209
phdr = phdrs + grub_be_to_cpu16 (ehdr.e_phnum);
210
ehdr.e_phnum = grub_cpu_to_be16 (grub_be_to_cpu16 (ehdr.e_phnum) + 1);
212
/* Fill in p_offset so the callees know where to write. */
213
phdr->p_offset = grub_cpu_to_be32 (ALIGN_UP (grub_util_get_fp_size (out),
216
load_modules (phdr, dir, mods, out);
221
/* Construct new segment header for the CHRP note. */
222
phdr = phdrs + grub_be_to_cpu16 (ehdr.e_phnum);
223
ehdr.e_phnum = grub_cpu_to_be16 (grub_be_to_cpu16 (ehdr.e_phnum) + 1);
225
/* Fill in p_offset so the callees know where to write. */
226
phdr->p_offset = grub_cpu_to_be32 (ALIGN_UP (grub_util_get_fp_size (out),
229
load_note (phdr, out);
232
/* Don't bother preserving the section headers. */
237
/* Append entire segment table to the file. */
238
phdroff = ALIGN_UP (grub_util_get_fp_size (out), sizeof (long));
239
grub_util_write_image_at (phdrs, grub_be_to_cpu16 (ehdr.e_phentsize)
240
* grub_be_to_cpu16 (ehdr.e_phnum), phdroff,
243
/* Write ELF header. */
244
ehdr.e_phoff = grub_cpu_to_be32 (phdroff);
245
grub_util_write_image_at (&ehdr, sizeof (ehdr), 0, out);
251
static struct option options[] =
253
{"directory", required_argument, 0, 'd'},
254
{"output", required_argument, 0, 'o'},
255
{"help", no_argument, 0, 'h'},
256
{"note", no_argument, 0, 'n'},
257
{"version", no_argument, 0, 'V'},
258
{"verbose", no_argument, 0, 'v'},
266
fprintf (stderr, "Try ``grub-mkimage --help'' for more information.\n");
269
Usage: grub-mkimage -o FILE [OPTION]... [MODULES]\n\
271
Make a bootable image of GRUB.\n\
273
-d, --directory=DIR use images and modules under DIR [default=%s]\n\
274
-o, --output=FILE output a generated image to FILE\n\
275
-h, --help display this message and exit\n\
276
-n, --note add NOTE segment for CHRP Open Firmware\n\
277
-V, --version print version information and exit\n\
278
-v, --verbose print verbose messages\n\
280
Report bugs to <%s>.\n\
281
", GRUB_DATADIR, PACKAGE_BUGREPORT);
287
main (int argc, char *argv[])
294
progname = "grub-mkimage";
298
int c = getopt_long (argc, argv, "d:o:hVvn", options, 0);
307
dir = xstrdup (optarg);
318
output = xstrdup (optarg);
321
printf ("grub-mkimage (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
335
fp = fopen (output, "wb");
337
grub_util_error ("cannot open %s", output);
339
add_segments (dir ? : GRUB_DATADIR, fp, chrp, argv + optind);