1 // SPDX-License-Identifier: GPL-2.0
3 * Definitions and wrapper functions for kernel decompressor
5 * Copyright IBM Corp. 2010
7 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
10 #include <linux/uaccess.h>
24 #define memmove memmove
25 #define memzero(s, n) memset((s), 0, (n))
27 /* Symbols defined by linker scripts */
28 extern char input_data[];
30 extern char _text, _end;
31 extern char _bss, _ebss;
33 static void error(char *m);
35 static unsigned long free_mem_ptr;
36 static unsigned long free_mem_end_ptr;
38 #ifdef CONFIG_HAVE_KERNEL_BZIP2
39 #define HEAP_SIZE 0x400000
41 #define HEAP_SIZE 0x10000
44 #ifdef CONFIG_KERNEL_GZIP
45 #include "../../../../lib/decompress_inflate.c"
48 #ifdef CONFIG_KERNEL_BZIP2
49 #include "../../../../lib/decompress_bunzip2.c"
52 #ifdef CONFIG_KERNEL_LZ4
53 #include "../../../../lib/decompress_unlz4.c"
56 #ifdef CONFIG_KERNEL_LZMA
57 #include "../../../../lib/decompress_unlzma.c"
60 #ifdef CONFIG_KERNEL_LZO
61 #include "../../../../lib/decompress_unlzo.c"
64 #ifdef CONFIG_KERNEL_XZ
65 #include "../../../../lib/decompress_unxz.c"
68 static int puts(const char *s)
74 void *memset(void *s, int c, size_t n)
84 void *memcpy(void *dest, const void *src, size_t n)
94 void *memmove(void *dest, const void *src, size_t n)
111 static void error(char *x)
113 unsigned long long psw = 0x000a0000deadbeefULL;
117 puts("\n\n -- System halted");
119 asm volatile("lpsw %0" : : "Q" (psw));
123 * Safe guard the ipl parameter block against a memory area that will be
124 * overwritten. The validity check for the ipl parameter block is complex
125 * (see cio_get_iplinfo and ipl_save_parameters) but if the pointer to
126 * the ipl parameter block intersects with the passed memory area we can
127 * safely assume that we can read from that memory. In that case just copy
128 * the memory to IPL_PARMBLOCK_ORIGIN even if there is no ipl parameter
131 static void check_ipl_parmblock(void *start, unsigned long size)
135 src = (void *)(unsigned long) S390_lowcore.ipl_parmblock_ptr;
136 if (src + PAGE_SIZE <= start || src >= start + size)
138 dst = (void *) IPL_PARMBLOCK_ORIGIN;
139 memmove(dst, src, PAGE_SIZE);
140 S390_lowcore.ipl_parmblock_ptr = IPL_PARMBLOCK_ORIGIN;
143 unsigned long decompress_kernel(void)
145 void *output, *kernel_end;
147 output = (void *) ALIGN((unsigned long) &_end + HEAP_SIZE, PAGE_SIZE);
148 kernel_end = output + SZ__bss_start;
149 check_ipl_parmblock((void *) 0, (unsigned long) kernel_end);
151 #ifdef CONFIG_BLK_DEV_INITRD
153 * Move the initrd right behind the end of the decompressed
154 * kernel image. This also prevents initrd corruption caused by
155 * bss clearing since kernel_end will always be located behind the
156 * current bss section..
158 if (INITRD_START && INITRD_SIZE && kernel_end > (void *) INITRD_START) {
159 check_ipl_parmblock(kernel_end, INITRD_SIZE);
160 memmove(kernel_end, (void *) INITRD_START, INITRD_SIZE);
161 INITRD_START = (unsigned long) kernel_end;
166 * Clear bss section. free_mem_ptr and free_mem_end_ptr need to be
167 * initialized afterwards since they reside in bss.
169 memset(&_bss, 0, &_ebss - &_bss);
170 free_mem_ptr = (unsigned long) &_end;
171 free_mem_end_ptr = free_mem_ptr + HEAP_SIZE;
173 puts("Uncompressing Linux... ");
174 __decompress(input_data, input_len, NULL, NULL, output, 0, NULL, error);
175 puts("Ok, booting the kernel.\n");
176 return (unsigned long) output;