3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 #include <environment.h>
31 #include "LzmaWrapper.h"
33 //#define DEBUG_ENABLE_BOOTSTRAP_PRINTF
35 DECLARE_GLOBAL_DATA_PTR;
37 #if (((CFG_ENV_ADDR+CFG_ENV_SIZE) < BOOTSTRAP_CFG_MONITOR_BASE) || (CFG_ENV_ADDR >= (BOOTSTRAP_CFG_MONITOR_BASE + CFG_MONITOR_LEN)) ) || defined(CFG_ENV_IS_IN_NVRAM)
38 #define TOTAL_MALLOC_LEN (CFG_MALLOC_LEN + CFG_ENV_SIZE)
40 #define TOTAL_MALLOC_LEN CFG_MALLOC_LEN
45 extern void bootstrap_relocate_code(ulong addr_sp, gd_t *gd, ulong addr_moni);
47 extern int timer_init(void);
49 extern ulong uboot_end_data_bootstrap;
50 extern ulong uboot_end_bootstrap;
53 * Begin and End of memory area for malloc(), and current "brk"
55 static ulong mem_malloc_start;
56 static ulong mem_malloc_end;
57 static ulong mem_malloc_brk;
60 * The Malloc area is immediately below the monitor copy in DRAM
62 static void mem_malloc_init(ulong dest_addr){
63 // ulong dest_addr = BOOTSTRAP_CFG_MONITOR_BASE + gd->reloc_off;
65 mem_malloc_end = dest_addr;
66 mem_malloc_start = dest_addr - TOTAL_MALLOC_LEN;
67 mem_malloc_brk = mem_malloc_start;
69 memset((void *)mem_malloc_start, 0, mem_malloc_end - mem_malloc_start);
72 void *malloc(unsigned int size){
73 if(size < (mem_malloc_end - mem_malloc_start)){
74 mem_malloc_start += size;
76 return((void *)(mem_malloc_start - size));
82 void *realloc(void *src, unsigned int size){
90 static int init_func_ram(void){
91 if((gd->ram_size = initdram()) > 0){
98 * Breath some life into the board...
100 * The first part of initialization is running from Flash memory;
101 * its main purpose is to initialize the RAM so that we
102 * can relocate the monitor code to RAM.
106 * All attempts to come up with a "common" initialization sequence
107 * that works for all boards and architectures failed: some of the
108 * requirements are just _too_ different. To get rid of the resulting
109 * mess of board dependend #ifdef'ed code we now make the whole
110 * initialization sequence configurable to the user.
112 * The requirements for any new initalization function is simple: it
113 * receives a pointer to the "global data" structure as it's only
114 * argument, and returns an integer return code, where 0 means
115 * "continue" and != 0 means "fatal error, hang the system".
117 typedef int(init_fnc_t)(void);
119 init_fnc_t *init_sequence[] = { timer_init,
124 void bootstrap_board_init_f(ulong bootflag){
127 init_fnc_t **init_fnc_ptr;
128 ulong addr, addr_sp, len = (ulong)&uboot_end_bootstrap - BOOTSTRAP_CFG_MONITOR_BASE;
131 /* Pointer is writable since we allocated a register for it.
135 /* compiler optimization barrier needed for GCC >= 3.4 */
136 __asm__ __volatile__("": : :"memory");
138 memset((void *)gd, 0, sizeof(gd_t));
140 for(init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr){
141 if((*init_fnc_ptr)() != 0){
147 * Now that we have DRAM mapped and working, we can
148 * relocate the code and continue running from DRAM.
150 addr = CFG_SDRAM_BASE + gd->ram_size;
153 * We can reserve some RAM "on top" here.
154 * round down to next 4 kB limit.
159 * Reserve memory for U-Boot code, data & bss
160 * round down to next 16 kB limit
163 addr &= ~(16 * 1024 - 1);
166 * Reserve memory for malloc() arena.
168 addr_sp = addr - TOTAL_MALLOC_LEN;
171 * (permanently) allocate a Board Info struct
172 * and a permanent copy of the "global" data
174 addr_sp -= sizeof(bd_t);
175 bd = (bd_t *)addr_sp;
178 addr_sp -= sizeof(gd_t);
179 id = (gd_t *)addr_sp;
182 * Reserve memory for boot params.
184 addr_sp -= CFG_BOOTPARAMS_LEN;
185 bd->bi_boot_params = addr_sp;
188 * Finally, we set up a new (bigger) stack.
190 * Leave some safety gap for SP, force alignment on 16 byte boundary
191 * Clear initial stack frame
195 s = (ulong *)addr_sp;
201 * Save local variables to board info struct
203 bd->bi_memstart = CFG_SDRAM_BASE; /* start of DRAM memory */
204 bd->bi_memsize = gd->ram_size; /* size of DRAM memory in bytes */
205 bd->bi_baudrate = gd->baudrate; /* Console Baudrate */
207 memcpy(id, (void *)gd, sizeof(gd_t));
209 bootstrap_relocate_code(addr_sp, id, addr);
211 /* NOTREACHED - relocate_code() does not return */
214 /************************************************************************
216 * This is the next part if the initialization sequence: we are now
217 * running from RAM and have a "normal" C environment, i. e. global
218 * data can be written, BSS has been cleared, the stack size in not
219 * that critical any more, etc.
221 ************************************************************************
223 void bootstrap_board_init_r(gd_t *id, ulong dest_addr){
226 ulong data, len, checksum;
227 image_header_t header;
228 image_header_t *hdr = &header;
229 unsigned int destLen;
232 /* initialize malloc() area */
233 mem_malloc_init(dest_addr);
235 addr = (ulong)((char *)(BOOTSTRAP_CFG_MONITOR_BASE + ((ulong)&uboot_end_data_bootstrap - dest_addr)));
236 memmove(&header, (char *)addr, sizeof(image_header_t));
238 if(ntohl(hdr->ih_magic) != IH_MAGIC){
242 data = (ulong)&header;
243 len = sizeof(image_header_t);
245 checksum = ntohl(hdr->ih_hcrc);
248 if(crc32(0, (unsigned char *)data, len) != checksum){
252 data = addr + sizeof(image_header_t);
253 len = ntohl(hdr->ih_size);
256 * If we've got less than 4 MB of malloc() space,
257 * use slower decompression algorithm which requires
258 * at most 2300 KB of memory.
263 i = lzma_inflate((unsigned char *)data, len, (unsigned char*)ntohl(hdr->ih_load), (int *)&destLen);
265 if(i != LZMA_RESULT_OK){
266 //do_reset(cmdtp, flag, argc, argv);
271 fn = (void *)ntohl(hdr->ih_load);