Avoid beyond bounds copy while caching ACL
[zen-stable.git] / arch / s390 / boot / compressed / misc.c
blob465eca756feb8d9ce36d70b10d6abd6cd850ad4e
1 /*
2 * Definitions and wrapper functions for kernel decompressor
4 * Copyright IBM Corp. 2010
6 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
7 */
9 #include <asm/uaccess.h>
10 #include <asm/page.h>
11 #include <asm/ipl.h>
12 #include "sizes.h"
15 * gzip declarations
17 #define STATIC static
19 #undef memset
20 #undef memcpy
21 #undef memmove
22 #define memmove memmove
23 #define memzero(s, n) memset((s), 0, (n))
25 /* Symbols defined by linker scripts */
26 extern char input_data[];
27 extern int input_len;
28 extern char _text, _end;
29 extern char _bss, _ebss;
31 static void error(char *m);
33 static unsigned long free_mem_ptr;
34 static unsigned long free_mem_end_ptr;
36 #ifdef CONFIG_HAVE_KERNEL_BZIP2
37 #define HEAP_SIZE 0x400000
38 #else
39 #define HEAP_SIZE 0x10000
40 #endif
42 #ifdef CONFIG_KERNEL_GZIP
43 #include "../../../../lib/decompress_inflate.c"
44 #endif
46 #ifdef CONFIG_KERNEL_BZIP2
47 #include "../../../../lib/decompress_bunzip2.c"
48 #endif
50 #ifdef CONFIG_KERNEL_LZMA
51 #include "../../../../lib/decompress_unlzma.c"
52 #endif
54 #ifdef CONFIG_KERNEL_LZO
55 #include "../../../../lib/decompress_unlzo.c"
56 #endif
58 #ifdef CONFIG_KERNEL_XZ
59 #include "../../../../lib/decompress_unxz.c"
60 #endif
62 extern _sclp_print_early(const char *);
64 static int puts(const char *s)
66 _sclp_print_early(s);
67 return 0;
70 void *memset(void *s, int c, size_t n)
72 char *xs;
74 if (c == 0)
75 return __builtin_memset(s, 0, n);
77 xs = (char *) s;
78 if (n > 0)
79 do {
80 *xs++ = c;
81 } while (--n > 0);
82 return s;
85 void *memcpy(void *__dest, __const void *__src, size_t __n)
87 return __builtin_memcpy(__dest, __src, __n);
90 void *memmove(void *__dest, __const void *__src, size_t __n)
92 char *d;
93 const char *s;
95 if (__dest <= __src)
96 return __builtin_memcpy(__dest, __src, __n);
97 d = __dest + __n;
98 s = __src + __n;
99 while (__n--)
100 *--d = *--s;
101 return __dest;
104 static void error(char *x)
106 unsigned long long psw = 0x000a0000deadbeefULL;
108 puts("\n\n");
109 puts(x);
110 puts("\n\n -- System halted");
112 asm volatile("lpsw %0" : : "Q" (psw));
116 * Safe guard the ipl parameter block against a memory area that will be
117 * overwritten. The validity check for the ipl parameter block is complex
118 * (see cio_get_iplinfo and ipl_save_parameters) but if the pointer to
119 * the ipl parameter block intersects with the passed memory area we can
120 * safely assume that we can read from that memory. In that case just copy
121 * the memory to IPL_PARMBLOCK_ORIGIN even if there is no ipl parameter
122 * block.
124 static void check_ipl_parmblock(void *start, unsigned long size)
126 void *src, *dst;
128 src = (void *)(unsigned long) S390_lowcore.ipl_parmblock_ptr;
129 if (src + PAGE_SIZE <= start || src >= start + size)
130 return;
131 dst = (void *) IPL_PARMBLOCK_ORIGIN;
132 memmove(dst, src, PAGE_SIZE);
133 S390_lowcore.ipl_parmblock_ptr = IPL_PARMBLOCK_ORIGIN;
136 unsigned long decompress_kernel(void)
138 unsigned long output_addr;
139 unsigned char *output;
141 output_addr = ((unsigned long) &_end + HEAP_SIZE + 4095UL) & -4096UL;
142 check_ipl_parmblock((void *) 0, output_addr + SZ__bss_start);
143 memset(&_bss, 0, &_ebss - &_bss);
144 free_mem_ptr = (unsigned long)&_end;
145 free_mem_end_ptr = free_mem_ptr + HEAP_SIZE;
146 output = (unsigned char *) output_addr;
148 #ifdef CONFIG_BLK_DEV_INITRD
150 * Move the initrd right behind the end of the decompressed
151 * kernel image.
153 if (INITRD_START && INITRD_SIZE &&
154 INITRD_START < (unsigned long) output + SZ__bss_start) {
155 check_ipl_parmblock(output + SZ__bss_start,
156 INITRD_START + INITRD_SIZE);
157 memmove(output + SZ__bss_start,
158 (void *) INITRD_START, INITRD_SIZE);
159 INITRD_START = (unsigned long) output + SZ__bss_start;
161 #endif
163 puts("Uncompressing Linux... ");
164 decompress(input_data, input_len, NULL, NULL, output, NULL, error);
165 puts("Ok, booting the kernel.\n");
166 return (unsigned long) output;