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

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Robert Millan
  • Date: 2009-07-02 13:23:51 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20090702132351-tanpn0ryyijp93gu
Tags: 1.96+20090702-1
* New SVN snapshot.
* rules: Remove duplicated files in sparc64-ieee1275 port.
* rules: Comment out -DGRUB_ASSUME_LINUX_HAS_FB_SUPPORT=1 setting.  We'll
  re-evaluate using it when it's more mature.  (Closes: #535026).

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  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/disk.h>
 
25
#include <grub/util/misc.h>
 
26
#include <grub/util/resolve.h>
 
27
#include <grub/misc.h>
 
28
 
 
29
#include <stdio.h>
 
30
#include <unistd.h>
 
31
#include <string.h>
 
32
#include <stdlib.h>
 
33
 
 
34
#define _GNU_SOURCE     1
 
35
#include <getopt.h>
 
36
 
 
37
static void
 
38
compress_kernel (char *kernel_img, size_t kernel_size,
 
39
                 char **core_img, size_t *core_size)
 
40
{
 
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;
 
46
}
 
47
 
 
48
static void
 
49
generate_image (const char *dir, const char *prefix, FILE *out, char *mods[], char *memdisk_path)
 
50
{
 
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;
 
56
  unsigned int num;
 
57
 
 
58
  path_list = grub_util_resolve_dependencies (dir, "moddep.lst", mods);
 
59
 
 
60
  kernel_path = grub_util_get_path (dir, "kernel.img");
 
61
  kernel_size = grub_util_get_image_size (kernel_path);
 
62
 
 
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));
 
67
 
 
68
  memdisk_size = 0;
 
69
  if (memdisk_path)
 
70
    {
 
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);
 
74
    }
 
75
 
 
76
  grub_util_info ("the total module size is 0x%x", total_module_size);
 
77
 
 
78
  kernel_img = xmalloc (kernel_size + total_module_size);
 
79
  grub_util_load_image (kernel_path, kernel_img);
 
80
 
 
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);
 
85
 
 
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;
 
91
 
 
92
  offset = kernel_size + sizeof (struct grub_module_info);
 
93
  for (p = path_list; p; p = p->next)
 
94
    {
 
95
      struct grub_module_header *header;
 
96
      size_t mod_size;
 
97
 
 
98
      mod_size = grub_util_get_image_size (p->name);
 
99
 
 
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);
 
104
 
 
105
      grub_util_load_image (p->name, kernel_img + offset);
 
106
      offset += mod_size;
 
107
    }
 
108
 
 
109
  if (memdisk_path)
 
110
    {
 
111
      struct grub_module_header *header;
 
112
 
 
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);
 
117
 
 
118
      grub_util_load_image (memdisk_path, kernel_img + offset);
 
119
      offset += memdisk_size;
 
120
    }
 
121
 
 
122
  compress_kernel (kernel_img, kernel_size + total_module_size,
 
123
                   &core_img, &core_size);
 
124
 
 
125
  grub_util_info ("the core size is 0x%x", core_size);
 
126
 
 
127
  num = ((core_size + GRUB_DISK_SECTOR_SIZE - 1) >> GRUB_DISK_SECTOR_BITS);
 
128
  num <<= GRUB_DISK_SECTOR_BITS;
 
129
 
 
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");
 
134
 
 
135
  boot_img = grub_util_read_image (boot_path);
 
136
 
 
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);
 
141
 
 
142
  grub_util_write_image (boot_img, boot_size, out);
 
143
  free (boot_img);
 
144
  free (boot_path);
 
145
 
 
146
  module_addr = (path_list
 
147
                 ? (GRUB_BOOT_MACHINE_IMAGE_ADDRESS + kernel_size)
 
148
                 : 0);
 
149
 
 
150
  grub_util_info ("the first module address is 0x%x", module_addr);
 
151
 
 
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);
 
156
 
 
157
  /* No compression support yet.  */
 
158
  *((grub_uint32_t *) (core_img + GRUB_KERNEL_MACHINE_COMPRESSED_SIZE))
 
159
    = grub_cpu_to_be32 (0);
 
160
 
 
161
  grub_util_write_image (core_img, core_size, out);
 
162
  free (kernel_img);
 
163
  free (core_img);
 
164
  free (kernel_path);
 
165
 
 
166
  while (path_list)
 
167
    {
 
168
      struct grub_util_path_list *next = path_list->next;
 
169
      free ((void *) path_list->name);
 
170
      free (path_list);
 
171
      path_list = next;
 
172
    }
 
173
}
 
174
 
 
175
static struct option options[] =
 
176
  {
 
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'},
 
184
    {0, 0, 0, 0}
 
185
  };
 
186
 
 
187
static void
 
188
usage (int status)
 
189
{
 
190
  if (status)
 
191
    fprintf (stderr, "Try ``grub-mkimage --help'' for more information.\n");
 
192
  else
 
193
    printf ("\
 
194
Usage: grub-mkimage [OPTION]... [MODULES]\n\
 
195
\n\
 
196
Make a bootable image of GRUB.\n\
 
197
\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\
 
205
\n\
 
206
Report bugs to <%s>.\n\
 
207
", GRUB_LIBDIR, DEFAULT_DIRECTORY, PACKAGE_BUGREPORT);
 
208
 
 
209
  exit (status);
 
210
}
 
211
 
 
212
int
 
213
main (int argc, char *argv[])
 
214
{
 
215
  char *output = NULL;
 
216
  char *dir = NULL;
 
217
  char *prefix = NULL;
 
218
  char *memdisk = NULL;
 
219
  FILE *fp = stdout;
 
220
 
 
221
  progname = "grub-mkimage";
 
222
  while (1)
 
223
    {
 
224
      int c = getopt_long (argc, argv, "d:p:m:o:hVv", options, 0);
 
225
 
 
226
      if (c == -1)
 
227
        break;
 
228
      else
 
229
        switch (c)
 
230
          {
 
231
          case 'o':
 
232
            if (output)
 
233
              free (output);
 
234
            output = xstrdup (optarg);
 
235
            break;
 
236
 
 
237
          case 'd':
 
238
            if (dir)
 
239
              free (dir);
 
240
            dir = xstrdup (optarg);
 
241
            break;
 
242
 
 
243
          case 'm':
 
244
            if (memdisk)
 
245
              free (memdisk);
 
246
            memdisk = xstrdup (optarg);
 
247
 
 
248
            if (prefix)
 
249
              free (prefix);
 
250
            prefix = xstrdup ("(memdisk)/boot/grub");
 
251
            break;
 
252
 
 
253
          case 'h':
 
254
            usage (0);
 
255
            break;
 
256
 
 
257
          case 'p':
 
258
            if (prefix)
 
259
              free (prefix);
 
260
            prefix = xstrdup (optarg);
 
261
            break;
 
262
 
 
263
          case 'V':
 
264
            printf ("grub-mkimage (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
 
265
            return 0;
 
266
 
 
267
          case 'v':
 
268
            verbosity++;
 
269
            break;
 
270
 
 
271
          default:
 
272
            usage (1);
 
273
            break;
 
274
          }
 
275
    }
 
276
 
 
277
  if (output)
 
278
    {
 
279
      fp = fopen (output, "wb");
 
280
      if (! fp)
 
281
        grub_util_error ("cannot open %s", output);
 
282
    }
 
283
 
 
284
  generate_image (dir ? : GRUB_LIBDIR,
 
285
                  prefix ? : DEFAULT_DIRECTORY, fp,
 
286
                  argv + optind, memdisk);
 
287
 
 
288
  fclose (fp);
 
289
 
 
290
  if (dir)
 
291
    free (dir);
 
292
 
 
293
  return 0;
 
294
}