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

bootloader: refactor pb_parse_header to protobuf

This commit is contained in:
Pavol Rusnak 2017-04-27 15:32:25 +02:00
parent 92b2deb932
commit 3250967d62
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
3 changed files with 16 additions and 4 deletions

View File

@ -12,6 +12,7 @@
#include "version.h"
#include "messages.h"
#include "protobuf.h"
#define IMAGE_MAGIC 0x465A5254 // TRZF
#define IMAGE_MAXSIZE (7 * 128 * 1024)
@ -164,13 +165,12 @@ void mainloop(void)
if (r != sizeof(buf)) {
continue;
}
uint16_t msg_id;
uint32_t msg_size;
// invalid header
if (buf[0] != '?' || buf[1] != '#' || buf[2] != '#') {
if (!pb_parse_header(buf, &msg_id, &msg_size)) {
continue;
}
uint16_t msg_id = (buf[3] << 8) + buf[4];
uint32_t msg_size = (buf[5] << 24) + (buf[6] << 16) + (buf[7] << 8) + buf[8];
(void)msg_size;
static uint32_t chunk = 0;
switch (msg_id) {
case 0: // Initialize

View File

@ -71,3 +71,13 @@ void pb_add_varint(PB_CTX *ctx, uint32_t field_number, uint32_t val)
pb_varint(ctx, field_number);
pb_varint(ctx, val);
}
bool pb_parse_header(const uint8_t *buf, uint16_t *msg_id, uint32_t *msg_size)
{
if (buf[0] != '?' || buf[1] != '#' || buf[2] != '#') {
return false;
}
*msg_id = (buf[3] << 8) + buf[4];
*msg_size = (buf[5] << 24) + (buf[6] << 16) + (buf[7] << 8) + buf[8];
return true;
}

View File

@ -17,4 +17,6 @@ void pb_add_bool(PB_CTX *ctx, uint32_t field_number, bool val);
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);
bool pb_parse_header(const uint8_t *buf, uint16_t *msg_id, uint32_t *msg_size);
#endif