mvebu: add support for the Linksys boot counter (backport of r46690)
authorImre Kaloz <kaloz@openwrt.org>
Wed, 19 Aug 2015 08:35:41 +0000 (08:35 +0000)
committerImre Kaloz <kaloz@openwrt.org>
Wed, 19 Aug 2015 08:35:41 +0000 (08:35 +0000)
The u-boot boot counter was never reset after a successful boot,
which sometimes could make some variables become out of sync.
This patch adds support for the boot counter and enables
auto_recovery unconditionally.

Signed-off-by: Jonas Gorski <jogo@openwrt.org>
Signed-off-by: Rob Mosher <nyt-openwrt@countercultured.net>
Signed-off-by: Imre Kaloz <kaloz@openwrt.org>
SVN-Revision: 46691

package/system/mtd/Makefile
package/system/mtd/src/Makefile
package/system/mtd/src/linksys_bootcount.c [new file with mode: 0644]
package/system/mtd/src/mtd.c
package/system/mtd/src/mtd.h
target/linux/mvebu/base-files/etc/init.d/linksys_recovery [new file with mode: 0755]
target/linux/mvebu/base-files/etc/init.d/u-boot_env [deleted file]

index 8d7bb44aa0ae094015bf56fe300335d7c8ff6fbf..ae1922fbd9b310a227088fe7f4d537533a6703ac 100644 (file)
@@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk
 include $(INCLUDE_DIR)/kernel.mk
 
 PKG_NAME:=mtd
-PKG_RELEASE:=20
+PKG_RELEASE:=21
 
 PKG_BUILD_DIR := $(KERNEL_BUILD_DIR)/$(PKG_NAME)
 STAMP_PREPARED := $(STAMP_PREPARED)_$(call confvar,CONFIG_MTD_REDBOOT_PARTS)
index 27ac339005fa7ec3c5bd8127f314d39e489dc804..40a165e4e64dbd760396cee8d57bd3439d019bc2 100644 (file)
@@ -10,6 +10,7 @@ obj.brcm47xx = $(obj.brcm)
 obj.bcm53xx = $(obj.brcm)
 obj.brcm63xx = imagetag.o
 obj.ramips = $(obj.seama)
+obj.mvebu = linksys_bootcount.o
 
 ifdef FIS_SUPPORT
   obj += fis.o
diff --git a/package/system/mtd/src/linksys_bootcount.c b/package/system/mtd/src/linksys_bootcount.c
new file mode 100644 (file)
index 0000000..95f75fe
--- /dev/null
@@ -0,0 +1,114 @@
+/*
+ * Linksys boot counter reset code for mtd
+ *
+ * Copyright (C) 2013 Jonas Gorski <jogo@openwrt.org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License v2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <endian.h>
+#include <string.h>
+#include <errno.h>
+
+#include <sys/ioctl.h>
+#include <mtd/mtd-user.h>
+
+#include "mtd.h"
+
+#define BOOTCOUNT_MAGIC        0x20110811
+
+struct bootcounter {
+       uint32_t magic;
+       uint32_t count;
+       uint32_t checksum;
+};
+
+static char page[2048];
+
+int mtd_resetbc(const char *mtd)
+{
+       struct mtd_info_user mtd_info;
+       struct bootcounter *curr = (struct bootcounter *)page;
+       unsigned int i;
+       int last_count = 0;
+       int num_bc;
+       int fd;
+       int ret;
+
+       fd = mtd_check_open(mtd);
+
+       if (ioctl(fd, MEMGETINFO, &mtd_info) < 0) {
+               fprintf(stderr, "failed to get mtd info!\n");
+               return -1;
+       }
+
+       num_bc = mtd_info.size / mtd_info.writesize;
+
+       for (i = 0; i < num_bc; i++) {
+               pread(fd, curr, sizeof(*curr), i * mtd_info.writesize);
+
+               if (curr->magic != BOOTCOUNT_MAGIC && curr->magic != 0xffffffff) {
+                       fprintf(stderr, "unexpected magic %08x, bailing out\n", curr->magic);
+                       goto out;
+               }
+
+               if (curr->magic == 0xffffffff)
+                       break;
+
+               last_count = curr->count;
+       }
+
+       /* no need to do writes when last boot count is already 0 */
+       if (last_count == 0)
+               goto out;
+
+
+       if (i == num_bc) {
+               struct erase_info_user erase_info;
+               erase_info.start = 0;
+               erase_info.length = mtd_info.size;
+
+               /* erase block */
+               ret = ioctl(fd, MEMERASE, &erase_info);
+               if (ret < 0) {
+                       fprintf(stderr, "failed to erase block: %i\n", ret);
+                       return -1;
+               }
+
+               i = 0;
+       }
+
+       memset(curr, 0xff, mtd_info.writesize);
+
+       curr->magic = BOOTCOUNT_MAGIC;
+       curr->count = 0;
+       curr->checksum = BOOTCOUNT_MAGIC;
+
+       ret = pwrite(fd, curr, mtd_info.writesize, i * mtd_info.writesize);
+       if (ret < 0)
+               fprintf(stderr, "failed to write: %i\n", ret);
+       sync();
+out:
+       close(fd);
+
+       return 0;
+}
index 741b57b48070d28d093fa5fc843d0ca84b3eb3b7..02476306f65ef04dd77dcd809667cc32501dc2cc 100644 (file)
@@ -640,6 +640,10 @@ static void usage(void)
        "        verify <imagefile>|-    verify <imagefile> (use - for stdin) to device\n"
        "        write <imagefile>|-     write <imagefile> (use - for stdin) to device\n"
        "        jffs2write <file>       append <file> to the jffs2 partition on the device\n");
+       if (mtd_resetbc) {
+           fprintf(stderr,
+       "        resetbc <device>        reset the uboot boot counter\n");
+       }
        if (mtd_fixtrx) {
            fprintf(stderr,
        "        fixtrx                  fix the checksum in a trx header on first boot\n");
@@ -706,6 +710,7 @@ int main (int argc, char **argv)
                CMD_FIXSEAMA,
                CMD_VERIFY,
                CMD_DUMP,
+               CMD_RESETBC,
        } cmd = -1;
 
        erase[0] = NULL;
@@ -800,6 +805,9 @@ int main (int argc, char **argv)
        } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
                cmd = CMD_ERASE;
                device = argv[1];
+       } else if (((strcmp(argv[0], "resetbc") == 0) && (argc == 2)) && mtd_resetbc) {
+               cmd = CMD_RESETBC;
+               device = argv[1];
        } else if (((strcmp(argv[0], "fixtrx") == 0) && (argc == 2)) && mtd_fixtrx) {
                cmd = CMD_FIXTRX;
                device = argv[1];
@@ -891,6 +899,10 @@ int main (int argc, char **argv)
                case CMD_FIXTRX:
                    if (mtd_fixtrx) {
                            mtd_fixtrx(device, offset);
+            }
+               case CMD_RESETBC:
+                   if (mtd_resetbc) {
+                           mtd_resetbc(device);
             }
                case CMD_FIXSEAMA:
                        if (mtd_fixseama)
index d94f394396c4943f97bd08d7d9d406520a2b4165..fb37b8b9fa6bf7e99eea1b8356c9d627f07eb1c4 100644 (file)
@@ -27,4 +27,5 @@ extern int trx_fixup(int fd, const char *name)  __attribute__ ((weak));
 extern int trx_check(int imagefd, const char *mtd, char *buf, int *len) __attribute__ ((weak));
 extern int mtd_fixtrx(const char *mtd, size_t offset) __attribute__ ((weak));
 extern int mtd_fixseama(const char *mtd, size_t offset) __attribute__ ((weak));
+extern int mtd_resetbc(const char *mtd) __attribute__ ((weak));
 #endif /* __mtd_h */
diff --git a/target/linux/mvebu/base-files/etc/init.d/linksys_recovery b/target/linux/mvebu/base-files/etc/init.d/linksys_recovery
new file mode 100755 (executable)
index 0000000..60d7be0
--- /dev/null
@@ -0,0 +1,20 @@
+#!/bin/sh /etc/rc.common
+# Copyright (C) 2015 OpenWrt.org
+
+START=97
+boot() {
+. /lib/functions.sh
+. /lib/mvebu.sh
+
+case $(mvebu_board_name) in
+       armada-385-linksys-caiman|armada-385-linksys-cobra|armada-xp-linksys-mamba)
+               # make sure auto_recovery in uboot is always on
+               AUTO_RECOVERY_ENA="`fw_printenv -n auto_recovery`"
+               if [ "$AUTO_RECOVERY_ENA" != "yes" ] ; then
+                       fw_setenv auto_recovery yes
+               fi
+               # reset the boot counter
+               mtd resetbc s_env
+               ;;
+esac
+}
diff --git a/target/linux/mvebu/base-files/etc/init.d/u-boot_env b/target/linux/mvebu/base-files/etc/init.d/u-boot_env
deleted file mode 100755 (executable)
index 82f36cb..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-#!/bin/sh /etc/rc.common
-# Copyright (C) 2015 OpenWrt.org
-
-START=97
-boot() {
-. /lib/functions.sh
-. /lib/mvebu.sh
-
-case $(mvebu_board_name) in
-       armada-385-linksys-caiman|armada-385-linksys-cobra|armada-xp-linksys-mamba)
-               fw_setenv auto_recovery off
-               ;;
-esac
-}