Commit 1f7473d2 by Enrico Pozzobon

mostly similar firmwares

parent 0bf790c5
......@@ -3,20 +3,22 @@
#include "uartp.h"
#define MAX_BYTES 100
#define CMDBUF_LEN 72
static uint8_t cmdbuf[CMDBUF_LEN];
//#define DEBUG
#define CRYPTO_BUSY A7
#define SerialOut Serial1
uint8_t npub[CRYPTO_NPUBBYTES];
uint8_t nsec[CRYPTO_NSECBYTES];
uint8_t k[CRYPTO_KEYBYTES];
uint8_t ad[MAX_BYTES];
unsigned long long int adlen;
uint8_t m[MAX_BYTES];
unsigned long long int mlen;
uint8_t c[MAX_BYTES];
unsigned long long int clen;
int res;
unsigned long long int adlen = 0;
unsigned long long int mlen = 0;
unsigned long long int clen = 0;
int res = 0;
void setup();
void loop();
......@@ -26,13 +28,13 @@ extern "C" {
#endif
void uart_wbyte(uint8_t x) {
Serial1.write((uint8_t) (x));
SerialOut.write((uint8_t) (x));
}
uint8_t uart_rbyte() {
int r;
do {
r = Serial1.read();
r = SerialOut.read();
} while (r == -1);
return (uint8_t) (0xff & r);
}
......@@ -41,56 +43,37 @@ uint8_t uart_rbyte() {
}
#endif
int dbg_printf(const char *format, ...) {
int r = 0;
#ifdef DEBUG
#define DEBUG_BUF_LEN 80
char printbuf[DEBUG_BUF_LEN+2];
va_list vargs;
va_start(vargs, format);
r = vsnprintf(printbuf+1, DEBUG_BUF_LEN, format, vargs);
va_end(vargs);
if (r < 0) {
memcpy(printbuf+1, "DEBUG ERROR\r\n", 13);
r = 13;
}
r = r > DEBUG_BUF_LEN ? DEBUG_BUF_LEN : r;
printbuf[0] = 0xde; // Debug messages should start with "\xde"
uartp_send(printbuf, r+1);
#endif
return r;
}
void my_assert(bool b) {
if (b)
return;
dbg_printf("Assertion failed\r\n");
for(;;)
yield();
}
void setup() {
Serial1.begin(115200);
SerialOut.begin(115200);
pinMode(CRYPTO_BUSY, OUTPUT);
digitalWrite(CRYPTO_BUSY, HIGH);
delay(100);
Serial1.print("Hello, World!");
memset(npub, 0, CRYPTO_NPUBBYTES);
memset(nsec, 0, CRYPTO_NSECBYTES);
memset(k, 0, CRYPTO_KEYBYTES);
memset(ad, 0, MAX_BYTES);
memset(m, 0, MAX_BYTES);
memset(c, 0, MAX_BYTES);
SerialOut.print("Hello, World!");
}
void loop() {
static uint8_t buf[256];
uint16_t len = uartp_recv(buf, 255);
uint8_t action = buf[0];
if (len == 0 || len > 255)
int res;
uint16_t len = uartp_recv(cmdbuf, CMDBUF_LEN - 1);
uint8_t action = cmdbuf[0];
if (len == 0 || len > CMDBUF_LEN - 1)
return;
uint16_t l = len - 1;
uint16_t rl = 0;
uint8_t *var = buf+1;
uint8_t *var = cmdbuf+1;
switch (action) {
case 'm': my_assert(l <= MAX_BYTES); memcpy(m, var, l); mlen = l; break;
case 'c': my_assert(l <= MAX_BYTES); memcpy(c, var, l); clen = l; break;
......@@ -124,10 +107,9 @@ void loop() {
case'S': var = nsec; rl = CRYPTO_NSECBYTES; break;
case'R': var = (uint8_t *) &res; rl = sizeof(res); break;
default:
dbg_printf("Unknown command\r\n");
my_assert(false);
}
buf[0] = action;
memcpy(buf+1, var, rl);
uartp_send(buf, rl+1);
cmdbuf[0] = action;
memcpy(cmdbuf+1, var, rl);
uartp_send(cmdbuf, rl+1);
}
......@@ -3,19 +3,22 @@
#include "uartp.h"
#define MAX_BYTES 100
#define CMDBUF_LEN 72
static uint8_t cmdbuf[CMDBUF_LEN];
//#define DEBUG
#define CRYPTO_BUSY 12
#define SerialOut Serial
uint8_t npub[CRYPTO_NPUBBYTES];
uint8_t nsec[CRYPTO_NSECBYTES];
uint8_t k[CRYPTO_KEYBYTES];
uint8_t ad[MAX_BYTES];
unsigned long long int adlen;
uint8_t m[MAX_BYTES];
unsigned long long int mlen;
uint8_t c[MAX_BYTES];
unsigned long long int clen;
unsigned long long int adlen = 0;
unsigned long long int mlen = 0;
unsigned long long int clen = 0;
int res = 0;
void setup();
void loop();
......@@ -24,14 +27,14 @@ void loop();
extern "C" {
#endif
void uart_write(uint8_t x) {
Serial.write((uint8_t) (x));
void uart_wbyte(uint8_t x) {
SerialOut.write((uint8_t) (x));
}
uint8_t uart_read() {
uint8_t uart_rbyte() {
int r;
do {
r = Serial.read();
r = SerialOut.read();
} while (r == -1);
return (uint8_t) (0xff & r);
}
......@@ -40,58 +43,37 @@ uint8_t uart_read() {
}
#endif
#ifdef DEBUG
#define DEBUG_BUF_LEN 80
int dbg_printf(const char *format, ...) {
char printbuf[DEBUG_BUF_LEN+2];
va_list vargs;
va_start(vargs, format);
int r = vsnprintf(printbuf+1, DEBUG_BUF_LEN, format, vargs);
va_end(vargs);
if (r < 0) {
memcpy(printbuf+1, "DEBUG ERROR\r\n", 13);
r = 13;
}
r = r > DEBUG_BUF_LEN ? DEBUG_BUF_LEN : r;
printbuf[0] = 0xde; // Debug messages should start with "\xde"
uartp_send(printbuf, r+1);
return r;
}
#else
#define dbg_printf(...) (0)
#endif
void my_assert(bool b) {
if (b)
return;
dbg_printf("Assertion failed\r\n");
for(;;)
yield();
}
void setup() {
Serial.begin(115200);
SerialOut.begin(115200);
pinMode(CRYPTO_BUSY, OUTPUT);
digitalWrite(CRYPTO_BUSY, HIGH);
delay(100);
Serial.print("Hello, World!");
memset(npub, 0, CRYPTO_NPUBBYTES);
memset(nsec, 0, CRYPTO_NSECBYTES);
memset(k, 0, CRYPTO_KEYBYTES);
memset(ad, 0, MAX_BYTES);
memset(m, 0, MAX_BYTES);
memset(c, 0, MAX_BYTES);
SerialOut.print("Hello, World!");
}
void loop() {
static uint8_t buf[256];
int res;
uint16_t len = uartp_recv(buf, 255);
uint8_t action = buf[0];
if (len == 0 || len > 255)
uint16_t len = uartp_recv(cmdbuf, CMDBUF_LEN - 1);
uint8_t action = cmdbuf[0];
if (len == 0 || len > CMDBUF_LEN - 1)
return;
uint16_t l = len - 1;
uint16_t rl = 0;
uint8_t *var = buf+1;
uint8_t *var = cmdbuf+1;
switch (action) {
case 'm': my_assert(l <= MAX_BYTES); memcpy(m, var, l); mlen = l; break;
case 'c': my_assert(l <= MAX_BYTES); memcpy(c, var, l); clen = l; break;
......@@ -123,11 +105,11 @@ void loop() {
case'K': var = k; rl = CRYPTO_KEYBYTES; break;
case'P': var = npub; rl = CRYPTO_NPUBBYTES; break;
case'S': var = nsec; rl = CRYPTO_NSECBYTES; break;
case'R': var = (uint8_t *) &res; rl = sizeof(res); break;
default:
dbg_printf("Unknown command\r\n");
my_assert(false);
}
buf[0] = action;
memcpy(buf+1, var, rl);
uartp_send(buf, rl+1);
cmdbuf[0] = action;
memcpy(cmdbuf+1, var, rl);
uartp_send(cmdbuf, rl+1);
}
#include <stdint.h>
#include "uartp.h"
extern void uart_write(uint8_t x);
extern uint8_t uart_read();
extern void uart_wbyte(uint8_t x);
extern uint8_t uart_rbyte();
// Simple serial protocol with packets and checksum
const uint8_t AMUX_TAG = 0xf9;
......@@ -13,24 +13,24 @@ void uartp_send(const void *src, uint16_t len) {
uint8_t len_ind_0, len_ind_1, fcs, info;
const uint8_t *buf = (const uint8_t *) src;
uart_write(AMUX_TAG);
uart_wbyte(AMUX_TAG);
len_ind_0 = (uint8_t) (0xff & len);
len_ind_1 = (uint8_t) (0xff & (len >> 7));
if (len < 128) {
uart_write(len_ind_0);
uart_wbyte(len_ind_0);
} else {
uart_write(len_ind_0 | AMUX_EXT);
uart_write(len_ind_1);
uart_wbyte(len_ind_0 | AMUX_EXT);
uart_wbyte(len_ind_1);
}
fcs = 0;
for (uint16_t i = 0; i < len; i++) {
info = buf[i];
fcs += info;
uart_write(buf[i]);
uart_wbyte(buf[i]);
}
fcs = 255 - fcs;
uart_write(fcs);
uart_write(AMUX_END);
uart_wbyte(fcs);
uart_wbyte(AMUX_END);
}
......@@ -44,13 +44,13 @@ uint16_t uartp_recv(void *dst, uint16_t buf_len) {
do {
tag_old = tag;
tag = uart_read();
tag = uart_rbyte();
} while(tag != AMUX_TAG || tag_old != AMUX_END);
len = (uint16_t) uart_read();
len = (uint16_t) uart_rbyte();
if (len & AMUX_EXT) {
len &= (~AMUX_EXT);
len |= (uint16_t) (uart_read() << 7);
len |= (uint16_t) (uart_rbyte() << 7);
}
if (len > buf_len) {
return len;
......@@ -59,12 +59,12 @@ uint16_t uartp_recv(void *dst, uint16_t buf_len) {
uint16_t i = 0;
cs = 0;
for (i = 0; i < len; i++) {
info = uart_read();
info = uart_rbyte();
buf[i] = info;
cs += info;
}
cs += uart_read();
tag = uart_read();
cs += uart_rbyte();
tag = uart_rbyte();
if (0xff == cs) {
if (AMUX_END == tag) {
return len;
......
......@@ -57,31 +57,9 @@ static inline void interrupts() {
// TODO: if necessary, provide a way to enable interrupts
}
#define DEBUG_BUF_LEN 80
int dbg_printf(const char *format, ...) {
int r = 0;
char printbuf[DEBUG_BUF_LEN+2];
va_list vargs;
va_start(vargs, format);
r = vsnprintf(printbuf+1, DEBUG_BUF_LEN, format, vargs);
va_end(vargs);
if (r < 0) {
memcpy(printbuf+1, "DEBUG ERROR\r\n", 13);
r = 13;
}
r = r > DEBUG_BUF_LEN ? DEBUG_BUF_LEN : r;
printbuf[0] = 0xde; // Debug messages should start with "\xde"
uartp_send(printbuf, r+1);
return r;
}
void my_assert(bool b) {
if (b)
return;
dbg_printf("Assertion failed\r\n");
Error_Handler();
for(;;);
}
......@@ -140,7 +118,6 @@ void test_loop() {
case'S': var = nsec; rl = CRYPTO_NSECBYTES; break;
case'R': var = (uint8_t *) &res; rl = sizeof(res); break;
default:
dbg_printf("Unknown command\r\n");
my_assert(false);
}
buf[0] = action;
......
......@@ -3,32 +3,38 @@
#include "uartp.h"
#define MAX_BYTES 100
#define CMDBUF_LEN 72
static uint8_t cmdbuf[CMDBUF_LEN];
//#define DEBUG
#define CRYPTO_BUSY 12
#define SerialOut Serial
uint8_t npub[CRYPTO_NPUBBYTES];
uint8_t nsec[CRYPTO_NSECBYTES];
uint8_t k[CRYPTO_KEYBYTES];
uint8_t ad[MAX_BYTES];
unsigned long long int adlen;
uint8_t m[MAX_BYTES];
unsigned long long int mlen;
uint8_t c[MAX_BYTES];
unsigned long long int clen;
unsigned long long int adlen = 0;
unsigned long long int mlen = 0;
unsigned long long int clen = 0;
int res = 0;
void setup();
void loop();
#ifdef __cplusplus
extern "C" {
#endif
void uart_write(uint8_t x) {
Serial.write((uint8_t) (x));
void uart_wbyte(uint8_t x) {
SerialOut.write((uint8_t) (x));
}
uint8_t uart_read() {
uint8_t uart_rbyte() {
int r;
do {
r = Serial.read();
r = SerialOut.read();
} while (r == -1);
return (uint8_t) (0xff & r);
}
......@@ -37,65 +43,44 @@ uint8_t uart_read() {
}
#endif
#ifdef DEBUG
#define DEBUG_BUF_LEN 80
int dbg_printf(const char *format, ...) {
char printbuf[DEBUG_BUF_LEN+2];
va_list vargs;
va_start(vargs, format);
int r = vsnprintf(printbuf+1, DEBUG_BUF_LEN, format, vargs);
va_end(vargs);
if (r < 0) {
memcpy(printbuf+1, "DEBUG ERROR\r\n", 13);
r = 13;
}
r = r > DEBUG_BUF_LEN ? DEBUG_BUF_LEN : r;
printbuf[0] = 0xde; // Debug messages should start with "\xde"
uartp_send(printbuf, r+1);
return r;
}
#else
#define dbg_printf(...) (0)
#endif
void assert(bool b) {
void my_assert(bool b) {
if (b)
return;
dbg_printf("Assertion failed\r\n");
for(;;)
yield();
}
void setup() {
Serial.begin(115200);
SerialOut.begin(115200);
pinMode(CRYPTO_BUSY, OUTPUT);
digitalWrite(CRYPTO_BUSY, HIGH);
delay(100);
Serial.print("Hello, World!");
memset(npub, 0, CRYPTO_NPUBBYTES);
memset(nsec, 0, CRYPTO_NSECBYTES);
memset(k, 0, CRYPTO_KEYBYTES);
memset(ad, 0, MAX_BYTES);
memset(m, 0, MAX_BYTES);
memset(c, 0, MAX_BYTES);
SerialOut.print("Hello, World!");
}
void loop() {
static uint8_t buf[256];
int res;
uint16_t len = uartp_recv(buf, 255);
uint8_t action = buf[0];
if (len == 0 || len > 255)
uint16_t len = uartp_recv(cmdbuf, CMDBUF_LEN - 1);
uint8_t action = cmdbuf[0];
if (len == 0 || len > CMDBUF_LEN - 1)
return;
uint16_t l = len - 1;
uint16_t rl = 0;
uint8_t *var = buf+1;
uint8_t *var = cmdbuf+1;
switch (action) {
case 'm': assert(l <= MAX_BYTES); memcpy(m, var, l); mlen = l; break;
case 'c': assert(l <= MAX_BYTES); memcpy(c, var, l); clen = l; break;
case 'a': assert(l <= MAX_BYTES); memcpy(ad, var, l); adlen = l; break;
case 'k': assert(l == CRYPTO_KEYBYTES); memcpy(k, var, l); break;
case 'p': assert(l == CRYPTO_NPUBBYTES); memcpy(npub, var, l); break;
case 's': assert(l == CRYPTO_NSECBYTES); memcpy(nsec, var, l); break;
case 'm': my_assert(l <= MAX_BYTES); memcpy(m, var, l); mlen = l; break;
case 'c': my_assert(l <= MAX_BYTES); memcpy(c, var, l); clen = l; break;
case 'a': my_assert(l <= MAX_BYTES); memcpy(ad, var, l); adlen = l; break;
case 'k': my_assert(l == CRYPTO_KEYBYTES); memcpy(k, var, l); break;
case 'p': my_assert(l == CRYPTO_NPUBBYTES); memcpy(npub, var, l); break;
case 's': my_assert(l == CRYPTO_NSECBYTES); memcpy(nsec, var, l); break;
case 'e':
noInterrupts();
asm("nop");
......@@ -120,11 +105,11 @@ void loop() {
case'K': var = k; rl = CRYPTO_KEYBYTES; break;
case'P': var = npub; rl = CRYPTO_NPUBBYTES; break;
case'S': var = nsec; rl = CRYPTO_NSECBYTES; break;
case'R': var = (uint8_t *) &res; rl = sizeof(res); break;
default:
dbg_printf("Unknown command\r\n");
assert(false);
my_assert(false);
}
buf[0] = action;
memcpy(buf+1, var, rl);
uartp_send(buf, rl+1);
cmdbuf[0] = action;
memcpy(cmdbuf+1, var, rl);
uartp_send(cmdbuf, rl+1);
}
#include <stdint.h>
#include "uartp.h"
extern void uart_write(uint8_t x);
extern uint8_t uart_read();
extern void uart_wbyte(uint8_t x);
extern uint8_t uart_rbyte();
// Simple serial protocol with packets and checksum
const uint8_t AMUX_TAG = 0xf9;
......@@ -13,24 +13,24 @@ void uartp_send(const void *src, uint16_t len) {
uint8_t len_ind_0, len_ind_1, fcs, info;
const uint8_t *buf = (const uint8_t *) src;
uart_write(AMUX_TAG);
uart_wbyte(AMUX_TAG);
len_ind_0 = (uint8_t) (0xff & len);
len_ind_1 = (uint8_t) (0xff & (len >> 7));
if (len < 128) {
uart_write(len_ind_0);
uart_wbyte(len_ind_0);
} else {
uart_write(len_ind_0 | AMUX_EXT);
uart_write(len_ind_1);
uart_wbyte(len_ind_0 | AMUX_EXT);
uart_wbyte(len_ind_1);
}
fcs = 0;
for (uint16_t i = 0; i < len; i++) {
info = buf[i];
fcs += info;
uart_write(buf[i]);
uart_wbyte(buf[i]);
}
fcs = 255 - fcs;
uart_write(fcs);
uart_write(AMUX_END);
uart_wbyte(fcs);
uart_wbyte(AMUX_END);
}
......@@ -44,13 +44,13 @@ uint16_t uartp_recv(void *dst, uint16_t buf_len) {
do {
tag_old = tag;
tag = uart_read();
tag = uart_rbyte();
} while(tag != AMUX_TAG || tag_old != AMUX_END);
len = (uint16_t) uart_read();
len = (uint16_t) uart_rbyte();
if (len & AMUX_EXT) {
len &= (~AMUX_EXT);
len |= (uint16_t) (uart_read() << 7);
len |= (uint16_t) (uart_rbyte() << 7);
}
if (len > buf_len) {
return len;
......@@ -59,12 +59,12 @@ uint16_t uartp_recv(void *dst, uint16_t buf_len) {
uint16_t i = 0;
cs = 0;
for (i = 0; i < len; i++) {
info = uart_read();
info = uart_rbyte();
buf[i] = info;
cs += info;
}
cs += uart_read();
tag = uart_read();
cs += uart_rbyte();
tag = uart_rbyte();
if (0xff == cs) {
if (AMUX_END == tag) {
return len;
......
......@@ -104,6 +104,21 @@ class UARTP:
else:
return buf
def stdin_read(n):
b = sys.stdin.buffer.read(n)
if len(b) != n:
sys.exit(1)
return b
def stdin_readvar():
l = stdin_read(4)
(l, ) = struct.unpack("<I", l)
v = stdin_read(l)
return v
def main(argv):
eprint(argv[0])
script_dir = os.path.split(argv[0])[0]
......@@ -113,22 +128,15 @@ def main(argv):
dev = get_serial()
flash(dev)
eprint("Flashed")
time.sleep(1)
time.sleep(0.1)
ser = serial.Serial(dev, baudrate=115200, timeout=5)
uartp = UARTP(ser)
def stdin_read(n):
b = sys.stdin.buffer.read(n)
if len(b) != n:
sys.exit(1)
return b
def stdin_readvar():
l = stdin_read(4)
(l, ) = struct.unpack("<I", l)
v = stdin_read(l)
return v
ser.setDTR(True)
time.sleep(0.01)
ser.setDTR(False)
time.sleep(1)
exp_hello = b"Hello, World!"
hello = ser.read(len(exp_hello))
......@@ -167,7 +175,7 @@ def main(argv):
c = struct.pack("B", action)
uartp.send(c)
ack = uartp.recv()
if len(ack) != 1 or ack[0] != action:
if len(ack) < 1 or ack[0] != action:
raise Exception("Unacknowledged variable transfer")
eprint("Operation %c completed successfully" % action)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment