diff options
Diffstat (limited to 'cgpt/cgpt_find.c')
-rw-r--r-- | cgpt/cgpt_find.c | 50 |
1 files changed, 46 insertions, 4 deletions
diff --git a/cgpt/cgpt_find.c b/cgpt/cgpt_find.c index c3193040..1a516d8c 100644 --- a/cgpt/cgpt_find.c +++ b/cgpt/cgpt_find.c @@ -8,6 +8,7 @@ #include <unistd.h> #include "cgpt.h" +#include "cgpt_nor.h" #include "cgptlib_internal.h" #include "vboot_host.h" @@ -145,6 +146,7 @@ static int do_search(CgptFindParams *params, char *fileName) { } +#define PROC_MTD "/proc/mtd" #define PROC_PARTITIONS "/proc/partitions" #define DEV_DIR "/dev" #define SYS_BLOCK_DIR "/sys/block" @@ -186,23 +188,23 @@ static char *is_wholedev(const char *basename) { return 0; } - // This scans all the physical devices it can find, looking for a match. It // returns true if any matches were found, false otherwise. static int scan_real_devs(CgptFindParams *params) { int found = 0; - char line[BUFSIZE]; char partname[128]; // max size for /proc/partition lines? FILE *fp; char *pathname; - fp = fopen(PROC_PARTITIONS, "r"); + fp = fopen(PROC_PARTITIONS, "re"); if (!fp) { perror("can't read " PROC_PARTITIONS); return found; } - while (fgets(line, sizeof(line), fp)) { + size_t line_length = 0; + char *line = NULL; + while (getline(&line, &line_length, fp) != -1) { int ma, mi; long long unsigned int sz; @@ -217,6 +219,46 @@ static int scan_real_devs(CgptFindParams *params) { } fclose(fp); + + fp = fopen(PROC_MTD, "re"); + if (!fp) { + free(line); + return found; + } + + while (getline(&line, &line_length, fp) != -1) { + uint64_t sz; + uint32_t erasesz; + char name[128]; + // dev: size erasesize name + if (sscanf(line, "%64[^:]: %" PRIx64 " %x \"%127[^\"]\"", + partname, &sz, &erasesz, name) != 4) + continue; + if (strcmp(partname, "mtd0") == 0) { + char temp_dir[] = "/tmp/cgpt_find.XXXXXX"; + if (params->drive_size == 0) { + if (GetMtdSize("/dev/mtd0", ¶ms->drive_size) != 0) { + perror("GetMtdSize"); + goto cleanup; + } + } + if (ReadNorFlash(temp_dir) != 0) { + perror("ReadNorFlash"); + goto cleanup; + } + char nor_file[64]; + if (snprintf(nor_file, sizeof(nor_file), "%s/rw_gpt", temp_dir) > 0) { + if (do_search(params, nor_file)) { + found++; + } + } + RemoveDir(temp_dir); + break; + } + } +cleanup: + fclose(fp); + free(line); return found; } |