Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
ApplicationChecksum.hxx
Go to the documentation of this file.
1
35#ifndef _OPENLCB_APPLICATIONCHECKSUM_HXX_
36#define _OPENLCB_APPLICATIONCHECKSUM_HXX_
37
38#ifdef ESP_PLATFORM
39#include "bootloader_hal.h"
40#else
42#endif
43
44extern "C" {
47{
48 uint32_t checksum[CHECKSUM_COUNT];
49 const void *flash_min;
50 const void *flash_max;
51 const struct app_header *app_header_ptr;
52 get_flash_boundaries(&flash_min, &flash_max, &app_header_ptr);
53
55 memcpy(&app_header, app_header_ptr, sizeof(app_header));
56
57 uint32_t pre_size = reinterpret_cast<const uint8_t *>(app_header_ptr) -
58 static_cast<const uint8_t *>(flash_min);
59 checksum_data(flash_min, pre_size, checksum);
60 if (memcmp(app_header.checksum_pre, checksum, sizeof(checksum)))
61 {
62 return false;
63 }
64 size_t flash_size = (size_t)flash_max - (size_t)flash_min;
65 if (app_header.app_size > flash_size) return false;
66 uint32_t post_offset = sizeof(struct app_header) +
67 (reinterpret_cast<const uint8_t *>(app_header_ptr) -
68 static_cast<const uint8_t *>(flash_min));
69 uint32_t post_size = (post_offset < app_header.app_size)
70 ? app_header.app_size - post_offset
71 : 0;
72 checksum_data(app_header_ptr + 1, post_size, checksum);
73 if (memcmp(app_header.checksum_post, checksum, sizeof(checksum)))
74 {
75 return false;
76 }
77 return true;
78}
79}
80
81#endif // _OPENLCB_APPLICATIONCHECKSUM_HXX_
bool check_application_checksum()
void get_flash_boundaries(const void **flash_min, const void **flash_max, const struct app_header **app_header)
Callback from the bootloader to retrieve flash boundaries.
void checksum_data(const void *data, uint32_t size, uint32_t *checksum)
Callback from the bootloader to calculate the checksum of a data block.
#define CHECKSUM_COUNT
Number of 32-bit words in one checksum data.
Static definitions of the application.
uint32_t app_size
The total length of the application binary.
uint32_t checksum_pre[CHECKSUM_COUNT]
Checksum data for the application info.
uint32_t checksum_post[CHECKSUM_COUNT]
checksum_post: is the checksum of the bytes between the app_header and the application end.