blob: 2751b3a8a66f447b238a5df4681569f22f0e39d4 [file] [log] [blame]
Martin Schwidefsky1844c9b2010-02-26 22:37:53 +01001/*
2 * Definitions and wrapper functions for kernel decompressor
3 *
4 * Copyright IBM Corp. 2010
5 *
6 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
7 */
8
9#include <asm/uaccess.h>
10#include <asm/page.h>
11#include <asm/ipl.h>
12#include "sizes.h"
13
14/*
15 * gzip declarations
16 */
17#define STATIC static
18
19#undef memset
20#undef memcpy
21#undef memmove
22#define memzero(s, n) memset((s), 0, (n))
23
24/* Symbols defined by linker scripts */
25extern char input_data[];
26extern int input_len;
Martin Schwidefsky06c0dd72010-03-24 11:49:57 +010027extern char _text, _end;
28extern char _bss, _ebss;
Martin Schwidefsky1844c9b2010-02-26 22:37:53 +010029
30static void error(char *m);
31
32static unsigned long free_mem_ptr;
33static unsigned long free_mem_end_ptr;
34
35#ifdef CONFIG_HAVE_KERNEL_BZIP2
36#define HEAP_SIZE 0x400000
37#else
38#define HEAP_SIZE 0x10000
39#endif
40
41#ifdef CONFIG_KERNEL_GZIP
42#include "../../../../lib/decompress_inflate.c"
43#endif
44
45#ifdef CONFIG_KERNEL_BZIP2
46#include "../../../../lib/decompress_bunzip2.c"
47#endif
48
49#ifdef CONFIG_KERNEL_LZMA
50#include "../../../../lib/decompress_unlzma.c"
51#endif
52
Heiko Carstenscdf56642010-05-26 23:27:12 +020053#ifdef CONFIG_KERNEL_LZO
54#include "../../../../lib/decompress_unlzo.c"
55#endif
56
Martin Schwidefsky1844c9b2010-02-26 22:37:53 +010057extern _sclp_print_early(const char *);
58
59int puts(const char *s)
60{
61 _sclp_print_early(s);
62 return 0;
63}
64
65void *memset(void *s, int c, size_t n)
66{
67 char *xs;
68
69 if (c == 0)
70 return __builtin_memset(s, 0, n);
71
72 xs = (char *) s;
73 if (n > 0)
74 do {
75 *xs++ = c;
76 } while (--n > 0);
77 return s;
78}
79
80void *memcpy(void *__dest, __const void *__src, size_t __n)
81{
82 return __builtin_memcpy(__dest, __src, __n);
83}
84
85void *memmove(void *__dest, __const void *__src, size_t __n)
86{
87 char *d;
88 const char *s;
89
90 if (__dest <= __src)
91 return __builtin_memcpy(__dest, __src, __n);
92 d = __dest + __n;
93 s = __src + __n;
94 while (__n--)
95 *--d = *--s;
96 return __dest;
97}
98
99static void error(char *x)
100{
101 unsigned long long psw = 0x000a0000deadbeefULL;
102
103 puts("\n\n");
104 puts(x);
105 puts("\n\n -- System halted");
106
107 asm volatile("lpsw %0" : : "Q" (psw));
108}
109
110/*
111 * Safe guard the ipl parameter block against a memory area that will be
112 * overwritten. The validity check for the ipl parameter block is complex
113 * (see cio_get_iplinfo and ipl_save_parameters) but if the pointer to
114 * the ipl parameter block intersects with the passed memory area we can
115 * safely assume that we can read from that memory. In that case just copy
116 * the memory to IPL_PARMBLOCK_ORIGIN even if there is no ipl parameter
117 * block.
118 */
119static void check_ipl_parmblock(void *start, unsigned long size)
120{
121 void *src, *dst;
122
123 src = (void *)(unsigned long) S390_lowcore.ipl_parmblock_ptr;
124 if (src + PAGE_SIZE <= start || src >= start + size)
125 return;
126 dst = (void *) IPL_PARMBLOCK_ORIGIN;
127 memmove(dst, src, PAGE_SIZE);
128 S390_lowcore.ipl_parmblock_ptr = IPL_PARMBLOCK_ORIGIN;
129}
130
131unsigned long decompress_kernel(void)
132{
133 unsigned long output_addr;
134 unsigned char *output;
135
Martin Schwidefskya8c8d7c2011-02-17 13:13:57 +0100136 output_addr = ((unsigned long) &_end + HEAP_SIZE + 4095UL) & -4096UL;
137 check_ipl_parmblock((void *) 0, output_addr + SZ__bss_start);
Martin Schwidefsky06c0dd72010-03-24 11:49:57 +0100138 memset(&_bss, 0, &_ebss - &_bss);
Martin Schwidefsky1844c9b2010-02-26 22:37:53 +0100139 free_mem_ptr = (unsigned long)&_end;
140 free_mem_end_ptr = free_mem_ptr + HEAP_SIZE;
Martin Schwidefskya8c8d7c2011-02-17 13:13:57 +0100141 output = (unsigned char *) output_addr;
Martin Schwidefsky1844c9b2010-02-26 22:37:53 +0100142
Martin Schwidefsky1844c9b2010-02-26 22:37:53 +0100143#ifdef CONFIG_BLK_DEV_INITRD
144 /*
145 * Move the initrd right behind the end of the decompressed
146 * kernel image.
147 */
148 if (INITRD_START && INITRD_SIZE &&
149 INITRD_START < (unsigned long) output + SZ__bss_start) {
150 check_ipl_parmblock(output + SZ__bss_start,
151 INITRD_START + INITRD_SIZE);
152 memmove(output + SZ__bss_start,
153 (void *) INITRD_START, INITRD_SIZE);
154 INITRD_START = (unsigned long) output + SZ__bss_start;
155 }
156#endif
157
158 puts("Uncompressing Linux... ");
159 decompress(input_data, input_len, NULL, NULL, output, NULL, error);
160 puts("Ok, booting the kernel.\n");
161 return (unsigned long) output;
162}
163