1 /*
2  * Kernel image loading.
3  *
4  * Copyright (C) 2011 Citrix Systems, Inc.
5  */
6 #include <xen/errno.h>
7 #include <xen/init.h>
8 #include <xen/lib.h>
9 #include <xen/mm.h>
10 #include <xen/domain_page.h>
11 #include <xen/sched.h>
12 #include <asm/byteorder.h>
13 #include <asm/setup.h>
14 #include <xen/libfdt/libfdt.h>
15 #include <xen/gunzip.h>
16 #include <xen/vmap.h>
17 
18 #include "kernel.h"
19 
20 #define UIMAGE_MAGIC          0x27051956
21 #define UIMAGE_NMLEN          32
22 
23 #define ZIMAGE32_MAGIC_OFFSET 0x24
24 #define ZIMAGE32_START_OFFSET 0x28
25 #define ZIMAGE32_END_OFFSET   0x2c
26 #define ZIMAGE32_HEADER_LEN   0x30
27 
28 #define ZIMAGE32_MAGIC 0x016f2818
29 
30 #define ZIMAGE64_MAGIC_V0 0x14000008
31 #define ZIMAGE64_MAGIC_V1 0x644d5241 /* "ARM\x64" */
32 
33 struct minimal_dtb_header {
34     uint32_t magic;
35     uint32_t total_size;
36     /* There are other fields but we don't use them yet. */
37 };
38 
39 #define DTB_MAGIC 0xd00dfeed
40 
41 /**
42  * copy_from_paddr - copy data from a physical address
43  * @dst: destination virtual address
44  * @paddr: source physical address
45  * @len: length to copy
46  */
copy_from_paddr(void * dst,paddr_t paddr,unsigned long len)47 void copy_from_paddr(void *dst, paddr_t paddr, unsigned long len)
48 {
49     void *src = (void *)FIXMAP_ADDR(FIXMAP_MISC);
50 
51     while (len) {
52         unsigned long l, s;
53 
54         s = paddr & (PAGE_SIZE-1);
55         l = min(PAGE_SIZE - s, len);
56 
57         set_fixmap(FIXMAP_MISC, maddr_to_mfn(paddr), PAGE_HYPERVISOR_WC);
58         memcpy(dst, src + s, l);
59         clean_dcache_va_range(dst, l);
60 
61         paddr += l;
62         dst += l;
63         len -= l;
64     }
65 
66     clear_fixmap(FIXMAP_MISC);
67 }
68 
place_modules(struct kernel_info * info,paddr_t kernbase,paddr_t kernend)69 static void place_modules(struct kernel_info *info,
70                           paddr_t kernbase, paddr_t kernend)
71 {
72     /* Align DTB and initrd size to 2Mb. Linux only requires 4 byte alignment */
73     const struct bootmodule *mod = info->initrd_bootmodule;
74     const paddr_t initrd_len = ROUNDUP(mod ? mod->size : 0, MB(2));
75     const paddr_t dtb_len = ROUNDUP(fdt_totalsize(info->fdt), MB(2));
76     const paddr_t modsize = initrd_len + dtb_len;
77 
78     /* Convenient */
79     const paddr_t rambase = info->mem.bank[0].start;
80     const paddr_t ramsize = info->mem.bank[0].size;
81     const paddr_t ramend = rambase + ramsize;
82     const paddr_t kernsize = ROUNDUP(kernend, MB(2)) - kernbase;
83     const paddr_t ram128mb = rambase + MB(128);
84 
85     paddr_t modbase;
86 
87     if ( modsize + kernsize > ramsize )
88         panic("Not enough memory in the first bank for the kernel+dtb+initrd");
89 
90     /*
91      * DTB must be loaded such that it does not conflict with the
92      * kernel decompressor. For 32-bit Linux Documentation/arm/Booting
93      * recommends just after the 128MB boundary while for 64-bit Linux
94      * the recommendation in Documentation/arm64/booting.txt is below
95      * 512MB.
96      *
97      * If the bootloader provides an initrd, it will be loaded just
98      * after the DTB.
99      *
100      * We try to place dtb+initrd at 128MB or if we have less RAM
101      * as high as possible. If there is no space then fallback to
102      * just before the kernel.
103      *
104      * If changing this then consider
105      * tools/libxc/xc_dom_arm.c:arch_setup_meminit as well.
106      */
107     if ( ramend >= ram128mb + modsize && kernend < ram128mb )
108         modbase = ram128mb;
109     else if ( ramend - modsize > ROUNDUP(kernend, MB(2)) )
110         modbase = ramend - modsize;
111     else if ( kernbase - rambase > modsize )
112         modbase = kernbase - modsize;
113     else
114     {
115         panic("Unable to find suitable location for dtb+initrd");
116         return;
117     }
118 
119     info->dtb_paddr = modbase;
120     info->initrd_paddr = info->dtb_paddr + dtb_len;
121 }
122 
kernel_zimage_place(struct kernel_info * info)123 static paddr_t kernel_zimage_place(struct kernel_info *info)
124 {
125     paddr_t load_addr;
126 
127 #ifdef CONFIG_ARM_64
128     if ( info->type == DOMAIN_64BIT )
129         return info->mem.bank[0].start + info->zimage.text_offset;
130 #endif
131 
132     /*
133      * If start is zero, the zImage is position independent, in this
134      * case Documentation/arm/Booting recommends loading below 128MiB
135      * and above 32MiB. Load it as high as possible within these
136      * constraints, while also avoiding the DTB.
137      */
138     if ( info->zimage.start == 0 )
139     {
140         paddr_t load_end;
141 
142         load_end = info->mem.bank[0].start + info->mem.bank[0].size;
143         load_end = MIN(info->mem.bank[0].start + MB(128), load_end);
144 
145         load_addr = load_end - info->zimage.len;
146         /* Align to 2MB */
147         load_addr &= ~((2 << 20) - 1);
148     }
149     else
150         load_addr = info->zimage.start;
151 
152     return load_addr;
153 }
154 
kernel_zimage_load(struct kernel_info * info)155 static void kernel_zimage_load(struct kernel_info *info)
156 {
157     paddr_t load_addr = kernel_zimage_place(info);
158     paddr_t paddr = info->zimage.kernel_addr;
159     paddr_t len = info->zimage.len;
160     unsigned long offs;
161 
162     info->entry = load_addr;
163 
164     place_modules(info, load_addr, load_addr + len);
165 
166     printk("Loading zImage from %"PRIpaddr" to %"PRIpaddr"-%"PRIpaddr"\n",
167            paddr, load_addr, load_addr + len);
168     for ( offs = 0; offs < len; )
169     {
170         uint64_t par;
171         paddr_t s, l, ma = 0;
172         void *dst;
173 
174         s = offs & ~PAGE_MASK;
175         l = min(PAGE_SIZE - s, len);
176 
177         par = gvirt_to_maddr(load_addr + offs, &ma, GV2M_WRITE);
178         if ( par )
179         {
180             panic("Unable to map translate guest address");
181             return;
182         }
183 
184         dst = map_domain_page(maddr_to_mfn(ma));
185 
186         copy_from_paddr(dst + s, paddr + offs, l);
187 
188         unmap_domain_page(dst);
189         offs += l;
190     }
191 }
192 
193 /*
194  * Uimage CPU Architecture Codes
195  */
196 #define IH_ARCH_ARM             2       /* ARM          */
197 #define IH_ARCH_ARM64           22      /* ARM64        */
198 
199 /*
200  * Check if the image is a uImage and setup kernel_info
201  */
kernel_uimage_probe(struct kernel_info * info,paddr_t addr,paddr_t size)202 static int kernel_uimage_probe(struct kernel_info *info,
203                                  paddr_t addr, paddr_t size)
204 {
205     struct {
206         __be32 magic;   /* Image Header Magic Number */
207         __be32 hcrc;    /* Image Header CRC Checksum */
208         __be32 time;    /* Image Creation Timestamp  */
209         __be32 size;    /* Image Data Size           */
210         __be32 load;    /* Data Load Address         */
211         __be32 ep;      /* Entry Point Address       */
212         __be32 dcrc;    /* Image Data CRC Checksum   */
213         uint8_t os;     /* Operating System          */
214         uint8_t arch;   /* CPU architecture          */
215         uint8_t type;   /* Image Type                */
216         uint8_t comp;   /* Compression Type          */
217         uint8_t name[UIMAGE_NMLEN]; /* Image Name  */
218     } uimage;
219 
220     uint32_t len;
221 
222     if ( size < sizeof(uimage) )
223         return -EINVAL;
224 
225     copy_from_paddr(&uimage, addr, sizeof(uimage));
226 
227     if ( be32_to_cpu(uimage.magic) != UIMAGE_MAGIC )
228         return -EINVAL;
229 
230     len = be32_to_cpu(uimage.size);
231 
232     if ( len > size - sizeof(uimage) )
233         return -EINVAL;
234 
235     info->zimage.kernel_addr = addr + sizeof(uimage);
236     info->zimage.len = len;
237 
238     info->entry = info->zimage.start;
239     info->load = kernel_zimage_load;
240 
241 #ifdef CONFIG_ARM_64
242     switch ( uimage.arch )
243     {
244     case IH_ARCH_ARM:
245         info->type = DOMAIN_32BIT;
246         break;
247     case IH_ARCH_ARM64:
248         info->type = DOMAIN_64BIT;
249         break;
250     default:
251         printk(XENLOG_ERR "Unsupported uImage arch type %d\n", uimage.arch);
252         return -EINVAL;
253     }
254 #endif
255 
256     return 0;
257 }
258 
output_length(char * image,unsigned long image_len)259 static __init uint32_t output_length(char *image, unsigned long image_len)
260 {
261     return *(uint32_t *)&image[image_len - 4];
262 }
263 
kernel_decompress(struct bootmodule * mod)264 static __init int kernel_decompress(struct bootmodule *mod)
265 {
266     char *output, *input;
267     char magic[2];
268     int rc;
269     unsigned kernel_order_out;
270     paddr_t output_size;
271     struct page_info *pages;
272     mfn_t mfn;
273     int i;
274     paddr_t addr = mod->start;
275     paddr_t size = mod->size;
276 
277     if ( size < 2 )
278         return -EINVAL;
279 
280     copy_from_paddr(magic, addr, sizeof(magic));
281 
282     /* only gzip is supported */
283     if ( !gzip_check(magic, size) )
284         return -EINVAL;
285 
286     input = ioremap_cache(addr, size);
287     if ( input == NULL )
288         return -EFAULT;
289 
290     output_size = output_length(input, size);
291     kernel_order_out = get_order_from_bytes(output_size);
292     pages = alloc_domheap_pages(NULL, kernel_order_out, 0);
293     if ( pages == NULL )
294     {
295         iounmap(input);
296         return -ENOMEM;
297     }
298     mfn = _mfn(page_to_mfn(pages));
299     output = __vmap(&mfn, 1 << kernel_order_out, 1, 1, PAGE_HYPERVISOR, VMAP_DEFAULT);
300 
301     rc = perform_gunzip(output, input, size);
302     clean_dcache_va_range(output, output_size);
303     iounmap(input);
304     vunmap(output);
305 
306     mod->start = page_to_maddr(pages);
307     mod->size = output_size;
308 
309     /*
310      * Need to free pages after output_size here because they won't be
311      * freed by discard_initial_modules
312      */
313     i = PFN_UP(output_size);
314     for ( ; i < (1 << kernel_order_out); i++ )
315         free_domheap_page(pages + i);
316 
317     /*
318      * Free the original kernel, update the pointers to the
319      * decompressed kernel
320      */
321     dt_unreserved_regions(addr, addr + size, init_domheap_pages, 0);
322 
323     return 0;
324 }
325 
326 #ifdef CONFIG_ARM_64
327 /*
328  * Check if the image is a 64-bit Image.
329  */
kernel_zimage64_probe(struct kernel_info * info,paddr_t addr,paddr_t size)330 static int kernel_zimage64_probe(struct kernel_info *info,
331                                  paddr_t addr, paddr_t size)
332 {
333     /* linux/Documentation/arm64/booting.txt */
334     struct {
335         uint32_t magic0;
336         uint32_t res0;
337         uint64_t text_offset;  /* Image load offset */
338         uint64_t res1;
339         uint64_t res2;
340         /* zImage V1 only from here */
341         uint64_t res3;
342         uint64_t res4;
343         uint64_t res5;
344         uint32_t magic1;
345         uint32_t res6;
346     } zimage;
347     uint64_t start, end;
348 
349     if ( size < sizeof(zimage) )
350         return -EINVAL;
351 
352     copy_from_paddr(&zimage, addr, sizeof(zimage));
353 
354     if ( zimage.magic0 != ZIMAGE64_MAGIC_V0 &&
355          zimage.magic1 != ZIMAGE64_MAGIC_V1 )
356         return -EINVAL;
357 
358     /* Currently there is no length in the header, so just use the size */
359     start = 0;
360     end = size;
361 
362     /*
363      * Given the above this check is a bit pointless, but leave it
364      * here in case someone adds a length field in the future.
365      */
366     if ( (end - start) > size )
367         return -EINVAL;
368 
369     info->zimage.kernel_addr = addr;
370     info->zimage.len = end - start;
371     info->zimage.text_offset = zimage.text_offset;
372 
373     info->load = kernel_zimage_load;
374 
375     info->type = DOMAIN_64BIT;
376 
377     return 0;
378 }
379 #endif
380 
381 /*
382  * Check if the image is a 32-bit zImage and setup kernel_info
383  */
kernel_zimage32_probe(struct kernel_info * info,paddr_t addr,paddr_t size)384 static int kernel_zimage32_probe(struct kernel_info *info,
385                                  paddr_t addr, paddr_t size)
386 {
387     uint32_t zimage[ZIMAGE32_HEADER_LEN/4];
388     uint32_t start, end;
389     struct minimal_dtb_header dtb_hdr;
390 
391     if ( size < ZIMAGE32_HEADER_LEN )
392         return -EINVAL;
393 
394     copy_from_paddr(zimage, addr, sizeof(zimage));
395 
396     if (zimage[ZIMAGE32_MAGIC_OFFSET/4] != ZIMAGE32_MAGIC)
397         return -EINVAL;
398 
399     start = zimage[ZIMAGE32_START_OFFSET/4];
400     end = zimage[ZIMAGE32_END_OFFSET/4];
401 
402     if ( (end - start) > size )
403         return -EINVAL;
404 
405     /*
406      * Check for an appended DTB.
407      */
408     if ( addr + end - start + sizeof(dtb_hdr) <= size )
409     {
410         copy_from_paddr(&dtb_hdr, addr + end - start, sizeof(dtb_hdr));
411         if (be32_to_cpu(dtb_hdr.magic) == DTB_MAGIC) {
412             end += be32_to_cpu(dtb_hdr.total_size);
413 
414             if ( end > addr + size )
415                 return -EINVAL;
416         }
417     }
418 
419     info->zimage.kernel_addr = addr;
420 
421     info->zimage.start = start;
422     info->zimage.len = end - start;
423 
424     info->load = kernel_zimage_load;
425 
426 #ifdef CONFIG_ARM_64
427     info->type = DOMAIN_32BIT;
428 #endif
429 
430     return 0;
431 }
432 
kernel_elf_load(struct kernel_info * info)433 static void kernel_elf_load(struct kernel_info *info)
434 {
435     /*
436      * TODO: can the ELF header be used to find the physical address
437      * to load the image to?  Instead of assuming virt == phys.
438      */
439     info->entry = info->elf.parms.virt_entry;
440 
441     place_modules(info,
442                   info->elf.parms.virt_kstart,
443                   info->elf.parms.virt_kend);
444 
445     printk("Loading ELF image into guest memory\n");
446     info->elf.elf.dest_base = (void*)(unsigned long)info->elf.parms.virt_kstart;
447     info->elf.elf.dest_size =
448          info->elf.parms.virt_kend - info->elf.parms.virt_kstart;
449 
450     elf_load_binary(&info->elf.elf);
451 
452     printk("Free temporary kernel buffer\n");
453     free_xenheap_pages(info->elf.kernel_img, info->elf.kernel_order);
454 }
455 
kernel_elf_probe(struct kernel_info * info,paddr_t addr,paddr_t size)456 static int kernel_elf_probe(struct kernel_info *info,
457                             paddr_t addr, paddr_t size)
458 {
459     int rc;
460 
461     memset(&info->elf.elf, 0, sizeof(info->elf.elf));
462 
463     info->elf.kernel_order = get_order_from_bytes(size);
464     info->elf.kernel_img = alloc_xenheap_pages(info->elf.kernel_order, 0);
465     if ( info->elf.kernel_img == NULL )
466         panic("Cannot allocate temporary buffer for kernel");
467 
468     copy_from_paddr(info->elf.kernel_img, addr, size);
469 
470     if ( (rc = elf_init(&info->elf.elf, info->elf.kernel_img, size )) != 0 )
471         goto err;
472 #ifdef CONFIG_VERBOSE_DEBUG
473     elf_set_verbose(&info->elf.elf);
474 #endif
475     elf_parse_binary(&info->elf.elf);
476     if ( (rc = elf_xen_parse(&info->elf.elf, &info->elf.parms)) != 0 )
477         goto err;
478 
479 #ifdef CONFIG_ARM_64
480     if ( elf_32bit(&info->elf.elf) )
481         info->type = DOMAIN_32BIT;
482     else if ( elf_64bit(&info->elf.elf) )
483         info->type = DOMAIN_64BIT;
484     else
485     {
486         printk("Unknown ELF class\n");
487         rc = -EINVAL;
488         goto err;
489     }
490 #endif
491 
492     info->load = kernel_elf_load;
493 
494     if ( elf_check_broken(&info->elf.elf) )
495         printk("Xen: warning: ELF kernel broken: %s\n",
496                elf_check_broken(&info->elf.elf));
497 
498     return 0;
499 err:
500     if ( elf_check_broken(&info->elf.elf) )
501         printk("Xen: ELF kernel broken: %s\n",
502                elf_check_broken(&info->elf.elf));
503 
504     free_xenheap_pages(info->elf.kernel_img, info->elf.kernel_order);
505     return rc;
506 }
507 
kernel_probe(struct kernel_info * info)508 int kernel_probe(struct kernel_info *info)
509 {
510     struct bootmodule *mod = boot_module_find_by_kind(BOOTMOD_KERNEL);
511     int rc;
512 
513     if ( !mod || !mod->size )
514     {
515         printk(XENLOG_ERR "Missing kernel boot module?\n");
516         return -ENOENT;
517     }
518 
519     info->kernel_bootmodule = mod;
520 
521     printk("Loading kernel from boot module @ %"PRIpaddr"\n", mod->start);
522 
523     info->initrd_bootmodule = boot_module_find_by_kind(BOOTMOD_RAMDISK);
524     if ( info->initrd_bootmodule )
525         printk("Loading ramdisk from boot module @ %"PRIpaddr"\n",
526                info->initrd_bootmodule->start);
527 
528     /* if it is a gzip'ed image, 32bit or 64bit, uncompress it */
529     rc = kernel_decompress(mod);
530     if (rc < 0 && rc != -EINVAL)
531         return rc;
532 
533 #ifdef CONFIG_ARM_64
534     rc = kernel_zimage64_probe(info, mod->start, mod->size);
535     if (rc < 0)
536 #endif
537         rc = kernel_uimage_probe(info, mod->start, mod->size);
538     if (rc < 0)
539         rc = kernel_zimage32_probe(info, mod->start, mod->size);
540     if (rc < 0)
541         rc = kernel_elf_probe(info, mod->start, mod->size);
542 
543     return rc;
544 }
545 
kernel_load(struct kernel_info * info)546 void kernel_load(struct kernel_info *info)
547 {
548     info->load(info);
549 }
550 
551 /*
552  * Local variables:
553  * mode: C
554  * c-file-style: "BSD"
555  * c-basic-offset: 4
556  * indent-tabs-mode: nil
557  * End:
558  */
559