diff --git a/Makefile b/Makefile index 7020159a45..8e51e7ee59 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ flash: ## flash firmware using st-flash st-flash write $(STMHAL_BUILD_DIR)/firmware1.bin 0x8020000 openocd: ## start openocd which connects to the device - openocd -f interface/stlink-v2.cfg -f target/stm32f4x_stlink.cfg + openocd -f interface/stlink-v2.cfg -f target/stm32f4x.cfg gdb: ## start remote gdb session which connects to the openocd gdb $(STMHAL_BUILD_DIR)/firmware.elf -ex 'target remote localhost:3333' diff --git a/assets/colorwheel.toif b/assets/colorwheel.toif index 7ae41becfc..bc4497be55 100644 Binary files a/assets/colorwheel.toif and b/assets/colorwheel.toif differ diff --git a/assets/lock.toig b/assets/lock.toig index 720f0cdf0d..de81220c73 100644 Binary files a/assets/lock.toig and b/assets/lock.toig differ diff --git a/assets/satoshilabs.toif b/assets/satoshilabs.toif index 497f32f14b..84f8a52c16 100644 Binary files a/assets/satoshilabs.toif and b/assets/satoshilabs.toif differ diff --git a/assets/trezor.toig b/assets/trezor.toig index de183b8feb..8ab461e3b0 100644 Binary files a/assets/trezor.toig and b/assets/trezor.toig differ diff --git a/docs/bootloader.md b/docs/bootloader.md index 634cd8a902..97da34d74a 100644 --- a/docs/bootloader.md +++ b/docs/bootloader.md @@ -1,6 +1,6 @@ #TREZOR OS Bootloader -All multibyte integer values are big endian! +All multibyte integer values are little endian! ##Firmware File Format diff --git a/docs/toif.md b/docs/toif.md index cd6b9e8466..0bba3b35b1 100644 --- a/docs/toif.md +++ b/docs/toif.md @@ -1,6 +1,6 @@ #TREZOR Optimized Image Format -All multibyte integer values are big endian! +All multibyte integer values are little endian! ##Header diff --git a/extmod/modtrezorcrypto/rand.c b/extmod/modtrezorcrypto/rand.c new file mode 100644 index 0000000000..ef7df098e1 --- /dev/null +++ b/extmod/modtrezorcrypto/rand.c @@ -0,0 +1,66 @@ +#include "rand.h" + +#ifdef UNIX +#include +#include +static FILE *frand = NULL; +#else +uint32_t rng_get(void); +#endif + +uint32_t random32(void) +{ +#ifdef UNIX + uint32_t r; + size_t len = sizeof(r); + if (!frand) { + frand = fopen("/dev/urandom", "r"); + } + size_t len_read = fread(&r, 1, len, frand); + (void)len_read; + assert(len_read == len); + return r; +#else + return rng_get(); +#endif +} + +uint32_t random_uniform(uint32_t n) +{ + uint32_t x, max = 0xFFFFFFFF - (0xFFFFFFFF % n); + while ((x = random32()) >= max); + return x / (max / n); +} + +void random_buffer(uint8_t *buf, size_t len) +{ +#ifdef UNIX + if (!frand) { + frand = fopen("/dev/urandom", "r"); + } + size_t len_read = fread(buf, 1, len, frand); + (void)len_read; + assert(len_read == len); +#else + size_t i; + uint32_t r = 0; + for (i = 0; i < len; i++) { + if (i % 4 == 0) { + r = random32(); + } + buf[i] = (r >> ((i % 4) * 8)) & 0xFF; + } +#endif +} + +void random_permute(char *str, size_t len) +{ + int i, j; + char t; + for (i = len - 1; i >= 1; i--) { + j = random_uniform(i + 1); + t = str[j]; + str[j] = str[i]; + str[i] = t; + } +} diff --git a/extmod/modtrezorcrypto/rand.h b/extmod/modtrezorcrypto/rand.h new file mode 100644 index 0000000000..4ad63b22e4 --- /dev/null +++ b/extmod/modtrezorcrypto/rand.h @@ -0,0 +1,12 @@ +#ifndef __RAND_H__ +#define __RAND_H__ + +#include +#include + +uint32_t random32(void); +uint32_t random_uniform(uint32_t n); +void random_buffer(uint8_t *buf, size_t len); +void random_permute(char *buf, size_t len); + +#endif diff --git a/extmod/modtrezorui/modtrezorui-display.h b/extmod/modtrezorui/modtrezorui-display.h index caeddbf3cb..a1f43f7bcc 100644 --- a/extmod/modtrezorui/modtrezorui-display.h +++ b/extmod/modtrezorui/modtrezorui-display.h @@ -206,9 +206,9 @@ STATIC mp_obj_t mod_TrezorUi_Display_image(size_t n_args, const mp_obj_t *args) if (bufinfo.len < 8 || memcmp(data, "TOIf", 4) != 0) { nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid image format")); } - mp_int_t w = (data[4] << 8) | data[5]; - mp_int_t h = (data[6] << 8) | data[7]; - mp_int_t datalen = (data[8] << 24) | (data[9] << 16) | (data[10] << 8) | data[11]; + mp_int_t w = *(uint16_t *)(data + 4); + mp_int_t h = *(uint16_t *)(data + 6); + mp_int_t datalen = *(uint32_t *)(data + 8); if ((x < 0) || (y < 0) || (x + w > RESX) || (y + h > RESY)) { nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Out of bounds")); } @@ -230,9 +230,9 @@ STATIC mp_obj_t mod_TrezorUi_Display_icon(size_t n_args, const mp_obj_t *args) { if (bufinfo.len < 8 || memcmp(data, "TOIg", 4) != 0) { nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid image format")); } - mp_int_t w = (data[4] << 8) | data[5]; - mp_int_t h = (data[6] << 8) | data[7]; - mp_int_t datalen = (data[8] << 24) | (data[9] << 16) | (data[10] << 8) | data[11]; + mp_int_t w = *(uint16_t *)(data + 4); + mp_int_t h = *(uint16_t *)(data + 6); + mp_int_t datalen = *(uint32_t *)(data + 8); if ((x < 0) || (y < 0) || (x + w > RESX) || (y + h > RESY)) { nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Out of bounds")); } diff --git a/src/playground/tap_64.toig b/src/playground/tap_64.toig index 9b3f1957dc..1bd987bd00 100644 Binary files a/src/playground/tap_64.toig and b/src/playground/tap_64.toig differ diff --git a/tools/png2toi b/tools/png2toi index b1899e9702..8bd6cfe3f2 100755 --- a/tools/png2toi +++ b/tools/png2toi @@ -45,25 +45,36 @@ def process_image(ifn): if im.mode == 'RGB': ofn = '%s.toif' % ifn[:-4] + pixeldata = process_rgb(w, h, pix) else: ofn = '%s.toig' % ifn[:-4] + pixeldata = process_grayscale(w, h, pix) + z = zlib.compressobj(level=9, wbits=10) + zdata = z.compress(pixeldata) + z.flush() + zdata = zdata[2:-4] # strip header and checksum + with open(ofn, 'wb') as f: if im.mode == 'RGB': f.write(bytes('TOIf', 'ascii')) else: f.write(bytes('TOIg', 'ascii')) - f.write(struct.pack('>HH', w, h)) - if im.mode == 'RGB': - pixeldata = process_rgb(w, h, pix) - else: - pixeldata = process_grayscale(w, h, pix) - z = zlib.compressobj(level=9, wbits=10) - zdata = z.compress(pixeldata) + z.flush() - zdata = zdata[2:-4] # strip header and checksum - f.write(struct.pack('>I', len(zdata))) + f.write(struct.pack('