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 | |
Pali Rohár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 49 | /* r0 - start of kernel before */ |
Pali Rohár | b043e87 | 2022-08-11 22:27:24 +0200 | [diff] [blame] | 50 | adr r0, kernoffs /* r0 - current address of kernoffs section */ |
| 51 | ldr r1, kernoffs /* r1 - offset of kernel image from kernoffs section */ |
Pali Rohár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 52 | add r0, r0, r1 |
| 53 | |
| 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ár | f8a6c90 | 2022-08-11 22:27:21 +0200 | [diff] [blame] | 66 | str r5, [r3] /* remove 4 bytes header of kernel uImage */ |
| 67 | str r5, [r3, #36] /* remove 4 bytes header of kernel zImage */ |
Pali Rohár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 68 | |
| 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ár | 0a8825c | 2021-06-18 15:27:03 +0200 | [diff] [blame] | 73 | 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ár | a4001fd | 2022-09-04 03:28:59 +0200 | [diff] [blame] | 79 | bne skip_copy /* skip if invalid image */ |
Pali Rohár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 80 | |
| 81 | copy_kernel_loop: |
| 82 | ldmdb r1!, {r3 - r10} |
| 83 | stmdb r2!, {r3 - r10} |
| 84 | cmp r1, r0 |
| 85 | bhi copy_kernel_loop |
| 86 | |
Pali Rohár | f8a6c90 | 2022-08-11 22:27:21 +0200 | [diff] [blame] | 87 | /* remove header in source kernel image */ |
Pali Rohár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 88 | mov r5, #0 |
Pali Rohár | 0a8825c | 2021-06-18 15:27:03 +0200 | [diff] [blame] | 89 | str r5, [r0] /* remove 4 bytes header of kernel uImage */ |
| 90 | str r5, [r0, #36] /* remove 4 bytes header of kernel zImage */ |
Pali Rohár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 91 | |
Pali Rohár | a4001fd | 2022-09-04 03:28:59 +0200 | [diff] [blame] | 92 | skip_copy: |
| 93 | |
Pali Rohár | c1aec22 | 2022-08-11 22:27:22 +0200 | [diff] [blame] | 94 | /* Returns */ |
| 95 | b save_boot_params_ret |