iPXE
console.c
Go to the documentation of this file.
1 #include "stddef.h"
2 #include <ipxe/console.h>
3 #include <ipxe/process.h>
4 #include <ipxe/nap.h>
5 
6 /** @file */
7 
8 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
9 
10 /** Current console usage */
12 
13 /** Console width */
15 
16 /** Console height */
18 
19 /**
20  * Write a single character to each console device
21  *
22  * @v character Character to be written
23  *
24  * The character is written out to all enabled console devices, using
25  * each device's console_driver::putchar() method.
26  */
27 void putchar ( int character ) {
28  struct console_driver *console;
29 
30  /* Automatic LF -> CR,LF translation */
31  if ( character == '\n' )
32  putchar ( '\r' );
33 
34  for_each_table_entry ( console, CONSOLES ) {
35  if ( ( ! ( console->disabled & CONSOLE_DISABLED_OUTPUT ) ) &&
36  ( console_usage & console->usage ) &&
37  console->putchar )
38  console->putchar ( character );
39  }
40 }
41 
42 /**
43  * Check to see if any input is available on any console
44  *
45  * @ret console Console device that has input available, or NULL
46  *
47  * All enabled console devices are checked once for available input
48  * using each device's console_driver::iskey() method. The first
49  * console device that has available input will be returned, if any.
50  */
51 static struct console_driver * has_input ( void ) {
52  struct console_driver *console;
53 
54  for_each_table_entry ( console, CONSOLES ) {
55  if ( ( ! ( console->disabled & CONSOLE_DISABLED_INPUT ) ) &&
56  console->iskey ) {
57  if ( console->iskey () )
58  return console;
59  }
60  }
61  return NULL;
62 }
63 
64 /**
65  * Read a single character from any console
66  *
67  * @ret character Character read from a console.
68  *
69  * A character will be read from the first enabled console device that
70  * has input available using that console's console_driver::getchar()
71  * method. If no console has input available to be read, this method
72  * will block. To perform a non-blocking read, use something like
73  *
74  * @code
75  *
76  * int key = iskey() ? getchar() : -1;
77  *
78  * @endcode
79  *
80  * The character read will not be echoed back to any console.
81  */
82 int getchar ( void ) {
83  struct console_driver *console;
84  int character;
85 
86  while ( 1 ) {
87  console = has_input();
88  if ( console && console->getchar ) {
89  character = console->getchar ();
90  break;
91  }
92 
93  /* Doze for a while (until the next interrupt). This works
94  * fine, because the keyboard is interrupt-driven, and the
95  * timer interrupt (approx. every 50msec) takes care of the
96  * serial port, which is read by polling. This reduces the
97  * power dissipation of a modern CPU considerably, and also
98  * makes Etherboot waiting for user interaction waste a lot
99  * less CPU time in a VMware session.
100  */
101  cpu_nap();
102 
103  /* Keep processing background tasks while we wait for
104  * input.
105  */
106  step();
107  }
108 
109  /* CR -> LF translation */
110  if ( character == '\r' )
111  character = '\n';
112 
113  return character;
114 }
115 
116 /**
117  * Check for available input on any console
118  *
119  * @ret is_available Input is available on a console
120  *
121  * All enabled console devices are checked once for available input
122  * using each device's console_driver::iskey() method. If any console
123  * device has input available, this call will return true. If this
124  * call returns true, you can then safely call getchar() without
125  * blocking.
126  */
127 int iskey ( void ) {
128  return has_input() ? 1 : 0;
129 }
130 
131 /**
132  * Configure console
133  *
134  * @v config Console configuration
135  * @ret rc Return status code
136  *
137  * The configuration is passed to all configurable consoles, including
138  * those which are currently disabled. Consoles may choose to enable
139  * or disable themselves depending upon the configuration.
140  *
141  * If configuration fails, then all consoles will be reset.
142  */
143 int console_configure ( struct console_configuration *config ) {
144  struct console_driver *console;
145  int rc;
146 
147  /* Reset console width and height */
149 
150  /* Try to configure each console */
151  for_each_table_entry ( console, CONSOLES ) {
152  if ( ( console->configure ) &&
153  ( ( rc = console->configure ( config ) ) != 0 ) )
154  goto err;
155  }
156 
157  return 0;
158 
159  err:
160  /* Reset all consoles, avoiding a potential infinite loop */
161  if ( config )
162  console_reset();
163  return rc;
164 }
static void console_set_size(unsigned int width, unsigned int height)
Set console size.
Definition: console.h:201
CPU sleeping.
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
unsigned int console_height
Console height.
Definition: console.c:17
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#define CONSOLE_DEFAULT_WIDTH
Default console width.
Definition: console.h:171
#define CONSOLES
Console driver table.
Definition: console.h:114
void putchar(int character)
Write a single character to each console device.
Definition: console.c:27
#define CONSOLE_DISABLED_INPUT
Console is disabled for input.
Definition: console.h:105
int(* configure)(struct console_configuration *config)
Configure console.
Definition: console.h:94
A console configuration.
Definition: console.h:24
int usage
Console usage bitmask.
Definition: console.h:101
#define CONSOLE_DISABLED_OUTPUT
Console is disabled for output.
Definition: console.h:108
void(* putchar)(int character)
Write a character to the console.
Definition: console.h:68
int console_usage
Current console usage.
Definition: console.c:11
User interaction.
static struct console_driver * has_input(void)
Check to see if any input is available on any console.
Definition: console.c:51
int getchar(void)
Read a single character from any console.
Definition: console.c:82
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:358
int(* iskey)(void)
Check for available input.
Definition: console.h:87
Processes.
static void console_reset(void)
Reset console.
Definition: console.h:214
int console_configure(struct console_configuration *config)
Configure console.
Definition: console.c:143
int disabled
Console disabled flags.
Definition: console.h:62
void cpu_nap(void)
Sleep until next CPU interrupt.
void step(void)
Single-step a single process.
Definition: process.c:98
A console driver.
Definition: console.h:55
int(* getchar)(void)
Read a character from the console.
Definition: console.h:78
#define CONSOLE_USAGE_STDOUT
Standard output.
Definition: console.h:141
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
int iskey(void)
Check for available input on any console.
Definition: console.c:127
unsigned int console_width
Console width.
Definition: console.c:14
#define CONSOLE_DEFAULT_HEIGHT
Default console height.
Definition: console.h:174