ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_dfu_boot.c
Go to the documentation of this file.
1
18
19#include "ra8_dfu.h"
20
25typedef enum : uint32_t {
26 k_ra8_dfu_crc_init = 0xFFFFFFFFU,
27 k_ra8_dfu_crc_poly = 0xEDB88320U,
28 k_ra8_dfu_crc_xout = 0xFFFFFFFFU,
29 k_ra8_dfu_crc_lsb = 0x00000001U,
31
33typedef enum : uint8_t {
36
38uint32_t ra8_dfu_crc32(const uint8_t* data, uint32_t len)
39{
40 uint32_t crc = (uint32_t)k_ra8_dfu_crc_init;
41 const uint32_t bound = (data == nullptr) ? 0U : len;
42 for (uint32_t i = 0U; i < bound; ++i) {
43 crc ^= (uint32_t)data[i];
44 for (uint8_t b = 0U; b < (uint8_t)k_ra8_dfu_crc_bits; ++b) {
45 if ((crc & (uint32_t)k_ra8_dfu_crc_lsb) != 0U) {
46 crc = (uint32_t)((crc >> 1U) ^ (uint32_t)k_ra8_dfu_crc_poly);
47 } else {
48 crc = (uint32_t)(crc >> 1U);
49 }
50 }
51 }
52 return (uint32_t)(crc ^ (uint32_t)k_ra8_dfu_crc_xout);
53}
54
55bool ra8_dfu_hdr_valid(const ra8_dfu_img_hdr_t* hdr, uint32_t computed_crc)
56{
57 if (hdr == nullptr) {
58 return false;
59 }
60 const bool magic_ok = (hdr->magic == (uint32_t)k_ra8_dfu_hdr_magic);
61 const bool len_ok = (hdr->img_len != 0U) && (hdr->img_len <= (uint32_t)k_ra8_dfu_img_max) &&
62 ((hdr->img_len % (uint32_t)k_ra8_dfu_page_size) == 0U);
63 const bool crc_ok = (computed_crc == hdr->img_crc32);
64 return magic_ok && len_ok && crc_ok;
65}
66
67bool ra8_dfu_run_target_valid(uint32_t entry, uint32_t img_len)
68{
69 const bool entry_ok = (entry == (uint32_t)k_ra8_dfu_run_base);
70 const bool len_ok = (img_len != 0U) && (img_len <= (uint32_t)k_ra8_dfu_img_max) &&
71 ((img_len % (uint32_t)k_ra8_dfu_page_size) == 0U);
72 return entry_ok && len_ok;
73}
74
75ra8_dfu_slot_t ra8_dfu_select_slot(bool a_valid, uint32_t a_seq, bool b_valid, uint32_t b_seq)
76{
77 if (!a_valid && !b_valid) {
79 }
80 if (a_valid && (!b_valid || (a_seq >= b_seq))) {
81 return k_ra8_dfu_slot_a;
82 }
83 return k_ra8_dfu_slot_b;
84}
85
87ra8_dfu_boot_decide(bool dfu_trigger, bool a_valid, uint32_t a_seq, bool b_valid, uint32_t b_seq)
88{
89 if (dfu_trigger) {
91 }
92 const ra8_dfu_slot_t slot = ra8_dfu_select_slot(a_valid, a_seq, b_valid, b_seq);
93 if (slot == k_ra8_dfu_slot_a) {
95 }
96 if (slot == k_ra8_dfu_slot_b) {
98 }
100}
Controller-agnostic USB-DFU MRAM bootloader core for the RA8D2.
ra8_dfu_slot_t
Application-slot identifier.
Definition ra8_dfu.h:133
@ k_ra8_dfu_slot_b
Slot B (0x02090000).
Definition ra8_dfu.h:135
@ k_ra8_dfu_slot_a
Slot A (0x02020000).
Definition ra8_dfu.h:134
@ k_ra8_dfu_slot_none
No valid slot present.
Definition ra8_dfu.h:136
ra8_dfu_action_t
Outcome of the reset-time boot decision.
Definition ra8_dfu.h:143
@ k_ra8_dfu_action_jump_b
Jump to the Slot B application.
Definition ra8_dfu.h:146
@ k_ra8_dfu_action_dfu
Enter the DFU device; do not jump.
Definition ra8_dfu.h:144
@ k_ra8_dfu_action_jump_a
Jump to the Slot A application.
Definition ra8_dfu.h:145
@ k_ra8_dfu_page_size
MRAM program page (32 bytes).
Definition ra8_dfu.h:101
@ k_ra8_dfu_hdr_magic
Valid-image header magic ("RA8D").
Definition ra8_dfu.h:105
@ k_ra8_dfu_img_max
Max image bytes (slot - header).
Definition ra8_dfu.h:103
@ k_ra8_dfu_run_base
SRAM copy-to-run / payload link base.
Definition ra8_dfu.h:126
ra8_dfu_crc_const_t
IEEE-802.3 reflected-CRC32 parameters (poly / init / final XOR).
@ k_ra8_dfu_crc_xout
Final output XOR.
@ k_ra8_dfu_crc_lsb
Low-bit test mask.
@ k_ra8_dfu_crc_poly
Reflected poly of 0x04C11DB7.
@ k_ra8_dfu_crc_init
Shift-register preset.
ra8_dfu_slot_t ra8_dfu_select_slot(bool a_valid, uint32_t a_seq, bool b_valid, uint32_t b_seq)
Pick the active slot from the two slots' validity + sequence.
ra8_dfu_crc_bits_t
Bits folded per input byte in the CRC32 inner loop.
@ k_ra8_dfu_crc_bits
One byte == eight shift iterations.
uint32_t ra8_dfu_crc32(const uint8_t *data, uint32_t len)
Implementation of ra8_dfu_crc32() – bitwise reflected CRC32.
bool ra8_dfu_hdr_valid(const ra8_dfu_img_hdr_t *hdr, uint32_t computed_crc)
Decide whether a slot header describes a valid bootable image.
bool ra8_dfu_run_target_valid(uint32_t entry, uint32_t img_len)
Decide whether a validated slot's image may be copied-to-run.
ra8_dfu_action_t ra8_dfu_boot_decide(bool dfu_trigger, bool a_valid, uint32_t a_seq, bool b_valid, uint32_t b_seq)
Reset-time boot decision: jump to a slot, or enter DFU.
32-byte application-image header at the base of each slot.
Definition ra8_dfu.h:174
uint32_t img_crc32
CRC32 (IEEE) over the image body.
Definition ra8_dfu.h:178
uint32_t magic
Must equal k_ra8_dfu_hdr_magic.
Definition ra8_dfu.h:175
uint32_t img_len
Image body length in bytes (32-byte multiple).
Definition ra8_dfu.h:177