選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

pxe_preboot.c 11KB

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