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
9#ifdef STM32H7XX
10//Since a 10-bit ECC code is associated to each 256-bit data Flash word,
11//only write operations by 256 bits are executed in the non-volatile memory.
12#define FLASH_ECC_LINE_SIZE (256 / 8)
13
14static blt_int8u flashBuffer[FLASH_ECC_LINE_SIZE];
15static blt_addr flashAddr = 0x0;
16
17#define FLASH_WRITE_STEP FLASH_ECC_LINE_SIZE
18#define FLASH_WRITE_ADDR_MASK (~(FLASH_WRITE_STEP - 1))
19
20static void FlashBufferReset()
21{
22 memset(flashBuffer, 0xff, sizeof(flashBuffer));
23 flashAddr = 0;
24}
25
27{
28 if (flashAddr == 0x0) {
29 return BLT_TRUE;
30 }
31
32 int result = intFlashWrite(flashAddr, (const char*)flashBuffer, sizeof(flashBuffer));
33
35
36 return (result == FLASH_RETURN_SUCCESS) ? BLT_TRUE : BLT_FALSE;
37}
38
40{
41 while (len) {
42 if ((addr & FLASH_WRITE_ADDR_MASK) != flashAddr) {
43 // crossing ECC line boundary
45
46 flashAddr = addr & FLASH_WRITE_ADDR_MASK;
47 }
48
49 off_t off = addr - flashAddr;
50 size_t chunk = minI(sizeof(flashBuffer) - off, len);
51 memcpy(flashBuffer + off, data, chunk);
52
53 addr += chunk;
54 data += chunk;
55 len -= chunk;
56 }
57
58 return BLT_TRUE;
59}
60
61#endif
62
63void FlashInit() {
64 // Flash already init by ChibiOS
65#ifdef STM32H7XX
67#endif
68}
69
71#ifdef STM32H7XX
72 return FLASH_BASE + 128 * 1024;
73#else // not STM32H7
74 return FLASH_BASE + 32 * 1024;
75#endif
76}
77
79 // don't allow overwriting the bootloader
81 return BLT_FALSE;
82 }
83
84#ifdef STM32H7XX
85 return FlashBufferedWrite(addr, len, data);
86#else // not STM32H7
87 return (FLASH_RETURN_SUCCESS == intFlashWrite(addr, (const char*)data, len)) ? BLT_TRUE : BLT_FALSE;
88#endif
89}
90
92 // don't allow erasing the bootloader
94 return BLT_FALSE;
95 }
96
97 if (!intFlashIsErased(addr, len)) {
98 return (FLASH_RETURN_SUCCESS == intFlashErase(addr, len)) ? BLT_TRUE : BLT_FALSE;
99 }
100
101 return BLT_TRUE;
102}
103
105#ifdef STM32H7XX
107#endif
108 return BLT_TRUE;
109}
110
112 return BLT_TRUE;
113}
114
116 // Naive check: if the first block is blank, there's no code there
118 return BLT_FALSE;
119 }
120
121 static const size_t checksumOffset = 0x1C;
122
123 // Now do the actual CRC check to ensure we didn't get stuck with a half-written firmware image
124 uint8_t* start = reinterpret_cast<uint8_t*>(FlashGetUserProgBaseAddress());
125
126 size_t imageSize = *reinterpret_cast<size_t*>(start + checksumOffset + 4);
127
128 if (imageSize > 1024 * 1024) {
129 // impossibly large size, invalid
130 return BLT_FALSE;
131 }
132
133 // part before checksum+size
134 uint32_t calcChecksum = crc32(start, checksumOffset);
135 // part after checksum+size
136 calcChecksum = crc32inc(start + checksumOffset + 4, calcChecksum, imageSize - (checksumOffset + 4));
137
138 uint32_t storedChecksum = *reinterpret_cast<uint32_t*>(start + checksumOffset);
139
140 return calcChecksum == storedChecksum ? BLT_TRUE : BLT_FALSE;
141}
142
144#ifdef STM32F7XX
145 // cleared bit indicates dual bank
146 return (FLASH->OPTCR & FLASH_OPTCR_nDBANK) == 0 ? BLT_TRUE : BLT_FALSE;
147#else
148 return BLT_TRUE;
149#endif
150}
constexpr uint8_t addr
Definition ads1015.cpp:14
beuint16_t off
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)
static blt_int8u flashBuffer[FLASH_ECC_LINE_SIZE]
blt_bool FlashVerifyChecksum()
static blt_bool FlashBufferedWrite(blt_addr addr, blt_int32u len, blt_int8u *data)
blt_addr FlashGetUserProgBaseAddress()
void FlashInit()
static blt_bool FlashBufferFlush()
static blt_addr flashAddr
blt_bool FlashDone()
blt_bool FlashWriteChecksum()
static void FlashBufferReset()
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