source.c 4.0 KB

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