2N/A/* openfw.c -- Open firmware support functions. */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2003,2004,2005,2007,2008,2009 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/types.h>
2N/A#include <grub/err.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/mm.h>
2N/A#include <grub/ieee1275/ieee1275.h>
2N/A#include <grub/net.h>
2N/A
2N/Aenum grub_ieee1275_parse_type
2N/A{
2N/A GRUB_PARSE_FILENAME,
2N/A GRUB_PARSE_PARTITION,
2N/A GRUB_PARSE_DEVICE,
2N/A GRUB_PARSE_DEVICE_TYPE
2N/A};
2N/A
2N/A/* Walk children of 'devpath', calling hook for each. */
2N/Aint
2N/Agrub_children_iterate (const char *devpath,
2N/A int (*hook) (struct grub_ieee1275_devalias *alias))
2N/A{
2N/A grub_ieee1275_phandle_t dev;
2N/A grub_ieee1275_phandle_t child;
2N/A char *childtype, *childpath;
2N/A char *childname;
2N/A int ret = 0;
2N/A
2N/A if (grub_ieee1275_finddevice (devpath, &dev))
2N/A return 0;
2N/A
2N/A if (grub_ieee1275_child (dev, &child))
2N/A return 0;
2N/A
2N/A childtype = grub_malloc (IEEE1275_MAX_PROP_LEN);
2N/A if (!childtype)
2N/A return 0;
2N/A childpath = grub_malloc (IEEE1275_MAX_PATH_LEN);
2N/A if (!childpath)
2N/A {
2N/A grub_free (childtype);
2N/A return 0;
2N/A }
2N/A childname = grub_malloc (IEEE1275_MAX_PROP_LEN);
2N/A if (!childname)
2N/A {
2N/A grub_free (childpath);
2N/A grub_free (childtype);
2N/A return 0;
2N/A }
2N/A
2N/A do
2N/A {
2N/A struct grub_ieee1275_devalias alias;
2N/A grub_ssize_t actual;
2N/A
2N/A if (grub_ieee1275_get_property (child, "device_type", childtype,
2N/A IEEE1275_MAX_PROP_LEN, &actual))
2N/A childtype[0] = 0;
2N/A
2N/A if (dev == child)
2N/A continue;
2N/A
2N/A if (grub_ieee1275_package_to_path (child, childpath,
2N/A IEEE1275_MAX_PATH_LEN, &actual))
2N/A continue;
2N/A
2N/A if (grub_strcmp (devpath, childpath) == 0)
2N/A continue;
2N/A
2N/A if (grub_ieee1275_get_property (child, "name", childname,
2N/A IEEE1275_MAX_PROP_LEN, &actual))
2N/A continue;
2N/A
2N/A alias.type = childtype;
2N/A alias.path = childpath;
2N/A alias.name = childname;
2N/A ret = hook (&alias);
2N/A if (ret)
2N/A break;
2N/A }
2N/A while (grub_ieee1275_peer (child, &child) != -1);
2N/A
2N/A grub_free (childname);
2N/A grub_free (childpath);
2N/A grub_free (childtype);
2N/A
2N/A return ret;
2N/A}
2N/A
2N/Aint
2N/Agrub_ieee1275_devices_iterate (int (*hook) (struct grub_ieee1275_devalias *alias))
2N/A{
2N/A auto int it_through (struct grub_ieee1275_devalias *alias);
2N/A int it_through (struct grub_ieee1275_devalias *alias)
2N/A {
2N/A if (hook (alias))
2N/A return 1;
2N/A return grub_children_iterate (alias->path, it_through);
2N/A }
2N/A
2N/A return grub_children_iterate ("/", it_through);
2N/A}
2N/A
2N/A/* Iterate through all device aliases. This function can be used to
2N/A find a device of a specific type. */
2N/Aint
2N/Agrub_devalias_iterate (int (*hook) (struct grub_ieee1275_devalias *alias))
2N/A{
2N/A grub_ieee1275_phandle_t aliases;
2N/A char *aliasname, *devtype;
2N/A grub_ssize_t actual;
2N/A struct grub_ieee1275_devalias alias;
2N/A int ret = 0;
2N/A
2N/A if (grub_ieee1275_finddevice ("/aliases", &aliases))
2N/A return 0;
2N/A
2N/A aliasname = grub_malloc (IEEE1275_MAX_PROP_LEN);
2N/A if (!aliasname)
2N/A return 0;
2N/A devtype = grub_malloc (IEEE1275_MAX_PROP_LEN);
2N/A if (!devtype)
2N/A {
2N/A grub_free (aliasname);
2N/A return 0;
2N/A }
2N/A
2N/A /* Find the first property. */
2N/A aliasname[0] = '\0';
2N/A
2N/A while (grub_ieee1275_next_property (aliases, aliasname, aliasname) > 0)
2N/A {
2N/A grub_ieee1275_phandle_t dev;
2N/A grub_ssize_t pathlen;
2N/A char *devpath;
2N/A
2N/A grub_dprintf ("devalias", "devalias name = %s\n", aliasname);
2N/A
2N/A grub_ieee1275_get_property_length (aliases, aliasname, &pathlen);
2N/A
2N/A /* The property `name' is a special case we should skip. */
2N/A if (!grub_strcmp (aliasname, "name"))
2N/A continue;
2N/A
2N/A /* Sun's OpenBoot often doesn't zero terminate the device alias
2N/A strings, so we will add a NULL byte at the end explicitly. */
2N/A pathlen += 1;
2N/A
2N/A devpath = grub_malloc (pathlen + 1);
2N/A if (! devpath)
2N/A {
2N/A grub_free (devtype);
2N/A grub_free (aliasname);
2N/A return 0;
2N/A }
2N/A
2N/A if (grub_ieee1275_get_property (aliases, aliasname, devpath, pathlen,
2N/A &actual) || actual < 0)
2N/A {
2N/A grub_dprintf ("devalias", "get_property (%s) failed\n", aliasname);
2N/A goto nextprop;
2N/A }
2N/A if (actual > pathlen)
2N/A actual = pathlen;
2N/A devpath[actual] = '\0';
2N/A devpath[pathlen] = '\0';
2N/A
2N/A if (grub_ieee1275_finddevice (devpath, &dev))
2N/A {
2N/A grub_dprintf ("devalias", "finddevice (%s) failed\n", devpath);
2N/A goto nextprop;
2N/A }
2N/A
2N/A if (grub_ieee1275_get_property (dev, "device_type", devtype,
2N/A IEEE1275_MAX_PROP_LEN, &actual))
2N/A {
2N/A /* NAND device don't have device_type property. */
2N/A devtype[0] = 0;
2N/A }
2N/A
2N/A alias.name = aliasname;
2N/A alias.path = devpath;
2N/A alias.type = devtype;
2N/A ret = hook (&alias);
2N/A
2N/Anextprop:
2N/A grub_free (devpath);
2N/A if (ret)
2N/A break;
2N/A }
2N/A
2N/A grub_free (devtype);
2N/A grub_free (aliasname);
2N/A return ret;
2N/A}
2N/A
2N/A/* Call the "map" method of /chosen/mmu. */
2N/Aint
2N/Agrub_ieee1275_map (grub_addr_t phys, grub_addr_t virt, grub_size_t size,
2N/A grub_uint32_t mode)
2N/A{
2N/A struct map_args {
2N/A struct grub_ieee1275_common_hdr common;
2N/A grub_ieee1275_cell_t method;
2N/A grub_ieee1275_cell_t ihandle;
2N/A grub_ieee1275_cell_t mode;
2N/A grub_ieee1275_cell_t size;
2N/A grub_ieee1275_cell_t virt;
2N/A#ifdef GRUB_MACHINE_SPARC64
2N/A grub_ieee1275_cell_t phys_high;
2N/A#endif
2N/A grub_ieee1275_cell_t phys_low;
2N/A grub_ieee1275_cell_t catch_result;
2N/A } args;
2N/A
2N/A INIT_IEEE1275_COMMON (&args.common, "call-method",
2N/A#ifdef GRUB_MACHINE_SPARC64
2N/A 7,
2N/A#else
2N/A 6,
2N/A#endif
2N/A 1);
2N/A args.method = (grub_ieee1275_cell_t) "map";
2N/A args.ihandle = grub_ieee1275_mmu;
2N/A#ifdef GRUB_MACHINE_SPARC64
2N/A args.phys_high = 0;
2N/A#endif
2N/A args.phys_low = phys;
2N/A args.virt = virt;
2N/A args.size = size;
2N/A args.mode = mode; /* Format is WIMG0PP. */
2N/A args.catch_result = (grub_ieee1275_cell_t) -1;
2N/A
2N/A if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
2N/A return -1;
2N/A
2N/A return args.catch_result;
2N/A}
2N/A
2N/Aint
2N/Agrub_claimmap (grub_addr_t addr, grub_size_t size)
2N/A{
2N/A if (grub_ieee1275_claim (addr, size, 0, 0))
2N/A return -1;
2N/A
2N/A if (! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_REAL_MODE)
2N/A && grub_ieee1275_map (addr, addr, size, 0x00))
2N/A {
2N/A grub_printf ("map failed: address 0x%llx, size 0x%llx\n",
2N/A (long long) addr, (long long) size);
2N/A grub_ieee1275_release (addr, size);
2N/A return -1;
2N/A }
2N/A
2N/A return 0;
2N/A}
2N/A
2N/A/* Get the device arguments of the Open Firmware node name `path'. */
2N/Astatic char *
2N/Agrub_ieee1275_get_devargs (const char *path)
2N/A{
2N/A char *colon = grub_strchr (path, ':');
2N/A
2N/A if (! colon)
2N/A return 0;
2N/A
2N/A return grub_strdup (colon + 1);
2N/A}
2N/A
2N/A/* Get the device path of the Open Firmware node name `path'. */
2N/Astatic char *
2N/Agrub_ieee1275_get_devname (const char *path)
2N/A{
2N/A char *colon = grub_strchr (path, ':');
2N/A char *newpath = 0;
2N/A int pathlen = grub_strlen (path);
2N/A auto int match_alias (struct grub_ieee1275_devalias *alias);
2N/A
2N/A int match_alias (struct grub_ieee1275_devalias *curalias)
2N/A {
2N/A /* briQ firmware can change capitalization in /chosen/bootpath. */
2N/A if (grub_strncasecmp (curalias->path, path, pathlen) == 0
2N/A && curalias->path[pathlen] == 0)
2N/A {
2N/A newpath = grub_strdup (curalias->name);
2N/A return 1;
2N/A }
2N/A
2N/A return 0;
2N/A }
2N/A
2N/A if (colon)
2N/A pathlen = (int)(colon - path);
2N/A
2N/A /* Try to find an alias for this device. */
2N/A grub_devalias_iterate (match_alias);
2N/A
2N/A if (! newpath)
2N/A newpath = grub_strndup (path, pathlen);
2N/A
2N/A return newpath;
2N/A}
2N/A
2N/Astatic char *
2N/Agrub_ieee1275_parse_args (const char *path, enum grub_ieee1275_parse_type ptype)
2N/A{
2N/A char type[64]; /* XXX check size. */
2N/A char *device = grub_ieee1275_get_devname (path);
2N/A char *ret = 0;
2N/A grub_ieee1275_phandle_t dev;
2N/A
2N/A /* We need to know what type of device it is in order to parse the full
2N/A file path properly. */
2N/A if (grub_ieee1275_finddevice (device, &dev))
2N/A {
2N/A grub_error (GRUB_ERR_UNKNOWN_DEVICE, "device %s not found", device);
2N/A goto fail;
2N/A }
2N/A if (grub_ieee1275_get_property (dev, "device_type", &type, sizeof type, 0))
2N/A {
2N/A grub_error (GRUB_ERR_UNKNOWN_DEVICE,
2N/A "device %s lacks a device_type property", device);
2N/A goto fail;
2N/A }
2N/A
2N/A switch (ptype)
2N/A {
2N/A case GRUB_PARSE_DEVICE:
2N/A ret = grub_strdup (device);
2N/A break;
2N/A case GRUB_PARSE_DEVICE_TYPE:
2N/A ret = grub_strdup (type);
2N/A break;
2N/A case GRUB_PARSE_FILENAME:
2N/A {
2N/A char *comma;
2N/A char *args;
2N/A
2N/A if (grub_strcmp ("block", type) != 0)
2N/A goto unknown;
2N/A
2N/A args = grub_ieee1275_get_devargs (path);
2N/A if (!args)
2N/A /* Shouldn't happen. */
2N/A return 0;
2N/A
2N/A /* The syntax of the device arguments is defined in the CHRP and PReP
2N/A IEEE1275 bindings: "[partition][,[filename]]". */
2N/A comma = grub_strchr (args, ',');
2N/A
2N/A if (comma)
2N/A {
2N/A char *filepath = comma + 1;
2N/A
2N/A /* Make sure filepath has leading backslash. */
2N/A if (filepath[0] != '\\')
2N/A ret = grub_xasprintf ("\\%s", filepath);
2N/A else
2N/A ret = grub_strdup (filepath);
2N/A }
2N/A grub_free (args);
2N/A }
2N/A break;
2N/A case GRUB_PARSE_PARTITION:
2N/A {
2N/A char *comma;
2N/A char *args;
2N/A
2N/A if (grub_strcmp ("block", type) != 0)
2N/A goto unknown;
2N/A
2N/A args = grub_ieee1275_get_devargs (path);
2N/A if (!args)
2N/A /* Shouldn't happen. */
2N/A return 0;
2N/A
2N/A comma = grub_strchr (args, ',');
2N/A if (!comma)
2N/A ret = grub_strdup (args);
2N/A else
2N/A ret = grub_strndup (args, (grub_size_t)(comma - args));
2N/A /* Consistently provide numbered partitions to GRUB.
2N/A OpenBOOT traditionally uses alphabetical partition
2N/A specifiers. */
2N/A if (ret[0] >= 'a' && ret[0] <= 'z')
2N/A ret[0] = '1' + (ret[0] - 'a');
2N/A grub_free (args);
2N/A }
2N/A break;
2N/A default:
2N/A unknown:
2N/A grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
2N/A "unsupported type %s for device %s", type, device);
2N/A }
2N/A
2N/Afail:
2N/A grub_free (device);
2N/A return ret;
2N/A}
2N/A
2N/Achar *
2N/Agrub_ieee1275_get_device_type (const char *path)
2N/A{
2N/A return grub_ieee1275_parse_args (path, GRUB_PARSE_DEVICE_TYPE);
2N/A}
2N/A
2N/Achar *
2N/Agrub_ieee1275_get_aliasdevname (const char *path)
2N/A{
2N/A return grub_ieee1275_parse_args (path, GRUB_PARSE_DEVICE);
2N/A}
2N/A
2N/Achar *
2N/Agrub_ieee1275_get_filename (const char *path)
2N/A{
2N/A return grub_ieee1275_parse_args (path, GRUB_PARSE_FILENAME);
2N/A}
2N/A
2N/A/* Convert a device name from IEEE1275 syntax to GRUB syntax. */
2N/Achar *
2N/Agrub_ieee1275_encode_devname (const char *path)
2N/A{
2N/A char *device = grub_ieee1275_get_devname (path);
2N/A char *partition = grub_ieee1275_parse_args (path, GRUB_PARSE_PARTITION);
2N/A char *encoding;
2N/A
2N/A if (partition && partition[0])
2N/A {
2N/A unsigned int partno = grub_strtoul (partition, 0, 0);
2N/A
2N/A if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_0_BASED_PARTITIONS))
2N/A /* GRUB partition 1 is OF partition 0. */
2N/A partno++;
2N/A
2N/A encoding = grub_xasprintf ("ieee1275/%s,%d", device, partno);
2N/A }
2N/A else
2N/A encoding = grub_strdup (device);
2N/A
2N/A grub_free (partition);
2N/A grub_free (device);
2N/A
2N/A return encoding;
2N/A}
2N/A
2N/A/* Resolve aliases. */
2N/Achar *
2N/Agrub_ieee1275_canonicalise_devname (const char *path)
2N/A{
2N/A struct canon_args
2N/A {
2N/A struct grub_ieee1275_common_hdr common;
2N/A grub_ieee1275_cell_t path;
2N/A grub_ieee1275_cell_t buf;
2N/A grub_ieee1275_cell_t inlen;
2N/A grub_ieee1275_cell_t outlen;
2N/A }
2N/A args;
2N/A char *buf = NULL;
2N/A grub_size_t bufsize = 64;
2N/A int i;
2N/A
2N/A for (i = 0; i < 2; i++)
2N/A {
2N/A grub_free (buf);
2N/A
2N/A buf = grub_malloc (bufsize);
2N/A if (!buf)
2N/A return NULL;
2N/A
2N/A INIT_IEEE1275_COMMON (&args.common, "canon", 3, 1);
2N/A args.path = (grub_ieee1275_cell_t) path;
2N/A args.buf = (grub_ieee1275_cell_t) buf;
2N/A args.inlen = (grub_ieee1275_cell_t) (bufsize - 1);
2N/A
2N/A if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
2N/A return 0;
2N/A if (args.outlen > bufsize - 1)
2N/A {
2N/A bufsize = args.outlen + 2;
2N/A continue;
2N/A }
2N/A return buf;
2N/A }
2N/A /* Shouldn't reach here. */
2N/A grub_free (buf);
2N/A return NULL;
2N/A}
2N/A