Skip to content

Instantly share code, notes, and snippets.

@RoootTheFox
Created November 28, 2024 01:09
Show Gist options
  • Save RoootTheFox/717efcc1f129dc86344df421f53ec993 to your computer and use it in GitHub Desktop.
Save RoootTheFox/717efcc1f129dc86344df421f53ec993 to your computer and use it in GitHub Desktop.
diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c
index bc00369..173ad2a 100644
--- a/wiringPi/wiringPi.c
+++ b/wiringPi/wiringPi.c
@@ -1077,6 +1077,74 @@ const char* GetPiRevision(char* line, int linelength, unsigned int* revision) {
*********************************************************************************
*/
+const char* hackyGetRevisionFix(char* line, int linelength, unsigned int* revision) {
+ FILE *modelFd;
+ char *c = NULL;
+
+ if ((modelFd = fopen ("/proc/device-tree/model", "r")) == NULL)
+ piGpioLayoutOops ("Unable to open /proc/device-tree/model");
+
+ while (fgets(line, linelength, modelFd) != NULL)
+ if (strncmp(line, "Raspberry Pi", 12) == 0) // look for line that starts w "Raspberry" (only line lol)
+ break;
+
+ fclose (modelFd);
+
+ if (strncmp(line, "Raspberry Pi", 12) != 0)
+ piGpioLayoutOops ("no stupid line") ;
+
+ // Chomp trailing CR/NL
+ for (c = &line[strlen(line) - 1]; (*c == '\n') || (*c == '\r'); --c)
+ *c = 0;
+
+ c = line;
+
+ // jump to the end of "Raspberry Pi" (12 chars)
+ c += 12; // ^
+ c++; // skip the space after "Raspberry Pi", directly to the ver model number
+
+ char mn = '\0'; // model num
+ char mr = 0; // model rev
+
+ // printf("pl c: %c at %i\n", *c, (int)c);
+ if (isdigit(*c)) {
+ mn = *c;
+ } else {
+ printf("err !! no model number found !!\n");
+ return NULL;
+ }
+
+ c += 2; // jump from model number to the M of "Model", checking if we *do* have that
+ if (*c != 'M') {
+ printf("ERR!! NOT MODEL, GOT %c\n", *c);
+ return NULL;
+ }
+
+ // we now confirmed that it does say "Model" after; get the board (A, B, ...)
+ c += 6; // this should land us directly on A or B (of the model)
+
+ if (isupper(*c)) {
+ mr = *c;
+ }
+
+ // printf("dbg RPI MODEL: %c - %c!\n", mn, mr);
+
+ char *r = NULL;
+
+ // https://www.raspberrypi.com/documentation/computers/raspberry-pi.html#raspberry-pi-revision-codes
+ if (mn == '4' && mr == 'B') {
+ r = "a03111";
+ } else if (mn == '3' && mr == 'B') {
+ r = "a52082"; // untested lmao
+ }
+
+ if (*r != NULL) {
+ *revision = (unsigned int)strtol (r, NULL, 16) ; // Hex number with no leading 0x
+ return r;
+ }
+ return NULL;
+}
+
void piBoardId (int *model, int *rev, int *mem, int *maker, int *warranty)
{
const int maxlength = 120;
@@ -1088,9 +1156,14 @@ void piBoardId (int *model, int *rev, int *mem, int *maker, int *warranty)
//piGpioLayoutOops ("this is only a test case");
c = GetPiRevision(line, maxlength, &revision); // device tree
+/*
if (NULL==c) {
c = GetPiRevisionLegacy(line, maxlength, &revision); // proc/cpuinfo
}
+*/
+ if (NULL==c) {
+ c = hackyGetRevisionFix(line, maxlength, &revision);
+ }
if (NULL==c) {
piGpioLayoutOops ("GetPiRevision failed!") ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment