[U-Boot] [PATCH 3/3] Add the NBC + netconsole corresponding documentation

2010-11-17 Thread tristan . lelong
From: Tristan Lelong tristan.lel...@blunderer.org


Signed-off-by: Tristan Lelong tristan.lel...@blunderer.org
---
 doc/README.netconsole |   66 +
 1 files changed, 66 insertions(+), 0 deletions(-)
 create mode 100644 doc/README.netconsole

diff --git a/doc/README.netconsole b/doc/README.netconsole
new file mode 100644
index 000..167f523
--- /dev/null
+++ b/doc/README.netconsole
@@ -0,0 +1,66 @@
+NETCONSOLE  NBC README
+
+# (C) Copyright 2010
+# Tristan Lelong, tristan.lel...@blunderer.org
+#
+# See file CREDITS for list of people who contributed to this
+# project.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 2 of
+# the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+# MA 02111-1307 USA
+
+Configuration Options:
+
+   CONFIG_NETCONSOLE
+  This enable the netconsole. Netconsole provide a remote access to 
+  u-boot command line thru udp stream.
+  basically, you just set some environment variables like:
+- stdin, stdout, and stderr to nc
+- ncip to remote ip address
+  and you can connect to the board ip, port  (default) using 
+  tools/netconsole board ip addr
+
+   CONFIG_CMD_NBC
+  This enable the Net Boot Controller feature that will listen for 
+  NBC magic packet at boot time. If one of NBC packet is received, 
+  u-boot reconfigure itself automagically to use the netconsole
+  and interrupt the autoboot.
+  The NBC packet contains the new board IP and the remote ip.
+  It can also target special boards using filters on hostname and/or 
ethaddr
+  To broadcast NBC packets just use:
+  tools/sendnbc -i new board ip [options]
+  Two environment variable will allow to protect your board from receiving 
+  NBC interruptions:
+   - nbcsource: only this remote IP can interrupt autoboot using NBC
+   - nbcinhibit: if this variable is present, NBC is fully inhibited
+
+
+NOTE:
+ NBC packet has the following format:
+
+One header
+ - NBC header (5 bytes):
+- magical number   1 byte  '0xD3'
+   - ASCII header  3 bytes NBC
+   - packet size   1 byte  'X'
+
+Several Data Chunks
+ - NBC data chunks (X-5 bytes)
+   - chunk name4 bytes IP\0\0 | MAC\0 | HOST
+   - chunk size1 byte  'Y'
+   - chunk dataY bytes 
+
+The IP chunk is mandatory, other are optional
+
-- 
1.7.2.3

___
U-Boot mailing list
U-Boot@lists.denx.de
http://lists.denx.de/mailman/listinfo/u-boot


[U-Boot] [PATCH 1/3] Add support for Net Boot Controller (NBC) packet

2010-11-17 Thread tristan . lelong
From: Tristan Lelong tristan.lel...@blunderer.org

This adds the core NBC feature:
 * Integration into u-boot
 * NBC packet processing
 * NetConsole reconfigure

Signed-off-by: Tristan Lelong tristan.lel...@blunderer.org
---
 common/main.c |5 ++
 include/net.h |6 ++-
 net/Makefile  |2 +
 net/nbc.c |  143 +
 net/nbc.h |   39 +++
 net/net.c |   17 +++
 6 files changed, 211 insertions(+), 1 deletions(-)
 create mode 100644 net/nbc.c
 create mode 100644 net/nbc.h

diff --git a/common/main.c b/common/main.c
index d97ccd7..e23b204 100644
--- a/common/main.c
+++ b/common/main.c
@@ -259,6 +259,11 @@ static __inline__ int abortboot(int bootdelay)
 
putc('\n');
 
+#ifdef CONFIG_CMD_NBC
+   if (!abort)
+   abort = NBCWait ();
+#endif
+
 #ifdef CONFIG_SILENT_CONSOLE
if (abort)
gd-flags = ~GD_FLG_SILENT;
diff --git a/include/net.h b/include/net.h
index a29dafc..25e177f 100644
--- a/include/net.h
+++ b/include/net.h
@@ -359,7 +359,7 @@ extern int  NetState;   /* Network loop 
state   */
 extern int NetRestartWrap; /* Tried all network devices
*/
 #endif
 
-typedef enum { BOOTP, RARP, ARP, TFTP, DHCP, PING, DNS, NFS, CDP, NETCONS, 
SNTP } proto_t;
+typedef enum { BOOTP, RARP, ARP, TFTP, DHCP, PING, DNS, NFS, CDP, NETCONS, 
SNTP, NBC } proto_t;
 
 /* from net/net.c */
 extern charBootFile[128];  /* Boot File name   
*/
@@ -384,6 +384,10 @@ extern IPaddr_tNetNtpServerIP; /* the 
ip address to NTP*/
 extern int NetTimeOffset;  /* offset time from UTC 
*/
 #endif
 
+#if defined(CONFIG_CMD_NBC)
+extern IPaddr_tNetSrcIP;   /* Packet Src IP addr 
(0 = unknown) */
+#endif
+
 /* Initialize the network adapter */
 extern int NetLoop(proto_t);
 
diff --git a/net/Makefile b/net/Makefile
index 216d1ec..735e8c6 100644
--- a/net/Makefile
+++ b/net/Makefile
@@ -35,6 +35,8 @@ COBJS-$(CONFIG_CMD_NFS)  += nfs.o
 COBJS-$(CONFIG_CMD_RARP) += rarp.o
 COBJS-$(CONFIG_CMD_SNTP) += sntp.o
 COBJS-$(CONFIG_CMD_NET)  += tftp.o
+COBJS-$(CONFIG_CMD_NBC) += nbc.o
+
 
 COBJS  := $(COBJS-y)
 SRCS   := $(COBJS:.o=.c)
diff --git a/net/nbc.c b/net/nbc.c
new file mode 100644
index 000..e1788e4
--- /dev/null
+++ b/net/nbc.c
@@ -0,0 +1,143 @@
+/*
+ * NBC support driver
+ * Network Boot Controller
+ *
+ * Copyright (c) 2010 Tristan Lelong tristan.lel...@blunderer.org
+ *
+ */
+
+#include common.h
+#include command.h
+#include net.h
+
+#include nbc.h
+
+#ifndef CONFIG_NETCONSOLE
+#error CONFIG_NETCONSOLE should be defined so that nbc can redirect 
stdin/stdout/stderr
+#endif
+
+static char NBC_Mode_On = 0;
+
+static void NBCHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
+{
+   int cnt = 0;
+   char src_ip[16] = ;
+   char *nbcsource = NULL;
+   char ip[16] = ;
+   char mac[18] = ;
+   char hostname[255] = ;
+
+   struct _nbc_packet *nbc_pkt = (struct _nbc_packet *)pkt;
+   nbcsource = getenv (nbcsource);
+
+   /* check packet validity */
+   if (nbc_pkt-id != 0xD3)
+   goto fail;
+   if (strncmp (nbc_pkt-head, NBC, 3))
+   goto fail;
+   if (nbc_pkt-size != len)
+   goto fail;
+
+   sprintf (src_ip, %lu.%lu.%lu.%lu, ((NetSrcIP  0)  0xFF),
+((NetSrcIP  8)  0xFF), ((NetSrcIP  16)  0xFF),
+((NetSrcIP  24)  0xFF));
+
+   if (nbcsource  strcmp (nbcsource, src_ip))
+   goto fail;
+
+   for (cnt = 0; cnt  (nbc_pkt-size - NBC_HEADER_SIZE);) {
+   struct _nbc_chunk *chk =
+   (struct _nbc_chunk *)(pkt + NBC_HEADER_SIZE + cnt);
+   if (strncmp (chk-name, IP\0\0, 4) == 0) {
+   if (chk-size != NBC_CHK_IP_SIZE)
+   goto fail;
+   unsigned char *pkt_ip = (unsigned char *)chk-data;
+   sprintf (ip, %d.%d.%d.%d, pkt_ip[0], pkt_ip[1],
+pkt_ip[2], pkt_ip[3]);
+   } else if (strncmp (chk-name, MAC\0, 4) == 0) {
+   if (chk-size != NBC_CHK_MAC_SIZE)
+   goto fail;
+   unsigned char *pkt_mac = (unsigned char *)chk-data;
+   sprintf (mac, %02X:%02X:%02X:%02X:%02X:%02X,
+pkt_mac[0], pkt_mac[1], pkt_mac[2], pkt_mac[3],
+pkt_mac[4], pkt_mac[5]);
+   } else if (strncmp (chk-name, HOST, 4) == 0) {
+   unsigned char *pkt_hostname =
+   (unsigned char *)chk-data;
+   if (chk-size = NBC_CHK_HOSTNAME_SIZE)
+   goto fail;
+   strncpy (hostname, pkt_hostname, chk-size

[U-Boot] [PATCH 0/3] Net Boot Controller

2010-11-17 Thread tristan . lelong
From: Tristan Lelong tristan.lel...@blunderer.org

This patch allow to interrupt u-boot autoboot from remote PC and 
auto-reconfigure a netconsole redirected to this remote PC

Tristan Lelong (3):
  Add support for Net Boot Controller (NBC) packet
  Add sendnbc tool to broadcast NBC magic packet
  Add the NBC + netconsole corresponding documentation

 common/main.c |5 +
 doc/README.netconsole |   66 +++
 include/net.h |6 +-
 net/Makefile  |2 +
 net/nbc.c |  143 
 net/nbc.h |   39 +
 net/net.c |   17 
 tools/Makefile|6 ++
 tools/sendnbc.c   |  219 +
 9 files changed, 502 insertions(+), 1 deletions(-)
 create mode 100644 doc/README.netconsole
 create mode 100644 net/nbc.c
 create mode 100644 net/nbc.h
 create mode 100644 tools/sendnbc.c

-- 
1.7.2.3

___
U-Boot mailing list
U-Boot@lists.denx.de
http://lists.denx.de/mailman/listinfo/u-boot


[U-Boot] [PATCH 2/3] Add sendnbc tool to broadcast NBC magic packet

2010-11-17 Thread tristan . lelong
From: Tristan Lelong tristan.lel...@blunderer.org

This tool send NBC packet filled with data provided on command line
It can be used in combination with netconsole by calling:
BOARD_IP=192.168.0.2 ./sendnbc -i $BOARD_IP  ./netconsole $BOARD_IP

Signed-off-by: Tristan Lelong tristan.lel...@blunderer.org
---
 tools/Makefile  |6 ++
 tools/sendnbc.c |  219 +++
 2 files changed, 225 insertions(+), 0 deletions(-)
 create mode 100644 tools/sendnbc.c

diff --git a/tools/Makefile b/tools/Makefile
index 619c9f2..e1719fc 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -45,6 +45,7 @@ CONFIG_CMD_LOADS = y
 CONFIG_CMD_NET = y
 CONFIG_INCA_IP = y
 CONFIG_NETCONSOLE = y
+CONFIG_CMD_NBC = y
 CONFIG_SHA1_CHECK_UB_IMG = y
 endif
 
@@ -64,6 +65,7 @@ BIN_FILES-$(CONFIG_CMD_LOADS) += img2srec$(SFX)
 BIN_FILES-$(CONFIG_INCA_IP) += inca-swap-bytes$(SFX)
 BIN_FILES-y += mkimage$(SFX)
 BIN_FILES-$(CONFIG_NETCONSOLE) += ncb$(SFX)
+BIN_FILES-$(CONFIG_CMD_NBC) += sendnbc$(SFX)
 BIN_FILES-$(CONFIG_SHA1_CHECK_UB_IMG) += ubsha1$(SFX)
 
 # Source files which exist outside the tools directory
@@ -86,6 +88,7 @@ NOPED_OBJ_FILES-y += kwbimage.o
 NOPED_OBJ_FILES-y += imximage.o
 NOPED_OBJ_FILES-y += mkimage.o
 OBJ_FILES-$(CONFIG_NETCONSOLE) += ncb.o
+OBJ_FILES-$(CONFIG_CMD_NBC) += sendnbc.o
 NOPED_OBJ_FILES-y += os_support.o
 OBJ_FILES-$(CONFIG_SHA1_CHECK_UB_IMG) += ubsha1.o
 
@@ -197,6 +200,9 @@ $(obj)ncb$(SFX):$(obj)ncb.o
$(HOSTCC) $(HOSTCFLAGS) $(HOSTLDFLAGS) -o $@ $^
$(HOSTSTRIP) $@
 
+$(obj)sendnbc$(SFX):   $(obj)sendnbc.o
+   $(HOSTCC) $(HOSTCFLAGS) $(HOSTLDFLAGS) -o $@ $^
+
 $(obj)ubsha1$(SFX):$(obj)os_support.o $(obj)sha1.o $(obj)ubsha1.o
$(HOSTCC) $(HOSTCFLAGS) $(HOSTLDFLAGS) -o $@ $^
 
diff --git a/tools/sendnbc.c b/tools/sendnbc.c
new file mode 100644
index 000..03bf5af
--- /dev/null
+++ b/tools/sendnbc.c
@@ -0,0 +1,219 @@
+#include stdio.h
+#include stdlib.h
+#include unistd.h
+#include string.h
+#include errno.h
+#include sys/socket.h
+#include netinet/in.h
+#include arpa/inet.h
+#include netinet/ether.h
+
+static void usage (char *argv0)
+{
+   printf
+   (SENDNBC: sends a magic packet to u-boot board to interrupt the 
autoboot\n);
+   printf ( and reconfigure u-boot in netconsole mode.\n);
+   printf
+   ( Use in combination with netconsole to connect and 
control\n);
+   printf (your u-boot device\n);
+   printf
+   (   Ex: BOARD_IP=192.168.0.2 ./sendnbc -i $BOARD_IP -d eth0  
./netconsole $BOARD_IP\n);
+   printf (\nusage: %s [options]\n, argv0);
+   printf ( -i ip addr: the ip addr to assign to u-boot board\n);
+   printf ( -m mac addr: the targeted board mac addr \n);
+   printf ( -n hostname: the targeted board hostname \n);
+   printf
+   ( -d device: the net interface to use for broadcasting (ex: 
eth0). Must be root to use this option\n);
+   printf ( -h: display this help\n);
+}
+
+static int build_nbc_pkt (char *buf, int buf_len, char *ip, char *mac,
+ char *hostname)
+{
+   int offset = 0;
+   char *size = NULL;
+   char *ip_size = NULL;
+   char *mac_size = NULL;
+   char *hostname_size = NULL;
+
+   struct in_addr binip;
+   struct ether_addr binmac;
+
+   memset (buf, 0, buf_len);
+
+   buf[0] = (char)0xD3;/* magic number */
+   offset += 1;
+
+   strcpy (buf + offset, NBC);   /* packet header */
+   offset += 3;
+
+   size = (char *)(buf + offset);  /* msg total size */
+   offset += 1;
+
+   strcpy (buf + offset, IP);/* IP chunk header */
+   offset += 4;
+
+   ip_size = (char *)(buf + offset);   /* IP chunk size */
+   offset += 1;
+
+   inet_pton (AF_INET, ip, binip);
+   memcpy (buf + offset, binip, sizeof (struct in_addr)); /* IP chunk 
data */
+   offset += sizeof (struct in_addr);
+   *ip_size = sizeof (struct in_addr);
+
+   if (mac) {
+   strcpy (buf + offset, MAC);   /* MAC chunk header */
+   offset += 4;
+
+   mac_size = (char *)(buf + offset);  /* MAC chunk size */
+   offset += 1;
+
+   ether_aton_r (mac, binmac);
+   memcpy (buf + offset, binmac, sizeof (struct ether_addr)); 
/* MAC chunk data */
+   offset += sizeof (struct ether_addr);
+   *mac_size = sizeof (struct ether_addr);
+   }
+
+   if (hostname) {
+   strcpy (buf + offset, HOST);  /* HOST chunk header */
+   offset += 4;
+
+   hostname_size = (char *)(buf + offset); /* HOST chunk size */
+   offset += 1;
+
+   strcpy (buf + offset, hostname);/* HOST chunk data */
+   offset += strlen (hostname);
+   *hostname_size = strlen (hostname);
+   }
+
+   *size = offset;
+
+   return offset;
+}
+
+int main (int