Skip to content

Commit

Permalink
Replace the Reloader mess with an all-in-one Bootloader update file.
Browse files Browse the repository at this point in the history
Also allow the update file to be supplied on SD Card (Enhanced Gotek only).

Fixes #229
  • Loading branch information
keirf committed May 23, 2019
1 parent 772c7d3 commit 756333a
Show file tree
Hide file tree
Showing 17 changed files with 197 additions and 80 deletions.
21 changes: 9 additions & 12 deletions Makefile
Expand Up @@ -4,7 +4,7 @@ export FW_VER := 2.10a
PROJ := FlashFloppy
VER := v$(FW_VER)

SUBDIRS += src bootloader reloader
SUBDIRS += src bootloader bl_update

.PHONY: all upd clean flash start serial gotek

Expand All @@ -17,31 +17,29 @@ all:
$(MAKE) -f $(ROOT)/Rules.mk all

clean:
rm -f *.hex *.upd *.rld *.dfu *.html
rm -f *.hex *.upd *.dfu *.html
$(MAKE) -f $(ROOT)/Rules.mk $@

gotek: all
mv FF.dfu FF_Gotek-$(VER).dfu
mv FF.upd FF_Gotek-$(VER).upd
mv FF.hex FF_Gotek-$(VER).hex
mv BL.rld FF_Gotek-Bootloader-$(VER).rld
mv RL.upd FF_Gotek-Reloader-$(VER).upd
mv BL.upd FF_Gotek-Bootloader-$(VER).upd

HXC_FF_URL := https://www.github.com/keirf/HxC_FF_File_Selector
HXC_FF_URL := $(HXC_FF_URL)/releases/download
HXC_FF_VER := v5-FF

dist:
rm -rf flashfloppy-*
mkdir -p flashfloppy-$(VER)/alt/reloader
mkdir -p flashfloppy-$(VER)/alt/bootloader
mkdir -p flashfloppy-$(VER)/alt/logfile
$(MAKE) clean
$(MAKE) gotek
cp -a FF_Gotek-$(VER).dfu flashfloppy-$(VER)/
cp -a FF_Gotek-$(VER).upd flashfloppy-$(VER)/
cp -a FF_Gotek-$(VER).hex flashfloppy-$(VER)/
cp -a FF_Gotek-Reloader-$(VER).upd flashfloppy-$(VER)/alt/reloader/
cp -a FF_Gotek-Bootloader-$(VER).rld flashfloppy-$(VER)/alt/reloader/
cp -a FF_Gotek-Bootloader-$(VER).upd flashfloppy-$(VER)/alt/bootloader/
$(MAKE) clean
debug=n logfile=y $(MAKE) -f $(ROOT)/Rules.mk upd
mv FF.upd flashfloppy-$(VER)/alt/logfile/FF_Gotek-Logfile-$(VER).upd
Expand Down Expand Up @@ -74,19 +72,18 @@ all:
$(MAKE) -C src -f $(ROOT)/Rules.mk $(PROJ).elf $(PROJ).bin $(PROJ).hex
bootloader=y $(MAKE) -C bootloader -f $(ROOT)/Rules.mk \
Bootloader.elf Bootloader.bin Bootloader.hex
reloader=y $(MAKE) -C reloader -f $(ROOT)/Rules.mk \
Reloader.elf Reloader.bin Reloader.hex
logfile=n $(MAKE) -C bl_update -f $(ROOT)/Rules.mk \
BL_Update.elf BL_Update.bin BL_Update.hex
srec_cat bootloader/Bootloader.hex -Intel src/$(PROJ).hex -Intel \
-o FF.hex -Intel
$(PYTHON) ./scripts/mk_update.py src/$(PROJ).bin FF.upd
$(PYTHON) ./scripts/mk_update.py bootloader/Bootloader.bin BL.rld
$(PYTHON) ./scripts/mk_update.py reloader/Reloader.bin RL.upd
$(PYTHON) ./scripts/mk_update.py bl_update/BL_Update.bin BL.upd
$(PYTHON) ./scripts/dfu-convert.py -i FF.hex FF.dfu

endif

BAUD=115200
DEV=/dev/ttyUSB1
DEV=/dev/ttyUSB0

flash:
sudo stm32flash -b $(BAUD) -w FF_Gotek-$(VER).hex $(DEV)
Expand Down
4 changes: 1 addition & 3 deletions Rules.mk
Expand Up @@ -21,9 +21,7 @@ FLAGS += -DNDEBUG
endif

# Following options are mutually exclusive
ifeq ($(reloader),y)
FLAGS += -DRELOADER=1
else ifeq ($(bootloader),y)
ifeq ($(bootloader),y)
FLAGS += -DBOOTLOADER=1
else ifeq ($(logfile),y)
FLAGS += -DLOGFILE=1
Expand Down
File renamed without changes.
8 changes: 1 addition & 7 deletions reloader/Makefile → bl_update/Makefile
@@ -1,28 +1,22 @@
RPATH = ../src

OBJS += update.o
OBJS += bl_update.o
OBJS += fpec.o

OBJS += build_info.o
OBJS += cancellation.o
OBJS += crc.o
OBJS += vectors.o
OBJS += fs.o
OBJS += spi.o
OBJS += string.o
OBJS += stm32f10x.o
OBJS += time.o
OBJS += timer.o
OBJS += util.o
OBJS += volume.o
OBJS += flash_cfg.o

OBJS-$(debug) += console.o

SUBDIRS += display
SUBDIRS += fatfs
SUBDIRS += gotek
SUBDIRS += usb

.PHONY: $(RPATH)/build_info.c
build_info.o: CFLAGS += -DFW_VER="\"$(FW_VER)\""
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion bootloader/Makefile
@@ -1,13 +1,14 @@
RPATH = ../src

OBJS += update.o
OBJS += fw_update.o
OBJS += fpec.o

OBJS += build_info.o
OBJS += cancellation.o
OBJS += crc.o
OBJS += vectors.o
OBJS += fs.o
OBJS += sd_spi.o
OBJS += spi.o
OBJS += string.o
OBJS += stm32f10x.o
Expand Down
2 changes: 1 addition & 1 deletion inc/cache.h
Expand Up @@ -9,7 +9,7 @@
* See the file COPYING for more details, or visit <http://unlicense.org>.
*/

#if !defined(BOOTLOADER) && !defined(RELOADER)
#if !defined(BOOTLOADER)

/* Use memory range (@start,@end) to cache data items of size @item_sz. */
struct cache *cache_init(void *start, void *end, unsigned int item_sz);
Expand Down
3 changes: 3 additions & 0 deletions inc/intrinsics.h
Expand Up @@ -13,6 +13,9 @@ struct exception_frame {
uint32_t r0, r1, r2, r3, r12, lr, pc, psr;
};

#define _STR(x) #x
#define STR(x) _STR(x)

#define __aligned(x) __attribute__((aligned(x)))
#define __packed __attribute((packed))
#define always_inline __inline__ __attribute__((always_inline))
Expand Down
4 changes: 0 additions & 4 deletions reloader/fatfs/Makefile

This file was deleted.

8 changes: 0 additions & 8 deletions reloader/usb/Makefile

This file was deleted.

14 changes: 0 additions & 14 deletions reloader/usb/stm32_usbh_msc/Makefile

This file was deleted.

1 change: 1 addition & 0 deletions scripts/stm32f10x.ld.S
Expand Up @@ -13,6 +13,7 @@ SECTIONS
.text : {
_stext = .;
*(.vector_table)
*(.vector_table*)
*(.text)
*(.text*)
*(.rodata)
Expand Down
171 changes: 171 additions & 0 deletions src/bl_update.c
@@ -0,0 +1,171 @@
/*
* bl_update.c
*
* Main firmware containing a payload of an updated bootloader.
*
* Procedure:
* - Place this *.UPD file on your USB stick and follow usual update process.
*
* Status messages:
* CLr -> Erasing flash
* Prg -> Programming flash
*
* Error messages:
* E05 -> Flash error (bad CRC on verify)
*
* Written & released by Keir Fraser <keir.xen@gmail.com>
*
* This is free and unencumbered software released into the public domain.
* See the file COPYING for more details, or visit <http://unlicense.org>.
*/

/* Reflash the main bootloader (first 32kB). */
#define FIRMWARE_START 0x08000000
#define FIRMWARE_END 0x08008000

/* The update payload. */
extern char update_start[], update_end[];
asm (
" .section .rodata\n"
" .align 4\n"
" .global update_start, update_end\n"
"update_start:\n"
" .incbin \"../bootloader/Bootloader.bin\"\n"
"update_end:\n"
" .previous\n"
);

/* Only the vector table in low 2kB, as we erase first page of firmware,
* and we mustn't erase the code we're executing. */
asm (
" .section .vector_table.padding\n"
" .balign "STR(FLASH_PAGE_SIZE)"\n"
" .previous\n"
);

int EXC_reset(void) __attribute__((alias("main")));

uint8_t board_id;

static void erase_old_firmware(void)
{
uint32_t p;
for (p = FIRMWARE_START; p < FIRMWARE_END; p += FLASH_PAGE_SIZE)
fpec_page_erase(p);
}

static void msg_display(const char *p)
{
printk("[%s]\n", p);
switch (display_mode) {
case DM_LED_7SEG:
led_7seg_write_string(p);
break;
case DM_LCD_1602:
lcd_write(6, 1, 0, p);
lcd_sync();
break;
}
}

static void display_setting(bool_t on)
{
switch (display_mode) {
case DM_LED_7SEG:
led_7seg_display_setting(on);
break;
case DM_LCD_1602:
lcd_backlight(on);
lcd_sync();
break;
}
}

int main(void)
{
void *buf = update_start;
unsigned int nr = update_end - update_start;
int retries = 0;

/* Relocate DATA. Initialise BSS. */
if (_sdat != _ldat)
memcpy(_sdat, _ldat, _edat-_sdat);
memset(_sbss, 0, _ebss-_sbss);

/* Initialise the world. */
stm32_init();
time_init();
console_init();
board_init();
delay_ms(200); /* 5v settle */

printk("\n** FF Update Firmware for Gotek\n", fw_ver);
printk("** Keir Fraser <keir.xen@gmail.com>\n");
printk("** https://github.com/keirf/FlashFloppy\n\n");

flash_ff_cfg_read();

display_init();
switch (display_mode) {
case DM_LED_7SEG:
msg_display("BLD");
break;
case DM_LCD_1602:
lcd_write(0, 0, 0, "New Bootloader..");
lcd_write(0, 1, 0, " [ ]");
lcd_sync();
break;
}

display_setting(TRUE);

for (retries = 0; retries < 5; retries++) {

/* Erase the old firmware. */
msg_display("CLR");
fpec_init();
erase_old_firmware();

/* Program the new firmware. */
msg_display("PRG");
fpec_write(buf, nr, FIRMWARE_START);

if (!memcmp((void *)FIRMWARE_START, buf, nr))
goto success;

}

/* An error occurred. Report it on the display. */
msg_display("ERR");

/* Erase the bootloader. It's now damaged. */
erase_old_firmware();

/* Spin forever. We're toast. */
for (;;)
continue;

success:
/* No errors. */
printk("Success!\n");

/* Clear the display. */
display_setting(FALSE);

/* All done. Erase ourself and reset. */
IRQ_global_disable();
fpec_page_erase((uint32_t)_stext);
system_reset();

return 0;
}

/*
* Local variables:
* mode: C
* c-file-style: "Linux"
* c-basic-offset: 4
* tab-width: 4
* indent-tabs-mode: nil
* End:
*/
6 changes: 3 additions & 3 deletions src/fatfs/ffconf.h
Expand Up @@ -8,7 +8,7 @@
/ Function Configurations
/---------------------------------------------------------------------------*/

#if defined(BOOTLOADER) || defined(RELOADER)
#if defined(BOOTLOADER)
#define FF_FS_READONLY 1
#else
#define FF_FS_READONLY 0
Expand All @@ -19,7 +19,7 @@
/ and optional writing functions as well. */


#if defined(BOOTLOADER) || defined(RELOADER)
#if defined(BOOTLOADER)
#define FF_FS_MINIMIZE 1
#else
#define FF_FS_MINIMIZE 0
Expand Down Expand Up @@ -50,7 +50,7 @@
/* This option switches f_mkfs() function. (0:Disable or 1:Enable) */


#if defined(BOOTLOADER) || defined(RELOADER)
#if defined(BOOTLOADER)
#define FF_USE_FASTSEEK 0
#else
#define FF_USE_FASTSEEK 1
Expand Down

0 comments on commit 756333a

Please sign in to comment.