From 5f512ab2097cce48d93cbf938d2fb6359734ea2e Mon Sep 17 00:00:00 2001 From: Dmitry Ilyin <6576495+widgetii@users.noreply.github.com> Date: Thu, 12 Nov 2026 16:04:18 +0300 Subject: [PATCH 1/3] gpio dump: every pin's mux and level, once and read-only `gpio scan` answers "what changes": it loops until Ctrl-C and lists only pins already muxed to GPIO. `gpio mux` writes. Neither answers "what is this board's GPIO state", which is the question when one firmware drives a peripheral and another does not. Comparing a stock Xiongmai N51813L with its OpenIPC sibling needed a hand-merged `reginfo` and a `gpio scan` that had to be killed by PID, because stock busybox has no `timeout`. `ipctool gpio dump [--json]` prints, for every pin of every PL061 bank, the function its pad carries now. Where that is the GPIO, or the table has no row for the pad, it also prints the direction and level. It reads each bank once, writes nothing and exits. `--json` gives one object per pin, so two cameras diff directly. SigmaStar and Novatek keep GPIO state outside these banks, so they are refused rather than misreported. --- README.md | 3 +- src/main.c | 3 +- src/reginfo.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 106 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4a79d6e..2fae991 100644 --- a/README.md +++ b/README.md @@ -139,7 +139,8 @@ Where: dump data from I2C/SPI device i2cdetect [-b, --bus] attempt to detect devices on I2C bus reginfo [--script] dump current status of pinmux registers - gpio (scan|mux) GPIO utilities + gpio (dump|scan|mux) GPIO utilities; dump [--json] prints + every pin's mux and level, read-only trace [--skip=usleep] [program arguments] dump original firmware calls and data structures -h, --help this help diff --git a/src/main.c b/src/main.c index 78c3332..41b14f2 100644 --- a/src/main.c +++ b/src/main.c @@ -118,7 +118,8 @@ void print_usage() { " reginfo [--script] dump current status of pinmux registers\n" " reginfo --pads what each pad carries, and what else it " "could\n" - " gpio (scan|mux) GPIO utilities\n" + " gpio (dump|scan|mux) GPIO utilities; dump [--json] prints\n" + " every pin's mux and level, read-only\n" " clocks|freq [--json] show CPU/peripheral PLL, DDR clock and\n" " per-die HPM bin (Hisilicon V4 / Goke " "V300\n" diff --git a/src/reginfo.c b/src/reginfo.c index 55ef8d5..d40dd69 100644 --- a/src/reginfo.c +++ b/src/reginfo.c @@ -4508,9 +4508,110 @@ static int gpio_scan_cmd() { return EXIT_SUCCESS; } +/* `ipctool gpio dump [--json]`: every GPIO pin of the SoC, once -- the + * function its pad carries now and, where that is the GPIO, the direction + * and level. + * + * `gpio scan` answers "what changes", loops until Ctrl-C and prints only + * pins already muxed to GPIO; `gpio mux` writes. Neither answers "what is + * this board's GPIO state", which is the question when one firmware drives + * a peripheral and another does not: dump both and diff. Nothing here + * writes a register, and it exits on its own, so it runs under the bare + * busybox of a stock firmware with no `timeout` to stop it. */ +static int gpio_dump_cmd(int argc, char **argv) { + const struct option long_options[] = { + {"json", no_argument, NULL, 'j'}, + {NULL, 0, NULL, 0}, + }; + bool json = false; + int res; + + optind = 1; + while ((res = getopt_long_only(argc, argv, "j", long_options, NULL)) != + -1) { + switch (res) { + case 'j': + json = true; + break; + default: + fprintf(stderr, "Usage: ipctool gpio dump [--json]\n"); + return EXIT_FAILURE; + } + } + + getchipname(); + int GPIO_Groups = 0; + size_t GPIO_Base = 0; + size_t GPIO_Offset = 0; + /* SigmaStar and Novatek keep their GPIO state elsewhere than the PL061 + * banks walked below; refuse rather than print a plausible lie. */ + if (sstar_gpio_supported() || novatek_gpio_supported() || + !get_chip_gpio_adress(&GPIO_Base, &GPIO_Offset, &GPIO_Groups)) { + fprintf(stderr, "gpio dump: not supported on this SoC yet\n"); + return EXIT_FAILURE; + } + + if (json) + printf("["); + bool first = true; + for (int group = 0; group < GPIO_Groups; group++) { + size_t bank = GPIO_Base + group * GPIO_Offset; + uint32_t dir = 0, data = 0; + /* PL061: the data register is masked by address bits 9..2, so +0x3fc + * reads all eight pins at once. */ + bool readable = mem_reg(bank + 0x400, &dir, OP_READ) && + mem_reg(bank + 0x3fc, &data, OP_READ); + + for (int pin = 0; pin < 8; pin++) { + int pad = group * 8 + pin; + char gpio[16]; + snprintf(gpio, sizeof(gpio), "GPIO%d_%d", group, pin); + + ipchw_padmux_t now; + int got = ipchw_padmux_get(pad, &now); + /* No row: the table does not name the pad, so nothing can select + * another function onto it and the pin is the GPIO. */ + const char *func = got == 1 ? now.func_name + : got == 0 ? NULL + : "unreadable"; + bool is_gpio = got == 0 || (got == 1 && !strcmp(func, gpio)); + bool show = readable && is_gpio; + int out = dir >> pin & 1, level = data >> pin & 1; + + if (json) { + printf("%s\n {\"gpio\": \"%s\", \"pad\": %d, \"function\": ", + first ? "" : ",", gpio, pad); + if (func) + printf("\"%s\"", func); + else + printf("null"); + printf(", \"gpio_muxed\": %s", is_gpio ? "true" : "false"); + if (show) + printf(", \"dir\": \"%s\", \"value\": %d", + out ? "out" : "in", level); + printf("}"); + } else { + printf("%-8s pad %-4d %-20s", gpio, pad, func ? func : "-"); + if (show) + printf(" %-3s %d", out ? "out" : "in", level); + else if (is_gpio) + printf(" bank unreadable"); + printf("\n"); + } + first = false; + } + } + if (json) + printf("\n]\n"); + + return EXIT_SUCCESS; +} + int gpio_cmd(int argc, char **argv) { if (argc > 1) { - if (!strcmp(argv[1], "scan")) + if (!strcmp(argv[1], "dump")) + return gpio_dump_cmd(argc - 1, argv + 1); + else if (!strcmp(argv[1], "scan")) return gpio_scan_cmd(); else if (!strcmp(argv[1], "mux")) return gpio_mux_cmd(argc - 1, argv + 1); From a42f199b76065ff13670f6595618f5a6645587c6 Mon Sep 17 00:00:00 2001 From: Dmitry Ilyin <6576495+widgetii@users.noreply.github.com> Date: Thu, 12 Nov 2026 16:21:23 +0300 Subject: [PATCH 2/3] gpio dump: no trailing padding on lines without a GPIO state Pad the function column only when a direction and level follow it, so no line ends in spaces and two dumps diff cleanly. --- src/reginfo.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/reginfo.c b/src/reginfo.c index d40dd69..88c0c9f 100644 --- a/src/reginfo.c +++ b/src/reginfo.c @@ -4591,12 +4591,16 @@ static int gpio_dump_cmd(int argc, char **argv) { out ? "out" : "in", level); printf("}"); } else { - printf("%-8s pad %-4d %-20s", gpio, pad, func ? func : "-"); + /* Pad the function column only when a state follows it, so + * no line ends in spaces and two dumps diff cleanly. */ + printf("%-8s pad %-4d ", gpio, pad); if (show) - printf(" %-3s %d", out ? "out" : "in", level); + printf("%-20s %-3s %d\n", func ? func : "-", + out ? "out" : "in", level); else if (is_gpio) - printf(" bank unreadable"); - printf("\n"); + printf("%-20s bank unreadable\n", func ? func : "-"); + else + printf("%s\n", func ? func : "-"); } first = false; } From 7eb85ce0088002447af16553c67c2b89cab17b32 Mon Sep 17 00:00:00 2001 From: Dmitry Ilyin <6576495+widgetii@users.noreply.github.com> Date: Thu, 12 Nov 2026 16:39:19 +0300 Subject: [PATCH 3/3] gpio dump: classify pads by ipchw_padmux_get()'s contract, fail on read errors Review fixes (Qodo on #279): - 0 from ipchw_padmux_get() is a selector the table does not name. It is now reported as mux "unknown" with no GPIO state, instead of being taken as GPIO. - IPCHW_PADMUX_NO_PAD is the pad with no mux row: nothing can select another function onto it, so it is the GPIO, as mux "none" with its direction and level. GPIO8_x and GPIO11_7 on Hi3518CV100 used to lose their state. - GPIO-ness comes from IPCHW_PADMUX_F_GPIO, the documented flag, not a name comparison. - An unreadable bank or mux register is reported on stderr and makes the exit status a failure. The dump is still printed, as well-formed JSON with an "error" field on affected GPIO pins, but never passes as complete. A SoC with no pin-mux table is refused up front. JSON gains a "mux" field: gpio, function, unknown, none or unreadable. --- src/reginfo.c | 76 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 61 insertions(+), 15 deletions(-) diff --git a/src/reginfo.c b/src/reginfo.c index 88c0c9f..e6ef730 100644 --- a/src/reginfo.c +++ b/src/reginfo.c @@ -4551,6 +4551,14 @@ static int gpio_dump_cmd(int argc, char **argv) { return EXIT_FAILURE; } + /* No pin-mux table at all: every pad would come back unclassified, and + * a dump that cannot say which pins are GPIOs is not one. */ + if (ipchw_padmux_by_prefix("", NULL, 0) < 0) { + fprintf(stderr, "gpio dump: no pin-mux table for this SoC\n"); + return EXIT_FAILURE; + } + + bool failed = false; if (json) printf("["); bool first = true; @@ -4561,26 +4569,55 @@ static int gpio_dump_cmd(int argc, char **argv) { * reads all eight pins at once. */ bool readable = mem_reg(bank + 0x400, &dir, OP_READ) && mem_reg(bank + 0x3fc, &data, OP_READ); + if (!readable) { + fprintf(stderr, "gpio dump: GPIO%d bank at %#zx unreadable\n", + group, bank); + failed = true; + } for (int pin = 0; pin < 8; pin++) { int pad = group * 8 + pin; char gpio[16]; snprintf(gpio, sizeof(gpio), "GPIO%d_%d", group, pin); + /* What the pad carries, per ipchw_padmux_get(): + * 1 a named row; the GPIO one is flagged F_GPIO + * 0 a selector the table does not name: unknown, and + * certainly not proof of GPIO + * NO_PAD the table has no row for the pad, so nothing can + * select another function onto it: it is the GPIO + * other <0 the mux register could not be read */ ipchw_padmux_t now; int got = ipchw_padmux_get(pad, &now); - /* No row: the table does not name the pad, so nothing can select - * another function onto it and the pin is the GPIO. */ - const char *func = got == 1 ? now.func_name - : got == 0 ? NULL - : "unreadable"; - bool is_gpio = got == 0 || (got == 1 && !strcmp(func, gpio)); + const char *mux, *func; + bool is_gpio; + if (got == 1) { + is_gpio = now.flags & IPCHW_PADMUX_F_GPIO; + mux = is_gpio ? "gpio" : "function"; + func = now.func_name; + } else if (got == 0) { + is_gpio = false; + mux = "unknown"; + func = NULL; + } else if (got == IPCHW_PADMUX_NO_PAD) { + is_gpio = true; + mux = "none"; + func = NULL; + } else { + is_gpio = false; + mux = "unreadable"; + func = NULL; + fprintf(stderr, "gpio dump: %s pad mux unreadable (%d)\n", gpio, + got); + failed = true; + } bool show = readable && is_gpio; int out = dir >> pin & 1, level = data >> pin & 1; if (json) { - printf("%s\n {\"gpio\": \"%s\", \"pad\": %d, \"function\": ", - first ? "" : ",", gpio, pad); + printf("%s\n {\"gpio\": \"%s\", \"pad\": %d, \"mux\": \"%s\", " + "\"function\": ", + first ? "" : ",", gpio, pad, mux); if (func) printf("\"%s\"", func); else @@ -4589,18 +4626,25 @@ static int gpio_dump_cmd(int argc, char **argv) { if (show) printf(", \"dir\": \"%s\", \"value\": %d", out ? "out" : "in", level); + else if (is_gpio) + printf(", \"error\": \"bank unreadable\""); printf("}"); } else { - /* Pad the function column only when a state follows it, so - * no line ends in spaces and two dumps diff cleanly. */ + /* The function, or the mux state in brackets when there is + * no named function. Pad the column only when a state follows + * it, so no line ends in spaces and two dumps diff cleanly. */ + char label[40]; + if (func) + snprintf(label, sizeof(label), "%s", func); + else + snprintf(label, sizeof(label), "[%s]", mux); printf("%-8s pad %-4d ", gpio, pad); if (show) - printf("%-20s %-3s %d\n", func ? func : "-", - out ? "out" : "in", level); + printf("%-20s %-3s %d\n", label, out ? "out" : "in", level); else if (is_gpio) - printf("%-20s bank unreadable\n", func ? func : "-"); + printf("%-20s bank unreadable\n", label); else - printf("%s\n", func ? func : "-"); + printf("%s\n", label); } first = false; } @@ -4608,7 +4652,9 @@ static int gpio_dump_cmd(int argc, char **argv) { if (json) printf("\n]\n"); - return EXIT_SUCCESS; + /* A partial dump is still printed, well-formed, but never reported as a + * complete one. */ + return failed ? EXIT_FAILURE : EXIT_SUCCESS; } int gpio_cmd(int argc, char **argv) {