xref: /openbmc/linux/arch/sh/boot/compressed/misc.c (revision 040f43e0)
159f00296SPaul Mundt /*
259f00296SPaul Mundt  * arch/sh/boot/compressed/misc.c
359f00296SPaul Mundt  *
459f00296SPaul Mundt  * This is a collection of several routines from gzip-1.0.3
559f00296SPaul Mundt  * adapted for Linux.
659f00296SPaul Mundt  *
759f00296SPaul Mundt  * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
859f00296SPaul Mundt  *
959f00296SPaul Mundt  * Adapted for SH by Stuart Menefy, Aug 1999
1059f00296SPaul Mundt  *
1159f00296SPaul Mundt  * Modified to use standard LinuxSH BIOS by Greg Banks 7Jul2000
1259f00296SPaul Mundt  */
1359f00296SPaul Mundt 
1459f00296SPaul Mundt #include <asm/uaccess.h>
1559f00296SPaul Mundt #include <asm/addrspace.h>
1659f00296SPaul Mundt #include <asm/page.h>
1759f00296SPaul Mundt #include <asm/sh_bios.h>
1859f00296SPaul Mundt 
1959f00296SPaul Mundt /*
2059f00296SPaul Mundt  * gzip declarations
2159f00296SPaul Mundt  */
2259f00296SPaul Mundt 
2359f00296SPaul Mundt #define STATIC static
2459f00296SPaul Mundt 
2559f00296SPaul Mundt #undef memset
2659f00296SPaul Mundt #undef memcpy
2759f00296SPaul Mundt #define memzero(s, n)     memset ((s), 0, (n))
2859f00296SPaul Mundt 
2959f00296SPaul Mundt /* cache.c */
3059f00296SPaul Mundt #define CACHE_ENABLE      0
3159f00296SPaul Mundt #define CACHE_DISABLE     1
3259f00296SPaul Mundt int cache_control(unsigned int command);
3359f00296SPaul Mundt 
3459f00296SPaul Mundt extern char input_data[];
3559f00296SPaul Mundt extern int input_len;
3659f00296SPaul Mundt static unsigned char *output;
3759f00296SPaul Mundt 
3859f00296SPaul Mundt static void error(char *m);
3959f00296SPaul Mundt 
4059f00296SPaul Mundt int puts(const char *);
4159f00296SPaul Mundt 
4259f00296SPaul Mundt extern int _text;		/* Defined in vmlinux.lds.S */
4359f00296SPaul Mundt extern int _end;
4459f00296SPaul Mundt static unsigned long free_mem_ptr;
4559f00296SPaul Mundt static unsigned long free_mem_end_ptr;
4659f00296SPaul Mundt 
4759f00296SPaul Mundt #ifdef CONFIG_HAVE_KERNEL_BZIP2
4859f00296SPaul Mundt #define HEAP_SIZE	0x400000
4959f00296SPaul Mundt #else
5059f00296SPaul Mundt #define HEAP_SIZE	0x10000
5159f00296SPaul Mundt #endif
5259f00296SPaul Mundt 
5359f00296SPaul Mundt #ifdef CONFIG_KERNEL_GZIP
5459f00296SPaul Mundt #include "../../../../lib/decompress_inflate.c"
5559f00296SPaul Mundt #endif
5659f00296SPaul Mundt 
5759f00296SPaul Mundt #ifdef CONFIG_KERNEL_BZIP2
5859f00296SPaul Mundt #include "../../../../lib/decompress_bunzip2.c"
5959f00296SPaul Mundt #endif
6059f00296SPaul Mundt 
6159f00296SPaul Mundt #ifdef CONFIG_KERNEL_LZMA
6259f00296SPaul Mundt #include "../../../../lib/decompress_unlzma.c"
6359f00296SPaul Mundt #endif
6459f00296SPaul Mundt 
6559f00296SPaul Mundt #ifdef CONFIG_SH_STANDARD_BIOS
6659f00296SPaul Mundt size_t strlen(const char *s)
6759f00296SPaul Mundt {
6859f00296SPaul Mundt 	int i = 0;
6959f00296SPaul Mundt 
7059f00296SPaul Mundt 	while (*s++)
7159f00296SPaul Mundt 		i++;
7259f00296SPaul Mundt 	return i;
7359f00296SPaul Mundt }
7459f00296SPaul Mundt 
7559f00296SPaul Mundt int puts(const char *s)
7659f00296SPaul Mundt {
7759f00296SPaul Mundt 	int len = strlen(s);
7859f00296SPaul Mundt 	sh_bios_console_write(s, len);
7959f00296SPaul Mundt 	return len;
8059f00296SPaul Mundt }
8159f00296SPaul Mundt #else
8259f00296SPaul Mundt int puts(const char *s)
8359f00296SPaul Mundt {
8459f00296SPaul Mundt 	/* This should be updated to use the sh-sci routines */
8559f00296SPaul Mundt 	return 0;
8659f00296SPaul Mundt }
8759f00296SPaul Mundt #endif
8859f00296SPaul Mundt 
8959f00296SPaul Mundt void* memset(void* s, int c, size_t n)
9059f00296SPaul Mundt {
9159f00296SPaul Mundt 	int i;
9259f00296SPaul Mundt 	char *ss = (char*)s;
9359f00296SPaul Mundt 
9459f00296SPaul Mundt 	for (i=0;i<n;i++) ss[i] = c;
9559f00296SPaul Mundt 	return s;
9659f00296SPaul Mundt }
9759f00296SPaul Mundt 
9859f00296SPaul Mundt void* memcpy(void* __dest, __const void* __src,
9959f00296SPaul Mundt 			    size_t __n)
10059f00296SPaul Mundt {
10159f00296SPaul Mundt 	int i;
10259f00296SPaul Mundt 	char *d = (char *)__dest, *s = (char *)__src;
10359f00296SPaul Mundt 
10459f00296SPaul Mundt 	for (i=0;i<__n;i++) d[i] = s[i];
10559f00296SPaul Mundt 	return __dest;
10659f00296SPaul Mundt }
10759f00296SPaul Mundt 
10859f00296SPaul Mundt static void error(char *x)
10959f00296SPaul Mundt {
11059f00296SPaul Mundt 	puts("\n\n");
11159f00296SPaul Mundt 	puts(x);
11259f00296SPaul Mundt 	puts("\n\n -- System halted");
11359f00296SPaul Mundt 
11459f00296SPaul Mundt 	while(1);	/* Halt */
11559f00296SPaul Mundt }
11659f00296SPaul Mundt 
11759f00296SPaul Mundt #ifdef CONFIG_SUPERH64
11859f00296SPaul Mundt #define stackalign	8
11959f00296SPaul Mundt #else
12059f00296SPaul Mundt #define stackalign	4
12159f00296SPaul Mundt #endif
12259f00296SPaul Mundt 
12359f00296SPaul Mundt #define STACK_SIZE (4096)
12459f00296SPaul Mundt long __attribute__ ((aligned(stackalign))) user_stack[STACK_SIZE];
12559f00296SPaul Mundt long *stack_start = &user_stack[STACK_SIZE];
12659f00296SPaul Mundt 
12759f00296SPaul Mundt void decompress_kernel(void)
12859f00296SPaul Mundt {
12959f00296SPaul Mundt 	unsigned long output_addr;
13059f00296SPaul Mundt 
131040f43e0SPaul Mundt #ifdef CONFIG_SUPERH64
132040f43e0SPaul Mundt 	output_addr = (CONFIG_MEMORY_START + 0x2000);
133040f43e0SPaul Mundt #else
13459f00296SPaul Mundt 	output_addr = PHYSADDR((unsigned long)&_text+PAGE_SIZE);
13559f00296SPaul Mundt #ifdef CONFIG_29BIT
13659f00296SPaul Mundt 	output_addr |= P2SEG;
13759f00296SPaul Mundt #endif
138040f43e0SPaul Mundt #endif
13959f00296SPaul Mundt 
14059f00296SPaul Mundt 	output = (unsigned char *)output_addr;
14159f00296SPaul Mundt 	free_mem_ptr = (unsigned long)&_end;
14259f00296SPaul Mundt 	free_mem_end_ptr = free_mem_ptr + HEAP_SIZE;
14359f00296SPaul Mundt 
14459f00296SPaul Mundt 	puts("Uncompressing Linux... ");
14559f00296SPaul Mundt 	cache_control(CACHE_ENABLE);
14659f00296SPaul Mundt 	decompress(input_data, input_len, NULL, NULL, output, NULL, error);
14759f00296SPaul Mundt 	cache_control(CACHE_DISABLE);
14859f00296SPaul Mundt 	puts("Ok, booting the kernel.\n");
14959f00296SPaul Mundt }
150