Kinetis Bootloader  2.0.0
Common bootloader for Kinetis devices
bl_context.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 #if !defined(__CONTEXT_H__)
31 #define __CONTEXT_H__
32 
33 #include "bootloader_common.h"
34 #include "bootloader/bl_peripheral.h"
35 #include "memory/memory.h"
36 #include "packet/command_packet.h"
37 #include "bootloader/bl_command.h"
38 #include "property/property.h"
39 
40 #if !defined(BOOTLOADER_HOST)
41 #include "flash/fsl_flash.h"
42 #if BL_FEATURE_ENCRYPTION
43 #include "security/aes_security.h"
44 #endif // #if BL_FEATURE_ENCRYPTION
45 #endif // #if !defined(BOOTLOADER_HOST)
46 
49 
51 // Definitions
53 
54 #if !defined(BOOTLOADER_HOST)
55 
57 typedef struct FlashDriverInterface
58 {
60  status_t (*flash_init)(flash_config_t *config);
61  status_t (*flash_erase_all)(flash_config_t *config, uint32_t key);
62  status_t (*flash_erase_all_unsecure)(flash_config_t *config, uint32_t key);
63  status_t (*flash_erase)(flash_config_t *config, uint32_t start, uint32_t lengthInBytes, uint32_t key);
64  status_t (*flash_program)(flash_config_t *config, uint32_t start, uint32_t *src, uint32_t lengthInBytes);
65  status_t (*flash_get_security_state)(flash_config_t *config, flash_security_state_t *state);
66  status_t (*flash_security_bypass)(flash_config_t *config, const uint8_t *backdoorKey);
67  status_t (*flash_verify_erase_all)(flash_config_t *config, flash_margin_value_t margin);
68  status_t (*flash_verify_erase)(flash_config_t *config,
69  uint32_t start,
70  uint32_t lengthInBytes,
71  flash_margin_value_t margin);
72  status_t (*flash_verify_program)(flash_config_t *config,
73  uint32_t start,
74  uint32_t lengthInBytes,
75  const uint32_t *expectedData,
76  flash_margin_value_t margin,
77  uint32_t *failedAddress,
78  uint32_t *failedData);
79  status_t (*flash_get_property)(flash_config_t *config, flash_property_tag_t whichProperty, uint32_t *value);
80  status_t (*flash_register_callback)(flash_config_t *config, flash_callback_t callback);
81  status_t (*flash_program_once)(flash_config_t *config, uint32_t index, uint32_t *src, uint32_t lengthInBytes);
82  status_t (*flash_read_once)(flash_config_t *config, uint32_t index, uint32_t *dst, uint32_t lengthInBytes);
83  status_t (*flash_read_resource)(flash_config_t *config,
84  uint32_t start,
85  uint32_t *dst,
86  uint32_t lengthInBytes,
88  status_t (*flash_prepare_execute_in_ram_functions)(flash_config_t *config);
89  status_t (*flash_is_execute_only)(flash_config_t *config,
90  uint32_t start,
91  uint32_t lengthInBytes,
92  flash_execute_only_access_state_t *access_state);
93  status_t (*flash_erase_all_execute_only_segments)(flash_config_t *config, uint32_t key);
94  status_t (*flash_verify_erase_all_execute_only_segments)(flash_config_t *config, flash_margin_value_t margin);
95  status_t (*flash_set_flexram_function)(flash_config_t *config, flash_flexram_function_option_t option);
96  status_t (*flash_program_section)(flash_config_t *config, uint32_t start, uint32_t *src, uint32_t lengthInBytes);
98 
100 typedef struct AesDriverInterface
101 {
102  void (*aes_init)(uint32_t *key);
103  void (*aes_encrypt)(uint32_t *in, uint32_t *key, uint32_t *out);
104  void (*aes_decrypt)(uint32_t *in, uint32_t *key, uint32_t *out);
106 
107 #else // #if !defined(BOOTLOADER_HOST)
108 
109 // Provide stub definitions for flash driver types for the host.
110 typedef uint32_t flash_driver_interface_t;
111 typedef uint32_t flash_config_t;
112 typedef uint32_t aes_driver_interface_t;
113 
114 #endif // #if !defined(BOOTLOADER_HOST)
115 
117 typedef struct _bootloaderContext
118 {
120 
126  const peripheral_descriptor_t *allPeripherals;
128 
129 
131 
132  const peripheral_descriptor_t *activePeripheral;
134 
136 
138 // Externs
140 
143 extern const aes_driver_interface_t g_aesInterface;
144 
146 
147 #endif // __CONTEXT_H__
148 // EOF
const aes_driver_interface_t * aesInterface
Interface to the AES driver.
Definition: bl_context.h:127
flash_config_t flashState
Flash driver instance.
Definition: bl_context.h:133
void(* flash_callback_t)(void)
callback type used for pflash block
Definition: src/drivers/flash/fsl_flash.h:421
const command_interface_t * commandInterface
Interface to command processor operations.
Definition: bl_context.h:124
standard_version_t version
flash driver API version number.
Definition: bl_context.h:59
bootloader_context_t g_bootloaderContext
Bootloader global context data.
Definition: bl_context.c:40
flash_read_resource_option_t
Enumeration for the two possible options of flash read resource command.
Definition: src/drivers/flash/fsl_flash.h:273
const memory_interface_t * memoryInterface
Abstract interface to memory operations.
Definition: bl_context.h:121
Interface for the flash driver.
Definition: bl_context.h:57
Structure of a memory map entry.
Definition: memory.h:86
Structure of bootloader global context.
Definition: bl_context.h:117
void aes_init(uint32_t *key)
An initialization function for the decryption peripheral Warning: The key must be word aligned...
Definition: aes_security.c:79
Interface for AES 128 functions.
Definition: bl_context.h:100
const peripheral_descriptor_t * allPeripherals
Array of all peripherals.
Definition: bl_context.h:126
Interface to property operations.
Definition: property.h:321
flash_flexram_function_option_t
Enumeration for the two possilbe options of set flexram function command.
Definition: src/drivers/flash/fsl_flash.h:317
flash_property_tag_t
Enumeration for various flash properties.
Definition: src/drivers/flash/fsl_flash.h:231
Structure of version property.
Definition: bootloader_common.h:168
const peripheral_descriptor_t * activePeripheral
The currently active peripheral.
Definition: bl_context.h:132
const flash_driver_interface_t * flashDriverInterface
Flash driver interface.
Definition: bl_context.h:125
void aes_decrypt(uint32_t *in, uint32_t *key, uint32_t *out)
Decrypts a 16 byte block of data in and out may use the same address so decrypting in place is suppor...
Definition: aes_security.c:138
flash_security_state_t
Enumeration for the three possible flash security states.
Definition: src/drivers/flash/fsl_flash.h:201
flash_margin_value_t
Enumeration for supported flash margin levels.
Definition: src/drivers/flash/fsl_flash.h:190
Flash driver state information.
Definition: src/drivers/flash/fsl_flash.h:443
void aes_encrypt(uint32_t *in, uint32_t *key, uint32_t *out)
Encrypts a 16 byte block of data in and out may use the same address so encrypting in place is suppor...
Definition: aes_security.c:113
flash_execute_only_access_state_t
Enumeration for the three possible flash execute access levels.
Definition: src/drivers/flash/fsl_flash.h:221
const flash_driver_interface_t g_flashDriverInterface
Function table for flash driver.
Definition: bl_tree_root.c:46
Interface to memory operations.
Definition: memory.h:64
const property_interface_t * propertyInterface
Interface to property store.
Definition: bl_context.h:123
int32_t status_t
Type used for all status and error return values.
Definition: fsl_common.h:121
const memory_map_entry_t * memoryMap
Memory map used by abstract memory interface.
Definition: bl_context.h:122
Interface to command processor operations.
Definition: bl_command.h:95