summaryrefslogtreecommitdiff
path: root/aoc_firmware.c
blob: 9e5bcd78ee1038d9afb90d7c0073df7878d4cfba (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Google Whitechapel AoC Firmware Loading Support
 *
 * Copyright (c) 2019 Google LLC
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
#define pr_fmt(fmt) "aoc-fw: " fmt

#include <linux/slab.h>
#include <linux/string.h>
#include <linux/types.h>
#include <linux/uaccess.h>

#include "aoc.h"
#include "aoc_firmware.h"
#include "aoc-interface.h"

struct aoc_superbin_header {
	u32 magic;
	u32 release_type;
	u32 firmware_version;
	u32 image_size;
	u32 bootloader_low;
	u32 bootloader_high;
	u32 bootloader_offset;
	u32 bootloader_size;
	u32 uuid_table_offset;
	u32 uuid_table_size;
	u8 date_time[20];
	u32 section_table_offset;
	u32 section_table_entry_size;
	u32 sram_offset;
	u32 a32_data_offset;
	u32 a32_data_size;
	u32 ff1_data_offset;
	u32 ff1_data_size;
	u32 hifi3z_data_offset;
	u32 hifi3z_data_size;
	u32 crc32;
} __packed;

static bool region_is_in_firmware(size_t start, size_t length,
				  const struct firmware *fw)
{
	return ((start + length) < fw->size);
}

bool _aoc_fw_is_valid(const struct firmware *fw)
{
	const struct aoc_superbin_header *header;
	u32 ipc_offset, bootloader_offset;
	u32 uuid_offset, uuid_size;

	if (!fw || !fw->data)
		return false;

	if (!region_is_in_firmware(0, sizeof(*header), fw))
		return false;

	header = (const struct aoc_superbin_header *)fw->data;
	if (le32_to_cpu(header->magic) != 0xaabbccdd)
		return false;

	/* Validate that the AoC firmware recognizes the messages known at
	 * compile time
	 */

	uuid_offset = le32_to_cpu(header->uuid_table_offset);
	uuid_size = le32_to_cpu(header->uuid_table_size);

	if (!region_is_in_firmware(uuid_offset, uuid_size, fw)) {
		pr_err("invalid method signature region\n");
		return false;
	}

	ipc_offset = _aoc_fw_bootloader_offset(fw);
	bootloader_offset = _aoc_fw_bootloader_offset(fw);

	/* The bootloader resides within the FW image, so make sure
	 * that value makes sense
	 */
	if (!region_is_in_firmware(bootloader_offset,
				   le32_to_cpu(header->bootloader_size), fw))
		return false;

	return true;
}

bool _aoc_fw_is_release(const struct firmware *fw)
{
	const struct aoc_superbin_header *header =
		(const struct aoc_superbin_header *)fw->data;

	return (le32_to_cpu(header->release_type) == 1);
}

bool _aoc_fw_is_compatible(const struct firmware *fw)
{
	const struct aoc_superbin_header *header =
		(const struct aoc_superbin_header *)fw->data;
	u32 uuid_offset, uuid_size;

	if (!_aoc_fw_is_release(fw))
		return true;

	uuid_offset = le32_to_cpu(header->uuid_table_offset);
	uuid_size = le32_to_cpu(header->uuid_table_size);

	if (AocInterfaceCheck(fw->data + uuid_offset, uuid_size) != 0) {
		pr_err("failed to validate method signature table\n");
		return false;
	}

	return true;
}

u32 _aoc_fw_bootloader_offset(const struct firmware *fw)
{
	const struct aoc_superbin_header *header =
		(const struct aoc_superbin_header *)fw->data;
	return le32_to_cpu(header->bootloader_offset);
}

u32 _aoc_fw_ipc_offset(const struct firmware *fw)
{
	const struct aoc_superbin_header *header =
		(const struct aoc_superbin_header *)fw->data;
	return le32_to_cpu(header->image_size);
}

/* Returns firmware version, or 0 on error */
u32 _aoc_fw_version(const struct firmware *fw)
{
	const struct aoc_superbin_header *header =
		(const struct aoc_superbin_header *)fw->data;
	return le32_to_cpu(header->firmware_version);
}

/* Returns firmware build date, or NULL on error */
const char *_aoc_fw_builddate(const struct firmware *fw)
{
	const struct aoc_superbin_header *header =
		(const struct aoc_superbin_header *)fw->data;
	size_t field_size = sizeof(header->date_time);
	const char *date = &header->date_time[0];

	if (strnlen(date, field_size) < field_size)
		return date;

	return NULL;
}

bool _aoc_fw_commit(const struct firmware *fw, void *dest)
{
	if (!_aoc_fw_is_valid(fw))
		return false;

	memcpy(dest, fw->data, fw->size);
	return true;
}