diff --git a/Makefile.am b/Makefile.am index dfa49b2..eda4c24 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,2 +1,2 @@ AUTOMAKE_OPTIONS = foreign -SUBDIRS = src +SUBDIRS = src scripts diff --git a/configure.ac b/configure.ac index 423d56f..1ce4a26 100644 --- a/configure.ac +++ b/configure.ac @@ -27,5 +27,6 @@ AC_CHECK_FUNCS([bzero getpagesize memset munmap select strstr strtoul strtoull]) AC_CONFIG_FILES([ Makefile src/Makefile + scripts/Makefile ]) AC_OUTPUT diff --git a/scripts/Makefile.am b/scripts/Makefile.am new file mode 100644 index 0000000..7fff2d2 --- /dev/null +++ b/scripts/Makefile.am @@ -0,0 +1 @@ +bin_SCRIPTS = splash-convert diff --git a/src/Makefile.am b/src/Makefile.am index 561e9d9..36223ca 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2,7 +2,7 @@ GITCOMMIT:= $(shell git describe --abbrev=12 --dirty --always) CFLAGS=-Wall -fno-tree-cselim -tshwctl_SOURCES = tshwctl.c fpga.c eval_cmdline.c helpers.c +tshwctl_SOURCES = tshwctl.c fpga.c helpers.c tshwctl_CPPFLAGS = -DGITCOMMIT="\"${GITCOMMIT}\"" lcdmesg_SOURCES = lcdmesg.c helpers.c fpga.c diff --git a/src/eval_cmdline.c b/src/eval_cmdline.c deleted file mode 100755 index 32c2276..0000000 --- a/src/eval_cmdline.c +++ /dev/null @@ -1,89 +0,0 @@ -/* SPDX-License-Identifier: BSD-2-Clause */ -/* Copyright (c) 2019-2022 Technologic Systems, Inc. dba embeddedTS */ - -/* Due to the nature of the parsing, the following conditions must be met: - * - * Each token to evaluate must be in the style of " var=val " The var is a - * string, while value can be in hex (prefixed with 0x), octal (prefixed - * with a 0), or decimal (all other numbers). There must be a non-number - * immediately after the end of the val. - * Each token to evaluate must be present once and only once. Multiple tokens - * of the same name will return the FIRST value in the cmdline. - * e.g. "... var=0xaa ... var=0x55 ..." Will return "var" as 0xaa. - */ - -#include -#include -#include -#include -#include -#include - -static FILE *cmd_fd = NULL; -static char *cmd_str = NULL; - -/* As /proc/cmdline is not a "real" file, its not possible to get the size - * unless the whole thing is read until EOF. - * Open /proc/cmdline, read until EOF while discarding the contents, then malloc - * some memory that is the length+1 of cmdline so there is a trailing NULL, then - * read the file in to memory from the file, close the file. - * - * Even though the dummy read is byte at a time, it is fast since it is a - * kernel pipe rather than real IO. This only needs to be done once at runtime, - * after that the cmdline in memory can be parsed multiple times. - */ -void eval_cmd_init(void) -{ - size_t sz, ret; - char buf; - - if (cmd_fd != NULL) { - return; - } - - cmd_fd = fopen("/proc/cmdline", "r"); - if ((int32_t)cmd_fd == -1) { - error(errno, errno, "Failed to open /proc/cmdline"); - } - - for (sz = 0; ; sz++) { - ret = fread(&buf, sizeof(char), 1, cmd_fd); - if (ret == 0) break; - } - - rewind(cmd_fd); - - cmd_str = (char *)calloc(sz+1, sizeof(char)); - if (cmd_str == NULL) { - fclose(cmd_fd); - error(errno, errno, "Failed to allocate memory"); - } - - fread(cmd_str, sizeof(char), sz, cmd_fd); - fclose(cmd_fd); -} - -/* Perform the actual evaluation of variable value similar to bash eval - * Note that token must be "var" and not "var=" - * - * Returns -1 if the token was not found in the kernel cmdline - * Otherwise returns value of the token - */ -int32_t eval_cmd(const char *token) -{ - char *ptr; - int32_t ret = -1; - - assert(cmd_str != NULL); - - ptr = strstr(cmd_str, token); - if (ptr != NULL) { - /* strstr points us to start of token, add length of token +1 to - * now point at the start of value, after the '=' sign. - */ - ptr += (strlen(token) + 1); - ret = (int32_t)strtoul(ptr, NULL, 0); - } - - return ret; -} diff --git a/src/eval_cmdline.h b/src/eval_cmdline.h deleted file mode 100755 index 78c73ca..0000000 --- a/src/eval_cmdline.h +++ /dev/null @@ -1,10 +0,0 @@ -/* SPDX-License-Identifier: BSD-2-Clause */ -/* Copyright (c) 2019-2022 Technologic Systems, Inc. dba embeddedTS */ - -#ifndef __EVAL_CMDLINE_H__ -#define __EVAL_CMDLINE_H__ - -void eval_cmd_init(void); -int32_t eval_cmd(const char *token); - -#endif diff --git a/src/helpers.c b/src/helpers.c index daec40d..3813d06 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -1,7 +1,9 @@ #include #include #include +#include #include +#include int get_model(void) { @@ -29,3 +31,25 @@ int get_model(void) return ret; } + +int chosen_read_u32(const char *name, uint32_t *value) +{ + char path[256]; + FILE *fp; + uint32_t be_val; + size_t n; + + snprintf(path, sizeof(path), "/sys/firmware/devicetree/base/chosen/%s", name); + + fp = fopen(path, "rb"); + if (!fp) + return errno; + n = fread(&be_val, 1, sizeof(be_val), fp); + fclose(fp); + + if (n != sizeof(be_val)) + return EIO; + + *value = be32toh(be_val); + return 0; +} diff --git a/src/helpers.h b/src/helpers.h index 4b9e2df..97f88a7 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -2,5 +2,6 @@ #define __HELPERS_H__ int get_model(void); +int chosen_read_u32(const char *name, uint32_t *value); #endif //__HELPERS_H__ diff --git a/src/tshwctl.c b/src/tshwctl.c index fc02c69..f0d54ad 100755 --- a/src/tshwctl.c +++ b/src/tshwctl.c @@ -13,7 +13,6 @@ #include #include #include -#include "eval_cmdline.h" #include "fpga.h" #include "helpers.h" @@ -24,17 +23,36 @@ int model = 0; void do_info(void) { - fpga_init(0x50004000); - eval_cmd_init(); + int ret; + fpga_init(0x50004000); printf("MODEL=%X\n", model); - if(model == 0x7100) { - printf("FPGA_REV=0x%X\n", fpeek32(0x0) >> 16); - printf("CPU_OPTS=0x%X\n", eval_cmd("cpu_opts")); - printf("IO_OPTS=0x%X\n", eval_cmd("io_opts")); - printf("IO_MODEL=0x%X\n", eval_cmd("io_model")); - } else if(model == 0x7250) { + if (model == 0x7100) { + uint32_t cpu_options, io_options, io_model; + + ret = chosen_read_u32("cpu-options", &cpu_options); + if (ret) { + fprintf(stderr, "cpu-options missing from device-tree chosen.\n" + "This may be an outdated u-boot\n"); + return; + } + + ret = chosen_read_u32("io-options", &io_options); + if (ret) { + return; + } + + ret = chosen_read_u32("io-model", &io_model); + if (ret) { + return; + } + + printf("FPGA_REV=%d\n", fpeek32(0x0)); + printf("CPU_OPTS=0x%X\n", cpu_options); + printf("IO_OPTS=0x%X\n", io_options); + printf("IO_MODEL=0x%X\n", io_model); + } else if (model == 0x7250) { uint32_t fpga_rev = fpeek32(0x0); uint32_t fpga_hash = fpeek32(0x4); uint32_t opts = fpeek32(0x8);