xref: /openbmc/linux/arch/arm/boot/compressed/misc.c (revision f4284724)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * misc.c
4  *
5  * This is a collection of several routines from gzip-1.0.3
6  * adapted for Linux.
7  *
8  * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
9  *
10  * Modified for ARM Linux by Russell King
11  *
12  * Nicolas Pitre <nico@visuaide.com>  1999/04/14 :
13  *  For this code to run directly from Flash, all constant variables must
14  *  be marked with 'const' and all other variables initialized at run-time
15  *  only.  This way all non constant variables will end up in the bss segment,
16  *  which should point to addresses in RAM and cleared to 0 on start.
17  *  This allows for a much quicker boot time.
18  */
19 
20 unsigned int __machine_arch_type;
21 
22 #include <linux/compiler.h>	/* for inline */
23 #include <linux/types.h>
24 #include <linux/linkage.h>
25 #include "misc.h"
26 #include "misc-ep93xx.h"
27 
28 static void putstr(const char *ptr);
29 
30 #include CONFIG_UNCOMPRESS_INCLUDE
31 
32 #ifdef CONFIG_DEBUG_ICEDCC
33 
34 #if defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_V6K) || defined(CONFIG_CPU_V7)
35 
36 static void icedcc_putc(int ch)
37 {
38 	int status, i = 0x4000000;
39 
40 	do {
41 		if (--i < 0)
42 			return;
43 
44 		asm volatile ("mrc p14, 0, %0, c0, c1, 0" : "=r" (status));
45 	} while (status & (1 << 29));
46 
47 	asm("mcr p14, 0, %0, c0, c5, 0" : : "r" (ch));
48 }
49 
50 
51 #elif defined(CONFIG_CPU_XSCALE)
52 
53 static void icedcc_putc(int ch)
54 {
55 	int status, i = 0x4000000;
56 
57 	do {
58 		if (--i < 0)
59 			return;
60 
61 		asm volatile ("mrc p14, 0, %0, c14, c0, 0" : "=r" (status));
62 	} while (status & (1 << 28));
63 
64 	asm("mcr p14, 0, %0, c8, c0, 0" : : "r" (ch));
65 }
66 
67 #else
68 
69 static void icedcc_putc(int ch)
70 {
71 	int status, i = 0x4000000;
72 
73 	do {
74 		if (--i < 0)
75 			return;
76 
77 		asm volatile ("mrc p14, 0, %0, c0, c0, 0" : "=r" (status));
78 	} while (status & 2);
79 
80 	asm("mcr p14, 0, %0, c1, c0, 0" : : "r" (ch));
81 }
82 
83 #endif
84 
85 #define putc(ch)	icedcc_putc(ch)
86 #endif
87 
88 static void putstr(const char *ptr)
89 {
90 	char c;
91 
92 	while ((c = *ptr++) != '\0') {
93 		if (c == '\n')
94 			putc('\r');
95 		putc(c);
96 	}
97 
98 	flush();
99 }
100 
101 /*
102  * gzip declarations
103  */
104 extern char input_data[];
105 extern char input_data_end[];
106 
107 unsigned char *output_data;
108 
109 unsigned long free_mem_ptr;
110 unsigned long free_mem_end_ptr;
111 
112 #ifndef arch_error
113 #define arch_error(x)
114 #endif
115 
116 void error(char *x)
117 {
118 	arch_error(x);
119 
120 	putstr("\n\n");
121 	putstr(x);
122 	putstr("\n\n -- System halted");
123 
124 	while(1);	/* Halt */
125 }
126 
127 asmlinkage void __div0(void)
128 {
129 	error("Attempting division by 0!");
130 }
131 
132 extern int do_decompress(u8 *input, int len, u8 *output, void (*error)(char *x));
133 
134 
135 void
136 decompress_kernel(unsigned long output_start, unsigned long free_mem_ptr_p,
137 		unsigned long free_mem_ptr_end_p,
138 		int arch_id)
139 {
140 	int ret;
141 
142 	output_data		= (unsigned char *)output_start;
143 	free_mem_ptr		= free_mem_ptr_p;
144 	free_mem_end_ptr	= free_mem_ptr_end_p;
145 	__machine_arch_type	= arch_id;
146 
147 #ifdef CONFIG_ARCH_EP93XX
148 	ep93xx_decomp_setup();
149 #endif
150 	arch_decomp_setup();
151 
152 	putstr("Uncompressing Linux...");
153 	ret = do_decompress(input_data, input_data_end - input_data,
154 			    output_data, error);
155 	if (ret)
156 		error("decompressor returned an error");
157 	else
158 		putstr(" done, booting the kernel.\n");
159 }
160 
161 void fortify_panic(const char *name)
162 {
163 	error("detected buffer overflow");
164 }
165