printk.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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>
  9. #include <asm/setup.h>
  10. #include <asm/sclp.h>
  11. #include <asm/uv.h>
  12. #include "boot.h"
  13. const char hex_asc[] = "0123456789abcdef";
  14. static char *as_hex(char *dst, unsigned long val, int pad)
  15. {
  16. char *p, *end = p = dst + max(pad, (int)__fls(val | 1) / 4 + 1);
  17. for (*p-- = 0; p >= dst; val >>= 4)
  18. *p-- = hex_asc[val & 0x0f];
  19. return end;
  20. }
  21. static char *symstart(char *p)
  22. {
  23. while (*p)
  24. p--;
  25. return p + 1;
  26. }
  27. static noinline char *findsym(unsigned long ip, unsigned short *off, unsigned short *len)
  28. {
  29. /* symbol entries are in a form "10000 c4 startup\0" */
  30. char *a = _decompressor_syms_start;
  31. char *b = _decompressor_syms_end;
  32. unsigned long start;
  33. unsigned long size;
  34. char *pivot;
  35. char *endp;
  36. while (a < b) {
  37. pivot = symstart(a + (b - a) / 2);
  38. start = simple_strtoull(pivot, &endp, 16);
  39. size = simple_strtoull(endp + 1, &endp, 16);
  40. if (ip < start) {
  41. b = pivot;
  42. continue;
  43. }
  44. if (ip > start + size) {
  45. a = pivot + strlen(pivot) + 1;
  46. continue;
  47. }
  48. *off = ip - start;
  49. *len = size;
  50. return endp + 1;
  51. }
  52. return NULL;
  53. }
  54. static noinline char *strsym(void *ip)
  55. {
  56. static char buf[64];
  57. unsigned short off;
  58. unsigned short len;
  59. char *p;
  60. p = findsym((unsigned long)ip, &off, &len);
  61. if (p) {
  62. strncpy(buf, p, sizeof(buf));
  63. /* reserve 15 bytes for offset/len in symbol+0x1234/0x1234 */
  64. p = buf + strnlen(buf, sizeof(buf) - 15);
  65. strcpy(p, "+0x");
  66. p = as_hex(p + 3, off, 0);
  67. strcpy(p, "/0x");
  68. as_hex(p + 3, len, 0);
  69. } else {
  70. as_hex(buf, (unsigned long)ip, 16);
  71. }
  72. return buf;
  73. }
  74. void boot_printk(const char *fmt, ...)
  75. {
  76. char buf[1024] = { 0 };
  77. char *end = buf + sizeof(buf) - 1; /* make sure buf is 0 terminated */
  78. unsigned long pad;
  79. char *p = buf;
  80. va_list args;
  81. va_start(args, fmt);
  82. for (; p < end && *fmt; fmt++) {
  83. if (*fmt != '%') {
  84. *p++ = *fmt;
  85. continue;
  86. }
  87. pad = isdigit(*++fmt) ? simple_strtol(fmt, (char **)&fmt, 10) : 0;
  88. switch (*fmt) {
  89. case 's':
  90. p = buf + strlcat(buf, va_arg(args, char *), sizeof(buf));
  91. break;
  92. case 'p':
  93. if (*++fmt != 'S')
  94. goto out;
  95. p = buf + strlcat(buf, strsym(va_arg(args, void *)), sizeof(buf));
  96. break;
  97. case 'l':
  98. if (*++fmt != 'x' || end - p <= max(sizeof(long) * 2, pad))
  99. goto out;
  100. p = as_hex(p, va_arg(args, unsigned long), pad);
  101. break;
  102. case 'x':
  103. if (end - p <= max(sizeof(int) * 2, pad))
  104. goto out;
  105. p = as_hex(p, va_arg(args, unsigned int), pad);
  106. break;
  107. default:
  108. goto out;
  109. }
  110. }
  111. out:
  112. va_end(args);
  113. sclp_early_printk(buf);
  114. }