diff --git a/src/main.c b/src/main.c index 5218b05..4a538be 100644 --- a/src/main.c +++ b/src/main.c @@ -49,19 +49,19 @@ options[] = { static __bfdev_noreturn void usage(void) { - printf("Usage: w80xprog [options]...\n"); - printf("\t-h, --help display this message\n"); - printf("\t-p, --port set device path\n"); - printf("\t-s, --speed set link baudrate\n"); - printf("\t-n, --nspeed set new baudrate\n"); - printf("\t-o, --secboot entry secboot mode\n"); - printf("\t-i, --info read the chip info\n"); - printf("\t-f, --flash flash chip with data from filename\n"); - printf("\t-e, --erase erase the entire chip\n"); - printf("\t-b, --bt set bluetooth mac address\n"); - printf("\t-w, --wifi set wifi mac address\n"); - printf("\t-g, --gain set power amplifier gain\n"); - printf("\t-r, --reset reset chip after operate\n"); + bfdev_log_err("Usage: w80xprog [options]...\n"); + bfdev_log_err("\t-h, --help display this message\n"); + bfdev_log_err("\t-p, --port set device path\n"); + bfdev_log_err("\t-s, --speed set link baudrate\n"); + bfdev_log_err("\t-n, --nspeed set new baudrate\n"); + bfdev_log_err("\t-o, --secboot entry secboot mode\n"); + bfdev_log_err("\t-i, --info read the chip info\n"); + bfdev_log_err("\t-f, --flash flash chip with data from filename\n"); + bfdev_log_err("\t-e, --erase erase the specific flash\n"); + bfdev_log_err("\t-b, --bt set bluetooth mac address\n"); + bfdev_log_err("\t-w, --wifi set wifi mac address\n"); + bfdev_log_err("\t-g, --gain set power amplifier gain\n"); + bfdev_log_err("\t-r, --reset reset chip after operate\n"); exit(1); } @@ -95,9 +95,10 @@ int main(int argc, char *const argv[]) flags = 0; esize = 0; - printf("w80xprog v" __bfdev_stringify(PROJECT_VERSION) "\n"); - printf("Copyright(c) 2021-2024 John Sanpe \n"); - printf("License GPLv2+: GNU GPL version 2 or later.\n\n"); + bfdev_log_clr_level(&bfdev_log_default); + bfdev_log_notice("w80xprog v" __bfdev_stringify(PROJECT_VERSION) "\n"); + bfdev_log_notice("Copyright(c) 2021-2024 John Sanpe \n"); + bfdev_log_notice("License GPLv2+: GNU GPL version 2 or later.\n\n"); for (;;) { arg = getopt_long(argc, argv, "p:ois:n:f:e:b:w:g:rh", options, &optidx); @@ -168,24 +169,24 @@ int main(int argc, char *const argv[]) retval = term_open(port); if (retval) { bfdev_errname(retval, &errname); - printf("Failed to open port: %s\n", errname); + bfdev_log_err("Failed to open port: %s\n", errname); return retval; } retval = term_setup(speed, 8, 1, 'N'); if (retval) { bfdev_errname(retval, &errname); - printf("Failed to setup port: %s\n", errname); + bfdev_log_err("Failed to setup port: %s\n", errname); return retval; } - term_rts(false); + term_reset(false); if (flags & FLAG_SECBOOT) { retval = entry_secboot(); if (retval) { bfdev_errname(retval, &errname); - printf("Failed to entry secboot: %s\n", errname); + bfdev_log_err("Failed to entry secboot: %s\n", errname); return retval; } } @@ -194,14 +195,14 @@ int main(int argc, char *const argv[]) retval = serial_speed(nspeed); if (retval) { bfdev_errname(retval, &errname); - printf("Failed to set chip speed: %s\n", errname); + bfdev_log_err("Failed to set chip speed: %s\n", errname); return retval; } retval = term_setspeed(nspeed); if (retval) { bfdev_errname(retval, &errname); - printf("Failed to set host speed: %s\n", errname); + bfdev_log_err("Failed to set host speed: %s\n", errname); return retval; } } @@ -210,7 +211,7 @@ int main(int argc, char *const argv[]) retval = chip_info(); if (retval) { bfdev_errname(retval, &errname); - printf("Failed to read info: %s\n", errname); + bfdev_log_err("Failed to read info: %s\n", errname); return retval; } } @@ -219,7 +220,7 @@ int main(int argc, char *const argv[]) retval = spinor_erase(eidx, esize); if (retval) { bfdev_errname(retval, &errname); - printf("Failed to erase chip: %s\n", errname); + bfdev_log_err("Failed to erase chip: %s\n", errname); return retval; } } @@ -228,7 +229,7 @@ int main(int argc, char *const argv[]) retval = flash_bmac(bmac); if (retval) { bfdev_errname(retval, &errname); - printf("Failed to flash bt mac: %s\n", errname); + bfdev_log_err("Failed to flash bt mac: %s\n", errname); return retval; } } @@ -237,7 +238,7 @@ int main(int argc, char *const argv[]) retval = flash_wmac(wmac); if (retval) { bfdev_errname(retval, &errname); - printf("Failed to flash wifi mac: %s\n", errname); + bfdev_log_err("Failed to flash wifi mac: %s\n", errname); return retval; } } @@ -246,7 +247,7 @@ int main(int argc, char *const argv[]) retval = flash_gain(gain); if (retval) { bfdev_errname(retval, &errname); - printf("Failed to flash rf gain: %s\n", errname); + bfdev_log_err("Failed to flash rf gain: %s\n", errname); return retval; } } @@ -258,26 +259,26 @@ int main(int argc, char *const argv[]) fd = open(file, O_RDONLY); if (fd < 0) { - printf("Failed to open file\n"); + bfdev_log_err("Failed to open file\n"); return fd; } retval = fstat(fd, &stat); if (retval) { - printf("Failed to fstat file\n"); + bfdev_log_err("Failed to fstat file\n"); return retval; } map = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0); if (map == MAP_FAILED) { - printf("Failed to mmap file\n"); + bfdev_log_err("Failed to mmap file\n"); return -BFDEV_ENOMEM; } retval = spinor_flash(map, stat.st_size); if (retval) { bfdev_errname(retval, &errname); - printf("Failed to flash chip: %s\n", errname); + bfdev_log_err("Failed to flash chip: %s\n", errname); return retval; } @@ -289,7 +290,7 @@ int main(int argc, char *const argv[]) retval = chip_reset(); if (retval) { bfdev_errname(retval, &errname); - printf("Failed to reset chip: %s\n", errname); + bfdev_log_err("Failed to reset chip: %s\n", errname); return retval; } } diff --git a/src/term.c b/src/term.c index ca3b0f9..f23da8a 100644 --- a/src/term.c +++ b/src/term.c @@ -107,7 +107,7 @@ term_setup(unsigned int speed, int databits, int stopbits, char parity) } int -term_rts(bool enable) +term_reset(bool enable) { unsigned int state; int retval; diff --git a/src/term.h b/src/term.h index c24c57a..71c4838 100644 --- a/src/term.h +++ b/src/term.h @@ -17,7 +17,7 @@ extern int term_setup(unsigned int speed, int databits, int stopbits, char parity); extern int -term_rts(bool enable); +term_reset(bool enable); extern int term_read(void *data, size_t len); diff --git a/src/w80xprog.c b/src/w80xprog.c index fafbca0..0259b7f 100644 --- a/src/w80xprog.c +++ b/src/w80xprog.c @@ -266,7 +266,7 @@ xmodem_transfer(uint8_t *src, unsigned int size) retry: if (bfdev_unlikely(!retry--)) { - printf("\tAbort Transfer after twenty retries\n"); + bfdev_log_err("\tAbort Transfer after twenty retries\n"); retval = -BFDEV_ETIMEDOUT; goto abort; } @@ -284,15 +284,15 @@ xmodem_transfer(uint8_t *src, unsigned int size) if (bfdev_unlikely(value != XMODEM_ACK)) { if (value == XMODEM_NAK) { - printf("\tTransfer Retry\n"); + bfdev_log_err("\tTransfer Retry\n"); goto retry; } if (value == XMODEM_CAN) { - printf("\tTransfer Cancelled\n"); + bfdev_log_err("\tTransfer Cancelled\n"); retval = -BFDEV_ECANCELED; } else { - printf("\tUnknow Retval %#04x\n", value); + bfdev_log_err("\tUnknow Retval %#04x\n", value); retval = -BFDEV_EREMOTEIO; } @@ -330,7 +330,7 @@ spinor_flash(uint8_t *src, size_t size) { int retval; - printf("Chip Flash:\n"); + bfdev_log_info("Chip Flash:\n"); retval = xmodem_transfer(src, size); if (retval) return retval; @@ -345,7 +345,7 @@ spinor_erase(uint16_t index, uint16_t size) uint8_t state; int retval; - printf("Chip Erase:\n"); + bfdev_log_info("Chip Erase:\n"); param.index = bfdev_cpu_to_le16(index & 0x7fff); param.count = bfdev_cpu_to_le16(BFDEV_DIV_ROUND_UP(size, 4096)); @@ -353,7 +353,7 @@ spinor_erase(uint16_t index, uint16_t size) if (retval) return retval; - printf("\t[%#04x]: %s\n", state, status_info(state)); + bfdev_log_info("\t[%#04x]: %s\n", state, status_info(state)); if (state != RETURN_NOMAL) return -BFDEV_ECONNABORTED; @@ -367,15 +367,15 @@ serial_speed(uint32_t speed) uint8_t state; int retval; - printf("Setting speed:\n"); + bfdev_log_info("Setting speed:\n"); param.speed = bfdev_cpu_to_le32(speed), retval = opcode_transfer(OPCODE_SET_FREQ, ¶m, &state, 1); if (retval) return retval; - printf("\t[%#04x]: %s\n", state, state == 0x06 ? "OK" : "Failed"); - if (state != 0x06) + bfdev_log_info("\t[%#04x]: %s\n", state, state == 6 ? "OK" : "Failed"); + if (state != 6) return -BFDEV_EBUSY; return -BFDEV_ENOERR; @@ -388,9 +388,9 @@ flash_bmac(const char *mac) uint8_t state; int retval; - printf("Flash BT MAC:\n"); + bfdev_log_info("Flash BT MAC:\n"); if (atoh(mac, ¶m.index[0], 6)) { - printf("\tIncorrect format\n"); + bfdev_log_err("\tIncorrect format\n"); return -BFDEV_EINVAL; } @@ -398,7 +398,7 @@ flash_bmac(const char *mac) if (retval) return retval; - printf("\t[%#04x]: %s\n", state, status_info(state)); + bfdev_log_info("\t[%#04x]: %s\n", state, status_info(state)); if (state != RETURN_NOMAL) return -BFDEV_ECONNABORTED; @@ -412,9 +412,9 @@ flash_wmac(const char *mac) uint8_t state; int retval; - printf("Flash WIFI MAC:\n"); + bfdev_log_info("Flash WIFI MAC:\n"); if (atoh(mac, ¶m.index[0], 6)) { - printf("\tincorrect format\n"); + bfdev_log_err("\tincorrect format\n"); return -BFDEV_EINVAL; } @@ -422,7 +422,7 @@ flash_wmac(const char *mac) if (retval) return retval; - printf("\t[%#04x]: %s\n", state, status_info(state)); + bfdev_log_info("\t[%#04x]: %s\n", state, status_info(state)); if (state != RETURN_NOMAL) return -BFDEV_ECONNABORTED; @@ -430,15 +430,15 @@ flash_wmac(const char *mac) } int -flash_gain(const char *mac) +flash_gain(const char *gain) { struct gain_flash param = {}; uint8_t state; int retval; - printf("Flash RF GAIN:\n"); - if (atoh(mac, ¶m.index[0], 84)) { - printf("\tincorrect format\n"); + bfdev_log_info("Flash RF GAIN:\n"); + if (atoh(gain, ¶m.index[0], 84)) { + bfdev_log_err("\tincorrect format\n"); return -BFDEV_EINVAL; } @@ -446,7 +446,7 @@ flash_gain(const char *mac) if (retval) return retval; - printf("\t[%#04x]: %s\n", state, status_info(state)); + bfdev_log_info("\t[%#04x]: %s\n", state, status_info(state)); if (state != RETURN_NOMAL) return -BFDEV_ECONNABORTED; @@ -459,14 +459,14 @@ chip_info(void) uint8_t buff[256]; int retval; - printf("Chip information:\n"); + bfdev_log_info("Chip information:\n"); retval = opcode_transfer(OPCODE_GET_BT_MAC, NULL, buff, REPLY_MAC_LEN); if (retval) return retval; buff[REPLY_MAC_LEN] = '\0'; format_haddr(buff); - printf("\tBT MAC: %s\n", buff); + bfdev_log_info("\tBT MAC: %s\n", buff); retval = opcode_transfer(OPCODE_GET_NET_MAC, NULL, buff, REPLY_MAC_LEN); if (retval) @@ -474,28 +474,28 @@ chip_info(void) buff[REPLY_MAC_LEN] = '\0'; format_haddr(buff); - printf("\tWIFI MAC: %s\n", buff); + bfdev_log_info("\tWIFI MAC: %s\n", buff); retval = opcode_transfer(OPCODE_GET_SPINOR, NULL, buff, REPLY_FLASH_LEN); if (retval) return retval; buff[REPLY_FLASH_LEN] = '\0'; - printf("\tFlash: %s\n", buff); + bfdev_log_info("\tFlash: %s\n", buff); retval = opcode_transfer(OPCODE_GET_VERSION, NULL, buff, REPLY_ROM_LEN); if (retval) return retval; buff[REPLY_ROM_LEN] = '\0'; - printf("\tROM: %s\n", buff); + bfdev_log_info("\tROM: %s\n", buff); retval = opcode_transfer(OPCODE_GET_GAIN, NULL, buff, REPLY_GAIN_LEN); if (retval) return retval; buff[REPLY_GAIN_LEN] = '\0'; - printf("\tRF GAIN: %s\n", buff); + bfdev_log_info("\tRF GAIN: %s\n", buff); return -BFDEV_ENOERR; } @@ -505,7 +505,7 @@ chip_reset(void) { int retval; - printf("Chip reset...\n"); + bfdev_log_info("Chip reset...\n"); retval = opcode_transfer(OPCODE_REBOOT, NULL, NULL, 0); if (retval) return retval; @@ -520,18 +520,20 @@ entry_secboot(void) unsigned int count, index; int retval; - printf("Entry secboot:\n"); - term_rts(true); + bfdev_log_info("Entry secboot:\n"); + term_reset(true); usleep(5000); term_flush(); term_print("AT+Z\r\n"); - term_rts(false); + term_reset(false); buff[0] = 0x1b; buff[1] = 0x1b; buff[2] = 0x1b; + index = 0; + memset(version, 0, sizeof(version)); for (count = 0; count < SECBOOT_RETRANS; ++count) { retval = term_write(buff, 3); @@ -552,11 +554,12 @@ entry_secboot(void) } if (strncmp((void *)version, "Secboot", 7)) { - printf("\tChip error\n"); + bfdev_log_err("\tChip error\n"); return -BFDEV_EPERM; } - printf("\tVersion: %s\n", version); + bfdev_log_info("\tVersion: %s\n", version); + sleep(1); return -BFDEV_ENOERR; }