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

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