Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Pali Rohár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2011-2012 |
Pali Rohár | 10a953d | 2020-04-01 00:35:08 +0200 | [diff] [blame] | 4 | * Pali Rohár <pali@kernel.org> |
Pali Rohár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <config.h> |
| 8 | |
Pali Rohár | b043e87 | 2022-08-11 22:27:24 +0200 | [diff] [blame] | 9 | kernoffs: /* offset of kernel image from this address */ |
| 10 | .word KERNEL_OFFSET - (. - CONFIG_SYS_TEXT_BASE) |
Pali Rohár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 11 | |
| 12 | kernaddr: /* address of kernel after copying */ |
| 13 | .word KERNEL_ADDRESS |
| 14 | |
| 15 | kernsize: /* maximal size of kernel image */ |
| 16 | .word KERNEL_MAXSIZE |
| 17 | |
Pali Rohár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 18 | imagesize: /* maximal size of image */ |
| 19 | .word IMAGE_MAXSIZE |
| 20 | |
| 21 | ih_magic: /* IH_MAGIC in big endian from include/image.h */ |
| 22 | .word 0x56190527 |
| 23 | |
Pali Rohár | 0a8825c | 2021-06-18 15:27:03 +0200 | [diff] [blame] | 24 | z_magic: /* LINUX_ARM_ZIMAGE_MAGIC */ |
| 25 | .word 0x016f2818 |
| 26 | |
Pali Rohár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 27 | /* |
| 28 | * Routine: save_boot_params (called after reset from start.S) |
| 29 | * Description: Copy attached kernel to address KERNEL_ADDRESS |
Pali Rohár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
| 32 | .global save_boot_params |
| 33 | save_boot_params: |
Pali Rohár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 34 | |
Pali Rohár | c1aec22 | 2022-08-11 22:27:22 +0200 | [diff] [blame] | 35 | /* |
| 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 |
| 40 | * matter what is set in CONFIG_SYS_TEXT_BASE. We have to detect |
| 41 | * 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 |
| 46 | * (CONFIG_SYS_TEXT_BASE). |
| 47 | */ |
Pali Rohár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 48 | |
| 49 | copy_kernel_start: |
Pali Rohár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 50 | /* r0 - start of kernel before */ |
Pali Rohár | b043e87 | 2022-08-11 22:27:24 +0200 | [diff] [blame] | 51 | adr r0, kernoffs /* r0 - current address of kernoffs section */ |
| 52 | ldr r1, kernoffs /* r1 - offset of kernel image from kernoffs section */ |
Pali Rohár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 53 | add r0, r0, r1 |
| 54 | |
| 55 | /* r3 - start of kernel after */ |
| 56 | ldr r3, kernaddr |
| 57 | |
| 58 | /* r2 - end of kernel after */ |
| 59 | ldr r1, kernsize |
| 60 | add r2, r3, r1 |
| 61 | |
| 62 | /* r1 - end of kernel before */ |
| 63 | add r1, r0, r1 |
| 64 | |
| 65 | /* remove header in target kernel */ |
| 66 | mov r5, #0 |
Pali Rohár | f8a6c90 | 2022-08-11 22:27:21 +0200 | [diff] [blame] | 67 | str r5, [r3] /* remove 4 bytes header of kernel uImage */ |
| 68 | str r5, [r3, #36] /* remove 4 bytes header of kernel zImage */ |
Pali Rohár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 69 | |
| 70 | /* check for valid kernel uImage */ |
| 71 | ldr r4, [r0] /* r4 - 4 bytes header of kernel */ |
| 72 | ldr r5, ih_magic /* r5 - IH_MAGIC */ |
| 73 | cmp r4, r5 |
Pali Rohár | 0a8825c | 2021-06-18 15:27:03 +0200 | [diff] [blame] | 74 | beq copy_kernel_loop |
| 75 | |
| 76 | /* check for valid kernel zImage */ |
| 77 | ldr r4, [r0, #36] /* r4 - 4 bytes header of kernel at offset 36 */ |
| 78 | ldr r5, z_magic /* r5 - LINUX_ARM_ZIMAGE_MAGIC */ |
| 79 | cmp r4, r5 |
Pali Rohár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 80 | bne copy_kernel_end /* skip if invalid image */ |
| 81 | |
| 82 | copy_kernel_loop: |
| 83 | ldmdb r1!, {r3 - r10} |
| 84 | stmdb r2!, {r3 - r10} |
| 85 | cmp r1, r0 |
| 86 | bhi copy_kernel_loop |
| 87 | |
| 88 | copy_kernel_end: |
Pali Rohár | f8a6c90 | 2022-08-11 22:27:21 +0200 | [diff] [blame] | 89 | |
| 90 | /* remove header in source kernel image */ |
Pali Rohár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 91 | mov r5, #0 |
Pali Rohár | 0a8825c | 2021-06-18 15:27:03 +0200 | [diff] [blame] | 92 | str r5, [r0] /* remove 4 bytes header of kernel uImage */ |
| 93 | str r5, [r0, #36] /* remove 4 bytes header of kernel zImage */ |
Pali Rohár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 94 | |
Pali Rohár | c1aec22 | 2022-08-11 22:27:22 +0200 | [diff] [blame] | 95 | /* Returns */ |
| 96 | b save_boot_params_ret |