cli.c 5.7 KB

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