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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. void *data = NULL;
  66. size_t len;
  67. int msgtype;
  68. struct dhcp_option_block *options;
  69. userptr_t buffer;
  70. int rc;
  71. DBG ( "PXENV_GET_CACHED_INFO %d", get_cached_info->PacketType );
  72. DBG ( " to %04x:%04x+%x", get_cached_info->Buffer.segment,
  73. get_cached_info->Buffer.offset, get_cached_info->BufferSize );
  74. /* This is really, really awkward to support with our multiple
  75. * sources of options.
  76. */
  77. len = get_cached_info->BufferSize;
  78. if ( len == 0 ) {
  79. len = sizeof ( basemem_packet );
  80. get_cached_info->Buffer.segment = rm_ds;
  81. get_cached_info->Buffer.offset =
  82. ( unsigned int ) ( & __from_data16 ( basemem_packet ) );
  83. get_cached_info->BufferLimit = len;
  84. }
  85. /* Allocate space for temporary copy */
  86. data = malloc ( len );
  87. if ( ! data ) {
  88. DBG ( " out of memory" );
  89. goto err;
  90. }
  91. /* Construct DHCP packet */
  92. if ( get_cached_info->PacketType == PXENV_PACKET_TYPE_DHCP_DISCOVER ) {
  93. msgtype = DHCPDISCOVER;
  94. options = &dhcp_request_options;
  95. } else {
  96. msgtype = DHCPACK;
  97. options = NULL;
  98. }
  99. if ( ( rc = create_dhcp_packet ( pxe_netdev, msgtype, data, len,
  100. &dhcppkt ) ) != 0 ) {
  101. DBG ( " failed to build packet" );
  102. goto err;
  103. }
  104. if ( ( rc = copy_dhcp_packet_options ( &dhcppkt, options ) ) != 0 ) {
  105. DBG ( " failed to copy options" );
  106. goto err;
  107. }
  108. /* Overwrite filename to work around Microsoft RIS bug */
  109. if ( pxe_ris_filename ) {
  110. strncpy ( dhcppkt.dhcphdr->file, pxe_ris_filename,
  111. sizeof ( dhcppkt.dhcphdr->file ) );
  112. }
  113. /* Copy packet to client buffer */
  114. buffer = real_to_user ( get_cached_info->Buffer.segment,
  115. get_cached_info->Buffer.offset );
  116. len = dhcppkt.len;
  117. copy_to_user ( buffer, 0, data, len );
  118. get_cached_info->BufferSize = len;
  119. free ( data );
  120. get_cached_info->Status = PXENV_STATUS_SUCCESS;
  121. return PXENV_EXIT_SUCCESS;
  122. err:
  123. if ( data )
  124. free ( data );
  125. get_cached_info->Status = PXENV_STATUS_OUT_OF_RESOURCES;
  126. return PXENV_EXIT_FAILURE;
  127. }
  128. /* PXENV_RESTART_TFTP
  129. *
  130. * Status: working
  131. */
  132. PXENV_EXIT_t pxenv_restart_tftp ( struct s_PXENV_TFTP_READ_FILE
  133. *restart_tftp ) {
  134. PXENV_EXIT_t tftp_exit;
  135. DBG ( "PXENV_RESTART_TFTP " );
  136. /* Work around Microsoft RIS bug */
  137. free ( pxe_ris_filename );
  138. pxe_ris_filename = strdup ( ( char * ) restart_tftp->FileName );
  139. if ( ! pxe_ris_filename ) {
  140. restart_tftp->Status = PXENV_STATUS_OUT_OF_RESOURCES;
  141. return PXENV_EXIT_FAILURE;
  142. }
  143. /* Words cannot describe the complete mismatch between the PXE
  144. * specification and any possible version of reality...
  145. */
  146. restart_tftp->Buffer = PXE_LOAD_PHYS; /* Fixed by spec, apparently */
  147. restart_tftp->BufferSize = ( 0xa0000 - PXE_LOAD_PHYS ); /* Near enough */
  148. tftp_exit = pxenv_tftp_read_file ( restart_tftp );
  149. if ( tftp_exit != PXENV_EXIT_SUCCESS )
  150. return tftp_exit;
  151. /* Fire up the new NBP */
  152. restart_tftp->Status = pxe_start_nbp();
  153. /* Not sure what "SUCCESS" actually means, since we can only
  154. * return if the new NBP failed to boot...
  155. */
  156. return PXENV_EXIT_SUCCESS;
  157. }
  158. /* PXENV_START_UNDI
  159. *
  160. * Status: working
  161. */
  162. PXENV_EXIT_t pxenv_start_undi ( struct s_PXENV_START_UNDI *start_undi ) {
  163. unsigned int bus_type;
  164. unsigned int location;
  165. struct net_device *netdev;
  166. DBG ( "PXENV_START_UNDI %04x:%04x:%04x",
  167. start_undi->AX, start_undi->BX, start_undi->DX );
  168. /* Determine bus type and location. Use a heuristic to decide
  169. * whether we are PCI or ISAPnP
  170. */
  171. if ( ( start_undi->DX >= ISAPNP_READ_PORT_MIN ) &&
  172. ( start_undi->DX <= ISAPNP_READ_PORT_MAX ) &&
  173. ( start_undi->BX >= ISAPNP_CSN_MIN ) &&
  174. ( start_undi->BX <= ISAPNP_CSN_MAX ) ) {
  175. bus_type = BUS_TYPE_ISAPNP;
  176. location = start_undi->BX;
  177. /* Record ISAPnP read port for use by isapnp.c */
  178. isapnp_read_port = start_undi->DX;
  179. } else {
  180. bus_type = BUS_TYPE_PCI;
  181. location = start_undi->AX;
  182. }
  183. /* Probe for devices, etc. */
  184. startup();
  185. /* Look for a matching net device */
  186. netdev = find_netdev_by_location ( bus_type, location );
  187. if ( ! netdev ) {
  188. DBG ( " no net device found" );
  189. start_undi->Status = PXENV_STATUS_UNDI_CANNOT_INITIALIZE_NIC;
  190. return PXENV_EXIT_FAILURE;
  191. }
  192. DBG ( " using netdev %s", netdev->name );
  193. /* Save as PXE net device */
  194. pxe_set_netdev ( netdev );
  195. /* Hook INT 1A */
  196. pxe_hook_int1a();
  197. start_undi->Status = PXENV_STATUS_SUCCESS;
  198. return PXENV_EXIT_SUCCESS;
  199. }
  200. /* PXENV_STOP_UNDI
  201. *
  202. * Status: working
  203. */
  204. PXENV_EXIT_t pxenv_stop_undi ( struct s_PXENV_STOP_UNDI *stop_undi ) {
  205. DBG ( "PXENV_STOP_UNDI" );
  206. /* Unhook INT 1A */
  207. pxe_unhook_int1a();
  208. /* Clear PXE net device */
  209. pxe_set_netdev ( NULL );
  210. /* Prepare for unload */
  211. shutdown();
  212. stop_undi->Status = PXENV_STATUS_SUCCESS;
  213. return PXENV_EXIT_SUCCESS;
  214. }
  215. /* PXENV_START_BASE
  216. *
  217. * Status: won't implement (requires major structural changes)
  218. */
  219. PXENV_EXIT_t pxenv_start_base ( struct s_PXENV_START_BASE *start_base ) {
  220. DBG ( "PXENV_START_BASE" );
  221. start_base->Status = PXENV_STATUS_UNSUPPORTED;
  222. return PXENV_EXIT_FAILURE;
  223. }
  224. /* PXENV_STOP_BASE
  225. *
  226. * Status: working
  227. */
  228. PXENV_EXIT_t pxenv_stop_base ( struct s_PXENV_STOP_BASE *stop_base ) {
  229. DBG ( "PXENV_STOP_BASE" );
  230. /* The only time we will be called is when the NBP is trying
  231. * to shut down the PXE stack. There's nothing we need to do
  232. * in this call.
  233. */
  234. stop_base->Status = PXENV_STATUS_SUCCESS;
  235. return PXENV_EXIT_SUCCESS;
  236. }