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