From patchwork Wed Mar 11 10:59:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Fr=C3=A9d=C3=A9ric_Danis?= X-Patchwork-Id: 243531 List-Id: U-Boot discussion From: frederic.danis at collabora.com (=?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Danis?=) Date: Wed, 11 Mar 2020 11:59:49 +0100 Subject: [PATCH] bootcount_ext: Add flag to enable/disable bootcount Message-ID: <20200311105949.23433-1-frederic.danis@collabora.com> After a successful upgrade, multiple problem during boot sequence may trigger the altbootcmd process. This patch adds a 3rd byte to the bootcount file to enable/disable the bootcount check. When reading old bootcount file, 2 bytes long, it will consider that bootcount is enabled, and update the file accordingly. The bootcount file is only saved when `upgrade_available` is true, this allows to save writes to the filesystem. Signed-off-by: Fr?d?ric Danis --- drivers/bootcount/bootcount_ext.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/drivers/bootcount/bootcount_ext.c b/drivers/bootcount/bootcount_ext.c index 075e590896..740bce0296 100644 --- a/drivers/bootcount/bootcount_ext.c +++ b/drivers/bootcount/bootcount_ext.c @@ -9,6 +9,8 @@ #define BC_MAGIC 0xbc +static u8 upgrade_available = 1; + void bootcount_store(ulong a) { u8 *buf; @@ -21,13 +23,18 @@ void bootcount_store(ulong a) return; } - buf = map_sysmem(CONFIG_SYS_BOOTCOUNT_ADDR, 2); + /* Only update bootcount during upgrade process */ + if (!upgrade_available) + return; + + buf = map_sysmem(CONFIG_SYS_BOOTCOUNT_ADDR, 3); buf[0] = BC_MAGIC; buf[1] = (a & 0xff); + buf[2] = upgrade_available; unmap_sysmem(buf); ret = fs_write(CONFIG_SYS_BOOTCOUNT_EXT_NAME, - CONFIG_SYS_BOOTCOUNT_ADDR, 0, 2, &len); + CONFIG_SYS_BOOTCOUNT_ADDR, 0, 3, &len); if (ret != 0) puts("Error storing bootcount\n"); } @@ -45,15 +52,20 @@ ulong bootcount_load(void) } ret = fs_read(CONFIG_SYS_BOOTCOUNT_EXT_NAME, CONFIG_SYS_BOOTCOUNT_ADDR, - 0, 2, &len_read); - if (ret != 0 || len_read != 2) { + 0, 3, &len_read); + if (ret != 0 || len_read < 2 || len_read > 3) { puts("Error loading bootcount\n"); return 0; } buf = map_sysmem(CONFIG_SYS_BOOTCOUNT_ADDR, 2); - if (buf[0] == BC_MAGIC) - ret = buf[1]; + /* Use the 3rd byte to enable/disable bootcount */ + if (buf[0] == BC_MAGIC) { + if (len_read == 3) + upgrade_available = buf[2]; + if (upgrade_available) + ret = buf[1]; + } unmap_sysmem(buf);