decompress.c 3.3 KB
Newer Older
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 7
 * Copyright (C) 2009 Lemote, Inc.
 * Author: Wu Zhangjin <wuzhangjin@gmail.com>
8 9
 */

10 11
#define DISABLE_BRANCH_PROFILING

12 13
#include <linux/types.h>
#include <linux/kernel.h>
14
#include <linux/string.h>
15
#include <linux/libfdt.h>
16 17

#include <asm/addrspace.h>
18
#include <asm/unaligned.h>
19
#include <asm-generic/vmlinux.lds.h>
20

21 22
/*
 * These two variables specify the free mem region
23 24 25 26 27 28 29 30 31
 * that can be used for temporary malloc area
 */
unsigned long free_mem_ptr;
unsigned long free_mem_end_ptr;

/* The linker tells us where the image is. */
extern unsigned char __image_begin, __image_end;

/* debug interfaces  */
32
#ifdef CONFIG_DEBUG_ZBOOT
33 34
extern void puts(const char *s);
extern void puthex(unsigned long long val);
35 36 37 38
#else
#define puts(s) do {} while (0)
#define puthex(val) do {} while (0)
#endif
39

40 41
extern char __appended_dtb[];

42 43 44 45 46 47 48 49 50 51 52 53 54
void error(char *x)
{
	puts("\n\n");
	puts(x);
	puts("\n\n -- System halted");

	while (1)
		;	/* Halt */
}

/* activate the code for pre-boot environment */
#define STATIC static

55
#ifdef CONFIG_KERNEL_GZIP
56 57 58 59 60 61 62
#include "../../../../lib/decompress_inflate.c"
#endif

#ifdef CONFIG_KERNEL_BZIP2
#include "../../../../lib/decompress_bunzip2.c"
#endif

63 64 65 66
#ifdef CONFIG_KERNEL_LZ4
#include "../../../../lib/decompress_unlz4.c"
#endif

67 68 69 70
#ifdef CONFIG_KERNEL_LZMA
#include "../../../../lib/decompress_unlzma.c"
#endif

71 72 73 74
#ifdef CONFIG_KERNEL_LZO
#include "../../../../lib/decompress_unlzo.c"
#endif

75 76 77 78
#ifdef CONFIG_KERNEL_XZ
#include "../../../../lib/decompress_unxz.c"
#endif

79 80 81 82
#ifdef CONFIG_KERNEL_ZSTD
#include "../../../../lib/decompress_unzstd.c"
#endif

83
const unsigned long __stack_chk_guard = 0x000a0dff;
84 85 86 87 88 89

void __stack_chk_fail(void)
{
	error("stack-protector: Kernel stack is corrupted\n");
}

90 91
void decompress_kernel(unsigned long boot_heap_start)
{
92 93 94
	unsigned long zimage_start, zimage_size;

	zimage_start = (unsigned long)(&__image_begin);
95 96 97 98
	zimage_size = (unsigned long)(&__image_end) -
	    (unsigned long)(&__image_begin);

	puts("zimage at:     ");
99
	puthex(zimage_start);
100
	puts(" ");
101
	puthex(zimage_size + zimage_start);
102 103
	puts("\n");

104
	/* This area are prepared for mallocing when decompressing */
105 106 107
	free_mem_ptr = boot_heap_start;
	free_mem_end_ptr = boot_heap_start + BOOT_HEAP_SIZE;

108
	/* Display standard Linux/MIPS boot prompt */
109 110 111
	puts("Uncompressing Linux at load address ");
	puthex(VMLINUX_LOAD_ADDRESS_ULL);
	puts("\n");
112

113
	/* Decompress the kernel with according algorithm */
114 115
	__decompress((char *)zimage_start, zimage_size, 0, 0,
		   (void *)VMLINUX_LOAD_ADDRESS_ULL, 0, 0, error);
116

117 118 119 120 121 122 123
	if (IS_ENABLED(CONFIG_MIPS_RAW_APPENDED_DTB) &&
	    fdt_magic((void *)&__appended_dtb) == FDT_MAGIC) {
		unsigned int image_size, dtb_size;

		dtb_size = fdt_totalsize((void *)&__appended_dtb);

		/* last four bytes is always image size in little endian */
124
		image_size = get_unaligned_le32((void *)&__image_end - 4);
125

126 127 128 129 130 131 132
		/* The device tree's address must be properly aligned  */
		image_size = ALIGN(image_size, STRUCT_ALIGNMENT);

		puts("Copy device tree to address  ");
		puthex(VMLINUX_LOAD_ADDRESS_ULL + image_size);
		puts("\n");

133 134 135 136 137
		/* copy dtb to where the booted kernel will expect it */
		memcpy((void *)VMLINUX_LOAD_ADDRESS_ULL + image_size,
		       __appended_dtb, dtb_size);
	}

138
	/* FIXME: should we flush cache here? */
139 140
	puts("Now, booting the kernel...\n");
}