mac80211: fix patch naming
[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
31 #include <sys/ioctl.h>
32 #include "mtd-api.h"
33 #include "mtd.h"
34
35 #define TRX_MAGIC 0x30524448 /* "HDR0" */
36 struct trx_header {
37 unsigned magic; /* "HDR0" */
38 unsigned len; /* Length of file including header */
39 unsigned crc32; /* 32-bit CRC from flag_version to end of file */
40 unsigned flag_version; /* 0:15 flags, 16:31 version */
41 unsigned offsets[3]; /* Offsets of partitions from start of header */
42 };
43
44 static unsigned long *crc32 = NULL;
45
46 static void init_crc32()
47 {
48 unsigned long crc;
49 unsigned long poly = 0xEDB88320L;
50 int n, bit;
51
52 if (crc32)
53 return;
54
55 crc32 = (unsigned long *) malloc(256 * sizeof(unsigned long));
56 if (!crc32) {
57 perror("malloc");
58 exit(1);
59 }
60
61 for (n = 0; n < 256; n++) {
62 crc = (unsigned long) n;
63 for (bit = 0; bit < 8; bit++)
64 crc = (crc & 1) ? (poly ^ (crc >> 1)) : (crc >> 1);
65 crc32[n] = crc;
66 }
67 }
68
69 static unsigned int crc32buf(char *buf, size_t len)
70 {
71 unsigned int crc = 0xFFFFFFFF;
72 for (; len; len--, buf++)
73 crc = crc32[(crc ^ *buf) & 0xff] ^ (crc >> 8);
74 return crc;
75 }
76
77 int
78 trx_fixup(int fd, const char *name)
79 {
80 struct mtd_info_user mtdInfo;
81 unsigned long len;
82 struct trx_header *trx;
83 void *ptr, *scan;
84 int bfd;
85
86 if (ioctl(fd, MEMGETINFO, &mtdInfo) < 0) {
87 fprintf(stderr, "Failed to get mtd info\n");
88 goto err;
89 }
90
91 len = mtdInfo.size;
92 if (mtdInfo.size <= 0) {
93 fprintf(stderr, "Invalid MTD device size\n");
94 goto err;
95 }
96
97 bfd = mtd_open(name, true);
98 ptr = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, bfd, 0);
99 if (!ptr || (ptr == (void *) -1)) {
100 perror("mmap");
101 goto err1;
102 }
103
104 trx = ptr;
105 if (trx->magic != TRX_MAGIC) {
106 fprintf(stderr, "TRX header not found\n");
107 goto err;
108 }
109
110 init_crc32();
111 scan = ptr + offsetof(struct trx_header, flag_version);
112 trx->crc32 = crc32buf(scan, trx->len - (scan - ptr));
113 msync(ptr, sizeof(struct trx_header), MS_SYNC|MS_INVALIDATE);
114 munmap(ptr, len);
115 close(bfd);
116 return 0;
117
118 err1:
119 close(bfd);
120 err:
121 fprintf(stderr, "Error fixing up TRX header\n");
122 return -1;
123 }
124
125 int
126 trx_check(int imagefd, const char *mtd, char *buf, int *len)
127 {
128 const struct trx_header *trx = (const struct trx_header *) buf;
129 int fd;
130
131 if (strcmp(mtd, "linux") != 0)
132 return 1;
133
134 *len = read(imagefd, buf, 32);
135 if (*len < 32) {
136 fprintf(stdout, "Could not get image header, file too small (%d bytes)\n", *len);
137 return 0;
138 }
139
140 if (trx->magic != TRX_MAGIC || trx->len < sizeof(struct trx_header)) {
141 if (quiet < 2) {
142 fprintf(stderr, "Bad trx header\n");
143 fprintf(stderr, "This is not the correct file format; refusing to flash.\n"
144 "Please specify the correct file or use -f to force.\n");
145 }
146 return 0;
147 }
148
149 /* check if image fits to mtd device */
150 fd = mtd_check_open(mtd);
151 if(fd < 0) {
152 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
153 exit(1);
154 }
155
156 if(mtdsize < trx->len) {
157 fprintf(stderr, "Image too big for partition: %s\n", mtd);
158 close(fd);
159 return 0;
160 }
161
162 close(fd);
163 return 1;
164 }
165
This page took 0.043973 seconds and 5 git commands to generate.