sbloader.h
/*
* Copyright (c) 2013, Freescale Semiconductor, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* o Neither the name of Freescale Semiconductor, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _loader_h
#define _loader_h
#include <stdint.h>
#include "bootloader_common.h"
#include "crc/crc32.h"
#ifndef SB_FILE_MAJOR_VERSION
#define SB_FILE_MAJOR_VERSION 1
#endif
enum _sbloader_status
{
kStatusRomLdrSectionOverrun = MAKE_STATUS(kStatusGroup_SBLoader, 0),
kStatusRomLdrSignature = MAKE_STATUS(kStatusGroup_SBLoader, 1),
kStatusRomLdrSectionLength = MAKE_STATUS(kStatusGroup_SBLoader, 2),
kStatusRomLdrUnencryptedOnly = MAKE_STATUS(kStatusGroup_SBLoader, 3),
kStatusRomLdrEOFReached = MAKE_STATUS(kStatusGroup_SBLoader, 4),
kStatusRomLdrChecksum = MAKE_STATUS(kStatusGroup_SBLoader, 5),
kStatusRomLdrCrc32Error = MAKE_STATUS(kStatusGroup_SBLoader, 6),
kStatusRomLdrUnknownCommand = MAKE_STATUS(kStatusGroup_SBLoader, 7),
kStatusRomLdrIdNotFound = MAKE_STATUS(kStatusGroup_SBLoader, 8),
kStatusRomLdrDataUnderrun = MAKE_STATUS(kStatusGroup_SBLoader, 9),
kStatusRomLdrJumpReturned = MAKE_STATUS(kStatusGroup_SBLoader, 10),
kStatusRomLdrCallFailed = MAKE_STATUS(kStatusGroup_SBLoader, 11),
kStatusRomLdrKeyNotFound = MAKE_STATUS(kStatusGroup_SBLoader, 12),
kStatusRomLdrSecureOnly = MAKE_STATUS(kStatusGroup_SBLoader, 13),
kStatusRomLdrResetReturned = MAKE_STATUS(kStatusGroup_SBLoader, 14)
};
#define BYTES_PER_CHUNK 16
#define BOOT_SIGNATURE 0x504d5453
#define BOOT_SIGNATURE2 0x6c746773
#define FFLG_DISPLAY_PROGRESS 0x0001
#define SFLG_SECTION_BOOTABLE 0x0001
#define CFLG_LAST_TAG 0x01
#define ROM_ERASE_ALL_MASK 0x01
#define ROM_ERASE_ALL_UNSECURE_MASK 0x02
#define ROM_JUMP_SP_MASK 0x02
#define ROM_MEM_SPACE_SHIFT 0x8
#define ROM_MEM_SPACE_MASK 0xf
enum _ldr_memory_space
{
kLdrMemorySpace_Internal,
kLdrMemorySpace_eMMC0,
kLdrMemorySpace_SD0,
kLdrMemorySpace_SDRAM0,
kLdrMemorySpace_IFR0,
kLdrMemorySpace_eFuse0,
kLdrMemorySpace_OTP0
};
#define ROM_MEM_CTRL_SHIFT 0x8
#define ROM_MEM_CTRL_MASK 0xf
enum _ldr_memory_ctrl
{
kLdrMemoryCtrl_InternalFlash,
kLdrMemoryCtrl_QSPI0,
kLdrMemoryCtrl_PNOR0
};
#define ROM_PROG_8BYTE_MASK 0x01
#define ROM_NOP_CMD 0x00
#define ROM_TAG_CMD 0x01
#define ROM_LOAD_CMD 0x02
#define ROM_FILL_CMD 0x03
#define ROM_JUMP_CMD 0x04
#define ROM_CALL_CMD 0x05
#define ROM_MODE_CMD 0x06
#define ROM_ERASE_CMD 0x07
#define ROM_RESET_CMD 0x08
#define ROM_MEM_ENABLE_CMD 0x09
#define ROM_PROG_CMD 0x0a
#define ROM_BOOT_SECTION_ID 1
#define ROM_BOOT_IMAGE_ID 2
typedef uint8_t chunk_t[BYTES_PER_CHUNK];
typedef struct _boot_cmd
{
uint8_t checksum;
uint8_t tag;
uint16_t flags;
uint32_t address;
uint32_t count;
uint32_t data;
} boot_cmd_t;
typedef struct _boot_hdr1
{
uint32_t hash;
uint32_t signature;
uint8_t major;
uint8_t minor;
uint16_t fileFlags;
uint32_t fileChunks;
} boot_hdr1_t;
typedef struct _boot_hdr2
{
uint32_t bootOffset;
uint32_t bootSectID;
uint16_t keyCount;
uint16_t keyOffset;
uint16_t hdrChunks;
uint16_t sectCount;
} boot_hdr2_t;
// Provides forward reference to the loader context definition.
typedef struct _ldr_Context ldr_Context_t;
typedef status_t (*pLdrFnc_t)(ldr_Context_t *);
typedef status_t (*pJumpFnc_t)(uint32_t);
typedef status_t (*pCallFnc_t)(uint32_t, uint32_t *);
struct _ldr_Context
{
pLdrFnc_t Action; // !< pointer to loader action function
uint32_t fileChunks; // !< chunks remaining in file
uint32_t sectChunks; // !< chunks remaining in section
uint32_t bootSectChunks; // !< number of chunks we need to complete the boot section
uint32_t receivedChunks; // !< number of chunks we need to complete the boot section
uint16_t fileFlags; // !< file header flags
uint16_t keyCount; // !< number of keys in the key dictionary
uint32_t objectID; // !< ID of the current boot section or image
crc32_data_t crc32; // !< crc calculated over load command payload
uint8_t *src; // !< source buffer address
chunk_t initVector; // !< decryption initialization vector
chunk_t dek; // !< chunk size DEK if the image is encrypted
chunk_t scratchPad; // !< chunk size scratch pad area
boot_cmd_t bootCmd; // !< current boot command
uint32_t skipCount; // !< Number of chunks to skip
bool skipToEnd; // !< true if skipping to end of file
};
#if defined(__cplusplus)
extern "C" {
#endif // __cplusplus
status_t sbloader_init(void);
status_t sbloader_pump(uint8_t *data, uint32_t length);
status_t sbloader_finalize(void);
#if defined(__cplusplus)
}
#endif // __cplusplus
#endif // _loader_h
boot_hdr1_t::minor
uint8_t minor
minor file format version
Definition: sbloader.h:164
boot_hdr1_t::hash
uint32_t hash
last 32-bits of SHA-1 hash
Definition: sbloader.h:161
_ldr_memory_space
_ldr_memory_space
Memory Space constants.
Definition: sbloader.h:100
boot_cmd_t
Boot command definition.
Definition: sbloader.h:148
boot_cmd_t::checksum
uint8_t checksum
8-bit checksum over command chunk
Definition: sbloader.h:150
boot_cmd_t::flags
uint16_t flags
command flags (modifier)
Definition: sbloader.h:152
boot_hdr2_t::bootSectID
uint32_t bootSectID
section ID of the first boot section
Definition: sbloader.h:173
boot_cmd_t::data
uint32_t data
data argument
Definition: sbloader.h:155
boot_hdr2_t::hdrChunks
uint16_t hdrChunks
number of chunks in the header
Definition: sbloader.h:176
boot_hdr2_t::keyOffset
uint16_t keyOffset
chunk offset to the key dictionary
Definition: sbloader.h:175
pLdrFnc_t
status_t(* pLdrFnc_t)(ldr_Context_t *)
Function pointer definition for all loader action functions.
Definition: sbloader.h:184
_ldr_memory_ctrl
_ldr_memory_ctrl
Memory Controller constants.
Definition: sbloader.h:118
boot_cmd_t::count
uint32_t count
count argument
Definition: sbloader.h:154
sbloader_finalize
status_t sbloader_finalize(void)
Implement jump or reset command.
Definition: sbloader.c:1131
boot_hdr2_t::keyCount
uint16_t keyCount
number of keys in the key dictionary
Definition: sbloader.h:174
boot_hdr1_t
Definition for boot image file header chunk 1.
Definition: sbloader.h:159
boot_hdr1_t::major
uint8_t major
major file format version
Definition: sbloader.h:163
sbloader_pump
status_t sbloader_pump(uint8_t *data, uint32_t length)
Pump the loader state machine.
Definition: sbloader.c:1050
sbloader_init
status_t sbloader_init(void)
Initialize the loader state machine.
Definition: sbloader.c:998
boot_hdr2_t::bootOffset
uint32_t bootOffset
chunk offset to the first boot section
Definition: sbloader.h:172
boot_hdr1_t::signature
uint32_t signature
must equal "STMP"
Definition: sbloader.h:162
kStatusGroup_SBLoader
SB loader status group number (101).
Definition: bootloader_common.h:144
crc32_data_t
State information for the CRC32 algorithm.
Definition: crc32.h:44
_ldr_Context
Loader context definition.
Definition: sbloader.h:193
boot_hdr1_t::fileChunks
uint32_t fileChunks
total chunks in the file
Definition: sbloader.h:166
boot_hdr1_t::fileFlags
uint16_t fileFlags
global file flags
Definition: sbloader.h:165
boot_hdr2_t::sectCount
uint16_t sectCount
number of sections in the image
Definition: sbloader.h:177
pJumpFnc_t
status_t(* pJumpFnc_t)(uint32_t)
Jump command function pointer definition.
Definition: sbloader.h:187
boot_cmd_t::tag
uint8_t tag
command tag (identifier)
Definition: sbloader.h:151
boot_cmd_t::address
uint32_t address
address argument
Definition: sbloader.h:153
_sbloader_status
_sbloader_status
SB loader status codes.
Definition: sbloader.h:48
boot_hdr2_t
Definition for boot image file header chunk 2.
Definition: sbloader.h:170
status_t
int32_t status_t
Type used for all status and error return values.
Definition: fsl_common.h:121
pCallFnc_t
status_t(* pCallFnc_t)(uint32_t, uint32_t *)
Call command function pointer definition.
Definition: sbloader.h:190