u-boot bootcount functionality

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

u-boot bootcount functionality

Jump to solution
2,073 Views
AB22
Contributor III

I have made the following modifications to the u-boot configuration (Yocto Scarthgap/i.MX8M Plus):

CONFIG_BOOTCOUNT=y
CONFIG_BOOTCOUNT_LIMIT=y
CONFIG_BOOTCOUNT_BOOTLIMIT=3

Every time I printenv bootcount at the u-boot prompt, the value is 1. I would like for the bootcount functionality to indicate when to fallback to a working partition during dual-copy update (SWUpdate) in the event the boot process repeatedly fails. What conditions cause the bootcount to increment? I can corrupt files and cause a stall at the u-boot prompt but nothing is providing a mechanism to initiate fallback.

0 Kudos
Reply
1 Solution
2,039 Views
Manuel_Salas
NXP TechSupport
NXP TechSupport

Hello @AB22 

I hope you are doing very well.

You can see the bootcount_env.c file under uboot-imx/drivers/bootcount.

// SPDX-License-Identifier: GPL-2.0+
/*
 * (C) Copyright 2013
 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
 */

#include <common.h>
#include <env.h>

void bootcount_store(ulong a)
{
	int upgrade_available = env_get_ulong("upgrade_available", 10, 0);

	if (upgrade_available) {
		env_set_ulong("bootcount", a);
		env_save();
	}
}

ulong bootcount_load(void)
{
	int upgrade_available = env_get_ulong("upgrade_available", 10, 0);
	ulong val = 0;

	if (upgrade_available)
		val = env_get_ulong("bootcount", 10, 0);

	return val;
}
 

You need to set the environment variable called upgrade_available on your U-boot.

u-boot=> setenv upgrade_available 1

 

After reset:

u-boot=> printenv bootcount
bootcount=2
 

I hope this can helps to you.

 

Best regards,

Salas.

 

View solution in original post

0 Kudos
Reply
4 Replies
2,040 Views
Manuel_Salas
NXP TechSupport
NXP TechSupport

Hello @AB22 

I hope you are doing very well.

You can see the bootcount_env.c file under uboot-imx/drivers/bootcount.

// SPDX-License-Identifier: GPL-2.0+
/*
 * (C) Copyright 2013
 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
 */

#include <common.h>
#include <env.h>

void bootcount_store(ulong a)
{
	int upgrade_available = env_get_ulong("upgrade_available", 10, 0);

	if (upgrade_available) {
		env_set_ulong("bootcount", a);
		env_save();
	}
}

ulong bootcount_load(void)
{
	int upgrade_available = env_get_ulong("upgrade_available", 10, 0);
	ulong val = 0;

	if (upgrade_available)
		val = env_get_ulong("bootcount", 10, 0);

	return val;
}
 

You need to set the environment variable called upgrade_available on your U-boot.

u-boot=> setenv upgrade_available 1

 

After reset:

u-boot=> printenv bootcount
bootcount=2
 

I hope this can helps to you.

 

Best regards,

Salas.

 

0 Kudos
Reply
2,036 Views
AB22
Contributor III
Thank you for pointing me to that file. It is quite helpful.
2,054 Views
xacov
Contributor I

It should not be a question. 

 

 
 
 
config BOOTCOUNT_ENV
bool "Boot counter in environment"
help
  If no softreset save registers are found on the hardware
  "bootcount" is stored in the environment. To prevent a
  saveenv on all reboots, the environment variable
  "upgrade_available" is used. If "upgrade_available" is
  0, "bootcount" is always 0. If "upgrade_available" is 1,
          "bootcount" is incremented in the environment.
  So the Userspace Application must set the "upgrade_available"
  and "bootcount" variables to 0, if the system booted successfully.

 

0 Kudos
Reply
2,035 Views
AB22
Contributor III
That is quite helpful. I misunderstood the usage of upgrade_available. Thank you.
0 Kudos
Reply
%3CLINGO-SUB%20id%3D%22lingo-sub-2035853%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3Eu-boot%20bootcount%20functionality%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2035853%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EI%20have%20made%20the%20following%20modifications%20to%20the%20u-boot%20configuration%20(Yocto%20Scarthgap%2Fi.MX8M%20Plus)%3A%3C%2FP%3E%3CP%3ECONFIG_BOOTCOUNT%3Dy%3CBR%20%2F%3ECONFIG_BOOTCOUNT_LIMIT%3Dy%3CBR%20%2F%3ECONFIG_BOOTCOUNT_BOOTLIMIT%3D3%3C%2FP%3E%3CP%3EEvery%20time%20I%20printenv%20bootcount%20at%20the%20u-boot%20prompt%2C%20the%20value%20is%201.%20I%20would%20like%20for%20the%20bootcount%20functionality%20to%20indicate%20when%20to%20fallback%20to%20a%20working%20partition%20during%20dual-copy%20update%20(SWUpdate)%20in%20the%20event%20the%20boot%20process%20repeatedly%20fails.%20What%20conditions%20cause%20the%20bootcount%20to%20increment%3F%20I%20can%20corrupt%20files%20and%20cause%20a%20stall%20at%20the%20u-boot%20prompt%20but%20nothing%20is%20providing%20a%20mechanism%20to%20initiate%20fallback.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2035853%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CLINGO-LABEL%3Ei.MX%208%20Family%20%7C%20i.MX%208QuadMax%20(8QM)%20%7C%208QuadPlus%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EYocto%20Project%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2036228%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ERe%3A%20u-boot%20bootcount%20functionality%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2036228%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3EThat%20is%20quite%20helpful.%20I%20misunderstood%20the%20usage%20of%20upgrade_available.%20Thank%20you.%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2036227%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ERe%3A%20u-boot%20bootcount%20functionality%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2036227%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3EThank%20you%20for%20pointing%20me%20to%20that%20file.%20It%20is%20quite%20helpful.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2036225%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ERe%3A%20u-boot%20bootcount%20functionality%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2036225%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHello%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F209988%22%20target%3D%22_blank%22%3E%40AB22%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20hope%20you%20are%20doing%20very%20well.%3C%2FP%3E%0A%3CP%3EYou%20can%20see%20the%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fnxp-imx%2Fuboot-imx%2Fblob%2Flf_v2022.04%2Fdrivers%2Fbootcount%2Fbootcount_env.c%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ebootcount_env.c%3C%2FA%3E%20file%20under%20uboot-imx%2Fdrivers%2Fbootcount.%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-c%22%3E%3CCODE%3E%2F%2F%20SPDX-License-Identifier%3A%20GPL-2.0%2B%0A%2F*%0A%20*%20(C)%20Copyright%202013%0A%20*%20Heiko%20Schocher%2C%20DENX%20Software%20Engineering%2C%20hs%40denx.de.%0A%20*%2F%0A%0A%23include%20%3CCOMMON.H%3E%0A%23include%20%3CENV.H%3E%0A%0Avoid%20bootcount_store(ulong%20a)%0A%7B%0A%09int%20upgrade_available%20%3D%20env_get_ulong(%22upgrade_available%22%2C%2010%2C%200)%3B%0A%0A%09if%20(upgrade_available)%20%7B%0A%09%09env_set_ulong(%22bootcount%22%2C%20a)%3B%0A%09%09env_save()%3B%0A%09%7D%0A%7D%0A%0Aulong%20bootcount_load(void)%0A%7B%0A%09int%20upgrade_available%20%3D%20env_get_ulong(%22upgrade_available%22%2C%2010%2C%200)%3B%0A%09ulong%20val%20%3D%200%3B%0A%0A%09if%20(upgrade_available)%0A%09%09val%20%3D%20env_get_ulong(%22bootcount%22%2C%2010%2C%200)%3B%0A%0A%09return%20val%3B%0A%7D%3C%2FENV.H%3E%3C%2FCOMMON.H%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CDIV%20id%3D%22tinyMceEditorAlejandro_Salas_0%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%3EYou%20need%20to%20set%20the%20environment%20variable%20called%20upgrade_available%20on%20your%20U-boot.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Eu-boot%3D%26gt%3B%20setenv%20upgrade_available%201%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CBR%20%2F%3E%0A%3CP%3EAfter%20reset%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Eu-boot%3D%26gt%3B%20printenv%20bootcount%0Abootcount%3D2%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CDIV%20id%3D%22tinyMceEditorAlejandro_Salas_1%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3EI%20hope%20this%20can%20helps%20to%20you.%3C%2FP%3E%0A%3CBR%20%2F%3E%0A%3CP%3EBest%20regards%2C%3C%2FP%3E%0A%3CP%3ESalas.%3C%2FP%3E%0A%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2035969%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ERe%3A%20u-boot%20bootcount%20functionality%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2035969%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EIt%20should%20not%20be%20a%20question.%26nbsp%3B%3C%2FP%3E%3CBR%20%2F%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fnxp-imx%2Fuboot-imx%2Fblob%2Flf-6.1.55-2.2.0%2Fdrivers%2Fbootcount%2FKconfig%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Fnxp-imx%2Fuboot-imx%2Fblob%2Flf-6.1.55-2.2.0%2Fdrivers%2Fbootcount%2FKconfig%3C%2FA%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3Econfig%20BOOTCOUNT_ENV%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Ebool%20%22Boot%20counter%20in%20environment%22%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Ehelp%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%26nbsp%3B%20If%20no%20softreset%20save%20registers%20are%20found%20on%20the%20hardware%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%26nbsp%3B%20%22bootcount%22%20is%20stored%20in%20the%20environment.%20To%20prevent%20a%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%26nbsp%3B%20saveenv%20on%20all%20reboots%2C%20the%20environment%20variable%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%26nbsp%3B%20%22upgrade_available%22%20is%20used.%20If%20%22upgrade_available%22%20is%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%26nbsp%3B%200%2C%20%22bootcount%22%20is%20always%200.%20If%20%22upgrade_available%22%20is%201%2C%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%22bootcount%22%20is%20incremented%20in%20the%20environment.%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%26nbsp%3B%20So%20the%20Userspace%20Application%20must%20set%20the%20%22upgrade_available%22%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%26nbsp%3B%20and%20%22bootcount%22%20variables%20to%200%2C%20if%20the%20system%20booted%20successfully.%3C%2FSPAN%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E