blob: c4c6a1cf221bde749673b59dccb6e3c6113a0b4c [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
Heiko Carstensd7b081a2011-03-15 17:08:32 +010022#define memmove memmove
Martin Schwidefsky1844c9b2010-02-26 22:37:53 +010023#define memzero(s, n) memset((s), 0, (n))
24
25/* Symbols defined by linker scripts */
26extern char input_data[];
27extern int input_len;
Martin Schwidefsky06c0dd72010-03-24 11:49:57 +010028extern char _text, _end;
29extern char _bss, _ebss;
Martin Schwidefsky1844c9b2010-02-26 22:37:53 +010030
31static void error(char *m);
32
33static unsigned long free_mem_ptr;
34static unsigned long free_mem_end_ptr;
35
36#ifdef CONFIG_HAVE_KERNEL_BZIP2
37#define HEAP_SIZE 0x400000
38#else
39#define HEAP_SIZE 0x10000
40#endif
41
42#ifdef CONFIG_KERNEL_GZIP
43#include "../../../../lib/decompress_inflate.c"
44#endif
45
46#ifdef CONFIG_KERNEL_BZIP2
47#include "../../../../lib/decompress_bunzip2.c"
48#endif
49
50#ifdef CONFIG_KERNEL_LZMA
51#include "../../../../lib/decompress_unlzma.c"
52#endif
53
Heiko Carstenscdf56642010-05-26 23:27:12 +020054#ifdef CONFIG_KERNEL_LZO
55#include "../../../../lib/decompress_unlzo.c"
56#endif
57
Heiko Carstensd7b081a2011-03-15 17:08:32 +010058#ifdef CONFIG_KERNEL_XZ
59#include "../../../../lib/decompress_unxz.c"
60#endif
61
Martin Schwidefsky1844c9b2010-02-26 22:37:53 +010062extern _sclp_print_early(const char *);
63
Martin Schwidefskyc4736d92011-10-30 15:17:11 +010064static int puts(const char *s)
Martin Schwidefsky1844c9b2010-02-26 22:37:53 +010065{
66 _sclp_print_early(s);
67 return 0;
68}
69
70void *memset(void *s, int c, size_t n)
71{
72 char *xs;
73
Heiko Carstens535c6112012-08-14 13:20:20 +020074 xs = s;
75 while (n--)
76 *xs++ = c;
Martin Schwidefsky1844c9b2010-02-26 22:37:53 +010077 return s;
78}
79
Heiko Carstens535c6112012-08-14 13:20:20 +020080void *memcpy(void *dest, const void *src, size_t n)
Martin Schwidefsky1844c9b2010-02-26 22:37:53 +010081{
Heiko Carstens535c6112012-08-14 13:20:20 +020082 const char *s = src;
83 char *d = dest;
84
85 while (n--)
86 *d++ = *s++;
87 return dest;
Martin Schwidefsky1844c9b2010-02-26 22:37:53 +010088}
89
Heiko Carstens535c6112012-08-14 13:20:20 +020090void *memmove(void *dest, const void *src, size_t n)
Martin Schwidefsky1844c9b2010-02-26 22:37:53 +010091{
Heiko Carstens535c6112012-08-14 13:20:20 +020092 const char *s = src;
93 char *d = dest;
Martin Schwidefsky1844c9b2010-02-26 22:37:53 +010094
Heiko Carstens535c6112012-08-14 13:20:20 +020095 if (d <= s) {
96 while (n--)
97 *d++ = *s++;
98 } else {
99 d += n;
100 s += n;
101 while (n--)
102 *--d = *--s;
103 }
104 return dest;
Martin Schwidefsky1844c9b2010-02-26 22:37:53 +0100105}
106
107static void error(char *x)
108{
109 unsigned long long psw = 0x000a0000deadbeefULL;
110
111 puts("\n\n");
112 puts(x);
113 puts("\n\n -- System halted");
114
115 asm volatile("lpsw %0" : : "Q" (psw));
116}
117
118/*
119 * Safe guard the ipl parameter block against a memory area that will be
120 * overwritten. The validity check for the ipl parameter block is complex
121 * (see cio_get_iplinfo and ipl_save_parameters) but if the pointer to
122 * the ipl parameter block intersects with the passed memory area we can
123 * safely assume that we can read from that memory. In that case just copy
124 * the memory to IPL_PARMBLOCK_ORIGIN even if there is no ipl parameter
125 * block.
126 */
127static void check_ipl_parmblock(void *start, unsigned long size)
128{
129 void *src, *dst;
130
131 src = (void *)(unsigned long) S390_lowcore.ipl_parmblock_ptr;
132 if (src + PAGE_SIZE <= start || src >= start + size)
133 return;
134 dst = (void *) IPL_PARMBLOCK_ORIGIN;
135 memmove(dst, src, PAGE_SIZE);
136 S390_lowcore.ipl_parmblock_ptr = IPL_PARMBLOCK_ORIGIN;
137}
138
139unsigned long decompress_kernel(void)
140{
141 unsigned long output_addr;
142 unsigned char *output;
143
Martin Schwidefskya8c8d7c2011-02-17 13:13:57 +0100144 output_addr = ((unsigned long) &_end + HEAP_SIZE + 4095UL) & -4096UL;
145 check_ipl_parmblock((void *) 0, output_addr + SZ__bss_start);
Martin Schwidefsky06c0dd72010-03-24 11:49:57 +0100146 memset(&_bss, 0, &_ebss - &_bss);
Martin Schwidefsky1844c9b2010-02-26 22:37:53 +0100147 free_mem_ptr = (unsigned long)&_end;
148 free_mem_end_ptr = free_mem_ptr + HEAP_SIZE;
Martin Schwidefskya8c8d7c2011-02-17 13:13:57 +0100149 output = (unsigned char *) output_addr;
Martin Schwidefsky1844c9b2010-02-26 22:37:53 +0100150
Martin Schwidefsky1844c9b2010-02-26 22:37:53 +0100151#ifdef CONFIG_BLK_DEV_INITRD
152 /*
153 * Move the initrd right behind the end of the decompressed
154 * kernel image.
155 */
156 if (INITRD_START && INITRD_SIZE &&
157 INITRD_START < (unsigned long) output + SZ__bss_start) {
158 check_ipl_parmblock(output + SZ__bss_start,
159 INITRD_START + INITRD_SIZE);
160 memmove(output + SZ__bss_start,
161 (void *) INITRD_START, INITRD_SIZE);
162 INITRD_START = (unsigned long) output + SZ__bss_start;
163 }
164#endif
165
166 puts("Uncompressing Linux... ");
167 decompress(input_data, input_len, NULL, NULL, output, NULL, error);
168 puts("Ok, booting the kernel.\n");
169 return (unsigned long) output;
170}
171