~darkmuggle-deactivatedaccount/ubuntu/quantal/grub2/fix-872244

« back to all changes in this revision

Viewing changes to util/i386/pc/grub-mkimage.c

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2006-01-05 15:20:40 UTC
  • mto: (17.3.1 squeeze) (1.9.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20060105152040-b72i5pq1a82z22yi
Tags: upstream-1.92
ImportĀ upstreamĀ versionĀ 1.92

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) 2002,2003,2004,2005  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 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program 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, write to the Free Software
 
18
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
19
 */
 
20
 
 
21
#include <config.h>
 
22
#include <grub/types.h>
 
23
#include <grub/machine/boot.h>
 
24
#include <grub/machine/kernel.h>
 
25
#include <grub/kernel.h>
 
26
#include <grub/disk.h>
 
27
#include <grub/util/misc.h>
 
28
#include <grub/util/resolve.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
#if defined(HAVE_LZO_LZO1X_H)
 
39
# include <lzo/lzo1x.h>
 
40
#elif defined(HAVE_LZO1X_H)
 
41
# include <lzo1x.h>
 
42
#endif
 
43
 
 
44
static void
 
45
compress_kernel (char *kernel_img, size_t kernel_size,
 
46
                 char **core_img, size_t *core_size)
 
47
{
 
48
  lzo_uint size;
 
49
  char *wrkmem;
 
50
  
 
51
  grub_util_info ("kernel_img=%p, kernel_size=0x%x", kernel_img, kernel_size);
 
52
  if (kernel_size < GRUB_KERNEL_MACHINE_RAW_SIZE)
 
53
    grub_util_error ("the core image is too small");
 
54
  
 
55
  if (lzo_init () != LZO_E_OK)
 
56
    grub_util_error ("cannot initialize LZO");
 
57
 
 
58
  *core_img = xmalloc (kernel_size + kernel_size / 64 + 16 + 3);
 
59
  wrkmem = xmalloc (LZO1X_999_MEM_COMPRESS);
 
60
 
 
61
  memcpy (*core_img, kernel_img, GRUB_KERNEL_MACHINE_RAW_SIZE);
 
62
  
 
63
  grub_util_info ("compressing the core image");
 
64
  if (lzo1x_999_compress (kernel_img + GRUB_KERNEL_MACHINE_RAW_SIZE,
 
65
                          kernel_size - GRUB_KERNEL_MACHINE_RAW_SIZE,
 
66
                          *core_img + GRUB_KERNEL_MACHINE_RAW_SIZE,
 
67
                          &size, wrkmem)
 
68
      != LZO_E_OK)
 
69
    grub_util_error ("cannot compress the kernel image");
 
70
 
 
71
  free (wrkmem);
 
72
 
 
73
  *core_size = (size_t) size + GRUB_KERNEL_MACHINE_RAW_SIZE;
 
74
}
 
75
 
 
76
static void
 
77
generate_image (const char *dir, FILE *out, char *mods[])
 
78
{
 
79
  grub_addr_t module_addr = 0;
 
80
  char *kernel_img, *boot_img, *core_img;
 
81
  size_t kernel_size, boot_size, total_module_size, core_size;
 
82
  char *kernel_path, *boot_path;
 
83
  unsigned num;
 
84
  size_t offset;
 
85
  struct grub_util_path_list *path_list, *p, *next;
 
86
  struct grub_module_info *modinfo;
 
87
 
 
88
  path_list = grub_util_resolve_dependencies (dir, "moddep.lst", mods);
 
89
 
 
90
  kernel_path = grub_util_get_path (dir, "kernel.img");
 
91
  kernel_size = grub_util_get_image_size (kernel_path);
 
92
 
 
93
  total_module_size = sizeof (struct grub_module_info);
 
94
  for (p = path_list; p; p = p->next)
 
95
    total_module_size += (grub_util_get_image_size (p->name)
 
96
                          + sizeof (struct grub_module_header));
 
97
 
 
98
  grub_util_info ("the total module size is 0x%x", total_module_size);
 
99
 
 
100
  kernel_img = xmalloc (kernel_size + total_module_size);
 
101
  grub_util_load_image (kernel_path, kernel_img);
 
102
 
 
103
  /* Fill in the grub_module_info structure.  */
 
104
  modinfo = (struct grub_module_info *) (kernel_img + kernel_size);
 
105
  modinfo->magic = GRUB_MODULE_MAGIC;
 
106
  modinfo->offset = sizeof (struct grub_module_info);
 
107
  modinfo->size = total_module_size;
 
108
 
 
109
  offset = kernel_size + sizeof (struct grub_module_info);
 
110
  for (p = path_list; p; p = p->next)
 
111
    {
 
112
      struct grub_module_header *header;
 
113
      size_t mod_size;
 
114
 
 
115
      mod_size = grub_util_get_image_size (p->name);
 
116
      
 
117
      header = (struct grub_module_header *) (kernel_img + offset);
 
118
      header->offset = grub_cpu_to_le32 (sizeof (*header));
 
119
      header->size = grub_cpu_to_le32 (mod_size + sizeof (*header));
 
120
      
 
121
      grub_util_load_image (p->name, kernel_img + offset + sizeof (*header));
 
122
 
 
123
      offset += sizeof (*header) + mod_size;
 
124
    }
 
125
 
 
126
  compress_kernel (kernel_img, kernel_size + total_module_size,
 
127
                   &core_img, &core_size);
 
128
 
 
129
  grub_util_info ("the core size is 0x%x", core_size);
 
130
  
 
131
  num = ((core_size + GRUB_DISK_SECTOR_SIZE - 1) >> GRUB_DISK_SECTOR_BITS);
 
132
  if (num > 0xffff)
 
133
    grub_util_error ("the core image is too big");
 
134
 
 
135
  boot_path = grub_util_get_path (dir, "diskboot.img");
 
136
  boot_size = grub_util_get_image_size (boot_path);
 
137
  if (boot_size != GRUB_DISK_SECTOR_SIZE)
 
138
    grub_util_error ("diskboot.img is not one sector size");
 
139
  
 
140
  boot_img = grub_util_read_image (boot_path);
 
141
  
 
142
  /* i386 is a little endian architecture.  */
 
143
  *((grub_uint16_t *) (boot_img + GRUB_DISK_SECTOR_SIZE
 
144
                       - GRUB_BOOT_MACHINE_LIST_SIZE + 4))
 
145
    = grub_cpu_to_le16 (num);
 
146
 
 
147
  grub_util_write_image (boot_img, boot_size, out);
 
148
  free (boot_img);
 
149
  free (boot_path);
 
150
  
 
151
  module_addr = (path_list
 
152
                 ? (GRUB_BOOT_MACHINE_KERNEL_ADDR + GRUB_DISK_SECTOR_SIZE
 
153
                    + kernel_size)
 
154
                 : 0);
 
155
 
 
156
  grub_util_info ("the first module address is 0x%x", module_addr);
 
157
  *((grub_uint32_t *) (core_img + GRUB_KERNEL_MACHINE_TOTAL_MODULE_SIZE))
 
158
    = grub_cpu_to_le32 (total_module_size);
 
159
  *((grub_uint32_t *) (core_img + GRUB_KERNEL_MACHINE_KERNEL_IMAGE_SIZE))
 
160
    = grub_cpu_to_le32 (kernel_size);
 
161
  *((grub_uint32_t *) (core_img + GRUB_KERNEL_MACHINE_COMPRESSED_SIZE))
 
162
    = grub_cpu_to_le32 (core_size - GRUB_KERNEL_MACHINE_RAW_SIZE);
 
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
      next = path_list->next;
 
172
      free ((void *) path_list->name);
 
173
      free (path_list);
 
174
      path_list = next;
 
175
    }
 
176
}
 
177
 
 
178
 
 
179
 
 
180
static struct option options[] =
 
181
  {
 
182
    {"directory", required_argument, 0, 'd'},
 
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 ``grub-mkimage --help'' for more information.\n");
 
195
  else
 
196
    printf ("\
 
197
Usage: grub-mkimage [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
  -o, --output=FILE       output a generated image to FILE [default=stdout]\n\
 
203
  -h, --help              display this message and exit\n\
 
204
  -V, --version           print version information and exit\n\
 
205
  -v, --verbose           print verbose messages\n\
 
206
\n\
 
207
Report bugs to <%s>.\n\
 
208
", GRUB_DATADIR, PACKAGE_BUGREPORT);
 
209
 
 
210
  exit (status);
 
211
}
 
212
 
 
213
int
 
214
main (int argc, char *argv[])
 
215
{
 
216
  char *output = 0;
 
217
  char *dir = 0;
 
218
  FILE *fp = stdout;
 
219
 
 
220
  progname = "grub-mkimage";
 
221
  
 
222
  while (1)
 
223
    {
 
224
      int c = getopt_long (argc, argv, "d: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
            
 
235
            output = xstrdup (optarg);
 
236
            break;
 
237
 
 
238
          case 'd':
 
239
            if (dir)
 
240
              free (dir);
 
241
 
 
242
            dir = xstrdup (optarg);
 
243
            break;
 
244
 
 
245
          case 'h':
 
246
            usage (0);
 
247
            break;
 
248
 
 
249
          case 'V':
 
250
            printf ("grub-mkimage (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
 
251
            return 0;
 
252
 
 
253
          case 'v':
 
254
            verbosity++;
 
255
            break;
 
256
 
 
257
          default:
 
258
            usage (1);
 
259
            break;
 
260
          }
 
261
    }
 
262
 
 
263
  if (output)
 
264
    {
 
265
      fp = fopen (output, "wb");
 
266
      if (! fp)
 
267
        grub_util_error ("cannot open %s", output);
 
268
    }
 
269
 
 
270
  generate_image (dir ? : GRUB_DATADIR, fp, argv + optind);
 
271
 
 
272
  fclose (fp);
 
273
 
 
274
  if (dir)
 
275
    free (dir);
 
276
 
 
277
  return 0;
 
278
}