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