cli.c 4.4 KB

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