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.

pxe_preboot.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /** @file
  2. *
  3. * PXE Preboot API
  4. *
  5. */
  6. /* PXE API interface for Etherboot.
  7. *
  8. * Copyright (C) 2004 Michael Brown <mbrown@fensystems.co.uk>.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of the
  13. * License, or any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  23. * 02110-1301, USA.
  24. *
  25. * You can also choose to distribute this program under the terms of
  26. * the Unmodified Binary Distribution Licence (as given in the file
  27. * COPYING.UBDL), provided that you have satisfied its requirements.
  28. */
  29. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  30. #include <stdint.h>
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include <setjmp.h>
  34. #include <ipxe/uaccess.h>
  35. #include <ipxe/dhcp.h>
  36. #include <ipxe/fakedhcp.h>
  37. #include <ipxe/device.h>
  38. #include <ipxe/netdevice.h>
  39. #include <ipxe/isapnp.h>
  40. #include <ipxe/init.h>
  41. #include <ipxe/if_ether.h>
  42. #include <basemem_packet.h>
  43. #include <biosint.h>
  44. #include "pxe.h"
  45. #include "pxe_call.h"
  46. /* Avoid dragging in isapnp.o unnecessarily */
  47. uint16_t isapnp_read_port;
  48. /** Zero-based versions of PXENV_GET_CACHED_INFO::PacketType */
  49. enum pxe_cached_info_indices {
  50. CACHED_INFO_DHCPDISCOVER = ( PXENV_PACKET_TYPE_DHCP_DISCOVER - 1 ),
  51. CACHED_INFO_DHCPACK = ( PXENV_PACKET_TYPE_DHCP_ACK - 1 ),
  52. CACHED_INFO_BINL = ( PXENV_PACKET_TYPE_CACHED_REPLY - 1 ),
  53. NUM_CACHED_INFOS
  54. };
  55. /** A cached DHCP packet */
  56. union pxe_cached_info {
  57. struct dhcphdr dhcphdr;
  58. /* This buffer must be *exactly* the size of a BOOTPLAYER_t
  59. * structure, otherwise WinPE will die horribly. It takes the
  60. * size of *our* buffer and feeds it in to us as the size of
  61. * one of *its* buffers. If our buffer is larger than it
  62. * expects, we therefore end up overwriting part of its data
  63. * segment, since it tells us to do so. (D'oh!)
  64. *
  65. * Note that a BOOTPLAYER_t is not necessarily large enough to
  66. * hold a DHCP packet; this is a flaw in the PXE spec.
  67. */
  68. BOOTPLAYER_t packet;
  69. } __attribute__ (( packed ));
  70. /** A PXE DHCP packet creator */
  71. struct pxe_dhcp_packet_creator {
  72. /** Create DHCP packet
  73. *
  74. * @v netdev Network device
  75. * @v data Buffer for DHCP packet
  76. * @v max_len Size of DHCP packet buffer
  77. * @ret rc Return status code
  78. */
  79. int ( * create ) ( struct net_device *netdev, void *data,
  80. size_t max_len );
  81. };
  82. /** PXE DHCP packet creators */
  83. static struct pxe_dhcp_packet_creator pxe_dhcp_packet_creators[] = {
  84. [CACHED_INFO_DHCPDISCOVER] = { create_fakedhcpdiscover },
  85. [CACHED_INFO_DHCPACK] = { create_fakedhcpack },
  86. [CACHED_INFO_BINL] = { create_fakepxebsack },
  87. };
  88. /**
  89. * Name PXENV_GET_CACHED_INFO packet type
  90. *
  91. * @v packet_type Packet type
  92. * @ret name Name of packet type
  93. */
  94. static inline __attribute__ (( always_inline )) const char *
  95. pxenv_get_cached_info_name ( int packet_type ) {
  96. switch ( packet_type ) {
  97. case PXENV_PACKET_TYPE_DHCP_DISCOVER:
  98. return "DHCPDISCOVER";
  99. case PXENV_PACKET_TYPE_DHCP_ACK:
  100. return "DHCPACK";
  101. case PXENV_PACKET_TYPE_CACHED_REPLY:
  102. return "BINL";
  103. default:
  104. return "<INVALID>";
  105. }
  106. }
  107. /* The case in which the caller doesn't supply a buffer is really
  108. * awkward to support given that we have multiple sources of options,
  109. * and that we don't actually store the DHCP packets. (We may not
  110. * even have performed DHCP; we may have obtained all configuration
  111. * from non-volatile stored options or from the command line.)
  112. *
  113. * Some NBPs rely on the buffers we provide being persistent, so we
  114. * can't just use the temporary packet buffer. 4.5kB of base memory
  115. * always wasted just because some clients are too lazy to provide
  116. * their own buffers...
  117. */
  118. static union pxe_cached_info __bss16_array ( cached_info, [NUM_CACHED_INFOS] );
  119. #define cached_info __use_data16 ( cached_info )
  120. /**
  121. * UNLOAD BASE CODE STACK
  122. *
  123. * @v None -
  124. * @ret ...
  125. *
  126. */
  127. static PXENV_EXIT_t
  128. pxenv_unload_stack ( struct s_PXENV_UNLOAD_STACK *unload_stack ) {
  129. DBGC ( &pxe_netdev, "PXENV_UNLOAD_STACK\n" );
  130. unload_stack->Status = PXENV_STATUS_SUCCESS;
  131. return PXENV_EXIT_SUCCESS;
  132. }
  133. /* PXENV_GET_CACHED_INFO
  134. *
  135. * Status: working
  136. */
  137. static PXENV_EXIT_t
  138. pxenv_get_cached_info ( struct s_PXENV_GET_CACHED_INFO *get_cached_info ) {
  139. struct pxe_dhcp_packet_creator *creator;
  140. union pxe_cached_info *info;
  141. unsigned int idx;
  142. size_t len;
  143. userptr_t buffer;
  144. int rc;
  145. DBGC ( &pxe_netdev, "PXENV_GET_CACHED_INFO %s to %04x:%04x+%x",
  146. pxenv_get_cached_info_name ( get_cached_info->PacketType ),
  147. get_cached_info->Buffer.segment,
  148. get_cached_info->Buffer.offset, get_cached_info->BufferSize );
  149. /* Sanity check */
  150. if ( ! pxe_netdev ) {
  151. DBGC ( &pxe_netdev, "PXENV_GET_CACHED_INFO called with no "
  152. "network device\n" );
  153. get_cached_info->Status = PXENV_STATUS_UNDI_INVALID_STATE;
  154. return PXENV_EXIT_FAILURE;
  155. }
  156. /* Sanity check */
  157. idx = ( get_cached_info->PacketType - 1 );
  158. if ( idx >= NUM_CACHED_INFOS ) {
  159. DBGC ( &pxe_netdev, " bad PacketType %d\n",
  160. get_cached_info->PacketType );
  161. goto err;
  162. }
  163. info = &cached_info[idx];
  164. /* Construct cached version of packet, if not already constructed. */
  165. if ( ! info->dhcphdr.op ) {
  166. /* Construct DHCP packet */
  167. creator = &pxe_dhcp_packet_creators[idx];
  168. if ( ( rc = creator->create ( pxe_netdev, info,
  169. sizeof ( *info ) ) ) != 0 ) {
  170. DBGC ( &pxe_netdev, " failed to build packet: %s\n",
  171. strerror ( rc ) );
  172. goto err;
  173. }
  174. }
  175. len = get_cached_info->BufferSize;
  176. if ( len == 0 ) {
  177. /* Point client at our cached buffer.
  178. *
  179. * To add to the fun, Intel decided at some point in
  180. * the evolution of the PXE specification to add the
  181. * BufferLimit field, which we are meant to fill in
  182. * with the length of our packet buffer, so that the
  183. * caller can safely modify the boot server reply
  184. * packet stored therein. However, this field was not
  185. * present in earlier versions of the PXE spec, and
  186. * there is at least one PXE NBP (Altiris) which
  187. * allocates only exactly enough space for this
  188. * earlier, shorter version of the structure. If we
  189. * actually fill in the BufferLimit field, we
  190. * therefore risk trashing random areas of the
  191. * caller's memory. If we *don't* fill it in, then
  192. * the caller is at liberty to assume that whatever
  193. * random value happened to be in that location
  194. * represents the length of the buffer we've just
  195. * passed back to it.
  196. *
  197. * Since older PXE stacks won't fill this field in
  198. * anyway, it's probably safe to assume that no
  199. * callers actually rely on it, so we choose to not
  200. * fill it in.
  201. */
  202. get_cached_info->Buffer.segment = rm_ds;
  203. get_cached_info->Buffer.offset = __from_data16 ( info );
  204. get_cached_info->BufferSize = sizeof ( *info );
  205. DBGC ( &pxe_netdev, " using %04x:%04x+%04x['%x']",
  206. get_cached_info->Buffer.segment,
  207. get_cached_info->Buffer.offset,
  208. get_cached_info->BufferSize,
  209. get_cached_info->BufferLimit );
  210. } else {
  211. /* Copy packet to client buffer */
  212. if ( len > sizeof ( *info ) )
  213. len = sizeof ( *info );
  214. if ( len < sizeof ( *info ) )
  215. DBGC ( &pxe_netdev, " buffer may be too short" );
  216. buffer = real_to_user ( get_cached_info->Buffer.segment,
  217. get_cached_info->Buffer.offset );
  218. copy_to_user ( buffer, 0, info, len );
  219. get_cached_info->BufferSize = len;
  220. }
  221. DBGC ( &pxe_netdev, "\n" );
  222. get_cached_info->Status = PXENV_STATUS_SUCCESS;
  223. return PXENV_EXIT_SUCCESS;
  224. err:
  225. get_cached_info->Status = PXENV_STATUS_OUT_OF_RESOURCES;
  226. return PXENV_EXIT_FAILURE;
  227. }
  228. /* PXENV_RESTART_TFTP
  229. *
  230. * Status: working
  231. */
  232. static PXENV_EXIT_t
  233. pxenv_restart_tftp ( struct s_PXENV_TFTP_READ_FILE *restart_tftp ) {
  234. PXENV_EXIT_t tftp_exit;
  235. DBGC ( &pxe_netdev, "PXENV_RESTART_TFTP\n" );
  236. /* Words cannot describe the complete mismatch between the PXE
  237. * specification and any possible version of reality...
  238. */
  239. restart_tftp->Buffer = PXE_LOAD_PHYS; /* Fixed by spec, apparently */
  240. restart_tftp->BufferSize = ( 0xa0000 - PXE_LOAD_PHYS ); /* Near enough */
  241. tftp_exit = pxenv_tftp_read_file ( restart_tftp );
  242. if ( tftp_exit != PXENV_EXIT_SUCCESS )
  243. return tftp_exit;
  244. /* Restart NBP */
  245. rmlongjmp ( pxe_restart_nbp, PXENV_RESTART_TFTP );
  246. }
  247. /* PXENV_START_UNDI
  248. *
  249. * Status: working
  250. */
  251. static PXENV_EXIT_t pxenv_start_undi ( struct s_PXENV_START_UNDI *start_undi ) {
  252. unsigned int bus_type;
  253. unsigned int location;
  254. struct net_device *netdev;
  255. DBGC ( &pxe_netdev, "PXENV_START_UNDI %04x:%04x:%04x\n",
  256. start_undi->AX, start_undi->BX, start_undi->DX );
  257. /* Determine bus type and location. Use a heuristic to decide
  258. * whether we are PCI or ISAPnP
  259. */
  260. if ( ( start_undi->DX >= ISAPNP_READ_PORT_MIN ) &&
  261. ( start_undi->DX <= ISAPNP_READ_PORT_MAX ) &&
  262. ( start_undi->BX >= ISAPNP_CSN_MIN ) &&
  263. ( start_undi->BX <= ISAPNP_CSN_MAX ) ) {
  264. bus_type = BUS_TYPE_ISAPNP;
  265. location = start_undi->BX;
  266. /* Record ISAPnP read port for use by isapnp.c */
  267. isapnp_read_port = start_undi->DX;
  268. } else {
  269. bus_type = BUS_TYPE_PCI;
  270. location = start_undi->AX;
  271. }
  272. /* Probe for devices, etc. */
  273. startup();
  274. /* Look for a matching net device */
  275. netdev = find_netdev_by_location ( bus_type, location );
  276. if ( ! netdev ) {
  277. DBGC ( &pxe_netdev, "PXENV_START_UNDI could not find matching "
  278. "net device\n" );
  279. start_undi->Status = PXENV_STATUS_UNDI_CANNOT_INITIALIZE_NIC;
  280. return PXENV_EXIT_FAILURE;
  281. }
  282. DBGC ( &pxe_netdev, "PXENV_START_UNDI found net device %s\n",
  283. netdev->name );
  284. /* Activate PXE */
  285. pxe_activate ( netdev );
  286. start_undi->Status = PXENV_STATUS_SUCCESS;
  287. return PXENV_EXIT_SUCCESS;
  288. }
  289. /* PXENV_STOP_UNDI
  290. *
  291. * Status: working
  292. */
  293. static PXENV_EXIT_t pxenv_stop_undi ( struct s_PXENV_STOP_UNDI *stop_undi ) {
  294. DBGC ( &pxe_netdev, "PXENV_STOP_UNDI\n" );
  295. /* Deactivate PXE */
  296. pxe_deactivate();
  297. /* Prepare for unload */
  298. shutdown_boot();
  299. /* Check to see if we still have any hooked interrupts */
  300. if ( hooked_bios_interrupts != 0 ) {
  301. DBGC ( &pxe_netdev, "PXENV_STOP_UNDI failed: %d interrupts "
  302. "still hooked\n", hooked_bios_interrupts );
  303. stop_undi->Status = PXENV_STATUS_KEEP_UNDI;
  304. return PXENV_EXIT_FAILURE;
  305. }
  306. stop_undi->Status = PXENV_STATUS_SUCCESS;
  307. return PXENV_EXIT_SUCCESS;
  308. }
  309. /* PXENV_START_BASE
  310. *
  311. * Status: won't implement (requires major structural changes)
  312. */
  313. static PXENV_EXIT_t pxenv_start_base ( struct s_PXENV_START_BASE *start_base ) {
  314. DBGC ( &pxe_netdev, "PXENV_START_BASE\n" );
  315. start_base->Status = PXENV_STATUS_UNSUPPORTED;
  316. return PXENV_EXIT_FAILURE;
  317. }
  318. /* PXENV_STOP_BASE
  319. *
  320. * Status: working
  321. */
  322. static PXENV_EXIT_t pxenv_stop_base ( struct s_PXENV_STOP_BASE *stop_base ) {
  323. DBGC ( &pxe_netdev, "PXENV_STOP_BASE\n" );
  324. /* The only time we will be called is when the NBP is trying
  325. * to shut down the PXE stack. There's nothing we need to do
  326. * in this call.
  327. */
  328. stop_base->Status = PXENV_STATUS_SUCCESS;
  329. return PXENV_EXIT_SUCCESS;
  330. }
  331. /** PXE preboot API */
  332. struct pxe_api_call pxe_preboot_api[] __pxe_api_call = {
  333. PXE_API_CALL ( PXENV_UNLOAD_STACK, pxenv_unload_stack,
  334. struct s_PXENV_UNLOAD_STACK ),
  335. PXE_API_CALL ( PXENV_GET_CACHED_INFO, pxenv_get_cached_info,
  336. struct s_PXENV_GET_CACHED_INFO ),
  337. PXE_API_CALL ( PXENV_RESTART_TFTP, pxenv_restart_tftp,
  338. struct s_PXENV_TFTP_READ_FILE ),
  339. PXE_API_CALL ( PXENV_START_UNDI, pxenv_start_undi,
  340. struct s_PXENV_START_UNDI ),
  341. PXE_API_CALL ( PXENV_STOP_UNDI, pxenv_stop_undi,
  342. struct s_PXENV_STOP_UNDI ),
  343. PXE_API_CALL ( PXENV_START_BASE, pxenv_start_base,
  344. struct s_PXENV_START_BASE ),
  345. PXE_API_CALL ( PXENV_STOP_BASE, pxenv_stop_base,
  346. struct s_PXENV_STOP_BASE ),
  347. };