1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "nvme-models.h"
static char *_fmt1 = "/sys/class/nvme/nvme%d/device/subsystem_vendor";
static char *_fmt2 = "/sys/class/nvme/nvme%d/device/subsystem_device";
static char *_fmt3 = "/sys/class/nvme/nvme%d/device/vendor";
static char *_fmt4 = "/sys/class/nvme/nvme%d/device/device";
static char *_fmt5 = "/sys/class/nvme/nvme%d/device/class";
static char fmt1[78];
static char fmt2[78];
static char fmt3[78];
static char fmt4[78];
static char fmt5[78];
static char *device_top;
static char *device_mid;
static char *device_final;
static char *class_top;
static char *class_mid;
static char *class_final;
static void free_all(void)
{
free(device_top);
device_top = NULL;
free(device_mid);
device_mid = NULL;
free(device_final);
device_final = NULL;
free(class_top);
class_top = NULL;
free(class_mid);
class_mid = NULL;
free(class_final);
class_final = NULL;
}
static char *find_data(char *data)
{
while (*data != '\0') {
if (*data >= '0' && *data <= '9')
return data;
data++;
}
return NULL;
}
static char *locate_info(char *data, bool is_inner, bool is_class)
{
char *orig = data;
char *locate;
if (!data)
return orig;
locate = find_data(data);
if (!locate)
return orig;
if (is_class)
return locate + 4;
if (!is_inner)
/* 4 to get over the number, 2 for spaces */
return locate + 4 + 2;
/* Inner data, has "sub_ven(space)sub_dev(space)(space)string */
return locate + 4 + 1 + 4 + 2;
}
static void format_and_print(char *save)
{
if (!class_mid) {
if (device_final)
snprintf(save, 1024, "%s %s %s",
locate_info(device_top, false, false),
locate_info(device_mid, false, false),
locate_info(device_final, true, false));
else
snprintf(save, 1024, "%s %s",
locate_info(device_top, false, false),
locate_info(device_mid, false, false));
} else {
if (device_final)
snprintf(save, 1024, "%s: %s %s %s",
locate_info(class_mid, false, true),
locate_info(device_top, false, false),
locate_info(device_mid, false, false),
locate_info(device_final, true, false));
else
snprintf(save, 1024, "%s: %s %s",
locate_info(class_mid, false, true),
locate_info(device_top, false, false),
locate_info(device_mid, false, false));
}
}
static void format_all(char *save, char *vendor, char *device)
{
if (device_top && device_mid)
format_and_print(save);
else if (device_top && !device_mid && class_mid)
snprintf(save, 1024, "%s: %s Device %s",
locate_info(class_mid, false, true),
locate_info(device_top, false, false),
device);
else if (!device_top && class_mid)
snprintf(save, 1024, "%s: Vendor %s Device %s",
locate_info(class_mid, false, true),
vendor,
device);
else
snprintf(save, 1024, "Unknown device");
}
static int is_final_match(char *line, char *search)
{
return !memcmp(&line[2], search, 2);
}
static int is_inner_sub_vendev(char *line, char *search, char *search2)
{
char combine[10];
snprintf(combine, sizeof(combine), "%s %s", &search[2], &search2[2]);
if (line[0] != '\t' && line[1] != '\t')
return 0;
return !memcmp(combine, &line[2], 9);
}
static int is_mid_level_match(char *line, char *device, bool class)
{
if (!class)
return !memcmp(&line[1], &device[2], 4);
return !memcmp(&line[1], device, 2);
}
static inline bool is_comment(char *line)
{
return line[0] == '#';
}
static int is_top_level_match(char *line, const char* device, bool class)
{
if (line[0] == '\t')
return false;
if (line[0] == '#')
return false;
if (!class)
return !memcmp(line, &device[2], 4);
if (line[0] != 'C')
return false;
/* Skipping C(SPACE) 0x */
return !memcmp(&line[2], &device[2], 2);
}
static inline int is_tab(char *line)
{
return line[0] == '\t';
}
static inline int is_class_info(char *line)
{
return !memcmp(line, "# C class", 9);
}
static void parse_vendor_device(char **line, FILE *file,
char *device, char *subdev,
char *subven)
{
bool device_single_found = false;
size_t amnt = 1024;
size_t found = 0;
char *newline;
while ((found = getline(line, &amnt, file)) != -1) {
newline = *line;
if (is_comment(newline))
continue;
if (!is_tab(newline))
return;
newline[found - 1] = '\0';
if (!device_single_found && is_mid_level_match(newline, device, false)) {
device_single_found = true;
device_mid = strdup(newline);
continue;
}
if (device_single_found && is_inner_sub_vendev(newline, subven, subdev)) {
device_final = strdup(newline);
break;
}
}
}
static void pull_class_info(char **_newline, FILE *file, char *class)
{
size_t amnt;
size_t size = 1024;
bool top_found = false;
bool mid_found = false;
char *newline;
while ((amnt = getline(_newline, &size, file)) != -1) {
newline = *_newline;
newline[amnt - 1] = '\0';
if (!top_found && is_top_level_match(newline, class, true)) {
class_top = strdup(newline);
top_found = true;
continue;
}
if (!mid_found && top_found &&
is_mid_level_match(newline, &class[4], true)) {
class_mid = strdup(newline);
mid_found = true;
continue;
}
if (top_found && mid_found &&
is_final_match(newline, &class[6])) {
class_final = strdup(newline);
break;
}
}
}
static int read_sys_node(char *where, char *save, size_t savesz)
{
char *new;
int fd, ret = 0, len;
fd = open(where, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "Failed to open %s with errno %s\n",
where, strerror(errno));
return 1;
}
/* -1 so we can safely use strstr below */
len = read(fd, save, savesz - 1);
if (!len)
ret = 1;
else {
save[len] = '\0';
new = strstr(save, "\n");
if (new)
new[0] = '\0';
}
close(fd);
return ret;
}
static FILE *open_pci_ids(void)
{
int i;
char *pci_ids_path;
FILE *fp;
const char* pci_ids[] = {
"/usr/share/hwdata/pci.ids", /* RHEL */
"/usr/share/pci.ids", /* SLES */
"/usr/share/misc/pci.ids", /* Ubuntu */
NULL
};
/* First check if user gave pci ids in environment */
if ((pci_ids_path = getenv("PCI_IDS_PATH")) != NULL) {
if ((fp = fopen(pci_ids_path, "r")) != NULL) {
return fp;
} else {
/* fail if user provided environment variable but could not open */
perror(pci_ids_path);
return NULL;
}
}
/* NO environment, check in predefined places */
for (i = 0; pci_ids[i] != NULL; i++) {
if ((fp = fopen(pci_ids[i], "r")) != NULL)
return fp;
}
fprintf(stderr, "Could not find pci.ids file\n");
return NULL;
}
char *nvme_product_name(int id)
{
char *line = NULL;
ssize_t amnt;
char vendor[7] = { 0 };
char device[7] = { 0 };
char sub_device[7] = { 0 };
char sub_vendor[7] = { 0 };
char class[13] = { 0 };
size_t size = 1024;
char ret;
FILE *file = open_pci_ids();
if (!file)
goto error1;
snprintf(fmt1, 78, _fmt1, id);
snprintf(fmt2, 78, _fmt2, id);
snprintf(fmt3, 78, _fmt3, id);
snprintf(fmt4, 78, _fmt4, id);
snprintf(fmt5, 78, _fmt5, id);
ret = read_sys_node(fmt1, sub_vendor, 7);
ret |= read_sys_node(fmt2, sub_device, 7);
ret |= read_sys_node(fmt3, vendor, 7);
ret |= read_sys_node(fmt4, device, 7);
ret |= read_sys_node(fmt5, class, 13);
if (ret)
goto error0;
line = malloc(1024);
if (!line) {
fprintf(stderr, "malloc: %s\n", strerror(errno));
goto error0;
}
while ((amnt = getline(&line, &size, file)) != -1) {
if (is_comment(line) && !is_class_info(line))
continue;
if (is_top_level_match(line, vendor, false)) {
line[amnt - 1] = '\0';
free(device_top);
device_top = strdup(line);
parse_vendor_device(&line, file,
device,
sub_device,
sub_vendor);
}
if (is_class_info(line))
pull_class_info(&line, file, class);
}
fclose(file);
format_all(line, vendor, device);
free_all();
return line;
error0:
fclose(file);
error1:
return strdup("NULL");
}
|