First git repo commit for the libreCMC project
[librecmc/librecmc.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@nbd.name>
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 <endian.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stddef.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <sys/mman.h>
32 #include <sys/stat.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <arpa/inet.h>
36
37 #include <sys/ioctl.h>
38 #include <mtd/mtd-user.h>
39 #include "mtd.h"
40 #include "seama.h"
41 #include "md5.h"
42
43 #if __BYTE_ORDER == __BIG_ENDIAN
44 #define STORE32_LE(X)           ((((X) & 0x000000FF) << 24) | (((X) & 0x0000FF00) << 8) | (((X) & 0x00FF0000) >> 8) | (((X) & 0xFF000000) >> 24))
45 #elif __BYTE_ORDER == __LITTLE_ENDIAN
46 #define STORE32_LE(X)           (X)
47 #else
48 #error unknown endianness!
49 #endif
50
51 ssize_t pread(int fd, void *buf, size_t count, off_t offset);
52 ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
53
54 int
55 seama_fix_md5(struct seama_entity_header *shdr, int fd, size_t data_offset, size_t data_size)
56 {
57         char *buf;
58         ssize_t res;
59         MD5_CTX ctx;
60         unsigned char digest[16];
61         int i;
62         int err = 0;
63
64         buf = malloc(data_size);
65         if (!buf) {
66                 err = -ENOMEM;
67                 goto err_out;
68         }
69
70         res = pread(fd, buf, data_size, data_offset);
71         if (res != data_size) {
72                 perror("pread");
73                 err = -EIO;
74                 goto err_free;
75         }
76
77         MD5_Init(&ctx);
78         MD5_Update(&ctx, buf, data_size);
79         MD5_Final(digest, &ctx);
80
81         if (!memcmp(digest, shdr->md5, sizeof(digest))) {
82                 if (quiet < 2)
83                         fprintf(stderr, "the header is fixed already\n");
84                 return -1;
85         }
86
87         if (quiet < 2) {
88                 fprintf(stderr, "new size:%u, new MD5: ", data_size);
89                 for (i = 0; i < sizeof(digest); i++)
90                         fprintf(stderr, "%02x", digest[i]);
91
92                 fprintf(stderr, "\n");
93         }
94
95         /* update the size in the image */
96         shdr->size = htonl(data_size);
97
98         /* update the checksum in the image */
99         memcpy(shdr->md5, digest, sizeof(digest));
100
101 err_free:
102         free(buf);
103 err_out:
104         return err;
105 }
106
107 int
108 mtd_fixseama(const char *mtd, size_t offset, size_t data_size)
109 {
110         int fd;
111         char *first_block;
112         ssize_t res;
113         size_t block_offset;
114         size_t data_offset;
115         struct seama_entity_header *shdr;
116
117         if (quiet < 2)
118                 fprintf(stderr, "Trying to fix SEAMA header in %s at 0x%x...\n",
119                         mtd, offset);
120
121         block_offset = offset & ~(erasesize - 1);
122         offset -= block_offset;
123
124         fd = mtd_check_open(mtd);
125         if(fd < 0) {
126                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
127                 exit(1);
128         }
129
130         if (block_offset + erasesize > mtdsize) {
131                 fprintf(stderr, "Offset too large, device size 0x%x\n",
132                         mtdsize);
133                 exit(1);
134         }
135
136         first_block = malloc(erasesize);
137         if (!first_block) {
138                 perror("malloc");
139                 exit(1);
140         }
141
142         res = pread(fd, first_block, erasesize, block_offset);
143         if (res != erasesize) {
144                 perror("pread");
145                 exit(1);
146         }
147
148         shdr = (struct seama_entity_header *)(first_block + offset);
149         if (shdr->magic != htonl(SEAMA_MAGIC)) {
150                 fprintf(stderr, "No SEAMA header found\n");
151                 exit(1);
152         } else if (!ntohl(shdr->size)) {
153                 fprintf(stderr, "Seama entity with empty image\n");
154                 exit(1);
155         }
156
157         data_offset = offset + sizeof(struct seama_entity_header) + ntohs(shdr->metasize);
158         if (!data_size)
159                 data_size = mtdsize - data_offset;
160         if (data_size > ntohl(shdr->size))
161                 data_size = ntohl(shdr->size);
162         if (seama_fix_md5(shdr, fd, data_offset, data_size))
163                 goto out;
164
165         if (mtd_erase_block(fd, block_offset)) {
166                 fprintf(stderr, "Can't erease block at 0x%x (%s)\n",
167                         block_offset, strerror(errno));
168                 exit(1);
169         }
170
171         if (quiet < 2)
172                 fprintf(stderr, "Rewriting block at 0x%x\n", block_offset);
173
174         if (pwrite(fd, first_block, erasesize, block_offset) != erasesize) {
175                 fprintf(stderr, "Error writing block (%s)\n", strerror(errno));
176                 exit(1);
177         }
178
179         if (quiet < 2)
180                 fprintf(stderr, "Done.\n");
181
182 out:
183         close (fd);
184         sync();
185
186         return 0;
187 }
188