~hamo/ubuntu/precise/grub2/grub2.hi_res

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson, Colin Watson, Robert Millan, Updated translations
  • Date: 2010-11-22 12:24:56 UTC
  • mfrom: (1.26.4 upstream) (17.3.36 sid)
  • mto: (17.3.43 sid)
  • mto: This revision was merged to the branch mainline in revision 89.
  • Revision ID: james.westby@ubuntu.com-20101122122456-y82z3sfb7k4zfdcc
Tags: 1.99~20101122-1
[ Colin Watson ]
* New Bazaar snapshot.  Too many changes to list in full, but some of the
  more user-visible ones are as follows:
  - GRUB script:
    + Function parameters, "break", "continue", "shift", "setparams",
      "return", and "!".
    + "export" command supports multiple variable names.
    + Multi-line quoted strings support.
    + Wildcard expansion.
  - sendkey support.
  - USB hotunplugging and USB serial support.
  - Rename CD-ROM to cd on BIOS.
  - Add new --boot-directory option to grub-install, grub-reboot, and
    grub-set-default; the old --root-directory option is still accepted
    but was often confusing.
  - Basic btrfs detection/UUID support (but no file reading yet).
  - bash-completion for utilities.
  - If a device is listed in device.map, always assume that it is
    BIOS-visible rather than using extra layers such as LVM or RAID.
  - Add grub-mknetdir script (closes: #550658).
  - Remove deprecated "root" command.
  - Handle RAID devices containing virtio components.
  - GRUB Legacy configuration file support (via grub-menulst2cfg).
  - Keyboard layout support (via grub-mklayout and grub-kbdcomp).
  - Check generated grub.cfg for syntax errors before saving.
  - Pause execution for at most ten seconds if any errors are displayed,
    so that the user has a chance to see them.
  - Support submenus.
  - Write embedding zone using Reed-Solomon, so that it's robust against
    being partially overwritten (closes: #550702, #591416, #593347).
  - GRUB_DISABLE_LINUX_RECOVERY and GRUB_DISABLE_NETBSD_RECOVERY merged
    into a single GRUB_DISABLE_RECOVERY variable.
  - Fix loader memory allocation failure (closes: #551627).
  - Don't call savedefault on recovery entries (closes: #589325).
  - Support triple-indirect blocks on ext2 (closes: #543924).
  - Recognise DDF1 fake RAID (closes: #603354).

[ Robert Millan ]
* Use dpkg architecture wildcards.

[ Updated translations ]
* Slovenian (Vanja Cvelbar).  Closes: #604003
* Dzongkha (dawa pemo via Tenzin Dendup).  Closes: #604102

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* grub-mkimage.c - make a bootable image */
2
 
/*
3
 
 *  GRUB  --  GRand Unified Bootloader
4
 
 *  Copyright (C) 2008,2009,2010  Free Software Foundation, Inc.
5
 
 *
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.
10
 
 *
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.
15
 
 *
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/>.
18
 
 */
19
 
#include <config.h>
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>
29
 
 
30
 
#include <stdio.h>
31
 
#include <unistd.h>
32
 
#include <string.h>
33
 
#include <stdlib.h>
34
 
 
35
 
#define _GNU_SOURCE     1
36
 
#include <getopt.h>
37
 
 
38
 
#include "progname.h"
39
 
 
40
 
static void
41
 
compress_kernel (char *kernel_img, size_t kernel_size,
42
 
                 char **core_img, size_t *core_size)
43
 
{
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;
49
 
}
50
 
 
51
 
static void
52
 
generate_image (const char *dir, const char *prefix, FILE *out, char *mods[], char *memdisk_path)
53
 
{
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;
59
 
  unsigned int num;
60
 
 
61
 
  path_list = grub_util_resolve_dependencies (dir, "moddep.lst", mods);
62
 
 
63
 
  kernel_path = grub_util_get_path (dir, "kernel.img");
64
 
  kernel_size = grub_util_get_image_size (kernel_path);
65
 
 
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));
70
 
 
71
 
  memdisk_size = 0;
72
 
  if (memdisk_path)
73
 
    {
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);
77
 
    }
78
 
 
79
 
  grub_util_info ("the total module size is 0x%x", total_module_size);
80
 
 
81
 
  kernel_img = xmalloc (kernel_size + total_module_size);
82
 
  grub_util_load_image (kernel_path, kernel_img);
83
 
 
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);
88
 
 
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;
94
 
 
95
 
  offset = kernel_size + sizeof (struct grub_module_info);
96
 
  for (p = path_list; p; p = p->next)
97
 
    {
98
 
      struct grub_module_header *header;
99
 
      size_t mod_size;
100
 
 
101
 
      mod_size = grub_util_get_image_size (p->name);
102
 
 
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);
107
 
 
108
 
      grub_util_load_image (p->name, kernel_img + offset);
109
 
      offset += mod_size;
110
 
    }
111
 
 
112
 
  if (memdisk_path)
113
 
    {
114
 
      struct grub_module_header *header;
115
 
 
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);
120
 
 
121
 
      grub_util_load_image (memdisk_path, kernel_img + offset);
122
 
      offset += memdisk_size;
123
 
    }
124
 
 
125
 
  compress_kernel (kernel_img, kernel_size + total_module_size,
126
 
                   &core_img, &core_size);
127
 
 
128
 
  grub_util_info ("the core size is 0x%x", core_size);
129
 
 
130
 
  num = ((core_size + GRUB_DISK_SECTOR_SIZE - 1) >> GRUB_DISK_SECTOR_BITS);
131
 
  num <<= GRUB_DISK_SECTOR_BITS;
132
 
 
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");
137
 
 
138
 
  boot_img = grub_util_read_image (boot_path);
139
 
 
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);
144
 
 
145
 
  grub_util_write_image (boot_img, boot_size, out);
146
 
  free (boot_img);
147
 
  free (boot_path);
148
 
 
149
 
  module_addr = (path_list
150
 
                 ? (GRUB_BOOT_MACHINE_IMAGE_ADDRESS + kernel_size)
151
 
                 : 0);
152
 
 
153
 
  grub_util_info ("the first module address is 0x%x", module_addr);
154
 
 
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);
159
 
 
160
 
  /* No compression support yet.  */
161
 
  *((grub_uint32_t *) (core_img + GRUB_KERNEL_MACHINE_COMPRESSED_SIZE))
162
 
    = grub_cpu_to_be32 (0);
163
 
 
164
 
  grub_util_write_image (core_img, core_size, out);
165
 
  free (kernel_img);
166
 
  free (core_img);
167
 
  free (kernel_path);
168
 
 
169
 
  while (path_list)
170
 
    {
171
 
      struct grub_util_path_list *next = path_list->next;
172
 
      free ((void *) path_list->name);
173
 
      free (path_list);
174
 
      path_list = next;
175
 
    }
176
 
}
177
 
 
178
 
static struct option options[] =
179
 
  {
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'},
187
 
    {0, 0, 0, 0}
188
 
  };
189
 
 
190
 
static void
191
 
usage (int status)
192
 
{
193
 
  if (status)
194
 
    fprintf (stderr, "Try `%s --help' for more information.\n", program_name);
195
 
  else
196
 
    printf ("\
197
 
Usage: %s [OPTION]... [MODULES]\n\
198
 
\n\
199
 
Make a bootable image of GRUB.\n\
200
 
\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\
208
 
\n\
209
 
Report bugs to <%s>.\n\
210
 
", program_name, GRUB_LIBDIR, DEFAULT_DIRECTORY, PACKAGE_BUGREPORT);
211
 
 
212
 
  exit (status);
213
 
}
214
 
 
215
 
int
216
 
main (int argc, char *argv[])
217
 
{
218
 
  char *output = NULL;
219
 
  char *dir = NULL;
220
 
  char *prefix = NULL;
221
 
  char *memdisk = NULL;
222
 
  FILE *fp = stdout;
223
 
 
224
 
  set_program_name (argv[0]);
225
 
 
226
 
  grub_util_init_nls ();
227
 
 
228
 
  while (1)
229
 
    {
230
 
      int c = getopt_long (argc, argv, "d:p:m:o:hVv", options, 0);
231
 
 
232
 
      if (c == -1)
233
 
        break;
234
 
      else
235
 
        switch (c)
236
 
          {
237
 
          case 'o':
238
 
            if (output)
239
 
              free (output);
240
 
            output = xstrdup (optarg);
241
 
            break;
242
 
 
243
 
          case 'd':
244
 
            if (dir)
245
 
              free (dir);
246
 
            dir = xstrdup (optarg);
247
 
            break;
248
 
 
249
 
          case 'm':
250
 
            if (memdisk)
251
 
              free (memdisk);
252
 
            memdisk = xstrdup (optarg);
253
 
 
254
 
            if (prefix)
255
 
              free (prefix);
256
 
            prefix = xstrdup ("(memdisk)/boot/grub");
257
 
            break;
258
 
 
259
 
          case 'h':
260
 
            usage (0);
261
 
            break;
262
 
 
263
 
          case 'p':
264
 
            if (prefix)
265
 
              free (prefix);
266
 
            prefix = xstrdup (optarg);
267
 
            break;
268
 
 
269
 
          case 'V':
270
 
            printf ("grub-mkimage (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
271
 
            return 0;
272
 
 
273
 
          case 'v':
274
 
            verbosity++;
275
 
            break;
276
 
 
277
 
          default:
278
 
            usage (1);
279
 
            break;
280
 
          }
281
 
    }
282
 
 
283
 
  if (output)
284
 
    {
285
 
      fp = fopen (output, "wb");
286
 
      if (! fp)
287
 
        grub_util_error ("cannot open %s", output);
288
 
    }
289
 
 
290
 
  generate_image (dir ? : GRUB_LIBDIR,
291
 
                  prefix ? : DEFAULT_DIRECTORY, fp,
292
 
                  argv + optind, memdisk);
293
 
 
294
 
  fclose (fp);
295
 
 
296
 
  if (dir)
297
 
    free (dir);
298
 
 
299
 
  return 0;
300
 
}