From 1e06482f7db284567b240ce6f59c644439ec813f Mon Sep 17 00:00:00 2001 From: David Bauer Date: Mon, 31 Dec 2018 16:24:25 +0100 Subject: mtd: add logic for TP-Link ramips recovery magic 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/src/tpl_ramips_recoveryflag.c | 94 ++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 package/system/mtd/src/tpl_ramips_recoveryflag.c (limited to 'package/system/mtd/src/tpl_ramips_recoveryflag.c') 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; +} -- cgit v1.2.3