blob: 281e922e5c652b0f51c924a975cd34a83f23c2e4 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
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>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008 */
9
Olivier Deprez0e641232021-09-23 10:07:05 +020010#define DISABLE_BRANCH_PROFILING
11
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000012#include <linux/types.h>
13#include <linux/kernel.h>
14#include <linux/string.h>
15#include <linux/libfdt.h>
16
17#include <asm/addrspace.h>
Olivier Deprez0e641232021-09-23 10:07:05 +020018#include <asm/unaligned.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000019
20/*
21 * These two variables specify the free mem region
22 * that can be used for temporary malloc area
23 */
24unsigned long free_mem_ptr;
25unsigned long free_mem_end_ptr;
26
27/* The linker tells us where the image is. */
28extern unsigned char __image_begin, __image_end;
29
30/* debug interfaces */
31#ifdef CONFIG_DEBUG_ZBOOT
32extern void puts(const char *s);
33extern void puthex(unsigned long long val);
34#else
35#define puts(s) do {} while (0)
36#define puthex(val) do {} while (0)
37#endif
38
39extern char __appended_dtb[];
40
41void error(char *x)
42{
43 puts("\n\n");
44 puts(x);
45 puts("\n\n -- System halted");
46
47 while (1)
48 ; /* Halt */
49}
50
51/* activate the code for pre-boot environment */
52#define STATIC static
53
54#ifdef CONFIG_KERNEL_GZIP
55#include "../../../../lib/decompress_inflate.c"
56#endif
57
58#ifdef CONFIG_KERNEL_BZIP2
59#include "../../../../lib/decompress_bunzip2.c"
60#endif
61
62#ifdef CONFIG_KERNEL_LZ4
63#include "../../../../lib/decompress_unlz4.c"
64#endif
65
66#ifdef CONFIG_KERNEL_LZMA
67#include "../../../../lib/decompress_unlzma.c"
68#endif
69
70#ifdef CONFIG_KERNEL_LZO
71#include "../../../../lib/decompress_unlzo.c"
72#endif
73
74#ifdef CONFIG_KERNEL_XZ
75#include "../../../../lib/decompress_unxz.c"
76#endif
77
78const unsigned long __stack_chk_guard = 0x000a0dff;
79
80void __stack_chk_fail(void)
81{
82 error("stack-protector: Kernel stack is corrupted\n");
83}
84
85void decompress_kernel(unsigned long boot_heap_start)
86{
87 unsigned long zimage_start, zimage_size;
88
89 zimage_start = (unsigned long)(&__image_begin);
90 zimage_size = (unsigned long)(&__image_end) -
91 (unsigned long)(&__image_begin);
92
93 puts("zimage at: ");
94 puthex(zimage_start);
95 puts(" ");
96 puthex(zimage_size + zimage_start);
97 puts("\n");
98
99 /* This area are prepared for mallocing when decompressing */
100 free_mem_ptr = boot_heap_start;
101 free_mem_end_ptr = boot_heap_start + BOOT_HEAP_SIZE;
102
103 /* Display standard Linux/MIPS boot prompt */
104 puts("Uncompressing Linux at load address ");
105 puthex(VMLINUX_LOAD_ADDRESS_ULL);
106 puts("\n");
107
108 /* Decompress the kernel with according algorithm */
109 __decompress((char *)zimage_start, zimage_size, 0, 0,
110 (void *)VMLINUX_LOAD_ADDRESS_ULL, 0, 0, error);
111
112 if (IS_ENABLED(CONFIG_MIPS_RAW_APPENDED_DTB) &&
113 fdt_magic((void *)&__appended_dtb) == FDT_MAGIC) {
114 unsigned int image_size, dtb_size;
115
116 dtb_size = fdt_totalsize((void *)&__appended_dtb);
117
118 /* last four bytes is always image size in little endian */
Olivier Deprez0e641232021-09-23 10:07:05 +0200119 image_size = get_unaligned_le32((void *)&__image_end - 4);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000120
121 /* copy dtb to where the booted kernel will expect it */
122 memcpy((void *)VMLINUX_LOAD_ADDRESS_ULL + image_size,
123 __appended_dtb, dtb_size);
124 }
125
126 /* FIXME: should we flush cache here? */
127 puts("Now, booting the kernel...\n");
128}