[process] Include process name in debug messages
[ipxe.git] / src / core / gdbserial.c
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
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <stddef.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <assert.h>
30 #include <ipxe/uart.h>
31 #include <ipxe/gdbstub.h>
32 #include <ipxe/gdbserial.h>
33 #include <config/serial.h>
34
35 /* UART port number */
36 #ifdef COMCONSOLE
37 #define GDBSERIAL_PORT COMCONSOLE
38 #else
39 #define GDBSERIAL_PORT 0
40 #endif
41
42 /* UART baud rate */
43 #ifdef COMPRESERVE
44 #define GDBSERIAL_BAUD 0
45 #else
46 #define GDBSERIAL_BAUD COMSPEED
47 #endif
48
49 /* UART line control register value */
50 #ifdef COMPRESERVE
51 #define GDBSERIAL_LCR 0
52 #else
53 #define GDBSERIAL_LCR UART_LCR_WPS ( COMDATA, COMPARITY, COMSTOP )
54 #endif
55
56 /** GDB serial UART */
57 static struct uart gdbserial_uart;
58
59 struct gdb_transport serial_gdb_transport __gdb_transport;
60
61 static size_t gdbserial_recv ( char *buf, size_t len ) {
62
63 assert ( len > 0 );
64 while ( ! uart_data_ready ( &gdbserial_uart ) ) {}
65 buf[0] = uart_receive ( &gdbserial_uart );
66 return 1;
67 }
68
69 static void gdbserial_send ( const char *buf, size_t len ) {
70
71 while ( len-- > 0 ) {
72 uart_transmit ( &gdbserial_uart, *buf++ );
73 }
74 }
75
76 static int gdbserial_init ( int argc, char **argv ) {
77 unsigned int port;
78 char *endp;
79
80 if ( argc == 0 ) {
81 port = GDBSERIAL_PORT;
82 } else if ( argc == 1 ) {
83 port = strtoul ( argv[0], &endp, 10 );
84 if ( *endp ) {
85 printf ( "serial: invalid port\n" );
86 return 1;
87 }
88 } else {
89 printf ( "serial: syntax <port>\n" );
90 return 1;
91 }
92
93 if ( ! gdbserial_configure ( port, GDBSERIAL_BAUD, GDBSERIAL_LCR ) ) {
94 printf ( "serial: unable to configure\n" );
95 return 1;
96 }
97
98 return 0;
99 }
100
101 struct gdb_transport serial_gdb_transport __gdb_transport = {
102 .name = "serial",
103 .init = gdbserial_init,
104 .recv = gdbserial_recv,
105 .send = gdbserial_send,
106 };
107
108 struct gdb_transport * gdbserial_configure ( unsigned int port,
109 unsigned int baud, uint8_t lcr ) {
110 int rc;
111
112 if ( ( rc = uart_select ( &gdbserial_uart, port ) ) != 0 )
113 return NULL;
114
115 if ( ( rc = uart_init ( &gdbserial_uart, baud, lcr ) ) != 0 )
116 return NULL;
117
118 return &serial_gdb_transport;
119 }