source.c 4.5 KB

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