xref: /openbmc/hiomapd/mtd.c (revision acdbdd14)
1 // SPDX-License-Identifier: Apache-2.0
2 // Copyright (C) 2018 IBM Corp.
3 #define _GNU_SOURCE
4 #include <stdbool.h>
5 #include <stdio.h>
6 #include <string.h>
7 
8 #include "common.h"
9 
is_pnor_part(const char * str)10 static bool is_pnor_part(const char *str)
11 {
12 	return strcasestr(str, "pnor") != NULL;
13 }
14 
get_dev_mtd(void)15 char *get_dev_mtd(void)
16 {
17 	FILE *f;
18 	char *ret = NULL, *pos = NULL;
19 	char line[255];
20 
21 	f = fopen("/proc/mtd", "r");
22 	if (!f)
23 		return NULL;
24 
25 	while (!pos && fgets(line, sizeof(line), f) != NULL) {
26 		/* Going to have issues if we didn't get the full line */
27 		if (line[strlen(line) - 1] != '\n')
28 			break;
29 
30 		if (is_pnor_part(line)) {
31 			pos = strchr(line, ':');
32 			if (!pos)
33 				break;
34 		}
35 	}
36 	fclose(f);
37 
38 	if (pos) {
39 		*pos = '\0';
40 		if (asprintf(&ret, "/dev/%s", line) == -1)
41 			ret = NULL;
42 	}
43 
44 	return ret;
45 }
46