2017-04-28 20:32:09 -06:00
|
|
|
#define BOOTSTUB
|
|
|
|
|
2019-12-10 09:14:30 -07:00
|
|
|
#define VERS_TAG 0x53524556
|
|
|
|
#define MIN_VERSION 2
|
|
|
|
|
2017-07-24 16:16:22 -06:00
|
|
|
#include "config.h"
|
|
|
|
#include "obj/gitversion.h"
|
|
|
|
|
2017-04-06 19:11:36 -06:00
|
|
|
#ifdef STM32F4
|
|
|
|
#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
|
|
|
|
|
2019-07-23 16:07:06 -06:00
|
|
|
// ******************** Prototypes ********************
|
|
|
|
void puts(const char *a){ UNUSED(a); }
|
|
|
|
void puth(unsigned int i){ UNUSED(i); }
|
|
|
|
void puth2(unsigned int i){ UNUSED(i); }
|
|
|
|
typedef struct board board;
|
|
|
|
typedef struct harness_configuration harness_configuration;
|
|
|
|
// No CAN support on bootloader
|
|
|
|
void can_flip_buses(uint8_t bus1, uint8_t bus2){UNUSED(bus1); UNUSED(bus2);}
|
|
|
|
void can_set_obd(int harness_orientation, bool obd){UNUSED(harness_orientation); UNUSED(obd);}
|
|
|
|
|
|
|
|
// ********************* Globals **********************
|
|
|
|
int hw_type = 0;
|
|
|
|
const board *current_board;
|
|
|
|
|
|
|
|
// ********************* Includes *********************
|
2017-04-17 14:57:34 -06:00
|
|
|
#include "libc.h"
|
2017-07-30 09:26:48 -06:00
|
|
|
#include "provision.h"
|
2019-12-05 15:19:29 -07:00
|
|
|
#include "critical.h"
|
2019-11-27 19:11:21 -07:00
|
|
|
#include "faults.h"
|
2017-04-06 19:11:36 -06:00
|
|
|
|
2019-12-05 15:19:29 -07:00
|
|
|
#include "drivers/registers.h"
|
2019-11-27 19:11:21 -07:00
|
|
|
#include "drivers/interrupts.h"
|
2019-05-21 09:28:04 -06:00
|
|
|
#include "drivers/clock.h"
|
2017-07-29 18:53:39 -06:00
|
|
|
#include "drivers/llgpio.h"
|
2019-10-05 21:34:20 -06:00
|
|
|
#include "drivers/adc.h"
|
2019-10-25 17:22:42 -06:00
|
|
|
#include "drivers/pwm.h"
|
2019-07-23 16:07:06 -06:00
|
|
|
|
|
|
|
#include "board.h"
|
|
|
|
|
2017-07-29 18:53:39 -06:00
|
|
|
#include "gpio.h"
|
|
|
|
|
2017-07-24 11:39:03 -06:00
|
|
|
#include "drivers/spi.h"
|
2017-07-24 16:16:22 -06:00
|
|
|
#include "drivers/usb.h"
|
2017-07-29 19:16:08 -06:00
|
|
|
//#include "drivers/uart.h"
|
|
|
|
|
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
|
|
|
|
2019-07-07 16:05:47 -06:00
|
|
|
void __initialize_hardware_early(void) {
|
2017-04-06 19:11:36 -06:00
|
|
|
early();
|
|
|
|
}
|
|
|
|
|
2019-07-07 16:05:47 -06:00
|
|
|
void fail(void) {
|
2017-07-24 16:16:22 -06:00
|
|
|
soft_flasher_start();
|
2017-04-25 19:03:58 -06:00
|
|
|
}
|
|
|
|
|
2017-07-22 15:28:11 -06:00
|
|
|
// know where to sig check
|
|
|
|
extern void *_app_start[];
|
|
|
|
|
2019-01-17 17:17:53 -07:00
|
|
|
// FIXME: sometimes your panda will fail flashing and will quickly blink a single Green LED
|
|
|
|
// BOUNTY: $200 coupon on shop.comma.ai or $100 check.
|
|
|
|
|
2019-07-07 16:05:47 -06:00
|
|
|
int main(void) {
|
2019-12-05 15:19:29 -07:00
|
|
|
// Init interrupt table
|
|
|
|
init_interrupts(true);
|
2019-11-27 19:11:21 -07:00
|
|
|
|
2019-08-28 13:53:51 -06:00
|
|
|
disable_interrupts();
|
2017-04-17 14:57:34 -06:00
|
|
|
clock_init();
|
2019-07-23 16:07:06 -06:00
|
|
|
detect_configuration();
|
|
|
|
detect_board_type();
|
2017-08-25 13:02:26 -06:00
|
|
|
|
2017-07-24 16:16:22 -06:00
|
|
|
if (enter_bootloader_mode == ENTER_SOFTLOADER_MAGIC) {
|
|
|
|
enter_bootloader_mode = 0;
|
|
|
|
soft_flasher_start();
|
|
|
|
}
|
|
|
|
|
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-07-27 16:54:55 -06:00
|
|
|
if ((len < 8) || (len > (0x1000000 - 0x4000 - 4 - RSANUMBYTES))) goto 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
|
|
|
|
2019-12-10 09:14:30 -07:00
|
|
|
// verify version, last bytes in the signed area
|
|
|
|
uint32_t vers[2] = {0};
|
|
|
|
memcpy(&vers, ((void*)&_app_start[0]) + len - sizeof(vers), sizeof(vers));
|
|
|
|
if (vers[0] != VERS_TAG || vers[1] < MIN_VERSION) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
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
|
2017-07-27 16:54:55 -06:00
|
|
|
fail:
|
2017-04-28 16:06:01 -06:00
|
|
|
fail();
|
2017-07-27 16:54:55 -06:00
|
|
|
return 0;
|
2017-04-28 16:06:01 -06:00
|
|
|
good:
|
2017-04-17 14:57:34 -06:00
|
|
|
// jump to flash
|
2019-07-07 16:05:47 -06:00
|
|
|
((void(*)(void)) _app_start[1])();
|
2017-04-06 19:11:36 -06:00
|
|
|
return 0;
|
|
|
|
}
|
2017-07-12 12:25:10 -06:00
|
|
|
|