cli.c 5.5 KB

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