diff --git a/firmware/basic/xxtea.c b/firmware/basic/xxtea.c index c768d85..9ae81f8 100644 --- a/firmware/basic/xxtea.c +++ b/firmware/basic/xxtea.c @@ -37,6 +37,21 @@ void htonlp(uint32_t *v, uint8_t n) } } +void xxtea_cbcmac(uint32_t mac[4], uint32_t *data, + uint32_t len, uint32_t const key[4]) +{ + if( len & 0x03 ) + return; + mac[0]=0;mac[1]=0;mac[2]=0;mac[3]=0; + for(int i=0; i>5^y<<2) + (y>>3^z<<4)) ^ ((sum^y) + (k[(p&3)^e] ^ z))) diff --git a/firmware/basic/xxtea.h b/firmware/basic/xxtea.h index e8b99c9..da93394 100644 --- a/firmware/basic/xxtea.h +++ b/firmware/basic/xxtea.h @@ -1,6 +1,8 @@ #ifndef _XXTEA_H_ #define _XXTEA_H_ +void xxtea_cbcmac(uint32_t mac[4], uint32_t *data, + uint32_t len, uint32_t const key[4]); void xxtea_encode_words(uint32_t *v, int n, uint32_t const k[4]); void xxtea_decode_words(uint32_t *v, int n, uint32_t const k[4]); diff --git a/firmware/filesystem/execute.c b/firmware/filesystem/execute.c index 3232f02..ee387b0 100644 --- a/firmware/filesystem/execute.c +++ b/firmware/filesystem/execute.c @@ -10,11 +10,16 @@ #include "filesystem/ff.h" #include "filesystem/select.h" +#include "basic/xxtea.h" + +const uint32_t signature_key[4] = {0,0,0,0}; +const uint32_t decode_key[4] = {0,0,0,0}; + extern void * sram_top; /**************************************************************************/ -void execute_file (const char * fname){ +void execute_file (const char * fname, uint8_t checksignature, uint8_t decode){ FRESULT res; FIL file; UINT readbytes; @@ -27,6 +32,7 @@ void execute_file (const char * fname){ dst=(void (*)(void)) 0x10001800; res=f_open(&file, fname, FA_OPEN_EXISTING|FA_READ); + //lcdPrint("open: "); //lcdPrintln(f_get_rc_string(res)); //lcdRefresh(); @@ -42,6 +48,26 @@ void execute_file (const char * fname){ return; }; + if( decode || checksignature ) + if( readbytes & 0x03 ) + return; + + if( checksignature ){ + uint32_t mac[4]; + uint32_t *data = (uint32_t*)dst; + uint32_t len = readbytes/4; + xxtea_cbcmac(mac, (uint32_t*)dst, len-4, signature_key); + if( data[len-4] != mac[0] || data[len-3] != mac[1] + || data[len-2] != mac[2] || data[len-1] != mac[3] ){ + return; + } + } + + if( decode ){ + uint32_t *data = (uint32_t*)dst; + uint32_t len = readbytes/4; + xxtea_decode_words(data, len, decode_key); + } //lcdPrintInt(readbytes); //lcdPrintln(" bytes"); //lcdRefresh(); @@ -60,6 +86,6 @@ void executeSelect(char *ext){ filename[2]=0; if( selectFile(filename+2,ext) == 0) - execute_file(filename); + execute_file(filename,0,0); }; diff --git a/firmware/filesystem/execute.h b/firmware/filesystem/execute.h index 7242d6d..8c52cae 100644 --- a/firmware/filesystem/execute.h +++ b/firmware/filesystem/execute.h @@ -1,7 +1,7 @@ #ifndef _EXECUTE_H_ #define _EXECUTE_H_ -void execute_file (const char * fname); +void execute_file (const char * fname, uint8_t checksignature, uint8_t decode); void executeSelect(char *ext); #endif diff --git a/firmware/funk/openbeacon.c b/firmware/funk/openbeacon.c index 835d89d..4c1810b 100644 --- a/firmware/funk/openbeacon.c +++ b/firmware/funk/openbeacon.c @@ -10,11 +10,22 @@ const uint32_t key[4] = { 0xB4595344,0xD3E119B6,0xA814D0EC,0xEFF5A24E }; const uint8_t useencryption = 1; const uint8_t mac[5] = {1,2,3,2,1}; -uint32_t oid = 0; -uint32_t seq = 0; -uint8_t strength = 0; +volatile uint32_t oid = 0; +volatile uint32_t seq = 0; +volatile uint8_t strength = 0; -void openbeaconSave() +void openbeaconShutdown(void) +{ + openbeaconSave(seq); +} + +void openbeaconSaveBlock(void) +{ + + openbeaconSave(seq + OPENBEACON_SAVE + 1); +} + +void openbeaconSave(uint32_t s) { FIL file; BYTE buf[4]; @@ -23,7 +34,7 @@ void openbeaconSave() if( f_open(&file, "beacon", FA_OPEN_ALWAYS|FA_WRITE) ) return; - uint32touint8p(seq, buf); + uint32touint8p(s, buf); if( f_write(&file, buf, 4, &readbytes) ) return; @@ -37,7 +48,7 @@ void openbeaconRead() BYTE buf[4]; UINT readbytes; - if( f_open(&file, "beacon", FA_OPEN_EXISTING|FA_READ) ) + if( f_open(&file, "beacon.cfg", FA_OPEN_EXISTING|FA_READ) ) return; if( f_read(&file, buf, 4, &readbytes) ) @@ -53,6 +64,7 @@ void openbeaconSetup(uint32_t id) oid = id; strength = 0; openbeaconRead(); + openbeaconSaveBlock(); } uint8_t openbeaconSendPacket(uint32_t id, uint32_t seq, @@ -80,11 +92,11 @@ uint8_t openbeaconSend(void) nrf_set_strength(strength); nrf_set_tx_mac(sizeof(mac), mac); - status = openbeaconSendPacket(oid, seq++, 0xFF, strength++); + status = openbeaconSendPacket(oid, seq, 0xFF, strength++); if( strength == 4 ) strength = 0; - if( seq % OPENBEACON_SAVECOUNTER == 0 ) - openbeaconSave(); + if( seq++ & OPENBEACON_SAVE == OPENBEACON_SAVE ) + openbeaconSaveBlock(); return status; } diff --git a/firmware/funk/openbeacon.h b/firmware/funk/openbeacon.h index 2d2a539..85d6469 100644 --- a/firmware/funk/openbeacon.h +++ b/firmware/funk/openbeacon.h @@ -5,8 +5,11 @@ #include "funk/nrf24l01p.h" #include "basic/byteorder.h" -#define OPENBEACON_SAVECOUNTER (1024*8) -void openbeaconSave(); +#define OPENBEACON_SAVE 0xFFFF + +void openbeaconShutdown(void); +void openbeaconSaveBlock(void); +void openbeaconSave(uint32_t s); void openbeaconRead(); void openbeaconSetup(uint32_t id); uint8_t openbeaconSendPacket(uint32_t id, uint32_t ctr,