Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

pxe_preboot.c 8.6KB

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