From 61dafa877b5e57ded3f7234f8d1db1ab259426dd Mon Sep 17 00:00:00 2001
From: Jeffrey Mann <mannj@embeddedplanet.com>
Date: Wed, 9 May 2007 09:42:55 -0400
Subject: [PATCH] Update to stand alone program feature

This patch contains updates to the stand alone program feature, including:
1. update and document new features in README.standalone
2. add SA= flag to allow for the stand alone program directory to
   be compiled with u-boot
3. added xf_func_inc() function. See documentation for a description.
4. removal of undesired #ifdef...#endif statements 
5. Note: XF_VERSION was not advanced. This will happen in the next patch 
   in this series. 

Signed-off-by: Jeffrey Mann <mannj@embeddedplanet.com>
---
 Makefile              |    6 ++++++
 common/exports.c      |   17 +++++++++++++++++
 doc/README.standalone |   32 ++++++++++++++++++++++++++++++++
 include/_exports.h    |    3 +--
 include/exports.h     |    3 +--
 mkconfig              |    6 ++++++
 6 files changed, 63 insertions(+), 4 deletions(-)

diff --git a/Makefile b/Makefile
index ceee2c7..531fcfb
--- a/Makefile
+++ b/Makefile
@@ -235,6 +235,12 @@ # Don't include stuff already done in $(
 SUBDIRS	= tools \
 	  examples
 
+# Include a directory with a stand alone program in it if the user has configured
+# u-boot for it. (This is done using the SA= flag. See README.standalone)	  
+ifeq ($(COMPILE_STAND_ALONE),y)
+SUBDIRS += $(STAND_ALONE_DIR)
+endif
+
 .PHONY : $(SUBDIRS)
 
 ifeq ($(CONFIG_NAND_U_BOOT),y)
diff --git a/common/exports.c b/common/exports.c
index 0cb4396..c8de2b8
--- a/common/exports.c
+++ b/common/exports.c
@@ -7,6 +7,23 @@ static void dummy(void)
 {
 }
 
+/*
+ * xf_func_inc(int) is used to determine whether a function has been compiled 
+ * with u-boot and available for use by a stand alone program . This allows a 
+ * stand alone program to ensure that a function exists before running code with
+ * the an exported function in it. 
+ *
+ * function_no is defined by the enum type of _exports.h, so it must be included
+ * in the stand alone program. For example, 
+ *   xf_func_inc(XF_foobar)
+ *   returns 0 if the function foobar() is available
+ *   returns 1 if the function foobar() has not been compiled with u-boot 
+ */
+static int func_inc(int function_no)
+{		
+	return (gd->jt[function_no] == (void *) dummy);
+}
+
 unsigned long get_version(void)
 {
 	return XF_VERSION;
diff --git a/doc/README.standalone b/doc/README.standalone
index 3998831..75beb13
--- a/doc/README.standalone
+++ b/doc/README.standalone
@@ -68,6 +68,11 @@ Design Notes on Exporting U-Boot Functio
      file:
 
 	EXPORT_FUNC(foobar)
+	
+	 It is important that these be appended to the end of the file so
+	 that the order of existing functions on the list does not change. 
+	 Also, functions in _exports.h should NOT be wrapped in #ifdef 
+	 statements.
 
    - Add the prototype for this function to the include/exports.h
      file:
@@ -78,6 +83,10 @@ Design Notes on Exporting U-Boot Functio
      appropriate (most likely, to the jumptable_init() function):
 
 	gd->jt[XF_foobar] = foobar;
+	
+	 The initialization of the jump table entry should be wrapped
+	 in appropriate #ifdef statements to prevent compile errors when 
+	 the	function is not compiled with u-boot. 
 
    - Increase the XF_VERSION value by one in the include/exports.h
      file
@@ -94,3 +103,26 @@ Design Notes on Exporting U-Boot Functio
    Note that these functions may only use call-clobbered registers;
    those registers that are used to pass the function's arguments,
    the stack contents and the return address should be left intact.
+
+7. To include a directory containing a stand alone program, use the 
+   SA= flag when selecting the board. For example:
+   
+   make BOARD_config SA=diagprog
+   
+   The directory 'diagprog' will be included as a subdirectory when 
+   u-boot is compiled. This allows for stand alone programs not kept
+   in the u-boot tree to be easily compiled with u-boot without 
+   changing the Makefile to accommodate them. 
+
+8. A stand alone program may call xf_func_inc( ) to discover if a 
+   certain function has been compiled with u-boot and therefore
+   available for use by the stand alone program. For example:
+   
+   xf_func_inc(XF_foobar)
+   
+   will return 0 if the function foobar() has been compiled with u-boot, 
+   and 1 if the function has not been compiled with u-boot. This
+   functionality allows for a stand alone program to check for the
+   presence of a certain optional u-boot feature before attempting
+   to utilize it. (exports.h must be included for the XF_function 
+   enumerated type to be defined.)
\ No newline at end of file
diff --git a/include/_exports.h b/include/_exports.h
index 2b8ec3d..5f19825
--- a/include/_exports.h
+++ b/include/_exports.h
@@ -15,7 +15,6 @@ EXPORT_FUNC(do_reset)
 EXPORT_FUNC(getenv)
 EXPORT_FUNC(setenv)
 EXPORT_FUNC(simple_strtoul)
-#if (CONFIG_COMMANDS & CFG_CMD_I2C)
 EXPORT_FUNC(i2c_write)
 EXPORT_FUNC(i2c_read)
-#endif	/* CFG_CMD_I2C */
+EXPORT_FUNC(xf_func_inc)
diff --git a/include/exports.h b/include/exports.h
index 8f7f617..22f9605
--- a/include/exports.h
+++ b/include/exports.h
@@ -23,10 +23,9 @@ void do_reset (void);
 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base);
 char *getenv (char *name);
 void setenv (char *varname, char *varvalue);
-#if (CONFIG_COMMANDS & CFG_CMD_I2C)
 int i2c_write (uchar, uint, int , uchar* , int);
 int i2c_read (uchar, uint, int , uchar* , int);
-#endif	/* CFG_CMD_I2C */
+int xf_func_inc(int);
 
 void app_startup(char **);
 
diff --git a/mkconfig b/mkconfig
index c3e4cea..70f2b77 100755
--- a/mkconfig
+++ b/mkconfig
@@ -72,6 +72,12 @@ echo "BOARD  = $4" >> config.mk
 
 [ "$6" ] && [ "$6" != "NULL" ] && echo "SOC    = $6" >> config.mk
 
+if [ -n "$SA" ]; then
+	echo "COMPILE_STAND_ALONE = y" >> config.mk 
+	echo "STAND_ALONE_DIR = ${SA}" >> config.mk 
+	echo "The directory \"${SA}\" will be included when u-boot is compiled" 
+	fi
+
 #
 # Create board specific header file
 #
-- 
1.4.1

