MCCI Trusted Bootloader
Simple trusted bootloader and tools for small embedded systems
mccibootloader_programandcheckflash.c
Go to the documentation of this file.
1/*
2
3Module: mccibootloader_programandcheckflash.c
4
5Function:
6 McciBootloader_programAndCheckflash()
7
8Copyright and License:
9 This file copyright (C) 2021 by
10
11 MCCI Corporation
12 3520 Krums Corners Road
13 Ithaca, NY 14850
14
15 See accompanying LICENSE file for copyright and license information.
16
17Author:
18 Terry Moore, MCCI Corporation March 2021
19
20*/
21
22#include "mcci_bootloader.h"
23
26
27/****************************************************************************\
28|
29| Manifest constants & typedefs.
30|
31\****************************************************************************/
32
33
34
35/****************************************************************************\
36|
37| Read-only data.
38|
39\****************************************************************************/
40
41
42
43/****************************************************************************\
44|
45| Variables.
46|
47\****************************************************************************/
48
49
50/*
51
52Name: McciBootloader_programAndCheckFlash()
53
54Function:
55 Program internal flash from storage image
56
57Definition:
58 McciBootloaderError_t McciBootloader_programAndCheckFlash(
59 McciBootloaderStorageAddress_t storageAddress,
60 const McciBootloader_AppInfo_t *pAppInfo
61 );
62
63Description:
64 Copy the selected image to internal flash in the following
65 sequence.
66
67 1. Erase the current contents of internal flash
68 2. Read through the image one buffer at a time, programming
69 the internal flash
70 3. Check the hash on the image
71
72Returns:
73 McciBootloaderError_t_OK only if the image was programmed and
74 the hash matches; otherwise a failure code.
75
76Notes:
77
78
79*/
80
83 McciBootloaderStorageAddress_t storageAddress,
84 const McciBootloader_AppInfo_t *pAppInfo
85 )
86 {
87 volatile const uint8_t * const targetAddress = (volatile const uint8_t *) pAppInfo->targetAddress;
88 size_t const overallSizeTight = pAppInfo->imagesize + pAppInfo->authsize;
89 const size_t blockSize = sizeof(g_McciBootloader_imageBlock);
90 size_t const overallSize = (overallSizeTight + blockSize - 1) & ~(blockSize - 1);
91
92 // erase in 4k chunks, to match program size.
94 targetAddress, overallSize
95 ))
97
98 // program in 4k chunks, up to the block that includes the
99 // last byte of the signature
100 McciBootloaderStorageAddress_t const addressEnd =
101 storageAddress + overallSize;
102
103 McciBootloaderStorageAddress_t addressCurrent;
104 volatile const uint8_t *targetCurrent;
105
106 for (addressCurrent = storageAddress, targetCurrent = targetAddress;
107 addressCurrent < addressEnd;
108 addressCurrent += blockSize, targetCurrent += blockSize)
109 {
110 /* read a block */
112 addressCurrent,
114 blockSize
115 ))
116 {
118 }
119
120 /* program this block */
122 targetCurrent,
124 blockSize
125 ))
126 {
128 }
129 }
130
131 /* finally, check the image */
133 (const void *)targetAddress, overallSizeTight
134 ))
135 {
137 }
138
140 }
141
142/**** end of mccibootloader_programandcheckflash.c ****/
@ McciBootloaderError_FlashWriteFailed
flash write failed during programming
@ McciBootloaderError_ReadFailed
storage read failed during program
@ McciBootloaderError_OK
successful
@ McciBootloaderError_FlashVerifyFailed
flash verify failed after programming
@ McciBootloaderError_EraseFailed
erase failed
static bool McciBootloaderPlatform_systemFlashWrite(volatile const void *pDestination, const void *pSource, size_t nBytes)
static bool McciBootloaderPlatform_systemFlashErase(volatile const void *targetAddress, size_t targetSize)
static bool McciBootloaderPlatform_storageRead(McciBootloaderStorageAddress_t hAddress, uint8_t *pBuffer, size_t nBuffer)
uint32_t McciBootloaderError_t
error codes for the bootloader
uint32_t McciBootloaderStorageAddress_t
Abstract type for storage byte addresses.
bool McciBootloader_checkCodeValid(const void *pBase, size_t nBytes)
uint8_t g_McciBootloader_imageBlock[4096]
McciBootloaderError_t McciBootloader_programAndCheckFlash(McciBootloaderStorageAddress_t storageAddress, const McciBootloader_AppInfo_t *pAppInfo)