solo1/targets/stm32l432/bootloader/main.c

188 wiersze
4.2 KiB
C

2019-02-12 22:18:17 +00:00
// Copyright 2019 SoloKeys Developers
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
2018-12-01 19:42:49 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
2019-02-02 05:23:01 +00:00
#include "stm32l4xx_ll_rcc.h"
#include "stm32l4xx_ll_gpio.h"
2019-02-02 05:23:01 +00:00
#include "stm32l4xx.h"
2018-12-01 19:42:49 +00:00
#include "cbor.h"
#include "device.h"
#include "ctaphid.h"
//#include "bsp.h"
#include "util.h"
#include "log.h"
#include "ctap.h"
#include "app.h"
#include "memory_layout.h"
2019-02-02 05:23:01 +00:00
#include "init.h"
2018-12-01 19:42:49 +00:00
2018-12-01 19:42:49 +00:00
uint8_t REBOOT_FLAG = 0;
2019-03-10 14:26:45 +00:00
void SystemClock_Config(void);
2019-02-26 20:30:57 +00:00
void BOOT_boot(void)
{
2018-12-03 01:30:28 +00:00
typedef void (*pFunction)(void);
uint32_t *bootAddress = (uint32_t *)(APPLICATION_START_ADDR);
/* Set new vector table */
SCB->VTOR = APPLICATION_START_ADDR;
/* Read new SP and PC from vector table */
2018-12-03 05:14:26 +00:00
__set_MSP(bootAddress[0]);
((pFunction)bootAddress[1])();
}
2018-12-01 19:42:49 +00:00
int main()
2018-12-01 19:42:49 +00:00
{
uint8_t hidmsg[64];
uint32_t t1 = 0;
2018-12-05 00:14:11 +00:00
uint32_t stboot_time = 0;
uint32_t boot = 1;
2018-12-01 19:42:49 +00:00
set_logging_mask(
/*0*/
TAG_GEN|
2018-12-01 19:42:49 +00:00
// TAG_MC |
// TAG_GA |
// TAG_WALLET |
TAG_STOR |
// TAG_CP |
// TAG_CTAP|
// TAG_HID|
/*TAG_U2F|*/
// TAG_PARSE |
// TAG_TIME|
// TAG_DUMP|
// TAG_DUMP2|
2018-12-03 04:31:34 +00:00
TAG_BOOT|
TAG_EXT|
2018-12-01 19:42:49 +00:00
TAG_GREEN|
TAG_RED|
TAG_ERR
);
2019-02-02 05:23:01 +00:00
// device_init();
2019-02-26 18:10:16 +00:00
2019-02-02 05:23:01 +00:00
init_gpio();
2019-02-02 05:23:01 +00:00
init_millisecond_timer(1);
#if DEBUG_LEVEL > 0
2019-02-26 20:30:57 +00:00
init_debug_uart();
2019-02-02 05:23:01 +00:00
#endif
2019-05-09 06:44:17 +00:00
device_init_button();
2018-12-01 19:42:49 +00:00
printf1(TAG_GEN,"init device\n");
t1 = millis();
while(device_is_button_pressed())
{
if ((millis() - t1) > 2000)
{
boot = 0;
break;
}
}
2018-12-05 05:14:28 +00:00
if (!is_bootloader_disabled())
{
2018-12-05 05:14:28 +00:00
stboot_time = millis();
if ( RCC->CSR & (1<<29) )// check if there was independent watchdog reset
{
RCC->CSR |= (1<<23); // clear reset flags
goto start_bootloader;
}
}
2018-12-05 05:14:28 +00:00
if (is_authorized_to_boot() && (boot || is_bootloader_disabled()))
{
BOOT_boot();
}
else
{
2019-01-03 01:59:37 +00:00
2018-12-05 05:14:28 +00:00
printf1(TAG_RED,"Not authorized to boot (%08x == %08lx)\r\n", AUTH_WORD_ADDR, *(uint32_t*)AUTH_WORD_ADDR);
}
start_bootloader:
2019-02-02 05:23:01 +00:00
SystemClock_Config();
init_gpio();
init_millisecond_timer(0);
init_pwm();
init_rng();
2018-12-03 01:30:28 +00:00
usbhid_init();
2019-02-02 05:23:01 +00:00
2018-12-03 01:30:28 +00:00
printf1(TAG_GEN,"init usb\n");
2018-12-03 04:31:34 +00:00
2018-12-01 19:42:49 +00:00
ctaphid_init();
2018-12-03 01:30:28 +00:00
printf1(TAG_GEN,"init ctaphid\n");
2018-12-01 19:42:49 +00:00
memset(hidmsg,0,sizeof(hidmsg));
printf1(TAG_GEN,"recv'ing hid msg \n");
2018-12-01 19:42:49 +00:00
extern volatile version_t current_firmware_version;
printf1(TAG_BOOT,"Current firmware version address: %p\r\n", &current_firmware_version);
printf1(TAG_BOOT,"Current firmware version: %d.%d.%d.%d (%02x.%02x.%02x.%02x)\r\n",
current_firmware_version.major, current_firmware_version.minor, current_firmware_version.patch, current_firmware_version.reserved,
current_firmware_version.major, current_firmware_version.minor, current_firmware_version.patch, current_firmware_version.reserved
);
dump_hex1(TAG_BOOT, (uint8_t*)(&current_firmware_version) - 16, 32);
2018-12-01 19:42:49 +00:00
while(1)
{
2018-12-04 01:30:35 +00:00
if (millis() - t1 > HEARTBEAT_PERIOD)
2018-12-01 19:42:49 +00:00
{
bootloader_heartbeat();
2018-12-01 19:42:49 +00:00
t1 = millis();
}
device_manage();
if (usbhid_recv(hidmsg) > 0)
{
ctaphid_handle_packet(hidmsg);
memset(hidmsg, 0, sizeof(hidmsg));
}
else
{
}
ctaphid_check_timeouts();
if (REBOOT_FLAG)
{
2018-12-03 05:01:41 +00:00
delay(250);
device_reboot();
}
2018-12-05 00:14:11 +00:00
#ifdef SOLO_HACKER
2019-01-03 01:59:37 +00:00
// Boot ST bootloader if button is held for 5s
2018-12-05 00:14:11 +00:00
if (!device_is_button_pressed())
{
stboot_time = millis();
}
2018-12-05 05:14:28 +00:00
if ((millis() - stboot_time) > 5000)
2018-12-05 00:14:11 +00:00
{
boot_st_bootloader();
}
#endif
2018-12-01 19:42:49 +00:00
}
// Should never get here
usbhid_close();
printf1(TAG_GREEN, "done\n");
return 0;
}