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

« back to all changes in this revision

Viewing changes to util/sparc64/ieee1275/grub-setup.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-setup.c - make GRUB usable */
2
 
/*
3
 
 *  GRUB  --  GRand Unified Bootloader
4
 
 *  Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,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
 
 
20
 
#include <config.h>
21
 
#include <grub/types.h>
22
 
#include <grub/util/misc.h>
23
 
#include <grub/device.h>
24
 
#include <grub/i18n.h>
25
 
#include <grub/disk.h>
26
 
#include <grub/file.h>
27
 
#include <grub/fs.h>
28
 
#include <grub/partition.h>
29
 
#include <grub/msdos_partition.h>
30
 
#include <grub/gpt_partition.h>
31
 
#include <grub/env.h>
32
 
#include <grub/util/hostdisk.h>
33
 
#include <grub/machine/boot.h>
34
 
#include <grub/machine/kernel.h>
35
 
#include <grub/term.h>
36
 
#include <grub/util/raid.h>
37
 
#include <grub/util/lvm.h>
38
 
 
39
 
#include <grub_setup_init.h>
40
 
 
41
 
#include <stdio.h>
42
 
#include <unistd.h>
43
 
#include <string.h>
44
 
#include <stdlib.h>
45
 
#include <sys/types.h>
46
 
#include <sys/stat.h>
47
 
#include <dirent.h>
48
 
#include <grub/util/getroot.h>
49
 
 
50
 
#define _GNU_SOURCE     1
51
 
#include <getopt.h>
52
 
 
53
 
#include "progname.h"
54
 
 
55
 
/* This program fills in various fields inside of the 'boot' and 'core'
56
 
 * image files.
57
 
 *
58
 
 * The 'boot' image needs to know the OBP path name of the root
59
 
 * device.  It also needs to know the initial block number of
60
 
 * 'core' (which is 'diskboot' concatenated with 'kernel' and
61
 
 * all the modules, this is created by grub-mkimage).  This resulting
62
 
 * 'boot' image is 512 bytes in size and is placed in the second block
63
 
 * of a partition.
64
 
 *
65
 
 * The initial 'diskboot' block acts as a loader for the actual GRUB
66
 
 * kernel.  It contains the loading code and then a block list.
67
 
 *
68
 
 * The block list of 'core' starts at the end of the 'diskboot' image
69
 
 * and works it's way backwards towards the end of the code of 'diskboot'.
70
 
 *
71
 
 * We patch up the images with the necessary values and write out the
72
 
 * result.
73
 
 */
74
 
 
75
 
#define DEFAULT_BOOT_FILE       "boot.img"
76
 
#define DEFAULT_CORE_FILE       "core.img"
77
 
 
78
 
/* This is the blocklist used in the diskboot image.  */
79
 
struct boot_blocklist
80
 
{
81
 
  grub_uint64_t start;
82
 
  grub_uint32_t len;
83
 
} __attribute__ ((packed));
84
 
 
85
 
void
86
 
grub_putchar (int c)
87
 
{
88
 
  putchar (c);
89
 
}
90
 
 
91
 
int
92
 
grub_getkey (void)
93
 
{
94
 
  return -1;
95
 
}
96
 
 
97
 
struct grub_handler_class grub_term_input_class;
98
 
struct grub_handler_class grub_term_output_class;
99
 
 
100
 
void
101
 
grub_refresh (void)
102
 
{
103
 
  fflush (stdout);
104
 
}
105
 
 
106
 
static char *compute_dest_ofpath (const char *dest)
107
 
{
108
 
  int len = strlen (dest);
109
 
  char *res, *p, c;
110
 
 
111
 
  res = xmalloc (len);
112
 
  p = res;
113
 
  while ((c = *dest++) != '\0')
114
 
    {
115
 
      if (c == '\\' && *dest == ',')
116
 
        {
117
 
          *p++ = ',';
118
 
          dest++;
119
 
        }
120
 
      else
121
 
        *p++ = c;
122
 
    }
123
 
  *p++ = '\0';
124
 
 
125
 
  return res;
126
 
}
127
 
 
128
 
static void
129
 
setup (const char *prefix, const char *dir,
130
 
       const char *boot_file, const char *core_file,
131
 
       const char *root, const char *dest)
132
 
{
133
 
  char *boot_path, *core_path;
134
 
  char *boot_img, *core_img;
135
 
  size_t boot_size, core_size;
136
 
  grub_uint16_t core_sectors;
137
 
  grub_device_t root_dev, dest_dev;
138
 
  char *boot_devpath, *dest_ofpath;
139
 
  grub_disk_addr_t *kernel_sector;
140
 
  struct boot_blocklist *first_block, *block;
141
 
  char *tmp_img;
142
 
  int i;
143
 
  grub_disk_addr_t first_sector;
144
 
  grub_uint16_t last_length = GRUB_DISK_SECTOR_SIZE;
145
 
  grub_file_t file;
146
 
  FILE *fp;
147
 
  struct { grub_uint64_t start; grub_uint64_t end; } embed_region;
148
 
  embed_region.start = embed_region.end = ~0UL;
149
 
 
150
 
  auto void NESTED_FUNC_ATTR save_first_sector (grub_disk_addr_t sector,
151
 
                                                unsigned int offset,
152
 
                                                unsigned int length);
153
 
  auto void NESTED_FUNC_ATTR save_blocklists (grub_disk_addr_t sector,
154
 
                                              unsigned int offset,
155
 
                                              unsigned int length);
156
 
 
157
 
  void NESTED_FUNC_ATTR save_first_sector (grub_disk_addr_t sector,
158
 
                                           unsigned int offset,
159
 
                                           unsigned int length)
160
 
    {
161
 
      grub_util_info ("first sector is <%llu,%u,%u>", sector, offset, length);
162
 
 
163
 
      if (offset != 0 || length != GRUB_DISK_SECTOR_SIZE)
164
 
        grub_util_error ("the first sector of the core file "
165
 
                         "is not sector-aligned");
166
 
 
167
 
      first_sector = sector;
168
 
    }
169
 
 
170
 
  void NESTED_FUNC_ATTR save_blocklists (grub_disk_addr_t sector,
171
 
                                         unsigned int offset,
172
 
                                         unsigned int length)
173
 
    {
174
 
      struct boot_blocklist *prev = block + 1;
175
 
 
176
 
      grub_util_info ("saving <%llu,%u,%u>", sector, offset, length);
177
 
 
178
 
      if (offset != 0 || last_length != GRUB_DISK_SECTOR_SIZE)
179
 
        grub_util_error ("non-sector-aligned data is found in the core file");
180
 
 
181
 
      if (block != first_block
182
 
          && (grub_be_to_cpu64 (prev->start)
183
 
              + grub_be_to_cpu16 (prev->len)) == sector)
184
 
        prev->len = grub_cpu_to_be16 (grub_be_to_cpu16 (prev->len) + 1);
185
 
      else
186
 
        {
187
 
          block->start = grub_cpu_to_be64 (sector);
188
 
          block->len = grub_cpu_to_be16 (1);
189
 
 
190
 
          block--;
191
 
          if (block->len)
192
 
            grub_util_error ("the sectors of the core file are too fragmented");
193
 
        }
194
 
 
195
 
      last_length = length;
196
 
    }
197
 
 
198
 
  dest_ofpath = compute_dest_ofpath (dest);
199
 
 
200
 
  /* Read the boot image by the OS service.  */
201
 
  boot_path = grub_util_get_path (dir, boot_file);
202
 
  boot_size = grub_util_get_image_size (boot_path);
203
 
  if (boot_size != GRUB_DISK_SECTOR_SIZE)
204
 
    grub_util_error ("the size of `%s' is not %d",
205
 
                     boot_path, GRUB_DISK_SECTOR_SIZE);
206
 
  boot_img = grub_util_read_image (boot_path);
207
 
  free (boot_path);
208
 
 
209
 
  /* Set the addresses of variables in the boot image.  */
210
 
  boot_devpath = (char *) (boot_img
211
 
                           + GRUB_BOOT_AOUT_HEADER_SIZE
212
 
                           + GRUB_BOOT_MACHINE_BOOT_DEVPATH);
213
 
  kernel_sector = (grub_disk_addr_t *) (boot_img
214
 
                                        + GRUB_BOOT_AOUT_HEADER_SIZE
215
 
                                        + GRUB_BOOT_MACHINE_KERNEL_SECTOR);
216
 
 
217
 
  core_path = grub_util_get_path (dir, core_file);
218
 
  core_size = grub_util_get_image_size (core_path);
219
 
  core_sectors = ((core_size + GRUB_DISK_SECTOR_SIZE - 1)
220
 
                  >> GRUB_DISK_SECTOR_BITS);
221
 
  if (core_size < GRUB_DISK_SECTOR_SIZE)
222
 
    grub_util_error ("the size of `%s' is too small", core_path);
223
 
 
224
 
  core_img = grub_util_read_image (core_path);
225
 
  free (core_path);
226
 
 
227
 
  /* Have FIRST_BLOCK to point to the first blocklist.  */
228
 
  first_block = (struct boot_blocklist *) (core_img
229
 
                                           + GRUB_DISK_SECTOR_SIZE
230
 
                                           - sizeof (*block));
231
 
 
232
 
  grub_util_info ("root is `%s', dest is `%s', and dest_ofpath is `%s'",
233
 
                  root, dest, dest_ofpath);
234
 
 
235
 
  /* Open the root device and the destination device.  */
236
 
  grub_util_info ("Opening root");
237
 
  root_dev = grub_device_open (root);
238
 
  if (! root_dev)
239
 
    grub_util_error ("%s", grub_errmsg);
240
 
 
241
 
  grub_util_info ("Opening dest");
242
 
  dest_dev = grub_device_open (dest);
243
 
  if (! dest_dev)
244
 
    grub_util_error ("%s", grub_errmsg);
245
 
 
246
 
  grub_util_info ("setting the root device to `%s'", root);
247
 
  if (grub_env_set ("root", root) != GRUB_ERR_NONE)
248
 
    grub_util_error ("%s", grub_errmsg);
249
 
 
250
 
  /* The core image must be put on a filesystem unfortunately.  */
251
 
  grub_util_info ("will leave the core image on the filesystem");
252
 
 
253
 
  /* Make sure that GRUB reads the identical image as the OS.  */
254
 
  tmp_img = xmalloc (core_size);
255
 
  core_path = grub_util_get_path (prefix, core_file);
256
 
 
257
 
  /* It is a Good Thing to sync two times.  */
258
 
  sync ();
259
 
  sync ();
260
 
 
261
 
#define MAX_TRIES       5
262
 
 
263
 
  for (i = 0; i < MAX_TRIES; i++)
264
 
    {
265
 
      grub_util_info ("attempting to read the core image `%s' from GRUB%s",
266
 
                      core_path, (i == 0) ? "" : " again");
267
 
 
268
 
      grub_disk_cache_invalidate_all ();
269
 
 
270
 
      file = grub_file_open (core_path);
271
 
      if (file)
272
 
        {
273
 
          if (grub_file_size (file) != core_size)
274
 
            grub_util_info ("succeeded in opening the core image but the size is different (%d != %d)",
275
 
                            (int) grub_file_size (file), (int) core_size);
276
 
          else if (grub_file_read (file, tmp_img, core_size)
277
 
                   != (grub_ssize_t) core_size)
278
 
            grub_util_info ("succeeded in opening the core image but cannot read %d bytes",
279
 
                            (int) core_size);
280
 
          else if (memcmp (core_img, tmp_img, core_size) != 0)
281
 
            {
282
 
#if 0
283
 
              FILE *dump;
284
 
              FILE *dump2;
285
 
 
286
 
              dump = fopen ("dump.img", "wb");
287
 
              if (dump)
288
 
                {
289
 
                  fwrite (tmp_img, 1, core_size, dump);
290
 
                  fclose (dump);
291
 
                }
292
 
 
293
 
              dump2 = fopen ("dump2.img", "wb");
294
 
              if (dump2)
295
 
                {
296
 
                  fwrite (core_img, 1, core_size, dump2);
297
 
                  fclose (dump2);
298
 
                }
299
 
 
300
 
#endif
301
 
              grub_util_info ("succeeded in opening the core image but the data is different");
302
 
            }
303
 
          else
304
 
            {
305
 
              grub_file_close (file);
306
 
              break;
307
 
            }
308
 
 
309
 
          grub_file_close (file);
310
 
        }
311
 
      else
312
 
        grub_util_info ("couldn't open the core image");
313
 
 
314
 
      if (grub_errno)
315
 
        grub_util_info ("error message = %s", grub_errmsg);
316
 
 
317
 
      grub_errno = GRUB_ERR_NONE;
318
 
      sync ();
319
 
      sleep (1);
320
 
    }
321
 
 
322
 
  if (i == MAX_TRIES)
323
 
    grub_util_error ("cannot read `%s' correctly", core_path);
324
 
 
325
 
  /* Clean out the blocklists.  */
326
 
  block = first_block;
327
 
  while (block->len)
328
 
    {
329
 
      block->start = 0;
330
 
      block->len = 0;
331
 
 
332
 
      block--;
333
 
 
334
 
      if ((char *) block <= core_img)
335
 
        grub_util_error ("no terminator in the core image");
336
 
    }
337
 
 
338
 
  /* Now read the core image to determine where the sectors are.  */
339
 
  file = grub_file_open (core_path);
340
 
  if (! file)
341
 
    grub_util_error ("%s", grub_errmsg);
342
 
 
343
 
  file->read_hook = save_first_sector;
344
 
  if (grub_file_read (file, tmp_img, GRUB_DISK_SECTOR_SIZE)
345
 
      != GRUB_DISK_SECTOR_SIZE)
346
 
    grub_util_error ("failed to read the first sector of the core image");
347
 
 
348
 
  block = first_block;
349
 
  file->read_hook = save_blocklists;
350
 
  if (grub_file_read (file, tmp_img, core_size - GRUB_DISK_SECTOR_SIZE)
351
 
      != (grub_ssize_t) core_size - GRUB_DISK_SECTOR_SIZE)
352
 
    grub_util_error ("failed to read the rest sectors of the core image");
353
 
 
354
 
  grub_file_close (file);
355
 
 
356
 
  free (core_path);
357
 
  free (tmp_img);
358
 
 
359
 
  *kernel_sector = grub_cpu_to_be64 (first_sector);
360
 
 
361
 
  strcpy(boot_devpath, dest_ofpath);
362
 
 
363
 
  grub_util_info ("boot device path %s, prefix is %s, dest is %s",
364
 
                  boot_devpath, prefix, dest);
365
 
 
366
 
  /* Write the first two sectors of the core image onto the disk.  */
367
 
  core_path = grub_util_get_path (dir, core_file);
368
 
  grub_util_info ("opening the core image `%s'", core_path);
369
 
  fp = fopen (core_path, "r+b");
370
 
  if (! fp)
371
 
    grub_util_error ("cannot open `%s'", core_path);
372
 
 
373
 
  grub_util_write_image (core_img, GRUB_DISK_SECTOR_SIZE, fp);
374
 
  fclose (fp);
375
 
  free (core_path);
376
 
 
377
 
  /* Write the boot image onto the disk.  */
378
 
  if (grub_disk_write (dest_dev->disk, 1, 0, GRUB_DISK_SECTOR_SIZE, boot_img))
379
 
    grub_util_error ("%s", grub_errmsg);
380
 
 
381
 
  /* Sync is a Good Thing.  */
382
 
  sync ();
383
 
 
384
 
  free (core_img);
385
 
  free (boot_img);
386
 
  grub_device_close (dest_dev);
387
 
  grub_device_close (root_dev);
388
 
}
389
 
 
390
 
static struct option options[] =
391
 
  {
392
 
    {"boot-image", required_argument, 0, 'b'},
393
 
    {"core-image", required_argument, 0, 'c'},
394
 
    {"directory", required_argument, 0, 'd'},
395
 
    {"device-map", required_argument, 0, 'm'},
396
 
    {"root-device", required_argument, 0, 'r'},
397
 
    {"help", no_argument, 0, 'h'},
398
 
    {"version", no_argument, 0, 'V'},
399
 
    {"verbose", no_argument, 0, 'v'},
400
 
    {0, 0, 0, 0}
401
 
  };
402
 
 
403
 
static void
404
 
usage (int status)
405
 
{
406
 
  if (status)
407
 
    fprintf (stderr, "Try `%s --help' for more information.\n", program_name);
408
 
  else
409
 
    printf ("\
410
 
Usage: %s [OPTION]... DEVICE\n\
411
 
\n\
412
 
Set up images to boot from DEVICE.\n\
413
 
DEVICE must be a GRUB device (e.g. `(hd0,1)').\n\
414
 
\n\
415
 
  -b, --boot-image=FILE   use FILE as the boot image [default=%s]\n\
416
 
  -c, --core-image=FILE   use FILE as the core image [default=%s]\n\
417
 
  -d, --directory=DIR     use GRUB files in the directory DIR [default=%s]\n\
418
 
  -m, --device-map=FILE   use FILE as the device map [default=%s]\n\
419
 
  -r, --root-device=DEV   use DEV as the root device [default=guessed]\n\
420
 
  -h, --help              display this message and exit\n\
421
 
  -V, --version           print version information and exit\n\
422
 
  -v, --verbose           print verbose messages\n\
423
 
\n\
424
 
Report bugs to <%s>.\n\
425
 
", program_name,
426
 
            DEFAULT_BOOT_FILE, DEFAULT_CORE_FILE, DEFAULT_DIRECTORY,
427
 
            DEFAULT_DEVICE_MAP, PACKAGE_BUGREPORT);
428
 
 
429
 
  exit (status);
430
 
}
431
 
 
432
 
struct grub_setup_info
433
 
{
434
 
  char *boot_file;
435
 
  char *core_file;
436
 
  char *dir;
437
 
  char *dev_map;
438
 
  char *root_dev;
439
 
  char *prefix;
440
 
  char *dest_dev;
441
 
};
442
 
 
443
 
static void
444
 
init_info (struct grub_setup_info *gp)
445
 
{
446
 
  gp->boot_file = NULL;
447
 
  gp->core_file = NULL;
448
 
  gp->dir = NULL;
449
 
  gp->dev_map = NULL;
450
 
  gp->root_dev = NULL;
451
 
  gp->prefix = NULL;
452
 
  gp->dest_dev = NULL;
453
 
}
454
 
 
455
 
static int
456
 
parse_options (struct grub_setup_info *gp, int argc, char *argv[])
457
 
{
458
 
  while (1)
459
 
    {
460
 
      int c = getopt_long (argc, argv, "b:c:d:m:r:hVv", options, 0);
461
 
 
462
 
      if (c == -1)
463
 
        break;
464
 
      else
465
 
        switch (c)
466
 
          {
467
 
          case 'b':
468
 
            if (gp->boot_file)
469
 
              free (gp->boot_file);
470
 
 
471
 
            gp->boot_file = xstrdup (optarg);
472
 
            break;
473
 
 
474
 
          case 'c':
475
 
            if (gp->core_file)
476
 
              free (gp->core_file);
477
 
 
478
 
            gp->core_file = xstrdup (optarg);
479
 
            break;
480
 
 
481
 
          case 'd':
482
 
            if (gp->dir)
483
 
              free (gp->dir);
484
 
 
485
 
            gp->dir = xstrdup (optarg);
486
 
            break;
487
 
 
488
 
          case 'm':
489
 
            if (gp->dev_map)
490
 
              free (gp->dev_map);
491
 
 
492
 
            gp->dev_map = xstrdup (optarg);
493
 
            break;
494
 
 
495
 
          case 'r':
496
 
            if (gp->root_dev)
497
 
              free (gp->root_dev);
498
 
 
499
 
            gp->root_dev = xstrdup (optarg);
500
 
            break;
501
 
 
502
 
          case 'h':
503
 
            usage (0);
504
 
            break;
505
 
 
506
 
          case 'V':
507
 
            printf ("grub-setup (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
508
 
            return 0;
509
 
 
510
 
          case 'v':
511
 
            verbosity++;
512
 
            break;
513
 
 
514
 
          default:
515
 
            usage (1);
516
 
            break;
517
 
          }
518
 
    }
519
 
 
520
 
  if (verbosity > 1)
521
 
    grub_env_set ("debug", "all");
522
 
 
523
 
  if (optind >= argc)
524
 
    {
525
 
      fprintf (stderr, "No device is specified.\n");
526
 
      usage (1);
527
 
    }
528
 
 
529
 
  if (optind + 1 != argc)
530
 
    {
531
 
      fprintf (stderr, "Unknown extra argument `%s'.\n", argv[optind + 1]);
532
 
      usage (1);
533
 
    }
534
 
  return 1;
535
 
}
536
 
 
537
 
static char *
538
 
get_device_name (char *dev)
539
 
{
540
 
  size_t len = strlen (dev);
541
 
 
542
 
  if (dev[0] != '(' || dev[len - 1] != ')')
543
 
    return 0;
544
 
 
545
 
  dev[len - 1] = '\0';
546
 
  return dev + 1;
547
 
}
548
 
 
549
 
static void
550
 
find_dest_dev (struct grub_setup_info *gp, char *argv[])
551
 
{
552
 
  gp->dest_dev = get_device_name (argv[optind]);
553
 
  if (! gp->dest_dev)
554
 
    {
555
 
      /* Possibly, the user specified an OS device file.  */
556
 
      gp->dest_dev = grub_util_get_grub_dev (argv[optind]);
557
 
      if (! gp->dest_dev)
558
 
        {
559
 
          fprintf (stderr, "Invalid device `%s'.\n", argv[optind]);
560
 
          usage (1);
561
 
        }
562
 
      grub_util_info ("transformed OS device `%s' into GRUB device `%s'",
563
 
                      argv[optind], gp->dest_dev);
564
 
    }
565
 
  else
566
 
    {
567
 
      /* For simplicity.  */
568
 
      gp->dest_dev = xstrdup (gp->dest_dev);
569
 
      grub_util_info ("Using `%s' as GRUB device", gp->dest_dev);
570
 
    }
571
 
}
572
 
 
573
 
static void
574
 
check_root_dev (struct grub_setup_info *gp)
575
 
{
576
 
  if (gp->root_dev)
577
 
    {
578
 
      char *tmp = get_device_name (gp->root_dev);
579
 
 
580
 
      if (! tmp)
581
 
        grub_util_error ("invalid root device `%s'", gp->root_dev);
582
 
 
583
 
      tmp = xstrdup (tmp);
584
 
      free (gp->root_dev);
585
 
      gp->root_dev = tmp;
586
 
    }
587
 
  else
588
 
    {
589
 
      char *dir = gp->dir ? gp->dir : DEFAULT_DIRECTORY;
590
 
      char *root_device = grub_guess_root_device (dir);
591
 
 
592
 
      gp->root_dev = grub_util_get_grub_dev (root_device);
593
 
      if (! gp->root_dev)
594
 
        {
595
 
          grub_util_info ("guessing the root device failed, because of `%s'",
596
 
                          grub_errmsg);
597
 
          grub_util_error ("cannot guess the root device. "
598
 
                           "Specify the option `--root-device'");
599
 
        }
600
 
      grub_util_info ("guessed root device `%s' and root_dev `%s' from "
601
 
                      "dir `%s'", root_device, gp->root_dev, dir);
602
 
    }
603
 
}
604
 
 
605
 
static void
606
 
free_memory (struct grub_setup_info *gp)
607
 
{
608
 
  free (gp->boot_file);
609
 
  free (gp->core_file);
610
 
  free (gp->dir);
611
 
  free (gp->dev_map);
612
 
  free (gp->root_dev);
613
 
  free (gp->prefix);
614
 
  free (gp->dest_dev);
615
 
}
616
 
 
617
 
int
618
 
main (int argc, char *argv[])
619
 
{
620
 
  struct grub_setup_info ginfo;
621
 
 
622
 
  set_program_name (argv[0]);
623
 
 
624
 
  grub_util_init_nls ();
625
 
 
626
 
  init_info (&ginfo);
627
 
  if (!parse_options (&ginfo, argc, argv))
628
 
    return 0;
629
 
 
630
 
  /* Initialize the emulated biosdisk driver.  */
631
 
  grub_util_biosdisk_init (ginfo.dev_map ? ginfo.dev_map : DEFAULT_DEVICE_MAP);
632
 
 
633
 
  /* Initialize all modules. */
634
 
  grub_init_all ();
635
 
 
636
 
  find_dest_dev (&ginfo, argv);
637
 
 
638
 
  ginfo.prefix = grub_get_prefix (ginfo.dir ? : DEFAULT_DIRECTORY);
639
 
 
640
 
  check_root_dev (&ginfo);
641
 
 
642
 
  /* Do the real work.  */
643
 
  setup (ginfo.prefix,
644
 
         ginfo.dir ? ginfo.dir : DEFAULT_DIRECTORY,
645
 
         ginfo.boot_file ? ginfo.boot_file : DEFAULT_BOOT_FILE,
646
 
         ginfo.core_file ? ginfo.core_file : DEFAULT_CORE_FILE,
647
 
         ginfo.root_dev, ginfo.dest_dev);
648
 
 
649
 
  /* Free resources.  */
650
 
  grub_fini_all ();
651
 
 
652
 
  free_memory (&ginfo);
653
 
 
654
 
  return 0;
655
 
}