From ff1254740806c2c2b7acdd87c53473a216e8f5eb Mon Sep 17 00:00:00 2001 From: Hector Bujanda Date: Tue, 4 Feb 2025 18:00:33 +0100 Subject: [PATCH] bootcount: skip resetting bootcount if bootlimit is not set bootcount utility started by bootcount-init script was inconditionally setting bootcount to 0 even if the functionality is disabled. According to documentation, bootcount functionality is only enabled if bootlimit variable is defined to a value greater than 0 in U-Boot environment. In platforms like cc6ul, that use MCA NVRAM to store bootcount, this was corrupting user NVRAM. Version increased to 1.1. Signed-off-by: Hector Bujanda --- .../bootcount/bootcount-bin/bootcount.c | 15 +++++++++-- .../bootcount/bootcount-bin/bootcount_env.c | 26 ++++++++++++++++++- .../bootcount-bin/include/bootcount_env.h | 12 ++++++++- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/meta-digi-dey/recipes-digi/bootcount/bootcount/bootcount-bin/bootcount.c b/meta-digi-dey/recipes-digi/bootcount/bootcount/bootcount-bin/bootcount.c index 07845a486..ae3c92511 100644 --- a/meta-digi-dey/recipes-digi/bootcount/bootcount/bootcount-bin/bootcount.c +++ b/meta-digi-dey/recipes-digi/bootcount/bootcount/bootcount-bin/bootcount.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2024 Digi International Inc. + * Copyright (c) 2023-2025 Digi International Inc. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this @@ -22,7 +22,7 @@ #include "bootcount_nvmem.h" #include "platform_utils.h" -#define VERSION "1.0" GIT_REVISION +#define VERSION "1.1" GIT_REVISION #define USAGE \ "Bootcount utility.\n" \ @@ -30,6 +30,9 @@ "\n" \ "Version: %s\n" \ "\n" \ + "For these commands to work, bootcount function must be enabled by\n" \ + "setting 'bootlimit' variable in U-Boot environment!!!\n" \ + "\n" \ "Usage: bootcount [options] \n\n" \ " -p --print Print the current bootcount value (Default action)\n" \ " -s --set= Set current bootcount to a specific value.\n" \ @@ -155,6 +158,7 @@ static void parse_options(int argc, char *argv[]) { */ int main(int argc, char *argv[]) { int ret = 0; + int bootlimit; struct platform_functions *pfuncs; /* Read and parse command line */ @@ -169,6 +173,13 @@ int main(int argc, char *argv[]) { goto end; } + bootlimit = read_bootlimit_env(); + if (bootlimit == -1 || bootlimit == 0) { + printf("\nBootcount functionality is disabled.\n"); + ret = -ENOTSUP; + goto end; + } + /* Execute the requested action. */ if (read) { ret = pfuncs->read_bootcount(); diff --git a/meta-digi-dey/recipes-digi/bootcount/bootcount/bootcount-bin/bootcount_env.c b/meta-digi-dey/recipes-digi/bootcount/bootcount/bootcount-bin/bootcount_env.c index 1c291840a..63734bba4 100644 --- a/meta-digi-dey/recipes-digi/bootcount/bootcount/bootcount-bin/bootcount_env.c +++ b/meta-digi-dey/recipes-digi/bootcount/bootcount/bootcount-bin/bootcount_env.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Digi International Inc. + * Copyright (c) 2023-2025, Digi International Inc. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this @@ -22,6 +22,7 @@ /* Environment variables. */ #define ENV_VAR_UPGRADE_AVAILABLE "upgrade_available" #define ENV_VAR_BOOTCOUNT "bootcount" +#define ENV_VAR_BOOTLIMIT "bootlimit" int read_bootcount_env() { int ret; @@ -46,6 +47,29 @@ int read_bootcount_env() { return ret; } +int read_bootlimit_env() { + int ret = -1; + char* endptr; + const char *var; + + /* Obtain 'bootlimit' value from environment. */ + uboot_getenv(ENV_VAR_BOOTLIMIT, &var); + if (var != NULL) { + /* Convert read value to integer. */ + ret = (int)strtoul(var, &endptr, 10); + if (*endptr) { + printf("Error: incorrect bootlimit value in environment\n"); + ret = -1; + } + } else { + /* Could not read bootlimit variable from U-Boot environment */ + ret = -1; + } + + free((char*)var); + return ret; +} + int write_bootcount_env(uint count) { int ret; char value_str[5]; diff --git a/meta-digi-dey/recipes-digi/bootcount/bootcount/bootcount-bin/include/bootcount_env.h b/meta-digi-dey/recipes-digi/bootcount/bootcount/bootcount-bin/include/bootcount_env.h index 072817cfd..4724365be 100644 --- a/meta-digi-dey/recipes-digi/bootcount/bootcount/bootcount-bin/include/bootcount_env.h +++ b/meta-digi-dey/recipes-digi/bootcount/bootcount/bootcount-bin/include/bootcount_env.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Digi International Inc. + * Copyright (c) 2023-2025, Digi International Inc. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this @@ -30,6 +30,16 @@ */ int read_bootcount_env(); +/** + * @brief Read the 'bootlimit' variable from the U-Boot environment. + * + * This function retrieves the 'bootlimit' value from the U-Boot environment and + * converts it to an integer. + * + * @return The 'bootlimit' value as an integer on success, -1 on error. + */ +int read_bootlimit_env(); + /** * @brief Set the 'bootcount' variable in the U-Boot environment. *