The bootcount feature is a mechanism integrated in U-Boot to keep track of the number of times a device tries to boot. This mechanism is particularly useful for embedded systems and devices where it is essential to take corrective actions when the system is unable to boot normally after a number of attempts.

The bootcount feature works as follows:

  1. U-Boot stores a boot count value in a non-volatile memory location of the device.

  2. Each time the device starts up, U-Boot increments this boot count value.

  3. If the system is able to fully boot, the boot count value is automatically reset from user space.

  4. If the system is not able to fully boot after a number of attempts, U-Boot tries an alternate boot command.

Bootcount workflow diagram

The implementation details of this feature may vary depending on the specific U-Boot version and hardware platform in use.

Boot count storage

The data stored by the bootcount feature is composed of two bytes:

  • The first byte is used as a magic number or header with a value of 0xBC.

  • The second byte is used to store the real boot count value.

In the ConnectCore MP15 Development Kit, U-Boot stores the boot count value in the board’s Real-Time Clock (RTC) user RAM registers. The Micro Crystal RV-3028 RTC provides two user RAM registers of one byte each to store data whose value is maintained by coin cell battery. The bootcount feature uses both registers, so they are unavailable for user applications.

U-Boot also creates a temporary variable bootcount with the current value that can be used by boot scripts.

Set boot count limit

By default, the boot count limit is set to zero (or undefined), and the boot count mechanism is disabled. To activate the bootcount mechanism, set the bootlimit environment variable to the number of boot attempts you desire for your device.

  1. Set the bootlimit:

    => setenv bootlimit <value>

    Where <value> is the number of times your device will attempt to boot.

    You can either leave the value empty or set it to 0 to disable the bootcount feature.
  2. Save the environment by issuing this command:

    => saveenv
  3. Reset the device so that the new boot count limit takes effect.

If you use one of the firmware install scripts and choose to install a dual boot system, the script automatically sets a bootlimit of three.

Actions after failed boot attempts

If the system is not able to fully boot after the boot attempts established in bootlimit, U-Boot executes the altbootcmd command instead of the standard bootcmd.

The default altbootcmd command takes different actions depending on the boot strategy:

  • Dual boot systems:

    • Swaps the active partition bank

    • Resets the boot count

    • Reboots the device to let it boot the alternate bank

  • Single boot systems:

    • Resets the boot count

    • Reboots the device

You can customize altbootcmd to perform a desired set of actions when U-Boot executes the command.
The RTC must be kept powered across boot attempts to preserve the boot count. Connect a coin cell battery to ensure the RTC never loses power.

Manage boot count

Reset boot count

When the system boot finishes, the boot count value is reset to 0 and saved so that the next boot starts with a count of 1. This is automatically done by the bootcount-init systemd service during the startup process. You can easily customize it by modifying the script located at <dey_sources_folder>/meta-digi/meta-digi-dey/recipes-digi/bootcount/bootcount/bootcount-init.

In addition to this, you can manually reset and save the boot count value from U-Boot or from the running system. See U-Boot command and Linux command to learn more.

Disable boot count

You can disable the bootcount feature from U-Boot by setting the bootlimit environment variable to 0:

=> setenv bootlimit 0
=> saveenv

U-Boot command

You can use U-Boot command bootcount to manage the boot count value from the bootloader. This command can be useful for tracking the number of system boots for maintenance or debugging purposes.

Print the current boot count value
=> bootcount print
1
Reset the boot count value to 0
=> bootcount reset
Note that this action does not reset the temporary bootcount variable, so the old boot count value will still be available in the environment.

Linux command

Digi Embedded Yocto provides a bootcount command to manage the boot count value from Linux. This command can be useful for maintenance or debugging purposes.

Print the current boot count value
# bootcount -p
0
Reset the boot count value to 0
# bootcount -r
Set the boot count value to 3
# bootcount -s 3
Note that once the system boots successfully, the boot count value is always reset to 0. Expect this value when executing bootcount -p command from Linux.