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 <asm/guest_access.h>
19 #include <asm/kernel.h>
20 
21 #define UIMAGE_MAGIC          0x27051956
22 #define UIMAGE_NMLEN          32
23 
24 #define ZIMAGE32_MAGIC_OFFSET 0x24
25 #define ZIMAGE32_START_OFFSET 0x28
26 #define ZIMAGE32_END_OFFSET   0x2c
27 #define ZIMAGE32_HEADER_LEN   0x30
28 
29 #define ZIMAGE32_MAGIC 0x016f2818
30 
31 #define ZIMAGE64_MAGIC_V0 0x14000008
32 #define ZIMAGE64_MAGIC_V1 0x644d5241 /* "ARM\x64" */
33 
34 struct minimal_dtb_header {
35     uint32_t magic;
36     uint32_t total_size;
37     /* There are other fields but we don't use them yet. */
38 };
39 
40 #define DTB_MAGIC 0xd00dfeed
41 
42 /**
43  * copy_from_paddr - copy data from a physical address
44  * @dst: destination virtual address
45  * @paddr: source physical address
46  * @len: length to copy
47  */
copy_from_paddr(void * dst,paddr_t paddr,unsigned long len)48 void __init copy_from_paddr(void *dst, paddr_t paddr, unsigned long len)
49 {
50     void *src = (void *)FIXMAP_ADDR(FIXMAP_MISC);
51 
52     while (len) {
53         unsigned long l, s;
54 
55         s = paddr & (PAGE_SIZE-1);
56         l = min(PAGE_SIZE - s, len);
57 
58         set_fixmap(FIXMAP_MISC, maddr_to_mfn(paddr), PAGE_HYPERVISOR_WC);
59         memcpy(dst, src + s, l);
60         clean_dcache_va_range(dst, l);
61         clear_fixmap(FIXMAP_MISC);
62 
63         paddr += l;
64         dst += l;
65         len -= l;
66     }
67 }
68 
place_modules(struct kernel_info * info,paddr_t kernbase,paddr_t kernend)69 static void __init 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\n");
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\n");
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 __init 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 __init 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     void *kernel;
161     int rc;
162 
163     info->entry = load_addr;
164 
165     place_modules(info, load_addr, load_addr + len);
166 
167     printk("Loading zImage from %"PRIpaddr" to %"PRIpaddr"-%"PRIpaddr"\n",
168            paddr, load_addr, load_addr + len);
169 
170     kernel = ioremap_wc(paddr, len);
171     if ( !kernel )
172         panic("Unable to map the hwdom kernel\n");
173 
174     rc = copy_to_guest_phys_flush_dcache(info->d, load_addr,
175                                          kernel, len);
176     if ( rc != 0 )
177         panic("Unable to copy the kernel in the hwdom memory\n");
178 
179     iounmap(kernel);
180 }
181 
182 /*
183  * Uimage CPU Architecture Codes
184  */
185 #define IH_ARCH_ARM             2       /* ARM          */
186 #define IH_ARCH_ARM64           22      /* ARM64        */
187 
188 /*
189  * Check if the image is a uImage and setup kernel_info
190  */
kernel_uimage_probe(struct kernel_info * info,paddr_t addr,paddr_t size)191 static int __init kernel_uimage_probe(struct kernel_info *info,
192                                       paddr_t addr, paddr_t size)
193 {
194     struct {
195         __be32 magic;   /* Image Header Magic Number */
196         __be32 hcrc;    /* Image Header CRC Checksum */
197         __be32 time;    /* Image Creation Timestamp  */
198         __be32 size;    /* Image Data Size           */
199         __be32 load;    /* Data Load Address         */
200         __be32 ep;      /* Entry Point Address       */
201         __be32 dcrc;    /* Image Data CRC Checksum   */
202         uint8_t os;     /* Operating System          */
203         uint8_t arch;   /* CPU architecture          */
204         uint8_t type;   /* Image Type                */
205         uint8_t comp;   /* Compression Type          */
206         uint8_t name[UIMAGE_NMLEN]; /* Image Name  */
207     } uimage;
208 
209     uint32_t len;
210 
211     if ( size < sizeof(uimage) )
212         return -EINVAL;
213 
214     copy_from_paddr(&uimage, addr, sizeof(uimage));
215 
216     if ( be32_to_cpu(uimage.magic) != UIMAGE_MAGIC )
217         return -EINVAL;
218 
219     len = be32_to_cpu(uimage.size);
220 
221     if ( len > size - sizeof(uimage) )
222         return -EINVAL;
223 
224     info->zimage.kernel_addr = addr + sizeof(uimage);
225     info->zimage.len = len;
226 
227     info->entry = info->zimage.start;
228     info->load = kernel_zimage_load;
229 
230 #ifdef CONFIG_ARM_64
231     switch ( uimage.arch )
232     {
233     case IH_ARCH_ARM:
234         info->type = DOMAIN_32BIT;
235         break;
236     case IH_ARCH_ARM64:
237         info->type = DOMAIN_64BIT;
238         break;
239     default:
240         printk(XENLOG_ERR "Unsupported uImage arch type %d\n", uimage.arch);
241         return -EINVAL;
242     }
243 #endif
244 
245     return 0;
246 }
247 
output_length(char * image,unsigned long image_len)248 static __init uint32_t output_length(char *image, unsigned long image_len)
249 {
250     return *(uint32_t *)&image[image_len - 4];
251 }
252 
kernel_decompress(struct bootmodule * mod)253 static __init int kernel_decompress(struct bootmodule *mod)
254 {
255     char *output, *input;
256     char magic[2];
257     int rc;
258     unsigned kernel_order_out;
259     paddr_t output_size;
260     struct page_info *pages;
261     mfn_t mfn;
262     int i;
263     paddr_t addr = mod->start;
264     paddr_t size = mod->size;
265 
266     if ( size < 2 )
267         return -EINVAL;
268 
269     copy_from_paddr(magic, addr, sizeof(magic));
270 
271     /* only gzip is supported */
272     if ( !gzip_check(magic, size) )
273         return -EINVAL;
274 
275     input = ioremap_cache(addr, size);
276     if ( input == NULL )
277         return -EFAULT;
278 
279     output_size = output_length(input, size);
280     kernel_order_out = get_order_from_bytes(output_size);
281     pages = alloc_domheap_pages(NULL, kernel_order_out, 0);
282     if ( pages == NULL )
283     {
284         iounmap(input);
285         return -ENOMEM;
286     }
287     mfn = page_to_mfn(pages);
288     output = __vmap(&mfn, 1 << kernel_order_out, 1, 1, PAGE_HYPERVISOR, VMAP_DEFAULT);
289 
290     rc = perform_gunzip(output, input, size);
291     clean_dcache_va_range(output, output_size);
292     iounmap(input);
293     vunmap(output);
294 
295     mod->start = page_to_maddr(pages);
296     mod->size = output_size;
297 
298     /*
299      * Need to free pages after output_size here because they won't be
300      * freed by discard_initial_modules
301      */
302     i = PFN_UP(output_size);
303     for ( ; i < (1 << kernel_order_out); i++ )
304         free_domheap_page(pages + i);
305 
306     /*
307      * Free the original kernel, update the pointers to the
308      * decompressed kernel
309      */
310     dt_unreserved_regions(addr, addr + size, init_domheap_pages, 0);
311 
312     return 0;
313 }
314 
315 #ifdef CONFIG_ARM_64
316 /*
317  * Check if the image is a 64-bit Image.
318  */
kernel_zimage64_probe(struct kernel_info * info,paddr_t addr,paddr_t size)319 static int __init kernel_zimage64_probe(struct kernel_info *info,
320                                         paddr_t addr, paddr_t size)
321 {
322     /* linux/Documentation/arm64/booting.txt */
323     struct {
324         uint32_t magic0;
325         uint32_t res0;
326         uint64_t text_offset;  /* Image load offset */
327         uint64_t res1;
328         uint64_t res2;
329         /* zImage V1 only from here */
330         uint64_t res3;
331         uint64_t res4;
332         uint64_t res5;
333         uint32_t magic1;
334         uint32_t res6;
335     } zimage;
336     uint64_t start, end;
337 
338     if ( size < sizeof(zimage) )
339         return -EINVAL;
340 
341     copy_from_paddr(&zimage, addr, sizeof(zimage));
342 
343     if ( zimage.magic0 != ZIMAGE64_MAGIC_V0 &&
344          zimage.magic1 != ZIMAGE64_MAGIC_V1 )
345         return -EINVAL;
346 
347     /* Currently there is no length in the header, so just use the size */
348     start = 0;
349     end = size;
350 
351     /*
352      * Given the above this check is a bit pointless, but leave it
353      * here in case someone adds a length field in the future.
354      */
355     if ( (end - start) > size )
356         return -EINVAL;
357 
358     info->zimage.kernel_addr = addr;
359     info->zimage.len = end - start;
360     info->zimage.text_offset = zimage.text_offset;
361 
362     info->load = kernel_zimage_load;
363 
364     info->type = DOMAIN_64BIT;
365 
366     return 0;
367 }
368 #endif
369 
370 /*
371  * Check if the image is a 32-bit zImage and setup kernel_info
372  */
kernel_zimage32_probe(struct kernel_info * info,paddr_t addr,paddr_t size)373 static int __init kernel_zimage32_probe(struct kernel_info *info,
374                                         paddr_t addr, paddr_t size)
375 {
376     uint32_t zimage[ZIMAGE32_HEADER_LEN/4];
377     uint32_t start, end;
378     struct minimal_dtb_header dtb_hdr;
379 
380     if ( size < ZIMAGE32_HEADER_LEN )
381         return -EINVAL;
382 
383     copy_from_paddr(zimage, addr, sizeof(zimage));
384 
385     if (zimage[ZIMAGE32_MAGIC_OFFSET/4] != ZIMAGE32_MAGIC)
386         return -EINVAL;
387 
388     start = zimage[ZIMAGE32_START_OFFSET/4];
389     end = zimage[ZIMAGE32_END_OFFSET/4];
390 
391     if ( (end - start) > size )
392         return -EINVAL;
393 
394     /*
395      * Check for an appended DTB.
396      */
397     if ( addr + end - start + sizeof(dtb_hdr) <= size )
398     {
399         copy_from_paddr(&dtb_hdr, addr + end - start, sizeof(dtb_hdr));
400         if (be32_to_cpu(dtb_hdr.magic) == DTB_MAGIC) {
401             end += be32_to_cpu(dtb_hdr.total_size);
402 
403             if ( end > addr + size )
404                 return -EINVAL;
405         }
406     }
407 
408     info->zimage.kernel_addr = addr;
409 
410     info->zimage.start = start;
411     info->zimage.len = end - start;
412 
413     info->load = kernel_zimage_load;
414 
415 #ifdef CONFIG_ARM_64
416     info->type = DOMAIN_32BIT;
417 #endif
418 
419     return 0;
420 }
421 
kernel_probe(struct kernel_info * info,const struct dt_device_node * domain)422 int __init kernel_probe(struct kernel_info *info,
423                         const struct dt_device_node *domain)
424 {
425     struct bootmodule *mod = NULL;
426     struct bootcmdline *cmd = NULL;
427     struct dt_device_node *node;
428     u64 kernel_addr, initrd_addr, dtb_addr, size;
429     int rc;
430 
431     /* domain is NULL only for the hardware domain */
432     if ( domain == NULL )
433     {
434         ASSERT(is_hardware_domain(info->d));
435 
436         mod = boot_module_find_by_kind(BOOTMOD_KERNEL);
437 
438         info->kernel_bootmodule = mod;
439         info->initrd_bootmodule = boot_module_find_by_kind(BOOTMOD_RAMDISK);
440 
441         cmd = boot_cmdline_find_by_kind(BOOTMOD_KERNEL);
442         if ( cmd )
443             info->cmdline = &cmd->cmdline[0];
444     }
445     else
446     {
447         const char *name = NULL;
448 
449         dt_for_each_child_node(domain, node)
450         {
451             if ( dt_device_is_compatible(node, "multiboot,kernel") )
452             {
453                 u32 len;
454                 const __be32 *val;
455 
456                 val = dt_get_property(node, "reg", &len);
457                 dt_get_range(&val, node, &kernel_addr, &size);
458                 mod = boot_module_find_by_addr_and_kind(
459                         BOOTMOD_KERNEL, kernel_addr);
460                 info->kernel_bootmodule = mod;
461             }
462             else if ( dt_device_is_compatible(node, "multiboot,ramdisk") )
463             {
464                 u32 len;
465                 const __be32 *val;
466 
467                 val = dt_get_property(node, "reg", &len);
468                 dt_get_range(&val, node, &initrd_addr, &size);
469                 info->initrd_bootmodule = boot_module_find_by_addr_and_kind(
470                         BOOTMOD_RAMDISK, initrd_addr);
471             }
472             else if ( dt_device_is_compatible(node, "multiboot,device-tree") )
473             {
474                 uint32_t len;
475                 const __be32 *val;
476 
477                 val = dt_get_property(node, "reg", &len);
478                 if ( val == NULL )
479                     continue;
480                 dt_get_range(&val, node, &dtb_addr, &size);
481                 info->dtb_bootmodule = boot_module_find_by_addr_and_kind(
482                         BOOTMOD_GUEST_DTB, dtb_addr);
483             }
484             else
485                 continue;
486         }
487         name = dt_node_name(domain);
488         cmd = boot_cmdline_find_by_name(name);
489         if ( cmd )
490             info->cmdline = &cmd->cmdline[0];
491     }
492     if ( !mod || !mod->size )
493     {
494         printk(XENLOG_ERR "Missing kernel boot module?\n");
495         return -ENOENT;
496     }
497 
498     printk("Loading %pd kernel from boot module @ %"PRIpaddr"\n",
499            info->d, info->kernel_bootmodule->start);
500     if ( info->initrd_bootmodule )
501         printk("Loading ramdisk from boot module @ %"PRIpaddr"\n",
502                info->initrd_bootmodule->start);
503 
504     /* if it is a gzip'ed image, 32bit or 64bit, uncompress it */
505     rc = kernel_decompress(mod);
506     if (rc < 0 && rc != -EINVAL)
507         return rc;
508 
509 #ifdef CONFIG_ARM_64
510     rc = kernel_zimage64_probe(info, mod->start, mod->size);
511     if (rc < 0)
512 #endif
513         rc = kernel_uimage_probe(info, mod->start, mod->size);
514     if (rc < 0)
515         rc = kernel_zimage32_probe(info, mod->start, mod->size);
516 
517     return rc;
518 }
519 
kernel_load(struct kernel_info * info)520 void __init kernel_load(struct kernel_info *info)
521 {
522     info->load(info);
523 }
524 
525 /*
526  * Local variables:
527  * mode: C
528  * c-file-style: "BSD"
529  * c-basic-offset: 4
530  * indent-tabs-mode: nil
531  * End:
532  */
533