2019-12-10 08:14:30 -08:00
|
|
|
#define VERS_TAG 0x53524556
|
|
|
|
|
#define MIN_VERSION 2
|
|
|
|
|
|
2021-07-02 18:25:35 -07:00
|
|
|
// ********************* Includes *********************
|
2025-07-19 21:58:58 -07:00
|
|
|
#include "board/config.h"
|
2017-04-06 18:11:36 -07:00
|
|
|
|
2025-07-19 21:58:58 -07:00
|
|
|
#include "board/drivers/led.h"
|
|
|
|
|
#include "board/drivers/pwm.h"
|
|
|
|
|
#include "board/drivers/usb.h"
|
2021-07-02 18:25:35 -07:00
|
|
|
|
2025-07-19 21:58:58 -07:00
|
|
|
#include "board/early_init.h"
|
|
|
|
|
#include "board/provision.h"
|
2017-07-29 18:16:08 -07:00
|
|
|
|
2017-04-25 18:03:58 -07:00
|
|
|
#include "crypto/rsa.h"
|
|
|
|
|
#include "crypto/sha.h"
|
|
|
|
|
|
2025-07-19 21:58:58 -07:00
|
|
|
#include "board/obj/cert.h"
|
|
|
|
|
#include "board/obj/gitversion.h"
|
|
|
|
|
#include "board/flasher.h"
|
2017-04-27 20:32:16 -07:00
|
|
|
|
2024-02-17 14:16:15 -08:00
|
|
|
// cppcheck-suppress unusedFunction ; used in headers not included in cppcheck
|
2019-07-07 15:05:47 -07:00
|
|
|
void __initialize_hardware_early(void) {
|
2021-07-02 18:25:35 -07:00
|
|
|
early_initialization();
|
2017-04-06 18:11:36 -07:00
|
|
|
}
|
|
|
|
|
|
2019-07-07 15:05:47 -07:00
|
|
|
void fail(void) {
|
2017-07-24 15:16:22 -07:00
|
|
|
soft_flasher_start();
|
2017-04-25 18:03:58 -07:00
|
|
|
}
|
|
|
|
|
|
2017-07-22 14:28:11 -07:00
|
|
|
// know where to sig check
|
|
|
|
|
extern void *_app_start[];
|
|
|
|
|
|
2019-07-07 15:05:47 -07:00
|
|
|
int main(void) {
|
2019-12-05 14:19:29 -08:00
|
|
|
// Init interrupt table
|
|
|
|
|
init_interrupts(true);
|
2019-11-27 18:11:21 -08:00
|
|
|
|
2019-08-28 12:53:51 -07:00
|
|
|
disable_interrupts();
|
2017-04-17 13:57:34 -07:00
|
|
|
clock_init();
|
2019-07-23 15:07:06 -07:00
|
|
|
detect_board_type();
|
2017-08-25 12:02:26 -07:00
|
|
|
|
2023-08-03 23:55:13 -07:00
|
|
|
#ifdef PANDA_JUNGLE
|
|
|
|
|
current_board->set_panda_power(true);
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-07-24 15:16:22 -07:00
|
|
|
if (enter_bootloader_mode == ENTER_SOFTLOADER_MAGIC) {
|
|
|
|
|
enter_bootloader_mode = 0;
|
|
|
|
|
soft_flasher_start();
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-25 18:03:58 -07:00
|
|
|
// validate length
|
2017-05-01 22:59:10 -07:00
|
|
|
int len = (int)_app_start[0];
|
2017-07-27 15:54:55 -07:00
|
|
|
if ((len < 8) || (len > (0x1000000 - 0x4000 - 4 - RSANUMBYTES))) goto fail;
|
2017-04-25 18:03:58 -07:00
|
|
|
|
|
|
|
|
// compute SHA hash
|
2017-05-01 22:59:10 -07:00
|
|
|
uint8_t digest[SHA_DIGEST_SIZE];
|
2017-04-26 10:41:57 -07:00
|
|
|
SHA_hash(&_app_start[1], len-4, digest);
|
2017-04-25 18:03:58 -07:00
|
|
|
|
2019-12-10 08:14:30 -08: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 18:03:58 -07:00
|
|
|
// verify RSA signature
|
2017-04-28 15:06:01 -07:00
|
|
|
if (RSA_verify(&release_rsa_key, ((void*)&_app_start[0]) + len, RSANUMBYTES, digest, SHA_DIGEST_SIZE)) {
|
|
|
|
|
goto good;
|
2017-04-26 10:41:57 -07:00
|
|
|
}
|
2017-04-17 13:57:34 -07:00
|
|
|
|
2017-04-28 20:13:00 -07: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 15:06:01 -07:00
|
|
|
}
|
2017-04-28 20:13:00 -07:00
|
|
|
#endif
|
2017-04-28 15:06:01 -07:00
|
|
|
|
|
|
|
|
// here is a failure
|
2017-07-27 15:54:55 -07:00
|
|
|
fail:
|
2017-04-28 15:06:01 -07:00
|
|
|
fail();
|
2017-07-27 15:54:55 -07:00
|
|
|
return 0;
|
2017-04-28 15:06:01 -07:00
|
|
|
good:
|
2017-04-17 13:57:34 -07:00
|
|
|
// jump to flash
|
2019-07-07 15:05:47 -07:00
|
|
|
((void(*)(void)) _app_start[1])();
|
2017-04-06 18:11:36 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|