1 /*
2  * Misc. bootloader code for many machines.
3  *
4  * Copyright 2001 MontaVista Software Inc.
5  * Author: Matt Porter <mporter@mvista.com> Derived from
6  * arch/ppc/boot/prep/misc.c
7  *
8  * Copyright (C) 2009 Lemote, Inc. & Institute of Computing Technology
9  * Author: Wu Zhangjin <wuzj@lemote.com>
10  *
11  * This program is free software; you can redistribute  it and/or modify it
12  * under  the terms of  the GNU General  Public License as published by the
13  * Free Software Foundation;  either version 2 of the  License, or (at your
14  * option) any later version.
15  */
16 
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 
20 #include <asm/addrspace.h>
21 
22 /* These two variables specify the free mem region
23  * that can be used for temporary malloc area
24  */
25 unsigned long free_mem_ptr;
26 unsigned long free_mem_end_ptr;
27 char *zimage_start;
28 
29 /* The linker tells us where the image is. */
30 extern unsigned char __image_begin, __image_end;
31 
32 /* debug interfaces  */
33 extern void puts(const char *s);
34 extern void puthex(unsigned long long val);
35 
36 void error(char *x)
37 {
38 	puts("\n\n");
39 	puts(x);
40 	puts("\n\n -- System halted");
41 
42 	while (1)
43 		;	/* Halt */
44 }
45 
46 /* activate the code for pre-boot environment */
47 #define STATIC static
48 
49 #ifdef CONFIG_KERNEL_GZIP
50 void *memcpy(void *dest, const void *src, size_t n)
51 {
52 	int i;
53 	const char *s = src;
54 	char *d = dest;
55 
56 	for (i = 0; i < n; i++)
57 		d[i] = s[i];
58 	return dest;
59 }
60 #include "../../../../lib/decompress_inflate.c"
61 #endif
62 
63 #ifdef CONFIG_KERNEL_BZIP2
64 void *memset(void *s, int c, size_t n)
65 {
66 	int i;
67 	char *ss = s;
68 
69 	for (i = 0; i < n; i++)
70 		ss[i] = c;
71 	return s;
72 }
73 #include "../../../../lib/decompress_bunzip2.c"
74 #endif
75 
76 #ifdef CONFIG_KERNEL_LZMA
77 #include "../../../../lib/decompress_unlzma.c"
78 #endif
79 
80 #ifdef CONFIG_KERNEL_LZO
81 #include "../../../../lib/decompress_unlzo.c"
82 #endif
83 
84 void decompress_kernel(unsigned long boot_heap_start)
85 {
86 	int zimage_size;
87 
88 	/*
89 	 * We link ourself to an arbitrary low address.  When we run, we
90 	 * relocate outself to that address.  __image_beign points to
91 	 * the part of the image where the zImage is. -- Tom
92 	 */
93 	zimage_start = (char *)(unsigned long)(&__image_begin);
94 	zimage_size = (unsigned long)(&__image_end) -
95 	    (unsigned long)(&__image_begin);
96 
97 	/*
98 	 * The zImage and initrd will be between start and _end, so they've
99 	 * already been moved once.  We're good to go now. -- Tom
100 	 */
101 	puts("zimage at:     ");
102 	puthex((unsigned long)zimage_start);
103 	puts(" ");
104 	puthex((unsigned long)(zimage_size + zimage_start));
105 	puts("\n");
106 
107 	/* this area are prepared for mallocing when decompressing */
108 	free_mem_ptr = boot_heap_start;
109 	free_mem_end_ptr = boot_heap_start + BOOT_HEAP_SIZE;
110 
111 	/* Display standard Linux/MIPS boot prompt for kernel args */
112 	puts("Uncompressing Linux at load address ");
113 	puthex(VMLINUX_LOAD_ADDRESS_ULL);
114 	puts("\n");
115 	/* Decompress the kernel with according algorithm */
116 	decompress(zimage_start, zimage_size, 0, 0,
117 		   (void *)VMLINUX_LOAD_ADDRESS_ULL, 0, error);
118 	/* FIXME: is there a need to flush cache here? */
119 	puts("Now, booting the kernel...\n");
120 }
121