sbi_console.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2019 Western Digital Corporation or its affiliates.
  5. *
  6. * Authors:
  7. * Anup Patel <anup.patel@wdc.com>
  8. */
  9. #include <sbi/riscv_locks.h>
  10. #include <sbi/sbi_console.h>
  11. #include <sbi/sbi_hart.h>
  12. #include <sbi/sbi_platform.h>
  13. #include <sbi/sbi_scratch.h>
  14. static const struct sbi_console_device *console_dev = NULL;
  15. static spinlock_t console_out_lock = SPIN_LOCK_INITIALIZER;
  16. bool sbi_isprintable(char c)
  17. {
  18. if (((31 < c) && (c < 127)) || (c == '\f') || (c == '\r') ||
  19. (c == '\n') || (c == '\t')) {
  20. return true;
  21. }
  22. return false;
  23. }
  24. int sbi_getc(void)
  25. {
  26. if (console_dev && console_dev->console_getc)
  27. return console_dev->console_getc();
  28. return -1;
  29. }
  30. void sbi_putc(char ch)
  31. {
  32. if (console_dev && console_dev->console_putc) {
  33. if (ch == '\n')
  34. console_dev->console_putc('\r');
  35. console_dev->console_putc(ch);
  36. }
  37. }
  38. void sbi_puts(const char *str)
  39. {
  40. spin_lock(&console_out_lock);
  41. while (*str) {
  42. sbi_putc(*str);
  43. str++;
  44. }
  45. spin_unlock(&console_out_lock);
  46. }
  47. void sbi_gets(char *s, int maxwidth, char endchar)
  48. {
  49. int ch;
  50. char *retval = s;
  51. while ((ch = sbi_getc()) != endchar && ch >= 0 && maxwidth > 1) {
  52. *retval = (char)ch;
  53. retval++;
  54. maxwidth--;
  55. }
  56. *retval = '\0';
  57. }
  58. #define PAD_RIGHT 1
  59. #define PAD_ZERO 2
  60. #define PAD_ALTERNATE 4
  61. #define PRINT_BUF_LEN 64
  62. #define va_start(v, l) __builtin_va_start((v), l)
  63. #define va_end __builtin_va_end
  64. #define va_arg __builtin_va_arg
  65. typedef __builtin_va_list va_list;
  66. static void printc(char **out, u32 *out_len, char ch)
  67. {
  68. if (!out) {
  69. sbi_putc(ch);
  70. return;
  71. }
  72. /*
  73. * The *printf entry point functions have enforced that (*out) can
  74. * only be null when out_len is non-null and its value is zero.
  75. */
  76. if (!out_len || *out_len > 1) {
  77. *(*out)++ = ch;
  78. **out = '\0';
  79. }
  80. if (out_len && *out_len > 0)
  81. --(*out_len);
  82. }
  83. static int prints(char **out, u32 *out_len, const char *string, int width,
  84. int flags)
  85. {
  86. int pc = 0;
  87. char padchar = ' ';
  88. if (width > 0) {
  89. int len = 0;
  90. const char *ptr;
  91. for (ptr = string; *ptr; ++ptr)
  92. ++len;
  93. if (len >= width)
  94. width = 0;
  95. else
  96. width -= len;
  97. if (flags & PAD_ZERO)
  98. padchar = '0';
  99. }
  100. if (!(flags & PAD_RIGHT)) {
  101. for (; width > 0; --width) {
  102. printc(out, out_len, padchar);
  103. ++pc;
  104. }
  105. }
  106. for (; *string; ++string) {
  107. printc(out, out_len, *string);
  108. ++pc;
  109. }
  110. for (; width > 0; --width) {
  111. printc(out, out_len, padchar);
  112. ++pc;
  113. }
  114. return pc;
  115. }
  116. static int printi(char **out, u32 *out_len, long long i, int b, int sg,
  117. int width, int flags, int letbase)
  118. {
  119. char print_buf[PRINT_BUF_LEN];
  120. char *s;
  121. int neg = 0, pc = 0;
  122. u64 t;
  123. unsigned long long u = i;
  124. if (sg && b == 10 && i < 0) {
  125. neg = 1;
  126. u = -i;
  127. }
  128. s = print_buf + PRINT_BUF_LEN - 1;
  129. *s = '\0';
  130. if (!u) {
  131. *--s = '0';
  132. } else {
  133. while (u) {
  134. t = u % b;
  135. u = u / b;
  136. if (t >= 10)
  137. t += letbase - '0' - 10;
  138. *--s = t + '0';
  139. }
  140. }
  141. if (flags & PAD_ALTERNATE) {
  142. if ((b == 16) && (letbase == 'A')) {
  143. *--s = 'X';
  144. } else if ((b == 16) && (letbase == 'a')) {
  145. *--s = 'x';
  146. }
  147. *--s = '0';
  148. }
  149. if (neg) {
  150. if (width && (flags & PAD_ZERO)) {
  151. printc(out, out_len, '-');
  152. ++pc;
  153. --width;
  154. } else {
  155. *--s = '-';
  156. }
  157. }
  158. return pc + prints(out, out_len, s, width, flags);
  159. }
  160. static int print(char **out, u32 *out_len, const char *format, va_list args)
  161. {
  162. int width, flags;
  163. int pc = 0;
  164. char scr[2];
  165. unsigned long long tmp;
  166. for (; *format != 0; ++format) {
  167. if (*format == '%') {
  168. ++format;
  169. width = flags = 0;
  170. if (*format == '\0')
  171. break;
  172. if (*format == '%')
  173. goto literal;
  174. /* Get flags */
  175. if (*format == '-') {
  176. ++format;
  177. flags = PAD_RIGHT;
  178. }
  179. if (*format == '#') {
  180. ++format;
  181. flags |= PAD_ALTERNATE;
  182. }
  183. while (*format == '0') {
  184. ++format;
  185. flags |= PAD_ZERO;
  186. }
  187. /* Get width */
  188. for (; *format >= '0' && *format <= '9'; ++format) {
  189. width *= 10;
  190. width += *format - '0';
  191. }
  192. if (*format == 's') {
  193. char *s = va_arg(args, char *);
  194. pc += prints(out, out_len, s ? s : "(null)",
  195. width, flags);
  196. continue;
  197. }
  198. if ((*format == 'd') || (*format == 'i')) {
  199. pc += printi(out, out_len, va_arg(args, int),
  200. 10, 1, width, flags, '0');
  201. continue;
  202. }
  203. if (*format == 'x') {
  204. pc += printi(out, out_len,
  205. va_arg(args, unsigned int), 16, 0,
  206. width, flags, 'a');
  207. continue;
  208. }
  209. if (*format == 'X') {
  210. pc += printi(out, out_len,
  211. va_arg(args, unsigned int), 16, 0,
  212. width, flags, 'A');
  213. continue;
  214. }
  215. if (*format == 'u') {
  216. pc += printi(out, out_len,
  217. va_arg(args, unsigned int), 10, 0,
  218. width, flags, 'a');
  219. continue;
  220. }
  221. if (*format == 'p') {
  222. pc += printi(out, out_len,
  223. va_arg(args, unsigned long), 16, 0,
  224. width, flags, 'a');
  225. continue;
  226. }
  227. if (*format == 'P') {
  228. pc += printi(out, out_len,
  229. va_arg(args, unsigned long), 16, 0,
  230. width, flags, 'A');
  231. continue;
  232. }
  233. if (*format == 'l' && *(format + 1) == 'l') {
  234. tmp = va_arg(args, unsigned long long);
  235. if (*(format + 2) == 'u') {
  236. format += 2;
  237. pc += printi(out, out_len, tmp, 10, 0,
  238. width, flags, 'a');
  239. } else if (*(format + 2) == 'x') {
  240. format += 2;
  241. pc += printi(out, out_len, tmp, 16, 0,
  242. width, flags, 'a');
  243. } else if (*(format + 2) == 'X') {
  244. format += 2;
  245. pc += printi(out, out_len, tmp, 16, 0,
  246. width, flags, 'A');
  247. } else {
  248. format += 1;
  249. pc += printi(out, out_len, tmp, 10, 1,
  250. width, flags, '0');
  251. }
  252. continue;
  253. } else if (*format == 'l') {
  254. if (*(format + 1) == 'u') {
  255. format += 1;
  256. pc += printi(
  257. out, out_len,
  258. va_arg(args, unsigned long), 10,
  259. 0, width, flags, 'a');
  260. } else if (*(format + 1) == 'x') {
  261. format += 1;
  262. pc += printi(
  263. out, out_len,
  264. va_arg(args, unsigned long), 16,
  265. 0, width, flags, 'a');
  266. } else if (*(format + 1) == 'X') {
  267. format += 1;
  268. pc += printi(
  269. out, out_len,
  270. va_arg(args, unsigned long), 16,
  271. 0, width, flags, 'A');
  272. } else {
  273. pc += printi(out, out_len,
  274. va_arg(args, long), 10, 1,
  275. width, flags, '0');
  276. }
  277. }
  278. if (*format == 'c') {
  279. /* char are converted to int then pushed on the stack */
  280. scr[0] = va_arg(args, int);
  281. scr[1] = '\0';
  282. pc += prints(out, out_len, scr, width, flags);
  283. continue;
  284. }
  285. } else {
  286. literal:
  287. printc(out, out_len, *format);
  288. ++pc;
  289. }
  290. }
  291. return pc;
  292. }
  293. int sbi_sprintf(char *out, const char *format, ...)
  294. {
  295. va_list args;
  296. int retval;
  297. if (unlikely(!out))
  298. sbi_panic("sbi_sprintf called with NULL output string\n");
  299. va_start(args, format);
  300. retval = print(&out, NULL, format, args);
  301. va_end(args);
  302. return retval;
  303. }
  304. int sbi_snprintf(char *out, u32 out_sz, const char *format, ...)
  305. {
  306. va_list args;
  307. int retval;
  308. if (unlikely(!out && out_sz != 0))
  309. sbi_panic("sbi_snprintf called with NULL output string and "
  310. "output size is not zero\n");
  311. va_start(args, format);
  312. retval = print(&out, &out_sz, format, args);
  313. va_end(args);
  314. return retval;
  315. }
  316. int sbi_printf(const char *format, ...)
  317. {
  318. va_list args;
  319. int retval;
  320. spin_lock(&console_out_lock);
  321. va_start(args, format);
  322. retval = print(NULL, NULL, format, args);
  323. va_end(args);
  324. spin_unlock(&console_out_lock);
  325. return retval;
  326. }
  327. int sbi_dprintf(const char *format, ...)
  328. {
  329. va_list args;
  330. int retval = 0;
  331. struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
  332. va_start(args, format);
  333. if (scratch->options & SBI_SCRATCH_DEBUG_PRINTS) {
  334. spin_lock(&console_out_lock);
  335. retval = print(NULL, NULL, format, args);
  336. spin_unlock(&console_out_lock);
  337. }
  338. va_end(args);
  339. return retval;
  340. }
  341. void sbi_panic(const char *format, ...)
  342. {
  343. va_list args;
  344. spin_lock(&console_out_lock);
  345. va_start(args, format);
  346. print(NULL, NULL, format, args);
  347. va_end(args);
  348. spin_unlock(&console_out_lock);
  349. sbi_hart_hang();
  350. }
  351. const struct sbi_console_device *sbi_console_get_device(void)
  352. {
  353. return console_dev;
  354. }
  355. void sbi_console_set_device(const struct sbi_console_device *dev)
  356. {
  357. if (!dev || console_dev)
  358. return;
  359. console_dev = dev;
  360. }
  361. int sbi_console_init(struct sbi_scratch *scratch)
  362. {
  363. return sbi_platform_console_init(sbi_platform_ptr(scratch));
  364. }