From patchwork Tue Aug 18 14:21:13 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: nyt-openwrt@countercultured.net X-Patchwork-Id: 508336 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from arrakis.dune.hu (arrakis.dune.hu [78.24.191.176]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 2046D14075D for ; Wed, 19 Aug 2015 00:21:45 +1000 (AEST) Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id 2B4AA28C5FB; Tue, 18 Aug 2015 16:20:43 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on arrakis.dune.hu X-Spam-Level: X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00, T_RP_MATCHES_RCVD autolearn=unavailable version=3.3.2 Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id D94FB28C5FB for ; Tue, 18 Aug 2015 16:20:39 +0200 (CEST) X-policyd-weight: using cached result; rate: -7.6 Received: from countercultured.net (countercultured.net [209.51.175.25]) by arrakis.dune.hu (Postfix) with SMTP for ; Tue, 18 Aug 2015 16:20:35 +0200 (CEST) Received: (qmail 23055 invoked by uid 1000); 18 Aug 2015 14:21:13 -0000 From: Rob Mosher To: kaloz@openwrt.org Date: Tue, 18 Aug 2015 10:21:13 -0400 Message-Id: <1439907673-23019-1-git-send-email-nyt-openwrt@countercultured.net> X-Mailer: git-send-email 2.1.4 Cc: Rob Mosher , openwrt-devel@lists.openwrt.org Subject: [OpenWrt-Devel] [PATCH] Fix for mvebu (WRT1900AC/WRT1200AC/etc) boot counter X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: openwrt-devel-bounces@lists.openwrt.org Sender: "openwrt-devel" The uboot boot counter was never reset after a successful boot. This patch prevents bootcmd and boot_part from becoming out of sync after a sysupgrade and ensures the proper partition is booted. Signed-off-by: Rob Mosher --- package/system/mtd/Makefile | 2 +- package/system/mtd/src/Makefile | 1 + package/system/mtd/src/mvebu.c | 100 +++++++++++++++++++++ .../linux/mvebu/base-files/etc/init.d/u-boot_env | 1 + 4 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 package/system/mtd/src/mvebu.c diff --git a/package/system/mtd/Makefile b/package/system/mtd/Makefile index 8d7bb44..ae1922f 100644 --- a/package/system/mtd/Makefile +++ b/package/system/mtd/Makefile @@ -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) diff --git a/package/system/mtd/src/Makefile b/package/system/mtd/src/Makefile index 27ac339..3424dd2 100644 --- a/package/system/mtd/src/Makefile +++ b/package/system/mtd/src/Makefile @@ -10,6 +10,7 @@ obj.brcm47xx = $(obj.brcm) obj.bcm53xx = $(obj.brcm) obj.brcm63xx = imagetag.o obj.ramips = $(obj.seama) +obj.mvebu = mvebu.c ifdef FIS_SUPPORT obj += fis.o diff --git a/package/system/mtd/src/mvebu.c b/package/system/mtd/src/mvebu.c new file mode 100644 index 0000000..0929a97 --- /dev/null +++ b/package/system/mtd/src/mvebu.c @@ -0,0 +1,100 @@ +/* + * GPL Header ... + */ + + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "mtd.h" + +#define BOOTCOUNT_MAGIC 0x20110811 + +struct bootcounter { + uint32_t magic; + uint32_t count; + uint32_t checksum; +}; + +static char page[2048]; + +int mtd_fixtrx(const char *mtd, size_t offset) +{ + 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!"); + 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) { + printf("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) { + printf("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) + printf("failed to write: %i\n", ret); + sync(); +out: + close(fd); + + return 0; +} 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 index 82f36cb..2869dd6 100755 --- a/target/linux/mvebu/base-files/etc/init.d/u-boot_env +++ b/target/linux/mvebu/base-files/etc/init.d/u-boot_env @@ -9,6 +9,7 @@ boot() { case $(mvebu_board_name) in armada-385-linksys-caiman|armada-385-linksys-cobra|armada-xp-linksys-mamba) fw_setenv auto_recovery off + mtd fixtrx s_env ;; esac }