511b2cc9b91ad4bcac34e0ee7268bd59eb26d472
[platform/kernel/linux-rpi.git] / arch / s390 / boot / compressed / misc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Definitions and wrapper functions for kernel decompressor
4  *
5  * Copyright IBM Corp. 2010
6  *
7  * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
8  */
9
10 #include <linux/uaccess.h>
11 #include <asm/page.h>
12 #include <asm/sclp.h>
13 #include <asm/ipl.h>
14 #include "sizes.h"
15
16 /*
17  * gzip declarations
18  */
19 #define STATIC static
20
21 #undef memset
22 #undef memcpy
23 #undef memmove
24 #define memmove memmove
25 #define memzero(s, n) memset((s), 0, (n))
26
27 /* Symbols defined by linker scripts */
28 extern char input_data[];
29 extern int input_len;
30 extern char _end[];
31 extern char _bss[], _ebss[];
32
33 static void error(char *m);
34
35 static unsigned long free_mem_ptr;
36 static unsigned long free_mem_end_ptr;
37
38 #ifdef CONFIG_HAVE_KERNEL_BZIP2
39 #define HEAP_SIZE       0x400000
40 #else
41 #define HEAP_SIZE       0x10000
42 #endif
43
44 #ifdef CONFIG_KERNEL_GZIP
45 #include "../../../../lib/decompress_inflate.c"
46 #endif
47
48 #ifdef CONFIG_KERNEL_BZIP2
49 #include "../../../../lib/decompress_bunzip2.c"
50 #endif
51
52 #ifdef CONFIG_KERNEL_LZ4
53 #include "../../../../lib/decompress_unlz4.c"
54 #endif
55
56 #ifdef CONFIG_KERNEL_LZMA
57 #include "../../../../lib/decompress_unlzma.c"
58 #endif
59
60 #ifdef CONFIG_KERNEL_LZO
61 #include "../../../../lib/decompress_unlzo.c"
62 #endif
63
64 #ifdef CONFIG_KERNEL_XZ
65 #include "../../../../lib/decompress_unxz.c"
66 #endif
67
68 static int puts(const char *s)
69 {
70         sclp_early_printk(s);
71         return 0;
72 }
73
74 void *memset(void *s, int c, size_t n)
75 {
76         char *xs;
77
78         xs = s;
79         while (n--)
80                 *xs++ = c;
81         return s;
82 }
83
84 void *memcpy(void *dest, const void *src, size_t n)
85 {
86         const char *s = src;
87         char *d = dest;
88
89         while (n--)
90                 *d++ = *s++;
91         return dest;
92 }
93
94 void *memmove(void *dest, const void *src, size_t n)
95 {
96         const char *s = src;
97         char *d = dest;
98
99         if (d <= s) {
100                 while (n--)
101                         *d++ = *s++;
102         } else {
103                 d += n;
104                 s += n;
105                 while (n--)
106                         *--d = *--s;
107         }
108         return dest;
109 }
110
111 static void error(char *x)
112 {
113         unsigned long long psw = 0x000a0000deadbeefULL;
114
115         puts("\n\n");
116         puts(x);
117         puts("\n\n -- System halted");
118
119         asm volatile("lpsw %0" : : "Q" (psw));
120 }
121
122 unsigned long decompress_kernel(void)
123 {
124         void *output, *kernel_end;
125
126         output = (void *) ALIGN((unsigned long) _end + HEAP_SIZE, PAGE_SIZE);
127         kernel_end = output + SZ__bss_start;
128
129 #ifdef CONFIG_BLK_DEV_INITRD
130         /*
131          * Move the initrd right behind the end of the decompressed
132          * kernel image. This also prevents initrd corruption caused by
133          * bss clearing since kernel_end will always be located behind the
134          * current bss section..
135          */
136         if (INITRD_START && INITRD_SIZE && kernel_end > (void *) INITRD_START) {
137                 memmove(kernel_end, (void *) INITRD_START, INITRD_SIZE);
138                 INITRD_START = (unsigned long) kernel_end;
139         }
140 #endif
141
142         /*
143          * Clear bss section. free_mem_ptr and free_mem_end_ptr need to be
144          * initialized afterwards since they reside in bss.
145          */
146         memset(_bss, 0, _ebss - _bss);
147         free_mem_ptr = (unsigned long) _end;
148         free_mem_end_ptr = free_mem_ptr + HEAP_SIZE;
149
150         __decompress(input_data, input_len, NULL, NULL, output, 0, NULL, error);
151         return (unsigned long) output;
152 }
153