您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

gdbserial.c 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (C) 2008 Stefan Hajnoczi <stefanha@gmail.com>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stddef.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <assert.h>
  28. #include <ipxe/uart.h>
  29. #include <ipxe/gdbstub.h>
  30. #include <ipxe/gdbserial.h>
  31. #include <config/serial.h>
  32. /* UART port number */
  33. #ifdef COMCONSOLE
  34. #define GDBSERIAL_PORT COMCONSOLE
  35. #else
  36. #define GDBSERIAL_PORT 0
  37. #endif
  38. /* UART baud rate */
  39. #ifdef COMPRESERVE
  40. #define GDBSERIAL_BAUD 0
  41. #else
  42. #define GDBSERIAL_BAUD COMSPEED
  43. #endif
  44. /* UART line control register value */
  45. #ifdef COMPRESERVE
  46. #define GDBSERIAL_LCR 0
  47. #else
  48. #define GDBSERIAL_LCR UART_LCR_WPS ( COMDATA, COMPARITY, COMSTOP )
  49. #endif
  50. /** GDB serial UART */
  51. static struct uart gdbserial_uart;
  52. struct gdb_transport serial_gdb_transport __gdb_transport;
  53. static size_t gdbserial_recv ( char *buf, size_t len ) {
  54. assert ( len > 0 );
  55. while ( ! uart_data_ready ( &gdbserial_uart ) ) {}
  56. buf[0] = uart_receive ( &gdbserial_uart );
  57. return 1;
  58. }
  59. static void gdbserial_send ( const char *buf, size_t len ) {
  60. while ( len-- > 0 ) {
  61. uart_transmit ( &gdbserial_uart, *buf++ );
  62. }
  63. }
  64. static int gdbserial_init ( int argc, char **argv ) {
  65. unsigned int port;
  66. char *endp;
  67. if ( argc == 0 ) {
  68. port = GDBSERIAL_PORT;
  69. } else if ( argc == 1 ) {
  70. port = strtoul ( argv[0], &endp, 10 );
  71. if ( *endp ) {
  72. printf ( "serial: invalid port\n" );
  73. return 1;
  74. }
  75. } else {
  76. printf ( "serial: syntax <port>\n" );
  77. return 1;
  78. }
  79. if ( ! gdbserial_configure ( port, GDBSERIAL_BAUD, GDBSERIAL_LCR ) ) {
  80. printf ( "serial: unable to configure\n" );
  81. return 1;
  82. }
  83. return 0;
  84. }
  85. struct gdb_transport serial_gdb_transport __gdb_transport = {
  86. .name = "serial",
  87. .init = gdbserial_init,
  88. .recv = gdbserial_recv,
  89. .send = gdbserial_send,
  90. };
  91. struct gdb_transport * gdbserial_configure ( unsigned int port,
  92. unsigned int baud, uint8_t lcr ) {
  93. int rc;
  94. if ( ( rc = uart_select ( &gdbserial_uart, port ) ) != 0 )
  95. return NULL;
  96. if ( ( rc = uart_init ( &gdbserial_uart, baud, lcr ) ) != 0 )
  97. return NULL;
  98. return &serial_gdb_transport;
  99. }