aboutsummaryrefslogtreecommitdiffstats
path: root/tools/power/acpi/tools/pfrut/pfrut.c
blob: 52aa0351533c315e4c7bfd4e6a318642757c5e73 (plain) (blame)
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// SPDX-License-Identifier: GPL-2.0
/*
 * Platform Firmware Runtime Update tool to do Management
 * Mode code injection/driver update and telemetry retrieval.
 *
 * This tool uses the interfaces provided by pfr_update and
 * pfr_telemetry drivers. These interfaces are exposed via
 * /dev/pfr_update and /dev/pfr_telemetry. Write operation
 * on the /dev/pfr_update is to load the EFI capsule into
 * kernel space. Mmap/read operations on /dev/pfr_telemetry
 * could be used to read the telemetry data to user space.
 */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <uuid/uuid.h>
#include PFRUT_HEADER

char *capsule_name;
int action, query_cap, log_type, log_level, log_read, log_getinfo,
	revid, log_revid;
int set_log_level, set_log_type,
	set_revid, set_log_revid;

char *progname;

#define LOG_ERR		0
#define LOG_WARN	1
#define LOG_INFO	2
#define LOG_VERB	4
#define LOG_EXEC_IDX	0
#define LOG_HISTORY_IDX	1
#define REVID_1		1
#define REVID_2		2

static int valid_log_level(int level)
{
	return level == LOG_ERR || level == LOG_WARN ||
	       level == LOG_INFO || level == LOG_VERB;
}

static int valid_log_type(int type)
{
	return type == LOG_EXEC_IDX || type == LOG_HISTORY_IDX;
}

static inline int valid_log_revid(int id)
{
	return id == REVID_1 || id == REVID_2;
}

static void help(void)
{
	fprintf(stderr,
		"usage: %s [OPTIONS]\n"
		" code injection:\n"
		"  -l, --load\n"
		"  -s, --stage\n"
		"  -a, --activate\n"
		"  -u, --update [stage and activate]\n"
		"  -q, --query\n"
		"  -d, --revid update\n"
		" telemetry:\n"
		"  -G, --getloginfo\n"
		"  -T, --type(0:execution, 1:history)\n"
		"  -L, --level(0, 1, 2, 4)\n"
		"  -R, --read\n"
		"  -D, --revid log\n",
		progname);
}

char *option_string = "l:sauqd:GT:L:RD:h";
static struct option long_options[] = {
	{"load", required_argument, 0, 'l'},
	{"stage", no_argument, 0, 's'},
	{"activate", no_argument, 0, 'a'},
	{"update", no_argument, 0, 'u'},
	{"query", no_argument, 0, 'q'},
	{"getloginfo", no_argument, 0, 'G'},
	{"type", required_argument, 0, 'T'},
	{"level", required_argument, 0, 'L'},
	{"read", no_argument, 0, 'R'},
	{"setrev", required_argument, 0, 'd'},
	{"setrevlog", required_argument, 0, 'D'},
	{"help", no_argument, 0, 'h'},
	{}
};

static void parse_options(int argc, char **argv)
{
	int option_index = 0;
	char *pathname;
	int opt;

	pathname = strdup(argv[0]);
	progname = basename(pathname);

	while ((opt = getopt_long_only(argc, argv, option_string,
				       long_options, &option_index)) != -1) {
		switch (opt) {
		case 'l':
			capsule_name = optarg;
			break;
		case 's':
			action = 1;
			break;
		case 'a':
			action = 2;
			break;
		case 'u':
			action = 3;
			break;
		case 'q':
			query_cap = 1;
			break;
		case 'G':
			log_getinfo = 1;
			break;
		case 'T':
			log_type = atoi(optarg);
			set_log_type = 1;
			break;
		case 'L':
			log_level = atoi(optarg);
			set_log_level = 1;
			break;
		case 'R':
			log_read = 1;
			break;
		case 'd':
			revid = atoi(optarg);
			set_revid = 1;
			break;
		case 'D':
			log_revid = atoi(optarg);
			set_log_revid = 1;
			break;
		case 'h':
			help();
			exit(0);
		default:
			break;
		}
	}
}

void print_cap(struct pfru_update_cap_info *cap)
{
	char *uuid;

	uuid = malloc(37);
	if (!uuid) {
		perror("Can not allocate uuid buffer\n");
		exit(1);
	}

	uuid_unparse(cap->code_type, uuid);
	printf("code injection image type:%s\n", uuid);
	printf("fw_version:%d\n", cap->fw_version);
	printf("code_rt_version:%d\n", cap->code_rt_version);

	uuid_unparse(cap->drv_type, uuid);
	printf("driver update image type:%s\n", uuid);
	printf("drv_rt_version:%d\n", cap->drv_rt_version);
	printf("drv_svn:%d\n", cap->drv_svn);

	uuid_unparse(cap->platform_id, uuid);
	printf("platform id:%s\n", uuid);
	uuid_unparse(cap->oem_id, uuid);
	printf("oem id:%s\n", uuid);
	printf("oem information length:%d\n", cap->oem_info_len);

	free(uuid);
}

int main(int argc, char *argv[])
{
	int fd_update, fd_update_log, fd_capsule;
	struct pfrt_log_data_info data_info;
	struct pfrt_log_info info;
	struct pfru_update_cap_info cap;
	void *addr_map_capsule;
	struct stat st;
	char *log_buf;
	int ret;

	if (getuid() != 0) {
		printf("Please run the tool as root - Exiting.\n");
		return 1;
	}

	parse_options(argc, argv);

	fd_update = open("/dev/acpi_pfr_update0", O_RDWR);
	if (fd_update < 0) {
		printf("PFRU device not supported - Quit...\n");
		return 1;
	}

	fd_update_log = open("/dev/acpi_pfr_telemetry0", O_RDWR);
	if (fd_update_log < 0) {
		printf("PFRT device not supported - Quit...\n");
		return 1;
	}

	if (query_cap) {
		ret = ioctl(fd_update, PFRU_IOC_QUERY_CAP, &cap);
		if (ret)
			perror("Query Update Capability info failed.");
		else
			print_cap(&cap);

		close(fd_update);
		close(fd_update_log);

		return ret;
	}

	if (log_getinfo) {
		ret = ioctl(fd_update_log, PFRT_LOG_IOC_GET_DATA_INFO, &data_info);
		if (ret) {
			perror("Get telemetry data info failed.");
			close(fd_update);
			close(fd_update_log);

			return 1;
		}

		ret = ioctl(fd_update_log, PFRT_LOG_IOC_GET_INFO, &info);
		if (ret) {
			perror("Get telemetry info failed.");
			close(fd_update);
			close(fd_update_log);

			return 1;
		}

		printf("log_level:%d\n", info.log_level);
		printf("log_type:%d\n", info.log_type);
		printf("log_revid:%d\n", info.log_revid);
		printf("max_data_size:%d\n", data_info.max_data_size);
		printf("chunk1_size:%d\n", data_info.chunk1_size);
		printf("chunk2_size:%d\n", data_info.chunk2_size);
		printf("rollover_cnt:%d\n", data_info.rollover_cnt);
		printf("reset_cnt:%d\n", data_info.reset_cnt);

		return 0;
	}

	info.log_level = -1;
	info.log_type = -1;
	info.log_revid = -1;

	if (set_log_level) {
		if (!valid_log_level(log_level)) {
			printf("Invalid log level %d\n",
			       log_level);
		} else {
			info.log_level = log_level;
		}
	}

	if (set_log_type) {
		if (!valid_log_type(log_type)) {
			printf("Invalid log type %d\n",
			       log_type);
		} else {
			info.log_type = log_type;
		}
	}

	if (set_log_revid) {
		if (!valid_log_revid(log_revid)) {
			printf("Invalid log revid %d, unchanged.\n",
			       log_revid);
		} else {
			info.log_revid = log_revid;
		}
	}

	ret = ioctl(fd_update_log, PFRT_LOG_IOC_SET_INFO, &info);
	if (ret) {
		perror("Log information set failed.(log_level, log_type, log_revid)");
		close(fd_update);
		close(fd_update_log);

		return 1;
	}

	if (set_revid) {
		ret = ioctl(fd_update, PFRU_IOC_SET_REV, &revid);
		if (ret) {
			perror("pfru update revid set failed");
			close(fd_update);
			close(fd_update_log);

			return 1;
		}

		printf("pfru update revid set to %d\n", revid);
	}

	if (capsule_name) {
		fd_capsule = open(capsule_name, O_RDONLY);
		if (fd_capsule < 0) {
			perror("Can not open capsule file...");
			close(fd_update);
			close(fd_update_log);

			return 1;
		}

		if (fstat(fd_capsule, &st) < 0) {
			perror("Can not fstat capsule file...");
			close(fd_capsule);
			close(fd_update);
			close(fd_update_log);

			return 1;
		}

		addr_map_capsule = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED,
					fd_capsule, 0);
		if (addr_map_capsule == MAP_FAILED) {
			perror("Failed to mmap capsule file.");
			close(fd_capsule);
			close(fd_update);
			close(fd_update_log);

			return 1;
		}

		ret = write(fd_update, (char *)addr_map_capsule, st.st_size);
		printf("Load %d bytes of capsule file into the system\n",
		       ret);

		if (ret == -1) {
			perror("Failed to load capsule file");
			close(fd_capsule);
			close(fd_update);
			close(fd_update_log);

			return 1;
		}

		munmap(addr_map_capsule, st.st_size);
		close(fd_capsule);
		printf("Load done.\n");
	}

	if (action) {
		if (action == 1) {
			ret = ioctl(fd_update, PFRU_IOC_STAGE, NULL);
		} else if (action == 2) {
			ret = ioctl(fd_update, PFRU_IOC_ACTIVATE, NULL);
		} else if (action == 3) {
			ret = ioctl(fd_update, PFRU_IOC_STAGE_ACTIVATE, NULL);
		} else {
			close(fd_update);
			close(fd_update_log);

			return 1;
		}
		printf("Update finished, return %d\n", ret);
	}

	close(fd_update);

	if (log_read) {
		void *p_mmap;
		int max_data_sz;

		ret = ioctl(fd_update_log, PFRT_LOG_IOC_GET_DATA_INFO, &data_info);
		if (ret) {
			perror("Get telemetry data info failed.");
			close(fd_update_log);

			return 1;
		}

		max_data_sz = data_info.max_data_size;
		if (!max_data_sz) {
			printf("No telemetry data available.\n");
			close(fd_update_log);

			return 1;
		}

		log_buf = malloc(max_data_sz + 1);
		if (!log_buf) {
			perror("log_buf allocate failed.");
			close(fd_update_log);

			return 1;
		}

		p_mmap = mmap(NULL, max_data_sz, PROT_READ, MAP_SHARED, fd_update_log, 0);
		if (p_mmap == MAP_FAILED) {
			perror("mmap error.");
			close(fd_update_log);

			return 1;
		}

		memcpy(log_buf, p_mmap, max_data_sz);
		log_buf[max_data_sz] = '\0';
		printf("%s\n", log_buf);
		free(log_buf);

		munmap(p_mmap, max_data_sz);
	}

	close(fd_update_log);

	return 0;
}