-
Notifications
You must be signed in to change notification settings - Fork 296
/
bootloader.c
477 lines (417 loc) · 14.4 KB
/
bootloader.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/**
******************************************************************************
* STM32 Bootloader Source
******************************************************************************
* @author Akos Pasztor
* @file bootloader.c
* @brief This file contains the functions of the bootloader. The bootloader
* implementation uses the official HAL library of ST.
* @see Please refer to README for detailed information.
******************************************************************************
* @copyright (c) 2019 Akos Pasztor. https://akospasztor.com
******************************************************************************
**/
/* Includes ------------------------------------------------------------------*/
#include "bootloader.h"
/* Private defines -----------------------------------------------------------*/
#define BOOTLOADER_VERSION_MAJOR 1 /*!< Major version */
#define BOOTLOADER_VERSION_MINOR 1 /*!< Minor version */
#define BOOTLOADER_VERSION_PATCH 0 /*!< Patch version */
#define BOOTLOADER_VERSION_RC 0 /*!< Release candidate version */
/* Private typedef -----------------------------------------------------------*/
typedef void (*pFunction)(void); /*!< Function pointer definition */
/* Private variables ---------------------------------------------------------*/
static uint32_t flash_ptr = APP_ADDRESS; /*!< Private variable for tracking flashing progress */
/**
* @brief This function initializes bootloader and flash.
* @param None
* @return Bootloader error code ::eBootloaderErrorCodes
* @retval BL_OK is returned in every case
*/
uint8_t Bootloader_Init(void)
{
__HAL_RCC_SYSCFG_CLK_ENABLE();
__HAL_RCC_FLASH_CLK_ENABLE();
/* Clear flash flags */
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
HAL_FLASH_Lock();
return BL_OK;
}
/**
* @brief This function erases the user application area in flash
* @param None
* @return Bootloader error code ::eBootloaderErrorCodes
* @retval BL_OK: upon success
* @retval BL_ERR: upon failure
*/
uint8_t Bootloader_Erase(void)
{
uint32_t NbrOfPages = 0;
uint32_t PageError = 0;
FLASH_EraseInitTypeDef pEraseInit;
HAL_StatusTypeDef status = HAL_OK;
HAL_FLASH_Unlock();
/* Get the number of pages to erase */
NbrOfPages = (FLASH_BASE + FLASH_SIZE - APP_ADDRESS) / FLASH_PAGE_SIZE;
if(NbrOfPages > FLASH_PAGE_NBPERBANK)
{
pEraseInit.Banks = FLASH_BANK_1;
pEraseInit.NbPages = NbrOfPages % FLASH_PAGE_NBPERBANK;
pEraseInit.Page = FLASH_PAGE_NBPERBANK - pEraseInit.NbPages;
pEraseInit.TypeErase = FLASH_TYPEERASE_PAGES;
status = HAL_FLASHEx_Erase(&pEraseInit, &PageError);
NbrOfPages = FLASH_PAGE_NBPERBANK;
}
if(status == HAL_OK)
{
pEraseInit.Banks = FLASH_BANK_2;
pEraseInit.NbPages = NbrOfPages;
pEraseInit.Page = FLASH_PAGE_NBPERBANK - pEraseInit.NbPages;
pEraseInit.TypeErase = FLASH_TYPEERASE_PAGES;
status = HAL_FLASHEx_Erase(&pEraseInit, &PageError);
}
HAL_FLASH_Lock();
return (status == HAL_OK) ? BL_OK : BL_ERASE_ERROR;
}
/**
* @brief Begin flash programming: this function unlocks the flash and sets
* the data pointer to the start of application flash area.
* @see README for futher information
* @return Bootloader error code ::eBootloaderErrorCodes
* @retval BL_OK is returned in every case
*/
uint8_t Bootloader_FlashBegin(void)
{
/* Reset flash destination address */
flash_ptr = APP_ADDRESS;
/* Unlock flash */
HAL_FLASH_Unlock();
return BL_OK;
}
/**
* @brief Program 64bit data into flash: this function writes a 4byte (64bit)
* data chunk into the flash and increments the data pointer.
* @see README for futher information
* @param data: 64bit data chunk to be written into flash
* @return Bootloader error code ::eBootloaderErrorCodes
* @retval BL_OK: upon success
* @retval BL_WRITE_ERROR: upon failure
*/
uint8_t Bootloader_FlashNext(uint64_t data)
{
if( !(flash_ptr <= (FLASH_BASE + FLASH_SIZE - 8)) || (flash_ptr < APP_ADDRESS) )
{
HAL_FLASH_Lock();
return BL_WRITE_ERROR;
}
if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, flash_ptr, data) == HAL_OK)
{
/* Check the written value */
if(*(uint64_t*)flash_ptr != data)
{
/* Flash content doesn't match source content */
HAL_FLASH_Lock();
return BL_WRITE_ERROR;
}
/* Increment Flash destination address */
flash_ptr += 8;
}
else
{
/* Error occurred while writing data into Flash */
HAL_FLASH_Lock();
return BL_WRITE_ERROR;
}
return BL_OK;
}
/**
* @brief Finish flash programming: this function finalizes the flash
* programming by locking the flash.
* @see README for futher information
* @param None
* @return Bootloader error code ::eBootloaderErrorCodes
* @retval BL_OK is returned in every case
*/
uint8_t Bootloader_FlashEnd(void)
{
/* Lock flash */
HAL_FLASH_Lock();
return BL_OK;
}
/**
* @brief This function returns the protection status of flash.
* @param None
* @return Flash protection status ::eFlashProtectionTypes
*/
uint8_t Bootloader_GetProtectionStatus(void)
{
FLASH_OBProgramInitTypeDef OBStruct = {0};
uint8_t protection = BL_PROTECTION_NONE;
HAL_FLASH_Unlock();
/* Bank 1 */
OBStruct.PCROPConfig = FLASH_BANK_1;
OBStruct.WRPArea = OB_WRPAREA_BANK1_AREAA;
HAL_FLASHEx_OBGetConfig(&OBStruct);
/* PCROP */
if(OBStruct.PCROPEndAddr > OBStruct.PCROPStartAddr)
{
if(OBStruct.PCROPStartAddr >= APP_ADDRESS)
{
protection |= BL_PROTECTION_PCROP;
}
}
/* WRP Area_A */
if(OBStruct.WRPEndOffset > OBStruct.WRPStartOffset)
{
if((OBStruct.WRPStartOffset * FLASH_PAGE_SIZE + FLASH_BASE) >= APP_ADDRESS)
{
protection |= BL_PROTECTION_WRP;
}
}
OBStruct.WRPArea = OB_WRPAREA_BANK1_AREAB;
HAL_FLASHEx_OBGetConfig(&OBStruct);
/* WRP Area_B */
if(OBStruct.WRPEndOffset > OBStruct.WRPStartOffset)
{
if((OBStruct.WRPStartOffset * FLASH_PAGE_SIZE + FLASH_BASE) >= APP_ADDRESS)
{
protection |= BL_PROTECTION_WRP;
}
}
/* Bank 2 */
OBStruct.PCROPConfig = FLASH_BANK_2;
OBStruct.WRPArea = OB_WRPAREA_BANK2_AREAA;
HAL_FLASHEx_OBGetConfig(&OBStruct);
/* PCROP */
if(OBStruct.PCROPEndAddr > OBStruct.PCROPStartAddr)
{
if(OBStruct.PCROPStartAddr >= APP_ADDRESS)
{
protection |= BL_PROTECTION_PCROP;
}
}
/* WRP Area_A */
if(OBStruct.WRPEndOffset > OBStruct.WRPStartOffset)
{
if((OBStruct.WRPStartOffset * FLASH_PAGE_SIZE + FLASH_BASE + FLASH_PAGE_SIZE * FLASH_PAGE_NBPERBANK) >= APP_ADDRESS)
{
protection |= BL_PROTECTION_WRP;
}
}
OBStruct.WRPArea = OB_WRPAREA_BANK2_AREAB;
HAL_FLASHEx_OBGetConfig(&OBStruct);
/* WRP Area_B */
if(OBStruct.WRPEndOffset > OBStruct.WRPStartOffset)
{
if((OBStruct.WRPStartOffset * FLASH_PAGE_SIZE + FLASH_BASE + FLASH_PAGE_SIZE * FLASH_PAGE_NBPERBANK) >= APP_ADDRESS)
{
protection |= BL_PROTECTION_WRP;
}
}
/* RDP */
if(OBStruct.RDPLevel != OB_RDP_LEVEL_0)
{
protection |= BL_PROTECTION_RDP;
}
HAL_FLASH_Lock();
return protection;
}
/**
* @brief This function configures the wirte protection of flash.
* @param protection: protection type ::eFlashProtectionTypes
* @return Bootloader error code ::eBootloaderErrorCodes
* @retval BL_OK: upon success
* @retval BL_OBP_ERROR: upon failure
*/
uint8_t Bootloader_ConfigProtection(uint32_t protection)
{
FLASH_OBProgramInitTypeDef OBStruct = {0};
HAL_StatusTypeDef status = HAL_ERROR;
status = HAL_FLASH_Unlock();
status |= HAL_FLASH_OB_Unlock();
/* Bank 1 */
OBStruct.WRPArea = OB_WRPAREA_BANK1_AREAA;
OBStruct.OptionType = OPTIONBYTE_WRP;
if(protection & BL_PROTECTION_WRP)
{
/* Enable WRP protection for application space */
OBStruct.WRPStartOffset = (APP_ADDRESS - FLASH_BASE) / FLASH_PAGE_SIZE;
OBStruct.WRPEndOffset = FLASH_PAGE_NBPERBANK - 1;
}
else
{
/* Remove WRP protection */
OBStruct.WRPStartOffset = 0xFF;
OBStruct.WRPEndOffset = 0x00;
}
status |= HAL_FLASHEx_OBProgram(&OBStruct);
/* Area B is not used */
OBStruct.WRPArea = OB_WRPAREA_BANK1_AREAB;
OBStruct.OptionType = OPTIONBYTE_WRP;
OBStruct.WRPStartOffset = 0xFF;
OBStruct.WRPEndOffset = 0x00;
status |= HAL_FLASHEx_OBProgram(&OBStruct);
/* Bank 2 */
OBStruct.WRPArea = OB_WRPAREA_BANK2_AREAA;
OBStruct.OptionType = OPTIONBYTE_WRP;
if(protection & BL_PROTECTION_WRP)
{
/* Enable WRP protection for application space */
OBStruct.WRPStartOffset = 0x00;
OBStruct.WRPEndOffset = FLASH_PAGE_NBPERBANK - 1;
}
else
{
/* Remove WRP protection */
OBStruct.WRPStartOffset = 0xFF;
OBStruct.WRPEndOffset = 0x00;
}
status |= HAL_FLASHEx_OBProgram(&OBStruct);
/* Area B is not used */
OBStruct.WRPArea = OB_WRPAREA_BANK2_AREAB;
OBStruct.OptionType = OPTIONBYTE_WRP;
OBStruct.WRPStartOffset = 0xFF;
OBStruct.WRPEndOffset = 0x00;
status |= HAL_FLASHEx_OBProgram(&OBStruct);
if(status == HAL_OK)
{
/* Loading Flash Option Bytes - this generates a system reset. */
status |= HAL_FLASH_OB_Launch();
}
status |= HAL_FLASH_OB_Lock();
status |= HAL_FLASH_Lock();
return (status == HAL_OK) ? BL_OK : BL_OBP_ERROR;
}
/**
* @brief This function checks whether the new application fits into flash.
* @param appsize: size of application
* @return Bootloader error code ::eBootloaderErrorCodes
* @retval BL_OK: if application fits into flash
* @retval BL_SIZE_ERROR: if application does not fit into flash
*/
uint8_t Bootloader_CheckSize(uint32_t appsize)
{
return ((FLASH_BASE + FLASH_SIZE - APP_ADDRESS) >= appsize) ? BL_OK : BL_SIZE_ERROR;
}
/**
* @brief This function verifies the checksum of application located in flash.
* If ::USE_CHECKSUM configuration parameter is disabled then the
* function always returns an error code.
* @param None
* @return Bootloader error code ::eBootloaderErrorCodes
* @retval BL_OK: if calculated checksum matches the application checksum
* @retval BL_CHKS_ERROR: upon checksum mismatch or when ::USE_CHECKSUM is
* disabled
*/
uint8_t Bootloader_VerifyChecksum(void)
{
#if (USE_CHECKSUM)
CRC_HandleTypeDef CrcHandle;
volatile uint32_t calculatedCrc = 0;
__HAL_RCC_CRC_CLK_ENABLE();
CrcHandle.Instance = CRC;
CrcHandle.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_ENABLE;
CrcHandle.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_ENABLE;
CrcHandle.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_NONE;
CrcHandle.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLE;
CrcHandle.InputDataFormat = CRC_INPUTDATA_FORMAT_WORDS;
if(HAL_CRC_Init(&CrcHandle) != HAL_OK)
{
return BL_CHKS_ERROR;
}
calculatedCrc = HAL_CRC_Calculate(&CrcHandle, (uint32_t*)APP_ADDRESS, APP_SIZE);
__HAL_RCC_CRC_FORCE_RESET();
__HAL_RCC_CRC_RELEASE_RESET();
if( (*(uint32_t*)CRC_ADDRESS) == calculatedCrc )
{
return BL_OK;
}
#endif
return BL_CHKS_ERROR;
}
/**
* @brief This function checks whether a valid application exists in flash.
* The check is performed by checking the very first DWORD (4 bytes) of
* the application firmware. In case of a valid application, this DWORD
* must represent the initialization location of stack pointer - which
* must be within the boundaries of RAM.
* @param None
* @return Bootloader error code ::eBootloaderErrorCodes
* @retval BL_OK: if first DWORD represents a valid stack pointer location
* @retval BL_NO_APP: first DWORD value is out of RAM boundaries
*/
uint8_t Bootloader_CheckForApplication(void)
{
return (((*(uint32_t*)APP_ADDRESS) - RAM_BASE) < RAM_SIZE) ? BL_OK : BL_NO_APP;
}
/**
* @brief This function performs the jump to the user application in flash.
* @details The function carries out the following operations:
* - De-initialize the clock and peripheral configuration
* - Stop the systick
* - Set the vector table location (if ::SET_VECTOR_TABLE is enabled)
* - Sets the stack pointer location
* - Perform the jump
* @param None
* @retval None
*/
void Bootloader_JumpToApplication(void)
{
uint32_t JumpAddress = *(__IO uint32_t*)(APP_ADDRESS + 4);
pFunction Jump = (pFunction)JumpAddress;
HAL_RCC_DeInit();
HAL_DeInit();
SysTick->CTRL = 0;
SysTick->LOAD = 0;
SysTick->VAL = 0;
#if (SET_VECTOR_TABLE)
SCB->VTOR = APP_ADDRESS;
#endif
__set_MSP(*(__IO uint32_t*)APP_ADDRESS);
Jump();
}
/**
* @brief This function performs the jump to the MCU System Memory (ST
* Bootloader).
* @details The function carries out the following operations:
* - De-initialize the clock and peripheral configuration
* - Stop the systick
* - Remap the system flash memory
* - Perform the jump
* @param None
* @retval None
*/
void Bootloader_JumpToSysMem(void)
{
uint32_t JumpAddress = *(__IO uint32_t*)(SYSMEM_ADDRESS + 4);
pFunction Jump = (pFunction)JumpAddress;
HAL_RCC_DeInit();
HAL_DeInit();
SysTick->CTRL = 0;
SysTick->LOAD = 0;
SysTick->VAL = 0;
__HAL_RCC_SYSCFG_CLK_ENABLE();
__HAL_SYSCFG_REMAPMEMORY_SYSTEMFLASH();
__set_MSP(*(__IO uint32_t*)SYSMEM_ADDRESS);
Jump();
while(1);
}
/**
* @brief This function returns the version number of the bootloader library.
* Semantic versioning is used for numbering.
* @see Semantic versioning: https://semver.org
* @param None
* @return Bootloader version number combined into an uint32_t:
* - [31:24] Major version
* - [23:16] Minor version
* - [15:8] Patch version
* - [7:0] Release candidate version
*/
uint32_t Bootloader_GetVersion(void)
{
return ((BOOTLOADER_VERSION_MAJOR << 24) |
(BOOTLOADER_VERSION_MINOR << 16) |
(BOOTLOADER_VERSION_PATCH << 8) |
(BOOTLOADER_VERSION_RC));
}