blob: 4f76e80b20f1a68c1463757a0be65c89cc84f26a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Pali Rohár248ef0a2012-10-29 07:54:01 +00002/*
3 * (C) Copyright 2011-2012
Pali Rohár10a953d2020-04-01 00:35:08 +02004 * Pali Rohár <pali@kernel.org>
Pali Rohár248ef0a2012-10-29 07:54:01 +00005 */
6
7#include <config.h>
8
9relocaddr: /* address of this relocaddr section after coping */
10 .word . /* address of section (calculated at compile time) */
11
12startaddr: /* address of u-boot after copying */
13 .word CONFIG_SYS_TEXT_BASE
14
15kernaddr: /* address of kernel after copying */
16 .word KERNEL_ADDRESS
17
18kernsize: /* maximal size of kernel image */
19 .word KERNEL_MAXSIZE
20
21kernoffs: /* offset of kernel image in loaded u-boot */
22 .word KERNEL_OFFSET
23
24imagesize: /* maximal size of image */
25 .word IMAGE_MAXSIZE
26
27ih_magic: /* IH_MAGIC in big endian from include/image.h */
28 .word 0x56190527
29
Pali Rohár0a8825c2021-06-18 15:27:03 +020030z_magic: /* LINUX_ARM_ZIMAGE_MAGIC */
31 .word 0x016f2818
32
Pali Rohár248ef0a2012-10-29 07:54:01 +000033/*
34 * Routine: save_boot_params (called after reset from start.S)
35 * Description: Copy attached kernel to address KERNEL_ADDRESS
Pali Rohár248ef0a2012-10-29 07:54:01 +000036 */
37
38.global save_boot_params
39save_boot_params:
Pali Rohár248ef0a2012-10-29 07:54:01 +000040
Pali Rohárc1aec222022-08-11 22:27:22 +020041/*
42 * Copy valid attached kernel to absolute address KERNEL_ADDRESS
43 *
44 * Nokia X-Loader is loading secondary image to address 0x80400000.
45 * NOLO is loading boot image to random place, so it doesn't really
46 * matter what is set in CONFIG_SYS_TEXT_BASE. We have to detect
47 * KERNEL_OFFSET from the current execution address and copy it to
48 * absolute address KERNEL_ADDRESS.
49 *
50 * Note that U-Boot has to be compiled with CONFIG_POSITION_INDEPENDENT
51 * because it is loaded at random address and not to the fixed address
52 * (CONFIG_SYS_TEXT_BASE).
53 */
Pali Rohár248ef0a2012-10-29 07:54:01 +000054
55copy_kernel_start:
56 adr r0, relocaddr /* r0 - address of section relocaddr */
57 ldr r1, relocaddr /* r1 - address of relocaddr after relocation */
Pali Rohár248ef0a2012-10-29 07:54:01 +000058
59 /* r4 - calculated offset */
Pali Rohár39e486e2022-08-11 22:27:23 +020060 sub r4, r0, r1
Pali Rohár248ef0a2012-10-29 07:54:01 +000061
62 /* r0 - start of kernel before */
63 ldr r0, startaddr
Pali Rohár39e486e2022-08-11 22:27:23 +020064 add r0, r0, r4
Pali Rohár248ef0a2012-10-29 07:54:01 +000065 ldr r1, kernoffs
66 add r0, r0, r1
67
68 /* r3 - start of kernel after */
69 ldr r3, kernaddr
70
71 /* r2 - end of kernel after */
72 ldr r1, kernsize
73 add r2, r3, r1
74
75 /* r1 - end of kernel before */
76 add r1, r0, r1
77
78 /* remove header in target kernel */
79 mov r5, #0
Pali Rohárf8a6c902022-08-11 22:27:21 +020080 str r5, [r3] /* remove 4 bytes header of kernel uImage */
81 str r5, [r3, #36] /* remove 4 bytes header of kernel zImage */
Pali Rohár248ef0a2012-10-29 07:54:01 +000082
83 /* check for valid kernel uImage */
84 ldr r4, [r0] /* r4 - 4 bytes header of kernel */
85 ldr r5, ih_magic /* r5 - IH_MAGIC */
86 cmp r4, r5
Pali Rohár0a8825c2021-06-18 15:27:03 +020087 beq copy_kernel_loop
88
89 /* check for valid kernel zImage */
90 ldr r4, [r0, #36] /* r4 - 4 bytes header of kernel at offset 36 */
91 ldr r5, z_magic /* r5 - LINUX_ARM_ZIMAGE_MAGIC */
92 cmp r4, r5
Pali Rohár248ef0a2012-10-29 07:54:01 +000093 bne copy_kernel_end /* skip if invalid image */
94
95copy_kernel_loop:
96 ldmdb r1!, {r3 - r10}
97 stmdb r2!, {r3 - r10}
98 cmp r1, r0
99 bhi copy_kernel_loop
100
101copy_kernel_end:
Pali Rohárf8a6c902022-08-11 22:27:21 +0200102
103 /* remove header in source kernel image */
Pali Rohár248ef0a2012-10-29 07:54:01 +0000104 mov r5, #0
Pali Rohár0a8825c2021-06-18 15:27:03 +0200105 str r5, [r0] /* remove 4 bytes header of kernel uImage */
106 str r5, [r0, #36] /* remove 4 bytes header of kernel zImage */
Pali Rohár248ef0a2012-10-29 07:54:01 +0000107
Pali Rohárc1aec222022-08-11 22:27:22 +0200108 /* Returns */
109 b save_boot_params_ret