blob: c95338db43377ccea95573ef616a31a2adb8d726 [file] [log] [blame]
Che-Liang Chiou305e9e52011-02-17 17:56:16 +08001/*
2 * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
7#include <stdio.h>
8#include <string.h>
9
10#include "fmap.h"
11
Julius Werner75efe872015-01-14 11:41:45 -080012static int is_fmap(uint8_t *ptr)
13{
14 FmapHeader *fmap_header = (FmapHeader *)ptr;
15
16 if (0 != memcmp(ptr, FMAP_SIGNATURE, FMAP_SIGNATURE_SIZE))
17 return 0;
18
19 if (fmap_header->fmap_ver_major == FMAP_VER_MAJOR)
20 return 1;
21
22 fprintf(stderr, "Found FMAP, but major version is %u instead of %u\n",
23 fmap_header->fmap_ver_major, FMAP_VER_MAJOR);
24 return 0;
25}
26
Bill Richardsonc0777be2014-08-27 15:42:24 -070027/* Find and point to the FMAP header within the buffer */
28FmapHeader *fmap_find(uint8_t *ptr, size_t size)
Che-Liang Chiou305e9e52011-02-17 17:56:16 +080029{
Julius Werner75efe872015-01-14 11:41:45 -080030 ssize_t offset, align;
31 ssize_t lim = size - sizeof(FmapHeader);
32
33 if (lim >= 0 && is_fmap(ptr))
34 return (FmapHeader *)ptr;
35
36 /* Search large alignments before small ones to find "right" FMAP. */
37 for (align = FMAP_SEARCH_STRIDE; align <= lim; align *= 2);
38 for (; align >= FMAP_SEARCH_STRIDE; align /= 2)
39 for (offset = align; offset <= lim; offset += align * 2)
40 if (is_fmap(ptr + offset))
41 return (FmapHeader *)(ptr + offset);
42
Bill Richardsonc0777be2014-08-27 15:42:24 -070043 return NULL;
Che-Liang Chiou305e9e52011-02-17 17:56:16 +080044}
45
Bill Richardsonc0777be2014-08-27 15:42:24 -070046/* Search for an area by name, return pointer to its beginning */
47uint8_t *fmap_find_by_name(uint8_t *ptr, size_t size, FmapHeader *fmap,
48 const char *name, FmapAreaHeader **ah_ptr)
49{
50 int i;
51 FmapAreaHeader *ah;
52
53 if (!fmap)
54 fmap = fmap_find(ptr, size);
55 if (!fmap)
56 return NULL;
57
58 ah = (FmapAreaHeader*)((void *)fmap + sizeof(FmapHeader));
59 for (i = 0; i < fmap->fmap_nareas; i++)
60 if (!strncmp(ah[i].area_name, name, FMAP_NAMELEN)) {
61 if (ah_ptr)
62 *ah_ptr = ah + i;
63 return ptr + ah[i].area_offset;
64 }
65
66 return NULL;
Che-Liang Chiou305e9e52011-02-17 17:56:16 +080067}