cli.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. /*
  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_SYS_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_SYS_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. int run_command_list(const char *cmd, int len, int flag)
  65. {
  66. int need_buff = 1;
  67. char *buff = (char *)cmd; /* cast away const */
  68. int rcode = 0;
  69. if (len == -1) {
  70. len = strlen(cmd);
  71. #ifdef CONFIG_SYS_HUSH_PARSER
  72. /* hush will never change our string */
  73. need_buff = 0;
  74. #else
  75. /* the built-in parser will change our string if it sees \n */
  76. need_buff = strchr(cmd, '\n') != NULL;
  77. #endif
  78. }
  79. if (need_buff) {
  80. buff = malloc(len + 1);
  81. if (!buff)
  82. return 1;
  83. memcpy(buff, cmd, len);
  84. buff[len] = '\0';
  85. }
  86. #ifdef CONFIG_SYS_HUSH_PARSER
  87. rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
  88. #else
  89. /*
  90. * This function will overwrite any \n it sees with a \0, which
  91. * is why it can't work with a const char *. Here we are making
  92. * using of internal knowledge of this function, to avoid always
  93. * doing a malloc() which is actually required only in a case that
  94. * is pretty rare.
  95. */
  96. rcode = cli_simple_run_command_list(buff, flag);
  97. #endif
  98. if (need_buff)
  99. free(buff);
  100. return rcode;
  101. }
  102. /****************************************************************************/
  103. #if defined(CONFIG_CMD_RUN)
  104. int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  105. {
  106. int i;
  107. if (argc < 2)
  108. return CMD_RET_USAGE;
  109. for (i = 1; i < argc; ++i) {
  110. char *arg;
  111. arg = getenv(argv[i]);
  112. if (arg == NULL) {
  113. printf("## Error: \"%s\" not defined\n", argv[i]);
  114. return 1;
  115. }
  116. if (run_command(arg, flag | CMD_FLAG_ENV) != 0)
  117. return 1;
  118. }
  119. return 0;
  120. }
  121. #endif
  122. #if CONFIG_IS_ENABLED(OF_CONTROL)
  123. bool cli_process_fdt(const char **cmdp)
  124. {
  125. /* Allow the fdt to override the boot command */
  126. char *env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
  127. if (env)
  128. *cmdp = env;
  129. /*
  130. * If the bootsecure option was chosen, use secure_boot_cmd().
  131. * Always use 'env' in this case, since bootsecure requres that the
  132. * bootcmd was specified in the FDT too.
  133. */
  134. return fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0) != 0;
  135. }
  136. /*
  137. * Runs the given boot command securely. Specifically:
  138. * - Doesn't run the command with the shell (run_command or parse_string_outer),
  139. * since that's a lot of code surface that an attacker might exploit.
  140. * Because of this, we don't do any argument parsing--the secure boot command
  141. * has to be a full-fledged u-boot command.
  142. * - Doesn't check for keypresses before booting, since that could be a
  143. * security hole; also disables Ctrl-C.
  144. * - Doesn't allow the command to return.
  145. *
  146. * Upon any failures, this function will drop into an infinite loop after
  147. * printing the error message to console.
  148. */
  149. void cli_secure_boot_cmd(const char *cmd)
  150. {
  151. cmd_tbl_t *cmdtp;
  152. int rc;
  153. if (!cmd) {
  154. printf("## Error: Secure boot command not specified\n");
  155. goto err;
  156. }
  157. /* Disable Ctrl-C just in case some command is used that checks it. */
  158. disable_ctrlc(1);
  159. /* Find the command directly. */
  160. cmdtp = find_cmd(cmd);
  161. if (!cmdtp) {
  162. printf("## Error: \"%s\" not defined\n", cmd);
  163. goto err;
  164. }
  165. /* Run the command, forcing no flags and faking argc and argv. */
  166. rc = (cmdtp->cmd)(cmdtp, 0, 1, (char **)&cmd);
  167. /* Shouldn't ever return from boot command. */
  168. printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
  169. err:
  170. /*
  171. * Not a whole lot to do here. Rebooting won't help much, since we'll
  172. * just end up right back here. Just loop.
  173. */
  174. hang();
  175. }
  176. #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
  177. void cli_loop(void)
  178. {
  179. #ifdef CONFIG_SYS_HUSH_PARSER
  180. parse_file_outer();
  181. /* This point is never reached */
  182. for (;;);
  183. #else
  184. cli_simple_loop();
  185. #endif /*CONFIG_SYS_HUSH_PARSER*/
  186. }
  187. void cli_init(void)
  188. {
  189. #ifdef CONFIG_SYS_HUSH_PARSER
  190. u_boot_hush_start();
  191. #endif
  192. #if defined(CONFIG_HUSH_INIT_VAR)
  193. hush_init_var();
  194. #endif
  195. }