1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * misc.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * This is a collection of several routines from gzip-1.0.3
61da177e4SLinus Torvalds * adapted for Linux.
71da177e4SLinus Torvalds *
81da177e4SLinus Torvalds * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
91da177e4SLinus Torvalds *
101da177e4SLinus Torvalds * Modified for ARM Linux by Russell King
111da177e4SLinus Torvalds *
121da177e4SLinus Torvalds * Nicolas Pitre <nico@visuaide.com> 1999/04/14 :
131da177e4SLinus Torvalds * For this code to run directly from Flash, all constant variables must
141da177e4SLinus Torvalds * be marked with 'const' and all other variables initialized at run-time
151da177e4SLinus Torvalds * only. This way all non constant variables will end up in the bss segment,
161da177e4SLinus Torvalds * which should point to addresses in RAM and cleared to 0 on start.
171da177e4SLinus Torvalds * This allows for a much quicker boot time.
181da177e4SLinus Torvalds */
191da177e4SLinus Torvalds
201da177e4SLinus Torvalds unsigned int __machine_arch_type;
211da177e4SLinus Torvalds
22aa0d3bb7SRusty Russell #include <linux/compiler.h> /* for inline */
23df4879faSNicolas Pitre #include <linux/types.h>
24e7db7b42SAlbin Tonnerre #include <linux/linkage.h>
255f8d561fSRussell King #include "misc.h"
26*32844a8eSBart Van Assche #ifdef CONFIG_ARCH_EP93XX
270361c7e5SArnd Bergmann #include "misc-ep93xx.h"
28*32844a8eSBart Van Assche #endif
29a081568dSRussell King
30a081568dSRussell King static void putstr(const char *ptr);
31a081568dSRussell King
32615967b0SShawn Guo #include CONFIG_UNCOMPRESS_INCLUDE
331da177e4SLinus Torvalds
341da177e4SLinus Torvalds #ifdef CONFIG_DEBUG_ICEDCC
357d95ded9STony Lindgren
36dfad549dSStephen Boyd #if defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_V6K) || defined(CONFIG_CPU_V7)
377d95ded9STony Lindgren
icedcc_putc(int ch)387d95ded9STony Lindgren static void icedcc_putc(int ch)
397d95ded9STony Lindgren {
407d95ded9STony Lindgren int status, i = 0x4000000;
417d95ded9STony Lindgren
427d95ded9STony Lindgren do {
437d95ded9STony Lindgren if (--i < 0)
447d95ded9STony Lindgren return;
457d95ded9STony Lindgren
467d95ded9STony Lindgren asm volatile ("mrc p14, 0, %0, c0, c1, 0" : "=r" (status));
477d95ded9STony Lindgren } while (status & (1 << 29));
487d95ded9STony Lindgren
497d95ded9STony Lindgren asm("mcr p14, 0, %0, c0, c5, 0" : : "r" (ch));
507d95ded9STony Lindgren }
51200b7a8dSTony Lindgren
52200b7a8dSTony Lindgren
53c633c3cfSJean-Christop PLAGNIOL-VILLARD #elif defined(CONFIG_CPU_XSCALE)
54c633c3cfSJean-Christop PLAGNIOL-VILLARD
icedcc_putc(int ch)55c633c3cfSJean-Christop PLAGNIOL-VILLARD static void icedcc_putc(int ch)
56c633c3cfSJean-Christop PLAGNIOL-VILLARD {
57c633c3cfSJean-Christop PLAGNIOL-VILLARD int status, i = 0x4000000;
58c633c3cfSJean-Christop PLAGNIOL-VILLARD
59c633c3cfSJean-Christop PLAGNIOL-VILLARD do {
60c633c3cfSJean-Christop PLAGNIOL-VILLARD if (--i < 0)
61c633c3cfSJean-Christop PLAGNIOL-VILLARD return;
62c633c3cfSJean-Christop PLAGNIOL-VILLARD
63c633c3cfSJean-Christop PLAGNIOL-VILLARD asm volatile ("mrc p14, 0, %0, c14, c0, 0" : "=r" (status));
64c633c3cfSJean-Christop PLAGNIOL-VILLARD } while (status & (1 << 28));
65c633c3cfSJean-Christop PLAGNIOL-VILLARD
66c633c3cfSJean-Christop PLAGNIOL-VILLARD asm("mcr p14, 0, %0, c8, c0, 0" : : "r" (ch));
67c633c3cfSJean-Christop PLAGNIOL-VILLARD }
687d95ded9STony Lindgren
697d95ded9STony Lindgren #else
707d95ded9STony Lindgren
icedcc_putc(int ch)71de4533a0SRussell King static void icedcc_putc(int ch)
72de4533a0SRussell King {
73de4533a0SRussell King int status, i = 0x4000000;
74de4533a0SRussell King
75de4533a0SRussell King do {
76de4533a0SRussell King if (--i < 0)
77de4533a0SRussell King return;
78de4533a0SRussell King
79b2556da5SUwe Zeisberger asm volatile ("mrc p14, 0, %0, c0, c0, 0" : "=r" (status));
80de4533a0SRussell King } while (status & 2);
81de4533a0SRussell King
82b2556da5SUwe Zeisberger asm("mcr p14, 0, %0, c1, c0, 0" : : "r" (ch));
83de4533a0SRussell King }
84de4533a0SRussell King
857d95ded9STony Lindgren #endif
867d95ded9STony Lindgren
87a081568dSRussell King #define putc(ch) icedcc_putc(ch)
88a081568dSRussell King #endif
891da177e4SLinus Torvalds
putstr(const char * ptr)90a081568dSRussell King static void putstr(const char *ptr)
911da177e4SLinus Torvalds {
92a081568dSRussell King char c;
93a081568dSRussell King
94a081568dSRussell King while ((c = *ptr++) != '\0') {
95a081568dSRussell King if (c == '\n')
96a081568dSRussell King putc('\r');
97a081568dSRussell King putc(c);
981da177e4SLinus Torvalds }
99a081568dSRussell King
100a081568dSRussell King flush();
1011da177e4SLinus Torvalds }
1021da177e4SLinus Torvalds
1031da177e4SLinus Torvalds /*
104e40f1e9fSNicolas Pitre * gzip declarations
1051da177e4SLinus Torvalds */
1065de813b6SRussell King unsigned char *output_data;
1071da177e4SLinus Torvalds
1085de813b6SRussell King unsigned long free_mem_ptr;
1095de813b6SRussell King unsigned long free_mem_end_ptr;
1101da177e4SLinus Torvalds
111f8c905d3SBen Dooks #ifndef arch_error
112f8c905d3SBen Dooks #define arch_error(x)
113f8c905d3SBen Dooks #endif
114f8c905d3SBen Dooks
error(char * x)1155de813b6SRussell King void error(char *x)
1161da177e4SLinus Torvalds {
117f8c905d3SBen Dooks arch_error(x);
118f8c905d3SBen Dooks
1191da177e4SLinus Torvalds putstr("\n\n");
1201da177e4SLinus Torvalds putstr(x);
1211da177e4SLinus Torvalds putstr("\n\n -- System halted");
1221da177e4SLinus Torvalds
1231da177e4SLinus Torvalds while(1); /* Halt */
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds
__div0(void)126e7db7b42SAlbin Tonnerre asmlinkage void __div0(void)
127e7db7b42SAlbin Tonnerre {
128e7db7b42SAlbin Tonnerre error("Attempting division by 0!");
129e7db7b42SAlbin Tonnerre }
130e7db7b42SAlbin Tonnerre
131e40f1e9fSNicolas Pitre void
decompress_kernel(unsigned long output_start,unsigned long free_mem_ptr_p,unsigned long free_mem_ptr_end_p,int arch_id)132e7db7b42SAlbin Tonnerre decompress_kernel(unsigned long output_start, unsigned long free_mem_ptr_p,
133e7db7b42SAlbin Tonnerre unsigned long free_mem_ptr_end_p,
1341da177e4SLinus Torvalds int arch_id)
1351da177e4SLinus Torvalds {
136ccc1c7c6SNicolas Pitre int ret;
137e7db7b42SAlbin Tonnerre
138e7db7b42SAlbin Tonnerre output_data = (unsigned char *)output_start;
1391da177e4SLinus Torvalds free_mem_ptr = free_mem_ptr_p;
1402d6ffccaSThomas Petazzoni free_mem_end_ptr = free_mem_ptr_end_p;
1411da177e4SLinus Torvalds __machine_arch_type = arch_id;
1421da177e4SLinus Torvalds
1430361c7e5SArnd Bergmann #ifdef CONFIG_ARCH_EP93XX
1440361c7e5SArnd Bergmann ep93xx_decomp_setup();
1450361c7e5SArnd Bergmann #endif
1461da177e4SLinus Torvalds arch_decomp_setup();
1471da177e4SLinus Torvalds
1481da177e4SLinus Torvalds putstr("Uncompressing Linux...");
149ccc1c7c6SNicolas Pitre ret = do_decompress(input_data, input_data_end - input_data,
1505de813b6SRussell King output_data, error);
151ccc1c7c6SNicolas Pitre if (ret)
152ccc1c7c6SNicolas Pitre error("decompressor returned an error");
153ccc1c7c6SNicolas Pitre else
1541da177e4SLinus Torvalds putstr(" done, booting the kernel.\n");
1551da177e4SLinus Torvalds }
156ee333554SJinbum Park
fortify_panic(const char * name)157ee333554SJinbum Park void fortify_panic(const char *name)
158ee333554SJinbum Park {
159ee333554SJinbum Park error("detected buffer overflow");
160ee333554SJinbum Park }
161