Kinetis Bootloader  2.0.0
Common bootloader for Kinetis devices
sbloader.h
1 /*
2  * Copyright (c) 2013, Freescale Semiconductor, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  * of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  * list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from this
17  * software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef _loader_h
32 #define _loader_h
33 
34 #include <stdint.h>
35 #include "bootloader_common.h"
36 #include "crc/crc32.h"
37 
40 
41 #ifndef SB_FILE_MAJOR_VERSION
42 #define SB_FILE_MAJOR_VERSION 1
45 #endif
46 
49 {
50  kStatusRomLdrSectionOverrun = MAKE_STATUS(kStatusGroup_SBLoader, 0),
51  kStatusRomLdrSignature = MAKE_STATUS(kStatusGroup_SBLoader, 1),
52  kStatusRomLdrSectionLength = MAKE_STATUS(kStatusGroup_SBLoader, 2),
53  kStatusRomLdrUnencryptedOnly = MAKE_STATUS(kStatusGroup_SBLoader, 3),
54  kStatusRomLdrEOFReached = MAKE_STATUS(kStatusGroup_SBLoader, 4),
55  kStatusRomLdrChecksum = MAKE_STATUS(kStatusGroup_SBLoader, 5),
56  kStatusRomLdrCrc32Error = MAKE_STATUS(kStatusGroup_SBLoader, 6),
57  kStatusRomLdrUnknownCommand = MAKE_STATUS(kStatusGroup_SBLoader, 7),
58  kStatusRomLdrIdNotFound = MAKE_STATUS(kStatusGroup_SBLoader, 8),
59  kStatusRomLdrDataUnderrun = MAKE_STATUS(kStatusGroup_SBLoader, 9),
60  kStatusRomLdrJumpReturned = MAKE_STATUS(kStatusGroup_SBLoader, 10),
61  kStatusRomLdrCallFailed = MAKE_STATUS(kStatusGroup_SBLoader, 11),
62  kStatusRomLdrKeyNotFound = MAKE_STATUS(kStatusGroup_SBLoader, 12),
63  kStatusRomLdrSecureOnly = MAKE_STATUS(kStatusGroup_SBLoader, 13),
64  kStatusRomLdrResetReturned = MAKE_STATUS(kStatusGroup_SBLoader, 14)
65 };
66 
69 #define BYTES_PER_CHUNK 16
70 
72 #define BOOT_SIGNATURE 0x504d5453
73 
75 #define BOOT_SIGNATURE2 0x6c746773
76 
78 #define FFLG_DISPLAY_PROGRESS 0x0001
79 
81 #define SFLG_SECTION_BOOTABLE 0x0001
82 
84 #define CFLG_LAST_TAG 0x01
85 
87 #define ROM_ERASE_ALL_MASK 0x01
88 #define ROM_ERASE_ALL_UNSECURE_MASK 0x02
89 
91 #define ROM_JUMP_SP_MASK 0x02
92 
94 #define ROM_MEM_SPACE_SHIFT 0x8
95 
97 #define ROM_MEM_SPACE_MASK 0xf
98 
101 {
102  kLdrMemorySpace_Internal,
103  kLdrMemorySpace_eMMC0,
104  kLdrMemorySpace_SD0,
105  kLdrMemorySpace_SDRAM0,
106  kLdrMemorySpace_IFR0,
107  kLdrMemorySpace_eFuse0,
108  kLdrMemorySpace_OTP0
109 };
110 
112 #define ROM_MEM_CTRL_SHIFT 0x8
113 
115 #define ROM_MEM_CTRL_MASK 0xf
116 
119 {
120  kLdrMemoryCtrl_InternalFlash,
121  kLdrMemoryCtrl_QSPI0,
122  kLdrMemoryCtrl_PNOR0
123 };
124 
126 #define ROM_PROG_8BYTE_MASK 0x01
127 
129 #define ROM_NOP_CMD 0x00
130 #define ROM_TAG_CMD 0x01
131 #define ROM_LOAD_CMD 0x02
132 #define ROM_FILL_CMD 0x03
133 #define ROM_JUMP_CMD 0x04
134 #define ROM_CALL_CMD 0x05
135 #define ROM_MODE_CMD 0x06
136 #define ROM_ERASE_CMD 0x07
137 #define ROM_RESET_CMD 0x08
138 #define ROM_MEM_ENABLE_CMD 0x09
139 #define ROM_PROG_CMD 0x0a
140 
142 #define ROM_BOOT_SECTION_ID 1
143 #define ROM_BOOT_IMAGE_ID 2
144 
145 typedef uint8_t chunk_t[BYTES_PER_CHUNK];
146 
148 typedef struct _boot_cmd
149 {
150  uint8_t checksum;
151  uint8_t tag;
152  uint16_t flags;
153  uint32_t address;
154  uint32_t count;
155  uint32_t data;
156 } boot_cmd_t;
157 
159 typedef struct _boot_hdr1
160 {
161  uint32_t hash;
162  uint32_t signature;
163  uint8_t major;
164  uint8_t minor;
165  uint16_t fileFlags;
166  uint32_t fileChunks;
167 } boot_hdr1_t;
168 
170 typedef struct _boot_hdr2
171 {
172  uint32_t bootOffset;
173  uint32_t bootSectID;
174  uint16_t keyCount;
175  uint16_t keyOffset;
176  uint16_t hdrChunks;
177  uint16_t sectCount;
178 } boot_hdr2_t;
179 
180 // Provides forward reference to the loader context definition.
181 typedef struct _ldr_Context ldr_Context_t;
182 
184 typedef status_t (*pLdrFnc_t)(ldr_Context_t *);
185 
187 typedef status_t (*pJumpFnc_t)(uint32_t);
188 
190 typedef status_t (*pCallFnc_t)(uint32_t, uint32_t *);
191 
194 {
195  pLdrFnc_t Action; // !< pointer to loader action function
196  uint32_t fileChunks; // !< chunks remaining in file
197  uint32_t sectChunks; // !< chunks remaining in section
198  uint32_t bootSectChunks; // !< number of chunks we need to complete the boot section
199  uint32_t receivedChunks; // !< number of chunks we need to complete the boot section
200  uint16_t fileFlags; // !< file header flags
201  uint16_t keyCount; // !< number of keys in the key dictionary
202  uint32_t objectID; // !< ID of the current boot section or image
203  crc32_data_t crc32; // !< crc calculated over load command payload
204  uint8_t *src; // !< source buffer address
205  chunk_t initVector; // !< decryption initialization vector
206  chunk_t dek; // !< chunk size DEK if the image is encrypted
207  chunk_t scratchPad; // !< chunk size scratch pad area
208  boot_cmd_t bootCmd; // !< current boot command
209  uint32_t skipCount; // !< Number of chunks to skip
210  bool skipToEnd; // !< true if skipping to end of file
211 };
212 
213 #if defined(__cplusplus)
214 extern "C" {
215 #endif // __cplusplus
216 
217 status_t sbloader_init(void);
218 status_t sbloader_pump(uint8_t *data, uint32_t length);
220 
221 #if defined(__cplusplus)
222 }
223 #endif // __cplusplus
224 
226 
227 #endif // _loader_h
uint8_t minor
minor file format version
Definition: sbloader.h:164
uint32_t hash
last 32-bits of SHA-1 hash
Definition: sbloader.h:161
_ldr_memory_space
Memory Space constants.
Definition: sbloader.h:100
Boot command definition.
Definition: sbloader.h:148
uint8_t checksum
8-bit checksum over command chunk
Definition: sbloader.h:150
uint16_t flags
command flags (modifier)
Definition: sbloader.h:152
uint32_t bootSectID
section ID of the first boot section
Definition: sbloader.h:173
uint32_t data
data argument
Definition: sbloader.h:155
uint16_t hdrChunks
number of chunks in the header
Definition: sbloader.h:176
uint16_t keyOffset
chunk offset to the key dictionary
Definition: sbloader.h:175
status_t(* pLdrFnc_t)(ldr_Context_t *)
Function pointer definition for all loader action functions.
Definition: sbloader.h:184
_ldr_memory_ctrl
Memory Controller constants.
Definition: sbloader.h:118
uint32_t count
count argument
Definition: sbloader.h:154
status_t sbloader_finalize(void)
Implement jump or reset command.
Definition: sbloader.c:1131
uint16_t keyCount
number of keys in the key dictionary
Definition: sbloader.h:174
Definition for boot image file header chunk 1.
Definition: sbloader.h:159
uint8_t major
major file format version
Definition: sbloader.h:163
status_t sbloader_pump(uint8_t *data, uint32_t length)
Pump the loader state machine.
Definition: sbloader.c:1050
status_t sbloader_init(void)
Initialize the loader state machine.
Definition: sbloader.c:998
uint32_t bootOffset
chunk offset to the first boot section
Definition: sbloader.h:172
uint32_t signature
must equal "STMP"
Definition: sbloader.h:162
SB loader status group number (101).
Definition: bootloader_common.h:144
State information for the CRC32 algorithm.
Definition: crc32.h:44
Loader context definition.
Definition: sbloader.h:193
uint32_t fileChunks
total chunks in the file
Definition: sbloader.h:166
uint16_t fileFlags
global file flags
Definition: sbloader.h:165
uint16_t sectCount
number of sections in the image
Definition: sbloader.h:177
status_t(* pJumpFnc_t)(uint32_t)
Jump command function pointer definition.
Definition: sbloader.h:187
uint8_t tag
command tag (identifier)
Definition: sbloader.h:151
uint32_t address
address argument
Definition: sbloader.h:153
_sbloader_status
SB loader status codes.
Definition: sbloader.h:48
Definition for boot image file header chunk 2.
Definition: sbloader.h:170
int32_t status_t
Type used for all status and error return values.
Definition: fsl_common.h:121
status_t(* pCallFnc_t)(uint32_t, uint32_t *)
Call command function pointer definition.
Definition: sbloader.h:190