cli.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2000
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. *
  6. * Add to readline cmdline-editing by
  7. * (C) Copyright 2005
  8. * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
  9. */
  10. #include <common.h>
  11. #include <bootstage.h>
  12. #include <cli.h>
  13. #include <cli_hush.h>
  14. #include <command.h>
  15. #include <console.h>
  16. #include <env.h>
  17. #include <fdtdec.h>
  18. #include <hang.h>
  19. #include <malloc.h>
  20. #include <asm/global_data.h>
  21. DECLARE_GLOBAL_DATA_PTR;
  22. #ifdef CONFIG_CMDLINE
  23. /*
  24. * Run a command using the selected parser.
  25. *
  26. * @param cmd Command to run
  27. * @param flag Execution flags (CMD_FLAG_...)
  28. * @return 0 on success, or != 0 on error.
  29. */
  30. int run_command(const char *cmd, int flag)
  31. {
  32. #if !CONFIG_IS_ENABLED(HUSH_PARSER)
  33. /*
  34. * cli_run_command can return 0 or 1 for success, so clean up
  35. * its result.
  36. */
  37. if (cli_simple_run_command(cmd, flag) == -1)
  38. return 1;
  39. return 0;
  40. #else
  41. int hush_flags = FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP;
  42. if (flag & CMD_FLAG_ENV)
  43. hush_flags |= FLAG_CONT_ON_NEWLINE;
  44. return parse_string_outer(cmd, hush_flags);
  45. #endif
  46. }
  47. /*
  48. * Run a command using the selected parser, and check if it is repeatable.
  49. *
  50. * @param cmd Command to run
  51. * @param flag Execution flags (CMD_FLAG_...)
  52. * @return 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
  53. */
  54. int run_command_repeatable(const char *cmd, int flag)
  55. {
  56. #ifndef CONFIG_HUSH_PARSER
  57. return cli_simple_run_command(cmd, flag);
  58. #else
  59. /*
  60. * parse_string_outer() returns 1 for failure, so clean up
  61. * its result.
  62. */
  63. if (parse_string_outer(cmd,
  64. FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP))
  65. return -1;
  66. return 0;
  67. #endif
  68. }
  69. #else
  70. __weak int board_run_command(const char *cmdline)
  71. {
  72. printf("## Commands are disabled. Please enable CONFIG_CMDLINE.\n");
  73. return 1;
  74. }
  75. #endif /* CONFIG_CMDLINE */
  76. int run_command_list(const char *cmd, int len, int flag)
  77. {
  78. int need_buff = 1;
  79. char *buff = (char *)cmd; /* cast away const */
  80. int rcode = 0;
  81. if (len == -1) {
  82. len = strlen(cmd);
  83. #ifdef CONFIG_HUSH_PARSER
  84. /* hush will never change our string */
  85. need_buff = 0;
  86. #else
  87. /* the built-in parser will change our string if it sees \n */
  88. need_buff = strchr(cmd, '\n') != NULL;
  89. #endif
  90. }
  91. if (need_buff) {
  92. buff = malloc(len + 1);
  93. if (!buff)
  94. return 1;
  95. memcpy(buff, cmd, len);
  96. buff[len] = '\0';
  97. }
  98. #ifdef CONFIG_HUSH_PARSER
  99. rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
  100. #else
  101. /*
  102. * This function will overwrite any \n it sees with a \0, which
  103. * is why it can't work with a const char *. Here we are making
  104. * using of internal knowledge of this function, to avoid always
  105. * doing a malloc() which is actually required only in a case that
  106. * is pretty rare.
  107. */
  108. #ifdef CONFIG_CMDLINE
  109. rcode = cli_simple_run_command_list(buff, flag);
  110. #else
  111. rcode = board_run_command(buff);
  112. #endif
  113. #endif
  114. if (need_buff)
  115. free(buff);
  116. return rcode;
  117. }
  118. /****************************************************************************/
  119. #if defined(CONFIG_CMD_RUN)
  120. int do_run(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  121. {
  122. int i;
  123. if (argc < 2)
  124. return CMD_RET_USAGE;
  125. for (i = 1; i < argc; ++i) {
  126. char *arg;
  127. arg = env_get(argv[i]);
  128. if (arg == NULL) {
  129. printf("## Error: \"%s\" not defined\n", argv[i]);
  130. return 1;
  131. }
  132. if (run_command(arg, flag | CMD_FLAG_ENV) != 0)
  133. return 1;
  134. }
  135. return 0;
  136. }
  137. #endif
  138. #if CONFIG_IS_ENABLED(OF_CONTROL)
  139. bool cli_process_fdt(const char **cmdp)
  140. {
  141. /* Allow the fdt to override the boot command */
  142. char *env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
  143. if (env)
  144. *cmdp = env;
  145. /*
  146. * If the bootsecure option was chosen, use secure_boot_cmd().
  147. * Always use 'env' in this case, since bootsecure requres that the
  148. * bootcmd was specified in the FDT too.
  149. */
  150. return fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0) != 0;
  151. }
  152. /*
  153. * Runs the given boot command securely. Specifically:
  154. * - Doesn't run the command with the shell (run_command or parse_string_outer),
  155. * since that's a lot of code surface that an attacker might exploit.
  156. * Because of this, we don't do any argument parsing--the secure boot command
  157. * has to be a full-fledged u-boot command.
  158. * - Doesn't check for keypresses before booting, since that could be a
  159. * security hole; also disables Ctrl-C.
  160. * - Doesn't allow the command to return.
  161. *
  162. * Upon any failures, this function will drop into an infinite loop after
  163. * printing the error message to console.
  164. */
  165. void cli_secure_boot_cmd(const char *cmd)
  166. {
  167. #ifdef CONFIG_CMDLINE
  168. struct cmd_tbl *cmdtp;
  169. #endif
  170. int rc;
  171. if (!cmd) {
  172. printf("## Error: Secure boot command not specified\n");
  173. goto err;
  174. }
  175. /* Disable Ctrl-C just in case some command is used that checks it. */
  176. disable_ctrlc(1);
  177. /* Find the command directly. */
  178. #ifdef CONFIG_CMDLINE
  179. cmdtp = find_cmd(cmd);
  180. if (!cmdtp) {
  181. printf("## Error: \"%s\" not defined\n", cmd);
  182. goto err;
  183. }
  184. /* Run the command, forcing no flags and faking argc and argv. */
  185. rc = (cmdtp->cmd)(cmdtp, 0, 1, (char **)&cmd);
  186. #else
  187. rc = board_run_command(cmd);
  188. #endif
  189. /* Shouldn't ever return from boot command. */
  190. printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
  191. err:
  192. /*
  193. * Not a whole lot to do here. Rebooting won't help much, since we'll
  194. * just end up right back here. Just loop.
  195. */
  196. hang();
  197. }
  198. #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
  199. void cli_loop(void)
  200. {
  201. bootstage_mark(BOOTSTAGE_ID_ENTER_CLI_LOOP);
  202. #ifdef CONFIG_HUSH_PARSER
  203. parse_file_outer();
  204. /* This point is never reached */
  205. for (;;);
  206. #elif defined(CONFIG_CMDLINE)
  207. cli_simple_loop();
  208. #else
  209. printf("## U-Boot command line is disabled. Please enable CONFIG_CMDLINE\n");
  210. #endif /*CONFIG_HUSH_PARSER*/
  211. }
  212. void cli_init(void)
  213. {
  214. #ifdef CONFIG_HUSH_PARSER
  215. u_boot_hush_start();
  216. #endif
  217. #if defined(CONFIG_HUSH_INIT_VAR)
  218. hush_init_var();
  219. #endif
  220. }