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 <hector.bujanda@digi.com>
This commit is contained in:
Hector Bujanda 2025-02-04 18:00:33 +01:00
parent 7357dabd61
commit ff12547408
3 changed files with 49 additions and 4 deletions

View File

@ -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 * 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 * License, v. 2.0. If a copy of the MPL was not distributed with this
@ -22,7 +22,7 @@
#include "bootcount_nvmem.h" #include "bootcount_nvmem.h"
#include "platform_utils.h" #include "platform_utils.h"
#define VERSION "1.0" GIT_REVISION #define VERSION "1.1" GIT_REVISION
#define USAGE \ #define USAGE \
"Bootcount utility.\n" \ "Bootcount utility.\n" \
@ -30,6 +30,9 @@
"\n" \ "\n" \
"Version: %s\n" \ "Version: %s\n" \
"\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" \ "Usage: bootcount [options] \n\n" \
" -p --print Print the current bootcount value (Default action)\n" \ " -p --print Print the current bootcount value (Default action)\n" \
" -s <value> --set=<value> Set current bootcount to a specific value.\n" \ " -s <value> --set=<value> 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 main(int argc, char *argv[]) {
int ret = 0; int ret = 0;
int bootlimit;
struct platform_functions *pfuncs; struct platform_functions *pfuncs;
/* Read and parse command line */ /* Read and parse command line */
@ -169,6 +173,13 @@ int main(int argc, char *argv[]) {
goto end; 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. */ /* Execute the requested action. */
if (read) { if (read) {
ret = pfuncs->read_bootcount(); ret = pfuncs->read_bootcount();

View File

@ -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 * 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 * License, v. 2.0. If a copy of the MPL was not distributed with this
@ -22,6 +22,7 @@
/* Environment variables. */ /* Environment variables. */
#define ENV_VAR_UPGRADE_AVAILABLE "upgrade_available" #define ENV_VAR_UPGRADE_AVAILABLE "upgrade_available"
#define ENV_VAR_BOOTCOUNT "bootcount" #define ENV_VAR_BOOTCOUNT "bootcount"
#define ENV_VAR_BOOTLIMIT "bootlimit"
int read_bootcount_env() { int read_bootcount_env() {
int ret; int ret;
@ -46,6 +47,29 @@ int read_bootcount_env() {
return ret; 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 write_bootcount_env(uint count) {
int ret; int ret;
char value_str[5]; char value_str[5];

View File

@ -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 * 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 * License, v. 2.0. If a copy of the MPL was not distributed with this
@ -30,6 +30,16 @@
*/ */
int read_bootcount_env(); 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. * @brief Set the 'bootcount' variable in the U-Boot environment.
* *