source.c 4.0 KB

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