renesas_spkgimage.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // SPDX-License-Identifier: BSD-2-Clause
  2. /*
  3. * Generate Renesas RZ/N1 BootROM header (SPKG)
  4. * (C) Copyright 2022 Schneider Electric
  5. *
  6. * Based on spkg_utility.c
  7. * (C) Copyright 2016 Renesas Electronics Europe Ltd
  8. */
  9. #include "imagetool.h"
  10. #include <limits.h>
  11. #include <image.h>
  12. #include <stdarg.h>
  13. #include <stdint.h>
  14. #include <u-boot/crc.h>
  15. #include "renesas_spkgimage.h"
  16. /* Note: the ordering of the bitfields does not matter */
  17. struct config_file {
  18. unsigned int version:1;
  19. unsigned int ecc_block_size:2;
  20. unsigned int ecc_enable:1;
  21. unsigned int ecc_scheme:3;
  22. unsigned int ecc_bytes:8;
  23. unsigned int blp_len;
  24. unsigned int padding;
  25. };
  26. static struct config_file conf;
  27. static int check_range(const char *name, int val, int min, int max)
  28. {
  29. if (val < min) {
  30. fprintf(stderr, "Warning: param '%s' adjusted to min %d\n",
  31. name, min);
  32. val = min;
  33. }
  34. if (val > max) {
  35. fprintf(stderr, "Warning: param '%s' adjusted to max %d\n",
  36. name, max);
  37. val = max;
  38. }
  39. return val;
  40. }
  41. static int spkgimage_parse_config_line(char *line, size_t line_num)
  42. {
  43. char *saveptr;
  44. char *delim = "\t ";
  45. char *name = strtok_r(line, delim, &saveptr);
  46. char *val_str = strtok_r(NULL, delim, &saveptr);
  47. int value = atoi(val_str);
  48. if (!strcmp("VERSION", name)) {
  49. conf.version = check_range(name, value, 1, 15);
  50. } else if (!strcmp("NAND_ECC_ENABLE", name)) {
  51. conf.ecc_enable = check_range(name, value, 0, 1);
  52. } else if (!strcmp("NAND_ECC_BLOCK_SIZE", name)) {
  53. conf.ecc_block_size = check_range(name, value, 0, 2);
  54. } else if (!strcmp("NAND_ECC_SCHEME", name)) {
  55. conf.ecc_scheme = check_range(name, value, 0, 7);
  56. } else if (!strcmp("NAND_BYTES_PER_ECC_BLOCK", name)) {
  57. conf.ecc_bytes = check_range(name, value, 0, 255);
  58. } else if (!strcmp("ADD_DUMMY_BLP", name)) {
  59. conf.blp_len = value ? SPKG_BLP_SIZE : 0;
  60. } else if (!strcmp("PADDING", name)) {
  61. if (strrchr(val_str, 'K'))
  62. value = value * 1024;
  63. else if (strrchr(val_str, 'M'))
  64. value = value * 1024 * 1024;
  65. conf.padding = check_range(name, value, 1, INT_MAX);
  66. } else {
  67. fprintf(stderr,
  68. "config error: unknown keyword on line %zu\n",
  69. line_num);
  70. return -EINVAL;
  71. }
  72. return 0;
  73. }
  74. static int spkgimage_parse_config_file(char *filename)
  75. {
  76. FILE *fcfg;
  77. char line[256];
  78. size_t line_num = 0;
  79. fcfg = fopen(filename, "r");
  80. if (!fcfg)
  81. return -EINVAL;
  82. while (fgets(line, sizeof(line), fcfg)) {
  83. line_num += 1;
  84. /* Skip blank lines and comments */
  85. if (line[0] == '\n' || line[0] == '#')
  86. continue;
  87. /* Strip any trailing newline */
  88. line[strcspn(line, "\n")] = 0;
  89. /* Parse the line */
  90. if (spkgimage_parse_config_line(line, line_num))
  91. return -EINVAL;
  92. }
  93. fclose(fcfg);
  94. /* Avoid divide-by-zero later on */
  95. if (!conf.padding)
  96. conf.padding = 1;
  97. return 0;
  98. }
  99. static int spkgimage_check_params(struct image_tool_params *params)
  100. {
  101. if (!params->addr) {
  102. fprintf(stderr, "Error: Load Address must be set.\n");
  103. return -EINVAL;
  104. }
  105. if (!params->imagename || !params->imagename[0]) {
  106. fprintf(stderr, "Error: Image name must be set.\n");
  107. return -EINVAL;
  108. }
  109. if (!params->datafile) {
  110. fprintf(stderr, "Error: Data filename must be set.\n");
  111. return -EINVAL;
  112. }
  113. return 0;
  114. }
  115. static int spkgimage_verify_header(unsigned char *ptr, int size,
  116. struct image_tool_params *param)
  117. {
  118. struct spkg_file *file = (struct spkg_file *)ptr;
  119. struct spkg_hdr *header = (struct spkg_hdr *)ptr;
  120. char marker[4] = SPKG_HEADER_MARKER;
  121. uint32_t payload_length;
  122. uint32_t crc;
  123. uint8_t *crc_buf;
  124. /* Check the marker bytes */
  125. if (memcmp(header->marker, marker, 4)) {
  126. fprintf(stderr, "Error: invalid marker bytes\n");
  127. return -EINVAL;
  128. }
  129. /* Check the CRC */
  130. crc = crc32(0, ptr, SPKG_HEADER_SIZE - SPKG_CRC_SIZE);
  131. if (crc != header->crc) {
  132. fprintf(stderr, "Error: invalid header CRC=\n");
  133. return -EINVAL;
  134. }
  135. /* Check all copies of header are the same */
  136. for (int i = 1; i < SPKG_HEADER_COUNT; i++) {
  137. if (memcmp(&header[0], &header[i], SPKG_HEADER_SIZE)) {
  138. fprintf(stderr, "Error: header %d mismatch\n", i);
  139. return -EINVAL;
  140. }
  141. }
  142. /* Check the payload CRC */
  143. payload_length = le32_to_cpu(header->payload_length) >> 8;
  144. crc_buf = file->payload + payload_length - SPKG_CRC_SIZE;
  145. crc = crc32(0, file->payload, payload_length - SPKG_CRC_SIZE);
  146. if (crc_buf[0] != (crc & 0xff) ||
  147. crc_buf[1] != (crc >> 8 & 0xff) ||
  148. crc_buf[2] != (crc >> 16 & 0xff) ||
  149. crc_buf[3] != (crc >> 24 & 0xff)) {
  150. fprintf(stderr, "Error: invalid payload CRC\n");
  151. return -EINVAL;
  152. }
  153. return 0;
  154. }
  155. static void spkgimage_print_header(const void *ptr,
  156. struct image_tool_params *image)
  157. {
  158. const struct spkg_hdr *h = ptr;
  159. uint32_t offset = le32_to_cpu(h->execution_offset);
  160. printf("Image type\t: Renesas SPKG Image\n");
  161. printf("Marker\t\t: %c%c%c%c\n",
  162. h->marker[0], h->marker[1], h->marker[2], h->marker[3]);
  163. printf("Version\t\t: %d\n", h->version);
  164. printf("ECC\t\t: ");
  165. if (h->ecc & 0x20)
  166. printf("Scheme %d, Block size %d, Strength %d\n",
  167. h->ecc_scheme, (h->ecc >> 1) & 3, h->ecc_bytes);
  168. else
  169. printf("Not enabled\n");
  170. printf("Payload length\t: %d\n", le32_to_cpu(h->payload_length) >> 8);
  171. printf("Load address\t: 0x%08x\n", le32_to_cpu(h->load_address));
  172. printf("Execution offset: 0x%08x (%s mode)\n", offset & ~1,
  173. offset & 1 ? "THUMB" : "ARM");
  174. printf("Header checksum\t: 0x%08x\n", le32_to_cpu(h->crc));
  175. }
  176. /*
  177. * This is the same as the macro version in include/kernel.h.
  178. * However we cannot include that header, because for host tools,
  179. * it ends up pulling in the host /usr/include/linux/kernel.h,
  180. * which lacks the definition of roundup().
  181. */
  182. static inline uint32_t roundup(uint32_t x, uint32_t y)
  183. {
  184. return ((x + y - 1) / y) * y;
  185. }
  186. static int spkgimage_vrec_header(struct image_tool_params *params,
  187. struct image_type_params *tparams)
  188. {
  189. struct stat s;
  190. struct spkg_file *out_buf;
  191. /* Parse the config file */
  192. if (spkgimage_parse_config_file(params->imagename)) {
  193. fprintf(stderr, "Error parsing config file\n");
  194. exit(EXIT_FAILURE);
  195. }
  196. /* Get size of input data file */
  197. if (stat(params->datafile, &s)) {
  198. fprintf(stderr, "Could not stat data file: %s: %s\n",
  199. params->datafile, strerror(errno));
  200. exit(EXIT_FAILURE);
  201. }
  202. params->orig_file_size = s.st_size;
  203. /* Determine size of resulting SPKG file */
  204. uint32_t header_len = SPKG_HEADER_SIZE * SPKG_HEADER_COUNT;
  205. uint32_t payload_len = conf.blp_len + s.st_size + SPKG_CRC_SIZE;
  206. uint32_t total_len = header_len + payload_len;
  207. /* Round up to next multiple of padding size */
  208. uint32_t padded_len = roundup(total_len, conf.padding);
  209. /* Number of padding bytes to add */
  210. conf.padding = padded_len - total_len;
  211. /* Fixup payload_len to include padding bytes */
  212. payload_len += conf.padding;
  213. /* Prepare the header */
  214. struct spkg_hdr header = {
  215. .marker = SPKG_HEADER_MARKER,
  216. .version = conf.version,
  217. .ecc = (conf.ecc_enable << 5) | (conf.ecc_block_size << 1),
  218. .ecc_scheme = conf.ecc_scheme,
  219. .ecc_bytes = conf.ecc_bytes,
  220. .payload_length = cpu_to_le32(payload_len << 8),
  221. .load_address = cpu_to_le32(params->addr),
  222. .execution_offset = cpu_to_le32(params->ep - params->addr),
  223. };
  224. header.crc = crc32(0, (uint8_t *)&header,
  225. sizeof(header) - SPKG_CRC_SIZE);
  226. /* The SPKG contains 8 copies of the header */
  227. out_buf = malloc(sizeof(struct spkg_file));
  228. if (!out_buf) {
  229. fprintf(stderr, "Error: Data filename must be set.\n");
  230. return -ENOMEM;
  231. }
  232. tparams->hdr = out_buf;
  233. tparams->header_size = sizeof(struct spkg_file);
  234. /* Fill the SPKG with the headers */
  235. for (int i = 0; i < SPKG_HEADER_COUNT; i++)
  236. memcpy(&out_buf->header[i], &header, sizeof(header));
  237. /* Extra bytes to allocate in the output file */
  238. return conf.blp_len + conf.padding + 4;
  239. }
  240. static void spkgimage_set_header(void *ptr, struct stat *sbuf, int ifd,
  241. struct image_tool_params *params)
  242. {
  243. uint8_t *payload = ptr + SPKG_HEADER_SIZE * SPKG_HEADER_COUNT;
  244. uint8_t *file_end = payload + conf.blp_len + params->orig_file_size;
  245. uint8_t *crc_buf = file_end + conf.padding;
  246. uint32_t crc;
  247. /* Make room for the Dummy BLp header */
  248. memmove(payload + conf.blp_len, payload, params->orig_file_size);
  249. /* Fill the SPKG with the Dummy BLp */
  250. memset(payload, 0x88, conf.blp_len);
  251. /*
  252. * mkimage copy_file() pads the input file with zeros.
  253. * Replace those zeros with flash friendly one bits.
  254. * The original version skipped the first 4 bytes,
  255. * probably an oversight, but for consistency we
  256. * keep the same behaviour.
  257. */
  258. if (conf.padding >= 4)
  259. memset(file_end + 4, 0xff, conf.padding - 4);
  260. /* Add Payload CRC */
  261. crc = crc32(0, payload, crc_buf - payload);
  262. crc_buf[0] = crc;
  263. crc_buf[1] = crc >> 8;
  264. crc_buf[2] = crc >> 16;
  265. crc_buf[3] = crc >> 24;
  266. }
  267. static int spkgimage_check_image_types(uint8_t type)
  268. {
  269. return type == IH_TYPE_RENESAS_SPKG ? 0 : -EINVAL;
  270. }
  271. /*
  272. * spkgimage type parameter definition
  273. */
  274. U_BOOT_IMAGE_TYPE(
  275. spkgimage,
  276. "Renesas SPKG Image",
  277. 0,
  278. NULL,
  279. spkgimage_check_params,
  280. spkgimage_verify_header,
  281. spkgimage_print_header,
  282. spkgimage_set_header,
  283. NULL,
  284. spkgimage_check_image_types,
  285. NULL,
  286. spkgimage_vrec_header
  287. );