blob: 253e2ee95a800642c66f3d9b9b3937149d85a3e1 [file] [log] [blame]
Ricardo Quesadac2cf3942021-07-16 16:49:04 -07001/* SPDX-License-Identifier: BSD-3-Clause */
2
Ricardo Quesada683b294d2021-09-03 17:47:43 -07003#include <assert.h>
Ricardo Quesadac2cf3942021-07-16 16:49:04 -07004#include <getopt.h>
Ricardo Quesada49a96a92021-08-16 11:25:52 -07005#include <stdbool.h>
Ricardo Quesadac2cf3942021-07-16 16:49:04 -07006#include <stdint.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <unistd.h>
11
12#include <common.h>
13#include <commonlib/bsd/elog.h>
Martin Rothe3e965b2022-02-25 17:28:04 -070014#include <flashrom.h>
Ricardo Quesadac2cf3942021-07-16 16:49:04 -070015
16#include "eventlog.h"
17
Ricardo Quesada683b294d2021-09-03 17:47:43 -070018/* Only refers to the data max size. The "-1" is the checksum byte */
19#define ELOG_MAX_EVENT_DATA_SIZE (ELOG_MAX_EVENT_SIZE - sizeof(struct event_header) - 1)
20
Ricardo Quesadac2cf3942021-07-16 16:49:04 -070021enum elogtool_return {
22 ELOGTOOL_EXIT_SUCCESS = 0,
23 ELOGTOOL_EXIT_BAD_ARGS,
Ricardo Quesada49a96a92021-08-16 11:25:52 -070024 ELOGTOOL_EXIT_READ_ERROR,
25 ELOGTOOL_EXIT_WRITE_ERROR,
Ricardo Quesadac2cf3942021-07-16 16:49:04 -070026 ELOGTOOL_EXIT_NOT_ENOUGH_MEMORY,
27 ELOGTOOL_EXIT_INVALID_ELOG_FORMAT,
Ricardo Quesada49a96a92021-08-16 11:25:52 -070028 ELOGTOOL_EXIT_NOT_ENOUGH_BUFFER_SPACE,
29};
30
31static int cmd_list(const struct buffer *);
32static int cmd_clear(const struct buffer *);
Ricardo Quesada683b294d2021-09-03 17:47:43 -070033static int cmd_add(const struct buffer *);
Ricardo Quesada49a96a92021-08-16 11:25:52 -070034
35static const struct {
36 const char *name;
37 int (*func)(const struct buffer *buf);
38 /* Whether it requires to write the buffer back */
39 bool write_back;
40} cmds[] = {
41 {"list", cmd_list, false},
42 {"clear", cmd_clear, true},
Ricardo Quesada683b294d2021-09-03 17:47:43 -070043 {"add", cmd_add, true},
Ricardo Quesadac2cf3942021-07-16 16:49:04 -070044};
45
Ricardo Quesada683b294d2021-09-03 17:47:43 -070046static char **cmd_argv; /* Command arguments */
47static char *argv0; /* Used as invoked_as */
48
Ricardo Quesadac2cf3942021-07-16 16:49:04 -070049static struct option long_options[] = {
50 {"file", required_argument, 0, 'f'},
51 {"help", no_argument, 0, 'h'},
52 {NULL, 0, 0, 0},
53};
54
55static void usage(char *invoked_as)
56{
Ricardo Quesada683b294d2021-09-03 17:47:43 -070057 fprintf(stderr, "elogtool: edit elog events\n\n"
Ricardo Quesadac2cf3942021-07-16 16:49:04 -070058 "USAGE:\n"
59 "\t%s COMMAND [-f <filename>]\n\n"
60 "where, COMMAND is:\n"
Ricardo Quesada683b294d2021-09-03 17:47:43 -070061 " list lists all the event logs in human readable format\n"
62 " clear clears all the event logs\n"
63 " add <event_type> [event_data] add an entry to the event log\n"
Ricardo Quesada49a96a92021-08-16 11:25:52 -070064 "\n"
Ricardo Quesadac2cf3942021-07-16 16:49:04 -070065 "ARGS\n"
Ricardo Quesada49a96a92021-08-16 11:25:52 -070066 "-f, --file <filename> File that holds event log partition.\n"
67 " If empty it will try to read/write from/to\n"
68 " the " ELOG_RW_REGION_NAME " using flashrom.\n"
Ricardo Quesadac2cf3942021-07-16 16:49:04 -070069 "-h, --help Print this help\n",
70 invoked_as);
71}
72
Ricardo Quesada49a96a92021-08-16 11:25:52 -070073/*
74 * If filename is empty, read RW_ELOG from flashrom.
75 * Otherwise read the RW_ELOG from a file.
76 * It fails if the ELOG header is invalid.
77 * On success, buffer must be freed by caller.
78 */
79static int elog_read(struct buffer *buffer, const char *filename)
Ricardo Quesadac2cf3942021-07-16 16:49:04 -070080{
Edward O'Callaghand74b8d92021-11-29 11:02:46 +110081 if (filename == NULL) {
Martin Rothe3e965b2022-02-25 17:28:04 -070082 uint8_t *buf;
83 uint32_t buf_size;
84
85 if (flashrom_read(FLASHROM_PROGRAMMER_INTERNAL_AP, ELOG_RW_REGION_NAME,
86 &buf, &buf_size) != VB2_SUCCESS) {
Ricardo Quesadac2cf3942021-07-16 16:49:04 -070087 fprintf(stderr, "Could not read RW_ELOG region using flashrom\n");
Ricardo Quesada49a96a92021-08-16 11:25:52 -070088 return ELOGTOOL_EXIT_READ_ERROR;
Ricardo Quesadac2cf3942021-07-16 16:49:04 -070089 }
Martin Rothe3e965b2022-02-25 17:28:04 -070090 buffer_init(buffer, NULL, buf, buf_size);
Ricardo Quesada49a96a92021-08-16 11:25:52 -070091 } else if (buffer_from_file(buffer, filename) != 0) {
92 fprintf(stderr, "Could not read input file: %s\n", filename);
93 return ELOGTOOL_EXIT_READ_ERROR;
Ricardo Quesadac2cf3942021-07-16 16:49:04 -070094 }
95
Ricardo Quesada49a96a92021-08-16 11:25:52 -070096 if (elog_verify_header(buffer_get(buffer)) != CB_SUCCESS) {
Ricardo Quesadac2cf3942021-07-16 16:49:04 -070097 fprintf(stderr, "FATAL: Invalid elog header\n");
Ricardo Quesada49a96a92021-08-16 11:25:52 -070098 buffer_delete(buffer);
Ricardo Quesadac2cf3942021-07-16 16:49:04 -070099 return ELOGTOOL_EXIT_INVALID_ELOG_FORMAT;
100 }
101
Ricardo Quesada49a96a92021-08-16 11:25:52 -0700102 return ELOGTOOL_EXIT_SUCCESS;
103}
Ricardo Quesadac2cf3942021-07-16 16:49:04 -0700104
Ricardo Quesada49a96a92021-08-16 11:25:52 -0700105/*
106 * If filename is NULL, it saves the buffer using flashrom.
107 * Otherwise, it saves the buffer in the given filename.
108 */
109static int elog_write(struct buffer *buf, const char *filename)
110{
111 if (filename == NULL) {
Martin Rothe3e965b2022-02-25 17:28:04 -0700112 if (flashrom_write(FLASHROM_PROGRAMMER_INTERNAL_AP, ELOG_RW_REGION_NAME,
113 buffer_get(buf), buffer_size(buf)) != VB2_SUCCESS) {
Ricardo Quesada49a96a92021-08-16 11:25:52 -0700114 fprintf(stderr,
115 "Failed to write to RW_ELOG region using flashrom\n");
116 return ELOGTOOL_EXIT_WRITE_ERROR;
117 }
118 return ELOGTOOL_EXIT_SUCCESS;
119 }
120
121 if (buffer_write_file(buf, filename) != 0) {
122 fprintf(stderr, "Failed to write to file %s\n", filename);
123 return ELOGTOOL_EXIT_WRITE_ERROR;
124 }
125 return ELOGTOOL_EXIT_SUCCESS;
126}
127
Ricardo Quesada683b294d2021-09-03 17:47:43 -0700128/* Buffer offset must point to a valid event_header struct */
Ricardo Quesadaa2bf94d2021-09-03 17:30:31 -0700129static size_t next_available_event_offset(const struct buffer *buf)
130{
131 const struct event_header *event;
132 struct buffer copy, *iter = &copy;
133
Ricardo Quesada683b294d2021-09-03 17:47:43 -0700134 assert(buffer_offset(buf) >= sizeof(struct elog_header));
135
Ricardo Quesadaa2bf94d2021-09-03 17:30:31 -0700136 buffer_clone(iter, buf);
137
138 while (buffer_size(iter) >= sizeof(struct event_header)) {
139 event = buffer_get(iter);
140 if (event->type == ELOG_TYPE_EOL || event->length == 0)
141 break;
142
143 assert(event->length <= buffer_size(iter));
144 buffer_seek(iter, event->length);
145 }
146
147 return buffer_offset(iter) - buffer_offset(buf);
148}
149
Ricardo Quesada683b294d2021-09-03 17:47:43 -0700150/*
151 * Shrinks buffer by ~bytes_to_shrink, then appends a LOG_CLEAR event,
152 * and finally fills the remaining area with EOL events.
153 * Buffer offset must point to a valid event_header struct.
154 */
155static int shrink_buffer(const struct buffer *buf, size_t bytes_to_shrink)
156{
157 struct buffer copy, *iter = &copy;
158 const struct event_header *event;
159 uint32_t cleared;
160 int remaining;
161 uint8_t *data;
162
163 assert(buffer_offset(buf) >= sizeof(struct elog_header));
164
165 buffer_clone(&copy, buf);
166
167 /* Save copy of first event for later */
168 data = buffer_get(buf);
169
170 /* Set buffer offset pointing to the event right after bytes_to_shrink */
171 while (buffer_offset(iter) < bytes_to_shrink) {
172 event = buffer_get(iter);
173 assert(!(event->type == ELOG_TYPE_EOL || event->length == 0));
174
175 buffer_seek(iter, event->length);
176 }
177
178 /* Must be relative to the buffer offset */
179 cleared = buffer_offset(iter) - buffer_offset(buf);
180 remaining = buffer_size(iter);
181
182 /* Overlapping copy */
183 memmove(data, data + cleared, remaining);
184 memset(data + remaining, ELOG_TYPE_EOL, cleared);
185
186 /* Re-init copy to have a clean offset. Needed for init_event() */
187 buffer_clone(&copy, buf);
188 buffer_seek(&copy, next_available_event_offset(&copy));
189
190 if (!eventlog_init_event(&copy, ELOG_TYPE_LOG_CLEAR, &cleared, sizeof(cleared)))
191 return ELOGTOOL_EXIT_NOT_ENOUGH_BUFFER_SPACE;
192
193 return ELOGTOOL_EXIT_SUCCESS;
194}
195
Ricardo Quesada49a96a92021-08-16 11:25:52 -0700196static int cmd_list(const struct buffer *buf)
197{
198 const struct event_header *event;
199 unsigned int count = 0;
200
201 /* Point to the first event */
202 event = buffer_get(buf) + sizeof(struct elog_header);
203
204 while ((const void *)(event) < buffer_end(buf)) {
Ricardo Quesadac2cf3942021-07-16 16:49:04 -0700205 if (event->type == ELOG_TYPE_EOL || event->length == 0)
206 break;
207
208 eventlog_print_event(event, count);
209 event = elog_get_next_event(event);
210 count++;
211 }
212
213 return ELOGTOOL_EXIT_SUCCESS;
214}
215
Ricardo Quesada49a96a92021-08-16 11:25:52 -0700216/*
217 * Clears the elog events from the given buffer, which is a valid RW_ELOG region.
218 * A LOG_CLEAR event is appended.
219 */
Ricardo Quesadaa2bf94d2021-09-03 17:30:31 -0700220static int cmd_clear(const struct buffer *buf)
Ricardo Quesadac2cf3942021-07-16 16:49:04 -0700221{
Ricardo Quesadaa2bf94d2021-09-03 17:30:31 -0700222 uint32_t used_data_size;
223 struct buffer copy;
Ricardo Quesadac2cf3942021-07-16 16:49:04 -0700224
Ricardo Quesada49a96a92021-08-16 11:25:52 -0700225 /* Clone the buffer to avoid changing the offset of the original buffer */
Ricardo Quesadaa2bf94d2021-09-03 17:30:31 -0700226 buffer_clone(&copy, buf);
227 buffer_seek(&copy, sizeof(struct elog_header));
Ricardo Quesadac2cf3942021-07-16 16:49:04 -0700228
Ricardo Quesada49a96a92021-08-16 11:25:52 -0700229 /*
230 * Calculate the size of the "used" buffer, needed for ELOG_TYPE_LOG_CLEAR.
231 * Then overwrite the entire buffer with ELOG_TYPE_EOL.
232 * Finally insert a LOG_CLEAR event into the buffer.
233 */
Ricardo Quesadaa2bf94d2021-09-03 17:30:31 -0700234 used_data_size = next_available_event_offset(&copy);
235 memset(buffer_get(&copy), ELOG_TYPE_EOL, buffer_size(&copy));
Ricardo Quesada49a96a92021-08-16 11:25:52 -0700236
Ricardo Quesadaa2bf94d2021-09-03 17:30:31 -0700237 if (!eventlog_init_event(&copy, ELOG_TYPE_LOG_CLEAR,
Ricardo Quesada49a96a92021-08-16 11:25:52 -0700238 &used_data_size, sizeof(used_data_size)))
239 return ELOGTOOL_EXIT_NOT_ENOUGH_BUFFER_SPACE;
240
241 return ELOGTOOL_EXIT_SUCCESS;
Ricardo Quesadac2cf3942021-07-16 16:49:04 -0700242}
243
Ricardo Quesada683b294d2021-09-03 17:47:43 -0700244static void cmd_add_usage(void)
245{
246 usage(argv0);
247
248 fprintf(stderr, "\n\nSpecific to ADD command:\n"
249 "\n"
250 "<event_type>: an hexadecimal number (0-255). Prefix '0x' is optional\n"
251 "[event_data]: (optional) a series of hexadecimal numbers. Must be:\n"
252 " - len(event_data) %% 2 == 0\n"
Ricardo Quesadae48043b2021-09-20 17:17:07 -0700253 " - len(event_data) in bytes <= %zu\n"
Ricardo Quesada683b294d2021-09-03 17:47:43 -0700254 "\n"
255 "Example:\n"
256 "%s add 0x16 01ABF0 # 01ABF0 is actually three bytes: 0x01, 0xAB and 0xF0\n"
257 "%s add 17 # 17 is in hexa\n",
258 ELOG_MAX_EVENT_DATA_SIZE, argv0, argv0
259 );
260}
261
262static int cmd_add_parse_args(uint8_t *type, uint8_t *data, size_t *data_size)
263{
264 char byte[3] = {0};
265 int argc = 0;
266 char *endptr;
267 long value;
268 int len;
269
270 while (cmd_argv[argc] != NULL)
271 argc++;
272
273 if (argc != 1 && argc != 2)
274 return ELOGTOOL_EXIT_BAD_ARGS;
275
276 /* Force type to be an hexa value to be consistent with the data values */
277 value = strtol(cmd_argv[0], NULL, 16);
278 if (value > 255) {
279 fprintf(stderr, "Error: Event type should be between 0-0xff; "
280 "got: 0x%04lx\n", value);
281 return ELOGTOOL_EXIT_BAD_ARGS;
282 }
283
284 *type = value;
285
286 if (argc == 1)
287 return ELOGTOOL_EXIT_SUCCESS;
288
289 /* Assuming argc == 2 */
290 len = strlen(cmd_argv[1]);
291
292 /* Needs 2 bytes per number */
293 if (len % 2 != 0) {
294 fprintf(stderr,
295 "Error: Event data length should be an even number; got: %d\n", len);
296 return ELOGTOOL_EXIT_BAD_ARGS;
297 }
298
299 *data_size = len / 2;
300
301 if (*data_size > ELOG_MAX_EVENT_DATA_SIZE) {
302 fprintf(stderr,
Ricardo Quesadae48043b2021-09-20 17:17:07 -0700303 "Error: Event data length (in bytes) should be <= %zu; got: %zu\n",
Ricardo Quesada683b294d2021-09-03 17:47:43 -0700304 ELOG_MAX_EVENT_DATA_SIZE, *data_size);
305 return ELOGTOOL_EXIT_BAD_ARGS;
306 }
307
308 for (unsigned int i = 0; i < *data_size; i++) {
309 byte[0] = *cmd_argv[1]++;
310 byte[1] = *cmd_argv[1]++;
311 data[i] = strtol(byte, &endptr, 16);
312 if (endptr != &byte[2]) {
313 fprintf(stderr, "Error: Event data length contains invalid data. "
314 "Only hexa digits are valid\n");
315 return ELOGTOOL_EXIT_BAD_ARGS;
316 }
317 }
318
319 return ELOGTOOL_EXIT_SUCCESS;
320}
321
322/* Appends an elog entry to EventLog buffer. */
323static int cmd_add(const struct buffer *buf)
324{
325 uint8_t data[ELOG_MAX_EVENT_DATA_SIZE];
326 size_t data_size = 0;
327 struct buffer copy;
328 uint8_t type = 0;
329 size_t next_event;
330 size_t threshold;
331 int ret;
332
333 if (cmd_add_parse_args(&type, data, &data_size) != ELOGTOOL_EXIT_SUCCESS) {
334 cmd_add_usage();
335 return ELOGTOOL_EXIT_BAD_ARGS;
336 }
337
338 buffer_clone(&copy, buf);
339 buffer_seek(&copy, sizeof(struct elog_header));
340
341 threshold = buffer_size(&copy) * 3 / 4;
342 next_event = next_available_event_offset(&copy);
343
344 if (next_event > threshold) {
345 /* Shrink ~ 1/4 of the size */
346 ret = shrink_buffer(&copy, buffer_size(buf) - threshold);
347 if (ret != ELOGTOOL_EXIT_SUCCESS)
348 return ret;
349 next_event = next_available_event_offset(&copy);
350 }
351
352 buffer_seek(&copy, next_event);
353
354 if (!eventlog_init_event(&copy, type, data, data_size))
355 return ELOGTOOL_EXIT_NOT_ENOUGH_BUFFER_SPACE;
356
357 return ELOGTOOL_EXIT_SUCCESS;
358}
359
Ricardo Quesadac2cf3942021-07-16 16:49:04 -0700360int main(int argc, char **argv)
361{
Ricardo Quesada49a96a92021-08-16 11:25:52 -0700362 char *filename = NULL;
363 struct buffer buf;
364 unsigned int i;
Ricardo Quesadac2cf3942021-07-16 16:49:04 -0700365 int argflag;
Ricardo Quesada49a96a92021-08-16 11:25:52 -0700366 int ret;
Ricardo Quesadac2cf3942021-07-16 16:49:04 -0700367
368 if (argc < 2) {
369 usage(argv[0]);
370 return ELOGTOOL_EXIT_BAD_ARGS;
371 }
372
373 while (1) {
374 int option_index;
375 argflag = getopt_long(argc, argv, "hf:", long_options, &option_index);
376 if (argflag == -1)
377 break;
378
379 switch (argflag) {
380 case 'h':
381 case '?':
382 usage(argv[0]);
383 return ELOGTOOL_EXIT_SUCCESS;
384
385 case 'f':
386 if (!optarg) {
387 usage(argv[0]);
388 return ELOGTOOL_EXIT_BAD_ARGS;
389 }
390
Ricardo Quesada49a96a92021-08-16 11:25:52 -0700391 filename = optarg;
Ricardo Quesadac2cf3942021-07-16 16:49:04 -0700392 break;
393
394 default:
395 break;
396 }
397 }
398
399 /* At least one command must be available. */
400 if (optind >= argc) {
401 usage(argv[0]);
402 return ELOGTOOL_EXIT_BAD_ARGS;
403 }
404
Ricardo Quesada49a96a92021-08-16 11:25:52 -0700405 /* Returned buffer must be freed. */
406 ret = elog_read(&buf, filename);
407 if (ret)
408 return ret;
Ricardo Quesadac2cf3942021-07-16 16:49:04 -0700409
Ricardo Quesada49a96a92021-08-16 11:25:52 -0700410 for (i = 0; i < ARRAY_SIZE(cmds); i++) {
411 if (!strcmp(cmds[i].name, argv[optind])) {
Ricardo Quesada683b294d2021-09-03 17:47:43 -0700412 /* For commands that parse their own arguments. */
413 cmd_argv = &argv[optind+1];
414 argv0 = argv[0];
Ricardo Quesada49a96a92021-08-16 11:25:52 -0700415 ret = cmds[i].func(&buf);
416 break;
417 }
418 }
419
Ricardo Quesadaa2bf94d2021-09-03 17:30:31 -0700420 if (i == ARRAY_SIZE(cmds)) {
421 usage(argv[0]);
Ricardo Quesada49a96a92021-08-16 11:25:52 -0700422 ret = ELOGTOOL_EXIT_BAD_ARGS;
Ricardo Quesadaa2bf94d2021-09-03 17:30:31 -0700423 }
Ricardo Quesada49a96a92021-08-16 11:25:52 -0700424
425 if (!ret && cmds[i].write_back)
426 ret = elog_write(&buf, filename);
427
428 buffer_delete(&buf);
429
430 return ret;
Ricardo Quesadac2cf3942021-07-16 16:49:04 -0700431}