Netbooting like PXE

Nathan Lynch ntl at pobox.com
Tue Oct 24 02:07:36 EST 2006


Paul Nasrat wrote:
> On Fri, 2006-10-20 at 10:56 +0200, Benoit Guillon wrote:
> > Hello,
> > 
> > I currently use yaboot to boot diskless boards, but the booting 
> > configuration can be different from one node to another.
> > 
> > Thus, an interesting evolution of yaboot would be that it can grab a 
> > configuration file whose name is built from its mac address first, and 
> > fallback to the default yaboot.conf file if the mac-file does not exist. 
> > It would behave like PXE clients, or like some X terminals like NCD.
> > 
> > Please find attached a patch that implements it, just to show that the 
> > changes are very small. I can of course send the complete source files, 
> > but I guess that you would code it in a more clean way :-)
> > 
> > What do you think?
> 
> I think Nathan also had a patch to do this more fully supporting
> pxelinux fall through.
> 
> Nathan can you send that to list and we can discuss both patches.

Okay, here it is... it's very old, I doubt it applies cleanly and it's
probably buggy.  Enjoy :-)

This is a combination of three patches (the first two for adding
prom_getproplen and ARRAY_SIZE) for getting the pxelinux config file
search working.

Seems to work fine on power5.


 include/prom.h   |    1
 include/yaboot.h |    2
 second/prom.c    |    6 ++
 second/yaboot.c  |  129 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 138 insertions(+)

Index: working/second/prom.c
===================================================================
--- working.orig/second/prom.c	2005-08-16 16:03:09.309499000 -0400
+++ working/second/prom.c	2005-08-16 16:08:08.517992000 -0400
@@ -154,6 +154,12 @@ prom_getprop (prom_handle pack, char *na
 }
 
 int
+prom_getproplen(prom_handle pack, const char *name)
+{
+     return (int)call_prom("getproplen", 2, 1, pack, name);
+}
+
+int
 prom_get_chosen (char *name, void *mem, int len)
 {
      return prom_getprop (prom_chosen, name, mem, len);
Index: working/include/prom.h
===================================================================
--- working.orig/include/prom.h	2001-12-01 04:58:10.000000000 -0500
+++ working/include/prom.h	2005-08-16 16:06:26.996614000 -0400
@@ -91,6 +91,7 @@ void prom_map (void *phys, void *virt, i
 prom_handle prom_finddevice (char *name);
 prom_handle prom_findpackage (char *path);
 int prom_getprop (prom_handle dev, char *name, void *buf, int len);
+int prom_getproplen(prom_handle, const char *);
 int prom_get_devtype (char *device);
 
 /* misc */
Index: working/include/yaboot.h
===================================================================
--- working.orig/include/yaboot.h	2005-08-16 18:48:20.916105000 -0400
+++ working/include/yaboot.h	2005-08-16 18:48:58.836146000 -0400
@@ -59,4 +59,6 @@ extern char bootdevice[];
 extern char *bootpath;
 extern int bootpartition;
 
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
+
 #endif
Index: working/second/yaboot.c
===================================================================
--- working.orig/second/yaboot.c	2005-08-16 19:14:02.889160000 -0400
+++ working/second/yaboot.c	2005-08-16 20:28:05.680489000 -0400
@@ -428,6 +428,128 @@ bail:
      return result;
 }
 
+/*
+ * "bootp-response" is the property name which is specified in
+ * the recommended practice doc for obp-tftp. However, pmac
+ * provides a "dhcp-response" property and chrp provides a
+ * "bootpreply-packet" property.  The latter appears to begin the
+ * bootp packet at offset 0x2a in the property for some reason.
+ */
+
+struct bootp_property_offset {
+     char *name; /* property name */
+     int offset; /* offset into property where bootp packet occurs */
+};
+static const struct bootp_property_offset bootp_response_properties[] = {
+     { .name = "bootp-response", .offset = 0 },
+     { .name = "dhcp-response", .offset = 0 },
+     { .name = "bootpreply-packet", .offset = 0x2a },
+};
+
+struct bootp_packet {
+     u8 op, htype, hlen, hops;
+     u32 xid;
+     u16 secs, flags;
+     u32 ciaddr, yiaddr, siaddr, giaddr;
+     unsigned char chaddr[16];
+     unsigned char sname[64];
+     unsigned char file[128];
+     /* vendor options go here if we need them */
+};
+
+/*
+ * Search for config file by MAC address, then by IP address.
+ * Basically copying pxelinux's algorithm.
+ * http://syslinux.zytor.com/pxe.php#config
+ */
+static int load_my_config_file(struct boot_fspec_t *orig_fspec)
+{
+     void *bootp_response = NULL;
+     char *propname;
+     struct bootp_packet *packet;
+     int i = 0, size, offset = 0, rc = 0;
+     prom_handle chosen;
+     struct boot_fspec_t fspec = *orig_fspec;
+
+     chosen = prom_finddevice("/chosen");
+     if (chosen < 0)
+	  return 0;
+
+     for (i = 0; i < ARRAY_SIZE(bootp_response_properties); i++) {
+	  propname = bootp_response_properties[i].name;
+	  size = prom_getproplen(chosen, propname);
+	  if (size <= 0)
+	       continue;
+
+	  DEBUG_F("using /chosen/%s\n", propname);
+	  offset = bootp_response_properties[i].offset;
+	  break;
+     }
+
+     if (size <= 0)
+	  goto out;
+
+     if (sizeof(*packet) > size - offset) {
+	  prom_printf("Malformed %s property?\n", propname);
+	  goto out;
+     }
+
+     bootp_response = malloc(size);
+     if (!bootp_response)
+	  goto out;
+
+     if (prom_getprop(chosen, propname, bootp_response, size) < 0)
+	  goto out;
+
+     packet = bootp_response + offset;
+
+     /*
+      * First, try to match on mac address with the hardware type
+      * prepended.
+      */
+
+     /* 3 chars per byte in chaddr + 2 chars for htype + \0 */
+     fspec.file = malloc(packet->hlen * 3 + 2 + 1);
+     if (!fspec.file)
+	  goto out;
+
+     sprintf(fspec.file, "%02x", packet->htype);
+
+     for (i = 0; i < packet->hlen; i++) {
+	  char tmp[4];
+	  sprintf(tmp, "-%02x", packet->chaddr[i]);
+	  strcat(fspec.file, tmp);
+     }
+
+     rc = load_config_file(&fspec);
+     if (rc)
+	  goto out;
+
+
+     /*
+      * Now try to match on IP.
+      */
+     free(fspec.file);
+     fspec.file = malloc(9);
+     sprintf(fspec.file, "%08X", packet->yiaddr);
+
+     while (strlen(fspec.file)) {
+	  rc = load_config_file(&fspec);
+	  if (rc)
+	       goto out;
+	  /* Chop one digit off the end, try again */
+	  fspec.file[strlen(fspec.file) - 1] = '\0';
+     }
+
+ out:
+     if (rc) /* modify original only on success */
+	  orig_fspec->file = fspec.file;
+     else
+	  free(fspec.file);
+     free(bootp_response);
+     return rc;
+}
+
 void maintabfunc (void)
 {
      if (useconf) {
@@ -1515,6 +1637,13 @@ yaboot_main(void)
 
      useconf = load_config_file(&boot);
 
+     /*
+      * If we're doing a netboot and the default config file hasn't
+      * been found yet, look for one which matches our MAC address.
+      */
+     if (!useconf && prom_get_devtype(boot.dev) == FILE_DEVICE_NET)
+	  useconf = load_my_config_file(&boot);
+
      prom_printf("Welcome to yaboot version " VERSION "\n");
      prom_printf("Enter \"help\" to get some basic usage information\n");
 





More information about the Yaboot-devel mailing list