You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pxemenu.c 9.6KB

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