blob: 1003bc87b9db8b0f360ca2e5d9ce747ca865627c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Macpaul Lin0d1f4742011-10-11 22:33:19 +00002/*
3 * Copyright (C) 2011 Andes Technology Corporation
4 * Shawn Lin, Andes Technology Corporation <nobuhiro@andestech.com>
5 * Macpaul Lin, Andes Technology Corporation <macpaul@andestech.com>
Macpaul Lin0d1f4742011-10-11 22:33:19 +00006 */
7
8#include <common.h>
9#include <command.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060010#include <env.h>
Simon Glassf11478f2019-12-28 10:45:07 -070011#include <hang.h>
Macpaul Lin0d1f4742011-10-11 22:33:19 +000012#include <image.h>
13#include <u-boot/zlib.h>
14#include <asm/byteorder.h>
rickf1113c92017-05-18 14:37:53 +080015#include <asm/bootm.h>
Macpaul Lin0d1f4742011-10-11 22:33:19 +000016
17DECLARE_GLOBAL_DATA_PTR;
18
19#if defined(CONFIG_SETUP_MEMORY_TAGS) || \
20 defined(CONFIG_CMDLINE_TAG) || \
21 defined(CONFIG_INITRD_TAG) || \
22 defined(CONFIG_SERIAL_TAG) || \
23 defined(CONFIG_REVISION_TAG)
24static void setup_start_tag(bd_t *bd);
25
26# ifdef CONFIG_SETUP_MEMORY_TAGS
27static void setup_memory_tags(bd_t *bd);
28# endif
29static void setup_commandline_tag(bd_t *bd, char *commandline);
30
31# ifdef CONFIG_INITRD_TAG
32static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end);
33# endif
34static void setup_end_tag(bd_t *bd);
35
36static struct tag *params;
37#endif /* CONFIG_SETUP_MEMORY_TAGS || CONFIG_CMDLINE_TAG || CONFIG_INITRD_TAG */
38
39int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images)
40{
41 bd_t *bd = gd->bd;
42 char *s;
43 int machid = bd->bi_arch_number;
44 void (*theKernel)(int zero, int arch, uint params);
45
46#ifdef CONFIG_CMDLINE_TAG
Simon Glass64b723f2017-08-03 12:22:12 -060047 char *commandline = env_get("bootargs");
Macpaul Lin0d1f4742011-10-11 22:33:19 +000048#endif
49
Andreas Bießmannda233ce2013-07-02 13:57:44 +020050 /*
51 * allow the PREP bootm subcommand, it is required for bootm to work
52 */
53 if (flag & BOOTM_STATE_OS_PREP)
54 return 0;
55
Macpaul Lin0d1f4742011-10-11 22:33:19 +000056 if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
57 return 1;
58
59 theKernel = (void (*)(int, int, uint))images->ep;
60
Simon Glass64b723f2017-08-03 12:22:12 -060061 s = env_get("machid");
Macpaul Lin0d1f4742011-10-11 22:33:19 +000062 if (s) {
63 machid = simple_strtoul(s, NULL, 16);
64 printf("Using machid 0x%x from environment\n", machid);
65 }
66
Simon Glass0169e6b2012-02-13 13:51:18 +000067 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
Macpaul Lin0d1f4742011-10-11 22:33:19 +000068
69 debug("## Transferring control to Linux (at address %08lx) ...\n",
70 (ulong)theKernel);
71
rickf1113c92017-05-18 14:37:53 +080072 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
73#ifdef CONFIG_OF_LIBFDT
74 debug("using: FDT\n");
75 if (image_setup_linux(images)) {
76 printf("FDT creation failed! hanging...");
77 hang();
78 }
79#endif
80 } else if (BOOTM_ENABLE_TAGS) {
Macpaul Lin0d1f4742011-10-11 22:33:19 +000081#if defined(CONFIG_SETUP_MEMORY_TAGS) || \
82 defined(CONFIG_CMDLINE_TAG) || \
83 defined(CONFIG_INITRD_TAG) || \
84 defined(CONFIG_SERIAL_TAG) || \
85 defined(CONFIG_REVISION_TAG)
86 setup_start_tag(bd);
87#ifdef CONFIG_SERIAL_TAG
88 setup_serial_tag(&params);
89#endif
90#ifdef CONFIG_REVISION_TAG
91 setup_revision_tag(&params);
92#endif
93#ifdef CONFIG_SETUP_MEMORY_TAGS
94 setup_memory_tags(bd);
95#endif
96#ifdef CONFIG_CMDLINE_TAG
97 setup_commandline_tag(bd, commandline);
98#endif
99#ifdef CONFIG_INITRD_TAG
100 if (images->rd_start && images->rd_end)
101 setup_initrd_tag(bd, images->rd_start, images->rd_end);
102#endif
103 setup_end_tag(bd);
104#endif
105
106 /* we assume that the kernel is in place */
107 printf("\nStarting kernel ...\n\n");
108
109#ifdef CONFIG_USB_DEVICE
110 {
111 extern void udc_disconnect(void);
112 udc_disconnect();
113 }
114#endif
rickf1113c92017-05-18 14:37:53 +0800115 }
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000116 cleanup_before_linux();
rickf1113c92017-05-18 14:37:53 +0800117 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len)
118 theKernel(0, machid, (unsigned long)images->ft_addr);
119 else
120 theKernel(0, machid, bd->bi_boot_params);
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000121 /* does not return */
122
123 return 1;
124}
125
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000126#if defined(CONFIG_SETUP_MEMORY_TAGS) || \
127 defined(CONFIG_CMDLINE_TAG) || \
128 defined(CONFIG_INITRD_TAG) || \
129 defined(CONFIG_SERIAL_TAG) || \
130 defined(CONFIG_REVISION_TAG)
131static void setup_start_tag(bd_t *bd)
132{
133 params = (struct tag *)bd->bi_boot_params;
134
135 params->hdr.tag = ATAG_CORE;
136 params->hdr.size = tag_size(tag_core);
137
138 params->u.core.flags = 0;
139 params->u.core.pagesize = 0;
140 params->u.core.rootdev = 0;
141
142 params = tag_next(params);
143}
144
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000145#ifdef CONFIG_SETUP_MEMORY_TAGS
146static void setup_memory_tags(bd_t *bd)
147{
148 int i;
149
150 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
151 params->hdr.tag = ATAG_MEM;
152 params->hdr.size = tag_size(tag_mem32);
153
154 params->u.mem.start = bd->bi_dram[i].start;
155 params->u.mem.size = bd->bi_dram[i].size;
156
157 params = tag_next(params);
158 }
159}
160#endif /* CONFIG_SETUP_MEMORY_TAGS */
161
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000162static void setup_commandline_tag(bd_t *bd, char *commandline)
163{
164 char *p;
165
166 if (!commandline)
167 return;
168
169 /* eat leading white space */
170 for (p = commandline; *p == ' '; p++)
171 ;
172
173 /* skip non-existent command lines so the kernel will still
174 * use its default command line.
175 */
176 if (*p == '\0')
177 return;
178
179 params->hdr.tag = ATAG_CMDLINE;
180 params->hdr.size =
181 (sizeof(struct tag_header) + strlen(p) + 1 + 4) >> 2;
182
183 strcpy(params->u.cmdline.cmdline, p)
184 ;
185
186 params = tag_next(params);
187}
188
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000189#ifdef CONFIG_INITRD_TAG
190static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end)
191{
192 /* an ATAG_INITRD node tells the kernel where the compressed
193 * ramdisk can be found. ATAG_RDIMG is a better name, actually.
194 */
195 params->hdr.tag = ATAG_INITRD2;
196 params->hdr.size = tag_size(tag_initrd);
197
198 params->u.initrd.start = initrd_start;
199 params->u.initrd.size = initrd_end - initrd_start;
200
201 params = tag_next(params);
202}
203#endif /* CONFIG_INITRD_TAG */
204
205#ifdef CONFIG_SERIAL_TAG
206void setup_serial_tag(struct tag **tmp)
207{
208 struct tag *params = *tmp;
209 struct tag_serialnr serialnr;
210 void get_board_serial(struct tag_serialnr *serialnr);
211
212 get_board_serial(&serialnr);
213 params->hdr.tag = ATAG_SERIAL;
214 params->hdr.size = tag_size(tag_serialnr);
215 params->u.serialnr.low = serialnr.low;
216 params->u.serialnr.high = serialnr.high;
217 params = tag_next(params);
218 *tmp = params;
219}
220#endif
221
222#ifdef CONFIG_REVISION_TAG
223void setup_revision_tag(struct tag **in_params)
224{
225 u32 rev = 0;
226 u32 get_board_rev(void);
227
228 rev = get_board_rev();
229 params->hdr.tag = ATAG_REVISION;
230 params->hdr.size = tag_size(tag_revision);
231 params->u.revision.rev = rev;
232 params = tag_next(params);
233}
234#endif /* CONFIG_REVISION_TAG */
235
Macpaul Lin0d1f4742011-10-11 22:33:19 +0000236static void setup_end_tag(bd_t *bd)
237{
238 params->hdr.tag = ATAG_NONE;
239 params->hdr.size = 0;
240}
241
242#endif /* CONFIG_SETUP_MEMORY_TAGS || CONFIG_CMDLINE_TAG || CONFIG_INITRD_TAG */