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