iPXE
pxemenu.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stdint.h>
00027 #include <stdlib.h>
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include <errno.h>
00031 #include <ctype.h>
00032 #include <byteswap.h>
00033 #include <curses.h>
00034 #include <ipxe/console.h>
00035 #include <ipxe/dhcp.h>
00036 #include <ipxe/keys.h>
00037 #include <ipxe/timer.h>
00038 #include <ipxe/uri.h>
00039 #include <ipxe/ansicol.h>
00040 #include <usr/dhcpmgmt.h>
00041 #include <usr/autoboot.h>
00042 
00043 /** @file
00044  *
00045  * PXE Boot Menus
00046  *
00047  */
00048 
00049 /** A PXE boot menu item */
00050 struct pxe_menu_item {
00051         /** Boot Server type */
00052         unsigned int type;
00053         /** Description */
00054         char *desc;
00055 };
00056 
00057 /**
00058  * A PXE boot menu
00059  *
00060  * This structure encapsulates the menu information provided via DHCP
00061  * options.
00062  */
00063 struct pxe_menu {
00064         /** Prompt string (optional) */
00065         const char *prompt;
00066         /** Timeout (in seconds)
00067          *
00068          * Negative indicates no timeout (i.e. wait indefinitely)
00069          */
00070         int timeout;
00071         /** Number of menu items */
00072         unsigned int num_items;
00073         /** Selected menu item */
00074         unsigned int selection;
00075         /** Menu items */
00076         struct pxe_menu_item items[0];
00077 };
00078 
00079 /**
00080  * Parse and allocate PXE boot menu
00081  *
00082  * @v menu              PXE boot menu to fill in
00083  * @ret rc              Return status code
00084  *
00085  * It is the callers responsibility to eventually free the allocated
00086  * boot menu.
00087  */
00088 static int pxe_menu_parse ( struct pxe_menu **menu ) {
00089         struct setting pxe_boot_menu_prompt_setting =
00090                 { .tag = DHCP_PXE_BOOT_MENU_PROMPT };
00091         struct setting pxe_boot_menu_setting =
00092                 { .tag = DHCP_PXE_BOOT_MENU };
00093         uint8_t raw_menu[256];
00094         int raw_prompt_len;
00095         int raw_menu_len;
00096         struct dhcp_pxe_boot_menu *raw_menu_item;
00097         struct dhcp_pxe_boot_menu_prompt *raw_menu_prompt;
00098         void *raw_menu_end;
00099         unsigned int num_menu_items;
00100         unsigned int i;
00101         int rc;
00102 
00103         /* Fetch raw menu */
00104         memset ( raw_menu, 0, sizeof ( raw_menu ) );
00105         if ( ( raw_menu_len = fetch_raw_setting ( NULL, &pxe_boot_menu_setting,
00106                                                   raw_menu,
00107                                                   sizeof ( raw_menu ) ) ) < 0 ){
00108                 rc = raw_menu_len;
00109                 DBG ( "Could not retrieve raw PXE boot menu: %s\n",
00110                       strerror ( rc ) );
00111                 return rc;
00112         }
00113         if ( raw_menu_len >= ( int ) sizeof ( raw_menu ) ) {
00114                 DBG ( "Raw PXE boot menu too large for buffer\n" );
00115                 return -ENOSPC;
00116         }
00117         raw_menu_end = ( raw_menu + raw_menu_len );
00118 
00119         /* Fetch raw prompt length */
00120         raw_prompt_len =
00121                 fetch_raw_setting ( NULL, &pxe_boot_menu_prompt_setting,
00122                                     NULL, 0 );
00123         if ( raw_prompt_len < 0 )
00124                 raw_prompt_len = 0;
00125 
00126         /* Count menu items */
00127         num_menu_items = 0;
00128         raw_menu_item = ( ( void * ) raw_menu );
00129         while ( 1 ) {
00130                 if ( ( ( ( void * ) raw_menu_item ) +
00131                        sizeof ( *raw_menu_item ) ) > raw_menu_end )
00132                         break;
00133                 if ( ( ( ( void * ) raw_menu_item ) +
00134                        sizeof ( *raw_menu_item ) +
00135                        raw_menu_item->desc_len ) > raw_menu_end )
00136                         break;
00137                 num_menu_items++;
00138                 raw_menu_item = ( ( ( void * ) raw_menu_item ) +
00139                                   sizeof ( *raw_menu_item ) +
00140                                   raw_menu_item->desc_len );
00141         }
00142 
00143         /* Allocate space for parsed menu */
00144         *menu = zalloc ( sizeof ( **menu ) +
00145                          ( num_menu_items * sizeof ( (*menu)->items[0] ) ) +
00146                          raw_menu_len + 1 /* NUL */ +
00147                          raw_prompt_len + 1 /* NUL */ );
00148         if ( ! *menu ) {
00149                 DBG ( "Could not allocate PXE boot menu\n" );
00150                 return -ENOMEM;
00151         }
00152 
00153         /* Fill in parsed menu */
00154         (*menu)->num_items = num_menu_items;
00155         raw_menu_item = ( ( ( void * ) (*menu) ) + sizeof ( **menu ) +
00156                           ( num_menu_items * sizeof ( (*menu)->items[0] ) ) );
00157         memcpy ( raw_menu_item, raw_menu, raw_menu_len );
00158         for ( i = 0 ; i < num_menu_items ; i++ ) {
00159                 (*menu)->items[i].type = le16_to_cpu ( raw_menu_item->type );
00160                 (*menu)->items[i].desc = raw_menu_item->desc;
00161                 /* Set type to 0; this ensures that the description
00162                  * for the previous menu item is NUL-terminated.
00163                  * (Final item is NUL-terminated anyway.)
00164                  */
00165                 raw_menu_item->type = 0;
00166                 raw_menu_item = ( ( ( void * ) raw_menu_item ) +
00167                                   sizeof ( *raw_menu_item ) +
00168                                   raw_menu_item->desc_len );
00169         }
00170         if ( raw_prompt_len ) {
00171                 raw_menu_prompt = ( ( ( void * ) raw_menu_item ) +
00172                                     1 /* NUL */ );
00173                 fetch_raw_setting ( NULL, &pxe_boot_menu_prompt_setting,
00174                                     raw_menu_prompt, raw_prompt_len );
00175                 (*menu)->timeout =
00176                         ( ( raw_menu_prompt->timeout == 0xff ) ?
00177                           -1 : raw_menu_prompt->timeout );
00178                 (*menu)->prompt = raw_menu_prompt->prompt;
00179         } else {
00180                 (*menu)->timeout = -1;
00181         }
00182 
00183         return 0;
00184 }
00185 
00186 /**
00187  * Draw PXE boot menu item
00188  *
00189  * @v menu              PXE boot menu
00190  * @v index             Index of item to draw
00191  * @v selected          Item is selected
00192  */
00193 static void pxe_menu_draw_item ( struct pxe_menu *menu,
00194                                  unsigned int index, int selected ) {
00195         char buf[COLS+1];
00196         size_t len;
00197         unsigned int row;
00198 
00199         /* Prepare space-padded row content */
00200         len = snprintf ( buf, sizeof ( buf ), " %c. %s",
00201                          ( 'A' + index ), menu->items[index].desc );
00202         while ( len < ( sizeof ( buf ) - 1 ) )
00203                 buf[len++] = ' ';
00204         buf[ sizeof ( buf ) - 1 ] = '\0';
00205 
00206         /* Draw row */
00207         row = ( LINES - menu->num_items + index );
00208         color_set ( ( selected ? CPAIR_PXE : CPAIR_DEFAULT ), NULL );
00209         mvprintw ( row, 0, "%s", buf );
00210         move ( row, 1 );
00211 }
00212 
00213 /**
00214  * Make selection from PXE boot menu
00215  *
00216  * @v menu              PXE boot menu
00217  * @ret rc              Return status code
00218  */
00219 static int pxe_menu_select ( struct pxe_menu *menu ) {
00220         int key;
00221         unsigned int key_selection;
00222         unsigned int i;
00223         int rc = 0;
00224 
00225         /* Initialise UI */
00226         initscr();
00227         start_color();
00228         color_set ( CPAIR_DEFAULT, NULL );
00229 
00230         /* Draw initial menu */
00231         for ( i = 0 ; i < menu->num_items ; i++ )
00232                 printf ( "\n" );
00233         for ( i = 0 ; i < menu->num_items ; i++ )
00234                 pxe_menu_draw_item ( menu, ( menu->num_items - i - 1 ), 0 );
00235 
00236         while ( 1 ) {
00237 
00238                 /* Highlight currently selected item */
00239                 pxe_menu_draw_item ( menu, menu->selection, 1 );
00240 
00241                 /* Wait for keyboard input */
00242                 key = getkey ( 0 );
00243 
00244                 /* Unhighlight currently selected item */
00245                 pxe_menu_draw_item ( menu, menu->selection, 0 );
00246 
00247                 /* Act upon key */
00248                 if ( ( key == CR ) || ( key == LF ) ) {
00249                         pxe_menu_draw_item ( menu, menu->selection, 1 );
00250                         break;
00251                 } else if ( ( key == CTRL_C ) || ( key == ESC ) ) {
00252                         rc = -ECANCELED;
00253                         break;
00254                 } else if ( key == KEY_UP ) {
00255                         if ( menu->selection > 0 )
00256                                 menu->selection--;
00257                 } else if ( key == KEY_DOWN ) {
00258                         if ( menu->selection < ( menu->num_items - 1 ) )
00259                                 menu->selection++;
00260                 } else if ( ( key < KEY_MIN ) &&
00261                             ( ( key_selection = ( toupper ( key ) - 'A' ) )
00262                               < menu->num_items ) ) {
00263                         menu->selection = key_selection;
00264                         pxe_menu_draw_item ( menu, menu->selection, 1 );
00265                         break;
00266                 }
00267         }
00268 
00269         /* Shut down UI */
00270         endwin();
00271 
00272         return rc;
00273 }
00274 
00275 /**
00276  * Prompt for (and make selection from) PXE boot menu
00277  *
00278  * @v menu              PXE boot menu
00279  * @ret rc              Return status code
00280  */
00281 static int pxe_menu_prompt_and_select ( struct pxe_menu *menu ) {
00282         unsigned long start = currticks();
00283         unsigned long now;
00284         unsigned long elapsed;
00285         size_t len = 0;
00286         int key;
00287         int rc = 0;
00288 
00289         /* Display menu immediately, if specified to do so */
00290         if ( menu->timeout < 0 ) {
00291                 if ( menu->prompt )
00292                         printf ( "%s\n", menu->prompt );
00293                 return pxe_menu_select ( menu );
00294         }
00295 
00296         /* Display prompt, if specified */
00297         if ( menu->prompt )
00298                 printf ( "%s", menu->prompt );
00299 
00300         /* Wait for timeout, if specified */
00301         while ( menu->timeout > 0 ) {
00302                 if ( ! len )
00303                         len = printf ( " (%d)", menu->timeout );
00304                 if ( iskey() ) {
00305                         key = getkey ( 0 );
00306                         if ( key == KEY_F8 ) {
00307                                 /* Display menu */
00308                                 printf ( "\n" );
00309                                 return pxe_menu_select ( menu );
00310                         } else if ( ( key == CTRL_C ) || ( key == ESC ) ) {
00311                                 /* Abort */
00312                                 rc = -ECANCELED;
00313                                 break;
00314                         } else {
00315                                 /* Stop waiting */
00316                                 break;
00317                         }
00318                 }
00319                 now = currticks();
00320                 elapsed = ( now - start );
00321                 if ( elapsed >= TICKS_PER_SEC ) {
00322                         menu->timeout -= 1;
00323                         do {
00324                                 printf ( "\b \b" );
00325                         } while ( --len );
00326                         start = now;
00327                 }
00328         }
00329 
00330         /* Return with default option selected */
00331         printf ( "\n" );
00332         return rc;
00333 }
00334 
00335 /**
00336  * Boot using PXE boot menu
00337  *
00338  * @ret rc              Return status code
00339  *
00340  * Note that a success return status indicates that a PXE boot menu
00341  * item has been selected, and that the DHCP session should perform a
00342  * boot server request/ack.
00343  */
00344 int pxe_menu_boot ( struct net_device *netdev ) {
00345         struct pxe_menu *menu;
00346         unsigned int pxe_type;
00347         struct settings *pxebs_settings;
00348         struct uri *uri;
00349         int rc;
00350 
00351         /* Parse and allocate boot menu */
00352         if ( ( rc = pxe_menu_parse ( &menu ) ) != 0 )
00353                 return rc;
00354 
00355         /* Make selection from boot menu */
00356         if ( ( rc = pxe_menu_prompt_and_select ( menu ) ) != 0 ) {
00357                 free ( menu );
00358                 return rc;
00359         }
00360         pxe_type = menu->items[menu->selection].type;
00361 
00362         /* Free boot menu */
00363         free ( menu );
00364 
00365         /* Return immediately if local boot selected */
00366         if ( ! pxe_type )
00367                 return 0;
00368 
00369         /* Attempt PXE Boot Server Discovery */
00370         if ( ( rc = pxebs ( netdev, pxe_type ) ) != 0 )
00371                 return rc;
00372 
00373         /* Fetch next server and filename */
00374         pxebs_settings = find_settings ( PXEBS_SETTINGS_NAME );
00375         assert ( pxebs_settings );
00376         uri = fetch_next_server_and_filename ( pxebs_settings );
00377         if ( ! uri )
00378                 return -ENOMEM;
00379 
00380         /* Attempt boot */
00381         rc = uriboot ( uri, NULL, 0, 0, NULL, URIBOOT_NO_SAN );
00382         uri_put ( uri );
00383         return rc;
00384 }