1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/stdarg.h>
4 #include <linux/string.h>
5 #include <linux/ctype.h>
6 #include <asm/stacktrace.h>
7 #include <asm/boot_data.h>
8 #include <asm/lowcore.h>
14 const char hex_asc[] = "0123456789abcdef";
16 static char *as_hex(char *dst, unsigned long val, int pad)
18 char *p, *end = p = dst + max(pad, (int)__fls(val | 1) / 4 + 1);
20 for (*p-- = 0; p >= dst; val >>= 4)
21 *p-- = hex_asc[val & 0x0f];
25 static char *symstart(char *p)
32 static noinline char *findsym(unsigned long ip, unsigned short *off, unsigned short *len)
34 /* symbol entries are in a form "10000 c4 startup\0" */
35 char *a = _decompressor_syms_start;
36 char *b = _decompressor_syms_end;
43 pivot = symstart(a + (b - a) / 2);
44 start = simple_strtoull(pivot, &endp, 16);
45 size = simple_strtoull(endp + 1, &endp, 16);
50 if (ip > start + size) {
51 a = pivot + strlen(pivot) + 1;
61 static noinline char *strsym(void *ip)
68 p = findsym((unsigned long)ip, &off, &len);
70 strncpy(buf, p, sizeof(buf));
71 /* reserve 15 bytes for offset/len in symbol+0x1234/0x1234 */
72 p = buf + strnlen(buf, sizeof(buf) - 15);
74 p = as_hex(p + 3, off, 0);
76 as_hex(p + 3, len, 0);
78 as_hex(buf, (unsigned long)ip, 16);
83 void boot_printk(const char *fmt, ...)
85 char buf[1024] = { 0 };
86 char *end = buf + sizeof(buf) - 1; /* make sure buf is 0 terminated */
92 for (; p < end && *fmt; fmt++) {
97 pad = isdigit(*++fmt) ? simple_strtol(fmt, (char **)&fmt, 10) : 0;
100 p = buf + strlcat(buf, va_arg(args, char *), sizeof(buf));
105 p = buf + strlcat(buf, strsym(va_arg(args, void *)), sizeof(buf));
108 if (*++fmt != 'x' || end - p <= max(sizeof(long) * 2, pad))
110 p = as_hex(p, va_arg(args, unsigned long), pad);
113 if (end - p <= max(sizeof(int) * 2, pad))
115 p = as_hex(p, va_arg(args, unsigned int), pad);
123 sclp_early_printk(buf);