1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright © 2019 Collabora Ltd
14 #include <fdt_support.h>
16 struct persistent_ram_buffer {
23 #define PERSISTENT_RAM_SIG (0x43474244) /* DBGC */
24 #define RAMOOPS_KERNMSG_HDR "===="
26 #define PSTORE_TYPE_DMESG 0
27 #define PSTORE_TYPE_CONSOLE 2
28 #define PSTORE_TYPE_FTRACE 3
29 #define PSTORE_TYPE_PMSG 7
30 #define PSTORE_TYPE_ALL 255
32 static phys_addr_t pstore_addr = CONFIG_CMD_PSTORE_MEM_ADDR;
33 static phys_size_t pstore_length = CONFIG_CMD_PSTORE_MEM_SIZE;
34 static unsigned int pstore_record_size = CONFIG_CMD_PSTORE_RECORD_SIZE;
35 static unsigned int pstore_console_size = CONFIG_CMD_PSTORE_CONSOLE_SIZE;
36 static unsigned int pstore_ftrace_size = CONFIG_CMD_PSTORE_FTRACE_SIZE;
37 static unsigned int pstore_pmsg_size = CONFIG_CMD_PSTORE_PMSG_SIZE;
38 static unsigned int pstore_ecc_size = CONFIG_CMD_PSTORE_ECC_SIZE;
39 static unsigned int buffer_size;
42 * pstore_read_kmsg_hdr() - Check kernel header and get compression flag if
44 * @buffer: Kernel messages buffer.
45 * @compressed: Returns TRUE if kernel buffer is compressed, else FALSE.
47 * Check if buffer starts with a kernel header of the form:
48 * ====<secs>.<nsecs>[-<compression>]\n
49 * If <compression> is equal to 'C' then the buffer is compressed, else iter
52 * Return: Length of kernel header.
54 static int pstore_read_kmsg_hdr(char *buffer, bool *compressed)
59 if (strncmp(RAMOOPS_KERNMSG_HDR, ptr, strlen(RAMOOPS_KERNMSG_HDR)) != 0)
62 ptr += strlen(RAMOOPS_KERNMSG_HDR);
64 ptr = strchr(ptr, '\n');
68 if (ptr[-2] == '-' && ptr[-1] == 'C')
71 return ptr - buffer + 1;
75 * pstore_get_buffer() - Get unwrapped record buffer
76 * @sig: Signature to check
77 * @buffer: Buffer containing wrapped record
78 * @size: wrapped record size
79 * @dest: Buffer used to store unwrapped record
81 * The record starts with <signature><start><size> header.
82 * The signature is 'DBGC' for all records except for Ftrace's record(s) wich
83 * use LINUX_VERSION_CODE ^ 'DBGC'.
84 * Use 0 for @sig to prevent checking signature.
85 * Start and size are 4 bytes long.
87 * Return: record's length
89 static u32 pstore_get_buffer(u32 sig, phys_addr_t buffer, u32 size, char *dest)
91 struct persistent_ram_buffer *prb =
92 (struct persistent_ram_buffer *)map_sysmem(buffer, size);
95 if (sig == 0 || prb->sig == sig) {
97 log_debug("found existing empty buffer\n");
101 if (prb->size > size) {
102 log_debug("found existing invalid buffer, size %u, start %u\n",
103 prb->size, prb->start);
107 log_debug("no valid data in buffer (sig = 0x%08x)\n", prb->sig);
111 log_debug("found existing buffer, size %u, start %u\n",
112 prb->size, prb->start);
114 memcpy(dest, &prb->data[prb->start], prb->size - prb->start);
115 memcpy(dest + prb->size - prb->start, &prb->data[0], prb->start);
117 dest_size = prb->size;
124 * pstore_init_buffer_size() - Init buffer size to largest record size
126 * Records, console, FTrace and user logs can use different buffer sizes.
127 * This function allows to retrieve the biggest one.
129 static void pstore_init_buffer_size(void)
131 if (pstore_record_size > buffer_size)
132 buffer_size = pstore_record_size;
134 if (pstore_console_size > buffer_size)
135 buffer_size = pstore_console_size;
137 if (pstore_ftrace_size > buffer_size)
138 buffer_size = pstore_ftrace_size;
140 if (pstore_pmsg_size > buffer_size)
141 buffer_size = pstore_pmsg_size;
145 * pstore_set() - Initialize PStore settings from command line arguments
146 * @cmdtp: Command data struct pointer
147 * @flag: Command flag
148 * @argc: Command-line argument count
149 * @argv: Array of command-line arguments
151 * Set pstore reserved memory info, starting at 'addr' for 'len' bytes.
152 * Default length for records is 4K.
153 * Mandatory arguments:
154 * - addr: ramoops starting address
155 * - len: ramoops total length
156 * Optional arguments:
157 * - record-size: size of one panic or oops record ('dump' type)
158 * - console-size: size of the kernel logs record
159 * - ftrace-size: size of the ftrace record(s), this can be a single record or
160 * divided in parts based on number of CPUs
161 * - pmsg-size: size of the user space logs record
162 * - ecc-size: enables/disables ECC support and specifies ECC buffer size in
163 * bytes (0 disables it, 1 is a special value, means 16 bytes ECC)
165 * Return: zero on success, CMD_RET_USAGE in case of misuse and negative
168 static int pstore_set(struct cmd_tbl *cmdtp, int flag, int argc,
172 return CMD_RET_USAGE;
174 /* Address is specified since argc > 2
176 pstore_addr = hextoul(argv[1], NULL);
178 /* Length is specified since argc > 2
180 pstore_length = hextoul(argv[2], NULL);
183 pstore_record_size = hextoul(argv[3], NULL);
186 pstore_console_size = hextoul(argv[4], NULL);
189 pstore_ftrace_size = hextoul(argv[5], NULL);
192 pstore_pmsg_size = hextoul(argv[6], NULL);
195 pstore_ecc_size = hextoul(argv[7], NULL);
197 if (pstore_length < (pstore_record_size + pstore_console_size
198 + pstore_ftrace_size + pstore_pmsg_size)) {
199 printf("pstore <len> should be larger than the sum of all records sizes\n");
203 log_debug("pstore set done: start 0x%08llx - length 0x%llx\n",
204 (unsigned long long)pstore_addr,
205 (unsigned long long)pstore_length);
211 * pstore_print_buffer() - Print buffer
213 * @buffer: buffer to print
216 * Print buffer type and content
218 static void pstore_print_buffer(char *type, char *buffer, u32 size)
222 printf("**** %s\n", type);
223 while (i < size && buffer[i] != 0) {
230 * pstore_display() - Display existing records in pstore reserved memory
231 * @cmdtp: Command data struct pointer
232 * @flag: Command flag
233 * @argc: Command-line argument count
234 * @argv: Array of command-line arguments
236 * A 'record-type' can be given to only display records of this kind.
237 * If no 'record-type' is given, all valid records are dispayed.
238 * 'record-type' can be one of 'dump', 'console', 'ftrace' or 'user'. For 'dump'
239 * and 'ftrace' types, a 'nb' can be given to only display one record.
241 * Return: zero on success, CMD_RET_USAGE in case of misuse and negative
244 static int pstore_display(struct cmd_tbl *cmdtp, int flag, int argc,
247 int type = PSTORE_TYPE_ALL;
255 if (!strcmp(argv[1], "dump"))
256 type = PSTORE_TYPE_DMESG;
257 else if (!strcmp(argv[1], "console"))
258 type = PSTORE_TYPE_CONSOLE;
259 else if (!strcmp(argv[1], "ftrace"))
260 type = PSTORE_TYPE_FTRACE;
261 else if (!strcmp(argv[1], "user"))
262 type = PSTORE_TYPE_PMSG;
264 return CMD_RET_USAGE;
267 if (pstore_length == 0) {
268 printf("Please set PStore configuration\n");
269 return CMD_RET_USAGE;
272 if (buffer_size == 0)
273 pstore_init_buffer_size();
275 buffer = malloc_cache_aligned(buffer_size);
277 if (type == PSTORE_TYPE_DMESG || type == PSTORE_TYPE_ALL) {
279 phys_addr_t ptr_end = ptr + pstore_length - pstore_pmsg_size
280 - pstore_ftrace_size - pstore_console_size;
283 ptr += dectoul(argv[2], NULL)
284 * pstore_record_size;
285 ptr_end = ptr + pstore_record_size;
288 while (ptr < ptr_end) {
289 size = pstore_get_buffer(PERSISTENT_RAM_SIG, ptr,
290 pstore_record_size, buffer);
291 ptr += pstore_record_size;
296 header_len = pstore_read_kmsg_hdr(buffer, &compressed);
297 if (header_len == 0) {
298 log_debug("no valid kernel header\n");
303 printf("Compressed buffer, display not available\n");
307 pstore_print_buffer("Dump", buffer + header_len,
312 if (type == PSTORE_TYPE_CONSOLE || type == PSTORE_TYPE_ALL) {
313 ptr = pstore_addr + pstore_length - pstore_pmsg_size
314 - pstore_ftrace_size - pstore_console_size;
315 size = pstore_get_buffer(PERSISTENT_RAM_SIG, ptr,
316 pstore_console_size, buffer);
318 pstore_print_buffer("Console", buffer, size);
321 if (type == PSTORE_TYPE_FTRACE || type == PSTORE_TYPE_ALL) {
322 ptr = pstore_addr + pstore_length - pstore_pmsg_size
323 - pstore_ftrace_size;
324 /* The FTrace record(s) uses LINUX_VERSION_CODE ^ 'DBGC'
325 * signature, pass 0 to pstore_get_buffer to prevent
328 size = pstore_get_buffer(0, ptr, pstore_ftrace_size, buffer);
330 pstore_print_buffer("FTrace", buffer, size);
333 if (type == PSTORE_TYPE_PMSG || type == PSTORE_TYPE_ALL) {
334 ptr = pstore_addr + pstore_length - pstore_pmsg_size;
335 size = pstore_get_buffer(PERSISTENT_RAM_SIG, ptr,
336 pstore_pmsg_size, buffer);
338 pstore_print_buffer("User", buffer, size);
347 * pstore_save() - Save existing records from pstore reserved memory
348 * @cmdtp: Command data struct pointer
349 * @flag: Command flag
350 * @argc: Command-line argument count
351 * @argv: Array of command-line arguments
353 * the records are saved under 'directory path', which should already exist,
354 * to partition 'part' on device type 'interface' instance 'dev'
355 * Filenames are automatically generated, depending on record type, like in
356 * /sys/fs/pstore under Linux
358 * Return: zero on success, CMD_RET_USAGE in case of misuse and negative
361 static int pstore_save(struct cmd_tbl *cmdtp, int flag, int argc,
364 phys_addr_t ptr, ptr_end;
367 char addr[19], length[19];
375 return CMD_RET_USAGE;
377 if (pstore_length == 0) {
378 printf("Please set PStore configuration\n");
379 return CMD_RET_USAGE;
382 if (buffer_size == 0)
383 pstore_init_buffer_size();
385 buffer = malloc_cache_aligned(buffer_size);
386 sprintf(addr, "0x%p", buffer);
388 save_argv[0] = argv[0];
389 save_argv[1] = argv[1];
390 save_argv[2] = argv[2];
393 save_argv[5] = length;
395 /* Save all Dump records */
397 ptr_end = ptr + pstore_length - pstore_pmsg_size - pstore_ftrace_size
398 - pstore_console_size;
400 while (ptr < ptr_end) {
401 size = pstore_get_buffer(PERSISTENT_RAM_SIG, ptr,
402 pstore_record_size, buffer);
403 ptr += pstore_record_size;
408 header_len = pstore_read_kmsg_hdr(buffer, &compressed);
409 if (header_len == 0) {
410 log_debug("no valid kernel header\n");
414 sprintf(addr, "0x%08lx", (ulong)map_to_sysmem(buffer + header_len));
415 sprintf(length, "0x%X", size - header_len);
416 sprintf(path, "%s/dmesg-ramoops-%u%s", argv[3], index,
417 compressed ? ".enc.z" : "");
418 do_save(cmdtp, flag, 6, save_argv, FS_TYPE_ANY);
422 sprintf(addr, "0x%08lx", (ulong)map_to_sysmem(buffer));
424 /* Save Console record */
425 size = pstore_get_buffer(PERSISTENT_RAM_SIG, ptr, pstore_console_size,
428 sprintf(length, "0x%X", size);
429 sprintf(path, "%s/console-ramoops-0", argv[3]);
430 do_save(cmdtp, flag, 6, save_argv, FS_TYPE_ANY);
432 ptr += pstore_console_size;
434 /* Save FTrace record(s)
435 * The FTrace record(s) uses LINUX_VERSION_CODE ^ 'DBGC' signature,
436 * pass 0 to pstore_get_buffer to prevent checking it
438 size = pstore_get_buffer(0, ptr, pstore_ftrace_size, buffer);
440 sprintf(length, "0x%X", size);
441 sprintf(path, "%s/ftrace-ramoops-0", argv[3]);
442 do_save(cmdtp, flag, 6, save_argv, FS_TYPE_ANY);
444 ptr += pstore_ftrace_size;
446 /* Save Console record */
447 size = pstore_get_buffer(PERSISTENT_RAM_SIG, ptr, pstore_pmsg_size,
450 sprintf(length, "0x%X", size);
451 sprintf(path, "%s/pmsg-ramoops-0", argv[3]);
452 do_save(cmdtp, flag, 6, save_argv, FS_TYPE_ANY);
460 static struct cmd_tbl cmd_pstore_sub[] = {
461 U_BOOT_CMD_MKENT(set, 8, 0, pstore_set, "", ""),
462 U_BOOT_CMD_MKENT(display, 3, 0, pstore_display, "", ""),
463 U_BOOT_CMD_MKENT(save, 4, 0, pstore_save, "", ""),
466 static int do_pstore(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
471 return CMD_RET_USAGE;
473 /* Strip off leading argument */
477 c = find_cmd_tbl(argv[0], cmd_pstore_sub, ARRAY_SIZE(cmd_pstore_sub));
480 return CMD_RET_USAGE;
482 return c->cmd(cmdtp, flag, argc, argv);
485 void fdt_fixup_pstore(void *blob)
488 int nodeoffset; /* node offset from libfdt */
492 nodeoffset = fdt_path_offset(blob, "/");
493 if (nodeoffset < 0) {
494 /* Not found or something else bad happened. */
495 log_err("fdt_path_offset() returned %s\n", fdt_strerror(nodeoffset));
499 nodeoffset = fdt_find_or_add_subnode(blob, nodeoffset, "reserved-memory");
500 if (nodeoffset < 0) {
501 log_err("Add 'reserved-memory' node failed: %s\n",
502 fdt_strerror(nodeoffset));
506 addr_cells = fdt_getprop_u32_default_node(blob, nodeoffset, 0, "#address-cells", 2);
507 size_cells = fdt_getprop_u32_default_node(blob, nodeoffset, 0, "#size-cells", 2);
508 fdt_setprop_u32(blob, nodeoffset, "#address-cells", addr_cells);
509 fdt_setprop_u32(blob, nodeoffset, "#size-cells", size_cells);
511 fdt_setprop_empty(blob, nodeoffset, "ranges");
513 sprintf(node, "ramoops@%llx", (unsigned long long)pstore_addr);
514 nodeoffset = fdt_add_subnode(blob, nodeoffset, node);
515 if (nodeoffset < 0) {
516 log_err("Add '%s' node failed: %s\n", node, fdt_strerror(nodeoffset));
520 fdt_setprop_string(blob, nodeoffset, "compatible", "ramoops");
522 if (addr_cells == 1) {
523 fdt_setprop_u32(blob, nodeoffset, "reg", pstore_addr);
524 } else if (addr_cells == 2) {
525 fdt_setprop_u64(blob, nodeoffset, "reg", pstore_addr);
527 log_err("Unsupported #address-cells: %u\n", addr_cells);
531 if (size_cells == 1) {
532 // Let's consider that the pstore_length fits in a 32 bits value
533 fdt_appendprop_u32(blob, nodeoffset, "reg", pstore_length);
534 } else if (size_cells == 2) {
535 fdt_appendprop_u64(blob, nodeoffset, "reg", pstore_length);
537 log_err("Unsupported #size-cells: %u\n", addr_cells);
541 fdt_setprop_u32(blob, nodeoffset, "record-size", pstore_record_size);
542 fdt_setprop_u32(blob, nodeoffset, "console-size", pstore_console_size);
543 fdt_setprop_u32(blob, nodeoffset, "ftrace-size", pstore_ftrace_size);
544 fdt_setprop_u32(blob, nodeoffset, "pmsg-size", pstore_pmsg_size);
545 fdt_setprop_u32(blob, nodeoffset, "ecc-size", pstore_ecc_size);
548 fdt_del_node_and_alias(blob, node);
551 U_BOOT_CMD(pstore, 10, 0, do_pstore,
552 "Manage Linux Persistent Storage",
553 "set <addr> <len> [record-size] [console-size] [ftrace-size] [pmsg_size] [ecc-size]\n"
554 "- Set pstore reserved memory info, starting at 'addr' for 'len' bytes.\n"
555 " Default length for records is 4K.\n"
556 " 'record-size' is the size of one panic or oops record ('dump' type).\n"
557 " 'console-size' is the size of the kernel logs record.\n"
558 " 'ftrace-size' is the size of the ftrace record(s), this can be a single\n"
559 " record or divided in parts based on number of CPUs.\n"
560 " 'pmsg-size' is the size of the user space logs record.\n"
561 " 'ecc-size' enables/disables ECC support and specifies ECC buffer size in\n"
562 " bytes (0 disables it, 1 is a special value, means 16 bytes ECC).\n"
563 "pstore display [record-type] [nb]\n"
564 "- Display existing records in pstore reserved memory. A 'record-type' can\n"
565 " be given to only display records of this kind. 'record-type' can be one\n"
566 " of 'dump', 'console', 'ftrace' or 'user'. For 'dump' and 'ftrace' types,\n"
567 " a 'nb' can be given to only display one record.\n"
568 "pstore save <interface> <dev[:part]> <directory-path>\n"
569 "- Save existing records in pstore reserved memory under 'directory path'\n"
570 " to partition 'part' on device type 'interface' instance 'dev'.\n"
571 " Filenames are automatically generated, depending on record type, like\n"
572 " in /sys/fs/pstore under Linux.\n"
573 " The 'directory-path' should already exist.\n"