1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Author: Huacai Chen <chenhuacai@loongson.cn>
4 * Copyright (C) 2020 Loongson Technology Co., Ltd.
5 */
6
7#include <linux/types.h>
8#include <linux/kernel.h>
9#include <linux/string.h>
10#include <linux/libfdt.h>
11
12#include <asm/addrspace.h>
13
14/*
15 * These two variables specify the free mem region
16 * that can be used for temporary malloc area
17 */
18unsigned long free_mem_ptr;
19unsigned long free_mem_end_ptr;
20
21/* The linker tells us where the image is. */
22extern unsigned char __image_begin, __image_end;
23
24/* debug interfaces  */
25#ifdef CONFIG_DEBUG_ZBOOT
26extern void puts(const char *s);
27extern void puthex(unsigned long long val);
28#else
29#define puts(s) do {} while (0)
30#define puthex(val) do {} while (0)
31#endif
32
33void error(char *x)
34{
35	puts("\n\n");
36	puts(x);
37	puts("\n\n -- System halted");
38
39	while (1)
40		;	/* Halt */
41}
42
43/* activate the code for pre-boot environment */
44#define STATIC static
45
46#ifdef CONFIG_KERNEL_GZIP
47#include "../../../../lib/decompress_inflate.c"
48#endif
49
50#ifdef CONFIG_KERNEL_BZIP2
51#include "../../../../lib/decompress_bunzip2.c"
52#endif
53
54#ifdef CONFIG_KERNEL_LZ4
55#include "../../../../lib/decompress_unlz4.c"
56#endif
57
58#ifdef CONFIG_KERNEL_LZMA
59#include "../../../../lib/decompress_unlzma.c"
60#endif
61
62#ifdef CONFIG_KERNEL_LZO
63#include "../../../../lib/decompress_unlzo.c"
64#endif
65
66#ifdef CONFIG_KERNEL_XZ
67#include "../../../../lib/decompress_unxz.c"
68#endif
69
70#ifdef CONFIG_KERNEL_ZSTD
71#include "../../../../lib/decompress_unzstd.c"
72#endif
73
74const unsigned long __stack_chk_guard = 0x000a0dff;
75
76void __stack_chk_fail(void)
77{
78	error("stack-protector: Kernel stack is corrupted\n");
79}
80
81void decompress_kernel(unsigned long boot_heap_start, long kdump_reloc_offset)
82{
83	unsigned long zimage_start, zimage_size;
84
85	zimage_start = (unsigned long)(&__image_begin);
86	zimage_size = (unsigned long)(&__image_end) -
87	    (unsigned long)(&__image_begin);
88
89	puts("zimage at:     ");
90	puthex(zimage_start);
91	puts(" ");
92	puthex(zimage_size + zimage_start);
93	puts("\n");
94
95	/* This area are prepared for mallocing when decompressing */
96	free_mem_ptr = boot_heap_start;
97	free_mem_end_ptr = boot_heap_start + BOOT_HEAP_SIZE;
98
99	/* Display standard Linux/LoongArch boot prompt */
100	puts("Uncompressing Linux at load address ");
101	puthex(VMLINUX_LOAD_ADDRESS_ULL);
102	puts("\n");
103
104	/* Decompress the kernel with according algorithm */
105	__decompress((char *)zimage_start, zimage_size, 0, 0,
106		   (void *)VMLINUX_LOAD_ADDRESS_ULL + kdump_reloc_offset, 0, 0, error);
107
108	puts("Now, booting the kernel...\n");
109}
110