1 /*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation;
5 * version 2.1 of the License.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; If not, see <http://www.gnu.org/licenses/>.
14 *
15 * Some of the field descriptions were copied from "The Multiboot
16 * Specification", Copyright 1995, 96 Bryan Ford <baford@cs.utah.edu>,
17 * Erich Stefan Boleyn <erich@uruk.org> Copyright 1999, 2000, 2001, 2002
18 * Free Software Foundation, Inc.
19 */
20
21 /******************************************************************************
22 *
23 * Loads simple binary images. It's like a .COM file in MS-DOS. No headers are
24 * present. The only requirement is that it must have a xen_bin_image table
25 * somewhere in the first 8192 bytes, starting on a 32-bit aligned address.
26 * Those familiar with the multiboot specification should recognize this, it's
27 * (almost) the same as the multiboot header.
28 * The layout of the xen_bin_image table is:
29 *
30 * Offset Type Name Note
31 * 0 uint32_t magic required
32 * 4 uint32_t flags required
33 * 8 uint32_t checksum required
34 * 12 uint32_t header_addr required
35 * 16 uint32_t load_addr required
36 * 20 uint32_t load_end_addr required
37 * 24 uint32_t bss_end_addr required
38 * 28 uint32_t entry_addr required
39 *
40 * - magic
41 * Magic number identifying the table. For images to be loaded by Xen 3, the
42 * magic value is 0x336ec578 ("xEn3" with the 0x80 bit of the "E" set).
43 * - flags
44 * bit 0: indicates whether the image needs to be loaded on a page boundary
45 * bit 1: reserved, must be 0 (the multiboot spec uses this bit to indicate
46 * that memory info should be passed to the image)
47 * bit 2: reserved, must be 0 (the multiboot spec uses this bit to indicate
48 * that the bootloader should pass video mode info to the image)
49 * bit 16: reserved, must be 1 (the multiboot spec uses this bit to indicate
50 * that the values in the fields header_addr - entry_addr are
51 * valid)
52 * All other bits should be set to 0.
53 * - checksum
54 * When added to "magic" and "flags", the resulting value should be 0.
55 * - header_addr
56 * Contains the virtual address corresponding to the beginning of the
57 * table - the memory location at which the magic value is supposed to be
58 * loaded. This field serves to synchronize the mapping between OS image
59 * offsets and virtual memory addresses.
60 * - load_addr
61 * Contains the virtual address of the beginning of the text segment. The
62 * offset in the OS image file at which to start loading is defined by the
63 * offset at which the table was found, minus (header addr - load addr).
64 * load addr must be less than or equal to header addr.
65 * - load_end_addr
66 * Contains the virtual address of the end of the data segment.
67 * (load_end_addr - load_addr) specifies how much data to load. This implies
68 * that the text and data segments must be consecutive in the OS image. If
69 * this field is zero, the domain builder assumes that the text and data
70 * segments occupy the whole OS image file.
71 * - bss_end_addr
72 * Contains the virtual address of the end of the bss segment. The domain
73 * builder initializes this area to zero, and reserves the memory it occupies
74 * to avoid placing boot modules and other data relevant to the loaded image
75 * in that area. If this field is zero, the domain builder assumes that no bss
76 * segment is present.
77 * - entry_addr
78 * The virtual address at which to start execution of the loaded image.
79 *
80 */
81
82 #include <stdlib.h>
83 #include <inttypes.h>
84
85 #include "xg_private.h"
86
87 #define round_pgup(_p) (((_p)+(PAGE_SIZE_X86-1))&PAGE_MASK_X86)
88 #define round_pgdown(_p) ((_p)&PAGE_MASK_X86)
89
90 struct xen_bin_image_table
91 {
92 uint32_t magic;
93 uint32_t flags;
94 uint32_t checksum;
95 uint32_t header_addr;
96 uint32_t load_addr;
97 uint32_t load_end_addr;
98 uint32_t bss_end_addr;
99 uint32_t entry_addr;
100 };
101
102 #define XEN_MULTIBOOT_MAGIC3 0x336ec578
103
104 #define XEN_MULTIBOOT_FLAG_ALIGN4K 0x00000001
105 #define XEN_MULTIBOOT_FLAG_NEEDMEMINFO 0x00000002
106 #define XEN_MULTIBOOT_FLAG_NEEDVIDINFO 0x00000004
107 #define XEN_MULTIBOOT_FLAG_ADDRSVALID 0x00010000
108 #define XEN_MULTIBOOT_FLAG_PAE_SHIFT 14
109 #define XEN_MULTIBOOT_FLAG_PAE_MASK (3 << XEN_MULTIBOOT_FLAG_PAE_SHIFT)
110
111 /* Flags we test for */
112 #define FLAGS_MASK ((~ 0) & (~ XEN_MULTIBOOT_FLAG_ALIGN4K) & \
113 (~ XEN_MULTIBOOT_FLAG_PAE_MASK))
114 #define FLAGS_REQUIRED XEN_MULTIBOOT_FLAG_ADDRSVALID
115
116 /* --------------------------------------------------------------------- */
117
find_table(struct xc_dom_image * dom)118 static struct xen_bin_image_table *find_table(struct xc_dom_image *dom)
119 {
120 struct xen_bin_image_table *table;
121 uint32_t *probe_ptr;
122 uint32_t *probe_end;
123
124 if ( dom->kernel_size < sizeof(*table) )
125 return NULL;
126 probe_ptr = dom->kernel_blob;
127 if ( dom->kernel_size > (8192 + sizeof(*table)) )
128 probe_end = dom->kernel_blob + 8192;
129 else
130 probe_end = dom->kernel_blob + dom->kernel_size - sizeof(*table);
131
132 for ( table = NULL; probe_ptr < probe_end; probe_ptr++ )
133 {
134 if ( *probe_ptr == XEN_MULTIBOOT_MAGIC3 )
135 {
136 table = (struct xen_bin_image_table *) probe_ptr;
137 /* Checksum correct? */
138 if ( (table->magic + table->flags + table->checksum) == 0 )
139 return table;
140 }
141 }
142 return NULL;
143 }
144
xc_dom_probe_bin_kernel(struct xc_dom_image * dom)145 static int xc_dom_probe_bin_kernel(struct xc_dom_image *dom)
146 {
147 return find_table(dom) ? 0 : -EINVAL;
148 }
149
xc_dom_parse_bin_kernel(struct xc_dom_image * dom)150 static int xc_dom_parse_bin_kernel(struct xc_dom_image *dom)
151 {
152 struct xen_bin_image_table *image_info;
153 char *image = dom->kernel_blob;
154 size_t image_size = dom->kernel_size;
155 uint32_t start_addr;
156 uint32_t load_end_addr;
157 uint32_t bss_end_addr;
158 uint32_t pae_flags;
159
160 image_info = find_table(dom);
161 if ( !image_info )
162 return -EINVAL;
163
164 DOMPRINTF("%s: multiboot header fields", __FUNCTION__);
165 DOMPRINTF(" flags: 0x%" PRIx32 "", image_info->flags);
166 DOMPRINTF(" header_addr: 0x%" PRIx32 "", image_info->header_addr);
167 DOMPRINTF(" load_addr: 0x%" PRIx32 "", image_info->load_addr);
168 DOMPRINTF(" load_end_addr: 0x%" PRIx32 "", image_info->load_end_addr);
169 DOMPRINTF(" bss_end_addr: 0x%" PRIx32 "", image_info->bss_end_addr);
170 DOMPRINTF(" entry_addr: 0x%" PRIx32 "", image_info->entry_addr);
171
172 /* Check the flags */
173 if ( (image_info->flags & FLAGS_MASK) != FLAGS_REQUIRED )
174 {
175 xc_dom_panic(dom->xch, XC_INVALID_KERNEL,
176 "%s: xen_bin_image_table flags required "
177 "0x%08" PRIx32 " found 0x%08" PRIx32 "",
178 __FUNCTION__, FLAGS_REQUIRED, image_info->flags & FLAGS_MASK);
179 return -EINVAL;
180 }
181
182 /* Sanity check on the addresses */
183 if ( (image_info->header_addr < image_info->load_addr) ||
184 ((char *) image_info - image) <
185 (image_info->header_addr - image_info->load_addr) )
186 {
187 xc_dom_panic(dom->xch, XC_INVALID_KERNEL, "%s: Invalid header_addr.",
188 __FUNCTION__);
189 return -EINVAL;
190 }
191
192 start_addr = image_info->header_addr - ((char *)image_info - image);
193 load_end_addr = image_info->load_end_addr ?: start_addr + image_size;
194 bss_end_addr = image_info->bss_end_addr ?: load_end_addr;
195
196 DOMPRINTF("%s: calculated addresses", __FUNCTION__);
197 DOMPRINTF(" start_addr: 0x%" PRIx32 "", start_addr);
198 DOMPRINTF(" load_end_addr: 0x%" PRIx32 "", load_end_addr);
199 DOMPRINTF(" bss_end_addr: 0x%" PRIx32 "", bss_end_addr);
200
201 if ( (start_addr + image_size) < load_end_addr )
202 {
203 xc_dom_panic(dom->xch, XC_INVALID_KERNEL, "%s: Invalid load_end_addr.",
204 __FUNCTION__);
205 return -EINVAL;
206 }
207
208 if ( bss_end_addr < load_end_addr)
209 {
210 xc_dom_panic(dom->xch, XC_INVALID_KERNEL, "%s: Invalid bss_end_addr.",
211 __FUNCTION__);
212 return -EINVAL;
213 }
214
215 dom->kernel_seg.vstart = image_info->load_addr;
216 dom->kernel_seg.vend = bss_end_addr;
217 dom->parms->virt_base = start_addr;
218 dom->parms->virt_entry = image_info->entry_addr;
219
220 pae_flags = image_info->flags & XEN_MULTIBOOT_FLAG_PAE_MASK;
221 switch (pae_flags >> XEN_MULTIBOOT_FLAG_PAE_SHIFT) {
222 case 0:
223 dom->guest_type = "xen-3.0-x86_32";
224 break;
225 case 1:
226 dom->guest_type = "xen-3.0-x86_32p";
227 break;
228 case 2:
229 dom->guest_type = "xen-3.0-x86_64";
230 break;
231 case 3:
232 /* Kernel detects PAE at runtime. So try to figure whenever
233 * xen supports PAE and advertise a PAE-capable kernel in case
234 * it does. */
235 dom->guest_type = "xen-3.0-x86_32";
236 if ( strstr(dom->xen_caps, "xen-3.0-x86_32p") )
237 {
238 DOMPRINTF("%s: PAE fixup", __FUNCTION__);
239 dom->guest_type = "xen-3.0-x86_32p";
240 dom->parms->pae = XEN_PAE_EXTCR3;
241 }
242 break;
243 }
244 return 0;
245 }
246
xc_dom_load_bin_kernel(struct xc_dom_image * dom)247 static int xc_dom_load_bin_kernel(struct xc_dom_image *dom)
248 {
249 struct xen_bin_image_table *image_info;
250 char *image = dom->kernel_blob;
251 char *dest;
252 size_t image_size = dom->kernel_size;
253 size_t dest_size;
254 uint32_t start_addr;
255 uint32_t load_end_addr;
256 uint32_t bss_end_addr;
257 uint32_t skip, text_size, bss_size;
258
259 image_info = find_table(dom);
260 if ( !image_info )
261 return -EINVAL;
262
263 start_addr = image_info->header_addr - ((char *)image_info - image);
264 load_end_addr = image_info->load_end_addr ?: start_addr + image_size;
265 bss_end_addr = image_info->bss_end_addr ?: load_end_addr;
266
267 /* It's possible that we need to skip the first part of the image */
268 skip = image_info->load_addr - start_addr;
269 text_size = load_end_addr - image_info->load_addr;
270 bss_size = bss_end_addr - load_end_addr;
271
272 DOMPRINTF("%s: calculated sizes", __FUNCTION__);
273 DOMPRINTF(" skip: 0x%" PRIx32 "", skip);
274 DOMPRINTF(" text_size: 0x%" PRIx32 "", text_size);
275 DOMPRINTF(" bss_size: 0x%" PRIx32 "", bss_size);
276
277 dest = xc_dom_vaddr_to_ptr(dom, dom->kernel_seg.vstart, &dest_size);
278 if ( dest == NULL )
279 {
280 DOMPRINTF("%s: xc_dom_vaddr_to_ptr(dom, dom->kernel_seg.vstart)"
281 " => NULL", __FUNCTION__);
282 return -EINVAL;
283 }
284
285 if ( dest_size < text_size ||
286 dest_size - text_size < bss_size )
287 {
288 DOMPRINTF("%s: mapped region is too small for image", __FUNCTION__);
289 return -EINVAL;
290 }
291
292 if ( image_size < skip ||
293 image_size - skip < text_size )
294 {
295 DOMPRINTF("%s: image is too small for declared text size",
296 __FUNCTION__);
297 return -EINVAL;
298 }
299
300 memcpy(dest, image + skip, text_size);
301 memset(dest + text_size, 0, bss_size);
302
303 return 0;
304 }
305
306 /* ------------------------------------------------------------------------ */
307
308 static struct xc_dom_loader bin_loader = {
309 .name = "multiboot-binary",
310 .probe = xc_dom_probe_bin_kernel,
311 .parser = xc_dom_parse_bin_kernel,
312 .loader = xc_dom_load_bin_kernel,
313 };
314
register_loader(void)315 static void __init register_loader(void)
316 {
317 xc_dom_register_loader(&bin_loader);
318 }
319
320 /*
321 * Local variables:
322 * mode: C
323 * c-file-style: "BSD"
324 * c-basic-offset: 4
325 * tab-width: 4
326 * indent-tabs-mode: nil
327 * End:
328 */
329