From 3c2df0a84a32294d35ae18cdb5e6140b2ac63b56 Mon Sep 17 00:00:00 2001 From: Serge Date: Sat, 15 Sep 2018 16:43:55 -0700 Subject: [PATCH] Read RD-5R image. --- dfu-libusb.c | 2 ++ hid-libusb.c | 95 ++++++++++++++++++++++++++++++++++++++++------------ radio.c | 1 + rd5r.c | 8 ++--- util.h | 2 ++ 5 files changed, 82 insertions(+), 26 deletions(-) diff --git a/dfu-libusb.c b/dfu-libusb.c index 81ed570..5281a3f 100644 --- a/dfu-libusb.c +++ b/dfu-libusb.c @@ -430,6 +430,8 @@ void dfu_reboot() { unsigned char cmd[2] = { 0x91, 0x05 }; + if (! ctx) + return; if (trace_flag) { printf("--- Send DNLOAD [2] "); print_hex(cmd, 2); diff --git a/hid-libusb.c b/hid-libusb.c index 6e12448..5d61c2b 100644 --- a/hid-libusb.c +++ b/hid-libusb.c @@ -32,10 +32,19 @@ #include #include "util.h" +static const unsigned char CMD_PRG[] = "\2PROGRA"; +static const unsigned char CMD_PRG2[] = "M\2"; +static const unsigned char CMD_ACK[] = "A"; +static const unsigned char CMD_READ[] = "Raan"; +static const unsigned char CMD_ENDR[] = "ENDR"; +static const unsigned char CMD_CWB0[] = "CWB\4\0\0\0\0"; +static const unsigned char CMD_CWB1[] = "CWB\4\0\1\0\0"; + static libusb_context *ctx = NULL; // libusb context static libusb_device_handle *dev; // libusb device static unsigned char receive_buf[42]; // receive buffer static volatile int nbytes_received = 0; // receive result +static unsigned offset = 0; // CWD offset #define HID_INTERFACE 0 // interface index #define TIMEOUT_MSEC 500 // receive timeout @@ -226,30 +235,28 @@ const char *hid_init(unsigned vid, unsigned pid) exit(-1); } - static const unsigned char CMD_PRG[] = "\2PROGRA"; - static const unsigned char CMD_PRG2[] = "M\2"; - static const unsigned char CMD_ACK = 0x41; static unsigned char reply[38]; + unsigned char ack; - hid_send_recv(CMD_PRG, 7, reply, 1); - if (reply[0] != CMD_ACK) { - fprintf(stderr, "Wrong reply %#x, expected %#x\n", reply[0], CMD_ACK); + hid_send_recv(CMD_PRG, 7, &ack, 1); + if (ack != CMD_ACK[0]) { + fprintf(stderr, "%s: Wrong PRD acknowledge %#x, expected %#x\n", + __func__, ack, CMD_ACK[0]); return 0; } hid_send_recv(CMD_PRG2, 2, reply, 16); -// ---Send 01 00 07 00 02 50 52 4f 47 52 41 -// ---Recv 03 00 01 00 41 00 00 00 00 00 00 00 00 00 00 00 -// 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 -// 00 00 00 00 00 00 00 00 00 00 -// ---Send 01 00 02 00 4d 02 -// ---Recv 03 00 10 00 42 46 2d 35 52 ff ff ff 56 32 31 30 -// 00 04 80 04 00 00 00 00 00 00 00 00 00 00 00 00 -// 00 00 00 00 00 00 00 00 00 00 + hid_send_recv(CMD_ACK, 1, &ack, 1); + if (ack != CMD_ACK[0]) { + fprintf(stderr, "%s: Wrong PRG2 acknowledge %#x, expected %#x\n", + __func__, ack, CMD_ACK[0]); + return 0; + } -// 42 46 2d 35 52 ff ff ff 56 32 31 30 00 04 80 04 -// B F - 5 R V 2 1 0 + // Reply: + // 42 46 2d 35 52 ff ff ff 56 32 31 30 00 04 80 04 + // B F - 5 R V 2 1 0 // Terminate the string. char *p = memchr(reply, 0xff, sizeof(reply)); @@ -260,10 +267,56 @@ const char *hid_init(unsigned vid, unsigned pid) void hid_close() { - if (ctx) { - libusb_release_interface(dev, HID_INTERFACE); - libusb_close(dev); - libusb_exit(ctx); - ctx = 0; + if (!ctx) + return; + + libusb_release_interface(dev, HID_INTERFACE); + libusb_close(dev); + libusb_exit(ctx); + ctx = 0; +} + +void hid_read_block(int bno, uint8_t *data, int nbytes) +{ + unsigned addr = bno * nbytes; + unsigned char ack, cmd[4], reply[32+4]; + int n; + + if (addr < 0x10000 && offset != 0) { + offset = 0; + hid_send_recv(CMD_CWB0, 8, &ack, 1); + if (ack != CMD_ACK[0]) { + fprintf(stderr, "%s: Wrong acknowledge %#x, expected %#x\n", + __func__, ack, CMD_ACK[0]); + exit(-1); + } + } else if (addr >= 0x10000 && offset == 0) { + offset = 0x00010000; + hid_send_recv(CMD_CWB1, 8, &ack, 1); + if (ack != CMD_ACK[0]) { + fprintf(stderr, "%s: Wrong acknowledge %#x, expected %#x\n", + __func__, ack, CMD_ACK[0]); + exit(-1); + } + } + + for (n=0; n> 8; + cmd[2] = addr + n; + cmd[3] = 32; + hid_send_recv(cmd, 4, reply, sizeof(reply)); + memcpy(data + n, reply + 4, 32); + } +} + +void hid_read_finish() +{ + unsigned char ack; + + hid_send_recv(CMD_ENDR, 4, &ack, 1); + if (ack != CMD_ACK[0]) { + fprintf(stderr, "%s: Wrong acknowledge %#x, expected %#x\n", + __func__, ack, CMD_ACK[0]); } } diff --git a/radio.c b/radio.c index 8669b95..dddfcc4 100644 --- a/radio.c +++ b/radio.c @@ -66,6 +66,7 @@ void radio_disconnect() // Restore the normal radio mode. dfu_reboot(); dfu_close(); + hid_close(); } // diff --git a/rd5r.c b/rd5r.c index 9acd9c7..bd6c17b 100644 --- a/rd5r.c +++ b/rd5r.c @@ -365,20 +365,18 @@ static void rd5r_print_version(radio_device_t *radio, FILE *out) // static void rd5r_download(radio_device_t *radio) { - //TODO -#if 0 int bno; for (bno=0; bno