All of lore.kernel.org
 help / color / mirror / Atom feed
* [U-Boot] [PATCH 0/14] Split out code from the enormous main.c
@ 2014-04-11  2:01 Simon Glass
  2014-04-11  2:01 ` [U-Boot] [PATCH 01/14] Remove unnecessary use of hush header file Simon Glass
                   ` (14 more replies)
  0 siblings, 15 replies; 30+ messages in thread
From: Simon Glass @ 2014-04-11  2:01 UTC (permalink / raw)
  To: u-boot

A previous series created a way of using if () instead of #ifdef for
controlling feature inclusion in U-Boot. The primary target of that series
was common/main.c which is full of #ifdefs. That work was put on hold while
the kbuild work was in progress.

Since kbuild is now complete, it is time to take another look. However, in
the meantime main.c has not improved. It seems like a good idea to try to
split the code out a bit, to make it more obvious what is happening in the
U-Boot start-up.

This series splits main into two main program and a CLI (Command-line
interpreter) parts. There are two CLIs - hush and simple, and each is put
in its own file, with a new cli.c to unify them.

New files are also created for autoboot and bootretry functionality.

Overall this series makes it easier to read what is happening in main.c,
and also clarifies the parser code.


Simon Glass (14):
  Remove unnecessary use of hush header file
  Rename hush to cli_hush
  move CLI prototypes to cli.h and add comments
  Split out simple parser and readline into separate files
  Add cli_ prefix to readline functions
  Move autoboot code to autoboot.c
  Move command line API into cli.c
  Move bootretry code into bootretry.c and clean up
  Rename bootretry functions and remove #ifdefs
  m68k: powerpc: Clean up do_mdm_init
  Simplify the main loop
  main: Hide the hush/simple details inside cli.c
  main: Make the execution path a little clearer in main.c
  main: Avoid unncessary strdup()/free()

 arch/arm/cpu/arm926ejs/kirkwood/cpu.c |    1 -
 arch/arm/cpu/arm926ejs/orion5x/cpu.c  |    1 -
 arch/m68k/lib/board.c                 |    7 -
 arch/powerpc/lib/board.c              |    8 -
 board/ait/cam_enc_4xx/cam_enc_4xx.c   |    4 +-
 board/amcc/yucca/cmd_yucca.c          |   21 +-
 board/eltec/elppc/misc.c              |   15 +-
 board/eltec/mhpc/mhpc.c               |   13 +-
 board/hymod/hymod.c                   |    8 +-
 board/hymod/input.c                   |   14 +-
 board/keymile/common/common.c         |    2 +-
 board/keymile/common/ivm.c            |    2 +-
 board/mcc200/auto_update.c            |    7 +-
 common/Makefile                       |   20 +-
 common/autoboot.c                     |  303 +++++++
 common/board_r.c                      |   14 -
 common/bootretry.c                    |   59 ++
 common/cli.c                          |  194 +++++
 common/{hush.c => cli_hush.c}         |   17 +-
 common/cli_readline.c                 |  621 ++++++++++++++
 common/cli_simple.c                   |  337 ++++++++
 common/cmd_bedbug.c                   |   29 +-
 common/cmd_bootm.c                    |    4 -
 common/cmd_bootmenu.c                 |    1 -
 common/cmd_dcr.c                      |    3 +-
 common/cmd_i2c.c                      |   17 +-
 common/cmd_mem.c                      |   17 +-
 common/cmd_nvedit.c                   |    5 +-
 common/cmd_pci.c                      |   13 +-
 common/main.c                         | 1527 +--------------------------------
 common/menu.c                         |    6 +-
 drivers/ddr/fsl/interactive.c         |    8 +-
 include/autoboot.h                    |   47 +
 include/bootretry.h                   |   59 ++
 include/cli.h                         |  149 ++++
 include/{hush.h => cli_hush.h}        |    4 +-
 include/common.h                      |    7 +-
 37 files changed, 1917 insertions(+), 1647 deletions(-)
 create mode 100644 common/autoboot.c
 create mode 100644 common/bootretry.c
 create mode 100644 common/cli.c
 rename common/{hush.c => cli_hush.c} (99%)
 create mode 100644 common/cli_readline.c
 create mode 100644 common/cli_simple.c
 create mode 100644 include/autoboot.h
 create mode 100644 include/bootretry.h
 create mode 100644 include/cli.h
 rename include/{hush.h => cli_hush.h} (93%)

-- 
1.9.1.423.g4596e3a

^ permalink raw reply	[flat|nested] 30+ messages in thread

* [U-Boot] [PATCH 01/14] Remove unnecessary use of hush header file
  2014-04-11  2:01 [U-Boot] [PATCH 0/14] Split out code from the enormous main.c Simon Glass
@ 2014-04-11  2:01 ` Simon Glass
  2014-05-30 15:27   ` Tom Rini
  2014-04-11  2:01 ` [U-Boot] [PATCH 02/14] Rename hush to cli_hush Simon Glass
                   ` (13 subsequent siblings)
  14 siblings, 1 reply; 30+ messages in thread
From: Simon Glass @ 2014-04-11  2:01 UTC (permalink / raw)
  To: u-boot

Some files include hush.h but don't actually use it. Remove this where
possible.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 arch/arm/cpu/arm926ejs/kirkwood/cpu.c | 1 -
 arch/arm/cpu/arm926ejs/orion5x/cpu.c  | 1 -
 board/ait/cam_enc_4xx/cam_enc_4xx.c   | 1 -
 board/mcc200/auto_update.c            | 7 +------
 common/cmd_bootm.c                    | 4 ----
 common/cmd_bootmenu.c                 | 1 -
 6 files changed, 1 insertion(+), 14 deletions(-)

diff --git a/arch/arm/cpu/arm926ejs/kirkwood/cpu.c b/arch/arm/cpu/arm926ejs/kirkwood/cpu.c
index d4711c0..0937506 100644
--- a/arch/arm/cpu/arm926ejs/kirkwood/cpu.c
+++ b/arch/arm/cpu/arm926ejs/kirkwood/cpu.c
@@ -13,7 +13,6 @@
 #include <asm/io.h>
 #include <asm/arch/cpu.h>
 #include <asm/arch/kirkwood.h>
-#include <hush.h>
 
 #define BUFLEN	16
 
diff --git a/arch/arm/cpu/arm926ejs/orion5x/cpu.c b/arch/arm/cpu/arm926ejs/orion5x/cpu.c
index b55c5f0..f88db3b 100644
--- a/arch/arm/cpu/arm926ejs/orion5x/cpu.c
+++ b/arch/arm/cpu/arm926ejs/orion5x/cpu.c
@@ -15,7 +15,6 @@
 #include <asm/io.h>
 #include <u-boot/md5.h>
 #include <asm/arch/cpu.h>
-#include <hush.h>
 
 #define BUFLEN	16
 
diff --git a/board/ait/cam_enc_4xx/cam_enc_4xx.c b/board/ait/cam_enc_4xx/cam_enc_4xx.c
index 7e1b16a..b5cc3ed 100644
--- a/board/ait/cam_enc_4xx/cam_enc_4xx.c
+++ b/board/ait/cam_enc_4xx/cam_enc_4xx.c
@@ -9,7 +9,6 @@
 
 #include <common.h>
 #include <errno.h>
-#include <hush.h>
 #include <linux/mtd/nand.h>
 #include <nand.h>
 #include <miiphy.h>
diff --git a/board/mcc200/auto_update.c b/board/mcc200/auto_update.c
index 2f622b0..43173ce 100644
--- a/board/mcc200/auto_update.c
+++ b/board/mcc200/auto_update.c
@@ -12,11 +12,6 @@
 #include <usb.h>
 #include <part.h>
 
-#ifdef CONFIG_SYS_HUSH_PARSER
-#include <hush.h>
-#endif
-
-
 #ifdef CONFIG_AUTO_UPDATE
 
 #ifndef CONFIG_USB_OHCI
@@ -247,7 +242,7 @@ int au_do_update(int idx, long sz)
 		/* parse_string_outer() runs off the end. */
 		addr[image_get_data_size (hdr)] = 0;
 		addr += 8;
-		parse_string_outer(addr, FLAG_PARSE_SEMICOLON);
+		run_command_list(addr, -1, 0);
 		return 0;
 	}
 
diff --git a/common/cmd_bootm.c b/common/cmd_bootm.c
index 9751edc..bfca006 100644
--- a/common/cmd_bootm.c
+++ b/common/cmd_bootm.c
@@ -32,10 +32,6 @@
 #include <usb.h>
 #endif
 
-#ifdef CONFIG_SYS_HUSH_PARSER
-#include <hush.h>
-#endif
-
 #if defined(CONFIG_OF_LIBFDT)
 #include <libfdt.h>
 #include <fdt_support.h>
diff --git a/common/cmd_bootmenu.c b/common/cmd_bootmenu.c
index 163d5b2..5879065 100644
--- a/common/cmd_bootmenu.c
+++ b/common/cmd_bootmenu.c
@@ -8,7 +8,6 @@
 #include <command.h>
 #include <ansi.h>
 #include <menu.h>
-#include <hush.h>
 #include <watchdog.h>
 #include <malloc.h>
 #include <linux/string.h>
-- 
1.9.1.423.g4596e3a

^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [U-Boot] [PATCH 02/14] Rename hush to cli_hush
  2014-04-11  2:01 [U-Boot] [PATCH 0/14] Split out code from the enormous main.c Simon Glass
  2014-04-11  2:01 ` [U-Boot] [PATCH 01/14] Remove unnecessary use of hush header file Simon Glass
@ 2014-04-11  2:01 ` Simon Glass
  2014-05-30 15:27   ` Tom Rini
  2014-04-11  2:01 ` [U-Boot] [PATCH 03/14] move CLI prototypes to cli.h and add comments Simon Glass
                   ` (12 subsequent siblings)
  14 siblings, 1 reply; 30+ messages in thread
From: Simon Glass @ 2014-04-11  2:01 UTC (permalink / raw)
  To: u-boot

Hush is a command-line interpreter, so rename it to make that clearer.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 board/keymile/common/common.c  | 2 +-
 board/keymile/common/ivm.c     | 2 +-
 common/Makefile                | 2 +-
 common/{hush.c => cli_hush.c}  | 4 ++--
 common/main.c                  | 2 +-
 include/{hush.h => cli_hush.h} | 4 ++--
 6 files changed, 8 insertions(+), 8 deletions(-)
 rename common/{hush.c => cli_hush.c} (99%)
 rename include/{hush.h => cli_hush.h} (93%)

diff --git a/board/keymile/common/common.c b/board/keymile/common/common.c
index f941e44..2ddb3da 100644
--- a/board/keymile/common/common.c
+++ b/board/keymile/common/common.c
@@ -12,7 +12,7 @@
 #include <ioports.h>
 #include <command.h>
 #include <malloc.h>
-#include <hush.h>
+#include <cli_hush.h>
 #include <net.h>
 #include <netdev.h>
 #include <asm/io.h>
diff --git a/board/keymile/common/ivm.c b/board/keymile/common/ivm.c
index f0e91bb..bffc08b 100644
--- a/board/keymile/common/ivm.c
+++ b/board/keymile/common/ivm.c
@@ -6,7 +6,7 @@
  */
 
 #include <common.h>
-#include <hush.h>
+#include <cli_hush.h>
 #include <i2c.h>
 #include "common.h"
 
diff --git a/common/Makefile b/common/Makefile
index cecd81a..12068ac 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -11,7 +11,7 @@ obj-y += main.o
 obj-y += command.o
 obj-y += exports.o
 obj-y += hash.o
-obj-$(CONFIG_SYS_HUSH_PARSER) += hush.o
+obj-$(CONFIG_SYS_HUSH_PARSER) += cli_hush.o
 obj-y += s_record.o
 obj-y += xyzModem.o
 obj-y += cmd_disk.o
diff --git a/common/hush.c b/common/cli_hush.c
similarity index 99%
rename from common/hush.c
rename to common/cli_hush.c
index df10267..070519f 100644
--- a/common/hush.c
+++ b/common/cli_hush.c
@@ -79,7 +79,7 @@
 #include <malloc.h>         /* malloc, free, realloc*/
 #include <linux/ctype.h>    /* isalpha, isdigit */
 #include <common.h>        /* readline */
-#include <hush.h>
+#include <cli_hush.h>
 #include <command.h>        /* find_cmd */
 #ifndef CONFIG_SYS_PROMPT_HUSH_PS2
 #define CONFIG_SYS_PROMPT_HUSH_PS2	"> "
@@ -222,7 +222,7 @@ struct child_prog {
 #endif
 	char **argv;				/* program name and arguments */
 	/* was quoted when parsed; copy of struct o_string.nonnull field */
-	int *argv_nonnull;			
+	int *argv_nonnull;
 #ifdef __U_BOOT__
 	int    argc;                            /* number of program arguments */
 #endif
diff --git a/common/main.c b/common/main.c
index 8b6f274..2eea9c5 100644
--- a/common/main.c
+++ b/common/main.c
@@ -14,7 +14,7 @@
 #include <common.h>
 #include <command.h>
 #include <fdtdec.h>
-#include <hush.h>
+#include <cli_hush.h>
 #include <malloc.h>
 #include <menu.h>
 #include <post.h>
diff --git a/include/hush.h b/include/cli_hush.h
similarity index 93%
rename from include/hush.h
rename to include/cli_hush.h
index 595303a..4951eef 100644
--- a/include/hush.h
+++ b/include/cli_hush.h
@@ -5,8 +5,8 @@
  * SPDX-License-Identifier:	GPL-2.0+
  */
 
-#ifndef _HUSH_H_
-#define _HUSH_H_
+#ifndef _CLI_HUSH_H_
+#define _CLI_HUSH_H_
 
 #define FLAG_EXIT_FROM_LOOP 1
 #define FLAG_PARSE_SEMICOLON (1 << 1)	  /* symbol ';' is special for parser */
-- 
1.9.1.423.g4596e3a

^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [U-Boot] [PATCH 03/14] move CLI prototypes to cli.h and add comments
  2014-04-11  2:01 [U-Boot] [PATCH 0/14] Split out code from the enormous main.c Simon Glass
  2014-04-11  2:01 ` [U-Boot] [PATCH 01/14] Remove unnecessary use of hush header file Simon Glass
  2014-04-11  2:01 ` [U-Boot] [PATCH 02/14] Rename hush to cli_hush Simon Glass
@ 2014-04-11  2:01 ` Simon Glass
  2014-05-30 15:27   ` Tom Rini
  2014-04-11  2:01 ` [U-Boot] [PATCH 04/14] Split out simple parser and readline into separate files Simon Glass
                   ` (11 subsequent siblings)
  14 siblings, 1 reply; 30+ messages in thread
From: Simon Glass @ 2014-04-11  2:01 UTC (permalink / raw)
  To: u-boot

Move the CLI prototypes from common.h to cli.h as part of an effort to
reduce the size of common.h.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 board/ait/cam_enc_4xx/cam_enc_4xx.c |   1 +
 board/amcc/yucca/cmd_yucca.c        |   1 +
 board/eltec/elppc/misc.c            |   1 +
 board/eltec/mhpc/mhpc.c             |   1 +
 board/hymod/hymod.c                 |   1 +
 board/hymod/input.c                 |   1 +
 common/cli_hush.c                   |   1 +
 common/cmd_bedbug.c                 |   1 +
 common/cmd_dcr.c                    |   1 +
 common/cmd_i2c.c                    |   1 +
 common/cmd_mem.c                    |   1 +
 common/cmd_nvedit.c                 |   1 +
 common/cmd_pci.c                    |   1 +
 common/main.c                       |   1 +
 common/menu.c                       |   1 +
 drivers/ddr/fsl/interactive.c       |   1 +
 include/cli.h                       | 102 ++++++++++++++++++++++++++++++++++++
 include/common.h                    |   4 --
 18 files changed, 118 insertions(+), 4 deletions(-)
 create mode 100644 include/cli.h

diff --git a/board/ait/cam_enc_4xx/cam_enc_4xx.c b/board/ait/cam_enc_4xx/cam_enc_4xx.c
index b5cc3ed..7c26ba0 100644
--- a/board/ait/cam_enc_4xx/cam_enc_4xx.c
+++ b/board/ait/cam_enc_4xx/cam_enc_4xx.c
@@ -8,6 +8,7 @@
  */
 
 #include <common.h>
+#include <cli.h>
 #include <errno.h>
 #include <linux/mtd/nand.h>
 #include <nand.h>
diff --git a/board/amcc/yucca/cmd_yucca.c b/board/amcc/yucca/cmd_yucca.c
index dc78b73..3257c29 100644
--- a/board/amcc/yucca/cmd_yucca.c
+++ b/board/amcc/yucca/cmd_yucca.c
@@ -8,6 +8,7 @@
  */
 
 #include <common.h>
+#include <cli.h>
 #include <command.h>
 #include "yucca.h"
 #include <i2c.h>
diff --git a/board/eltec/elppc/misc.c b/board/eltec/elppc/misc.c
index d80eaba..70d1f9a 100644
--- a/board/eltec/elppc/misc.c
+++ b/board/eltec/elppc/misc.c
@@ -7,6 +7,7 @@
 
 /* includes */
 #include <common.h>
+#include <cli.h>
 #include <linux/ctype.h>
 #include <pci.h>
 #include <net.h>
diff --git a/board/eltec/mhpc/mhpc.c b/board/eltec/mhpc/mhpc.c
index f3f564f..ff9e0ab 100644
--- a/board/eltec/mhpc/mhpc.c
+++ b/board/eltec/mhpc/mhpc.c
@@ -14,6 +14,7 @@
  * SPDX-License-Identifier:	GPL-2.0+
  */
 #include <common.h>
+#include <cli.h>
 #include <linux/ctype.h>
 #include <commproc.h>
 #include "mpc8xx.h"
diff --git a/board/hymod/hymod.c b/board/hymod/hymod.c
index 5fec914..ea49e26 100644
--- a/board/hymod/hymod.c
+++ b/board/hymod/hymod.c
@@ -8,6 +8,7 @@
  */
 
 #include <common.h>
+#include <cli.h>
 #include <mpc8260.h>
 #include <mpc8260_irq.h>
 #include <ioports.h>
diff --git a/board/hymod/input.c b/board/hymod/input.c
index 184902c..23d3f19 100644
--- a/board/hymod/input.c
+++ b/board/hymod/input.c
@@ -6,6 +6,7 @@
  */
 
 #include <common.h>
+#include <cli.h>
 
 int
 hymod_get_serno (const char *prompt)
diff --git a/common/cli_hush.c b/common/cli_hush.c
index 070519f..48cec88 100644
--- a/common/cli_hush.c
+++ b/common/cli_hush.c
@@ -79,6 +79,7 @@
 #include <malloc.h>         /* malloc, free, realloc*/
 #include <linux/ctype.h>    /* isalpha, isdigit */
 #include <common.h>        /* readline */
+#include <cli.h>
 #include <cli_hush.h>
 #include <command.h>        /* find_cmd */
 #ifndef CONFIG_SYS_PROMPT_HUSH_PS2
diff --git a/common/cmd_bedbug.c b/common/cmd_bedbug.c
index 77b6e3e..f1a70ef 100644
--- a/common/cmd_bedbug.c
+++ b/common/cmd_bedbug.c
@@ -3,6 +3,7 @@
  */
 
 #include <common.h>
+#include <cli.h>
 #include <command.h>
 #include <linux/ctype.h>
 #include <net.h>
diff --git a/common/cmd_dcr.c b/common/cmd_dcr.c
index 896f79f..c5bcb8b 100644
--- a/common/cmd_dcr.c
+++ b/common/cmd_dcr.c
@@ -10,6 +10,7 @@
  */
 
 #include <common.h>
+#include <cli.h>
 #include <config.h>
 #include <command.h>
 
diff --git a/common/cmd_i2c.c b/common/cmd_i2c.c
index ebce7d4..8ccde68 100644
--- a/common/cmd_i2c.c
+++ b/common/cmd_i2c.c
@@ -66,6 +66,7 @@
  */
 
 #include <common.h>
+#include <cli.h>
 #include <command.h>
 #include <edid.h>
 #include <environment.h>
diff --git a/common/cmd_mem.c b/common/cmd_mem.c
index 5b03c2d..4b8738b 100644
--- a/common/cmd_mem.c
+++ b/common/cmd_mem.c
@@ -12,6 +12,7 @@
  */
 
 #include <common.h>
+#include <cli.h>
 #include <command.h>
 #ifdef CONFIG_HAS_DATAFLASH
 #include <dataflash.h>
diff --git a/common/cmd_nvedit.c b/common/cmd_nvedit.c
index c53601c..99a21b2 100644
--- a/common/cmd_nvedit.c
+++ b/common/cmd_nvedit.c
@@ -25,6 +25,7 @@
  */
 
 #include <common.h>
+#include <cli.h>
 #include <command.h>
 #include <environment.h>
 #include <search.h>
diff --git a/common/cmd_pci.c b/common/cmd_pci.c
index d3e7c08..ddda207 100644
--- a/common/cmd_pci.c
+++ b/common/cmd_pci.c
@@ -14,6 +14,7 @@
  */
 
 #include <common.h>
+#include <cli.h>
 #include <command.h>
 #include <asm/processor.h>
 #include <asm/io.h>
diff --git a/common/main.c b/common/main.c
index 2eea9c5..815759a 100644
--- a/common/main.c
+++ b/common/main.c
@@ -12,6 +12,7 @@
 /* #define	DEBUG	*/
 
 #include <common.h>
+#include <cli.h>
 #include <command.h>
 #include <fdtdec.h>
 #include <cli_hush.h>
diff --git a/common/menu.c b/common/menu.c
index ba393ad..88d4697 100644
--- a/common/menu.c
+++ b/common/menu.c
@@ -5,6 +5,7 @@
  */
 
 #include <common.h>
+#include <cli.h>
 #include <malloc.h>
 #include <errno.h>
 #include <linux/list.h>
diff --git a/drivers/ddr/fsl/interactive.c b/drivers/ddr/fsl/interactive.c
index ebf3ed6..3b311b1 100644
--- a/drivers/ddr/fsl/interactive.c
+++ b/drivers/ddr/fsl/interactive.c
@@ -12,6 +12,7 @@
  */
 
 #include <common.h>
+#include <cli.h>
 #include <linux/ctype.h>
 #include <asm/types.h>
 #include <asm/io.h>
diff --git a/include/cli.h b/include/cli.h
new file mode 100644
index 0000000..0075bd4
--- /dev/null
+++ b/include/cli.h
@@ -0,0 +1,102 @@
+/*
+ * (C) Copyright 2014 Google, Inc
+ * Simon Glass <sjg@chromium.org>
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#ifndef __CLI_H
+#define __CLI_H
+
+/**
+ * Go into the command loop
+ *
+ * This will return if we get a timeout waiting for a command. See
+ * CONFIG_BOOT_RETRY_TIME.
+ */
+void cli_loop(void);
+
+/**
+ * cli_simple_run_command() - Execute a command with the simple CLI
+ *
+ * @cmd:	String containing the command to execute
+ * @flag	Flag value - see CMD_FLAG_...
+ * @return 1  - command executed, repeatable
+ *	0  - command executed but not repeatable, interrupted commands are
+ *	     always considered not repeatable
+ *	-1 - not executed (unrecognized, bootd recursion or too many args)
+ *           (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
+ *           considered unrecognized)
+ */
+int cli_simple_run_command(const char *cmd, int flag);
+
+/**
+ * cli_simple_run_command_list() - Execute a list of command
+ *
+ * The commands should be separated by ; or \n and will be executed
+ * by the built-in parser.
+ *
+ * This function cannot take a const char * for the command, since if it
+ * finds newlines in the string, it replaces them with \0.
+ *
+ * @param cmd	String containing list of commands
+ * @param flag	Execution flags (CMD_FLAG_...)
+ * @return 0 on success, or != 0 on error.
+ */
+int cli_simple_run_command_list(char *cmd, int flag);
+
+/**
+ * cli_readline() - read a line into the console_buffer
+ *
+ * This is a convenience function which calls cli_readline_into_buffer().
+ *
+ * @prompt: Prompt to display
+ * @return command line length excluding terminator, or -ve on error
+ */
+int readline(const char *const prompt);
+
+/**
+ * readline_into_buffer() - read a line into a buffer
+ *
+ * Display the prompt, then read a command line into @buffer. The
+ * maximum line length is CONFIG_SYS_CBSIZE including a \0 terminator, which
+ * will always be added.
+ *
+ * The command is echoed as it is typed. Command editing is supported if
+ * CONFIG_CMDLINE_EDITING is defined. Tab auto-complete is supported if
+ * CONFIG_AUTO_COMPLETE is defined. If CONFIG_BOOT_RETRY_TIME is defined,
+ * then a timeout will be applied.
+ *
+ * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
+ * time out when time goes past endtime (timebase time in ticks).
+ *
+ * @prompt:	Prompt to display
+ * @buffer:	Place to put the line that is entered
+ * @timeout:	Timeout in milliseconds, 0 if none
+ * @return command line length excluding terminator, or -ve on error: of the
+ * timeout is exceeded (either CONFIG_BOOT_RETRY_TIME or the timeout
+ * parameter), then -2 is returned. If a break is detected (Ctrl-C) then
+ * -1 is returned.
+ */
+int readline_into_buffer(const char *const prompt, char *buffer, int timeout);
+
+/**
+ * parse_line() - split a command line down into separate arguments
+ *
+ * The argv[] array is filled with pointers into @line, and each argument
+ * is terminated by \0 (i.e. @line is changed in the process unless there
+ * is only one argument).
+ *
+ * #argv is terminated by a NULL after the last argument pointer.
+ *
+ * At most CONFIG_SYS_MAXARGS arguments are permited - if there are more
+ * than that then an error is printed, and this function returns
+ * CONFIG_SYS_MAXARGS, with argv[] set up to that point.
+ *
+ * @line:	Command line to parse
+ * @args:	Array to hold arguments
+ * @return number of arguments
+ */
+int parse_line(char *line, char *argv[]);
+
+#endif
diff --git a/include/common.h b/include/common.h
index cbd3c9e..abe57c6 100644
--- a/include/common.h
+++ b/include/common.h
@@ -286,10 +286,6 @@ int run_command(const char *cmd, int flag);
  * @return 0 on success, or != 0 on error.
  */
 int run_command_list(const char *cmd, int len, int flag);
-int	readline	(const char *const prompt);
-int	readline_into_buffer(const char *const prompt, char *buffer,
-			int timeout);
-int	parse_line (char *, char *[]);
 void	init_cmd_timeout(void);
 void	reset_cmd_timeout(void);
 extern char console_buffer[];
-- 
1.9.1.423.g4596e3a

^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [U-Boot] [PATCH 04/14] Split out simple parser and readline into separate files
  2014-04-11  2:01 [U-Boot] [PATCH 0/14] Split out code from the enormous main.c Simon Glass
                   ` (2 preceding siblings ...)
  2014-04-11  2:01 ` [U-Boot] [PATCH 03/14] move CLI prototypes to cli.h and add comments Simon Glass
@ 2014-04-11  2:01 ` Simon Glass
  2014-05-30 15:27   ` Tom Rini
  2014-04-11  2:01 ` [U-Boot] [PATCH 05/14] Add cli_ prefix to readline functions Simon Glass
                   ` (10 subsequent siblings)
  14 siblings, 1 reply; 30+ messages in thread
From: Simon Glass @ 2014-04-11  2:01 UTC (permalink / raw)
  To: u-boot

It doesn't make sense to have the simple parser and the readline code
all in main. Split them out into separate files.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 common/Makefile       |    9 +-
 common/cli_readline.c |  670 ++++++++++++++++++++++++++++++++
 common/cli_simple.c   |  338 ++++++++++++++++
 common/main.c         | 1017 +------------------------------------------------
 include/cli.h         |    5 +
 5 files changed, 1026 insertions(+), 1013 deletions(-)
 create mode 100644 common/cli_readline.c
 create mode 100644 common/cli_simple.c

diff --git a/common/Makefile b/common/Makefile
index 12068ac..44967ea 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -11,7 +11,14 @@ obj-y += main.o
 obj-y += command.o
 obj-y += exports.o
 obj-y += hash.o
-obj-$(CONFIG_SYS_HUSH_PARSER) += cli_hush.o
+ifdef CONFIG_SYS_HUSH_PARSER
+obj-y += cli_hush.o
+endif
+
+# We always have this since drivers/ddr/fs/interactive.c needs it
+obj-y += cli_simple.o
+
+obj-y += cli_readline.o
 obj-y += s_record.o
 obj-y += xyzModem.o
 obj-y += cmd_disk.o
diff --git a/common/cli_readline.c b/common/cli_readline.c
new file mode 100644
index 0000000..cea0b7c
--- /dev/null
+++ b/common/cli_readline.c
@@ -0,0 +1,670 @@
+/*
+ * (C) Copyright 2000
+ * Wolfgang Denk, DENX Software Engineering, wd at denx.de.
+ *
+ * Add to readline cmdline-editing by
+ * (C) Copyright 2005
+ * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <cli.h>
+#include <watchdog.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+static const char erase_seq[] = "\b \b";	/* erase sequence */
+static const char   tab_seq[] = "        ";	/* used to expand TABs */
+
+#ifdef CONFIG_BOOT_RETRY_TIME
+static uint64_t endtime;      /* must be set, default is instant timeout */
+static int      retry_time = -1; /* -1 so can call readline before main_loop */
+#endif
+
+char console_buffer[CONFIG_SYS_CBSIZE + 1];	/* console I/O buffer	*/
+
+#ifndef CONFIG_BOOT_RETRY_MIN
+#define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
+#endif
+
+static char *delete_char (char *buffer, char *p, int *colp, int *np, int plen)
+{
+	char *s;
+
+	if (*np == 0)
+		return p;
+
+	if (*(--p) == '\t') {		/* will retype the whole line */
+		while (*colp > plen) {
+			puts(erase_seq);
+			(*colp)--;
+		}
+		for (s = buffer; s < p; ++s) {
+			if (*s == '\t') {
+				puts(tab_seq + ((*colp) & 07));
+				*colp += 8 - ((*colp) & 07);
+			} else {
+				++(*colp);
+				putc(*s);
+			}
+		}
+	} else {
+		puts(erase_seq);
+		(*colp)--;
+	}
+	(*np)--;
+
+	return p;
+}
+
+#ifdef CONFIG_CMDLINE_EDITING
+
+/*
+ * cmdline-editing related codes from vivi.
+ * Author: Janghoon Lyu <nandy@mizi.com>
+ */
+
+#define putnstr(str, n)	printf("%.*s", (int)n, str)
+
+#define CTL_CH(c)		((c) - 'a' + 1)
+#define CTL_BACKSPACE		('\b')
+#define DEL			((char)255)
+#define DEL7			((char)127)
+#define CREAD_HIST_CHAR		('!')
+
+#define getcmd_putch(ch)	putc(ch)
+#define getcmd_getch()		getc()
+#define getcmd_cbeep()		getcmd_putch('\a')
+
+#define HIST_MAX		20
+#define HIST_SIZE		CONFIG_SYS_CBSIZE
+
+static int hist_max;
+static int hist_add_idx;
+static int hist_cur = -1;
+static unsigned hist_num;
+
+static char *hist_list[HIST_MAX];
+static char hist_lines[HIST_MAX][HIST_SIZE + 1];	/* Save room for NULL */
+
+#define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
+
+static void hist_init(void)
+{
+	int i;
+
+	hist_max = 0;
+	hist_add_idx = 0;
+	hist_cur = -1;
+	hist_num = 0;
+
+	for (i = 0; i < HIST_MAX; i++) {
+		hist_list[i] = hist_lines[i];
+		hist_list[i][0] = '\0';
+	}
+}
+
+static void cread_add_to_hist(char *line)
+{
+	strcpy(hist_list[hist_add_idx], line);
+
+	if (++hist_add_idx >= HIST_MAX)
+		hist_add_idx = 0;
+
+	if (hist_add_idx > hist_max)
+		hist_max = hist_add_idx;
+
+	hist_num++;
+}
+
+static char *hist_prev(void)
+{
+	char *ret;
+	int old_cur;
+
+	if (hist_cur < 0)
+		return NULL;
+
+	old_cur = hist_cur;
+	if (--hist_cur < 0)
+		hist_cur = hist_max;
+
+	if (hist_cur == hist_add_idx) {
+		hist_cur = old_cur;
+		ret = NULL;
+	} else {
+		ret = hist_list[hist_cur];
+	}
+
+	return ret;
+}
+
+static char *hist_next(void)
+{
+	char *ret;
+
+	if (hist_cur < 0)
+		return NULL;
+
+	if (hist_cur == hist_add_idx)
+		return NULL;
+
+	if (++hist_cur > hist_max)
+		hist_cur = 0;
+
+	if (hist_cur == hist_add_idx)
+		ret = "";
+	else
+		ret = hist_list[hist_cur];
+
+	return ret;
+}
+
+#ifndef CONFIG_CMDLINE_EDITING
+static void cread_print_hist_list(void)
+{
+	int i;
+	unsigned long n;
+
+	n = hist_num - hist_max;
+
+	i = hist_add_idx + 1;
+	while (1) {
+		if (i > hist_max)
+			i = 0;
+		if (i == hist_add_idx)
+			break;
+		printf("%s\n", hist_list[i]);
+		n++;
+		i++;
+	}
+}
+#endif /* CONFIG_CMDLINE_EDITING */
+
+#define BEGINNING_OF_LINE() {			\
+	while (num) {				\
+		getcmd_putch(CTL_BACKSPACE);	\
+		num--;				\
+	}					\
+}
+
+#define ERASE_TO_EOL() {				\
+	if (num < eol_num) {				\
+		printf("%*s", (int)(eol_num - num), ""); \
+		do {					\
+			getcmd_putch(CTL_BACKSPACE);	\
+		} while (--eol_num > num);		\
+	}						\
+}
+
+#define REFRESH_TO_EOL() {			\
+	if (num < eol_num) {			\
+		wlen = eol_num - num;		\
+		putnstr(buf + num, wlen);	\
+		num = eol_num;			\
+	}					\
+}
+
+static void cread_add_char(char ichar, int insert, unsigned long *num,
+	       unsigned long *eol_num, char *buf, unsigned long len)
+{
+	unsigned long wlen;
+
+	/* room ??? */
+	if (insert || *num == *eol_num) {
+		if (*eol_num > len - 1) {
+			getcmd_cbeep();
+			return;
+		}
+		(*eol_num)++;
+	}
+
+	if (insert) {
+		wlen = *eol_num - *num;
+		if (wlen > 1)
+			memmove(&buf[*num+1], &buf[*num], wlen-1);
+
+		buf[*num] = ichar;
+		putnstr(buf + *num, wlen);
+		(*num)++;
+		while (--wlen)
+			getcmd_putch(CTL_BACKSPACE);
+	} else {
+		/* echo the character */
+		wlen = 1;
+		buf[*num] = ichar;
+		putnstr(buf + *num, wlen);
+		(*num)++;
+	}
+}
+
+static void cread_add_str(char *str, int strsize, int insert,
+			  unsigned long *num, unsigned long *eol_num,
+			  char *buf, unsigned long len)
+{
+	while (strsize--) {
+		cread_add_char(*str, insert, num, eol_num, buf, len);
+		str++;
+	}
+}
+
+static int cread_line(const char *const prompt, char *buf, unsigned int *len,
+		int timeout)
+{
+	unsigned long num = 0;
+	unsigned long eol_num = 0;
+	unsigned long wlen;
+	char ichar;
+	int insert = 1;
+	int esc_len = 0;
+	char esc_save[8];
+	int init_len = strlen(buf);
+	int first = 1;
+
+	if (init_len)
+		cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
+
+	while (1) {
+#ifdef CONFIG_BOOT_RETRY_TIME
+		while (!tstc()) {	/* while no incoming data */
+			if (retry_time >= 0 && get_ticks() > endtime)
+				return -2;	/* timed out */
+			WATCHDOG_RESET();
+		}
+#endif
+		if (first && timeout) {
+			uint64_t etime = endtick(timeout);
+
+			while (!tstc()) {	/* while no incoming data */
+				if (get_ticks() >= etime)
+					return -2;	/* timed out */
+				WATCHDOG_RESET();
+			}
+			first = 0;
+		}
+
+		ichar = getcmd_getch();
+
+		if ((ichar == '\n') || (ichar == '\r')) {
+			putc('\n');
+			break;
+		}
+
+		/*
+		 * handle standard linux xterm esc sequences for arrow key, etc.
+		 */
+		if (esc_len != 0) {
+			if (esc_len == 1) {
+				if (ichar == '[') {
+					esc_save[esc_len] = ichar;
+					esc_len = 2;
+				} else {
+					cread_add_str(esc_save, esc_len,
+						      insert, &num, &eol_num,
+						      buf, *len);
+					esc_len = 0;
+				}
+				continue;
+			}
+
+			switch (ichar) {
+			case 'D':	/* <- key */
+				ichar = CTL_CH('b');
+				esc_len = 0;
+				break;
+			case 'C':	/* -> key */
+				ichar = CTL_CH('f');
+				esc_len = 0;
+				break;	/* pass off to ^F handler */
+			case 'H':	/* Home key */
+				ichar = CTL_CH('a');
+				esc_len = 0;
+				break;	/* pass off to ^A handler */
+			case 'A':	/* up arrow */
+				ichar = CTL_CH('p');
+				esc_len = 0;
+				break;	/* pass off to ^P handler */
+			case 'B':	/* down arrow */
+				ichar = CTL_CH('n');
+				esc_len = 0;
+				break;	/* pass off to ^N handler */
+			default:
+				esc_save[esc_len++] = ichar;
+				cread_add_str(esc_save, esc_len, insert,
+					      &num, &eol_num, buf, *len);
+				esc_len = 0;
+				continue;
+			}
+		}
+
+		switch (ichar) {
+		case 0x1b:
+			if (esc_len == 0) {
+				esc_save[esc_len] = ichar;
+				esc_len = 1;
+			} else {
+				puts("impossible condition #876\n");
+				esc_len = 0;
+			}
+			break;
+
+		case CTL_CH('a'):
+			BEGINNING_OF_LINE();
+			break;
+		case CTL_CH('c'):	/* ^C - break */
+			*buf = '\0';	/* discard input */
+			return -1;
+		case CTL_CH('f'):
+			if (num < eol_num) {
+				getcmd_putch(buf[num]);
+				num++;
+			}
+			break;
+		case CTL_CH('b'):
+			if (num) {
+				getcmd_putch(CTL_BACKSPACE);
+				num--;
+			}
+			break;
+		case CTL_CH('d'):
+			if (num < eol_num) {
+				wlen = eol_num - num - 1;
+				if (wlen) {
+					memmove(&buf[num], &buf[num+1], wlen);
+					putnstr(buf + num, wlen);
+				}
+
+				getcmd_putch(' ');
+				do {
+					getcmd_putch(CTL_BACKSPACE);
+				} while (wlen--);
+				eol_num--;
+			}
+			break;
+		case CTL_CH('k'):
+			ERASE_TO_EOL();
+			break;
+		case CTL_CH('e'):
+			REFRESH_TO_EOL();
+			break;
+		case CTL_CH('o'):
+			insert = !insert;
+			break;
+		case CTL_CH('x'):
+		case CTL_CH('u'):
+			BEGINNING_OF_LINE();
+			ERASE_TO_EOL();
+			break;
+		case DEL:
+		case DEL7:
+		case 8:
+			if (num) {
+				wlen = eol_num - num;
+				num--;
+				memmove(&buf[num], &buf[num+1], wlen);
+				getcmd_putch(CTL_BACKSPACE);
+				putnstr(buf + num, wlen);
+				getcmd_putch(' ');
+				do {
+					getcmd_putch(CTL_BACKSPACE);
+				} while (wlen--);
+				eol_num--;
+			}
+			break;
+		case CTL_CH('p'):
+		case CTL_CH('n'):
+		{
+			char *hline;
+
+			esc_len = 0;
+
+			if (ichar == CTL_CH('p'))
+				hline = hist_prev();
+			else
+				hline = hist_next();
+
+			if (!hline) {
+				getcmd_cbeep();
+				continue;
+			}
+
+			/* nuke the current line */
+			/* first, go home */
+			BEGINNING_OF_LINE();
+
+			/* erase to end of line */
+			ERASE_TO_EOL();
+
+			/* copy new line into place and display */
+			strcpy(buf, hline);
+			eol_num = strlen(buf);
+			REFRESH_TO_EOL();
+			continue;
+		}
+#ifdef CONFIG_AUTO_COMPLETE
+		case '\t': {
+			int num2, col;
+
+			/* do not autocomplete when in the middle */
+			if (num < eol_num) {
+				getcmd_cbeep();
+				break;
+			}
+
+			buf[num] = '\0';
+			col = strlen(prompt) + eol_num;
+			num2 = num;
+			if (cmd_auto_complete(prompt, buf, &num2, &col)) {
+				col = num2 - num;
+				num += col;
+				eol_num += col;
+			}
+			break;
+		}
+#endif
+		default:
+			cread_add_char(ichar, insert, &num, &eol_num, buf,
+				       *len);
+			break;
+		}
+	}
+	*len = eol_num;
+	buf[eol_num] = '\0';	/* lose the newline */
+
+	if (buf[0] && buf[0] != CREAD_HIST_CHAR)
+		cread_add_to_hist(buf);
+	hist_cur = hist_add_idx;
+
+	return 0;
+}
+
+#endif /* CONFIG_CMDLINE_EDITING */
+
+/****************************************************************************/
+
+int readline(const char *const prompt)
+{
+	/*
+	 * If console_buffer isn't 0-length the user will be prompted to modify
+	 * it instead of entering it from scratch as desired.
+	 */
+	console_buffer[0] = '\0';
+
+	return readline_into_buffer(prompt, console_buffer, 0);
+}
+
+
+int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
+{
+	char *p = buffer;
+#ifdef CONFIG_CMDLINE_EDITING
+	unsigned int len = CONFIG_SYS_CBSIZE;
+	int rc;
+	static int initted;
+
+	/*
+	 * History uses a global array which is not
+	 * writable until after relocation to RAM.
+	 * Revert to non-history version if still
+	 * running from flash.
+	 */
+	if (gd->flags & GD_FLG_RELOC) {
+		if (!initted) {
+			hist_init();
+			initted = 1;
+		}
+
+		if (prompt)
+			puts(prompt);
+
+		rc = cread_line(prompt, p, &len, timeout);
+		return rc < 0 ? rc : len;
+
+	} else {
+#endif	/* CONFIG_CMDLINE_EDITING */
+	char *p_buf = p;
+	int	n = 0;				/* buffer index		*/
+	int	plen = 0;			/* prompt length	*/
+	int	col;				/* output column cnt	*/
+	char	c;
+
+	/* print prompt */
+	if (prompt) {
+		plen = strlen(prompt);
+		puts(prompt);
+	}
+	col = plen;
+
+	for (;;) {
+#ifdef CONFIG_BOOT_RETRY_TIME
+		while (!tstc()) {	/* while no incoming data */
+			if (retry_time >= 0 && get_ticks() > endtime)
+				return -2;	/* timed out */
+			WATCHDOG_RESET();
+		}
+#endif
+		WATCHDOG_RESET();	/* Trigger watchdog, if needed */
+
+#ifdef CONFIG_SHOW_ACTIVITY
+		while (!tstc()) {
+			show_activity(0);
+			WATCHDOG_RESET();
+		}
+#endif
+		c = getc();
+
+		/*
+		 * Special character handling
+		 */
+		switch (c) {
+		case '\r':			/* Enter		*/
+		case '\n':
+			*p = '\0';
+			puts("\r\n");
+			return p - p_buf;
+
+		case '\0':			/* nul			*/
+			continue;
+
+		case 0x03:			/* ^C - break		*/
+			p_buf[0] = '\0';	/* discard input */
+			return -1;
+
+		case 0x15:			/* ^U - erase line	*/
+			while (col > plen) {
+				puts(erase_seq);
+				--col;
+			}
+			p = p_buf;
+			n = 0;
+			continue;
+
+		case 0x17:			/* ^W - erase word	*/
+			p = delete_char(p_buf, p, &col, &n, plen);
+			while ((n > 0) && (*p != ' '))
+				p = delete_char(p_buf, p, &col, &n, plen);
+			continue;
+
+		case 0x08:			/* ^H  - backspace	*/
+		case 0x7F:			/* DEL - backspace	*/
+			p = delete_char(p_buf, p, &col, &n, plen);
+			continue;
+
+		default:
+			/*
+			 * Must be a normal character then
+			 */
+			if (n < CONFIG_SYS_CBSIZE-2) {
+				if (c == '\t') {	/* expand TABs */
+#ifdef CONFIG_AUTO_COMPLETE
+					/*
+					 * if auto completion triggered just
+					 * continue
+					 */
+					*p = '\0';
+					if (cmd_auto_complete(prompt,
+							      console_buffer,
+							      &n, &col)) {
+						p = p_buf + n;	/* reset */
+						continue;
+					}
+#endif
+					puts(tab_seq + (col & 07));
+					col += 8 - (col & 07);
+				} else {
+					char buf[2];
+
+					/*
+					 * Echo input using puts() to force an
+					 * LCD flush if we are using an LCD
+					 */
+					++col;
+					buf[0] = c;
+					buf[1] = '\0';
+					puts(buf);
+				}
+				*p++ = c;
+				++n;
+			} else {			/* Buffer full */
+				putc('\a');
+			}
+		}
+	}
+#ifdef CONFIG_CMDLINE_EDITING
+	}
+#endif
+}
+
+#ifdef CONFIG_BOOT_RETRY_TIME
+/***************************************************************************
+ * initialize command line timeout
+ */
+void init_cmd_timeout(void)
+{
+	char *s = getenv("bootretry");
+
+	if (s != NULL)
+		retry_time = (int)simple_strtol(s, NULL, 10);
+	else
+		retry_time =  CONFIG_BOOT_RETRY_TIME;
+
+	if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
+		retry_time = CONFIG_BOOT_RETRY_MIN;
+}
+
+/***************************************************************************
+ * reset command line timeout to retry_time seconds
+ */
+void reset_cmd_timeout(void)
+{
+	endtime = endtick(retry_time);
+}
+
+void bootretry_dont_retry(void)
+{
+	retry_time = -1;
+}
+
+#endif
diff --git a/common/cli_simple.c b/common/cli_simple.c
new file mode 100644
index 0000000..0610615
--- /dev/null
+++ b/common/cli_simple.c
@@ -0,0 +1,338 @@
+/*
+ * (C) Copyright 2000
+ * Wolfgang Denk, DENX Software Engineering, wd at denx.de.
+ *
+ * Add to readline cmdline-editing by
+ * (C) Copyright 2005
+ * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <cli.h>
+#include <linux/ctype.h>
+
+#define DEBUG_PARSER	0	/* set to 1 to debug */
+
+#define debug_parser(fmt, args...)		\
+	debug_cond(DEBUG_PARSER, fmt, ##args)
+
+
+int parse_line(char *line, char *argv[])
+{
+	int nargs = 0;
+
+	debug_parser("%s: \"%s\"\n", __func__, line);
+	while (nargs < CONFIG_SYS_MAXARGS) {
+		/* skip any white space */
+		while (isblank(*line))
+			++line;
+
+		if (*line == '\0') {	/* end of line, no more args	*/
+			argv[nargs] = NULL;
+			debug_parser("%s: nargs=%d\n", __func__, nargs);
+			return nargs;
+		}
+
+		argv[nargs++] = line;	/* begin of argument string	*/
+
+		/* find end of string */
+		while (*line && !isblank(*line))
+			++line;
+
+		if (*line == '\0') {	/* end of line, no more args	*/
+			argv[nargs] = NULL;
+			debug_parser("parse_line: nargs=%d\n", nargs);
+			return nargs;
+		}
+
+		*line++ = '\0';		/* terminate current arg	 */
+	}
+
+	printf("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
+
+	debug_parser("%s: nargs=%d\n", __func__, nargs);
+	return nargs;
+}
+
+static void process_macros(const char *input, char *output)
+{
+	char c, prev;
+	const char *varname_start = NULL;
+	int inputcnt = strlen(input);
+	int outputcnt = CONFIG_SYS_CBSIZE;
+	int state = 0;		/* 0 = waiting for '$'  */
+
+	/* 1 = waiting for '(' or '{' */
+	/* 2 = waiting for ')' or '}' */
+	/* 3 = waiting for '''  */
+	char *output_start = output;
+
+	debug_parser("[PROCESS_MACROS] INPUT len %zd: \"%s\"\n", strlen(input),
+		     input);
+
+	prev = '\0';		/* previous character   */
+
+	while (inputcnt && outputcnt) {
+		c = *input++;
+		inputcnt--;
+
+		if (state != 3) {
+			/* remove one level of escape characters */
+			if ((c == '\\') && (prev != '\\')) {
+				if (inputcnt-- == 0)
+					break;
+				prev = c;
+				c = *input++;
+			}
+		}
+
+		switch (state) {
+		case 0:	/* Waiting for (unescaped) $    */
+			if ((c == '\'') && (prev != '\\')) {
+				state = 3;
+				break;
+			}
+			if ((c == '$') && (prev != '\\')) {
+				state++;
+			} else {
+				*(output++) = c;
+				outputcnt--;
+			}
+			break;
+		case 1:	/* Waiting for (        */
+			if (c == '(' || c == '{') {
+				state++;
+				varname_start = input;
+			} else {
+				state = 0;
+				*(output++) = '$';
+				outputcnt--;
+
+				if (outputcnt) {
+					*(output++) = c;
+					outputcnt--;
+				}
+			}
+			break;
+		case 2:	/* Waiting for )        */
+			if (c == ')' || c == '}') {
+				int i;
+				char envname[CONFIG_SYS_CBSIZE], *envval;
+				/* Varname # of chars */
+				int envcnt = input - varname_start - 1;
+
+				/* Get the varname */
+				for (i = 0; i < envcnt; i++)
+					envname[i] = varname_start[i];
+				envname[i] = 0;
+
+				/* Get its value */
+				envval = getenv(envname);
+
+				/* Copy into the line if it exists */
+				if (envval != NULL)
+					while ((*envval) && outputcnt) {
+						*(output++) = *(envval++);
+						outputcnt--;
+					}
+				/* Look for another '$' */
+				state = 0;
+			}
+			break;
+		case 3:	/* Waiting for '        */
+			if ((c == '\'') && (prev != '\\')) {
+				state = 0;
+			} else {
+				*(output++) = c;
+				outputcnt--;
+			}
+			break;
+		}
+		prev = c;
+	}
+
+	if (outputcnt)
+		*output = 0;
+	else
+		*(output - 1) = 0;
+
+	debug_parser("[PROCESS_MACROS] OUTPUT len %zd: \"%s\"\n",
+		     strlen(output_start), output_start);
+}
+
+ /*
+ * WARNING:
+ *
+ * We must create a temporary copy of the command since the command we get
+ * may be the result from getenv(), which returns a pointer directly to
+ * the environment data, which may change magicly when the command we run
+ * creates or modifies environment variables (like "bootp" does).
+ */
+int cli_simple_run_command(const char *cmd, int flag)
+{
+	char cmdbuf[CONFIG_SYS_CBSIZE];	/* working copy of cmd		*/
+	char *token;			/* start of token in cmdbuf	*/
+	char *sep;			/* end of token (separator) in cmdbuf */
+	char finaltoken[CONFIG_SYS_CBSIZE];
+	char *str = cmdbuf;
+	char *argv[CONFIG_SYS_MAXARGS + 1];	/* NULL terminated	*/
+	int argc, inquotes;
+	int repeatable = 1;
+	int rc = 0;
+
+	debug_parser("[RUN_COMMAND] cmd[%p]=\"", cmd);
+	if (DEBUG_PARSER) {
+		/* use puts - string may be loooong */
+		puts(cmd ? cmd : "NULL");
+		puts("\"\n");
+	}
+	clear_ctrlc();		/* forget any previous Control C */
+
+	if (!cmd || !*cmd)
+		return -1;	/* empty command */
+
+	if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
+		puts("## Command too long!\n");
+		return -1;
+	}
+
+	strcpy(cmdbuf, cmd);
+
+	/* Process separators and check for invalid
+	 * repeatable commands
+	 */
+
+	debug_parser("[PROCESS_SEPARATORS] %s\n", cmd);
+	while (*str) {
+		/*
+		 * Find separator, or string end
+		 * Allow simple escape of ';' by writing "\;"
+		 */
+		for (inquotes = 0, sep = str; *sep; sep++) {
+			if ((*sep == '\'') &&
+			    (*(sep - 1) != '\\'))
+				inquotes = !inquotes;
+
+			if (!inquotes &&
+			    (*sep == ';') &&	/* separator		*/
+			    (sep != str) &&	/* past string start	*/
+			    (*(sep - 1) != '\\'))	/* and NOT escaped */
+				break;
+		}
+
+		/*
+		 * Limit the token to data between separators
+		 */
+		token = str;
+		if (*sep) {
+			str = sep + 1;	/* start of command for next pass */
+			*sep = '\0';
+		} else {
+			str = sep;	/* no more commands for next pass */
+		}
+		debug_parser("token: \"%s\"\n", token);
+
+		/* find macros in this token and replace them */
+		process_macros(token, finaltoken);
+
+		/* Extract arguments */
+		argc = parse_line(finaltoken, argv);
+		if (argc == 0) {
+			rc = -1;	/* no command at all */
+			continue;
+		}
+
+		if (cmd_process(flag, argc, argv, &repeatable, NULL))
+			rc = -1;
+
+		/* Did the user stop this? */
+		if (had_ctrlc())
+			return -1;	/* if stopped then not repeatable */
+	}
+
+	return rc ? rc : repeatable;
+}
+
+void cli_loop(void)
+{
+	static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
+
+	int len;
+	int flag;
+	int rc = 1;
+
+	for (;;) {
+#ifdef CONFIG_BOOT_RETRY_TIME
+		if (rc >= 0) {
+			/* Saw enough of a valid command to
+			 * restart the timeout.
+			 */
+			reset_cmd_timeout();
+		}
+#endif
+		len = readline(CONFIG_SYS_PROMPT);
+
+		flag = 0;	/* assume no special flags for now */
+		if (len > 0)
+			strcpy(lastcommand, console_buffer);
+		else if (len == 0)
+			flag |= CMD_FLAG_REPEAT;
+#ifdef CONFIG_BOOT_RETRY_TIME
+		else if (len == -2) {
+			/* -2 means timed out, retry autoboot
+			 */
+			puts("\nTimed out waiting for command\n");
+# ifdef CONFIG_RESET_TO_RETRY
+			/* Reinit board to run initialization code again */
+			do_reset(NULL, 0, 0, NULL);
+# else
+			return;		/* retry autoboot */
+# endif
+		}
+#endif
+
+		if (len == -1)
+			puts("<INTERRUPT>\n");
+		else
+			rc = run_command(lastcommand, flag);
+
+		if (rc <= 0) {
+			/* invalid command or not repeatable, forget it */
+			lastcommand[0] = 0;
+		}
+	}
+}
+
+int cli_simple_run_command_list(char *cmd, int flag)
+{
+	char *line, *next;
+	int rcode = 0;
+
+	/*
+	 * Break into individual lines, and execute each line; terminate on
+	 * error.
+	 */
+	next = cmd;
+	line = cmd;
+	while (*next) {
+		if (*next == '\n') {
+			*next = '\0';
+			/* run only non-empty commands */
+			if (*line) {
+				debug("** exec: \"%s\"\n", line);
+				if (cli_simple_run_command(line, 0) < 0) {
+					rcode = 1;
+					break;
+				}
+			}
+			line = next + 1;
+		}
+		++next;
+	}
+	if (rcode == 0 && *line)
+		rcode = (cli_simple_run_command(line, 0) >= 0);
+
+	return rcode;
+}
diff --git a/common/main.c b/common/main.c
index 815759a..9470e47 100644
--- a/common/main.c
+++ b/common/main.c
@@ -2,10 +2,6 @@
  * (C) Copyright 2000
  * Wolfgang Denk, DENX Software Engineering, wd at denx.de.
  *
- * Add to readline cmdline-editing by
- * (C) Copyright 2005
- * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
- *
  * SPDX-License-Identifier:	GPL-2.0+
  */
 
@@ -20,8 +16,6 @@
 #include <menu.h>
 #include <post.h>
 #include <version.h>
-#include <watchdog.h>
-#include <linux/ctype.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -33,34 +27,12 @@ void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progre
 
 #define MAX_DELAY_STOP_STR 32
 
-#define DEBUG_PARSER	0	/* set to 1 to debug */
-
-#define debug_parser(fmt, args...)		\
-	debug_cond(DEBUG_PARSER, fmt, ##args)
-
 #ifndef DEBUG_BOOTKEYS
 #define DEBUG_BOOTKEYS 0
 #endif
 #define debug_bootkeys(fmt, args...)		\
 	debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
 
-char        console_buffer[CONFIG_SYS_CBSIZE + 1];	/* console I/O buffer	*/
-
-static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
-static const char erase_seq[] = "\b \b";		/* erase sequence	*/
-static const char   tab_seq[] = "        ";		/* used to expand TABs	*/
-
-#ifdef CONFIG_BOOT_RETRY_TIME
-static uint64_t endtime = 0;  /* must be set, default is instant timeout */
-static int      retry_time = -1; /* -1 so can call readline before main_loop */
-#endif
-
-#define	endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
-
-#ifndef CONFIG_BOOT_RETRY_MIN
-#define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
-#endif
-
 #ifdef CONFIG_MODEM_SUPPORT
 int do_mdm_init = 0;
 extern void mdm_init(void); /* defined in board.c */
@@ -162,7 +134,7 @@ static int abortboot_keyed(int bootdelay)
 #  ifdef CONFIG_BOOT_RETRY_TIME
 				/* don't retry auto boot */
 				if (! delaykey[i].retry)
-					retry_time = -1;
+					bootretry_dont_retry();
 #  endif
 				abort = 1;
 			}
@@ -416,12 +388,6 @@ static void process_boot_delay(void)
 
 void main_loop(void)
 {
-#ifndef CONFIG_SYS_HUSH_PARSER
-	static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
-	int len;
-	int rc = 1;
-	int flag;
-#endif
 #ifdef CONFIG_PREBOOT
 	char *p;
 #endif
@@ -483,943 +449,13 @@ void main_loop(void)
 	/* This point is never reached */
 	for (;;);
 #else
-	for (;;) {
-#ifdef CONFIG_BOOT_RETRY_TIME
-		if (rc >= 0) {
-			/* Saw enough of a valid command to
-			 * restart the timeout.
-			 */
-			reset_cmd_timeout();
-		}
-#endif
-		len = readline (CONFIG_SYS_PROMPT);
-
-		flag = 0;	/* assume no special flags for now */
-		if (len > 0)
-			strcpy (lastcommand, console_buffer);
-		else if (len == 0)
-			flag |= CMD_FLAG_REPEAT;
-#ifdef CONFIG_BOOT_RETRY_TIME
-		else if (len == -2) {
-			/* -2 means timed out, retry autoboot
-			 */
-			puts ("\nTimed out waiting for command\n");
-# ifdef CONFIG_RESET_TO_RETRY
-			/* Reinit board to run initialization code again */
-			do_reset (NULL, 0, 0, NULL);
-# else
-			return;		/* retry autoboot */
-# endif
-		}
-#endif
-
-		if (len == -1)
-			puts ("<INTERRUPT>\n");
-		else
-			rc = run_command(lastcommand, flag);
-
-		if (rc <= 0) {
-			/* invalid command or not repeatable, forget it */
-			lastcommand[0] = 0;
-		}
-	}
+	cli_loop();
 #endif /*CONFIG_SYS_HUSH_PARSER*/
 }
 
-#ifdef CONFIG_BOOT_RETRY_TIME
-/***************************************************************************
- * initialize command line timeout
- */
-void init_cmd_timeout(void)
-{
-	char *s = getenv ("bootretry");
-
-	if (s != NULL)
-		retry_time = (int)simple_strtol(s, NULL, 10);
-	else
-		retry_time =  CONFIG_BOOT_RETRY_TIME;
-
-	if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
-		retry_time = CONFIG_BOOT_RETRY_MIN;
-}
-
-/***************************************************************************
- * reset command line timeout to retry_time seconds
- */
-void reset_cmd_timeout(void)
-{
-	endtime = endtick(retry_time);
-}
-#endif
-
-#ifdef CONFIG_CMDLINE_EDITING
-
-/*
- * cmdline-editing related codes from vivi.
- * Author: Janghoon Lyu <nandy@mizi.com>
- */
-
-#define putnstr(str,n)	do {			\
-		printf ("%.*s", (int)n, str);	\
-	} while (0)
-
-#define CTL_CH(c)		((c) - 'a' + 1)
-#define CTL_BACKSPACE		('\b')
-#define DEL			((char)255)
-#define DEL7			((char)127)
-#define CREAD_HIST_CHAR		('!')
-
-#define getcmd_putch(ch)	putc(ch)
-#define getcmd_getch()		getc()
-#define getcmd_cbeep()		getcmd_putch('\a')
-
-#define HIST_MAX		20
-#define HIST_SIZE		CONFIG_SYS_CBSIZE
-
-static int hist_max;
-static int hist_add_idx;
-static int hist_cur = -1;
-static unsigned hist_num;
-
-static char *hist_list[HIST_MAX];
-static char hist_lines[HIST_MAX][HIST_SIZE + 1];	/* Save room for NULL */
-
-#define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
-
-static void hist_init(void)
-{
-	int i;
-
-	hist_max = 0;
-	hist_add_idx = 0;
-	hist_cur = -1;
-	hist_num = 0;
-
-	for (i = 0; i < HIST_MAX; i++) {
-		hist_list[i] = hist_lines[i];
-		hist_list[i][0] = '\0';
-	}
-}
-
-static void cread_add_to_hist(char *line)
-{
-	strcpy(hist_list[hist_add_idx], line);
-
-	if (++hist_add_idx >= HIST_MAX)
-		hist_add_idx = 0;
-
-	if (hist_add_idx > hist_max)
-		hist_max = hist_add_idx;
-
-	hist_num++;
-}
-
-static char* hist_prev(void)
-{
-	char *ret;
-	int old_cur;
-
-	if (hist_cur < 0)
-		return NULL;
-
-	old_cur = hist_cur;
-	if (--hist_cur < 0)
-		hist_cur = hist_max;
-
-	if (hist_cur == hist_add_idx) {
-		hist_cur = old_cur;
-		ret = NULL;
-	} else
-		ret = hist_list[hist_cur];
-
-	return (ret);
-}
-
-static char* hist_next(void)
-{
-	char *ret;
-
-	if (hist_cur < 0)
-		return NULL;
-
-	if (hist_cur == hist_add_idx)
-		return NULL;
-
-	if (++hist_cur > hist_max)
-		hist_cur = 0;
-
-	if (hist_cur == hist_add_idx) {
-		ret = "";
-	} else
-		ret = hist_list[hist_cur];
-
-	return (ret);
-}
-
-#ifndef CONFIG_CMDLINE_EDITING
-static void cread_print_hist_list(void)
-{
-	int i;
-	unsigned long n;
-
-	n = hist_num - hist_max;
-
-	i = hist_add_idx + 1;
-	while (1) {
-		if (i > hist_max)
-			i = 0;
-		if (i == hist_add_idx)
-			break;
-		printf("%s\n", hist_list[i]);
-		n++;
-		i++;
-	}
-}
-#endif /* CONFIG_CMDLINE_EDITING */
-
-#define BEGINNING_OF_LINE() {			\
-	while (num) {				\
-		getcmd_putch(CTL_BACKSPACE);	\
-		num--;				\
-	}					\
-}
-
-#define ERASE_TO_EOL() {				\
-	if (num < eol_num) {				\
-		printf("%*s", (int)(eol_num - num), ""); \
-		do {					\
-			getcmd_putch(CTL_BACKSPACE);	\
-		} while (--eol_num > num);		\
-	}						\
-}
-
-#define REFRESH_TO_EOL() {			\
-	if (num < eol_num) {			\
-		wlen = eol_num - num;		\
-		putnstr(buf + num, wlen);	\
-		num = eol_num;			\
-	}					\
-}
-
-static void cread_add_char(char ichar, int insert, unsigned long *num,
-	       unsigned long *eol_num, char *buf, unsigned long len)
-{
-	unsigned long wlen;
-
-	/* room ??? */
-	if (insert || *num == *eol_num) {
-		if (*eol_num > len - 1) {
-			getcmd_cbeep();
-			return;
-		}
-		(*eol_num)++;
-	}
-
-	if (insert) {
-		wlen = *eol_num - *num;
-		if (wlen > 1) {
-			memmove(&buf[*num+1], &buf[*num], wlen-1);
-		}
-
-		buf[*num] = ichar;
-		putnstr(buf + *num, wlen);
-		(*num)++;
-		while (--wlen) {
-			getcmd_putch(CTL_BACKSPACE);
-		}
-	} else {
-		/* echo the character */
-		wlen = 1;
-		buf[*num] = ichar;
-		putnstr(buf + *num, wlen);
-		(*num)++;
-	}
-}
-
-static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
-	      unsigned long *eol_num, char *buf, unsigned long len)
-{
-	while (strsize--) {
-		cread_add_char(*str, insert, num, eol_num, buf, len);
-		str++;
-	}
-}
-
-static int cread_line(const char *const prompt, char *buf, unsigned int *len,
-		int timeout)
-{
-	unsigned long num = 0;
-	unsigned long eol_num = 0;
-	unsigned long wlen;
-	char ichar;
-	int insert = 1;
-	int esc_len = 0;
-	char esc_save[8];
-	int init_len = strlen(buf);
-	int first = 1;
-
-	if (init_len)
-		cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
-
-	while (1) {
-#ifdef CONFIG_BOOT_RETRY_TIME
-		while (!tstc()) {	/* while no incoming data */
-			if (retry_time >= 0 && get_ticks() > endtime)
-				return (-2);	/* timed out */
-			WATCHDOG_RESET();
-		}
-#endif
-		if (first && timeout) {
-			uint64_t etime = endtick(timeout);
-
-			while (!tstc()) {	/* while no incoming data */
-				if (get_ticks() >= etime)
-					return -2;	/* timed out */
-				WATCHDOG_RESET();
-			}
-			first = 0;
-		}
-
-		ichar = getcmd_getch();
-
-		if ((ichar == '\n') || (ichar == '\r')) {
-			putc('\n');
-			break;
-		}
-
-		/*
-		 * handle standard linux xterm esc sequences for arrow key, etc.
-		 */
-		if (esc_len != 0) {
-			if (esc_len == 1) {
-				if (ichar == '[') {
-					esc_save[esc_len] = ichar;
-					esc_len = 2;
-				} else {
-					cread_add_str(esc_save, esc_len, insert,
-						      &num, &eol_num, buf, *len);
-					esc_len = 0;
-				}
-				continue;
-			}
-
-			switch (ichar) {
-
-			case 'D':	/* <- key */
-				ichar = CTL_CH('b');
-				esc_len = 0;
-				break;
-			case 'C':	/* -> key */
-				ichar = CTL_CH('f');
-				esc_len = 0;
-				break;	/* pass off to ^F handler */
-			case 'H':	/* Home key */
-				ichar = CTL_CH('a');
-				esc_len = 0;
-				break;	/* pass off to ^A handler */
-			case 'A':	/* up arrow */
-				ichar = CTL_CH('p');
-				esc_len = 0;
-				break;	/* pass off to ^P handler */
-			case 'B':	/* down arrow */
-				ichar = CTL_CH('n');
-				esc_len = 0;
-				break;	/* pass off to ^N handler */
-			default:
-				esc_save[esc_len++] = ichar;
-				cread_add_str(esc_save, esc_len, insert,
-					      &num, &eol_num, buf, *len);
-				esc_len = 0;
-				continue;
-			}
-		}
-
-		switch (ichar) {
-		case 0x1b:
-			if (esc_len == 0) {
-				esc_save[esc_len] = ichar;
-				esc_len = 1;
-			} else {
-				puts("impossible condition #876\n");
-				esc_len = 0;
-			}
-			break;
-
-		case CTL_CH('a'):
-			BEGINNING_OF_LINE();
-			break;
-		case CTL_CH('c'):	/* ^C - break */
-			*buf = '\0';	/* discard input */
-			return (-1);
-		case CTL_CH('f'):
-			if (num < eol_num) {
-				getcmd_putch(buf[num]);
-				num++;
-			}
-			break;
-		case CTL_CH('b'):
-			if (num) {
-				getcmd_putch(CTL_BACKSPACE);
-				num--;
-			}
-			break;
-		case CTL_CH('d'):
-			if (num < eol_num) {
-				wlen = eol_num - num - 1;
-				if (wlen) {
-					memmove(&buf[num], &buf[num+1], wlen);
-					putnstr(buf + num, wlen);
-				}
-
-				getcmd_putch(' ');
-				do {
-					getcmd_putch(CTL_BACKSPACE);
-				} while (wlen--);
-				eol_num--;
-			}
-			break;
-		case CTL_CH('k'):
-			ERASE_TO_EOL();
-			break;
-		case CTL_CH('e'):
-			REFRESH_TO_EOL();
-			break;
-		case CTL_CH('o'):
-			insert = !insert;
-			break;
-		case CTL_CH('x'):
-		case CTL_CH('u'):
-			BEGINNING_OF_LINE();
-			ERASE_TO_EOL();
-			break;
-		case DEL:
-		case DEL7:
-		case 8:
-			if (num) {
-				wlen = eol_num - num;
-				num--;
-				memmove(&buf[num], &buf[num+1], wlen);
-				getcmd_putch(CTL_BACKSPACE);
-				putnstr(buf + num, wlen);
-				getcmd_putch(' ');
-				do {
-					getcmd_putch(CTL_BACKSPACE);
-				} while (wlen--);
-				eol_num--;
-			}
-			break;
-		case CTL_CH('p'):
-		case CTL_CH('n'):
-		{
-			char * hline;
-
-			esc_len = 0;
-
-			if (ichar == CTL_CH('p'))
-				hline = hist_prev();
-			else
-				hline = hist_next();
-
-			if (!hline) {
-				getcmd_cbeep();
-				continue;
-			}
-
-			/* nuke the current line */
-			/* first, go home */
-			BEGINNING_OF_LINE();
-
-			/* erase to end of line */
-			ERASE_TO_EOL();
-
-			/* copy new line into place and display */
-			strcpy(buf, hline);
-			eol_num = strlen(buf);
-			REFRESH_TO_EOL();
-			continue;
-		}
-#ifdef CONFIG_AUTO_COMPLETE
-		case '\t': {
-			int num2, col;
-
-			/* do not autocomplete when in the middle */
-			if (num < eol_num) {
-				getcmd_cbeep();
-				break;
-			}
-
-			buf[num] = '\0';
-			col = strlen(prompt) + eol_num;
-			num2 = num;
-			if (cmd_auto_complete(prompt, buf, &num2, &col)) {
-				col = num2 - num;
-				num += col;
-				eol_num += col;
-			}
-			break;
-		}
-#endif
-		default:
-			cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
-			break;
-		}
-	}
-	*len = eol_num;
-	buf[eol_num] = '\0';	/* lose the newline */
-
-	if (buf[0] && buf[0] != CREAD_HIST_CHAR)
-		cread_add_to_hist(buf);
-	hist_cur = hist_add_idx;
-
-	return 0;
-}
-
-#endif /* CONFIG_CMDLINE_EDITING */
-
 /****************************************************************************/
 
 /*
- * Prompt for input and read a line.
- * If  CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
- * time out when time goes past endtime (timebase time in ticks).
- * Return:	number of read characters
- *		-1 if break
- *		-2 if timed out
- */
-int readline (const char *const prompt)
-{
-	/*
-	 * If console_buffer isn't 0-length the user will be prompted to modify
-	 * it instead of entering it from scratch as desired.
-	 */
-	console_buffer[0] = '\0';
-
-	return readline_into_buffer(prompt, console_buffer, 0);
-}
-
-
-int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
-{
-	char *p = buffer;
-#ifdef CONFIG_CMDLINE_EDITING
-	unsigned int len = CONFIG_SYS_CBSIZE;
-	int rc;
-	static int initted = 0;
-
-	/*
-	 * History uses a global array which is not
-	 * writable until after relocation to RAM.
-	 * Revert to non-history version if still
-	 * running from flash.
-	 */
-	if (gd->flags & GD_FLG_RELOC) {
-		if (!initted) {
-			hist_init();
-			initted = 1;
-		}
-
-		if (prompt)
-			puts (prompt);
-
-		rc = cread_line(prompt, p, &len, timeout);
-		return rc < 0 ? rc : len;
-
-	} else {
-#endif	/* CONFIG_CMDLINE_EDITING */
-	char * p_buf = p;
-	int	n = 0;				/* buffer index		*/
-	int	plen = 0;			/* prompt length	*/
-	int	col;				/* output column cnt	*/
-	char	c;
-
-	/* print prompt */
-	if (prompt) {
-		plen = strlen (prompt);
-		puts (prompt);
-	}
-	col = plen;
-
-	for (;;) {
-#ifdef CONFIG_BOOT_RETRY_TIME
-		while (!tstc()) {	/* while no incoming data */
-			if (retry_time >= 0 && get_ticks() > endtime)
-				return (-2);	/* timed out */
-			WATCHDOG_RESET();
-		}
-#endif
-		WATCHDOG_RESET();		/* Trigger watchdog, if needed */
-
-#ifdef CONFIG_SHOW_ACTIVITY
-		while (!tstc()) {
-			show_activity(0);
-			WATCHDOG_RESET();
-		}
-#endif
-		c = getc();
-
-		/*
-		 * Special character handling
-		 */
-		switch (c) {
-		case '\r':			/* Enter		*/
-		case '\n':
-			*p = '\0';
-			puts ("\r\n");
-			return p - p_buf;
-
-		case '\0':			/* nul			*/
-			continue;
-
-		case 0x03:			/* ^C - break		*/
-			p_buf[0] = '\0';	/* discard input */
-			return -1;
-
-		case 0x15:			/* ^U - erase line	*/
-			while (col > plen) {
-				puts (erase_seq);
-				--col;
-			}
-			p = p_buf;
-			n = 0;
-			continue;
-
-		case 0x17:			/* ^W - erase word	*/
-			p=delete_char(p_buf, p, &col, &n, plen);
-			while ((n > 0) && (*p != ' ')) {
-				p=delete_char(p_buf, p, &col, &n, plen);
-			}
-			continue;
-
-		case 0x08:			/* ^H  - backspace	*/
-		case 0x7F:			/* DEL - backspace	*/
-			p=delete_char(p_buf, p, &col, &n, plen);
-			continue;
-
-		default:
-			/*
-			 * Must be a normal character then
-			 */
-			if (n < CONFIG_SYS_CBSIZE-2) {
-				if (c == '\t') {	/* expand TABs */
-#ifdef CONFIG_AUTO_COMPLETE
-					/* if auto completion triggered just continue */
-					*p = '\0';
-					if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
-						p = p_buf + n;	/* reset */
-						continue;
-					}
-#endif
-					puts (tab_seq+(col&07));
-					col += 8 - (col&07);
-				} else {
-					char buf[2];
-
-					/*
-					 * Echo input using puts() to force an
-					 * LCD flush if we are using an LCD
-					 */
-					++col;
-					buf[0] = c;
-					buf[1] = '\0';
-					puts(buf);
-				}
-				*p++ = c;
-				++n;
-			} else {			/* Buffer full		*/
-				putc ('\a');
-			}
-		}
-	}
-#ifdef CONFIG_CMDLINE_EDITING
-	}
-#endif
-}
-
-/****************************************************************************/
-
-static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
-{
-	char *s;
-
-	if (*np == 0) {
-		return (p);
-	}
-
-	if (*(--p) == '\t') {			/* will retype the whole line	*/
-		while (*colp > plen) {
-			puts (erase_seq);
-			(*colp)--;
-		}
-		for (s=buffer; s<p; ++s) {
-			if (*s == '\t') {
-				puts (tab_seq+((*colp) & 07));
-				*colp += 8 - ((*colp) & 07);
-			} else {
-				++(*colp);
-				putc (*s);
-			}
-		}
-	} else {
-		puts (erase_seq);
-		(*colp)--;
-	}
-	(*np)--;
-	return (p);
-}
-
-/****************************************************************************/
-
-int parse_line (char *line, char *argv[])
-{
-	int nargs = 0;
-
-	debug_parser("parse_line: \"%s\"\n", line);
-	while (nargs < CONFIG_SYS_MAXARGS) {
-
-		/* skip any white space */
-		while (isblank(*line))
-			++line;
-
-		if (*line == '\0') {	/* end of line, no more args	*/
-			argv[nargs] = NULL;
-			debug_parser("parse_line: nargs=%d\n", nargs);
-			return nargs;
-		}
-
-		argv[nargs++] = line;	/* begin of argument string	*/
-
-		/* find end of string */
-		while (*line && !isblank(*line))
-			++line;
-
-		if (*line == '\0') {	/* end of line, no more args	*/
-			argv[nargs] = NULL;
-			debug_parser("parse_line: nargs=%d\n", nargs);
-			return nargs;
-		}
-
-		*line++ = '\0';		/* terminate current arg	 */
-	}
-
-	printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
-
-	debug_parser("parse_line: nargs=%d\n", nargs);
-	return (nargs);
-}
-
-/****************************************************************************/
-
-#ifndef CONFIG_SYS_HUSH_PARSER
-static void process_macros (const char *input, char *output)
-{
-	char c, prev;
-	const char *varname_start = NULL;
-	int inputcnt = strlen (input);
-	int outputcnt = CONFIG_SYS_CBSIZE;
-	int state = 0;		/* 0 = waiting for '$'  */
-
-	/* 1 = waiting for '(' or '{' */
-	/* 2 = waiting for ')' or '}' */
-	/* 3 = waiting for '''  */
-	char *output_start = output;
-
-	debug_parser("[PROCESS_MACROS] INPUT len %zd: \"%s\"\n", strlen(input),
-		     input);
-
-	prev = '\0';		/* previous character   */
-
-	while (inputcnt && outputcnt) {
-		c = *input++;
-		inputcnt--;
-
-		if (state != 3) {
-			/* remove one level of escape characters */
-			if ((c == '\\') && (prev != '\\')) {
-				if (inputcnt-- == 0)
-					break;
-				prev = c;
-				c = *input++;
-			}
-		}
-
-		switch (state) {
-		case 0:	/* Waiting for (unescaped) $    */
-			if ((c == '\'') && (prev != '\\')) {
-				state = 3;
-				break;
-			}
-			if ((c == '$') && (prev != '\\')) {
-				state++;
-			} else {
-				*(output++) = c;
-				outputcnt--;
-			}
-			break;
-		case 1:	/* Waiting for (        */
-			if (c == '(' || c == '{') {
-				state++;
-				varname_start = input;
-			} else {
-				state = 0;
-				*(output++) = '$';
-				outputcnt--;
-
-				if (outputcnt) {
-					*(output++) = c;
-					outputcnt--;
-				}
-			}
-			break;
-		case 2:	/* Waiting for )        */
-			if (c == ')' || c == '}') {
-				int i;
-				char envname[CONFIG_SYS_CBSIZE], *envval;
-				int envcnt = input - varname_start - 1;	/* Varname # of chars */
-
-				/* Get the varname */
-				for (i = 0; i < envcnt; i++) {
-					envname[i] = varname_start[i];
-				}
-				envname[i] = 0;
-
-				/* Get its value */
-				envval = getenv (envname);
-
-				/* Copy into the line if it exists */
-				if (envval != NULL)
-					while ((*envval) && outputcnt) {
-						*(output++) = *(envval++);
-						outputcnt--;
-					}
-				/* Look for another '$' */
-				state = 0;
-			}
-			break;
-		case 3:	/* Waiting for '        */
-			if ((c == '\'') && (prev != '\\')) {
-				state = 0;
-			} else {
-				*(output++) = c;
-				outputcnt--;
-			}
-			break;
-		}
-		prev = c;
-	}
-
-	if (outputcnt)
-		*output = 0;
-	else
-		*(output - 1) = 0;
-
-	debug_parser("[PROCESS_MACROS] OUTPUT len %zd: \"%s\"\n",
-		     strlen(output_start), output_start);
-}
-
-/****************************************************************************
- * returns:
- *	1  - command executed, repeatable
- *	0  - command executed but not repeatable, interrupted commands are
- *	     always considered not repeatable
- *	-1 - not executed (unrecognized, bootd recursion or too many args)
- *           (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
- *           considered unrecognized)
- *
- * WARNING:
- *
- * We must create a temporary copy of the command since the command we get
- * may be the result from getenv(), which returns a pointer directly to
- * the environment data, which may change magicly when the command we run
- * creates or modifies environment variables (like "bootp" does).
- */
-static int builtin_run_command(const char *cmd, int flag)
-{
-	char cmdbuf[CONFIG_SYS_CBSIZE];	/* working copy of cmd		*/
-	char *token;			/* start of token in cmdbuf	*/
-	char *sep;			/* end of token (separator) in cmdbuf */
-	char finaltoken[CONFIG_SYS_CBSIZE];
-	char *str = cmdbuf;
-	char *argv[CONFIG_SYS_MAXARGS + 1];	/* NULL terminated	*/
-	int argc, inquotes;
-	int repeatable = 1;
-	int rc = 0;
-
-	debug_parser("[RUN_COMMAND] cmd[%p]=\"", cmd);
-	if (DEBUG_PARSER) {
-		/* use puts - string may be loooong */
-		puts(cmd ? cmd : "NULL");
-		puts("\"\n");
-	}
-	clear_ctrlc();		/* forget any previous Control C */
-
-	if (!cmd || !*cmd) {
-		return -1;	/* empty command */
-	}
-
-	if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
-		puts ("## Command too long!\n");
-		return -1;
-	}
-
-	strcpy (cmdbuf, cmd);
-
-	/* Process separators and check for invalid
-	 * repeatable commands
-	 */
-
-	debug_parser("[PROCESS_SEPARATORS] %s\n", cmd);
-	while (*str) {
-
-		/*
-		 * Find separator, or string end
-		 * Allow simple escape of ';' by writing "\;"
-		 */
-		for (inquotes = 0, sep = str; *sep; sep++) {
-			if ((*sep=='\'') &&
-			    (*(sep-1) != '\\'))
-				inquotes=!inquotes;
-
-			if (!inquotes &&
-			    (*sep == ';') &&	/* separator		*/
-			    ( sep != str) &&	/* past string start	*/
-			    (*(sep-1) != '\\'))	/* and NOT escaped	*/
-				break;
-		}
-
-		/*
-		 * Limit the token to data between separators
-		 */
-		token = str;
-		if (*sep) {
-			str = sep + 1;	/* start of command for next pass */
-			*sep = '\0';
-		}
-		else
-			str = sep;	/* no more commands for next pass */
-		debug_parser("token: \"%s\"\n", token);
-
-		/* find macros in this token and replace them */
-		process_macros (token, finaltoken);
-
-		/* Extract arguments */
-		if ((argc = parse_line (finaltoken, argv)) == 0) {
-			rc = -1;	/* no command at all */
-			continue;
-		}
-
-		if (cmd_process(flag, argc, argv, &repeatable, NULL))
-			rc = -1;
-
-		/* Did the user stop this? */
-		if (had_ctrlc ())
-			return -1;	/* if stopped then not repeatable */
-	}
-
-	return rc ? rc : repeatable;
-}
-#endif
-
-/*
  * Run a command using the selected parser.
  *
  * @param cmd	Command to run
@@ -1430,10 +466,10 @@ int run_command(const char *cmd, int flag)
 {
 #ifndef CONFIG_SYS_HUSH_PARSER
 	/*
-	 * builtin_run_command can return 0 or 1 for success, so clean up
+	 * cli_run_command can return 0 or 1 for success, so clean up
 	 * its result.
 	 */
-	if (builtin_run_command(cmd, flag) == -1)
+	if (cli_simple_run_command(cmd, flag) == -1)
 		return 1;
 
 	return 0;
@@ -1443,49 +479,6 @@ int run_command(const char *cmd, int flag)
 #endif
 }
 
-#ifndef CONFIG_SYS_HUSH_PARSER
-/**
- * Execute a list of command separated by ; or \n using the built-in parser.
- *
- * This function cannot take a const char * for the command, since if it
- * finds newlines in the string, it replaces them with \0.
- *
- * @param cmd	String containing list of commands
- * @param flag	Execution flags (CMD_FLAG_...)
- * @return 0 on success, or != 0 on error.
- */
-static int builtin_run_command_list(char *cmd, int flag)
-{
-	char *line, *next;
-	int rcode = 0;
-
-	/*
-	 * Break into individual lines, and execute each line; terminate on
-	 * error.
-	 */
-	line = next = cmd;
-	while (*next) {
-		if (*next == '\n') {
-			*next = '\0';
-			/* run only non-empty commands */
-			if (*line) {
-				debug("** exec: \"%s\"\n", line);
-				if (builtin_run_command(line, 0) < 0) {
-					rcode = 1;
-					break;
-				}
-			}
-			line = next + 1;
-		}
-		++next;
-	}
-	if (rcode == 0 && *line)
-		rcode = (builtin_run_command(line, 0) >= 0);
-
-	return rcode;
-}
-#endif
-
 int run_command_list(const char *cmd, int len, int flag)
 {
 	int need_buff = 1;
@@ -1519,7 +512,7 @@ int run_command_list(const char *cmd, int len, int flag)
 	 * doing a malloc() which is actually required only in a case that
 	 * is pretty rare.
 	 */
-	rcode = builtin_run_command_list(buff, flag);
+	rcode = cli_simple_run_command_list(buff, flag);
 	if (need_buff)
 		free(buff);
 #endif
diff --git a/include/cli.h b/include/cli.h
index 0075bd4..b04539f 100644
--- a/include/cli.h
+++ b/include/cli.h
@@ -99,4 +99,9 @@ int readline_into_buffer(const char *const prompt, char *buffer, int timeout);
  */
 int parse_line(char *line, char *argv[]);
 
+/** bootretry_dont_retry() - Indicate that we should not retry the boot */
+void bootretry_dont_retry(void);
+
+#define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
+
 #endif
-- 
1.9.1.423.g4596e3a

^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [U-Boot] [PATCH 05/14] Add cli_ prefix to readline functions
  2014-04-11  2:01 [U-Boot] [PATCH 0/14] Split out code from the enormous main.c Simon Glass
                   ` (3 preceding siblings ...)
  2014-04-11  2:01 ` [U-Boot] [PATCH 04/14] Split out simple parser and readline into separate files Simon Glass
@ 2014-04-11  2:01 ` Simon Glass
  2014-05-30 15:27   ` Tom Rini
  2014-04-11  2:01 ` [U-Boot] [PATCH 06/14] Move autoboot code to autoboot.c Simon Glass
                   ` (9 subsequent siblings)
  14 siblings, 1 reply; 30+ messages in thread
From: Simon Glass @ 2014-04-11  2:01 UTC (permalink / raw)
  To: u-boot

This makes it clear where the code resides.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 board/ait/cam_enc_4xx/cam_enc_4xx.c |  2 +-
 board/amcc/yucca/cmd_yucca.c        | 20 ++++++++++----------
 board/eltec/elppc/misc.c            | 14 +++++++-------
 board/eltec/mhpc/mhpc.c             | 12 ++++++------
 board/hymod/hymod.c                 |  2 +-
 board/hymod/input.c                 |  4 ++--
 common/cli_hush.c                   |  4 ++--
 common/cli_readline.c               |  7 ++++---
 common/cli_simple.c                 |  6 +++---
 common/cmd_bedbug.c                 | 28 ++++++++++++++--------------
 common/cmd_dcr.c                    |  2 +-
 common/cmd_i2c.c                    |  2 +-
 common/cmd_mem.c                    |  2 +-
 common/cmd_nvedit.c                 |  4 ++--
 common/cmd_pci.c                    |  2 +-
 common/menu.c                       |  5 +++--
 drivers/ddr/fsl/interactive.c       |  7 ++++---
 include/cli.h                       |  7 ++++---
 18 files changed, 67 insertions(+), 63 deletions(-)

diff --git a/board/ait/cam_enc_4xx/cam_enc_4xx.c b/board/ait/cam_enc_4xx/cam_enc_4xx.c
index 7c26ba0..9aa1d7a 100644
--- a/board/ait/cam_enc_4xx/cam_enc_4xx.c
+++ b/board/ait/cam_enc_4xx/cam_enc_4xx.c
@@ -777,7 +777,7 @@ static void ait_menu_read_env(char *name)
 
 	sprintf(output, "%s old: %s value: ", name, getenv(name));
 	memset(cbuf, 0, CONFIG_SYS_CBSIZE);
-	readret = readline_into_buffer(output, cbuf, 0);
+	readret = cli_readline_into_buffer(output, cbuf, 0);
 
 	if (readret >= 0) {
 		ret = setenv(name, cbuf);
diff --git a/board/amcc/yucca/cmd_yucca.c b/board/amcc/yucca/cmd_yucca.c
index 3257c29..c1724bf 100644
--- a/board/amcc/yucca/cmd_yucca.c
+++ b/board/amcc/yucca/cmd_yucca.c
@@ -52,7 +52,7 @@ static int setBootStrapClock(cmd_tbl_t *cmdtp, int incrflag, int flag,
 
 	do {
 		printf("enter sys clock frequency 33 or 66 MHz or quit to abort\n");
-		nbytes = readline (" ? ");
+		nbytes = cli_readline(" ? ");
 
 		if (strcmp(console_buffer, "quit") == 0)
 			return 0;
@@ -75,7 +75,7 @@ static int setBootStrapClock(cmd_tbl_t *cmdtp, int incrflag, int flag,
 			printf("enter cpu clock frequency 400, 500, 533 MHz or quit to abort\n");
 #endif
 		}
-		nbytes = readline (" ? ");
+		nbytes = cli_readline(" ? ");
 
 		if (strcmp(console_buffer, "quit") == 0)
 			return 0;
@@ -119,7 +119,7 @@ static int setBootStrapClock(cmd_tbl_t *cmdtp, int incrflag, int flag,
 				printf("enter plb clock frequency 133, 166 MHz or quit to abort\n");
 
 #endif
-			nbytes = readline (" ? ");
+			nbytes = cli_readline(" ? ");
 
 			if (strcmp(console_buffer, "quit") == 0)
 				return 0;
@@ -143,7 +143,7 @@ static int setBootStrapClock(cmd_tbl_t *cmdtp, int incrflag, int flag,
 
 	do {
 		printf("enter Pci-X clock frequency 33, 66, 100 or 133 MHz or quit to abort\n");
-		nbytes = readline (" ? ");
+		nbytes = cli_readline(" ? ");
 
 		if (strcmp(console_buffer, "quit") == 0)
 			return 0;
@@ -164,13 +164,13 @@ static int setBootStrapClock(cmd_tbl_t *cmdtp, int incrflag, int flag,
 	printf("Pci-X clk = %s MHz\n", pcixClock);
 
 	do {
-		printf("\npress [y] to write I2C bootstrap \n");
-		printf("or [n] to abort.  \n");
-		printf("Don't forget to set board switches \n");
-		printf("according to your choice before re-starting \n");
-		printf("(refer to 440spe_uboot_kit_um_1_01.pdf) \n");
+		printf("\npress [y] to write I2C bootstrap\n");
+		printf("or [n] to abort.\n");
+		printf("Don't forget to set board switches\n");
+		printf("according to your choice before re-starting\n");
+		printf("(refer to 440spe_uboot_kit_um_1_01.pdf)\n");
 
-		nbytes = readline (" ? ");
+		nbytes = cli_readline(" ? ");
 		if (strcmp(console_buffer, "n") == 0)
 			return 0;
 
diff --git a/board/eltec/elppc/misc.c b/board/eltec/elppc/misc.c
index 70d1f9a..2acf800 100644
--- a/board/eltec/elppc/misc.c
+++ b/board/eltec/elppc/misc.c
@@ -114,7 +114,7 @@ int misc_init_r (void)
 		printf ("Press key:\n  <c> to copy current revision info to nvram.\n");
 		printf ("  <r> to reenter revision info.\n");
 		printf ("=> ");
-		if (0 != readline (NULL)) {
+		if (0 != cli_readline(NULL)) {
 			switch ((char) toupper (console_buffer[0])) {
 			case 'C':
 				copyNv = 1;
@@ -131,7 +131,7 @@ int misc_init_r (void)
 		memcpy (buf, &eerev.revision[0][0], 14);	/* save all revision info */
 		printf ("Enter revision number (0-9): %c  ",
 			eerev.revision[0][0]);
-		if (0 != readline (NULL)) {
+		if (0 != cli_readline(NULL)) {
 			eerev.revision[0][0] =
 				(char) toupper (console_buffer[0]);
 			memcpy (&eerev.revision[1][0], buf, 12);	/* shift rest of rev info */
@@ -139,14 +139,14 @@ int misc_init_r (void)
 
 		printf ("Enter revision character (A-Z): %c  ",
 			eerev.revision[0][1]);
-		if (1 == readline (NULL)) {
+		if (1 == cli_readline(NULL)) {
 			eerev.revision[0][1] =
 				(char) toupper (console_buffer[0]);
 		}
 
 		printf ("Enter board name (V-XXXX-XXXX): %s  ",
 			(char *) &eerev.board);
-		if (11 == readline (NULL)) {
+		if (11 == cli_readline(NULL)) {
 			for (i = 0; i < 11; i++)
 				eerev.board[i] =
 					(char) toupper (console_buffer[i]);
@@ -154,14 +154,14 @@ int misc_init_r (void)
 		}
 
 		printf ("Enter serial number: %s ", (char *) &eerev.serial);
-		if (6 == readline (NULL)) {
+		if (6 == cli_readline(NULL)) {
 			for (i = 0; i < 6; i++)
 				eerev.serial[i] = console_buffer[i];
 			eerev.serial[6] = '\0';
 		}
 
 		printf ("Enter ether node ID with leading zero (HEX): %02x%02x%02x%02x%02x%02x  ", eerev.etheraddr[0], eerev.etheraddr[1], eerev.etheraddr[2], eerev.etheraddr[3], eerev.etheraddr[4], eerev.etheraddr[5]);
-		if (12 == readline (NULL)) {
+		if (12 == cli_readline(NULL)) {
 			for (i = 0; i < 12; i += 2)
 				eerev.etheraddr[i >> 1] =
 					(char) (16 *
@@ -176,7 +176,7 @@ int misc_init_r (void)
 		l = strlen ((char *) &eerev.text);
 		printf ("Add to text section (max 64 chr): %s ",
 			(char *) &eerev.text);
-		if (0 != readline (NULL)) {
+		if (0 != cli_readline(NULL)) {
 			for (i = l; i < 63; i++)
 				eerev.text[i] = console_buffer[i - l];
 			eerev.text[63] = '\0';
diff --git a/board/eltec/mhpc/mhpc.c b/board/eltec/mhpc/mhpc.c
index ff9e0ab..5781b2a 100644
--- a/board/eltec/mhpc/mhpc.c
+++ b/board/eltec/mhpc/mhpc.c
@@ -147,21 +147,21 @@ int misc_init_r (void)
 	if (strncmp ((char *) &mhpcRevInfo.board[2], "MHPC", 4) != 0) {
 		printf ("Enter revision number (0-9): %c  ",
 			mhpcRevInfo.revision[0]);
-		if (0 != readline (NULL)) {
+		if (0 != cli_readline(NULL)) {
 			mhpcRevInfo.revision[0] =
 				(char) toupper (console_buffer[0]);
 		}
 
 		printf ("Enter revision character (A-Z): %c  ",
 			mhpcRevInfo.revision[1]);
-		if (1 == readline (NULL)) {
+		if (1 == cli_readline(NULL)) {
 			mhpcRevInfo.revision[1] =
 				(char) toupper (console_buffer[0]);
 		}
 
 		printf ("Enter board name (V-XXXX-XXXX): %s  ",
 			(char *) &mhpcRevInfo.board);
-		if (11 == readline (NULL)) {
+		if (11 == cli_readline(NULL)) {
 			for (i = 0; i < 11; i++) {
 				mhpcRevInfo.board[i] =
 					(char) toupper (console_buffer[i]);
@@ -178,7 +178,7 @@ int misc_init_r (void)
 		do {
 			printf ("\nEnter sensor number (0-255): %d  ",
 				(int) mhpcRevInfo.sensor);
-			if (0 != readline (NULL)) {
+			if (0 != cli_readline(NULL)) {
 				mhpcRevInfo.sensor =
 					(unsigned char)
 					simple_strtoul (console_buffer, NULL,
@@ -188,7 +188,7 @@ int misc_init_r (void)
 
 		printf ("Enter serial number: %s ",
 			(char *) &mhpcRevInfo.serial);
-		if (6 == readline (NULL)) {
+		if (6 == cli_readline(NULL)) {
 			for (i = 0; i < 6; i++) {
 				mhpcRevInfo.serial[i] = console_buffer[i];
 			}
@@ -196,7 +196,7 @@ int misc_init_r (void)
 		}
 
 		printf ("Enter ether node ID with leading zero (HEX): %02x%02x%02x%02x%02x%02x  ", mhpcRevInfo.etheraddr[0], mhpcRevInfo.etheraddr[1], mhpcRevInfo.etheraddr[2], mhpcRevInfo.etheraddr[3], mhpcRevInfo.etheraddr[4], mhpcRevInfo.etheraddr[5]);
-		if (12 == readline (NULL)) {
+		if (12 == cli_readline(NULL)) {
 			for (i = 0; i < 12; i += 2) {
 				mhpcRevInfo.etheraddr[i >> 1] =
 					(char) (16 *
diff --git a/board/hymod/hymod.c b/board/hymod/hymod.c
index ea49e26..55ffd67 100644
--- a/board/hymod/hymod.c
+++ b/board/hymod/hymod.c
@@ -416,7 +416,7 @@ last_stage_init (void)
 
 #ifdef CONFIG_BOOT_RETRY_TIME
 	/*
-	 * we use the readline () function, but we also want
+	 * we use the cli_readline() function, but we also want
 	 * command timeout enabled
 	 */
 	init_cmd_timeout ();
diff --git a/board/hymod/input.c b/board/hymod/input.c
index 23d3f19..59ad6aa 100644
--- a/board/hymod/input.c
+++ b/board/hymod/input.c
@@ -19,7 +19,7 @@ hymod_get_serno (const char *prompt)
 		reset_cmd_timeout ();
 #endif
 
-		n = readline (prompt);
+		n = cli_readline(prompt);
 
 		if (n < 0)
 			return (n);
@@ -47,7 +47,7 @@ hymod_get_ethaddr (void)
 		reset_cmd_timeout ();
 #endif
 
-		n = readline ("Enter board ethernet address: ");
+		n = cli_readline("Enter board ethernet address: ");
 
 		if (n < 0)
 			return (n);
diff --git a/common/cli_hush.c b/common/cli_hush.c
index 48cec88..c6bb30b 100644
--- a/common/cli_hush.c
+++ b/common/cli_hush.c
@@ -1007,9 +1007,9 @@ static void get_user_input(struct in_str *i)
 #endif
 	i->__promptme = 1;
 	if (i->promptmode == 1) {
-		n = readline(CONFIG_SYS_PROMPT);
+		n = cli_readline(CONFIG_SYS_PROMPT);
 	} else {
-		n = readline(CONFIG_SYS_PROMPT_HUSH_PS2);
+		n = cli_readline(CONFIG_SYS_PROMPT_HUSH_PS2);
 	}
 #ifdef CONFIG_BOOT_RETRY_TIME
 	if (n == -2) {
diff --git a/common/cli_readline.c b/common/cli_readline.c
index cea0b7c..df446b8 100644
--- a/common/cli_readline.c
+++ b/common/cli_readline.c
@@ -484,7 +484,7 @@ static int cread_line(const char *const prompt, char *buf, unsigned int *len,
 
 /****************************************************************************/
 
-int readline(const char *const prompt)
+int cli_readline(const char *const prompt)
 {
 	/*
 	 * If console_buffer isn't 0-length the user will be prompted to modify
@@ -492,11 +492,12 @@ int readline(const char *const prompt)
 	 */
 	console_buffer[0] = '\0';
 
-	return readline_into_buffer(prompt, console_buffer, 0);
+	return cli_readline_into_buffer(prompt, console_buffer, 0);
 }
 
 
-int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
+int cli_readline_into_buffer(const char *const prompt, char *buffer,
+			     int timeout)
 {
 	char *p = buffer;
 #ifdef CONFIG_CMDLINE_EDITING
diff --git a/common/cli_simple.c b/common/cli_simple.c
index 0610615..3039cfc 100644
--- a/common/cli_simple.c
+++ b/common/cli_simple.c
@@ -19,7 +19,7 @@
 	debug_cond(DEBUG_PARSER, fmt, ##args)
 
 
-int parse_line(char *line, char *argv[])
+int cli_simple_parse_line(char *line, char *argv[])
 {
 	int nargs = 0;
 
@@ -238,7 +238,7 @@ int cli_simple_run_command(const char *cmd, int flag)
 		process_macros(token, finaltoken);
 
 		/* Extract arguments */
-		argc = parse_line(finaltoken, argv);
+		argc = cli_simple_parse_line(finaltoken, argv);
 		if (argc == 0) {
 			rc = -1;	/* no command at all */
 			continue;
@@ -272,7 +272,7 @@ void cli_loop(void)
 			reset_cmd_timeout();
 		}
 #endif
-		len = readline(CONFIG_SYS_PROMPT);
+		len = cli_readline(CONFIG_SYS_PROMPT);
 
 		flag = 0;	/* assume no special flags for now */
 		if (len > 0)
diff --git a/common/cmd_bedbug.c b/common/cmd_bedbug.c
index f1a70ef..bdcf712 100644
--- a/common/cmd_bedbug.c
+++ b/common/cmd_bedbug.c
@@ -20,7 +20,7 @@ extern int run_command __P ((const char *, int));
 ulong dis_last_addr = 0;	/* Last address disassembled   */
 ulong dis_last_len = 20;	/* Default disassembler length */
 CPU_DEBUG_CTX bug_ctx;		/* Bedbug context structure    */
-\f
+
 
 /* ======================================================================
  * U-Boot's puts function does not append a newline, so the bedbug stuff
@@ -34,7 +34,7 @@ int bedbug_puts (const char *str)
 	printf ("%s\r\n", str);
 	return 0;
 }				/* bedbug_puts */
-\f
+
 
 
 /* ======================================================================
@@ -66,7 +66,7 @@ void bedbug_init (void)
 
 	return;
 }				/* bedbug_init */
-\f
+
 
 
 /* ======================================================================
@@ -107,7 +107,7 @@ int do_bedbug_dis (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
 U_BOOT_CMD (ds, 3, 1, do_bedbug_dis,
 	    "disassemble memory",
 	    "ds <address> [# instructions]");
-\f
+
 /* ======================================================================
  * Entry point from the interpreter to the assembler.  Assembles
  * instructions in consecutive memory locations until a '.' (period) is
@@ -135,7 +135,7 @@ int do_bedbug_asm (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
 			F_RADHEX);
 
 		sprintf (prompt, "%08lx:    ", mem_addr);
-		readline (prompt);
+		cli_readline(prompt);
 
 		if (console_buffer[0] && strcmp (console_buffer, ".")) {
 			if ((instr =
@@ -157,7 +157,7 @@ int do_bedbug_asm (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
 
 U_BOOT_CMD (as, 2, 0, do_bedbug_asm,
 	    "assemble memory", "as <address>");
-\f
+
 /* ======================================================================
  * Used to set a break point from the interpreter.  Simply calls into the
  * CPU-specific break point set routine.
@@ -178,7 +178,7 @@ U_BOOT_CMD (break, 3, 0, do_bedbug_break,
 	    "break <address> - Break at an address\n"
 	    "break off <bp#> - Disable breakpoint.\n"
 	    "break show      - List breakpoints.");
-\f
+
 /* ======================================================================
  * Called from the debug interrupt routine.  Simply calls the CPU-specific
  * breakpoint handling routine.
@@ -193,7 +193,7 @@ void do_bedbug_breakpoint (struct pt_regs *regs)
 
 	return;
 }				/* do_bedbug_breakpoint */
-\f
+
 
 
 /* ======================================================================
@@ -226,7 +226,7 @@ void bedbug_main_loop (unsigned long addr, struct pt_regs *regs)
 
 	/* A miniature main loop */
 	while (bug_ctx.stopped) {
-		len = readline (prompt_str);
+		len = cli_readline(prompt_str);
 
 		flag = 0;	/* assume no special flags for now */
 
@@ -251,7 +251,7 @@ void bedbug_main_loop (unsigned long addr, struct pt_regs *regs)
 
 	return;
 }				/* bedbug_main_loop */
-\f
+
 
 
 /* ======================================================================
@@ -275,7 +275,7 @@ int do_bedbug_continue (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv
 U_BOOT_CMD (continue, 1, 0, do_bedbug_continue,
 	    "continue from a breakpoint",
 	    "");
-\f
+
 /* ======================================================================
  * Interpreter command to continue to the next instruction, stepping into
  * subroutines.  Works by calling the find_next_addr() routine to compute
@@ -306,7 +306,7 @@ int do_bedbug_step (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
 U_BOOT_CMD (step, 1, 1, do_bedbug_step,
 	    "single step execution.",
 	    "");
-\f
+
 /* ======================================================================
  * Interpreter command to continue to the next instruction, stepping over
  * subroutines.  Works by calling the find_next_addr() routine to compute
@@ -337,7 +337,7 @@ int do_bedbug_next (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
 U_BOOT_CMD (next, 1, 1, do_bedbug_next,
 	    "single step execution, stepping over subroutines.",
 	    "");
-\f
+
 /* ======================================================================
  * Interpreter command to print the current stack.  This assumes an EABI
  * architecture, so it starts with GPR R1 and works back up the stack.
@@ -382,7 +382,7 @@ int do_bedbug_stack (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
 U_BOOT_CMD (where, 1, 1, do_bedbug_stack,
 	    "Print the running stack.",
 	    "");
-\f
+
 /* ======================================================================
  * Interpreter command to dump the registers.  Calls the CPU-specific
  * show registers routine.
diff --git a/common/cmd_dcr.c b/common/cmd_dcr.c
index c5bcb8b..4fddd80 100644
--- a/common/cmd_dcr.c
+++ b/common/cmd_dcr.c
@@ -63,7 +63,7 @@ int do_setdcr (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
 	do {
 		value = get_dcr (dcrn);
 		printf ("%04x: %08lx", dcrn, value);
-		nbytes = readline (" ? ");
+		nbytes = cli_readline(" ? ");
 		if (nbytes == 0) {
 			/*
 			 * <CR> pressed as only input, don't modify current
diff --git a/common/cmd_i2c.c b/common/cmd_i2c.c
index 8ccde68..7158b5a 100644
--- a/common/cmd_i2c.c
+++ b/common/cmd_i2c.c
@@ -613,7 +613,7 @@ mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const arg
 				printf(" %08lx", data);
 		}
 
-		nbytes = readline (" ? ");
+		nbytes = cli_readline(" ? ");
 		if (nbytes == 0) {
 			/*
 			 * <CR> pressed as only input, don't modify current
diff --git a/common/cmd_mem.c b/common/cmd_mem.c
index 4b8738b..15a4b84 100644
--- a/common/cmd_mem.c
+++ b/common/cmd_mem.c
@@ -1150,7 +1150,7 @@ mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
 		else
 			printf(" %02x", *((u8 *)ptr));
 
-		nbytes = readline (" ? ");
+		nbytes = cli_readline(" ? ");
 		if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
 			/* <CR> pressed as only input, don't modify current
 			 * location and move to next. "-" pressed will go back.
diff --git a/common/cmd_nvedit.c b/common/cmd_nvedit.c
index 99a21b2..6cd538c 100644
--- a/common/cmd_nvedit.c
+++ b/common/cmd_nvedit.c
@@ -408,7 +408,7 @@ int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 		return 1;
 
 	/* prompt for input */
-	len = readline(message);
+	len = cli_readline(message);
 
 	if (size < len)
 		console_buffer[size] = '\0';
@@ -591,7 +591,7 @@ static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
 	else
 		buffer[0] = '\0';
 
-	if (readline_into_buffer("edit: ", buffer, 0) < 0)
+	if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
 		return 1;
 
 	return setenv(argv[1], buffer);
diff --git a/common/cmd_pci.c b/common/cmd_pci.c
index ddda207..1ed55ce 100644
--- a/common/cmd_pci.c
+++ b/common/cmd_pci.c
@@ -346,7 +346,7 @@ pci_cfg_modify (pci_dev_t bdf, ulong addr, ulong size, ulong value, int incrflag
 			printf(" %02x", val1);
 		}
 
-		nbytes = readline (" ? ");
+		nbytes = cli_readline(" ? ");
 		if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
 			/* <CR> pressed as only input, don't modify current
 			 * location and move to next. "-" pressed will go back.
diff --git a/common/menu.c b/common/menu.c
index 88d4697..94afeb2 100644
--- a/common/menu.c
+++ b/common/menu.c
@@ -197,8 +197,9 @@ static inline int menu_interactive_choice(struct menu *m, void **choice)
 		menu_display(m);
 
 		if (!m->item_choice) {
-			readret = readline_into_buffer("Enter choice: ", cbuf,
-					m->timeout / 10);
+			readret = cli_readline_into_buffer("Enter choice: ",
+							   cbuf,
+							   m->timeout / 10);
 
 			if (readret >= 0) {
 				choice_item = menu_item_by_key(m, cbuf);
diff --git a/drivers/ddr/fsl/interactive.c b/drivers/ddr/fsl/interactive.c
index 3b311b1..88b3b4c 100644
--- a/drivers/ddr/fsl/interactive.c
+++ b/drivers/ddr/fsl/interactive.c
@@ -1496,11 +1496,12 @@ unsigned long long fsl_ddr_interactive(fsl_ddr_info_t *pinfo, int var_is_set)
 		} else {
 			/*
 			 * No need to worry for buffer overflow here in
-			 * this function;  readline() maxes out at CFG_CBSIZE
+			 * this function;  cli_readline() maxes out at
+			 * CFG_CBSIZE
 			 */
-			readline_into_buffer(prompt, buffer, 0);
+			cli_readline_into_buffer(prompt, buffer, 0);
 		}
-		argc = parse_line(buffer, argv);
+		argc = cli_simple_parse_line(buffer, argv);
 		if (argc == 0)
 			continue;
 
diff --git a/include/cli.h b/include/cli.h
index b04539f..61f8aee 100644
--- a/include/cli.h
+++ b/include/cli.h
@@ -53,7 +53,7 @@ int cli_simple_run_command_list(char *cmd, int flag);
  * @prompt: Prompt to display
  * @return command line length excluding terminator, or -ve on error
  */
-int readline(const char *const prompt);
+int cli_readline(const char *const prompt);
 
 /**
  * readline_into_buffer() - read a line into a buffer
@@ -78,7 +78,8 @@ int readline(const char *const prompt);
  * parameter), then -2 is returned. If a break is detected (Ctrl-C) then
  * -1 is returned.
  */
-int readline_into_buffer(const char *const prompt, char *buffer, int timeout);
+int cli_readline_into_buffer(const char *const prompt, char *buffer,
+				int timeout);
 
 /**
  * parse_line() - split a command line down into separate arguments
@@ -97,7 +98,7 @@ int readline_into_buffer(const char *const prompt, char *buffer, int timeout);
  * @args:	Array to hold arguments
  * @return number of arguments
  */
-int parse_line(char *line, char *argv[]);
+int cli_simple_parse_line(char *line, char *argv[]);
 
 /** bootretry_dont_retry() - Indicate that we should not retry the boot */
 void bootretry_dont_retry(void);
-- 
1.9.1.423.g4596e3a

^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [U-Boot] [PATCH 06/14] Move autoboot code to autoboot.c
  2014-04-11  2:01 [U-Boot] [PATCH 0/14] Split out code from the enormous main.c Simon Glass
                   ` (4 preceding siblings ...)
  2014-04-11  2:01 ` [U-Boot] [PATCH 05/14] Add cli_ prefix to readline functions Simon Glass
@ 2014-04-11  2:01 ` Simon Glass
  2014-05-30 15:28   ` Tom Rini
  2014-04-11  2:01 ` [U-Boot] [PATCH 07/14] Move command line API into cli.c Simon Glass
                   ` (8 subsequent siblings)
  14 siblings, 1 reply; 30+ messages in thread
From: Simon Glass @ 2014-04-11  2:01 UTC (permalink / raw)
  To: u-boot

The autoboot code is complex and long. It deserves its own file with
a simple interface from main.c.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 common/Makefile    |   5 +
 common/autoboot.c  | 363 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 common/main.c      | 366 +----------------------------------------------------
 include/autoboot.h |  23 ++++
 4 files changed, 393 insertions(+), 364 deletions(-)
 create mode 100644 common/autoboot.c
 create mode 100644 include/autoboot.h

diff --git a/common/Makefile b/common/Makefile
index 44967ea..b707e39 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -23,6 +23,11 @@ obj-y += s_record.o
 obj-y += xyzModem.o
 obj-y += cmd_disk.o
 
+# This option is not just y/n - it can have a numeric value
+ifdef CONFIG_BOOTDELAY
+obj-y += autoboot.o
+endif
+
 # boards
 obj-$(CONFIG_SYS_GENERIC_BOARD) += board_f.o
 obj-$(CONFIG_SYS_GENERIC_BOARD) += board_r.o
diff --git a/common/autoboot.c b/common/autoboot.c
new file mode 100644
index 0000000..6933e3f
--- /dev/null
+++ b/common/autoboot.c
@@ -0,0 +1,363 @@
+/*
+ * (C) Copyright 2000
+ * Wolfgang Denk, DENX Software Engineering, wd at denx.de.
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <cli.h>
+#include <fdtdec.h>
+#include <menu.h>
+#include <post.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+#define MAX_DELAY_STOP_STR 32
+
+#ifndef DEBUG_BOOTKEYS
+#define DEBUG_BOOTKEYS 0
+#endif
+#define debug_bootkeys(fmt, args...)		\
+	debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
+
+/***************************************************************************
+ * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
+ * returns: 0 -  no key string, allow autoboot 1 - got key string, abort
+ */
+# if defined(CONFIG_AUTOBOOT_KEYED)
+static int abortboot_keyed(int bootdelay)
+{
+	int abort = 0;
+	uint64_t etime = endtick(bootdelay);
+	struct {
+		char *str;
+		u_int len;
+		int retry;
+	}
+	delaykey[] = {
+		{ str: getenv("bootdelaykey"),  retry: 1 },
+		{ str: getenv("bootdelaykey2"), retry: 1 },
+		{ str: getenv("bootstopkey"),   retry: 0 },
+		{ str: getenv("bootstopkey2"),  retry: 0 },
+	};
+
+	char presskey[MAX_DELAY_STOP_STR];
+	u_int presskey_len = 0;
+	u_int presskey_max = 0;
+	u_int i;
+
+#ifndef CONFIG_ZERO_BOOTDELAY_CHECK
+	if (bootdelay == 0)
+		return 0;
+#endif
+
+#  ifdef CONFIG_AUTOBOOT_PROMPT
+	printf(CONFIG_AUTOBOOT_PROMPT);
+#  endif
+
+#  ifdef CONFIG_AUTOBOOT_DELAY_STR
+	if (delaykey[0].str == NULL)
+		delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
+#  endif
+#  ifdef CONFIG_AUTOBOOT_DELAY_STR2
+	if (delaykey[1].str == NULL)
+		delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
+#  endif
+#  ifdef CONFIG_AUTOBOOT_STOP_STR
+	if (delaykey[2].str == NULL)
+		delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
+#  endif
+#  ifdef CONFIG_AUTOBOOT_STOP_STR2
+	if (delaykey[3].str == NULL)
+		delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
+#  endif
+
+	for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
+		delaykey[i].len = delaykey[i].str == NULL ?
+				    0 : strlen(delaykey[i].str);
+		delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
+				    MAX_DELAY_STOP_STR : delaykey[i].len;
+
+		presskey_max = presskey_max > delaykey[i].len ?
+				    presskey_max : delaykey[i].len;
+
+		debug_bootkeys("%s key:<%s>\n",
+			       delaykey[i].retry ? "delay" : "stop",
+			       delaykey[i].str ? delaykey[i].str : "NULL");
+	}
+
+	/* In order to keep up with incoming data, check timeout only
+	 * when catch up.
+	 */
+	do {
+		if (tstc()) {
+			if (presskey_len < presskey_max) {
+				presskey[presskey_len++] = getc();
+			} else {
+				for (i = 0; i < presskey_max - 1; i++)
+					presskey[i] = presskey[i + 1];
+
+				presskey[i] = getc();
+			}
+		}
+
+		for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i++) {
+			if (delaykey[i].len > 0 &&
+			    presskey_len >= delaykey[i].len &&
+				memcmp(presskey + presskey_len -
+					delaykey[i].len, delaykey[i].str,
+					delaykey[i].len) == 0) {
+					debug_bootkeys("got %skey\n",
+						delaykey[i].retry ? "delay" :
+						"stop");
+
+#  ifdef CONFIG_BOOT_RETRY_TIME
+				/* don't retry auto boot */
+				if (!delaykey[i].retry)
+					bootretry_dont_retry();
+#  endif
+				abort = 1;
+			}
+		}
+	} while (!abort && get_ticks() <= etime);
+
+	if (!abort)
+		debug_bootkeys("key timeout\n");
+
+#ifdef CONFIG_SILENT_CONSOLE
+	if (abort)
+		gd->flags &= ~GD_FLG_SILENT;
+#endif
+
+	return abort;
+}
+
+# else	/* !defined(CONFIG_AUTOBOOT_KEYED) */
+
+#ifdef CONFIG_MENUKEY
+static int menukey;
+#endif
+
+static int abortboot_normal(int bootdelay)
+{
+	int abort = 0;
+	unsigned long ts;
+
+#ifdef CONFIG_MENUPROMPT
+	printf(CONFIG_MENUPROMPT);
+#else
+	if (bootdelay >= 0)
+		printf("Hit any key to stop autoboot: %2d ", bootdelay);
+#endif
+
+#if defined CONFIG_ZERO_BOOTDELAY_CHECK
+	/*
+	 * Check if key already pressed
+	 * Don't check if bootdelay < 0
+	 */
+	if (bootdelay >= 0) {
+		if (tstc()) {	/* we got a key press	*/
+			(void) getc();  /* consume input	*/
+			puts("\b\b\b 0");
+			abort = 1;	/* don't auto boot	*/
+		}
+	}
+#endif
+
+	while ((bootdelay > 0) && (!abort)) {
+		--bootdelay;
+		/* delay 1000 ms */
+		ts = get_timer(0);
+		do {
+			if (tstc()) {	/* we got a key press	*/
+				abort  = 1;	/* don't auto boot	*/
+				bootdelay = 0;	/* no more delay	*/
+# ifdef CONFIG_MENUKEY
+				menukey = getc();
+# else
+				(void) getc();  /* consume input	*/
+# endif
+				break;
+			}
+			udelay(10000);
+		} while (!abort && get_timer(ts) < 1000);
+
+		printf("\b\b\b%2d ", bootdelay);
+	}
+
+	putc('\n');
+
+#ifdef CONFIG_SILENT_CONSOLE
+	if (abort)
+		gd->flags &= ~GD_FLG_SILENT;
+#endif
+
+	return abort;
+}
+# endif	/* CONFIG_AUTOBOOT_KEYED */
+
+static int abortboot(int bootdelay)
+{
+#ifdef CONFIG_AUTOBOOT_KEYED
+	return abortboot_keyed(bootdelay);
+#else
+	return abortboot_normal(bootdelay);
+#endif
+}
+
+/*
+ * Runs the given boot command securely.  Specifically:
+ * - Doesn't run the command with the shell (run_command or parse_string_outer),
+ *   since that's a lot of code surface that an attacker might exploit.
+ *   Because of this, we don't do any argument parsing--the secure boot command
+ *   has to be a full-fledged u-boot command.
+ * - Doesn't check for keypresses before booting, since that could be a
+ *   security hole; also disables Ctrl-C.
+ * - Doesn't allow the command to return.
+ *
+ * Upon any failures, this function will drop into an infinite loop after
+ * printing the error message to console.
+ */
+
+#if defined(CONFIG_OF_CONTROL)
+static void secure_boot_cmd(char *cmd)
+{
+	cmd_tbl_t *cmdtp;
+	int rc;
+
+	if (!cmd) {
+		printf("## Error: Secure boot command not specified\n");
+		goto err;
+	}
+
+	/* Disable Ctrl-C just in case some command is used that checks it. */
+	disable_ctrlc(1);
+
+	/* Find the command directly. */
+	cmdtp = find_cmd(cmd);
+	if (!cmdtp) {
+		printf("## Error: \"%s\" not defined\n", cmd);
+		goto err;
+	}
+
+	/* Run the command, forcing no flags and faking argc and argv. */
+	rc = (cmdtp->cmd)(cmdtp, 0, 1, &cmd);
+
+	/* Shouldn't ever return from boot command. */
+	printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
+
+err:
+	/*
+	 * Not a whole lot to do here.  Rebooting won't help much, since we'll
+	 * just end up right back here.  Just loop.
+	 */
+	hang();
+}
+
+static void process_fdt_options(const void *blob)
+{
+	ulong addr;
+
+	/* Add an env variable to point to a kernel payload, if available */
+	addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
+	if (addr)
+		setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
+
+	/* Add an env variable to point to a root disk, if available */
+	addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
+	if (addr)
+		setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
+}
+#endif /* CONFIG_OF_CONTROL */
+
+void bootdelay_process(void)
+{
+#ifdef CONFIG_OF_CONTROL
+	char *env;
+#endif
+	char *s;
+	int bootdelay;
+#ifdef CONFIG_BOOTCOUNT_LIMIT
+	unsigned long bootcount = 0;
+	unsigned long bootlimit = 0;
+#endif /* CONFIG_BOOTCOUNT_LIMIT */
+
+#ifdef CONFIG_BOOTCOUNT_LIMIT
+	bootcount = bootcount_load();
+	bootcount++;
+	bootcount_store(bootcount);
+	setenv_ulong("bootcount", bootcount);
+	bootlimit = getenv_ulong("bootlimit", 10, 0);
+#endif /* CONFIG_BOOTCOUNT_LIMIT */
+
+	s = getenv("bootdelay");
+	bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
+
+#ifdef CONFIG_OF_CONTROL
+	bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
+			bootdelay);
+#endif
+
+	debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
+
+#if defined(CONFIG_MENU_SHOW)
+	bootdelay = menu_show(bootdelay);
+#endif
+# ifdef CONFIG_BOOT_RETRY_TIME
+	init_cmd_timeout();
+# endif	/* CONFIG_BOOT_RETRY_TIME */
+
+#ifdef CONFIG_POST
+	if (gd->flags & GD_FLG_POSTFAIL) {
+		s = getenv("failbootcmd");
+	} else
+#endif /* CONFIG_POST */
+#ifdef CONFIG_BOOTCOUNT_LIMIT
+	if (bootlimit && (bootcount > bootlimit)) {
+		printf("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
+		       (unsigned)bootlimit);
+		s = getenv("altbootcmd");
+	} else
+#endif /* CONFIG_BOOTCOUNT_LIMIT */
+		s = getenv("bootcmd");
+#ifdef CONFIG_OF_CONTROL
+	/* Allow the fdt to override the boot command */
+	env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
+	if (env)
+		s = env;
+
+	process_fdt_options(gd->fdt_blob);
+
+	/*
+	 * If the bootsecure option was chosen, use secure_boot_cmd().
+	 * Always use 'env' in this case, since bootsecure requres that the
+	 * bootcmd was specified in the FDT too.
+	 */
+	if (fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0))
+		secure_boot_cmd(env);
+
+#endif /* CONFIG_OF_CONTROL */
+
+	debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
+
+	if (bootdelay != -1 && s && !abortboot(bootdelay)) {
+#if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
+		int prev = disable_ctrlc(1);	/* disable Control C checking */
+#endif
+
+		run_command_list(s, -1, 0);
+
+#if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
+		disable_ctrlc(prev);	/* restore Control C checking */
+#endif
+	}
+
+#ifdef CONFIG_MENUKEY
+	if (menukey == CONFIG_MENUKEY) {
+		s = getenv("menucmd");
+		if (s)
+			run_command_list(s, -1, 0);
+	}
+#endif /* CONFIG_MENUKEY */
+}
diff --git a/common/main.c b/common/main.c
index 9470e47..45f1b5c 100644
--- a/common/main.c
+++ b/common/main.c
@@ -8,384 +8,24 @@
 /* #define	DEBUG	*/
 
 #include <common.h>
+#include <autoboot.h>
 #include <cli.h>
 #include <command.h>
-#include <fdtdec.h>
 #include <cli_hush.h>
 #include <malloc.h>
-#include <menu.h>
-#include <post.h>
 #include <version.h>
 
-DECLARE_GLOBAL_DATA_PTR;
-
 /*
  * Board-specific Platform code can reimplement show_boot_progress () if needed
  */
 void inline __show_boot_progress (int val) {}
 void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
 
-#define MAX_DELAY_STOP_STR 32
-
-#ifndef DEBUG_BOOTKEYS
-#define DEBUG_BOOTKEYS 0
-#endif
-#define debug_bootkeys(fmt, args...)		\
-	debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
-
 #ifdef CONFIG_MODEM_SUPPORT
 int do_mdm_init = 0;
 extern void mdm_init(void); /* defined in board.c */
 #endif
 
-/***************************************************************************
- * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
- * returns: 0 -  no key string, allow autoboot 1 - got key string, abort
- */
-#if defined(CONFIG_BOOTDELAY)
-# if defined(CONFIG_AUTOBOOT_KEYED)
-static int abortboot_keyed(int bootdelay)
-{
-	int abort = 0;
-	uint64_t etime = endtick(bootdelay);
-	struct {
-		char* str;
-		u_int len;
-		int retry;
-	}
-	delaykey [] = {
-		{ str: getenv ("bootdelaykey"),  retry: 1 },
-		{ str: getenv ("bootdelaykey2"), retry: 1 },
-		{ str: getenv ("bootstopkey"),   retry: 0 },
-		{ str: getenv ("bootstopkey2"),  retry: 0 },
-	};
-
-	char presskey [MAX_DELAY_STOP_STR];
-	u_int presskey_len = 0;
-	u_int presskey_max = 0;
-	u_int i;
-
-#ifndef CONFIG_ZERO_BOOTDELAY_CHECK
-	if (bootdelay == 0)
-		return 0;
-#endif
-
-#  ifdef CONFIG_AUTOBOOT_PROMPT
-	printf(CONFIG_AUTOBOOT_PROMPT);
-#  endif
-
-#  ifdef CONFIG_AUTOBOOT_DELAY_STR
-	if (delaykey[0].str == NULL)
-		delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
-#  endif
-#  ifdef CONFIG_AUTOBOOT_DELAY_STR2
-	if (delaykey[1].str == NULL)
-		delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
-#  endif
-#  ifdef CONFIG_AUTOBOOT_STOP_STR
-	if (delaykey[2].str == NULL)
-		delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
-#  endif
-#  ifdef CONFIG_AUTOBOOT_STOP_STR2
-	if (delaykey[3].str == NULL)
-		delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
-#  endif
-
-	for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
-		delaykey[i].len = delaykey[i].str == NULL ?
-				    0 : strlen (delaykey[i].str);
-		delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
-				    MAX_DELAY_STOP_STR : delaykey[i].len;
-
-		presskey_max = presskey_max > delaykey[i].len ?
-				    presskey_max : delaykey[i].len;
-
-		debug_bootkeys("%s key:<%s>\n",
-			       delaykey[i].retry ? "delay" : "stop",
-			       delaykey[i].str ? delaykey[i].str : "NULL");
-	}
-
-	/* In order to keep up with incoming data, check timeout only
-	 * when catch up.
-	 */
-	do {
-		if (tstc()) {
-			if (presskey_len < presskey_max) {
-				presskey [presskey_len ++] = getc();
-			}
-			else {
-				for (i = 0; i < presskey_max - 1; i ++)
-					presskey [i] = presskey [i + 1];
-
-				presskey [i] = getc();
-			}
-		}
-
-		for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
-			if (delaykey[i].len > 0 &&
-			    presskey_len >= delaykey[i].len &&
-			    memcmp (presskey + presskey_len - delaykey[i].len,
-				    delaykey[i].str,
-				    delaykey[i].len) == 0) {
-				debug_bootkeys("got %skey\n",
-					       delaykey[i].retry ? "delay" :
-					       "stop");
-
-#  ifdef CONFIG_BOOT_RETRY_TIME
-				/* don't retry auto boot */
-				if (! delaykey[i].retry)
-					bootretry_dont_retry();
-#  endif
-				abort = 1;
-			}
-		}
-	} while (!abort && get_ticks() <= etime);
-
-	if (!abort)
-		debug_bootkeys("key timeout\n");
-
-#ifdef CONFIG_SILENT_CONSOLE
-	if (abort)
-		gd->flags &= ~GD_FLG_SILENT;
-#endif
-
-	return abort;
-}
-
-# else	/* !defined(CONFIG_AUTOBOOT_KEYED) */
-
-#ifdef CONFIG_MENUKEY
-static int menukey = 0;
-#endif
-
-static int abortboot_normal(int bootdelay)
-{
-	int abort = 0;
-	unsigned long ts;
-
-#ifdef CONFIG_MENUPROMPT
-	printf(CONFIG_MENUPROMPT);
-#else
-	if (bootdelay >= 0)
-		printf("Hit any key to stop autoboot: %2d ", bootdelay);
-#endif
-
-#if defined CONFIG_ZERO_BOOTDELAY_CHECK
-	/*
-	 * Check if key already pressed
-	 * Don't check if bootdelay < 0
-	 */
-	if (bootdelay >= 0) {
-		if (tstc()) {	/* we got a key press	*/
-			(void) getc();  /* consume input	*/
-			puts ("\b\b\b 0");
-			abort = 1;	/* don't auto boot	*/
-		}
-	}
-#endif
-
-	while ((bootdelay > 0) && (!abort)) {
-		--bootdelay;
-		/* delay 1000 ms */
-		ts = get_timer(0);
-		do {
-			if (tstc()) {	/* we got a key press	*/
-				abort  = 1;	/* don't auto boot	*/
-				bootdelay = 0;	/* no more delay	*/
-# ifdef CONFIG_MENUKEY
-				menukey = getc();
-# else
-				(void) getc();  /* consume input	*/
-# endif
-				break;
-			}
-			udelay(10000);
-		} while (!abort && get_timer(ts) < 1000);
-
-		printf("\b\b\b%2d ", bootdelay);
-	}
-
-	putc('\n');
-
-#ifdef CONFIG_SILENT_CONSOLE
-	if (abort)
-		gd->flags &= ~GD_FLG_SILENT;
-#endif
-
-	return abort;
-}
-# endif	/* CONFIG_AUTOBOOT_KEYED */
-
-static int abortboot(int bootdelay)
-{
-#ifdef CONFIG_AUTOBOOT_KEYED
-	return abortboot_keyed(bootdelay);
-#else
-	return abortboot_normal(bootdelay);
-#endif
-}
-#endif	/* CONFIG_BOOTDELAY */
-
-/*
- * Runs the given boot command securely.  Specifically:
- * - Doesn't run the command with the shell (run_command or parse_string_outer),
- *   since that's a lot of code surface that an attacker might exploit.
- *   Because of this, we don't do any argument parsing--the secure boot command
- *   has to be a full-fledged u-boot command.
- * - Doesn't check for keypresses before booting, since that could be a
- *   security hole; also disables Ctrl-C.
- * - Doesn't allow the command to return.
- *
- * Upon any failures, this function will drop into an infinite loop after
- * printing the error message to console.
- */
-
-#if defined(CONFIG_BOOTDELAY) && defined(CONFIG_OF_CONTROL)
-static void secure_boot_cmd(char *cmd)
-{
-	cmd_tbl_t *cmdtp;
-	int rc;
-
-	if (!cmd) {
-		printf("## Error: Secure boot command not specified\n");
-		goto err;
-	}
-
-	/* Disable Ctrl-C just in case some command is used that checks it. */
-	disable_ctrlc(1);
-
-	/* Find the command directly. */
-	cmdtp = find_cmd(cmd);
-	if (!cmdtp) {
-		printf("## Error: \"%s\" not defined\n", cmd);
-		goto err;
-	}
-
-	/* Run the command, forcing no flags and faking argc and argv. */
-	rc = (cmdtp->cmd)(cmdtp, 0, 1, &cmd);
-
-	/* Shouldn't ever return from boot command. */
-	printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
-
-err:
-	/*
-	 * Not a whole lot to do here.  Rebooting won't help much, since we'll
-	 * just end up right back here.  Just loop.
-	 */
-	hang();
-}
-
-static void process_fdt_options(const void *blob)
-{
-	ulong addr;
-
-	/* Add an env variable to point to a kernel payload, if available */
-	addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
-	if (addr)
-		setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
-
-	/* Add an env variable to point to a root disk, if available */
-	addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
-	if (addr)
-		setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
-}
-#endif /* CONFIG_OF_CONTROL */
-
-#ifdef CONFIG_BOOTDELAY
-static void process_boot_delay(void)
-{
-#ifdef CONFIG_OF_CONTROL
-	char *env;
-#endif
-	char *s;
-	int bootdelay;
-#ifdef CONFIG_BOOTCOUNT_LIMIT
-	unsigned long bootcount = 0;
-	unsigned long bootlimit = 0;
-#endif /* CONFIG_BOOTCOUNT_LIMIT */
-
-#ifdef CONFIG_BOOTCOUNT_LIMIT
-	bootcount = bootcount_load();
-	bootcount++;
-	bootcount_store (bootcount);
-	setenv_ulong("bootcount", bootcount);
-	bootlimit = getenv_ulong("bootlimit", 10, 0);
-#endif /* CONFIG_BOOTCOUNT_LIMIT */
-
-	s = getenv ("bootdelay");
-	bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
-
-#ifdef CONFIG_OF_CONTROL
-	bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay",
-			bootdelay);
-#endif
-
-	debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
-
-#if defined(CONFIG_MENU_SHOW)
-	bootdelay = menu_show(bootdelay);
-#endif
-# ifdef CONFIG_BOOT_RETRY_TIME
-	init_cmd_timeout ();
-# endif	/* CONFIG_BOOT_RETRY_TIME */
-
-#ifdef CONFIG_POST
-	if (gd->flags & GD_FLG_POSTFAIL) {
-		s = getenv("failbootcmd");
-	}
-	else
-#endif /* CONFIG_POST */
-#ifdef CONFIG_BOOTCOUNT_LIMIT
-	if (bootlimit && (bootcount > bootlimit)) {
-		printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
-			(unsigned)bootlimit);
-		s = getenv ("altbootcmd");
-	}
-	else
-#endif /* CONFIG_BOOTCOUNT_LIMIT */
-		s = getenv ("bootcmd");
-#ifdef CONFIG_OF_CONTROL
-	/* Allow the fdt to override the boot command */
-	env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
-	if (env)
-		s = env;
-
-	process_fdt_options(gd->fdt_blob);
-
-	/*
-	 * If the bootsecure option was chosen, use secure_boot_cmd().
-	 * Always use 'env' in this case, since bootsecure requres that the
-	 * bootcmd was specified in the FDT too.
-	 */
-	if (fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0))
-		secure_boot_cmd(env);
-
-#endif /* CONFIG_OF_CONTROL */
-
-	debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
-
-	if (bootdelay != -1 && s && !abortboot(bootdelay)) {
-#if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
-		int prev = disable_ctrlc(1);	/* disable Control C checking */
-#endif
-
-		run_command_list(s, -1, 0);
-
-#if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
-		disable_ctrlc(prev);	/* restore Control C checking */
-#endif
-	}
-
-#ifdef CONFIG_MENUKEY
-	if (menukey == CONFIG_MENUKEY) {
-		s = getenv("menucmd");
-		if (s)
-			run_command_list(s, -1, 0);
-	}
-#endif /* CONFIG_MENUKEY */
-}
-#endif /* CONFIG_BOOTDELAY */
-
 void main_loop(void)
 {
 #ifdef CONFIG_PREBOOT
@@ -438,9 +78,7 @@ void main_loop(void)
 	update_tftp(0UL);
 #endif /* CONFIG_UPDATE_TFTP */
 
-#ifdef CONFIG_BOOTDELAY
-	process_boot_delay();
-#endif
+	bootdelay_process();
 	/*
 	 * Main Loop for Monitor Command Processing
 	 */
diff --git a/include/autoboot.h b/include/autoboot.h
new file mode 100644
index 0000000..aaae4af
--- /dev/null
+++ b/include/autoboot.h
@@ -0,0 +1,23 @@
+/*
+ * (C) Copyright 2000
+ * Wolfgang Denk, DENX Software Engineering, wd at denx.de.
+ *
+ * Add to readline cmdline-editing by
+ * (C) Copyright 2005
+ * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#ifndef __AUTOBOOT_H
+#define __AUTOBOOT_H
+
+#ifdef CONFIG_BOOTDELAY
+void bootdelay_process(void);
+#else
+static inline void bootdelay_process(void)
+{
+}
+#endif
+
+#endif
-- 
1.9.1.423.g4596e3a

^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [U-Boot] [PATCH 07/14] Move command line API into cli.c
  2014-04-11  2:01 [U-Boot] [PATCH 0/14] Split out code from the enormous main.c Simon Glass
                   ` (5 preceding siblings ...)
  2014-04-11  2:01 ` [U-Boot] [PATCH 06/14] Move autoboot code to autoboot.c Simon Glass
@ 2014-04-11  2:01 ` Simon Glass
  2014-05-30 15:28   ` Tom Rini
  2014-04-11  2:01 ` [U-Boot] [PATCH 08/14] Move bootretry code into bootretry.c and clean up Simon Glass
                   ` (7 subsequent siblings)
  14 siblings, 1 reply; 30+ messages in thread
From: Simon Glass @ 2014-04-11  2:01 UTC (permalink / raw)
  To: u-boot

We now have a single entry point to the CLI, whether simple or hush. Put
this in its own file.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 common/Makefile |   1 +
 common/cli.c    | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 common/main.c   |  93 -------------------------------------------------
 3 files changed, 107 insertions(+), 93 deletions(-)
 create mode 100644 common/cli.c

diff --git a/common/Makefile b/common/Makefile
index b707e39..41b6ef5 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -18,6 +18,7 @@ endif
 # We always have this since drivers/ddr/fs/interactive.c needs it
 obj-y += cli_simple.o
 
+obj-y += cli.o
 obj-y += cli_readline.o
 obj-y += s_record.o
 obj-y += xyzModem.o
diff --git a/common/cli.c b/common/cli.c
new file mode 100644
index 0000000..9cf7ba1
--- /dev/null
+++ b/common/cli.c
@@ -0,0 +1,106 @@
+/*
+ * (C) Copyright 2000
+ * Wolfgang Denk, DENX Software Engineering, wd at denx.de.
+ *
+ * Add to readline cmdline-editing by
+ * (C) Copyright 2005
+ * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <cli.h>
+#include <cli_hush.h>
+#include <malloc.h>
+
+/*
+ * Run a command using the selected parser.
+ *
+ * @param cmd	Command to run
+ * @param flag	Execution flags (CMD_FLAG_...)
+ * @return 0 on success, or != 0 on error.
+ */
+int run_command(const char *cmd, int flag)
+{
+#ifndef CONFIG_SYS_HUSH_PARSER
+	/*
+	 * cli_run_command can return 0 or 1 for success, so clean up
+	 * its result.
+	 */
+	if (cli_simple_run_command(cmd, flag) == -1)
+		return 1;
+
+	return 0;
+#else
+	return parse_string_outer(cmd,
+			FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
+#endif
+}
+
+int run_command_list(const char *cmd, int len, int flag)
+{
+	int need_buff = 1;
+	char *buff = (char *)cmd;	/* cast away const */
+	int rcode = 0;
+
+	if (len == -1) {
+		len = strlen(cmd);
+#ifdef CONFIG_SYS_HUSH_PARSER
+		/* hush will never change our string */
+		need_buff = 0;
+#else
+		/* the built-in parser will change our string if it sees \n */
+		need_buff = strchr(cmd, '\n') != NULL;
+#endif
+	}
+	if (need_buff) {
+		buff = malloc(len + 1);
+		if (!buff)
+			return 1;
+		memcpy(buff, cmd, len);
+		buff[len] = '\0';
+	}
+#ifdef CONFIG_SYS_HUSH_PARSER
+	rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
+#else
+	/*
+	 * This function will overwrite any \n it sees with a \0, which
+	 * is why it can't work with a const char *. Here we are making
+	 * using of internal knowledge of this function, to avoid always
+	 * doing a malloc() which is actually required only in a case that
+	 * is pretty rare.
+	 */
+	rcode = cli_simple_run_command_list(buff, flag);
+	if (need_buff)
+		free(buff);
+#endif
+
+	return rcode;
+}
+
+/****************************************************************************/
+
+#if defined(CONFIG_CMD_RUN)
+int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+	int i;
+
+	if (argc < 2)
+		return CMD_RET_USAGE;
+
+	for (i = 1; i < argc; ++i) {
+		char *arg;
+
+		arg = getenv(argv[i]);
+		if (arg == NULL) {
+			printf("## Error: \"%s\" not defined\n", argv[i]);
+			return 1;
+		}
+
+		if (run_command(arg, flag) != 0)
+			return 1;
+	}
+	return 0;
+}
+#endif
diff --git a/common/main.c b/common/main.c
index 45f1b5c..91a5b18 100644
--- a/common/main.c
+++ b/common/main.c
@@ -10,7 +10,6 @@
 #include <common.h>
 #include <autoboot.h>
 #include <cli.h>
-#include <command.h>
 #include <cli_hush.h>
 #include <malloc.h>
 #include <version.h>
@@ -90,95 +89,3 @@ void main_loop(void)
 	cli_loop();
 #endif /*CONFIG_SYS_HUSH_PARSER*/
 }
-
-/****************************************************************************/
-
-/*
- * Run a command using the selected parser.
- *
- * @param cmd	Command to run
- * @param flag	Execution flags (CMD_FLAG_...)
- * @return 0 on success, or != 0 on error.
- */
-int run_command(const char *cmd, int flag)
-{
-#ifndef CONFIG_SYS_HUSH_PARSER
-	/*
-	 * cli_run_command can return 0 or 1 for success, so clean up
-	 * its result.
-	 */
-	if (cli_simple_run_command(cmd, flag) == -1)
-		return 1;
-
-	return 0;
-#else
-	return parse_string_outer(cmd,
-			FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
-#endif
-}
-
-int run_command_list(const char *cmd, int len, int flag)
-{
-	int need_buff = 1;
-	char *buff = (char *)cmd;	/* cast away const */
-	int rcode = 0;
-
-	if (len == -1) {
-		len = strlen(cmd);
-#ifdef CONFIG_SYS_HUSH_PARSER
-		/* hush will never change our string */
-		need_buff = 0;
-#else
-		/* the built-in parser will change our string if it sees \n */
-		need_buff = strchr(cmd, '\n') != NULL;
-#endif
-	}
-	if (need_buff) {
-		buff = malloc(len + 1);
-		if (!buff)
-			return 1;
-		memcpy(buff, cmd, len);
-		buff[len] = '\0';
-	}
-#ifdef CONFIG_SYS_HUSH_PARSER
-	rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
-#else
-	/*
-	 * This function will overwrite any \n it sees with a \0, which
-	 * is why it can't work with a const char *. Here we are making
-	 * using of internal knowledge of this function, to avoid always
-	 * doing a malloc() which is actually required only in a case that
-	 * is pretty rare.
-	 */
-	rcode = cli_simple_run_command_list(buff, flag);
-	if (need_buff)
-		free(buff);
-#endif
-
-	return rcode;
-}
-
-/****************************************************************************/
-
-#if defined(CONFIG_CMD_RUN)
-int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
-{
-	int i;
-
-	if (argc < 2)
-		return CMD_RET_USAGE;
-
-	for (i=1; i<argc; ++i) {
-		char *arg;
-
-		if ((arg = getenv (argv[i])) == NULL) {
-			printf ("## Error: \"%s\" not defined\n", argv[i]);
-			return 1;
-		}
-
-		if (run_command(arg, flag) != 0)
-			return 1;
-	}
-	return 0;
-}
-#endif
-- 
1.9.1.423.g4596e3a

^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [U-Boot] [PATCH 08/14] Move bootretry code into bootretry.c and clean up
  2014-04-11  2:01 [U-Boot] [PATCH 0/14] Split out code from the enormous main.c Simon Glass
                   ` (6 preceding siblings ...)
  2014-04-11  2:01 ` [U-Boot] [PATCH 07/14] Move command line API into cli.c Simon Glass
@ 2014-04-11  2:01 ` Simon Glass
  2014-05-30 15:28   ` Tom Rini
  2014-04-11  2:01 ` [U-Boot] [PATCH 09/14] Rename bootretry functions and remove #ifdefs Simon Glass
                   ` (6 subsequent siblings)
  14 siblings, 1 reply; 30+ messages in thread
From: Simon Glass @ 2014-04-11  2:01 UTC (permalink / raw)
  To: u-boot

This code is only used by one board, so it seems a shame to clutter up
the readline code with it. Move it into its own file.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 board/hymod/hymod.c   |  1 +
 board/hymod/input.c   |  1 +
 common/Makefile       |  5 +++++
 common/autoboot.c     |  1 +
 common/bootretry.c    | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++
 common/cli_hush.c     |  1 +
 common/cli_readline.c | 60 +++++----------------------------------------------
 common/cli_simple.c   |  1 +
 common/cmd_i2c.c      |  1 +
 common/cmd_mem.c      |  1 +
 common/cmd_pci.c      |  1 +
 include/bootretry.h   | 31 ++++++++++++++++++++++++++
 include/common.h      |  2 --
 13 files changed, 108 insertions(+), 57 deletions(-)
 create mode 100644 common/bootretry.c
 create mode 100644 include/bootretry.h

diff --git a/board/hymod/hymod.c b/board/hymod/hymod.c
index 55ffd67..f6990e9 100644
--- a/board/hymod/hymod.c
+++ b/board/hymod/hymod.c
@@ -8,6 +8,7 @@
  */
 
 #include <common.h>
+#include <bootretry.h>
 #include <cli.h>
 #include <mpc8260.h>
 #include <mpc8260_irq.h>
diff --git a/board/hymod/input.c b/board/hymod/input.c
index 59ad6aa..2f857c0 100644
--- a/board/hymod/input.c
+++ b/board/hymod/input.c
@@ -6,6 +6,7 @@
  */
 
 #include <common.h>
+#include <bootretry.h>
 #include <cli.h>
 
 int
diff --git a/common/Makefile b/common/Makefile
index 41b6ef5..9fdb752 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -29,6 +29,11 @@ ifdef CONFIG_BOOTDELAY
 obj-y += autoboot.o
 endif
 
+# This option is not just y/n - it can have a numeric value
+ifdef CONFIG_BOOT_RETRY_TIME
+obj-y += bootretry.o
+endif
+
 # boards
 obj-$(CONFIG_SYS_GENERIC_BOARD) += board_f.o
 obj-$(CONFIG_SYS_GENERIC_BOARD) += board_r.o
diff --git a/common/autoboot.c b/common/autoboot.c
index 6933e3f..5f8d9c3 100644
--- a/common/autoboot.c
+++ b/common/autoboot.c
@@ -6,6 +6,7 @@
  */
 
 #include <common.h>
+#include <bootretry.h>
 #include <cli.h>
 #include <fdtdec.h>
 #include <menu.h>
diff --git a/common/bootretry.c b/common/bootretry.c
new file mode 100644
index 0000000..12653c0
--- /dev/null
+++ b/common/bootretry.c
@@ -0,0 +1,59 @@
+/*
+ * (C) Copyright 2000
+ * Wolfgang Denk, DENX Software Engineering, wd at denx.de.
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <bootretry.h>
+#include <cli.h>
+#include <errno.h>
+#include <watchdog.h>
+
+#ifndef CONFIG_BOOT_RETRY_MIN
+#define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
+#endif
+
+static uint64_t endtime;  /* must be set, default is instant timeout */
+static int      retry_time = -1; /* -1 so can call readline before main_loop */
+
+/***************************************************************************
+ * initialize command line timeout
+ */
+void init_cmd_timeout(void)
+{
+	char *s = getenv("bootretry");
+
+	if (s != NULL)
+		retry_time = (int)simple_strtol(s, NULL, 10);
+	else
+		retry_time = CONFIG_BOOT_RETRY_TIME;
+
+	if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
+		retry_time = CONFIG_BOOT_RETRY_MIN;
+}
+
+/***************************************************************************
+ * reset command line timeout to retry_time seconds
+ */
+void reset_cmd_timeout(void)
+{
+	endtime = endtick(retry_time);
+}
+
+int bootretry_tstc_timeout(void)
+{
+	while (!tstc()) {	/* while no incoming data */
+		if (retry_time >= 0 && get_ticks() > endtime)
+			return -ETIMEDOUT;
+		WATCHDOG_RESET();
+	}
+
+	return 0;
+}
+
+void bootretry_dont_retry(void)
+{
+	retry_time = -1;
+}
diff --git a/common/cli_hush.c b/common/cli_hush.c
index c6bb30b..2515b69 100644
--- a/common/cli_hush.c
+++ b/common/cli_hush.c
@@ -79,6 +79,7 @@
 #include <malloc.h>         /* malloc, free, realloc*/
 #include <linux/ctype.h>    /* isalpha, isdigit */
 #include <common.h>        /* readline */
+#include <bootretry.h>
 #include <cli.h>
 #include <cli_hush.h>
 #include <command.h>        /* find_cmd */
diff --git a/common/cli_readline.c b/common/cli_readline.c
index df446b8..9a9fb35 100644
--- a/common/cli_readline.c
+++ b/common/cli_readline.c
@@ -10,6 +10,7 @@
  */
 
 #include <common.h>
+#include <bootretry.h>
 #include <cli.h>
 #include <watchdog.h>
 
@@ -18,17 +19,8 @@ DECLARE_GLOBAL_DATA_PTR;
 static const char erase_seq[] = "\b \b";	/* erase sequence */
 static const char   tab_seq[] = "        ";	/* used to expand TABs */
 
-#ifdef CONFIG_BOOT_RETRY_TIME
-static uint64_t endtime;      /* must be set, default is instant timeout */
-static int      retry_time = -1; /* -1 so can call readline before main_loop */
-#endif
-
 char console_buffer[CONFIG_SYS_CBSIZE + 1];	/* console I/O buffer	*/
 
-#ifndef CONFIG_BOOT_RETRY_MIN
-#define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
-#endif
-
 static char *delete_char (char *buffer, char *p, int *colp, int *np, int plen)
 {
 	char *s;
@@ -267,13 +259,8 @@ static int cread_line(const char *const prompt, char *buf, unsigned int *len,
 		cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
 
 	while (1) {
-#ifdef CONFIG_BOOT_RETRY_TIME
-		while (!tstc()) {	/* while no incoming data */
-			if (retry_time >= 0 && get_ticks() > endtime)
-				return -2;	/* timed out */
-			WATCHDOG_RESET();
-		}
-#endif
+		if (bootretry_tstc_timeout())
+			return -2;	/* timed out */
 		if (first && timeout) {
 			uint64_t etime = endtick(timeout);
 
@@ -539,13 +526,8 @@ int cli_readline_into_buffer(const char *const prompt, char *buffer,
 	col = plen;
 
 	for (;;) {
-#ifdef CONFIG_BOOT_RETRY_TIME
-		while (!tstc()) {	/* while no incoming data */
-			if (retry_time >= 0 && get_ticks() > endtime)
-				return -2;	/* timed out */
-			WATCHDOG_RESET();
-		}
-#endif
+		if (bootretry_tstc_timeout())
+			return -2;	/* timed out */
 		WATCHDOG_RESET();	/* Trigger watchdog, if needed */
 
 #ifdef CONFIG_SHOW_ACTIVITY
@@ -637,35 +619,3 @@ int cli_readline_into_buffer(const char *const prompt, char *buffer,
 	}
 #endif
 }
-
-#ifdef CONFIG_BOOT_RETRY_TIME
-/***************************************************************************
- * initialize command line timeout
- */
-void init_cmd_timeout(void)
-{
-	char *s = getenv("bootretry");
-
-	if (s != NULL)
-		retry_time = (int)simple_strtol(s, NULL, 10);
-	else
-		retry_time =  CONFIG_BOOT_RETRY_TIME;
-
-	if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
-		retry_time = CONFIG_BOOT_RETRY_MIN;
-}
-
-/***************************************************************************
- * reset command line timeout to retry_time seconds
- */
-void reset_cmd_timeout(void)
-{
-	endtime = endtick(retry_time);
-}
-
-void bootretry_dont_retry(void)
-{
-	retry_time = -1;
-}
-
-#endif
diff --git a/common/cli_simple.c b/common/cli_simple.c
index 3039cfc..5b7e2ce 100644
--- a/common/cli_simple.c
+++ b/common/cli_simple.c
@@ -10,6 +10,7 @@
  */
 
 #include <common.h>
+#include <bootretry.h>
 #include <cli.h>
 #include <linux/ctype.h>
 
diff --git a/common/cmd_i2c.c b/common/cmd_i2c.c
index 7158b5a..4fa1213 100644
--- a/common/cmd_i2c.c
+++ b/common/cmd_i2c.c
@@ -66,6 +66,7 @@
  */
 
 #include <common.h>
+#include <bootretry.h>
 #include <cli.h>
 #include <command.h>
 #include <edid.h>
diff --git a/common/cmd_mem.c b/common/cmd_mem.c
index 15a4b84..9bd7c0e 100644
--- a/common/cmd_mem.c
+++ b/common/cmd_mem.c
@@ -12,6 +12,7 @@
  */
 
 #include <common.h>
+#include <bootretry.h>
 #include <cli.h>
 #include <command.h>
 #ifdef CONFIG_HAS_DATAFLASH
diff --git a/common/cmd_pci.c b/common/cmd_pci.c
index 1ed55ce..180b35a 100644
--- a/common/cmd_pci.c
+++ b/common/cmd_pci.c
@@ -14,6 +14,7 @@
  */
 
 #include <common.h>
+#include <bootretry.h>
 #include <cli.h>
 #include <command.h>
 #include <asm/processor.h>
diff --git a/include/bootretry.h b/include/bootretry.h
new file mode 100644
index 0000000..025c29d
--- /dev/null
+++ b/include/bootretry.h
@@ -0,0 +1,31 @@
+/*
+ * (C) Copyright 2000
+ * Wolfgang Denk, DENX Software Engineering, wd at denx.de.
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#ifndef __bootretry_h
+#define __bootretry_h
+
+#ifdef CONFIG_BOOT_RETRY_TIME
+/**
+ * bootretry_tstc_timeout() - ensure we get a keypress before timeout
+ *
+ * Check for a keypress repeatedly, resetting the watchdog each time. If a
+ * keypress is not received within the command timeout, return an error.
+ *
+ * @return 0 if a key is received in time, -ETIMEDOUT if not
+ */
+int bootretry_tstc_timeout(void);
+#else
+static inline int bootretry_tstc_timeout(void)
+{
+	return 0;
+}
+#endif
+
+void init_cmd_timeout(void);
+void reset_cmd_timeout(void);
+
+#endif
diff --git a/include/common.h b/include/common.h
index abe57c6..fbf3b14 100644
--- a/include/common.h
+++ b/include/common.h
@@ -286,8 +286,6 @@ int run_command(const char *cmd, int flag);
  * @return 0 on success, or != 0 on error.
  */
 int run_command_list(const char *cmd, int len, int flag);
-void	init_cmd_timeout(void);
-void	reset_cmd_timeout(void);
 extern char console_buffer[];
 
 /* arch/$(ARCH)/lib/board.c */
-- 
1.9.1.423.g4596e3a

^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [U-Boot] [PATCH 09/14] Rename bootretry functions and remove #ifdefs
  2014-04-11  2:01 [U-Boot] [PATCH 0/14] Split out code from the enormous main.c Simon Glass
                   ` (7 preceding siblings ...)
  2014-04-11  2:01 ` [U-Boot] [PATCH 08/14] Move bootretry code into bootretry.c and clean up Simon Glass
@ 2014-04-11  2:01 ` Simon Glass
  2014-05-30 15:28   ` Tom Rini
  2014-04-11  2:01 ` [U-Boot] [PATCH 10/14] m68k: powerpc: Clean up do_mdm_init Simon Glass
                   ` (5 subsequent siblings)
  14 siblings, 1 reply; 30+ messages in thread
From: Simon Glass @ 2014-04-11  2:01 UTC (permalink / raw)
  To: u-boot

Add a bootretry_ prefix to these two functions, and remove the need for
the #ifdef around everything (it moves to the Makefile).

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 board/hymod/hymod.c |  4 +---
 board/hymod/input.c |  8 ++------
 common/autoboot.c   |  6 +-----
 common/bootretry.c  |  4 ++--
 common/cli_hush.c   |  7 +------
 common/cli_simple.c |  4 +---
 common/cmd_i2c.c    | 13 ++++---------
 common/cmd_mem.c    | 13 ++++---------
 common/cmd_pci.c    |  9 +++------
 include/bootretry.h | 38 +++++++++++++++++++++++++++++++++-----
 include/cli.h       |  3 ---
 11 files changed, 52 insertions(+), 57 deletions(-)

diff --git a/board/hymod/hymod.c b/board/hymod/hymod.c
index f6990e9..0183f78 100644
--- a/board/hymod/hymod.c
+++ b/board/hymod/hymod.c
@@ -415,13 +415,11 @@ last_stage_init (void)
 	hymod_conf_t *cp = &gd->bd->bi_hymod_conf;
 	int rc;
 
-#ifdef CONFIG_BOOT_RETRY_TIME
 	/*
 	 * we use the cli_readline() function, but we also want
 	 * command timeout enabled
 	 */
-	init_cmd_timeout ();
-#endif
+	bootretry_init_cmd_timeout();
 
 	memset ((void *) cp, 0, sizeof (*cp));
 
diff --git a/board/hymod/input.c b/board/hymod/input.c
index 2f857c0..a9035d3 100644
--- a/board/hymod/input.c
+++ b/board/hymod/input.c
@@ -16,9 +16,7 @@ hymod_get_serno (const char *prompt)
 		int n, serno;
 		char *p;
 
-#ifdef CONFIG_BOOT_RETRY_TIME
-		reset_cmd_timeout ();
-#endif
+		bootretry_reset_cmd_timeout();
 
 		n = cli_readline(prompt);
 
@@ -44,9 +42,7 @@ hymod_get_ethaddr (void)
 	for (;;) {
 		int n;
 
-#ifdef CONFIG_BOOT_RETRY_TIME
-		reset_cmd_timeout ();
-#endif
+		bootretry_reset_cmd_timeout();
 
 		n = cli_readline("Enter board ethernet address: ");
 
diff --git a/common/autoboot.c b/common/autoboot.c
index 5f8d9c3..9843898 100644
--- a/common/autoboot.c
+++ b/common/autoboot.c
@@ -113,11 +113,9 @@ static int abortboot_keyed(int bootdelay)
 						delaykey[i].retry ? "delay" :
 						"stop");
 
-#  ifdef CONFIG_BOOT_RETRY_TIME
 				/* don't retry auto boot */
 				if (!delaykey[i].retry)
 					bootretry_dont_retry();
-#  endif
 				abort = 1;
 			}
 		}
@@ -305,9 +303,7 @@ void bootdelay_process(void)
 #if defined(CONFIG_MENU_SHOW)
 	bootdelay = menu_show(bootdelay);
 #endif
-# ifdef CONFIG_BOOT_RETRY_TIME
-	init_cmd_timeout();
-# endif	/* CONFIG_BOOT_RETRY_TIME */
+	bootretry_init_cmd_timeout();
 
 #ifdef CONFIG_POST
 	if (gd->flags & GD_FLG_POSTFAIL) {
diff --git a/common/bootretry.c b/common/bootretry.c
index 12653c0..2d82798 100644
--- a/common/bootretry.c
+++ b/common/bootretry.c
@@ -21,7 +21,7 @@ static int      retry_time = -1; /* -1 so can call readline before main_loop */
 /***************************************************************************
  * initialize command line timeout
  */
-void init_cmd_timeout(void)
+void bootretry_init_cmd_timeout(void)
 {
 	char *s = getenv("bootretry");
 
@@ -37,7 +37,7 @@ void init_cmd_timeout(void)
 /***************************************************************************
  * reset command line timeout to retry_time seconds
  */
-void reset_cmd_timeout(void)
+void bootretry_reset_cmd_timeout(void)
 {
 	endtime = endtick(retry_time);
 }
diff --git a/common/cli_hush.c b/common/cli_hush.c
index 2515b69..990c080 100644
--- a/common/cli_hush.c
+++ b/common/cli_hush.c
@@ -1000,12 +1000,7 @@ static void get_user_input(struct in_str *i)
 	int n;
 	static char the_command[CONFIG_SYS_CBSIZE];
 
-#ifdef CONFIG_BOOT_RETRY_TIME
-#  ifndef CONFIG_RESET_TO_RETRY
-#	error "This currently only works with CONFIG_RESET_TO_RETRY enabled"
-#  endif
-	reset_cmd_timeout();
-#endif
+	bootretry_reset_cmd_timeout();
 	i->__promptme = 1;
 	if (i->promptmode == 1) {
 		n = cli_readline(CONFIG_SYS_PROMPT);
diff --git a/common/cli_simple.c b/common/cli_simple.c
index 5b7e2ce..bba586e 100644
--- a/common/cli_simple.c
+++ b/common/cli_simple.c
@@ -265,14 +265,12 @@ void cli_loop(void)
 	int rc = 1;
 
 	for (;;) {
-#ifdef CONFIG_BOOT_RETRY_TIME
 		if (rc >= 0) {
 			/* Saw enough of a valid command to
 			 * restart the timeout.
 			 */
-			reset_cmd_timeout();
+			bootretry_reset_cmd_timeout();
 		}
-#endif
 		len = cli_readline(CONFIG_SYS_PROMPT);
 
 		flag = 0;	/* assume no special flags for now */
diff --git a/common/cmd_i2c.c b/common/cmd_i2c.c
index 4fa1213..d714658 100644
--- a/common/cmd_i2c.c
+++ b/common/cmd_i2c.c
@@ -564,9 +564,7 @@ mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const arg
 	if (argc != 3)
 		return CMD_RET_USAGE;
 
-#ifdef CONFIG_BOOT_RETRY_TIME
-	reset_cmd_timeout();	/* got a good command to get here */
-#endif
+	bootretry_reset_cmd_timeout();	/* got a good command to get here */
 	/*
 	 * We use the last specified parameters, unless new ones are
 	 * entered.
@@ -623,9 +621,8 @@ mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const arg
 			if (incrflag)
 				addr += size;
 			nbytes = size;
-#ifdef CONFIG_BOOT_RETRY_TIME
-			reset_cmd_timeout(); /* good enough to not time out */
-#endif
+			/* good enough to not time out */
+			bootretry_reset_cmd_timeout();
 		}
 #ifdef CONFIG_BOOT_RETRY_TIME
 		else if (nbytes == -2)
@@ -642,12 +639,10 @@ mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const arg
 			data = be32_to_cpu(data);
 			nbytes = endp - console_buffer;
 			if (nbytes) {
-#ifdef CONFIG_BOOT_RETRY_TIME
 				/*
 				 * good enough to not time out
 				 */
-				reset_cmd_timeout();
-#endif
+				bootretry_reset_cmd_timeout();
 				if (i2c_write(chip, addr, alen, (uchar *)&data, size) != 0)
 					puts ("Error writing the chip.\n");
 #ifdef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
diff --git a/common/cmd_mem.c b/common/cmd_mem.c
index 9bd7c0e..1febddb 100644
--- a/common/cmd_mem.c
+++ b/common/cmd_mem.c
@@ -1098,9 +1098,7 @@ mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
 	if (argc != 2)
 		return CMD_RET_USAGE;
 
-#ifdef CONFIG_BOOT_RETRY_TIME
-	reset_cmd_timeout();	/* got a good command to get here */
-#endif
+	bootretry_reset_cmd_timeout();	/* got a good command to get here */
 	/* We use the last specified parameters, unless new ones are
 	 * entered.
 	 */
@@ -1159,9 +1157,8 @@ mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
 			if (incrflag)
 				addr += nbytes ? -size : size;
 			nbytes = 1;
-#ifdef CONFIG_BOOT_RETRY_TIME
-			reset_cmd_timeout(); /* good enough to not time out */
-#endif
+			/* good enough to not time out */
+			bootretry_reset_cmd_timeout();
 		}
 #ifdef CONFIG_BOOT_RETRY_TIME
 		else if (nbytes == -2) {
@@ -1177,11 +1174,9 @@ mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
 #endif
 			nbytes = endp - console_buffer;
 			if (nbytes) {
-#ifdef CONFIG_BOOT_RETRY_TIME
 				/* good enough to not time out
 				 */
-				reset_cmd_timeout();
-#endif
+				bootretry_reset_cmd_timeout();
 				if (size == 4)
 					*((u32 *)ptr) = i;
 #ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
diff --git a/common/cmd_pci.c b/common/cmd_pci.c
index 180b35a..a1ba42e 100644
--- a/common/cmd_pci.c
+++ b/common/cmd_pci.c
@@ -355,9 +355,8 @@ pci_cfg_modify (pci_dev_t bdf, ulong addr, ulong size, ulong value, int incrflag
 			if (incrflag)
 				addr += nbytes ? -size : size;
 			nbytes = 1;
-#ifdef CONFIG_BOOT_RETRY_TIME
-			reset_cmd_timeout(); /* good enough to not time out */
-#endif
+			/* good enough to not time out */
+			bootretry_reset_cmd_timeout();
 		}
 #ifdef CONFIG_BOOT_RETRY_TIME
 		else if (nbytes == -2) {
@@ -369,11 +368,9 @@ pci_cfg_modify (pci_dev_t bdf, ulong addr, ulong size, ulong value, int incrflag
 			i = simple_strtoul(console_buffer, &endp, 16);
 			nbytes = endp - console_buffer;
 			if (nbytes) {
-#ifdef CONFIG_BOOT_RETRY_TIME
 				/* good enough to not time out
 				 */
-				reset_cmd_timeout();
-#endif
+				bootretry_reset_cmd_timeout();
 				pci_cfg_write (bdf, addr, size, i);
 				if (incrflag)
 					addr += size;
diff --git a/include/bootretry.h b/include/bootretry.h
index 025c29d..2ecd7a4 100644
--- a/include/bootretry.h
+++ b/include/bootretry.h
@@ -5,8 +5,8 @@
  * SPDX-License-Identifier:	GPL-2.0+
  */
 
-#ifndef __bootretry_h
-#define __bootretry_h
+#ifndef __BOOTRETRY_H
+#define __BOOTRETRY_H
 
 #ifdef CONFIG_BOOT_RETRY_TIME
 /**
@@ -18,14 +18,42 @@
  * @return 0 if a key is received in time, -ETIMEDOUT if not
  */
 int bootretry_tstc_timeout(void);
+
+/**
+ * bootretry_init_cmd_timeout() - set up command timeout
+ *
+ * Get the required command timeout from the environment.
+ */
+void bootretry_init_cmd_timeout(void);
+
+/**
+ * bootretry_reset_cmd_timeout() - reset command timeout
+ *
+ * Reset the command timeout so that the user has a fresh start. This is
+ * typically used when input is received from the user.
+ */
+void bootretry_reset_cmd_timeout(void);
+
+/** bootretry_dont_retry() - Indicate that we should not retry the boot */
+void bootretry_dont_retry(void);
 #else
 static inline int bootretry_tstc_timeout(void)
 {
 	return 0;
 }
-#endif
 
-void init_cmd_timeout(void);
-void reset_cmd_timeout(void);
+static inline void bootretry_init_cmd_timeout(void)
+{
+}
+
+static inline void bootretry_reset_cmd_timeout(void)
+{
+}
+
+static inline void bootretry_dont_retry(void)
+{
+}
+
+#endif
 
 #endif
diff --git a/include/cli.h b/include/cli.h
index 61f8aee..10dbc66 100644
--- a/include/cli.h
+++ b/include/cli.h
@@ -100,9 +100,6 @@ int cli_readline_into_buffer(const char *const prompt, char *buffer,
  */
 int cli_simple_parse_line(char *line, char *argv[]);
 
-/** bootretry_dont_retry() - Indicate that we should not retry the boot */
-void bootretry_dont_retry(void);
-
 #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
 
 #endif
-- 
1.9.1.423.g4596e3a

^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [U-Boot] [PATCH 10/14] m68k: powerpc: Clean up do_mdm_init
  2014-04-11  2:01 [U-Boot] [PATCH 0/14] Split out code from the enormous main.c Simon Glass
                   ` (8 preceding siblings ...)
  2014-04-11  2:01 ` [U-Boot] [PATCH 09/14] Rename bootretry functions and remove #ifdefs Simon Glass
@ 2014-04-11  2:01 ` Simon Glass
  2014-05-30 15:28   ` Tom Rini
  2014-04-11  2:01 ` [U-Boot] [PATCH 11/14] Simplify the main loop Simon Glass
                   ` (4 subsequent siblings)
  14 siblings, 1 reply; 30+ messages in thread
From: Simon Glass @ 2014-04-11  2:01 UTC (permalink / raw)
  To: u-boot

This code seems unnecessarily complex. We really just need to check the
global_data. Now that is it all in one place, and not arch-specific, this
is pretty easy.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 arch/m68k/lib/board.c    |  7 -------
 arch/powerpc/lib/board.c |  8 --------
 common/board_r.c         | 14 --------------
 common/main.c            | 11 ++++-------
 include/common.h         |  1 +
 5 files changed, 5 insertions(+), 36 deletions(-)

diff --git a/arch/m68k/lib/board.c b/arch/m68k/lib/board.c
index e75b6a9..5a853ad 100644
--- a/arch/m68k/lib/board.c
+++ b/arch/m68k/lib/board.c
@@ -629,13 +629,6 @@ void board_init_r (gd_t *id, ulong dest_addr)
 	}
 #endif
 
-#ifdef CONFIG_MODEM_SUPPORT
- {
-	 extern int do_mdm_init;
-	 do_mdm_init = gd->do_mdm_init;
- }
-#endif
-
 #ifdef CONFIG_WATCHDOG
 	/* disable watchdog if environment is set */
 	if ((s = getenv ("watchdog")) != NULL) {
diff --git a/arch/powerpc/lib/board.c b/arch/powerpc/lib/board.c
index f86c6f3..8c0f817 100644
--- a/arch/powerpc/lib/board.c
+++ b/arch/powerpc/lib/board.c
@@ -976,14 +976,6 @@ void board_init_r(gd_t *id, ulong dest_addr)
 	kbd_init();
 #endif
 
-#ifdef CONFIG_MODEM_SUPPORT
-	{
-		extern int do_mdm_init;
-
-		do_mdm_init = gd->do_mdm_init;
-	}
-#endif
-
 	/* Initialization complete - start the monitor */
 
 	/* main_loop() can return to retry autoboot, if so just run it again. */
diff --git a/common/board_r.c b/common/board_r.c
index 8629a65..31f90ea 100644
--- a/common/board_r.c
+++ b/common/board_r.c
@@ -703,17 +703,6 @@ static int initr_kbd(void)
 }
 #endif
 
-#ifdef CONFIG_MODEM_SUPPORT
-static int initr_modem(void)
-{
-	/* TODO: with new initcalls, move this into the driver */
-	extern int do_mdm_init;
-
-	do_mdm_init = gd->do_mdm_init;
-	return 0;
-}
-#endif
-
 static int run_main_loop(void)
 {
 #ifdef CONFIG_SANDBOX
@@ -928,9 +917,6 @@ init_fnc_t init_sequence_r[] = {
 #ifdef CONFIG_PS2KBD
 	initr_kbd,
 #endif
-#ifdef CONFIG_MODEM_SUPPORT
-	initr_modem,
-#endif
 	run_main_loop,
 };
 
diff --git a/common/main.c b/common/main.c
index 91a5b18..0cb3973 100644
--- a/common/main.c
+++ b/common/main.c
@@ -14,17 +14,14 @@
 #include <malloc.h>
 #include <version.h>
 
+DECLARE_GLOBAL_DATA_PTR;
+
 /*
  * Board-specific Platform code can reimplement show_boot_progress () if needed
  */
 void inline __show_boot_progress (int val) {}
 void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
 
-#ifdef CONFIG_MODEM_SUPPORT
-int do_mdm_init = 0;
-extern void mdm_init(void); /* defined in board.c */
-#endif
-
 void main_loop(void)
 {
 #ifdef CONFIG_PREBOOT
@@ -34,8 +31,8 @@ void main_loop(void)
 	bootstage_mark_name(BOOTSTAGE_ID_MAIN_LOOP, "main_loop");
 
 #ifdef CONFIG_MODEM_SUPPORT
-	debug("DEBUG: main_loop:   do_mdm_init=%d\n", do_mdm_init);
-	if (do_mdm_init) {
+	debug("DEBUG: main_loop:   gd->do_mdm_init=%lu\n", gd->do_mdm_init);
+	if (gd->do_mdm_init) {
 		char *str = strdup(getenv("mdm_cmd"));
 		setenv("preboot", str);  /* set or delete definition */
 		if (str != NULL)
diff --git a/include/common.h b/include/common.h
index fbf3b14..3975e34 100644
--- a/include/common.h
+++ b/include/common.h
@@ -299,6 +299,7 @@ extern ulong monitor_flash_len;
 int mac_read_from_eeprom(void);
 extern u8 __dtb_dt_begin[];	/* embedded device tree blob */
 int set_cpu_clk_info(void);
+int mdm_init(void);
 #if defined(CONFIG_DISPLAY_CPUINFO)
 int print_cpuinfo(void);
 #else
-- 
1.9.1.423.g4596e3a

^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [U-Boot] [PATCH 11/14] Simplify the main loop
  2014-04-11  2:01 [U-Boot] [PATCH 0/14] Split out code from the enormous main.c Simon Glass
                   ` (9 preceding siblings ...)
  2014-04-11  2:01 ` [U-Boot] [PATCH 10/14] m68k: powerpc: Clean up do_mdm_init Simon Glass
@ 2014-04-11  2:01 ` Simon Glass
  2014-05-30 15:28   ` Tom Rini
  2014-04-11  2:01 ` [U-Boot] [PATCH 12/14] main: Hide the hush/simple details inside cli.c Simon Glass
                   ` (3 subsequent siblings)
  14 siblings, 1 reply; 30+ messages in thread
From: Simon Glass @ 2014-04-11  2:01 UTC (permalink / raw)
  To: u-boot

The main loop is easier to follow if the code is grouped into separate
functions. Make this change, so that main_loop() is easier to read.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 common/main.c | 47 ++++++++++++++++++++++++++---------------------
 1 file changed, 26 insertions(+), 21 deletions(-)

diff --git a/common/main.c b/common/main.c
index 0cb3973..d95e5fc 100644
--- a/common/main.c
+++ b/common/main.c
@@ -22,14 +22,8 @@ DECLARE_GLOBAL_DATA_PTR;
 void inline __show_boot_progress (int val) {}
 void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
 
-void main_loop(void)
+static void modem_init(void)
 {
-#ifdef CONFIG_PREBOOT
-	char *p;
-#endif
-
-	bootstage_mark_name(BOOTSTAGE_ID_MAIN_LOOP, "main_loop");
-
 #ifdef CONFIG_MODEM_SUPPORT
 	debug("DEBUG: main_loop:   gd->do_mdm_init=%lu\n", gd->do_mdm_init);
 	if (gd->do_mdm_init) {
@@ -40,22 +34,13 @@ void main_loop(void)
 		mdm_init(); /* wait for modem connection */
 	}
 #endif  /* CONFIG_MODEM_SUPPORT */
+}
 
-#ifdef CONFIG_VERSION_VARIABLE
-	{
-		setenv("ver", version_string);  /* set version variable */
-	}
-#endif /* CONFIG_VERSION_VARIABLE */
-
-#ifdef CONFIG_SYS_HUSH_PARSER
-	u_boot_hush_start();
-#endif
-
-#if defined(CONFIG_HUSH_INIT_VAR)
-	hush_init_var();
-#endif
-
+static void run_preboot_environment_command(void)
+{
 #ifdef CONFIG_PREBOOT
+	char *p;
+
 	p = getenv("preboot");
 	if (p != NULL) {
 # ifdef CONFIG_AUTOBOOT_KEYED
@@ -69,6 +54,26 @@ void main_loop(void)
 # endif
 	}
 #endif /* CONFIG_PREBOOT */
+}
+
+void main_loop(void)
+{
+	bootstage_mark_name(BOOTSTAGE_ID_MAIN_LOOP, "main_loop");
+
+	modem_init();
+#ifdef CONFIG_VERSION_VARIABLE
+	setenv("ver", version_string);  /* set version variable */
+#endif /* CONFIG_VERSION_VARIABLE */
+
+#ifdef CONFIG_SYS_HUSH_PARSER
+	u_boot_hush_start();
+#endif
+
+#if defined(CONFIG_HUSH_INIT_VAR)
+	hush_init_var();
+#endif
+
+	run_preboot_environment_command();
 
 #if defined(CONFIG_UPDATE_TFTP)
 	update_tftp(0UL);
-- 
1.9.1.423.g4596e3a

^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [U-Boot] [PATCH 12/14] main: Hide the hush/simple details inside cli.c
  2014-04-11  2:01 [U-Boot] [PATCH 0/14] Split out code from the enormous main.c Simon Glass
                   ` (10 preceding siblings ...)
  2014-04-11  2:01 ` [U-Boot] [PATCH 11/14] Simplify the main loop Simon Glass
@ 2014-04-11  2:01 ` Simon Glass
  2014-05-30 15:28   ` Tom Rini
  2014-04-11  2:01 ` [U-Boot] [PATCH 13/14] main: Make the execution path a little clearer in main.c Simon Glass
                   ` (2 subsequent siblings)
  14 siblings, 1 reply; 30+ messages in thread
From: Simon Glass @ 2014-04-11  2:01 UTC (permalink / raw)
  To: u-boot

Move these details from main (which doesn't care which parser is used) to
cli.c where they belong.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 common/cli.c        | 22 ++++++++++++++++++++++
 common/cli_simple.c |  2 +-
 common/main.c       | 16 ++--------------
 include/cli.h       | 13 ++++++++++++-
 4 files changed, 37 insertions(+), 16 deletions(-)

diff --git a/common/cli.c b/common/cli.c
index 9cf7ba1..4ac9b3f 100644
--- a/common/cli.c
+++ b/common/cli.c
@@ -104,3 +104,25 @@ int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 	return 0;
 }
 #endif
+
+void cli_loop(void)
+{
+#ifdef CONFIG_SYS_HUSH_PARSER
+	parse_file_outer();
+	/* This point is never reached */
+	for (;;);
+#else
+	cli_simple_loop();
+#endif /*CONFIG_SYS_HUSH_PARSER*/
+}
+
+void cli_init(void)
+{
+#ifdef CONFIG_SYS_HUSH_PARSER
+	u_boot_hush_start();
+#endif
+
+#if defined(CONFIG_HUSH_INIT_VAR)
+	hush_init_var();
+#endif
+}
diff --git a/common/cli_simple.c b/common/cli_simple.c
index bba586e..413c2eb 100644
--- a/common/cli_simple.c
+++ b/common/cli_simple.c
@@ -256,7 +256,7 @@ int cli_simple_run_command(const char *cmd, int flag)
 	return rc ? rc : repeatable;
 }
 
-void cli_loop(void)
+void cli_simple_loop(void)
 {
 	static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
 
diff --git a/common/main.c b/common/main.c
index d95e5fc..12f6ac4 100644
--- a/common/main.c
+++ b/common/main.c
@@ -10,7 +10,6 @@
 #include <common.h>
 #include <autoboot.h>
 #include <cli.h>
-#include <cli_hush.h>
 #include <malloc.h>
 #include <version.h>
 
@@ -65,13 +64,7 @@ void main_loop(void)
 	setenv("ver", version_string);  /* set version variable */
 #endif /* CONFIG_VERSION_VARIABLE */
 
-#ifdef CONFIG_SYS_HUSH_PARSER
-	u_boot_hush_start();
-#endif
-
-#if defined(CONFIG_HUSH_INIT_VAR)
-	hush_init_var();
-#endif
+	cli_init();
 
 	run_preboot_environment_command();
 
@@ -83,11 +76,6 @@ void main_loop(void)
 	/*
 	 * Main Loop for Monitor Command Processing
 	 */
-#ifdef CONFIG_SYS_HUSH_PARSER
-	parse_file_outer();
-	/* This point is never reached */
-	for (;;);
-#else
+
 	cli_loop();
-#endif /*CONFIG_SYS_HUSH_PARSER*/
 }
diff --git a/include/cli.h b/include/cli.h
index 10dbc66..5158976 100644
--- a/include/cli.h
+++ b/include/cli.h
@@ -14,7 +14,7 @@
  * This will return if we get a timeout waiting for a command. See
  * CONFIG_BOOT_RETRY_TIME.
  */
-void cli_loop(void);
+void cli_simple_loop(void);
 
 /**
  * cli_simple_run_command() - Execute a command with the simple CLI
@@ -100,6 +100,17 @@ int cli_readline_into_buffer(const char *const prompt, char *buffer,
  */
 int cli_simple_parse_line(char *line, char *argv[]);
 
+/**
+ * Go into the command loop
+ *
+ * This will return if we get a timeout waiting for a command, but only for
+ * the simple parser (not hush). See CONFIG_BOOT_RETRY_TIME.
+ */
+void cli_loop(void);
+
+/** Set up the command line interpreter ready for action */
+void cli_init(void);
+
 #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
 
 #endif
-- 
1.9.1.423.g4596e3a

^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [U-Boot] [PATCH 13/14] main: Make the execution path a little clearer in main.c
  2014-04-11  2:01 [U-Boot] [PATCH 0/14] Split out code from the enormous main.c Simon Glass
                   ` (11 preceding siblings ...)
  2014-04-11  2:01 ` [U-Boot] [PATCH 12/14] main: Hide the hush/simple details inside cli.c Simon Glass
@ 2014-04-11  2:01 ` Simon Glass
  2014-05-30 15:28   ` Tom Rini
  2014-04-11  2:01 ` [U-Boot] [PATCH 14/14] main: Avoid unncessary strdup()/free() Simon Glass
  2014-04-21 17:26 ` [U-Boot] [PATCH 0/14] Split out code from the enormous main.c Simon Glass
  14 siblings, 1 reply; 30+ messages in thread
From: Simon Glass @ 2014-04-11  2:01 UTC (permalink / raw)
  To: u-boot

bootdelay_process() never returns in some circumstances, whichs makes the
control flow confusing. Change it so that the decision about how to execute
the boot command is made in the main_loop() code, so it is easier to follow.
Move CLI stuff to cli.c.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 common/autoboot.c  | 81 ++++++++----------------------------------------------
 common/cli.c       | 66 ++++++++++++++++++++++++++++++++++++++++++++
 common/main.c      | 12 +++++---
 include/autoboot.h | 28 +++++++++++++++++--
 include/cli.h      | 33 ++++++++++++++++++++++
 5 files changed, 145 insertions(+), 75 deletions(-)

diff --git a/common/autoboot.c b/common/autoboot.c
index 9843898..dc24cae 100644
--- a/common/autoboot.c
+++ b/common/autoboot.c
@@ -22,6 +22,9 @@ DECLARE_GLOBAL_DATA_PTR;
 #define debug_bootkeys(fmt, args...)		\
 	debug_cond(DEBUG_BOOTKEYS, fmt, ##args)
 
+/* Stored value of bootdelay, used by autoboot_command() */
+static int stored_bootdelay;
+
 /***************************************************************************
  * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
  * returns: 0 -  no key string, allow autoboot 1 - got key string, abort
@@ -205,57 +208,9 @@ static int abortboot(int bootdelay)
 #endif
 }
 
-/*
- * Runs the given boot command securely.  Specifically:
- * - Doesn't run the command with the shell (run_command or parse_string_outer),
- *   since that's a lot of code surface that an attacker might exploit.
- *   Because of this, we don't do any argument parsing--the secure boot command
- *   has to be a full-fledged u-boot command.
- * - Doesn't check for keypresses before booting, since that could be a
- *   security hole; also disables Ctrl-C.
- * - Doesn't allow the command to return.
- *
- * Upon any failures, this function will drop into an infinite loop after
- * printing the error message to console.
- */
-
-#if defined(CONFIG_OF_CONTROL)
-static void secure_boot_cmd(char *cmd)
-{
-	cmd_tbl_t *cmdtp;
-	int rc;
-
-	if (!cmd) {
-		printf("## Error: Secure boot command not specified\n");
-		goto err;
-	}
-
-	/* Disable Ctrl-C just in case some command is used that checks it. */
-	disable_ctrlc(1);
-
-	/* Find the command directly. */
-	cmdtp = find_cmd(cmd);
-	if (!cmdtp) {
-		printf("## Error: \"%s\" not defined\n", cmd);
-		goto err;
-	}
-
-	/* Run the command, forcing no flags and faking argc and argv. */
-	rc = (cmdtp->cmd)(cmdtp, 0, 1, &cmd);
-
-	/* Shouldn't ever return from boot command. */
-	printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
-
-err:
-	/*
-	 * Not a whole lot to do here.  Rebooting won't help much, since we'll
-	 * just end up right back here.  Just loop.
-	 */
-	hang();
-}
-
 static void process_fdt_options(const void *blob)
 {
+#if defined(CONFIG_OF_CONTROL)
 	ulong addr;
 
 	/* Add an env variable to point to a kernel payload, if available */
@@ -267,14 +222,11 @@ static void process_fdt_options(const void *blob)
 	addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
 	if (addr)
 		setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
-}
 #endif /* CONFIG_OF_CONTROL */
+}
 
-void bootdelay_process(void)
+const char *bootdelay_process(void)
 {
-#ifdef CONFIG_OF_CONTROL
-	char *env;
-#endif
 	char *s;
 	int bootdelay;
 #ifdef CONFIG_BOOTCOUNT_LIMIT
@@ -318,27 +270,18 @@ void bootdelay_process(void)
 	} else
 #endif /* CONFIG_BOOTCOUNT_LIMIT */
 		s = getenv("bootcmd");
-#ifdef CONFIG_OF_CONTROL
-	/* Allow the fdt to override the boot command */
-	env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
-	if (env)
-		s = env;
 
 	process_fdt_options(gd->fdt_blob);
+	stored_bootdelay = bootdelay;
 
-	/*
-	 * If the bootsecure option was chosen, use secure_boot_cmd().
-	 * Always use 'env' in this case, since bootsecure requres that the
-	 * bootcmd was specified in the FDT too.
-	 */
-	if (fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0))
-		secure_boot_cmd(env);
-
-#endif /* CONFIG_OF_CONTROL */
+	return s;
+}
 
+void autoboot_command(const char *s)
+{
 	debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
 
-	if (bootdelay != -1 && s && !abortboot(bootdelay)) {
+	if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
 #if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC)
 		int prev = disable_ctrlc(1);	/* disable Control C checking */
 #endif
diff --git a/common/cli.c b/common/cli.c
index 4ac9b3f..ea6bfb3 100644
--- a/common/cli.c
+++ b/common/cli.c
@@ -12,8 +12,11 @@
 #include <common.h>
 #include <cli.h>
 #include <cli_hush.h>
+#include <fdtdec.h>
 #include <malloc.h>
 
+DECLARE_GLOBAL_DATA_PTR;
+
 /*
  * Run a command using the selected parser.
  *
@@ -105,6 +108,69 @@ int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 }
 #endif
 
+#ifdef CONFIG_OF_CONTROL
+bool cli_process_fdt(const char **cmdp)
+{
+	/* Allow the fdt to override the boot command */
+	char *env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
+	if (env)
+		*cmdp = env;
+	/*
+	 * If the bootsecure option was chosen, use secure_boot_cmd().
+	 * Always use 'env' in this case, since bootsecure requres that the
+	 * bootcmd was specified in the FDT too.
+	 */
+	return fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0) != 0;
+}
+
+/*
+ * Runs the given boot command securely.  Specifically:
+ * - Doesn't run the command with the shell (run_command or parse_string_outer),
+ *   since that's a lot of code surface that an attacker might exploit.
+ *   Because of this, we don't do any argument parsing--the secure boot command
+ *   has to be a full-fledged u-boot command.
+ * - Doesn't check for keypresses before booting, since that could be a
+ *   security hole; also disables Ctrl-C.
+ * - Doesn't allow the command to return.
+ *
+ * Upon any failures, this function will drop into an infinite loop after
+ * printing the error message to console.
+ */
+void cli_secure_boot_cmd(const char *cmd)
+{
+	cmd_tbl_t *cmdtp;
+	int rc;
+
+	if (!cmd) {
+		printf("## Error: Secure boot command not specified\n");
+		goto err;
+	}
+
+	/* Disable Ctrl-C just in case some command is used that checks it. */
+	disable_ctrlc(1);
+
+	/* Find the command directly. */
+	cmdtp = find_cmd(cmd);
+	if (!cmdtp) {
+		printf("## Error: \"%s\" not defined\n", cmd);
+		goto err;
+	}
+
+	/* Run the command, forcing no flags and faking argc and argv. */
+	rc = (cmdtp->cmd)(cmdtp, 0, 1, (char **)&cmd);
+
+	/* Shouldn't ever return from boot command. */
+	printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
+
+err:
+	/*
+	 * Not a whole lot to do here.  Rebooting won't help much, since we'll
+	 * just end up right back here.  Just loop.
+	 */
+	hang();
+}
+#endif /* CONFIG_OF_CONTROL */
+
 void cli_loop(void)
 {
 #ifdef CONFIG_SYS_HUSH_PARSER
diff --git a/common/main.c b/common/main.c
index 12f6ac4..b0d3ea4 100644
--- a/common/main.c
+++ b/common/main.c
@@ -55,8 +55,11 @@ static void run_preboot_environment_command(void)
 #endif /* CONFIG_PREBOOT */
 }
 
+/* We come here after U-Boot is initialised and ready to process commands */
 void main_loop(void)
 {
+	const char *s;
+
 	bootstage_mark_name(BOOTSTAGE_ID_MAIN_LOOP, "main_loop");
 
 	modem_init();
@@ -72,10 +75,11 @@ void main_loop(void)
 	update_tftp(0UL);
 #endif /* CONFIG_UPDATE_TFTP */
 
-	bootdelay_process();
-	/*
-	 * Main Loop for Monitor Command Processing
-	 */
+	s = bootdelay_process();
+	if (cli_process_fdt(&s))
+		cli_secure_boot_cmd(s);
+
+	autoboot_command(s);
 
 	cli_loop();
 }
diff --git a/include/autoboot.h b/include/autoboot.h
index aaae4af..3a9059a 100644
--- a/include/autoboot.h
+++ b/include/autoboot.h
@@ -13,9 +13,33 @@
 #define __AUTOBOOT_H
 
 #ifdef CONFIG_BOOTDELAY
-void bootdelay_process(void);
+/**
+ * bootdelay_process() - process the bootd delay
+ *
+ * Process the boot delay, boot limit, then get the value of either
+ * bootcmd, failbootcmd or altbootcmd depending on the current state.
+ * Return this command so it can be executed.
+ *
+ * @return command to executed
+ */
+const char *bootdelay_process(void);
+
+/**
+ * autoboot_command() - run the autoboot command
+ *
+ * If enabled, run the autoboot command returned from bootdelay_process().
+ * Also do the CONFIG_MENUKEY processing if enabled.
+ *
+ * @cmd: Command to run
+ */
+void autoboot_command(const char *cmd);
 #else
-static inline void bootdelay_process(void)
+static inline const char *bootdelay_process(void)
+{
+	return NULL;
+}
+
+static inline void autoboot_command(const char *s)
 {
 }
 #endif
diff --git a/include/cli.h b/include/cli.h
index 5158976..6994262 100644
--- a/include/cli.h
+++ b/include/cli.h
@@ -100,6 +100,39 @@ int cli_readline_into_buffer(const char *const prompt, char *buffer,
  */
 int cli_simple_parse_line(char *line, char *argv[]);
 
+#ifdef CONFIG_OF_CONTROL
+/**
+ * cli_process_fdt() - process the boot command from the FDT
+ *
+ * If bootcmmd is defined in the /config node of the FDT, we use that
+ * as the boot command. Further, if bootsecure is set to 1 (in the same
+ * node) then we return true, indicating that the command should be executed
+ * as securely as possible, avoiding the CLI parser.
+ *
+ * @cmdp:	On entry, the command that will be executed if the FDT does
+ *		not have a command. Returns the command to execute after
+ *		checking the FDT.
+ * @return true to execute securely, else false
+ */
+bool cli_process_fdt(const char **cmdp);
+
+/** cli_secure_boot_cmd() - execute a command as securely as possible
+ *
+ * This avoids using the parser, thus executing the command with the
+ * smallest amount of code. Parameters are not supported.
+ */
+void cli_secure_boot_cmd(const char *cmd);
+#else
+static inline bool cli_process_fdt(const char **cmdp)
+{
+	return false;
+}
+
+static inline void cli_secure_boot_cmd(const char *cmd)
+{
+}
+#endif /* CONFIG_OF_CONTROL */
+
 /**
  * Go into the command loop
  *
-- 
1.9.1.423.g4596e3a

^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [U-Boot] [PATCH 14/14] main: Avoid unncessary strdup()/free()
  2014-04-11  2:01 [U-Boot] [PATCH 0/14] Split out code from the enormous main.c Simon Glass
                   ` (12 preceding siblings ...)
  2014-04-11  2:01 ` [U-Boot] [PATCH 13/14] main: Make the execution path a little clearer in main.c Simon Glass
@ 2014-04-11  2:01 ` Simon Glass
  2014-05-30 15:29   ` Tom Rini
  2014-04-21 17:26 ` [U-Boot] [PATCH 0/14] Split out code from the enormous main.c Simon Glass
  14 siblings, 1 reply; 30+ messages in thread
From: Simon Glass @ 2014-04-11  2:01 UTC (permalink / raw)
  To: u-boot

It doesn't seem necessary to use memory allocation in this code. The setenv()
will make a copy anyway.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 common/main.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/common/main.c b/common/main.c
index b0d3ea4..ab1f708 100644
--- a/common/main.c
+++ b/common/main.c
@@ -10,7 +10,6 @@
 #include <common.h>
 #include <autoboot.h>
 #include <cli.h>
-#include <malloc.h>
 #include <version.h>
 
 DECLARE_GLOBAL_DATA_PTR;
@@ -26,10 +25,9 @@ static void modem_init(void)
 #ifdef CONFIG_MODEM_SUPPORT
 	debug("DEBUG: main_loop:   gd->do_mdm_init=%lu\n", gd->do_mdm_init);
 	if (gd->do_mdm_init) {
-		char *str = strdup(getenv("mdm_cmd"));
+		char *str = getenv("mdm_cmd");
+
 		setenv("preboot", str);  /* set or delete definition */
-		if (str != NULL)
-			free(str);
 		mdm_init(); /* wait for modem connection */
 	}
 #endif  /* CONFIG_MODEM_SUPPORT */
-- 
1.9.1.423.g4596e3a

^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [U-Boot] [PATCH 0/14] Split out code from the enormous main.c
  2014-04-11  2:01 [U-Boot] [PATCH 0/14] Split out code from the enormous main.c Simon Glass
                   ` (13 preceding siblings ...)
  2014-04-11  2:01 ` [U-Boot] [PATCH 14/14] main: Avoid unncessary strdup()/free() Simon Glass
@ 2014-04-21 17:26 ` Simon Glass
  14 siblings, 0 replies; 30+ messages in thread
From: Simon Glass @ 2014-04-21 17:26 UTC (permalink / raw)
  To: u-boot

Hi,

On 10 April 2014 20:01, Simon Glass <sjg@chromium.org> wrote:
> A previous series created a way of using if () instead of #ifdef for
> controlling feature inclusion in U-Boot. The primary target of that series
> was common/main.c which is full of #ifdefs. That work was put on hold while
> the kbuild work was in progress.
>
> Since kbuild is now complete, it is time to take another look. However, in
> the meantime main.c has not improved. It seems like a good idea to try to
> split the code out a bit, to make it more obvious what is happening in the
> U-Boot start-up.
>
> This series splits main into two main program and a CLI (Command-line
> interpreter) parts. There are two CLIs - hush and simple, and each is put
> in its own file, with a new cli.c to unify them.
>
> New files are also created for autoboot and bootretry functionality.
>
> Overall this series makes it easier to read what is happening in main.c,
> and also clarifies the parser code.
>
>
> Simon Glass (14):
>   Remove unnecessary use of hush header file
>   Rename hush to cli_hush
>   move CLI prototypes to cli.h and add comments
>   Split out simple parser and readline into separate files
>   Add cli_ prefix to readline functions
>   Move autoboot code to autoboot.c
>   Move command line API into cli.c
>   Move bootretry code into bootretry.c and clean up
>   Rename bootretry functions and remove #ifdefs
>   m68k: powerpc: Clean up do_mdm_init
>   Simplify the main loop
>   main: Hide the hush/simple details inside cli.c
>   main: Make the execution path a little clearer in main.c
>   main: Avoid unncessary strdup()/free()

Any comments on this series please? Since it touches main it may be
painful to rebase. Also I'm holding off on the autoconf stuff until I
get some thoughts on this one.

Regards,
Simon

^ permalink raw reply	[flat|nested] 30+ messages in thread

* [U-Boot] [PATCH 01/14] Remove unnecessary use of hush header file
  2014-04-11  2:01 ` [U-Boot] [PATCH 01/14] Remove unnecessary use of hush header file Simon Glass
@ 2014-05-30 15:27   ` Tom Rini
  0 siblings, 0 replies; 30+ messages in thread
From: Tom Rini @ 2014-05-30 15:27 UTC (permalink / raw)
  To: u-boot

On Thu, Apr 10, 2014 at 08:01:23PM -0600, Simon Glass wrote:

> Some files include hush.h but don't actually use it. Remove this where
> possible.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140530/7925dc6d/attachment.pgp>

^ permalink raw reply	[flat|nested] 30+ messages in thread

* [U-Boot] [PATCH 02/14] Rename hush to cli_hush
  2014-04-11  2:01 ` [U-Boot] [PATCH 02/14] Rename hush to cli_hush Simon Glass
@ 2014-05-30 15:27   ` Tom Rini
  0 siblings, 0 replies; 30+ messages in thread
From: Tom Rini @ 2014-05-30 15:27 UTC (permalink / raw)
  To: u-boot

On Thu, Apr 10, 2014 at 08:01:24PM -0600, Simon Glass wrote:

> Hush is a command-line interpreter, so rename it to make that clearer.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140530/eec031e8/attachment.pgp>

^ permalink raw reply	[flat|nested] 30+ messages in thread

* [U-Boot] [PATCH 03/14] move CLI prototypes to cli.h and add comments
  2014-04-11  2:01 ` [U-Boot] [PATCH 03/14] move CLI prototypes to cli.h and add comments Simon Glass
@ 2014-05-30 15:27   ` Tom Rini
  0 siblings, 0 replies; 30+ messages in thread
From: Tom Rini @ 2014-05-30 15:27 UTC (permalink / raw)
  To: u-boot

On Thu, Apr 10, 2014 at 08:01:25PM -0600, Simon Glass wrote:

> Move the CLI prototypes from common.h to cli.h as part of an effort to
> reduce the size of common.h.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140530/98cda603/attachment.pgp>

^ permalink raw reply	[flat|nested] 30+ messages in thread

* [U-Boot] [PATCH 04/14] Split out simple parser and readline into separate files
  2014-04-11  2:01 ` [U-Boot] [PATCH 04/14] Split out simple parser and readline into separate files Simon Glass
@ 2014-05-30 15:27   ` Tom Rini
  0 siblings, 0 replies; 30+ messages in thread
From: Tom Rini @ 2014-05-30 15:27 UTC (permalink / raw)
  To: u-boot

On Thu, Apr 10, 2014 at 08:01:26PM -0600, Simon Glass wrote:

> It doesn't make sense to have the simple parser and the readline code
> all in main. Split them out into separate files.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140530/6b902795/attachment.pgp>

^ permalink raw reply	[flat|nested] 30+ messages in thread

* [U-Boot] [PATCH 05/14] Add cli_ prefix to readline functions
  2014-04-11  2:01 ` [U-Boot] [PATCH 05/14] Add cli_ prefix to readline functions Simon Glass
@ 2014-05-30 15:27   ` Tom Rini
  0 siblings, 0 replies; 30+ messages in thread
From: Tom Rini @ 2014-05-30 15:27 UTC (permalink / raw)
  To: u-boot

On Thu, Apr 10, 2014 at 08:01:27PM -0600, Simon Glass wrote:

> This makes it clear where the code resides.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140530/f83264ee/attachment.pgp>

^ permalink raw reply	[flat|nested] 30+ messages in thread

* [U-Boot] [PATCH 06/14] Move autoboot code to autoboot.c
  2014-04-11  2:01 ` [U-Boot] [PATCH 06/14] Move autoboot code to autoboot.c Simon Glass
@ 2014-05-30 15:28   ` Tom Rini
  0 siblings, 0 replies; 30+ messages in thread
From: Tom Rini @ 2014-05-30 15:28 UTC (permalink / raw)
  To: u-boot

On Thu, Apr 10, 2014 at 08:01:28PM -0600, Simon Glass wrote:

> The autoboot code is complex and long. It deserves its own file with
> a simple interface from main.c.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140530/b43f5402/attachment.pgp>

^ permalink raw reply	[flat|nested] 30+ messages in thread

* [U-Boot] [PATCH 07/14] Move command line API into cli.c
  2014-04-11  2:01 ` [U-Boot] [PATCH 07/14] Move command line API into cli.c Simon Glass
@ 2014-05-30 15:28   ` Tom Rini
  0 siblings, 0 replies; 30+ messages in thread
From: Tom Rini @ 2014-05-30 15:28 UTC (permalink / raw)
  To: u-boot

On Thu, Apr 10, 2014 at 08:01:29PM -0600, Simon Glass wrote:

> We now have a single entry point to the CLI, whether simple or hush. Put
> this in its own file.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140530/84515be8/attachment.pgp>

^ permalink raw reply	[flat|nested] 30+ messages in thread

* [U-Boot] [PATCH 08/14] Move bootretry code into bootretry.c and clean up
  2014-04-11  2:01 ` [U-Boot] [PATCH 08/14] Move bootretry code into bootretry.c and clean up Simon Glass
@ 2014-05-30 15:28   ` Tom Rini
  0 siblings, 0 replies; 30+ messages in thread
From: Tom Rini @ 2014-05-30 15:28 UTC (permalink / raw)
  To: u-boot

On Thu, Apr 10, 2014 at 08:01:30PM -0600, Simon Glass wrote:

> This code is only used by one board, so it seems a shame to clutter up
> the readline code with it. Move it into its own file.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140530/453a996d/attachment.pgp>

^ permalink raw reply	[flat|nested] 30+ messages in thread

* [U-Boot] [PATCH 09/14] Rename bootretry functions and remove #ifdefs
  2014-04-11  2:01 ` [U-Boot] [PATCH 09/14] Rename bootretry functions and remove #ifdefs Simon Glass
@ 2014-05-30 15:28   ` Tom Rini
  0 siblings, 0 replies; 30+ messages in thread
From: Tom Rini @ 2014-05-30 15:28 UTC (permalink / raw)
  To: u-boot

On Thu, Apr 10, 2014 at 08:01:31PM -0600, Simon Glass wrote:

> Add a bootretry_ prefix to these two functions, and remove the need for
> the #ifdef around everything (it moves to the Makefile).
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140530/96b91fbe/attachment.pgp>

^ permalink raw reply	[flat|nested] 30+ messages in thread

* [U-Boot] [PATCH 10/14] m68k: powerpc: Clean up do_mdm_init
  2014-04-11  2:01 ` [U-Boot] [PATCH 10/14] m68k: powerpc: Clean up do_mdm_init Simon Glass
@ 2014-05-30 15:28   ` Tom Rini
  0 siblings, 0 replies; 30+ messages in thread
From: Tom Rini @ 2014-05-30 15:28 UTC (permalink / raw)
  To: u-boot

On Thu, Apr 10, 2014 at 08:01:32PM -0600, Simon Glass wrote:

> This code seems unnecessarily complex. We really just need to check the
> global_data. Now that is it all in one place, and not arch-specific, this
> is pretty easy.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140530/e39e3587/attachment.pgp>

^ permalink raw reply	[flat|nested] 30+ messages in thread

* [U-Boot] [PATCH 11/14] Simplify the main loop
  2014-04-11  2:01 ` [U-Boot] [PATCH 11/14] Simplify the main loop Simon Glass
@ 2014-05-30 15:28   ` Tom Rini
  0 siblings, 0 replies; 30+ messages in thread
From: Tom Rini @ 2014-05-30 15:28 UTC (permalink / raw)
  To: u-boot

On Thu, Apr 10, 2014 at 08:01:33PM -0600, Simon Glass wrote:

> The main loop is easier to follow if the code is grouped into separate
> functions. Make this change, so that main_loop() is easier to read.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140530/9a8a7e2c/attachment.pgp>

^ permalink raw reply	[flat|nested] 30+ messages in thread

* [U-Boot] [PATCH 12/14] main: Hide the hush/simple details inside cli.c
  2014-04-11  2:01 ` [U-Boot] [PATCH 12/14] main: Hide the hush/simple details inside cli.c Simon Glass
@ 2014-05-30 15:28   ` Tom Rini
  0 siblings, 0 replies; 30+ messages in thread
From: Tom Rini @ 2014-05-30 15:28 UTC (permalink / raw)
  To: u-boot

On Thu, Apr 10, 2014 at 08:01:34PM -0600, Simon Glass wrote:

> Move these details from main (which doesn't care which parser is used) to
> cli.c where they belong.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140530/c95f4cb1/attachment.pgp>

^ permalink raw reply	[flat|nested] 30+ messages in thread

* [U-Boot] [PATCH 13/14] main: Make the execution path a little clearer in main.c
  2014-04-11  2:01 ` [U-Boot] [PATCH 13/14] main: Make the execution path a little clearer in main.c Simon Glass
@ 2014-05-30 15:28   ` Tom Rini
  0 siblings, 0 replies; 30+ messages in thread
From: Tom Rini @ 2014-05-30 15:28 UTC (permalink / raw)
  To: u-boot

On Thu, Apr 10, 2014 at 08:01:35PM -0600, Simon Glass wrote:

> bootdelay_process() never returns in some circumstances, whichs makes the
> control flow confusing. Change it so that the decision about how to execute
> the boot command is made in the main_loop() code, so it is easier to follow.
> Move CLI stuff to cli.c.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140530/ea4b3f05/attachment.pgp>

^ permalink raw reply	[flat|nested] 30+ messages in thread

* [U-Boot] [PATCH 14/14] main: Avoid unncessary strdup()/free()
  2014-04-11  2:01 ` [U-Boot] [PATCH 14/14] main: Avoid unncessary strdup()/free() Simon Glass
@ 2014-05-30 15:29   ` Tom Rini
  0 siblings, 0 replies; 30+ messages in thread
From: Tom Rini @ 2014-05-30 15:29 UTC (permalink / raw)
  To: u-boot

On Thu, Apr 10, 2014 at 08:01:36PM -0600, Simon Glass wrote:

> It doesn't seem necessary to use memory allocation in this code. The setenv()
> will make a copy anyway.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140530/7d096c43/attachment.pgp>

^ permalink raw reply	[flat|nested] 30+ messages in thread

end of thread, other threads:[~2014-05-30 15:29 UTC | newest]

Thread overview: 30+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-04-11  2:01 [U-Boot] [PATCH 0/14] Split out code from the enormous main.c Simon Glass
2014-04-11  2:01 ` [U-Boot] [PATCH 01/14] Remove unnecessary use of hush header file Simon Glass
2014-05-30 15:27   ` Tom Rini
2014-04-11  2:01 ` [U-Boot] [PATCH 02/14] Rename hush to cli_hush Simon Glass
2014-05-30 15:27   ` Tom Rini
2014-04-11  2:01 ` [U-Boot] [PATCH 03/14] move CLI prototypes to cli.h and add comments Simon Glass
2014-05-30 15:27   ` Tom Rini
2014-04-11  2:01 ` [U-Boot] [PATCH 04/14] Split out simple parser and readline into separate files Simon Glass
2014-05-30 15:27   ` Tom Rini
2014-04-11  2:01 ` [U-Boot] [PATCH 05/14] Add cli_ prefix to readline functions Simon Glass
2014-05-30 15:27   ` Tom Rini
2014-04-11  2:01 ` [U-Boot] [PATCH 06/14] Move autoboot code to autoboot.c Simon Glass
2014-05-30 15:28   ` Tom Rini
2014-04-11  2:01 ` [U-Boot] [PATCH 07/14] Move command line API into cli.c Simon Glass
2014-05-30 15:28   ` Tom Rini
2014-04-11  2:01 ` [U-Boot] [PATCH 08/14] Move bootretry code into bootretry.c and clean up Simon Glass
2014-05-30 15:28   ` Tom Rini
2014-04-11  2:01 ` [U-Boot] [PATCH 09/14] Rename bootretry functions and remove #ifdefs Simon Glass
2014-05-30 15:28   ` Tom Rini
2014-04-11  2:01 ` [U-Boot] [PATCH 10/14] m68k: powerpc: Clean up do_mdm_init Simon Glass
2014-05-30 15:28   ` Tom Rini
2014-04-11  2:01 ` [U-Boot] [PATCH 11/14] Simplify the main loop Simon Glass
2014-05-30 15:28   ` Tom Rini
2014-04-11  2:01 ` [U-Boot] [PATCH 12/14] main: Hide the hush/simple details inside cli.c Simon Glass
2014-05-30 15:28   ` Tom Rini
2014-04-11  2:01 ` [U-Boot] [PATCH 13/14] main: Make the execution path a little clearer in main.c Simon Glass
2014-05-30 15:28   ` Tom Rini
2014-04-11  2:01 ` [U-Boot] [PATCH 14/14] main: Avoid unncessary strdup()/free() Simon Glass
2014-05-30 15:29   ` Tom Rini
2014-04-21 17:26 ` [U-Boot] [PATCH 0/14] Split out code from the enormous main.c Simon Glass

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.