use little endian for toif, fix random

This commit is contained in:
Pavol Rusnak 2016-04-27 18:45:00 +02:00
parent b7013437ee
commit bc7cb88cdf
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
13 changed files with 110 additions and 21 deletions

View File

@ -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'

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,6 +1,6 @@
#TREZOR OS Bootloader
All multibyte integer values are big endian!
All multibyte integer values are little endian!
##Firmware File Format

View File

@ -1,6 +1,6 @@
#TREZOR Optimized Image Format
All multibyte integer values are big endian!
All multibyte integer values are little endian!
##Header

View File

@ -0,0 +1,66 @@
#include "rand.h"
#ifdef UNIX
#include <stdio.h>
#include <assert.h>
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;
}
}

View File

@ -0,0 +1,12 @@
#ifndef __RAND_H__
#define __RAND_H__
#include <stdint.h>
#include <stdlib.h>
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

View File

@ -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"));
}

Binary file not shown.

View File

@ -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('<HH', w, h))
f.write(struct.pack('<I', len(zdata)))
f.write(zdata)
print('Written %s ... %d bytes' % (ofn, 4 + 4 + len(zdata)))
print('Written %s ... %d bytes' % (ofn, 4 + 4 + len(zdata)))
with open(ofn + '.h', 'wt') as f:
f.write('static const uint8_t toi_%s[] = {\n' % ifn[:-4])
if im.mode == 'RGB':
f.write(" 'T', 'O', 'I', 'f',\n")
else:
f.write(" 'T', 'O', 'I', 'g',\n")
f.write(' (uint16_t)%d, (uint16_t)%d,\n' % (w, h))
f.write(' (uint32_t)%d,\n' % len(zdata))
f.write(' ')
for b in zdata:
f.write(' 0x%02x,' % b)
f.write('\n};\n')
print('Written %s ...' % (ofn + '.h'))
def main():

2
vendor/micropython vendored

@ -1 +1 @@
Subproject commit 8678e9d6158fd0fb3b564266aa718334f5522d52
Subproject commit 130c6ff92931b7895616a5c4a230ddbe625d0b94