Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

pxemenu.c 9.6KB

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 <console.h>
  28. #include <ipxe/dhcp.h>
  29. #include <ipxe/keys.h>
  30. #include <ipxe/timer.h>
  31. #include <ipxe/process.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. while ( ! iskey() )
  220. step();
  221. key = getkey();
  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();
  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 in_addr next_server;
  318. char filename[256];
  319. int rc;
  320. /* Parse and allocate boot menu */
  321. if ( ( rc = pxe_menu_parse ( &menu ) ) != 0 )
  322. return rc;
  323. /* Make selection from boot menu */
  324. if ( ( rc = pxe_menu_prompt_and_select ( menu ) ) != 0 ) {
  325. free ( menu );
  326. return rc;
  327. }
  328. pxe_type = menu->items[menu->selection].type;
  329. /* Free boot menu */
  330. free ( menu );
  331. /* Return immediately if local boot selected */
  332. if ( ! pxe_type )
  333. return 0;
  334. /* Attempt PXE Boot Server Discovery */
  335. if ( ( rc = pxebs ( netdev, pxe_type ) ) != 0 )
  336. return rc;
  337. /* Attempt boot */
  338. pxebs_settings = find_settings ( PXEBS_SETTINGS_NAME );
  339. assert ( pxebs_settings );
  340. fetch_ipv4_setting ( pxebs_settings, &next_server_setting,
  341. &next_server );
  342. fetch_string_setting ( pxebs_settings, &filename_setting,
  343. filename, sizeof ( filename ) );
  344. return boot_next_server_and_filename ( next_server, filename );
  345. }