mtd: base-files: Unify dual-firmware devices (Linksys)
[openwrt/staging/dedeckeh.git] / package / system / mtd / src / linksys_bootcount.c
1 /*
2 * Linksys boot counter reset code for mtd
3 *
4 * Copyright (C) 2013 Jonas Gorski <jogo@openwrt.org>
5 * Portions Copyright (c) 2019, Jeff Kletsky
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 v2
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
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 <endian.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <stdint.h>
33 #include <syslog.h>
34
35 #include <sys/ioctl.h>
36 #include <mtd/mtd-user.h>
37
38 #include "mtd.h"
39
40 #define BOOTCOUNT_MAGIC 0x20110811
41
42 /*
43 * EA6350v3, and potentially other NOR-boot devices,
44 * use an offset increment of 16 between records,
45 * not mtd_info_user.writesize (often 1 on NOR devices).
46 */
47
48 #define BC_OFFSET_INCREMENT_MIN 16
49
50
51
52 #define DLOG_OPEN()
53
54 #define DLOG_ERR(...) do { \
55 fprintf(stderr, "ERROR: " __VA_ARGS__); fprintf(stderr, "\n"); \
56 } while (0)
57
58 #define DLOG_NOTICE(...) do { \
59 fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n"); \
60 } while (0)
61
62 #define DLOG_DEBUG(...)
63
64
65
66 struct bootcounter {
67 uint32_t magic;
68 uint32_t count;
69 uint32_t checksum;
70 };
71
72 static char page[2048];
73
74 int mtd_resetbc(const char *mtd)
75 {
76 struct mtd_info_user mtd_info;
77 struct bootcounter *curr = (struct bootcounter *)page;
78 unsigned int i;
79 unsigned int bc_offset_increment;
80 int last_count = 0;
81 int num_bc;
82 int fd;
83 int ret;
84 int retval = 0;
85
86 DLOG_OPEN();
87
88 fd = mtd_check_open(mtd);
89
90 if (ioctl(fd, MEMGETINFO, &mtd_info) < 0) {
91 DLOG_ERR("Unable to obtain mtd_info for given partition name.");
92
93 retval = -1;
94 goto out;
95 }
96
97
98 /* Detect need to override increment (for EA6350v3) */
99
100 if (mtd_info.writesize < BC_OFFSET_INCREMENT_MIN) {
101
102 bc_offset_increment = BC_OFFSET_INCREMENT_MIN;
103 DLOG_DEBUG("Offset increment set to %i for writesize of %i",
104 bc_offset_increment, mtd_info.writesize);
105 } else {
106
107 bc_offset_increment = mtd_info.writesize;
108 }
109
110 num_bc = mtd_info.size / bc_offset_increment;
111
112 for (i = 0; i < num_bc; i++) {
113 pread(fd, curr, sizeof(*curr), i * bc_offset_increment);
114
115 /* Existing code assumes erase is to 0xff; left as-is (2019) */
116
117 if (curr->magic != BOOTCOUNT_MAGIC &&
118 curr->magic != 0xffffffff) {
119 DLOG_ERR("Unexpected magic %08x at offset %08x; aborting.",
120 curr->magic, i * bc_offset_increment);
121
122 retval = -2;
123 goto out;
124 }
125
126 if (curr->magic == 0xffffffff)
127 break;
128
129 last_count = curr->count;
130 }
131
132
133 if (last_count == 0) { /* bootcount is already 0 */
134
135 retval = 0;
136 goto out;
137 }
138
139
140 if (i == num_bc) {
141 DLOG_NOTICE("Boot-count log full with %i entries; erasing (expected occasionally).",
142 i);
143
144 struct erase_info_user erase_info;
145 erase_info.start = 0;
146 erase_info.length = mtd_info.size;
147
148 ret = ioctl(fd, MEMERASE, &erase_info);
149 if (ret < 0) {
150 DLOG_ERR("Failed to erase boot-count log MTD; ioctl() MEMERASE returned %i",
151 ret);
152
153 retval = -3;
154 goto out;
155 }
156
157 i = 0;
158 }
159
160 memset(curr, 0xff, bc_offset_increment);
161
162 curr->magic = BOOTCOUNT_MAGIC;
163 curr->count = 0;
164 curr->checksum = BOOTCOUNT_MAGIC;
165
166 /* Assumes bc_offset_increment is a multiple of mtd_info.writesize */
167
168 ret = pwrite(fd, curr, bc_offset_increment, i * bc_offset_increment);
169 if (ret < 0) {
170 DLOG_ERR("Failed to write boot-count log entry; pwrite() returned %i",
171 errno);
172 retval = -4;
173 goto out;
174
175 } else {
176 sync();
177
178 DLOG_NOTICE("Boot count sucessfully reset to zero.");
179
180 retval = 0;
181 goto out;
182 }
183
184 out:
185 close(fd);
186 return retval;
187 }