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