xref: /openbmc/u-boot/arch/nds32/lib/bootm.c (revision e8f80a5a)
1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2463d47f6SMacpaul Lin /*
3463d47f6SMacpaul Lin  * Copyright (C) 2011 Andes Technology Corporation
4463d47f6SMacpaul Lin  * Shawn Lin, Andes Technology Corporation <nobuhiro@andestech.com>
5463d47f6SMacpaul Lin  * Macpaul Lin, Andes Technology Corporation <macpaul@andestech.com>
6463d47f6SMacpaul Lin  */
7463d47f6SMacpaul Lin 
8463d47f6SMacpaul Lin #include <common.h>
9463d47f6SMacpaul Lin #include <command.h>
10463d47f6SMacpaul Lin #include <image.h>
11463d47f6SMacpaul Lin #include <u-boot/zlib.h>
12463d47f6SMacpaul Lin #include <asm/byteorder.h>
13b841b6e9Srick #include <asm/bootm.h>
14463d47f6SMacpaul Lin 
15463d47f6SMacpaul Lin DECLARE_GLOBAL_DATA_PTR;
16463d47f6SMacpaul Lin 
17463d47f6SMacpaul Lin #if defined(CONFIG_SETUP_MEMORY_TAGS) || \
18463d47f6SMacpaul Lin 	defined(CONFIG_CMDLINE_TAG) || \
19463d47f6SMacpaul Lin 	defined(CONFIG_INITRD_TAG) || \
20463d47f6SMacpaul Lin 	defined(CONFIG_SERIAL_TAG) || \
21463d47f6SMacpaul Lin 	defined(CONFIG_REVISION_TAG)
22463d47f6SMacpaul Lin static void setup_start_tag(bd_t *bd);
23463d47f6SMacpaul Lin 
24463d47f6SMacpaul Lin # ifdef CONFIG_SETUP_MEMORY_TAGS
25463d47f6SMacpaul Lin static void setup_memory_tags(bd_t *bd);
26463d47f6SMacpaul Lin # endif
27463d47f6SMacpaul Lin static void setup_commandline_tag(bd_t *bd, char *commandline);
28463d47f6SMacpaul Lin 
29463d47f6SMacpaul Lin # ifdef CONFIG_INITRD_TAG
30463d47f6SMacpaul Lin static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end);
31463d47f6SMacpaul Lin # endif
32463d47f6SMacpaul Lin static void setup_end_tag(bd_t *bd);
33463d47f6SMacpaul Lin 
34463d47f6SMacpaul Lin static struct tag *params;
35463d47f6SMacpaul Lin #endif /* CONFIG_SETUP_MEMORY_TAGS || CONFIG_CMDLINE_TAG || CONFIG_INITRD_TAG */
36463d47f6SMacpaul Lin 
do_bootm_linux(int flag,int argc,char * argv[],bootm_headers_t * images)37463d47f6SMacpaul Lin int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images)
38463d47f6SMacpaul Lin {
39463d47f6SMacpaul Lin 	bd_t	*bd = gd->bd;
40463d47f6SMacpaul Lin 	char	*s;
41463d47f6SMacpaul Lin 	int	machid = bd->bi_arch_number;
42463d47f6SMacpaul Lin 	void	(*theKernel)(int zero, int arch, uint params);
43463d47f6SMacpaul Lin 
44463d47f6SMacpaul Lin #ifdef CONFIG_CMDLINE_TAG
4500caae6dSSimon Glass 	char *commandline = env_get("bootargs");
46463d47f6SMacpaul Lin #endif
47463d47f6SMacpaul Lin 
482cb0e55aSAndreas Bießmann 	/*
492cb0e55aSAndreas Bießmann 	 * allow the PREP bootm subcommand, it is required for bootm to work
502cb0e55aSAndreas Bießmann 	 */
512cb0e55aSAndreas Bießmann 	if (flag & BOOTM_STATE_OS_PREP)
522cb0e55aSAndreas Bießmann 		return 0;
532cb0e55aSAndreas Bießmann 
54463d47f6SMacpaul Lin 	if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
55463d47f6SMacpaul Lin 		return 1;
56463d47f6SMacpaul Lin 
57463d47f6SMacpaul Lin 	theKernel = (void (*)(int, int, uint))images->ep;
58463d47f6SMacpaul Lin 
5900caae6dSSimon Glass 	s = env_get("machid");
60463d47f6SMacpaul Lin 	if (s) {
61463d47f6SMacpaul Lin 		machid = simple_strtoul(s, NULL, 16);
62463d47f6SMacpaul Lin 		printf("Using machid 0x%x from environment\n", machid);
63463d47f6SMacpaul Lin 	}
64463d47f6SMacpaul Lin 
65770605e4SSimon Glass 	bootstage_mark(BOOTSTAGE_ID_RUN_OS);
66463d47f6SMacpaul Lin 
67463d47f6SMacpaul Lin 	debug("## Transferring control to Linux (at address %08lx) ...\n",
68463d47f6SMacpaul Lin 	       (ulong)theKernel);
69463d47f6SMacpaul Lin 
70b841b6e9Srick 	if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
71b841b6e9Srick #ifdef CONFIG_OF_LIBFDT
72b841b6e9Srick 		debug("using: FDT\n");
73b841b6e9Srick 		if (image_setup_linux(images)) {
74b841b6e9Srick 			printf("FDT creation failed! hanging...");
75b841b6e9Srick 			hang();
76b841b6e9Srick 		}
77b841b6e9Srick #endif
78b841b6e9Srick 	} else if (BOOTM_ENABLE_TAGS) {
79463d47f6SMacpaul Lin #if defined(CONFIG_SETUP_MEMORY_TAGS) || \
80463d47f6SMacpaul Lin 	defined(CONFIG_CMDLINE_TAG) || \
81463d47f6SMacpaul Lin 	defined(CONFIG_INITRD_TAG) || \
82463d47f6SMacpaul Lin 	defined(CONFIG_SERIAL_TAG) || \
83463d47f6SMacpaul Lin 	defined(CONFIG_REVISION_TAG)
84463d47f6SMacpaul Lin 	setup_start_tag(bd);
85463d47f6SMacpaul Lin #ifdef CONFIG_SERIAL_TAG
86463d47f6SMacpaul Lin 	setup_serial_tag(&params);
87463d47f6SMacpaul Lin #endif
88463d47f6SMacpaul Lin #ifdef CONFIG_REVISION_TAG
89463d47f6SMacpaul Lin 	setup_revision_tag(&params);
90463d47f6SMacpaul Lin #endif
91463d47f6SMacpaul Lin #ifdef CONFIG_SETUP_MEMORY_TAGS
92463d47f6SMacpaul Lin 	setup_memory_tags(bd);
93463d47f6SMacpaul Lin #endif
94463d47f6SMacpaul Lin #ifdef CONFIG_CMDLINE_TAG
95463d47f6SMacpaul Lin 	setup_commandline_tag(bd, commandline);
96463d47f6SMacpaul Lin #endif
97463d47f6SMacpaul Lin #ifdef CONFIG_INITRD_TAG
98463d47f6SMacpaul Lin 	if (images->rd_start && images->rd_end)
99463d47f6SMacpaul Lin 		setup_initrd_tag(bd, images->rd_start, images->rd_end);
100463d47f6SMacpaul Lin #endif
101463d47f6SMacpaul Lin 	setup_end_tag(bd);
102463d47f6SMacpaul Lin #endif
103463d47f6SMacpaul Lin 
104463d47f6SMacpaul Lin 	/* we assume that the kernel is in place */
105463d47f6SMacpaul Lin 	printf("\nStarting kernel ...\n\n");
106463d47f6SMacpaul Lin 
107463d47f6SMacpaul Lin #ifdef CONFIG_USB_DEVICE
108463d47f6SMacpaul Lin 	{
109463d47f6SMacpaul Lin 		extern void udc_disconnect(void);
110463d47f6SMacpaul Lin 		udc_disconnect();
111463d47f6SMacpaul Lin 	}
112463d47f6SMacpaul Lin #endif
113b841b6e9Srick 	}
114463d47f6SMacpaul Lin 	cleanup_before_linux();
115b841b6e9Srick 	if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len)
116b841b6e9Srick 		theKernel(0, machid, (unsigned long)images->ft_addr);
117b841b6e9Srick 	else
118463d47f6SMacpaul Lin 		theKernel(0, machid, bd->bi_boot_params);
119463d47f6SMacpaul Lin 	/* does not return */
120463d47f6SMacpaul Lin 
121463d47f6SMacpaul Lin 	return 1;
122463d47f6SMacpaul Lin }
123463d47f6SMacpaul Lin 
124463d47f6SMacpaul Lin #if defined(CONFIG_SETUP_MEMORY_TAGS) || \
125463d47f6SMacpaul Lin 	defined(CONFIG_CMDLINE_TAG) || \
126463d47f6SMacpaul Lin 	defined(CONFIG_INITRD_TAG) || \
127463d47f6SMacpaul Lin 	defined(CONFIG_SERIAL_TAG) || \
128463d47f6SMacpaul Lin 	defined(CONFIG_REVISION_TAG)
setup_start_tag(bd_t * bd)129463d47f6SMacpaul Lin static void setup_start_tag(bd_t *bd)
130463d47f6SMacpaul Lin {
131463d47f6SMacpaul Lin 	params = (struct tag *)bd->bi_boot_params;
132463d47f6SMacpaul Lin 
133463d47f6SMacpaul Lin 	params->hdr.tag = ATAG_CORE;
134463d47f6SMacpaul Lin 	params->hdr.size = tag_size(tag_core);
135463d47f6SMacpaul Lin 
136463d47f6SMacpaul Lin 	params->u.core.flags = 0;
137463d47f6SMacpaul Lin 	params->u.core.pagesize = 0;
138463d47f6SMacpaul Lin 	params->u.core.rootdev = 0;
139463d47f6SMacpaul Lin 
140463d47f6SMacpaul Lin 	params = tag_next(params);
141463d47f6SMacpaul Lin }
142463d47f6SMacpaul Lin 
143463d47f6SMacpaul Lin #ifdef CONFIG_SETUP_MEMORY_TAGS
setup_memory_tags(bd_t * bd)144463d47f6SMacpaul Lin static void setup_memory_tags(bd_t *bd)
145463d47f6SMacpaul Lin {
146463d47f6SMacpaul Lin 	int i;
147463d47f6SMacpaul Lin 
148463d47f6SMacpaul Lin 	for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
149463d47f6SMacpaul Lin 		params->hdr.tag = ATAG_MEM;
150463d47f6SMacpaul Lin 		params->hdr.size = tag_size(tag_mem32);
151463d47f6SMacpaul Lin 
152463d47f6SMacpaul Lin 		params->u.mem.start = bd->bi_dram[i].start;
153463d47f6SMacpaul Lin 		params->u.mem.size = bd->bi_dram[i].size;
154463d47f6SMacpaul Lin 
155463d47f6SMacpaul Lin 		params = tag_next(params);
156463d47f6SMacpaul Lin 	}
157463d47f6SMacpaul Lin }
158463d47f6SMacpaul Lin #endif /* CONFIG_SETUP_MEMORY_TAGS */
159463d47f6SMacpaul Lin 
setup_commandline_tag(bd_t * bd,char * commandline)160463d47f6SMacpaul Lin static void setup_commandline_tag(bd_t *bd, char *commandline)
161463d47f6SMacpaul Lin {
162463d47f6SMacpaul Lin 	char *p;
163463d47f6SMacpaul Lin 
164463d47f6SMacpaul Lin 	if (!commandline)
165463d47f6SMacpaul Lin 		return;
166463d47f6SMacpaul Lin 
167463d47f6SMacpaul Lin 	/* eat leading white space */
168463d47f6SMacpaul Lin 	for (p = commandline; *p == ' '; p++)
169463d47f6SMacpaul Lin 		;
170463d47f6SMacpaul Lin 
171463d47f6SMacpaul Lin 	/* skip non-existent command lines so the kernel will still
172463d47f6SMacpaul Lin 	 * use its default command line.
173463d47f6SMacpaul Lin 	 */
174463d47f6SMacpaul Lin 	if (*p == '\0')
175463d47f6SMacpaul Lin 		return;
176463d47f6SMacpaul Lin 
177463d47f6SMacpaul Lin 	params->hdr.tag = ATAG_CMDLINE;
178463d47f6SMacpaul Lin 	params->hdr.size =
179463d47f6SMacpaul Lin 		(sizeof(struct tag_header) + strlen(p) + 1 + 4) >> 2;
180463d47f6SMacpaul Lin 
181463d47f6SMacpaul Lin 	strcpy(params->u.cmdline.cmdline, p)
182463d47f6SMacpaul Lin 		;
183463d47f6SMacpaul Lin 
184463d47f6SMacpaul Lin 	params = tag_next(params);
185463d47f6SMacpaul Lin }
186463d47f6SMacpaul Lin 
187463d47f6SMacpaul Lin #ifdef CONFIG_INITRD_TAG
setup_initrd_tag(bd_t * bd,ulong initrd_start,ulong initrd_end)188463d47f6SMacpaul Lin static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end)
189463d47f6SMacpaul Lin {
190463d47f6SMacpaul Lin 	/* an ATAG_INITRD node tells the kernel where the compressed
191463d47f6SMacpaul Lin 	 * ramdisk can be found. ATAG_RDIMG is a better name, actually.
192463d47f6SMacpaul Lin 	 */
193463d47f6SMacpaul Lin 	params->hdr.tag = ATAG_INITRD2;
194463d47f6SMacpaul Lin 	params->hdr.size = tag_size(tag_initrd);
195463d47f6SMacpaul Lin 
196463d47f6SMacpaul Lin 	params->u.initrd.start = initrd_start;
197463d47f6SMacpaul Lin 	params->u.initrd.size = initrd_end - initrd_start;
198463d47f6SMacpaul Lin 
199463d47f6SMacpaul Lin 	params = tag_next(params);
200463d47f6SMacpaul Lin }
201463d47f6SMacpaul Lin #endif /* CONFIG_INITRD_TAG */
202463d47f6SMacpaul Lin 
203463d47f6SMacpaul Lin #ifdef CONFIG_SERIAL_TAG
setup_serial_tag(struct tag ** tmp)204463d47f6SMacpaul Lin void setup_serial_tag(struct tag **tmp)
205463d47f6SMacpaul Lin {
206463d47f6SMacpaul Lin 	struct tag *params = *tmp;
207463d47f6SMacpaul Lin 	struct tag_serialnr serialnr;
208463d47f6SMacpaul Lin 	void get_board_serial(struct tag_serialnr *serialnr);
209463d47f6SMacpaul Lin 
210463d47f6SMacpaul Lin 	get_board_serial(&serialnr);
211463d47f6SMacpaul Lin 	params->hdr.tag = ATAG_SERIAL;
212463d47f6SMacpaul Lin 	params->hdr.size = tag_size(tag_serialnr);
213463d47f6SMacpaul Lin 	params->u.serialnr.low = serialnr.low;
214463d47f6SMacpaul Lin 	params->u.serialnr.high = serialnr.high;
215463d47f6SMacpaul Lin 	params = tag_next(params);
216463d47f6SMacpaul Lin 	*tmp = params;
217463d47f6SMacpaul Lin }
218463d47f6SMacpaul Lin #endif
219463d47f6SMacpaul Lin 
220463d47f6SMacpaul Lin #ifdef CONFIG_REVISION_TAG
setup_revision_tag(struct tag ** in_params)221463d47f6SMacpaul Lin void setup_revision_tag(struct tag **in_params)
222463d47f6SMacpaul Lin {
223463d47f6SMacpaul Lin 	u32 rev = 0;
224463d47f6SMacpaul Lin 	u32 get_board_rev(void);
225463d47f6SMacpaul Lin 
226463d47f6SMacpaul Lin 	rev = get_board_rev();
227463d47f6SMacpaul Lin 	params->hdr.tag = ATAG_REVISION;
228463d47f6SMacpaul Lin 	params->hdr.size = tag_size(tag_revision);
229463d47f6SMacpaul Lin 	params->u.revision.rev = rev;
230463d47f6SMacpaul Lin 	params = tag_next(params);
231463d47f6SMacpaul Lin }
232463d47f6SMacpaul Lin #endif  /* CONFIG_REVISION_TAG */
233463d47f6SMacpaul Lin 
setup_end_tag(bd_t * bd)234463d47f6SMacpaul Lin static void setup_end_tag(bd_t *bd)
235463d47f6SMacpaul Lin {
236463d47f6SMacpaul Lin 	params->hdr.tag = ATAG_NONE;
237463d47f6SMacpaul Lin 	params->hdr.size = 0;
238463d47f6SMacpaul Lin }
239463d47f6SMacpaul Lin 
240463d47f6SMacpaul Lin #endif /* CONFIG_SETUP_MEMORY_TAGS || CONFIG_CMDLINE_TAG || CONFIG_INITRD_TAG */
241