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
20 FILE_LICENCE ( GPL2_OR_LATER );
26 #include <ipxe/console.h>
31 * @v fmt Format string
34 void dbg_printf ( const char *fmt, ... ) {
38 /* Mark console as in use for debugging messages */
39 saved_usage = console_set_usage ( CONSOLE_USAGE_DEBUG );
42 va_start ( args, fmt );
43 vprintf ( fmt, args );
46 /* Restore console usage */
47 console_set_usage ( saved_usage );
51 * Pause until a key is pressed
54 void dbg_pause ( void ) {
55 dbg_printf ( "\nPress a key..." );
57 dbg_printf ( "\r \r" );
61 * Indicate more data to follow and pause until a key is pressed
64 void dbg_more ( void ) {
65 dbg_printf ( "---more---" );
67 dbg_printf ( "\r \r" );
71 * Print row of a hex dump with specified display address
73 * @v dispaddr Display address
74 * @v data Data to print
75 * @v len Length of data
76 * @v offset Starting offset within data
78 static void dbg_hex_dump_da_row ( unsigned long dispaddr, const void *data,
79 unsigned long len, unsigned int offset ) {
80 const uint8_t *bytes = data;
84 dbg_printf ( "%08lx :", ( dispaddr + offset ) );
85 for ( i = offset ; i < ( offset + 16 ) ; i++ ) {
90 dbg_printf ( "%c%02x",
91 ( ( ( i % 16 ) == 8 ) ? '-' : ' ' ), bytes[i] );
94 for ( i = offset ; i < ( offset + 16 ) ; i++ ) {
100 dbg_printf ( "%c", ( isprint ( byte ) ? byte : '.' ) );
106 * Print hex dump with specified display address
108 * @v dispaddr Display address
109 * @v data Data to print
110 * @v len Length of data
112 void dbg_hex_dump_da ( unsigned long dispaddr, const void *data,
113 unsigned long len ) {
116 for ( offset = 0 ; offset < len ; offset += 16 ) {
117 dbg_hex_dump_da_row ( dispaddr, data, len, offset );
122 * Base message stream colour
124 * We default to using 31 (red foreground) as the base colour.
127 #define DBGCOL_MIN 31
131 * Maximum number of separately coloured message streams
133 * Six is the realistic maximum; there are 8 basic ANSI colours, one
134 * of which will be the terminal default and one of which will be
135 * invisible on the terminal because it matches the background colour.
138 #define DBGCOL_MAX ( DBGCOL_MIN + 6 - 1 )
141 /** A colour assigned to an autocolourised debug message stream */
143 /** Message stream ID */
144 unsigned long stream;
145 /** Last recorded usage */
146 unsigned long last_used;
150 * Choose colour index for debug autocolourisation
152 * @v stream Message stream ID
153 * @ret colour Colour ID
155 static int dbg_autocolour ( unsigned long stream ) {
156 static struct autocolour acs[ DBGCOL_MAX - DBGCOL_MIN + 1 ];
157 static unsigned long use;
160 unsigned int oldest_last_used;
162 /* Increment usage iteration counter */
165 /* Scan through list for a currently assigned colour */
166 for ( i = 0 ; i < ( sizeof ( acs ) / sizeof ( acs[0] ) ) ; i++ ) {
167 if ( acs[i].stream == stream ) {
168 acs[i].last_used = use;
173 /* No colour found; evict the oldest from the list */
175 oldest_last_used = use;
176 for ( i = 0 ; i < ( sizeof ( acs ) / sizeof ( acs[0] ) ) ; i++ ) {
177 if ( acs[i].last_used < oldest_last_used ) {
178 oldest_last_used = acs[i].last_used;
182 acs[oldest].stream = stream;
183 acs[oldest].last_used = use;
188 * Select automatic colour for debug messages
190 * @v stream Message stream ID
192 void dbg_autocolourise ( unsigned long stream ) {
193 dbg_printf ( "\033[%dm",
194 ( stream ? ( DBGCOL_MIN + dbg_autocolour ( stream ) ) :0));
198 * Revert to normal colour
201 void dbg_decolourise ( void ) {
202 dbg_printf ( "\033[0m" );