panda/board/bootstub.c

85 lines
1.8 KiB
C
Raw Normal View History

2017-04-29 10:32:09 +08:00
#define BOOTSTUB
#define VERS_TAG 0x53524556
#define MIN_VERSION 2
// ********************* Includes *********************
2017-07-25 06:16:22 +08:00
#include "config.h"
2017-04-07 09:11:36 +08:00
#include "drivers/pwm.h"
2017-07-25 06:16:22 +08:00
#include "drivers/usb.h"
#include "early_init.h"
#include "provision.h"
2017-07-30 09:16:08 +08:00
2017-04-26 09:03:58 +08:00
#include "crypto/rsa.h"
#include "crypto/sha.h"
#include "obj/cert.h"
#include "obj/gitversion.h"
2021-07-15 04:49:28 +08:00
#include "flasher.h"
2017-04-28 11:32:16 +08:00
2019-07-08 06:05:47 +08:00
void __initialize_hardware_early(void) {
early_initialization();
2017-04-07 09:11:36 +08:00
}
2019-07-08 06:05:47 +08:00
void fail(void) {
2017-07-25 06:16:22 +08:00
soft_flasher_start();
2017-04-26 09:03:58 +08:00
}
2017-07-23 05:28:11 +08:00
// know where to sig check
extern void *_app_start[];
2019-01-18 08:17:53 +08: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-08 06:05:47 +08:00
int main(void) {
// Init interrupt table
init_interrupts(true);
disable_interrupts();
2017-04-18 04:57:34 +08:00
clock_init();
Black (#254) * late usb * Added type support for black panda * Added harness presence and orientation detection * harness relay driving code * Added intercept support in black panda code. Switched around can0 and can2 * Disable ADCs after orientation detection. Ignition interrupts via harness * WIP: Hardware abstraction layer + black panda bringup * Fixed bootstub build * Fixed bootstub for pedal * Fixed infinite loops * Got CAN buses working on white again * Fixed pedal build and black can interfaces * Got CAN buses working on black panda * Finished loopback test for black panda * Erase all flash sectors on the panda. Increased binary limit. Added extra python functions. * Fixed python * Made new code MISRA compliant * Cleaned up ignition. Fixed build * Fixed health packet * Fixed CAN mode on black bug. Changed OBD to switch on ELM mode * Fixes from Github review * Fixed MISRA issue for pedal * Fixed failing gmlan tests * ELM327 safety: allow diagnostic on all buses * Cleaned up EON relay code * delete only 3 sectors instead of 11 to allow a new build to be flashed. Much faster to flash * Removed CAN only can0 output mode. Does not make sense on black panda due to reversibility issues. * Added heartbeat logic for EON code on panda. Go to NOOUTPUT if EON does not send a heartbeat for 5 seconds. * Remove all CAN buses live on EON startup. Shouldn't be necessary to have this separate case * Formatting * Added file I forgot to push * Added heartbeat to testing code to make sure EON tests don't fail. Should probably find a better way to do this though. Heartbeat thread didn't work, concurrent USB connection issues... * Safety: support black panda for Honda Bosch * Disable OBD2 if setting to NOOUTPUT mode * Run safety tests for all hw_types * Fail test if subtest fails * fix safety tests
2019-07-24 06:07:06 +08:00
detect_board_type();
2017-07-25 06:16:22 +08:00
if (enter_bootloader_mode == ENTER_SOFTLOADER_MAGIC) {
enter_bootloader_mode = 0;
soft_flasher_start();
}
2017-04-26 09:03:58 +08:00
// validate length
2017-05-02 13:59:10 +08:00
int len = (int)_app_start[0];
2017-07-28 06:54:55 +08:00
if ((len < 8) || (len > (0x1000000 - 0x4000 - 4 - RSANUMBYTES))) goto fail;
2017-04-26 09:03:58 +08:00
// compute SHA hash
2017-05-02 13:59:10 +08:00
uint8_t digest[SHA_DIGEST_SIZE];
2017-04-27 01:41:57 +08:00
SHA_hash(&_app_start[1], len-4, digest);
2017-04-26 09:03:58 +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-26 09:03:58 +08:00
// verify RSA signature
2017-04-29 06:06:01 +08:00
if (RSA_verify(&release_rsa_key, ((void*)&_app_start[0]) + len, RSANUMBYTES, digest, SHA_DIGEST_SIZE)) {
goto good;
2017-04-27 01:41:57 +08:00
}
2017-04-18 04:57:34 +08:00
2017-04-29 11:13:00 +08: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-29 06:06:01 +08:00
}
2017-04-29 11:13:00 +08:00
#endif
2017-04-29 06:06:01 +08:00
// here is a failure
2017-07-28 06:54:55 +08:00
fail:
2017-04-29 06:06:01 +08:00
fail();
2017-07-28 06:54:55 +08:00
return 0;
2017-04-29 06:06:01 +08:00
good:
2017-04-18 04:57:34 +08:00
// jump to flash
2019-07-08 06:05:47 +08:00
((void(*)(void)) _app_start[1])();
2017-04-07 09:11:36 +08:00
return 0;
}