1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-22 22:38:08 +00:00

bootloader: process upload message WIP

This commit is contained in:
Pavol Rusnak 2017-04-27 19:18:57 +02:00
parent 3250967d62
commit 44b32aa4a6
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
3 changed files with 63 additions and 20 deletions

View File

@ -2,6 +2,7 @@
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include <assert.h>
#include "common.h" #include "common.h"
#include "display.h" #include "display.h"
@ -118,7 +119,7 @@ int usb_init_all(void) {
0xc0 // END_COLLECTION 0xc0 // END_COLLECTION
}; };
static const usb_hid_info_t hid_info = { static const usb_hid_info_t hid_info = {
.iface_num = 0x00, .iface_num = 0,
.ep_in = USB_EP_DIR_IN | 0x01, .ep_in = USB_EP_DIR_IN | 0x01,
.ep_out = USB_EP_DIR_OUT | 0x01, .ep_out = USB_EP_DIR_OUT | 0x01,
.subclass = 0, .subclass = 0,
@ -143,6 +144,41 @@ int usb_init_all(void) {
return 0; return 0;
} }
#define UPLOAD_CHUNK_SIZE (128*1024)
#define USB_PACKET_SIZE 64
#define USB_IFACE_NUM 0
void process_upload_chunk(const uint8_t *buf, uint32_t len)
{
// TODO: write to flash
}
uint32_t process_upload_message(uint32_t msg_size, const uint8_t *initbuf, uint32_t initlen)
{
int remains = msg_size - initlen;
// TODO: process initbuf
if (initbuf[0] != 0x0A) {
return 0; // ERROR - payload field not found
}
uint32_t payload_len;
uint32_t p = pb_read_varint(initbuf, &payload_len);
process_upload_chunk(initbuf + p, initlen - p);
uint8_t buf[USB_PACKET_SIZE];
while (remains > 0) {
int r = usb_hid_read_blocking(USB_IFACE_NUM, buf, USB_PACKET_SIZE, 100);
if (r <= 0) {
continue;
}
assert(r == USB_PACKET_SIZE);
process_upload_chunk(buf, 63);
remains -= USB_PACKET_SIZE;
}
DPRINTLN("done");
return 0; // should return >0 if more data required
}
void mainloop(void) void mainloop(void)
{ {
if (0 != flash_init()) { if (0 != flash_init()) {
@ -153,52 +189,45 @@ void mainloop(void)
__fatal_error("usb_init_all failed"); __fatal_error("usb_init_all failed");
} }
uint8_t buf[64]; uint8_t buf[USB_PACKET_SIZE];
for (;;) { for (;;) {
int iface = usb_hid_read_select(1); // 1ms timeout int r = usb_hid_read_blocking(USB_IFACE_NUM, buf, USB_PACKET_SIZE, 100);
if (iface < 0) { if (r <= 0) {
continue;
}
ssize_t r = usb_hid_read(iface, buf, sizeof(buf));
// invalid length
if (r != sizeof(buf)) {
continue; continue;
} }
assert(r == USB_PACKET_SIZE);
uint16_t msg_id; uint16_t msg_id;
uint32_t msg_size; uint32_t msg_size;
// invalid header // invalid header
if (!pb_parse_header(buf, &msg_id, &msg_size)) { if (!pb_parse_header(buf, &msg_id, &msg_size)) {
continue; continue;
} }
static uint32_t chunk = 0;
switch (msg_id) { switch (msg_id) {
case 0: // Initialize case 0: // Initialize
DPRINTLN("received Initialize"); DPRINTLN("received Initialize");
send_msg_Features(iface, false); send_msg_Features(USB_IFACE_NUM, false);
break; break;
case 1: // Ping case 1: // Ping
DPRINTLN("received Ping"); DPRINTLN("received Ping");
send_msg_Success(iface); send_msg_Success(USB_IFACE_NUM);
break; break;
case 6: // FirmwareErase case 6: // FirmwareErase
DPRINTLN("received FirmwareErase"); DPRINTLN("received FirmwareErase");
send_msg_FirmwareRequest(iface, 0, 128 * 1024); send_msg_FirmwareRequest(USB_IFACE_NUM, 0, UPLOAD_CHUNK_SIZE);
chunk = 0;
break; break;
case 7: // FirmwareUpload case 7: // FirmwareUpload
DPRINTLN("received FirmwareUpload"); DPRINTLN("received FirmwareUpload");
// TODO: process chunk uint32_t req_offset = process_upload_message(msg_size, buf + PB_HEADER_LEN, USB_PACKET_SIZE - PB_HEADER_LEN);
chunk++; if (req_offset > 0) {
if (chunk <= 3) { send_msg_FirmwareRequest(USB_IFACE_NUM, req_offset, UPLOAD_CHUNK_SIZE);
send_msg_FirmwareRequest(iface, chunk * 128 * 1024, 128 * 1024);
} else { } else {
send_msg_Success(iface); send_msg_Success(USB_IFACE_NUM);
} }
break; break;
default: default:
DPRINTLN("received unknown message"); DPRINTLN("received unknown message");
send_msg_Failure(iface); send_msg_Failure(USB_IFACE_NUM);
break; break;
} }
} }

View File

@ -81,3 +81,14 @@ bool pb_parse_header(const uint8_t *buf, uint16_t *msg_id, uint32_t *msg_size)
*msg_size = (buf[5] << 24) + (buf[6] << 16) + (buf[7] << 8) + buf[8]; *msg_size = (buf[5] << 24) + (buf[6] << 16) + (buf[7] << 8) + buf[8];
return true; return true;
} }
uint32_t pb_read_varint(const uint8_t *buf, uint32_t *num)
{
uint32_t offset = 0;
*num = 0;
do {
*num += ((*(buf + offset)) & 0x7F) << (7 * offset);
offset++;
} while ((*(buf + offset)) & 0x80);
return offset;
}

View File

@ -4,6 +4,8 @@
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#define PB_HEADER_LEN 9
typedef struct { typedef struct {
uint8_t buf[128]; uint8_t buf[128];
uint32_t pos; uint32_t pos;
@ -18,5 +20,6 @@ void pb_add_string(PB_CTX *ctx, uint32_t field_number, const char *val);
void pb_add_varint(PB_CTX *ctx, uint32_t field_number, uint32_t val); void pb_add_varint(PB_CTX *ctx, uint32_t field_number, uint32_t val);
bool pb_parse_header(const uint8_t *buf, uint16_t *msg_id, uint32_t *msg_size); bool pb_parse_header(const uint8_t *buf, uint16_t *msg_id, uint32_t *msg_size);
uint32_t pb_read_varint(const uint8_t *buf, uint32_t *num);
#endif #endif