Adds dump mode and no-fix mode
This commit is contained in:
parent
5b9a07c92f
commit
d1cc5c3f4c
|
@ -22,7 +22,9 @@ add_executable(umcumgr
|
|||
src/image.c
|
||||
src/main.c
|
||||
src/crc16_sw.c
|
||||
src/gpio.c src/error.c)
|
||||
src/gpio.c
|
||||
src/error.c
|
||||
src/dump.c)
|
||||
|
||||
if(SANITIZE)
|
||||
message(STATUS "SANITIZE Enabled")
|
||||
|
|
|
@ -0,0 +1,71 @@
|
|||
//
|
||||
// Created by nock on 2019-06-19.
|
||||
//
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "dump.h"
|
||||
|
||||
static bool g_enabled = false;
|
||||
|
||||
void dump_enable(void) {
|
||||
g_enabled = true;
|
||||
}
|
||||
|
||||
static char *hex(unsigned char in) {
|
||||
static char out[3] = {0};
|
||||
static char lookup[] = {
|
||||
'0', '1', '2', '3', '4', '5', '6', '7',
|
||||
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
|
||||
};
|
||||
out[0] = lookup[in >> 4];
|
||||
out[1] = lookup[in & 0xf];
|
||||
out[2] = 0;
|
||||
return out;
|
||||
}
|
||||
|
||||
static void hex_print(char *in, size_t len) {
|
||||
char tmp[len*2+1];
|
||||
tmp[len] = 0;
|
||||
for (int i = 0; i < len; i++) {
|
||||
strcat(tmp, hex(in[i]));
|
||||
}
|
||||
printf("%s\n", tmp);
|
||||
}
|
||||
|
||||
static void printlen(void *src, size_t len){
|
||||
uint8_t *p = src;
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
//fprintf(stderr, "printlen: %u %s %c\n", i, hex(p[i]), p[i]);
|
||||
putchar(p[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void dump(char const * const data, dump_t dir) {
|
||||
dump_raw((uint8_t const * const)data, strlen(data), dir);
|
||||
}
|
||||
|
||||
void dump_raw(uint8_t const * const data, size_t len, dump_t dir) {
|
||||
if (!g_enabled || len == 0) {
|
||||
return;
|
||||
}
|
||||
switch (dir) {
|
||||
case DUMP_TXO:
|
||||
fprintf(stderr, "Sent:\n\t");
|
||||
break;
|
||||
case DUMP_RXI:
|
||||
fprintf(stderr, "Received:\n\t");
|
||||
break;
|
||||
case DUMP_RXI_BUFFER_UPDATE:
|
||||
fprintf(stderr, "Partial RX:\n\t");
|
||||
default:
|
||||
fprintf(stderr, "Notice: ");
|
||||
break;
|
||||
}
|
||||
for (size_t i = 0; i < len; i++){
|
||||
fputc(data[i], stderr);
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
//
|
||||
// Created by nock on 2019-06-19.
|
||||
//
|
||||
|
||||
#ifndef UMCUMGR_DUMP_H
|
||||
#define UMCUMGR_DUMP_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
typedef enum dump_types {
|
||||
DUMP_TXO,
|
||||
DUMP_RXI,
|
||||
DUMP_RXI_BUFFER_UPDATE,
|
||||
DUMP_LAST
|
||||
} dump_t;
|
||||
|
||||
void dump_enable();
|
||||
void dump_raw(uint8_t const * const, size_t, dump_t);
|
||||
void dump(char const * const, dump_t);
|
||||
|
||||
#endif //UMCUMGR_DUMP_H
|
54
src/image.c
54
src/image.c
|
@ -13,6 +13,7 @@
|
|||
|
||||
#include "base64.h"
|
||||
#include "crc.h"
|
||||
#include "dump.h"
|
||||
#include "error.h"
|
||||
#include "fixkey-msg.h"
|
||||
#include "fixkey-payload.h"
|
||||
|
@ -47,6 +48,7 @@
|
|||
static int serial_fd = -1;
|
||||
|
||||
static bool g_fast_mode = false;
|
||||
static bool g_no_fix = false;
|
||||
|
||||
void image_set_fast_mode(void) {
|
||||
g_fast_mode = true;
|
||||
|
@ -60,18 +62,6 @@ void image_help(void){
|
|||
BAIL_OUT(ERR_USAGE);
|
||||
}
|
||||
|
||||
static char *hex(unsigned char in) {
|
||||
static char out[3] = {0};
|
||||
static char lookup[] = {
|
||||
'0', '1', '2', '3', '4', '5', '6', '7',
|
||||
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
|
||||
};
|
||||
out[0] = lookup[in >> 4];
|
||||
out[1] = lookup[in & 0xf];
|
||||
out[2] = 0;
|
||||
return out;
|
||||
}
|
||||
|
||||
static void read_response(uint8_t *dest, size_t *len) {
|
||||
static uint8_t buf[BOOT_SERIAL_IN_MAX*2] = {0};
|
||||
static uint8_t *p = buf;
|
||||
|
@ -81,6 +71,7 @@ static void read_response(uint8_t *dest, size_t *len) {
|
|||
if (bytes_read < 0) {
|
||||
if (errno == EAGAIN) {
|
||||
if (timer_expired()) {
|
||||
dump_raw(buf, p-buf, DUMP_RXI);
|
||||
BAIL_OUT(ERR_TIMEOUT);
|
||||
}
|
||||
usleep(5000);
|
||||
|
@ -88,6 +79,7 @@ static void read_response(uint8_t *dest, size_t *len) {
|
|||
}
|
||||
BAIL_OUT(ERR_SERIAL_PORT);
|
||||
}
|
||||
dump_raw(p, bytes_read, DUMP_RXI_BUFFER_UPDATE);
|
||||
p += bytes_read;
|
||||
uint8_t *nl_p = memchr(buf, '\n', p-buf);
|
||||
if (nl_p) {
|
||||
|
@ -105,22 +97,7 @@ static void read_response(uint8_t *dest, size_t *len) {
|
|||
}
|
||||
}
|
||||
|
||||
static void hex_print(char *in, size_t len) {
|
||||
char tmp[len*2+1];
|
||||
tmp[len] = 0;
|
||||
for (int i = 0; i < len; i++) {
|
||||
strcat(tmp, hex(in[i]));
|
||||
}
|
||||
printf("%s\n", tmp);
|
||||
}
|
||||
|
||||
static void printlen(void *src, size_t len){
|
||||
uint8_t *p = src;
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
//fprintf(stderr, "printlen: %u %s %c\n", i, hex(p[i]), p[i]);
|
||||
putchar(p[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static bool assert_pkt_start(uint8_t const *buf){
|
||||
return (buf[0] == SHELL_NLIP_PKT_START1 && buf[1] == SHELL_NLIP_PKT_START2);
|
||||
|
@ -144,6 +121,7 @@ static inline void _cbor_ensure_success(CborError err, const char *function, con
|
|||
static char *get_version(void) {
|
||||
serial_flush(serial_fd);
|
||||
int rc = write(serial_fd, IMAGE_LIST_CMD, sizeof(IMAGE_LIST_CMD) - 1);
|
||||
dump_raw((uint8_t *)IMAGE_LIST_CMD, sizeof(IMAGE_LIST_CMD)-1, DUMP_TXO);
|
||||
if (rc < sizeof(IMAGE_LIST_CMD) - 1) {
|
||||
BAIL_OUT(ERR_SERIAL_PORT);
|
||||
}
|
||||
|
@ -151,13 +129,18 @@ static char *get_version(void) {
|
|||
size_t in_len;
|
||||
|
||||
read_response(tmp, &in_len);
|
||||
dump_raw(tmp, in_len, DUMP_RXI);
|
||||
|
||||
// Process the response
|
||||
CborParser parser;
|
||||
CborValue it;
|
||||
|
||||
// Validate packet start marker
|
||||
if (in_len < 2 || !assert_pkt_start(tmp)) {
|
||||
if (in_len < 2) {
|
||||
BAIL_OUT(ERR_INVALID_PACKET);
|
||||
}
|
||||
|
||||
if (!assert_pkt_start(tmp)) {
|
||||
BAIL_OUT(ERR_INVALID_PACKET);
|
||||
}
|
||||
|
||||
|
@ -245,11 +228,13 @@ static void wrap_and_send_pkt(uint8_t *data, size_t len, bool fast_mode) {
|
|||
|
||||
if (pos == out_buf) {
|
||||
write(serial_fd, start_pkt, sizeof(start_pkt));
|
||||
dump_raw(start_pkt, sizeof(start_pkt), DUMP_TXO);
|
||||
} else {
|
||||
if (!fast_mode) {
|
||||
usleep(20000);
|
||||
}
|
||||
write(serial_fd, start_data, sizeof(start_data));
|
||||
dump_raw(start_data, sizeof(start_data), DUMP_TXO);
|
||||
}
|
||||
|
||||
size_t remaining = out_buf + out_len - pos;
|
||||
|
@ -261,7 +246,9 @@ static void wrap_and_send_pkt(uint8_t *data, size_t len, bool fast_mode) {
|
|||
}
|
||||
|
||||
write(serial_fd, pos, to_write);
|
||||
dump_raw(pos, to_write, DUMP_TXO);
|
||||
write(serial_fd, "\n", 1);
|
||||
dump_raw("\n", 1, DUMP_TXO);
|
||||
pos += to_write;
|
||||
}
|
||||
free(out_buf);
|
||||
|
@ -433,6 +420,10 @@ void fix_and_upload(char *filename) {
|
|||
do_upload(filename, image_get_fast_mode() || has_new_bootloader); // If we just updated the BL, it supports fast
|
||||
}
|
||||
|
||||
void image_set_no_fix(void) {
|
||||
g_no_fix = true;
|
||||
}
|
||||
|
||||
void image_main(int fd, int argc, char **argv){
|
||||
gpio_ble_req_bootloader();
|
||||
serial_fd = fd;
|
||||
|
@ -453,7 +444,12 @@ void image_main(int fd, int argc, char **argv){
|
|||
if (argc < 2) {
|
||||
fprintf(stderr, "umcumgr image upload <binary file>\n");
|
||||
}
|
||||
fix_and_upload(argv[1]);
|
||||
if (g_no_fix) {
|
||||
printf("Writing %s into slot 1\n", argv[1]);
|
||||
do_upload(argv[1], image_get_fast_mode());
|
||||
} else {
|
||||
fix_and_upload(argv[1]);
|
||||
}
|
||||
} else {
|
||||
image_help();
|
||||
}
|
||||
|
|
|
@ -11,5 +11,6 @@
|
|||
|
||||
void image_main(int, int, char **);
|
||||
void image_set_fast_mode(void);
|
||||
void image_set_no_fix(void);
|
||||
|
||||
#endif //UMCUMGR_IMAGE_H
|
||||
|
|
10
src/main.c
10
src/main.c
|
@ -6,6 +6,7 @@
|
|||
#include <termios.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "dump.h"
|
||||
#include "image.h"
|
||||
#include "serial_util.h"
|
||||
|
||||
|
@ -15,8 +16,7 @@ int main(int argc, char **argv) {
|
|||
int c;
|
||||
char *speed = NULL;
|
||||
char *serial_port = DEFAULT_SERIAL_PORT;
|
||||
bool fast_mode = false;
|
||||
while ((c = getopt(argc, argv, "c:s:f")) != -1) {
|
||||
while ((c = getopt(argc, argv, "c:s:fnd")) != -1) {
|
||||
switch (c) {
|
||||
case 'c':
|
||||
serial_port = optarg;
|
||||
|
@ -26,6 +26,12 @@ int main(int argc, char **argv) {
|
|||
case 'f':
|
||||
image_set_fast_mode();
|
||||
break;
|
||||
case 'n':
|
||||
image_set_no_fix();
|
||||
break;
|
||||
case 'd':
|
||||
dump_enable();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue