These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / arch / i386 / interface / pxe / pxe_preboot.c
1 /** @file
2  *
3  * PXE Preboot API
4  *
5  */
6
7 /* PXE API interface for Etherboot.
8  *
9  * Copyright (C) 2004 Michael Brown <mbrown@fensystems.co.uk>.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation; either version 2 of the
14  * License, or any later version.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24  * 02110-1301, USA.
25  *
26  * You can also choose to distribute this program under the terms of
27  * the Unmodified Binary Distribution Licence (as given in the file
28  * COPYING.UBDL), provided that you have satisfied its requirements.
29  */
30
31 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
32
33 #include <stdint.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <setjmp.h>
37 #include <ipxe/uaccess.h>
38 #include <ipxe/dhcp.h>
39 #include <ipxe/fakedhcp.h>
40 #include <ipxe/device.h>
41 #include <ipxe/netdevice.h>
42 #include <ipxe/isapnp.h>
43 #include <ipxe/init.h>
44 #include <ipxe/if_ether.h>
45 #include <basemem_packet.h>
46 #include <biosint.h>
47 #include "pxe.h"
48 #include "pxe_call.h"
49
50 /* Avoid dragging in isapnp.o unnecessarily */
51 uint16_t isapnp_read_port;
52
53 /** Zero-based versions of PXENV_GET_CACHED_INFO::PacketType */
54 enum pxe_cached_info_indices {
55         CACHED_INFO_DHCPDISCOVER = ( PXENV_PACKET_TYPE_DHCP_DISCOVER - 1 ),
56         CACHED_INFO_DHCPACK = ( PXENV_PACKET_TYPE_DHCP_ACK - 1 ),
57         CACHED_INFO_BINL = ( PXENV_PACKET_TYPE_CACHED_REPLY - 1 ),
58         NUM_CACHED_INFOS
59 };
60
61 /** A cached DHCP packet */
62 union pxe_cached_info {
63         struct dhcphdr dhcphdr;
64         /* This buffer must be *exactly* the size of a BOOTPLAYER_t
65          * structure, otherwise WinPE will die horribly.  It takes the
66          * size of *our* buffer and feeds it in to us as the size of
67          * one of *its* buffers.  If our buffer is larger than it
68          * expects, we therefore end up overwriting part of its data
69          * segment, since it tells us to do so.  (D'oh!)
70          *
71          * Note that a BOOTPLAYER_t is not necessarily large enough to
72          * hold a DHCP packet; this is a flaw in the PXE spec.
73          */
74         BOOTPLAYER_t packet;
75 } __attribute__ (( packed ));
76
77 /** A PXE DHCP packet creator */
78 struct pxe_dhcp_packet_creator {
79         /** Create DHCP packet
80          *
81          * @v netdev            Network device
82          * @v data              Buffer for DHCP packet
83          * @v max_len           Size of DHCP packet buffer
84          * @ret rc              Return status code
85          */
86         int ( * create ) ( struct net_device *netdev, void *data,
87                            size_t max_len );
88 };
89
90 /** PXE DHCP packet creators */
91 static struct pxe_dhcp_packet_creator pxe_dhcp_packet_creators[] = {
92         [CACHED_INFO_DHCPDISCOVER] = { create_fakedhcpdiscover },
93         [CACHED_INFO_DHCPACK] = { create_fakedhcpack },
94         [CACHED_INFO_BINL] = { create_fakepxebsack },
95 };
96
97 /**
98  * Name PXENV_GET_CACHED_INFO packet type
99  *
100  * @v packet_type       Packet type
101  * @ret name            Name of packet type
102  */
103 static inline __attribute__ (( always_inline )) const char *
104 pxenv_get_cached_info_name ( int packet_type ) {
105         switch ( packet_type ) {
106         case PXENV_PACKET_TYPE_DHCP_DISCOVER:
107                 return "DHCPDISCOVER";
108         case PXENV_PACKET_TYPE_DHCP_ACK:
109                 return "DHCPACK";
110         case PXENV_PACKET_TYPE_CACHED_REPLY:
111                 return "BINL";
112         default:
113                 return "<INVALID>";
114         }
115 }
116
117 /* The case in which the caller doesn't supply a buffer is really
118  * awkward to support given that we have multiple sources of options,
119  * and that we don't actually store the DHCP packets.  (We may not
120  * even have performed DHCP; we may have obtained all configuration
121  * from non-volatile stored options or from the command line.)
122  *
123  * Some NBPs rely on the buffers we provide being persistent, so we
124  * can't just use the temporary packet buffer.  4.5kB of base memory
125  * always wasted just because some clients are too lazy to provide
126  * their own buffers...
127  */
128 static union pxe_cached_info __bss16_array ( cached_info, [NUM_CACHED_INFOS] );
129 #define cached_info __use_data16 ( cached_info )
130
131 /**
132  * UNLOAD BASE CODE STACK
133  *
134  * @v None                              -
135  * @ret ...
136  *
137  */
138 static PXENV_EXIT_t
139 pxenv_unload_stack ( struct s_PXENV_UNLOAD_STACK *unload_stack ) {
140         DBGC ( &pxe_netdev, "PXENV_UNLOAD_STACK\n" );
141
142         unload_stack->Status = PXENV_STATUS_SUCCESS;
143         return PXENV_EXIT_SUCCESS;
144 }
145
146 /* PXENV_GET_CACHED_INFO
147  *
148  * Status: working
149  */
150 static PXENV_EXIT_t
151 pxenv_get_cached_info ( struct s_PXENV_GET_CACHED_INFO *get_cached_info ) {
152         struct pxe_dhcp_packet_creator *creator;
153         union pxe_cached_info *info;
154         unsigned int idx;
155         size_t len;
156         userptr_t buffer;
157         int rc;
158
159         DBGC ( &pxe_netdev, "PXENV_GET_CACHED_INFO %s to %04x:%04x+%x",
160                pxenv_get_cached_info_name ( get_cached_info->PacketType ),
161                get_cached_info->Buffer.segment,
162                get_cached_info->Buffer.offset, get_cached_info->BufferSize );
163
164         /* Sanity check */
165         if ( ! pxe_netdev ) {
166                 DBGC ( &pxe_netdev, "PXENV_GET_CACHED_INFO called with no "
167                        "network device\n" );
168                 get_cached_info->Status = PXENV_STATUS_UNDI_INVALID_STATE;
169                 return PXENV_EXIT_FAILURE;
170         }
171
172         /* Sanity check */
173         idx = ( get_cached_info->PacketType - 1 );
174         if ( idx >= NUM_CACHED_INFOS ) {
175                 DBGC ( &pxe_netdev, " bad PacketType %d\n",
176                        get_cached_info->PacketType );
177                 goto err;
178         }
179         info = &cached_info[idx];
180
181         /* Construct DHCP packet */
182         creator = &pxe_dhcp_packet_creators[idx];
183         if ( ( rc = creator->create ( pxe_netdev, info,
184                                       sizeof ( *info ) ) ) != 0 ) {
185                 DBGC ( &pxe_netdev, " failed to build packet: %s\n",
186                        strerror ( rc ) );
187                 goto err;
188         }
189
190         /* Copy packet (if applicable) */
191         len = get_cached_info->BufferSize;
192         if ( len == 0 ) {
193                 /* Point client at our cached buffer.
194                  *
195                  * To add to the fun, Intel decided at some point in
196                  * the evolution of the PXE specification to add the
197                  * BufferLimit field, which we are meant to fill in
198                  * with the length of our packet buffer, so that the
199                  * caller can safely modify the boot server reply
200                  * packet stored therein.  However, this field was not
201                  * present in earlier versions of the PXE spec, and
202                  * there is at least one PXE NBP (Altiris) which
203                  * allocates only exactly enough space for this
204                  * earlier, shorter version of the structure.  If we
205                  * actually fill in the BufferLimit field, we
206                  * therefore risk trashing random areas of the
207                  * caller's memory.  If we *don't* fill it in, then
208                  * the caller is at liberty to assume that whatever
209                  * random value happened to be in that location
210                  * represents the length of the buffer we've just
211                  * passed back to it.
212                  *
213                  * Since older PXE stacks won't fill this field in
214                  * anyway, it's probably safe to assume that no
215                  * callers actually rely on it, so we choose to not
216                  * fill it in.
217                  */
218                 get_cached_info->Buffer.segment = rm_ds;
219                 get_cached_info->Buffer.offset = __from_data16 ( info );
220                 get_cached_info->BufferSize = sizeof ( *info );
221                 DBGC ( &pxe_netdev, " using %04x:%04x+%04x['%x']",
222                        get_cached_info->Buffer.segment,
223                        get_cached_info->Buffer.offset,
224                        get_cached_info->BufferSize,
225                        get_cached_info->BufferLimit );
226         } else {
227                 /* Copy packet to client buffer */
228                 if ( len > sizeof ( *info ) )
229                         len = sizeof ( *info );
230                 if ( len < sizeof ( *info ) )
231                         DBGC ( &pxe_netdev, " buffer may be too short" );
232                 buffer = real_to_user ( get_cached_info->Buffer.segment,
233                                         get_cached_info->Buffer.offset );
234                 copy_to_user ( buffer, 0, info, len );
235                 get_cached_info->BufferSize = len;
236         }
237
238         DBGC ( &pxe_netdev, "\n" );
239         get_cached_info->Status = PXENV_STATUS_SUCCESS;
240         return PXENV_EXIT_SUCCESS;
241
242  err:
243         get_cached_info->Status = PXENV_STATUS_OUT_OF_RESOURCES;
244         return PXENV_EXIT_FAILURE;
245 }
246
247 /* PXENV_RESTART_TFTP
248  *
249  * Status: working
250  */
251 static PXENV_EXIT_t
252 pxenv_restart_tftp ( struct s_PXENV_TFTP_READ_FILE *restart_tftp ) {
253         PXENV_EXIT_t tftp_exit;
254
255         DBGC ( &pxe_netdev, "PXENV_RESTART_TFTP\n" );
256
257         /* Words cannot describe the complete mismatch between the PXE
258          * specification and any possible version of reality...
259          */
260         restart_tftp->Buffer = PXE_LOAD_PHYS; /* Fixed by spec, apparently */
261         restart_tftp->BufferSize = ( 0xa0000 - PXE_LOAD_PHYS ); /* Near enough */
262         tftp_exit = pxenv_tftp_read_file ( restart_tftp );
263         if ( tftp_exit != PXENV_EXIT_SUCCESS )
264                 return tftp_exit;
265
266         /* Restart NBP */
267         rmlongjmp ( pxe_restart_nbp, PXENV_RESTART_TFTP );
268 }
269
270 /* PXENV_START_UNDI
271  *
272  * Status: working
273  */
274 static PXENV_EXIT_t pxenv_start_undi ( struct s_PXENV_START_UNDI *start_undi ) {
275         unsigned int bus_type;
276         unsigned int location;
277         struct net_device *netdev;
278
279         DBGC ( &pxe_netdev, "PXENV_START_UNDI %04x:%04x:%04x\n",
280                start_undi->AX, start_undi->BX, start_undi->DX );
281
282         /* Determine bus type and location.  Use a heuristic to decide
283          * whether we are PCI or ISAPnP
284          */
285         if ( ( start_undi->DX >= ISAPNP_READ_PORT_MIN ) &&
286              ( start_undi->DX <= ISAPNP_READ_PORT_MAX ) &&
287              ( start_undi->BX >= ISAPNP_CSN_MIN ) &&
288              ( start_undi->BX <= ISAPNP_CSN_MAX ) ) {
289                 bus_type = BUS_TYPE_ISAPNP;
290                 location = start_undi->BX;
291                 /* Record ISAPnP read port for use by isapnp.c */
292                 isapnp_read_port = start_undi->DX;
293         } else {
294                 bus_type = BUS_TYPE_PCI;
295                 location = start_undi->AX;
296         }
297
298         /* Probe for devices, etc. */
299         startup();
300
301         /* Look for a matching net device */
302         netdev = find_netdev_by_location ( bus_type, location );
303         if ( ! netdev ) {
304                 DBGC ( &pxe_netdev, "PXENV_START_UNDI could not find matching "
305                        "net device\n" );
306                 start_undi->Status = PXENV_STATUS_UNDI_CANNOT_INITIALIZE_NIC;
307                 return PXENV_EXIT_FAILURE;
308         }
309         DBGC ( &pxe_netdev, "PXENV_START_UNDI found net device %s\n",
310                netdev->name );
311
312         /* Activate PXE */
313         pxe_activate ( netdev );
314
315         start_undi->Status = PXENV_STATUS_SUCCESS;
316         return PXENV_EXIT_SUCCESS;
317 }
318
319 /* PXENV_STOP_UNDI
320  *
321  * Status: working
322  */
323 static PXENV_EXIT_t pxenv_stop_undi ( struct s_PXENV_STOP_UNDI *stop_undi ) {
324         DBGC ( &pxe_netdev, "PXENV_STOP_UNDI\n" );
325
326         /* Deactivate PXE */
327         pxe_deactivate();
328
329         /* Prepare for unload */
330         shutdown_boot();
331
332         /* Check to see if we still have any hooked interrupts */
333         if ( hooked_bios_interrupts != 0 ) {
334                 DBGC ( &pxe_netdev, "PXENV_STOP_UNDI failed: %d interrupts "
335                        "still hooked\n", hooked_bios_interrupts );
336                 stop_undi->Status = PXENV_STATUS_KEEP_UNDI;
337                 return PXENV_EXIT_FAILURE;
338         }
339
340         stop_undi->Status = PXENV_STATUS_SUCCESS;
341         return PXENV_EXIT_SUCCESS;
342 }
343
344 /* PXENV_START_BASE
345  *
346  * Status: won't implement (requires major structural changes)
347  */
348 static PXENV_EXIT_t pxenv_start_base ( struct s_PXENV_START_BASE *start_base ) {
349         DBGC ( &pxe_netdev, "PXENV_START_BASE\n" );
350
351         start_base->Status = PXENV_STATUS_UNSUPPORTED;
352         return PXENV_EXIT_FAILURE;
353 }
354
355 /* PXENV_STOP_BASE
356  *
357  * Status: working
358  */
359 static PXENV_EXIT_t pxenv_stop_base ( struct s_PXENV_STOP_BASE *stop_base ) {
360         DBGC ( &pxe_netdev, "PXENV_STOP_BASE\n" );
361
362         /* The only time we will be called is when the NBP is trying
363          * to shut down the PXE stack.  There's nothing we need to do
364          * in this call.
365          */
366
367         stop_base->Status = PXENV_STATUS_SUCCESS;
368         return PXENV_EXIT_SUCCESS;
369 }
370
371 /** PXE preboot API */
372 struct pxe_api_call pxe_preboot_api[] __pxe_api_call = {
373         PXE_API_CALL ( PXENV_UNLOAD_STACK, pxenv_unload_stack,
374                        struct s_PXENV_UNLOAD_STACK ),
375         PXE_API_CALL ( PXENV_GET_CACHED_INFO, pxenv_get_cached_info,
376                        struct s_PXENV_GET_CACHED_INFO ),
377         PXE_API_CALL ( PXENV_RESTART_TFTP, pxenv_restart_tftp,
378                        struct s_PXENV_TFTP_READ_FILE ),
379         PXE_API_CALL ( PXENV_START_UNDI, pxenv_start_undi,
380                        struct s_PXENV_START_UNDI ),
381         PXE_API_CALL ( PXENV_STOP_UNDI, pxenv_stop_undi,
382                        struct s_PXENV_STOP_UNDI ),
383         PXE_API_CALL ( PXENV_START_BASE, pxenv_start_base,
384                        struct s_PXENV_START_BASE ),
385         PXE_API_CALL ( PXENV_STOP_BASE, pxenv_stop_base,
386                        struct s_PXENV_STOP_BASE ),
387 };