[zbin] Fix check for existence of most recent output byte
[ipxe.git] / src / usr / pxemenu.c
1 /*
2 * Copyright (C) 2009 Michael Brown <mbrown@fensystems.co.uk>.
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
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <ctype.h>
28 #include <byteswap.h>
29 #include <curses.h>
30 #include <ipxe/console.h>
31 #include <ipxe/dhcp.h>
32 #include <ipxe/keys.h>
33 #include <ipxe/timer.h>
34 #include <ipxe/uri.h>
35 #include <ipxe/ansicol.h>
36 #include <usr/dhcpmgmt.h>
37 #include <usr/autoboot.h>
38
39 /** @file
40 *
41 * PXE Boot Menus
42 *
43 */
44
45 /** A PXE boot menu item */
46 struct pxe_menu_item {
47 /** Boot Server type */
48 unsigned int type;
49 /** Description */
50 char *desc;
51 };
52
53 /**
54 * A PXE boot menu
55 *
56 * This structure encapsulates the menu information provided via DHCP
57 * options.
58 */
59 struct pxe_menu {
60 /** Prompt string (optional) */
61 const char *prompt;
62 /** Timeout (in seconds)
63 *
64 * Negative indicates no timeout (i.e. wait indefinitely)
65 */
66 int timeout;
67 /** Number of menu items */
68 unsigned int num_items;
69 /** Selected menu item */
70 unsigned int selection;
71 /** Menu items */
72 struct pxe_menu_item items[0];
73 };
74
75 /**
76 * Parse and allocate PXE boot menu
77 *
78 * @v menu PXE boot menu to fill in
79 * @ret rc Return status code
80 *
81 * It is the callers responsibility to eventually free the allocated
82 * boot menu.
83 */
84 static int pxe_menu_parse ( struct pxe_menu **menu ) {
85 struct setting pxe_boot_menu_prompt_setting =
86 { .tag = DHCP_PXE_BOOT_MENU_PROMPT };
87 struct setting pxe_boot_menu_setting =
88 { .tag = DHCP_PXE_BOOT_MENU };
89 uint8_t raw_menu[256];
90 int raw_prompt_len;
91 int raw_menu_len;
92 struct dhcp_pxe_boot_menu *raw_menu_item;
93 struct dhcp_pxe_boot_menu_prompt *raw_menu_prompt;
94 void *raw_menu_end;
95 unsigned int num_menu_items;
96 unsigned int i;
97 int rc;
98
99 /* Fetch raw menu */
100 memset ( raw_menu, 0, sizeof ( raw_menu ) );
101 if ( ( raw_menu_len = fetch_raw_setting ( NULL, &pxe_boot_menu_setting,
102 raw_menu,
103 sizeof ( raw_menu ) ) ) < 0 ){
104 rc = raw_menu_len;
105 DBG ( "Could not retrieve raw PXE boot menu: %s\n",
106 strerror ( rc ) );
107 return rc;
108 }
109 if ( raw_menu_len >= ( int ) sizeof ( raw_menu ) ) {
110 DBG ( "Raw PXE boot menu too large for buffer\n" );
111 return -ENOSPC;
112 }
113 raw_menu_end = ( raw_menu + raw_menu_len );
114
115 /* Fetch raw prompt length */
116 raw_prompt_len =
117 fetch_raw_setting ( NULL, &pxe_boot_menu_prompt_setting,
118 NULL, 0 );
119 if ( raw_prompt_len < 0 )
120 raw_prompt_len = 0;
121
122 /* Count menu items */
123 num_menu_items = 0;
124 raw_menu_item = ( ( void * ) raw_menu );
125 while ( 1 ) {
126 if ( ( ( ( void * ) raw_menu_item ) +
127 sizeof ( *raw_menu_item ) ) > raw_menu_end )
128 break;
129 if ( ( ( ( void * ) raw_menu_item ) +
130 sizeof ( *raw_menu_item ) +
131 raw_menu_item->desc_len ) > raw_menu_end )
132 break;
133 num_menu_items++;
134 raw_menu_item = ( ( ( void * ) raw_menu_item ) +
135 sizeof ( *raw_menu_item ) +
136 raw_menu_item->desc_len );
137 }
138
139 /* Allocate space for parsed menu */
140 *menu = zalloc ( sizeof ( **menu ) +
141 ( num_menu_items * sizeof ( (*menu)->items[0] ) ) +
142 raw_menu_len + 1 /* NUL */ +
143 raw_prompt_len + 1 /* NUL */ );
144 if ( ! *menu ) {
145 DBG ( "Could not allocate PXE boot menu\n" );
146 return -ENOMEM;
147 }
148
149 /* Fill in parsed menu */
150 (*menu)->num_items = num_menu_items;
151 raw_menu_item = ( ( ( void * ) (*menu) ) + sizeof ( **menu ) +
152 ( num_menu_items * sizeof ( (*menu)->items[0] ) ) );
153 memcpy ( raw_menu_item, raw_menu, raw_menu_len );
154 for ( i = 0 ; i < num_menu_items ; i++ ) {
155 (*menu)->items[i].type = le16_to_cpu ( raw_menu_item->type );
156 (*menu)->items[i].desc = raw_menu_item->desc;
157 /* Set type to 0; this ensures that the description
158 * for the previous menu item is NUL-terminated.
159 * (Final item is NUL-terminated anyway.)
160 */
161 raw_menu_item->type = 0;
162 raw_menu_item = ( ( ( void * ) raw_menu_item ) +
163 sizeof ( *raw_menu_item ) +
164 raw_menu_item->desc_len );
165 }
166 if ( raw_prompt_len ) {
167 raw_menu_prompt = ( ( ( void * ) raw_menu_item ) +
168 1 /* NUL */ );
169 fetch_raw_setting ( NULL, &pxe_boot_menu_prompt_setting,
170 raw_menu_prompt, raw_prompt_len );
171 (*menu)->timeout =
172 ( ( raw_menu_prompt->timeout == 0xff ) ?
173 -1 : raw_menu_prompt->timeout );
174 (*menu)->prompt = raw_menu_prompt->prompt;
175 } else {
176 (*menu)->timeout = -1;
177 }
178
179 return 0;
180 }
181
182 /**
183 * Draw PXE boot menu item
184 *
185 * @v menu PXE boot menu
186 * @v index Index of item to draw
187 * @v selected Item is selected
188 */
189 static void pxe_menu_draw_item ( struct pxe_menu *menu,
190 unsigned int index, int selected ) {
191 char buf[COLS+1];
192 size_t len;
193 unsigned int row;
194
195 /* Prepare space-padded row content */
196 len = snprintf ( buf, sizeof ( buf ), " %c. %s",
197 ( 'A' + index ), menu->items[index].desc );
198 while ( len < ( sizeof ( buf ) - 1 ) )
199 buf[len++] = ' ';
200 buf[ sizeof ( buf ) - 1 ] = '\0';
201
202 /* Draw row */
203 row = ( LINES - menu->num_items + index );
204 color_set ( ( selected ? CPAIR_PXE : CPAIR_DEFAULT ), NULL );
205 mvprintw ( row, 0, "%s", buf );
206 move ( row, 1 );
207 }
208
209 /**
210 * Make selection from PXE boot menu
211 *
212 * @v menu PXE boot menu
213 * @ret rc Return status code
214 */
215 static int pxe_menu_select ( struct pxe_menu *menu ) {
216 int key;
217 unsigned int key_selection;
218 unsigned int i;
219 int rc = 0;
220
221 /* Initialise UI */
222 initscr();
223 start_color();
224 color_set ( CPAIR_DEFAULT, NULL );
225
226 /* Draw initial menu */
227 for ( i = 0 ; i < menu->num_items ; i++ )
228 printf ( "\n" );
229 for ( i = 0 ; i < menu->num_items ; i++ )
230 pxe_menu_draw_item ( menu, ( menu->num_items - i - 1 ), 0 );
231
232 while ( 1 ) {
233
234 /* Highlight currently selected item */
235 pxe_menu_draw_item ( menu, menu->selection, 1 );
236
237 /* Wait for keyboard input */
238 key = getkey ( 0 );
239
240 /* Unhighlight currently selected item */
241 pxe_menu_draw_item ( menu, menu->selection, 0 );
242
243 /* Act upon key */
244 if ( ( key == CR ) || ( key == LF ) ) {
245 pxe_menu_draw_item ( menu, menu->selection, 1 );
246 break;
247 } else if ( ( key == CTRL_C ) || ( key == ESC ) ) {
248 rc = -ECANCELED;
249 break;
250 } else if ( key == KEY_UP ) {
251 if ( menu->selection > 0 )
252 menu->selection--;
253 } else if ( key == KEY_DOWN ) {
254 if ( menu->selection < ( menu->num_items - 1 ) )
255 menu->selection++;
256 } else if ( ( key < KEY_MIN ) &&
257 ( ( key_selection = ( toupper ( key ) - 'A' ) )
258 < menu->num_items ) ) {
259 menu->selection = key_selection;
260 pxe_menu_draw_item ( menu, menu->selection, 1 );
261 break;
262 }
263 }
264
265 /* Shut down UI */
266 endwin();
267
268 return rc;
269 }
270
271 /**
272 * Prompt for (and make selection from) PXE boot menu
273 *
274 * @v menu PXE boot menu
275 * @ret rc Return status code
276 */
277 static int pxe_menu_prompt_and_select ( struct pxe_menu *menu ) {
278 unsigned long start = currticks();
279 unsigned long now;
280 unsigned long elapsed;
281 size_t len = 0;
282 int key;
283 int rc = 0;
284
285 /* Display menu immediately, if specified to do so */
286 if ( menu->timeout < 0 ) {
287 if ( menu->prompt )
288 printf ( "%s\n", menu->prompt );
289 return pxe_menu_select ( menu );
290 }
291
292 /* Display prompt, if specified */
293 if ( menu->prompt )
294 printf ( "%s", menu->prompt );
295
296 /* Wait for timeout, if specified */
297 while ( menu->timeout > 0 ) {
298 if ( ! len )
299 len = printf ( " (%d)", menu->timeout );
300 if ( iskey() ) {
301 key = getkey ( 0 );
302 if ( key == KEY_F8 ) {
303 /* Display menu */
304 printf ( "\n" );
305 return pxe_menu_select ( menu );
306 } else if ( ( key == CTRL_C ) || ( key == ESC ) ) {
307 /* Abort */
308 rc = -ECANCELED;
309 break;
310 } else {
311 /* Stop waiting */
312 break;
313 }
314 }
315 now = currticks();
316 elapsed = ( now - start );
317 if ( elapsed >= TICKS_PER_SEC ) {
318 menu->timeout -= 1;
319 do {
320 printf ( "\b \b" );
321 } while ( --len );
322 start = now;
323 }
324 }
325
326 /* Return with default option selected */
327 printf ( "\n" );
328 return rc;
329 }
330
331 /**
332 * Boot using PXE boot menu
333 *
334 * @ret rc Return status code
335 *
336 * Note that a success return status indicates that a PXE boot menu
337 * item has been selected, and that the DHCP session should perform a
338 * boot server request/ack.
339 */
340 int pxe_menu_boot ( struct net_device *netdev ) {
341 struct pxe_menu *menu;
342 unsigned int pxe_type;
343 struct settings *pxebs_settings;
344 struct uri *uri;
345 int rc;
346
347 /* Parse and allocate boot menu */
348 if ( ( rc = pxe_menu_parse ( &menu ) ) != 0 )
349 return rc;
350
351 /* Make selection from boot menu */
352 if ( ( rc = pxe_menu_prompt_and_select ( menu ) ) != 0 ) {
353 free ( menu );
354 return rc;
355 }
356 pxe_type = menu->items[menu->selection].type;
357
358 /* Free boot menu */
359 free ( menu );
360
361 /* Return immediately if local boot selected */
362 if ( ! pxe_type )
363 return 0;
364
365 /* Attempt PXE Boot Server Discovery */
366 if ( ( rc = pxebs ( netdev, pxe_type ) ) != 0 )
367 return rc;
368
369 /* Fetch next server and filename */
370 pxebs_settings = find_settings ( PXEBS_SETTINGS_NAME );
371 assert ( pxebs_settings );
372 uri = fetch_next_server_and_filename ( pxebs_settings );
373 if ( ! uri )
374 return -ENOMEM;
375
376 /* Attempt boot */
377 rc = uriboot ( uri, NULL, 0, URIBOOT_NO_SAN );
378 uri_put ( uri );
379 return rc;
380 }