2 * Initialization and support routines for self-booting
5 * Copyright 2004, Broadcom Corporation
8 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
9 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
10 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
11 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
29 void c_main(unsigned long ra
);
32 static chipcregs_t
*cc
;
34 static struct sflash
*sflash
;
36 extern char text_start
[], text_end
[];
37 extern char data_start
[], data_end
[];
38 extern char bss_start
[], bss_end
[];
40 #define INBUFSIZ 4096 /* Buffer size */
41 #define WSIZE 0x8000 /* window size--must be a power of two, and */
42 /* at least 32K for zip's deflate method */
44 static uchar
*inbuf
; /* input buffer */
45 static ulong insize
; /* valid bytes in inbuf */
46 static ulong inptr
; /* index of next byte to be processed in inbuf */
48 static uchar
*outbuf
; /* output buffer */
49 static ulong bytes_out
; /* valid bytes in outbuf */
51 static ulong inoff
; /* offset of input data */
58 for (insize
= 0; insize
< INBUFSIZ
; insize
+= bytes
, inoff
+= bytes
) {
60 if ((bytes
= sflash_read(cc
, inoff
, INBUFSIZ
- insize
,
64 *((uint32
*) &inbuf
[insize
]) = *((uint32
*) KSEG1ADDR(SB_FLASH1
+ inoff
));
65 bytes
= sizeof(uint32
);
74 /* Defines for gzip/bzip */
75 #define malloc(size) MALLOC(NULL, size)
76 #define free(addr) MFREE(NULL, addr, 0)
81 printf("\n\n%s\n\n -- System halted", x
);
95 #define memzero(s, n) memset ((s), 0, (n))
97 typedef unsigned char uch
;
98 typedef unsigned short ush
;
99 typedef unsigned long ulg
;
101 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
103 /* Diagnostic functions (stubbed out) */
105 #define Assert(cond, msg)
110 #define Tracecv(c, x)
112 static uchar
*window
; /* Sliding window buffer */
113 static unsigned outcnt
; /* bytes in window buffer */
116 gzip_mark(void **ptr
)
118 /* I'm not sure what the pourpose of this is, there are no malloc
119 * calls without free's in the gunzip code.
124 gzip_release(void **ptr
)
128 static void flush_window(void);
130 #include "gzip_inflate.c"
132 /* ===========================================================================
133 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
134 * (Used for the decompressed data only.)
144 out
= &outbuf
[bytes_out
];
145 for (n
= 0; n
< outcnt
; n
++) {
147 c
= crc_32_tab
[((int)c
^ ch
) & 0xff] ^ (c
>> 8);
150 bytes_out
+= (ulg
)outcnt
;
155 #elif defined(USE_BZIP2)
161 void bz_internal_error(int i
)
165 sprintf(msg
, "Bzip2 internal error: %d", i
);
169 #include "bzip2_inflate.c"
177 bzstream
.bzalloc
= 0;
180 bzstream
.avail_in
= 0;
182 if ((ret
= BZ2_bzDecompressInit(&bzstream
, 0, 1)) != BZ_OK
)
186 if (bzstream
.avail_in
== 0) {
188 bzstream
.next_in
= inbuf
;
189 bzstream
.avail_in
= insize
;
191 bzstream
.next_out
= &outbuf
[bytes_out
];
192 bzstream
.avail_out
= WSIZE
;
193 if ((ret
= BZ2_bzDecompress(&bzstream
)) != BZ_OK
)
195 bytes_out
= bzstream
.total_out_lo32
;
199 if (ret
== BZ_STREAM_END
)
200 ret
= BZ2_bzDecompressEnd(&bzstream
);
208 #endif /* defined(USE_GZIP) */
210 extern char input_data
[];
211 extern int input_len
;
218 /* Offset from beginning of flash */
220 inoff
= ((ulong
)text_end
- (ulong
)text_start
) + ((ulong
)input_data
- (ulong
)data_start
);
222 inoff
= (ulong
) input_data
- (ulong
) text_start
;
223 #endif /* CONFIG_XIP */
224 outbuf
= (uchar
*) LOADADDR
;
226 inbuf
= malloc(INBUFSIZ
); /* input buffer */
228 #if defined(USE_GZIP)
229 window
= malloc(WSIZE
);
230 printf("Decompressing...");
233 #elif defined(USE_BZIP2)
234 /* Small decompression algorithm uses up to 2300k of memory */
235 printf("Decompressing...");
238 printf("Copying...");
239 while (bytes_out
< input_len
) {
241 memcpy(&outbuf
[bytes_out
], inbuf
, insize
);
244 #endif /* defined(USE_GZIP) */
246 printf("error %d\n", ret
);
252 sflash_self(chipcregs_t
*cc
)
254 unsigned char *start
= text_start
;
255 unsigned char *end
= data_end
;
256 unsigned char *cur
= start
;
257 unsigned int erasesize
, len
;
261 printf("Erasing sector 0x%x...", (cur
- start
));
262 if ((erasesize
= sflash_erase(cc
, cur
- start
)) < 0) {
266 while (sflash_poll(cc
, cur
- start
));
270 printf("Writing sector 0x%x...", (cur
- start
));
272 if ((len
= sflash_write(cc
, cur
- start
, erasesize
, cur
)) < 0)
274 while (sflash_poll(cc
, cur
- start
));
287 c_main(unsigned long ra
)
289 /* Basic initialization */
290 sbh
= (sb_t
*)osl_init();
293 /* Initialize and turn caches on */
295 #endif /* CFG_UNCACHED */
297 cc
= sb_setcore(sbh
, SB_CC
, 0);
299 /* Initialize serial flash */
300 sflash
= cc
? sflash_init(cc
) : NULL
;
302 /* Copy self to flash if we booted from SDRAM */
303 if (PHYSADDR(ra
) < SB_FLASH1
) {
311 /* Flush all caches */
315 /* Jump to load address */
316 ((void (*)(void)) LOADADDR
)();