2 * Definitions and wrapper functions for kernel decompressor
4 * (C) 2017 Helge Deller <deller@gmx.de>
7 #include <linux/uaccess.h>
8 #include <asm/unaligned.h>
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
[];
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"
44 #ifdef CONFIG_KERNEL_BZIP2
45 #include "../../../../lib/decompress_bunzip2.c"
48 #ifdef CONFIG_KERNEL_LZ4
49 #include "../../../../lib/decompress_unlz4.c"
52 #ifdef CONFIG_KERNEL_LZMA
53 #include "../../../../lib/decompress_unlzma.c"
56 #ifdef CONFIG_KERNEL_LZO
57 #include "../../../../lib/decompress_unlzo.c"
60 #ifdef CONFIG_KERNEL_XZ
61 #include "../../../../lib/decompress_unxz.c"
64 void *memmove(void *dest
, const void *src
, size_t n
)
81 void *memset(void *s
, int c
, size_t count
)
90 void *memcpy(void *d
, const void *s
, size_t len
)
92 char *dest
= (char *)d
;
93 const char *source
= (const char *)s
;
100 size_t strlen(const char *s
)
104 for (sc
= s
; *sc
!= '\0'; ++sc
)
109 char *strchr(const char *s
, int c
)
119 int puts(const char *s
)
121 const char *nuline
= s
;
123 while ((nuline
= strchr(s
, '\n')) != NULL
) {
125 pdc_iodc_print(s
, nuline
- s
);
126 pdc_iodc_print("\r\n", 2);
130 pdc_iodc_print(s
, strlen(s
));
135 static int putchar(int c
)
145 void __noreturn
error(char *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";
158 int i
= sizeof(str
)-1;
162 str
[i
--] = hex
[num
& 0x0f];
173 int printf(const char *fmt
, ...)
190 print_hex(va_arg(args
, unsigned long));
197 /* helper functions for libgcc */
204 void *malloc(size_t size
)
206 return malloc_gzip(size
);
212 return free_gzip(ptr
);
216 static void flush_data_cache(char *start
, unsigned long length
)
218 char *end
= start
+ length
;
221 asm volatile("fdc 0(%0)" : : "r" (start
));
222 asm volatile("fic 0(%%sr0,%0)" : : "r" (start
));
224 } while (start
< end
);
225 asm volatile("fdc 0(%0)" : : "r" (end
));
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
)
236 unsigned long len
, len_all
;
239 parisc_narrow_firmware
= 0;
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
);
256 error("Output len too big.");
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
;
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
);
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
;