cmd_source.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 <asm/byteorder.h>
  20. #include <asm/io.h>
  21. #if defined(CONFIG_8xx)
  22. #include <mpc8xx.h>
  23. #endif
  24. int
  25. source (ulong addr, const char *fit_uname)
  26. {
  27. ulong len;
  28. const image_header_t *hdr;
  29. ulong *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. 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 = (ulong *)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. #if defined(CONFIG_FIT)
  75. case IMAGE_FORMAT_FIT:
  76. if (fit_uname == NULL) {
  77. puts ("No FIT subimage unit name\n");
  78. return 1;
  79. }
  80. fit_hdr = buf;
  81. if (!fit_check_format (fit_hdr)) {
  82. puts ("Bad FIT image format\n");
  83. return 1;
  84. }
  85. /* get script component image node offset */
  86. noffset = fit_image_get_node (fit_hdr, fit_uname);
  87. if (noffset < 0) {
  88. printf ("Can't find '%s' FIT subimage\n", fit_uname);
  89. return 1;
  90. }
  91. if (!fit_image_check_type (fit_hdr, noffset, IH_TYPE_SCRIPT)) {
  92. puts ("Not a image image\n");
  93. return 1;
  94. }
  95. /* verify integrity */
  96. if (verify) {
  97. if (!fit_image_verify(fit_hdr, noffset)) {
  98. puts ("Bad Data Hash\n");
  99. return 1;
  100. }
  101. }
  102. /* get script subimage data address and length */
  103. if (fit_image_get_data (fit_hdr, noffset, &fit_data, &fit_len)) {
  104. puts ("Could not find script subimage data\n");
  105. return 1;
  106. }
  107. data = (ulong *)fit_data;
  108. len = (ulong)fit_len;
  109. break;
  110. #endif
  111. default:
  112. puts ("Wrong image format for \"source\" command\n");
  113. return 1;
  114. }
  115. debug ("** Script length: %ld\n", len);
  116. return run_command_list((char *)data, len, 0);
  117. }
  118. /**************************************************/
  119. #if defined(CONFIG_CMD_SOURCE)
  120. int
  121. 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