stm32image.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * Copyright (c) 2017-2022, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <asm/byteorder.h>
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <stdint.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <sys/mman.h>
  14. #include <sys/stat.h>
  15. #include <sys/types.h>
  16. #include <unistd.h>
  17. /* Magic = 'S' 'T' 'M' 0x32 */
  18. #define HEADER_MAGIC __be32_to_cpu(0x53544D32)
  19. #define VER_MAJOR 2
  20. #define VER_MINOR 1
  21. #define VER_VARIANT 0
  22. #define HEADER_VERSION_V1 0x1
  23. #define HEADER_VERSION_V2 0x2
  24. #define PADDING_HEADER_MAGIC __be32_to_cpu(0x5354FFFF)
  25. #define PADDING_HEADER_FLAG (1 << 31)
  26. #define PADDING_HEADER_LENGTH 0x180
  27. struct stm32_header_v1 {
  28. uint32_t magic_number;
  29. uint8_t image_signature[64];
  30. uint32_t image_checksum;
  31. uint8_t header_version[4];
  32. uint32_t image_length;
  33. uint32_t image_entry_point;
  34. uint32_t reserved1;
  35. uint32_t load_address;
  36. uint32_t reserved2;
  37. uint32_t version_number;
  38. uint32_t option_flags;
  39. uint32_t ecdsa_algorithm;
  40. uint8_t ecdsa_public_key[64];
  41. uint8_t padding[83];
  42. uint8_t binary_type;
  43. };
  44. struct stm32_header_v2 {
  45. uint32_t magic_number;
  46. uint8_t image_signature[64];
  47. uint32_t image_checksum;
  48. uint8_t header_version[4];
  49. uint32_t image_length;
  50. uint32_t image_entry_point;
  51. uint32_t reserved1;
  52. uint32_t load_address;
  53. uint32_t reserved2;
  54. uint32_t version_number;
  55. uint32_t extension_flags;
  56. uint32_t extension_headers_length;
  57. uint32_t binary_type;
  58. uint8_t padding[16];
  59. uint32_t extension_header_type;
  60. uint32_t extension_header_length;
  61. uint8_t extension_padding[376];
  62. };
  63. static void stm32image_default_header(void *ptr)
  64. {
  65. struct stm32_header_v1 *header = (struct stm32_header_v1 *)ptr;
  66. if (!header) {
  67. return;
  68. }
  69. header->magic_number = HEADER_MAGIC;
  70. header->version_number = __cpu_to_le32(0);
  71. }
  72. static uint32_t stm32image_checksum(void *start, uint32_t len,
  73. uint32_t header_size)
  74. {
  75. uint32_t csum = 0;
  76. uint8_t *p;
  77. if (len < header_size) {
  78. return 0;
  79. }
  80. p = (unsigned char *)start + header_size;
  81. len -= header_size;
  82. while (len > 0) {
  83. csum += *p;
  84. p++;
  85. len--;
  86. }
  87. return csum;
  88. }
  89. static void stm32image_print_header(const void *ptr)
  90. {
  91. struct stm32_header_v1 *stm32hdr = (struct stm32_header_v1 *)ptr;
  92. struct stm32_header_v2 *stm32hdr_v2 = (struct stm32_header_v2 *)ptr;
  93. printf("Image Type : ST Microelectronics STM32 V%d.%d\n",
  94. stm32hdr->header_version[VER_MAJOR],
  95. stm32hdr->header_version[VER_MINOR]);
  96. printf("Image Size : %lu bytes\n",
  97. (unsigned long)__le32_to_cpu(stm32hdr->image_length));
  98. printf("Image Load : 0x%08x\n",
  99. __le32_to_cpu(stm32hdr->load_address));
  100. printf("Entry Point : 0x%08x\n",
  101. __le32_to_cpu(stm32hdr->image_entry_point));
  102. printf("Checksum : 0x%08x\n",
  103. __le32_to_cpu(stm32hdr->image_checksum));
  104. switch (stm32hdr->header_version[VER_MAJOR]) {
  105. case HEADER_VERSION_V1:
  106. printf("Option : 0x%08x\n",
  107. __le32_to_cpu(stm32hdr->option_flags));
  108. break;
  109. case HEADER_VERSION_V2:
  110. printf("Extension : 0x%08x\n",
  111. __le32_to_cpu(stm32hdr_v2->extension_flags));
  112. break;
  113. default:
  114. printf("Incorrect header version\n");
  115. }
  116. printf("Version : 0x%08x\n",
  117. __le32_to_cpu(stm32hdr->version_number));
  118. }
  119. static int stm32image_set_header(void *ptr, struct stat *sbuf, int ifd,
  120. uint32_t loadaddr, uint32_t ep, uint32_t ver,
  121. uint32_t major, uint32_t minor,
  122. uint32_t binary_type, uint32_t header_size)
  123. {
  124. struct stm32_header_v1 *stm32hdr = (struct stm32_header_v1 *)ptr;
  125. struct stm32_header_v2 *stm32hdr_v2 = (struct stm32_header_v2 *)ptr;
  126. uint32_t ext_size = 0U;
  127. uint32_t ext_flags = 0U;
  128. stm32image_default_header(ptr);
  129. stm32hdr->header_version[VER_MAJOR] = major;
  130. stm32hdr->header_version[VER_MINOR] = minor;
  131. stm32hdr->load_address = __cpu_to_le32(loadaddr);
  132. stm32hdr->image_entry_point = __cpu_to_le32(ep);
  133. stm32hdr->image_length = __cpu_to_le32((uint32_t)sbuf->st_size -
  134. header_size);
  135. stm32hdr->image_checksum =
  136. __cpu_to_le32(stm32image_checksum(ptr, sbuf->st_size,
  137. header_size));
  138. switch (stm32hdr->header_version[VER_MAJOR]) {
  139. case HEADER_VERSION_V1:
  140. /* Default option for header v1 : bit0 => no signature */
  141. stm32hdr->option_flags = __cpu_to_le32(0x00000001);
  142. stm32hdr->ecdsa_algorithm = __cpu_to_le32(1);
  143. stm32hdr->binary_type = (uint8_t)binary_type;
  144. break;
  145. case HEADER_VERSION_V2:
  146. stm32hdr_v2->binary_type = binary_type;
  147. ext_size += PADDING_HEADER_LENGTH;
  148. ext_flags |= PADDING_HEADER_FLAG;
  149. stm32hdr_v2->extension_flags =
  150. __cpu_to_le32(ext_flags);
  151. stm32hdr_v2->extension_headers_length =
  152. __cpu_to_le32(ext_size);
  153. stm32hdr_v2->extension_header_type = PADDING_HEADER_MAGIC;
  154. stm32hdr_v2->extension_header_length =
  155. __cpu_to_le32(PADDING_HEADER_LENGTH);
  156. break;
  157. default:
  158. return -1;
  159. }
  160. stm32hdr->version_number = __cpu_to_le32(ver);
  161. return 0;
  162. }
  163. static int stm32image_create_header_file(char *srcname, char *destname,
  164. uint32_t loadaddr, uint32_t entry,
  165. uint32_t version, uint32_t major,
  166. uint32_t minor, uint32_t binary_type)
  167. {
  168. int src_fd, dest_fd, header_size;
  169. struct stat sbuf;
  170. unsigned char *ptr;
  171. void *stm32image_header;
  172. dest_fd = open(destname, O_RDWR | O_CREAT | O_TRUNC | O_APPEND, 0666);
  173. if (dest_fd == -1) {
  174. fprintf(stderr, "Can't open %s: %s\n", destname,
  175. strerror(errno));
  176. return -1;
  177. }
  178. src_fd = open(srcname, O_RDONLY);
  179. if (src_fd == -1) {
  180. fprintf(stderr, "Can't open %s: %s\n", srcname,
  181. strerror(errno));
  182. return -1;
  183. }
  184. if (fstat(src_fd, &sbuf) < 0) {
  185. return -1;
  186. }
  187. ptr = mmap(NULL, sbuf.st_size, PROT_READ, MAP_SHARED, src_fd, 0);
  188. if (ptr == MAP_FAILED) {
  189. fprintf(stderr, "Can't read %s\n", srcname);
  190. return -1;
  191. }
  192. switch (major) {
  193. case HEADER_VERSION_V1:
  194. stm32image_header = malloc(sizeof(struct stm32_header_v1));
  195. header_size = sizeof(struct stm32_header_v1);
  196. break;
  197. case HEADER_VERSION_V2:
  198. stm32image_header = malloc(sizeof(struct stm32_header_v2));
  199. header_size = sizeof(struct stm32_header_v2);
  200. break;
  201. default:
  202. return -1;
  203. }
  204. memset(stm32image_header, 0, header_size);
  205. if (write(dest_fd, stm32image_header, header_size) !=
  206. header_size) {
  207. fprintf(stderr, "Write error %s: %s\n", destname,
  208. strerror(errno));
  209. free(stm32image_header);
  210. return -1;
  211. }
  212. free(stm32image_header);
  213. if (write(dest_fd, ptr, sbuf.st_size) != sbuf.st_size) {
  214. fprintf(stderr, "Write error on %s: %s\n", destname,
  215. strerror(errno));
  216. return -1;
  217. }
  218. munmap((void *)ptr, sbuf.st_size);
  219. close(src_fd);
  220. if (fstat(dest_fd, &sbuf) < 0) {
  221. return -1;
  222. }
  223. ptr = mmap(0, sbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED,
  224. dest_fd, 0);
  225. if (ptr == MAP_FAILED) {
  226. fprintf(stderr, "Can't write %s\n", destname);
  227. return -1;
  228. }
  229. if (stm32image_set_header(ptr, &sbuf, dest_fd, loadaddr,
  230. entry, version, major, minor,
  231. binary_type, header_size) != 0) {
  232. return -1;
  233. }
  234. stm32image_print_header(ptr);
  235. munmap((void *)ptr, sbuf.st_size);
  236. close(dest_fd);
  237. return 0;
  238. }
  239. int main(int argc, char *argv[])
  240. {
  241. int opt;
  242. int loadaddr = -1;
  243. int entry = -1;
  244. int err = 0;
  245. int version = 0;
  246. int binary_type = -1;
  247. int major = HEADER_VERSION_V2;
  248. int minor = 0;
  249. char *dest = NULL;
  250. char *src = NULL;
  251. while ((opt = getopt(argc, argv, ":b:s:d:l:e:v:m:n:")) != -1) {
  252. switch (opt) {
  253. case 'b':
  254. binary_type = strtol(optarg, NULL, 0);
  255. break;
  256. case 's':
  257. src = optarg;
  258. break;
  259. case 'd':
  260. dest = optarg;
  261. break;
  262. case 'l':
  263. loadaddr = strtol(optarg, NULL, 0);
  264. break;
  265. case 'e':
  266. entry = strtol(optarg, NULL, 0);
  267. break;
  268. case 'v':
  269. version = strtol(optarg, NULL, 0);
  270. break;
  271. case 'm':
  272. major = strtol(optarg, NULL, 0);
  273. break;
  274. case 'n':
  275. minor = strtol(optarg, NULL, 0);
  276. break;
  277. default:
  278. fprintf(stderr,
  279. "Usage : %s [-s srcfile] [-d destfile] [-l loadaddr] [-e entry_point] [-m major] [-n minor] [-b binary_type]\n",
  280. argv[0]);
  281. return -1;
  282. }
  283. }
  284. if (!src) {
  285. fprintf(stderr, "Missing -s option\n");
  286. return -1;
  287. }
  288. if (!dest) {
  289. fprintf(stderr, "Missing -d option\n");
  290. return -1;
  291. }
  292. if (loadaddr == -1) {
  293. fprintf(stderr, "Missing -l option\n");
  294. return -1;
  295. }
  296. if (entry == -1) {
  297. fprintf(stderr, "Missing -e option\n");
  298. return -1;
  299. }
  300. if (binary_type == -1) {
  301. fprintf(stderr, "Missing -b option\n");
  302. return -1;
  303. }
  304. err = stm32image_create_header_file(src, dest, loadaddr,
  305. entry, version, major, minor,
  306. binary_type);
  307. return err;
  308. }