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