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

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