cli.c 5.7 KB

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