Skip to content

Commit 322cedc

Browse files
committed
chore: enhance cross-platform compatibility and code quality
1. Fixed flash size logging by using PRIu64 format specifier 2. Implemented new align_up() utility function 3. Improved EMMC command handling consistency across platforms 4. Applied clang-format standardization across modified files 5. Updated default chip type configuration
1 parent 62d473f commit 322cedc

File tree

10 files changed

+294
-272
lines changed

10 files changed

+294
-272
lines changed

cskburn/src/main.c

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <errno.h>
22
#include <getopt.h>
3+
#include <inttypes.h>
34
#include <libgen.h>
45
#include <stdbool.h>
56
#include <stdint.h>
@@ -29,13 +30,14 @@
2930
#define DEFAULT_RESET_ATTEMPTS 4
3031
#define DEFAULT_RESET_DELAY 500
3132

32-
#define DEFAULT_CHIP CHIP_TYPE_ARCS
33+
#define DEFAULT_CHIP CHIP_TYPE_4
3334

34-
#define CHIP_TO_STR(chip) \
35-
((chip) == CHIP_TYPE_3 ? "3" : \
36-
(chip) == CHIP_TYPE_4 ? "4" : \
37-
(chip) == CHIP_TYPE_6 ? "6" : \
38-
(chip) == CHIP_TYPE_ARCS ? "arcs" : "unknown")
35+
#define CHIP_TO_STR(chip) \
36+
((chip) == CHIP_TYPE_3 ? "3" \
37+
: (chip) == CHIP_TYPE_4 ? "4" \
38+
: (chip) == CHIP_TYPE_6 ? "6" \
39+
: (chip) == CHIP_TYPE_ARCS ? "arcs" \
40+
: "unknown")
3941

4042
static struct option long_options[] = {
4143
{"help", no_argument, NULL, 'h'},
@@ -62,6 +64,7 @@ static struct option long_options[] = {
6264
{"nand-dat1", required_argument, NULL, 0},
6365
{"nand-dat2", required_argument, NULL, 0},
6466
{"nand-dat3", required_argument, NULL, 0},
67+
{"emmc", no_argument, NULL, 0},
6568
{"ram", no_argument, NULL, 'r'},
6669
{"jump", required_argument, NULL, 0},
6770
{"chip-id", no_argument, NULL, 0},
@@ -72,7 +75,6 @@ static struct option long_options[] = {
7275
{"unlock", no_argument, NULL, 0},
7376
{"verify", required_argument, NULL, 0},
7477
{"verify-all", no_argument, NULL, 0},
75-
{"emmc", no_argument, NULL, 0},
7678
{"probe-timeout", required_argument, NULL, 0},
7779
{"reset-attempts", required_argument, NULL, 0},
7880
{"reset-delay", required_argument, NULL, 0},
@@ -128,7 +130,7 @@ static struct {
128130
bool repeat;
129131
cskburn_protocol_t protocol;
130132
cskburn_action_t action;
131-
uint32_t chip;
133+
chip_type_t chip;
132134
#ifndef WITHOUT_USB
133135
char *usb;
134136
int16_t usb_bus;
@@ -265,6 +267,8 @@ print_help(const char *progname)
265267
LOGI(" verify all partitions after burning");
266268
LOGI(" -n, --nand");
267269
LOGI(" burn to NAND flash (CSK6 only)");
270+
LOGI(" --emmc");
271+
LOGI(" burn to eMMC (arcs only)");
268272
LOGI(" --probe-timeout <ms>");
269273
LOGI(" timeout for probing device (default: %d ms)", DEFAULT_PROBE_TIMEOUT);
270274
LOGI(" --reset-attempts <n>");
@@ -293,8 +297,6 @@ print_help(const char *progname)
293297
LOGI(" unlock the flash");
294298
LOGI(" --verify <addr:size>");
295299
LOGI(" verify specified flash region");
296-
LOGI(" --emmc");
297-
LOGI(" burn to emmc (arcs only)");
298300
LOGI("");
299301

300302
LOGI("Example:");
@@ -353,15 +355,15 @@ main(int argc, char **argv)
353355
options.chip = CHIP_TYPE_ARCS;
354356
} else {
355357
if (!scan_int(optarg, &options.chip)) {
356-
LOGE("ERROR: Only 3, 4 6 or arcs of chip is supported");
358+
LOGE("ERROR: Only 3, 4, 6 or arcs of chip is supported");
357359
return EINVAL;
358360
}
359361
}
360-
if (options.chip != 6 && options.chip != 4 && options.chip != 3 && options.chip != CHIP_TYPE_ARCS) {
361-
LOGE("ERROR: Only 3, 4 6 or arcs of chip is supported");
362+
if (options.chip != 6 && options.chip != 4 && options.chip != 3 &&
363+
options.chip != CHIP_TYPE_ARCS) {
364+
LOGE("ERROR: Only 3, 4, 6 or arcs of chip is supported");
362365
return EINVAL;
363366
}
364-
if (options.chip == 3) options.chip = CHIP_TYPE_4;
365367
break;
366368
case 'n':
367369
options.target = TARGET_NAND;
@@ -651,14 +653,13 @@ main(int argc, char **argv)
651653

652654
if (options.chip == CHIP_TYPE_ARCS && options.target == TARGET_FLASH) {
653655
if (options.erase_count + parts_cnt > MAX_ERASE_PARTS) {
654-
LOGE("ERROR: Only up to %d partitions can be erased at the same time",
655-
MAX_ERASE_PARTS);
656+
LOGE("ERROR: Only up to %d partitions can be erased at the same time", MAX_ERASE_PARTS);
656657
ret = EINVAL;
657658
goto exit;
658659
}
659660
for (int i = options.erase_count; i < options.erase_count + parts_cnt; i++) {
660661
options.erase_parts[i].addr = parts[i].addr;
661-
options.erase_parts[i].size = (parts[i].reader->size + 4095) & ~4095;
662+
options.erase_parts[i].size = align_up(parts[i].reader->size, 4096);
662663
}
663664
options.erase_count += parts_cnt;
664665
}
@@ -902,27 +903,27 @@ serial_burn(cskburn_partition_t *parts, int parts_cnt)
902903

903904
LOGD("flash-id: %02X%02X%02X", (flash_id) & 0xFF, (flash_id >> 8) & 0xFF,
904905
(flash_id >> 16) & 0xFF);
905-
LOGI("Detected flash size: %lu MB", flash_size >> 20);
906+
LOGI("Detected flash size: %" PRIu64 " MB", flash_size >> 20);
906907
} else if (options.target == TARGET_NAND) {
907908
if ((ret = cskburn_serial_init_nand(dev, &nand_config, &flash_size)) != 0) {
908909
LOGE_RET(ret, "ERROR: Failed initializing NAND");
909910
goto err_enter;
910911
}
911912

912-
LOGI("Detected NAND size: %lu MB", flash_size >> 20);
913+
LOGI("Detected NAND size: %" PRIu64 " MB", flash_size >> 20);
913914
} else if (options.target == TARGET_EMMC) {
914915
card_info_t card_info;
915916

916917
if ((ret = cskburn_serial_get_emmc_info(dev, &card_info)) != 0) {
917-
LOGE_RET(ret, "ERROR: Failed get emmc info");
918+
LOGE_RET(ret, "ERROR: Failed get eMMC info");
918919
goto err_enter;
919920
}
920921

921-
LOGI("Detected emmc info, sctcnts: 0x%x sctsize: 0x%x erasize: 0x%x cardtype: %u",
922-
card_info.sctcnts, card_info.sctsize, card_info.erasize, card_info.cardtype);
922+
LOGD("Detected eMMC info, sctcnts: 0x%x sctsize: 0x%x erasize: 0x%x cardtype: %u",
923+
card_info.sctcnts, card_info.sctsize, card_info.erasize, card_info.cardtype);
923924

924925
flash_size = (uint64_t)card_info.sctcnts * card_info.sctsize;
925-
LOGI("flash_size: %lu MB", flash_size >> 20);
926+
LOGI("Detected eMMC size: %" PRIu64 " MB", flash_size >> 20);
926927
}
927928

928929
if (options.unlock) {
@@ -935,13 +936,13 @@ serial_burn(cskburn_partition_t *parts, int parts_cnt)
935936
for (int i = 0; i < options.read_count; i++) {
936937
if (options.read_parts[i].addr >= flash_size) {
937938
LOGE("ERROR: The starting boundary of read address (0x%08X) exceeds the capacity of "
938-
"flash (%lu MB)",
939+
"flash (%" PRIu64 " MB)",
939940
options.read_parts[i].addr, flash_size >> 20);
940941
ret = -EINVAL;
941942
goto err_enter;
942943
} else if (options.read_parts[i].addr + options.read_parts[i].size > flash_size) {
943944
LOGE("ERROR: The ending boundary of read address (0x%08X) exceeds the capacity of "
944-
"flash (%lu MB)",
945+
"flash (%" PRIu64 " MB)",
945946
options.read_parts[i].addr + options.read_parts[i].size, flash_size >> 20);
946947
ret = -EINVAL;
947948
goto err_enter;
@@ -951,12 +952,12 @@ serial_burn(cskburn_partition_t *parts, int parts_cnt)
951952
for (int i = 0; i < options.verify_count; i++) {
952953
if (options.verify_parts[i].addr >= flash_size) {
953954
LOGE("ERROR: The starting boundary of verify address (0x%08X) exceeds the capacity of "
954-
"flash (%lu MB)",
955+
"flash (%" PRIu64 " MB)",
955956
options.verify_parts[i].addr, flash_size >> 20);
956957
goto err_enter;
957958
} else if (options.verify_parts[i].addr + options.verify_parts[i].size > flash_size) {
958959
LOGE("ERROR: The ending boundary of verify address (0x%08X) exceeds the capacity of "
959-
"flash (%lu MB)",
960+
"flash (%" PRIu64 " MB)",
960961
options.verify_parts[i].addr + options.verify_parts[i].size, flash_size >> 20);
961962
goto err_enter;
962963
}
@@ -973,13 +974,13 @@ serial_burn(cskburn_partition_t *parts, int parts_cnt)
973974
goto err_enter;
974975
} else if (options.erase_parts[i].addr >= flash_size) {
975976
LOGE("ERROR: The starting boundary of erase address (0x%08X) exceeds the capacity of "
976-
"flash (%lu MB)",
977+
"flash (%" PRIu64 " MB)",
977978
options.erase_parts[i].addr, flash_size >> 20);
978979
ret = -EINVAL;
979980
goto err_enter;
980981
} else if (options.erase_parts[i].addr + options.erase_parts[i].size > flash_size) {
981982
LOGE("ERROR: The ending boundary of erase address (0x%08X) exceeds the capacity of "
982-
"flash (%lu MB)",
983+
"flash (%" PRIu64 " MB)",
983984
options.erase_parts[i].addr + options.erase_parts[i].size, flash_size >> 20);
984985
ret = -EINVAL;
985986
goto err_enter;
@@ -1006,13 +1007,13 @@ serial_burn(cskburn_partition_t *parts, int parts_cnt)
10061007
if (options.target == TARGET_FLASH || options.target == TARGET_NAND) {
10071008
if (parts[i].addr >= flash_size) {
10081009
LOGE("ERROR: The starting boundary of partition %d (0x%08X) exceeds the capacity "
1009-
"of target (%lu MB)",
1010+
"of target (%" PRIu64 " MB)",
10101011
i + 1, parts[i].addr, flash_size >> 20);
10111012
ret = -EINVAL;
10121013
goto err_enter;
10131014
} else if (parts[i].addr + parts[i].reader->size > flash_size) {
10141015
LOGE("ERROR: The ending boundary of partition %d (0x%08X) exceeds the capacity of "
1015-
"target (%lu MB)",
1016+
"target (%" PRIu64 " MB)",
10161017
i + 1, parts[i].addr + parts[i].reader->size, flash_size >> 20);
10171018
ret = -EINVAL;
10181019
goto err_enter;

cskburn/src/utils.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,11 @@ is_aligned(uint32_t addr, uint32_t align)
9090
{
9191
return addr % align == 0;
9292
}
93+
94+
uint32_t
95+
align_up(uint32_t value, uint32_t align)
96+
{
97+
// align must be a power of 2 (such as 4, 8, 16...)
98+
if (align == 0) return value;
99+
return (value + align - 1) & ~(align - 1);
100+
}

cskburn/src/utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ bool has_extname(char *path, const char *extname);
2020

2121
bool is_aligned(uint32_t addr, uint32_t align);
2222

23+
uint32_t align_up(uint32_t value, uint32_t align);
24+
2325
#endif // __CSKBURN_UTILS__

libcskburn_serial/include/cskburn_serial.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ typedef struct {
3838

3939
#pragma pack(1)
4040
typedef struct {
41-
uint32_t sctcnts;
42-
uint32_t sctsize;
43-
uint32_t erasize;
44-
uint32_t cardtype;
41+
uint32_t sctcnts;
42+
uint32_t sctsize;
43+
uint32_t erasize;
44+
uint32_t cardtype;
4545
} card_info_t;
4646
#pragma pack()
4747

libcskburn_serial/src/cmd.c

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -570,12 +570,14 @@ cmd_flash_erase_region(cskburn_serial_device_t *dev, uint32_t address, uint32_t
570570
NULL, calc_timeout(size, TIMEOUT_FLASH_ERASE_PER_MB));
571571
}
572572

573-
int cmd_flash_lock(cskburn_serial_device_t *dev)
573+
int
574+
cmd_flash_lock(cskburn_serial_device_t *dev)
574575
{
575576
return check_command(dev, CMD_FLASH_LOCK, 0, CHECKSUM_NONE, NULL, TIMEOUT_FLASH_DATA);
576577
}
577578

578-
int cmd_flash_unlock(cskburn_serial_device_t *dev)
579+
int
580+
cmd_flash_unlock(cskburn_serial_device_t *dev)
579581
{
580582
return check_command(dev, CMD_FLASH_UNLOCK, 0, CHECKSUM_NONE, NULL, TIMEOUT_FLASH_DATA);
581583
}
@@ -671,7 +673,8 @@ cmd_change_baud(cskburn_serial_device_t *dev, uint32_t baud, uint32_t old_baud)
671673
return 0;
672674
}
673675

674-
int cmd_get_card_info(cskburn_serial_device_t *dev, card_info_t *info)
676+
int
677+
cmd_emmc_get_info(cskburn_serial_device_t *dev, card_info_t *info)
675678
{
676679
uint8_t ret_buf[STATUS_BYTES_LEN + sizeof(card_info_t)];
677680
uint16_t ret_len = 0;
@@ -698,7 +701,8 @@ int cmd_get_card_info(cskburn_serial_device_t *dev, card_info_t *info)
698701
return 0;
699702
}
700703

701-
int cmd_emmc_begin(cskburn_serial_device_t *dev, uint32_t size, uint32_t blocks, uint32_t block_size,
704+
int
705+
cmd_emmc_begin(cskburn_serial_device_t *dev, uint32_t size, uint32_t blocks, uint32_t block_size,
702706
uint32_t offset)
703707
{
704708
cmd_flash_begin_t *cmd = (cmd_flash_begin_t *)dev->req_cmd;
@@ -712,7 +716,8 @@ int cmd_emmc_begin(cskburn_serial_device_t *dev, uint32_t size, uint32_t blocks,
712716
dev, CMD_EMMC_BEGIN, sizeof(cmd_flash_begin_t), CHECKSUM_NONE, NULL, TIMEOUT_DEFAULT);
713717
}
714718

715-
int cmd_emmc_block(cskburn_serial_device_t *dev, uint8_t *data, uint32_t data_len, uint32_t seq)
719+
int
720+
cmd_emmc_block(cskburn_serial_device_t *dev, uint8_t *data, uint32_t data_len, uint32_t seq)
716721
{
717722
cmd_flash_block_t *cmd = (cmd_flash_block_t *)dev->req_cmd;
718723
memset(cmd, 0, sizeof(cmd_flash_block_t));
@@ -740,7 +745,8 @@ int cmd_emmc_block(cskburn_serial_device_t *dev, uint8_t *data, uint32_t data_le
740745
return ret;
741746
}
742747

743-
int cmd_emmc_finish(cskburn_serial_device_t *dev)
748+
int
749+
cmd_emmc_finish(cskburn_serial_device_t *dev)
744750
{
745751
cmd_flash_finish_t *cmd = (cmd_flash_finish_t *)dev->req_cmd;
746752
memset(cmd, 0, sizeof(cmd_flash_finish_t));
@@ -749,18 +755,20 @@ int cmd_emmc_finish(cskburn_serial_device_t *dev)
749755
dev, CMD_EMMC_END, sizeof(uint32_t), CHECKSUM_NONE, NULL, TIMEOUT_FLASH_END);
750756
}
751757

752-
int cmd_emmc_erase_region(cskburn_serial_device_t *dev, uint32_t address, uint32_t size)
758+
int
759+
cmd_emmc_erase_region(cskburn_serial_device_t *dev, uint32_t address, uint32_t size)
753760
{
754761
cmd_flash_erase_t *cmd = (cmd_flash_erase_t *)dev->req_cmd;
755762
memset(cmd, 0, sizeof(cmd_flash_erase_t));
756763
cmd->address = address;
757764
cmd->size = size;
758765

759-
return check_command(dev, CMD_EMMC_ERASE_DATA, sizeof(cmd_flash_erase_t), CHECKSUM_NONE,
760-
NULL, calc_timeout(size, TIMEOUT_FLASH_ERASE_PER_MB));
766+
return check_command(dev, CMD_EMMC_ERASE_DATA, sizeof(cmd_flash_erase_t), CHECKSUM_NONE, NULL,
767+
calc_timeout(size, TIMEOUT_FLASH_ERASE_PER_MB));
761768
}
762769

763-
int cmd_emmc_md5(cskburn_serial_device_t *dev, uint32_t address, uint32_t size, uint8_t *md5)
770+
int
771+
cmd_emmc_md5(cskburn_serial_device_t *dev, uint32_t address, uint32_t size, uint8_t *md5)
764772
{
765773
uint8_t ret_buf[STATUS_BYTES_LEN + 16];
766774
uint16_t ret_len = 0;
@@ -791,7 +799,8 @@ int cmd_emmc_md5(cskburn_serial_device_t *dev, uint32_t address, uint32_t size,
791799
return 0;
792800
}
793801

794-
int cmd_read_emmc(cskburn_serial_device_t *dev, uint32_t address, uint32_t size, uint8_t *data,
802+
int
803+
cmd_read_emmc(cskburn_serial_device_t *dev, uint32_t address, uint32_t size, uint8_t *data,
795804
uint32_t *data_len)
796805
{
797806
static uint8_t ret_buf[STATUS_BYTES_LEN + FLASH_READ_SIZE];
@@ -802,8 +811,8 @@ int cmd_read_emmc(cskburn_serial_device_t *dev, uint32_t address, uint32_t size,
802811
cmd->address = address;
803812
cmd->size = size;
804813

805-
int ret = command(dev, CMD_EMMC_READ_DATA, sizeof(cmd_read_flash_t), CHECKSUM_NONE, NULL, ret_buf,
806-
&ret_len, sizeof(ret_buf), TIMEOUT_FLASH_DATA);
814+
int ret = command(dev, CMD_EMMC_READ_DATA, sizeof(cmd_read_flash_t), CHECKSUM_NONE, NULL,
815+
ret_buf, &ret_len, sizeof(ret_buf), TIMEOUT_FLASH_DATA);
807816
if (ret != 0) {
808817
return ret;
809818
}

libcskburn_serial/src/cmd.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,14 @@ int cmd_read_flash(cskburn_serial_device_t *dev, uint32_t address, uint32_t size
8282

8383
int cmd_change_baud(cskburn_serial_device_t *dev, uint32_t baud, uint32_t old_baud);
8484

85-
int cmd_get_card_info(cskburn_serial_device_t *dev, card_info_t *info);
86-
int cmd_emmc_begin(cskburn_serial_device_t *dev, uint32_t size, uint32_t blocks, uint32_t block_size, uint32_t offset);
85+
int cmd_emmc_get_info(cskburn_serial_device_t *dev, card_info_t *info);
86+
int cmd_emmc_begin(cskburn_serial_device_t *dev, uint32_t size, uint32_t blocks,
87+
uint32_t block_size, uint32_t offset);
8788
int cmd_emmc_block(cskburn_serial_device_t *dev, uint8_t *data, uint32_t data_len, uint32_t seq);
8889
int cmd_emmc_finish(cskburn_serial_device_t *dev);
8990
int cmd_emmc_erase_region(cskburn_serial_device_t *dev, uint32_t address, uint32_t size);
9091
int cmd_emmc_md5(cskburn_serial_device_t *dev, uint32_t address, uint32_t size, uint8_t *md5);
91-
int cmd_read_emmc(cskburn_serial_device_t *dev, uint32_t address, uint32_t size, uint8_t *data, uint32_t *data_len);
92+
int cmd_read_emmc(cskburn_serial_device_t *dev, uint32_t address, uint32_t size, uint8_t *data,
93+
uint32_t *data_len);
9294

9395
#endif // __LIB_CSKBURN_SERIAL_CMD__

0 commit comments

Comments
 (0)