From e8caec734f92e236b3b1771c4a27d1f41a735d7c Mon Sep 17 00:00:00 2001
From: Jerome Forissier <jerome.forissier@linaro.org>
Date: Thu, 15 Dec 2016 17:07:07 +0100
Subject: [PATCH] Compile head.bin from head.S

Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>
Reviewed-by: Igor Opaniuk <igor.opaniuk@linaro.org>
---
 rpi3.mk                |  14 +++++--
 rpi3/firmware/head.S   |  88 +++++++++++++++++++++++++++++++++++++++++
 rpi3/firmware/head.bin | Bin 32768 -> 0 bytes
 3 files changed, 99 insertions(+), 3 deletions(-)
 create mode 100644 rpi3/firmware/head.S
 delete mode 100644 rpi3/firmware/head.bin

diff --git a/rpi3.mk b/rpi3.mk
index fa9f41f..328878d 100644
--- a/rpi3.mk
+++ b/rpi3.mk
@@ -41,7 +41,7 @@ U-BOOT_BIN		?= $(U-BOOT_PATH)/u-boot.bin
 U-BOOT_JTAG_BIN		?= $(U-BOOT_PATH)/u-boot-jtag.bin
 
 RPI3_FIRMWARE_PATH	?= $(BUILD_PATH)/rpi3/firmware
-RPI3_HEAD_BIN		?= $(RPI3_FIRMWARE_PATH)/head.bin
+RPI3_HEAD_BIN		?= $(ROOT)/out/head.bin
 RPI3_BOOT_CONFIG	?= $(RPI3_FIRMWARE_PATH)/config.txt
 RPI3_UBOOT_ENV		?= $(RPI3_FIRMWARE_PATH)/uboot.env
 RPI3_STOCK_FW_PATH	?= $(ROOT)/rpi3_firmware
@@ -58,7 +58,7 @@ MODULE_OUTPUT		?= $(ROOT)/module_output
 all: rpi3-firmware arm-tf optee-os optee-client xtest u-boot \
 	linux update_rootfs
 all-clean: arm-tf-clean busybox-clean u-boot-clean optee-os-clean \
-	optee-client-clean rpi3-firmware-clean
+	optee-client-clean rpi3-firmware-clean head-bin-clean
 
 -include toolchain.mk
 
@@ -97,7 +97,7 @@ arm-tf-clean:
 U-BOOT_EXPORTS ?= CROSS_COMPILE=$(LEGACY_AARCH64_CROSS_COMPILE) ARCH=arm64
 
 .PHONY: u-boot
-u-boot:
+u-boot: $(RPI3_HEAD_BIN)
 	$(U-BOOT_EXPORTS) $(MAKE) -C $(U-BOOT_PATH) rpi_3_defconfig
 	$(U-BOOT_EXPORTS) $(MAKE) -C $(U-BOOT_PATH) all
 	cd $(U-BOOT_PATH) && cat $(RPI3_HEAD_BIN) $(U-BOOT_BIN) > $(U-BOOT_JTAG_BIN)
@@ -105,6 +105,14 @@ u-boot:
 u-boot-clean:
 	$(U-BOOT_EXPORTS) $(MAKE) -C $(U-BOOT_PATH) clean
 
+$(RPI3_HEAD_BIN): $(RPI3_FIRMWARE_PATH)/head.S
+	mkdir -p $(ROOT)/out/
+	$(AARCH64_CROSS_COMPILE)as $< -o $(ROOT)/out/head.o
+	$(AARCH64_CROSS_COMPILE)objcopy -O binary $(ROOT)/out/head.o $@
+
+head-bin-clean:
+	rm -f $(RPI3_HEAD_BIN) $(ROOT)/out/head.o
+
 ################################################################################
 # Busybox
 ################################################################################
diff --git a/rpi3/firmware/head.S b/rpi3/firmware/head.S
new file mode 100644
index 0000000..ec155f9
--- /dev/null
+++ b/rpi3/firmware/head.S
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2016, Linaro Limited
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+/*
+ * Reverse-engineered from head.bin previously committed in the Git repository.
+ * Build with:
+ *   aarch64-linux-gnu-as head.S -o head.o
+ *   aarch64-linux-gnu-objcopy -O binary head.o head.bin
+ */
+		mov	x0, #0x40000000
+		str	wzr, [x0]
+		mov	w1, #0x80000000
+		str	w1, [x0, #8]
+
+		ldr	x0, _19_2_MHz
+		msr	cntfrq_el0, x0
+
+		msr	cntvoff_el2, xzr
+
+		mov	x0, #0x33ff
+		msr	cptr_el3, x0
+
+		mov	x0, #0x5b0
+		msr	scr_el3, x0
+
+		mov	x0, #0x40
+		msr	s3_1_c15_c2_1, x0
+
+		mrs	x6, mpidr_el1
+		and	x6, x6, #0x3
+		cbz	x6, core0
+
+		adr	x5, spin_table
+spin:
+		wfe
+		ldr	x4, [x5, x6, lsl #3]
+		cbz	x4, spin
+		mov	x0, #0x0
+		b	1f
+core0:
+		ldr	x4, core0_init
+		ldr	x0, core0_init_parm
+1:
+		mov	x1, #0x0
+		mov	x2, #0x0
+		mov	x3, #0x0
+		br	x4
+
+_19_2_MHz:
+		.word	0x0124f800
+		.rept	23
+		.word	0x0
+		.endr
+		.balign	8, 0
+core0_init:
+		.word	0x8000
+		.balign	8, 0
+spin_table:
+		.rept	8
+		.word	0x0
+		.endr
+core0_init_parm:
+		.word	0x100
+		.p2align	15, 0
+
diff --git a/rpi3/firmware/head.bin b/rpi3/firmware/head.bin
deleted file mode 100644
index e8321c79d0f9859de2ef94aa96d636790e4b05d8..0000000000000000000000000000000000000000
GIT binary patch
literal 0
HcmV?d00001

literal 32768
zcmeIuAx=Y45Czct`4yFrU<ia(un1(=fU3i>23((F0jz-#B%&dyFD&3C5B(JjK-HDP
z3Tm1l&X~zeCX<_!ywpju>hgNijk8Lhd4}`07%tkdc&M|J%a*rN^V`RzPQTCEbFSOR
zX!iJ(EZ;{tzMb~%>HcoB-fovkseN*~-u)RynRmPS=x<kQ$>cMet1gGEvgh-|rr0xt
zGXew%5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ
zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U
rAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U_%8xKMFc9)

-- 
GitLab