ICARUS OS (Intelligent Cooperative Architecture for Real-time Unified Systems) 0.1.0
Preemptive Real-Time Operating System for ARM Cortex-M7
Loading...
Searching...
No Matches
cs.c File Reference

Background Checksum integrity monitor implementation. More...

#include "icarus/icarus.h"
#include <string.h>

Go to the source code of this file.

Data Structures

struct  cs_entry_t
 Per-region entry in the monitor table. More...
 

Macros

#define CS_SELFTEST_EXPECTED   0x29B1u
 Known CRC16-CCITT test vector.
 

Functions

static bool cs_selftest (void)
 Run the CRC engine self-test against a known vector.
 
void __cs_init (void)
 Privileged implementation of cs_init().
 
void __cs_set_callback (cs_mismatch_fn fn)
 Privileged implementation of cs_set_callback().
 
bool __cs_add_region (uint8_t idx, const uint8_t *addr, uint32_t size)
 Privileged implementation of cs_add_region().
 
bool __cs_enable (uint8_t idx, bool enabled)
 Privileged implementation of cs_enable().
 
bool __cs_rebaseline (uint8_t idx)
 Privileged implementation of cs_rebaseline().
 
uint8_t __cs_check_all (void)
 Privileged implementation of cs_check_all().
 
bool __cs_get_region (uint8_t idx, cs_region_t *out)
 Privileged implementation of cs_get_region().
 
uint8_t __cs_region_count (void)
 Privileged implementation of cs_region_count().
 

Variables

static cs_entry_t regions [8]
 Region table — privileged DTCM, zero wait-state.
 
static cs_mismatch_fn mismatch_cb
 User-supplied mismatch callback.
 
static bool cs_hw_ok
 CRC engine self-test result.
 

Detailed Description

Background Checksum integrity monitor implementation.

Version
0.1.0

Maintains a table of up to CS_MAX_REGIONS memory regions, each with a baseline CRC16-CCITT. cs_check_all() recomputes the CRC over each enabled region and invokes the mismatch callback on any discrepancy.

CRC computation uses crc16_ccitt() from icarus/crc.h, which runs on the STM32H7 hardware CRC peripheral on target and falls back to a software loop under HOST_TEST.

cs_init() performs a self-test of the CRC engine against a known test vector ("123456789" → 0x29B1 for CRC16-CCITT). If the self-test fails (e.g. HW CRC peripheral damaged by radiation), the cs_hw_ok flag is cleared and cs_check_all() reports region index 0xFF via the mismatch callback to signal a CRC engine failure.

Memory placement:
  • Region table + state: DTCM_DATA_PRIV
  • All functions: ITCM_FUNC
Author
Souham Biswas
Date
2026

Definition in file cs.c.

Macro Definition Documentation

◆ CS_SELFTEST_EXPECTED

#define CS_SELFTEST_EXPECTED   0x29B1u

Known CRC16-CCITT test vector.

"123456789" (9 bytes) → CRC = 0x29B1 per ITU-T V.41.

Definition at line 65 of file cs.c.

Function Documentation

◆ __cs_add_region()

bool __cs_add_region ( uint8_t  idx,
const uint8_t *  addr,
uint32_t  size 
)

Privileged implementation of cs_add_region().

Parameters
[in]idxRegion index.
[in]addrStart address.
[in]sizeSize in bytes.
Return values
trueRegion registered and baseline CRC computed.
falseInvalid parameters.

Computes the baseline CRC immediately from the current memory contents using crc16_ccitt() (HW-accelerated on target, software fallback under HOST_TEST).

Definition at line 118 of file cs.c.

References cs_entry_t::addr, cs_entry_t::baseline, crc16_ccitt(), CS_MAX_REGIONS, cs_entry_t::enabled, regions, cs_entry_t::registered, and cs_entry_t::size.

Referenced by cs_add_region(), and SVC_Handler_C().

◆ __cs_check_all()

uint8_t __cs_check_all ( void  )

Privileged implementation of cs_check_all().

Returns
Number of regions that failed the CRC check.

First checks cs_hw_ok; if the CRC engine itself is suspect, invokes the callback with region_idx = 0xFF (sentinel) and returns 1 without scanning any regions.

Otherwise iterates all enabled regions, recomputes CRC, and invokes the callback for each mismatch.

Definition at line 172 of file cs.c.

References crc16_ccitt(), cs_hw_ok, CS_MAX_REGIONS, cs_entry_t::enabled, mismatch_cb, and regions.

Referenced by cs_check_all(), and SVC_Handler_C().

◆ __cs_enable()

bool __cs_enable ( uint8_t  idx,
bool  enabled 
)

Privileged implementation of cs_enable().

Parameters
[in]idxRegion index.
[in]enabledEnable/disable flag.

Definition at line 137 of file cs.c.

References CS_MAX_REGIONS, cs_entry_t::enabled, and regions.

Referenced by cs_enable(), and SVC_Handler_C().

◆ __cs_get_region()

bool __cs_get_region ( uint8_t  idx,
cs_region_t out 
)

Privileged implementation of cs_get_region().

Parameters
[in]idxRegion index.
[out]outDestination descriptor.

Definition at line 204 of file cs.c.

References cs_region_t::addr, cs_entry_t::addr, cs_region_t::baseline, cs_entry_t::baseline, CS_MAX_REGIONS, cs_region_t::enabled, cs_entry_t::enabled, regions, cs_region_t::size, and cs_entry_t::size.

Referenced by cs_get_region(), and SVC_Handler_C().

◆ __cs_init()

void __cs_init ( void  )

Privileged implementation of cs_init().

Clears all regions, runs the CRC self-test, and stores the result in cs_hw_ok.

Definition at line 91 of file cs.c.

References cs_hw_ok, cs_selftest(), mismatch_cb, and regions.

Referenced by cs_init(), and SVC_Handler_C().

◆ __cs_rebaseline()

bool __cs_rebaseline ( uint8_t  idx)

Privileged implementation of cs_rebaseline().

Parameters
[in]idxRegion index.

Recomputes the CRC from current memory and stores it as the new baseline. Call after a legitimate write to a monitored region.

Definition at line 152 of file cs.c.

References cs_entry_t::baseline, crc16_ccitt(), CS_MAX_REGIONS, and regions.

Referenced by cs_rebaseline(), and SVC_Handler_C().

◆ __cs_region_count()

uint8_t __cs_region_count ( void  )

Privileged implementation of cs_region_count().

Returns
Number of registered (non-empty) region slots.

Definition at line 222 of file cs.c.

References CS_MAX_REGIONS, and regions.

Referenced by cs_region_count(), and SVC_Handler_C().

◆ __cs_set_callback()

void __cs_set_callback ( cs_mismatch_fn  fn)

Privileged implementation of cs_set_callback().

Parameters
[in]fnMismatch callback, or NULL to disable.

Definition at line 101 of file cs.c.

References mismatch_cb.

Referenced by cs_set_callback(), and SVC_Handler_C().

◆ cs_selftest()

static bool cs_selftest ( void  )
static

Run the CRC engine self-test against a known vector.

Return values
trueCRC engine produces the expected result.
falseCRC engine is damaged or misconfigured.

Definition at line 76 of file cs.c.

References crc16_ccitt(), and CS_SELFTEST_EXPECTED.

Referenced by __cs_init().

Variable Documentation

◆ cs_hw_ok

bool cs_hw_ok
static

CRC engine self-test result.

False if HW CRC is suspect.

Definition at line 59 of file cs.c.

Referenced by __cs_check_all(), and __cs_init().

◆ mismatch_cb

cs_mismatch_fn mismatch_cb
static

User-supplied mismatch callback.

Definition at line 56 of file cs.c.

Referenced by __cs_check_all(), __cs_init(), and __cs_set_callback().

◆ regions

cs_entry_t regions[8]
static

Region table — privileged DTCM, zero wait-state.

Definition at line 53 of file cs.c.

Referenced by __cs_add_region(), __cs_check_all(), __cs_enable(), __cs_get_region(), __cs_init(), __cs_rebaseline(), and __cs_region_count().