summaryrefslogtreecommitdiff
path: root/src/vendorcode/google/chromeos/sar.c
blob: 895958860bb3fce063d3b9e79cc53d6b81d18979 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* SPDX-License-Identifier: GPL-2.0-only */

#include <cbfs.h>
#include <console/console.h>
#include <drivers/vpd/vpd.h>
#include <lib.h>
#include <sar.h>
#include <stdlib.h>
#include <string.h>
#include <types.h>

#define LEGACY_BYTES_PER_GEO_OFFSET	6
#define LEGACY_BYTES_PER_SAR_LIMIT	10
#define LEGACY_NUM_SAR_LIMITS		4
#define LEGACY_SAR_BIN_SIZE		81
#define LEGACY_SAR_WGDS_BIN_SIZE	119
#define LEGACY_SAR_NUM_WGDS_GROUPS	3

static uint8_t *wifi_hextostr(const char *sar_str, size_t str_len, size_t *sar_bin_len,
			      bool legacy_hex_format)
{
	uint8_t *sar_bin = NULL;
	size_t bin_len;

	if (!legacy_hex_format) {
		sar_bin = malloc(str_len);
		if (!sar_bin) {
			printk(BIOS_ERR, "ERROR: Failed to allocate space for SAR binary!\n");
			return NULL;
		}

		memcpy(sar_bin, sar_str, str_len);
		*sar_bin_len = str_len;
	} else {
		bin_len = ((str_len - 1) / 2);
		sar_bin = malloc(bin_len);
		if (!sar_bin) {
			printk(BIOS_ERR, "ERROR: Failed to allocate space for SAR binary!\n");
			return NULL;
		}

		if (hexstrtobin(sar_str, (uint8_t *)sar_bin, bin_len) != bin_len) {
			printk(BIOS_ERR, "ERROR: sar_limits contains non-hex value!\n");
			free(sar_bin);
			return NULL;
		}

		*sar_bin_len = bin_len;
	}

	return sar_bin;
}

static int sar_table_size(const struct sar_profile *sar)
{
	if (sar == NULL)
		return 0;

	return (sizeof(struct sar_profile) + ((1 + sar->dsar_set_count) * sar->chains_count *
					      sar->subbands_count));
}

static int wgds_table_size(const struct geo_profile *geo)
{
	if (geo == NULL)
		return 0;

	return sizeof(struct geo_profile) + (geo->chains_count * geo->bands_count);
}

static int gain_table_size(const struct gain_profile *gain)
{
	if (gain == NULL)
		return 0;

	return sizeof(struct gain_profile) + (gain->chains_count * gain->bands_count);
}

static int sar_avg_table_size(const struct avg_profile *sar_avg)
{
	if (sar_avg == NULL)
		return 0;

	return sizeof(struct avg_profile);
}

static int dsm_table_size(const struct dsm_profile *dsm)
{
	if (dsm == NULL)
		return 0;

	return sizeof(struct dsm_profile);
}

static bool valid_legacy_length(size_t bin_len)
{
	if (bin_len == LEGACY_SAR_WGDS_BIN_SIZE)
		return true;

	if (bin_len == LEGACY_SAR_BIN_SIZE && !CONFIG(GEO_SAR_ENABLE))
		return true;

	return false;
}

static int sar_header_size(void)
{
	return (MAX_PROFILE_COUNT * sizeof(uint16_t)) + sizeof(struct sar_header);
}

static int fill_wifi_sar_limits(union wifi_sar_limits *sar_limits, const uint8_t *sar_bin,
				size_t sar_bin_size)
{
	struct sar_header *header;
	size_t i = 0, expected_sar_bin_size;
	size_t header_size = sar_header_size();

	if (sar_bin_size < header_size) {
		printk(BIOS_ERR, "ERROR: Invalid SAR format!\n");
		return -1;
	}

	header = (struct sar_header *)sar_bin;

	if (header->version != SAR_FILE_REVISION) {
		printk(BIOS_ERR, "ERROR: Invalid SAR file version: %d!\n", header->version);
		return -1;
	}

	for (i = 0; i < MAX_PROFILE_COUNT; i++) {
		if (header->offsets[i] > sar_bin_size) {
			printk(BIOS_ERR, "ERROR: Offset is outside the file size!\n");
			return -1;
		}

		if (header->offsets[i])
			sar_limits->profile[i] = (void *) (sar_bin + header->offsets[i]);
	}

	expected_sar_bin_size = header_size;
	expected_sar_bin_size += sar_table_size(sar_limits->sar);
	expected_sar_bin_size += wgds_table_size(sar_limits->wgds);
	expected_sar_bin_size += gain_table_size(sar_limits->ppag);
	expected_sar_bin_size += sar_avg_table_size(sar_limits->wtas);
	expected_sar_bin_size += dsm_table_size(sar_limits->dsm);

	if (sar_bin_size != expected_sar_bin_size) {
		printk(BIOS_ERR, "ERROR: Invalid SAR size, expected: %ld, obtained: %ld\n",
		       expected_sar_bin_size, sar_bin_size);
		return -1;
	}

	return 0;
}

static int fill_wifi_sar_limits_legacy(union wifi_sar_limits *sar_limits,
				       const uint8_t *sar_bin, size_t sar_bin_size)
{
	uint8_t *new_sar_bin;
	size_t size = sar_bin_size + sizeof(struct sar_profile);

	if (CONFIG(GEO_SAR_ENABLE))
		size += sizeof(struct geo_profile);

	new_sar_bin = malloc(size);
	if (!new_sar_bin) {
		printk(BIOS_ERR, "ERROR: Failed to allocate space for SAR binary!\n");
		return -1;
	}

	sar_limits->sar = (struct sar_profile *) new_sar_bin;
	sar_limits->sar->revision = 0;
	sar_limits->sar->dsar_set_count = CONFIG_DSAR_SET_NUM;
	sar_limits->sar->chains_count = SAR_REV0_CHAINS_COUNT;
	sar_limits->sar->subbands_count = SAR_REV0_SUBBANDS_COUNT;
	memcpy(&sar_limits->sar->sar_table, sar_bin,
	       LEGACY_BYTES_PER_SAR_LIMIT * LEGACY_NUM_SAR_LIMITS);

	if (!CONFIG(GEO_SAR_ENABLE))
		return 0;

	sar_limits->wgds = (struct geo_profile *)(new_sar_bin +
						  sar_table_size(sar_limits->sar));
	sar_limits->wgds->revision = 0;
	sar_limits->wgds->chains_count = LEGACY_SAR_NUM_WGDS_GROUPS;
	sar_limits->wgds->bands_count = LEGACY_BYTES_PER_GEO_OFFSET;
	memcpy(&sar_limits->wgds->wgds_table,
	       sar_bin + LEGACY_BYTES_PER_SAR_LIMIT * LEGACY_NUM_SAR_LIMITS + REVISION_SIZE,
	       LEGACY_BYTES_PER_GEO_OFFSET * LEGACY_SAR_NUM_WGDS_GROUPS);

	return 0;
}

/*
 * Retrieve WiFi SAR limits data from CBFS and decode it
 * Legacy WiFi SAR data is expected in the format: [<WRDD><EWRD>][WGDS]
 *
 * [<WRDD><EWRD>] = NUM_SAR_LIMITS * BYTES_PER_SAR_LIMIT bytes.
 * [WGDS]=[WGDS_REVISION][WGDS_DATA]
 *
 * Current SAR configuration data is expected in the format:
 * "$SAR" Marker
 * Version
 * Offset count
 * Offsets
 * [SAR_REVISION,DSAR_SET_COUNT,CHAINS_COUNT,SUBBANDS_COUNT <WRDD>[EWRD]]
 * [WGDS_REVISION,CHAINS_COUNT,SUBBANDS_COUNT<WGDS_DATA>]
 * [PPAG_REVISION,MODE,CHAINS_COUNT,SUBBANDS_COUNT<PPAG_DATA>]
 * [WTAS_REVISION, WTAS_DATA]
 * [DSM_RETURN_VALUES]
 *
 * The configuration data will always have the revision added in the file for each of the
 * block, based on the revision number and validity, size of the specific block will be
 * calculated.
 *
 * [WGDS_DATA] = [GROUP#0][GROUP#1][GROUP#2]
 *
 * [GROUP#<i>] =
 *	Supported by Revision 0, 1 and 2
 *              [2.4Ghz - Max Allowed][2.4Ghz - Chain A Offset][2.4Ghz - Chain B Offset]
 *              [5Ghz - Max Allowed][5Ghz - Chain A Offset][5Ghz - Chain B Offset]
 *	Supported by Revision 1 and 2
 *              [6Ghz - Max Allowed][6Ghz - Chain A Offset][6Ghz - Chain B Offset]
 *
 * [GROUP#0] is for FCC
 * [GROUP#1] is for Europe/Japan
 * [GROUP#2] is for ROW
 *
 * [PPAG_DATA] = [ANT_gain Table Chain A] [ANT_gain Table Chain A]
 *
 * [ANT_gain Table] =
 *	Supported by Revision 0, 1 and 2
 *              [Antenna gain used for 2400MHz frequency]
 *              [Antenna gain used for 5150-5350MHz frequency]
 *              [Antenna gain used for 5350-5470MHz frequency]
 *              [Antenna gain used for 5470-5725MHz frequency]
 *              [Antenna gain used for 5725-5945MHz frequency]
 *	Supported by Revision 1 and 2
 *              [Antenna gain used for 5945-6165MHz frequency]
 *              [Antenna gain used for 6165-6405MHz frequency]
 *              [Antenna gain used for 6405-6525MHz frequency]
 *              [Antenna gain used for 6525-6705MHz frequency]
 *              [Antenna gain used for 6705-6865MHz frequency]
 *              [Antenna gain used for 6865-7105MHz frequency]
 *
 * [WTAS_DATA] =
 *      [Enable/disable the TAS feature]
 *      [Number of blocked countries that are not approved by the OEM to support this feature]
 *      [deny_list_entry_<1-16>: ISO country code to block]
 */
int get_wifi_sar_limits(union wifi_sar_limits *sar_limits)
{
	const char *filename;
	size_t sar_bin_len, sar_str_len;
	uint8_t *sar_bin;
	char *sar_str;
	int ret = -1;
	bool legacy_hex_format = false;

	filename = get_wifi_sar_cbfs_filename();
	if (filename == NULL) {
		printk(BIOS_ERR, "ERROR: Filename missing for CBFS SAR file!\n");
		return ret;
	}

	sar_str = cbfs_map(filename, &sar_str_len);
	if (!sar_str) {
		printk(BIOS_ERR, "ERROR: Failed to get the %s file size!\n", filename);
		return ret;
	}

	if (strncmp(sar_str, SAR_STR_PREFIX, SAR_STR_PREFIX_SIZE) == 0) {
		legacy_hex_format = false;
	} else if (valid_legacy_length(sar_str_len)) {
		legacy_hex_format = true;
	} else {
		printk(BIOS_ERR, "ERROR: Invalid SAR format!\n");
		goto error;
	}

	sar_bin = wifi_hextostr(sar_str, sar_str_len, &sar_bin_len, legacy_hex_format);
	if (sar_bin == NULL) {
		printk(BIOS_ERR, "ERROR: Failed to parse SAR file %s\n", filename);
		goto error;
	}

	memset(sar_limits, 0, sizeof(*sar_limits));
	if (legacy_hex_format) {
		ret = fill_wifi_sar_limits_legacy(sar_limits, sar_bin, sar_bin_len);
		free(sar_bin);
	} else {
		ret = fill_wifi_sar_limits(sar_limits, sar_bin, sar_bin_len);
		if (ret < 0)
			free(sar_bin);
	}

error:
	cbfs_unmap(sar_str);
	return ret;
}

__weak
const char *get_wifi_sar_cbfs_filename(void)
{
	return WIFI_SAR_CBFS_DEFAULT_FILENAME;
}