]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - lib_mips/bootm.c
[new uImage] Rename architecture specific bootm code files
[karo-tx-uboot.git] / lib_mips / bootm.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 <asm/byteorder.h>
29 #include <asm/addrspace.h>
30
31 DECLARE_GLOBAL_DATA_PTR;
32
33 #define LINUX_MAX_ENVS          256
34 #define LINUX_MAX_ARGS          256
35
36 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
37
38 static int      linux_argc;
39 static char **  linux_argv;
40
41 static char **  linux_env;
42 static char *   linux_env_p;
43 static int      linux_env_idx;
44
45 static void linux_params_init (ulong start, char * commandline);
46 static void linux_env_set (char * env_name, char * env_val);
47
48
49 void do_bootm_linux (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[],
50                      image_header_t *hdr, int verify)
51 {
52         ulong rd_data, rd_len;
53         ulong initrd_start, initrd_end;
54         image_header_t *rd_hdr;
55
56         void (*theKernel) (int, char **, char **, int *);
57         char *commandline = getenv ("bootargs");
58         char env_buf[12];
59
60         theKernel =
61                 (void (*)(int, char **, char **, int *))image_get_ep (hdr);
62
63         /*
64          * Check if there is an initrd image
65          */
66         if (argc >= 3) {
67                 show_boot_progress (9);
68
69                 rd_hdr = (image_header_t *)simple_strtoul (argv[2], NULL, 16);
70                 printf ("## Loading Ramdisk Image at %08lx ...\n", rd_hdr);
71
72                 if (!image_check_magic (rd_hdr)) {
73                         printf ("Bad Magic Number\n");
74                         show_boot_progress (-10);
75                         do_reset (cmdtp, flag, argc, argv);
76                 }
77
78                 if (!image_check_hcrc (rd_hdr)) {
79                         printf ("Bad Header Checksum\n");
80                         show_boot_progress (-11);
81                         do_reset (cmdtp, flag, argc, argv);
82                 }
83
84                 show_boot_progress (10);
85                 print_image_hdr (rd_hdr);
86
87                 rd_data = image_get_data (rd_hdr);
88                 rd_len = image_get_data_size (rd_hdr);
89
90                 if (verify) {
91                         printf ("   Verifying Checksum ... ");
92                         if (!image_check_dcrc (rd_hdr)) {
93                                 printf ("Bad Data CRC\n");
94                                 show_boot_progress (-12);
95                                 do_reset (cmdtp, flag, argc, argv);
96                         }
97                         printf ("OK\n");
98                 }
99
100                 show_boot_progress (11);
101
102                 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
103                     !image_check_arch (rd_hdr, IH_ARCH_MIPS) ||
104                     !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
105                         printf ("No Linux MIPS Ramdisk Image\n");
106                         show_boot_progress (-13);
107                         do_reset (cmdtp, flag, argc, argv);
108                 }
109
110                 /*
111                  * Now check if we have a multifile image
112                  */
113         } else if (image_check_type (hdr, IH_TYPE_MULTI)) {
114                 /*
115                  * Get second entry data start address and len
116                  */
117                 show_boot_progress (13);
118                 image_multi_getimg (hdr, 1, &rd_data, &rd_len);
119         } else {
120                 /*
121                  * no initrd image
122                  */
123                 show_boot_progress (14);
124
125                 rd_data = rd_len = 0;
126         }
127
128 #ifdef  DEBUG
129         if (!rd_data) {
130                 printf ("No initrd\n");
131         }
132 #endif
133
134         if (rd_data) {
135                 initrd_start = rd_data;
136                 initrd_end = initrd_start + rd_len;
137         } else {
138                 initrd_start = 0;
139                 initrd_end = 0;
140         }
141
142         show_boot_progress (15);
143
144 #ifdef DEBUG
145         printf ("## Transferring control to Linux (at address %08lx) ...\n",
146                 (ulong) theKernel);
147 #endif
148
149         linux_params_init (UNCACHED_SDRAM (gd->bd->bi_boot_params), commandline);
150
151 #ifdef CONFIG_MEMSIZE_IN_BYTES
152         sprintf (env_buf, "%lu", gd->ram_size);
153 #ifdef DEBUG
154         printf ("## Giving linux memsize in bytes, %lu\n", gd->ram_size);
155 #endif
156 #else
157         sprintf (env_buf, "%lu", gd->ram_size >> 20);
158 #ifdef DEBUG
159         printf ("## Giving linux memsize in MB, %lu\n", gd->ram_size >> 20);
160 #endif
161 #endif /* CONFIG_MEMSIZE_IN_BYTES */
162
163         linux_env_set ("memsize", env_buf);
164
165         sprintf (env_buf, "0x%08X", (uint) UNCACHED_SDRAM (initrd_start));
166         linux_env_set ("initrd_start", env_buf);
167
168         sprintf (env_buf, "0x%X", (uint) (initrd_end - initrd_start));
169         linux_env_set ("initrd_size", env_buf);
170
171         sprintf (env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
172         linux_env_set ("flash_start", env_buf);
173
174         sprintf (env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
175         linux_env_set ("flash_size", env_buf);
176
177         /* we assume that the kernel is in place */
178         printf ("\nStarting kernel ...\n\n");
179
180         theKernel (linux_argc, linux_argv, linux_env, 0);
181 }
182
183 static void linux_params_init (ulong start, char *line)
184 {
185         char *next, *quote, *argp;
186
187         linux_argc = 1;
188         linux_argv = (char **) start;
189         linux_argv[0] = 0;
190         argp = (char *) (linux_argv + LINUX_MAX_ARGS);
191
192         next = line;
193
194         while (line && *line && linux_argc < LINUX_MAX_ARGS) {
195                 quote = strchr (line, '"');
196                 next = strchr (line, ' ');
197
198                 while (next != NULL && quote != NULL && quote < next) {
199                         /* we found a left quote before the next blank
200                          * now we have to find the matching right quote
201                          */
202                         next = strchr (quote + 1, '"');
203                         if (next != NULL) {
204                                 quote = strchr (next + 1, '"');
205                                 next = strchr (next + 1, ' ');
206                         }
207                 }
208
209                 if (next == NULL) {
210                         next = line + strlen (line);
211                 }
212
213                 linux_argv[linux_argc] = argp;
214                 memcpy (argp, line, next - line);
215                 argp[next - line] = 0;
216
217                 argp += next - line + 1;
218                 linux_argc++;
219
220                 if (*next)
221                         next++;
222
223                 line = next;
224         }
225
226         linux_env = (char **) (((ulong) argp + 15) & ~15);
227         linux_env[0] = 0;
228         linux_env_p = (char *) (linux_env + LINUX_MAX_ENVS);
229         linux_env_idx = 0;
230 }
231
232 static void linux_env_set (char *env_name, char *env_val)
233 {
234         if (linux_env_idx < LINUX_MAX_ENVS - 1) {
235                 linux_env[linux_env_idx] = linux_env_p;
236
237                 strcpy (linux_env_p, env_name);
238                 linux_env_p += strlen (env_name);
239
240                 strcpy (linux_env_p, "=");
241                 linux_env_p += 1;
242
243                 strcpy (linux_env_p, env_val);
244                 linux_env_p += strlen (env_val);
245
246                 linux_env_p++;
247                 linux_env[++linux_env_idx] = 0;
248         }
249 }