-
Notifications
You must be signed in to change notification settings - Fork 145
/
eeprom.c
70 lines (56 loc) · 1.49 KB
/
eeprom.c
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
/*
* XCOMM on-board calibration EEPROM methods.
*
* Copyright 2015 Analog Devices Inc.
*
* Licensed under the GPL-2.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <libgen.h>
#include "eeprom.h"
/* Recursively search a given path (defaulting to /sys) for an XCOMM compatible
* EEPROM file.
*
* If a matching EEPROM file is found the path is returned, otherwise returns
* NULL. Note that the string for the returned path is obtained with malloc and
* should be freed. It's up to the caller to free the allocated moemroy.
*/
const char *find_eeprom(const char *path)
{
char *eeprom_path = NULL;
char eeprom_names[512];
FILE *fp = NULL;
char cmd[512];
if (path == NULL) {
path = "/sys";
}
snprintf(cmd, sizeof(cmd), "find %s -name eeprom 2>/dev/null", path);
fp = popen(cmd, "r");
if (fp == NULL) {
perror("popen");
return NULL;
}
while (fgets(eeprom_names, sizeof(eeprom_names), fp) != NULL) {
struct stat eeprom_file;
char *__basename = NULL;
if (eeprom_names[strlen(eeprom_names) - 1] == '\n')
eeprom_names[strlen(eeprom_names) - 1] = '\0';
__basename = strdup(eeprom_names);
stat(eeprom_names, &eeprom_file);
if (S_ISREG(eeprom_file.st_mode) &&
!strcmp(basename(__basename), "eeprom") &&
eeprom_file.st_size == FAB_SIZE_FRU_EEPROM) {
eeprom_path = strdup(eeprom_names);
free(__basename);
break;
}
free(__basename);
}
pclose(fp);
return eeprom_path;
}