2017-04-28 20:32:09 -06:00
|
|
|
#define BOOTSTUB
|
|
|
|
|
2017-04-06 19:11:36 -06:00
|
|
|
#ifdef STM32F4
|
|
|
|
#define PANDA
|
|
|
|
#include "stm32f4xx.h"
|
2017-04-28 20:32:09 -06:00
|
|
|
#include "stm32f4xx_hal_gpio_ex.h"
|
2017-04-06 19:11:36 -06:00
|
|
|
#else
|
|
|
|
#include "stm32f2xx.h"
|
2017-04-28 20:32:09 -06:00
|
|
|
#include "stm32f2xx_hal_gpio_ex.h"
|
2017-04-06 19:11:36 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "early.h"
|
2017-04-17 14:57:34 -06:00
|
|
|
#include "libc.h"
|
2017-04-28 20:32:09 -06:00
|
|
|
#include "spi.h"
|
2017-04-06 19:11:36 -06:00
|
|
|
|
2017-04-25 19:03:58 -06:00
|
|
|
#include "crypto/rsa.h"
|
|
|
|
#include "crypto/sha.h"
|
|
|
|
|
|
|
|
#include "obj/cert.h"
|
|
|
|
|
2017-04-28 20:32:09 -06:00
|
|
|
#include "spi_flasher.h"
|
2017-04-27 21:32:16 -06:00
|
|
|
|
2017-04-06 19:11:36 -06:00
|
|
|
void __initialize_hardware_early() {
|
|
|
|
early();
|
|
|
|
}
|
|
|
|
|
2017-04-25 19:03:58 -06:00
|
|
|
void fail() {
|
2017-04-28 20:32:09 -06:00
|
|
|
#ifdef PANDA
|
2017-05-03 23:28:22 -06:00
|
|
|
volatile int i;
|
|
|
|
// detect usb host
|
|
|
|
GPIOA->PUPDR |= GPIO_PUPDR_PUPDR11_0;
|
|
|
|
for (i=0;i<PULL_EFFECTIVE_DELAY;i++);
|
|
|
|
int no_usb = GPIOA->IDR & (1 << 11);
|
|
|
|
GPIOA->PUPDR &= ~(GPIO_PUPDR_PUPDR11_0);
|
|
|
|
|
|
|
|
if (no_usb) {
|
|
|
|
// no usb host, go to SPI flasher
|
|
|
|
spi_flasher();
|
|
|
|
} else {
|
|
|
|
// has usb host, go to USB flasher
|
|
|
|
enter_bootloader_mode = ENTER_BOOTLOADER_MAGIC;
|
|
|
|
NVIC_SystemReset();
|
|
|
|
}
|
2017-04-28 20:32:09 -06:00
|
|
|
#else
|
2017-04-25 19:03:58 -06:00
|
|
|
enter_bootloader_mode = ENTER_BOOTLOADER_MAGIC;
|
|
|
|
NVIC_SystemReset();
|
2017-04-28 20:32:09 -06:00
|
|
|
#endif
|
2017-04-25 19:03:58 -06:00
|
|
|
}
|
|
|
|
|
2017-04-06 19:11:36 -06:00
|
|
|
int main() {
|
2017-04-17 14:57:34 -06:00
|
|
|
clock_init();
|
|
|
|
|
2017-04-25 19:03:58 -06:00
|
|
|
// validate length
|
2017-05-01 23:59:10 -06:00
|
|
|
int len = (int)_app_start[0];
|
2017-05-03 23:28:22 -06:00
|
|
|
if ((len < 8) || (((uint32_t)&_app_start[0] + RSANUMBYTES) >= 0x8100000)) fail();
|
2017-04-25 19:03:58 -06:00
|
|
|
|
|
|
|
// compute SHA hash
|
2017-05-01 23:59:10 -06:00
|
|
|
uint8_t digest[SHA_DIGEST_SIZE];
|
2017-04-26 11:41:57 -06:00
|
|
|
SHA_hash(&_app_start[1], len-4, digest);
|
2017-04-25 19:03:58 -06:00
|
|
|
|
|
|
|
// verify RSA signature
|
2017-04-28 16:06:01 -06:00
|
|
|
if (RSA_verify(&release_rsa_key, ((void*)&_app_start[0]) + len, RSANUMBYTES, digest, SHA_DIGEST_SIZE)) {
|
|
|
|
goto good;
|
2017-04-26 11:41:57 -06:00
|
|
|
}
|
2017-04-17 14:57:34 -06:00
|
|
|
|
2017-04-28 21:13:00 -06:00
|
|
|
// allow debug if built from source
|
|
|
|
#ifdef ALLOW_DEBUG
|
|
|
|
if (RSA_verify(&debug_rsa_key, ((void*)&_app_start[0]) + len, RSANUMBYTES, digest, SHA_DIGEST_SIZE)) {
|
|
|
|
goto good;
|
2017-04-28 16:06:01 -06:00
|
|
|
}
|
2017-04-28 21:13:00 -06:00
|
|
|
#endif
|
2017-04-28 16:06:01 -06:00
|
|
|
|
|
|
|
// here is a failure
|
|
|
|
fail();
|
|
|
|
good:
|
2017-04-17 14:57:34 -06:00
|
|
|
// jump to flash
|
|
|
|
((void(*)()) _app_start[1])();
|
2017-04-06 19:11:36 -06:00
|
|
|
return 0;
|
|
|
|
}
|