ar71xx: add kmod-ledtrig-usbdev to the relevant profiles
[openwrt.git] / package / 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-api.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 int
105 trx_check(int imagefd, const char *mtd, char *buf, int *len)
106 {
107 const struct trx_header *trx = (const struct trx_header *) buf;
108 int fd;
109
110 if (strcmp(mtd, "linux") != 0)
111 return 1;
112
113 *len = read(imagefd, buf, 32);
114 if (*len < 32) {
115 fprintf(stdout, "Could not get image header, file too small (%d bytes)\n", *len);
116 return 0;
117 }
118
119 if (trx->magic != TRX_MAGIC || trx->len < sizeof(struct trx_header)) {
120 if (quiet < 2) {
121 fprintf(stderr, "Bad trx header\n");
122 fprintf(stderr, "This is not the correct file format; refusing to flash.\n"
123 "Please specify the correct file or use -f to force.\n");
124 }
125 return 0;
126 }
127
128 /* check if image fits to mtd device */
129 fd = mtd_check_open(mtd);
130 if(fd < 0) {
131 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
132 exit(1);
133 }
134
135 if(mtdsize < trx->len) {
136 fprintf(stderr, "Image too big for partition: %s\n", mtd);
137 close(fd);
138 return 0;
139 }
140
141 close(fd);
142 return 1;
143 }
144
145 int
146 mtd_fixtrx(const char *mtd, size_t offset)
147 {
148 int fd;
149 struct trx_header *trx;
150 char *buf;
151 ssize_t res;
152 size_t block_offset;
153
154 if (quiet < 2)
155 fprintf(stderr, "Trying to fix trx header in %s at 0x%x...\n", mtd, offset);
156
157 block_offset = offset & ~(erasesize - 1);
158 offset -= block_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 if (block_offset + erasesize > mtdsize) {
167 fprintf(stderr, "Offset too large, device size 0x%x\n", mtdsize);
168 exit(1);
169 }
170
171 buf = malloc(erasesize);
172 if (!buf) {
173 perror("malloc");
174 exit(1);
175 }
176
177 res = pread(fd, buf, erasesize, block_offset);
178 if (res != erasesize) {
179 perror("pread");
180 exit(1);
181 }
182
183 trx = (struct trx_header *) (buf + offset);
184 if (trx->magic != STORE32_LE(0x30524448)) {
185 fprintf(stderr, "No trx magic found\n");
186 exit(1);
187 }
188
189 if (trx->len == STORE32_LE(erasesize - offset)) {
190 if (quiet < 2)
191 fprintf(stderr, "Header already fixed, exiting\n");
192 close(fd);
193 return 0;
194 }
195
196 trx->len = STORE32_LE(erasesize - offset);
197
198 trx->crc32 = STORE32_LE(crc32buf((char*) &trx->flag_version, erasesize - offset - 3*4));
199 if (mtd_erase_block(fd, block_offset)) {
200 fprintf(stderr, "Can't erease block at 0x%x (%s)\n", block_offset, strerror(errno));
201 exit(1);
202 }
203
204 if (quiet < 2)
205 fprintf(stderr, "New crc32: 0x%x, rewriting block\n", trx->crc32);
206
207 if (pwrite(fd, buf, erasesize, block_offset) != erasesize) {
208 fprintf(stderr, "Error writing block (%s)\n", strerror(errno));
209 exit(1);
210 }
211
212 if (quiet < 2)
213 fprintf(stderr, "Done.\n");
214
215 close (fd);
216 sync();
217 return 0;
218
219 }
220
This page took 0.055186 seconds and 5 git commands to generate.