blob: 348973e6d72752d9199479d9d4ee0d0cffe8ecea [file] [log] [blame]
Andrew Jeffery4fe996c2018-02-27 12:16:48 +10301// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2018 IBM Corp.
Andrew Jeffery41f211b2017-04-12 14:08:03 +09303#define _GNU_SOURCE
4#include <stdbool.h>
5#include <stdio.h>
6#include <string.h>
7
8#include "common.h"
9
10static bool is_pnor_part(const char *str)
11{
12 return strcasestr(str, "pnor") != NULL;
13}
14
15char *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}