From b6d05d709f2c49ed268a0f847d0e8135446a47b5 Mon Sep 17 00:00:00 2001 From: Rafael Diniz Date: Tue, 2 May 2023 18:29:17 +0300 Subject: [PATCH] applied ubuntu patches --- devLib/Makefile | 6 ++-- gpio/Makefile | 2 +- gpio/gpio.c | 6 ---- wiringPi/Makefile | 1 + wiringPi/wiringPi.c | 90 ++++++++++++++++++++++++++++++++++++++--------------- 5 files changed, 70 insertions(+), 35 deletions(-) diff --git a/devLib/Makefile b/devLib/Makefile index 611e423..4157a4b 100644 --- a/devLib/Makefile +++ b/devLib/Makefile @@ -37,11 +37,11 @@ DYNAMIC=libwiringPiDev.so.$(VERSION) #DEBUG = -g -O0 DEBUG = -O2 CC ?= gcc -INCLUDE = -I. +INCLUDE = -I. -I../wiringPi DEFS = -D_GNU_SOURCE CFLAGS = $(DEBUG) $(DEFS) -Wformat=2 -Wall -Winline $(INCLUDE) -pipe -fPIC $(EXTRA_CFLAGS) -LIBS = +LIBS = -lpthread -lwiringPi ############################################################################### @@ -68,7 +68,7 @@ $(STATIC): $(OBJ) $(DYNAMIC): $(OBJ) $Q echo "[Link (Dynamic)]" - $Q $(CC) -shared -Wl,-soname,libwiringPiDev.so$(WIRINGPI_SONAME_SUFFIX) -o libwiringPiDev.so.$(VERSION) -lpthread $(OBJ) + $Q $(CC) -shared -Wl,-soname,libwiringPiDev.so$(WIRINGPI_SONAME_SUFFIX) -o libwiringPiDev.so.$(VERSION) -L ../wiringPi $(OBJ) $(LIBS) .c.o: $Q echo [Compile] $< diff --git a/gpio/Makefile b/gpio/Makefile index 249bb24..f61e7de 100644 --- a/gpio/Makefile +++ b/gpio/Makefile @@ -37,7 +37,7 @@ INCLUDE = -I$(DESTDIR)$(PREFIX)/include CFLAGS = $(DEBUG) -Wall -Wextra $(INCLUDE) -Winline -pipe $(EXTRA_CFLAGS) LDFLAGS = -L$(DESTDIR)$(PREFIX)/lib -LIBS = -lwiringPi -lwiringPiDev -lpthread -lrt -lm -lcrypt +LIBS = -lwiringPi -lwiringPiDev -lpthread # May not need to alter anything below this line ############################################################################### diff --git a/gpio/gpio.c b/gpio/gpio.c index 46b36df..90a4a1e 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -1371,12 +1371,6 @@ int main (int argc, char *argv []) exit (EXIT_SUCCESS) ; } - if (geteuid () != 0) - { - fprintf (stderr, "%s: Must be root to run. Program should be suid root. This is an error.\n", argv [0]) ; - exit (EXIT_FAILURE) ; - } - // Initial test for /sys/class/gpio operations: /**/ if (strcasecmp (argv [1], "exports" ) == 0) { doExports (argc, argv) ; return 0 ; } diff --git a/wiringPi/Makefile b/wiringPi/Makefile index 28501ec..71a2857 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -76,6 +76,7 @@ static: $(DYNAMIC): $(OBJ) $Q echo "[Link (Dynamic)]" $Q $(CC) -shared -Wl,-soname,libwiringPi.so$(WIRINGPI_SONAME_SUFFIX) -o libwiringPi.so.$(VERSION) $(OBJ) $(LIBS) + $Q ln -sf libwiringPi.so.$(VERSION) libwiringPi.so .c.o: $Q echo [Compile] $< diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index d4b1e24..c669d53 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -70,6 +70,7 @@ #include #include #include +#include #include "softPwm.h" #include "softTone.h" @@ -737,7 +738,8 @@ static void usingGpioMemCheck (const char *what) static void piGpioLayoutOops (const char *why) { - fprintf (stderr, "Oops: Unable to determine board revision from /proc/cpuinfo\n") ; + fprintf (stderr, "Oops: Unable to determine board revision from /proc/device-tree/system/linux,revision\n"); + fprintf (stderr, "or from /proc/cpuinfo\n") ; fprintf (stderr, " -> %s\n", why) ; fprintf (stderr, " -> You'd best google the error to find out why.\n") ; //fprintf (stderr, " -> http://www.raspberrypi.org/phpBB3/viewtopic.php?p=184410#p184410\n") ; @@ -754,8 +756,30 @@ int piGpioLayout (void) if (gpioLayout != -1) // No point checking twice return gpioLayout ; +// The "/proc/device-tree/compatible" file consists of a sequence of +// NUL-terminated strings. We expect to find brcm,bcm283[765] on pi platforms + + if ((cpuFd = fopen ("/proc/device-tree/compatible", "r")) != NULL) { + size_t linelen; + linelen = fread(line, 1, sizeof(line), cpuFd); + fclose(cpuFd); + cpuFd = NULL; + line[linelen] = '\0'; + c = line; + while ((size_t)(c - line) < linelen) { + if ((strncmp(c, "brcm,bcm2837", strlen(c)) == 0) || + (strncmp(c, "brcm,bcm2836", strlen(c)) == 0)) { + gpioLayout = 2; + if (wiringPiDebug) + printf ("piGpioLayoutOops: Returning revision: %d\n", gpioLayout) ; + return gpioLayout; + } + c += strlen(c) + 1; + } + } + if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL) - piGpioLayoutOops ("Unable to open /proc/cpuinfo") ; + piGpioLayoutOops ("Unable to open /proc/device-tree/compatible or /proc/cpuinfo") ; // Start by looking for the Architecture to make sure we're really running // on a Pi. I'm getting fed-up with people whinging at me because @@ -966,47 +990,63 @@ void piBoardId (int *model, int *rev, int *mem, int *maker, int *warranty) (void)piGpioLayout () ; // Call this first to make sure all's OK. Don't care about the result. - if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL) - piGpioLayoutOops ("Unable to open /proc/cpuinfo") ; +// The /proc/device-tree/system/linux,revision (on modern versions of Raspbian +// and a few other distros) stores the revision code as a big-endian 32-bit +// integer - while (fgets (line, 120, cpuFd) != NULL) - if (strncmp (line, "Revision", 8) == 0) - break ; + if ((cpuFd = fopen ("/proc/device-tree/system/linux,revision", "r")) != NULL) { + revision = 0; + fread(&revision, sizeof(revision), 1, cpuFd); + fclose(cpuFd); + if (revision) + revision = ntohl(revision); + else + piGpioLayoutOops ("No revision in /proc/device-tree/system/linux,revision"); + } - fclose (cpuFd) ; + else if ((cpuFd = fopen ("/proc/cpuinfo", "r")) != NULL) { - if (strncmp (line, "Revision", 8) != 0) - piGpioLayoutOops ("No \"Revision\" line") ; + while (fgets (line, 120, cpuFd) != NULL) + if (strncmp (line, "Revision", 8) == 0) + break ; + + fclose (cpuFd) ; + + if (strncmp (line, "Revision", 8) != 0) + piGpioLayoutOops ("No \"Revision\" line") ; // Chomp trailing CR/NL - for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c) - *c = 0 ; + for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c) + *c = 0 ; - if (wiringPiDebug) - printf ("piBoardId: Revision string: %s\n", line) ; + if (wiringPiDebug) + printf ("piBoardId: Revision string: %s\n", line) ; // Need to work out if it's using the new or old encoding scheme: // Scan to the first character of the revision number - for (c = line ; *c ; ++c) - if (*c == ':') - break ; + for (c = line ; *c ; ++c) + if (*c == ':') + break ; - if (*c != ':') - piGpioLayoutOops ("Bogus \"Revision\" line (no colon)") ; + if (*c != ':') + piGpioLayoutOops ("Bogus \"Revision\" line (no colon)") ; // Chomp spaces - ++c ; - while (isspace (*c)) - ++c ; + ++c ; + while (isspace (*c)) + ++c ; - if (!isxdigit (*c)) - piGpioLayoutOops ("Bogus \"Revision\" line (no hex digit at start of revision)") ; + if (!isxdigit (*c)) + piGpioLayoutOops ("Bogus \"Revision\" line (no hex digit at start of revision)") ; - revision = (unsigned int)strtol (c, NULL, 16) ; // Hex number with no leading 0x + revision = (unsigned int)strtol (c, NULL, 16) ; // Hex number with no leading 0x + } + else + piGpioLayoutOops ("Unable to open /proc/device-tree/system/linux,revision or /proc/cpuinfo") ; // Check for new way: