xref: /openbmc/phosphor-mboxd/mtd.c (revision acee6839)
1 /*
2  * Mailbox Daemon Implementation
3  *
4  * Copyright 2018 IBM
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19 #define _GNU_SOURCE
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include <string.h>
23 
24 #include "common.h"
25 
26 static bool is_pnor_part(const char *str)
27 {
28 	return strcasestr(str, "pnor") != NULL;
29 }
30 
31 char *get_dev_mtd(void)
32 {
33 	FILE *f;
34 	char *ret = NULL, *pos = NULL;
35 	char line[255];
36 
37 	f = fopen("/proc/mtd", "r");
38 	if (!f)
39 		return NULL;
40 
41 	while (!pos && fgets(line, sizeof(line), f) != NULL) {
42 		/* Going to have issues if we didn't get the full line */
43 		if (line[strlen(line) - 1] != '\n')
44 			break;
45 
46 		if (is_pnor_part(line)) {
47 			pos = strchr(line, ':');
48 			if (!pos)
49 				break;
50 		}
51 	}
52 	fclose(f);
53 
54 	if (pos) {
55 		*pos = '\0';
56 		if (asprintf(&ret, "/dev/%s", line) == -1)
57 			ret = NULL;
58 	}
59 
60 	return ret;
61 }
62