blob: 19d6f2caffd549e187c0cba063f75e1421bfe629 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00006 */
7
wdenka6c7ad22002-12-03 21:28:10 +00008/* #define DEBUG */
9
wdenkc6097192002-11-03 00:24:07 +000010#include <common.h>
Simon Glass66ded172014-04-10 20:01:28 -060011#include <autoboot.h>
Simon Glass18d66532014-04-10 20:01:25 -060012#include <cli.h>
wdenkc6097192002-11-03 00:24:07 +000013#include <command.h>
Simon Glasseca86fa2014-04-10 20:01:24 -060014#include <cli_hush.h>
Simon Glassfbcdf322013-05-15 06:23:59 +000015#include <malloc.h>
Simon Glassfbcdf322013-05-15 06:23:59 +000016#include <version.h>
wdenkbdccc4f2003-08-05 17:43:17 +000017
Heiko Schocherfad63402007-07-13 09:54:17 +020018/*
19 * Board-specific Platform code can reimplement show_boot_progress () if needed
20 */
21void inline __show_boot_progress (int val) {}
Emil Medve5e2c08c2009-05-12 13:48:32 -050022void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
Heiko Schocherfad63402007-07-13 09:54:17 +020023
wdenkc6097192002-11-03 00:24:07 +000024#ifdef CONFIG_MODEM_SUPPORT
25int do_mdm_init = 0;
26extern void mdm_init(void); /* defined in board.c */
27#endif
28
Simon Glassbc2b4c22013-05-15 06:23:56 +000029void main_loop(void)
30{
Simon Glassbc2b4c22013-05-15 06:23:56 +000031#ifdef CONFIG_PREBOOT
32 char *p;
33#endif
34
35 bootstage_mark_name(BOOTSTAGE_ID_MAIN_LOOP, "main_loop");
36
Simon Glass0f605c12014-03-22 17:14:51 -060037#ifndef CONFIG_SYS_GENERIC_BOARD
38 puts("Warning: Your board does not use generic board. Please read\n");
39 puts("doc/README.generic-board and take action. Boards not\n");
40 puts("upgraded by the late 2014 may break or be removed.\n");
41#endif
42
Simon Glassbc2b4c22013-05-15 06:23:56 +000043#ifdef CONFIG_MODEM_SUPPORT
44 debug("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
45 if (do_mdm_init) {
46 char *str = strdup(getenv("mdm_cmd"));
47 setenv("preboot", str); /* set or delete definition */
48 if (str != NULL)
49 free(str);
50 mdm_init(); /* wait for modem connection */
51 }
52#endif /* CONFIG_MODEM_SUPPORT */
53
54#ifdef CONFIG_VERSION_VARIABLE
55 {
56 setenv("ver", version_string); /* set version variable */
57 }
58#endif /* CONFIG_VERSION_VARIABLE */
59
60#ifdef CONFIG_SYS_HUSH_PARSER
61 u_boot_hush_start();
62#endif
63
64#if defined(CONFIG_HUSH_INIT_VAR)
65 hush_init_var();
66#endif
67
68#ifdef CONFIG_PREBOOT
69 p = getenv("preboot");
70 if (p != NULL) {
71# ifdef CONFIG_AUTOBOOT_KEYED
72 int prev = disable_ctrlc(1); /* disable Control C checking */
73# endif
74
75 run_command_list(p, -1, 0);
76
77# ifdef CONFIG_AUTOBOOT_KEYED
78 disable_ctrlc(prev); /* restore Control C checking */
79# endif
80 }
81#endif /* CONFIG_PREBOOT */
82
83#if defined(CONFIG_UPDATE_TFTP)
84 update_tftp(0UL);
85#endif /* CONFIG_UPDATE_TFTP */
86
Simon Glass66ded172014-04-10 20:01:28 -060087 bootdelay_process();
wdenkc6097192002-11-03 00:24:07 +000088 /*
89 * Main Loop for Monitor Command Processing
90 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020091#ifdef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +000092 parse_file_outer();
93 /* This point is never reached */
94 for (;;);
95#else
Simon Glass6493ccc2014-04-10 20:01:26 -060096 cli_loop();
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020097#endif /*CONFIG_SYS_HUSH_PARSER*/
wdenkc6097192002-11-03 00:24:07 +000098}
99
wdenkc6097192002-11-03 00:24:07 +0000100/****************************************************************************/
101
102/*
Simon Glass53071532012-02-14 19:59:21 +0000103 * Run a command using the selected parser.
104 *
105 * @param cmd Command to run
106 * @param flag Execution flags (CMD_FLAG_...)
107 * @return 0 on success, or != 0 on error.
108 */
109int run_command(const char *cmd, int flag)
110{
111#ifndef CONFIG_SYS_HUSH_PARSER
112 /*
Simon Glass6493ccc2014-04-10 20:01:26 -0600113 * cli_run_command can return 0 or 1 for success, so clean up
Simon Glass53071532012-02-14 19:59:21 +0000114 * its result.
115 */
Simon Glass6493ccc2014-04-10 20:01:26 -0600116 if (cli_simple_run_command(cmd, flag) == -1)
Simon Glass53071532012-02-14 19:59:21 +0000117 return 1;
118
119 return 0;
120#else
121 return parse_string_outer(cmd,
122 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
123#endif
124}
125
Simon Glassd51004a2012-03-30 21:30:55 +0000126int run_command_list(const char *cmd, int len, int flag)
127{
128 int need_buff = 1;
129 char *buff = (char *)cmd; /* cast away const */
130 int rcode = 0;
131
132 if (len == -1) {
133 len = strlen(cmd);
134#ifdef CONFIG_SYS_HUSH_PARSER
135 /* hush will never change our string */
136 need_buff = 0;
137#else
138 /* the built-in parser will change our string if it sees \n */
139 need_buff = strchr(cmd, '\n') != NULL;
140#endif
141 }
142 if (need_buff) {
143 buff = malloc(len + 1);
144 if (!buff)
145 return 1;
146 memcpy(buff, cmd, len);
147 buff[len] = '\0';
148 }
149#ifdef CONFIG_SYS_HUSH_PARSER
150 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
151#else
152 /*
153 * This function will overwrite any \n it sees with a \0, which
154 * is why it can't work with a const char *. Here we are making
155 * using of internal knowledge of this function, to avoid always
156 * doing a malloc() which is actually required only in a case that
157 * is pretty rare.
158 */
Simon Glass6493ccc2014-04-10 20:01:26 -0600159 rcode = cli_simple_run_command_list(buff, flag);
Simon Glassd51004a2012-03-30 21:30:55 +0000160 if (need_buff)
161 free(buff);
162#endif
163
164 return rcode;
165}
166
wdenkc6097192002-11-03 00:24:07 +0000167/****************************************************************************/
168
Jon Loeligerc3517f92007-07-08 18:10:08 -0500169#if defined(CONFIG_CMD_RUN)
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200170int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
wdenkc6097192002-11-03 00:24:07 +0000171{
172 int i;
wdenkc6097192002-11-03 00:24:07 +0000173
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200174 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000175 return CMD_RET_USAGE;
wdenkc6097192002-11-03 00:24:07 +0000176
177 for (i=1; i<argc; ++i) {
wdenk3e386912003-04-05 00:53:31 +0000178 char *arg;
179
180 if ((arg = getenv (argv[i])) == NULL) {
181 printf ("## Error: \"%s\" not defined\n", argv[i]);
182 return 1;
183 }
Jason Hobbsc8a20792011-08-31 05:37:24 +0000184
Simon Glass1992dbf2013-10-25 23:01:32 -0600185 if (run_command_list(arg, -1, flag) != 0)
wdenk3e386912003-04-05 00:53:31 +0000186 return 1;
wdenkc6097192002-11-03 00:24:07 +0000187 }
wdenk3e386912003-04-05 00:53:31 +0000188 return 0;
wdenkc6097192002-11-03 00:24:07 +0000189}
Jon Loeliger90253172007-07-10 11:02:44 -0500190#endif