Yann Gautier | ba46a93 | 2018-07-05 16:50:22 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017-2018, STMicroelectronics - All Rights Reserved |
| 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 | |
| 48 | static struct stm32_header stm32image_header; |
| 49 | |
| 50 | static void stm32image_default_header(struct stm32_header *ptr) |
| 51 | { |
| 52 | if (!ptr) { |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | ptr->magic_number = HEADER_MAGIC; |
| 57 | ptr->header_version[VER_MAJOR] = HEADER_VERSION_V1; |
| 58 | ptr->option_flags = HEADER_DEFAULT_OPTION; |
| 59 | ptr->ecdsa_algorithm = 1; |
| 60 | ptr->version_number = 0; |
| 61 | ptr->binary_type = TF_BINARY_TYPE; |
| 62 | } |
| 63 | |
| 64 | static uint32_t stm32image_checksum(void *start, uint32_t len) |
| 65 | { |
| 66 | uint32_t csum = 0; |
| 67 | uint32_t hdr_len = sizeof(struct stm32_header); |
| 68 | uint8_t *p; |
| 69 | |
| 70 | if (len < hdr_len) { |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | p = (unsigned char *)start + hdr_len; |
| 75 | len -= hdr_len; |
| 76 | |
| 77 | while (len > 0) { |
| 78 | csum += *p; |
| 79 | p++; |
| 80 | len--; |
| 81 | } |
| 82 | |
| 83 | return csum; |
| 84 | } |
| 85 | |
| 86 | static void stm32image_print_header(const void *ptr) |
| 87 | { |
| 88 | struct stm32_header *stm32hdr = (struct stm32_header *)ptr; |
| 89 | |
| 90 | printf("Image Type : ST Microelectronics STM32 V%d.%d\n", |
| 91 | stm32hdr->header_version[VER_MAJOR], |
| 92 | stm32hdr->header_version[VER_MINOR]); |
| 93 | printf("Image Size : %lu bytes\n", |
| 94 | (unsigned long)__le32_to_cpu(stm32hdr->image_length)); |
| 95 | printf("Image Load : 0x%08x\n", |
| 96 | __le32_to_cpu(stm32hdr->load_address)); |
| 97 | printf("Entry Point : 0x%08x\n", |
| 98 | __le32_to_cpu(stm32hdr->image_entry_point)); |
| 99 | printf("Checksum : 0x%08x\n", |
| 100 | __le32_to_cpu(stm32hdr->image_checksum)); |
| 101 | printf("Option : 0x%08x\n", |
| 102 | __le32_to_cpu(stm32hdr->option_flags)); |
| 103 | printf("Version : 0x%08x\n", |
| 104 | __le32_to_cpu(stm32hdr->version_number)); |
| 105 | } |
| 106 | |
| 107 | static void stm32image_set_header(void *ptr, struct stat *sbuf, int ifd, |
| 108 | uint32_t loadaddr, uint32_t ep, uint32_t ver) |
| 109 | { |
| 110 | struct stm32_header *stm32hdr = (struct stm32_header *)ptr; |
| 111 | |
| 112 | stm32image_default_header(stm32hdr); |
| 113 | |
| 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)); |
| 118 | stm32hdr->image_checksum = stm32image_checksum(ptr, sbuf->st_size); |
| 119 | stm32hdr->version_number = __cpu_to_le32(ver); |
| 120 | } |
| 121 | |
| 122 | static int stm32image_create_header_file(char *srcname, char *destname, |
| 123 | uint32_t loadaddr, uint32_t entry, |
| 124 | uint32_t version) |
| 125 | { |
| 126 | int src_fd, dest_fd; |
| 127 | struct stat sbuf; |
| 128 | unsigned char *ptr; |
| 129 | |
| 130 | dest_fd = open(destname, O_RDWR | O_CREAT | O_TRUNC | O_APPEND, 0666); |
| 131 | if (dest_fd == -1) { |
| 132 | fprintf(stderr, "Can't open %s: %s\n", destname, |
| 133 | strerror(errno)); |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | src_fd = open(srcname, O_RDONLY); |
| 138 | if (src_fd == -1) { |
| 139 | fprintf(stderr, "Can't open %s: %s\n", srcname, |
| 140 | strerror(errno)); |
| 141 | return -1; |
| 142 | } |
| 143 | |
| 144 | if (fstat(src_fd, &sbuf) < 0) { |
| 145 | return -1; |
| 146 | } |
| 147 | |
| 148 | ptr = mmap(NULL, sbuf.st_size, PROT_READ, MAP_SHARED, src_fd, 0); |
| 149 | if (ptr == MAP_FAILED) { |
| 150 | fprintf(stderr, "Can't read %s\n", srcname); |
| 151 | return -1; |
| 152 | } |
| 153 | |
| 154 | memset(&stm32image_header, 0, sizeof(struct stm32_header)); |
| 155 | |
| 156 | if (write(dest_fd, &stm32image_header, sizeof(struct stm32_header)) != |
| 157 | sizeof(struct stm32_header)) { |
| 158 | fprintf(stderr, "Write error %s: %s\n", destname, |
| 159 | strerror(errno)); |
| 160 | return -1; |
| 161 | } |
| 162 | |
| 163 | if (write(dest_fd, ptr, sbuf.st_size) != sbuf.st_size) { |
| 164 | fprintf(stderr, "Write error on %s: %s\n", destname, |
| 165 | strerror(errno)); |
| 166 | return -1; |
| 167 | } |
| 168 | |
| 169 | munmap((void *)ptr, sbuf.st_size); |
| 170 | close(src_fd); |
| 171 | |
| 172 | if (fstat(dest_fd, &sbuf) < 0) { |
| 173 | return -1; |
| 174 | } |
| 175 | |
| 176 | ptr = mmap(0, sbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, |
| 177 | dest_fd, 0); |
| 178 | |
| 179 | if (ptr == MAP_FAILED) { |
| 180 | fprintf(stderr, "Can't read %s\n", srcname); |
| 181 | return -1; |
| 182 | } |
| 183 | |
| 184 | stm32image_set_header(ptr, &sbuf, dest_fd, loadaddr, entry, version); |
| 185 | |
| 186 | stm32image_print_header(ptr); |
| 187 | |
| 188 | munmap((void *)ptr, sbuf.st_size); |
| 189 | close(dest_fd); |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | int main(int argc, char *argv[]) |
| 194 | { |
| 195 | int opt, loadaddr = -1, entry = -1, err = 0, version = 0; |
| 196 | char *dest = NULL, *src = NULL; |
| 197 | |
| 198 | while ((opt = getopt(argc, argv, ":s:d:l:e:v:")) != -1) { |
| 199 | switch (opt) { |
| 200 | case 's': |
| 201 | src = optarg; |
| 202 | break; |
| 203 | case 'd': |
| 204 | dest = optarg; |
| 205 | break; |
| 206 | case 'l': |
| 207 | loadaddr = strtol(optarg, NULL, 16); |
| 208 | break; |
| 209 | case 'e': |
| 210 | entry = strtol(optarg, NULL, 16); |
| 211 | break; |
| 212 | case 'v': |
| 213 | version = strtol(optarg, NULL, 10); |
| 214 | break; |
| 215 | default: |
| 216 | fprintf(stderr, |
| 217 | "Usage : %s [-s srcfile] [-d destfile] [-l loadaddr] [-e entry_point]\n", |
| 218 | argv[0]); |
| 219 | return -1; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | if (!src) { |
| 224 | fprintf(stderr, "Missing -s option\n"); |
| 225 | return -1; |
| 226 | } |
| 227 | |
| 228 | if (!dest) { |
| 229 | fprintf(stderr, "Missing -d option\n"); |
| 230 | return -1; |
| 231 | } |
| 232 | |
| 233 | if (loadaddr == -1) { |
| 234 | fprintf(stderr, "Missing -l option\n"); |
| 235 | return -1; |
| 236 | } |
| 237 | |
| 238 | if (entry == -1) { |
| 239 | fprintf(stderr, "Missing -e option\n"); |
| 240 | return -1; |
| 241 | } |
| 242 | |
| 243 | err = stm32image_create_header_file(src, dest, loadaddr, |
| 244 | entry, version); |
| 245 | |
| 246 | return err; |
| 247 | } |