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 8.4KB

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