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