1/*
2 * Copyright 2018 The Hafnium Authors.
3 *
4 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
7 */
8
9/**
10 * This is a generic entry point for an image. It carries out the operations
11 * required to prepare the loaded image to be run. Specifically, it performs
12 * relocations and zeroing of the bss section using registers x25 and above.
13 */
14.section .init.entry, "ax"
15.global entry
16entry:
17	/* Linux aarch64 image header. */
18	b 0f
19	.word 0
20	.quad 0x1000      /* text_offset */
21	.quad image_size  /* image_size */
22	.quad 0           /* flags */
23	.quad 0           /* res2 */
24	.quad 0           /* res3 */
25	.quad 0           /* res4 */
26	.word 0x644d5241  /* magic */
27	.word 0
28
29	/*
30	 * Calculate the difference between the actual load address and the
31	 * preferred one. We'll use this to relocate.
32	 */
330:	adrp x25, entry
34	add x25, x25, :lo12:entry
35
36	ldr w29, =ORIGIN_ADDRESS
37
38	sub x25, x25, x29
39
40	/* Find where the relocations begin and end. */
41	adrp x29, rela_begin
42	add x29, x29, :lo12:rela_begin
43
44	adrp x30, rela_end
45	add x30, x30, :lo12:rela_end
46
47	/* Iterate over all relocations. */
481:	cmp x29, x30
49	b.eq 2f
50
51	ldp x26, x27, [x29], #16
52	ldr x28, [x29], #8
53
54	cmp w27, #1027 /* R_AARCH64_RELATIVE */
55#	b.ne 1b
56	b.ne .
57
58	add x28, x28, x25
59	str x28, [x26, x25]
60	b 1b
61
62	/* Zero out the bss section. */
632:	adrp x29, bss_begin
64	add x29, x29, :lo12:bss_begin
65
66	adrp x30, bss_end
67	add x30, x30, :lo12:bss_end
68
693:	cmp x29, x30
70	b.hs 4f
71
72	stp xzr, xzr, [x29], #16
73	b 3b
74
75	/* Zero out the stack section. */
764:	adrp x29, stacks_begin
77	add x29, x29, :lo12:stacks_begin
78
79	adrp x30, stacks_end
80	add x30, x30, :lo12:stacks_end
81
825:	cmp x29, x30
83	b.hs 6f
84
85	stp xzr, xzr, [x29], #16
86	b 5b
87
88	/* Branch to the entry point for the specific image. */
896:	b image_entry
90