cli.c 6.4 KB

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