cli.c 5.1 KB

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