2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
30 #include <ipxe/console.h>
35 * @v fmt Format string
38 void dbg_printf ( const char *fmt, ... ) {
42 /* Mark console as in use for debugging messages */
43 saved_usage = console_set_usage ( CONSOLE_USAGE_DEBUG );
46 va_start ( args, fmt );
47 vprintf ( fmt, args );
50 /* Restore console usage */
51 console_set_usage ( saved_usage );
55 * Pause until a key is pressed
58 void dbg_pause ( void ) {
59 dbg_printf ( "\nPress a key..." );
61 dbg_printf ( "\r \r" );
65 * Indicate more data to follow and pause until a key is pressed
68 void dbg_more ( void ) {
69 dbg_printf ( "---more---" );
71 dbg_printf ( "\r \r" );
75 * Print row of a hex dump with specified display address
77 * @v dispaddr Display address
78 * @v data Data to print
79 * @v len Length of data
80 * @v offset Starting offset within data
82 static void dbg_hex_dump_da_row ( unsigned long dispaddr, const void *data,
83 unsigned long len, unsigned int offset ) {
84 const uint8_t *bytes = data;
88 dbg_printf ( "%08lx :", ( dispaddr + offset ) );
89 for ( i = offset ; i < ( offset + 16 ) ; i++ ) {
94 dbg_printf ( "%c%02x",
95 ( ( ( i % 16 ) == 8 ) ? '-' : ' ' ), bytes[i] );
98 for ( i = offset ; i < ( offset + 16 ) ; i++ ) {
104 dbg_printf ( "%c", ( isprint ( byte ) ? byte : '.' ) );
110 * Print hex dump with specified display address
112 * @v dispaddr Display address
113 * @v data Data to print
114 * @v len Length of data
116 void dbg_hex_dump_da ( unsigned long dispaddr, const void *data,
117 unsigned long len ) {
120 for ( offset = 0 ; offset < len ; offset += 16 ) {
121 dbg_hex_dump_da_row ( dispaddr, data, len, offset );
126 * Base message stream colour
128 * We default to using 31 (red foreground) as the base colour.
131 #define DBGCOL_MIN 31
135 * Maximum number of separately coloured message streams
137 * Six is the realistic maximum; there are 8 basic ANSI colours, one
138 * of which will be the terminal default and one of which will be
139 * invisible on the terminal because it matches the background colour.
142 #define DBGCOL_MAX ( DBGCOL_MIN + 6 - 1 )
145 /** A colour assigned to an autocolourised debug message stream */
147 /** Message stream ID */
148 unsigned long stream;
149 /** Last recorded usage */
150 unsigned long last_used;
154 * Choose colour index for debug autocolourisation
156 * @v stream Message stream ID
157 * @ret colour Colour ID
159 static int dbg_autocolour ( unsigned long stream ) {
160 static struct autocolour acs[ DBGCOL_MAX - DBGCOL_MIN + 1 ];
161 static unsigned long use;
164 unsigned int oldest_last_used;
166 /* Increment usage iteration counter */
169 /* Scan through list for a currently assigned colour */
170 for ( i = 0 ; i < ( sizeof ( acs ) / sizeof ( acs[0] ) ) ; i++ ) {
171 if ( acs[i].stream == stream ) {
172 acs[i].last_used = use;
177 /* No colour found; evict the oldest from the list */
179 oldest_last_used = use;
180 for ( i = 0 ; i < ( sizeof ( acs ) / sizeof ( acs[0] ) ) ; i++ ) {
181 if ( acs[i].last_used < oldest_last_used ) {
182 oldest_last_used = acs[i].last_used;
186 acs[oldest].stream = stream;
187 acs[oldest].last_used = use;
192 * Select automatic colour for debug messages
194 * @v stream Message stream ID
196 void dbg_autocolourise ( unsigned long stream ) {
197 dbg_printf ( "\033[%dm",
198 ( stream ? ( DBGCOL_MIN + dbg_autocolour ( stream ) ) :0));
202 * Revert to normal colour
205 void dbg_decolourise ( void ) {
206 dbg_printf ( "\033[0m" );