xref: /linux/arch/mips/boot/compressed/decompress.c (revision a460513ed4b6994bfeb7bd86f72853140bc1ac12)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright 2001 MontaVista Software Inc.
4  * Author: Matt Porter <mporter@mvista.com>
5  *
6  * Copyright (C) 2009 Lemote, Inc.
7  * Author: Wu Zhangjin <wuzhangjin@gmail.com>
8  */
9 
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/string.h>
13 #include <linux/libfdt.h>
14 
15 #include <asm/addrspace.h>
16 #include <asm/unaligned.h>
17 #include <asm-generic/vmlinux.lds.h>
18 
19 /*
20  * These two variables specify the free mem region
21  * that can be used for temporary malloc area
22  */
23 unsigned long free_mem_ptr;
24 unsigned long free_mem_end_ptr;
25 
26 /* The linker tells us where the image is. */
27 extern unsigned char __image_begin, __image_end;
28 
29 /* debug interfaces  */
30 #ifdef CONFIG_DEBUG_ZBOOT
31 extern void puts(const char *s);
32 extern void puthex(unsigned long long val);
33 #else
34 #define puts(s) do {} while (0)
35 #define puthex(val) do {} while (0)
36 #endif
37 
38 extern char __appended_dtb[];
39 
40 void error(char *x)
41 {
42 	puts("\n\n");
43 	puts(x);
44 	puts("\n\n -- System halted");
45 
46 	while (1)
47 		;	/* Halt */
48 }
49 
50 /* activate the code for pre-boot environment */
51 #define STATIC static
52 
53 #ifdef CONFIG_KERNEL_GZIP
54 #include "../../../../lib/decompress_inflate.c"
55 #endif
56 
57 #ifdef CONFIG_KERNEL_BZIP2
58 #include "../../../../lib/decompress_bunzip2.c"
59 #endif
60 
61 #ifdef CONFIG_KERNEL_LZ4
62 #include "../../../../lib/decompress_unlz4.c"
63 #endif
64 
65 #ifdef CONFIG_KERNEL_LZMA
66 #include "../../../../lib/decompress_unlzma.c"
67 #endif
68 
69 #ifdef CONFIG_KERNEL_LZO
70 #include "../../../../lib/decompress_unlzo.c"
71 #endif
72 
73 #ifdef CONFIG_KERNEL_XZ
74 #include "../../../../lib/decompress_unxz.c"
75 #endif
76 
77 #ifdef CONFIG_KERNEL_ZSTD
78 #include "../../../../lib/decompress_unzstd.c"
79 #endif
80 
81 const unsigned long __stack_chk_guard = 0x000a0dff;
82 
83 void __stack_chk_fail(void)
84 {
85 	error("stack-protector: Kernel stack is corrupted\n");
86 }
87 
88 void decompress_kernel(unsigned long boot_heap_start)
89 {
90 	unsigned long zimage_start, zimage_size;
91 
92 	zimage_start = (unsigned long)(&__image_begin);
93 	zimage_size = (unsigned long)(&__image_end) -
94 	    (unsigned long)(&__image_begin);
95 
96 	puts("zimage at:     ");
97 	puthex(zimage_start);
98 	puts(" ");
99 	puthex(zimage_size + zimage_start);
100 	puts("\n");
101 
102 	/* This area are prepared for mallocing when decompressing */
103 	free_mem_ptr = boot_heap_start;
104 	free_mem_end_ptr = boot_heap_start + BOOT_HEAP_SIZE;
105 
106 	/* Display standard Linux/MIPS boot prompt */
107 	puts("Uncompressing Linux at load address ");
108 	puthex(VMLINUX_LOAD_ADDRESS_ULL);
109 	puts("\n");
110 
111 	/* Decompress the kernel with according algorithm */
112 	__decompress((char *)zimage_start, zimage_size, 0, 0,
113 		   (void *)VMLINUX_LOAD_ADDRESS_ULL, 0, 0, error);
114 
115 	if (IS_ENABLED(CONFIG_MIPS_RAW_APPENDED_DTB) &&
116 	    fdt_magic((void *)&__appended_dtb) == FDT_MAGIC) {
117 		unsigned int image_size, dtb_size;
118 
119 		dtb_size = fdt_totalsize((void *)&__appended_dtb);
120 
121 		/* last four bytes is always image size in little endian */
122 		image_size = get_unaligned_le32((void *)&__image_end - 4);
123 
124 		/* The device tree's address must be properly aligned  */
125 		image_size = ALIGN(image_size, STRUCT_ALIGNMENT);
126 
127 		puts("Copy device tree to address  ");
128 		puthex(VMLINUX_LOAD_ADDRESS_ULL + image_size);
129 		puts("\n");
130 
131 		/* copy dtb to where the booted kernel will expect it */
132 		memcpy((void *)VMLINUX_LOAD_ADDRESS_ULL + image_size,
133 		       __appended_dtb, dtb_size);
134 	}
135 
136 	/* FIXME: should we flush cache here? */
137 	puts("Now, booting the kernel...\n");
138 }
139