Linux 4.18.10
[linux/fpc-iii.git] / arch / parisc / boot / compressed / misc.c
blobf57118e1f6b4265257799ae2cf8ea356077e20b9
1 /*
2 * Definitions and wrapper functions for kernel decompressor
4 * (C) 2017 Helge Deller <deller@gmx.de>
5 */
7 #include <linux/uaccess.h>
8 #include <asm/unaligned.h>
9 #include <asm/page.h>
10 #include "sizes.h"
13 * gzip declarations
15 #define STATIC static
17 #undef memmove
18 #define memmove memmove
19 #define memzero(s, n) memset((s), 0, (n))
21 #define malloc malloc_gzip
22 #define free free_gzip
24 /* Symbols defined by linker scripts */
25 extern char input_data[];
26 extern int input_len;
27 /* output_len is inserted by the linker possibly at an unaligned address */
28 extern __le32 output_len __aligned(1);
29 extern char _text, _end;
30 extern char _bss, _ebss;
31 extern char _startcode_end;
32 extern void startup_continue(void *entry, unsigned long cmdline,
33 unsigned long rd_start, unsigned long rd_end) __noreturn;
35 void error(char *m) __noreturn;
37 static unsigned long free_mem_ptr;
38 static unsigned long free_mem_end_ptr;
40 #ifdef CONFIG_KERNEL_GZIP
41 #include "../../../../lib/decompress_inflate.c"
42 #endif
44 #ifdef CONFIG_KERNEL_BZIP2
45 #include "../../../../lib/decompress_bunzip2.c"
46 #endif
48 #ifdef CONFIG_KERNEL_LZ4
49 #include "../../../../lib/decompress_unlz4.c"
50 #endif
52 #ifdef CONFIG_KERNEL_LZMA
53 #include "../../../../lib/decompress_unlzma.c"
54 #endif
56 #ifdef CONFIG_KERNEL_LZO
57 #include "../../../../lib/decompress_unlzo.c"
58 #endif
60 #ifdef CONFIG_KERNEL_XZ
61 #include "../../../../lib/decompress_unxz.c"
62 #endif
64 void *memmove(void *dest, const void *src, size_t n)
66 const char *s = src;
67 char *d = dest;
69 if (d <= s) {
70 while (n--)
71 *d++ = *s++;
72 } else {
73 d += n;
74 s += n;
75 while (n--)
76 *--d = *--s;
78 return dest;
81 void *memset(void *s, int c, size_t count)
83 char *xs = (char *)s;
85 while (count--)
86 *xs++ = c;
87 return s;
90 void *memcpy(void *d, const void *s, size_t len)
92 char *dest = (char *)d;
93 const char *source = (const char *)s;
95 while (len--)
96 *dest++ = *source++;
97 return d;
100 size_t strlen(const char *s)
102 const char *sc;
104 for (sc = s; *sc != '\0'; ++sc)
106 return sc - s;
109 char *strchr(const char *s, int c)
111 while (*s) {
112 if (*s == (char)c)
113 return (char *)s;
114 ++s;
116 return NULL;
119 int puts(const char *s)
121 const char *nuline = s;
123 while ((nuline = strchr(s, '\n')) != NULL) {
124 if (nuline != s)
125 pdc_iodc_print(s, nuline - s);
126 pdc_iodc_print("\r\n", 2);
127 s = nuline + 1;
129 if (*s != '\0')
130 pdc_iodc_print(s, strlen(s));
132 return 0;
135 static int putchar(int c)
137 char buf[2];
139 buf[0] = c;
140 buf[1] = '\0';
141 puts(buf);
142 return c;
145 void __noreturn error(char *x)
147 puts("\n\n");
148 puts(x);
149 puts("\n\n -- System halted");
150 while (1) /* wait forever */
154 static int print_hex(unsigned long num)
156 const char hex[] = "0123456789abcdef";
157 char str[40];
158 int i = sizeof(str)-1;
160 str[i--] = '\0';
161 do {
162 str[i--] = hex[num & 0x0f];
163 num >>= 4;
164 } while (num);
166 str[i--] = 'x';
167 str[i] = '0';
168 puts(&str[i]);
170 return 0;
173 int printf(const char *fmt, ...)
175 va_list args;
176 int i = 0;
178 va_start(args, fmt);
180 while (fmt[i]) {
181 if (fmt[i] != '%') {
182 put:
183 putchar(fmt[i++]);
184 continue;
187 if (fmt[++i] == '%')
188 goto put;
189 ++i;
190 print_hex(va_arg(args, unsigned long));
193 va_end(args);
194 return 0;
197 /* helper functions for libgcc */
198 void abort(void)
200 error("aborted.");
203 #undef malloc
204 void *malloc(size_t size)
206 return malloc_gzip(size);
209 #undef free
210 void free(void *ptr)
212 return free_gzip(ptr);
216 static void flush_data_cache(char *start, unsigned long length)
218 char *end = start + length;
220 do {
221 asm volatile("fdc 0(%0)" : : "r" (start));
222 asm volatile("fic 0(%%sr0,%0)" : : "r" (start));
223 start += 16;
224 } while (start < end);
225 asm volatile("fdc 0(%0)" : : "r" (end));
227 asm ("sync");
230 unsigned long decompress_kernel(unsigned int started_wide,
231 unsigned int command_line,
232 const unsigned int rd_start,
233 const unsigned int rd_end)
235 char *output;
236 unsigned long len, len_all;
238 #ifdef CONFIG_64BIT
239 parisc_narrow_firmware = 0;
240 #endif
242 set_firmware_width_unlocked();
244 putchar('U'); /* if you get this p and no more, string storage */
245 /* in $GLOBAL$ is wrong or %dp is wrong */
246 puts("ncompressing ...\n");
248 output = (char *) KERNEL_BINARY_TEXT_START;
249 len_all = __pa(SZ_end) - __pa(SZparisc_kernel_start);
251 if ((unsigned long) &_startcode_end > (unsigned long) output)
252 error("Bootcode overlaps kernel code");
254 len = get_unaligned_le32(&output_len);
255 if (len > len_all)
256 error("Output len too big.");
257 else
258 memset(&output[len], 0, len_all - len);
261 * Initialize free_mem_ptr and free_mem_end_ptr.
263 free_mem_ptr = (unsigned long) &_ebss;
264 free_mem_ptr += 2*1024*1024; /* leave 2 MB for stack */
266 /* Limit memory for bootoader to 1GB */
267 #define ARTIFICIAL_LIMIT (1*1024*1024*1024)
268 free_mem_end_ptr = PAGE0->imm_max_mem;
269 if (free_mem_end_ptr > ARTIFICIAL_LIMIT)
270 free_mem_end_ptr = ARTIFICIAL_LIMIT;
272 #ifdef CONFIG_BLK_DEV_INITRD
273 /* if we have ramdisk this is at end of memory */
274 if (rd_start && rd_start < free_mem_end_ptr)
275 free_mem_end_ptr = rd_start;
276 #endif
278 #ifdef DEBUG
279 printf("startcode_end = %x\n", &_startcode_end);
280 printf("commandline = %x\n", command_line);
281 printf("rd_start = %x\n", rd_start);
282 printf("rd_end = %x\n", rd_end);
284 printf("free_ptr = %x\n", free_mem_ptr);
285 printf("free_ptr_end = %x\n", free_mem_end_ptr);
287 printf("input_data = %x\n", input_data);
288 printf("input_len = %x\n", input_len);
289 printf("output = %x\n", output);
290 printf("output_len = %x\n", len);
291 printf("output_max = %x\n", len_all);
292 #endif
294 __decompress(input_data, input_len, NULL, NULL,
295 output, 0, NULL, error);
297 flush_data_cache(output, len);
299 printf("Booting kernel ...\n\n");
301 return (unsigned long) output;