1
/* openfw.c -- Open firmware support funtions. */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
6
* This program 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.
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.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
#include <grub/misc.h>
24
#include <grub/machine/kernel.h> /* Needed ? */
25
#include <grub/ieee1275/ieee1275.h>
27
enum grub_ieee1275_parse_type
33
/* Walk children of 'devpath', calling hook for each. */
35
grub_children_iterate (char *devpath,
36
int (*hook) (struct grub_ieee1275_devalias *alias))
38
grub_ieee1275_phandle_t dev;
39
grub_ieee1275_phandle_t child;
41
grub_ieee1275_finddevice (devpath, &dev);
42
if (((signed) dev) == -1)
43
return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "Unknown device");
45
grub_ieee1275_child (dev, &child);
46
if (((signed) child) == -1)
47
return grub_error (GRUB_ERR_BAD_DEVICE, "Device has no children");
51
/* XXX: Don't use hardcoded path lengths. */
56
struct grub_ieee1275_devalias alias;
59
grub_ieee1275_get_property (child, "device_type", childtype,
60
sizeof childtype, &actual);
64
grub_ieee1275_package_to_path (child, childpath, sizeof childpath,
69
grub_ieee1275_get_property (child, "name", childname,
70
sizeof childname, &actual);
74
grub_sprintf (fullname, "%s/%s", devpath, childname);
76
alias.type = childtype;
77
alias.path = childpath;
78
alias.name = fullname;
81
while (grub_ieee1275_peer (child, &child));
86
/* Iterate through all device aliases. This function can be used to
87
find a device of a specific type. */
89
grub_devalias_iterate (int (*hook) (struct grub_ieee1275_devalias *alias))
91
grub_ieee1275_phandle_t devalias;
94
grub_ieee1275_cell_t flags;
95
struct grub_ieee1275_devalias alias;
97
if (grub_ieee1275_finddevice ("/aliases", &devalias))
102
while (grub_ieee1275_next_property (devalias, aliasname, aliasname, &flags) != -1
103
&& ((signed) flags) != -1 )
105
grub_ieee1275_phandle_t dev;
106
grub_ssize_t pathlen, typelen;
107
char *devpath, *devtype;
109
grub_dprintf ("devalias", "devalias name = %s\n", aliasname);
111
/* The property `name' is a special case we should skip. */
112
if (!grub_strcmp (aliasname, "name"))
115
grub_ieee1275_get_property_length (devalias, aliasname, &pathlen);
116
devpath = grub_malloc (pathlen);
120
if (grub_ieee1275_get_property (devalias, aliasname, devpath, pathlen,
123
grub_dprintf ("devalias", "get_property (%s) failed\n", aliasname);
128
if (grub_ieee1275_finddevice (devpath, &dev) || ((signed) dev) == -1)
130
grub_dprintf ("devalias", "finddevice (%s) failed\n", devpath);
135
grub_ieee1275_get_property_length (dev, "device_type", &typelen);
136
devtype = grub_malloc (typelen);
142
if (grub_ieee1275_get_property (dev, "device_type", devtype, typelen, &actual))
144
grub_dprintf ("devalias", "get device type failed\n");
150
alias.name = aliasname;
152
alias.type = devtype;
167
/* FIXME (sparc64) */
169
/* Call the "map" method of /chosen/mmu. */
171
grub_map (grub_addr_t phys, grub_addr_t virt, grub_uint32_t size,
175
struct grub_ieee1275_common_hdr common;
177
grub_ieee1275_ihandle_t ihandle;
184
grub_ieee1275_ihandle_t mmu;
187
grub_ieee1275_get_property (grub_ieee1275_chosen, "mmu", &mmu, sizeof mmu,
189
if (len != sizeof mmu)
192
INIT_IEEE1275_COMMON (&args.common, "call-method", 6, 1);
198
args.mode = mode; /* Format is WIMG0PP. */
200
if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
203
return args.catch_result;
208
grub_claimmap (grub_addr_t addr, grub_size_t size)
210
if (grub_ieee1275_claim (addr, size, 0, 0))
215
/* Get the device arguments of the Open Firmware node name `path'. */
217
grub_ieee1275_get_devargs (const char *path)
219
char *colon = grub_strchr (path, ':');
224
return grub_strdup (colon + 1);
227
/* Get the device path of the Open Firmware node name `path'. */
229
grub_ieee1275_get_devname (const char *path)
231
char *colon = grub_strchr (path, ':');
233
int pathlen = grub_strlen (path);
234
auto int match_alias (struct grub_ieee1275_devalias *alias);
236
int match_alias (struct grub_ieee1275_devalias *curalias)
238
/* briQ firmware can change capitalization in /chosen/bootpath. */
239
if (! grub_strncasecmp (curalias->path, path, pathlen))
241
newpath = grub_strndup (curalias->name, grub_strlen (curalias->name));
249
pathlen = (int)(colon - path);
251
/* Try to find an alias for this device. */
252
grub_devalias_iterate (match_alias);
255
newpath = grub_strdup (path);
261
grub_ieee1275_parse_args (const char *path, enum grub_ieee1275_parse_type ptype)
263
char type[64]; /* XXX check size. */
264
char *device = grub_ieee1275_get_devname (path);
265
char *args = grub_ieee1275_get_devargs (path);
267
grub_ieee1275_phandle_t dev;
270
/* Shouldn't happen. */
273
/* We need to know what type of device it is in order to parse the full
274
file path properly. */
275
if (grub_ieee1275_finddevice (device, &dev))
277
grub_error (GRUB_ERR_UNKNOWN_DEVICE, "Device %s not found\n", device);
280
if (grub_ieee1275_get_property (dev, "device_type", type, sizeof type, 0))
282
grub_error (GRUB_ERR_UNKNOWN_DEVICE,
283
"Device %s lacks a device_type property\n", device);
287
if (!grub_strcmp ("block", type))
289
/* The syntax of the device arguments is defined in the CHRP and PReP
290
IEEE1275 bindings: "[partition][,[filename]]". */
291
char *comma = grub_strchr (args, ',');
293
if (ptype == GRUB_PARSE_FILENAME)
297
char *filepath = comma + 1;
299
ret = grub_malloc (grub_strlen (filepath) + 1);
300
/* Make sure filepath has leading backslash. */
301
if (filepath[0] != '\\')
302
grub_sprintf (ret, "\\%s", filepath);
304
grub_strcpy (ret, filepath);
307
else if (ptype == GRUB_PARSE_PARTITION)
310
ret = grub_strdup (args);
312
ret = grub_strndup (args, (grub_size_t)(comma - args));
317
/* XXX Handle net devices by configuring & registering a grub_net_dev
318
here, then return its name?
319
Example path: "net:<server ip>,<file name>,<client ip>,<gateway
320
ip>,<bootp retries>,<tftp retries>". */
321
grub_printf ("Unsupported type %s for device %s\n", type, device);
331
grub_ieee1275_get_filename (const char *path)
333
return grub_ieee1275_parse_args (path, GRUB_PARSE_FILENAME);
336
/* Convert a device name from IEEE1275 syntax to GRUB syntax. */
338
grub_ieee1275_encode_devname (const char *path)
340
char *device = grub_ieee1275_get_devname (path);
341
char *partition = grub_ieee1275_parse_args (path, GRUB_PARSE_PARTITION);
346
unsigned int partno = grub_strtoul (partition, 0, 0);
348
/* Assume partno will require less than five bytes to encode. */
349
encoding = grub_malloc (grub_strlen (device) + 3 + 5);
350
grub_sprintf (encoding, "(%s,%d)", device, partno);
354
encoding = grub_malloc (grub_strlen (device) + 2);
355
grub_sprintf (encoding, "(%s)", device);
358
grub_free (partition);
367
grub_ieee1275_interpret ("reset-all", 0);
373
grub_ieee1275_interpret ("power-off", 0);