bootloader/bootloader.c

127 lines
3.1 KiB
C
Raw Normal View History

2022-05-19 08:57:38 -04:00
/*
===============================================================================
Name : bootloader.c
Author : $(author)
Version :
Copyright : $(copyright)
Description : main definition
===============================================================================
*/
#ifdef __USE_CMSIS
#include "LPC17xx.h"
#endif
#include <cr_section_macros.h>
#include <stdio.h>
#include <string.h>
#include "uart_commands.h"
error_code status;
2022-05-24 10:52:09 -04:00
/*
is_even_assembly:
ldr sp, [r0]
ret
*/
2022-05-19 08:57:38 -04:00
2022-05-24 06:48:02 -04:00
char* argvs[3][10];
2022-05-19 08:57:38 -04:00
cmd_t cmd;
2022-05-28 13:30:55 -04:00
uint32_t* size_store = (uint32_t*) 0x3400;
crc_t* crc_store = (crc_t*) 0x3000;
2022-05-24 06:48:02 -04:00
2022-05-19 08:57:38 -04:00
uint32_t offset, total_size, offset_pointer;
crc_t crc_prog, crc_data;
// TODO: insert other include files here
// TODO: insert other definitions and declarations here
2022-05-19 15:37:51 -04:00
char buff[256];
2022-05-22 13:52:44 -04:00
2022-05-28 13:30:55 -04:00
int is_code_valid(){
crc_t expected_crc = *crc_store;
size_t application_size = *size_store;
crc_t real_crc = crc_init();
real_crc = crc_update(real_crc, (void*)APP_OFFSET, application_size);
real_crc = crc_finalize(real_crc);
error_code is_error = uart_commands_check(real_crc, expected_crc);
if(is_error == ok){
return 1;
}
else return 0;
}
void write_app_info(crc_t* checksum, size_t* size){
iap_prepare_sectors(&status, addr_to_sector(0x3000), addr_to_sector(0x3fff));
if(status != ok){
uart_send_err();
}
iap_erase_sectors(&status, addr_to_sector(0x3000), addr_to_sector(0x3fff));
if(status != ok){
uart_send_err();
}
iap_prepare_sectors(&status, addr_to_sector(0x3000), addr_to_sector(0x3fff));
if(status != ok){
uart_send_err();
}
iap_copy_to_flash(&status, (long unsigned int *)crc_store, (long unsigned int*)checksum, 0x0400);
iap_copy_to_flash(&status, (long unsigned int *)size_store, (long unsigned int*)size, 0x0400);
}
2022-05-19 08:57:38 -04:00
int main(void) {
2022-05-28 13:30:55 -04:00
cmd.argv = (char**)argvs;
//verify b is pressed
//checksum is correct (
//bootloader_init();
2022-05-19 08:57:38 -04:00
uart_init(115200);
2022-05-28 13:30:55 -04:00
if(is_code_valid()){
SCB->VTOR = APP_OFFSET;
__asm("ldr r0,=0x4000");
__asm("ldr sp,[r0]");
__asm("ldr pc,[r0,#4]");
}else{
while(1){
uart_receive_command(buff);
uart_parse_command(buff, &cmd);
if (strncmp("GETID", cmd.argv[0], 5)==0){
uart_commands_getid(&status);
}else if(strncmp("GETSE", cmd.argv[0], 5)==0){
uart_commands_getserial(&status);
}else if(strncmp("PROG", cmd.argv[0], 4)==0){
crc_prog = uart_string_to_int(cmd.argv[3]);
total_size = uart_string_to_int(cmd.argv[2]);
offset = uart_string_to_int(cmd.argv[1]);
write_app_info(&crc_prog, (unsigned int *)&total_size);
crc_data = uart_commands_prog(&status, total_size);
offset_pointer = offset;
}else if(strcmp("DATA", cmd.argv[0])==0){
int size = uart_string_to_int(cmd.argv[1]);
crc_t local_checksum = uart_string_to_int(cmd.argv[2]);
uart_commands_data(&status, size, &crc_data, local_checksum, offset_pointer);
if(status == 0) offset_pointer += size;
}else if(strncmp("CHECK", cmd.argv[0], 5)==0){
error_code is_error = uart_commands_check(crc_prog, crc_data);
if(is_error == ok){
uart_send_ok();
}else{
uart_send_err();
}
2022-05-19 08:57:38 -04:00
2022-05-28 13:30:55 -04:00
}
2022-05-19 15:37:51 -04:00
}
2022-05-28 13:30:55 -04:00
}
2022-05-19 08:57:38 -04:00
while(1);
return 0 ;
}