1 /*
2  * Copyright 2001 MontaVista Software Inc.
3  * Author: Matt Porter <mporter@mvista.com>
4  *
5  * Copyright (C) 2009 Lemote, Inc.
6  * Author: Wu Zhangjin <wuzhangjin@gmail.com>
7  *
8  * This program is free software; you can redistribute	it and/or modify it
9  * under  the terms of	the GNU General	 Public License as published by the
10  * Free Software Foundation;  either version 2 of the  License, or (at your
11  * option) any later version.
12  */
13 
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/string.h>
17 #include <linux/libfdt.h>
18 
19 #include <asm/addrspace.h>
20 
21 /*
22  * These two variables specify the free mem region
23  * that can be used for temporary malloc area
24  */
25 unsigned long free_mem_ptr;
26 unsigned long free_mem_end_ptr;
27 
28 /* The linker tells us where the image is. */
29 extern unsigned char __image_begin, __image_end;
30 
31 /* debug interfaces  */
32 #ifdef CONFIG_DEBUG_ZBOOT
33 extern void puts(const char *s);
34 extern void puthex(unsigned long long val);
35 #else
36 #define puts(s) do {} while (0)
37 #define puthex(val) do {} while (0)
38 #endif
39 
40 extern char __appended_dtb[];
41 
42 void error(char *x)
43 {
44 	puts("\n\n");
45 	puts(x);
46 	puts("\n\n -- System halted");
47 
48 	while (1)
49 		;	/* Halt */
50 }
51 
52 /* activate the code for pre-boot environment */
53 #define STATIC static
54 
55 #ifdef CONFIG_KERNEL_GZIP
56 #include "../../../../lib/decompress_inflate.c"
57 #endif
58 
59 #ifdef CONFIG_KERNEL_BZIP2
60 #include "../../../../lib/decompress_bunzip2.c"
61 #endif
62 
63 #ifdef CONFIG_KERNEL_LZ4
64 #include "../../../../lib/decompress_unlz4.c"
65 #endif
66 
67 #ifdef CONFIG_KERNEL_LZMA
68 #include "../../../../lib/decompress_unlzma.c"
69 #endif
70 
71 #ifdef CONFIG_KERNEL_LZO
72 #include "../../../../lib/decompress_unlzo.c"
73 #endif
74 
75 #ifdef CONFIG_KERNEL_XZ
76 #include "../../../../lib/decompress_unxz.c"
77 #endif
78 
79 unsigned long __stack_chk_guard;
80 
81 void __stack_chk_guard_setup(void)
82 {
83 	__stack_chk_guard = 0x000a0dff;
84 }
85 
86 void __stack_chk_fail(void)
87 {
88 	error("stack-protector: Kernel stack is corrupted\n");
89 }
90 
91 void decompress_kernel(unsigned long boot_heap_start)
92 {
93 	unsigned long zimage_start, zimage_size;
94 
95 	__stack_chk_guard_setup();
96 
97 	zimage_start = (unsigned long)(&__image_begin);
98 	zimage_size = (unsigned long)(&__image_end) -
99 	    (unsigned long)(&__image_begin);
100 
101 	puts("zimage at:     ");
102 	puthex(zimage_start);
103 	puts(" ");
104 	puthex(zimage_size + zimage_start);
105 	puts("\n");
106 
107 	/* This area are prepared for mallocing when decompressing */
108 	free_mem_ptr = boot_heap_start;
109 	free_mem_end_ptr = boot_heap_start + BOOT_HEAP_SIZE;
110 
111 	/* Display standard Linux/MIPS boot prompt */
112 	puts("Uncompressing Linux at load address ");
113 	puthex(VMLINUX_LOAD_ADDRESS_ULL);
114 	puts("\n");
115 
116 	/* Decompress the kernel with according algorithm */
117 	__decompress((char *)zimage_start, zimage_size, 0, 0,
118 		   (void *)VMLINUX_LOAD_ADDRESS_ULL, 0, 0, error);
119 
120 	if (IS_ENABLED(CONFIG_MIPS_RAW_APPENDED_DTB) &&
121 	    fdt_magic((void *)&__appended_dtb) == FDT_MAGIC) {
122 		unsigned int image_size, dtb_size;
123 
124 		dtb_size = fdt_totalsize((void *)&__appended_dtb);
125 
126 		/* last four bytes is always image size in little endian */
127 		image_size = le32_to_cpup((void *)&__image_end - 4);
128 
129 		/* copy dtb to where the booted kernel will expect it */
130 		memcpy((void *)VMLINUX_LOAD_ADDRESS_ULL + image_size,
131 		       __appended_dtb, dtb_size);
132 	}
133 
134 	/* FIXME: should we flush cache here? */
135 	puts("Now, booting the kernel...\n");
136 }
137