From patchwork Mon Dec 31 15:24:25 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Bauer X-Patchwork-Id: 1019618 X-Patchwork-Delegate: openwrt@kresin.me Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=david-bauer.net Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="GPS6XxTZ"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43T1My002jz9s3l for ; Tue, 1 Jan 2019 02:25:17 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Subject:MIME-Version:References: In-Reply-To:Message-Id:Date:To:From:Reply-To:Cc:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=zqaepdUiNqPfKUvB37Av93trNTla6160E0lQI/A2IW0=; b=GPS6XxTZ6tUuo4VVw+6jm5G8MP dgeRa7Vddb1ookxROQB6go71pfNeYJqJYA6UZgHUItf3cx/vi4csW7FAtzDhgiyLe0vvk73fi262r vOQ8bD1nBP+1sjWCOEfNBzFwQAIx8lkzXiKuMA+/PtkictAWAQcrddL7LDiCevqdgt9c6TksaQTJM FJlIm4ZpljHRmM1mSjvw6wKhN1btI9/OtMqKgN7RZGKo3KkGcrphX0qGoWOw7RcqEV2c/2ILIOKiO OgfsOylJRuGYfqHLV3sZzZmhz1APHr7xmwzTu+0AQXhsaxiBAV0KUjF5l37ghyltBWEfmG6pZje0p C1eUvJvw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1gdzR7-00048Z-Py; Mon, 31 Dec 2018 15:25:01 +0000 Received: from mars.blocktrron.ovh ([2001:41d0:401:3000::cbd] helo=mail.blocktrron.ovh) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gdzQv-0003zb-9s for openwrt-devel@lists.openwrt.org; Mon, 31 Dec 2018 15:24:51 +0000 Received: from localhost.localdomain (ip-109-40-128-178.web.vodafone.de [109.40.128.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.blocktrron.ovh (Postfix) with ESMTPSA id E0F371F8CC for ; Mon, 31 Dec 2018 16:24:41 +0100 (CET) From: David Bauer To: openwrt-devel@lists.openwrt.org Date: Mon, 31 Dec 2018 16:24:25 +0100 Message-Id: <20181231152426.4887-2-mail@david-bauer.net> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20181231152426.4887-1-mail@david-bauer.net> References: <20181231152426.4887-1-mail@david-bauer.net> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20181231_072449_653365_97F762E2 X-CRM114-Status: GOOD ( 23.85 ) X-Spam-Score: -0.0 (/) X-Spam-Report: SpamAssassin version 3.4.2 on bombadil.infradead.org summary: Content analysis details: (-0.0 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_PASS SPF: sender matches SPF record Subject: [OpenWrt-Devel] [PATCH v3 2/3] mtd: add logic for TP-Link ramips recovery magic X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org This adds an option to set the recovery flag of newer TP-Link MediaTek boards and remove it after a successful write. To make use of this feature, add the '-t' option to mtd-write. The '-t' option takes the mtd partition containing the recovery flag (usually 'romfile') as an argument. Make sure this partition is not flagged as read-only! Example: > mtd -t romfile write owrt.bin firmware This command writes the recovery-flag before it begins writing the image to the firmware partition. After the image-write has been successful, the recovery flag is removed. This way, the TP-Link web-recovery is automatically enabled on an unsucessful flash (e.g. power loss). This option is only available if the mtd package is compiled for the ramips target. Signed-off-by: David Bauer --- package/system/mtd/Makefile | 2 +- package/system/mtd/src/Makefile | 3 +- package/system/mtd/src/mtd.c | 33 ++++++- package/system/mtd/src/mtd.h | 1 + .../system/mtd/src/tpl_ramips_recoveryflag.c | 94 +++++++++++++++++++ 5 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 package/system/mtd/src/tpl_ramips_recoveryflag.c diff --git a/package/system/mtd/Makefile b/package/system/mtd/Makefile index 65de47f23f..c307555436 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:=23 +PKG_RELEASE:=24 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 daeadbcd35..90b72dec02 100644 --- a/package/system/mtd/src/Makefile +++ b/package/system/mtd/src/Makefile @@ -6,12 +6,13 @@ obj = mtd.o jffs2.o crc32.o md5.o obj.seama = seama.o md5.o obj.wrg = wrg.o md5.o obj.wrgg = wrgg.o md5.o +obj.tpl = tpl_ramips_recoveryflag.o obj.ar71xx = trx.o $(obj.seama) $(obj.wrgg) obj.brcm = trx.o obj.brcm47xx = $(obj.brcm) obj.bcm53xx = $(obj.brcm) $(obj.seama) obj.brcm63xx = imagetag.o -obj.ramips = $(obj.seama) $(obj.wrg) +obj.ramips = $(obj.seama) $(obj.tpl) $(obj.wrg) obj.mvebu = linksys_bootcount.o obj.kirkwood = linksys_bootcount.o obj.ipq806x = linksys_bootcount.o diff --git a/package/system/mtd/src/mtd.c b/package/system/mtd/src/mtd.c index fa04c0f95b..eccb4f6a1a 100644 --- a/package/system/mtd/src/mtd.c +++ b/package/system/mtd/src/mtd.c @@ -85,6 +85,7 @@ static char *buf = NULL; static char *imagefile = NULL; static enum mtd_image_format imageformat = MTD_IMAGE_FORMAT_UNKNOWN; static char *jffs2file = NULL, *jffs2dir = JFFS2_DEFAULT_DIR; +static char *tpl_uboot_args_part; static int buflen = 0; int quiet; int no_erase; @@ -554,6 +555,17 @@ resume: lseek(fd, part_offset, SEEK_SET); } + /* Write TP-Link recovery flag */ + if (tpl_uboot_args_part && mtd_tpl_recoverflag_write) { + if (quiet < 2) + fprintf(stderr, "Writing recovery flag to %s\n", tpl_uboot_args_part); + result = mtd_tpl_recoverflag_write(tpl_uboot_args_part, true); + if (result < 0) { + fprintf(stderr, "Could not write TP-Link recovery flag to %s: %i", mtd, result); + exit(1); + } + } + indicate_writing(mtd); w = e = 0; @@ -716,6 +728,18 @@ resume: #endif close(fd); + + /* Clear TP-Link recovery flag */ + if (tpl_uboot_args_part && mtd_tpl_recoverflag_write) { + if (quiet < 2) + fprintf(stderr, "Removing recovery flag from %s\n", tpl_uboot_args_part); + result = mtd_tpl_recoverflag_write(tpl_uboot_args_part, false); + if (result < 0) { + fprintf(stderr, "Could not clear TP-Link recovery flag to %s: %i", mtd, result); + exit(1); + } + } + return 0; } @@ -771,6 +795,10 @@ static void usage(void) fprintf(stderr, " -c datasize amount of data to be used for checksum calculation (for fixtrx / fixseama / fixwrg / fixwrgg)\n"); } + if (mtd_tpl_recoverflag_write) { + fprintf(stderr, + " -t write TP-Link recovery-flag to (for write)\n"); + } fprintf(stderr, #ifdef FIS_SUPPORT " -F [:[:]][,...]\n" @@ -828,7 +856,7 @@ int main (int argc, char **argv) #ifdef FIS_SUPPORT "F:" #endif - "frnqe:d:s:j:p:o:c:l:")) != -1) + "frnqe:d:s:j:p:o:c:t:l:")) != -1) switch (ch) { case 'f': force = 1; @@ -896,6 +924,9 @@ int main (int argc, char **argv) usage(); } break; + case 't': + tpl_uboot_args_part = optarg; + break; #ifdef FIS_SUPPORT case 'F': fis_layout = optarg; diff --git a/package/system/mtd/src/mtd.h b/package/system/mtd/src/mtd.h index 0250a90e0b..3eda6159c6 100644 --- a/package/system/mtd/src/mtd.h +++ b/package/system/mtd/src/mtd.h @@ -30,4 +30,5 @@ extern int mtd_fixseama(const char *mtd, size_t offset, size_t data_size) __attr extern int mtd_fixwrg(const char *mtd, size_t offset, size_t data_size) __attribute__ ((weak)); extern int mtd_fixwrgg(const char *mtd, size_t offset, size_t data_size) __attribute__ ((weak)); extern int mtd_resetbc(const char *mtd) __attribute__ ((weak)); +extern int mtd_tpl_recoverflag_write(const char *mtd, const bool recovery_active) __attribute__ ((weak)); #endif /* __mtd_h */ diff --git a/package/system/mtd/src/tpl_ramips_recoveryflag.c b/package/system/mtd/src/tpl_ramips_recoveryflag.c new file mode 100644 index 0000000000..3711e01317 --- /dev/null +++ b/package/system/mtd/src/tpl_ramips_recoveryflag.c @@ -0,0 +1,94 @@ +/* + * TP-Link recovery flag set and unset code for ramips target + * + * Copyright (C) 2018 David Bauer + * + * 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. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "mtd.h" + + +#define TPL_RECOVER_MAGIC 0x89abcdef +#define TPL_NO_RECOVER_MAGIC 0x00000000 + + +struct uboot_args { + uint32_t magic; +}; + +int mtd_tpl_recoverflag_write(const char *mtd, const bool recovery_active) +{ + struct erase_info_user erase_info; + struct uboot_args *args; + uint32_t magic; + int ret = 0; + int fd; + + args = malloc(erasesize); + if (!args) { + fprintf(stderr, "Could not allocate memory!\n"); + return -1; + } + + fd = mtd_check_open(mtd); + if (fd < 0) { + fprintf(stderr, "Could not open mtd device: %s\n", mtd); + ret = -1; + goto out; + } + + /* read first block (containing the magic) */ + pread(fd, args, erasesize, 0); + + /* set magic to desired value */ + magic = TPL_RECOVER_MAGIC; + if (!recovery_active) + magic = TPL_NO_RECOVER_MAGIC; + + /* no need to write when magic is already set correctly */ + if (magic == args->magic) + goto out; + + /* erase first block (containing the magic) */ + erase_info.start = 0; + erase_info.length = erasesize; + + ret = ioctl(fd, MEMERASE, &erase_info); + if (ret < 0) { + fprintf(stderr, "failed to erase block: %i\n", ret); + goto out; + } + + /* write magic to flash */ + args->magic = magic; + + ret = pwrite(fd, args, erasesize, 0); + if (ret < 0) + fprintf(stderr, "failed to write: %i\n", ret); + + sync(); +out: + free(args); + close(fd); + + return ret; +}