b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * trx.c |
| 3 | * |
| 4 | * Copyright (C) 2005 Mike Baker |
| 5 | * Copyright (C) 2008 Felix Fietkau <nbd@nbd.name> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version 2 |
| 10 | * of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <stddef.h> |
| 25 | #include <unistd.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <sys/mman.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <endian.h> |
| 30 | #include <string.h> |
| 31 | #include <errno.h> |
| 32 | #include <netinet/in.h> |
| 33 | |
| 34 | #include <sys/ioctl.h> |
| 35 | #include <mtd/mtd-user.h> |
| 36 | #include "mtd.h" |
| 37 | #include "crc32.h" |
| 38 | |
| 39 | #define TRX_CRC32_DATA_OFFSET 12 /* First 12 bytes are not covered by CRC32 */ |
| 40 | #define TRX_CRC32_DATA_SIZE 16 |
| 41 | struct trx_header { |
| 42 | uint32_t magic; /* "HDR0" */ |
| 43 | uint32_t len; /* Length of file including header */ |
| 44 | uint32_t crc32; /* 32-bit CRC from flag_version to end of file */ |
| 45 | uint32_t flag_version; /* 0:15 flags, 16:31 version */ |
| 46 | uint32_t offsets[3]; /* Offsets of partitions from start of header */ |
| 47 | }; |
| 48 | |
| 49 | #define min(x,y) ({ \ |
| 50 | typeof(x) _x = (x); \ |
| 51 | typeof(y) _y = (y); \ |
| 52 | (void) (&_x == &_y); \ |
| 53 | _x < _y ? _x : _y; }) |
| 54 | |
| 55 | #if __BYTE_ORDER == __BIG_ENDIAN |
| 56 | #define STORE32_LE(X) ((((X) & 0x000000FF) << 24) | (((X) & 0x0000FF00) << 8) | (((X) & 0x00FF0000) >> 8) | (((X) & 0xFF000000) >> 24)) |
| 57 | #elif __BYTE_ORDER == __LITTLE_ENDIAN |
| 58 | #define STORE32_LE(X) (X) |
| 59 | #else |
| 60 | #error unknown endianness! |
| 61 | #endif |
| 62 | |
| 63 | ssize_t pread(int fd, void *buf, size_t count, off_t offset); |
| 64 | ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset); |
| 65 | |
| 66 | int |
| 67 | trx_fixup(int fd, const char *name) |
| 68 | { |
| 69 | struct mtd_info_user mtdInfo; |
| 70 | unsigned long len; |
| 71 | struct trx_header *trx; |
| 72 | void *ptr, *scan; |
| 73 | int bfd; |
| 74 | |
| 75 | if (ioctl(fd, MEMGETINFO, &mtdInfo) < 0) { |
| 76 | fprintf(stderr, "Failed to get mtd info\n"); |
| 77 | goto err; |
| 78 | } |
| 79 | |
| 80 | len = mtdInfo.size; |
| 81 | if (mtdInfo.size <= 0) { |
| 82 | fprintf(stderr, "Invalid MTD device size\n"); |
| 83 | goto err; |
| 84 | } |
| 85 | |
| 86 | bfd = mtd_open(name, true); |
| 87 | ptr = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, bfd, 0); |
| 88 | if (!ptr || (ptr == (void *) -1)) { |
| 89 | perror("mmap"); |
| 90 | fprintf(stderr, "Mapping the TRX header failed\n"); |
| 91 | goto err1; |
| 92 | } |
| 93 | |
| 94 | trx = ptr; |
| 95 | if (ntohl(trx->magic) != opt_trxmagic) { |
| 96 | fprintf(stderr, "TRX header not found\n"); |
| 97 | goto err; |
| 98 | } |
| 99 | |
| 100 | scan = ptr + offsetof(struct trx_header, flag_version); |
| 101 | trx->crc32 = crc32buf(scan, trx->len - (scan - ptr)); |
| 102 | msync(ptr, sizeof(struct trx_header), MS_SYNC|MS_INVALIDATE); |
| 103 | munmap(ptr, len); |
| 104 | close(bfd); |
| 105 | return 0; |
| 106 | |
| 107 | err1: |
| 108 | close(bfd); |
| 109 | err: |
| 110 | return -1; |
| 111 | } |
| 112 | |
| 113 | int |
| 114 | trx_check(int imagefd, const char *mtd, char *buf, int *len) |
| 115 | { |
| 116 | const struct trx_header *trx = (const struct trx_header *) buf; |
| 117 | int fd; |
| 118 | |
| 119 | if (strcmp(mtd, "firmware") != 0) |
| 120 | return 1; |
| 121 | |
| 122 | if (*len < 32) { |
| 123 | *len += read(imagefd, buf + *len, 32 - *len); |
| 124 | if (*len < 32) { |
| 125 | fprintf(stdout, "Could not get image header, file too small (%d bytes)\n", *len); |
| 126 | return 0; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if (ntohl(trx->magic) != opt_trxmagic || |
| 131 | trx->len < sizeof(struct trx_header)) { |
| 132 | if (quiet < 2) { |
| 133 | fprintf(stderr, "Bad trx header\n"); |
| 134 | fprintf(stderr, "This is not the correct file format; refusing to flash.\n" |
| 135 | "Please specify the correct file or use -f to force.\n"); |
| 136 | } |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | /* check if image fits to mtd device */ |
| 141 | fd = mtd_check_open(mtd); |
| 142 | if(fd < 0) { |
| 143 | fprintf(stderr, "Could not open mtd device: %s\n", mtd); |
| 144 | exit(1); |
| 145 | } |
| 146 | |
| 147 | if(mtdsize < trx->len) { |
| 148 | fprintf(stderr, "Image too big for partition: %s\n", mtd); |
| 149 | close(fd); |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | close(fd); |
| 154 | return 1; |
| 155 | } |
| 156 | |
| 157 | int |
| 158 | mtd_fixtrx(const char *mtd, size_t offset, size_t data_size) |
| 159 | { |
| 160 | size_t data_offset; |
| 161 | int fd; |
| 162 | struct trx_header *trx; |
| 163 | char *first_block; |
| 164 | char *buf, *to; |
| 165 | ssize_t res; |
| 166 | size_t block_offset; |
| 167 | |
| 168 | if (quiet < 2) |
| 169 | fprintf(stderr, "Trying to fix trx header in %s at 0x%zx...\n", mtd, offset); |
| 170 | |
| 171 | fd = mtd_check_open(mtd); |
| 172 | if(fd < 0) { |
| 173 | fprintf(stderr, "Could not open mtd device: %s\n", mtd); |
| 174 | exit(1); |
| 175 | } |
| 176 | |
| 177 | data_offset = offset + TRX_CRC32_DATA_OFFSET; |
| 178 | if (data_size) |
| 179 | data_size += TRX_CRC32_DATA_SIZE; |
| 180 | else |
| 181 | data_size = erasesize - TRX_CRC32_DATA_OFFSET; |
| 182 | |
| 183 | block_offset = offset & ~(erasesize - 1); |
| 184 | offset -= block_offset; |
| 185 | |
| 186 | if (data_offset + data_size > mtdsize) { |
| 187 | fprintf(stderr, "Offset too large, device size 0x%x\n", mtdsize); |
| 188 | exit(1); |
| 189 | } |
| 190 | |
| 191 | first_block = malloc(erasesize); |
| 192 | if (!first_block) { |
| 193 | perror("malloc"); |
| 194 | exit(1); |
| 195 | } |
| 196 | |
| 197 | res = pread(fd, first_block, erasesize, block_offset); |
| 198 | if (res != erasesize) { |
| 199 | perror("pread"); |
| 200 | exit(1); |
| 201 | } |
| 202 | |
| 203 | trx = (struct trx_header *)(first_block + offset); |
| 204 | if (ntohl(trx->magic) != opt_trxmagic) { |
| 205 | fprintf(stderr, "No trx magic found\n"); |
| 206 | exit(1); |
| 207 | } |
| 208 | |
| 209 | buf = malloc(data_size); |
| 210 | if (!buf) { |
| 211 | perror("malloc"); |
| 212 | exit(1); |
| 213 | } |
| 214 | |
| 215 | to = buf; |
| 216 | while (data_size) { |
| 217 | size_t read_block_offset = data_offset & ~(erasesize - 1); |
| 218 | size_t read_chunk; |
| 219 | |
| 220 | read_chunk = erasesize - (data_offset & (erasesize - 1)); |
| 221 | read_chunk = min(read_chunk, data_size); |
| 222 | |
| 223 | /* Read from good blocks only to match CFE behavior */ |
| 224 | if (!mtd_block_is_bad(fd, read_block_offset)) { |
| 225 | res = pread(fd, to, read_chunk, data_offset); |
| 226 | if (res != read_chunk) { |
| 227 | perror("pread"); |
| 228 | exit(1); |
| 229 | } |
| 230 | to += read_chunk; |
| 231 | } |
| 232 | |
| 233 | data_offset += read_chunk; |
| 234 | data_size -= read_chunk; |
| 235 | } |
| 236 | data_size = to - buf; |
| 237 | |
| 238 | if (trx->len == STORE32_LE(data_size + TRX_CRC32_DATA_OFFSET) && |
| 239 | trx->crc32 == STORE32_LE(crc32buf(buf, data_size))) { |
| 240 | if (quiet < 2) |
| 241 | fprintf(stderr, "Header already fixed, exiting\n"); |
| 242 | close(fd); |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | trx->len = STORE32_LE(data_size + offsetof(struct trx_header, flag_version)); |
| 247 | |
| 248 | trx->crc32 = STORE32_LE(crc32buf(buf, data_size)); |
| 249 | if (mtd_erase_block(fd, block_offset)) { |
| 250 | fprintf(stderr, "Can't erease block at 0x%zx (%s)\n", block_offset, strerror(errno)); |
| 251 | exit(1); |
| 252 | } |
| 253 | |
| 254 | if (quiet < 2) |
| 255 | fprintf(stderr, "New crc32: 0x%x, rewriting block\n", trx->crc32); |
| 256 | |
| 257 | if (pwrite(fd, first_block, erasesize, block_offset) != erasesize) { |
| 258 | fprintf(stderr, "Error writing block (%s)\n", strerror(errno)); |
| 259 | exit(1); |
| 260 | } |
| 261 | |
| 262 | if (quiet < 2) |
| 263 | fprintf(stderr, "Done.\n"); |
| 264 | |
| 265 | close (fd); |
| 266 | sync(); |
| 267 | return 0; |
| 268 | |
| 269 | } |