diff --git a/.checkpatch.conf b/.checkpatch.conf index e92f205..84c1ffc 100644 --- a/.checkpatch.conf +++ b/.checkpatch.conf @@ -1,5 +1,35 @@ --no-tree --strict +--max-line-length=120 --ignore FILE_PATH_CHANGES --ignore EMAIL_SUBJECT --ignore SPLIT_STRING + +# NEW_TYPEDEFS reports "do not add new typedefs" +# typedef struct __attribute__((__packed__)) sparse_header { +--ignore NEW_TYPEDEFS + +# PREFER_DEFINED_ATTRIBUTE_MACRO reports this kind of messages: +# WARNING: Prefer __packed over __attribute__((__packed__)) +--ignore PREFER_DEFINED_ATTRIBUTE_MACRO + +# PREFER_KERNEL_TYPES reports this kind of messages (when using --strict): +# "Prefer kernel type 'u32' over 'uint32_t'" +--ignore PREFER_KERNEL_TYPES + +# BRACES reports this kind of messages: +# braces {} are not necessary for any arm of this statement +--ignore BRACES + +# CAMELCASE reports this kind of messages: +# Avoid CamelCase: +--ignore CAMELCASE + +# AVOID_EXTERNS reports this kind of messages: +# externs should be avoided in .c files +# extern const char *__progname; +--ignore AVOID_EXTERNS + +# COMMIT_LOG_LONG_LINE reports line lengths > 75 in commit log +# Lets ignore this +--ignore COMMIT_LOG_LONG_LINE diff --git a/.gitignore b/.gitignore index 39a04fa..cd245ea 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ compile_commands.json .cache .version.h version.h +scripts +.checkpatch-camelcase.git. diff --git a/Makefile b/Makefile index 2febafb..9fd2568 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,13 @@ KS_OUT := ks KS_SRCS := ks.c sahara.c util.c ux.c oscompat.c KS_OBJS := $(KS_SRCS:.c=.o) +CHECKPATCH_SOURCES := $(shell find . -type f \( -name "*.c" -o -name "*.h" -o -name "*.sh" \) ! -name "sha2.c" ! -name "sha2.h" ! -name "*version.h") +CHECKPATCH_ROOT := https://raw.githubusercontent.com/torvalds/linux/v6.15/scripts +CHECKPATCH_URL := $(CHECKPATCH_ROOT)/checkpatch.pl +CHECKPATCH_SP_URL := $(CHECKPATCH_ROOT)/spelling.txt +CHECKPATCH := ./.scripts/checkpatch.pl +CHECKPATCH_SP := ./.scripts/spelling.txt + default: $(QDL) $(RAMDUMP) $(KS_OUT) $(QDL): $(QDL_OBJS) @@ -42,6 +49,9 @@ clean: rm -f $(KS_OUT) $(KS_OBJS) rm -f compile_commands.json rm -f version.h .version.h + rm -f $(CHECKPATCH) + rm -f $(CHECKPATCH_SP) + if [ -d .scripts ]; then rmdir .scripts; fi install: $(QDL) $(RAMDUMP) $(KS_OUT) install -d $(DESTDIR)$(prefix)/bin @@ -50,3 +60,25 @@ install: $(QDL) $(RAMDUMP) $(KS_OUT) tests: default tests: @./tests/run_tests.sh + +# Target to download checkpatch.pl if not present +$(CHECKPATCH): + @echo "Downloading checkpatch.pl..." + @mkdir -p $(dir $(CHECKPATCH)) + @curl -sSfL $(CHECKPATCH_URL) -o $(CHECKPATCH) + @curl -sSfL $(CHECKPATCH_SP_URL) -o $(CHECKPATCH_SP) + @chmod +x $(CHECKPATCH) + +check: $(CHECKPATCH) + @echo "Running checkpatch on source files (excluding sha2.c and sha2.h)..." + @for file in $(CHECKPATCH_SOURCES); do \ + perl $(CHECKPATCH) --no-tree -f $$file || exit 1; \ + done + +check-cached: $(CHECKPATCH) + @echo "Running checkpatch on staged changes..." + @git diff --cached --name-only --diff-filter=ACMRT | grep -E '\.(c|h)$$' | while read file; do \ + if [ -f "$$file" ]; then \ + git show :$$file | perl $(CHECKPATCH) --no-tree -; \ + fi \ + done diff --git a/firehose.c b/firehose.c index e3afb28..9978dc6 100644 --- a/firehose.c +++ b/firehose.c @@ -681,7 +681,8 @@ int firehose_apply_ufs_common(struct qdl_device *qdl, struct ufs_common *ufs) xml_setpropf(node_to_send, "bConfigDescrLock", "%d", ufs->bConfigDescrLock); if (ufs->wb) { - xml_setpropf(node_to_send, "bWriteBoosterBufferPreserveUserSpaceEn", "%d", ufs->bWriteBoosterBufferPreserveUserSpaceEn); + xml_setpropf(node_to_send, "bWriteBoosterBufferPreserveUserSpaceEn", + "%d", ufs->bWriteBoosterBufferPreserveUserSpaceEn); xml_setpropf(node_to_send, "bWriteBoosterBufferType", "%d", ufs->bWriteBoosterBufferType); xml_setpropf(node_to_send, "shared_wb_buffer_size_in_kb", "%d", ufs->shared_wb_buffer_size_in_kb); } diff --git a/ks.c b/ks.c index 29211cb..5b000c1 100644 --- a/ks.c +++ b/ks.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: BSD-3-Clause #include #include #include diff --git a/oscompat.c b/oscompat.c index d66385e..e4af3ea 100644 --- a/oscompat.c +++ b/oscompat.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: BSD-3-Clause #ifdef _WIN32 #include diff --git a/oscompat.h b/oscompat.h index 883126e..b7730bd 100644 --- a/oscompat.h +++ b/oscompat.h @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ #ifndef __OSCOMPAT_H__ #define __OSCOMPAT_H__ diff --git a/patch.h b/patch.h index f1374b8..9726303 100644 --- a/patch.h +++ b/patch.h @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ #ifndef __PATCH_H__ #define __PATCH_H__ diff --git a/program.c b/program.c index c59f98b..585cf07 100644 --- a/program.c +++ b/program.c @@ -72,7 +72,7 @@ static int load_program_tag(xmlNode *node, bool is_nand) if (is_nand) { program->pages_per_block = attr_as_unsigned(node, "PAGES_PER_BLOCK", &errors); - if (NULL != xmlGetProp(node, (xmlChar *)"last_sector")) { + if (xmlGetProp(node, (xmlChar *)"last_sector")) { program->last_sector = attr_as_unsigned(node, "last_sector", &errors); } } else { diff --git a/program.h b/program.h index 11aa025..773a343 100644 --- a/program.h +++ b/program.h @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ #ifndef __PROGRAM_H__ #define __PROGRAM_H__ diff --git a/qdl.h b/qdl.h index 93203b9..ea79429 100644 --- a/qdl.h +++ b/qdl.h @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ #ifndef __QDL_H__ #define __QDL_H__ @@ -11,13 +12,22 @@ #define container_of(ptr, typecast, member) ({ \ void *_ptr = (void *)(ptr); \ - ((typecast *)(_ptr - offsetof(typecast, member))); }) + ((typeof(typecast) *)(_ptr - offsetof(typecast, member))); }) + +#define MIN(x, y) ({ \ + __typeof__(x) _x = (x); \ + __typeof__(y) _y = (y); \ + _x < _y ? _x : _y; \ +}) + +#define ROUND_UP(x, a) ({ \ + __typeof__(x) _x = (x); \ + __typeof__(a) _a = (a); \ + (_x + _a - 1) & ~(_a - 1); \ +}) #define MAPPING_SZ 64 -#define MIN(x, y) ((x) < (y) ? (x) : (y)) -#define ROUND_UP(x, a) (((x) + (a) - 1) & ~((a) - 1)) - enum QDL_DEVICE_TYPE { QDL_DEVICE_USB, QDL_DEVICE_SIM, diff --git a/ramdump.c b/ramdump.c index 9e9838c..baa87ff 100644 --- a/ramdump.c +++ b/ramdump.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: BSD-3-Clause #include #include #include diff --git a/read.h b/read.h index a8fce4a..f64b334 100644 --- a/read.h +++ b/read.h @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ #ifndef __READ_H__ #define __READ_H__ diff --git a/sahara.c b/sahara.c index b8f3b18..75af10a 100644 --- a/sahara.c +++ b/sahara.c @@ -19,8 +19,6 @@ #include "qdl.h" #include "oscompat.h" -#define MIN(x, y) ((x) < (y) ? (x) : (y)) - #define SAHARA_HELLO_CMD 0x1 /* Min protocol version 1.0 */ #define SAHARA_HELLO_RESP_CMD 0x2 /* Min protocol version 1.0 */ #define SAHARA_READ_DATA_CMD 0x3 /* Min protocol version 1.0 */ @@ -420,8 +418,10 @@ static void sahara_debug64(struct qdl_device *qdl, struct sahara_pkt *pkt, if (sahara_debug64_filter(table[i].filename, filter)) continue; - ux_debug("%-2d: type 0x%" PRIx64 " address: 0x%" PRIx64 " length: 0x%" PRIx64 " region: %s filename: %s\n", - i, table[i].type, table[i].addr, table[i].length, table[i].region, table[i].filename); + ux_debug("%-2d: type 0x%" PRIx64 " address: 0x%" PRIx64 " length: 0x%" + PRIx64 " region: %s filename: %s\n", + i, table[i].type, table[i].addr, table[i].length, + table[i].region, table[i].filename); n = sahara_debug64_one(qdl, table[i], ramdump_path); if (n < 0) diff --git a/sim.c b/sim.c index 29e9a77..ac117e2 100644 --- a/sim.c +++ b/sim.c @@ -20,10 +20,7 @@ static int sim_open(struct qdl_device *qdl, const char *serial) return 0; } -static void sim_close(struct qdl_device *qdl) -{ - return; -} +static void sim_close(struct qdl_device *qdl) {} static int sim_read(struct qdl_device *qdl, void *buf, size_t len, unsigned int timeout) { @@ -35,10 +32,7 @@ static int sim_write(struct qdl_device *qdl, const void *buf, size_t len) return len; } -static void sim_set_out_chunk_size(struct qdl_device *qdl, long size) -{ - return; -} +static void sim_set_out_chunk_size(struct qdl_device *qdl, long size) {} struct qdl_device *sim_init(void) { diff --git a/ufs.c b/ufs.c index 51ce843..d6751f7 100644 --- a/ufs.c +++ b/ufs.c @@ -31,7 +31,6 @@ static const char notice_bconfigdescrlock[] = "\n" " and don't use command line parameter --finalize-provisioning.\n\n" "In case of mismatch between CL and XML provisioning is not performed.\n\n"; - bool ufs_need_provisioning(void) { return !!ufs_epilogue_p; @@ -63,7 +62,9 @@ struct ufs_common *ufs_parse_common_params(xmlNode *node, bool finalize_provisio /* These parameters are optional */ errors = 0; - result->bWriteBoosterBufferPreserveUserSpaceEn = !!attr_as_unsigned(node, "bWriteBoosterBufferPreserveUserSpaceEn", &errors); + result->bWriteBoosterBufferPreserveUserSpaceEn = !!attr_as_unsigned(node, + "bWriteBoosterBufferPreserveUserSpaceEn", + &errors); result->bWriteBoosterBufferType = !!attr_as_unsigned(node, "bWriteBoosterBufferType", &errors); result->shared_wb_buffer_size_in_kb = attr_as_unsigned(node, "shared_wb_buffer_size_in_kb", &errors); result->wb = !errors; diff --git a/ufs.h b/ufs.h index f74127b..ffbe33c 100644 --- a/ufs.h +++ b/ufs.h @@ -47,9 +47,9 @@ struct ufs_epilogue { int ufs_load(const char *ufs_file, bool finalize_provisioning); int ufs_provisioning_execute(struct qdl_device *qdl, - int (*apply_ufs_common)(struct qdl_device *qdl, struct ufs_common *ufs), - int (*apply_ufs_body)(struct qdl_device *qdl, struct ufs_body *ufs), - int (*apply_ufs_epilogue)(struct qdl_device *qdl, struct ufs_epilogue *ufs, bool commit)); + int (*apply_ufs_common)(struct qdl_device *qdl, struct ufs_common *ufs), + int (*apply_ufs_body)(struct qdl_device *qdl, struct ufs_body *ufs), + int (*apply_ufs_epilogue)(struct qdl_device *qdl, struct ufs_epilogue *ufs, bool commit)); bool ufs_need_provisioning(void); #endif diff --git a/usb.c b/usb.c index 73e77bc..a5e4e6c 100644 --- a/usb.c +++ b/usb.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: BSD-3-Clause #include #include #include diff --git a/util.c b/util.c index a6c466e..8e41896 100644 --- a/util.c +++ b/util.c @@ -11,10 +11,9 @@ #include #include +#include "qdl.h" #include "version.h" -#define MIN(x, y) ((x) < (y) ? (x) : (y)) - static uint8_t to_hex(uint8_t ch) { ch &= 0xf; diff --git a/ux.c b/ux.c index 267630f..587daa2 100644 --- a/ux.c +++ b/ux.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: BSD-3-Clause #include #ifdef _WIN32 #include @@ -9,13 +10,14 @@ #include "qdl.h" -#define MIN(x, y) ((x) < (y) ? (x) : (y)) - #define UX_PROGRESS_REFRESH_RATE 10 -#define UX_PROGRESS_SIZE_MAX 120 +#define UX_PROGRESS_SIZE_MAX 80 -static const char * const progress_hashes = "########################################################################################################################"; -static const char * const progress_dashes = "------------------------------------------------------------------------------------------------------------------------"; +#define HASHES "################################################################################" +#define DASHES "--------------------------------------------------------------------------------" + +static const char * const progress_hashes = HASHES; +static const char * const progress_dashes = DASHES; static unsigned int ux_width; static unsigned int ux_cur_line_length; @@ -44,15 +46,15 @@ static void ux_clear_line(void) void ux_init(void) { - CONSOLE_SCREEN_BUFFER_INFO csbi; + CONSOLE_SCREEN_BUFFER_INFO csbi; int columns; - HANDLE stdoutHandle = GetStdHandle(STD_OUTPUT_HANDLE); + HANDLE stdoutHandle = GetStdHandle(STD_OUTPUT_HANDLE); - if (GetConsoleScreenBufferInfo(stdoutHandle, &csbi)) { + if (GetConsoleScreenBufferInfo(stdoutHandle, &csbi)) { columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; ux_width = MIN(columns, UX_PROGRESS_SIZE_MAX); - } + } } #else @@ -160,9 +162,9 @@ void ux_progress(const char *fmt, unsigned int value, unsigned int max, ...) printf("%-20.20s [%.*s%.*s] %1.2f%%%n\r", task_name, bars, progress_hashes, - dashes, progress_dashes, - percent * 100, - &ux_cur_line_length); + dashes, progress_dashes, + percent * 100, + &ux_cur_line_length); fflush(stdout); gettimeofday(&last_progress_update, NULL); diff --git a/vip.h b/vip.h index b916c79..f59199d 100644 --- a/vip.h +++ b/vip.h @@ -16,6 +16,7 @@ enum vip_state { VIP_SEND_DATA, VIP_MAX, }; + #define MAX_CHAINED_FILES 32 struct vip_transfer_data {