f725e3
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
f725e3
From: Paulo Flabiano Smorigo <pfsmorigo@br.ibm.com>
f725e3
Date: Tue, 4 Feb 2014 19:00:55 -0200
f725e3
Subject: [PATCH] add bootpath parser for open firmware
f725e3
f725e3
It enables net boot even when there is no bootp/dhcp server.
f725e3
f725e3
* grub-core/net/drivers/ieee1275/ofnet.c: Add grub_ieee1275_parse_bootpath and
f725e3
call it at grub_ieee1275_net_config_real.
f725e3
* grub-core/kern/ieee1275/init.c: Add bootpath to grub_ieee1275_net_config.
f725e3
* include/grub/ieee1275/ieee1275.h: Likewise.
f725e3
---
f725e3
 grub-core/kern/ieee1275/init.c         |   7 +--
f725e3
 grub-core/net/drivers/ieee1275/ofnet.c | 107 ++++++++++++++++++++++++++++++++-
f725e3
 include/grub/ieee1275/ieee1275.h       |   5 +-
f725e3
 ChangeLog                              |  13 ++++
f725e3
 4 files changed, 125 insertions(+), 7 deletions(-)
f725e3
f725e3
diff --git a/grub-core/kern/ieee1275/init.c b/grub-core/kern/ieee1275/init.c
f725e3
index 89b2822e14d..d5bd74d3552 100644
f725e3
--- a/grub-core/kern/ieee1275/init.c
f725e3
+++ b/grub-core/kern/ieee1275/init.c
f725e3
@@ -80,9 +80,8 @@ grub_translate_ieee1275_path (char *filepath)
f725e3
     }
f725e3
 }
f725e3
 
f725e3
-void (*grub_ieee1275_net_config) (const char *dev,
f725e3
-				  char **device,
f725e3
-				  char **path);
f725e3
+void (*grub_ieee1275_net_config) (const char *dev, char **device, char **path,
f725e3
+                                  char *bootpath);
f725e3
 void
f725e3
 grub_machine_get_bootlocation (char **device, char **path)
f725e3
 {
f725e3
@@ -126,7 +125,7 @@ grub_machine_get_bootlocation (char **device, char **path)
f725e3
       *ptr = 0;
f725e3
 
f725e3
       if (grub_ieee1275_net_config)
f725e3
-	grub_ieee1275_net_config (canon, device, path);
f725e3
+	grub_ieee1275_net_config (canon, device, path, bootpath);
f725e3
       grub_free (dev);
f725e3
       grub_free (canon);
f725e3
     }
f725e3
diff --git a/grub-core/net/drivers/ieee1275/ofnet.c b/grub-core/net/drivers/ieee1275/ofnet.c
f725e3
index 4483c9122f7..eea8e71d300 100644
f725e3
--- a/grub-core/net/drivers/ieee1275/ofnet.c
f725e3
+++ b/grub-core/net/drivers/ieee1275/ofnet.c
f725e3
@@ -127,8 +127,111 @@ bootp_response_properties[] =
f725e3
     { .name = "bootpreply-packet", .offset = 0x2a},
f725e3
   };
f725e3
 
f725e3
+enum
f725e3
+{
f725e3
+  BOOTARGS_SERVER_ADDR,
f725e3
+  BOOTARGS_FILENAME,
f725e3
+  BOOTARGS_CLIENT_ADDR,
f725e3
+  BOOTARGS_GATEWAY_ADDR,
f725e3
+  BOOTARGS_BOOTP_RETRIES,
f725e3
+  BOOTARGS_TFTP_RETRIES,
f725e3
+  BOOTARGS_SUBNET_MASK,
f725e3
+  BOOTARGS_BLOCKSIZE
f725e3
+};
f725e3
+
f725e3
+static int
f725e3
+grub_ieee1275_parse_bootpath (const char *devpath, char *bootpath,
f725e3
+                              char **device, struct grub_net_card **card)
f725e3
+{
f725e3
+  char *args;
f725e3
+  char *comma_char = 0;
f725e3
+  char *equal_char = 0;
f725e3
+  grub_size_t field_counter = 0;
f725e3
+
f725e3
+  grub_net_network_level_address_t client_addr, gateway_addr, subnet_mask;
f725e3
+  grub_net_link_level_address_t hw_addr;
f725e3
+  grub_net_interface_flags_t flags = 0;
f725e3
+  struct grub_net_network_level_interface *inter;
f725e3
+
f725e3
+  hw_addr.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f725e3
+
f725e3
+  args = bootpath + grub_strlen (devpath) + 1;
f725e3
+  do
f725e3
+    {
f725e3
+      comma_char = grub_strchr (args, ',');
f725e3
+      if (comma_char != 0)
f725e3
+        *comma_char = 0;
f725e3
+
f725e3
+      /* Check if it's an option (like speed=auto) and not a default parameter */
f725e3
+      equal_char = grub_strchr (args, '=');
f725e3
+      if (equal_char != 0)
f725e3
+        {
f725e3
+          *equal_char = 0;
f725e3
+          grub_env_set_net_property ((*card)->name, args, equal_char + 1,
f725e3
+                                     grub_strlen(equal_char + 1));
f725e3
+          *equal_char = '=';
f725e3
+        }
f725e3
+      else
f725e3
+        {
f725e3
+          switch (field_counter++)
f725e3
+            {
f725e3
+            case BOOTARGS_SERVER_ADDR:
f725e3
+              *device = grub_xasprintf ("tftp,%s", args);
f725e3
+              if (!*device)
f725e3
+                return grub_errno;
f725e3
+              break;
f725e3
+
f725e3
+            case BOOTARGS_CLIENT_ADDR:
f725e3
+              grub_net_resolve_address (args, &client_addr);
f725e3
+              break;
f725e3
+
f725e3
+            case BOOTARGS_GATEWAY_ADDR:
f725e3
+              grub_net_resolve_address (args, &gateway_addr);
f725e3
+              break;
f725e3
+
f725e3
+            case BOOTARGS_SUBNET_MASK:
f725e3
+              grub_net_resolve_address (args, &subnet_mask);
f725e3
+              break;
f725e3
+            }
f725e3
+        }
f725e3
+      args = comma_char + 1;
f725e3
+      if (comma_char != 0)
f725e3
+        *comma_char = ',';
f725e3
+    } while (comma_char != 0);
f725e3
+
f725e3
+  if ((client_addr.ipv4 != 0) && (subnet_mask.ipv4 != 0))
f725e3
+    {
f725e3
+      grub_ieee1275_phandle_t devhandle;
f725e3
+      grub_ieee1275_finddevice (devpath, &devhandle);
f725e3
+      grub_ieee1275_get_property (devhandle, "mac-address",
f725e3
+                                  hw_addr.mac, sizeof(hw_addr.mac), 0);
f725e3
+      inter = grub_net_add_addr ((*card)->name, *card, &client_addr, &hw_addr,
f725e3
+                                 flags);
f725e3
+      grub_net_add_ipv4_local (inter,
f725e3
+                          __builtin_ctz (~grub_le_to_cpu32 (subnet_mask.ipv4)));
f725e3
+    }
f725e3
+
f725e3
+  if (gateway_addr.ipv4 != 0)
f725e3
+    {
f725e3
+      grub_net_network_level_netaddress_t target;
f725e3
+      char *rname;
f725e3
+
f725e3
+      target.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4;
f725e3
+      target.ipv4.base = 0;
f725e3
+      target.ipv4.masksize = 0;
f725e3
+      rname = grub_xasprintf ("%s:default", ((*card)->name));
f725e3
+      if (rname)
f725e3
+        grub_net_add_route_gw (rname, target, gateway_addr);
f725e3
+      else
f725e3
+        return grub_errno;
f725e3
+    }
f725e3
+
f725e3
+  return 0;
f725e3
+}
f725e3
+
f725e3
 static void
f725e3
-grub_ieee1275_net_config_real (const char *devpath, char **device, char **path)
f725e3
+grub_ieee1275_net_config_real (const char *devpath, char **device, char **path,
f725e3
+                               char *bootpath)
f725e3
 {
f725e3
   struct grub_net_card *card;
f725e3
 
f725e3
@@ -158,6 +261,8 @@ grub_ieee1275_net_config_real (const char *devpath, char **device, char **path)
f725e3
       }
f725e3
     grub_free (canon);
f725e3
 
f725e3
+    grub_ieee1275_parse_bootpath (devpath, bootpath, device, &card;;
f725e3
+
f725e3
     for (i = 0; i < ARRAY_SIZE (bootp_response_properties); i++)
f725e3
       if (grub_ieee1275_get_property_length (grub_ieee1275_chosen,
f725e3
 					     bootp_response_properties[i].name,
f725e3
diff --git a/include/grub/ieee1275/ieee1275.h b/include/grub/ieee1275/ieee1275.h
f725e3
index dc54beabb4b..8e425130327 100644
f725e3
--- a/include/grub/ieee1275/ieee1275.h
f725e3
+++ b/include/grub/ieee1275/ieee1275.h
f725e3
@@ -70,8 +70,9 @@ struct grub_ieee1275_devalias
f725e3
 };
f725e3
 
f725e3
 extern void (*EXPORT_VAR(grub_ieee1275_net_config)) (const char *dev,
f725e3
-						     char **device,
f725e3
-						     char **path);
f725e3
+                                                     char **device,
f725e3
+                                                     char **path,
f725e3
+                                                     char *bootargs);
f725e3
 
f725e3
 /* Maps a device alias to a pathname.  */
f725e3
 extern grub_ieee1275_phandle_t EXPORT_VAR(grub_ieee1275_chosen);
f725e3
diff --git a/ChangeLog b/ChangeLog
f725e3
index 63f5aa322c3..5237631583a 100644
f725e3
--- a/ChangeLog
f725e3
+++ b/ChangeLog
f725e3
@@ -1,3 +1,16 @@
f725e3
+2014-02-04  Paulo Flabiano Smorigo  <pfsmorigo@br.ibm.com>
f725e3
+
f725e3
+	Add bootpath parser for open firmware.
f725e3
+
f725e3
+	It enables net boot even when there is no bootp/dhcp server.
f725e3
+
f725e3
+	* grub-core/net/drivers/ieee1275/ofnet.c: Add grub_ieee1275_parse_bootpath
f725e3
+	and call it at grub_ieee1275_net_config_real.
f725e3
+	* grub-core/kern/ieee1275/init.c: Add bootpath to
f725e3
+	grub_ieee1275_net_config.
f725e3
+	* include/grub/ieee1275/ieee1275.h: Likewise.
f725e3
+
f725e3
+
f725e3
 2014-02-04  Paulo Flabiano Smorigo  <pfsmorigo@br.ibm.com>
f725e3
 
f725e3
 	Add grub_env_set_net_property function.