summaryrefslogtreecommitdiff
path: root/tools/bfapei/bfapei.c
blob: 73a421ceca01ceb798c059f023f0256423a0f53f (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
/*
 * bfapei.c: main program for the tool to make a Blob of APEI tables
 *
 * This file is subject to the terms and conditions of the GNU General
 * Public License.  See the file "COPYING" in the main directory of this
 * archive for more details.
 *
 * Copyright (c) 2013, Tomasz Nowicki <tomasz.nowicki@linaro.org>
 *
 * NB: all values are assumed to be little-endian in the blob.
 *
 */

#include <ctype.h>
#include <endian.h>
#include <errno.h>
#include <libgen.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/queue.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <build_aml.h>
#include <check_aml.h>

#include "bfapei.h"

/*
 * Overview of blob for APEI testing
 *
 *      +--------------------+ 0x0 base address, need to be obtained from HEST.
 *      |                    |
 *      |  place for status  | HEST and BERT reference to this region
 *      |  block error info  | during error servicing.
 *      |                    |
 *      +--------------------+ +0x100
 *      |                    |
 *      |  place for ERST    | ERST driver reference to this region during
 *      |  registers         | persistent error handling.
 *      |                    |
 *      +--------------------+ +0x200
 *      |                    |
 *
 *     ~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *      |                    |
 *      +--------------------+ +0x2000
 *      |                    |
 *      |  buffer for dmesg  | kernel log is dumping here during panic
 *      |  log               | when there is a kernel oops or panic
 *      |                    |
 *      +--------------------+ +0x12000
 */

static void usage(void) {
	printf("%s %s\n", PROGNAME, VERSION);
	printf("usage: %s -d <dir> -o <blob> -i <manifest>\n", PROGNAME);
	printf("-t [hest|bert|erst]\n");
	printf("   -d <dir>      => directory of ASL files\n");
	printf("   -o <blob>     => file name for resulting APEI blob\n");
	printf("   -i <manifest> => list of AML files needed\n");
	printf("   -t            => blob type for APEI\n");
	printf("   -q            => if given, supress output\n");
}

static uint64_t obtain_esb(char *homedir, char *manifest_name)
{
	int err, table_size, i, error_source_count;
	struct acpi_hest_generic *generic;
	struct acpi_table_hest *hest_hdr;
	struct table *p;
	char *blob;
	FILE *fp;

	/* What tables can we handle with */
	err = read_manifest(homedir, manifest_name);
	if (err) {
		printf("? could not read manifest: %s\n", manifest_name);
		return 0;
	}

	/* HEST table would be candidate */
	p = find_table("hest");
	if (!p) {
		printf("Not found hest table!\n");
		return 0;
	}

	if (!p->aml_name) {
		printf("No aml file created for %s table!\n", p->asl_name);
		return 0;
	}

	table_size = get_file_size(p->aml_name);
	blob = (char *)malloc(table_size);
	if (!blob) {
		printf("Memory allocation error!\n");
		return 0;
	}

	fp = fopen(p->aml_name, "r");
	if (!fp) {
		printf("File %s can not be opened!\n", p->aml_name);
		return 0;
	}
	table_size = fread(blob, 1, table_size, fp);
	fclose(fp);

	/* How many error sources we should traverse looking for GHES ? */
	hest_hdr = (struct acpi_table_hest *)blob;
	error_source_count = hest_hdr->error_source_count;

	blob = (char *)(hest_hdr + 1);
	generic = (struct acpi_hest_generic *)blob;
	for (i = 0; i < error_source_count; i++) {
		if (generic->header.type == ACPI_HEST_TYPE_GENERIC_ERROR)
			return generic->error_status_address.address;
	}

	return 0;
}

static int bfapei_expand_buf(char **buf, int *size, int reqr_size)
{
	if (*size >= reqr_size)
		return BFAPEI_OK;

	*buf = realloc(*buf, reqr_size);
	if (!(*buf)) {
		printf("Error during memory allocation!\n");
		return BFAPEI_FAIL;
	}
	memset(*buf + *size, 0, reqr_size - *size);
	*size = reqr_size;

	return BFAPEI_OK;
}

static int bfapei_hest(char **buf, int *size, uint64_t paddr, int status)
{
	struct acpi_hest_generic_status *block_ptr;
	struct acpi_hest_generic_data *gdata;
	struct cper_sec_mem_err *mem_err;
	uint64_t *add_ptr;
	int reqr_size = sizeof(uint64_t) +
			sizeof(struct acpi_hest_generic_status) +
			sizeof(struct acpi_hest_generic_data) +
			sizeof (struct cper_sec_mem_err);

	if (bfapei_expand_buf(buf, size, reqr_size))
		return BFAPEI_FAIL;

	/*
	 * Fill in blob which will be parsed by GHES driver.
	 */

	/* First point to generic error status block */
	add_ptr = (uint64_t *) *buf;
	*add_ptr = paddr + sizeof(uint64_t);

	/* Fill in generic error status block */
	block_ptr = (struct acpi_hest_generic_status *) (++add_ptr);
	block_ptr->block_status = status;
	block_ptr->data_length = sizeof(struct acpi_hest_generic_data);
	block_ptr->error_severity = GHES_SEV_CORRECTED;

	/* Fill in generic error data entry */
	gdata = (struct acpi_hest_generic_data *) (block_ptr + 1);
	memcpy(gdata->section_type, (void *) &CPER_SEC_PLATFORM_MEM,
			sizeof(uuid_le));
	gdata->error_data_length = sizeof(struct cper_sec_mem_err);
	block_ptr->data_length += gdata->error_data_length;

	mem_err = (struct cper_sec_mem_err *) (gdata + 1);
	/* Place for more specific err info */

	return BFAPEI_OK;
}

static int bfapei_erst(char **buf, int *size, uint64_t paddr)
{
	int reqr_size = 0x200;

	if (bfapei_expand_buf(buf, size, reqr_size))
		return BFAPEI_FAIL;

	/*
	 * Fill in ERST registers.
	 */
	WRITE_ERST_REG(*buf, ACPI_ERST_GET_COMMAND_STATUS, ERST_STATUS_SUCCESS);
	WRITE_ERST_REG(*buf, ACPI_ERST_CHECK_BUSY_STATUS, 1);

	WRITE_ERST_REG(*buf, ACPI_ERST_GET_ERROR_RANGE, paddr + 0x2000);
	WRITE_ERST_REG(*buf, ACPI_ERST_GET_ERROR_LENGTH, 0x10000);
	WRITE_ERST_REG(*buf, ACPI_ERST_GET_ERROR_ATTRIBUTES, ERST_RANGE_SLOW);

	return BFAPEI_OK;
}

int main(int argc, char *argv[]) {
	char *homedir, *apei_blob_name, *manifest_name, *blob_type;
	int opt, size = 0, quiet = 0;
	uint64_t paddr;
	char *buf = NULL;;

	while ((opt = getopt(argc, argv, "d:o:i:t:q")) != EOF) {
		switch (opt) {
		case 'd':
			homedir = optarg;
			break;
		case 'o':
			apei_blob_name = optarg;
			break;
		case 'i':
			manifest_name = optarg;
			break;
		case 't':
			blob_type = optarg;
			break;
		case 'q':
			quiet = 1;
			break;
		default:
			usage();
			return BFAPEI_FAIL;
		}
	}

	if ((argc > (optind + 1)) || (apei_blob_name == NULL) ||
	    (homedir == NULL) || (manifest_name == NULL) ||
	    (blob_type == NULL)) {
		printf("Missing a parameter value\n");
		usage();
		return BFAPEI_FAIL;
	}

	paddr = obtain_esb(homedir, manifest_name);
	if (!paddr) {
		printf("No Error Status Block address obtained !\n");
		return BFAPEI_FAIL;
	}

	if (strncmp(blob_type, "hest", 4) == 0) {
		if (bfapei_hest(&buf, &size, paddr, 0))
			return BFAPEI_FAIL;
	}

	/* Set status flag so error will be visible for BERT driver */
	if (strncmp(blob_type, "bert", 4) == 0) {
		if (bfapei_hest(&buf, &size, paddr, 1))
			return BFAPEI_FAIL;
	}

	if (strncmp(blob_type, "erst", 4) == 0) {
		if (bfapei_erst(&buf, &size, paddr))
			return BFAPEI_FAIL;
	}

	/* Save buffer to specified file. */
	write_blob(homedir, apei_blob_name, (unsigned char *)buf, size);

	if (!quiet) {
		printf("%s blob for %s APEI table testing created\n", apei_blob_name, blob_type);
		printf("Physical address: 0x%llx\n", (long long unsigned int)paddr);
	}

	return BFAPEI_OK;
}