dm: mei: add sysfs read functions
mei requires reading of u8, u32 and uuid
sysfs files.
Acked-by: Wang, Yu1
#include "mei.h"
+#ifndef BIT
+#define BIT(x) (1 << (x))
+#endif
+
+#define DEV_NAME_SIZE sizeof(((struct dirent *)0)->d_name)
+
#ifndef UUID_STR_LEN
#define UUID_STR_LEN 37
#endif
@@ -146,3 +152,72 @@ refcnt_put(const struct refcnt *ref)
if (__sync_sub_and_fetch((int *)&ref->count, 1) == 0)
ref->destroy(ref);
}
+
+static int mei_sysfs_read_property_file(const char *fname, char *buf, size_t sz)
+{
+ int fd;
+ int rc;
+
+ if (!buf)
+ return -EINVAL;
+
+ if (!sz)
+ return 0;
+
+ fd = open(fname, O_RDONLY);
+ if (fd < 0) {
+ DPRINTF("open failed %s %d\n", fname, errno);
+ return -1;
+ }
+
+ rc = read(fd, buf, sz);
+
+ close(fd);
+
+ return rc;
+}
+
+static int mei_sysfs_read_property_u8(const char *fname, uint8_t *u8_property)
+{
+ char buf[4] = {0};
+ unsigned long int res;
+
+ if (mei_sysfs_read_property_file(fname, buf, sizeof(buf) - 1) < 0)
+ return -1;
+
+ res = strtoul(buf, NULL, 10);
+ if (res >= 256)
+ return -1;
+
+ *u8_property = (uint8_t)res;
+
+ return 0;
+}
+
+static int mei_sysfs_read_property_u32(const char *fname,
+ uint32_t *u32_property)
+{
+ char buf[32] = {0};
+ unsigned long int res;
+
+ if (mei_sysfs_read_property_file(fname, buf, sizeof(buf) - 1) < 0)
+ return -1;
+
+ res = strtoul(buf, NULL, 10);
+ if (res == ULONG_MAX)
+ return -1;
+
+ *u32_property = res;
+
+ return 0;
+}
+
+static int mei_sysfs_read_property_uuid(char *fname, guid_t *uuid)
+{
+ char buf[UUID_STR_LEN] = {0};
+
+ if (mei_sysfs_read_property_file(fname, buf, sizeof(buf) - 1) < 0)
+ return -1;
+
+ return guid_parse(buf, sizeof(buf), uuid);
+}
