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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. #include <stdint.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <gpxe/uaccess.h>
  28. #include <gpxe/dhcp.h>
  29. #include <gpxe/device.h>
  30. #include <gpxe/netdevice.h>
  31. #include <gpxe/isapnp.h>
  32. #include <gpxe/init.h>
  33. #include <gpxe/if_ether.h>
  34. #include <basemem_packet.h>
  35. #include "pxe.h"
  36. #include "pxe_call.h"
  37. /* Avoid dragging in isapnp.o unnecessarily */
  38. uint16_t isapnp_read_port;
  39. /** Zero-based versions of PXENV_GET_CACHED_INFO::PacketType */
  40. enum pxe_cached_info_indices {
  41. CACHED_INFO_DHCPDISCOVER = ( PXENV_PACKET_TYPE_DHCP_DISCOVER - 1 ),
  42. CACHED_INFO_DHCPACK = ( PXENV_PACKET_TYPE_DHCP_ACK - 1 ),
  43. CACHED_INFO_BINL = ( PXENV_PACKET_TYPE_CACHED_REPLY - 1 ),
  44. NUM_CACHED_INFOS
  45. };
  46. /** A cached DHCP packet */
  47. union pxe_cached_info {
  48. struct dhcphdr dhcphdr;
  49. char raw[ETH_FRAME_LEN];
  50. };
  51. /* The case in which the caller doesn't supply a buffer is really
  52. * awkward to support given that we have multiple sources of options,
  53. * and that we don't actually store the DHCP packets. (We may not
  54. * even have performed DHCP; we may have obtained all configuration
  55. * from non-volatile stored options or from the command line.)
  56. *
  57. * Some NBPs rely on the buffers we provide being persistent, so we
  58. * can't just use the temporary packet buffer. 4.5kB of base memory
  59. * always wasted just because some clients are too lazy to provide
  60. * their own buffers...
  61. */
  62. static union pxe_cached_info __bss16_array ( cached_info, [NUM_CACHED_INFOS] );
  63. #define cached_info __use_data16 ( cached_info )
  64. /**
  65. * Set PXE cached TFTP filename
  66. *
  67. * @v filename TFTP filename
  68. *
  69. * This is a bug-for-bug compatibility hack needed in order to work
  70. * with Microsoft Remote Installation Services (RIS). The filename
  71. * used in a call to PXENV_RESTART_TFTP or PXENV_TFTP_READ_FILE must
  72. * be returned as the DHCP filename in subsequent calls to
  73. * PXENV_GET_CACHED_INFO.
  74. */
  75. void pxe_set_cached_filename ( const unsigned char *filename ) {
  76. memcpy ( cached_info[CACHED_INFO_DHCPACK].dhcphdr.file, filename,
  77. sizeof ( cached_info[CACHED_INFO_DHCPACK].dhcphdr.file ) );
  78. memcpy ( cached_info[CACHED_INFO_BINL].dhcphdr.file, filename,
  79. sizeof ( cached_info[CACHED_INFO_BINL].dhcphdr.file ) );
  80. }
  81. /**
  82. * UNLOAD BASE CODE STACK
  83. *
  84. * @v None -
  85. * @ret ...
  86. *
  87. */
  88. PXENV_EXIT_t pxenv_unload_stack ( struct s_PXENV_UNLOAD_STACK *unload_stack ) {
  89. DBG ( "PXENV_UNLOAD_STACK" );
  90. unload_stack->Status = PXENV_STATUS_SUCCESS;
  91. return PXENV_EXIT_SUCCESS;
  92. }
  93. /* PXENV_GET_CACHED_INFO
  94. *
  95. * Status: working
  96. */
  97. PXENV_EXIT_t pxenv_get_cached_info ( struct s_PXENV_GET_CACHED_INFO
  98. *get_cached_info ) {
  99. struct dhcp_packet dhcppkt;
  100. int ( * dhcp_packet_creator ) ( struct net_device *, int,
  101. struct dhcp_option_block *, void *,
  102. size_t, struct dhcp_packet * );
  103. unsigned int idx;
  104. unsigned int msgtype;
  105. size_t len;
  106. userptr_t buffer;
  107. int rc;
  108. DBG ( "PXENV_GET_CACHED_INFO %d", get_cached_info->PacketType );
  109. DBG ( " to %04x:%04x+%x", get_cached_info->Buffer.segment,
  110. get_cached_info->Buffer.offset, get_cached_info->BufferSize );
  111. /* Sanity check */
  112. idx = ( get_cached_info->PacketType - 1 );
  113. if ( idx >= ( sizeof ( cached_info ) / sizeof ( cached_info[0] ) ) ) {
  114. DBG ( " bad PacketType" );
  115. goto err;
  116. }
  117. /* Construct cached version of packet, if not already constructed. */
  118. if ( ! cached_info[idx].dhcphdr.op ) {
  119. /* Construct DHCP packet */
  120. if ( get_cached_info->PacketType ==
  121. PXENV_PACKET_TYPE_DHCP_DISCOVER ) {
  122. dhcp_packet_creator = create_dhcp_request;
  123. msgtype = DHCPDISCOVER;
  124. } else {
  125. dhcp_packet_creator = create_dhcp_response;
  126. msgtype = DHCPACK;
  127. }
  128. if ( ( rc = dhcp_packet_creator ( pxe_netdev, msgtype,
  129. NULL, &cached_info[idx],
  130. sizeof ( cached_info[idx] ),
  131. &dhcppkt ) ) != 0 ) {
  132. DBG ( " failed to build packet" );
  133. goto err;
  134. }
  135. }
  136. len = get_cached_info->BufferSize;
  137. if ( len == 0 ) {
  138. /* Point client at our cached buffer.
  139. *
  140. * To add to the fun, Intel decided at some point in
  141. * the evolution of the PXE specification to add the
  142. * BufferLimit field, which we are meant to fill in
  143. * with the length of our packet buffer, so that the
  144. * caller can safely modify the boot server reply
  145. * packet stored therein. However, this field was not
  146. * present in earlier versions of the PXE spec, and
  147. * there is at least one PXE NBP (Altiris) which
  148. * allocates only exactly enough space for this
  149. * earlier, shorter version of the structure. If we
  150. * actually fill in the BufferLimit field, we
  151. * therefore risk trashing random areas of the
  152. * caller's memory. If we *don't* fill it in, then
  153. * the caller is at liberty to assume that whatever
  154. * random value happened to be in that location
  155. * represents the length of the buffer we've just
  156. * passed back to it.
  157. *
  158. * Since older PXE stacks won't fill this field in
  159. * anyway, it's probably safe to assume that no
  160. * callers actually rely on it, so we choose to not
  161. * fill it in.
  162. */
  163. get_cached_info->Buffer.segment = rm_ds;
  164. get_cached_info->Buffer.offset =
  165. ( unsigned ) ( & __from_data16 ( cached_info[idx] ) );
  166. get_cached_info->BufferSize = sizeof ( cached_info[idx] );
  167. DBG ( " returning %04x:%04x+%04x['%x']",
  168. get_cached_info->Buffer.segment,
  169. get_cached_info->Buffer.offset,
  170. get_cached_info->BufferSize,
  171. get_cached_info->BufferLimit );
  172. } else {
  173. /* Copy packet to client buffer */
  174. if ( len > sizeof ( cached_info[idx] ) )
  175. len = sizeof ( cached_info[idx] );
  176. if ( len < sizeof ( cached_info[idx] ) )
  177. DBG ( " buffer may be too short" );
  178. buffer = real_to_user ( get_cached_info->Buffer.segment,
  179. get_cached_info->Buffer.offset );
  180. copy_to_user ( buffer, 0, &cached_info[idx], len );
  181. get_cached_info->BufferSize = len;
  182. }
  183. get_cached_info->Status = PXENV_STATUS_SUCCESS;
  184. return PXENV_EXIT_SUCCESS;
  185. err:
  186. get_cached_info->Status = PXENV_STATUS_OUT_OF_RESOURCES;
  187. return PXENV_EXIT_FAILURE;
  188. }
  189. /* PXENV_RESTART_TFTP
  190. *
  191. * Status: working
  192. */
  193. PXENV_EXIT_t pxenv_restart_tftp ( struct s_PXENV_TFTP_READ_FILE
  194. *restart_tftp ) {
  195. PXENV_EXIT_t tftp_exit;
  196. DBG ( "PXENV_RESTART_TFTP " );
  197. /* Intel bug-for-bug hack */
  198. pxe_set_cached_filename ( restart_tftp->FileName );
  199. /* Words cannot describe the complete mismatch between the PXE
  200. * specification and any possible version of reality...
  201. */
  202. restart_tftp->Buffer = PXE_LOAD_PHYS; /* Fixed by spec, apparently */
  203. restart_tftp->BufferSize = ( 0xa0000 - PXE_LOAD_PHYS ); /* Near enough */
  204. tftp_exit = pxenv_tftp_read_file ( restart_tftp );
  205. if ( tftp_exit != PXENV_EXIT_SUCCESS )
  206. return tftp_exit;
  207. /* Fire up the new NBP */
  208. restart_tftp->Status = pxe_start_nbp();
  209. /* Not sure what "SUCCESS" actually means, since we can only
  210. * return if the new NBP failed to boot...
  211. */
  212. return PXENV_EXIT_SUCCESS;
  213. }
  214. /* PXENV_START_UNDI
  215. *
  216. * Status: working
  217. */
  218. PXENV_EXIT_t pxenv_start_undi ( struct s_PXENV_START_UNDI *start_undi ) {
  219. unsigned int bus_type;
  220. unsigned int location;
  221. struct net_device *netdev;
  222. DBG ( "PXENV_START_UNDI %04x:%04x:%04x",
  223. start_undi->AX, start_undi->BX, start_undi->DX );
  224. /* Determine bus type and location. Use a heuristic to decide
  225. * whether we are PCI or ISAPnP
  226. */
  227. if ( ( start_undi->DX >= ISAPNP_READ_PORT_MIN ) &&
  228. ( start_undi->DX <= ISAPNP_READ_PORT_MAX ) &&
  229. ( start_undi->BX >= ISAPNP_CSN_MIN ) &&
  230. ( start_undi->BX <= ISAPNP_CSN_MAX ) ) {
  231. bus_type = BUS_TYPE_ISAPNP;
  232. location = start_undi->BX;
  233. /* Record ISAPnP read port for use by isapnp.c */
  234. isapnp_read_port = start_undi->DX;
  235. } else {
  236. bus_type = BUS_TYPE_PCI;
  237. location = start_undi->AX;
  238. }
  239. /* Probe for devices, etc. */
  240. startup();
  241. /* Look for a matching net device */
  242. netdev = find_netdev_by_location ( bus_type, location );
  243. if ( ! netdev ) {
  244. DBG ( " no net device found" );
  245. start_undi->Status = PXENV_STATUS_UNDI_CANNOT_INITIALIZE_NIC;
  246. return PXENV_EXIT_FAILURE;
  247. }
  248. DBG ( " using netdev %s", netdev->name );
  249. /* Save as PXE net device */
  250. pxe_set_netdev ( netdev );
  251. /* Hook INT 1A */
  252. pxe_hook_int1a();
  253. start_undi->Status = PXENV_STATUS_SUCCESS;
  254. return PXENV_EXIT_SUCCESS;
  255. }
  256. /* PXENV_STOP_UNDI
  257. *
  258. * Status: working
  259. */
  260. PXENV_EXIT_t pxenv_stop_undi ( struct s_PXENV_STOP_UNDI *stop_undi ) {
  261. DBG ( "PXENV_STOP_UNDI" );
  262. /* Unhook INT 1A */
  263. pxe_unhook_int1a();
  264. /* Clear PXE net device */
  265. pxe_set_netdev ( NULL );
  266. /* Prepare for unload */
  267. shutdown();
  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. }