Blob Blame Raw
From ae439540d2e2027ea4d9c5dcb18cdc9fc120c676 Mon Sep 17 00:00:00 2001
From: Vladimir 'phcoder' Serbinenko <phcoder@gmail.com>
Date: Sat, 2 Mar 2013 23:51:19 +0100
Subject: [PATCH 186/364] 	Move to more hookless approach in IEEE1275
 devices handling.

---
 ChangeLog                              |   4 +
 grub-core/disk/ieee1275/nand.c         |  31 ++--
 grub-core/disk/ieee1275/ofdisk.c       | 172 +++++++++---------
 grub-core/kern/ieee1275/openfw.c       | 308 ++++++++++++++++++---------------
 grub-core/net/drivers/ieee1275/ofnet.c |  23 +--
 grub-core/term/ieee1275/escc.c         |  41 ++---
 grub-core/term/ieee1275/serial.c       |  73 ++++----
 grub-core/video/ieee1275.c             |  26 +--
 include/grub/ieee1275/ieee1275.h       |  37 ++--
 9 files changed, 376 insertions(+), 339 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 3231b4b..f107544 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2013-03-02  Vladimir Serbinenko  <phcoder@gmail.com>
 
+	Move to more hookless approach in IEEE1275 devices handling.
+
+2013-03-02  Vladimir Serbinenko  <phcoder@gmail.com>
+
 	* grub-core/kern/term.c (grub_term_normal_color),
 	(grub_term_highlight_color): Add back lost defaults.
 
diff --git a/grub-core/disk/ieee1275/nand.c b/grub-core/disk/ieee1275/nand.c
index b2844b1..30ea0f2 100644
--- a/grub-core/disk/ieee1275/nand.c
+++ b/grub-core/disk/ieee1275/nand.c
@@ -36,22 +36,29 @@ static int
 grub_nand_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
 		   grub_disk_pull_t pull)
 {
-  auto int dev_iterate (struct grub_ieee1275_devalias *alias);
-  int dev_iterate (struct grub_ieee1275_devalias *alias)
-  {
-    if (grub_strcmp (alias->name, "nand") == 0)
-      {
-	hook (alias->name, hook_data);
-	return 1;
-      }
-    
-    return 0;
-  }
+  static int have_nand = -1;
 
   if (pull != GRUB_DISK_PULL_NONE)
     return 0;
 
-  return grub_devalias_iterate (dev_iterate);
+  if (have_nand == -1)
+    {
+      struct grub_ieee1275_devalias alias;
+
+      have_nand = 0;
+      FOR_IEEE1275_DEVALIASES(alias)
+	if (grub_strcmp (alias->name, "nand") == 0)
+	  {
+	    have_nand = 1;
+	    break;
+	  }
+      grub_ieee1275_devalias_free (&alias);
+    }
+
+  if (have_nand)
+    return hook ("nand", hook_data);
+
+  return 0;
 }
 
 static grub_err_t
diff --git a/grub-core/disk/ieee1275/ofdisk.c b/grub-core/disk/ieee1275/ofdisk.c
index 2130cb1..1d4de90 100644
--- a/grub-core/disk/ieee1275/ofdisk.c
+++ b/grub-core/disk/ieee1275/ofdisk.c
@@ -114,107 +114,113 @@ ofdisk_hash_add (char *devpath, char *curcan)
 }
 
 static void
-scan (void)
+dev_iterate_real (const char *name, const char *path)
 {
-  auto int dev_iterate_real (const char *name, const char *path);
-
-  int dev_iterate_real (const char *name, const char *path)
-    {
-      struct ofdisk_hash_ent *op;
+  struct ofdisk_hash_ent *op;
 
-      grub_dprintf ("disk", "disk name = %s, path = %s\n", name,
-		    path);
+  grub_dprintf ("disk", "disk name = %s, path = %s\n", name,
+		path);
 
-      op = ofdisk_hash_find (path);
-      if (!op)
+  op = ofdisk_hash_find (path);
+  if (!op)
+    {
+      char *name_dup = grub_strdup (name);
+      char *can = grub_strdup (path);
+      if (!name_dup || !can)
 	{
-	  char *name_dup = grub_strdup (name);
-	  char *can = grub_strdup (path);
-	  if (!name_dup || !can)
-	    {
-	      grub_errno = GRUB_ERR_NONE;
-	      grub_free (name_dup);
-	      grub_free (can);
-	      return 0;
-	    }
-	  op = ofdisk_hash_add (name_dup, can);
+	  grub_errno = GRUB_ERR_NONE;
+	  grub_free (name_dup);
+	  grub_free (can);
+	  return;
 	}
-      return 0;
+      op = ofdisk_hash_add (name_dup, can);
     }
+  return;
+}
 
-  auto int dev_iterate_alias (struct grub_ieee1275_devalias *alias);
-  int dev_iterate_alias (struct grub_ieee1275_devalias *alias)
-  {
-    if (grub_strcmp (alias->type, "block") != 0)
-      return 0;
-    return dev_iterate_real (alias->name, alias->path);
-  }
-
-  auto int dev_iterate (struct grub_ieee1275_devalias *alias);
-  int dev_iterate (struct grub_ieee1275_devalias *alias)
-  {
-    if (grub_strcmp (alias->type, "vscsi") == 0)
+static void
+dev_iterate (const struct grub_ieee1275_devalias *alias)
+{
+  if (grub_strcmp (alias->type, "vscsi") == 0)
+    {
+      static grub_ieee1275_ihandle_t ihandle;
+      struct set_color_args
       {
-	static grub_ieee1275_ihandle_t ihandle;
-	struct set_color_args
-	{
-	  struct grub_ieee1275_common_hdr common;
-	  grub_ieee1275_cell_t method;
-	  grub_ieee1275_cell_t ihandle;
-	  grub_ieee1275_cell_t catch_result;
-	  grub_ieee1275_cell_t nentries;
-	  grub_ieee1275_cell_t table;
-	}
-	args;
-	char *buf, *bufptr;
-	unsigned i;
+	struct grub_ieee1275_common_hdr common;
+	grub_ieee1275_cell_t method;
+	grub_ieee1275_cell_t ihandle;
+	grub_ieee1275_cell_t catch_result;
+	grub_ieee1275_cell_t nentries;
+	grub_ieee1275_cell_t table;
+      }
+      args;
+      char *buf, *bufptr;
+      unsigned i;
 
-	if (grub_ieee1275_open (alias->path, &ihandle))
-	  return 0;
+      if (grub_ieee1275_open (alias->path, &ihandle))
+	return;
     
-	INIT_IEEE1275_COMMON (&args.common, "call-method", 2, 3);
-	args.method = (grub_ieee1275_cell_t) "vscsi-report-luns";
-	args.ihandle = ihandle;
-	args.table = 0;
-	args.nentries = 0;
+      INIT_IEEE1275_COMMON (&args.common, "call-method", 2, 3);
+      args.method = (grub_ieee1275_cell_t) "vscsi-report-luns";
+      args.ihandle = ihandle;
+      args.table = 0;
+      args.nentries = 0;
 
-	if (IEEE1275_CALL_ENTRY_FN (&args) == -1 || args.catch_result)
-	  {
-	    grub_ieee1275_close (ihandle);
-	    return 0;
-	  }
+      if (IEEE1275_CALL_ENTRY_FN (&args) == -1 || args.catch_result)
+	{
+	  grub_ieee1275_close (ihandle);
+	  return;
+	}
 
-	buf = grub_malloc (grub_strlen (alias->path) + 32);
-	if (!buf)
-	  return 0;
-	bufptr = grub_stpcpy (buf, alias->path);
+      buf = grub_malloc (grub_strlen (alias->path) + 32);
+      if (!buf)
+	return;
+      bufptr = grub_stpcpy (buf, alias->path);
 
-	for (i = 0; i < args.nentries; i++)
-	  {
-	    grub_uint64_t *ptr;
+      for (i = 0; i < args.nentries; i++)
+	{
+	  grub_uint64_t *ptr;
 
-	    ptr = *(grub_uint64_t **) (args.table + 4 + 8 * i);
-	    while (*ptr)
-	      {
-		grub_snprintf (bufptr, 32, "/disk@%" PRIxGRUB_UINT64_T, *ptr++);
-		if (dev_iterate_real (buf, buf))
-		  return 1;
-	      }
-	  }
-	grub_ieee1275_close (ihandle);
-	grub_free (buf);
-	return 0;
-      }
+	  ptr = *(grub_uint64_t **) (args.table + 4 + 8 * i);
+	  while (*ptr)
+	    {
+	      grub_snprintf (bufptr, 32, "/disk@%" PRIxGRUB_UINT64_T, *ptr++);
+	      dev_iterate_real (buf, buf);
+	    }
+	}
+      grub_ieee1275_close (ihandle);
+      grub_free (buf);
+      return;
+    }
 
-    if (!grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_NO_TREE_SCANNING_FOR_DISKS)
-	&& grub_strcmp (alias->type, "block") == 0)
-      return dev_iterate_real (alias->path, alias->path);
+  if (!grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_NO_TREE_SCANNING_FOR_DISKS)
+      && grub_strcmp (alias->type, "block") == 0)
+    {
+      dev_iterate_real (alias->path, alias->path);
+      return;
+    }
+
+  {
+    struct grub_ieee1275_devalias child;
 
-    return grub_children_iterate (alias->path, dev_iterate);
+    FOR_IEEE1275_DEVCHILDREN(alias->path, child)
+      dev_iterate (&child);
   }
+}
+
+static void
+scan (void)
+{
+  struct grub_ieee1275_devalias alias;
+  FOR_IEEE1275_DEVALIASES(alias)
+    {
+      if (grub_strcmp (alias.type, "block") != 0)
+	continue;
+      dev_iterate_real (alias.name, alias.path);
+    }
 
-  grub_devalias_iterate (dev_iterate_alias);
-  grub_children_iterate ("/", dev_iterate);
+  FOR_IEEE1275_DEVCHILDREN("/", alias)
+    dev_iterate (&alias);
 }
 
 static int
diff --git a/grub-core/kern/ieee1275/openfw.c b/grub-core/kern/ieee1275/openfw.c
index 40abaa3..90c092c 100644
--- a/grub-core/kern/ieee1275/openfw.c
+++ b/grub-core/kern/ieee1275/openfw.c
@@ -32,184 +32,227 @@ enum grub_ieee1275_parse_type
   GRUB_PARSE_DEVICE_TYPE
 };
 
-/* Walk children of 'devpath', calling hook for each.  */
-int
-grub_children_iterate (const char *devpath,
-		       int (*hook) (struct grub_ieee1275_devalias *alias))
+static int
+fill_alias (struct grub_ieee1275_devalias *alias)
 {
-  grub_ieee1275_phandle_t dev;
-  grub_ieee1275_phandle_t child;
-  char *childtype, *childpath;
-  char *childname;
-  int ret = 0;
+  grub_ssize_t actual;
 
-  if (grub_ieee1275_finddevice (devpath, &dev))
+  if (grub_ieee1275_get_property (alias->phandle, "device_type", alias->type,
+				  IEEE1275_MAX_PROP_LEN, &actual))
+    alias->type[0] = 0;
+
+  if (alias->parent_dev == alias->phandle)
     return 0;
 
-  if (grub_ieee1275_child (dev, &child))
+  if (grub_ieee1275_package_to_path (alias->phandle, alias->path,
+				     IEEE1275_MAX_PATH_LEN, &actual))
     return 0;
 
-  childtype = grub_malloc (IEEE1275_MAX_PROP_LEN);
-  if (!childtype)
+  if (grub_strcmp (alias->parent_path, alias->path) == 0)
     return 0;
-  childpath = grub_malloc (IEEE1275_MAX_PATH_LEN);
-  if (!childpath)
+
+  if (grub_ieee1275_get_property (alias->phandle, "name", alias->name,
+				  IEEE1275_MAX_PROP_LEN, &actual))
+    return 0;
+  grub_dprintf ("devalias", "device path=%s\n", alias->path);
+  return 1;
+}
+
+void
+grub_ieee1275_devalias_free (struct grub_ieee1275_devalias *alias)
+{
+  grub_free (alias->name);
+  grub_free (alias->type);
+  grub_free (alias->path);
+  grub_free (alias->parent_path);
+  alias->name = 0;
+  alias->type = 0;
+  alias->path = 0;
+  alias->parent_path = 0;
+  alias->phandle = GRUB_IEEE1275_PHANDLE_INVALID;
+}
+
+void
+grub_ieee1275_children_peer (struct grub_ieee1275_devalias *alias)
+{
+  while (grub_ieee1275_peer (alias->phandle, &alias->phandle) != -1)
+    if (fill_alias (alias))
+      return;
+  grub_ieee1275_devalias_free (alias);
+}
+
+void
+grub_ieee1275_children_first (const char *devpath,
+			      struct grub_ieee1275_devalias *alias)
+{
+  grub_ieee1275_phandle_t dev;
+
+  grub_dprintf ("devalias", "iterating children of %s\n",
+		devpath);
+
+  alias->name = 0;
+  alias->path = 0;
+  alias->parent_path = 0;
+  alias->type = 0;
+
+  if (grub_ieee1275_finddevice (devpath, &dev))
+    return;
+
+  if (grub_ieee1275_child (dev, &alias->phandle))
+    return;
+
+  alias->type = grub_malloc (IEEE1275_MAX_PROP_LEN);
+  if (!alias->type)
+    return;
+  alias->path = grub_malloc (IEEE1275_MAX_PATH_LEN);
+  if (!alias->path)
     {
-      grub_free (childtype);
-      return 0;
+      grub_free (alias->type);
+      return;
     }
-  childname = grub_malloc (IEEE1275_MAX_PROP_LEN);
-  if (!childname)
+  alias->parent_path = grub_strdup (devpath);
+  if (!alias->parent_path)
     {
-      grub_free (childpath);
-      grub_free (childtype);
-      return 0;
+      grub_free (alias->path);
+      grub_free (alias->type);
+      return;
     }
 
-  do
+  alias->name = grub_malloc (IEEE1275_MAX_PROP_LEN);
+  if (!alias->name)
     {
-      struct grub_ieee1275_devalias alias;
-      grub_ssize_t actual;
-
-      if (grub_ieee1275_get_property (child, "device_type", childtype,
-				      IEEE1275_MAX_PROP_LEN, &actual))
-	childtype[0] = 0;
-
-      if (dev == child)
-	continue;
-
-      if (grub_ieee1275_package_to_path (child, childpath,
-					 IEEE1275_MAX_PATH_LEN, &actual))
-	continue;
-
-      if (grub_strcmp (devpath, childpath) == 0)
-	continue;
+      grub_free (alias->path);
+      grub_free (alias->type);
+      grub_free (alias->parent_path);
+      return;
+    }
+  if (!fill_alias (alias))
+    grub_ieee1275_children_peer (alias);
+}
 
-      if (grub_ieee1275_get_property (child, "name", childname,
-				      IEEE1275_MAX_PROP_LEN, &actual))
-	continue;
+static int
+iterate_recursively (const char *path,
+		     int (*hook) (struct grub_ieee1275_devalias *alias))
+{
+  struct grub_ieee1275_devalias alias;
+  int ret = 0;
 
-      alias.type = childtype;
-      alias.path = childpath;
-      alias.name = childname;
+  FOR_IEEE1275_DEVCHILDREN(path, alias)
+    {
       ret = hook (&alias);
       if (ret)
 	break;
+      ret = iterate_recursively (alias.path, hook);
+      if (ret)
+	break;
     }
-  while (grub_ieee1275_peer (child, &child) != -1);
-
-  grub_free (childname);
-  grub_free (childpath);
-  grub_free (childtype);
-
+  grub_ieee1275_devalias_free (&alias);
   return ret;
 }
 
 int
 grub_ieee1275_devices_iterate (int (*hook) (struct grub_ieee1275_devalias *alias))
 {
-  auto int it_through (struct grub_ieee1275_devalias *alias);
-  int it_through (struct grub_ieee1275_devalias *alias)
-  {
-    if (hook (alias))
-      return 1;
-    return grub_children_iterate (alias->path, it_through);
-  }
-
-  return grub_children_iterate ("/", it_through);
+  return iterate_recursively ("/", hook);
 }
 
-/* Iterate through all device aliases.  This function can be used to
-   find a device of a specific type.  */
-int
-grub_devalias_iterate (int (*hook) (struct grub_ieee1275_devalias *alias))
+void
+grub_ieee1275_devalias_init_iterator (struct grub_ieee1275_devalias *alias)
 {
-  grub_ieee1275_phandle_t aliases;
-  char *aliasname, *devtype;
-  grub_ssize_t actual;
-  struct grub_ieee1275_devalias alias;
-  int ret = 0;
+  alias->name = 0;
+  alias->path = 0;
+  alias->parent_path = 0;
+  alias->type = 0;
 
-  if (grub_ieee1275_finddevice ("/aliases", &aliases))
-    return 0;
+  grub_dprintf ("devalias", "iterating aliases\n");
 
-  aliasname = grub_malloc (IEEE1275_MAX_PROP_LEN);
-  if (!aliasname)
-    return 0;
-  devtype = grub_malloc (IEEE1275_MAX_PROP_LEN);
-  if (!devtype)
+  if (grub_ieee1275_finddevice ("/aliases", &alias->parent_dev))
+    return;
+
+  alias->name = grub_malloc (IEEE1275_MAX_PROP_LEN);
+  if (!alias->name)
+    return;
+
+  alias->type = grub_malloc (IEEE1275_MAX_PROP_LEN);
+  if (!alias->type)
     {
-      grub_free (aliasname);
-      return 0;
+      grub_free (alias->name);
+      alias->name = 0;
+      return;
     }
 
-  /* Find the first property.  */
-  aliasname[0] = '\0';
+  alias->name[0] = '\0';
+}
 
-  while (grub_ieee1275_next_property (aliases, aliasname, aliasname) > 0)
+int
+grub_ieee1275_devalias_next (struct grub_ieee1275_devalias *alias)
+{
+  if (!alias->name)
+    return 0;
+  while (1)
     {
-      grub_ieee1275_phandle_t dev;
       grub_ssize_t pathlen;
-      char *devpath;
+      grub_ssize_t actual;
+
+      if (alias->path)
+	{
+	  grub_free (alias->path);
+	  alias->path = 0;
+	}
+      if (grub_ieee1275_next_property (alias->parent_dev, alias->name,
+				       alias->name) <= 0)
+	{
+	  grub_ieee1275_devalias_free (alias);
+	  return 0;
+	}
 
-      grub_dprintf ("devalias", "devalias name = %s\n", aliasname);
+      grub_dprintf ("devalias", "devalias name = %s\n", alias->name);
 
-      grub_ieee1275_get_property_length (aliases, aliasname, &pathlen);
+      grub_ieee1275_get_property_length (alias->parent_dev, alias->name, &pathlen);
 
       /* The property `name' is a special case we should skip.  */
-      if (!grub_strcmp (aliasname, "name"))
+      if (grub_strcmp (alias->name, "name") == 0)
 	continue;
 
       /* Sun's OpenBoot often doesn't zero terminate the device alias
 	 strings, so we will add a NULL byte at the end explicitly.  */
       pathlen += 1;
 
-      devpath = grub_malloc (pathlen + 1);
-      if (! devpath)
+      alias->path = grub_malloc (pathlen + 1);
+      if (! alias->path)
 	{
-	  grub_free (devtype);
-	  grub_free (aliasname);
+	  grub_ieee1275_devalias_free (alias);
 	  return 0;
 	}
 
-      if (grub_ieee1275_get_property (aliases, aliasname, devpath, pathlen,
-				      &actual) || actual < 0)
+      if (grub_ieee1275_get_property (alias->parent_dev, alias->name, alias->path,
+				      pathlen, &actual) || actual < 0)
 	{
-	  grub_dprintf ("devalias", "get_property (%s) failed\n", aliasname);
-	  goto nextprop;
+	  grub_dprintf ("devalias", "get_property (%s) failed\n", alias->name);
+	  grub_free (alias->path);
+	  continue;
 	}
       if (actual > pathlen)
 	actual = pathlen;
-      devpath[actual] = '\0';
-      devpath[pathlen] = '\0';
+      alias->path[actual] = '\0';
+      alias->path[pathlen] = '\0';
 
-      if (grub_ieee1275_finddevice (devpath, &dev))
+      if (grub_ieee1275_finddevice (alias->path, &alias->phandle))
 	{
-	  grub_dprintf ("devalias", "finddevice (%s) failed\n", devpath);
-	  goto nextprop;
+	  grub_dprintf ("devalias", "finddevice (%s) failed\n", alias->path);
+	  grub_free (alias->path);
+	  alias->path = 0;
+	  continue;
 	}
 
-      if (grub_ieee1275_get_property (dev, "device_type", devtype,
+      if (grub_ieee1275_get_property (alias->phandle, "device_type", alias->type,
 				      IEEE1275_MAX_PROP_LEN, &actual))
 	{
 	  /* NAND device don't have device_type property.  */
-          devtype[0] = 0;
+          alias->type[0] = 0;
 	}
-
-      alias.name = aliasname;
-      alias.path = devpath;
-      alias.type = devtype;
-      ret = hook (&alias);
-
-nextprop:
-      grub_free (devpath);
-      if (ret)
-	break;
+      return 1;
     }
-
-  grub_free (devtype);
-  grub_free (aliasname);
-  return ret;
 }
 
 /* Call the "map" method of /chosen/mmu.  */
@@ -286,37 +329,28 @@ grub_ieee1275_get_devargs (const char *path)
 }
 
 /* Get the device path of the Open Firmware node name `path'.  */
-static char *
+char *
 grub_ieee1275_get_devname (const char *path)
 {
   char *colon = grub_strchr (path, ':');
-  char *newpath = 0;
   int pathlen = grub_strlen (path);
-  auto int match_alias (struct grub_ieee1275_devalias *alias);
-
-  int match_alias (struct grub_ieee1275_devalias *curalias)
-    {
-      /* briQ firmware can change capitalization in /chosen/bootpath.  */
-      if (grub_strncasecmp (curalias->path, path, pathlen) == 0
-	  && curalias->path[pathlen] == 0)
-        {
-	  newpath = grub_strdup (curalias->name);
-	  return 1;
-	}
-
-      return 0;
-    }
-
+  struct grub_ieee1275_devalias curalias;
   if (colon)
     pathlen = (int)(colon - path);
 
   /* Try to find an alias for this device.  */
-  grub_devalias_iterate (match_alias);
-
-  if (! newpath)
-    newpath = grub_strndup (path, pathlen);
+  FOR_IEEE1275_DEVALIASES (curalias)
+    /* briQ firmware can change capitalization in /chosen/bootpath.  */
+    if (grub_strncasecmp (curalias.path, path, pathlen) == 0
+	&& curalias.path[pathlen] == 0)
+      {
+	char *newpath;
+	newpath = grub_strdup (curalias.name);
+	grub_ieee1275_devalias_free (&curalias);
+	return newpath;
+      }
 
-  return newpath;
+  return grub_strndup (path, pathlen);
 }
 
 static char *
diff --git a/grub-core/net/drivers/ieee1275/ofnet.c b/grub-core/net/drivers/ieee1275/ofnet.c
index 7e8e2a7..1acfb73 100644
--- a/grub-core/net/drivers/ieee1275/ofnet.c
+++ b/grub-core/net/drivers/ieee1275/ofnet.c
@@ -198,27 +198,6 @@ grub_ieee1275_net_config_real (const char *devpath, char **device, char **path)
   }
 }
 
-static char *
-find_alias (const char *fullname)
-{
-  char *ret = NULL;
-  auto int find_alias_hook (struct grub_ieee1275_devalias *alias);
-
-  int find_alias_hook (struct grub_ieee1275_devalias *alias)
-  {
-    if (grub_strcmp (alias->path, fullname) == 0)
-      {
-	ret = grub_strdup (alias->name);
-	return 1;
-      }
-    return 0;
-  }
-
-  grub_devalias_iterate (find_alias_hook);
-  grub_errno = GRUB_ERR_NONE;
-  return ret;
-}
-
 static int
 search_net_devices (struct grub_ieee1275_devalias *alias)
 {
@@ -308,7 +287,7 @@ search_net_devices (struct grub_ieee1275_devalias *alias)
   card->driver = NULL;
   card->data = ofdata;
   card->flags = 0;
-  shortname = find_alias (alias->path);
+  shortname = grub_ieee1275_get_devname (alias->path);
   card->name = grub_xasprintf ("ofnet_%s", shortname ? : alias->path);
   card->idle_poll_delay_ms = 10;
   grub_free (shortname);
diff --git a/grub-core/term/ieee1275/escc.c b/grub-core/term/ieee1275/escc.c
index 6d7b636..40de3f7 100644
--- a/grub-core/term/ieee1275/escc.c
+++ b/grub-core/term/ieee1275/escc.c
@@ -200,6 +200,7 @@ struct grub_serial_driver grub_escc_driver =
   };
 
 static struct grub_escc_descriptor escc_descs[2];
+static char *macio = 0;
 
 static void
 add_device (grub_addr_t addr, int channel)
@@ -243,38 +244,34 @@ add_device (grub_addr_t addr, int channel)
   grub_serial_register (port);
 }
 
+static int
+find_macio (struct grub_ieee1275_devalias *alias)
+{
+  if (grub_strcmp (alias->type, "mac-io") != 0)
+    return 0;
+  macio = grub_strdup (alias->path);
+  return 1;
+}
+
 GRUB_MOD_INIT (escc)
 {
-  char *macio = 0;
-  char *escc = 0;
   grub_uint32_t macio_addr[4];
   grub_uint32_t escc_addr[2];
   grub_ieee1275_phandle_t dev;
-
-  auto int find_macio (struct grub_ieee1275_devalias *alias);
-  auto int find_escc (struct grub_ieee1275_devalias *alias);
-
-  int find_macio (struct grub_ieee1275_devalias *alias)
-    {
-      if (grub_strcmp (alias->type, "mac-io") != 0)
-	return 0;
-      macio = grub_strdup (alias->path);
-      return 1;
-    }
-
-  int find_escc (struct grub_ieee1275_devalias *alias)
-    {
-      if (grub_strcmp (alias->type, "escc") != 0)
-	return 0;
-      escc = grub_strdup (alias->path);
-      return 1;
-    }
+  struct grub_ieee1275_devalias alias;
+  char *escc = 0;
 
   grub_ieee1275_devices_iterate (find_macio);
   if (!macio)
     return;
 
-  grub_children_iterate (macio, find_escc);
+  FOR_IEEE1275_DEVCHILDREN(macio, alias)
+    if (grub_strcmp (alias.type, "escc") == 0)
+      {
+	escc = grub_strdup (alias.path);
+	break;
+      }
+  grub_ieee1275_devalias_free (&alias);
   if (!escc)
     {
       grub_free (macio);
diff --git a/grub-core/term/ieee1275/serial.c b/grub-core/term/ieee1275/serial.c
index 09a5a03..cda97d0 100644
--- a/grub-core/term/ieee1275/serial.c
+++ b/grub-core/term/ieee1275/serial.c
@@ -180,58 +180,53 @@ ofserial_hash_add (char *devpath, char *curcan)
   return p;
 }
 
-void
-grub_ofserial_init (void)
+static void
+dev_iterate_real (struct grub_ieee1275_devalias *alias,
+		  int use_name)
 {
-  auto int dev_iterate_real (struct grub_ieee1275_devalias *alias,
-			     int use_name);
-
-  int dev_iterate_real (struct grub_ieee1275_devalias *alias,
-			int use_name)
-    {
-      struct ofserial_hash_ent *op;
+  struct ofserial_hash_ent *op;
 
-      if (grub_strcmp (alias->type, "serial") != 0)
-	return 0;
+  if (grub_strcmp (alias->type, "serial") != 0)
+    return;
 
-      grub_dprintf ("serial", "serial name = %s, path = %s\n", alias->name,
-		    alias->path);
+  grub_dprintf ("serial", "serial name = %s, path = %s\n", alias->name,
+		alias->path);
 
-      op = ofserial_hash_find (alias->path);
-      if (!op)
+  op = ofserial_hash_find (alias->path);
+  if (!op)
+    {
+      char *name = grub_strdup (use_name ? alias->name : alias->path);
+      char *can = grub_strdup (alias->path);
+      if (!name || !can)
 	{
-	  char *name = grub_strdup (use_name ? alias->name : alias->path);
-	  char *can = grub_strdup (alias->path);
-	  if (!name || !can)
-	    {
-	      grub_errno = GRUB_ERR_NONE;
-	      grub_free (name);
-	      grub_free (can);
-	      return 0;
-	    }
-	  op = ofserial_hash_add (name, can);
+	  grub_errno = GRUB_ERR_NONE;
+	  grub_free (name);
+	  grub_free (can);
+	  return;
 	}
-      return 0;
+      op = ofserial_hash_add (name, can);
     }
+  return;
+}
 
-  auto int dev_iterate_alias (struct grub_ieee1275_devalias *alias);
-  int dev_iterate_alias (struct grub_ieee1275_devalias *alias)
-  {
-    return dev_iterate_real (alias, 1);
-  }
-
-  auto int dev_iterate (struct grub_ieee1275_devalias *alias);
-  int dev_iterate (struct grub_ieee1275_devalias *alias)
-  {
-    return dev_iterate_real (alias, 0);
-  }
+static int
+dev_iterate (struct grub_ieee1275_devalias *alias)
+{
+  dev_iterate_real (alias, 0);
+  return 0;
+}
 
+void
+grub_ofserial_init (void)
+{
   unsigned i;
   grub_err_t err;
+  struct grub_ieee1275_devalias alias;
 
-  grub_devalias_iterate (dev_iterate_alias);
-  grub_ieee1275_devices_iterate (dev_iterate);
+  FOR_IEEE1275_DEVALIASES(alias)
+    dev_iterate_real (&alias, 1);
 
+  grub_ieee1275_devices_iterate (dev_iterate);
   
   for (i = 0; i < ARRAY_SIZE (ofserial_hash); i++)
     {
diff --git a/grub-core/video/ieee1275.c b/grub-core/video/ieee1275.c
index 84f9b89..93feeb5 100644
--- a/grub-core/video/ieee1275.c
+++ b/grub-core/video/ieee1275.c
@@ -54,22 +54,22 @@ set_video_mode (unsigned width __attribute__ ((unused)),
   /* TODO */
 }
 
+static int
+find_display_hook (struct grub_ieee1275_devalias *alias)
+{
+  if (grub_strcmp (alias->type, "display") == 0)
+    {
+      grub_dprintf ("video", "Found display %s\n", alias->path);
+      display = grub_strdup (alias->path);
+      return 1;
+    }
+  return 0;
+}
+
 static void
 find_display (void)
 {
-  auto int hook (struct grub_ieee1275_devalias *alias);
-  int hook (struct grub_ieee1275_devalias *alias)
-  {
-    if (grub_strcmp (alias->type, "display") == 0)
-      {
-	grub_dprintf ("video", "Found display %s\n", alias->path);
-	display = grub_strdup (alias->path);
-	return 1;
-      }
-    return 0;
-  }
-  
-  grub_ieee1275_devices_iterate (hook);
+  grub_ieee1275_devices_iterate (find_display_hook);
 }
 
 static grub_err_t
diff --git a/include/grub/ieee1275/ieee1275.h b/include/grub/ieee1275/ieee1275.h
index ee9b707..1e8ba6f 100644
--- a/include/grub/ieee1275/ieee1275.h
+++ b/include/grub/ieee1275/ieee1275.h
@@ -24,13 +24,6 @@
 #include <grub/types.h>
 #include <grub/machine/ieee1275.h>
 
-struct grub_ieee1275_devalias
-{
-  char *name;
-  char *path;
-  char *type;
-};
-
 struct grub_ieee1275_mem_region
 {
   unsigned int start;
@@ -64,6 +57,18 @@ struct grub_ieee1275_common_hdr
 typedef grub_uint32_t grub_ieee1275_ihandle_t;
 typedef grub_uint32_t grub_ieee1275_phandle_t;
 
+#define GRUB_IEEE1275_PHANDLE_INVALID  ((grub_ieee1275_phandle_t) -1)
+
+struct grub_ieee1275_devalias
+{
+  char *name;
+  char *path;
+  char *type;
+  char *parent_path;
+  grub_ieee1275_phandle_t phandle;
+  grub_ieee1275_phandle_t parent_dev;
+};
+
 extern void (*EXPORT_VAR(grub_ieee1275_net_config)) (const char *dev,
 						     char **device,
 						     char **path);
@@ -192,10 +197,6 @@ int EXPORT_FUNC(grub_ieee1275_set_color) (grub_ieee1275_ihandle_t ihandle,
 int EXPORT_FUNC(grub_ieee1275_milliseconds) (grub_uint32_t *msecs);
 
 
-int EXPORT_FUNC(grub_devalias_iterate)
-     (int (*hook) (struct grub_ieee1275_devalias *alias));
-int EXPORT_FUNC(grub_children_iterate) (const char *devpath,
-     int (*hook) (struct grub_ieee1275_devalias *alias));
 grub_err_t EXPORT_FUNC(grub_claimmap) (grub_addr_t addr, grub_size_t size);
 
 int
@@ -210,5 +211,19 @@ int EXPORT_FUNC(grub_ieee1275_devices_iterate) (int (*hook)
 char *EXPORT_FUNC(grub_ieee1275_get_aliasdevname) (const char *path);
 char *EXPORT_FUNC(grub_ieee1275_canonicalise_devname) (const char *path);
 char *EXPORT_FUNC(grub_ieee1275_get_device_type) (const char *path);
+char *EXPORT_FUNC(grub_ieee1275_get_devname) (const char *path);
+
+void EXPORT_FUNC(grub_ieee1275_devalias_init_iterator) (struct grub_ieee1275_devalias *alias);
+void EXPORT_FUNC(grub_ieee1275_devalias_free) (struct grub_ieee1275_devalias *alias);
+int EXPORT_FUNC(grub_ieee1275_devalias_next) (struct grub_ieee1275_devalias *alias);
+void EXPORT_FUNC(grub_ieee1275_children_peer) (struct grub_ieee1275_devalias *alias);
+void EXPORT_FUNC(grub_ieee1275_children_first) (const char *devpath,
+						struct grub_ieee1275_devalias *alias);
+
+#define FOR_IEEE1275_DEVALIASES(alias) for (grub_ieee1275_devalias_init_iterator (&(alias)); grub_ieee1275_devalias_next (&(alias));)
+
+#define FOR_IEEE1275_DEVCHILDREN(devpath, alias) for (grub_ieee1275_children_first ((devpath), &(alias)); \
+						      (alias).name;	\
+						      grub_ieee1275_children_peer (&(alias)))
 
 #endif /* ! GRUB_IEEE1275_HEADER */
-- 
1.8.1.4