sandbox.c 4.4 KB

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