mtd: seama: move buf allocation to the MD5 function
[15.05/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(struct seama_entity_header *shdr, int fd, size_t len, size_t block_offset)
55 {
56         char *buf;
57         char *data;
58         ssize_t res;
59         size_t msize;
60         size_t isize;
61         MD5_CTX ctx;
62         unsigned char digest[16];
63         int i;
64         int err = 0;
65
66         if (len < sizeof(struct seama_entity_header))
67                 return -1;
68
69         buf = malloc(len);
70         if (!buf) {
71                 err = -ENOMEM;
72                 goto err_out;
73         }
74
75         res = pread(fd, buf, len, block_offset);
76         if (res != len) {
77                 perror("pread");
78                 err = -EIO;
79                 goto err_free;
80         }
81
82         isize = ntohl(shdr->size);
83         msize = ntohs(shdr->metasize);
84         if (isize == 0) {
85                 /* the image contains no checksum */
86                 return -1;
87         }
88
89         len -= sizeof(struct seama_entity_header) + msize;
90         if (isize > len)
91                 isize = len;
92
93         data = buf + sizeof(struct seama_entity_header) + msize;
94
95         MD5_Init(&ctx);
96         MD5_Update(&ctx, data, isize);
97         MD5_Final(digest, &ctx);
98
99         if (!memcmp(digest, shdr->md5, sizeof(digest))) {
100                 if (quiet < 2)
101                         fprintf(stderr, "the header is fixed already\n");
102                 return -1;
103         }
104
105         if (quiet < 2) {
106                 fprintf(stderr, "new size:%u, new MD5: ", isize);
107                 for (i = 0; i < sizeof(digest); i++)
108                         fprintf(stderr, "%02x", digest[i]);
109
110                 fprintf(stderr, "\n");
111         }
112
113         /* update the size in the image */
114         shdr->size = htonl(isize);
115
116         /* update the checksum in the image */
117         memcpy(shdr->md5, digest, sizeof(digest));
118
119 err_free:
120         free(buf);
121 err_out:
122         return err;
123 }
124
125 int
126 mtd_fixseama(const char *mtd, size_t offset)
127 {
128         int fd;
129         char *first_block;
130         ssize_t res;
131         size_t block_offset;
132         struct seama_entity_header *shdr;
133
134         if (quiet < 2)
135                 fprintf(stderr, "Trying to fix SEAMA header in %s at 0x%x...\n",
136                         mtd, offset);
137
138         block_offset = offset & ~(erasesize - 1);
139         offset -= block_offset;
140
141         fd = mtd_check_open(mtd);
142         if(fd < 0) {
143                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
144                 exit(1);
145         }
146
147         if (block_offset + erasesize > mtdsize) {
148                 fprintf(stderr, "Offset too large, device size 0x%x\n",
149                         mtdsize);
150                 exit(1);
151         }
152
153         first_block = malloc(erasesize);
154         if (!first_block) {
155                 perror("malloc");
156                 exit(1);
157         }
158
159         res = pread(fd, first_block, erasesize, block_offset);
160         if (res != erasesize) {
161                 perror("pread");
162                 exit(1);
163         }
164
165         shdr = (struct seama_entity_header *)first_block;
166         if (shdr->magic != htonl(SEAMA_MAGIC)) {
167                 fprintf(stderr, "No SEAMA header found\n");
168                 return -1;
169         }
170
171         if (seama_fix_md5(shdr, fd, mtdsize, block_offset))
172                 goto out;
173
174         if (mtd_erase_block(fd, block_offset)) {
175                 fprintf(stderr, "Can't erease block at 0x%x (%s)\n",
176                         block_offset, strerror(errno));
177                 exit(1);
178         }
179
180         if (quiet < 2)
181                 fprintf(stderr, "Rewriting block at 0x%x\n", block_offset);
182
183         if (pwrite(fd, first_block, erasesize, block_offset) != erasesize) {
184                 fprintf(stderr, "Error writing block (%s)\n", strerror(errno));
185                 exit(1);
186         }
187
188         if (quiet < 2)
189                 fprintf(stderr, "Done.\n");
190
191 out:
192         close (fd);
193         sync();
194
195         return 0;
196 }
197