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

« back to all changes in this revision

Viewing changes to grub-core/kern/ieee1275/openfw.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
/*  openfw.c -- Open firmware support functions.  */
 
2
/*
 
3
 *  GRUB  --  GRand Unified Bootloader
 
4
 *  Copyright (C) 2003,2004,2005,2007,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
 
 
20
#include <grub/types.h>
 
21
#include <grub/err.h>
 
22
#include <grub/misc.h>
 
23
#include <grub/mm.h>
 
24
#include <grub/ieee1275/ieee1275.h>
 
25
 
 
26
enum grub_ieee1275_parse_type
 
27
{
 
28
  GRUB_PARSE_FILENAME,
 
29
  GRUB_PARSE_PARTITION,
 
30
};
 
31
 
 
32
/* Walk children of 'devpath', calling hook for each.  */
 
33
int
 
34
grub_children_iterate (char *devpath,
 
35
                       int (*hook) (struct grub_ieee1275_devalias *alias))
 
36
{
 
37
  grub_ieee1275_phandle_t dev;
 
38
  grub_ieee1275_phandle_t child;
 
39
  char *childtype, *childpath;
 
40
  char *childname;
 
41
  int ret = 0;
 
42
 
 
43
  if (grub_ieee1275_finddevice (devpath, &dev))
 
44
    return 0;
 
45
 
 
46
  if (grub_ieee1275_child (dev, &child))
 
47
    return 0;
 
48
 
 
49
  childtype = grub_malloc (IEEE1275_MAX_PROP_LEN);
 
50
  if (!childtype)
 
51
    return 0;
 
52
  childpath = grub_malloc (IEEE1275_MAX_PATH_LEN);
 
53
  if (!childpath)
 
54
    {
 
55
      grub_free (childtype);
 
56
      return 0;
 
57
    }
 
58
  childname = grub_malloc (IEEE1275_MAX_PROP_LEN);
 
59
  if (!childname)
 
60
    {
 
61
      grub_free (childpath);
 
62
      grub_free (childtype);
 
63
      return 0;
 
64
    }
 
65
 
 
66
  do
 
67
    {
 
68
      struct grub_ieee1275_devalias alias;
 
69
      grub_ssize_t actual;
 
70
 
 
71
      if (grub_ieee1275_get_property (child, "device_type", childtype,
 
72
                                      IEEE1275_MAX_PROP_LEN, &actual))
 
73
        childtype[0] = 0;
 
74
 
 
75
      if (dev == child)
 
76
        continue;
 
77
 
 
78
      if (grub_ieee1275_package_to_path (child, childpath,
 
79
                                         IEEE1275_MAX_PATH_LEN, &actual))
 
80
        continue;
 
81
 
 
82
      if (grub_strcmp (devpath, childpath) == 0)
 
83
        continue;
 
84
 
 
85
      if (grub_ieee1275_get_property (child, "name", childname,
 
86
                                      IEEE1275_MAX_PROP_LEN, &actual))
 
87
        continue;
 
88
 
 
89
      alias.type = childtype;
 
90
      alias.path = childpath;
 
91
      alias.name = childname;
 
92
      ret = hook (&alias);
 
93
      if (ret)
 
94
        break;
 
95
    }
 
96
  while (grub_ieee1275_peer (child, &child) != -1);
 
97
 
 
98
  grub_free (childname);
 
99
  grub_free (childpath);
 
100
  grub_free (childtype);
 
101
 
 
102
  return ret;
 
103
}
 
104
 
 
105
int
 
106
grub_ieee1275_devices_iterate (int (*hook) (struct grub_ieee1275_devalias *alias))
 
107
{
 
108
  auto int it_through (struct grub_ieee1275_devalias *alias);
 
109
  int it_through (struct grub_ieee1275_devalias *alias)
 
110
  {
 
111
    if (hook (alias))
 
112
      return 1;
 
113
    return grub_children_iterate (alias->path, it_through);
 
114
  }
 
115
 
 
116
  return grub_children_iterate ("/", it_through);
 
117
}
 
118
 
 
119
/* Iterate through all device aliases.  This function can be used to
 
120
   find a device of a specific type.  */
 
121
int
 
122
grub_devalias_iterate (int (*hook) (struct grub_ieee1275_devalias *alias))
 
123
{
 
124
  grub_ieee1275_phandle_t aliases;
 
125
  char *aliasname, *devtype;
 
126
  grub_ssize_t actual;
 
127
  struct grub_ieee1275_devalias alias;
 
128
  int ret = 0;
 
129
 
 
130
  if (grub_ieee1275_finddevice ("/aliases", &aliases))
 
131
    return 0;
 
132
 
 
133
  aliasname = grub_malloc (IEEE1275_MAX_PROP_LEN);
 
134
  if (!aliasname)
 
135
    return 0;
 
136
  devtype = grub_malloc (IEEE1275_MAX_PROP_LEN);
 
137
  if (!devtype)
 
138
    {
 
139
      grub_free (aliasname);
 
140
      return 0;
 
141
    }
 
142
 
 
143
  /* Find the first property.  */
 
144
  aliasname[0] = '\0';
 
145
 
 
146
  while (grub_ieee1275_next_property (aliases, aliasname, aliasname) > 0)
 
147
    {
 
148
      grub_ieee1275_phandle_t dev;
 
149
      grub_ssize_t pathlen;
 
150
      char *devpath;
 
151
 
 
152
      grub_dprintf ("devalias", "devalias name = %s\n", aliasname);
 
153
 
 
154
      grub_ieee1275_get_property_length (aliases, aliasname, &pathlen);
 
155
 
 
156
      /* The property `name' is a special case we should skip.  */
 
157
      if (!grub_strcmp (aliasname, "name"))
 
158
        continue;
 
159
 
 
160
      /* Sun's OpenBoot often doesn't zero terminate the device alias
 
161
         strings, so we will add a NULL byte at the end explicitly.  */
 
162
      pathlen += 1;
 
163
 
 
164
      devpath = grub_malloc (pathlen);
 
165
      if (! devpath)
 
166
        {
 
167
          grub_free (devtype);
 
168
          grub_free (aliasname);
 
169
          return 0;
 
170
        }
 
171
 
 
172
      if (grub_ieee1275_get_property (aliases, aliasname, devpath, pathlen,
 
173
                                      &actual))
 
174
        {
 
175
          grub_dprintf ("devalias", "get_property (%s) failed\n", aliasname);
 
176
          goto nextprop;
 
177
        }
 
178
      devpath [actual] = '\0';
 
179
 
 
180
      if (grub_ieee1275_finddevice (devpath, &dev))
 
181
        {
 
182
          grub_dprintf ("devalias", "finddevice (%s) failed\n", devpath);
 
183
          goto nextprop;
 
184
        }
 
185
 
 
186
      if (grub_ieee1275_get_property (dev, "device_type", devtype,
 
187
                                      IEEE1275_MAX_PROP_LEN, &actual))
 
188
        {
 
189
          /* NAND device don't have device_type property.  */
 
190
          devtype[0] = 0;
 
191
        }
 
192
 
 
193
      alias.name = aliasname;
 
194
      alias.path = devpath;
 
195
      alias.type = devtype;
 
196
      ret = hook (&alias);
 
197
 
 
198
nextprop:
 
199
      grub_free (devpath);
 
200
      if (ret)
 
201
        break;
 
202
    }
 
203
 
 
204
  grub_free (devtype);
 
205
  grub_free (aliasname);
 
206
  return ret;
 
207
}
 
208
 
 
209
/* Call the "map" method of /chosen/mmu.  */
 
210
int
 
211
grub_ieee1275_map (grub_addr_t phys, grub_addr_t virt, grub_size_t size,
 
212
                   grub_uint32_t mode)
 
213
{
 
214
  struct map_args {
 
215
    struct grub_ieee1275_common_hdr common;
 
216
    grub_ieee1275_cell_t method;
 
217
    grub_ieee1275_cell_t ihandle;
 
218
    grub_ieee1275_cell_t mode;
 
219
    grub_ieee1275_cell_t size;
 
220
    grub_ieee1275_cell_t virt;
 
221
#ifdef GRUB_MACHINE_SPARC64
 
222
    grub_ieee1275_cell_t phys_high;
 
223
#endif
 
224
    grub_ieee1275_cell_t phys_low;
 
225
    grub_ieee1275_cell_t catch_result;
 
226
  } args;
 
227
 
 
228
  INIT_IEEE1275_COMMON (&args.common, "call-method",
 
229
#ifdef GRUB_MACHINE_SPARC64
 
230
                        7,
 
231
#else
 
232
                        6,
 
233
#endif
 
234
                        1);
 
235
  args.method = (grub_ieee1275_cell_t) "map";
 
236
  args.ihandle = grub_ieee1275_mmu;
 
237
#ifdef GRUB_MACHINE_SPARC64
 
238
  args.phys_high = 0;
 
239
#endif
 
240
  args.phys_low = phys;
 
241
  args.virt = virt;
 
242
  args.size = size;
 
243
  args.mode = mode; /* Format is WIMG0PP.  */
 
244
  args.catch_result = (grub_ieee1275_cell_t) -1;
 
245
 
 
246
  if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
 
247
    return -1;
 
248
 
 
249
  return args.catch_result;
 
250
}
 
251
 
 
252
int
 
253
grub_claimmap (grub_addr_t addr, grub_size_t size)
 
254
{
 
255
  if (grub_ieee1275_claim (addr, size, 0, 0))
 
256
    return -1;
 
257
 
 
258
  if (! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_REAL_MODE)
 
259
      && grub_ieee1275_map (addr, addr, size, 0x00))
 
260
    {
 
261
      grub_printf ("map failed: address 0x%llx, size 0x%llx\n",
 
262
                   (long long) addr, (long long) size);
 
263
      grub_ieee1275_release (addr, size);
 
264
      return -1;
 
265
    }
 
266
 
 
267
  return 0;
 
268
}
 
269
 
 
270
/* Get the device arguments of the Open Firmware node name `path'.  */
 
271
static char *
 
272
grub_ieee1275_get_devargs (const char *path)
 
273
{
 
274
  char *colon = grub_strchr (path, ':');
 
275
 
 
276
  if (! colon)
 
277
    return 0;
 
278
 
 
279
  return grub_strdup (colon + 1);
 
280
}
 
281
 
 
282
/* Get the device path of the Open Firmware node name `path'.  */
 
283
static char *
 
284
grub_ieee1275_get_devname (const char *path)
 
285
{
 
286
  char *colon = grub_strchr (path, ':');
 
287
  char *newpath = 0;
 
288
  int pathlen = grub_strlen (path);
 
289
  auto int match_alias (struct grub_ieee1275_devalias *alias);
 
290
 
 
291
  int match_alias (struct grub_ieee1275_devalias *curalias)
 
292
    {
 
293
      /* briQ firmware can change capitalization in /chosen/bootpath.  */
 
294
      if (! grub_strncasecmp (curalias->path, path, pathlen))
 
295
        {
 
296
          newpath = grub_strdup (curalias->name);
 
297
          return 1;
 
298
        }
 
299
 
 
300
      return 0;
 
301
    }
 
302
 
 
303
  if (colon)
 
304
    pathlen = (int)(colon - path);
 
305
 
 
306
  /* Try to find an alias for this device.  */
 
307
  grub_devalias_iterate (match_alias);
 
308
 
 
309
  if (! newpath)
 
310
    newpath = grub_strndup (path, pathlen);
 
311
 
 
312
  return newpath;
 
313
}
 
314
 
 
315
static char *
 
316
grub_ieee1275_parse_args (const char *path, enum grub_ieee1275_parse_type ptype)
 
317
{
 
318
  char type[64]; /* XXX check size.  */
 
319
  char *device = grub_ieee1275_get_devname (path);
 
320
  char *args = grub_ieee1275_get_devargs (path);
 
321
  char *ret = 0;
 
322
  grub_ieee1275_phandle_t dev;
 
323
 
 
324
  if (!args)
 
325
    /* Shouldn't happen.  */
 
326
    return 0;
 
327
 
 
328
  /* We need to know what type of device it is in order to parse the full
 
329
     file path properly.  */
 
330
  if (grub_ieee1275_finddevice (device, &dev))
 
331
    {
 
332
      grub_error (GRUB_ERR_UNKNOWN_DEVICE, "device %s not found", device);
 
333
      goto fail;
 
334
    }
 
335
  if (grub_ieee1275_get_property (dev, "device_type", &type, sizeof type, 0))
 
336
    {
 
337
      grub_error (GRUB_ERR_UNKNOWN_DEVICE,
 
338
                  "device %s lacks a device_type property", device);
 
339
      goto fail;
 
340
    }
 
341
 
 
342
  if (!grub_strcmp ("block", type))
 
343
    {
 
344
      /* The syntax of the device arguments is defined in the CHRP and PReP
 
345
         IEEE1275 bindings: "[partition][,[filename]]".  */
 
346
      char *comma = grub_strchr (args, ',');
 
347
 
 
348
      if (ptype == GRUB_PARSE_FILENAME)
 
349
        {
 
350
          if (comma)
 
351
            {
 
352
              char *filepath = comma + 1;
 
353
 
 
354
              /* Make sure filepath has leading backslash.  */
 
355
              if (filepath[0] != '\\')
 
356
                ret = grub_xasprintf ("\\%s", filepath);
 
357
              else
 
358
                ret = grub_strdup (filepath);
 
359
            }
 
360
        }
 
361
      else if (ptype == GRUB_PARSE_PARTITION)
 
362
        {
 
363
          if (!comma)
 
364
            ret = grub_strdup (args);
 
365
          else
 
366
            ret = grub_strndup (args, (grub_size_t)(comma - args));
 
367
        }
 
368
    }
 
369
  else
 
370
    {
 
371
      /* XXX Handle net devices by configuring & registering a grub_net_dev
 
372
         here, then return its name?
 
373
         Example path: "net:<server ip>,<file name>,<client ip>,<gateway
 
374
         ip>,<bootp retries>,<tftp retries>".  */
 
375
      grub_printf ("Unsupported type %s for device %s\n", type, device);
 
376
    }
 
377
 
 
378
fail:
 
379
  grub_free (device);
 
380
  grub_free (args);
 
381
  return ret;
 
382
}
 
383
 
 
384
char *
 
385
grub_ieee1275_get_filename (const char *path)
 
386
{
 
387
  return grub_ieee1275_parse_args (path, GRUB_PARSE_FILENAME);
 
388
}
 
389
 
 
390
/* Convert a device name from IEEE1275 syntax to GRUB syntax.  */
 
391
char *
 
392
grub_ieee1275_encode_devname (const char *path)
 
393
{
 
394
  char *device = grub_ieee1275_get_devname (path);
 
395
  char *partition = grub_ieee1275_parse_args (path, GRUB_PARSE_PARTITION);
 
396
  char *encoding;
 
397
 
 
398
  if (partition && partition[0])
 
399
    {
 
400
      unsigned int partno = grub_strtoul (partition, 0, 0);
 
401
 
 
402
      if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_0_BASED_PARTITIONS))
 
403
        /* GRUB partition 1 is OF partition 0.  */
 
404
        partno++;
 
405
 
 
406
      encoding = grub_xasprintf ("(%s,%d)", device, partno);
 
407
    }
 
408
  else
 
409
    encoding = grub_xasprintf ("(%s)", device);
 
410
 
 
411
  grub_free (partition);
 
412
  grub_free (device);
 
413
 
 
414
  return encoding;
 
415
}
 
416
 
 
417
/* On i386, a firmware-independant grub_reboot() is provided by realmode.S.  */
 
418
#ifndef __i386__
 
419
void
 
420
grub_reboot (void)
 
421
{
 
422
  grub_ieee1275_interpret ("reset-all", 0);
 
423
  for (;;) ;
 
424
}
 
425
#endif
 
426
 
 
427
/* Resolve aliases.  */
 
428
char *
 
429
grub_ieee1275_canonicalise_devname (const char *path)
 
430
{
 
431
  struct canon_args
 
432
  {
 
433
    struct grub_ieee1275_common_hdr common;
 
434
    grub_ieee1275_cell_t path;
 
435
    grub_ieee1275_cell_t buf;
 
436
    grub_ieee1275_cell_t inlen;
 
437
    grub_ieee1275_cell_t outlen;
 
438
  }
 
439
  args;
 
440
  char *buf = NULL;
 
441
  grub_size_t bufsize = 64;
 
442
  int i;
 
443
 
 
444
  for (i = 0; i < 2; i++)
 
445
    {
 
446
      grub_free (buf);
 
447
 
 
448
      buf = grub_malloc (bufsize);
 
449
      if (!buf)
 
450
        return NULL;
 
451
 
 
452
      INIT_IEEE1275_COMMON (&args.common, "canon", 3, 1);
 
453
      args.path = (grub_ieee1275_cell_t) path;
 
454
      args.buf = (grub_ieee1275_cell_t) buf;
 
455
      args.inlen = (grub_ieee1275_cell_t) (bufsize - 1);
 
456
 
 
457
      if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
 
458
        return 0;
 
459
      if (args.outlen > bufsize - 1)
 
460
        {
 
461
          bufsize = args.outlen + 2;
 
462
          continue;
 
463
        }
 
464
      return buf;
 
465
    }
 
466
  /* Shouldn't reach here.  */
 
467
  grub_free (buf);
 
468
  return NULL;
 
469
}