sbi_console.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. if (*out) {
  70. if (out_len && (0 < *out_len)) {
  71. **out = ch;
  72. ++(*out);
  73. (*out_len)--;
  74. } else {
  75. **out = ch;
  76. ++(*out);
  77. }
  78. }
  79. } else {
  80. sbi_putc(ch);
  81. }
  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, acnt = 0;
  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 out;
  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. acnt += sizeof(char *);
  195. pc += prints(out, out_len, s ? s : "(null)",
  196. width, flags);
  197. continue;
  198. }
  199. if ((*format == 'd') || (*format == 'i')) {
  200. pc += printi(out, out_len, va_arg(args, int),
  201. 10, 1, width, flags, '0');
  202. acnt += sizeof(int);
  203. continue;
  204. }
  205. if (*format == 'x') {
  206. pc += printi(out, out_len,
  207. va_arg(args, unsigned int), 16, 0,
  208. width, flags, 'a');
  209. acnt += sizeof(unsigned int);
  210. continue;
  211. }
  212. if (*format == 'X') {
  213. pc += printi(out, out_len,
  214. va_arg(args, unsigned int), 16, 0,
  215. width, flags, 'A');
  216. acnt += sizeof(unsigned int);
  217. continue;
  218. }
  219. if (*format == 'u') {
  220. pc += printi(out, out_len,
  221. va_arg(args, unsigned int), 10, 0,
  222. width, flags, 'a');
  223. acnt += sizeof(unsigned int);
  224. continue;
  225. }
  226. if (*format == 'p') {
  227. pc += printi(out, out_len,
  228. va_arg(args, unsigned long), 16, 0,
  229. width, flags, 'a');
  230. acnt += sizeof(unsigned long);
  231. continue;
  232. }
  233. if (*format == 'P') {
  234. pc += printi(out, out_len,
  235. va_arg(args, unsigned long), 16, 0,
  236. width, flags, 'A');
  237. acnt += sizeof(unsigned long);
  238. continue;
  239. }
  240. if (*format == 'l' && *(format + 1) == 'l') {
  241. while (acnt &
  242. (sizeof(unsigned long long) - 1)) {
  243. va_arg(args, int);
  244. acnt += sizeof(int);
  245. }
  246. if (sizeof(unsigned long long) ==
  247. sizeof(unsigned long)) {
  248. tmp = va_arg(args, unsigned long long);
  249. acnt += sizeof(unsigned long long);
  250. } else {
  251. ((unsigned long *)&tmp)[0] =
  252. va_arg(args, unsigned long);
  253. ((unsigned long *)&tmp)[1] =
  254. va_arg(args, unsigned long);
  255. acnt += 2 * sizeof(unsigned long);
  256. }
  257. if (*(format + 2) == 'u') {
  258. format += 2;
  259. pc += printi(out, out_len, tmp, 10, 0,
  260. width, flags, 'a');
  261. } else if (*(format + 2) == 'x') {
  262. format += 2;
  263. pc += printi(out, out_len, tmp, 16, 0,
  264. width, flags, 'a');
  265. } else if (*(format + 2) == 'X') {
  266. format += 2;
  267. pc += printi(out, out_len, tmp, 16, 0,
  268. width, flags, 'A');
  269. } else {
  270. format += 1;
  271. pc += printi(out, out_len, tmp, 10, 1,
  272. width, flags, '0');
  273. }
  274. continue;
  275. } else if (*format == 'l') {
  276. if (*(format + 1) == 'u') {
  277. format += 1;
  278. pc += printi(
  279. out, out_len,
  280. va_arg(args, unsigned long), 10,
  281. 0, width, flags, 'a');
  282. } else if (*(format + 1) == 'x') {
  283. format += 1;
  284. pc += printi(
  285. out, out_len,
  286. va_arg(args, unsigned long), 16,
  287. 0, width, flags, 'a');
  288. acnt += sizeof(unsigned long);
  289. } else if (*(format + 1) == 'X') {
  290. format += 1;
  291. pc += printi(
  292. out, out_len,
  293. va_arg(args, unsigned long), 16,
  294. 0, width, flags, 'A');
  295. acnt += sizeof(unsigned long);
  296. } else {
  297. pc += printi(out, out_len,
  298. va_arg(args, long), 10, 1,
  299. width, flags, '0');
  300. acnt += sizeof(long);
  301. }
  302. }
  303. if (*format == 'c') {
  304. /* char are converted to int then pushed on the stack */
  305. scr[0] = va_arg(args, int);
  306. scr[1] = '\0';
  307. pc += prints(out, out_len, scr, width, flags);
  308. acnt += sizeof(int);
  309. continue;
  310. }
  311. } else {
  312. out:
  313. printc(out, out_len, *format);
  314. ++pc;
  315. }
  316. }
  317. if (out)
  318. **out = '\0';
  319. return pc;
  320. }
  321. int sbi_sprintf(char *out, const char *format, ...)
  322. {
  323. va_list args;
  324. int retval;
  325. va_start(args, format);
  326. retval = print(&out, NULL, format, args);
  327. va_end(args);
  328. return retval;
  329. }
  330. int sbi_snprintf(char *out, u32 out_sz, const char *format, ...)
  331. {
  332. va_list args;
  333. int retval;
  334. va_start(args, format);
  335. retval = print(&out, &out_sz, format, args);
  336. va_end(args);
  337. return retval;
  338. }
  339. int sbi_printf(const char *format, ...)
  340. {
  341. va_list args;
  342. int retval;
  343. spin_lock(&console_out_lock);
  344. va_start(args, format);
  345. retval = print(NULL, NULL, format, args);
  346. va_end(args);
  347. spin_unlock(&console_out_lock);
  348. return retval;
  349. }
  350. int sbi_dprintf(const char *format, ...)
  351. {
  352. va_list args;
  353. int retval = 0;
  354. struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
  355. va_start(args, format);
  356. if (scratch->options & SBI_SCRATCH_DEBUG_PRINTS) {
  357. spin_lock(&console_out_lock);
  358. retval = print(NULL, NULL, format, args);
  359. spin_unlock(&console_out_lock);
  360. }
  361. va_end(args);
  362. return retval;
  363. }
  364. void sbi_panic(const char *format, ...)
  365. {
  366. va_list args;
  367. spin_lock(&console_out_lock);
  368. va_start(args, format);
  369. print(NULL, NULL, format, args);
  370. va_end(args);
  371. spin_unlock(&console_out_lock);
  372. sbi_hart_hang();
  373. }
  374. const struct sbi_console_device *sbi_console_get_device(void)
  375. {
  376. return console_dev;
  377. }
  378. void sbi_console_set_device(const struct sbi_console_device *dev)
  379. {
  380. if (!dev || console_dev)
  381. return;
  382. console_dev = dev;
  383. }
  384. int sbi_console_init(struct sbi_scratch *scratch)
  385. {
  386. return sbi_platform_console_init(sbi_platform_ptr(scratch));
  387. }