From a861f4a89f8fb404a1a4f32d7160dc37b19d7209 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Sun, 20 Jun 2010 18:10:26 +0200 Subject: bwbootloader: add bootstrapper --- src/bwbootloader/avrconfig.h | 4 ++++ src/bwbootloader/bwbootloader.c | 9 ++++++--- src/bwbootloader/bwbootloader.h | 14 ++++++++++++++ src/bwbootloader/prog.c | 7 ++++++- src/bwbootloader/spacer.avr.S | 7 +++++++ 5 files changed, 37 insertions(+), 4 deletions(-) (limited to 'src/bwbootloader') diff --git a/src/bwbootloader/avrconfig.h b/src/bwbootloader/avrconfig.h index 9c378b9..09a802b 100644 --- a/src/bwbootloader/avrconfig.h +++ b/src/bwbootloader/avrconfig.h @@ -56,4 +56,8 @@ /** Slave transmission buffer size. */ #define AC_TWI_SLAVE_SEND_BUFFER_SIZE (64 + 4) +/* bwbootloader */ +/** Make a bootstrapper (i.e. application to reprogram the bootloader). */ +#define AC_BWBOOTLOADER_BOOTSTRAP 0 + #endif /* avrconfig_h */ diff --git a/src/bwbootloader/bwbootloader.c b/src/bwbootloader/bwbootloader.c index 82644ca..1f6f606 100644 --- a/src/bwbootloader/bwbootloader.c +++ b/src/bwbootloader/bwbootloader.c @@ -42,6 +42,9 @@ main (void) { twi_init (0xb8); timer_init (); +#if AC_BWBOOTLOADER_BOOTSTRAP + start_delay_pause = 1; +#endif /* Handle bootloader messages. */ start_delay = 40; while (start_delay) @@ -78,10 +81,10 @@ bwbootloader_recv (const uint8_t *buffer, uint8_t size) if (size != 1) break; rbuffer[0] = buffer[0]; - memcpy (&rbuffer[1], "bwbootloader", 12); + memcpy (&rbuffer[1], BWBOOTLOADER_SIGNATURE, 12); rbuffer[13] = PROG_PAGE_SIZE; - rbuffer[14] = (PROG_FLASH_SIZE - BWBOOTLOADER_SIZE) & 0xff; - rbuffer[15] = (PROG_FLASH_SIZE - BWBOOTLOADER_SIZE) >> 8; + rbuffer[14] = BWBOOTLOADER_LIMIT & 0xff; + rbuffer[15] = BWBOOTLOADER_LIMIT >> 8; twi_slave_update (rbuffer, 16); return; case BWBOOTLOADER_START: diff --git a/src/bwbootloader/bwbootloader.h b/src/bwbootloader/bwbootloader.h index a6a51b9..d292b48 100644 --- a/src/bwbootloader/bwbootloader.h +++ b/src/bwbootloader/bwbootloader.h @@ -27,6 +27,20 @@ /** Reserved size for bootloader. */ #define BWBOOTLOADER_SIZE 1024 +/** Limit of writable flash. */ +#if !AC_BWBOOTLOADER_BOOTSTRAP +# define BWBOOTLOADER_LIMIT (PROG_FLASH_SIZE - BWBOOTLOADER_SIZE) +#else +# define BWBOOTLOADER_LIMIT PROG_FLASH_SIZE +#endif + +/** Signature. */ +#if !AC_BWBOOTLOADER_BOOTSTRAP +# define BWBOOTLOADER_SIGNATURE "bwbootloader" +#else +# define BWBOOTLOADER_SIGNATURE "bwbootstrapp" +#endif + #ifndef __ASSEMBLER__ /** Bootloader commands. */ diff --git a/src/bwbootloader/prog.c b/src/bwbootloader/prog.c index 2bd7fa6..f2b5144 100644 --- a/src/bwbootloader/prog.c +++ b/src/bwbootloader/prog.c @@ -40,7 +40,7 @@ uint8_t prog_page_write (uint16_t address, const uint8_t *buffer) { /* Bootloader area is protected. */ - if (address >= PROG_FLASH_SIZE - BWBOOTLOADER_SIZE) + if (address >= BWBOOTLOADER_LIMIT) return 0; /* Aligned write only. */ if (address % PROG_PAGE_SIZE != 0) @@ -53,7 +53,12 @@ prog_page_write (uint16_t address, const uint8_t *buffer) { /* Protect reset vector. */ if (address == 0 && i == 0) + { w = pgm_read_word (0); + if (AC_BWBOOTLOADER_BOOTSTRAP + && w != *(const uint16_t *) (buffer)) + return 0; + } else w = *(const uint16_t *) (buffer + i); boot_page_fill (address + i, w); diff --git a/src/bwbootloader/spacer.avr.S b/src/bwbootloader/spacer.avr.S index 215c395..59c7754 100644 --- a/src/bwbootloader/spacer.avr.S +++ b/src/bwbootloader/spacer.avr.S @@ -25,7 +25,14 @@ #include #include "bwbootloader.h" +#if !AC_BWBOOTLOADER_BOOTSTRAP .section .progmem .global application_entry application_entry: .skip FLASHEND + 1 - BWBOOTLOADER_SIZE - _VECTORS_SIZE, 0xff +#else + .global application_entry + .set application_entry, 0 + .section .progmem + .skip SPM_PAGESIZE - _VECTORS_SIZE, 0xff +#endif -- cgit v1.2.3