-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
169 lines (133 loc) · 5.27 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# http://github.com/usbarmory/tamago-example
#
# Copyright (c) WithSecure Corporation
# https://foundry.withsecure.com
#
# Use of this source code is governed by the license
# that can be found in the LICENSE file.
BUILD_USER ?= $(shell whoami)
BUILD_HOST ?= $(shell hostname)
BUILD_DATE ?= $(shell /bin/date -u "+%Y-%m-%d %H:%M:%S")
BUILD = ${BUILD_USER}@${BUILD_HOST} on ${BUILD_DATE}
REV = $(shell git rev-parse --short HEAD 2> /dev/null)
SHELL = /bin/bash
APP := example
TARGET ?= "usbarmory"
TEXT_START := 0x80010000 # ramStart (defined in mem.go under relevant tamago/soc package) + 0x10000
ifeq ($(TARGET),sifive_u)
GOENV := GO_EXTLINK_ENABLED=0 CGO_ENABLED=0 GOOS=tamago GOARCH=riscv64
ENTRY_POINT := _rt0_riscv64_tamago
QEMU ?= qemu-system-riscv64 -machine sifive_u -m 512M \
-nographic -monitor none -serial stdio -net none \
-semihosting \
-dtb $(CURDIR)/qemu.dtb \
-bios $(CURDIR)/bios/bios.bin
else
ifeq ($(TARGET),mx6ullevk)
UART1 := stdio
UART2 := null
NET := nic,model=imx.enet,netdev=net0 -netdev tap,id=net0,ifname=tap0,script=no,downscript=no
else
UART1 := null
UART2 := stdio
NET := none
endif
GOENV := GO_EXTLINK_ENABLED=0 CGO_ENABLED=0 GOOS=tamago GOARM=7 GOARCH=arm
ENTRY_POINT := _rt0_arm_tamago
QEMU ?= qemu-system-arm -machine mcimx6ul-evk -cpu cortex-a7 -m 512M \
-nographic -monitor none -serial $(UART1) -serial $(UART2) -net $(NET) \
-semihosting
endif
GOFLAGS := -tags ${TARGET},linkramsize,native -trimpath -ldflags "-s -w -T $(TEXT_START) -E $(ENTRY_POINT) -R 0x1000 -X 'main.Build=${BUILD}' -X 'main.Revision=${REV}'"
.PHONY: clean qemu qemu-gdb
check_tamago:
@if [ "${TAMAGO}" == "" ] || [ ! -f "${TAMAGO}" ]; then \
echo 'You need to set the TAMAGO variable to a compiled version of https://github.com/usbarmory/tamago-go'; \
exit 1; \
fi
clean:
@rm -fr $(APP) $(APP).bin $(APP).imx $(APP)-signed.imx $(APP).csf $(APP).dcd cmd/IMX6ULL.yaml qemu.dtb bios/bios.bin
#### generic targets ####
all: $(APP)
elf: $(APP)
qemu: GOFLAGS := $(GOFLAGS:native=semihosting)
qemu: $(APP)
$(QEMU) -kernel $(APP)
qemu-gdb: GOFLAGS := $(GOFLAGS:native=semihosting)
qemu-gdb: GOFLAGS := $(GOFLAGS:-w=)
qemu-gdb: GOFLAGS := $(GOFLAGS:-s=)
qemu-gdb: $(APP)
$(QEMU) -kernel $(APP) -S -s
#### ARM targets ####
imx: $(APP).imx
imx_signed: $(APP)-signed.imx
check_hab_keys:
@if [ "${HAB_KEYS}" == "" ]; then \
echo 'You need to set the HAB_KEYS variable to the path of secure boot keys'; \
echo 'See https://github.com/usbarmory/usbarmory/wiki/Secure-boot-(Mk-II)'; \
exit 1; \
fi
$(APP).bin: CROSS_COMPILE=arm-none-eabi-
$(APP).bin: $(APP)
$(CROSS_COMPILE)objcopy -j .text -j .rodata -j .shstrtab -j .typelink \
-j .itablink -j .gopclntab -j .go.buildinfo -j .noptrdata -j .data \
-j .bss --set-section-flags .bss=alloc,load,contents \
-j .noptrbss --set-section-flags .noptrbss=alloc,load,contents \
$(APP) -O binary $(APP).bin
$(APP).imx: $(APP).bin $(APP).dcd
mkimage -n $(APP).dcd -T imximage -e $(TEXT_START) -d $(APP).bin $(APP).imx
# Copy entry point from ELF file
dd if=$(APP) of=$(APP).imx bs=1 count=4 skip=24 seek=4 conv=notrunc
IMX6ULL.yaml: check_tamago
IMX6ULL.yaml: GOMODCACHE=$(shell ${TAMAGO} env GOMODCACHE)
IMX6ULL.yaml: CRUCIBLE_PKG=$(shell grep "github.com/usbarmory/crucible v" go.mod | awk '{print $$1"@"$$2}')
IMX6ULL.yaml:
${TAMAGO} install github.com/usbarmory/crucible/cmd/habtool
cp -f $(GOMODCACHE)/$(CRUCIBLE_PKG)/cmd/crucible/fusemaps/IMX6ULL.yaml cmd/IMX6ULL.yaml
$(APP).dcd: check_tamago
$(APP).dcd: GOMODCACHE=$(shell ${TAMAGO} env GOMODCACHE)
$(APP).dcd: TAMAGO_PKG=$(shell grep "github.com/usbarmory/tamago v" go.mod | awk '{print $$1"@"$$2}')
$(APP).dcd:
@if test "${TARGET}" = "usbarmory"; then \
cp -f $(GOMODCACHE)/$(TAMAGO_PKG)/board/usbarmory/mk2/imximage.cfg $(APP).dcd; \
elif test "${TARGET}" = "mx6ullevk"; then \
cp -f $(GOMODCACHE)/$(TAMAGO_PKG)/board/nxp/mx6ullevk/imximage.cfg $(APP).dcd; \
else \
echo "invalid target - options are: usbarmory, mx6ullevk"; \
exit 1; \
fi
#### RISC-V targets ####
qemu.dtb: GOMODCACHE=$(shell ${TAMAGO} env GOMODCACHE)
qemu.dtb: TAMAGO_PKG=$(shell grep "github.com/usbarmory/tamago v" go.mod | awk '{print $$1"@"$$2}')
qemu.dtb:
echo $(GOMODCACHE)
echo $(TAMAGO_PKG)
dtc -I dts -O dtb $(GOMODCACHE)/$(TAMAGO_PKG)/board/qemu/sifive_u/qemu-riscv64-sifive_u.dts -o $(CURDIR)/qemu.dtb 2> /dev/null
#### application target ####
ifeq ($(TARGET),sifive_u)
$(APP): check_tamago qemu.dtb
$(GOENV) $(TAMAGO) build $(GOFLAGS) -o ${APP} && \
RT0=$$(riscv64-linux-gnu-readelf -a $(APP)|grep -i 'Entry point' | cut -dx -f2) && \
echo ".equ RT0_RISCV64_TAMAGO, 0x$$RT0" > $(CURDIR)/bios/cfg.inc && \
cd $(CURDIR)/bios && ./build.sh
else
$(APP): check_tamago IMX6ULL.yaml
$(GOENV) $(TAMAGO) build $(GOFLAGS) -o ${APP}
endif
#### HAB secure boot ####
$(APP)-signed.imx: check_tamago check_hab_keys $(APP).imx
${TAMAGO} install github.com/usbarmory/crucible/cmd/habtool
$(shell ${TAMAGO} env GOPATH)/bin/habtool \
-A ${HAB_KEYS}/CSF_1_key.pem \
-a ${HAB_KEYS}/CSF_1_crt.pem \
-B ${HAB_KEYS}/IMG_1_key.pem \
-b ${HAB_KEYS}/IMG_1_crt.pem \
-t ${HAB_KEYS}/SRK_1_2_3_4_table.bin \
-x 1 \
-s \
-i $(APP).imx \
-o $(APP).csf && \
cat $(APP).imx $(APP).csf > $(APP)-signed.imx
# t9 stanzas
boot: example.imx
sudo ~/bin/armory-boot-usb -i example.imx -t 30