These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / arch / i386 / image / pxe_image.c
1 /*
2  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 /**
27  * @file
28  *
29  * PXE image format
30  *
31  */
32
33 #include <pxe.h>
34 #include <pxe_call.h>
35 #include <ipxe/uaccess.h>
36 #include <ipxe/image.h>
37 #include <ipxe/segment.h>
38 #include <ipxe/netdevice.h>
39 #include <ipxe/features.h>
40 #include <ipxe/console.h>
41 #include <ipxe/efi/efi.h>
42 #include <ipxe/efi/IndustryStandard/PeImage.h>
43
44 FEATURE ( FEATURE_IMAGE, "PXE", DHCP_EB_FEATURE_PXE, 1 );
45
46 /** PXE command line */
47 const char *pxe_cmdline;
48
49 /**
50  * Execute PXE image
51  *
52  * @v image             PXE image
53  * @ret rc              Return status code
54  */
55 static int pxe_exec ( struct image *image ) {
56         userptr_t buffer = real_to_user ( 0, 0x7c00 );
57         struct net_device *netdev;
58         int rc;
59
60         /* Verify and prepare segment */
61         if ( ( rc = prep_segment ( buffer, image->len, image->len ) ) != 0 ) {
62                 DBGC ( image, "IMAGE %p could not prepare segment: %s\n",
63                        image, strerror ( rc ) );
64                 return rc;
65         }
66
67         /* Copy image to segment */
68         memcpy_user ( buffer, 0, image->data, 0, image->len );
69
70         /* Arbitrarily pick the most recently opened network device */
71         if ( ( netdev = last_opened_netdev() ) == NULL ) {
72                 DBGC ( image, "IMAGE %p could not locate PXE net device\n",
73                        image );
74                 return -ENODEV;
75         }
76         netdev_get ( netdev );
77
78         /* Activate PXE */
79         pxe_activate ( netdev );
80
81         /* Set PXE command line */
82         pxe_cmdline = image->cmdline;
83
84         /* Reset console since PXE NBP will probably use it */
85         console_reset();
86
87         /* Start PXE NBP */
88         rc = pxe_start_nbp();
89
90         /* Clear PXE command line */
91         pxe_cmdline = NULL;
92
93         /* Deactivate PXE */
94         pxe_deactivate();
95
96         /* Try to reopen network device.  Ignore errors, since the NBP
97          * may have called PXENV_STOP_UNDI.
98          */
99         netdev_open ( netdev );
100         netdev_put ( netdev );
101
102         return rc;
103 }
104
105 /**
106  * Probe PXE image
107  *
108  * @v image             PXE file
109  * @ret rc              Return status code
110  */
111 int pxe_probe ( struct image *image ) {
112
113         /* Images too large to fit in base memory cannot be PXE
114          * images.  We include this check to help prevent unrecognised
115          * images from being marked as PXE images, since PXE images
116          * have no signature we can check against.
117          */
118         if ( image->len > ( 0xa0000 - 0x7c00 ) )
119                 return -ENOEXEC;
120
121         /* Rejecting zero-length images is also useful, since these
122          * end up looking to the user like bugs in iPXE.
123          */
124         if ( ! image->len )
125                 return -ENOEXEC;
126
127         return 0;
128 }
129
130 /**
131  * Probe PXE image (with rejection of potential EFI images)
132  *
133  * @v image             PXE file
134  * @ret rc              Return status code
135  */
136 int pxe_probe_no_mz ( struct image *image ) {
137         uint16_t magic;
138         int rc;
139
140         /* Probe PXE image */
141         if ( ( rc = pxe_probe ( image ) ) != 0 )
142                 return rc;
143
144         /* Reject image with an "MZ" signature which may indicate an
145          * EFI image incorrectly handed out to a BIOS system.
146          */
147         if ( image->len >= sizeof ( magic ) ) {
148                 copy_from_user ( &magic, image->data, 0, sizeof ( magic ) );
149                 if ( magic == cpu_to_le16 ( EFI_IMAGE_DOS_SIGNATURE ) ) {
150                         DBGC ( image, "IMAGE %p may be an EFI image\n",
151                                image );
152                         return -ENOTTY;
153                 }
154         }
155
156         return 0;
157 }
158
159 /** PXE image type */
160 struct image_type pxe_image_type[] __image_type ( PROBE_PXE ) = {
161         {
162                 .name = "PXE-NBP",
163                 .probe = pxe_probe_no_mz,
164                 .exec = pxe_exec,
165         },
166         {
167                 .name = "PXE-NBP (may be EFI?)",
168                 .probe = pxe_probe,
169                 .exec = pxe_exec,
170         },
171 };