spl_tool.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #define _GNU_SOURCE
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <getopt.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <stdint.h>
  10. #include <string.h>
  11. #include <stdbool.h>
  12. #include <unistd.h>
  13. #include <endian.h>
  14. #include <errno.h>
  15. #include <limits.h>
  16. #define NOSIZE ((size_t)-1)
  17. extern uint32_t crc32(uint32_t iv, uint32_t sv, const void *data, size_t n);
  18. extern uint32_t crc32_final(uint32_t iv);
  19. /* all uint32_t ends up little endian in output header */
  20. struct __attribute__((__packed__)) ubootsplhdr {
  21. uint32_t sofs; /* offset of spl header: 64+256+256 = 0x240 */
  22. uint32_t bofs; /* SBL_BAK_OFFSET: Offset of backup SBL from Flash info start (from input_sbl_normal.cfg) */
  23. uint8_t zro2[636];
  24. uint32_t vers; /* version: shall be 0x01010101
  25. * (from https://doc-en.rvspace.org/VisionFive2/SWTRM/VisionFive2_SW_TRM/create_spl.html) */
  26. uint32_t fsiz; /* u-boot-spl.bin size in bytes */
  27. uint32_t res1; /* Offset from HDR to SPL_IMAGE, 0x400 (00 04 00 00) currently */
  28. uint32_t crcs; /* CRC32 of u-boot-spl.bin */
  29. uint8_t zro3[364];
  30. };
  31. struct hdr_conf_t {
  32. const char name[PATH_MAX];
  33. uint32_t vers;
  34. uint32_t bofs;
  35. bool creat_hdr_flag;
  36. bool fixed_img_hdr_flag;
  37. };
  38. static struct ubootsplhdr ubsplhdr;
  39. static struct ubootsplhdr imghdr;
  40. static struct hdr_conf_t g_hdr_conf;
  41. static char ubootspl[181072-sizeof(struct ubootsplhdr)+1];
  42. static char outpath[PATH_MAX];
  43. #define DEFVERSID 0x01010101
  44. #define DEFBACKUP 0x200000U
  45. #define CRCFAILED 0x5A5A5A5A
  46. static void xerror(int errnoval, const char *s)
  47. {
  48. if (errnoval) perror(s);
  49. else fprintf(stderr, "%s\n", s);
  50. exit(2);
  51. }
  52. static void usage(void)
  53. {
  54. const char help[] = {
  55. "\n StarFive spl tool\n\n"
  56. "usage:\n"
  57. "-c, --creat-splhdr creat spl hdr\n"
  58. "-i, --fix-imghdr fixed img hdr for emmc boot.\n"
  59. "-a, --spl-bak-addr set backup SPL addr(default: 0x200000)\n"
  60. "-v, --version set version (default: 0x01010101)\n"
  61. "-f, --file input file name(spl/img)\n"
  62. "-h, --help show this information\n"
  63. };
  64. puts(help);
  65. }
  66. static int parse_args(int argc, char **argv)
  67. {
  68. uint32_t v;
  69. enum {
  70. OPTION_CREAD_HDR = 1,
  71. OPTION_FIXED_HDR,
  72. OPTION_SBL_BAK_OFFSET,
  73. OPTION_VERSION,
  74. OPTION_FILENAME,
  75. OPTION_HELP,
  76. };
  77. static struct option long_options[] =
  78. {
  79. {"creat-splhdr" , no_argument, NULL, OPTION_CREAD_HDR},
  80. {"fix-imghdr" , no_argument, NULL, OPTION_FIXED_HDR},
  81. {"spl-bak-addr" , required_argument, NULL, OPTION_SBL_BAK_OFFSET},
  82. {"version", required_argument, NULL, OPTION_VERSION},
  83. {"file", required_argument, NULL, OPTION_FILENAME},
  84. {"help", no_argument, NULL, OPTION_HELP},
  85. {0, 0, 0, 0}
  86. };
  87. while (1)
  88. {
  89. /* getopt_long stores the option index here. */
  90. int option_index = 0;
  91. int c = getopt_long(argc, argv, "cio:v:f:h", long_options, &option_index);
  92. /* Detect the end of the options. */
  93. if (c == -1)
  94. break;
  95. switch (c) {
  96. case 0:
  97. /* If this option set a flag, do nothing else now. */
  98. if (long_options[option_index].flag != 0)
  99. break;
  100. case 'c':
  101. case OPTION_CREAD_HDR:
  102. g_hdr_conf.creat_hdr_flag = true;
  103. g_hdr_conf.fixed_img_hdr_flag = false;
  104. break;
  105. case 'i':
  106. case OPTION_FIXED_HDR:
  107. g_hdr_conf.fixed_img_hdr_flag = true;
  108. g_hdr_conf.creat_hdr_flag = false;
  109. break;
  110. case 'a':
  111. case OPTION_SBL_BAK_OFFSET:
  112. v = (uint32_t)strtoul(optarg, NULL, 16);
  113. v = htole32(v);
  114. g_hdr_conf.bofs = v;
  115. break;
  116. case 'v':
  117. case OPTION_VERSION:
  118. v = (uint32_t)strtoul(optarg, NULL, 16);
  119. v = htole32(v);
  120. g_hdr_conf.vers = v;
  121. break;
  122. case 'f':
  123. case OPTION_FILENAME:
  124. strcpy((char*)g_hdr_conf.name, optarg);
  125. break;
  126. case 'h':
  127. case OPTION_HELP:
  128. usage();
  129. break;
  130. default:
  131. usage();
  132. break;
  133. }
  134. }
  135. return 0;
  136. }
  137. int spl_creat_hdr(struct hdr_conf_t *conf)
  138. {
  139. int fd;
  140. uint32_t v;
  141. size_t sz;
  142. if (!conf->creat_hdr_flag)
  143. return 0;
  144. ubsplhdr.sofs = htole32(0x240U);
  145. ubsplhdr.res1 = htole32(0x400U);
  146. ubsplhdr.bofs = conf->bofs ? conf->bofs : htole32(DEFBACKUP);
  147. ubsplhdr.vers = conf->vers ? conf->vers : htole32(DEFVERSID);
  148. printf("ubsplhdr.sofs:%#x, ubsplhdr.bofs:%#x, ubsplhdr.vers:%#x name:%s\n",
  149. ubsplhdr.sofs, ubsplhdr.bofs, ubsplhdr.vers, conf->name);
  150. fd = open(conf->name, O_RDONLY);
  151. if (fd == -1) xerror(errno, conf->name);
  152. sz = (size_t)read(fd, ubootspl, sizeof(ubootspl));
  153. if (sz == NOSIZE) xerror(errno, conf->name);
  154. if (sz >= (sizeof(ubootspl)))
  155. xerror(0, "File too large! Please rebuild your SPL with -Os. Maximum allowed size is 180048 bytes.");
  156. v = htole32((uint32_t)sz);
  157. ubsplhdr.fsiz = v;
  158. close(fd);
  159. snprintf(outpath, sizeof(outpath), "%s.normal.out", conf->name);
  160. fd = creat(outpath, 0666);
  161. if (fd == -1) xerror(errno, outpath);
  162. v = crc32(~0U, 0x04c11db7U, ubootspl, sz);
  163. v = crc32_final(v);
  164. v = htole32(v);
  165. ubsplhdr.crcs = v;
  166. write(fd, &ubsplhdr, sizeof(struct ubootsplhdr));
  167. write(fd, ubootspl, sz);
  168. close(fd);
  169. printf("SPL written to %s successfully.\n", outpath);
  170. return 0;
  171. }
  172. int img_fixed_hdr(struct hdr_conf_t *conf)
  173. {
  174. int fd;
  175. size_t sz;
  176. if (!conf->fixed_img_hdr_flag)
  177. return 0;
  178. fd = open(conf->name, O_RDWR);
  179. if (fd == -1) xerror(errno, conf->name);
  180. sz = (size_t)read(fd, &imghdr, sizeof(imghdr));
  181. if (sz == NOSIZE) xerror(errno, conf->name);
  182. /* When starting with emmc, bootrom will read 0x0 instead of partition 0. (Known issues).
  183. Read GPT PMBR+Header, then write the backup address at 0x4, and write the wrong CRC
  184. check value at 0x290, so that bootrom CRC check fails and jump to the backup address
  185. to load the real spl. */
  186. imghdr.bofs = conf->bofs ? conf->bofs : htole32(DEFBACKUP);
  187. imghdr.crcs = htole32(CRCFAILED);
  188. lseek(fd, 0x0, SEEK_SET);
  189. write(fd, &imghdr, sizeof(imghdr));
  190. close(fd);
  191. printf("IMG %s fixed hdr successfully.\n", conf->name);
  192. return 0;
  193. }
  194. int main(int argc, char **argv)
  195. {
  196. parse_args(argc, argv);
  197. spl_creat_hdr(&g_hdr_conf);
  198. img_fixed_hdr(&g_hdr_conf);
  199. }