From 8df1af553461dc4e073a906c613c17f2374e8e79 Mon Sep 17 00:00:00 2001 From: xychen Date: Mon, 20 May 2024 11:11:10 +0800 Subject: [PATCH] =?UTF-8?q?cli:=20=E6=B8=85=E7=90=86=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xychen --- cskburn/src/main.c | 69 ++++++++++++++++++++---------------- cskburn/src/read_parts.h | 4 +-- cskburn/src/read_parts_bin.c | 6 ++-- cskburn/src/read_parts_hex.c | 9 ++--- 4 files changed, 49 insertions(+), 39 deletions(-) diff --git a/cskburn/src/main.c b/cskburn/src/main.c index 07bc69d..30f24da 100644 --- a/cskburn/src/main.c +++ b/cskburn/src/main.c @@ -323,7 +323,7 @@ main(int argc, char **argv) sscanf(optarg, "%d", &options.chip); if (options.chip != 6 && options.chip != 4 && options.chip != 3) { LOGE("ERROR: Only 3, 4 or 6 of chip is supported"); - return -1; + return EINVAL; } if (options.chip == 3) options.chip = 4; break; @@ -345,7 +345,7 @@ main(int argc, char **argv) if (options.read_count >= MAX_FLASH_PARTS) { LOGE("ERROR: Only up to %d partitions can be read at the same time", MAX_FLASH_PARTS); - return -1; + return EINVAL; } uint16_t index = options.read_count; @@ -354,7 +354,7 @@ main(int argc, char **argv) &options.read_parts[index].size, &options.read_parts[index].path)) { LOGE("ERROR: Argument of --read should be addr:size:path (e.g. -u " "0x00000000:102400:app.bin)"); - return -1; + return EINVAL; } options.read_count++; @@ -363,7 +363,7 @@ main(int argc, char **argv) if (options.erase_count >= MAX_ERASE_PARTS) { LOGE("ERROR: Only up to %d partitions can be erased at the same time", MAX_ERASE_PARTS); - return -1; + return EINVAL; } uint16_t index = options.erase_count; @@ -372,7 +372,7 @@ main(int argc, char **argv) &options.erase_parts[index].size)) { LOGE("ERROR: Argument of --erase should be addr:size (e.g. -u " "0x00000000:102400)"); - return -1; + return EINVAL; } options.erase_count++; @@ -384,7 +384,7 @@ main(int argc, char **argv) if (options.verify_count >= MAX_VERIFY_PARTS) { LOGE("ERROR: Only up to %d partitions can be verified at the same time", MAX_VERIFY_PARTS); - return -1; + return EINVAL; } uint16_t index = options.verify_count; @@ -393,7 +393,7 @@ main(int argc, char **argv) &options.verify_parts[index].size)) { LOGE("ERROR: Argument of --verify should be addr:size (e.g. -u " "0x00000000:102400)"); - return -1; + return EINVAL; } options.verify_count++; @@ -473,7 +473,7 @@ main(int argc, char **argv) } else if (strcmp(name, "jump") == 0) { if (!scan_int(optarg, &options.jump_address)) { LOGE("ERROR: Invalid jump address"); - return -1; + return EINVAL; } break; } else if (strcmp(name, "no-reset") == 0) { @@ -502,7 +502,7 @@ main(int argc, char **argv) options.burner_len = read_file(options.burner, options.burner_buf, MAX_IMAGE_SIZE); if (options.burner_len == 0) { LOGE_RET(-errno, "ERROR: Failed reading %s", options.burner); - return -1; + return errno; } } @@ -511,16 +511,16 @@ main(int argc, char **argv) } else { #ifdef WITHOUT_USB LOGE("ERROR: A port of serial device should be specified (e.g. -s %s)", example_serial_dev); - return -1; + return EINVAL; #else if (options.chip == 6) { LOGE("ERROR: USB burning is not supported by chip family 6"); - return -1; + return ENOTSUP; } if (options.usb != NULL && strcmp(options.usb, "-") != 0) { if (sscanf(options.usb, "%hu:%hu\n", &options.usb_bus, &options.usb_addr) != 2) { LOGE("ERROR: Argument of -u/--usb should be : (e.g. -u 020:004)"); - return -1; + return EINVAL; } } options.protocol = PROTO_USB; @@ -531,35 +531,35 @@ main(int argc, char **argv) #ifndef WITHOUT_USB if (options.protocol != PROTO_SERIAL) { LOGE("ERROR: NAND is supported only in serial burning"); - return -1; + return ENOTSUP; } #endif if (options.chip != 6) { LOGE("ERROR: NAND is only supported by chip family 6"); - return -1; + return ENOTSUP; } if (options.read_count > 0) { LOGE("ERROR: Reading is not supported on NAND yet"); - return -1; + return ENOTSUP; } if (options.erase_all || options.erase_count > 0) { LOGE("ERROR: Erasing is not supported on NAND yet"); - return -1; + return ENOTSUP; } } else if (options.target == TARGET_RAM) { #ifndef WITHOUT_USB if (options.protocol != PROTO_SERIAL) { LOGE("ERROR: RAM is supported only in serial burning"); - return -1; + return ENOTSUP; } #endif if (options.erase_all || options.erase_count > 0) { LOGE("ERROR: Erasing is not supported on RAM"); - return -1; + return ENOTSUP; } if (options.verify_all || options.verify_count > 0) { LOGE("ERROR: Verifying is not supported on RAM"); - return -1; + return ENOTSUP; } } @@ -569,7 +569,7 @@ main(int argc, char **argv) if (usb_check()) { return 0; } else { - return -1; + return ENOENT; } } #endif @@ -585,14 +585,12 @@ main(int argc, char **argv) char **parts_argv = argv + optind; int parts_argc = argc - optind; memset(parts, 0, sizeof(parts)); - if (!read_parts_bin(parts_argv, parts_argc, parts + parts_cnt, &parts_cnt, - MAX_FLASH_PARTS - parts_cnt)) { - ret = -1; + if ((ret = read_parts_bin(parts_argv, parts_argc, parts + parts_cnt, &parts_cnt, + MAX_FLASH_PARTS - parts_cnt)) != 0) { goto exit; } - if (!read_parts_hex(parts_argv, parts_argc, parts + parts_cnt, &parts_cnt, MAX_IMAGE_SIZE, - MAX_FLASH_PARTS - parts_cnt, base_addr)) { - ret = -1; + if ((ret = read_parts_hex(parts_argv, parts_argc, parts + parts_cnt, &parts_cnt, MAX_IMAGE_SIZE, + MAX_FLASH_PARTS - parts_cnt, base_addr)) != 0) { goto exit; } @@ -607,8 +605,7 @@ main(int argc, char **argv) } if (options.protocol == PROTO_SERIAL) { - if (serial_burn(parts, parts_cnt) != 0) { - ret = -1; + if ((ret = serial_burn(parts, parts_cnt)) != 0) { goto exit; } #ifndef WITHOUT_USB @@ -621,7 +618,7 @@ main(int argc, char **argv) } } else { if (!usb_burn(parts, parts_cnt)) { - ret = -1; + ret = -EIO; goto exit; } } @@ -632,7 +629,7 @@ main(int argc, char **argv) for (int i = 0; i < parts_cnt; i++) { parts[i].reader->close(&parts[i].reader); } - return ret; + return -ret; } static void @@ -861,11 +858,13 @@ serial_burn(cskburn_partition_t *parts, int parts_cnt) LOGE("ERROR: The starting boundary of read address (0x%08X) exceeds the capacity of " "flash (%llu MB)", options.read_parts[i].addr, flash_size >> 20); + ret = -EINVAL; goto err_enter; } else if (options.read_parts[i].addr + options.read_parts[i].size > flash_size) { LOGE("ERROR: The ending boundary of read address (0x%08X) exceeds the capacity of " "flash (%llu MB)", options.read_parts[i].addr + options.read_parts[i].size, flash_size >> 20); + ret = -EINVAL; goto err_enter; } } @@ -873,19 +872,23 @@ serial_burn(cskburn_partition_t *parts, int parts_cnt) for (int i = 0; i < options.erase_count; i++) { if (!is_aligned(options.erase_parts[i].addr, 4 * 1024)) { LOGE("ERROR: Erase address (0x%08X) should be 4K aligned", options.erase_parts[i].addr); + ret = -EINVAL; goto err_enter; } else if (!is_aligned(options.erase_parts[i].size, 4 * 1024)) { LOGE("ERROR: Erase size (0x%08X) should be 4K aligned", options.erase_parts[i].size); + ret = -EINVAL; goto err_enter; } else if (options.erase_parts[i].addr >= flash_size) { LOGE("ERROR: The starting boundary of erase address (0x%08X) exceeds the capacity of " "flash (%llu MB)", options.erase_parts[i].addr, flash_size >> 20); + ret = -EINVAL; goto err_enter; } else if (options.erase_parts[i].addr + options.erase_parts[i].size > flash_size) { LOGE("ERROR: The ending boundary of erase address (0x%08X) exceeds the capacity of " "flash (%llu MB)", options.erase_parts[i].addr + options.erase_parts[i].size, flash_size >> 20); + ret = -EINVAL; goto err_enter; } } @@ -895,12 +898,14 @@ serial_burn(cskburn_partition_t *parts, int parts_cnt) if (!is_aligned(parts[i].addr, 512)) { LOGE("ERROR: Address of partition %d (0x%08X) should be 512 bytes aligned", i + 1, parts[i].addr); + ret = -EINVAL; goto err_enter; } } else if (options.target == TARGET_FLASH) { if (!is_aligned(parts[i].addr, 4 * 1024)) { LOGE("ERROR: Address of partition %d (0x%08X) should be 4K aligned", i + 1, parts[i].addr); + ret = -EINVAL; goto err_enter; } } @@ -910,11 +915,13 @@ serial_burn(cskburn_partition_t *parts, int parts_cnt) LOGE("ERROR: The starting boundary of partition %d (0x%08X) exceeds the capacity " "of target (%llu MB)", i + 1, parts[i].addr, flash_size >> 20); + ret = -EINVAL; goto err_enter; } else if (parts[i].addr + parts[i].reader->size > flash_size) { LOGE("ERROR: The ending boundary of partition %d (0x%08X) exceeds the capacity of " "target (%llu MB)", i + 1, parts[i].addr + parts[i].reader->size, flash_size >> 20); + ret = -EINVAL; goto err_enter; } } @@ -931,6 +938,7 @@ serial_burn(cskburn_partition_t *parts, int parts_cnt) writer_t *writer = filewriter_open(options.read_parts[i].path); if (writer == NULL) { LOGE_RET(-errno, "ERROR: Failed opening %s", options.read_parts[i].path); + ret = -errno; goto err_enter; } @@ -996,6 +1004,7 @@ serial_burn(cskburn_partition_t *parts, int parts_cnt) char md5_str[MD5_SIZE * 2 + 1] = {0}; if (verify_finish(parts[i].reader, image_md5) != 0) { LOGE("ERROR: Failed calculating MD5"); + ret = -EIO; goto err_write; } if ((ret = cskburn_serial_verify(dev, options.target, parts[i].addr, diff --git a/cskburn/src/read_parts.h b/cskburn/src/read_parts.h index 0e253df..dac0525 100644 --- a/cskburn/src/read_parts.h +++ b/cskburn/src/read_parts.h @@ -12,10 +12,10 @@ typedef struct { reader_t *reader; } cskburn_partition_t; -bool read_parts_bin( +int read_parts_bin( char **argv, int argc, cskburn_partition_t *parts, int *parts_cnt, int parts_cnt_limit); -bool read_parts_hex(char **argv, int argc, cskburn_partition_t *parts, int *parts_cnt, +int read_parts_hex(char **argv, int argc, cskburn_partition_t *parts, int *parts_cnt, uint32_t part_size_limit, int parts_cnt_limit, uint32_t base_addr); #endif // __CSKBURN_READ_PARTS__ diff --git a/cskburn/src/read_parts_bin.c b/cskburn/src/read_parts_bin.c index 1603822..c61ae3f 100644 --- a/cskburn/src/read_parts_bin.c +++ b/cskburn/src/read_parts_bin.c @@ -7,7 +7,7 @@ #include "read_parts.h" #include "utils.h" -bool +int read_parts_bin( char **argv, int argc, cskburn_partition_t *parts, int *parts_cnt, int parts_cnt_limit) { @@ -27,7 +27,7 @@ read_parts_bin( reader_t *reader = filereader_open(parts[cnt].path); if (reader == NULL) { LOGE_RET(-errno, "ERROR: Failed reading %s", parts[cnt].path); - return false; + return -errno; } parts[cnt].reader = reader; @@ -36,5 +36,5 @@ read_parts_bin( cnt++; } *parts_cnt += cnt; - return true; + return 0; } diff --git a/cskburn/src/read_parts_hex.c b/cskburn/src/read_parts_hex.c index 99424fe..4fa1699 100644 --- a/cskburn/src/read_parts_hex.c +++ b/cskburn/src/read_parts_hex.c @@ -13,11 +13,11 @@ static int append_part(cskburn_partition_t *parts, int *part_idx, uint32_t part_size_limit, int parts_cnt_limit, const char *path, uint8_t *buf, uint32_t addr, uint32_t size); -bool +int read_parts_hex(char **argv, int argc, cskburn_partition_t *parts, int *parts_cnt, uint32_t part_size_limit, int parts_cnt_limit, uint32_t base_addr) { - bool ret = false; + int ret = 0; uint8_t *hex_buf = malloc(MAX_HEX_SIZE); uint8_t *hex_ptr; @@ -35,7 +35,8 @@ read_parts_hex(char **argv, int argc, cskburn_partition_t *parts, int *parts_cnt hex_len = read_file(argv[i], hex_ptr, MAX_HEX_SIZE); hex_parsed = 0; if (hex_len == 0) { - LOGE_RET(-errno, "ERROR: Failed reading %s", argv[i]); + ret = -errno; + LOGE_RET(ret, "ERROR: Failed reading %s", argv[i]); goto exit; } else { LOGD("Parsing HEX file: %s, size: %d", argv[i], hex_len); @@ -79,12 +80,12 @@ read_parts_hex(char **argv, int argc, cskburn_partition_t *parts, int *parts_cnt break; } else { LOGE("Failed parsing HEX file: %d", status); + ret = -EIO; goto exit; } } } } - ret = true; exit: LOGD("Parsed %d parts from HEXs", cnt);