You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					108 lines
				
				2.4 KiB
			
		
		
			
		
	
	
					108 lines
				
				2.4 KiB
			| 
								 
											6 years ago
										 
									 | 
							
								#define BOOTSTUB
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#include "config.h"
							 | 
						||
| 
								 | 
							
								#include "obj/gitversion.h"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#ifdef STM32F4
							 | 
						||
| 
								 | 
							
								  #define PANDA
							 | 
						||
| 
								 | 
							
								  #include "stm32f4xx.h"
							 | 
						||
| 
								 | 
							
								  #include "stm32f4xx_hal_gpio_ex.h"
							 | 
						||
| 
								 | 
							
								#else
							 | 
						||
| 
								 | 
							
								  #include "stm32f2xx.h"
							 | 
						||
| 
								 | 
							
								  #include "stm32f2xx_hal_gpio_ex.h"
							 | 
						||
| 
								 | 
							
								#endif
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								// ******************** 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 *********************
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								#include "libc.h"
							 | 
						||
| 
								 | 
							
								#include "provision.h"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#include "drivers/clock.h"
							 | 
						||
| 
								 | 
							
								#include "drivers/llgpio.h"
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								#include "board.h"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								#include "gpio.h"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#include "drivers/spi.h"
							 | 
						||
| 
								 | 
							
								#include "drivers/usb.h"
							 | 
						||
| 
								 | 
							
								//#include "drivers/uart.h"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#include "crypto/rsa.h"
							 | 
						||
| 
								 | 
							
								#include "crypto/sha.h"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#include "obj/cert.h"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#include "spi_flasher.h"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								void __initialize_hardware_early(void) {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  early();
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								void fail(void) {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  soft_flasher_start();
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								// know where to sig check
							 | 
						||
| 
								 | 
							
								extern void *_app_start[];
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								// 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.
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								int main(void) {
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  disable_interrupts();
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  clock_init();
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  detect_configuration();
							 | 
						||
| 
								 | 
							
								  detect_board_type();
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  current_board->set_usb_power_mode(USB_POWER_CLIENT);
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								  if (enter_bootloader_mode == ENTER_SOFTLOADER_MAGIC) {
							 | 
						||
| 
								 | 
							
								    enter_bootloader_mode = 0;
							 | 
						||
| 
								 | 
							
								    soft_flasher_start();
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  // validate length
							 | 
						||
| 
								 | 
							
								  int len = (int)_app_start[0];
							 | 
						||
| 
								 | 
							
								  if ((len < 8) || (len > (0x1000000 - 0x4000 - 4 - RSANUMBYTES))) goto fail;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  // compute SHA hash
							 | 
						||
| 
								 | 
							
								  uint8_t digest[SHA_DIGEST_SIZE];
							 | 
						||
| 
								 | 
							
								  SHA_hash(&_app_start[1], len-4, digest);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  // verify RSA signature
							 | 
						||
| 
								 | 
							
								  if (RSA_verify(&release_rsa_key, ((void*)&_app_start[0]) + len, RSANUMBYTES, digest, SHA_DIGEST_SIZE)) {
							 | 
						||
| 
								 | 
							
								    goto good;
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  // 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;
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								#endif
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								// here is a failure
							 | 
						||
| 
								 | 
							
								fail:
							 | 
						||
| 
								 | 
							
								  fail();
							 | 
						||
| 
								 | 
							
								  return 0;
							 | 
						||
| 
								 | 
							
								good:
							 | 
						||
| 
								 | 
							
								  // jump to flash
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  ((void(*)(void)) _app_start[1])();
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  return 0;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 |