sunxi: USB download gadget cable detection
[oweals/u-boot.git] / common / bootm_os.c
1 /*
2  * (C) Copyright 2000-2009
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
8 #include <common.h>
9 #include <bootm.h>
10 #include <fdt_support.h>
11 #include <libfdt.h>
12 #include <malloc.h>
13 #include <vxworks.h>
14
15 DECLARE_GLOBAL_DATA_PTR;
16
17 static int do_bootm_standalone(int flag, int argc, char * const argv[],
18                                bootm_headers_t *images)
19 {
20         char *s;
21         int (*appl)(int, char *const[]);
22
23         /* Don't start if "autostart" is set to "no" */
24         s = getenv("autostart");
25         if ((s != NULL) && !strcmp(s, "no")) {
26                 setenv_hex("filesize", images->os.image_len);
27                 return 0;
28         }
29         appl = (int (*)(int, char * const []))images->ep;
30         appl(argc, argv);
31         return 0;
32 }
33
34 /*******************************************************************/
35 /* OS booting routines */
36 /*******************************************************************/
37
38 #if defined(CONFIG_BOOTM_NETBSD) || defined(CONFIG_BOOTM_PLAN9)
39 static void copy_args(char *dest, int argc, char * const argv[], char delim)
40 {
41         int i;
42
43         for (i = 0; i < argc; i++) {
44                 if (i > 0)
45                         *dest++ = delim;
46                 strcpy(dest, argv[i]);
47                 dest += strlen(argv[i]);
48         }
49 }
50 #endif
51
52 #ifdef CONFIG_BOOTM_NETBSD
53 static int do_bootm_netbsd(int flag, int argc, char * const argv[],
54                             bootm_headers_t *images)
55 {
56         void (*loader)(bd_t *, image_header_t *, char *, char *);
57         image_header_t *os_hdr, *hdr;
58         ulong kernel_data, kernel_len;
59         char *consdev;
60         char *cmdline;
61
62         if (flag != BOOTM_STATE_OS_GO)
63                 return 0;
64
65 #if defined(CONFIG_FIT)
66         if (!images->legacy_hdr_valid) {
67                 fit_unsupported_reset("NetBSD");
68                 return 1;
69         }
70 #endif
71         hdr = images->legacy_hdr_os;
72
73         /*
74          * Booting a (NetBSD) kernel image
75          *
76          * This process is pretty similar to a standalone application:
77          * The (first part of an multi-) image must be a stage-2 loader,
78          * which in turn is responsible for loading & invoking the actual
79          * kernel.  The only differences are the parameters being passed:
80          * besides the board info strucure, the loader expects a command
81          * line, the name of the console device, and (optionally) the
82          * address of the original image header.
83          */
84         os_hdr = NULL;
85         if (image_check_type(&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
86                 image_multi_getimg(hdr, 1, &kernel_data, &kernel_len);
87                 if (kernel_len)
88                         os_hdr = hdr;
89         }
90
91         consdev = "";
92 #if   defined(CONFIG_8xx_CONS_SMC1)
93         consdev = "smc1";
94 #elif defined(CONFIG_8xx_CONS_SMC2)
95         consdev = "smc2";
96 #elif defined(CONFIG_8xx_CONS_SCC2)
97         consdev = "scc2";
98 #elif defined(CONFIG_8xx_CONS_SCC3)
99         consdev = "scc3";
100 #endif
101
102         if (argc > 0) {
103                 ulong len;
104                 int   i;
105
106                 for (i = 0, len = 0; i < argc; i += 1)
107                         len += strlen(argv[i]) + 1;
108                 cmdline = malloc(len);
109                 copy_args(cmdline, argc, argv, ' ');
110         } else {
111                 cmdline = getenv("bootargs");
112                 if (cmdline == NULL)
113                         cmdline = "";
114         }
115
116         loader = (void (*)(bd_t *, image_header_t *, char *, char *))images->ep;
117
118         printf("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
119                (ulong)loader);
120
121         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
122
123         /*
124          * NetBSD Stage-2 Loader Parameters:
125          *   arg[0]: pointer to board info data
126          *   arg[1]: image load address
127          *   arg[2]: char pointer to the console device to use
128          *   arg[3]: char pointer to the boot arguments
129          */
130         (*loader)(gd->bd, os_hdr, consdev, cmdline);
131
132         return 1;
133 }
134 #endif /* CONFIG_BOOTM_NETBSD*/
135
136 #ifdef CONFIG_LYNXKDI
137 static int do_bootm_lynxkdi(int flag, int argc, char * const argv[],
138                              bootm_headers_t *images)
139 {
140         image_header_t *hdr = &images->legacy_hdr_os_copy;
141
142         if (flag != BOOTM_STATE_OS_GO)
143                 return 0;
144
145 #if defined(CONFIG_FIT)
146         if (!images->legacy_hdr_valid) {
147                 fit_unsupported_reset("Lynx");
148                 return 1;
149         }
150 #endif
151
152         lynxkdi_boot((image_header_t *)hdr);
153
154         return 1;
155 }
156 #endif /* CONFIG_LYNXKDI */
157
158 #ifdef CONFIG_BOOTM_RTEMS
159 static int do_bootm_rtems(int flag, int argc, char * const argv[],
160                            bootm_headers_t *images)
161 {
162         void (*entry_point)(bd_t *);
163
164         if (flag != BOOTM_STATE_OS_GO)
165                 return 0;
166
167 #if defined(CONFIG_FIT)
168         if (!images->legacy_hdr_valid) {
169                 fit_unsupported_reset("RTEMS");
170                 return 1;
171         }
172 #endif
173
174         entry_point = (void (*)(bd_t *))images->ep;
175
176         printf("## Transferring control to RTEMS (at address %08lx) ...\n",
177                (ulong)entry_point);
178
179         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
180
181         /*
182          * RTEMS Parameters:
183          *   r3: ptr to board info data
184          */
185         (*entry_point)(gd->bd);
186
187         return 1;
188 }
189 #endif /* CONFIG_BOOTM_RTEMS */
190
191 #if defined(CONFIG_BOOTM_OSE)
192 static int do_bootm_ose(int flag, int argc, char * const argv[],
193                            bootm_headers_t *images)
194 {
195         void (*entry_point)(void);
196
197         if (flag != BOOTM_STATE_OS_GO)
198                 return 0;
199
200 #if defined(CONFIG_FIT)
201         if (!images->legacy_hdr_valid) {
202                 fit_unsupported_reset("OSE");
203                 return 1;
204         }
205 #endif
206
207         entry_point = (void (*)(void))images->ep;
208
209         printf("## Transferring control to OSE (at address %08lx) ...\n",
210                (ulong)entry_point);
211
212         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
213
214         /*
215          * OSE Parameters:
216          *   None
217          */
218         (*entry_point)();
219
220         return 1;
221 }
222 #endif /* CONFIG_BOOTM_OSE */
223
224 #if defined(CONFIG_BOOTM_PLAN9)
225 static int do_bootm_plan9(int flag, int argc, char * const argv[],
226                            bootm_headers_t *images)
227 {
228         void (*entry_point)(void);
229         char *s;
230
231         if (flag != BOOTM_STATE_OS_GO)
232                 return 0;
233
234 #if defined(CONFIG_FIT)
235         if (!images->legacy_hdr_valid) {
236                 fit_unsupported_reset("Plan 9");
237                 return 1;
238         }
239 #endif
240
241         /* See README.plan9 */
242         s = getenv("confaddr");
243         if (s != NULL) {
244                 char *confaddr = (char *)simple_strtoul(s, NULL, 16);
245
246                 if (argc > 0) {
247                         copy_args(confaddr, argc, argv, '\n');
248                 } else {
249                         s = getenv("bootargs");
250                         if (s != NULL)
251                                 strcpy(confaddr, s);
252                 }
253         }
254
255         entry_point = (void (*)(void))images->ep;
256
257         printf("## Transferring control to Plan 9 (at address %08lx) ...\n",
258                (ulong)entry_point);
259
260         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
261
262         /*
263          * Plan 9 Parameters:
264          *   None
265          */
266         (*entry_point)();
267
268         return 1;
269 }
270 #endif /* CONFIG_BOOTM_PLAN9 */
271
272 #if defined(CONFIG_BOOTM_VXWORKS) && \
273         (defined(CONFIG_PPC) || defined(CONFIG_ARM))
274
275 void do_bootvx_fdt(bootm_headers_t *images)
276 {
277 #if defined(CONFIG_OF_LIBFDT)
278         int ret;
279         char *bootline;
280         ulong of_size = images->ft_len;
281         char **of_flat_tree = &images->ft_addr;
282         struct lmb *lmb = &images->lmb;
283
284         if (*of_flat_tree) {
285                 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
286
287                 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
288                 if (ret)
289                         return;
290
291                 ret = fdt_add_subnode(*of_flat_tree, 0, "chosen");
292                 if ((ret >= 0 || ret == -FDT_ERR_EXISTS)) {
293                         bootline = getenv("bootargs");
294                         if (bootline) {
295                                 ret = fdt_find_and_setprop(*of_flat_tree,
296                                                 "/chosen", "bootargs",
297                                                 bootline,
298                                                 strlen(bootline) + 1, 1);
299                                 if (ret < 0) {
300                                         printf("## ERROR: %s : %s\n", __func__,
301                                                fdt_strerror(ret));
302                                         return;
303                                 }
304                         }
305                 } else {
306                         printf("## ERROR: %s : %s\n", __func__,
307                                fdt_strerror(ret));
308                         return;
309                 }
310         }
311 #endif
312
313         boot_prep_vxworks(images);
314
315         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
316
317 #if defined(CONFIG_OF_LIBFDT)
318         printf("## Starting vxWorks at 0x%08lx, device tree at 0x%08lx ...\n",
319                (ulong)images->ep, (ulong)*of_flat_tree);
320 #else
321         printf("## Starting vxWorks at 0x%08lx\n", (ulong)images->ep);
322 #endif
323
324         boot_jump_vxworks(images);
325
326         puts("## vxWorks terminated\n");
327 }
328
329 static int do_bootm_vxworks(int flag, int argc, char * const argv[],
330                              bootm_headers_t *images)
331 {
332         if (flag != BOOTM_STATE_OS_GO)
333                 return 0;
334
335 #if defined(CONFIG_FIT)
336         if (!images->legacy_hdr_valid) {
337                 fit_unsupported_reset("VxWorks");
338                 return 1;
339         }
340 #endif
341
342         do_bootvx_fdt(images);
343
344         return 1;
345 }
346 #endif
347
348 #if defined(CONFIG_CMD_ELF)
349 static int do_bootm_qnxelf(int flag, int argc, char * const argv[],
350                             bootm_headers_t *images)
351 {
352         char *local_args[2];
353         char str[16];
354
355         if (flag != BOOTM_STATE_OS_GO)
356                 return 0;
357
358 #if defined(CONFIG_FIT)
359         if (!images->legacy_hdr_valid) {
360                 fit_unsupported_reset("QNX");
361                 return 1;
362         }
363 #endif
364
365         sprintf(str, "%lx", images->ep); /* write entry-point into string */
366         local_args[0] = argv[0];
367         local_args[1] = str;    /* and provide it via the arguments */
368         do_bootelf(NULL, 0, 2, local_args);
369
370         return 1;
371 }
372 #endif
373
374 #ifdef CONFIG_INTEGRITY
375 static int do_bootm_integrity(int flag, int argc, char * const argv[],
376                            bootm_headers_t *images)
377 {
378         void (*entry_point)(void);
379
380         if (flag != BOOTM_STATE_OS_GO)
381                 return 0;
382
383 #if defined(CONFIG_FIT)
384         if (!images->legacy_hdr_valid) {
385                 fit_unsupported_reset("INTEGRITY");
386                 return 1;
387         }
388 #endif
389
390         entry_point = (void (*)(void))images->ep;
391
392         printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
393                (ulong)entry_point);
394
395         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
396
397         /*
398          * INTEGRITY Parameters:
399          *   None
400          */
401         (*entry_point)();
402
403         return 1;
404 }
405 #endif
406
407 #ifdef CONFIG_BOOTM_OPENRTOS
408 static int do_bootm_openrtos(int flag, int argc, char * const argv[],
409                            bootm_headers_t *images)
410 {
411         void (*entry_point)(void);
412
413         if (flag != BOOTM_STATE_OS_GO)
414                 return 0;
415
416         entry_point = (void (*)(void))images->ep;
417
418         printf("## Transferring control to OpenRTOS (at address %08lx) ...\n",
419                 (ulong)entry_point);
420
421         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
422
423         /*
424          * OpenRTOS Parameters:
425          *   None
426          */
427         (*entry_point)();
428
429         return 1;
430 }
431 #endif
432
433 static boot_os_fn *boot_os[] = {
434         [IH_OS_U_BOOT] = do_bootm_standalone,
435 #ifdef CONFIG_BOOTM_LINUX
436         [IH_OS_LINUX] = do_bootm_linux,
437 #endif
438 #ifdef CONFIG_BOOTM_NETBSD
439         [IH_OS_NETBSD] = do_bootm_netbsd,
440 #endif
441 #ifdef CONFIG_LYNXKDI
442         [IH_OS_LYNXOS] = do_bootm_lynxkdi,
443 #endif
444 #ifdef CONFIG_BOOTM_RTEMS
445         [IH_OS_RTEMS] = do_bootm_rtems,
446 #endif
447 #if defined(CONFIG_BOOTM_OSE)
448         [IH_OS_OSE] = do_bootm_ose,
449 #endif
450 #if defined(CONFIG_BOOTM_PLAN9)
451         [IH_OS_PLAN9] = do_bootm_plan9,
452 #endif
453 #if defined(CONFIG_BOOTM_VXWORKS) && \
454         (defined(CONFIG_PPC) || defined(CONFIG_ARM))
455         [IH_OS_VXWORKS] = do_bootm_vxworks,
456 #endif
457 #if defined(CONFIG_CMD_ELF)
458         [IH_OS_QNX] = do_bootm_qnxelf,
459 #endif
460 #ifdef CONFIG_INTEGRITY
461         [IH_OS_INTEGRITY] = do_bootm_integrity,
462 #endif
463 #ifdef CONFIG_BOOTM_OPENRTOS
464         [IH_OS_OPENRTOS] = do_bootm_openrtos,
465 #endif
466 };
467
468 /* Allow for arch specific config before we boot */
469 __weak void arch_preboot_os(void)
470 {
471         /* please define platform specific arch_preboot_os() */
472 }
473
474 int boot_selected_os(int argc, char * const argv[], int state,
475                      bootm_headers_t *images, boot_os_fn *boot_fn)
476 {
477         arch_preboot_os();
478         boot_fn(state, argc, argv, images);
479
480         /* Stand-alone may return when 'autostart' is 'no' */
481         if (images->os.type == IH_TYPE_STANDALONE ||
482             state == BOOTM_STATE_OS_FAKE_GO) /* We expect to return */
483                 return 0;
484         bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
485 #ifdef DEBUG
486         puts("\n## Control returned to monitor - resetting...\n");
487 #endif
488         return BOOTM_ERR_RESET;
489 }
490
491 boot_os_fn *bootm_os_get_boot_func(int os)
492 {
493 #ifdef CONFIG_NEEDS_MANUAL_RELOC
494         static bool relocated;
495
496         if (!relocated) {
497                 int i;
498
499                 /* relocate boot function table */
500                 for (i = 0; i < ARRAY_SIZE(boot_os); i++)
501                         if (boot_os[i] != NULL)
502                                 boot_os[i] += gd->reloc_off;
503
504                 relocated = true;
505         }
506 #endif
507         return boot_os[os];
508 }