]> Git Repo - J-u-boot.git/blob - arch/arm/lib/bootm.c
bootm: Adjust arguments of boot_os_fn
[J-u-boot.git] / arch / arm / lib / bootm.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /* Copyright (C) 2011
3  * Corscience GmbH & Co. KG - Simon Schwarz <[email protected]>
4  *  - Added prep subcommand support
5  *  - Reorganized source - modeled after powerpc version
6  *
7  * (C) Copyright 2002
8  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
9  * Marius Groeger <[email protected]>
10  *
11  * Copyright (C) 2001  Erik Mouw ([email protected])
12  */
13
14 #include <common.h>
15 #include <bootm.h>
16 #include <bootstage.h>
17 #include <command.h>
18 #include <cpu_func.h>
19 #include <dm.h>
20 #include <log.h>
21 #include <asm/global_data.h>
22 #include <dm/root.h>
23 #include <env.h>
24 #include <image.h>
25 #include <u-boot/zlib.h>
26 #include <asm/byteorder.h>
27 #include <linux/libfdt.h>
28 #include <mapmem.h>
29 #include <fdt_support.h>
30 #include <asm/bootm.h>
31 #include <asm/secure.h>
32 #include <linux/compiler.h>
33 #include <bootm.h>
34 #include <vxworks.h>
35 #include <asm/cache.h>
36
37 #ifdef CONFIG_ARMV7_NONSEC
38 #include <asm/armv7.h>
39 #endif
40 #include <asm/setup.h>
41
42 DECLARE_GLOBAL_DATA_PTR;
43
44 static struct tag *params;
45
46 __weak void board_quiesce_devices(void)
47 {
48 }
49
50 /**
51  * announce_and_cleanup() - Print message and prepare for kernel boot
52  *
53  * @fake: non-zero to do everything except actually boot
54  */
55 static void announce_and_cleanup(int fake)
56 {
57         bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
58 #ifdef CONFIG_BOOTSTAGE_FDT
59         bootstage_fdt_add_report();
60 #endif
61 #ifdef CONFIG_BOOTSTAGE_REPORT
62         bootstage_report();
63 #endif
64
65 #ifdef CONFIG_USB_DEVICE
66         udc_disconnect();
67 #endif
68
69         board_quiesce_devices();
70
71         printf("\nStarting kernel ...%s\n\n", fake ?
72                 "(fake run for tracing)" : "");
73         /*
74          * Call remove function of all devices with a removal flag set.
75          * This may be useful for last-stage operations, like cancelling
76          * of DMA operation or releasing device internal buffers.
77          */
78         dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL | DM_REMOVE_NON_VITAL);
79
80         /* Remove all active vital devices next */
81         dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
82
83         cleanup_before_linux();
84 }
85
86 static void setup_start_tag (struct bd_info *bd)
87 {
88         params = (struct tag *)bd->bi_boot_params;
89
90         params->hdr.tag = ATAG_CORE;
91         params->hdr.size = tag_size (tag_core);
92
93         params->u.core.flags = 0;
94         params->u.core.pagesize = 0;
95         params->u.core.rootdev = 0;
96
97         params = tag_next (params);
98 }
99
100 static void setup_memory_tags(struct bd_info *bd)
101 {
102         int i;
103
104         for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
105                 params->hdr.tag = ATAG_MEM;
106                 params->hdr.size = tag_size (tag_mem32);
107
108                 params->u.mem.start = bd->bi_dram[i].start;
109                 params->u.mem.size = bd->bi_dram[i].size;
110
111                 params = tag_next (params);
112         }
113 }
114
115 static void setup_commandline_tag(struct bd_info *bd, char *commandline)
116 {
117         char *p;
118
119         if (!commandline)
120                 return;
121
122         /* eat leading white space */
123         for (p = commandline; *p == ' '; p++);
124
125         /* skip non-existent command lines so the kernel will still
126          * use its default command line.
127          */
128         if (*p == '\0')
129                 return;
130
131         params->hdr.tag = ATAG_CMDLINE;
132         params->hdr.size =
133                 (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2;
134
135         strcpy (params->u.cmdline.cmdline, p);
136
137         params = tag_next (params);
138 }
139
140 static void setup_initrd_tag(struct bd_info *bd, ulong initrd_start,
141                              ulong initrd_end)
142 {
143         /* an ATAG_INITRD node tells the kernel where the compressed
144          * ramdisk can be found. ATAG_RDIMG is a better name, actually.
145          */
146         params->hdr.tag = ATAG_INITRD2;
147         params->hdr.size = tag_size (tag_initrd);
148
149         params->u.initrd.start = initrd_start;
150         params->u.initrd.size = initrd_end - initrd_start;
151
152         params = tag_next (params);
153 }
154
155 static void setup_serial_tag(struct tag **tmp)
156 {
157         struct tag *params = *tmp;
158         struct tag_serialnr serialnr;
159
160         get_board_serial(&serialnr);
161         params->hdr.tag = ATAG_SERIAL;
162         params->hdr.size = tag_size (tag_serialnr);
163         params->u.serialnr.low = serialnr.low;
164         params->u.serialnr.high= serialnr.high;
165         params = tag_next (params);
166         *tmp = params;
167 }
168
169 static void setup_revision_tag(struct tag **in_params)
170 {
171         u32 rev = 0;
172
173         rev = get_board_rev();
174         params->hdr.tag = ATAG_REVISION;
175         params->hdr.size = tag_size (tag_revision);
176         params->u.revision.rev = rev;
177         params = tag_next (params);
178 }
179
180 static void setup_end_tag(struct bd_info *bd)
181 {
182         params->hdr.tag = ATAG_NONE;
183         params->hdr.size = 0;
184 }
185
186 __weak void setup_board_tags(struct tag **in_params) {}
187
188 #ifdef CONFIG_ARM64
189 static void do_nonsec_virt_switch(void)
190 {
191         smp_kick_all_cpus();
192         dcache_disable();       /* flush cache before swtiching to EL2 */
193 }
194 #endif
195
196 __weak void board_prep_linux(struct bootm_headers *images) { }
197
198 /* Subcommand: PREP */
199 static void boot_prep_linux(struct bootm_headers *images)
200 {
201         char *commandline = env_get("bootargs");
202
203         if (CONFIG_IS_ENABLED(OF_LIBFDT) && IS_ENABLED(CONFIG_LMB) && images->ft_len) {
204                 debug("using: FDT\n");
205                 if (image_setup_linux(images)) {
206                         panic("FDT creation failed!");
207                 }
208         } else if (BOOTM_ENABLE_TAGS) {
209                 debug("using: ATAGS\n");
210                 setup_start_tag(gd->bd);
211                 if (BOOTM_ENABLE_SERIAL_TAG)
212                         setup_serial_tag(&params);
213                 if (BOOTM_ENABLE_CMDLINE_TAG)
214                         setup_commandline_tag(gd->bd, commandline);
215                 if (BOOTM_ENABLE_REVISION_TAG)
216                         setup_revision_tag(&params);
217                 if (BOOTM_ENABLE_MEMORY_TAGS)
218                         setup_memory_tags(gd->bd);
219                 if (BOOTM_ENABLE_INITRD_TAG) {
220                         /*
221                          * In boot_ramdisk_high(), it may relocate ramdisk to
222                          * a specified location. And set images->initrd_start &
223                          * images->initrd_end to relocated ramdisk's start/end
224                          * addresses. So use them instead of images->rd_start &
225                          * images->rd_end when possible.
226                          */
227                         if (images->initrd_start && images->initrd_end) {
228                                 setup_initrd_tag(gd->bd, images->initrd_start,
229                                                  images->initrd_end);
230                         } else if (images->rd_start && images->rd_end) {
231                                 setup_initrd_tag(gd->bd, images->rd_start,
232                                                  images->rd_end);
233                         }
234                 }
235                 setup_board_tags(&params);
236                 setup_end_tag(gd->bd);
237         } else {
238                 panic("FDT and ATAGS support not compiled in\n");
239         }
240
241         board_prep_linux(images);
242 }
243
244 __weak bool armv7_boot_nonsec_default(void)
245 {
246 #ifdef CONFIG_ARMV7_BOOT_SEC_DEFAULT
247         return false;
248 #else
249         return true;
250 #endif
251 }
252
253 #ifdef CONFIG_ARMV7_NONSEC
254 bool armv7_boot_nonsec(void)
255 {
256         char *s = env_get("bootm_boot_mode");
257         bool nonsec = armv7_boot_nonsec_default();
258
259         if (s && !strcmp(s, "sec"))
260                 nonsec = false;
261
262         if (s && !strcmp(s, "nonsec"))
263                 nonsec = true;
264
265         return nonsec;
266 }
267 #endif
268
269 #ifdef CONFIG_ARM64
270 __weak void update_os_arch_secondary_cores(uint8_t os_arch)
271 {
272 }
273
274 #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
275 static void switch_to_el1(void)
276 {
277         if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) &&
278             (images.os.arch == IH_ARCH_ARM))
279                 armv8_switch_to_el1(0, (u64)gd->bd->bi_arch_number,
280                                     (u64)images.ft_addr, 0,
281                                     (u64)images.ep,
282                                     ES_TO_AARCH32);
283         else
284                 armv8_switch_to_el1((u64)images.ft_addr, 0, 0, 0,
285                                     images.ep,
286                                     ES_TO_AARCH64);
287 }
288 #endif
289 #endif
290
291 /* Subcommand: GO */
292 static void boot_jump_linux(struct bootm_headers *images, int flag)
293 {
294 #ifdef CONFIG_ARM64
295         void (*kernel_entry)(void *fdt_addr, void *res0, void *res1,
296                         void *res2);
297         int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
298
299         kernel_entry = (void (*)(void *fdt_addr, void *res0, void *res1,
300                                 void *res2))images->ep;
301
302         debug("## Transferring control to Linux (at address %lx)...\n",
303                 (ulong) kernel_entry);
304         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
305
306         announce_and_cleanup(fake);
307
308         if (!fake) {
309 #ifdef CONFIG_ARMV8_PSCI
310                 armv8_setup_psci();
311 #endif
312                 do_nonsec_virt_switch();
313
314                 update_os_arch_secondary_cores(images->os.arch);
315
316 #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
317                 armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0,
318                                     (u64)switch_to_el1, ES_TO_AARCH64);
319 #else
320                 if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) &&
321                     (images->os.arch == IH_ARCH_ARM))
322                         armv8_switch_to_el2(0, (u64)gd->bd->bi_arch_number,
323                                             (u64)images->ft_addr, 0,
324                                             (u64)images->ep,
325                                             ES_TO_AARCH32);
326                 else
327                         armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0,
328                                             images->ep,
329                                             ES_TO_AARCH64);
330 #endif
331         }
332 #else
333         unsigned long machid = gd->bd->bi_arch_number;
334         char *s;
335         void (*kernel_entry)(int zero, int arch, uint params);
336         unsigned long r2;
337         int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
338
339         kernel_entry = (void (*)(int, int, uint))images->ep;
340 #ifdef CONFIG_CPU_V7M
341         ulong addr = (ulong)kernel_entry | 1;
342         kernel_entry = (void *)addr;
343 #endif
344         s = env_get("machid");
345         if (s) {
346                 if (strict_strtoul(s, 16, &machid) < 0) {
347                         debug("strict_strtoul failed!\n");
348                         return;
349                 }
350                 printf("Using machid 0x%lx from environment\n", machid);
351         }
352
353         debug("## Transferring control to Linux (at address %08lx)" \
354                 "...\n", (ulong) kernel_entry);
355         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
356         announce_and_cleanup(fake);
357
358         if (CONFIG_IS_ENABLED(OF_LIBFDT) && images->ft_len)
359                 r2 = (unsigned long)images->ft_addr;
360         else
361                 r2 = gd->bd->bi_boot_params;
362
363         if (!fake) {
364 #ifdef CONFIG_ARMV7_NONSEC
365                 if (armv7_boot_nonsec()) {
366                         armv7_init_nonsec();
367                         secure_ram_addr(_do_nonsec_entry)(kernel_entry,
368                                                           0, machid, r2);
369                 } else
370 #endif
371                         kernel_entry(0, machid, r2);
372         }
373 #endif
374 }
375
376 /* Main Entry point for arm bootm implementation
377  *
378  * Modeled after the powerpc implementation
379  * DIFFERENCE: Instead of calling prep and go at the end
380  * they are called if subcommand is equal 0.
381  */
382 int do_bootm_linux(int flag, struct bootm_info *bmi)
383 {
384         struct bootm_headers *images = bmi->images;
385
386         /* No need for those on ARM */
387         if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
388                 return -1;
389
390         if (flag & BOOTM_STATE_OS_PREP) {
391                 boot_prep_linux(images);
392                 return 0;
393         }
394
395         if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
396                 boot_jump_linux(images, flag);
397                 return 0;
398         }
399
400         boot_prep_linux(images);
401         boot_jump_linux(images, flag);
402         return 0;
403 }
404
405 #if defined(CONFIG_BOOTM_VXWORKS)
406 void boot_prep_vxworks(struct bootm_headers *images)
407 {
408 #if defined(CONFIG_OF_LIBFDT)
409         int off;
410
411         if (images->ft_addr) {
412                 off = fdt_path_offset(images->ft_addr, "/memory");
413                 if (off > 0) {
414                         if (arch_fixup_fdt(images->ft_addr))
415                                 puts("## WARNING: fixup memory failed!\n");
416                 }
417         }
418 #endif
419         cleanup_before_linux();
420 }
421
422 void boot_jump_vxworks(struct bootm_headers *images)
423 {
424 #if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI)
425         armv8_setup_psci();
426         smp_kick_all_cpus();
427 #endif
428
429         /* ARM VxWorks requires device tree physical address to be passed */
430         ((void (*)(void *))images->ep)(images->ft_addr);
431 }
432 #endif
This page took 0.048405 seconds and 4 git commands to generate.