mtd: disable trx_check() for ar71xx, fixes a regression introduced in r42403
[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@openwrt.org>
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 <string.h>
30 #include <errno.h>
31
32 #include <sys/ioctl.h>
33 #include <mtd/mtd-user.h>
34 #include "mtd.h"
35 #include "crc32.h"
36
37 #define TRX_MAGIC 0x30524448 /* "HDR0" */
38 struct trx_header {
39 uint32_t magic; /* "HDR0" */
40 uint32_t len; /* Length of file including header */
41 uint32_t crc32; /* 32-bit CRC from flag_version to end of file */
42 uint32_t flag_version; /* 0:15 flags, 16:31 version */
43 uint32_t offsets[3]; /* Offsets of partitions from start of header */
44 };
45
46 #if __BYTE_ORDER == __BIG_ENDIAN
47 #define STORE32_LE(X) ((((X) & 0x000000FF) << 24) | (((X) & 0x0000FF00) << 8) | (((X) & 0x00FF0000) >> 8) | (((X) & 0xFF000000) >> 24))
48 #elif __BYTE_ORDER == __LITTLE_ENDIAN
49 #define STORE32_LE(X) (X)
50 #else
51 #error unknown endianness!
52 #endif
53
54 ssize_t pread(int fd, void *buf, size_t count, off_t offset);
55 ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
56
57 int
58 trx_fixup(int fd, const char *name)
59 {
60 struct mtd_info_user mtdInfo;
61 unsigned long len;
62 struct trx_header *trx;
63 void *ptr, *scan;
64 int bfd;
65
66 if (ioctl(fd, MEMGETINFO, &mtdInfo) < 0) {
67 fprintf(stderr, "Failed to get mtd info\n");
68 goto err;
69 }
70
71 len = mtdInfo.size;
72 if (mtdInfo.size <= 0) {
73 fprintf(stderr, "Invalid MTD device size\n");
74 goto err;
75 }
76
77 bfd = mtd_open(name, true);
78 ptr = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, bfd, 0);
79 if (!ptr || (ptr == (void *) -1)) {
80 perror("mmap");
81 goto err1;
82 }
83
84 trx = ptr;
85 if (trx->magic != TRX_MAGIC) {
86 fprintf(stderr, "TRX header not found\n");
87 goto err;
88 }
89
90 scan = ptr + offsetof(struct trx_header, flag_version);
91 trx->crc32 = crc32buf(scan, trx->len - (scan - ptr));
92 msync(ptr, sizeof(struct trx_header), MS_SYNC|MS_INVALIDATE);
93 munmap(ptr, len);
94 close(bfd);
95 return 0;
96
97 err1:
98 close(bfd);
99 err:
100 fprintf(stderr, "Error fixing up TRX header\n");
101 return -1;
102 }
103
104 #ifndef target_ar71xx
105 int
106 trx_check(int imagefd, const char *mtd, char *buf, int *len)
107 {
108 const struct trx_header *trx = (const struct trx_header *) buf;
109 int fd;
110
111 if (strcmp(mtd, "firmware") != 0)
112 return 1;
113
114 *len = read(imagefd, buf, 32);
115 if (*len < 32) {
116 fprintf(stdout, "Could not get image header, file too small (%d bytes)\n", *len);
117 return 0;
118 }
119
120 if (trx->magic != TRX_MAGIC || trx->len < sizeof(struct trx_header)) {
121 if (quiet < 2) {
122 fprintf(stderr, "Bad trx header\n");
123 fprintf(stderr, "This is not the correct file format; refusing to flash.\n"
124 "Please specify the correct file or use -f to force.\n");
125 }
126 return 0;
127 }
128
129 /* check if image fits to mtd device */
130 fd = mtd_check_open(mtd);
131 if(fd < 0) {
132 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
133 exit(1);
134 }
135
136 if(mtdsize < trx->len) {
137 fprintf(stderr, "Image too big for partition: %s\n", mtd);
138 close(fd);
139 return 0;
140 }
141
142 close(fd);
143 return 1;
144 }
145 #endif
146
147 int
148 mtd_fixtrx(const char *mtd, size_t offset)
149 {
150 int fd;
151 struct trx_header *trx;
152 char *buf;
153 ssize_t res;
154 size_t block_offset;
155
156 if (quiet < 2)
157 fprintf(stderr, "Trying to fix trx header in %s at 0x%x...\n", mtd, offset);
158
159 fd = mtd_check_open(mtd);
160 if(fd < 0) {
161 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
162 exit(1);
163 }
164
165 block_offset = offset & ~(erasesize - 1);
166 offset -= block_offset;
167
168 if (block_offset + erasesize > mtdsize) {
169 fprintf(stderr, "Offset too large, device size 0x%x\n", mtdsize);
170 exit(1);
171 }
172
173 buf = malloc(erasesize);
174 if (!buf) {
175 perror("malloc");
176 exit(1);
177 }
178
179 res = pread(fd, buf, erasesize, block_offset);
180 if (res != erasesize) {
181 perror("pread");
182 exit(1);
183 }
184
185 trx = (struct trx_header *) (buf + offset);
186 if (trx->magic != STORE32_LE(0x30524448)) {
187 fprintf(stderr, "No trx magic found\n");
188 exit(1);
189 }
190
191 if (trx->len == STORE32_LE(erasesize - offset)) {
192 if (quiet < 2)
193 fprintf(stderr, "Header already fixed, exiting\n");
194 close(fd);
195 return 0;
196 }
197
198 trx->len = STORE32_LE(erasesize - offset);
199
200 trx->crc32 = STORE32_LE(crc32buf((char*) &trx->flag_version, erasesize - offset - 3*4));
201 if (mtd_erase_block(fd, block_offset)) {
202 fprintf(stderr, "Can't erease block at 0x%x (%s)\n", block_offset, strerror(errno));
203 exit(1);
204 }
205
206 if (quiet < 2)
207 fprintf(stderr, "New crc32: 0x%x, rewriting block\n", trx->crc32);
208
209 if (pwrite(fd, buf, erasesize, block_offset) != erasesize) {
210 fprintf(stderr, "Error writing block (%s)\n", strerror(errno));
211 exit(1);
212 }
213
214 if (quiet < 2)
215 fprintf(stderr, "Done.\n");
216
217 close (fd);
218 sync();
219 return 0;
220
221 }
222