Martin Schwidefsky | 1844c9b | 2010-02-26 22:37:53 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 9 | #include <linux/uaccess.h> |
Martin Schwidefsky | 1844c9b | 2010-02-26 22:37:53 +0100 | [diff] [blame] | 10 | #include <asm/page.h> |
Chen Gang | fbf87df | 2015-01-03 17:29:07 +0800 | [diff] [blame] | 11 | #include <asm/sclp.h> |
Martin Schwidefsky | 1844c9b | 2010-02-26 22:37:53 +0100 | [diff] [blame] | 12 | #include <asm/ipl.h> |
| 13 | #include "sizes.h" |
| 14 | |
| 15 | /* |
| 16 | * gzip declarations |
| 17 | */ |
| 18 | #define STATIC static |
| 19 | |
| 20 | #undef memset |
| 21 | #undef memcpy |
| 22 | #undef memmove |
Heiko Carstens | d7b081a | 2011-03-15 17:08:32 +0100 | [diff] [blame] | 23 | #define memmove memmove |
Martin Schwidefsky | 1844c9b | 2010-02-26 22:37:53 +0100 | [diff] [blame] | 24 | #define memzero(s, n) memset((s), 0, (n)) |
| 25 | |
| 26 | /* Symbols defined by linker scripts */ |
| 27 | extern char input_data[]; |
| 28 | extern int input_len; |
Martin Schwidefsky | 06c0dd7 | 2010-03-24 11:49:57 +0100 | [diff] [blame] | 29 | extern char _text, _end; |
| 30 | extern char _bss, _ebss; |
Martin Schwidefsky | 1844c9b | 2010-02-26 22:37:53 +0100 | [diff] [blame] | 31 | |
| 32 | static void error(char *m); |
| 33 | |
| 34 | static unsigned long free_mem_ptr; |
| 35 | static unsigned long free_mem_end_ptr; |
| 36 | |
| 37 | #ifdef CONFIG_HAVE_KERNEL_BZIP2 |
| 38 | #define HEAP_SIZE 0x400000 |
| 39 | #else |
| 40 | #define HEAP_SIZE 0x10000 |
| 41 | #endif |
| 42 | |
| 43 | #ifdef CONFIG_KERNEL_GZIP |
| 44 | #include "../../../../lib/decompress_inflate.c" |
| 45 | #endif |
| 46 | |
| 47 | #ifdef CONFIG_KERNEL_BZIP2 |
| 48 | #include "../../../../lib/decompress_bunzip2.c" |
| 49 | #endif |
| 50 | |
Heiko Carstens | 8e2872c | 2013-07-18 15:18:24 +0200 | [diff] [blame] | 51 | #ifdef CONFIG_KERNEL_LZ4 |
| 52 | #include "../../../../lib/decompress_unlz4.c" |
| 53 | #endif |
| 54 | |
Martin Schwidefsky | 1844c9b | 2010-02-26 22:37:53 +0100 | [diff] [blame] | 55 | #ifdef CONFIG_KERNEL_LZMA |
| 56 | #include "../../../../lib/decompress_unlzma.c" |
| 57 | #endif |
| 58 | |
Heiko Carstens | cdf5664 | 2010-05-26 23:27:12 +0200 | [diff] [blame] | 59 | #ifdef CONFIG_KERNEL_LZO |
| 60 | #include "../../../../lib/decompress_unlzo.c" |
| 61 | #endif |
| 62 | |
Heiko Carstens | d7b081a | 2011-03-15 17:08:32 +0100 | [diff] [blame] | 63 | #ifdef CONFIG_KERNEL_XZ |
| 64 | #include "../../../../lib/decompress_unxz.c" |
| 65 | #endif |
| 66 | |
Martin Schwidefsky | c4736d9 | 2011-10-30 15:17:11 +0100 | [diff] [blame] | 67 | static int puts(const char *s) |
Martin Schwidefsky | 1844c9b | 2010-02-26 22:37:53 +0100 | [diff] [blame] | 68 | { |
Heiko Carstens | d5ab7a3 | 2017-01-24 15:58:52 +0100 | [diff] [blame] | 69 | sclp_early_printk(s); |
Martin Schwidefsky | 1844c9b | 2010-02-26 22:37:53 +0100 | [diff] [blame] | 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | void *memset(void *s, int c, size_t n) |
| 74 | { |
| 75 | char *xs; |
| 76 | |
Heiko Carstens | 535c611 | 2012-08-14 13:20:20 +0200 | [diff] [blame] | 77 | xs = s; |
| 78 | while (n--) |
| 79 | *xs++ = c; |
Martin Schwidefsky | 1844c9b | 2010-02-26 22:37:53 +0100 | [diff] [blame] | 80 | return s; |
| 81 | } |
| 82 | |
Heiko Carstens | 535c611 | 2012-08-14 13:20:20 +0200 | [diff] [blame] | 83 | void *memcpy(void *dest, const void *src, size_t n) |
Martin Schwidefsky | 1844c9b | 2010-02-26 22:37:53 +0100 | [diff] [blame] | 84 | { |
Heiko Carstens | 535c611 | 2012-08-14 13:20:20 +0200 | [diff] [blame] | 85 | const char *s = src; |
| 86 | char *d = dest; |
| 87 | |
| 88 | while (n--) |
| 89 | *d++ = *s++; |
| 90 | return dest; |
Martin Schwidefsky | 1844c9b | 2010-02-26 22:37:53 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Heiko Carstens | 535c611 | 2012-08-14 13:20:20 +0200 | [diff] [blame] | 93 | void *memmove(void *dest, const void *src, size_t n) |
Martin Schwidefsky | 1844c9b | 2010-02-26 22:37:53 +0100 | [diff] [blame] | 94 | { |
Heiko Carstens | 535c611 | 2012-08-14 13:20:20 +0200 | [diff] [blame] | 95 | const char *s = src; |
| 96 | char *d = dest; |
Martin Schwidefsky | 1844c9b | 2010-02-26 22:37:53 +0100 | [diff] [blame] | 97 | |
Heiko Carstens | 535c611 | 2012-08-14 13:20:20 +0200 | [diff] [blame] | 98 | if (d <= s) { |
| 99 | while (n--) |
| 100 | *d++ = *s++; |
| 101 | } else { |
| 102 | d += n; |
| 103 | s += n; |
| 104 | while (n--) |
| 105 | *--d = *--s; |
| 106 | } |
| 107 | return dest; |
Martin Schwidefsky | 1844c9b | 2010-02-26 22:37:53 +0100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static void error(char *x) |
| 111 | { |
| 112 | unsigned long long psw = 0x000a0000deadbeefULL; |
| 113 | |
| 114 | puts("\n\n"); |
| 115 | puts(x); |
| 116 | puts("\n\n -- System halted"); |
| 117 | |
| 118 | asm volatile("lpsw %0" : : "Q" (psw)); |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * Safe guard the ipl parameter block against a memory area that will be |
| 123 | * overwritten. The validity check for the ipl parameter block is complex |
| 124 | * (see cio_get_iplinfo and ipl_save_parameters) but if the pointer to |
| 125 | * the ipl parameter block intersects with the passed memory area we can |
| 126 | * safely assume that we can read from that memory. In that case just copy |
| 127 | * the memory to IPL_PARMBLOCK_ORIGIN even if there is no ipl parameter |
| 128 | * block. |
| 129 | */ |
| 130 | static void check_ipl_parmblock(void *start, unsigned long size) |
| 131 | { |
| 132 | void *src, *dst; |
| 133 | |
| 134 | src = (void *)(unsigned long) S390_lowcore.ipl_parmblock_ptr; |
| 135 | if (src + PAGE_SIZE <= start || src >= start + size) |
| 136 | return; |
| 137 | dst = (void *) IPL_PARMBLOCK_ORIGIN; |
| 138 | memmove(dst, src, PAGE_SIZE); |
| 139 | S390_lowcore.ipl_parmblock_ptr = IPL_PARMBLOCK_ORIGIN; |
| 140 | } |
| 141 | |
| 142 | unsigned long decompress_kernel(void) |
| 143 | { |
| 144 | unsigned long output_addr; |
| 145 | unsigned char *output; |
| 146 | |
Martin Schwidefsky | a8c8d7c | 2011-02-17 13:13:57 +0100 | [diff] [blame] | 147 | output_addr = ((unsigned long) &_end + HEAP_SIZE + 4095UL) & -4096UL; |
| 148 | check_ipl_parmblock((void *) 0, output_addr + SZ__bss_start); |
Martin Schwidefsky | 06c0dd7 | 2010-03-24 11:49:57 +0100 | [diff] [blame] | 149 | memset(&_bss, 0, &_ebss - &_bss); |
Martin Schwidefsky | 1844c9b | 2010-02-26 22:37:53 +0100 | [diff] [blame] | 150 | free_mem_ptr = (unsigned long)&_end; |
| 151 | free_mem_end_ptr = free_mem_ptr + HEAP_SIZE; |
Martin Schwidefsky | a8c8d7c | 2011-02-17 13:13:57 +0100 | [diff] [blame] | 152 | output = (unsigned char *) output_addr; |
Martin Schwidefsky | 1844c9b | 2010-02-26 22:37:53 +0100 | [diff] [blame] | 153 | |
Martin Schwidefsky | 1844c9b | 2010-02-26 22:37:53 +0100 | [diff] [blame] | 154 | #ifdef CONFIG_BLK_DEV_INITRD |
| 155 | /* |
| 156 | * Move the initrd right behind the end of the decompressed |
| 157 | * kernel image. |
| 158 | */ |
| 159 | if (INITRD_START && INITRD_SIZE && |
| 160 | INITRD_START < (unsigned long) output + SZ__bss_start) { |
| 161 | check_ipl_parmblock(output + SZ__bss_start, |
| 162 | INITRD_START + INITRD_SIZE); |
| 163 | memmove(output + SZ__bss_start, |
| 164 | (void *) INITRD_START, INITRD_SIZE); |
| 165 | INITRD_START = (unsigned long) output + SZ__bss_start; |
| 166 | } |
| 167 | #endif |
| 168 | |
| 169 | puts("Uncompressing Linux... "); |
Yinghai Lu | 2d3862d | 2015-09-09 15:39:12 -0700 | [diff] [blame] | 170 | __decompress(input_data, input_len, NULL, NULL, output, 0, NULL, error); |
Martin Schwidefsky | 1844c9b | 2010-02-26 22:37:53 +0100 | [diff] [blame] | 171 | puts("Ok, booting the kernel.\n"); |
| 172 | return (unsigned long) output; |
| 173 | } |
| 174 | |