diff options
author | Pascal Terjan <pterjan@mageia.org> | 2020-06-12 18:03:11 +0000 |
---|---|---|
committer | Pascal Terjan <pterjan@mageia.org> | 2020-06-12 18:03:11 +0000 |
commit | 8a58fe8a571bda32f1d3db475922c9c9b15d66ae (patch) | |
tree | 890dc2d21e3f744028b915f8b510cfea82be22e1 | |
parent | 0272d101c2c5aeb73dd682e17eb87fcb786a9909 (diff) | |
download | drakx-8a58fe8a571bda32f1d3db475922c9c9b15d66ae.tar drakx-8a58fe8a571bda32f1d3db475922c9c9b15d66ae.tar.gz drakx-8a58fe8a571bda32f1d3db475922c9c9b15d66ae.tar.bz2 drakx-8a58fe8a571bda32f1d3db475922c9c9b15d66ae.tar.xz drakx-8a58fe8a571bda32f1d3db475922c9c9b15d66ae.zip |
Simplify NVME code
-rw-r--r-- | mdk-stage1/probing.c | 112 |
1 files changed, 45 insertions, 67 deletions
diff --git a/mdk-stage1/probing.c b/mdk-stage1/probing.c index 5850f95c5..c140d2604 100644 --- a/mdk-stage1/probing.c +++ b/mdk-stage1/probing.c @@ -46,6 +46,7 @@ #include <sys/mount.h> #include <pci/pci.h> #include <libldetect.h> +#include <errno.h> #include <glob.h> #include "stage1.h" @@ -538,6 +539,33 @@ void probe_that_type(enum driver_type type, enum media_bus bus __attribute__ ((u static struct media_info * medias = NULL; +// Read a short string from a file and strips it, intended for sysfs attributes +static ssize_t read_attribute(char *path, char *buf) { + ssize_t l = 0; + int fd = open(path, O_RDONLY); + buf[0] = '\0'; + if (fd == -1) { + log_message("Failed to open %s for reading", path); + } else { + ssize_t n = read(fd, buf, 32); + if (n == -1) { + log_message("Couldn't read file (%s)", path); + } else { + // Strip whitespaces and newline + for (int i = n-1; i >= 0; i--) { + if (buf[i] == '\n' || buf[i] == ' ') + continue; + l = i+1; + break; + } + buf[l] = '\0'; + } + close(fd); + } + log_message("Content of %s was %s", path, buf); + return l; +} + void find_media(enum media_bus bus) { char b[50]; @@ -883,83 +911,33 @@ void find_media(enum media_bus bus) * */ log_message("looking for NVME"); { - int missing_vendor = 0; glob_t globbuf; - glob("/sys/block/nvme*/device/vendor", 0, NULL, &globbuf); - - if (globbuf.gl_pathc == 0) { - glob("/sys/block/nvme*/device/model", 0, NULL, &globbuf); - if (globbuf.gl_pathc != 0) { - missing_vendor = 1; - } - } + glob("/sys/block/nvme*", 0, NULL, &globbuf); for (int i = 0; i < globbuf.gl_pathc; i++) { - char *path = globbuf.gl_pathv[i]; - char *model = "Unknown NVME Disk"; + char *name, *pathend; + char path[64]; + char model[64]; int vendor_length = 0; - int fd; - // Get device name by skipping /sys/block/ - char *name_end = strchr(path + 11, '/'); - if (!name_end) { - log_message("Strange path from glob: %s", path); - continue; - } + strncpy(path, globbuf.gl_pathv[i], sizeof(path)); + name = strdup(path + 11); + pathend = path + strlen(path); - // Read vendor name - if (!missing_vendor) { - fd = open(path, O_RDONLY); - buf[0] = '\0'; - if (fd == -1) { - log_message("Failed to open %s for reading", path); - } else { - int n = read(fd, buf, sizeof(buf)); - if (n == -1) { - log_message("Couldn't read vendor file (%s)", path); - } else { - // Strip whitespaces and newline - for (int j = n-1; j >= 0; j--) { - if (buf[j] == '\n' || buf[j] == ' ') - continue; - vendor_length = j+1; - break; - } - buf[vendor_length] = ' '; - buf[vendor_length+1] = '\0'; - vendor_length++; - } - close(fd); - } - } + strcpy(model, "Unknown NVME Disk"); - // Read model name + strcpy(pathend, "/device/vendor"); + vendor_length = read_attribute(path, model); - // Filename can be model or vendor, simpler to use same code in both cases - strcpy(buf, path); - char *dirpath = strrchr(buf, '/'); - strcpy(dirpath+1, "model"); - fd = open(path, O_RDONLY); - if (fd == -1) { - log_message("Failed to open %s for reading", path); - } else { - int n = read(fd, buf+vendor_length, sizeof(buf)-vendor_length); - if (n == -1) { - log_message("Couldn't read model file (%s)", path); - } else { - // Strip whitespaces and newline - for (int j = n-1; j >= 0; j--) { - if (buf[vendor_length+j] == '\n' || buf[vendor_length+j] == ' ') - continue; - buf[vendor_length+j+1] = '\0'; - break; - } - model = buf; - } - close(fd); + if (vendor_length) { + strcat(model, " "); + vendor_length++; } - tmp[count].name = strndup(path + 11, name_end - path - 11); + strcpy(pathend, "/device/model"); + read_attribute(path, model+vendor_length); + + tmp[count].name = name; tmp[count].type = DISK; tmp[count].model = strdup(model); count++; |