blob: 44f32f114e756e7a7fa3876886e5f9cc8788dfd5 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Pali Roháred407be2012-10-29 07:54:01 +00002/*
3 * (C) Copyright 2011-2012
Pali Rohár53086652020-04-01 00:35:08 +02004 * Pali Rohár <pali@kernel.org>
Pali Roháred407be2012-10-29 07:54:01 +00005 */
6
7#include <config.h>
Pali Rohár544071a2022-11-24 00:45:00 +01008#include <linux/linkage.h>
Pali Roháred407be2012-10-29 07:54:01 +00009
Pali Rohár0ba6ce42022-08-11 22:27:24 +020010kernoffs: /* offset of kernel image from this address */
Simon Glass98463902022-10-20 18:22:39 -060011 .word . - CONFIG_TEXT_BASE - KERNEL_OFFSET
Pali Roháred407be2012-10-29 07:54:01 +000012
13kernaddr: /* address of kernel after copying */
14 .word KERNEL_ADDRESS
15
16kernsize: /* maximal size of kernel image */
17 .word KERNEL_MAXSIZE
18
Pali Roháred407be2012-10-29 07:54:01 +000019imagesize: /* maximal size of image */
20 .word IMAGE_MAXSIZE
21
22ih_magic: /* IH_MAGIC in big endian from include/image.h */
23 .word 0x56190527
24
Pali Rohárcc434fc2021-06-18 15:27:03 +020025z_magic: /* LINUX_ARM_ZIMAGE_MAGIC */
26 .word 0x016f2818
27
Pali Roháred407be2012-10-29 07:54:01 +000028/*
29 * Routine: save_boot_params (called after reset from start.S)
30 * Description: Copy attached kernel to address KERNEL_ADDRESS
Pali Roháred407be2012-10-29 07:54:01 +000031 */
32
Pali Rohár544071a2022-11-24 00:45:00 +010033ENTRY(save_boot_params)
Pali Roháred407be2012-10-29 07:54:01 +000034
Pali Rohárc5be5f62022-08-11 22:27:22 +020035/*
36 * Copy valid attached kernel to absolute address KERNEL_ADDRESS
37 *
38 * Nokia X-Loader is loading secondary image to address 0x80400000.
39 * NOLO is loading boot image to random place, so it doesn't really
Simon Glass98463902022-10-20 18:22:39 -060040 * matter what is set in CONFIG_TEXT_BASE. We have to detect
Pali Rohárc5be5f62022-08-11 22:27:22 +020041 * KERNEL_OFFSET from the current execution address and copy it to
42 * absolute address KERNEL_ADDRESS.
43 *
44 * Note that U-Boot has to be compiled with CONFIG_POSITION_INDEPENDENT
45 * because it is loaded at random address and not to the fixed address
Simon Glass98463902022-10-20 18:22:39 -060046 * (CONFIG_TEXT_BASE).
Pali Rohárc5be5f62022-08-11 22:27:22 +020047 */
Pali Roháred407be2012-10-29 07:54:01 +000048
Pali Roháred407be2012-10-29 07:54:01 +000049 /* r0 - start of kernel before */
Pali Rohár0ba6ce42022-08-11 22:27:24 +020050 adr r0, kernoffs /* r0 - current address of kernoffs section */
51 ldr r1, kernoffs /* r1 - offset of kernel image from kernoffs section */
Pali Rohár892759f2022-10-09 21:37:13 +020052 sub r0, r0, r1
Pali Roháred407be2012-10-29 07:54:01 +000053
54 /* r3 - start of kernel after */
55 ldr r3, kernaddr
56
57 /* r2 - end of kernel after */
58 ldr r1, kernsize
59 add r2, r3, r1
60
61 /* r1 - end of kernel before */
62 add r1, r0, r1
63
64 /* remove header in target kernel */
65 mov r5, #0
Pali Rohár7c4ad982022-08-11 22:27:21 +020066 str r5, [r3] /* remove 4 bytes header of kernel uImage */
67 str r5, [r3, #36] /* remove 4 bytes header of kernel zImage */
Pali Roháred407be2012-10-29 07:54:01 +000068
69 /* check for valid kernel uImage */
70 ldr r4, [r0] /* r4 - 4 bytes header of kernel */
71 ldr r5, ih_magic /* r5 - IH_MAGIC */
72 cmp r4, r5
Pali Rohárcc434fc2021-06-18 15:27:03 +020073 beq copy_kernel_loop
74
75 /* check for valid kernel zImage */
76 ldr r4, [r0, #36] /* r4 - 4 bytes header of kernel at offset 36 */
77 ldr r5, z_magic /* r5 - LINUX_ARM_ZIMAGE_MAGIC */
78 cmp r4, r5
Pali Rohár5c9bf1d2022-09-04 03:28:59 +020079 bne skip_copy /* skip if invalid image */
Pali Roháred407be2012-10-29 07:54:01 +000080
81copy_kernel_loop:
82 ldmdb r1!, {r3 - r10}
83 stmdb r2!, {r3 - r10}
84 cmp r1, r0
85 bhi copy_kernel_loop
86
Pali Rohár7c4ad982022-08-11 22:27:21 +020087 /* remove header in source kernel image */
Pali Roháred407be2012-10-29 07:54:01 +000088 mov r5, #0
Pali Rohárcc434fc2021-06-18 15:27:03 +020089 str r5, [r0] /* remove 4 bytes header of kernel uImage */
90 str r5, [r0, #36] /* remove 4 bytes header of kernel zImage */
Pali Roháred407be2012-10-29 07:54:01 +000091
Pali Rohár5c9bf1d2022-09-04 03:28:59 +020092skip_copy:
93
Pali Rohárc5be5f62022-08-11 22:27:22 +020094 /* Returns */
95 b save_boot_params_ret
Pali Rohár544071a2022-11-24 00:45:00 +010096
97ENDPROC(save_boot_params)