iPXE
gdbserial.c
Go to the documentation of this file.
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 ) {
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 
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 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
static void gdbserial_send(const char *buf, size_t len)
Definition: gdbserial.c:69
unsigned long strtoul(const char *string, char **endp, int base)
Convert string to numeric value.
Definition: string.c:471
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
static int gdbserial_init(int argc, char **argv)
Definition: gdbserial.c:76
A 16550-compatible UART.
Definition: uart.h:80
static int uart_data_ready(struct uart *uart)
Check if received data is ready.
Definition: uart.h:109
const char * name
Transport name.
Definition: gdbstub.h:22
void uart_transmit(struct uart *uart, uint8_t data)
Transmit data.
Definition: uart.c:48
static struct uart gdbserial_uart
GDB serial UART.
Definition: gdbserial.c:57
struct gdb_transport serial_gdb_transport __gdb_transport
Definition: gdbserial.c:59
int uart_init(struct uart *uart, unsigned int baud, uint8_t lcr)
Initialise UART.
Definition: uart.c:113
static uint8_t uart_receive(struct uart *uart)
Receive data.
Definition: uart.h:122
#define GDBSERIAL_BAUD
Definition: gdbserial.c:46
#define GDBSERIAL_PORT
Definition: gdbserial.c:37
u8 port
Port number.
Definition: CIB_PRM.h:31
Assertions.
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
struct gdb_transport * gdbserial_configure(unsigned int port, unsigned int baud, uint8_t lcr)
Definition: gdbserial.c:108
unsigned char uint8_t
Definition: stdint.h:10
GDB remote debugging over serial.
Serial port configuration.
static size_t gdbserial_recv(char *buf, size_t len)
Definition: gdbserial.c:61
16550-compatible UART
GDB remote debugging.
uint32_t len
Length.
Definition: ena.h:14
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
A transport mechanism for the GDB protocol.
Definition: gdbstub.h:20
int uart_select(struct uart *uart, unsigned int port)
Select UART port.
Definition: x86_uart.c:50
#define GDBSERIAL_LCR
Definition: gdbserial.c:53