Skip to contentSkip to conversation

Split out code from the enormous main.c

PATCH New Simon Glass 2 participants 30 messages Apr 11, 2014 – May 30, 2014

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover Split out code from the enormous main.c

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%)
Signature · 1 line
1.9.1.423.g4596e3a
#1 lore raw

PATCH 1/14 Remove unnecessary use of hush header file

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

---

 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(-)
arch/arm/cpu/arm926ejs/kirkwood/cpu.c +0 -1
index d4711c0..0937506 100644
@@ -13,7 +13,6 @@
 #include <asm/io.h>
 #include <asm/arch/cpu.h>
 #include <asm/arch/kirkwood.h>
-#include <hush.h>
 
 #define BUFLEN	16
 
arch/arm/cpu/arm926ejs/orion5x/cpu.c +0 -1
index b55c5f0..f88db3b 100644
@@ -15,7 +15,6 @@
 #include <asm/io.h>
 #include <u-boot/md5.h>
 #include <asm/arch/cpu.h>
-#include <hush.h>
 
 #define BUFLEN	16
 
board/ait/cam_enc_4xx/cam_enc_4xx.c +0 -1
index 7e1b16a..b5cc3ed 100644
@@ -9,7 +9,6 @@
 
 #include <common.h>
 #include <errno.h>
-#include <hush.h>
 #include <linux/mtd/nand.h>
 #include <nand.h>
 #include <miiphy.h>
board/mcc200/auto_update.c +1 -6
index 2f622b0..43173ce 100644
@@ -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;
 	}
 
common/cmd_bootm.c +0 -4
index 9751edc..bfca006 100644
@@ -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>
common/cmd_bootmenu.c +0 -1
index 163d5b2..5879065 100644
@@ -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>
Signature · 1 line
1.9.1.423.g4596e3a
#3 lore raw

PATCH 2/14 Rename hush to cli_hush

In reply to Simon Glass

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

---

 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%)
board/keymile/common/common.c +1 -1
index f941e44..2ddb3da 100644
@@ -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>
board/keymile/common/ivm.c +1 -1
index f0e91bb..bffc08b 100644
@@ -6,7 +6,7 @@
  */
 
 #include <common.h>
-#include <hush.h>
+#include <cli_hush.h>
 #include <i2c.h>
 #include "common.h"
 
common/Makefile +1 -1
index cecd81a..12068ac 100644
@@ -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
common/cli_hush.c renamed from common/hush.c +2 -2
similarity index 99%
rename from common/hush.c
rename to common/cli_hush.c
index df10267..070519f 100644
@@ -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
common/main.c +1 -1
index 8b6f274..2eea9c5 100644
@@ -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>
include/cli_hush.h renamed from include/hush.h +2 -2
similarity index 93%
rename from include/hush.h
rename to include/cli_hush.h
index 595303a..4951eef 100644
@@ -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 */
Signature · 1 line
1.9.1.423.g4596e3a
#5 lore raw

PATCH 3/14 move CLI prototypes to cli.h and add comments

In reply to Simon Glass

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

---

 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
board/ait/cam_enc_4xx/cam_enc_4xx.c +1 -0
index b5cc3ed..7c26ba0 100644
@@ -8,6 +8,7 @@
  */
 
 #include <common.h>
+#include <cli.h>
 #include <errno.h>
 #include <linux/mtd/nand.h>
 #include <nand.h>
board/amcc/yucca/cmd_yucca.c +1 -0
index dc78b73..3257c29 100644
@@ -8,6 +8,7 @@
  */
 
 #include <common.h>
+#include <cli.h>
 #include <command.h>
 #include "yucca.h"
 #include <i2c.h>
board/eltec/elppc/misc.c +1 -0
index d80eaba..70d1f9a 100644
@@ -7,6 +7,7 @@
 
 /* includes */
 #include <common.h>
+#include <cli.h>
 #include <linux/ctype.h>
 #include <pci.h>
 #include <net.h>
board/eltec/mhpc/mhpc.c +1 -0
index f3f564f..ff9e0ab 100644
@@ -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"
board/hymod/hymod.c +1 -0
index 5fec914..ea49e26 100644
@@ -8,6 +8,7 @@
  */
 
 #include <common.h>
+#include <cli.h>
 #include <mpc8260.h>
 #include <mpc8260_irq.h>
 #include <ioports.h>
board/hymod/input.c +1 -0
index 184902c..23d3f19 100644
@@ -6,6 +6,7 @@
  */
 
 #include <common.h>
+#include <cli.h>
 
 int
 hymod_get_serno (const char *prompt)
common/cli_hush.c +1 -0
index 070519f..48cec88 100644
@@ -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
common/cmd_bedbug.c +1 -0
index 77b6e3e..f1a70ef 100644
@@ -3,6 +3,7 @@
  */
 
 #include <common.h>
+#include <cli.h>
 #include <command.h>
 #include <linux/ctype.h>
 #include <net.h>
common/cmd_dcr.c +1 -0
index 896f79f..c5bcb8b 100644
@@ -10,6 +10,7 @@
  */
 
 #include <common.h>
+#include <cli.h>
 #include <config.h>
 #include <command.h>
 
common/cmd_i2c.c +1 -0
index ebce7d4..8ccde68 100644
@@ -66,6 +66,7 @@
  */
 
 #include <common.h>
+#include <cli.h>
 #include <command.h>
 #include <edid.h>
 #include <environment.h>
common/cmd_mem.c +1 -0
index 5b03c2d..4b8738b 100644
@@ -12,6 +12,7 @@
  */
 
 #include <common.h>
+#include <cli.h>
 #include <command.h>
 #ifdef CONFIG_HAS_DATAFLASH
 #include <dataflash.h>
common/cmd_nvedit.c +1 -0
index c53601c..99a21b2 100644
@@ -25,6 +25,7 @@
  */
 
 #include <common.h>
+#include <cli.h>
 #include <command.h>
 #include <environment.h>
 #include <search.h>
common/cmd_pci.c +1 -0
index d3e7c08..ddda207 100644
@@ -14,6 +14,7 @@
  */
 
 #include <common.h>
+#include <cli.h>
 #include <command.h>
 #include <asm/processor.h>
 #include <asm/io.h>
common/main.c +1 -0
index 2eea9c5..815759a 100644
@@ -12,6 +12,7 @@
 /* #define	DEBUG	*/
 
 #include <common.h>
+#include <cli.h>
 #include <command.h>
 #include <fdtdec.h>
 #include <cli_hush.h>
common/menu.c +1 -0
index ba393ad..88d4697 100644
@@ -5,6 +5,7 @@
  */
 
 #include <common.h>
+#include <cli.h>
 #include <malloc.h>
 #include <errno.h>
 #include <linux/list.h>
drivers/ddr/fsl/interactive.c +1 -0
index ebf3ed6..3b311b1 100644
@@ -12,6 +12,7 @@
  */
 
 #include <common.h>
+#include <cli.h>
 #include <linux/ctype.h>
 #include <asm/types.h>
 #include <asm/io.h>
include/cli.h new file +102 -0

102 lines not on this page · raw message

include/common.h +0 -4
index cbd3c9e..abe57c6 100644
@@ -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[];
Signature · 1 line
1.9.1.423.g4596e3a
#7 lore raw

PATCH 4/14 Split out simple parser and readline into separate files

In reply to Simon Glass

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

---

 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
common/Makefile +8 -1
index 12068ac..44967ea 100644
@@ -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
common/cli_readline.c new file +670 -0

670 lines not on this page · raw message

common/cli_simple.c new file +338 -0

338 lines not on this page · raw message

common/main.c +5 -1012

1,085 lines not on this page · raw message

include/cli.h +5 -0
index 0075bd4..b04539f 100644
@@ -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
Signature · 1 line
1.9.1.423.g4596e3a
#9 lore raw

PATCH 5/14 Add cli_ prefix to readline functions

In reply to Simon Glass

This makes it clear where the code resides.

---

 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(-)
board/ait/cam_enc_4xx/cam_enc_4xx.c +1 -1
index 7c26ba0..9aa1d7a 100644
@@ -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);
board/amcc/yucca/cmd_yucca.c +10 -10
index 3257c29..c1724bf 100644
@@ -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;
 
board/eltec/elppc/misc.c +7 -7
index 70d1f9a..2acf800 100644
@@ -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';
board/eltec/mhpc/mhpc.c +6 -6
index ff9e0ab..5781b2a 100644
@@ -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 *
board/hymod/hymod.c +1 -1
index ea49e26..55ffd67 100644
@@ -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 ();
board/hymod/input.c +2 -2
index 23d3f19..59ad6aa 100644
@@ -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);
common/cli_hush.c +2 -2
index 48cec88..c6bb30b 100644
@@ -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) {
common/cli_readline.c +4 -3
index cea0b7c..df446b8 100644
@@ -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
common/cli_simple.c +3 -3
index 0610615..3039cfc 100644
@@ -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)
common/cmd_bedbug.c +14 -14
index f1a70ef..bdcf712 100644
@@ -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    */
-
+
 
 /* ======================================================================
  * 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 */
-
+
 
 
 /* ======================================================================
@@ -66,7 +66,7 @@ void bedbug_init (void)
 
 	return;
 }				/* bedbug_init */
-
+
 
 
 /* ======================================================================
@@ -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]");
-
+
 /* ======================================================================
  * 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>");
-
+
 /* ======================================================================
  * 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.");
-
+
 /* ======================================================================
  * 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 */
-
+
 
 
 /* ======================================================================
@@ -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 */
-
+
 
 
 /* ======================================================================
@@ -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",
 	    "");
-
+
 /* ======================================================================
  * 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.",
 	    "");
-
+
 /* ======================================================================
  * 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.",
 	    "");
-
+
 /* ======================================================================
  * 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.",
 	    "");
-
+
 /* ======================================================================
  * Interpreter command to dump the registers.  Calls the CPU-specific
  * show registers routine.
common/cmd_dcr.c +1 -1
index c5bcb8b..4fddd80 100644
@@ -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
common/cmd_i2c.c +1 -1
index 8ccde68..7158b5a 100644
@@ -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
common/cmd_mem.c +1 -1
index 4b8738b..15a4b84 100644
@@ -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.
common/cmd_nvedit.c +2 -2
index 99a21b2..6cd538c 100644
@@ -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);
common/cmd_pci.c +1 -1
index ddda207..1ed55ce 100644
@@ -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.
common/menu.c +3 -2
index 88d4697..94afeb2 100644
@@ -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);
drivers/ddr/fsl/interactive.c +4 -3
index 3b311b1..88b3b4c 100644
@@ -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;
 
include/cli.h +4 -3
index b04539f..61f8aee 100644
@@ -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);
Signature · 1 line
1.9.1.423.g4596e3a
#11 lore raw

PATCH 6/14 Move autoboot code to autoboot.c

In reply to Simon Glass

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

---

 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
common/Makefile +5 -0
index 44967ea..b707e39 100644
@@ -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
common/autoboot.c new file +363 -0

363 lines not on this page · raw message

common/main.c +2 -364

395 lines not on this page · raw message

include/autoboot.h new file +23 -0
new file mode 100644
index 0000000..aaae4af
@@ -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, <[email protected]>
+ *
+ * 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
Signature · 1 line
1.9.1.423.g4596e3a
#13 lore raw

PATCH 7/14 Move command line API into cli.c

In reply to Simon Glass

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

---

 common/Makefile |   1 +
 common/cli.c    | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 common/main.c   |  93 -------------------------------------------------
 3 files changed, 107 insertions(+), 93 deletions(-)
 create mode 100644 common/cli.c
common/Makefile +1 -0
index b707e39..41b6ef5 100644
@@ -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
common/cli.c new file +106 -0

106 lines not on this page · raw message

common/main.c +0 -93

102 lines not on this page · raw message

Signature · 1 line
1.9.1.423.g4596e3a
#15 lore raw

PATCH 8/14 Move bootretry code into bootretry.c and clean up

In reply to Simon Glass

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.

---

 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
board/hymod/hymod.c +1 -0
index 55ffd67..f6990e9 100644
@@ -8,6 +8,7 @@
  */
 
 #include <common.h>
+#include <bootretry.h>
 #include <cli.h>
 #include <mpc8260.h>
 #include <mpc8260_irq.h>
board/hymod/input.c +1 -0
index 59ad6aa..2f857c0 100644
@@ -6,6 +6,7 @@
  */
 
 #include <common.h>
+#include <bootretry.h>
 #include <cli.h>
 
 int
common/Makefile +5 -0
index 41b6ef5..9fdb752 100644
@@ -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
common/autoboot.c +1 -0
index 6933e3f..5f8d9c3 100644
@@ -6,6 +6,7 @@
  */
 
 #include <common.h>
+#include <bootretry.h>
 #include <cli.h>
 #include <fdtdec.h>
 #include <menu.h>
common/bootretry.c new file +59 -0

59 lines not on this page · raw message

common/cli_hush.c +1 -0
index c6bb30b..2515b69 100644
@@ -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 */
common/cli_readline.c +5 -55

89 lines not on this page · raw message

common/cli_simple.c +1 -0
index 3039cfc..5b7e2ce 100644
@@ -10,6 +10,7 @@
  */
 
 #include <common.h>
+#include <bootretry.h>
 #include <cli.h>
 #include <linux/ctype.h>
 
common/cmd_i2c.c +1 -0
index 7158b5a..4fa1213 100644
@@ -66,6 +66,7 @@
  */
 
 #include <common.h>
+#include <bootretry.h>
 #include <cli.h>
 #include <command.h>
 #include <edid.h>
common/cmd_mem.c +1 -0
index 15a4b84..9bd7c0e 100644
@@ -12,6 +12,7 @@
  */
 
 #include <common.h>
+#include <bootretry.h>
 #include <cli.h>
 #include <command.h>
 #ifdef CONFIG_HAS_DATAFLASH
common/cmd_pci.c +1 -0
index 1ed55ce..180b35a 100644
@@ -14,6 +14,7 @@
  */
 
 #include <common.h>
+#include <bootretry.h>
 #include <cli.h>
 #include <command.h>
 #include <asm/processor.h>
include/bootretry.h new file +31 -0
new file mode 100644
index 0000000..025c29d
@@ -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
include/common.h +0 -2
index abe57c6..fbf3b14 100644
@@ -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 */
Signature · 1 line
1.9.1.423.g4596e3a
#17 lore raw

PATCH 9/14 Rename bootretry functions and remove #ifdefs

In reply to Simon Glass

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

---

 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(-)
board/hymod/hymod.c +1 -3
index f6990e9..0183f78 100644
@@ -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));
 
board/hymod/input.c +2 -6
index 2f857c0..a9035d3 100644
@@ -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: ");
 
common/autoboot.c +1 -5
index 5f8d9c3..9843898 100644
@@ -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) {
common/bootretry.c +2 -2
index 12653c0..2d82798 100644
@@ -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);
 }
common/cli_hush.c +1 -6
index 2515b69..990c080 100644
@@ -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);
common/cli_simple.c +1 -3
index 5b7e2ce..bba586e 100644
@@ -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 */
common/cmd_i2c.c +4 -9
index 4fa1213..d714658 100644
@@ -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
common/cmd_mem.c +4 -9
index 9bd7c0e..1febddb 100644
@@ -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
common/cmd_pci.c +3 -6
index 180b35a..a1ba42e 100644
@@ -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;
include/bootretry.h +33 -5
index 025c29d..2ecd7a4 100644
@@ -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
include/cli.h +0 -3
index 61f8aee..10dbc66 100644
@@ -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
Signature · 1 line
1.9.1.423.g4596e3a
#19 lore raw

PATCH 10/14 m68k: powerpc: Clean up do_mdm_init

In reply to Simon Glass

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.

---

 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(-)
arch/m68k/lib/board.c +0 -7
index e75b6a9..5a853ad 100644
@@ -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) {
arch/powerpc/lib/board.c +0 -8
index f86c6f3..8c0f817 100644
@@ -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. */
common/board_r.c +0 -14
index 8629a65..31f90ea 100644
@@ -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,
 };
 
common/main.c +4 -7
index 91a5b18..0cb3973 100644
@@ -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)
include/common.h +1 -0
index fbf3b14..3975e34 100644
@@ -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
Signature · 1 line
1.9.1.423.g4596e3a
#21 lore raw

PATCH 11/14 Simplify the main loop

In reply to Simon Glass

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.

---

 common/main.c | 47 ++++++++++++++++++++++++++---------------------
 1 file changed, 26 insertions(+), 21 deletions(-)
common/main.c +26 -21
index 0cb3973..d95e5fc 100644
@@ -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);
Signature · 1 line
1.9.1.423.g4596e3a
#23 lore raw

PATCH 12/14 main: Hide the hush/simple details inside cli.c

In reply to Simon Glass

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

---

 common/cli.c        | 22 ++++++++++++++++++++++
 common/cli_simple.c |  2 +-
 common/main.c       | 16 ++--------------
 include/cli.h       | 13 ++++++++++++-
 4 files changed, 37 insertions(+), 16 deletions(-)
common/cli.c +22 -0
index 9cf7ba1..4ac9b3f 100644
@@ -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
+}
common/cli_simple.c +1 -1
index bba586e..413c2eb 100644
@@ -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, };
 
common/main.c +2 -14
index d95e5fc..12f6ac4 100644
@@ -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*/
 }
include/cli.h +12 -1
index 10dbc66..5158976 100644
@@ -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
Signature · 1 line
1.9.1.423.g4596e3a
#25 lore raw

PATCH 13/14 main: Make the execution path a little clearer in main.c

In reply to Simon Glass

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.

---

 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(-)
common/autoboot.c +12 -69
index 9843898..dc24cae 100644
@@ -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
common/cli.c +66 -0

80 lines not on this page · raw message

common/main.c +8 -4
index 12f6ac4..b0d3ea4 100644
@@ -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();
 }
include/autoboot.h +26 -2
index aaae4af..3a9059a 100644
@@ -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
include/cli.h +33 -0
index 5158976..6994262 100644
@@ -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
  *
Signature · 1 line
1.9.1.423.g4596e3a
#27 lore raw

PATCH 14/14 main: Avoid unncessary strdup()/free()

In reply to Simon Glass

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

---

 common/main.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
common/main.c +2 -4
index b0d3ea4..ab1f708 100644
@@ -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 */
Signature · 1 line
1.9.1.423.g4596e3a
#29 lore raw

cover Split out code from the enormous main.c

In reply to Simon Glass

Hi,

Quoted 20 lines · Simon Glass

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.

Quoted 15 lines

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

#2 lore raw

PATCH 1/14 Remove unnecessary use of hush header file

In reply to Simon Glass
Quoted 4 lines · Simon Glass

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

Applied to u-boot/master, thanks!

Signature · 8 lines
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>
#4 lore raw

PATCH 2/14 Rename hush to cli_hush

In reply to Simon Glass
Quoted 3 lines · Simon Glass

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

Applied to u-boot/master, thanks!

Signature · 8 lines
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>
#6 lore raw

PATCH 3/14 move CLI prototypes to cli.h and add comments

In reply to Simon Glass
Quoted 4 lines · Simon Glass

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

Applied to u-boot/master, thanks!

Signature · 8 lines
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>
#8 lore raw

PATCH 4/14 Split out simple parser and readline into separate files

In reply to Simon Glass
Quoted 4 lines · Simon Glass

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

Applied to u-boot/master, thanks!

Signature · 8 lines
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>
#10 lore raw

PATCH 5/14 Add cli_ prefix to readline functions

In reply to Simon Glass
Quoted 3 lines · Simon Glass

This makes it clear where the code resides.

Applied to u-boot/master, thanks!

Signature · 8 lines
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>
#12 lore raw

PATCH 6/14 Move autoboot code to autoboot.c

In reply to Simon Glass
Quoted 4 lines · Simon Glass

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

Applied to u-boot/master, thanks!

Signature · 8 lines
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>
#14 lore raw

PATCH 7/14 Move command line API into cli.c

In reply to Simon Glass
Quoted 4 lines · Simon Glass

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

Applied to u-boot/master, thanks!

Signature · 8 lines
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>
#16 lore raw

PATCH 8/14 Move bootretry code into bootretry.c and clean up

In reply to Simon Glass
Quoted 4 lines · Simon Glass

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.

Applied to u-boot/master, thanks!

Signature · 8 lines
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>
#18 lore raw

PATCH 9/14 Rename bootretry functions and remove #ifdefs

In reply to Simon Glass
Quoted 4 lines · Simon Glass

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

Applied to u-boot/master, thanks!

Signature · 8 lines
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>
#20 lore raw

PATCH 10/14 m68k: powerpc: Clean up do_mdm_init

In reply to Simon Glass
Quoted 5 lines · Simon Glass

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.

Applied to u-boot/master, thanks!

Signature · 8 lines
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>
#22 lore raw

PATCH 11/14 Simplify the main loop

In reply to Simon Glass
Quoted 4 lines · Simon Glass

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.

Applied to u-boot/master, thanks!

Signature · 8 lines
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>
#24 lore raw

PATCH 12/14 main: Hide the hush/simple details inside cli.c

In reply to Simon Glass
Quoted 4 lines · Simon Glass

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

Applied to u-boot/master, thanks!

Signature · 8 lines
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>
#26 lore raw

PATCH 13/14 main: Make the execution path a little clearer in main.c

In reply to Simon Glass
Quoted 6 lines · Simon Glass

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.

Applied to u-boot/master, thanks!

Signature · 8 lines
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>
#28 lore raw

PATCH 14/14 main: Avoid unncessary strdup()/free()

In reply to Simon Glass
Quoted 4 lines · Simon Glass

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

Applied to u-boot/master, thanks!

Signature · 8 lines
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>