GNU Linux-libre 4.4.283-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
20 #include <asm/addrspace.h>
21
22 /*
23  * These two variables specify the free mem region
24  * that can be used for temporary malloc area
25  */
26 unsigned long free_mem_ptr;
27 unsigned long free_mem_end_ptr;
28
29 /* The linker tells us where the image is. */
30 extern unsigned char __image_begin, __image_end;
31
32 /* debug interfaces  */
33 #ifdef CONFIG_DEBUG_ZBOOT
34 extern void puts(const char *s);
35 extern void puthex(unsigned long long val);
36 #else
37 #define puts(s) do {} while (0)
38 #define puthex(val) do {} while (0)
39 #endif
40
41 void error(char *x)
42 {
43         puts("\n\n");
44         puts(x);
45         puts("\n\n -- System halted");
46
47         while (1)
48                 ;       /* Halt */
49 }
50
51 /* activate the code for pre-boot environment */
52 #define STATIC static
53
54 #ifdef CONFIG_KERNEL_GZIP
55 #include "../../../../lib/decompress_inflate.c"
56 #endif
57
58 #ifdef CONFIG_KERNEL_BZIP2
59 #include "../../../../lib/decompress_bunzip2.c"
60 #endif
61
62 #ifdef CONFIG_KERNEL_LZ4
63 #include "../../../../lib/decompress_unlz4.c"
64 #endif
65
66 #ifdef CONFIG_KERNEL_LZMA
67 #include "../../../../lib/decompress_unlzma.c"
68 #endif
69
70 #ifdef CONFIG_KERNEL_LZO
71 #include "../../../../lib/decompress_unlzo.c"
72 #endif
73
74 #ifdef CONFIG_KERNEL_XZ
75 #include "../../../../lib/decompress_unxz.c"
76 #endif
77
78 unsigned long __stack_chk_guard;
79
80 void __stack_chk_guard_setup(void)
81 {
82         __stack_chk_guard = 0x000a0dff;
83 }
84
85 void __stack_chk_fail(void)
86 {
87         error("stack-protector: Kernel stack is corrupted\n");
88 }
89
90 void decompress_kernel(unsigned long boot_heap_start)
91 {
92         unsigned long zimage_start, zimage_size;
93
94         __stack_chk_guard_setup();
95
96         zimage_start = (unsigned long)(&__image_begin);
97         zimage_size = (unsigned long)(&__image_end) -
98             (unsigned long)(&__image_begin);
99
100         puts("zimage at:     ");
101         puthex(zimage_start);
102         puts(" ");
103         puthex(zimage_size + zimage_start);
104         puts("\n");
105
106         /* This area are prepared for mallocing when decompressing */
107         free_mem_ptr = boot_heap_start;
108         free_mem_end_ptr = boot_heap_start + BOOT_HEAP_SIZE;
109
110         /* Display standard Linux/MIPS boot prompt */
111         puts("Uncompressing Linux at load address ");
112         puthex(VMLINUX_LOAD_ADDRESS_ULL);
113         puts("\n");
114
115         /* Decompress the kernel with according algorithm */
116         __decompress((char *)zimage_start, zimage_size, 0, 0,
117                    (void *)VMLINUX_LOAD_ADDRESS_ULL, 0, 0, error);
118
119         /* FIXME: should we flush cache here? */
120         puts("Now, booting the kernel...\n");
121 }