source.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2001
  4. * Kyle Harris, kharris@nexus-tech.net
  5. */
  6. /*
  7. * The "source" command allows to define "script images", i. e. files
  8. * that contain command sequences that can be executed by the command
  9. * interpreter. It returns the exit status of the last command
  10. * executed from the script. This is very similar to running a shell
  11. * script in a UNIX shell, hence the name for the command.
  12. */
  13. /* #define DEBUG */
  14. #include <common.h>
  15. #include <command.h>
  16. #include <env.h>
  17. #include <image.h>
  18. #include <log.h>
  19. #include <malloc.h>
  20. #include <mapmem.h>
  21. #include <asm/byteorder.h>
  22. #include <asm/io.h>
  23. #if defined(CONFIG_FIT)
  24. /**
  25. * get_default_image() - Return default property from /images
  26. *
  27. * Return: Pointer to value of default property (or NULL)
  28. */
  29. static const char *get_default_image(const void *fit)
  30. {
  31. int images_noffset;
  32. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  33. if (images_noffset < 0)
  34. return NULL;
  35. return fdt_getprop(fit, images_noffset, FIT_DEFAULT_PROP, NULL);
  36. }
  37. #endif
  38. int image_source_script(ulong addr, const char *fit_uname)
  39. {
  40. ulong len;
  41. #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
  42. const image_header_t *hdr;
  43. #endif
  44. u32 *data;
  45. int verify;
  46. void *buf;
  47. #if defined(CONFIG_FIT)
  48. const void* fit_hdr;
  49. int noffset;
  50. const void *fit_data;
  51. size_t fit_len;
  52. #endif
  53. verify = env_get_yesno("verify");
  54. buf = map_sysmem(addr, 0);
  55. switch (genimg_get_format(buf)) {
  56. #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
  57. case IMAGE_FORMAT_LEGACY:
  58. hdr = buf;
  59. if (!image_check_magic (hdr)) {
  60. puts ("Bad magic number\n");
  61. return 1;
  62. }
  63. if (!image_check_hcrc (hdr)) {
  64. puts ("Bad header crc\n");
  65. return 1;
  66. }
  67. if (verify) {
  68. if (!image_check_dcrc (hdr)) {
  69. puts ("Bad data crc\n");
  70. return 1;
  71. }
  72. }
  73. if (!image_check_type (hdr, IH_TYPE_SCRIPT)) {
  74. puts ("Bad image type\n");
  75. return 1;
  76. }
  77. /* get length of script */
  78. data = (u32 *)image_get_data (hdr);
  79. if ((len = uimage_to_cpu (*data)) == 0) {
  80. puts ("Empty Script\n");
  81. return 1;
  82. }
  83. /*
  84. * scripts are just multi-image files with one component, seek
  85. * past the zero-terminated sequence of image lengths to get
  86. * to the actual image data
  87. */
  88. while (*data++);
  89. break;
  90. #endif
  91. #if defined(CONFIG_FIT)
  92. case IMAGE_FORMAT_FIT:
  93. fit_hdr = buf;
  94. if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
  95. puts ("Bad FIT image format\n");
  96. return 1;
  97. }
  98. if (!fit_uname)
  99. fit_uname = get_default_image(fit_hdr);
  100. if (!fit_uname) {
  101. puts("No FIT subimage unit name\n");
  102. return 1;
  103. }
  104. /* get script component image node offset */
  105. noffset = fit_image_get_node (fit_hdr, fit_uname);
  106. if (noffset < 0) {
  107. printf ("Can't find '%s' FIT subimage\n", fit_uname);
  108. return 1;
  109. }
  110. if (!fit_image_check_type (fit_hdr, noffset, IH_TYPE_SCRIPT)) {
  111. puts ("Not a image image\n");
  112. return 1;
  113. }
  114. /* verify integrity */
  115. if (verify) {
  116. if (!fit_image_verify(fit_hdr, noffset)) {
  117. puts ("Bad Data Hash\n");
  118. return 1;
  119. }
  120. }
  121. /* get script subimage data address and length */
  122. if (fit_image_get_data (fit_hdr, noffset, &fit_data, &fit_len)) {
  123. puts ("Could not find script subimage data\n");
  124. return 1;
  125. }
  126. data = (u32 *)fit_data;
  127. len = (ulong)fit_len;
  128. break;
  129. #endif
  130. default:
  131. puts ("Wrong image format for \"source\" command\n");
  132. return 1;
  133. }
  134. debug("** Script length: %ld\n", len);
  135. return run_command_list((char *)data, len, 0);
  136. }
  137. /**************************************************/
  138. #if defined(CONFIG_CMD_SOURCE)
  139. static int do_source(struct cmd_tbl *cmdtp, int flag, int argc,
  140. char *const argv[])
  141. {
  142. ulong addr;
  143. int rcode;
  144. const char *fit_uname = NULL;
  145. /* Find script image */
  146. if (argc < 2) {
  147. addr = CONFIG_SYS_LOAD_ADDR;
  148. debug("* source: default load address = 0x%08lx\n", addr);
  149. #if defined(CONFIG_FIT)
  150. } else if (fit_parse_subimage(argv[1], image_load_addr, &addr,
  151. &fit_uname)) {
  152. debug("* source: subimage '%s' from FIT image at 0x%08lx\n",
  153. fit_uname, addr);
  154. #endif
  155. } else {
  156. addr = hextoul(argv[1], NULL);
  157. debug("* source: cmdline image address = 0x%08lx\n", addr);
  158. }
  159. printf ("## Executing script at %08lx\n", addr);
  160. rcode = image_source_script(addr, fit_uname);
  161. return rcode;
  162. }
  163. #ifdef CONFIG_SYS_LONGHELP
  164. static char source_help_text[] =
  165. "[addr]\n"
  166. "\t- run script starting at addr\n"
  167. "\t- A valid image header must be present"
  168. #if defined(CONFIG_FIT)
  169. "\n"
  170. "For FIT format uImage addr must include subimage\n"
  171. "unit name in the form of addr:<subimg_uname>"
  172. #endif
  173. "";
  174. #endif
  175. U_BOOT_CMD(
  176. source, 2, 0, do_source,
  177. "run script from memory", source_help_text
  178. );
  179. #endif