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.
33 #include <sys/ioctl.h>
34 #include <mtd/mtd-user.h>
38 #define BOOTCOUNT_MAGIC 0x20110811
46 static char page
[2048];
48 int mtd_resetbc(const char *mtd
)
50 struct mtd_info_user mtd_info
;
51 struct bootcounter
*curr
= (struct bootcounter
*)page
;
58 fd
= mtd_check_open(mtd
);
60 if (ioctl(fd
, MEMGETINFO
, &mtd_info
) < 0) {
61 fprintf(stderr
, "failed to get mtd info!\n");
65 num_bc
= mtd_info
.size
/ 16;
67 for (i
= 0; i
< num_bc
; i
++) {
68 pread(fd
, curr
, sizeof(*curr
), i
* 16);
70 if (curr
->magic
!= (BOOTCOUNT_MAGIC
) && curr
->magic
!= 0xffffffff) {
71 fprintf(stderr
, "unexpected magic %08x, bailing out\n", curr
->magic
);
75 if (curr
->magic
== 0xffffffff)
78 last_count
= curr
->count
;
81 /* no need to do writes when last boot count is already 0 */
87 struct erase_info_user erase_info
;
89 erase_info
.length
= mtd_info
.size
;
92 ret
= ioctl(fd
, MEMERASE
, &erase_info
);
94 fprintf(stderr
, "failed to erase block: %i\n", ret
);
101 memset(curr
, 0xff, 16);
103 curr
->magic
= BOOTCOUNT_MAGIC
;
105 curr
->checksum
= BOOTCOUNT_MAGIC
;
107 ret
= pwrite(fd
, curr
, 16, i
* 16);
109 fprintf(stderr
, "failed to write: %i\n", ret
);