oxnas: drop target
[openwrt/openwrt.git] / package / system / mtd / src / trx.c
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
33 #include <sys/ioctl.h>
34 #include <mtd/mtd-user.h>
35 #include "mtd.h"
36 #include "crc32.h"
37
38 #define TRX_CRC32_DATA_OFFSET 12 /* First 12 bytes are not covered by CRC32 */
39 #define TRX_CRC32_DATA_SIZE 16
40 struct trx_header {
41 uint32_t magic; /* "HDR0" */
42 uint32_t len; /* Length of file including header */
43 uint32_t crc32; /* 32-bit CRC from flag_version to end of file */
44 uint32_t flag_version; /* 0:15 flags, 16:31 version */
45 uint32_t offsets[3]; /* Offsets of partitions from start of header */
46 };
47
48 #define min(x,y) ({ \
49 typeof(x) _x = (x); \
50 typeof(y) _y = (y); \
51 (void) (&_x == &_y); \
52 _x < _y ? _x : _y; })
53
54 #if __BYTE_ORDER == __BIG_ENDIAN
55 #define STORE32_LE(X) ((((X) & 0x000000FF) << 24) | (((X) & 0x0000FF00) << 8) | (((X) & 0x00FF0000) >> 8) | (((X) & 0xFF000000) >> 24))
56 #elif __BYTE_ORDER == __LITTLE_ENDIAN
57 #define STORE32_LE(X) (X)
58 #else
59 #error unknown endianness!
60 #endif
61
62 ssize_t pread(int fd, void *buf, size_t count, off_t offset);
63 ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
64
65 int
66 trx_fixup(int fd, const char *name)
67 {
68 struct mtd_info_user mtdInfo;
69 unsigned long len;
70 struct trx_header *trx;
71 void *ptr, *scan;
72 int bfd;
73
74 if (ioctl(fd, MEMGETINFO, &mtdInfo) < 0) {
75 fprintf(stderr, "Failed to get mtd info\n");
76 goto err;
77 }
78
79 len = mtdInfo.size;
80 if (mtdInfo.size <= 0) {
81 fprintf(stderr, "Invalid MTD device size\n");
82 goto err;
83 }
84
85 bfd = mtd_open(name, true);
86 ptr = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, bfd, 0);
87 if (!ptr || (ptr == (void *) -1)) {
88 perror("mmap");
89 fprintf(stderr, "Mapping the TRX header failed\n");
90 goto err1;
91 }
92
93 trx = ptr;
94 if (ntohl(trx->magic) != opt_trxmagic) {
95 fprintf(stderr, "TRX header not found\n");
96 goto err;
97 }
98
99 scan = ptr + offsetof(struct trx_header, flag_version);
100 trx->crc32 = crc32buf(scan, trx->len - (scan - ptr));
101 msync(ptr, sizeof(struct trx_header), MS_SYNC|MS_INVALIDATE);
102 munmap(ptr, len);
103 close(bfd);
104 return 0;
105
106 err1:
107 close(bfd);
108 err:
109 return -1;
110 }
111
112 int
113 trx_check(int imagefd, const char *mtd, char *buf, int *len)
114 {
115 const struct trx_header *trx = (const struct trx_header *) buf;
116 int fd;
117
118 if (strcmp(mtd, "firmware") != 0)
119 return 1;
120
121 if (*len < 32) {
122 *len += read(imagefd, buf + *len, 32 - *len);
123 if (*len < 32) {
124 fprintf(stdout, "Could not get image header, file too small (%d bytes)\n", *len);
125 return 0;
126 }
127 }
128
129 if (ntohl(trx->magic) != opt_trxmagic ||
130 trx->len < sizeof(struct trx_header)) {
131 if (quiet < 2) {
132 fprintf(stderr, "Bad trx header\n");
133 fprintf(stderr, "This is not the correct file format; refusing to flash.\n"
134 "Please specify the correct file or use -f to force.\n");
135 }
136 return 0;
137 }
138
139 /* check if image fits to mtd device */
140 fd = mtd_check_open(mtd);
141 if(fd < 0) {
142 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
143 exit(1);
144 }
145
146 if(mtdsize < trx->len) {
147 fprintf(stderr, "Image too big for partition: %s\n", mtd);
148 close(fd);
149 return 0;
150 }
151
152 close(fd);
153 return 1;
154 }
155
156 int
157 mtd_fixtrx(const char *mtd, size_t offset, size_t data_size)
158 {
159 size_t data_offset;
160 int fd;
161 struct trx_header *trx;
162 char *first_block;
163 char *buf, *to;
164 ssize_t res;
165 size_t block_offset;
166
167 if (quiet < 2)
168 fprintf(stderr, "Trying to fix trx header in %s at 0x%x...\n", mtd, offset);
169
170 fd = mtd_check_open(mtd);
171 if(fd < 0) {
172 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
173 exit(1);
174 }
175
176 data_offset = offset + TRX_CRC32_DATA_OFFSET;
177 if (data_size)
178 data_size += TRX_CRC32_DATA_SIZE;
179 else
180 data_size = erasesize - TRX_CRC32_DATA_OFFSET;
181
182 block_offset = offset & ~(erasesize - 1);
183 offset -= block_offset;
184
185 if (data_offset + data_size > mtdsize) {
186 fprintf(stderr, "Offset too large, device size 0x%x\n", mtdsize);
187 exit(1);
188 }
189
190 first_block = malloc(erasesize);
191 if (!first_block) {
192 perror("malloc");
193 exit(1);
194 }
195
196 res = pread(fd, first_block, erasesize, block_offset);
197 if (res != erasesize) {
198 perror("pread");
199 exit(1);
200 }
201
202 trx = (struct trx_header *)(first_block + offset);
203 if (ntohl(trx->magic) != opt_trxmagic) {
204 fprintf(stderr, "No trx magic found\n");
205 exit(1);
206 }
207
208 buf = malloc(data_size);
209 if (!buf) {
210 perror("malloc");
211 exit(1);
212 }
213
214 to = buf;
215 while (data_size) {
216 size_t read_block_offset = data_offset & ~(erasesize - 1);
217 size_t read_chunk;
218
219 read_chunk = erasesize - (data_offset & (erasesize - 1));
220 read_chunk = min(read_chunk, data_size);
221
222 /* Read from good blocks only to match CFE behavior */
223 if (!mtd_block_is_bad(fd, read_block_offset)) {
224 res = pread(fd, to, read_chunk, data_offset);
225 if (res != read_chunk) {
226 perror("pread");
227 exit(1);
228 }
229 to += read_chunk;
230 }
231
232 data_offset += read_chunk;
233 data_size -= read_chunk;
234 }
235 data_size = to - buf;
236
237 if (trx->len == STORE32_LE(data_size + TRX_CRC32_DATA_OFFSET) &&
238 trx->crc32 == STORE32_LE(crc32buf(buf, data_size))) {
239 if (quiet < 2)
240 fprintf(stderr, "Header already fixed, exiting\n");
241 close(fd);
242 return 0;
243 }
244
245 trx->len = STORE32_LE(data_size + offsetof(struct trx_header, flag_version));
246
247 trx->crc32 = STORE32_LE(crc32buf(buf, data_size));
248 if (mtd_erase_block(fd, block_offset)) {
249 fprintf(stderr, "Can't erease block at 0x%x (%s)\n", block_offset, strerror(errno));
250 exit(1);
251 }
252
253 if (quiet < 2)
254 fprintf(stderr, "New crc32: 0x%x, rewriting block\n", trx->crc32);
255
256 if (pwrite(fd, first_block, erasesize, block_offset) != erasesize) {
257 fprintf(stderr, "Error writing block (%s)\n", strerror(errno));
258 exit(1);
259 }
260
261 if (quiet < 2)
262 fprintf(stderr, "Done.\n");
263
264 close (fd);
265 sync();
266 return 0;
267
268 }