rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
openblt_flash.cpp
Go to the documentation of this file.
1#include "pch.h"
2#include "flash_int.h"
3
4extern "C" {
5 #include "boot.h"
6 #include "flash.h"
7}
8
9void FlashInit() {
10 // Flash already init by ChibiOS
11}
12
14#ifdef STM32H7XX
15 return FLASH_BASE + 128 * 1024;
16#else // not STM32H7
17 return FLASH_BASE + 32 * 1024;
18#endif
19}
20
22 // don't allow overwriting the bootloader
24 return BLT_FALSE;
25 }
26
27 return (FLASH_RETURN_SUCCESS == intFlashWrite(addr, (const char*)data, len)) ? BLT_TRUE : BLT_FALSE;
28
29 return BLT_TRUE;
30}
31
33 // don't allow erasing the bootloader
35 return BLT_FALSE;
36 }
37
38 if (!intFlashIsErased(addr, len)) {
39 return (FLASH_RETURN_SUCCESS == intFlashErase(addr, len)) ? BLT_TRUE : BLT_FALSE;
40 }
41
42 return BLT_TRUE;
43}
44
46 return BLT_TRUE;
47}
48
50 return BLT_TRUE;
51}
52
54 // Naive check: if the first block is blank, there's no code there
56 return BLT_FALSE;
57 }
58
59 static const size_t checksumOffset = 0x1C;
60
61 // Now do the actual CRC check to ensure we didn't get stuck with a half-written firmware image
62 uint8_t* start = reinterpret_cast<uint8_t*>(FlashGetUserProgBaseAddress());
63
64 size_t imageSize = *reinterpret_cast<size_t*>(start + checksumOffset + 4);
65
66 if (imageSize > 1024 * 1024) {
67 // impossibly large size, invalid
68 return BLT_FALSE;
69 }
70
71 // part before checksum+size
72 uint32_t calcChecksum = crc32(start, checksumOffset);
73 // part after checksum+size
74 calcChecksum = crc32inc(start + checksumOffset + 4, calcChecksum, imageSize - (checksumOffset + 4));
75
76 uint32_t storedChecksum = *reinterpret_cast<uint32_t*>(start + checksumOffset);
77
78 return calcChecksum == storedChecksum ? BLT_TRUE : BLT_FALSE;
79}
80
82#ifdef STM32F7XX
83 // cleared bit indicates dual bank
84 return (FLASH->OPTCR & FLASH_OPTCR_nDBANK) == 0 ? BLT_TRUE : BLT_FALSE;
85#else
86 return BLT_TRUE;
87#endif
88}
constexpr uint8_t addr
Definition ads1015.cpp:14
return FLASH_RETURN_SUCCESS
Definition flash_int.cpp:80
int intFlashErase(flashaddr_t address, size_t size)
Erase the sectors containing the span of size bytes starting at address.
int intFlashWrite(flashaddr_t address, const char *buffer, size_t size)
Copy data from a buffer to the flash memory.
bool intFlashIsErased(flashaddr_t address, size_t size)
Check if the size bytes of flash memory starting at address are erased.
Definition flash_int.cpp:89
blt_bool isFlashDualBank(void)
blt_bool FlashVerifyChecksum()
blt_addr FlashGetUserProgBaseAddress()
void FlashInit()
blt_bool FlashDone()
blt_bool FlashWriteChecksum()
blt_bool FlashWrite(blt_addr addr, blt_int32u len, blt_int8u *data)
blt_bool FlashErase(blt_addr addr, blt_int32u len)
unsigned char blt_int8u
Definition types.h:49
unsigned char blt_bool
Definition types.h:46
unsigned int blt_int32u
Definition types.h:53
unsigned long blt_addr
Definition types.h:48