1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Platform Firmware Runtime Update tool to do Management
4 * Mode code injection/driver update and telemetry retrieval.
5 *
6 * This tool uses the interfaces provided by pfr_update and
7 * pfr_telemetry drivers. These interfaces are exposed via
8 * /dev/pfr_update and /dev/pfr_telemetry. Write operation
9 * on the /dev/pfr_update is to load the EFI capsule into
10 * kernel space. Mmap/read operations on /dev/pfr_telemetry
11 * could be used to read the telemetry data to user space.
12 */
13 #define _GNU_SOURCE
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <getopt.h>
22 #include <sys/ioctl.h>
23 #include <sys/mman.h>
24 #include <uuid/uuid.h>
25 #include PFRUT_HEADER
26
27 char *capsule_name;
28 int action, query_cap, log_type, log_level, log_read, log_getinfo,
29 revid, log_revid;
30 int set_log_level, set_log_type,
31 set_revid, set_log_revid;
32
33 char *progname;
34
35 #define LOG_ERR 0
36 #define LOG_WARN 1
37 #define LOG_INFO 2
38 #define LOG_VERB 4
39 #define LOG_EXEC_IDX 0
40 #define LOG_HISTORY_IDX 1
41 #define REVID_1 1
42 #define REVID_2 2
43
valid_log_level(int level)44 static int valid_log_level(int level)
45 {
46 return level == LOG_ERR || level == LOG_WARN ||
47 level == LOG_INFO || level == LOG_VERB;
48 }
49
valid_log_type(int type)50 static int valid_log_type(int type)
51 {
52 return type == LOG_EXEC_IDX || type == LOG_HISTORY_IDX;
53 }
54
valid_log_revid(int id)55 static inline int valid_log_revid(int id)
56 {
57 return id == REVID_1 || id == REVID_2;
58 }
59
help(void)60 static void help(void)
61 {
62 fprintf(stderr,
63 "usage: %s [OPTIONS]\n"
64 " code injection:\n"
65 " -l, --load\n"
66 " -s, --stage\n"
67 " -a, --activate\n"
68 " -u, --update [stage and activate]\n"
69 " -q, --query\n"
70 " -d, --revid update\n"
71 " telemetry:\n"
72 " -G, --getloginfo\n"
73 " -T, --type(0:execution, 1:history)\n"
74 " -L, --level(0, 1, 2, 4)\n"
75 " -R, --read\n"
76 " -D, --revid log\n",
77 progname);
78 }
79
80 char *option_string = "l:sauqd:GT:L:RD:h";
81 static struct option long_options[] = {
82 {"load", required_argument, 0, 'l'},
83 {"stage", no_argument, 0, 's'},
84 {"activate", no_argument, 0, 'a'},
85 {"update", no_argument, 0, 'u'},
86 {"query", no_argument, 0, 'q'},
87 {"getloginfo", no_argument, 0, 'G'},
88 {"type", required_argument, 0, 'T'},
89 {"level", required_argument, 0, 'L'},
90 {"read", no_argument, 0, 'R'},
91 {"setrev", required_argument, 0, 'd'},
92 {"setrevlog", required_argument, 0, 'D'},
93 {"help", no_argument, 0, 'h'},
94 {}
95 };
96
parse_options(int argc,char ** argv)97 static void parse_options(int argc, char **argv)
98 {
99 int option_index = 0;
100 char *pathname;
101 int opt;
102
103 pathname = strdup(argv[0]);
104 progname = basename(pathname);
105
106 while ((opt = getopt_long_only(argc, argv, option_string,
107 long_options, &option_index)) != -1) {
108 switch (opt) {
109 case 'l':
110 capsule_name = optarg;
111 break;
112 case 's':
113 action = 1;
114 break;
115 case 'a':
116 action = 2;
117 break;
118 case 'u':
119 action = 3;
120 break;
121 case 'q':
122 query_cap = 1;
123 break;
124 case 'G':
125 log_getinfo = 1;
126 break;
127 case 'T':
128 log_type = atoi(optarg);
129 set_log_type = 1;
130 break;
131 case 'L':
132 log_level = atoi(optarg);
133 set_log_level = 1;
134 break;
135 case 'R':
136 log_read = 1;
137 break;
138 case 'd':
139 revid = atoi(optarg);
140 set_revid = 1;
141 break;
142 case 'D':
143 log_revid = atoi(optarg);
144 set_log_revid = 1;
145 break;
146 case 'h':
147 help();
148 exit(0);
149 default:
150 break;
151 }
152 }
153 }
154
print_cap(struct pfru_update_cap_info * cap)155 void print_cap(struct pfru_update_cap_info *cap)
156 {
157 char *uuid;
158
159 uuid = malloc(37);
160 if (!uuid) {
161 perror("Can not allocate uuid buffer\n");
162 exit(1);
163 }
164
165 uuid_unparse(cap->code_type, uuid);
166 printf("code injection image type:%s\n", uuid);
167 printf("fw_version:%d\n", cap->fw_version);
168 printf("code_rt_version:%d\n", cap->code_rt_version);
169
170 uuid_unparse(cap->drv_type, uuid);
171 printf("driver update image type:%s\n", uuid);
172 printf("drv_rt_version:%d\n", cap->drv_rt_version);
173 printf("drv_svn:%d\n", cap->drv_svn);
174
175 uuid_unparse(cap->platform_id, uuid);
176 printf("platform id:%s\n", uuid);
177 uuid_unparse(cap->oem_id, uuid);
178 printf("oem id:%s\n", uuid);
179 printf("oem information length:%d\n", cap->oem_info_len);
180
181 free(uuid);
182 }
183
main(int argc,char * argv[])184 int main(int argc, char *argv[])
185 {
186 int fd_update, fd_update_log, fd_capsule;
187 struct pfrt_log_data_info data_info;
188 struct pfrt_log_info info;
189 struct pfru_update_cap_info cap;
190 void *addr_map_capsule;
191 struct stat st;
192 char *log_buf;
193 int ret;
194
195 if (getuid() != 0) {
196 printf("Please run the tool as root - Exiting.\n");
197 return 1;
198 }
199
200 parse_options(argc, argv);
201
202 fd_update = open("/dev/acpi_pfr_update0", O_RDWR);
203 if (fd_update < 0) {
204 printf("PFRU device not supported - Quit...\n");
205 return 1;
206 }
207
208 fd_update_log = open("/dev/acpi_pfr_telemetry0", O_RDWR);
209 if (fd_update_log < 0) {
210 printf("PFRT device not supported - Quit...\n");
211 return 1;
212 }
213
214 if (query_cap) {
215 ret = ioctl(fd_update, PFRU_IOC_QUERY_CAP, &cap);
216 if (ret)
217 perror("Query Update Capability info failed.");
218 else
219 print_cap(&cap);
220
221 close(fd_update);
222 close(fd_update_log);
223
224 return ret;
225 }
226
227 if (log_getinfo) {
228 ret = ioctl(fd_update_log, PFRT_LOG_IOC_GET_DATA_INFO, &data_info);
229 if (ret) {
230 perror("Get telemetry data info failed.");
231 close(fd_update);
232 close(fd_update_log);
233
234 return 1;
235 }
236
237 ret = ioctl(fd_update_log, PFRT_LOG_IOC_GET_INFO, &info);
238 if (ret) {
239 perror("Get telemetry info failed.");
240 close(fd_update);
241 close(fd_update_log);
242
243 return 1;
244 }
245
246 printf("log_level:%d\n", info.log_level);
247 printf("log_type:%d\n", info.log_type);
248 printf("log_revid:%d\n", info.log_revid);
249 printf("max_data_size:%d\n", data_info.max_data_size);
250 printf("chunk1_size:%d\n", data_info.chunk1_size);
251 printf("chunk2_size:%d\n", data_info.chunk2_size);
252 printf("rollover_cnt:%d\n", data_info.rollover_cnt);
253 printf("reset_cnt:%d\n", data_info.reset_cnt);
254
255 return 0;
256 }
257
258 info.log_level = -1;
259 info.log_type = -1;
260 info.log_revid = -1;
261
262 if (set_log_level) {
263 if (!valid_log_level(log_level)) {
264 printf("Invalid log level %d\n",
265 log_level);
266 } else {
267 info.log_level = log_level;
268 }
269 }
270
271 if (set_log_type) {
272 if (!valid_log_type(log_type)) {
273 printf("Invalid log type %d\n",
274 log_type);
275 } else {
276 info.log_type = log_type;
277 }
278 }
279
280 if (set_log_revid) {
281 if (!valid_log_revid(log_revid)) {
282 printf("Invalid log revid %d, unchanged.\n",
283 log_revid);
284 } else {
285 info.log_revid = log_revid;
286 }
287 }
288
289 ret = ioctl(fd_update_log, PFRT_LOG_IOC_SET_INFO, &info);
290 if (ret) {
291 perror("Log information set failed.(log_level, log_type, log_revid)");
292 close(fd_update);
293 close(fd_update_log);
294
295 return 1;
296 }
297
298 if (set_revid) {
299 ret = ioctl(fd_update, PFRU_IOC_SET_REV, &revid);
300 if (ret) {
301 perror("pfru update revid set failed");
302 close(fd_update);
303 close(fd_update_log);
304
305 return 1;
306 }
307
308 printf("pfru update revid set to %d\n", revid);
309 }
310
311 if (capsule_name) {
312 fd_capsule = open(capsule_name, O_RDONLY);
313 if (fd_capsule < 0) {
314 perror("Can not open capsule file...");
315 close(fd_update);
316 close(fd_update_log);
317
318 return 1;
319 }
320
321 if (fstat(fd_capsule, &st) < 0) {
322 perror("Can not fstat capsule file...");
323 close(fd_capsule);
324 close(fd_update);
325 close(fd_update_log);
326
327 return 1;
328 }
329
330 addr_map_capsule = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED,
331 fd_capsule, 0);
332 if (addr_map_capsule == MAP_FAILED) {
333 perror("Failed to mmap capsule file.");
334 close(fd_capsule);
335 close(fd_update);
336 close(fd_update_log);
337
338 return 1;
339 }
340
341 ret = write(fd_update, (char *)addr_map_capsule, st.st_size);
342 printf("Load %d bytes of capsule file into the system\n",
343 ret);
344
345 if (ret == -1) {
346 perror("Failed to load capsule file");
347 close(fd_capsule);
348 close(fd_update);
349 close(fd_update_log);
350
351 return 1;
352 }
353
354 munmap(addr_map_capsule, st.st_size);
355 close(fd_capsule);
356 printf("Load done.\n");
357 }
358
359 if (action) {
360 if (action == 1) {
361 ret = ioctl(fd_update, PFRU_IOC_STAGE, NULL);
362 } else if (action == 2) {
363 ret = ioctl(fd_update, PFRU_IOC_ACTIVATE, NULL);
364 } else if (action == 3) {
365 ret = ioctl(fd_update, PFRU_IOC_STAGE_ACTIVATE, NULL);
366 } else {
367 close(fd_update);
368 close(fd_update_log);
369
370 return 1;
371 }
372 printf("Update finished, return %d\n", ret);
373 }
374
375 close(fd_update);
376
377 if (log_read) {
378 void *p_mmap;
379 int max_data_sz;
380
381 ret = ioctl(fd_update_log, PFRT_LOG_IOC_GET_DATA_INFO, &data_info);
382 if (ret) {
383 perror("Get telemetry data info failed.");
384 close(fd_update_log);
385
386 return 1;
387 }
388
389 max_data_sz = data_info.max_data_size;
390 if (!max_data_sz) {
391 printf("No telemetry data available.\n");
392 close(fd_update_log);
393
394 return 1;
395 }
396
397 log_buf = malloc(max_data_sz + 1);
398 if (!log_buf) {
399 perror("log_buf allocate failed.");
400 close(fd_update_log);
401
402 return 1;
403 }
404
405 p_mmap = mmap(NULL, max_data_sz, PROT_READ, MAP_SHARED, fd_update_log, 0);
406 if (p_mmap == MAP_FAILED) {
407 perror("mmap error.");
408 close(fd_update_log);
409
410 return 1;
411 }
412
413 memcpy(log_buf, p_mmap, max_data_sz);
414 log_buf[max_data_sz] = '\0';
415 printf("%s\n", log_buf);
416 free(log_buf);
417
418 munmap(p_mmap, max_data_sz);
419 }
420
421 close(fd_update_log);
422
423 return 0;
424 }
425