How to implement bootcount in u boot

I need to implement count mechanism in u-boot so that after rebooting a system my count variable should get increment. Any help will be appreciated.

Two suggestions: a) You can use “fw_setenv” in the running Linux system to edit the u-boot environment and b) edit the default bootcmd in u-boot to manage a counter.

I tried to edit default bootcmd in u-boot, which is inside apalis_imx6.h file but my changes are not getting reflected in default environment.

Hi @Nitish ,

Welcome to Toradex Community!

Could you please explain what exactly you want to achieve?
For modifying U-Boot Env,

  • You need to change the apalis_imx6.h file and then replace it on the module.
  • For first time use “# env default -a” and “# saveenv” command. Which will save permanent your modified Env in the eMMC.

I want to implement a counter that should increment whenever system reboot. To achieve this I have made following changes in my apalis_imx6.h file.

#define CONFIG_BOOTCOMMAND "run count; run emmcboot; echo; echo emmcboot failed; "
"run distro_bootcmd; usb start; "
“setenv stdout serial,vga; setenv stdin serial,usbkbd”
#endif

#ifndef CONFIG_SPL_BUILD
#define BOOTENV_RUN_NET_USB_START “”
#define BOOT_TARGET_DEVICES(func)
func(MMC, mmc, 1)
func(MMC, mmc, 2)
func(MMC, mmc, 0)
func(USB, usb, 0)
func(DHCP, dhcp, na)
#include
#include
#undef CONFIG_ISO_PARTITION
#else
#define BOOTENV
#endif

#define DFU_ALT_EMMC_INFO
“u-boot.imx raw 0x2 0x3ff mmcpart 0;”
“boot part 0 1;”
“rootfs part 0 2;”
“zImage fat 0 1;”
“imx6q-colibri-eval-v3.dtb fat 0 1;”
“imx6q-colibri-cam-eval-v3.dtb fat 0 1”

#define COUNT
“setenv bootcount 0”
“count=setexpr bootcount ${bootcount} + 1”

#define EMMC_BOOTCMD
"emmcargs=ip=off root=/dev/mmcblk0p2 ro rootfstype=ext4 "
“rootwait\0”
"emmcboot=run setup; "
"setenv bootargs ${defargs} ${emmcargs} ${setupargs} "
"${vidargs}; echo Booting from internal eMMC chip…; "
"run emmcdtbload; load mmc 0:1 ${kernel_addr_r} "
"${boot_file} && run fdt_fixup && "
“bootz ${kernel_addr_r} ${dtbparam}\0”
“emmcdtbload=setenv dtbparam; load mmc 0:1 ${fdt_addr_r} "
“${fdt_file} && setenv dtbparam " - ${fdt_addr_r}” && true\0”

So I have define a COUNT in apalis_imx6.h but my changes are not getting reflected in default environment. I have done #env default -a and #saveenv but still mychanges are not getting reflected.

Hi @Nitish ,

Can you please check this.