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.

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. 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_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 = fetch_setting_len ( NULL,
  111. &pxe_boot_menu_prompt_setting );
  112. if ( raw_prompt_len < 0 )
  113. raw_prompt_len = 0;
  114. /* Count menu items */
  115. num_menu_items = 0;
  116. raw_menu_item = ( ( void * ) raw_menu );
  117. while ( 1 ) {
  118. if ( ( ( ( void * ) raw_menu_item ) +
  119. sizeof ( *raw_menu_item ) ) > raw_menu_end )
  120. break;
  121. if ( ( ( ( void * ) raw_menu_item ) +
  122. sizeof ( *raw_menu_item ) +
  123. raw_menu_item->desc_len ) > raw_menu_end )
  124. break;
  125. num_menu_items++;
  126. raw_menu_item = ( ( ( void * ) raw_menu_item ) +
  127. sizeof ( *raw_menu_item ) +
  128. raw_menu_item->desc_len );
  129. }
  130. /* Allocate space for parsed menu */
  131. *menu = zalloc ( sizeof ( **menu ) +
  132. ( num_menu_items * sizeof ( (*menu)->items[0] ) ) +
  133. raw_menu_len + 1 /* NUL */ +
  134. raw_prompt_len + 1 /* NUL */ );
  135. if ( ! *menu ) {
  136. DBG ( "Could not allocate PXE boot menu\n" );
  137. return -ENOMEM;
  138. }
  139. /* Fill in parsed menu */
  140. (*menu)->num_items = num_menu_items;
  141. raw_menu_item = ( ( ( void * ) (*menu) ) + sizeof ( **menu ) +
  142. ( num_menu_items * sizeof ( (*menu)->items[0] ) ) );
  143. memcpy ( raw_menu_item, raw_menu, raw_menu_len );
  144. for ( i = 0 ; i < num_menu_items ; i++ ) {
  145. (*menu)->items[i].type = le16_to_cpu ( raw_menu_item->type );
  146. (*menu)->items[i].desc = raw_menu_item->desc;
  147. /* Set type to 0; this ensures that the description
  148. * for the previous menu item is NUL-terminated.
  149. * (Final item is NUL-terminated anyway.)
  150. */
  151. raw_menu_item->type = 0;
  152. raw_menu_item = ( ( ( void * ) raw_menu_item ) +
  153. sizeof ( *raw_menu_item ) +
  154. raw_menu_item->desc_len );
  155. }
  156. if ( raw_prompt_len ) {
  157. raw_menu_prompt = ( ( ( void * ) raw_menu_item ) +
  158. 1 /* NUL */ );
  159. fetch_setting ( NULL, &pxe_boot_menu_prompt_setting,
  160. raw_menu_prompt, raw_prompt_len );
  161. (*menu)->timeout =
  162. ( ( raw_menu_prompt->timeout == 0xff ) ?
  163. -1 : raw_menu_prompt->timeout );
  164. (*menu)->prompt = raw_menu_prompt->prompt;
  165. } else {
  166. (*menu)->timeout = -1;
  167. }
  168. return 0;
  169. }
  170. /**
  171. * Draw PXE boot menu item
  172. *
  173. * @v menu PXE boot menu
  174. * @v index Index of item to draw
  175. * @v selected Item is selected
  176. */
  177. static void pxe_menu_draw_item ( struct pxe_menu *menu,
  178. unsigned int index, int selected ) {
  179. char buf[COLS+1];
  180. size_t len;
  181. unsigned int row;
  182. /* Prepare space-padded row content */
  183. len = snprintf ( buf, sizeof ( buf ), " %c. %s",
  184. ( 'A' + index ), menu->items[index].desc );
  185. while ( len < ( sizeof ( buf ) - 1 ) )
  186. buf[len++] = ' ';
  187. buf[ sizeof ( buf ) - 1 ] = '\0';
  188. /* Draw row */
  189. row = ( LINES - menu->num_items + index );
  190. color_set ( ( selected ? CPAIR_SELECT : CPAIR_NORMAL ), NULL );
  191. mvprintw ( row, 0, "%s", buf );
  192. move ( row, 1 );
  193. }
  194. /**
  195. * Make selection from PXE boot menu
  196. *
  197. * @v menu PXE boot menu
  198. * @ret rc Return status code
  199. */
  200. static int pxe_menu_select ( struct pxe_menu *menu ) {
  201. int key;
  202. unsigned int key_selection;
  203. unsigned int i;
  204. int rc = 0;
  205. /* Initialise UI */
  206. initscr();
  207. start_color();
  208. init_pair ( CPAIR_NORMAL, COLOR_WHITE, COLOR_BLACK );
  209. init_pair ( CPAIR_SELECT, COLOR_BLACK, COLOR_WHITE );
  210. color_set ( CPAIR_NORMAL, NULL );
  211. /* Draw initial menu */
  212. for ( i = 0 ; i < menu->num_items ; i++ )
  213. printf ( "\n" );
  214. for ( i = 0 ; i < menu->num_items ; i++ )
  215. pxe_menu_draw_item ( menu, ( menu->num_items - i - 1 ), 0 );
  216. while ( 1 ) {
  217. /* Highlight currently selected item */
  218. pxe_menu_draw_item ( menu, menu->selection, 1 );
  219. /* Wait for keyboard input */
  220. key = getkey ( 0 );
  221. /* Unhighlight currently selected item */
  222. pxe_menu_draw_item ( menu, menu->selection, 0 );
  223. /* Act upon key */
  224. if ( ( key == CR ) || ( key == LF ) ) {
  225. pxe_menu_draw_item ( menu, menu->selection, 1 );
  226. break;
  227. } else if ( ( key == CTRL_C ) || ( key == ESC ) ) {
  228. rc = -ECANCELED;
  229. break;
  230. } else if ( key == KEY_UP ) {
  231. if ( menu->selection > 0 )
  232. menu->selection--;
  233. } else if ( key == KEY_DOWN ) {
  234. if ( menu->selection < ( menu->num_items - 1 ) )
  235. menu->selection++;
  236. } else if ( ( key < KEY_MIN ) &&
  237. ( ( key_selection = ( toupper ( key ) - 'A' ) )
  238. < menu->num_items ) ) {
  239. menu->selection = key_selection;
  240. pxe_menu_draw_item ( menu, menu->selection, 1 );
  241. break;
  242. }
  243. }
  244. /* Shut down UI */
  245. endwin();
  246. return rc;
  247. }
  248. /**
  249. * Prompt for (and make selection from) PXE boot menu
  250. *
  251. * @v menu PXE boot menu
  252. * @ret rc Return status code
  253. */
  254. static int pxe_menu_prompt_and_select ( struct pxe_menu *menu ) {
  255. unsigned long start = currticks();
  256. unsigned long now;
  257. unsigned long elapsed;
  258. size_t len = 0;
  259. int key;
  260. int rc = 0;
  261. /* Display menu immediately, if specified to do so */
  262. if ( menu->timeout < 0 ) {
  263. if ( menu->prompt )
  264. printf ( "%s\n", menu->prompt );
  265. return pxe_menu_select ( menu );
  266. }
  267. /* Display prompt, if specified */
  268. if ( menu->prompt )
  269. printf ( "%s", menu->prompt );
  270. /* Wait for timeout, if specified */
  271. while ( menu->timeout > 0 ) {
  272. if ( ! len )
  273. len = printf ( " (%d)", menu->timeout );
  274. if ( iskey() ) {
  275. key = getkey ( 0 );
  276. if ( key == KEY_F8 ) {
  277. /* Display menu */
  278. printf ( "\n" );
  279. return pxe_menu_select ( menu );
  280. } else if ( ( key == CTRL_C ) || ( key == ESC ) ) {
  281. /* Abort */
  282. rc = -ECANCELED;
  283. break;
  284. } else {
  285. /* Stop waiting */
  286. break;
  287. }
  288. }
  289. now = currticks();
  290. elapsed = ( now - start );
  291. if ( elapsed >= TICKS_PER_SEC ) {
  292. menu->timeout -= 1;
  293. do {
  294. printf ( "\b \b" );
  295. } while ( --len );
  296. start = now;
  297. }
  298. }
  299. /* Return with default option selected */
  300. printf ( "\n" );
  301. return rc;
  302. }
  303. /**
  304. * Boot using PXE boot menu
  305. *
  306. * @ret rc Return status code
  307. *
  308. * Note that a success return status indicates that a PXE boot menu
  309. * item has been selected, and that the DHCP session should perform a
  310. * boot server request/ack.
  311. */
  312. int pxe_menu_boot ( struct net_device *netdev ) {
  313. struct pxe_menu *menu;
  314. unsigned int pxe_type;
  315. struct settings *pxebs_settings;
  316. struct uri *uri;
  317. int rc;
  318. /* Parse and allocate boot menu */
  319. if ( ( rc = pxe_menu_parse ( &menu ) ) != 0 )
  320. return rc;
  321. /* Make selection from boot menu */
  322. if ( ( rc = pxe_menu_prompt_and_select ( menu ) ) != 0 ) {
  323. free ( menu );
  324. return rc;
  325. }
  326. pxe_type = menu->items[menu->selection].type;
  327. /* Free boot menu */
  328. free ( menu );
  329. /* Return immediately if local boot selected */
  330. if ( ! pxe_type )
  331. return 0;
  332. /* Attempt PXE Boot Server Discovery */
  333. if ( ( rc = pxebs ( netdev, pxe_type ) ) != 0 )
  334. return rc;
  335. /* Fetch next server and filename */
  336. pxebs_settings = find_settings ( PXEBS_SETTINGS_NAME );
  337. assert ( pxebs_settings );
  338. uri = fetch_next_server_and_filename ( pxebs_settings );
  339. if ( ! uri )
  340. return -ENOMEM;
  341. /* Attempt boot */
  342. rc = uriboot ( uri, NULL, 0, URIBOOT_NO_SAN );
  343. uri_put ( uri );
  344. return rc;
  345. }