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