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

« back to all changes in this revision

Viewing changes to util/i386/pc/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
1
/* grub-mkimage.c - make a bootable image */
2
2
/*
3
3
 *  GRUB  --  GRand Unified Bootloader
4
 
 *  Copyright (C) 2002,2003,2004,2005,2006,2007,2008  Free Software Foundation, Inc.
 
4
 *  Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009  Free Software Foundation, Inc.
5
5
 *
6
6
 *  GRUB is free software: you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
36
36
#define _GNU_SOURCE     1
37
37
#include <getopt.h>
38
38
 
 
39
#if defined(ENABLE_LZO)
 
40
 
39
41
#if defined(HAVE_LZO_LZO1X_H)
40
42
# include <lzo/lzo1x.h>
41
43
#elif defined(HAVE_LZO1X_H)
42
44
# include <lzo1x.h>
43
45
#endif
44
46
 
 
47
#elif defined(ENABLE_LZMA)
 
48
 
 
49
#include <grub/lib/LzmaEnc.h>
 
50
 
 
51
#endif
 
52
 
 
53
#if defined(ENABLE_LZO)
 
54
 
45
55
static void
46
56
compress_kernel (char *kernel_img, size_t kernel_size,
47
57
                 char **core_img, size_t *core_size)
48
58
{
49
59
  lzo_uint size;
50
60
  char *wrkmem;
51
 
  
52
 
  grub_util_info ("kernel_img=%p, kernel_size=0x%x", kernel_img, kernel_size);
 
61
 
53
62
  if (kernel_size < GRUB_KERNEL_MACHINE_RAW_SIZE)
54
63
    grub_util_error ("the core image is too small");
55
 
  
 
64
 
56
65
  if (lzo_init () != LZO_E_OK)
57
66
    grub_util_error ("cannot initialize LZO");
58
67
 
60
69
  wrkmem = xmalloc (LZO1X_999_MEM_COMPRESS);
61
70
 
62
71
  memcpy (*core_img, kernel_img, GRUB_KERNEL_MACHINE_RAW_SIZE);
63
 
  
 
72
 
64
73
  grub_util_info ("compressing the core image");
65
74
  if (lzo1x_999_compress ((const lzo_byte *) (kernel_img
66
75
                                              + GRUB_KERNEL_MACHINE_RAW_SIZE),
76
85
  *core_size = (size_t) size + GRUB_KERNEL_MACHINE_RAW_SIZE;
77
86
}
78
87
 
79
 
static void
80
 
generate_image (const char *dir, char *prefix, FILE *out, char *mods[], char *memdisk_path)
81
 
{
82
 
  grub_addr_t module_addr = 0;
 
88
#elif defined(ENABLE_LZMA)
 
89
 
 
90
static void *SzAlloc(void *p, size_t size) { p = p; return xmalloc(size); }
 
91
static void SzFree(void *p, void *address) { p = p; free(address); }
 
92
static ISzAlloc g_Alloc = { SzAlloc, SzFree };
 
93
 
 
94
static void
 
95
compress_kernel (char *kernel_img, size_t kernel_size,
 
96
                 char **core_img, size_t *core_size)
 
97
{
 
98
  CLzmaEncProps props;
 
99
  unsigned char out_props[5];
 
100
  size_t out_props_size = 5;
 
101
 
 
102
  LzmaEncProps_Init(&props);
 
103
  props.dictSize = 1 << 16;
 
104
  props.lc = 3;
 
105
  props.lp = 0;
 
106
  props.pb = 2;
 
107
  props.numThreads = 1;
 
108
 
 
109
  if (kernel_size < GRUB_KERNEL_MACHINE_RAW_SIZE)
 
110
    grub_util_error ("the core image is too small");
 
111
 
 
112
  *core_img = xmalloc (kernel_size);
 
113
  memcpy (*core_img, kernel_img, GRUB_KERNEL_MACHINE_RAW_SIZE);
 
114
 
 
115
  *core_size = kernel_size - GRUB_KERNEL_MACHINE_RAW_SIZE;
 
116
  if (LzmaEncode((unsigned char *) *core_img + GRUB_KERNEL_MACHINE_RAW_SIZE,
 
117
                 core_size,
 
118
                 (unsigned char *) kernel_img + GRUB_KERNEL_MACHINE_RAW_SIZE,
 
119
                 kernel_size - GRUB_KERNEL_MACHINE_RAW_SIZE,
 
120
                 &props, out_props, &out_props_size,
 
121
                 0, NULL, &g_Alloc, &g_Alloc) != SZ_OK)
 
122
    grub_util_error ("cannot compress the kernel image");
 
123
 
 
124
  *core_size += GRUB_KERNEL_MACHINE_RAW_SIZE;
 
125
}
 
126
 
 
127
#else
 
128
 
 
129
static void
 
130
compress_kernel (char *kernel_img, size_t kernel_size,
 
131
               char **core_img, size_t *core_size)
 
132
{
 
133
  *core_img = xmalloc (kernel_size);
 
134
  memcpy (*core_img, kernel_img, kernel_size);
 
135
  *core_size = kernel_size;
 
136
}
 
137
 
 
138
#endif
 
139
 
 
140
static void
 
141
generate_image (const char *dir, char *prefix, FILE *out, char *mods[],
 
142
                char *memdisk_path, char *config_path)
 
143
{
83
144
  char *kernel_img, *boot_img, *core_img;
84
 
  size_t kernel_size, boot_size, total_module_size, core_size, memdisk_size = 0;
 
145
  size_t kernel_size, boot_size, total_module_size, core_size;
 
146
  size_t memdisk_size = 0, config_size = 0;
85
147
  char *kernel_path, *boot_path;
86
148
  unsigned num;
87
149
  size_t offset;
94
156
  kernel_size = grub_util_get_image_size (kernel_path);
95
157
 
96
158
  total_module_size = sizeof (struct grub_module_info);
 
159
 
 
160
  if (memdisk_path)
 
161
    {
 
162
      memdisk_size = ALIGN_UP(grub_util_get_image_size (memdisk_path), 512);
 
163
      grub_util_info ("the size of memory disk is 0x%x", memdisk_size);
 
164
      total_module_size += memdisk_size + sizeof (struct grub_module_header);
 
165
    }
 
166
 
 
167
  if (config_path)
 
168
    {
 
169
      config_size = grub_util_get_image_size (config_path) + 1;
 
170
      grub_util_info ("the size of config file is 0x%x", config_size);
 
171
      total_module_size += config_size + sizeof (struct grub_module_header);
 
172
    }
 
173
 
97
174
  for (p = path_list; p; p = p->next)
98
175
    total_module_size += (grub_util_get_image_size (p->name)
99
176
                          + sizeof (struct grub_module_header));
100
177
 
101
178
  grub_util_info ("the total module size is 0x%x", total_module_size);
102
179
 
103
 
  if (memdisk_path)
104
 
    {
105
 
      memdisk_size = ALIGN_UP(grub_util_get_image_size (memdisk_path), 512);
106
 
      grub_util_info ("the size of memory disk is 0x%x", memdisk_size);
107
 
    }
108
 
 
109
 
  kernel_img = xmalloc (kernel_size + total_module_size + memdisk_size);
 
180
  kernel_img = xmalloc (kernel_size + total_module_size);
110
181
  grub_util_load_image (kernel_path, kernel_img);
111
182
 
112
183
  if (GRUB_KERNEL_MACHINE_PREFIX + strlen (prefix) + 1 > GRUB_KERNEL_MACHINE_DATA_END)
115
186
 
116
187
  /* Fill in the grub_module_info structure.  */
117
188
  modinfo = (struct grub_module_info *) (kernel_img + kernel_size);
 
189
  memset (modinfo, 0, sizeof (struct grub_module_info));
118
190
  modinfo->magic = GRUB_MODULE_MAGIC;
119
191
  modinfo->offset = sizeof (struct grub_module_info);
120
192
  modinfo->size = total_module_size;
126
198
      size_t mod_size;
127
199
 
128
200
      mod_size = grub_util_get_image_size (p->name);
129
 
      
 
201
 
130
202
      header = (struct grub_module_header *) (kernel_img + offset);
131
 
      header->offset = grub_cpu_to_le32 (sizeof (*header));
 
203
      memset (header, 0, sizeof (struct grub_module_header));
 
204
      header->type = grub_cpu_to_le32 (OBJ_TYPE_ELF);
132
205
      header->size = grub_cpu_to_le32 (mod_size + sizeof (*header));
133
206
      offset += sizeof (*header);
134
207
 
138
211
 
139
212
  if (memdisk_path)
140
213
    {
 
214
      struct grub_module_header *header;
 
215
 
 
216
      header = (struct grub_module_header *) (kernel_img + offset);
 
217
      memset (header, 0, sizeof (struct grub_module_header));
 
218
      header->type = grub_cpu_to_le32 (OBJ_TYPE_MEMDISK);
 
219
      header->size = grub_cpu_to_le32 (memdisk_size + sizeof (*header));
 
220
      offset += sizeof (*header);
 
221
 
141
222
      grub_util_load_image (memdisk_path, kernel_img + offset);
142
223
      offset += memdisk_size;
143
224
    }
144
225
 
145
 
  compress_kernel (kernel_img, kernel_size + total_module_size + memdisk_size,
 
226
  if (config_path)
 
227
    {
 
228
      struct grub_module_header *header;
 
229
 
 
230
      header = (struct grub_module_header *) (kernel_img + offset);
 
231
      memset (header, 0, sizeof (struct grub_module_header));
 
232
      header->type = grub_cpu_to_le32 (OBJ_TYPE_CONFIG);
 
233
      header->size = grub_cpu_to_le32 (config_size + sizeof (*header));
 
234
      offset += sizeof (*header);
 
235
 
 
236
      grub_util_load_image (config_path, kernel_img + offset);
 
237
      offset += config_size;
 
238
      *(kernel_img + offset - 1) = 0;
 
239
    }
 
240
 
 
241
  grub_util_info ("kernel_img=%p, kernel_size=0x%x", kernel_img, kernel_size);
 
242
  compress_kernel (kernel_img, kernel_size + total_module_size,
146
243
                   &core_img, &core_size);
147
244
 
148
245
  grub_util_info ("the core size is 0x%x", core_size);
149
 
  
 
246
 
150
247
  num = ((core_size + GRUB_DISK_SECTOR_SIZE - 1) >> GRUB_DISK_SECTOR_BITS);
151
248
  if (num > 0xffff)
152
249
    grub_util_error ("the core image is too big");
153
250
 
 
251
#if defined(GRUB_MACHINE_PCBIOS)
 
252
 
154
253
  boot_path = grub_util_get_path (dir, "diskboot.img");
155
254
  boot_size = grub_util_get_image_size (boot_path);
156
255
  if (boot_size != GRUB_DISK_SECTOR_SIZE)
157
256
    grub_util_error ("diskboot.img is not one sector size");
158
 
  
 
257
 
159
258
  boot_img = grub_util_read_image (boot_path);
160
 
  
 
259
 
161
260
  /* i386 is a little endian architecture.  */
162
261
  *((grub_uint16_t *) (boot_img + GRUB_DISK_SECTOR_SIZE
163
262
                       - GRUB_BOOT_MACHINE_LIST_SIZE + 8))
166
265
  grub_util_write_image (boot_img, boot_size, out);
167
266
  free (boot_img);
168
267
  free (boot_path);
169
 
  
170
 
  module_addr = (path_list
171
 
                 ? (GRUB_BOOT_MACHINE_KERNEL_ADDR + GRUB_DISK_SECTOR_SIZE
172
 
                    + kernel_size)
173
 
                 : 0);
174
 
 
175
 
  grub_util_info ("the first module address is 0x%x", module_addr);
 
268
 
 
269
#elif defined(GRUB_MACHINE_QEMU)
 
270
 
 
271
  {
 
272
    char *rom_img;
 
273
    size_t rom_size;
 
274
 
 
275
    boot_path = grub_util_get_path (dir, "boot.img");
 
276
    boot_size = grub_util_get_image_size (boot_path);
 
277
    boot_img = grub_util_read_image (boot_path);
 
278
 
 
279
    /* Rom sizes must be 64k-aligned.  */
 
280
    rom_size = ALIGN_UP (core_size + boot_size, 64 * 1024);
 
281
 
 
282
    rom_img = xmalloc (rom_size);
 
283
    memset (rom_img, 0, rom_size);
 
284
 
 
285
    *((grub_int32_t *) (core_img + GRUB_KERNEL_MACHINE_CORE_ENTRY_ADDR))
 
286
      = grub_cpu_to_le32 ((grub_uint32_t) -rom_size);
 
287
 
 
288
    memcpy (rom_img, core_img, core_size);
 
289
 
 
290
    *((grub_int32_t *) (boot_img + GRUB_BOOT_MACHINE_CORE_ENTRY_ADDR))
 
291
      = grub_cpu_to_le32 ((grub_uint32_t) -rom_size);
 
292
 
 
293
    memcpy (rom_img + rom_size - boot_size, boot_img, boot_size);
 
294
 
 
295
    free (core_img);
 
296
    core_img = rom_img;
 
297
    core_size = rom_size;
 
298
 
 
299
    free (boot_img);
 
300
    free (boot_path);
 
301
  }
 
302
 
 
303
#endif
 
304
 
 
305
#ifdef GRUB_KERNEL_MACHINE_TOTAL_MODULE_SIZE
176
306
  *((grub_uint32_t *) (core_img + GRUB_KERNEL_MACHINE_TOTAL_MODULE_SIZE))
177
307
    = grub_cpu_to_le32 (total_module_size);
 
308
#endif
178
309
  *((grub_uint32_t *) (core_img + GRUB_KERNEL_MACHINE_KERNEL_IMAGE_SIZE))
179
310
    = grub_cpu_to_le32 (kernel_size);
180
 
  *((grub_uint32_t *) (core_img + GRUB_KERNEL_MACHINE_MEMDISK_IMAGE_SIZE))
181
 
    = grub_cpu_to_le32 (memdisk_size);
 
311
#ifdef GRUB_KERNEL_MACHINE_COMPRESSED_SIZE
182
312
  *((grub_uint32_t *) (core_img + GRUB_KERNEL_MACHINE_COMPRESSED_SIZE))
183
313
    = grub_cpu_to_le32 (core_size - GRUB_KERNEL_MACHINE_RAW_SIZE);
184
 
 
185
 
  if (core_size > GRUB_MEMORY_MACHINE_UPPER - GRUB_MEMORY_MACHINE_LINK_ADDR)
186
 
    grub_util_error ("Core image is too big (%p > %p)\n", core_size,
187
 
                     GRUB_MEMORY_MACHINE_UPPER - GRUB_MEMORY_MACHINE_LINK_ADDR);
188
 
  
 
314
#endif
 
315
 
 
316
#if defined(GRUB_KERNEL_MACHINE_INSTALL_DOS_PART) && defined(GRUB_KERNEL_MACHINE_INSTALL_BSD_PART)
 
317
  /* If we included a drive in our prefix, let GRUB know it doesn't have to
 
318
     prepend the drive told by BIOS.  */
 
319
  if (prefix[0] == '(')
 
320
    {
 
321
      *((grub_int32_t *) (core_img + GRUB_KERNEL_MACHINE_INSTALL_DOS_PART))
 
322
        = grub_cpu_to_le32 (-2);
 
323
      *((grub_int32_t *) (core_img + GRUB_KERNEL_MACHINE_INSTALL_BSD_PART))
 
324
        = grub_cpu_to_le32 (-2);
 
325
    }
 
326
#endif
 
327
 
 
328
#ifdef GRUB_MACHINE_PCBIOS
 
329
  if (GRUB_KERNEL_MACHINE_LINK_ADDR + core_size > GRUB_MEMORY_MACHINE_UPPER)
 
330
    grub_util_error ("Core image is too big (%p > %p)\n",
 
331
                     GRUB_KERNEL_MACHINE_LINK_ADDR + core_size, GRUB_MEMORY_MACHINE_UPPER);
 
332
#endif
 
333
 
189
334
  grub_util_write_image (core_img, core_size, out);
190
335
  free (kernel_img);
191
336
  free (core_img);
207
352
    {"directory", required_argument, 0, 'd'},
208
353
    {"prefix", required_argument, 0, 'p'},
209
354
    {"memdisk", required_argument, 0, 'm'},
 
355
    {"config", required_argument, 0, 'c'},
210
356
    {"output", required_argument, 0, 'o'},
211
357
    {"help", no_argument, 0, 'h'},
212
358
    {"version", no_argument, 0, 'V'},
228
374
  -d, --directory=DIR     use images and modules under DIR [default=%s]\n\
229
375
  -p, --prefix=DIR        set grub_prefix directory [default=%s]\n\
230
376
  -m, --memdisk=FILE      embed FILE as a memdisk image\n\
 
377
  -c, --config=FILE       embed FILE as boot config\n\
231
378
  -o, --output=FILE       output a generated image to FILE [default=stdout]\n\
232
379
  -h, --help              display this message and exit\n\
233
380
  -V, --version           print version information and exit\n\
246
393
  char *dir = NULL;
247
394
  char *prefix = NULL;
248
395
  char *memdisk = NULL;
 
396
  char *config = NULL;
249
397
  FILE *fp = stdout;
250
398
 
251
399
  progname = "grub-mkimage";
252
 
  
 
400
 
253
401
  while (1)
254
402
    {
255
 
      int c = getopt_long (argc, argv, "d:p:m:o:hVv", options, 0);
 
403
      int c = getopt_long (argc, argv, "d:p:m:c:o:hVv", options, 0);
256
404
 
257
405
      if (c == -1)
258
406
        break;
262
410
          case 'o':
263
411
            if (output)
264
412
              free (output);
265
 
            
 
413
 
266
414
            output = xstrdup (optarg);
267
415
            break;
268
416
 
278
426
              free (memdisk);
279
427
 
280
428
            memdisk = xstrdup (optarg);
 
429
 
 
430
            if (prefix)
 
431
              free (prefix);
 
432
 
 
433
            prefix = xstrdup ("(memdisk)/boot/grub");
 
434
            break;
 
435
 
 
436
          case 'c':
 
437
            if (config)
 
438
              free (config);
 
439
 
 
440
            config = xstrdup (optarg);
281
441
            break;
282
442
 
283
443
          case 'h':
310
470
      fp = fopen (output, "wb");
311
471
      if (! fp)
312
472
        grub_util_error ("cannot open %s", output);
 
473
      free (output);
313
474
    }
314
475
 
315
 
  generate_image (dir ? : GRUB_LIBDIR, prefix ? : DEFAULT_DIRECTORY, fp, argv + optind, memdisk);
 
476
  generate_image (dir ? : GRUB_LIBDIR, prefix ? : DEFAULT_DIRECTORY, fp,
 
477
                  argv + optind, memdisk, config);
316
478
 
317
479
  fclose (fp);
318
480