cli.c 5.0 KB

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