diff --git a/ChangeLog b/ChangeLog index ccbc36e..ef8dd91 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2017-09-29 NIIBE Yutaka + * src/main.c [FLASH_UPGRADE_SUPPORT] (main): Factor out flash ROM upgrade + support. + (calculate_regnual_entry_address): Likewise. + * src/usb_ctrl.c (usb_setup, download_check_crc32): Likewise. + * src/openpgp.c (modify_binary): Fix for 64-bit machine. * src/openpgp-do.c (encrypt, decrypt): Likewise. (gpg_data_scan): Likewise. diff --git a/src/Makefile b/src/Makefile index 8ebeb41..2742959 100644 --- a/src/Makefile +++ b/src/Makefile @@ -31,7 +31,9 @@ USE_USB = yes USE_ADC = yes USE_EVENTFLAG = yes -ifneq ($(EMULATION),) +ifeq ($(EMULATION),) +DEFS += -DFLASH_UPGRADE_SUPPORT +else DEFS += -DBN256_C_IMPLEMENTATION endif diff --git a/src/main.c b/src/main.c index a66396a..70b701a 100644 --- a/src/main.c +++ b/src/main.c @@ -1,7 +1,7 @@ /* * main.c - main routine of Gnuk * - * Copyright (C) 2010, 2011, 2012, 2013, 2015, 2016 + * Copyright (C) 2010, 2011, 2012, 2013, 2015, 2016, 2017 * Free Software Initiative of Japan * Author: NIIBE Yutaka * @@ -35,8 +35,11 @@ #include "usb_lld.h" #include "usb-cdc.h" #include "random.h" +#ifdef GNU_LINUX_EMULATION +#include +#else #include "mcu/stm32f103.h" - +#endif /* * main thread does 1-bit LED display output @@ -167,22 +170,24 @@ led_blink (int spec) eventflag_signal (&led_event, spec); } +#ifdef FLASH_UPGRADE_SUPPORT /* * In Gnuk 1.0.[12], reGNUal was not relocatable. * Now, it's relocatable, but we need to calculate its entry address * based on it's pre-defined address. */ #define REGNUAL_START_ADDRESS_COMPATIBLE 0x20001400 -static uint32_t +static uintptr_t calculate_regnual_entry_address (const uint8_t *addr) { const uint8_t *p = addr + 4; - uint32_t v = p[0] + (p[1] << 8) + (p[2] << 16) + (p[3] << 24); + uintptr_t v = p[0] + (p[1] << 8) + (p[2] << 16) + (p[3] << 24); v -= REGNUAL_START_ADDRESS_COMPATIBLE; - v += (uint32_t)addr; + v += (uintptr_t)addr; return v; } +#endif #define STACK_MAIN #define STACK_PROCESS_1 @@ -206,7 +211,9 @@ extern uint32_t bDeviceState; int main (int argc, char *argv[]) { - uint32_t entry; +#ifdef FLASH_UPGRADE_SUPPORT + uintptr_t entry; +#endif chopstx_t ccid_thd; (void)argc; @@ -287,8 +294,9 @@ main (int argc, char *argv[]) /* Finish application. */ chopstx_join (ccid_thd, NULL); +#ifdef FLASH_UPGRADE_SUPPORT /* Set vector */ - SCB->VTOR = (uint32_t)&_regnual_start; + SCB->VTOR = (uintptr_t)&_regnual_start; entry = calculate_regnual_entry_address (&_regnual_start); #ifdef DFU_SUPPORT #define FLASH_SYS_START_ADDR 0x08000000 @@ -296,7 +304,7 @@ main (int argc, char *argv[]) #define CHIP_ID_REG ((uint32_t *)0xE0042000) { extern uint8_t _sys; - uint32_t addr; + uintptr_t addr; handler *new_vector = (handler *)FLASH_SYS_START_ADDR; void (*func) (void (*)(void)) = (void (*)(void (*)(void)))new_vector[9]; uint32_t flash_page_size = 1024; /* 1KiB default */ @@ -320,6 +328,9 @@ main (int argc, char *argv[]) /* Leave Gnuk to exec reGNUal */ flash_erase_all_and_exec ((void (*)(void))entry); #endif +#else + exit (0); +#endif /* Never reached */ return 0; diff --git a/src/usb_ctrl.c b/src/usb_ctrl.c index 41370c6..5481823 100644 --- a/src/usb_ctrl.c +++ b/src/usb_ctrl.c @@ -202,13 +202,16 @@ static const uint8_t data_rate_table[] = { 0x80, 0x25, 0, 0, }; /* dwDataRate */ static const uint8_t lun_table[] = { 0, 0, 0, 0, }; #endif +#ifdef FLASH_UPGRADE_SUPPORT static const uint8_t *const mem_info[] = { &_regnual_start, __heap_end__, }; +#endif #define USB_FSIJ_GNUK_MEMINFO 0 #define USB_FSIJ_GNUK_DOWNLOAD 1 #define USB_FSIJ_GNUK_EXEC 2 #define USB_FSIJ_GNUK_CARD_CHANGE 3 +#ifdef FLASH_UPGRADE_SUPPORT /* After calling this function, CRC module remain enabled. */ static int download_check_crc32 (struct usb_dev *dev, const uint32_t *end_p) @@ -226,6 +229,7 @@ download_check_crc32 (struct usb_dev *dev, const uint32_t *end_p) return -1; } +#endif int usb_setup (struct usb_dev *dev) @@ -237,15 +241,22 @@ usb_setup (struct usb_dev *dev) { if (USB_SETUP_GET (arg->type)) { +#ifdef FLASH_UPGRADE_SUPPORT if (arg->request == USB_FSIJ_GNUK_MEMINFO) return usb_lld_ctrl_send (dev, mem_info, sizeof (mem_info)); +#else + return -1; +#endif } else /* SETUP_SET */ { +#ifdef FLASH_UPGRADE_SUPPORT uint8_t *addr = sram_address ((arg->value * 0x100) + arg->index); +#endif if (arg->request == USB_FSIJ_GNUK_DOWNLOAD) { +#ifdef FLASH_UPGRADE_SUPPORT if (*ccid_state_p != CCID_STATE_EXITED) return -1; @@ -257,9 +268,13 @@ usb_setup (struct usb_dev *dev) 256 - (arg->index + arg->len)); return usb_lld_ctrl_recv (dev, addr, arg->len); +#else + return -1; +#endif } else if (arg->request == USB_FSIJ_GNUK_EXEC && arg->len == 0) { +#ifdef FLASH_UPGRADE_SUPPORT if (*ccid_state_p != CCID_STATE_EXITED) return -1; @@ -267,6 +282,9 @@ usb_setup (struct usb_dev *dev) return -1; return download_check_crc32 (dev, (uint32_t *)addr); +#else + return -1; +#endif } else if (arg->request == USB_FSIJ_GNUK_CARD_CHANGE && arg->len == 0) {