cli.c 5.4 KB

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