1 /* MN10300 Miscellaneous helper routines for kernel decompressor
3 * Copyright (C) 2007 Matsushita Electric Industrial Co., Ltd.
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Modified by David Howells (dhowells@redhat.com)
6 * - Derived from arch/x86/boot/compressed/misc_32.c
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public Licence
10 * as published by the Free Software Foundation; either version
11 * 2 of the Licence, or (at your option) any later version.
13 #include <linux/compiler.h>
14 #include <asm/serial-regs.h>
17 #ifndef CONFIG_GDBSTUB_ON_TTYSx
18 /* display 'Uncompressing Linux... ' messages on ttyS0 or ttyS1 */
20 #define CYG_DEV_BASE 0xA6FB0000
22 #define CYG_DEV_BASE 0xA6FC0000
25 #define CYG_DEV_THR (*((volatile __u8*)(CYG_DEV_BASE + 0x00)))
26 #define CYG_DEV_MCR (*((volatile __u8*)(CYG_DEV_BASE + 0x10)))
27 #define SIO_MCR_DTR 0x01
28 #define SIO_MCR_RTS 0x02
29 #define CYG_DEV_LSR (*((volatile __u8*)(CYG_DEV_BASE + 0x14)))
30 #define SIO_LSR_THRE 0x20 /* transmitter holding register empty */
31 #define SIO_LSR_TEMT 0x40 /* transmitter register empty */
32 #define CYG_DEV_MSR (*((volatile __u8*)(CYG_DEV_BASE + 0x18)))
33 #define SIO_MSR_CTS 0x10 /* clear to send */
34 #define SIO_MSR_DSR 0x20 /* data set ready */
36 #define LSR_WAIT_FOR(STATE) \
37 do { while (!(CYG_DEV_LSR & SIO_LSR_##STATE)) {} } while (0)
38 #define FLOWCTL_QUERY(LINE) \
39 ({ CYG_DEV_MSR & SIO_MSR_##LINE; })
40 #define FLOWCTL_WAIT_FOR(LINE) \
41 do { while (!(CYG_DEV_MSR & SIO_MSR_##LINE)) {} } while (0)
42 #define FLOWCTL_CLEAR(LINE) \
43 do { CYG_DEV_MCR &= ~SIO_MCR_##LINE; } while (0)
44 #define FLOWCTL_SET(LINE) \
45 do { CYG_DEV_MCR |= SIO_MCR_##LINE; } while (0)
58 static inline void *memset(const void *s, int c, size_t n)
61 char *ss = (char *) s;
63 for (i = 0; i < n; i++)
68 #define memzero(s, n) memset((s), 0, (n))
70 static inline void *memcpy(void *__dest, const void *__src, size_t __n)
73 const char *s = __src;
76 for (i = 0; i < __n; i++)
81 typedef unsigned char uch;
82 typedef unsigned short ush;
83 typedef unsigned long ulg;
85 #define WSIZE 0x8000 /* Window size must be at least 32k, and a power of
88 static uch *inbuf; /* input buffer */
89 static uch window[WSIZE]; /* sliding window buffer */
91 static unsigned insize; /* valid bytes in inbuf */
92 static unsigned inptr; /* index of next byte to be processed in inbuf */
93 static unsigned outcnt; /* bytes in output buffer */
96 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
97 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
98 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
99 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
100 #define COMMENT 0x10 /* bit 4 set: file comment present */
101 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
102 #define RESERVED 0xC0 /* bit 6,7: reserved */
104 /* Diagnostic functions */
106 # define Assert(cond, msg) { if (!(cond)) error(msg); }
107 # define Trace(x) fprintf x
108 # define Tracev(x) { if (verbose) fprintf x ; }
109 # define Tracevv(x) { if (verbose > 1) fprintf x ; }
110 # define Tracec(c, x) { if (verbose && (c)) fprintf x ; }
111 # define Tracecv(c, x) { if (verbose > 1 && (c)) fprintf x ; }
113 # define Assert(cond, msg)
117 # define Tracec(c, x)
118 # define Tracecv(c, x)
121 static int fill_inbuf(void);
122 static void flush_window(void);
123 static void error(const char *) __attribute__((noreturn));
124 static void kputs(const char *);
126 static inline unsigned char get_byte(void)
128 unsigned char ch = inptr < insize ? inbuf[inptr++] : fill_inbuf();
132 hex[0] = ((ch & 0x0f) > 9) ?
133 ((ch & 0x0f) + 'A' - 0xa) : ((ch & 0x0f) + '0');
134 hex[1] = ((ch >> 4) > 9) ?
135 ((ch >> 4) + 'A' - 0xa) : ((ch >> 4) + '0');
143 * This is set up by the setup-routine at boot-time
145 #define EXT_MEM_K (*(unsigned short *)0x90002)
146 #ifndef STANDARD_MEMORY_BIOS_CALL
147 #define ALT_MEM_K (*(unsigned long *) 0x901e0)
149 #define SCREEN_INFO (*(struct screen_info *)0x90000)
151 static long bytes_out;
152 static uch *output_data;
153 static unsigned long output_ptr;
156 static unsigned long free_mem_ptr = (unsigned long) &end;
157 static unsigned long free_mem_end_ptr = (unsigned long) &end + 0x90000;
159 #define INPLACE_MOVE_ROUTINE 0x1000
160 #define LOW_BUFFER_START 0x2000
161 #define LOW_BUFFER_END 0x90000
162 #define LOW_BUFFER_SIZE (LOW_BUFFER_END - LOW_BUFFER_START)
163 #define HEAP_SIZE 0x3000
164 static int high_loaded;
165 static uch *high_buffer_start /* = (uch *)(((ulg)&end) + HEAP_SIZE)*/;
167 static char *vidmem = (char *)0xb8000;
168 static int lines, cols;
170 #include "../../../../lib/inflate.c"
172 static inline void scroll(void)
176 memcpy(vidmem, vidmem + cols * 2, (lines - 1) * cols * 2);
177 for (i = (lines - 1) * cols * 2; i < lines * cols * 2; i += 2)
181 static inline void kputchar(unsigned char ch)
183 #ifdef CONFIG_MN10300_UNIT_ASB2305
184 while (SC0STR & SC01STR_TBF)
189 while (SC0STR & SC01STR_TBF)
196 while (SC1STR & SC01STR_TBF)
201 while (SC1STR & SC01STR_TBF)
210 static void kputs(const char *s)
212 #ifdef CONFIG_DEBUG_DECOMPRESS_KERNEL
213 #ifndef CONFIG_GDBSTUB_ON_TTYSx
236 #endif /* CONFIG_DEBUG_DECOMPRESS_KERNEL */
239 /* ===========================================================================
240 * Fill the input buffer. This is called only when the buffer is empty
241 * and at least one byte is really needed.
243 static int fill_inbuf()
246 error("ran out of input data\n");
254 /* ===========================================================================
255 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
256 * (Used for the decompressed data only.)
258 static void flush_window_low(void)
260 ulg c = crc; /* temporary variable */
265 out = &output_data[output_ptr];
266 for (n = 0; n < outcnt; n++) {
268 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
271 bytes_out += (ulg)outcnt;
272 output_ptr += (ulg)outcnt;
276 static void flush_window_high(void)
278 ulg c = crc; /* temporary variable */
282 for (n = 0; n < outcnt; n++) {
283 ch = *output_data++ = *in++;
284 if ((ulg) output_data == LOW_BUFFER_END)
285 output_data = high_buffer_start;
286 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
289 bytes_out += (ulg)outcnt;
293 static void flush_window(void)
301 static void error(const char *x)
305 kputs("\n\n -- System halted");
311 #define STACK_SIZE (4096)
313 long user_stack[STACK_SIZE];
318 } stack_start = { &user_stack[STACK_SIZE], 0 };
320 void setup_normal_output_buffer(void)
322 #ifdef STANDARD_MEMORY_BIOS_CALL
323 if (EXT_MEM_K < 1024)
324 error("Less than 2MB of memory.\n");
326 if ((ALT_MEM_K > EXT_MEM_K ? ALT_MEM_K : EXT_MEM_K) < 1024)
327 error("Less than 2MB of memory.\n");
329 output_data = (char *) 0x100000; /* Points to 1M */
333 uch *low_buffer_start;
335 uch *high_buffer_start;
339 void setup_output_buffer_if_we_run_high(struct moveparams *mv)
341 high_buffer_start = (uch *)(((ulg) &end) + HEAP_SIZE);
342 #ifdef STANDARD_MEMORY_BIOS_CALL
343 if (EXT_MEM_K < (3 * 1024))
344 error("Less than 4MB of memory.\n");
346 if ((ALT_MEM_K > EXT_MEM_K ? ALT_MEM_K : EXT_MEM_K) < (3 * 1024))
347 error("Less than 4MB of memory.\n");
349 mv->low_buffer_start = output_data = (char *) LOW_BUFFER_START;
351 free_mem_end_ptr = (long) high_buffer_start;
352 if (0x100000 + LOW_BUFFER_SIZE > (ulg) high_buffer_start) {
353 high_buffer_start = (uch *)(0x100000 + LOW_BUFFER_SIZE);
354 mv->hcount = 0; /* say: we need not to move high_buffer */
358 mv->high_buffer_start = high_buffer_start;
361 void close_output_buffer_if_we_run_high(struct moveparams *mv)
363 mv->lcount = bytes_out;
364 if (bytes_out > LOW_BUFFER_SIZE) {
365 mv->lcount = LOW_BUFFER_SIZE;
367 mv->hcount = bytes_out - LOW_BUFFER_SIZE;
378 int decompress_kernel(struct moveparams *mv)
385 output_data = (char *) CONFIG_KERNEL_TEXT_ADDRESS;
388 kputs("Uncompressing Linux... ");
390 kputs("Ok, booting the kernel.\n");