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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include <ctype.h>
  26. #include <byteswap.h>
  27. #include <curses.h>
  28. #include <ipxe/console.h>
  29. #include <ipxe/dhcp.h>
  30. #include <ipxe/keys.h>
  31. #include <ipxe/timer.h>
  32. #include <ipxe/uri.h>
  33. #include <usr/dhcpmgmt.h>
  34. #include <usr/autoboot.h>
  35. /** @file
  36. *
  37. * PXE Boot Menus
  38. *
  39. */
  40. /* Colour pairs */
  41. #define CPAIR_NORMAL 1
  42. #define CPAIR_SELECT 2
  43. /** A PXE boot menu item */
  44. struct pxe_menu_item {
  45. /** Boot Server type */
  46. unsigned int type;
  47. /** Description */
  48. char *desc;
  49. };
  50. /**
  51. * A PXE boot menu
  52. *
  53. * This structure encapsulates the menu information provided via DHCP
  54. * options.
  55. */
  56. struct pxe_menu {
  57. /** Prompt string (optional) */
  58. const char *prompt;
  59. /** Timeout (in seconds)
  60. *
  61. * Negative indicates no timeout (i.e. wait indefinitely)
  62. */
  63. int timeout;
  64. /** Number of menu items */
  65. unsigned int num_items;
  66. /** Selected menu item */
  67. unsigned int selection;
  68. /** Menu items */
  69. struct pxe_menu_item items[0];
  70. };
  71. /**
  72. * Parse and allocate PXE boot menu
  73. *
  74. * @v menu PXE boot menu to fill in
  75. * @ret rc Return status code
  76. *
  77. * It is the callers responsibility to eventually free the allocated
  78. * boot menu.
  79. */
  80. static int pxe_menu_parse ( struct pxe_menu **menu ) {
  81. struct setting pxe_boot_menu_prompt_setting =
  82. { .tag = DHCP_PXE_BOOT_MENU_PROMPT };
  83. struct setting pxe_boot_menu_setting =
  84. { .tag = DHCP_PXE_BOOT_MENU };
  85. uint8_t raw_menu[256];
  86. int raw_prompt_len;
  87. int raw_menu_len;
  88. struct dhcp_pxe_boot_menu *raw_menu_item;
  89. struct dhcp_pxe_boot_menu_prompt *raw_menu_prompt;
  90. void *raw_menu_end;
  91. unsigned int num_menu_items;
  92. unsigned int i;
  93. int rc;
  94. /* Fetch raw menu */
  95. memset ( raw_menu, 0, sizeof ( raw_menu ) );
  96. if ( ( raw_menu_len = fetch_raw_setting ( NULL, &pxe_boot_menu_setting,
  97. raw_menu,
  98. sizeof ( raw_menu ) ) ) < 0 ){
  99. rc = raw_menu_len;
  100. DBG ( "Could not retrieve raw PXE boot menu: %s\n",
  101. strerror ( rc ) );
  102. return rc;
  103. }
  104. if ( raw_menu_len >= ( int ) sizeof ( raw_menu ) ) {
  105. DBG ( "Raw PXE boot menu too large for buffer\n" );
  106. return -ENOSPC;
  107. }
  108. raw_menu_end = ( raw_menu + raw_menu_len );
  109. /* Fetch raw prompt length */
  110. raw_prompt_len =
  111. fetch_raw_setting ( NULL, &pxe_boot_menu_prompt_setting,
  112. NULL, 0 );
  113. if ( raw_prompt_len < 0 )
  114. raw_prompt_len = 0;
  115. /* Count menu items */
  116. num_menu_items = 0;
  117. raw_menu_item = ( ( void * ) raw_menu );
  118. while ( 1 ) {
  119. if ( ( ( ( void * ) raw_menu_item ) +
  120. sizeof ( *raw_menu_item ) ) > raw_menu_end )
  121. break;
  122. if ( ( ( ( void * ) raw_menu_item ) +
  123. sizeof ( *raw_menu_item ) +
  124. raw_menu_item->desc_len ) > raw_menu_end )
  125. break;
  126. num_menu_items++;
  127. raw_menu_item = ( ( ( void * ) raw_menu_item ) +
  128. sizeof ( *raw_menu_item ) +
  129. raw_menu_item->desc_len );
  130. }
  131. /* Allocate space for parsed menu */
  132. *menu = zalloc ( sizeof ( **menu ) +
  133. ( num_menu_items * sizeof ( (*menu)->items[0] ) ) +
  134. raw_menu_len + 1 /* NUL */ +
  135. raw_prompt_len + 1 /* NUL */ );
  136. if ( ! *menu ) {
  137. DBG ( "Could not allocate PXE boot menu\n" );
  138. return -ENOMEM;
  139. }
  140. /* Fill in parsed menu */
  141. (*menu)->num_items = num_menu_items;
  142. raw_menu_item = ( ( ( void * ) (*menu) ) + sizeof ( **menu ) +
  143. ( num_menu_items * sizeof ( (*menu)->items[0] ) ) );
  144. memcpy ( raw_menu_item, raw_menu, raw_menu_len );
  145. for ( i = 0 ; i < num_menu_items ; i++ ) {
  146. (*menu)->items[i].type = le16_to_cpu ( raw_menu_item->type );
  147. (*menu)->items[i].desc = raw_menu_item->desc;
  148. /* Set type to 0; this ensures that the description
  149. * for the previous menu item is NUL-terminated.
  150. * (Final item is NUL-terminated anyway.)
  151. */
  152. raw_menu_item->type = 0;
  153. raw_menu_item = ( ( ( void * ) raw_menu_item ) +
  154. sizeof ( *raw_menu_item ) +
  155. raw_menu_item->desc_len );
  156. }
  157. if ( raw_prompt_len ) {
  158. raw_menu_prompt = ( ( ( void * ) raw_menu_item ) +
  159. 1 /* NUL */ );
  160. fetch_raw_setting ( NULL, &pxe_boot_menu_prompt_setting,
  161. raw_menu_prompt, raw_prompt_len );
  162. (*menu)->timeout =
  163. ( ( raw_menu_prompt->timeout == 0xff ) ?
  164. -1 : raw_menu_prompt->timeout );
  165. (*menu)->prompt = raw_menu_prompt->prompt;
  166. } else {
  167. (*menu)->timeout = -1;
  168. }
  169. return 0;
  170. }
  171. /**
  172. * Draw PXE boot menu item
  173. *
  174. * @v menu PXE boot menu
  175. * @v index Index of item to draw
  176. * @v selected Item is selected
  177. */
  178. static void pxe_menu_draw_item ( struct pxe_menu *menu,
  179. unsigned int index, int selected ) {
  180. char buf[COLS+1];
  181. size_t len;
  182. unsigned int row;
  183. /* Prepare space-padded row content */
  184. len = snprintf ( buf, sizeof ( buf ), " %c. %s",
  185. ( 'A' + index ), menu->items[index].desc );
  186. while ( len < ( sizeof ( buf ) - 1 ) )
  187. buf[len++] = ' ';
  188. buf[ sizeof ( buf ) - 1 ] = '\0';
  189. /* Draw row */
  190. row = ( LINES - menu->num_items + index );
  191. color_set ( ( selected ? CPAIR_SELECT : CPAIR_NORMAL ), NULL );
  192. mvprintw ( row, 0, "%s", buf );
  193. move ( row, 1 );
  194. }
  195. /**
  196. * Make selection from PXE boot menu
  197. *
  198. * @v menu PXE boot menu
  199. * @ret rc Return status code
  200. */
  201. static int pxe_menu_select ( struct pxe_menu *menu ) {
  202. int key;
  203. unsigned int key_selection;
  204. unsigned int i;
  205. int rc = 0;
  206. /* Initialise UI */
  207. initscr();
  208. start_color();
  209. init_pair ( CPAIR_NORMAL, COLOR_WHITE, COLOR_BLACK );
  210. init_pair ( CPAIR_SELECT, COLOR_BLACK, COLOR_WHITE );
  211. color_set ( CPAIR_NORMAL, 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, URIBOOT_NO_SAN );
  344. uri_put ( uri );
  345. return rc;
  346. }