From 669c2005d37dbc5fbae8e73d314a17a19e6fc0cf Mon Sep 17 00:00:00 2001 From: Igor Opaniuk Date: Tue, 24 Jun 2025 06:16:44 +0200 Subject: [PATCH 01/13] checkpatch: add more exceptions Add more rules to ignore, as some of them incorrectly report false positive results, for example in [1]. [1] https://github.com/linux-msm/qdl/actions/runs/15830061230/job/44624781156?pr=37 Signed-off-by: Igor Opaniuk --- .checkpatch.conf | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) 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 From eab45794d2177bae40d0836e6543471acd1d730e Mon Sep 17 00:00:00 2001 From: Igor Opaniuk Date: Tue, 24 Jun 2025 06:34:25 +0200 Subject: [PATCH 02/13] firehose: address checkpatch issues Address this checkpatch issue: CHECK: line length of 136 exceeds 120 columns + xml_setpropf(node_to_send, "bWriteBoosterBufferPreserveUserSpaceEn", "%d", ufs->bWriteBoosterBufferPreserveUserSpaceEn); Signed-off-by: Igor Opaniuk --- firehose.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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); } From fbb965174d074bc1de85845b7de39587d0f5d1e9 Mon Sep 17 00:00:00 2001 From: Igor Opaniuk Date: Tue, 24 Jun 2025 06:35:31 +0200 Subject: [PATCH 03/13] ufs: address checkpatch issues Address these issues reported by checkpatch.pl: CHECK: Alignment should match open parenthesis +int ufs_provisioning_execute(struct qdl_device *qdl, + int (*apply_ufs_common)(struct qdl_device *qdl, struct ufs_common *ufs), CHECK: line length of 133 exceeds 120 columns + result->bWriteBoosterBufferPreserveUserSpaceEn = !!attr_as_unsigned(node, "bWriteBoosterBufferPreserveUserSpaceEn", &errors); CHECK: Please don't use multiple blank lines Signed-off-by: Igor Opaniuk --- ufs.c | 5 +++-- ufs.h | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) 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 From a79ccefb2e3c93d46d86e081450e7be3c3ba92a9 Mon Sep 17 00:00:00 2001 From: Igor Opaniuk Date: Tue, 24 Jun 2025 06:36:33 +0200 Subject: [PATCH 04/13] sim: address checkpatch issues Address these issues reported by checkpatch.pl script: WARNING: void function return statements are not generally useful + return; +} WARNING: void function return statements are not generally useful + return; +} Signed-off-by: Igor Opaniuk --- sim.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) 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) { From e46302830cfa8d9af85e83dfc333744ee2a9b5d9 Mon Sep 17 00:00:00 2001 From: Igor Opaniuk Date: Tue, 24 Jun 2025 06:42:37 +0200 Subject: [PATCH 05/13] util: address checkpatch issues CHECK: Macro argument reuse 'x' - possible side-effects? +#define MIN(x, y) ((x) < (y) ? (x) : (y)) CHECK: Macro argument reuse 'y' - possible side-effects? +#define MIN(x, y) ((x) < (y) ? (x) : (y)) Signed-off-by: Igor Opaniuk --- util.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/util.c b/util.c index a6c466e..44c3d72 100644 --- a/util.c +++ b/util.c @@ -13,7 +13,11 @@ #include "version.h" -#define MIN(x, y) ((x) < (y) ? (x) : (y)) +#define MIN(x, y) ({ \ + __typeof__(x) _x = (x); \ + __typeof__(y) _y = (y); \ + _x < _y ? _x : _y; \ +}) static uint8_t to_hex(uint8_t ch) { From cc4dced2f80ae7bf79bde1c06f1daee6f08c7712 Mon Sep 17 00:00:00 2001 From: Igor Opaniuk Date: Tue, 24 Jun 2025 06:44:03 +0200 Subject: [PATCH 06/13] sahara: address checkpatch issues CHECK: Macro argument reuse 'x' - possible side-effects? +#define MIN(x, y) ((x) < (y) ? (x) : (y)) CHECK: Macro argument reuse 'y' - possible side-effects? +#define MIN(x, y) ((x) < (y) ? (x) : (y)) CHECK: line length of 123 exceeds 120 columns + ux_debug("%-2d: type 0x%" PRIx64 " address: 0x%" PRIx64 " length: 0x%" PRIx64 " region: %s filename: %s\ Signed-off-by: Igor Opaniuk --- sahara.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/sahara.c b/sahara.c index b8f3b18..40d8f69 100644 --- a/sahara.c +++ b/sahara.c @@ -19,7 +19,11 @@ #include "qdl.h" #include "oscompat.h" -#define MIN(x, y) ((x) < (y) ? (x) : (y)) +#define MIN(x, y) ({ \ + __typeof__(x) _x = (x); \ + __typeof__(y) _y = (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 */ @@ -420,8 +424,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) From 67472bf7502136a6c68a320cc5720fd8eed137d2 Mon Sep 17 00:00:00 2001 From: Igor Opaniuk Date: Tue, 24 Jun 2025 19:54:49 +0200 Subject: [PATCH 07/13] qdl: address checkpatch issues CHECK: Macro argument 'typecast' may be better as '(typecast)' to Signed-off-by: Igor Opaniuk --- qdl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qdl.h b/qdl.h index 93203b9..4d35ef0 100644 --- a/qdl.h +++ b/qdl.h @@ -11,7 +11,7 @@ #define container_of(ptr, typecast, member) ({ \ void *_ptr = (void *)(ptr); \ - ((typecast *)(_ptr - offsetof(typecast, member))); }) + ((typeof(typecast) *)(_ptr - offsetof(typecast, member))); }) #define MAPPING_SZ 64 From adf906c646bbab770ef94366b50a6a572b2d32e1 Mon Sep 17 00:00:00 2001 From: Igor Opaniuk Date: Tue, 24 Jun 2025 06:47:46 +0200 Subject: [PATCH 08/13] ux: address checkpatch issues CHECK: Macro argument reuse 'x' - possible side-effects? +#define MIN(x, y) ((x) < (y) ? (x) : (y)) CHECK: Macro argument reuse 'y' - possible side-effects? +#define MIN(x, y) ((x) < (y) ? (x) : (y)) CHECK: line length of 167 exceeds 120 columns +static const char * const progress_hashes = "########################################################################################################################"; CHECK: line length of 167 exceeds 120 columns +static const char * const progress_dashes = "------------------------------------------------------------------------------------------------------------------------"; WARNING: please, no spaces at the start of a line + CONSOLE_SCREEN_BUFFER_INFO csbi;$ WARNING: please, no spaces at the start of a line + HANDLE stdoutHandle = GetStdHandle(STD_OUTPUT_HANDLE);$ WARNING: please, no spaces at the start of a line + if (GetConsoleScreenBufferInfo(stdoutHandle, &csbi)) {$ WARNING: suspect code indent for conditional statements (4, 16) + if (GetConsoleScreenBufferInfo(stdoutHandle, &csbi)) { + columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; WARNING: please, no spaces at the start of a line + }$ Signed-off-by: Igor Opaniuk --- ux.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/ux.c b/ux.c index 267630f..d91d5a0 100644 --- a/ux.c +++ b/ux.c @@ -9,13 +9,20 @@ #include "qdl.h" -#define MIN(x, y) ((x) < (y) ? (x) : (y)) +#define MIN(x, y) ({ \ + __typeof__(x) _x = (x); \ + __typeof__(y) _y = (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 +51,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 +167,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); From 03560e9e0431268e03c33aefc035d668f3680344 Mon Sep 17 00:00:00 2001 From: Igor Opaniuk Date: Tue, 24 Jun 2025 14:31:26 +0200 Subject: [PATCH 09/13] vip: address checkpatch issues CHECK: Please use a blank line after function/struct/union/enum declarations +}; Signed-off-by: Igor Opaniuk --- vip.h | 1 + 1 file changed, 1 insertion(+) 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 { From 3e5426d5cbe98f0b10e113217abb42135cb61e18 Mon Sep 17 00:00:00 2001 From: Igor Opaniuk Date: Tue, 24 Jun 2025 15:08:12 +0200 Subject: [PATCH 10/13] program: address checkpatch issues WARNING: Comparisons should place the constant on the right side of the test if (NULL != xmlGetProp(node, (xmlChar *)"last_sector")) { Signed-off-by: Igor Opaniuk --- program.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 { From ec418a144fded8bb127abb322fbd67c307c74a89 Mon Sep 17 00:00:00 2001 From: Igor Opaniuk Date: Tue, 24 Jun 2025 06:49:51 +0200 Subject: [PATCH 11/13] spdx: add missing license identifiers Add missing SPDX license identifiers to source files. Signed-off-by: Igor Opaniuk --- ks.c | 1 + oscompat.c | 1 + oscompat.h | 1 + patch.h | 1 + program.h | 1 + qdl.h | 1 + ramdump.c | 1 + read.h | 1 + usb.c | 1 + ux.c | 1 + 10 files changed, 10 insertions(+) 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.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 4d35ef0..ea13ab8 100644 --- a/qdl.h +++ b/qdl.h @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ #ifndef __QDL_H__ #define __QDL_H__ 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/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/ux.c b/ux.c index d91d5a0..236cf02 100644 --- a/ux.c +++ b/ux.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: BSD-3-Clause #include #ifdef _WIN32 #include From d48d852ca2f2d1ecb6332a541100942e6209b23d Mon Sep 17 00:00:00 2001 From: Igor Opaniuk Date: Tue, 24 Jun 2025 06:51:22 +0200 Subject: [PATCH 12/13] makefile: add check and check-cached targets `check` target runs checkpatch.pl on all sources files. `check-cached` target runs checkpatch.pl on staged changes. Signed-off-by: Igor Opaniuk --- .gitignore | 2 ++ Makefile | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) 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 From 25562d208d053497d1015ab29724fe686b3ea38c Mon Sep 17 00:00:00 2001 From: Igor Opaniuk Date: Tue, 24 Jun 2025 08:00:41 +0200 Subject: [PATCH 13/13] qdl: move MIN and ROUND_UP macros Move MIN and ROUND_UP macros to avoid code duplication. Signed-off-by: Igor Opaniuk --- qdl.h | 15 ++++++++++++--- sahara.c | 6 ------ util.c | 7 +------ ux.c | 6 ------ 4 files changed, 13 insertions(+), 21 deletions(-) diff --git a/qdl.h b/qdl.h index ea13ab8..ea79429 100644 --- a/qdl.h +++ b/qdl.h @@ -14,10 +14,19 @@ void *_ptr = (void *)(ptr); \ ((typeof(typecast) *)(_ptr - offsetof(typecast, member))); }) -#define MAPPING_SZ 64 +#define MIN(x, y) ({ \ + __typeof__(x) _x = (x); \ + __typeof__(y) _y = (y); \ + _x < _y ? _x : _y; \ +}) -#define MIN(x, y) ((x) < (y) ? (x) : (y)) -#define ROUND_UP(x, a) (((x) + (a) - 1) & ~((a) - 1)) +#define ROUND_UP(x, a) ({ \ + __typeof__(x) _x = (x); \ + __typeof__(a) _a = (a); \ + (_x + _a - 1) & ~(_a - 1); \ +}) + +#define MAPPING_SZ 64 enum QDL_DEVICE_TYPE { QDL_DEVICE_USB, diff --git a/sahara.c b/sahara.c index 40d8f69..75af10a 100644 --- a/sahara.c +++ b/sahara.c @@ -19,12 +19,6 @@ #include "qdl.h" #include "oscompat.h" -#define MIN(x, y) ({ \ - __typeof__(x) _x = (x); \ - __typeof__(y) _y = (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 */ diff --git a/util.c b/util.c index 44c3d72..8e41896 100644 --- a/util.c +++ b/util.c @@ -11,14 +11,9 @@ #include #include +#include "qdl.h" #include "version.h" -#define MIN(x, y) ({ \ - __typeof__(x) _x = (x); \ - __typeof__(y) _y = (y); \ - _x < _y ? _x : _y; \ -}) - static uint8_t to_hex(uint8_t ch) { ch &= 0xf; diff --git a/ux.c b/ux.c index 236cf02..587daa2 100644 --- a/ux.c +++ b/ux.c @@ -10,12 +10,6 @@ #include "qdl.h" -#define MIN(x, y) ({ \ - __typeof__(x) _x = (x); \ - __typeof__(y) _y = (y); \ - _x < _y ? _x : _y; \ -}) - #define UX_PROGRESS_REFRESH_RATE 10 #define UX_PROGRESS_SIZE_MAX 80