cli.c 5.4 KB

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