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>
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 (dev == (grub_ieee1275_phandle_t) -1)
43
return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "Unknown device");
45
grub_ieee1275_child (dev, &child);
46
if (child == (grub_ieee1275_phandle_t) -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
struct grub_ieee1275_devalias alias;
96
if (grub_ieee1275_finddevice ("/aliases", &devalias))
99
/* XXX: Is this the right way to find the first property? */
102
/* XXX: Are the while conditions correct? */
103
while (grub_ieee1275_next_property (devalias, aliasname, aliasname, &actual)
106
grub_ieee1275_phandle_t dev;
109
/* XXX: This should be large enough for any possible case. */
112
grub_ieee1275_get_property_length (devalias, aliasname, &pathlen);
114
/* The property `name' is a special case we should skip. */
115
if (!grub_strcmp (aliasname, "name"))
118
devpath = grub_malloc (pathlen);
122
if (grub_ieee1275_get_property (devalias, aliasname, devpath, pathlen,
129
if (grub_ieee1275_finddevice (devpath, &dev)
130
|| dev == (grub_ieee1275_phandle_t) -1)
136
if (grub_ieee1275_get_property (dev, "device_type", devtype, sizeof devtype,
143
alias.name = aliasname;
145
alias.type = devtype;
154
/* Call the "map" method of /chosen/mmu. */
156
grub_map (grub_addr_t phys, grub_addr_t virt, grub_uint32_t size,
160
struct grub_ieee1275_common_hdr common;
162
grub_ieee1275_ihandle_t ihandle;
169
grub_ieee1275_ihandle_t mmu;
172
grub_ieee1275_get_property (grub_ieee1275_chosen, "mmu", &mmu, sizeof mmu,
174
if (len != sizeof mmu)
177
INIT_IEEE1275_COMMON (&args.common, "call-method", 6, 1);
183
args.mode = mode; /* Format is WIMG0PP. */
185
if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
188
return args.catch_result;
192
grub_claimmap (grub_addr_t addr, grub_size_t size)
194
if (grub_ieee1275_claim (addr, size, 0, 0))
197
if (! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_REAL_MODE)
198
&& grub_map (addr, addr, size, 0x00))
200
grub_printf ("map failed: address 0x%x, size 0x%x\n", addr, size);
201
grub_ieee1275_release (addr, size);
208
/* Get the device arguments of the Open Firmware node name `path'. */
210
grub_ieee1275_get_devargs (const char *path)
212
char *colon = grub_strchr (path, ':');
217
return grub_strdup (colon + 1);
220
/* Get the device path of the Open Firmware node name `path'. */
222
grub_ieee1275_get_devname (const char *path)
224
char *colon = grub_strchr (path, ':');
226
int pathlen = grub_strlen (path);
227
auto int match_alias (struct grub_ieee1275_devalias *alias);
229
int match_alias (struct grub_ieee1275_devalias *curalias)
231
/* briQ firmware can change capitalization in /chosen/bootpath. */
232
if (! grub_strncasecmp (curalias->path, path, pathlen))
234
newpath = grub_strdup (curalias->name);
242
pathlen = (int)(colon - path);
244
/* Try to find an alias for this device. */
245
grub_devalias_iterate (match_alias);
248
newpath = grub_strndup (path, pathlen);
254
grub_ieee1275_parse_args (const char *path, enum grub_ieee1275_parse_type ptype)
256
char type[64]; /* XXX check size. */
257
char *device = grub_ieee1275_get_devname (path);
258
char *args = grub_ieee1275_get_devargs (path);
260
grub_ieee1275_phandle_t dev;
263
/* Shouldn't happen. */
266
/* We need to know what type of device it is in order to parse the full
267
file path properly. */
268
if (grub_ieee1275_finddevice (device, &dev))
270
grub_error (GRUB_ERR_UNKNOWN_DEVICE, "Device %s not found\n", device);
273
if (grub_ieee1275_get_property (dev, "device_type", &type, sizeof type, 0))
275
grub_error (GRUB_ERR_UNKNOWN_DEVICE,
276
"Device %s lacks a device_type property\n", device);
280
if (!grub_strcmp ("block", type))
282
/* The syntax of the device arguments is defined in the CHRP and PReP
283
IEEE1275 bindings: "[partition][,[filename]]". */
284
char *comma = grub_strchr (args, ',');
286
if (ptype == GRUB_PARSE_FILENAME)
290
char *filepath = comma + 1;
292
ret = grub_malloc (grub_strlen (filepath) + 1);
293
/* Make sure filepath has leading backslash. */
294
if (filepath[0] != '\\')
295
grub_sprintf (ret, "\\%s", filepath);
297
grub_strcpy (ret, filepath);
300
else if (ptype == GRUB_PARSE_PARTITION)
303
ret = grub_strdup (args);
305
ret = grub_strndup (args, (grub_size_t)(comma - args));
310
/* XXX Handle net devices by configuring & registering a grub_net_dev
311
here, then return its name?
312
Example path: "net:<server ip>,<file name>,<client ip>,<gateway
313
ip>,<bootp retries>,<tftp retries>". */
314
grub_printf ("Unsupported type %s for device %s\n", type, device);
324
grub_ieee1275_get_filename (const char *path)
326
return grub_ieee1275_parse_args (path, GRUB_PARSE_FILENAME);
329
/* Convert a device name from IEEE1275 syntax to GRUB syntax. */
331
grub_ieee1275_encode_devname (const char *path)
333
char *device = grub_ieee1275_get_devname (path);
334
char *partition = grub_ieee1275_parse_args (path, GRUB_PARSE_PARTITION);
339
unsigned int partno = grub_strtoul (partition, 0, 0);
341
/* GRUB partition numbering is 0-based. */
342
if (! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_0_BASED_PARTITIONS))
345
/* Assume partno will require less than five bytes to encode. */
346
encoding = grub_malloc (grub_strlen (device) + 3 + 5);
347
grub_sprintf (encoding, "(%s,%d)", device, partno);
351
encoding = grub_malloc (grub_strlen (device) + 2);
352
grub_sprintf (encoding, "(%s)", device);
355
grub_free (partition);
364
grub_ieee1275_interpret ("reset-all", 0);
370
grub_ieee1275_interpret ("shut-down", 0);