2 * Copyright (C) 2009 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 );
34 #include <ipxe/console.h>
35 #include <ipxe/dhcp.h>
36 #include <ipxe/keys.h>
37 #include <ipxe/timer.h>
39 #include <ipxe/ansicol.h>
40 #include <usr/dhcpmgmt.h>
41 #include <usr/autoboot.h>
49 /** A PXE boot menu item */
50 struct pxe_menu_item {
51 /** Boot Server type */
60 * This structure encapsulates the menu information provided via DHCP
64 /** Prompt string (optional) */
66 /** Timeout (in seconds)
68 * Negative indicates no timeout (i.e. wait indefinitely)
71 /** Number of menu items */
72 unsigned int num_items;
73 /** Selected menu item */
74 unsigned int selection;
76 struct pxe_menu_item items[0];
80 * Parse and allocate PXE boot menu
82 * @v menu PXE boot menu to fill in
83 * @ret rc Return status code
85 * It is the callers responsibility to eventually free the allocated
88 static int pxe_menu_parse ( struct pxe_menu **menu ) {
89 struct setting pxe_boot_menu_prompt_setting =
90 { .tag = DHCP_PXE_BOOT_MENU_PROMPT };
91 struct setting pxe_boot_menu_setting =
92 { .tag = DHCP_PXE_BOOT_MENU };
93 uint8_t raw_menu[256];
96 struct dhcp_pxe_boot_menu *raw_menu_item;
97 struct dhcp_pxe_boot_menu_prompt *raw_menu_prompt;
99 unsigned int num_menu_items;
104 memset ( raw_menu, 0, sizeof ( raw_menu ) );
105 if ( ( raw_menu_len = fetch_raw_setting ( NULL, &pxe_boot_menu_setting,
107 sizeof ( raw_menu ) ) ) < 0 ){
109 DBG ( "Could not retrieve raw PXE boot menu: %s\n",
113 if ( raw_menu_len >= ( int ) sizeof ( raw_menu ) ) {
114 DBG ( "Raw PXE boot menu too large for buffer\n" );
117 raw_menu_end = ( raw_menu + raw_menu_len );
119 /* Fetch raw prompt length */
121 fetch_raw_setting ( NULL, &pxe_boot_menu_prompt_setting,
123 if ( raw_prompt_len < 0 )
126 /* Count menu items */
128 raw_menu_item = ( ( void * ) raw_menu );
130 if ( ( ( ( void * ) raw_menu_item ) +
131 sizeof ( *raw_menu_item ) ) > raw_menu_end )
133 if ( ( ( ( void * ) raw_menu_item ) +
134 sizeof ( *raw_menu_item ) +
135 raw_menu_item->desc_len ) > raw_menu_end )
138 raw_menu_item = ( ( ( void * ) raw_menu_item ) +
139 sizeof ( *raw_menu_item ) +
140 raw_menu_item->desc_len );
143 /* Allocate space for parsed menu */
144 *menu = zalloc ( sizeof ( **menu ) +
145 ( num_menu_items * sizeof ( (*menu)->items[0] ) ) +
146 raw_menu_len + 1 /* NUL */ +
147 raw_prompt_len + 1 /* NUL */ );
149 DBG ( "Could not allocate PXE boot menu\n" );
153 /* Fill in parsed menu */
154 (*menu)->num_items = num_menu_items;
155 raw_menu_item = ( ( ( void * ) (*menu) ) + sizeof ( **menu ) +
156 ( num_menu_items * sizeof ( (*menu)->items[0] ) ) );
157 memcpy ( raw_menu_item, raw_menu, raw_menu_len );
158 for ( i = 0 ; i < num_menu_items ; i++ ) {
159 (*menu)->items[i].type = le16_to_cpu ( raw_menu_item->type );
160 (*menu)->items[i].desc = raw_menu_item->desc;
161 /* Set type to 0; this ensures that the description
162 * for the previous menu item is NUL-terminated.
163 * (Final item is NUL-terminated anyway.)
165 raw_menu_item->type = 0;
166 raw_menu_item = ( ( ( void * ) raw_menu_item ) +
167 sizeof ( *raw_menu_item ) +
168 raw_menu_item->desc_len );
170 if ( raw_prompt_len ) {
171 raw_menu_prompt = ( ( ( void * ) raw_menu_item ) +
173 fetch_raw_setting ( NULL, &pxe_boot_menu_prompt_setting,
174 raw_menu_prompt, raw_prompt_len );
176 ( ( raw_menu_prompt->timeout == 0xff ) ?
177 -1 : raw_menu_prompt->timeout );
178 (*menu)->prompt = raw_menu_prompt->prompt;
180 (*menu)->timeout = -1;
187 * Draw PXE boot menu item
189 * @v menu PXE boot menu
190 * @v index Index of item to draw
191 * @v selected Item is selected
193 static void pxe_menu_draw_item ( struct pxe_menu *menu,
194 unsigned int index, int selected ) {
199 /* Prepare space-padded row content */
200 len = snprintf ( buf, sizeof ( buf ), " %c. %s",
201 ( 'A' + index ), menu->items[index].desc );
202 while ( len < ( sizeof ( buf ) - 1 ) )
204 buf[ sizeof ( buf ) - 1 ] = '\0';
207 row = ( LINES - menu->num_items + index );
208 color_set ( ( selected ? CPAIR_PXE : CPAIR_DEFAULT ), NULL );
209 mvprintw ( row, 0, "%s", buf );
214 * Make selection from PXE boot menu
216 * @v menu PXE boot menu
217 * @ret rc Return status code
219 static int pxe_menu_select ( struct pxe_menu *menu ) {
221 unsigned int key_selection;
228 color_set ( CPAIR_DEFAULT, NULL );
230 /* Draw initial menu */
231 for ( i = 0 ; i < menu->num_items ; i++ )
233 for ( i = 0 ; i < menu->num_items ; i++ )
234 pxe_menu_draw_item ( menu, ( menu->num_items - i - 1 ), 0 );
238 /* Highlight currently selected item */
239 pxe_menu_draw_item ( menu, menu->selection, 1 );
241 /* Wait for keyboard input */
244 /* Unhighlight currently selected item */
245 pxe_menu_draw_item ( menu, menu->selection, 0 );
248 if ( ( key == CR ) || ( key == LF ) ) {
249 pxe_menu_draw_item ( menu, menu->selection, 1 );
251 } else if ( ( key == CTRL_C ) || ( key == ESC ) ) {
254 } else if ( key == KEY_UP ) {
255 if ( menu->selection > 0 )
257 } else if ( key == KEY_DOWN ) {
258 if ( menu->selection < ( menu->num_items - 1 ) )
260 } else if ( ( key < KEY_MIN ) &&
261 ( ( key_selection = ( toupper ( key ) - 'A' ) )
262 < menu->num_items ) ) {
263 menu->selection = key_selection;
264 pxe_menu_draw_item ( menu, menu->selection, 1 );
276 * Prompt for (and make selection from) PXE boot menu
278 * @v menu PXE boot menu
279 * @ret rc Return status code
281 static int pxe_menu_prompt_and_select ( struct pxe_menu *menu ) {
282 unsigned long start = currticks();
284 unsigned long elapsed;
289 /* Display menu immediately, if specified to do so */
290 if ( menu->timeout < 0 ) {
292 printf ( "%s\n", menu->prompt );
293 return pxe_menu_select ( menu );
296 /* Display prompt, if specified */
298 printf ( "%s", menu->prompt );
300 /* Wait for timeout, if specified */
301 while ( menu->timeout > 0 ) {
303 len = printf ( " (%d)", menu->timeout );
306 if ( key == KEY_F8 ) {
309 return pxe_menu_select ( menu );
310 } else if ( ( key == CTRL_C ) || ( key == ESC ) ) {
320 elapsed = ( now - start );
321 if ( elapsed >= TICKS_PER_SEC ) {
330 /* Return with default option selected */
336 * Boot using PXE boot menu
338 * @ret rc Return status code
340 * Note that a success return status indicates that a PXE boot menu
341 * item has been selected, and that the DHCP session should perform a
342 * boot server request/ack.
344 int pxe_menu_boot ( struct net_device *netdev ) {
345 struct pxe_menu *menu;
346 unsigned int pxe_type;
347 struct settings *pxebs_settings;
351 /* Parse and allocate boot menu */
352 if ( ( rc = pxe_menu_parse ( &menu ) ) != 0 )
355 /* Make selection from boot menu */
356 if ( ( rc = pxe_menu_prompt_and_select ( menu ) ) != 0 ) {
360 pxe_type = menu->items[menu->selection].type;
365 /* Return immediately if local boot selected */
369 /* Attempt PXE Boot Server Discovery */
370 if ( ( rc = pxebs ( netdev, pxe_type ) ) != 0 )
373 /* Fetch next server and filename */
374 pxebs_settings = find_settings ( PXEBS_SETTINGS_NAME );
375 assert ( pxebs_settings );
376 uri = fetch_next_server_and_filename ( pxebs_settings );
381 rc = uriboot ( uri, NULL, 0, URIBOOT_NO_SAN );