1edd4a866SHeiko Carstens // SPDX-License-Identifier: GPL-2.0 2edd4a866SHeiko Carstens /* 3edd4a866SHeiko Carstens * Definitions and wrapper functions for kernel decompressor 4edd4a866SHeiko Carstens * 5edd4a866SHeiko Carstens * Copyright IBM Corp. 2010 6edd4a866SHeiko Carstens * 7edd4a866SHeiko Carstens * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com> 8edd4a866SHeiko Carstens */ 9edd4a866SHeiko Carstens 10edd4a866SHeiko Carstens #include <linux/kernel.h> 11edd4a866SHeiko Carstens #include <linux/string.h> 12edd4a866SHeiko Carstens #include <asm/page.h> 13edd4a866SHeiko Carstens #include "decompressor.h" 14edd4a866SHeiko Carstens 15edd4a866SHeiko Carstens /* 16edd4a866SHeiko Carstens * gzip declarations 17edd4a866SHeiko Carstens */ 18edd4a866SHeiko Carstens #define STATIC static 19edd4a866SHeiko Carstens 20edd4a866SHeiko Carstens #undef memset 21edd4a866SHeiko Carstens #undef memcpy 22edd4a866SHeiko Carstens #undef memmove 23edd4a866SHeiko Carstens #define memmove memmove 24edd4a866SHeiko Carstens #define memzero(s, n) memset((s), 0, (n)) 25edd4a866SHeiko Carstens 26f993d24aSMasahiro Yamada #if defined(CONFIG_KERNEL_BZIP2) 27edd4a866SHeiko Carstens #define BOOT_HEAP_SIZE 0x400000 28f993d24aSMasahiro Yamada #elif defined(CONFIG_KERNEL_ZSTD) 29edd4a866SHeiko Carstens #define BOOT_HEAP_SIZE 0x30000 30edd4a866SHeiko Carstens #else 31edd4a866SHeiko Carstens #define BOOT_HEAP_SIZE 0x10000 32edd4a866SHeiko Carstens #endif 33edd4a866SHeiko Carstens 34edd4a866SHeiko Carstens static unsigned long free_mem_ptr = (unsigned long) _end; 35edd4a866SHeiko Carstens static unsigned long free_mem_end_ptr = (unsigned long) _end + BOOT_HEAP_SIZE; 36edd4a866SHeiko Carstens 37edd4a866SHeiko Carstens #ifdef CONFIG_KERNEL_GZIP 38edd4a866SHeiko Carstens #include "../../../../lib/decompress_inflate.c" 39edd4a866SHeiko Carstens #endif 40edd4a866SHeiko Carstens 41edd4a866SHeiko Carstens #ifdef CONFIG_KERNEL_BZIP2 42edd4a866SHeiko Carstens #include "../../../../lib/decompress_bunzip2.c" 43edd4a866SHeiko Carstens #endif 44edd4a866SHeiko Carstens 45edd4a866SHeiko Carstens #ifdef CONFIG_KERNEL_LZ4 46edd4a866SHeiko Carstens #include "../../../../lib/decompress_unlz4.c" 47edd4a866SHeiko Carstens #endif 48edd4a866SHeiko Carstens 49edd4a866SHeiko Carstens #ifdef CONFIG_KERNEL_LZMA 50edd4a866SHeiko Carstens #include "../../../../lib/decompress_unlzma.c" 51edd4a866SHeiko Carstens #endif 52edd4a866SHeiko Carstens 53edd4a866SHeiko Carstens #ifdef CONFIG_KERNEL_LZO 54edd4a866SHeiko Carstens #include "../../../../lib/decompress_unlzo.c" 55edd4a866SHeiko Carstens #endif 56edd4a866SHeiko Carstens 57edd4a866SHeiko Carstens #ifdef CONFIG_KERNEL_XZ 58edd4a866SHeiko Carstens #include "../../../../lib/decompress_unxz.c" 59edd4a866SHeiko Carstens #endif 60edd4a866SHeiko Carstens 61edd4a866SHeiko Carstens #ifdef CONFIG_KERNEL_ZSTD 62edd4a866SHeiko Carstens #include "../../../../lib/decompress_unzstd.c" 63edd4a866SHeiko Carstens #endif 64edd4a866SHeiko Carstens 65edd4a866SHeiko Carstens #define decompress_offset ALIGN((unsigned long)_end + BOOT_HEAP_SIZE, PAGE_SIZE) 66edd4a866SHeiko Carstens 67edd4a866SHeiko Carstens unsigned long mem_safe_offset(void) 68edd4a866SHeiko Carstens { 69edd4a866SHeiko Carstens /* 70edd4a866SHeiko Carstens * due to 4MB HEAD_SIZE for bzip2 71edd4a866SHeiko Carstens * 'decompress_offset + vmlinux.image_size' could be larger than 72edd4a866SHeiko Carstens * kernel at final position + its .bss, so take the larger of two 73edd4a866SHeiko Carstens */ 74edd4a866SHeiko Carstens return max(decompress_offset + vmlinux.image_size, 75edd4a866SHeiko Carstens vmlinux.default_lma + vmlinux.image_size + vmlinux.bss_size); 76edd4a866SHeiko Carstens } 77edd4a866SHeiko Carstens 78edd4a866SHeiko Carstens void *decompress_kernel(void) 79edd4a866SHeiko Carstens { 80edd4a866SHeiko Carstens void *output = (void *)decompress_offset; 81edd4a866SHeiko Carstens 82edd4a866SHeiko Carstens __decompress(_compressed_start, _compressed_end - _compressed_start, 83*7ab41c2cSVasily Gorbik NULL, NULL, output, vmlinux.image_size, NULL, error); 84edd4a866SHeiko Carstens return output; 85edd4a866SHeiko Carstens } 86