Skip to main content
Version: Next

5.8: Dynamic Firmware hash

1. Introduction

image-20260411122554795

1.1:Workflow

  1. Integrate the information block generation code into the firmware and write the verification data into SRAM.
  2. Use the PowerWriter programmer to program the target firmware into the chip.
  3. PowerWriter automatically searches and reads the firmware block information generated and verified independently by the MCU firmware in SRAM.

1.2:Application Scenarios

  • When verifying chip data, check whether the data programmed into the chip is complete without reading data or using the verification function.
  • Simplify the data verification process: instead of reading all Flash data, only read the verification block information.
  • The MCU independently calculates and generates dynamic verification information (instead of static verification), enabling quick location when the Flash firmware is abnormally modified.
  • Other unlisted scenarios.

1.3:Limitations

  • It is necessary to ensure that the target chip's SRAM is readable. For some chips, SRAM can be read when RDP1 is enabled, but this is not true for all chips.
  • The project needs to integrate the information block generation code and use PowerWriter to program the target firmware into the chip.

2 Code Integration

2.1 Example Source Code

powerwriter_auto_flash_region_hash_utils.zip,— The key structure definitions in the code are as follows, please check the comment explanations carefully:

/* PowerWriter will search for data through this marker */
#define powerwriter_flash_magic "powerwriter_flash_region_chunks"

/* Number of firmware block information items, default is 1, can be modified to other values */
#define powerwriter_region_count (1)

/* Standard armcc compiler export symbols for single-section firmware */
extern const uint32_t Load$$LR$$LR_IROM1$$Base;
extern const uint32_t Load$$LR$$LR_IROM1$$Length;
extern const uint32_t Load$$LR$$LR_IROM1$$Limit;

/* flash section info item struct */
#pragma pack(push, 1)
typedef struct flash_section_info_item_t {
uint32_t start; /* Firmware start address */
uint32_t end; /* Firmware end address */
uint32_t size; /* Firmware size */
uint32_t crc32; /* Firmware crc32 */
uint32_t checksum;/* Firmware checksum */
} flash_section_info_item_t;

/* flash section info struct */
typedef struct flash_section_info_t {
char m_flash_hash_magic[32]; /* powerwriter_flash_magic */
uint8_t m_flash_hash_count; /* Number of block information items in firmware */
flash_section_info_item_t m_flash_hash_info[powerwriter_region_count]; /* Information block storage */
} flash_section_info_t;
#pragma pack(pop)
tip

Currently, only automatic integration source code for MDK single-section firmware (a single segment in one hex file) is provided. The implementation process for multi-section firmware projects or GCC/IAR compilers is similar; please populate the verification data structure according to the above structure and sample project.

2.2 Example Project and Steps

powerwriter_auto_flash_region_hash_mdk.zip — Quick integration steps:

  • Add powerwriter_auto_hash.c and powerwriter_auto_hash.h to the target project.
  • At the beginning of the main function, add the fw_section_info_initial function for generating and populating the information block.

#include "powerwriter_auto_hash.h"
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
fw_section_info_initial();

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
caution

The fw_section_info_initial function should be placed as early as possible. Before PowerWriter reads the information block, it will reset the target chip and make it run, so that the chip can calculate the result and store it in memory as soon as possible for PowerWriter to search.

3: Practical Operation Demonstration

1

image-20260411121751734