SAM4L USB Host Mass Storage Bootloader Application with CRC Check and AES decryption support.
Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries.
#include <asf.h>
#include <string.h>
#include "conf_usb_host.h"
#include "conf_bootloader.h"
#include "main.h"
#include "aesa.h"
Macros | |
#define | GPIO_BOOT_PIN_MASK (1U << (BOOT_LOAD_PIN & 0x1F)) |
#define | GPIO_BOOT_PIN_PORT |
Functions | |
static void | aes_decrypt (uint32_t *encrypted_data, uint32_t size) |
AES Decryption routine. More... | |
static void | bootloader_mode_check () |
Function to check the bootloader mode activaton. More... | |
static void | bootloader_system_init () |
Initializes the device for the bootloader. More... | |
static void | console_init (void) |
Initializes the console output. More... | |
static void | crc32_calculate (uint32_t address, uint32_t size) |
static void | init_aes () |
AES Initialization routine. More... | |
static bool | integrity_check () |
Function to check the integrity of the firmware. More... | |
int | main (void) |
Main function. More... | |
void | main_usb_connection_event (uhc_device_t *dev, bool b_present) |
Callback on enumeration status change of a MSC device. More... | |
void | main_usb_sof_event (void) |
Notify that a SOF has been sent (each 1 ms) More... | |
static bool | program_memory () |
Function to program the Flash. More... | |
static void | start_application (void) |
Function to start the application. More... | |
static void | start_application_with_wdt () |
Function to issue a WDT reset to start the application. More... | |
Variables | |
COMPILER_WORD_ALIGNED volatile uint32_t | aes_output [FLASH_BUFFER_SIZE/4] |
COMPILER_WORD_ALIGNED volatile uint8_t | buffer [FLASH_BUFFER_SIZE] |
static crccu_dscr_type_t | crc_dscr |
static DIR | file_dir |
static FIL | file_object |
static uint32_t | firmware_crc = 0 |
static FATFS | fs |
struct aes_config | g_aes_cfg |
struct aes_dev_inst | g_aes_inst |
char | input_file_name [] |
static volatile bool | lun_connected = false |
static volatile uint32_t | sof_count = 0 |
#define GPIO_BOOT_PIN_MASK (1U << (BOOT_LOAD_PIN & 0x1F)) |
Referenced by bootloader_mode_check().
#define GPIO_BOOT_PIN_PORT |
|
static |
AES Decryption routine.
References aes_read_output_data(), aes_read_status(), aes_write_input_data(), and g_aes_inst.
Referenced by integrity_check(), and program_memory().
|
static |
Function to check the bootloader mode activaton.
References APP_START_ADDRESS, BOOT_GP_FUSE_BIT, BOOT_LOAD_PIN, BOOT_LOAD_PIN_ACTIVE_LVL, flashcalw_read_gp_fuse_bit(), GPIO_BOOT_PIN_MASK, and start_application().
Referenced by main().
|
static |
Initializes the device for the bootloader.
References board_init(), console_init(), cpu_irq_enable, sleepmgr_init(), sysclk_enable_peripheral_clock(), sysclk_init(), and uhc_start().
Referenced by main().
|
static |
Initializes the console output.
References sam_usart_opt_t::baudrate, sysclk_enable_peripheral_clock(), sysclk_get_peripheral_bus_hz(), usart_enable_tx(), and usart_init_rs232().
Referenced by bootloader_system_init().
|
static |
References APP_CRC_POLYNOMIAL_TYPE, crccu_configure_descriptor(), crccu_configure_mode(), crccu_enable_dma(), crccu_get_dma_status(), CRCCU_TR_CTRL_IEN_DISABLE, and CRCCU_TR_CTRL_TRWIDTH_BYTE.
Referenced by integrity_check(), and program_memory().
|
static |
AES Initialization routine.
References AES_CBC_MODE, AES_CFB_SIZE_128, aes_dev_inst::aes_cfg, AES_COUNTERMEASURE_TYPE_ALL, AES_DECRYPTION, aes_enable(), aes_get_config_defaults(), aes_init(), AES_KEY_SIZE_128, AES_MANUAL_MODE, aes_set_config(), aes_set_new_message(), aes_write_initvector(), aes_write_key(), aes_config::cfb_size, aes_config::countermeasure_mask, aes_config::dma_mode, aes_config::encrypt_mode, g_aes_cfg, g_aes_inst, aes_config::key_size, and aes_config::opmode.
Referenced by integrity_check().
|
static |
Function to check the integrity of the firmware.
References aes_decrypt(), APP_BINARY_OFFSET, APP_CRC_POLYNOMIAL_TYPE, APP_SIGNATURE, buffer, crc32_calculate(), crccu_read_crc_value(), crccu_reset(), f_close(), f_lseek(), f_open(), f_read(), FA_OPEN_EXISTING, FA_READ, firmware_crc, init_aes(), and input_file_name.
Referenced by main().
int main | ( | void | ) |
Main function.
Execution starts here.
References APP_MAX_SIZE, BOOT_GP_FUSE_BIT, bootloader_mode_check(), bootloader_system_init(), cpu_irq_disable, f_close(), f_mount(), f_open(), f_opendir(), FA_OPEN_EXISTING, FA_READ, flashcalw_erase_gp_fuse_bit(), flashcalw_is_security_bit_active(), flashcalw_read_gp_fuse_bit(), flashcalw_set_security_bit(), FR_OK, FIL::fsize, input_file_name, integrity_check(), lun_connected, program_memory(), sof_count, start_application_with_wdt(), uhc_stop(), and uhi_msc_mem_get_lun().
void main_usb_connection_event | ( | uhc_device_t * | dev, |
bool | b_present | ||
) |
Callback on enumeration status change of a MSC device.
Notify that a USB device has been connected or disconnected.
References lun_connected, sof_count, and UNUSED.
void main_usb_sof_event | ( | void | ) |
Notify that a SOF has been sent (each 1 ms)
References sof_count.
|
static |
Function to program the Flash.
Decrypt the firmware and program it.
References aes_decrypt(), aes_set_new_message(), APP_BINARY_OFFSET, APP_CRC_POLYNOMIAL_TYPE, APP_START_ADDRESS, BOOT_LED, BOOT_LED_OFF_LVL, BOOT_LED_ON_LVL, buffer, cpu_irq_disable, cpu_irq_enable, crc32_calculate(), crccu_read_crc_value(), crccu_reset(), f_close(), f_open(), f_read(), FA_OPEN_EXISTING, FA_READ, firmware_crc, FLASH_PAGE_SIZE, flashcalw_erase_page(), flashcalw_is_lock_error(), flashcalw_is_programming_error(), flashcalw_memcpy(), g_aes_inst, input_file_name, ioport_set_pin_level(), and min.
Referenced by main().
|
static |
Function to start the application.
References APP_START_ADDRESS.
Referenced by bootloader_mode_check().
|
static |
Function to issue a WDT reset to start the application.
This will reset the clock & peripheral configurations.
References BOOT_LED, BOOT_LED_ON_LVL, cpu_irq_disable, and ioport_set_pin_level().
Referenced by main().
COMPILER_WORD_ALIGNED volatile uint32_t aes_output[FLASH_BUFFER_SIZE/4] |
COMPILER_WORD_ALIGNED volatile uint8_t buffer[FLASH_BUFFER_SIZE] |
Referenced by integrity_check(), and program_memory().
|
static |
|
static |
|
static |
|
static |
Referenced by integrity_check(), and program_memory().
|
static |
Referenced by chk_mounted(), and f_mount().
struct aes_config g_aes_cfg |
Referenced by init_aes().
struct aes_dev_inst g_aes_inst |
Referenced by aes_decrypt(), init_aes(), and program_memory().
char input_file_name[] |
Referenced by integrity_check(), main(), and program_memory().
Referenced by main(), and main_usb_connection_event().
|
static |
Referenced by main(), main_usb_connection_event(), and main_usb_sof_event().