Make a best-effort assessment if the given fmap is real */
| 38 | |
| 39 | /* Make a best-effort assessment if the given fmap is real */ |
| 40 | static int is_valid_fmap(const struct fmap *fmap) |
| 41 | { |
| 42 | if (memcmp(fmap, FMAP_SIGNATURE, strlen(FMAP_SIGNATURE)) != 0) |
| 43 | return 0; |
| 44 | /* strings containing the magic tend to fail here */ |
| 45 | if (fmap->ver_major != FMAP_VER_MAJOR) |
| 46 | return 0; |
| 47 | /* a basic consistency check: flash should be larger than fmap */ |
| 48 | if (le32toh(fmap->size) < |
| 49 | sizeof(*fmap) + le16toh(fmap->nareas) * sizeof(struct fmap_area)) |
| 50 | return 0; |
| 51 | |
| 52 | /* fmap-alikes along binary data tend to fail on having a valid, |
| 53 | * null-terminated string in the name field.*/ |
| 54 | int i = 0; |
| 55 | while (i < FMAP_STRLEN) { |
| 56 | if (fmap->name[i] == 0) |
| 57 | break; |
| 58 | if (!isgraph(fmap->name[i])) |
| 59 | return 0; |
| 60 | if (i == FMAP_STRLEN - 1) { |
| 61 | /* name is specified to be null terminated single-word string |
| 62 | * without spaces. We did not break in the 0 test, we know it |
| 63 | * is a printable spaceless string but we're seeing FMAP_STRLEN |
| 64 | * symbols, which is one too many. |
| 65 | */ |
| 66 | return 0; |
| 67 | } |
| 68 | i++; |
| 69 | } |
| 70 | return 1; |
| 71 | |
| 72 | } |
| 73 | |
| 74 | /* brute force linear search */ |
| 75 | static long int fmap_lsearch(const uint8_t *image, size_t len) |
no test coverage detected