/* Primary Bootloader startup: runs immediately after reset */
/* Must be fast: OEM requirement typically < 100 ms to first CAN/Ethernet frame */
#include
#include "Pbl_Cfg.h"
#include "Flash_If.h"
#include "Wdg_If.h"
typedef enum {
BOOT_REASON_POWER_ON = 0x00u,
BOOT_REASON_REPROG_REQ = 0x01u, /* application set reprog flag */
BOOT_REASON_APP_INVALID= 0x02u, /* app CRC failed */
BOOT_REASON_WDG_RESET = 0x03u, /* watchdog triggered */
BOOT_REASON_SOFT_RESET = 0x04u, /* ECUReset service */
} BootReason_t;
void PBL_Main(void)
{
/* 1. Init minimal hardware: clocks, watchdog service, SRAM */
Wdg_Init();
Clock_Init();
Ram_Init(); /* zero BSS, copy DATA section */
Wdg_Trigger(); /* service WDG: startup must complete before WDG expires */
/* 2. Read boot flags from NvM / dedicated flash sector */
BootReason_t reason = BootFlags_Read();
/* 3. Decision: reprogram or run application */
if (reason == BOOT_REASON_REPROG_REQ) {
BootFlags_Clear(); /* clear flag to avoid infinite reprog loop */
PBL_EnterReprogrammingMode(); /* wait for UDS tester */
/* never returns normally */
}
/* 4. Validate application */
if (App_IsValid()) {
Wdg_Trigger();
PBL_JumpToApplication(); /* relocate vectors; jump */
/* never returns */
}
/* 5. Application invalid: check for SBL (recovery) */
if (Sbl_IsPresent() && Sbl_IsValid()) {
PBL_JumpToSbl();
/* never returns */
}
/* 6. Nothing to run: enter reprogramming mode and wait for tester */
PBL_EnterReprogrammingMode();
} PBL Startup Sequence
Application Validity Check
#include
#include "Crc.h"
/* Application validity: checked at every boot by PBL */
/* Typical: CRC-32 over application flash region, stored in app header */
typedef struct {
uint32_t magic; /* 0xA5A5A5A5: valid signature pattern */
uint32_t version; /* application SW version (BCD: 0x01040001 = 1.4.0.1) */
uint32_t app_crc32; /* CRC-32 over [APP_BASE+256 ... APP_END] */
uint32_t app_length; /* byte count of application */
uint8_t reserved[240]; /* pad to 256 bytes total */
} AppHeader_t;
#define APP_BASE 0x80020000u
#define APP_HEADER_ADDR ((const AppHeader_t *)APP_BASE)
#define APP_DATA_START (APP_BASE + sizeof(AppHeader_t))
#define MAGIC_VALID 0xA5A5A5A5u
boolean App_IsValid(void)
{
const AppHeader_t *hdr = APP_HEADER_ADDR;
/* Check magic number */
if (hdr->magic != MAGIC_VALID) {
return FALSE;
}
/* Check application length is within bounds */
if ((hdr->app_length == 0u) || (hdr->app_length > APP_MAX_SIZE)) {
return FALSE;
}
/* Compute CRC-32 over application code */
uint32_t computed_crc = Crc_CalculateCRC32(
(const uint8_t *)APP_DATA_START,
hdr->app_length,
CRC32_INITIAL_VALUE,
TRUE);
return (boolean)(computed_crc == hdr->app_crc32);
}
/* Note: CRC calculation at boot adds latency: 256 kB @ 1 cycle/byte = ~1 ms at 300 MHz */
/* Alternative: store SHA-256 hash + verify with HSM (secure boot, covered later) */ Boot Flags and Reprogramming Trigger
| Flag | Value | Set By | Cleared By | Action |
|---|---|---|---|---|
| REPROG_REQUESTED | 0xA5A5 | Application (before ECUReset) | PBL on entry to reprog mode | PBL enters reprogramming mode |
| REPROG_IN_PROGRESS | 0x5A5A | PBL (during session) | Tester checksum verify success | Prevents partial flash on power loss |
| APP_UPDATED | 0x1234 | SBL after successful write | Application on first boot | Application validates its own update |
| WDG_RESET_COUNT | 0x00–0xFF | Incremented on WDG reset | Cleared by app on clean boot | PBL stays in reprogram after N WDG resets |
Watchdog Management During Boot
/* Watchdog during boot: must be serviced throughout PBL execution */
/* PBL must complete its full startup + CRC check + jump within WDG timeout */
#define WDG_TIMEOUT_MS 100u /* OEM typical: 100 ms boot window */
/* Critical: CRC check of large flash regions must not starve the WDG */
/* Solution: compute CRC in chunks; service WDG between chunks */
uint32_t App_CrcWithWdgService(const uint8_t *data, uint32_t len)
{
uint32_t crc = CRC32_INITIAL_VALUE;
uint32_t chunk = 4096u; /* 4 kB chunks */
uint32_t offset = 0u;
while (offset < len) {
uint32_t to_process = (len - offset < chunk) ? (len - offset) : chunk;
crc = Crc_CalculateCRC32(&data[offset], to_process, crc, FALSE);
offset += to_process;
Wdg_Trigger(); /* service watchdog between chunks */
}
return Crc_CalculateCRC32(NULL, 0u, crc, TRUE); /* finalise */
}
/* Alternative: use internal CRC hardware (Aurix FCE module) */
/* FCE can compute CRC-32 of 8 MB PFlash in < 1 ms — no WDG chunks needed */Summary
The boot decision sequence (check reprogramming flag → validate application → check SBL → wait for tester) must be deterministic and fast. OEM requirements typically mandate ECU-to-first-response within 100 ms of power-on, which means the entire PBL startup including CRC validation must complete in under 80 ms. Hardware CRC engines (Aurix FCE, Cortex-M DWT CRC) compute CRC-32 over multi-megabyte regions in milliseconds, eliminating the need for chunk-based CRC with watchdog servicing. Boot flags stored in a dedicated non-erasable flash sector (with ECC) are the safest reprogramming trigger mechanism — they survive power loss and watchdog resets.
🔬 Deep Dive — Core Concepts Expanded
This section builds on the foundational concepts covered above with additional technical depth, edge cases, and configuration nuances that separate competent engineers from experts. When working on production ECU projects, the details covered here are the ones most commonly responsible for integration delays and late-phase defects.
Key principles to reinforce:
- Configuration over coding: In AUTOSAR and automotive middleware environments, correctness is largely determined by ARXML configuration, not application code. A correctly implemented algorithm can produce wrong results due to a single misconfigured parameter.
- Traceability as a first-class concern: Every configuration decision should be traceable to a requirement, safety goal, or architecture decision. Undocumented configuration choices are a common source of regression defects when ECUs are updated.
- Cross-module dependencies: In tightly integrated automotive software stacks, changing one module's configuration often requires corresponding updates in dependent modules. Always perform a dependency impact analysis before submitting configuration changes.
🏭 How This Topic Appears in Production Projects
- Project integration phase: The concepts covered in this lesson are most commonly encountered during ECU integration testing — when multiple software components from different teams are combined for the first time. Issues that were invisible in unit tests frequently surface at this stage.
- Supplier/OEM interface: This is a topic that frequently appears in technical discussions between Tier-1 ECU suppliers and OEM system integrators. Engineers who can speak fluently about these details earn credibility and are often brought into critical design review meetings.
- Automotive tool ecosystem: Vector CANoe/CANalyzer, dSPACE tools, and ETAS INCA are the standard tools used to validate and measure the correct behaviour of the systems described in this lesson. Familiarity with these tools alongside the conceptual knowledge dramatically accelerates debugging in real projects.
⚠️ Common Mistakes and How to Avoid Them
- Assuming default configuration is correct: Automotive software tools ship with default configurations that are designed to compile and link, not to meet project-specific requirements. Every configuration parameter needs to be consciously set. 'It compiled' is not the same as 'it is correctly configured'.
- Skipping documentation of configuration rationale: In a 3-year ECU project with team turnover, undocumented configuration choices become tribal knowledge that disappears when engineers leave. Document why a parameter is set to a specific value, not just what it is set to.
- Testing only the happy path: Automotive ECUs must behave correctly under fault conditions, voltage variations, and communication errors. Always test the error handling paths as rigorously as the nominal operation. Many production escapes originate in untested error branches.
- Version mismatches between teams: In a multi-team project, the BSW team, SWC team, and system integration team may use different versions of the same ARXML file. Version management of all ARXML files in a shared repository is mandatory, not optional.
📊 Industry Note
Engineers who master both the theoretical concepts and the practical toolchain skills covered in this course are among the most sought-after professionals in the automotive software industry. The combination of AUTOSAR standards knowledge, safety engineering understanding, and hands-on configuration experience commands premium salaries at OEMs and Tier-1 suppliers globally.