Skip to content

Commit

Permalink
cli: 打印更友好的 errno
Browse files Browse the repository at this point in the history
  • Loading branch information
xychen committed Jan 8, 2024
1 parent c95666a commit ac87aee
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 27 deletions.
34 changes: 19 additions & 15 deletions cskburn/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stdlib.h>
#include <string.h>

#include "errid.h"
#include "log.h"
#include "msleep.h"
#include "read_parts.h"
Expand Down Expand Up @@ -489,7 +490,8 @@ main(int argc, char **argv)
options.burner_buf = (uint8_t *)malloc(MAX_IMAGE_SIZE);
options.burner_len = read_file(options.burner, options.burner_buf, MAX_IMAGE_SIZE);
if (options.burner_len == 0) {
LOGE("ERROR: Failed reading %s: %s", options.burner, strerror(errno));
LOGE("ERROR: Failed reading %s: %s (%s)", options.burner, errid(errno),
strerror(errno));
return -1;
}
}
Expand Down Expand Up @@ -803,7 +805,7 @@ serial_burn(cskburn_partition_t *parts, int parts_cnt)

cskburn_serial_device_t *dev = NULL;
if ((ret = cskburn_serial_open(&dev, options.serial, options.chip)) != 0) {
LOGE("ERROR: Failed opening device: %d (%s)", ret, strerror(-ret));
LOGE("ERROR: Failed opening device: %s (%s)", errid(-ret), strerror(-ret));
goto err_open;
}

Expand All @@ -814,7 +816,7 @@ serial_burn(cskburn_partition_t *parts, int parts_cnt)
if (options.read_chip_id) {
uint8_t id[CHIP_ID_LEN] = {0};
if ((ret = cskburn_serial_read_chip_id(dev, id)) != 0) {
LOGE("ERROR: Failed reading device: %d (%s)", ret, strerror(-ret));
LOGE("ERROR: Failed reading device: %s (%s)", errid(-ret), strerror(-ret));
goto err_enter;
}

Expand All @@ -828,7 +830,7 @@ serial_burn(cskburn_partition_t *parts, int parts_cnt)
uint32_t flash_id = 0;

if ((ret = cskburn_serial_get_flash_info(dev, &flash_id, &flash_size)) != 0) {
LOGE("ERROR: Failed detecting flash type: %d (%s)", ret, strerror(-ret));
LOGE("ERROR: Failed detecting flash type: %s (%s)", errid(-ret), strerror(-ret));
goto err_enter;
}

Expand All @@ -837,7 +839,7 @@ serial_burn(cskburn_partition_t *parts, int parts_cnt)
LOGI("Detected flash size: %llu MB", flash_size >> 20);
} else if (options.target == TARGET_NAND) {
if ((ret = cskburn_serial_init_nand(dev, &nand_config, &flash_size)) != 0) {
LOGE("ERROR: Failed initializing NAND: %d (%s)", ret, strerror(-ret));
LOGE("ERROR: Failed initializing NAND: %s (%s)", errid(-ret), strerror(-ret));
goto err_enter;
}

Expand Down Expand Up @@ -918,15 +920,16 @@ serial_burn(cskburn_partition_t *parts, int parts_cnt)

writer_t *writer = filewriter_open(options.read_parts[i].path);
if (writer == NULL) {
LOGE("ERROR: Failed opening %s: %s", options.read_parts[i].path, strerror(errno));
LOGE("ERROR: Failed opening %s: %s (%s)", options.read_parts[i].path, errid(errno),
strerror(errno));
goto err_enter;
}

LOGI("Reading region 0x%08X-0x%08X...", addr, addr + size);
if ((ret = cskburn_serial_read(dev, options.target, addr, size, writer,
options.progress ? print_progress : NULL)) != 0) {
LOGE("ERROR: Failed reading region 0x%08X-0x%08X: %d (%s)", addr, addr + size, ret,
strerror(-ret));
LOGE("ERROR: Failed reading region 0x%08X-0x%08X: %s (%s)", addr, addr + size,
errid(-ret), strerror(-ret));
goto err_enter;
}

Expand All @@ -936,7 +939,7 @@ serial_burn(cskburn_partition_t *parts, int parts_cnt)
if (options.erase_all) {
LOGI("Erasing entire flash...");
if ((ret = cskburn_serial_erase_all(dev, options.target)) != 0) {
LOGE("ERROR: Failed erasing device: %d (%s)", ret, strerror(-ret));
LOGE("ERROR: Failed erasing device: %s (%s)", errid(-ret), strerror(-ret));
goto err_enter;
}
} else {
Expand All @@ -945,8 +948,8 @@ serial_burn(cskburn_partition_t *parts, int parts_cnt)
uint32_t size = options.erase_parts[i].size;
LOGI("Erasing region 0x%08X-0x%08X...", addr, addr + size);
if ((ret = cskburn_serial_erase(dev, options.target, addr, size)) != 0) {
LOGE("ERROR: Failed erasing region 0x%08X-0x%08X: %d (%s)", addr, addr + size, ret,
strerror(-ret));
LOGE("ERROR: Failed erasing region 0x%08X-0x%08X: %s (%s)", addr, addr + size,
errid(-ret), strerror(-ret));
goto err_enter;
}
}
Expand All @@ -959,8 +962,8 @@ serial_burn(cskburn_partition_t *parts, int parts_cnt)
uint32_t addr = options.verify_parts[i].addr;
uint32_t size = options.verify_parts[i].size;
if ((ret = cskburn_serial_verify(dev, options.target, addr, size, md5)) != 0) {
LOGE("ERROR: Failed verifing region 0x%08X-0x%08X: %d (%s)", addr, addr + size, ret,
strerror(-ret));
LOGE("ERROR: Failed verifing region 0x%08X-0x%08X: %s (%s)", addr, addr + size,
errid(-ret), strerror(-ret));
goto err_enter;
}
md5_to_str(md5_str, md5);
Expand All @@ -977,7 +980,7 @@ serial_burn(cskburn_partition_t *parts, int parts_cnt)
(float)parts[i].reader->size / 1024.0f);
if ((ret = cskburn_serial_write(dev, options.target, parts[i].addr, parts[i].reader,
jump_addr, options.progress ? print_progress : NULL)) != 0) {
LOGE("ERROR: Failed burning partition %d: %d (%s)", i + 1, ret, strerror(-ret));
LOGE("ERROR: Failed burning partition %d: %s (%s)", i + 1, errid(-ret), strerror(-ret));
goto err_write;
}

Expand All @@ -991,7 +994,8 @@ serial_burn(cskburn_partition_t *parts, int parts_cnt)
}
if ((ret = cskburn_serial_verify(dev, options.target, parts[i].addr,
parts[i].reader->size, flash_md5)) != 0) {
LOGE("ERROR: Failed verifing partition %d: %d (%s)", i + 1, ret, strerror(-ret));
LOGE("ERROR: Failed verifing partition %d: %s (%s)", i + 1, errid(-ret),
strerror(-ret));
goto err_write;
}
md5_to_str(md5_str, flash_md5);
Expand Down
4 changes: 3 additions & 1 deletion cskburn/src/read_parts_bin.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdlib.h>
#include <string.h>

#include "errid.h"
#include "fsio.h"
#include "log.h"
#include "read_parts.h"
Expand All @@ -26,7 +27,8 @@ read_parts_bin(

reader_t *reader = filereader_open(parts[cnt].path);
if (reader == NULL) {
LOGE("ERROR: Failed reading %s: %s", parts[cnt].path, strerror(errno));
LOGE("ERROR: Failed reading %s: %s (%s)", parts[cnt].path, errid(errno),
strerror(errno));
return false;
}

Expand Down
3 changes: 2 additions & 1 deletion cskburn/src/read_parts_hex.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdlib.h>
#include <string.h>

#include "errid.h"
#include "intelhex/intelhex.h"
#include "log.h"
#include "memio.h"
Expand Down Expand Up @@ -35,7 +36,7 @@ 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("ERROR: Failed reading %s: %s", argv[i], strerror(errno));
LOGE("ERROR: Failed reading %s: %s (%s)", argv[i], errid(errno), strerror(errno));
goto exit;
} else {
LOGD("Parsing HEX file: %s, size: %d", argv[i], hex_len);
Expand Down
7 changes: 4 additions & 3 deletions libcskburn_serial/src/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string.h>

#include "core.h"
#include "errid.h"
#include "log.h"
#include "msleep.h"
#include "serial.h"
Expand Down Expand Up @@ -169,13 +170,13 @@ command(cskburn_serial_device_t *dev, uint8_t op, uint16_t in_len, uint32_t in_c

uint32_t req_len = sizeof(csk_command_t) + in_len;
if ((ret = command_send(dev, op, dev->req_buf, req_len, timeout)) < 0) {
LOGD("DEBUG: Failed to write command %02X: %d (%s)", op, ret, strerror(-ret));
LOGD("DEBUG: Failed to write command %02X: %s (%s)", op, errid(-ret), strerror(-ret));
goto exit;
}

uint8_t *res_ptr;
if ((ret = command_recv(dev, op, &res_ptr, timeout)) < 0) {
LOGD("DEBUG: Failed to read command %02X: %d (%s)", op, ret, strerror(-ret));
LOGD("DEBUG: Failed to read command %02X: %s (%s)", op, errid(-ret), strerror(-ret));
goto exit;
}

Expand Down Expand Up @@ -629,7 +630,7 @@ cmd_change_baud(cskburn_serial_device_t *dev, uint32_t baud, uint32_t old_baud)

ret = serial_set_speed(dev->serial, baud);
if (ret != 0) {
LOGD("DEBUG: Failed to set baudrate: %d (%s)", ret, strerror(-ret));
LOGD("DEBUG: Failed to set baudrate: %s (%s)", errid(-ret), strerror(-ret));
return ret;
}

Expand Down
12 changes: 7 additions & 5 deletions libcskburn_serial/src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "cmd.h"
#include "cskburn_serial.h"
#include "errid.h"
#include "log.h"
#include "msleep.h"
#include "serial.h"
Expand Down Expand Up @@ -144,12 +145,12 @@ cskburn_serial_enter(
// of it to speed up the process.
if (dev->chip == 6 && baud_rate != BAUD_RATE_INIT) {
if ((ret = cmd_change_baud(dev, baud_rate, BAUD_RATE_INIT)) != 0) {
LOGE("ERROR: Failed changing baud rate: %d (%s)", ret, strerror(-ret));
LOGE("ERROR: Failed changing baud rate: %s (%s)", errid(-ret), strerror(-ret));
return ret;
}

if ((ret = try_sync(dev, 2000)) != 0) {
LOGE("ERROR: Device not synced after baud rate change: %d (%s)", ret,
LOGE("ERROR: Device not synced after baud rate change: %s (%s)", errid(-ret),
strerror(-ret));
return ret;
}
Expand Down Expand Up @@ -188,19 +189,20 @@ cskburn_serial_enter(
}

if ((ret = try_sync(dev, 2000)) != 0) {
LOGE("ERROR: Device not recognized: %d (%s)", ret, strerror(-ret));
LOGE("ERROR: Device not recognized: %s (%s)", errid(-ret), strerror(-ret));
return ret;
}

if ((ret = cmd_change_baud(dev, baud_rate, BAUD_RATE_INIT)) != 0) {
LOGE("ERROR: Failed changing baud rate: %d (%s)", ret, strerror(-ret));
LOGE("ERROR: Failed changing baud rate: %s (%s)", errid(-ret), strerror(-ret));
return ret;
}

msleep(200);

if ((ret = try_sync(dev, 2000)) != 0) {
LOGE("ERROR: Device not synced after baud rate change: %d (%s)", ret, strerror(-ret));
LOGE("ERROR: Device not synced after baud rate change: %s (%s)", errid(-ret),
strerror(-ret));
return ret;
}

Expand Down
1 change: 1 addition & 0 deletions liblog/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ project(log)
add_library(
${PROJECT_NAME} STATIC
src/log.c
src/errid.c
)

target_include_directories(
Expand Down
6 changes: 6 additions & 0 deletions liblog/include/errid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef __LIB_CSKBURN_ERRID__
#define __LIB_CSKBURN_ERRID__

const char *errid(int errnum);

#endif // __LIB_CSKBURN_ERRID__
36 changes: 36 additions & 0 deletions liblog/src/errid.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <errid.h>
#include <errno.h>
#include <stdlib.h>

#define ERRID(name) [name] = #name

static const char *errids[] = {
ERRID(EPERM),
ERRID(ENOENT),
ERRID(EIO),
ERRID(ENXIO),
ERRID(EBADF),
ERRID(ENOMEM),
ERRID(EACCES),
ERRID(EFAULT),
ERRID(EBUSY),
ERRID(EEXIST),
ERRID(ENODEV),
ERRID(EINVAL),
ERRID(EAGAIN),
ERRID(ENOTSUP),
ERRID(ETIMEDOUT),
ERRID(ENOSYS),
ERRID(EBADMSG),
};

#define ERRID_MAX (sizeof(errids) / sizeof(errids[0]))

const char *
errid(int errnum)
{
if (errnum < 0 || errnum >= ERRID_MAX) return "UNKNOWN";
const char *errid = errids[errnum];
if (errid == NULL) return "UNKNOWN";
return errid;
}
5 changes: 3 additions & 2 deletions libslip/src/slip.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stdlib.h>
#include <string.h>

#include "errid.h"
#include "log.h"
#include "serial.h"
#include "time_monotonic.h"
Expand Down Expand Up @@ -77,7 +78,7 @@ slip_read(slip_dev_t *dev, uint8_t *buf, size_t count, uint64_t timeout)
continue;
}
} else if (r < 0) {
LOGD("DEBUG: Failed reading command: %zd (%s)", r, strerror(-r));
LOGD("DEBUG: Failed reading command: %s (%s)", errid(-r), strerror(-r));
return r;
}

Expand Down Expand Up @@ -169,7 +170,7 @@ slip_write(slip_dev_t *dev, const uint8_t *buf, size_t count, uint64_t timeout)
continue;
}
} else if (r < 0) {
LOGD("DEBUG: Failed writing command: %zd (%s)", r, strerror(-r));
LOGD("DEBUG: Failed writing command: %s (%s)", errid(-r), strerror(-r));
return r;
}

Expand Down

0 comments on commit ac87aee

Please sign in to comment.