diff --git a/Makefile b/Makefile index 8ef594a..16df0b5 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ LD:=$(CROSS_COMPILE)gcc USECFS:=$(HOME)/src/usecfs WOLFSSL:=$(HOME)/src/wolfssl -OBJS:=startup.o main.o system.o $(USECFS)/src/usecfs_dev_spi.o $(USECFS)/src/usecfs.o mem.o led.o \ +OBJS:=startup.o main.o system.o usecfs_dev_spi.o $(USECFS)/src/usecfs.o mem.o led.o \ i2c.o display.o font_twisted.o spi.o spi_flash.o button.o systick.o newlib.o uart.o ui.o \ sdcard.o random.o OBJS+= \ @@ -30,7 +30,7 @@ LSCRIPT:=target.ld OBJCOPY:=$(CROSS_COMPILE)objcopy -CFLAGS:=-mcpu=cortex-m3 -mthumb -Wall -Wno-main -Wstack-usage=200 -ffreestanding -Wno-unused -DBLOCK_SIZE=512 -I$(USECFS)/src -I. -I$(WOLFSSL) -Ilib/unicore-mx/include +CFLAGS:=-mcpu=cortex-m3 -mthumb -Wall -Wno-main -Wstack-usage=200 -ffreestanding -Wno-unused -DBLOCK_SIZE=4096 -I$(USECFS)/src -I. -I$(WOLFSSL) -Ilib/unicore-mx/include CFLAGS+=-specs=nano.specs -lc -lg CFLAGS+=$(UMXFLAGS) #CFLAGS+=-O2 diff --git a/button.c b/button.c index 8830131..c1b030a 100644 --- a/button.c +++ b/button.c @@ -19,6 +19,16 @@ #include "unicore-mx/stm32/f4/adc.h" #include "unicore-mx/stm32/f4/nvic.h" +// Uncomment to enable debug +//#define BUTTON_DEBUG + + +#ifdef BUTTON_DEBUG +# define DBG printf +#else +# define DBG(...) do {} while (0) +#endif + #define BUTTON_DEBOUNCE_TIME 50 #define BUTTON_HOLD_TIME 1500 @@ -142,7 +152,7 @@ int button_poll(void (*callback)(uint8_t press, int hold)) if((Buttons[b].state == IDLE) || (Buttons[b].state == RELEASING)) { Buttons[b].state = PRESSING; Buttons[b].transition_start_timestamp = jiffies; - printf("%06u: ST: PRESSING\r\n", jiffies); + DBG("%06u: ST: PRESSING\r\n", jiffies); button_press_pending = 0; return 0; } else { @@ -153,13 +163,13 @@ int button_poll(void (*callback)(uint8_t press, int hold)) Buttons[b].state = HOLD; Buttons[b].transition_start_timestamp = jiffies; button_press_pending = 0; - printf("%06u: ST: HOLD\r\n", jiffies); + DBG("%06u: ST: HOLD\r\n", jiffies); } } else if (p_interval > BUTTON_DEBOUNCE_TIME) { if (Buttons[b].state == PRESSING) { callback(b, 0); Buttons[b].state = PRESSED; - printf("%06u: ST: PRESSED\r\n", jiffies); + DBG("%06u: ST: PRESSED\r\n", jiffies); button_press_pending = 0; } if (Buttons[b].state == HOLD) { @@ -178,12 +188,12 @@ int button_poll(void (*callback)(uint8_t press, int hold)) if ((jiffies - Buttons[i].transition_start_timestamp) > (BUTTON_DEBOUNCE_TIME)) { Buttons[i].state = IDLE; Buttons[i].transition_start_timestamp = 0; - printf("%06u: ST: IDLE\r\n", jiffies); + DBG("%06u: ST: IDLE\r\n", jiffies); } } else if (Buttons[i].state != IDLE) { Buttons[i].state = RELEASING; Buttons[i].transition_start_timestamp = jiffies; - printf("%06u: ST:0\r\n", jiffies); + DBG("%06u: ST:0\r\n", jiffies); } } } diff --git a/sdcard.c b/sdcard.c index 0e4e354..2a204b7 100644 --- a/sdcard.c +++ b/sdcard.c @@ -31,9 +31,7 @@ #include "system.h" #include "sdcard.h" -#if (!defined(BLOCK_SIZE)) || (BLOCK_SIZE != 512) -# error BLOCK_SIZE undefined or invalid -#endif +#define SDCARD_BLOCK_SIZE 512 #ifndef NULL #define NULL (void *)(0x00000000) @@ -391,7 +389,7 @@ sdio_scr(struct dev_sd *sd, SDIO_CARD c) { * Read a Block from our Card * * NB: There is a possibly useless test in this code, during the read - * phase it allows that the SDIO card might try to send more than BLOCK_SIZE + * phase it allows that the SDIO card might try to send more than SDCARD_BLOCK_SIZE * bytes (128 32 bit longs) and allows it to do so, constantly over * writing the last long in the just-in-case-over-long-by-1 data buffer. * To compromise the system you would need a borked or custom crafted @@ -410,14 +408,14 @@ int sdcard_read(void *dev, void *_buf, uint32_t lba) uint32_t buf_len = 0; if (! SDIO_CARD_CCS(sd->card)) { - addr = lba * BLOCK_SIZE; // non HC cards use byte address + addr = lba * SDCARD_BLOCK_SIZE; // non HC cards use byte address } err = sdio_select(sd, sd->card->rca); if (! err) { - err = stm32_sdio_command(sd, 16, BLOCK_SIZE); + err = stm32_sdio_command(sd, 16, SDCARD_BLOCK_SIZE); if (!err) { SDIO_DTIMER = 0xffffffff; - SDIO_DLEN = BLOCK_SIZE; + SDIO_DLEN = SDCARD_BLOCK_SIZE; SDIO_DCTRL = SDIO_DCTRL_DBLOCKSIZE_9 | SDIO_DCTRL_DTDIR | SDIO_DCTRL_DTEN; @@ -442,7 +440,7 @@ int sdcard_read(void *dev, void *_buf, uint32_t lba) err = SDIO_EUNKNOWN; // Unknown Error! } } else { - err = BLOCK_SIZE; + err = SDCARD_BLOCK_SIZE; } } } @@ -471,16 +469,16 @@ int sdcard_write(void *dev, void *_buf, uint32_t lba) if (! SDIO_CARD_CCS(c)) { - addr = lba * BLOCK_SIZE; // non HC cards use byte address + addr = lba * SDCARD_BLOCK_SIZE; // non HC cards use byte address } err = sdio_select(sd, c->rca); if (! err) { - /* Set Block Size to BLOCK_SIZE */ - err = stm32_sdio_command(sd, 16, BLOCK_SIZE); + /* Set Block Size to SDCARD_BLOCK_SIZE */ + err = stm32_sdio_command(sd, 16, SDCARD_BLOCK_SIZE); if (!err) { SDIO_DTIMER = 0xffffffff; - SDIO_DLEN = BLOCK_SIZE; + SDIO_DLEN = SDCARD_BLOCK_SIZE; SDIO_DCTRL = SDIO_DCTRL_DBLOCKSIZE_9 | SDIO_DCTRL_DTEN; err = stm32_sdio_command(sd, 24, addr); @@ -509,7 +507,7 @@ int sdcard_write(void *dev, void *_buf, uint32_t lba) // deselect the card (void) sdio_select(sd, 0); if (err == 0) - return BLOCK_SIZE; + return SDCARD_BLOCK_SIZE; return err; } @@ -689,10 +687,10 @@ stm32_sdio_open(struct dev_sd *sd) { * In the V1 Case : * Size = 1< #include "usecfs.h" + +/* Uncomment for device initialization */ +//#define DEVICE_INITIALIZATION + static const char welcome_message0[]= " There is no "; static const char welcome_message1[]= "knowledge that "; static const char welcome_message2[]= " is not power. "; + static const uint8_t uuid[UUID_LEN] = { 0xb4, 0x6b, 0x82, 0x05, 0xb6, 0xcd, 0x28, 0xaa, 0xc7, 0x81, 0x2e, 0x2d, 0xfd, 0xdd, 0x5e, 0x70, 0x3f, 0xbf, 0x09, 0x03, 0x1b, 0x5f, 0xbe, 0xc6, @@ -55,15 +60,22 @@ void ui_msg(const char *txt) void lock(void) { //led_on(RED_LED); - ui_msg("Locked."); + ui_msg("Device is Locked"); fsm_state = ST_LOCKED; } +static void pwrst(void) +{ + memset(password, 0, MAX_PASSWORD); + pin_idx = 0; +} + int unlock(void) { uint8_t stored_uuid[UUID_LEN]; if (pin_idx < 6) { - ui_msg("pin code too short"); + display_text(6, "Failed."); + pwrst(); return -1; } display_scroll(NULL, 0); @@ -72,7 +84,7 @@ int unlock(void) display_text(5, "Unlocking secret"); if ((usecfs_init(password, 0, stored_uuid) == 0) && (memcmp(stored_uuid, uuid, UUID_LEN) == 0)) { - memset(password, 0, MAX_PASSWORD); + pwrst(); fsm_state = ST_UNLOCKED; ui_msg("Device UNLOCKED"); return 0; @@ -85,8 +97,7 @@ int unlock(void) } #endif display_text(6, "Failed."); - memset(password, 0, MAX_PASSWORD); - pin_idx = 0; + pwrst(); fsm_state = ST_LOCKED; return -1; } @@ -134,9 +145,8 @@ void ui_button_press(uint16_t button) int i; char c; if (fsm_state == ST_LOCKED) { + pwrst(); ui_msg("Insert pin:"); - pin_idx = 0; - memset(password, 0, MAX_PASSWORD); fsm_state = ST_PIN_ENTRY; return; }