sandbox.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2011 The Chromium OS Authors.
  4. */
  5. /*
  6. * This provide a test serial port. It provides an emulated serial port where
  7. * a test program and read out the serial output and inject serial input for
  8. * U-Boot.
  9. */
  10. #include <common.h>
  11. #include <dm.h>
  12. #include <fdtdec.h>
  13. #include <lcd.h>
  14. #include <os.h>
  15. #include <serial.h>
  16. #include <video.h>
  17. #include <linux/compiler.h>
  18. #include <asm/state.h>
  19. DECLARE_GLOBAL_DATA_PTR;
  20. /*
  21. *
  22. * serial_buf: A buffer that holds keyboard characters for the
  23. * Sandbox U-Boot.
  24. *
  25. * invariants:
  26. * serial_buf_write == serial_buf_read -> empty buffer
  27. * (serial_buf_write + 1) % 16 == serial_buf_read -> full buffer
  28. */
  29. static char serial_buf[16];
  30. static unsigned int serial_buf_write;
  31. static unsigned int serial_buf_read;
  32. struct sandbox_serial_platdata {
  33. int colour; /* Text colour to use for output, -1 for none */
  34. };
  35. struct sandbox_serial_priv {
  36. bool start_of_line;
  37. };
  38. /**
  39. * output_ansi_colour() - Output an ANSI colour code
  40. *
  41. * @colour: Colour to output (0-7)
  42. */
  43. static void output_ansi_colour(int colour)
  44. {
  45. char ansi_code[] = "\x1b[1;3Xm";
  46. ansi_code[5] = '0' + colour;
  47. os_write(1, ansi_code, sizeof(ansi_code) - 1);
  48. }
  49. static void output_ansi_reset(void)
  50. {
  51. os_write(1, "\x1b[0m", 4);
  52. }
  53. static int sandbox_serial_probe(struct udevice *dev)
  54. {
  55. struct sandbox_state *state = state_get_current();
  56. struct sandbox_serial_priv *priv = dev_get_priv(dev);
  57. if (state->term_raw != STATE_TERM_COOKED)
  58. os_tty_raw(0, state->term_raw == STATE_TERM_RAW_WITH_SIGS);
  59. priv->start_of_line = 0;
  60. return 0;
  61. }
  62. static int sandbox_serial_remove(struct udevice *dev)
  63. {
  64. struct sandbox_serial_platdata *plat = dev->platdata;
  65. if (plat->colour != -1)
  66. output_ansi_reset();
  67. return 0;
  68. }
  69. static int sandbox_serial_putc(struct udevice *dev, const char ch)
  70. {
  71. struct sandbox_serial_priv *priv = dev_get_priv(dev);
  72. struct sandbox_serial_platdata *plat = dev->platdata;
  73. if (priv->start_of_line && plat->colour != -1) {
  74. priv->start_of_line = false;
  75. output_ansi_colour(plat->colour);
  76. }
  77. os_write(1, &ch, 1);
  78. if (ch == '\n')
  79. priv->start_of_line = true;
  80. return 0;
  81. }
  82. static unsigned int increment_buffer_index(unsigned int index)
  83. {
  84. return (index + 1) % ARRAY_SIZE(serial_buf);
  85. }
  86. static int sandbox_serial_pending(struct udevice *dev, bool input)
  87. {
  88. const unsigned int next_index =
  89. increment_buffer_index(serial_buf_write);
  90. ssize_t count;
  91. if (!input)
  92. return 0;
  93. os_usleep(100);
  94. #ifndef CONFIG_SPL_BUILD
  95. video_sync_all();
  96. #endif
  97. if (next_index == serial_buf_read)
  98. return 1; /* buffer full */
  99. count = os_read_no_block(0, &serial_buf[serial_buf_write], 1);
  100. if (count == 1)
  101. serial_buf_write = next_index;
  102. return serial_buf_write != serial_buf_read;
  103. }
  104. static int sandbox_serial_getc(struct udevice *dev)
  105. {
  106. int result;
  107. if (!sandbox_serial_pending(dev, true))
  108. return -EAGAIN; /* buffer empty */
  109. result = serial_buf[serial_buf_read];
  110. serial_buf_read = increment_buffer_index(serial_buf_read);
  111. return result;
  112. }
  113. static const char * const ansi_colour[] = {
  114. "black", "red", "green", "yellow", "blue", "megenta", "cyan",
  115. "white",
  116. };
  117. static int sandbox_serial_ofdata_to_platdata(struct udevice *dev)
  118. {
  119. struct sandbox_serial_platdata *plat = dev->platdata;
  120. const char *colour;
  121. int i;
  122. plat->colour = -1;
  123. colour = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
  124. "sandbox,text-colour", NULL);
  125. if (colour) {
  126. for (i = 0; i < ARRAY_SIZE(ansi_colour); i++) {
  127. if (!strcmp(colour, ansi_colour[i])) {
  128. plat->colour = i;
  129. break;
  130. }
  131. }
  132. }
  133. return 0;
  134. }
  135. static const struct dm_serial_ops sandbox_serial_ops = {
  136. .putc = sandbox_serial_putc,
  137. .pending = sandbox_serial_pending,
  138. .getc = sandbox_serial_getc,
  139. };
  140. static const struct udevice_id sandbox_serial_ids[] = {
  141. { .compatible = "sandbox,serial" },
  142. { }
  143. };
  144. U_BOOT_DRIVER(serial_sandbox) = {
  145. .name = "serial_sandbox",
  146. .id = UCLASS_SERIAL,
  147. .of_match = sandbox_serial_ids,
  148. .ofdata_to_platdata = sandbox_serial_ofdata_to_platdata,
  149. .platdata_auto_alloc_size = sizeof(struct sandbox_serial_platdata),
  150. .priv_auto_alloc_size = sizeof(struct sandbox_serial_priv),
  151. .probe = sandbox_serial_probe,
  152. .remove = sandbox_serial_remove,
  153. .ops = &sandbox_serial_ops,
  154. .flags = DM_FLAG_PRE_RELOC,
  155. };
  156. static const struct sandbox_serial_platdata platdata_non_fdt = {
  157. .colour = -1,
  158. };
  159. U_BOOT_DEVICE(serial_sandbox_non_fdt) = {
  160. .name = "serial_sandbox",
  161. .platdata = &platdata_non_fdt,
  162. };