1
/* openfw.c -- Open firmware support functions. */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2003,2004,2005,2007,2008,2009 Free Software Foundation, Inc.
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.
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.
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/>.
20
#include <grub/types.h>
22
#include <grub/misc.h>
24
#include <grub/ieee1275/ieee1275.h>
26
enum grub_ieee1275_parse_type
32
/* Walk children of 'devpath', calling hook for each. */
34
grub_children_iterate (char *devpath,
35
int (*hook) (struct grub_ieee1275_devalias *alias))
37
grub_ieee1275_phandle_t dev;
38
grub_ieee1275_phandle_t child;
39
char *childtype, *childpath;
43
if (grub_ieee1275_finddevice (devpath, &dev))
46
if (grub_ieee1275_child (dev, &child))
49
childtype = grub_malloc (IEEE1275_MAX_PROP_LEN);
52
childpath = grub_malloc (IEEE1275_MAX_PATH_LEN);
55
grub_free (childtype);
58
childname = grub_malloc (IEEE1275_MAX_PROP_LEN);
61
grub_free (childpath);
62
grub_free (childtype);
68
struct grub_ieee1275_devalias alias;
71
if (grub_ieee1275_get_property (child, "device_type", childtype,
72
IEEE1275_MAX_PROP_LEN, &actual))
78
if (grub_ieee1275_package_to_path (child, childpath,
79
IEEE1275_MAX_PATH_LEN, &actual))
82
if (grub_strcmp (devpath, childpath) == 0)
85
if (grub_ieee1275_get_property (child, "name", childname,
86
IEEE1275_MAX_PROP_LEN, &actual))
89
alias.type = childtype;
90
alias.path = childpath;
91
alias.name = childname;
96
while (grub_ieee1275_peer (child, &child) != -1);
98
grub_free (childname);
99
grub_free (childpath);
100
grub_free (childtype);
106
grub_ieee1275_devices_iterate (int (*hook) (struct grub_ieee1275_devalias *alias))
108
auto int it_through (struct grub_ieee1275_devalias *alias);
109
int it_through (struct grub_ieee1275_devalias *alias)
113
return grub_children_iterate (alias->path, it_through);
116
return grub_children_iterate ("/", it_through);
119
/* Iterate through all device aliases. This function can be used to
120
find a device of a specific type. */
122
grub_devalias_iterate (int (*hook) (struct grub_ieee1275_devalias *alias))
124
grub_ieee1275_phandle_t aliases;
125
char *aliasname, *devtype;
127
struct grub_ieee1275_devalias alias;
130
if (grub_ieee1275_finddevice ("/aliases", &aliases))
133
aliasname = grub_malloc (IEEE1275_MAX_PROP_LEN);
136
devtype = grub_malloc (IEEE1275_MAX_PROP_LEN);
139
grub_free (aliasname);
143
/* Find the first property. */
146
while (grub_ieee1275_next_property (aliases, aliasname, aliasname) > 0)
148
grub_ieee1275_phandle_t dev;
149
grub_ssize_t pathlen;
152
grub_dprintf ("devalias", "devalias name = %s\n", aliasname);
154
grub_ieee1275_get_property_length (aliases, aliasname, &pathlen);
156
/* The property `name' is a special case we should skip. */
157
if (!grub_strcmp (aliasname, "name"))
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. */
164
devpath = grub_malloc (pathlen);
168
grub_free (aliasname);
172
if (grub_ieee1275_get_property (aliases, aliasname, devpath, pathlen,
175
grub_dprintf ("devalias", "get_property (%s) failed\n", aliasname);
178
devpath [actual] = '\0';
180
if (grub_ieee1275_finddevice (devpath, &dev))
182
grub_dprintf ("devalias", "finddevice (%s) failed\n", devpath);
186
if (grub_ieee1275_get_property (dev, "device_type", devtype,
187
IEEE1275_MAX_PROP_LEN, &actual))
189
/* NAND device don't have device_type property. */
193
alias.name = aliasname;
194
alias.path = devpath;
195
alias.type = devtype;
205
grub_free (aliasname);
209
/* Call the "map" method of /chosen/mmu. */
211
grub_ieee1275_map (grub_addr_t phys, grub_addr_t virt, grub_size_t size,
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;
224
grub_ieee1275_cell_t phys_low;
225
grub_ieee1275_cell_t catch_result;
228
INIT_IEEE1275_COMMON (&args.common, "call-method",
229
#ifdef GRUB_MACHINE_SPARC64
235
args.method = (grub_ieee1275_cell_t) "map";
236
args.ihandle = grub_ieee1275_mmu;
237
#ifdef GRUB_MACHINE_SPARC64
240
args.phys_low = phys;
243
args.mode = mode; /* Format is WIMG0PP. */
244
args.catch_result = (grub_ieee1275_cell_t) -1;
246
if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
249
return args.catch_result;
253
grub_claimmap (grub_addr_t addr, grub_size_t size)
255
if (grub_ieee1275_claim (addr, size, 0, 0))
258
if (! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_REAL_MODE)
259
&& grub_ieee1275_map (addr, addr, size, 0x00))
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);
270
/* Get the device arguments of the Open Firmware node name `path'. */
272
grub_ieee1275_get_devargs (const char *path)
274
char *colon = grub_strchr (path, ':');
279
return grub_strdup (colon + 1);
282
/* Get the device path of the Open Firmware node name `path'. */
284
grub_ieee1275_get_devname (const char *path)
286
char *colon = grub_strchr (path, ':');
288
int pathlen = grub_strlen (path);
289
auto int match_alias (struct grub_ieee1275_devalias *alias);
291
int match_alias (struct grub_ieee1275_devalias *curalias)
293
/* briQ firmware can change capitalization in /chosen/bootpath. */
294
if (! grub_strncasecmp (curalias->path, path, pathlen))
296
newpath = grub_strdup (curalias->name);
304
pathlen = (int)(colon - path);
306
/* Try to find an alias for this device. */
307
grub_devalias_iterate (match_alias);
310
newpath = grub_strndup (path, pathlen);
316
grub_ieee1275_parse_args (const char *path, enum grub_ieee1275_parse_type ptype)
318
char type[64]; /* XXX check size. */
319
char *device = grub_ieee1275_get_devname (path);
320
char *args = grub_ieee1275_get_devargs (path);
322
grub_ieee1275_phandle_t dev;
325
/* Shouldn't happen. */
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))
332
grub_error (GRUB_ERR_UNKNOWN_DEVICE, "device %s not found", device);
335
if (grub_ieee1275_get_property (dev, "device_type", &type, sizeof type, 0))
337
grub_error (GRUB_ERR_UNKNOWN_DEVICE,
338
"device %s lacks a device_type property", device);
342
if (!grub_strcmp ("block", type))
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, ',');
348
if (ptype == GRUB_PARSE_FILENAME)
352
char *filepath = comma + 1;
354
/* Make sure filepath has leading backslash. */
355
if (filepath[0] != '\\')
356
ret = grub_xasprintf ("\\%s", filepath);
358
ret = grub_strdup (filepath);
361
else if (ptype == GRUB_PARSE_PARTITION)
364
ret = grub_strdup (args);
366
ret = grub_strndup (args, (grub_size_t)(comma - args));
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);
385
grub_ieee1275_get_filename (const char *path)
387
return grub_ieee1275_parse_args (path, GRUB_PARSE_FILENAME);
390
/* Convert a device name from IEEE1275 syntax to GRUB syntax. */
392
grub_ieee1275_encode_devname (const char *path)
394
char *device = grub_ieee1275_get_devname (path);
395
char *partition = grub_ieee1275_parse_args (path, GRUB_PARSE_PARTITION);
398
if (partition && partition[0])
400
unsigned int partno = grub_strtoul (partition, 0, 0);
402
if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_0_BASED_PARTITIONS))
403
/* GRUB partition 1 is OF partition 0. */
406
encoding = grub_xasprintf ("(%s,%d)", device, partno);
409
encoding = grub_xasprintf ("(%s)", device);
411
grub_free (partition);
417
/* On i386, a firmware-independant grub_reboot() is provided by realmode.S. */
422
grub_ieee1275_interpret ("reset-all", 0);
427
/* Resolve aliases. */
429
grub_ieee1275_canonicalise_devname (const char *path)
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;
441
grub_size_t bufsize = 64;
444
for (i = 0; i < 2; i++)
448
buf = grub_malloc (bufsize);
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);
457
if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
459
if (args.outlen > bufsize - 1)
461
bufsize = args.outlen + 2;
466
/* Shouldn't reach here. */