Skip to content
Snippets Groups Projects

Bootloader rework

Merged schneider requested to merge schneider/bootloader-rework into master
Files
2
+ 55
15
@@ -32,6 +32,24 @@
DIR dir;
FATFS FatFs;
int format(void)
{
BYTE work[FF_MAX_SS * 16];
/* Create FAT volume */
int res = f_mkfs("", FM_ANY | FM_SFD, 0, work, sizeof work);
if (res != FR_OK) {
printf("Failed to make new FS %d\n", res);
return -1;
}
f_setlabel("card10");
if (res != FR_OK) {
printf("Failed to set volume name %d\n", res);
return -1;
}
return 0;
}
int mount(void)
{
FRESULT res;
@@ -195,6 +213,19 @@ static void pmic_button(bool falling)
}
}
static void msc(void)
{
bootloader_display_header();
bootloader_display_line(3, "USB activated.", 0xffff);
bootloader_display_line(4, "Ready.", 0xffff);
run_usbmsc();
// If we return, don't try to boot. Maybe rather trigger a software reset.
// Reason: Not sure in which state the USB peripheral is and what kind
// of interrupts are active.
while (1)
;
}
/******************************************************************************/
int main(void)
{
@@ -210,16 +241,7 @@ int main(void)
// If the button is pressed, we go into MSC mode.
if (PB_Get(3)) {
bootloader_display_header();
bootloader_display_line(3, "USB activated.", 0xffff);
bootloader_display_line(4, "Ready.", 0xffff);
run_usbmsc();
// If we return, don't try to boot. Maybe rather trigger a software reset.
// Reason: Not sure in which state the USB peripheral is and what kind
// of interrupts are active.
while (1)
;
msc();
}
if (mount() == 0) {
@@ -244,6 +266,7 @@ int main(void)
);
erase_partition();
flash_partition();
f_unlink("card10.bin");
bootloader_display_line(
4, "Trying to boot", 0xffff
);
@@ -251,14 +274,31 @@ int main(void)
printf("No update needed\n");
}
}
} else {
bootloader_display_header();
bootloader_display_line(
3, "Failed to mount filesystem", 0xffff
);
printf("Failed to mount the external flash\n");
bootloader_display_line(3, "Creating new filesystem", 0xffff);
printf("Creating new filesystem\n");
if (format() == 0) {
/* Drop into MSC after a reboot */
card10_reset();
} else {
bootloader_display_line(
3, "Failed to create new filesystem", 0xffff
);
printf("Feiled to create new filesystem\n");
/* Prevent bootloops */
while (1) {
}
}
}
bootloader_display_line(4, "Trying to boot", 0xffff);
/* Get the intital SP of the firmware. If it is 0xFFFFFFFF, no image has been
* flashed yet. Drop into MSC for initial flashing. */
if (*((uint32_t *)PARTITION_START) == 0xFFFFFFFF) {
printf("No valid image in flash\n");
msc();
}
printf("Trying to boot\n");
Loading