2 * Linksys boot counter reset code for mtd
4 * Copyright (C) 2013 Jonas Gorski <jogo@openwrt.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License v2
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 #include <sys/ioctl.h>
33 #include <mtd/mtd-user.h>
37 #define BOOTCOUNT_MAGIC 0x20110811
45 static char page
[2048];
47 int mtd_resetbc(const char *mtd
)
49 struct mtd_info_user mtd_info
;
50 struct bootcounter
*curr
= (struct bootcounter
*)page
;
57 fd
= mtd_check_open(mtd
);
59 if (ioctl(fd
, MEMGETINFO
, &mtd_info
) < 0) {
60 fprintf(stderr
, "failed to get mtd info!\n");
64 num_bc
= mtd_info
.size
/ mtd_info
.writesize
;
66 for (i
= 0; i
< num_bc
; i
++) {
67 pread(fd
, curr
, sizeof(*curr
), i
* mtd_info
.writesize
);
69 if (curr
->magic
!= BOOTCOUNT_MAGIC
&& curr
->magic
!= 0xffffffff) {
70 fprintf(stderr
, "unexpected magic %08x, bailing out\n", curr
->magic
);
74 if (curr
->magic
== 0xffffffff)
77 last_count
= curr
->count
;
80 /* no need to do writes when last boot count is already 0 */
86 struct erase_info_user erase_info
;
88 erase_info
.length
= mtd_info
.size
;
91 ret
= ioctl(fd
, MEMERASE
, &erase_info
);
93 fprintf(stderr
, "failed to erase block: %i\n", ret
);
100 memset(curr
, 0xff, mtd_info
.writesize
);
102 curr
->magic
= BOOTCOUNT_MAGIC
;
104 curr
->checksum
= BOOTCOUNT_MAGIC
;
106 ret
= pwrite(fd
, curr
, mtd_info
.writesize
, i
* mtd_info
.writesize
);
108 fprintf(stderr
, "failed to write: %i\n", ret
);