blob: 593e99f0864777c794167d4215e188b9d5efbb70 [file] [log] [blame]
developerfc05f9d2022-05-06 13:09:53 +08001// SPDX-License-Identifier: GPL-2.0
2
3/*
4 * wrgg.c
5 *
6 * Copyright (C) 2005 Mike Baker
7 * Copyright (C) 2008 Felix Fietkau <nbd@nbd.name>
8 * Copyright (C) 2011-2012 Gabor Juhos <juhosg@openwrt.org>
9 * Copyright (C) 2016 Stijn Tintel <stijn@linux-ipv6.be>
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 */
25
26#include <endian.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <stddef.h>
30#include <unistd.h>
31#include <fcntl.h>
32#include <sys/mman.h>
33#include <sys/stat.h>
34#include <string.h>
35#include <errno.h>
36#include <arpa/inet.h>
37
38#include <sys/ioctl.h>
39#include <mtd/mtd-user.h>
40#include "mtd.h"
41#include "wrgg.h"
42#include "md5.h"
43
44static inline uint32_t le32_to_cpu(uint8_t *buf)
45{
46 return buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24;
47}
48
49ssize_t pread(int fd, void *buf, size_t count, off_t offset);
50ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
51
52int
53wrgg_fix_md5(struct wrgg03_header *shdr, int fd, size_t data_offset, size_t data_size)
54{
55 char *buf;
56 ssize_t res;
57 MD5_CTX ctx;
58 unsigned char digest[16];
59 int i;
60 int err = 0;
61
62 buf = malloc(data_size);
63 if (!buf) {
64 err = -ENOMEM;
65 goto err_out;
66 }
67
68 res = pread(fd, buf, data_size, data_offset);
69 if (res != data_size) {
70 perror("pread");
71 err = -EIO;
72 goto err_free;
73 }
74
75 MD5_Init(&ctx);
76 MD5_Update(&ctx, (char *)&shdr->offset, sizeof(shdr->offset));
77 MD5_Update(&ctx, (char *)&shdr->dev_name, sizeof(shdr->dev_name));
78 MD5_Update(&ctx, buf, data_size);
79 MD5_Final(digest, &ctx);
80
81 if (!memcmp(digest, shdr->digest, 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 = data_size;
97
98 /* update the checksum in the image */
99 memcpy(shdr->digest, digest, sizeof(digest));
100
101err_free:
102 free(buf);
103err_out:
104 return err;
105}
106
107int
108mtd_fixwrgg(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 wrgg03_header *shdr;
116
117 if (quiet < 2)
118 fprintf(stderr, "Trying to fix WRGG 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 wrgg03_header *)(first_block + offset);
149
150 /* The magic is always stored in little-endian byte order */
151 if (le32_to_cpu((uint8_t *)&shdr->magic1) != WRGG03_MAGIC) {
152 fprintf(stderr, "magic1 = %x\n", shdr->magic1);
153 fprintf(stderr, "WRGG03_MAGIC = %x\n", WRGG03_MAGIC);
154 fprintf(stderr, "No WRGG header found\n");
155 exit(1);
156 } else if (!shdr->size) {
157 fprintf(stderr, "WRGG entity with empty image\n");
158 exit(1);
159 }
160
161 data_offset = offset + sizeof(struct wrgg03_header);
162 if (!data_size)
163 data_size = mtdsize - data_offset;
164 if (data_size > shdr->size)
165 data_size = shdr->size;
166 if (wrgg_fix_md5(shdr, fd, data_offset, data_size))
167 goto out;
168
169 if (mtd_erase_block(fd, block_offset)) {
170 fprintf(stderr, "Can't erease block at 0x%x (%s)\n",
171 block_offset, strerror(errno));
172 exit(1);
173 }
174
175 if (quiet < 2)
176 fprintf(stderr, "Rewriting block at 0x%x\n", block_offset);
177
178 if (pwrite(fd, first_block, erasesize, block_offset) != erasesize) {
179 fprintf(stderr, "Error writing block (%s)\n", strerror(errno));
180 exit(1);
181 }
182
183 if (quiet < 2)
184 fprintf(stderr, "Done.\n");
185
186out:
187 close (fd);
188 sync();
189
190 return 0;
191}