]> git.sur5r.net Git - u-boot/blob - lib_m68k/m68k_linux.c
[new uImage] Move CHUNKSZ definition to image.h
[u-boot] / lib_m68k / m68k_linux.c
1 /*
2  * (C) Copyright 2003
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
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.
17  *
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, MA  02111-1307  USA
21  *
22  */
23
24 #include <common.h>
25 #include <command.h>
26 #include <image.h>
27 #include <zlib.h>
28 #include <bzlib.h>
29 #include <watchdog.h>
30 #include <environment.h>
31 #include <asm/byteorder.h>
32
33 DECLARE_GLOBAL_DATA_PTR;
34
35 #define PHYSADDR(x) x
36
37 #define LINUX_MAX_ENVS          256
38 #define LINUX_MAX_ARGS          256
39
40 #ifdef CONFIG_SHOW_BOOT_PROGRESS
41 # include <status_led.h>
42 # define SHOW_BOOT_PROGRESS(arg)        show_boot_progress(arg)
43 #else
44 # define SHOW_BOOT_PROGRESS(arg)
45 #endif
46
47 extern image_header_t header;
48
49 int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
50
51 void do_bootm_linux(cmd_tbl_t * cmdtp, int flag,
52                     int argc, char *argv[],
53                     ulong addr, ulong * len_ptr, int verify)
54 {
55         ulong sp;
56         ulong len;
57         ulong initrd_start, initrd_end;
58         ulong cmd_start, cmd_end;
59         ulong initrd_high;
60         ulong data;
61         int initrd_copy_to_ram = 1;
62         char *cmdline;
63         char *s;
64         bd_t *kbd;
65         void (*kernel) (bd_t *, ulong, ulong, ulong, ulong);
66         image_header_t *hdr = &header;
67
68         if ((s = getenv("initrd_high")) != NULL) {
69                 /* a value of "no" or a similar string will act like 0,
70                  * turning the "load high" feature off. This is intentional.
71                  */
72                 initrd_high = simple_strtoul(s, NULL, 16);
73                 if (initrd_high == ~0)
74                         initrd_copy_to_ram = 0;
75         } else {                /* not set, no restrictions to load high */
76                 initrd_high = ~0;
77         }
78
79 #ifdef CONFIG_LOGBUFFER
80         kbd = gd->bd;
81         /* Prevent initrd from overwriting logbuffer */
82         if (initrd_high < (kbd->bi_memsize - LOGBUFF_LEN - LOGBUFF_OVERHEAD))
83                 initrd_high = kbd->bi_memsize - LOGBUFF_LEN - LOGBUFF_OVERHEAD;
84         debug("## Logbuffer at 0x%08lX ", kbd->bi_memsize - LOGBUFF_LEN);
85 #endif
86
87         /*
88          * Booting a (Linux) kernel image
89          *
90          * Allocate space for command line and board info - the
91          * address should be as high as possible within the reach of
92          * the kernel (see CFG_BOOTMAPSZ settings), but in unused
93          * memory, which means far enough below the current stack
94          * pointer.
95          */
96         asm("movel %%a7, %%d0\n"
97             "movel %%d0, %0\n": "=d"(sp): :"%d0");
98
99         debug("## Current stack ends at 0x%08lX ", sp);
100
101         sp -= 2048;             /* just to be sure */
102         if (sp > CFG_BOOTMAPSZ)
103                 sp = CFG_BOOTMAPSZ;
104         sp &= ~0xF;
105
106         debug("=> set upper limit to 0x%08lX\n", sp);
107
108         cmdline = (char *)((sp - CFG_BARGSIZE) & ~0xF);
109         kbd = (bd_t *) (((ulong) cmdline - sizeof(bd_t)) & ~0xF);
110
111         if ((s = getenv("bootargs")) == NULL)
112                 s = "";
113
114         strcpy(cmdline, s);
115
116         cmd_start = (ulong) & cmdline[0];
117         cmd_end = cmd_start + strlen(cmdline);
118
119         *kbd = *(gd->bd);
120
121 #ifdef  DEBUG
122         printf("## cmdline at 0x%08lX ... 0x%08lX\n", cmd_start, cmd_end);
123
124         do_bdinfo(NULL, 0, 0, NULL);
125 #endif
126
127         if ((s = getenv("clocks_in_mhz")) != NULL) {
128                 /* convert all clock information to MHz */
129                 kbd->bi_intfreq /= 1000000L;
130                 kbd->bi_busfreq /= 1000000L;
131         }
132
133         kernel =
134             (void (*)(bd_t *, ulong, ulong, ulong, ulong))image_get_ep (hdr);
135
136         /*
137          * Check if there is an initrd image
138          */
139
140         if (argc >= 3) {
141                 debug("Not skipping initrd\n");
142                 SHOW_BOOT_PROGRESS(9);
143
144                 addr = simple_strtoul(argv[2], NULL, 16);
145                 hdr = (image_header_t *)addr;
146
147                 printf("## Loading RAMDisk Image at %08lx ...\n", addr);
148
149                 if (!image_check_magic (hdr)) {
150                         puts("Bad Magic Number\n");
151                         SHOW_BOOT_PROGRESS(-10);
152                         do_reset(cmdtp, flag, argc, argv);
153                 }
154
155                 if (!image_check_hcrc (hdr)) {
156                         puts("Bad Header Checksum\n");
157                         SHOW_BOOT_PROGRESS(-11);
158                         do_reset(cmdtp, flag, argc, argv);
159                 }
160
161                 SHOW_BOOT_PROGRESS(10);
162
163                 print_image_hdr (hdr);
164
165                 data = image_get_data (hdr);
166                 len = image_get_data_size (hdr);
167
168                 if (verify) {
169                         puts("   Verifying Checksum ... ");
170                         if (!image_check_dcrc_wd (hdr, CHUNKSZ)) {
171                                 puts("Bad Data CRC\n");
172                                 SHOW_BOOT_PROGRESS(-12);
173                                 do_reset(cmdtp, flag, argc, argv);
174                         }
175                         puts("OK\n");
176                 }
177
178                 SHOW_BOOT_PROGRESS(11);
179
180                 if (!image_check_os (hdr, IH_OS_LINUX) ||
181                     !image_check_arch (hdr, IH_ARCH_M68K) ||
182                     !image_check_type (hdr, IH_TYPE_RAMDISK)) {
183                         puts("No Linux ColdFire Ramdisk Image\n");
184                         SHOW_BOOT_PROGRESS(-13);
185                         do_reset(cmdtp, flag, argc, argv);
186                 }
187
188                 /*
189                  * Now check if we have a multifile image
190                  */
191         } else if (image_check_type (hdr, IH_TYPE_MULTI) && (len_ptr[1])) {
192                 u_long tail = image_to_cpu (len_ptr[0]) % 4;
193                 int i;
194
195                 SHOW_BOOT_PROGRESS(13);
196
197                 /* skip kernel length and terminator */
198                 data = (ulong) (&len_ptr[2]);
199                 /* skip any additional image length fields */
200                 for (i = 1; len_ptr[i]; ++i)
201                         data += 4;
202                 /* add kernel length, and align */
203                 data += image_to_cpu (len_ptr[0]);
204                 if (tail) {
205                         data += 4 - tail;
206                 }
207
208                 len = image_to_cpu (len_ptr[1]);
209
210         } else {
211                 /*
212                  * no initrd image
213                  */
214                 SHOW_BOOT_PROGRESS(14);
215
216                 len = data = 0;
217         }
218
219         if (!data) {
220                 debug("No initrd\n");
221         }
222
223         if (data) {
224                 if (!initrd_copy_to_ram) {      /* zero-copy ramdisk support */
225                         initrd_start = data;
226                         initrd_end = initrd_start + len;
227                 } else {
228                         initrd_start = (ulong) kbd - len;
229                         initrd_start &= ~(4096 - 1);    /* align on page */
230
231                         if (initrd_high) {
232                                 ulong nsp;
233
234                                 /*
235                                  * the inital ramdisk does not need to be within
236                                  * CFG_BOOTMAPSZ as it is not accessed until after
237                                  * the mm system is initialised.
238                                  *
239                                  * do the stack bottom calculation again and see if
240                                  * the initrd will fit just below the monitor stack
241                                  * bottom without overwriting the area allocated
242                                  * above for command line args and board info.
243                                  */
244                                 asm("movel %%a7, %%d0\n"
245                                     "movel %%d0, %0\n": "=d"(nsp): :"%d0");
246
247                                 nsp -= 2048;    /* just to be sure */
248                                 nsp &= ~0xF;
249
250                                 if (nsp > initrd_high)  /* limit as specified */
251                                         nsp = initrd_high;
252
253                                         nsp -= len;
254                                 nsp &= ~(4096 - 1);     /* align on page */
255
256                                 if (nsp >= sp)
257                                         initrd_start = nsp;
258                         }
259
260                         SHOW_BOOT_PROGRESS(12);
261
262                         debug
263                             ("## initrd at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
264                              data, data + len - 1, len, len);
265
266                         initrd_end = initrd_start + len;
267                         printf("   Loading Ramdisk to %08lx, end %08lx ... ",
268                                initrd_start, initrd_end);
269 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
270                         {
271                                 size_t l = len;
272                                 void *to = (void *)initrd_start;
273                                 void *from = (void *)data;
274
275                                 while (l > 0) {
276                                         size_t tail =
277                                             (l > CHUNKSZ) ? CHUNKSZ : l;
278                                         WATCHDOG_RESET();
279                                         memmove(to, from, tail);
280                                         to += tail;
281                                         from += tail;
282                                         l -= tail;
283                                 }
284                         }
285 #else                           /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
286                         memmove((void *)initrd_start, (void *)data, len);
287 #endif                          /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
288                         puts("OK\n");
289                 }
290         } else {
291                 initrd_start = 0;
292                 initrd_end = 0;
293         }
294
295         debug("## Transferring control to Linux (at address %08lx) ...\n",
296               (ulong) kernel);
297
298         SHOW_BOOT_PROGRESS(15);
299
300         /*
301          * Linux Kernel Parameters (passing board info data):
302          *   r3: ptr to board info data
303          *   r4: initrd_start or 0 if no initrd
304          *   r5: initrd_end - unused if r4 is 0
305          *   r6: Start of command line string
306          *   r7: End   of command line string
307          */
308         (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
309         /* does not return */
310 }