wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
wdenk | a6c7ad2 | 2002-12-03 21:28:10 +0000 | [diff] [blame] | 8 | /* #define DEBUG */ |
| 9 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 10 | #include <common.h> |
Simon Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame^] | 11 | #include <autoboot.h> |
Simon Glass | 18d6653 | 2014-04-10 20:01:25 -0600 | [diff] [blame] | 12 | #include <cli.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 13 | #include <command.h> |
Simon Glass | eca86fa | 2014-04-10 20:01:24 -0600 | [diff] [blame] | 14 | #include <cli_hush.h> |
Simon Glass | fbcdf32 | 2013-05-15 06:23:59 +0000 | [diff] [blame] | 15 | #include <malloc.h> |
Simon Glass | fbcdf32 | 2013-05-15 06:23:59 +0000 | [diff] [blame] | 16 | #include <version.h> |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 17 | |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 18 | /* |
| 19 | * Board-specific Platform code can reimplement show_boot_progress () if needed |
| 20 | */ |
| 21 | void inline __show_boot_progress (int val) {} |
Emil Medve | 5e2c08c | 2009-05-12 13:48:32 -0500 | [diff] [blame] | 22 | void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress"))); |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 23 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 24 | #ifdef CONFIG_MODEM_SUPPORT |
| 25 | int do_mdm_init = 0; |
| 26 | extern void mdm_init(void); /* defined in board.c */ |
| 27 | #endif |
| 28 | |
Simon Glass | bc2b4c2 | 2013-05-15 06:23:56 +0000 | [diff] [blame] | 29 | void main_loop(void) |
| 30 | { |
Simon Glass | bc2b4c2 | 2013-05-15 06:23:56 +0000 | [diff] [blame] | 31 | #ifdef CONFIG_PREBOOT |
| 32 | char *p; |
| 33 | #endif |
| 34 | |
| 35 | bootstage_mark_name(BOOTSTAGE_ID_MAIN_LOOP, "main_loop"); |
| 36 | |
Simon Glass | 0f605c1 | 2014-03-22 17:14:51 -0600 | [diff] [blame] | 37 | #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 Glass | bc2b4c2 | 2013-05-15 06:23:56 +0000 | [diff] [blame] | 43 | #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 Glass | 66ded17 | 2014-04-10 20:01:28 -0600 | [diff] [blame^] | 87 | bootdelay_process(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 88 | /* |
| 89 | * Main Loop for Monitor Command Processing |
| 90 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 91 | #ifdef CONFIG_SYS_HUSH_PARSER |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 92 | parse_file_outer(); |
| 93 | /* This point is never reached */ |
| 94 | for (;;); |
| 95 | #else |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 96 | cli_loop(); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 97 | #endif /*CONFIG_SYS_HUSH_PARSER*/ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 98 | } |
| 99 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 100 | /****************************************************************************/ |
| 101 | |
| 102 | /* |
Simon Glass | 5307153 | 2012-02-14 19:59:21 +0000 | [diff] [blame] | 103 | * 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 | */ |
| 109 | int run_command(const char *cmd, int flag) |
| 110 | { |
| 111 | #ifndef CONFIG_SYS_HUSH_PARSER |
| 112 | /* |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 113 | * cli_run_command can return 0 or 1 for success, so clean up |
Simon Glass | 5307153 | 2012-02-14 19:59:21 +0000 | [diff] [blame] | 114 | * its result. |
| 115 | */ |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 116 | if (cli_simple_run_command(cmd, flag) == -1) |
Simon Glass | 5307153 | 2012-02-14 19:59:21 +0000 | [diff] [blame] | 117 | 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 Glass | d51004a | 2012-03-30 21:30:55 +0000 | [diff] [blame] | 126 | int 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 Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 159 | rcode = cli_simple_run_command_list(buff, flag); |
Simon Glass | d51004a | 2012-03-30 21:30:55 +0000 | [diff] [blame] | 160 | if (need_buff) |
| 161 | free(buff); |
| 162 | #endif |
| 163 | |
| 164 | return rcode; |
| 165 | } |
| 166 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 167 | /****************************************************************************/ |
| 168 | |
Jon Loeliger | c3517f9 | 2007-07-08 18:10:08 -0500 | [diff] [blame] | 169 | #if defined(CONFIG_CMD_RUN) |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 170 | int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[]) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 171 | { |
| 172 | int i; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 173 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 174 | if (argc < 2) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 175 | return CMD_RET_USAGE; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 176 | |
| 177 | for (i=1; i<argc; ++i) { |
wdenk | 3e38691 | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 178 | char *arg; |
| 179 | |
| 180 | if ((arg = getenv (argv[i])) == NULL) { |
| 181 | printf ("## Error: \"%s\" not defined\n", argv[i]); |
| 182 | return 1; |
| 183 | } |
Jason Hobbs | c8a2079 | 2011-08-31 05:37:24 +0000 | [diff] [blame] | 184 | |
Simon Glass | 1992dbf | 2013-10-25 23:01:32 -0600 | [diff] [blame] | 185 | if (run_command_list(arg, -1, flag) != 0) |
wdenk | 3e38691 | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 186 | return 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 187 | } |
wdenk | 3e38691 | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 188 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 189 | } |
Jon Loeliger | 9025317 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 190 | #endif |