MCCI Trusted Bootloader
Simple trusted bootloader and tools for small embedded systems
mccibootloaderplatform_checkimagevalid.c
Go to the documentation of this file.
1/*
2
3Module: mccibootloaderplatform_checkimagevalid.c
4
5Function:
6 McciBootloaderPlatform_checkImageValid()
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
23
24#include "mcci_bootloader.h"
26
27#include "mcci_tweetnacl_hash.h"
28#include "mcci_tweetnacl_sign.h"
29
30/****************************************************************************\
31|
32| Manifest constants & typedefs.
33|
34\****************************************************************************/
35
36
37
38/****************************************************************************\
39|
40| Read-only data.
41|
42\****************************************************************************/
43
44
45
46/****************************************************************************\
47|
48| Variables.
49|
50\****************************************************************************/
51
52
53/*
54
55Name: McciBootloaderPlatform_checkImageValid()
56
57Function:
58 Validate an image header according to the architectural rules.
59
60Definition:
61 const McciBootloader_AppInfo_t *
62 McciBootloaderPlatform_checkImageValid(
63 const void *pHeader,
64 size_t nHeader,
65 uintptr_t targetAddress,
66 size_t targetSize
67 );
68
69Description:
70 Given a pointer to the header of an image (not necessarily at
71 its final location), a count of the image bytes, and a description
72 of where the image is intended to live, this function confirms
73 that the image is valid for execution.
74
75Returns:
76 non-NULL pointer to the AppInfo block if the image passes all the
77 checks, NLL if it fails any.
78
79Notes:
80
81
82*/
83
84const McciBootloader_AppInfo_t *
86 const void *pHeader,
87 size_t nHeader,
88 uintptr_t targetAddress,
89 size_t targetSize
90 )
91 {
92 const McciBootloader_CortexPageZero_t * const pPageZero = pHeader;
93
94 if (nHeader < sizeof(*pPageZero))
95 return NULL;
96
97 /* check the stack pointer */
98 {
99 uint32_t pStack = pPageZero->CortexAppEntry.stack;
100
101 if (pStack & 3)
102 return NULL;
103
104 /* stack pointer must be reasonable */
105 if (pStack < (uint32_t) &g_McciBootloader_SocRamBase + 16)
106 return NULL;
107 if ((uint32_t) &g_McciBootloader_SocRamTop < pStack)
108 return NULL;
109 }
110
111 /* check the program counter */
112 {
113 uint32_t pEntry = pPageZero->CortexAppEntry.entry;
114
115 /* must be odd-aligned indicating Thumb instructions */
116 if ((pEntry & 1) == 0)
117 return NULL;
118
119 /* must be in the target region */
120 pEntry &= ~UINT32_C(1);
121 if (pEntry < targetAddress + sizeof(*pPageZero))
122 return NULL;
123
124 if (targetAddress + targetSize <= pEntry)
125 return NULL;
126 }
127
128 const McciBootloader_AppInfo_t * const pAppInfo =
130 pPageZero, sizeof(*pPageZero)
131 );
132
133 if (pAppInfo == NULL)
134 return NULL;
135
136 if (pAppInfo->targetAddress != targetAddress)
137 return NULL;
138
139 if (pAppInfo->authsize != sizeof(McciBootloader_SignatureBlock_t))
140 return NULL;
141
142 if (pAppInfo->imagesize + pAppInfo->authsize > targetSize)
143 return NULL;
144
145 return pAppInfo;
146 }
147
148/**** end of mccibootloaderplatform_checkimagevalid.c ****/
void * g_McciBootloader_SocRamTop
void * g_McciBootloader_SocRamBase
const McciBootloader_AppInfo_t * McciBootloaderPlatform_checkImageValid(const void *pHeader, size_t nHeader, uintptr_t targetAddress, size_t targetSize)
const McciBootloader_AppInfo_t * McciBootloaderPlatform_getAppInfo(const void *pHeader, size_t nHeader)
uint32_t entry
uint32_t stack