1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright © 2019 Collabora Ltd
14 struct persistent_ram_buffer {
21 #define PERSISTENT_RAM_SIG (0x43474244) /* DBGC */
22 #define RAMOOPS_KERNMSG_HDR "===="
24 #define PSTORE_TYPE_DMESG 0
25 #define PSTORE_TYPE_CONSOLE 2
26 #define PSTORE_TYPE_FTRACE 3
27 #define PSTORE_TYPE_PMSG 7
28 #define PSTORE_TYPE_ALL 255
30 static phys_addr_t pstore_addr = CONFIG_CMD_PSTORE_MEM_ADDR;
31 static phys_size_t pstore_length = CONFIG_CMD_PSTORE_MEM_SIZE;
32 static unsigned int pstore_record_size = CONFIG_CMD_PSTORE_RECORD_SIZE;
33 static unsigned int pstore_console_size = CONFIG_CMD_PSTORE_CONSOLE_SIZE;
34 static unsigned int pstore_ftrace_size = CONFIG_CMD_PSTORE_FTRACE_SIZE;
35 static unsigned int pstore_pmsg_size = CONFIG_CMD_PSTORE_PMSG_SIZE;
36 static unsigned int pstore_ecc_size = CONFIG_CMD_PSTORE_ECC_SIZE;
37 static unsigned int buffer_size;
40 * pstore_read_kmsg_hdr() - Check kernel header and get compression flag if
42 * @buffer: Kernel messages buffer.
43 * @compressed: Returns TRUE if kernel buffer is compressed, else FALSE.
45 * Check if buffer starts with a kernel header of the form:
46 * ====<secs>.<nsecs>[-<compression>]\n
47 * If <compression> is equal to 'C' then the buffer is compressed, else iter
50 * Return: Length of kernel header.
52 static int pstore_read_kmsg_hdr(char *buffer, bool *compressed)
57 if (strncmp(RAMOOPS_KERNMSG_HDR, ptr, strlen(RAMOOPS_KERNMSG_HDR)) != 0)
60 ptr += strlen(RAMOOPS_KERNMSG_HDR);
62 ptr = strchr(ptr, '\n');
66 if (ptr[-2] == '-' && ptr[-1] == 'C')
69 return ptr - buffer + 1;
73 * pstore_get_buffer() - Get unwrapped record buffer
74 * @sig: Signature to check
75 * @buffer: Buffer containing wrapped record
76 * @size: wrapped record size
77 * @dest: Buffer used to store unwrapped record
79 * The record starts with <signature><start><size> header.
80 * The signature is 'DBGC' for all records except for Ftrace's record(s) wich
81 * use LINUX_VERSION_CODE ^ 'DBGC'.
82 * Use 0 for @sig to prevent checking signature.
83 * Start and size are 4 bytes long.
85 * Return: record's length
87 static u32 pstore_get_buffer(u32 sig, phys_addr_t buffer, u32 size, char *dest)
89 struct persistent_ram_buffer *prb =
90 (struct persistent_ram_buffer *)map_sysmem(buffer, size);
93 if (sig == 0 || prb->sig == sig) {
95 log_debug("found existing empty buffer\n");
99 if (prb->size > size) {
100 log_debug("found existing invalid buffer, size %u, start %u\n",
101 prb->size, prb->start);
105 log_debug("no valid data in buffer (sig = 0x%08x)\n", prb->sig);
109 log_debug("found existing buffer, size %u, start %u\n",
110 prb->size, prb->start);
112 memcpy(dest, &prb->data[prb->start], prb->size - prb->start);
113 memcpy(dest + prb->size - prb->start, &prb->data[0], prb->start);
115 dest_size = prb->size;
122 * pstore_init_buffer_size() - Init buffer size to largest record size
124 * Records, console, FTrace and user logs can use different buffer sizes.
125 * This function allows to retrieve the biggest one.
127 static void pstore_init_buffer_size(void)
129 if (pstore_record_size > buffer_size)
130 buffer_size = pstore_record_size;
132 if (pstore_console_size > buffer_size)
133 buffer_size = pstore_console_size;
135 if (pstore_ftrace_size > buffer_size)
136 buffer_size = pstore_ftrace_size;
138 if (pstore_pmsg_size > buffer_size)
139 buffer_size = pstore_pmsg_size;
143 * pstore_set() - Initialize PStore settings from command line arguments
144 * @cmdtp: Command data struct pointer
145 * @flag: Command flag
146 * @argc: Command-line argument count
147 * @argv: Array of command-line arguments
149 * Set pstore reserved memory info, starting at 'addr' for 'len' bytes.
150 * Default length for records is 4K.
151 * Mandatory arguments:
152 * - addr: ramoops starting address
153 * - len: ramoops total length
154 * Optional arguments:
155 * - record-size: size of one panic or oops record ('dump' type)
156 * - console-size: size of the kernel logs record
157 * - ftrace-size: size of the ftrace record(s), this can be a single record or
158 * divided in parts based on number of CPUs
159 * - pmsg-size: size of the user space logs record
160 * - ecc-size: enables/disables ECC support and specifies ECC buffer size in
161 * bytes (0 disables it, 1 is a special value, means 16 bytes ECC)
163 * Return: zero on success, CMD_RET_USAGE in case of misuse and negative
166 static int pstore_set(struct cmd_tbl *cmdtp, int flag, int argc,
170 return CMD_RET_USAGE;
172 /* Address is specified since argc > 2
174 pstore_addr = simple_strtoul(argv[1], NULL, 16);
176 /* Length is specified since argc > 2
178 pstore_length = simple_strtoul(argv[2], NULL, 16);
181 pstore_record_size = simple_strtoul(argv[3], NULL, 16);
184 pstore_console_size = simple_strtoul(argv[4], NULL, 16);
187 pstore_ftrace_size = simple_strtoul(argv[5], NULL, 16);
190 pstore_pmsg_size = simple_strtoul(argv[6], NULL, 16);
193 pstore_ecc_size = simple_strtoul(argv[7], NULL, 16);
195 if (pstore_length < (pstore_record_size + pstore_console_size
196 + pstore_ftrace_size + pstore_pmsg_size)) {
197 printf("pstore <len> should be larger than the sum of all records sizes\n");
201 log_debug("pstore set done: start 0x%08llx - length 0x%llx\n",
202 (unsigned long long)pstore_addr,
203 (unsigned long long)pstore_length);
209 * pstore_print_buffer() - Print buffer
211 * @buffer: buffer to print
214 * Print buffer type and content
216 static void pstore_print_buffer(char *type, char *buffer, u32 size)
220 printf("**** %s\n", type);
221 while (i < size && buffer[i] != 0) {
228 * pstore_display() - Display existing records in pstore reserved memory
229 * @cmdtp: Command data struct pointer
230 * @flag: Command flag
231 * @argc: Command-line argument count
232 * @argv: Array of command-line arguments
234 * A 'record-type' can be given to only display records of this kind.
235 * If no 'record-type' is given, all valid records are dispayed.
236 * 'record-type' can be one of 'dump', 'console', 'ftrace' or 'user'. For 'dump'
237 * and 'ftrace' types, a 'nb' can be given to only display one record.
239 * Return: zero on success, CMD_RET_USAGE in case of misuse and negative
242 static int pstore_display(struct cmd_tbl *cmdtp, int flag, int argc,
245 int type = PSTORE_TYPE_ALL;
253 if (!strcmp(argv[1], "dump"))
254 type = PSTORE_TYPE_DMESG;
255 else if (!strcmp(argv[1], "console"))
256 type = PSTORE_TYPE_CONSOLE;
257 else if (!strcmp(argv[1], "ftrace"))
258 type = PSTORE_TYPE_FTRACE;
259 else if (!strcmp(argv[1], "user"))
260 type = PSTORE_TYPE_PMSG;
262 return CMD_RET_USAGE;
265 if (pstore_length == 0) {
266 printf("Please set PStore configuration\n");
267 return CMD_RET_USAGE;
270 if (buffer_size == 0)
271 pstore_init_buffer_size();
273 buffer = malloc_cache_aligned(buffer_size);
275 if (type == PSTORE_TYPE_DMESG || type == PSTORE_TYPE_ALL) {
277 phys_addr_t ptr_end = ptr + pstore_length - pstore_pmsg_size
278 - pstore_ftrace_size - pstore_console_size;
281 ptr += simple_strtoul(argv[2], NULL, 10)
282 * pstore_record_size;
283 ptr_end = ptr + pstore_record_size;
286 while (ptr < ptr_end) {
287 size = pstore_get_buffer(PERSISTENT_RAM_SIG, ptr,
288 pstore_record_size, buffer);
289 ptr += pstore_record_size;
294 header_len = pstore_read_kmsg_hdr(buffer, &compressed);
295 if (header_len == 0) {
296 log_debug("no valid kernel header\n");
301 printf("Compressed buffer, display not available\n");
305 pstore_print_buffer("Dump", buffer + header_len,
310 if (type == PSTORE_TYPE_CONSOLE || type == PSTORE_TYPE_ALL) {
311 ptr = pstore_addr + pstore_length - pstore_pmsg_size
312 - pstore_ftrace_size - pstore_console_size;
313 size = pstore_get_buffer(PERSISTENT_RAM_SIG, ptr,
314 pstore_console_size, buffer);
316 pstore_print_buffer("Console", buffer, size);
319 if (type == PSTORE_TYPE_FTRACE || type == PSTORE_TYPE_ALL) {
320 ptr = pstore_addr + pstore_length - pstore_pmsg_size
321 - pstore_ftrace_size;
322 /* The FTrace record(s) uses LINUX_VERSION_CODE ^ 'DBGC'
323 * signature, pass 0 to pstore_get_buffer to prevent
326 size = pstore_get_buffer(0, ptr, pstore_ftrace_size, buffer);
328 pstore_print_buffer("FTrace", buffer, size);
331 if (type == PSTORE_TYPE_PMSG || type == PSTORE_TYPE_ALL) {
332 ptr = pstore_addr + pstore_length - pstore_pmsg_size;
333 size = pstore_get_buffer(PERSISTENT_RAM_SIG, ptr,
334 pstore_pmsg_size, buffer);
336 pstore_print_buffer("User", buffer, size);
345 * pstore_save() - Save existing records from pstore reserved memory
346 * @cmdtp: Command data struct pointer
347 * @flag: Command flag
348 * @argc: Command-line argument count
349 * @argv: Array of command-line arguments
351 * the records are saved under 'directory path', which should already exist,
352 * to partition 'part' on device type 'interface' instance 'dev'
353 * Filenames are automatically generated, depending on record type, like in
354 * /sys/fs/pstore under Linux
356 * Return: zero on success, CMD_RET_USAGE in case of misuse and negative
359 static int pstore_save(struct cmd_tbl *cmdtp, int flag, int argc,
362 phys_addr_t ptr, ptr_end;
365 char addr[19], length[19];
373 return CMD_RET_USAGE;
375 if (pstore_length == 0) {
376 printf("Please set PStore configuration\n");
377 return CMD_RET_USAGE;
380 if (buffer_size == 0)
381 pstore_init_buffer_size();
383 buffer = malloc_cache_aligned(buffer_size);
384 sprintf(addr, "0x%p", buffer);
386 save_argv[0] = argv[0];
387 save_argv[1] = argv[1];
388 save_argv[2] = argv[2];
391 save_argv[5] = length;
393 /* Save all Dump records */
395 ptr_end = ptr + pstore_length - pstore_pmsg_size - pstore_ftrace_size
396 - pstore_console_size;
398 while (ptr < ptr_end) {
399 size = pstore_get_buffer(PERSISTENT_RAM_SIG, ptr,
400 pstore_record_size, buffer);
401 ptr += pstore_record_size;
406 header_len = pstore_read_kmsg_hdr(buffer, &compressed);
407 if (header_len == 0) {
408 log_debug("no valid kernel header\n");
412 sprintf(addr, "0x%08lx", (ulong)map_to_sysmem(buffer + header_len));
413 sprintf(length, "0x%X", size - header_len);
414 sprintf(path, "%s/dmesg-ramoops-%u%s", argv[3], index,
415 compressed ? ".enc.z" : "");
416 do_save(cmdtp, flag, 6, save_argv, FS_TYPE_ANY);
420 sprintf(addr, "0x%08lx", (ulong)map_to_sysmem(buffer));
422 /* Save Console record */
423 size = pstore_get_buffer(PERSISTENT_RAM_SIG, ptr, pstore_console_size,
426 sprintf(length, "0x%X", size);
427 sprintf(path, "%s/console-ramoops-0", argv[3]);
428 do_save(cmdtp, flag, 6, save_argv, FS_TYPE_ANY);
430 ptr += pstore_console_size;
432 /* Save FTrace record(s)
433 * The FTrace record(s) uses LINUX_VERSION_CODE ^ 'DBGC' signature,
434 * pass 0 to pstore_get_buffer to prevent checking it
436 size = pstore_get_buffer(0, ptr, pstore_ftrace_size, buffer);
438 sprintf(length, "0x%X", size);
439 sprintf(path, "%s/ftrace-ramoops-0", argv[3]);
440 do_save(cmdtp, flag, 6, save_argv, FS_TYPE_ANY);
442 ptr += pstore_ftrace_size;
444 /* Save Console record */
445 size = pstore_get_buffer(PERSISTENT_RAM_SIG, ptr, pstore_pmsg_size,
448 sprintf(length, "0x%X", size);
449 sprintf(path, "%s/pmsg-ramoops-0", argv[3]);
450 do_save(cmdtp, flag, 6, save_argv, FS_TYPE_ANY);
458 static struct cmd_tbl cmd_pstore_sub[] = {
459 U_BOOT_CMD_MKENT(set, 8, 0, pstore_set, "", ""),
460 U_BOOT_CMD_MKENT(display, 3, 0, pstore_display, "", ""),
461 U_BOOT_CMD_MKENT(save, 4, 0, pstore_save, "", ""),
464 static int do_pstore(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
469 return CMD_RET_USAGE;
471 /* Strip off leading argument */
475 c = find_cmd_tbl(argv[0], cmd_pstore_sub, ARRAY_SIZE(cmd_pstore_sub));
478 return CMD_RET_USAGE;
480 return c->cmd(cmdtp, flag, argc, argv);
483 void fdt_fixup_pstore(void *blob)
486 int nodeoffset; /* node offset from libfdt */
488 nodeoffset = fdt_path_offset(blob, "/");
489 if (nodeoffset < 0) {
490 /* Not found or something else bad happened. */
491 log_err("fdt_path_offset() returned %s\n", fdt_strerror(nodeoffset));
495 nodeoffset = fdt_add_subnode(blob, nodeoffset, "reserved-memory");
496 if (nodeoffset < 0) {
497 log_err("Add 'reserved-memory' node failed: %s\n",
498 fdt_strerror(nodeoffset));
501 fdt_setprop_u32(blob, nodeoffset, "#address-cells", 2);
502 fdt_setprop_u32(blob, nodeoffset, "#size-cells", 2);
503 fdt_setprop_empty(blob, nodeoffset, "ranges");
505 sprintf(node, "ramoops@%llx", (unsigned long long)pstore_addr);
506 nodeoffset = fdt_add_subnode(blob, nodeoffset, node);
507 if (nodeoffset < 0) {
508 log_err("Add '%s' node failed: %s\n", node, fdt_strerror(nodeoffset));
511 fdt_setprop_string(blob, nodeoffset, "compatible", "ramoops");
512 fdt_setprop_u64(blob, nodeoffset, "reg", pstore_addr);
513 fdt_appendprop_u64(blob, nodeoffset, "reg", pstore_length);
514 fdt_setprop_u32(blob, nodeoffset, "record-size", pstore_record_size);
515 fdt_setprop_u32(blob, nodeoffset, "console-size", pstore_console_size);
516 fdt_setprop_u32(blob, nodeoffset, "ftrace-size", pstore_ftrace_size);
517 fdt_setprop_u32(blob, nodeoffset, "pmsg-size", pstore_pmsg_size);
518 fdt_setprop_u32(blob, nodeoffset, "ecc-size", pstore_ecc_size);
521 U_BOOT_CMD(pstore, 10, 0, do_pstore,
522 "Manage Linux Persistent Storage",
523 "set <addr> <len> [record-size] [console-size] [ftrace-size] [pmsg_size] [ecc-size]\n"
524 "- Set pstore reserved memory info, starting at 'addr' for 'len' bytes.\n"
525 " Default length for records is 4K.\n"
526 " 'record-size' is the size of one panic or oops record ('dump' type).\n"
527 " 'console-size' is the size of the kernel logs record.\n"
528 " 'ftrace-size' is the size of the ftrace record(s), this can be a single\n"
529 " record or divided in parts based on number of CPUs.\n"
530 " 'pmsg-size' is the size of the user space logs record.\n"
531 " 'ecc-size' enables/disables ECC support and specifies ECC buffer size in\n"
532 " bytes (0 disables it, 1 is a special value, means 16 bytes ECC).\n"
533 "pstore display [record-type] [nb]\n"
534 "- Display existing records in pstore reserved memory. A 'record-type' can\n"
535 " be given to only display records of this kind. 'record-type' can be one\n"
536 " of 'dump', 'console', 'ftrace' or 'user'. For 'dump' and 'ftrace' types,\n"
537 " a 'nb' can be given to only display one record.\n"
538 "pstore save <interface> <dev[:part]> <directory-path>\n"
539 "- Save existing records in pstore reserved memory under 'directory path'\n"
540 " to partition 'part' on device type 'interface' instance 'dev'.\n"
541 " Filenames are automatically generated, depending on record type, like\n"
542 " in /sys/fs/pstore under Linux.\n"
543 " The 'directory-path' should already exist.\n"