8894883c26680d42adb5f67563342e80b6ecdcdb
[openwrt.git] / package / system / mtd / src / seama.c
1 /*
2  * seama.c
3  *
4  * Copyright (C) 2011-2012 Gabor Juhos <juhosg@openwrt.org>
5  *
6  * Based on the trx fixup code:
7  *   Copyright (C) 2005 Mike Baker
8  *   Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stddef.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <sys/mman.h>
31 #include <sys/stat.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <arpa/inet.h>
35
36 #include <sys/ioctl.h>
37 #include <mtd/mtd-user.h>
38 #include "mtd.h"
39 #include "seama.h"
40 #include "md5.h"
41
42 #if __BYTE_ORDER == __BIG_ENDIAN
43 #define STORE32_LE(X)           ((((X) & 0x000000FF) << 24) | (((X) & 0x0000FF00) << 8) | (((X) & 0x00FF0000) >> 8) | (((X) & 0xFF000000) >> 24))
44 #elif __BYTE_ORDER == __LITTLE_ENDIAN
45 #define STORE32_LE(X)           (X)
46 #else
47 #error unknown endianness!
48 #endif
49
50 ssize_t pread(int fd, void *buf, size_t count, off_t offset);
51 ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
52
53 int
54 seama_fix_md5(char *buf, size_t len)
55 {
56         struct seama_hdr *shdr = (struct seama_hdr *) buf;
57         char *data;
58         size_t msize;
59         size_t isize;
60         MD5_CTX ctx;
61         unsigned char digest[16];
62         int i;
63
64         if (len < sizeof(struct seama_hdr))
65                 return -1;
66
67         isize = ntohl(shdr->size);
68         msize = ntohs(shdr->metasize);
69         if (isize == 0) {
70                 /* the image contains no checksum */
71                 return -1;
72         }
73
74         len -= sizeof(struct seama_hdr) + sizeof(digest) + msize;
75         if (isize > len)
76                 isize = len;
77
78         data = buf + sizeof(struct seama_hdr) + sizeof(digest) + msize;
79
80         MD5_Init(&ctx);
81         MD5_Update(&ctx, data, isize);
82         MD5_Final(digest, &ctx);
83
84         if (!memcmp(digest, &buf[sizeof(struct seama_hdr)], sizeof(digest))) {
85                 if (quiet < 2)
86                         fprintf(stderr, "the header is fixed already\n");
87                 return -1;
88         }
89
90         if (quiet < 2) {
91                 fprintf(stderr, "new size:%u, new MD5: ", isize);
92                 for (i = 0; i < sizeof(digest); i++)
93                         fprintf(stderr, "%02x", digest[i]);
94
95                 fprintf(stderr, "\n");
96         }
97
98         /* update the size in the image */
99         shdr->size = htonl(isize);
100
101         /* update the checksum in the image */
102         for (i = 0; i < sizeof(digest); i++)
103                 buf[sizeof(struct seama_hdr) + i] = digest[i];
104
105         return 0;
106 }
107
108 int
109 mtd_fixseama(const char *mtd, size_t offset)
110 {
111         int fd;
112         char *first_block;
113         char *buf;
114         ssize_t res;
115         size_t block_offset;
116         struct seama_hdr *shdr;
117
118         if (quiet < 2)
119                 fprintf(stderr, "Trying to fix SEAMA header in %s at 0x%x...\n",
120                         mtd, offset);
121
122         block_offset = offset & ~(erasesize - 1);
123         offset -= block_offset;
124
125         fd = mtd_check_open(mtd);
126         if(fd < 0) {
127                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
128                 exit(1);
129         }
130
131         if (block_offset + erasesize > mtdsize) {
132                 fprintf(stderr, "Offset too large, device size 0x%x\n",
133                         mtdsize);
134                 exit(1);
135         }
136
137         first_block = malloc(erasesize);
138         if (!first_block) {
139                 perror("malloc");
140                 exit(1);
141         }
142
143         res = pread(fd, first_block, erasesize, block_offset);
144         if (res != erasesize) {
145                 perror("pread");
146                 exit(1);
147         }
148
149         shdr = (struct seama_hdr *)first_block;
150         if (shdr->magic != htonl(SEAMA_MAGIC)) {
151                 fprintf(stderr, "No SEAMA header found\n");
152                 return -1;
153         }
154
155         buf = malloc(mtdsize);
156         if (!buf) {
157                 perror("malloc");
158                 exit(1);
159         }
160
161         res = pread(fd, buf, mtdsize, block_offset);
162         if (res != mtdsize) {
163                 perror("pread");
164                 exit(1);
165         }
166
167         if (seama_fix_md5(buf, mtdsize))
168                 goto out;
169
170         if (mtd_erase_block(fd, block_offset)) {
171                 fprintf(stderr, "Can't erease block at 0x%x (%s)\n",
172                         block_offset, strerror(errno));
173                 exit(1);
174         }
175
176         if (quiet < 2)
177                 fprintf(stderr, "Rewriting block at 0x%x\n", block_offset);
178
179         if (pwrite(fd, buf, erasesize, block_offset) != erasesize) {
180                 fprintf(stderr, "Error writing block (%s)\n", strerror(errno));
181                 exit(1);
182         }
183
184         if (quiet < 2)
185                 fprintf(stderr, "Done.\n");
186
187 out:
188         close (fd);
189         sync();
190
191         return 0;
192 }
193