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 | |
| 9 | relocaddr: /* address of this relocaddr section after coping */ |
| 10 | .word . /* address of section (calculated at compile time) */ |
| 11 | |
| 12 | startaddr: /* address of u-boot after copying */ |
| 13 | .word CONFIG_SYS_TEXT_BASE |
| 14 | |
| 15 | kernaddr: /* address of kernel after copying */ |
| 16 | .word KERNEL_ADDRESS |
| 17 | |
| 18 | kernsize: /* maximal size of kernel image */ |
| 19 | .word KERNEL_MAXSIZE |
| 20 | |
| 21 | kernoffs: /* offset of kernel image in loaded u-boot */ |
| 22 | .word KERNEL_OFFSET |
| 23 | |
| 24 | imagesize: /* maximal size of image */ |
| 25 | .word IMAGE_MAXSIZE |
| 26 | |
| 27 | ih_magic: /* IH_MAGIC in big endian from include/image.h */ |
| 28 | .word 0x56190527 |
| 29 | |
Pali Rohár | 0a8825c | 2021-06-18 15:27:03 +0200 | [diff] [blame] | 30 | z_magic: /* LINUX_ARM_ZIMAGE_MAGIC */ |
| 31 | .word 0x016f2818 |
| 32 | |
Pali Rohár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 33 | /* |
| 34 | * Routine: save_boot_params (called after reset from start.S) |
| 35 | * Description: Copy attached kernel to address KERNEL_ADDRESS |
Pali Rohár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 36 | */ |
| 37 | |
| 38 | .global save_boot_params |
| 39 | save_boot_params: |
Pali Rohár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 40 | |
Pali Rohár | c1aec22 | 2022-08-11 22:27:22 +0200 | [diff] [blame] | 41 | /* |
| 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ár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 54 | |
| 55 | copy_kernel_start: |
| 56 | adr r0, relocaddr /* r0 - address of section relocaddr */ |
| 57 | ldr r1, relocaddr /* r1 - address of relocaddr after relocation */ |
Pali Rohár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 58 | |
| 59 | /* r4 - calculated offset */ |
Pali Rohár | 39e486e | 2022-08-11 22:27:23 +0200 | [diff] [blame^] | 60 | sub r4, r0, r1 |
Pali Rohár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 61 | |
| 62 | /* r0 - start of kernel before */ |
| 63 | ldr r0, startaddr |
Pali Rohár | 39e486e | 2022-08-11 22:27:23 +0200 | [diff] [blame^] | 64 | add r0, r0, r4 |
Pali Rohár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 65 | 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ár | f8a6c90 | 2022-08-11 22:27:21 +0200 | [diff] [blame] | 80 | str r5, [r3] /* remove 4 bytes header of kernel uImage */ |
| 81 | str r5, [r3, #36] /* remove 4 bytes header of kernel zImage */ |
Pali Rohár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 82 | |
| 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ár | 0a8825c | 2021-06-18 15:27:03 +0200 | [diff] [blame] | 87 | 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ár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 93 | bne copy_kernel_end /* skip if invalid image */ |
| 94 | |
| 95 | copy_kernel_loop: |
| 96 | ldmdb r1!, {r3 - r10} |
| 97 | stmdb r2!, {r3 - r10} |
| 98 | cmp r1, r0 |
| 99 | bhi copy_kernel_loop |
| 100 | |
| 101 | copy_kernel_end: |
Pali Rohár | f8a6c90 | 2022-08-11 22:27:21 +0200 | [diff] [blame] | 102 | |
| 103 | /* remove header in source kernel image */ |
Pali Rohár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 104 | mov r5, #0 |
Pali Rohár | 0a8825c | 2021-06-18 15:27:03 +0200 | [diff] [blame] | 105 | str r5, [r0] /* remove 4 bytes header of kernel uImage */ |
| 106 | str r5, [r0, #36] /* remove 4 bytes header of kernel zImage */ |
Pali Rohár | 248ef0a | 2012-10-29 07:54:01 +0000 | [diff] [blame] | 107 | |
Pali Rohár | c1aec22 | 2022-08-11 22:27:22 +0200 | [diff] [blame] | 108 | /* Returns */ |
| 109 | b save_boot_params_ret |