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

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