2 * Linksys boot counter reset code for mtd
4 * Copyright (C) 2013 Jonas Gorski <jogo@openwrt.org>
5 * Portions Copyright (c) 2019, Jeff Kletsky
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.
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.
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.
35 #include <sys/ioctl.h>
36 #include <mtd/mtd-user.h>
40 #define BOOTCOUNT_MAGIC 0x20110811
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).
48 #define BC_OFFSET_INCREMENT_MIN 16
54 #define DLOG_ERR(...) do { \
55 fprintf(stderr, "ERROR: " __VA_ARGS__); fprintf(stderr, "\n"); \
58 #define DLOG_NOTICE(...) do { \
59 fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n"); \
62 #define DLOG_DEBUG(...)
72 static char page
[2048];
74 int mtd_resetbc(const char *mtd
)
76 struct mtd_info_user mtd_info
;
77 struct bootcounter
*curr
= (struct bootcounter
*)page
;
79 unsigned int bc_offset_increment
;
88 fd
= mtd_check_open(mtd
);
90 if (ioctl(fd
, MEMGETINFO
, &mtd_info
) < 0) {
91 DLOG_ERR("Unable to obtain mtd_info for given partition name.");
98 /* Detect need to override increment (for EA6350v3) */
100 if (mtd_info
.writesize
< BC_OFFSET_INCREMENT_MIN
) {
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
);
107 bc_offset_increment
= mtd_info
.writesize
;
110 num_bc
= mtd_info
.size
/ bc_offset_increment
;
112 for (i
= 0; i
< num_bc
; i
++) {
113 pread(fd
, curr
, sizeof(*curr
), i
* bc_offset_increment
);
115 /* Existing code assumes erase is to 0xff; left as-is (2019) */
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
);
126 if (curr
->magic
== 0xffffffff)
129 last_count
= curr
->count
;
133 if (last_count
== 0) { /* bootcount is already 0 */
141 DLOG_NOTICE("Boot-count log full with %i entries; erasing (expected occasionally).",
144 struct erase_info_user erase_info
;
145 erase_info
.start
= 0;
146 erase_info
.length
= mtd_info
.size
;
148 ret
= ioctl(fd
, MEMERASE
, &erase_info
);
150 DLOG_ERR("Failed to erase boot-count log MTD; ioctl() MEMERASE returned %i",
160 memset(curr
, 0xff, bc_offset_increment
);
162 curr
->magic
= BOOTCOUNT_MAGIC
;
164 curr
->checksum
= BOOTCOUNT_MAGIC
;
166 /* Assumes bc_offset_increment is a multiple of mtd_info.writesize */
168 ret
= pwrite(fd
, curr
, bc_offset_increment
, i
* bc_offset_increment
);
170 DLOG_ERR("Failed to write boot-count log entry; pwrite() returned %i",
178 DLOG_NOTICE("Boot count sucessfully reset to zero.");