summaryrefslogtreecommitdiff
path: root/futility/traversal.c
blob: 99cc477cace11dac786a645a6c5113aaaafbfa6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "fmap.h"
#include "file_type.h"
#include "futility.h"
#include "traversal.h"

/* These are the expected areas, in order of traversal */
struct bios_fmap_s bios_area[] = {
	{BIOS_FMAP_GBB,	       "GBB",         "GBB Area"},
	{BIOS_FMAP_FW_MAIN_A,  "FW_MAIN_A",   "Firmware A Data"},
	{BIOS_FMAP_FW_MAIN_B,  "FW_MAIN_B",   "Firmware B Data"},
	{BIOS_FMAP_VBLOCK_A,   "VBLOCK_A",    "Firmware A Key"},
	{BIOS_FMAP_VBLOCK_B,   "VBLOCK_B",    "Firmware B Key"},
};
BUILD_ASSERT(ARRAY_SIZE(bios_area) == NUM_BIOS_COMPONENTS);


void fmap_limit_area(FmapAreaHeader *ah, uint32_t len)
{
	uint32_t sum = ah->area_offset + ah->area_size;
	if (sum < ah->area_size || sum > len) {
		Debug("%s(%s) 0x%x + 0x%x > 0x%x\n",
		      __func__, ah->area_name,
		      ah->area_offset, ah->area_size, len);
		ah->area_offset = 0;
		ah->area_size = 0;
	}
}

enum futil_file_type ft_recognize_bios_image(uint8_t *buf, uint32_t len)
{
	FmapHeader *fmap;
	int i;

	fmap = fmap_find(buf, len);
	if (!fmap)
		return FILE_TYPE_UNKNOWN;

	for (i = 0; i < NUM_BIOS_COMPONENTS; i++)
		if (!fmap_find_by_name(buf, len, fmap,
				       bios_area[i].name, 0))
			break;
	if (i == NUM_BIOS_COMPONENTS)
		return FILE_TYPE_BIOS_IMAGE;

	for (i = 0; i < NUM_BIOS_COMPONENTS; i++)
		if (!fmap_find_by_name(buf, len, fmap,
				       bios_area[i].oldname, 0))
			break;
	if (i == NUM_BIOS_COMPONENTS)
		return FILE_TYPE_OLD_BIOS_IMAGE;

	return FILE_TYPE_UNKNOWN;
}