1 /*
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
is_fmap(uint8_t * ptr)12 static 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
27 /* Find and point to the FMAP header within the buffer */
fmap_find(uint8_t * ptr,size_t size)28 FmapHeader *fmap_find(uint8_t *ptr, size_t size)
29 {
30 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
43 return NULL;
44 }
45
46 /* Search for an area by name, return pointer to its beginning */
fmap_find_by_name(uint8_t * ptr,size_t size,FmapHeader * fmap,const char * name,FmapAreaHeader ** ah_ptr)47 uint8_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;
67 }
68