[v1.03][RISCV] Search backwards in device-tree string

This commit is contained in:
Dr-Noob
2023-04-07 15:24:33 +02:00
parent 0ef08ef53e
commit 69dd47bab0

View File

@@ -5,32 +5,38 @@
#include "udev.h"
#define _PATH_DEVTREE "/proc/device-tree/compatible"
#define DEVTREE_HARDWARE_FIELD 3
#define DEVTREE_HARDWARE_FIELD 0
// TODO: Works only for DEVTREE_FIELD == DEVTREE_HARDWARE_FIELD
char* get_field_from_devtree(int DEVTREE_FIELD) {
int filelen;
char* buf;
if((buf = read_file(_PATH_DEVTREE, &filelen)) == NULL) {
printWarn("read_file: %s: %s:\n", _PATH_DEVTREE, strerror(errno));
printWarn("read_file: %s: %s", _PATH_DEVTREE, strerror(errno));
return NULL;
}
// Here we would use strstr to find the comma
// Here we would use strstr to find the comma.
// However, the device-tree file may contain NULL
// bytes in the middle of the string, which would
// cause strstr to return NULL even when there might
// be an occurence after the NULL byte
//
// We iterate the string backwards to find the field
// in position n-DEVTREE_HARDWARE_FIELD where n
// is the number of fields.
int i=0;
char* tmp1 = buf;
char* tmp1 = buf+filelen-1;
do {
tmp1++;
tmp1--;
if(*tmp1 == ',') i++;
} while(filelen > (tmp1-buf) && i < DEVTREE_FIELD);
} while(tmp1 != buf && i <= DEVTREE_FIELD);
if(tmp1 == buf) {
printWarn("get_field_from_devtree: Unable to find field %d", DEVTREE_FIELD);
return NULL;
}
tmp1++;
if((tmp1-buf) >= filelen) return NULL;
int strlen = filelen-(tmp1-buf);
char* hardware = emalloc(sizeof(char) * strlen);
memset(hardware, 0, sizeof(char) * strlen);