~ubuntu-branches/debian/sid/grub2/sid-200907171837

« back to all changes in this revision

Viewing changes to util/powerpc/ieee1275/grub-mkimage.c

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2006-01-05 15:20:40 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060105152040-1ab076d4n3y2o5yf
Tags: 1.92-1
* New upstream release.
  - Add support for GPT partition table format.
  - Add a new command "play" to play an audio file on PC.
  - Add support for Linux/ADFS partition table format.
  - Add support for BASH-like scripting.
  - Add support for Apple HFS+ filesystems.
* 01_fix_grub-install.patch: Added. Fix grub-install to use
  /bin/grub-mkimage instead of /sbin/grub-mkimage. Closes: #338824
* Do not use CDBS tarball mode anymore. Closes: #344272  

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  GRUB  --  GRand Unified Bootloader
 
3
 *  Copyright (C) 2004, 2005  Free Software Foundation, Inc.
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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.
 
18
 */
 
19
 
 
20
#include <stdio.h>
 
21
#include <stdint.h>
 
22
#include <unistd.h>
 
23
#include <fcntl.h>
 
24
#include <getopt.h>
 
25
#include <stdlib.h>
 
26
#include <string.h>
 
27
#include <grub/elf.h>
 
28
#include <grub/util/misc.h>
 
29
#include <grub/util/resolve.h>
 
30
#include <grub/kernel.h>
 
31
#include <grub/machine/kernel.h>
 
32
 
 
33
#define ALIGN_UP(addr, align) ((long)((char *)addr + align - 1) & ~(align - 1))
 
34
 
 
35
#define GRUB_IEEE1275_NOTE_NAME "PowerPC"
 
36
#define GRUB_IEEE1275_NOTE_TYPE 0x1275
 
37
 
 
38
/* These structures are defined according to the CHRP binding to IEEE1275,
 
39
   "Client Program Format" section.  */
 
40
 
 
41
struct grub_ieee1275_note_hdr
 
42
{
 
43
  grub_uint32_t namesz;
 
44
  grub_uint32_t descsz;
 
45
  grub_uint32_t type;
 
46
  char name[sizeof (GRUB_IEEE1275_NOTE_NAME)];
 
47
};
 
48
 
 
49
struct grub_ieee1275_note_desc
 
50
{
 
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;
 
57
};
 
58
 
 
59
struct grub_ieee1275_note
 
60
{
 
61
  struct grub_ieee1275_note_hdr header;
 
62
  struct grub_ieee1275_note_desc descriptor;
 
63
};
 
64
 
 
65
void
 
66
load_note (Elf32_Phdr *phdr, FILE *out)
 
67
{
 
68
  struct grub_ieee1275_note note;
 
69
  int note_size = sizeof (struct grub_ieee1275_note);
 
70
 
 
71
  grub_util_info ("adding CHRP NOTE segment");
 
72
 
 
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);
 
83
 
 
84
  /* Write the note data to the new segment.  */
 
85
  grub_util_write_image_at (&note, note_size,
 
86
                            grub_be_to_cpu32 (phdr->p_offset), out);
 
87
 
 
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));
 
92
  phdr->p_vaddr = 0;
 
93
  phdr->p_paddr = 0;
 
94
  phdr->p_filesz = grub_cpu_to_be32 (note_size);
 
95
  phdr->p_memsz = 0;
 
96
}
 
97
 
 
98
void
 
99
load_modules (Elf32_Phdr *phdr, const char *dir, char *mods[], FILE *out)
 
100
{
 
101
  char *module_img;
 
102
  struct grub_util_path_list *path_list;
 
103
  struct grub_util_path_list *p;
 
104
  struct grub_module_info *modinfo;
 
105
  size_t offset;
 
106
  size_t total_module_size;
 
107
 
 
108
  path_list = grub_util_resolve_dependencies (dir, "moddep.lst", mods);
 
109
 
 
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)
 
113
    {
 
114
      total_module_size += (grub_util_get_image_size (p->name)
 
115
          + sizeof (struct grub_module_header));
 
116
    }
 
117
 
 
118
  grub_util_info ("the total module size is 0x%x", total_module_size);
 
119
 
 
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);
 
125
 
 
126
  /* Load all the modules, with headers, into module_img.  */
 
127
  for (p = path_list; p; p = p->next)
 
128
    {
 
129
      struct grub_module_header *header;
 
130
      size_t mod_size;
 
131
 
 
132
      grub_util_info ("adding module %s", p->name);
 
133
 
 
134
      mod_size = grub_util_get_image_size (p->name);
 
135
 
 
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));
 
139
 
 
140
      grub_util_load_image (p->name, module_img + offset + sizeof (*header));
 
141
 
 
142
      offset += sizeof (*header) + mod_size;
 
143
    }
 
144
 
 
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);
 
148
 
 
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);
 
157
}
 
158
 
 
159
void
 
160
add_segments (char *dir, FILE *out, int chrp, char *mods[])
 
161
{
 
162
  Elf32_Ehdr ehdr;
 
163
  Elf32_Phdr *phdrs = NULL;
 
164
  Elf32_Phdr *phdr;
 
165
  FILE *in;
 
166
  char *kernel_path;
 
167
  off_t phdroff;
 
168
  int i;
 
169
 
 
170
  /* Read ELF header.  */
 
171
  kernel_path = grub_util_get_path (dir, "grubof");
 
172
  in = fopen (kernel_path, "rb");
 
173
  if (! in)
 
174
    grub_util_error ("cannot open %s", kernel_path);
 
175
 
 
176
  grub_util_read_at (&ehdr, sizeof (ehdr), 0, in);
 
177
  
 
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++)
 
182
    {
 
183
      char *segment_img;
 
184
 
 
185
      phdr = phdrs + i;
 
186
 
 
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))),
 
191
                         in);
 
192
      grub_util_info ("copying segment %d, type %d", i,
 
193
                      grub_be_to_cpu32 (phdr->p_type));
 
194
 
 
195
      /* Read segment data and write it to new file.  */
 
196
      segment_img = xmalloc (grub_be_to_cpu32 (phdr->p_filesz));
 
197
  
 
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);
 
202
 
 
203
      free (segment_img);
 
204
    }
 
205
 
 
206
  if (mods[0] != NULL)
 
207
    {
 
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);
 
211
 
 
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),
 
214
                                                   sizeof (long)));
 
215
 
 
216
      load_modules (phdr, dir, mods, out);
 
217
    }
 
218
 
 
219
  if (chrp)
 
220
    {
 
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);
 
224
 
 
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),
 
227
                                                   sizeof (long)));
 
228
 
 
229
      load_note (phdr, out);
 
230
    }
 
231
 
 
232
  /* Don't bother preserving the section headers.  */
 
233
  ehdr.e_shoff = 0;
 
234
  ehdr.e_shnum = 0;
 
235
  ehdr.e_shstrndx = 0;
 
236
 
 
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,
 
241
                            out);
 
242
 
 
243
  /* Write ELF header.  */
 
244
  ehdr.e_phoff = grub_cpu_to_be32 (phdroff);
 
245
  grub_util_write_image_at (&ehdr, sizeof (ehdr), 0, out);
 
246
 
 
247
  free (phdrs);
 
248
  free (kernel_path);
 
249
}
 
250
 
 
251
static struct option options[] =
 
252
  {
 
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'},
 
259
    { 0, 0, 0, 0 },
 
260
  };
 
261
 
 
262
static void
 
263
usage (int status)
 
264
{
 
265
  if (status)
 
266
    fprintf (stderr, "Try ``grub-mkimage --help'' for more information.\n");
 
267
  else
 
268
    printf ("\
 
269
Usage: grub-mkimage -o FILE [OPTION]... [MODULES]\n\
 
270
\n\
 
271
Make a bootable image of GRUB.\n\
 
272
\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\
 
279
\n\
 
280
Report bugs to <%s>.\n\
 
281
", GRUB_DATADIR, PACKAGE_BUGREPORT);
 
282
 
 
283
  exit (status);
 
284
}
 
285
 
 
286
int
 
287
main (int argc, char *argv[])
 
288
{
 
289
  FILE *fp;
 
290
  char *output = NULL;
 
291
  char *dir = NULL;
 
292
  int chrp = 0;
 
293
 
 
294
  progname = "grub-mkimage";
 
295
 
 
296
  while (1)
 
297
    {
 
298
      int c = getopt_long (argc, argv, "d:o:hVvn", options, 0);
 
299
      if (c == -1)
 
300
        break;
 
301
 
 
302
      switch (c)
 
303
        {
 
304
          case 'd':
 
305
            if (dir)
 
306
              free (dir);
 
307
            dir = xstrdup (optarg);
 
308
            break;
 
309
          case 'h':
 
310
            usage (0);
 
311
            break;
 
312
          case 'n':
 
313
            chrp = 1;
 
314
            break;
 
315
          case 'o':
 
316
            if (output)
 
317
              free (output);
 
318
            output = xstrdup (optarg);
 
319
            break;
 
320
          case 'V':
 
321
            printf ("grub-mkimage (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
 
322
            return 0;
 
323
          case 'v':
 
324
            verbosity++;
 
325
            break;
 
326
          default:
 
327
            usage (1);
 
328
            break;
 
329
        }
 
330
  }
 
331
 
 
332
  if (!output)
 
333
    usage (1);
 
334
 
 
335
  fp = fopen (output, "wb");
 
336
  if (! fp)
 
337
    grub_util_error ("cannot open %s", output);
 
338
 
 
339
  add_segments (dir ? : GRUB_DATADIR, fp, chrp, argv + optind);
 
340
 
 
341
  fclose (fp);
 
342
 
 
343
  return 0;
 
344
}