* Patch by Nicolas Lacressonnière, 12 Nov 2003:
[platform/kernel/u-boot.git] / lib_arm / armlinux.c
1 /*
2  * (C) Copyright 2002
3  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
4  * Marius Groeger <mgroeger@sysgo.de>
5  *
6  * Copyright (C) 2001  Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
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 <asm/byteorder.h>
29 #ifdef CONFIG_HAS_DATAFLASH
30 #include <dataflash.h>
31 #endif
32
33 #include <asm/setup.h>
34 #define tag_size(type)  ((sizeof(struct tag_header) + sizeof(struct type)) >> 2)
35 #define tag_next(t)     ((struct tag *)((u32 *)(t) + (t)->hdr.size))
36
37 /*cmd_boot.c*/
38 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
39
40 #if defined (CONFIG_SETUP_MEMORY_TAGS) || \
41     defined (CONFIG_CMDLINE_TAG) || \
42     defined (CONFIG_INITRD_TAG) || \
43     defined (CONFIG_VFD)
44 static void setup_start_tag (bd_t *bd);
45
46 # ifdef CONFIG_SETUP_MEMORY_TAGS
47 static void setup_memory_tags (bd_t *bd);
48 # endif
49 static void setup_commandline_tag (bd_t *bd, char *commandline);
50
51 #if 0
52 static void setup_ramdisk_tag (bd_t *bd);
53 #endif
54 # ifdef CONFIG_INITRD_TAG
55 static void setup_initrd_tag (bd_t *bd, ulong initrd_start,
56                               ulong initrd_end);
57 # endif
58 static void setup_end_tag (bd_t *bd);
59
60 # if defined (CONFIG_VFD)
61 static void setup_videolfb_tag (gd_t *gd);
62 # endif
63
64
65 static struct tag *params;
66 #endif /* CONFIG_SETUP_MEMORY_TAGS || CONFIG_CMDLINE_TAG || CONFIG_INITRD_TAG */
67
68 #ifdef CONFIG_SHOW_BOOT_PROGRESS
69 # include <status_led.h>
70 # define SHOW_BOOT_PROGRESS(arg)        show_boot_progress(arg)
71 #else
72 # define SHOW_BOOT_PROGRESS(arg)
73 #endif
74
75 extern image_header_t header;   /* from cmd_bootm.c */
76
77
78 void do_bootm_linux (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
79                      ulong addr, ulong *len_ptr, int verify)
80 {
81         DECLARE_GLOBAL_DATA_PTR;
82
83         ulong len = 0, checksum;
84         ulong initrd_start, initrd_end;
85         ulong data;
86         void (*theKernel) (int zero, int arch);
87         image_header_t *hdr = &header;
88         bd_t *bd = gd->bd;
89
90 #ifdef CONFIG_CMDLINE_TAG
91         char *commandline = getenv ("bootargs");
92 #endif
93
94         theKernel = (void (*)(int, int)) ntohl (hdr->ih_ep);
95
96         /*
97          * Check if there is an initrd image
98          */
99         if (argc >= 3) {
100                 SHOW_BOOT_PROGRESS (9);
101
102                 addr = simple_strtoul (argv[2], NULL, 16);
103
104                 printf ("## Loading Ramdisk Image at %08lx ...\n", addr);
105
106                 /* Copy header so we can blank CRC field for re-calculation */
107 #ifdef CONFIG_HAS_DATAFLASH
108                 if (addr_dataflash (addr)) {
109                         read_dataflash (addr, sizeof (image_header_t),
110                                         (char *) &header);
111                 } else
112 #endif
113                         memcpy (&header, (char *) addr,
114                                 sizeof (image_header_t));
115
116                 if (ntohl (hdr->ih_magic) != IH_MAGIC) {
117                         printf ("Bad Magic Number\n");
118                         SHOW_BOOT_PROGRESS (-10);
119                         do_reset (cmdtp, flag, argc, argv);
120                 }
121
122                 data = (ulong) & header;
123                 len = sizeof (image_header_t);
124
125                 checksum = ntohl (hdr->ih_hcrc);
126                 hdr->ih_hcrc = 0;
127
128                 if (crc32 (0, (char *) data, len) != checksum) {
129                         printf ("Bad Header Checksum\n");
130                         SHOW_BOOT_PROGRESS (-11);
131                         do_reset (cmdtp, flag, argc, argv);
132                 }
133
134                 SHOW_BOOT_PROGRESS (10);
135
136                 print_image_hdr (hdr);
137
138                 data = addr + sizeof (image_header_t);
139                 len = ntohl (hdr->ih_size);
140
141 #ifdef CONFIG_HAS_DATAFLASH
142                 if (addr_dataflash (addr)) {
143                         read_dataflash (data, len, (char *) CFG_LOAD_ADDR);
144                         data = CFG_LOAD_ADDR;
145                 }
146 #endif
147
148                 if (verify) {
149                         ulong csum = 0;
150
151                         printf ("   Verifying Checksum ... ");
152                         csum = crc32 (0, (char *) data, len);
153                         if (csum != ntohl (hdr->ih_dcrc)) {
154                                 printf ("Bad Data CRC\n");
155                                 SHOW_BOOT_PROGRESS (-12);
156                                 do_reset (cmdtp, flag, argc, argv);
157                         }
158                         printf ("OK\n");
159                 }
160
161                 SHOW_BOOT_PROGRESS (11);
162
163                 if ((hdr->ih_os != IH_OS_LINUX) ||
164                     (hdr->ih_arch != IH_CPU_ARM) ||
165                     (hdr->ih_type != IH_TYPE_RAMDISK)) {
166                         printf ("No Linux ARM Ramdisk Image\n");
167                         SHOW_BOOT_PROGRESS (-13);
168                         do_reset (cmdtp, flag, argc, argv);
169                 }
170
171                 /*
172                  * Now check if we have a multifile image
173                  */
174         } else if ((hdr->ih_type == IH_TYPE_MULTI) && (len_ptr[1])) {
175                 ulong tail = ntohl (len_ptr[0]) % 4;
176                 int i;
177
178                 SHOW_BOOT_PROGRESS (13);
179
180                 /* skip kernel length and terminator */
181                 data = (ulong) (&len_ptr[2]);
182                 /* skip any additional image length fields */
183                 for (i = 1; len_ptr[i]; ++i)
184                         data += 4;
185                 /* add kernel length, and align */
186                 data += ntohl (len_ptr[0]);
187                 if (tail) {
188                         data += 4 - tail;
189                 }
190
191                 len = ntohl (len_ptr[1]);
192
193         } else {
194                 /*
195                  * no initrd image
196                  */
197                 SHOW_BOOT_PROGRESS (14);
198
199                 len = data = 0;
200         }
201
202 #ifdef  DEBUG
203         if (!data) {
204                 printf ("No initrd\n");
205         }
206 #endif
207
208         if (data) {
209                 initrd_start = data;
210                 initrd_end = initrd_start + len;
211         } else {
212                 initrd_start = 0;
213                 initrd_end = 0;
214         }
215
216         SHOW_BOOT_PROGRESS (15);
217
218         debug ("## Transferring control to Linux (at address %08lx) ...\n",
219                (ulong) theKernel);
220
221 #if defined (CONFIG_SETUP_MEMORY_TAGS) || \
222     defined (CONFIG_CMDLINE_TAG) || \
223     defined (CONFIG_INITRD_TAG) || \
224     defined (CONFIG_VFD)
225         setup_start_tag (bd);
226 #ifdef CONFIG_SETUP_MEMORY_TAGS
227         setup_memory_tags (bd);
228 #endif
229 #ifdef CONFIG_CMDLINE_TAG
230         setup_commandline_tag (bd, commandline);
231 #endif
232 #ifdef CONFIG_INITRD_TAG
233         if (initrd_start && initrd_end)
234                 setup_initrd_tag (bd, initrd_start, initrd_end);
235 #endif
236 #if defined (CONFIG_VFD)
237         setup_videolfb_tag ((gd_t *) gd);
238 #endif
239         setup_end_tag (bd);
240 #endif
241
242         /* we assume that the kernel is in place */
243         printf ("\nStarting kernel ...\n\n");
244
245         cleanup_before_linux ();
246
247         theKernel (0, bd->bi_arch_number);
248 }
249
250
251 #if defined (CONFIG_SETUP_MEMORY_TAGS) || \
252     defined (CONFIG_CMDLINE_TAG) || \
253     defined (CONFIG_INITRD_TAG) || \
254     defined (CONFIG_VFD)
255 static void setup_start_tag (bd_t *bd)
256 {
257         params = (struct tag *) bd->bi_boot_params;
258
259         params->hdr.tag = ATAG_CORE;
260         params->hdr.size = tag_size (tag_core);
261
262         params->u.core.flags = 0;
263         params->u.core.pagesize = 0;
264         params->u.core.rootdev = 0;
265
266         params = tag_next (params);
267 }
268
269
270 #ifdef CONFIG_SETUP_MEMORY_TAGS
271 static void setup_memory_tags (bd_t *bd)
272 {
273         int i;
274
275         for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
276                 params->hdr.tag = ATAG_MEM;
277                 params->hdr.size = tag_size (tag_mem32);
278
279                 params->u.mem.start = bd->bi_dram[i].start;
280                 params->u.mem.size = bd->bi_dram[i].size;
281
282                 params = tag_next (params);
283         }
284 }
285 #endif /* CONFIG_SETUP_MEMORY_TAGS */
286
287
288 static void setup_commandline_tag (bd_t *bd, char *commandline)
289 {
290         char *p;
291
292         /* eat leading white space */
293         for (p = commandline; *p == ' '; p++);
294
295         /* skip non-existent command lines so the kernel will still
296          * use its default command line.
297          */
298         if (*p == '\0')
299                 return;
300
301         params->hdr.tag = ATAG_CMDLINE;
302         params->hdr.size =
303                 (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2;
304
305         strcpy (params->u.cmdline.cmdline, p);
306
307         params = tag_next (params);
308 }
309
310
311 #ifndef ATAG_INITRD2
312 #define ATAG_INITRD2    0x54420005
313 #endif
314
315 #ifdef CONFIG_INITRD_TAG
316 static void setup_initrd_tag (bd_t *bd, ulong initrd_start, ulong initrd_end)
317 {
318         /* an ATAG_INITRD node tells the kernel where the compressed
319          * ramdisk can be found. ATAG_RDIMG is a better name, actually.
320          */
321         params->hdr.tag = ATAG_INITRD2;
322         params->hdr.size = tag_size (tag_initrd);
323
324         params->u.initrd.start = initrd_start;
325         params->u.initrd.size = initrd_end - initrd_start;
326
327         params = tag_next (params);
328 }
329 #endif /* CONFIG_INITRD_TAG */
330
331
332 #if defined (CONFIG_VFD)
333 static void setup_videolfb_tag (gd_t *gd)
334 {
335         /* An ATAG_VIDEOLFB node tells the kernel where and how large
336          * the framebuffer for video was allocated (among other things).
337          * Note that a _physical_ address is passed !
338          *
339          * We only use it to pass the address and size, the other entries
340          * in the tag_videolfb are not of interest.
341          */
342         params->hdr.tag = ATAG_VIDEOLFB;
343         params->hdr.size = tag_size (tag_videolfb);
344
345         params->u.videolfb.lfb_base = (u32) gd->fb_base;
346         /* 7168 = 256*4*56/8 - actually 2 pages (8192 bytes) are allocated */
347         params->u.videolfb.lfb_size = 7168;
348
349         params = tag_next (params);
350 }
351 #endif
352
353 static void setup_end_tag (bd_t *bd)
354 {
355         params->hdr.tag = ATAG_NONE;
356         params->hdr.size = 0;
357 }
358
359 #endif /* CONFIG_SETUP_MEMORY_TAGS || CONFIG_CMDLINE_TAG || CONFIG_INITRD_TAG */