From afb9817a884e78c0a4120738572404bea05f63c6 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 26 Nov 2022 12:55:17 +0100 Subject: [PATCH 1/9] Add script for semi-automatic msp printing --- runners/lpc55/debug/print_msp.gdb | 20 ++++++++++++++++++++ runners/lpc55/debug/print_msp.py | 24 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 runners/lpc55/debug/print_msp.gdb create mode 100644 runners/lpc55/debug/print_msp.py diff --git a/runners/lpc55/debug/print_msp.gdb b/runners/lpc55/debug/print_msp.gdb new file mode 100644 index 00000000..77d3f4e6 --- /dev/null +++ b/runners/lpc55/debug/print_msp.gdb @@ -0,0 +1,20 @@ +set history save on +set confirm off +set pagination off + +define lmsp + while (1) + # f + i r msp + step 100 + end +end + + +# dir src/ +target extended-remote :2331 +# file runner-lpc55-nk3xn.elf +# load runner-lpc55-nk3xn.elf +monitor reset +# file artifacts/runner-lpc55-nk3xn.elf +source print_msp.py \ No newline at end of file diff --git a/runners/lpc55/debug/print_msp.py b/runners/lpc55/debug/print_msp.py new file mode 100644 index 00000000..423eda9c --- /dev/null +++ b/runners/lpc55/debug/print_msp.py @@ -0,0 +1,24 @@ +import gdb + + +class WalkMSP(gdb.Command): + + def __init__(self): + super(WalkMSP, self).__init__("walkmsp", gdb.COMMAND_USER) + + def invoke(self, args, from_tty): + print(args) + while True: + try: + # gdb.execute("f") + gdb.execute("p/x $msp") + gdb.execute("stepi 100") + except KeyboardInterrupt: + break + except gdb.error: + continue + except Exception: + pass + + +WalkMSP() From 4a43d669d6ac616152018ddf9bfc50919ad070e8 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 26 Nov 2022 14:13:43 +0100 Subject: [PATCH 2/9] Extend build information reports Add cargo-bloat calls to monitor code size per function and crate Sort non-text symbols according to the size --- runners/embedded/Makefile | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/runners/embedded/Makefile b/runners/embedded/Makefile index 31fc93a5..e0aeb249 100644 --- a/runners/embedded/Makefile +++ b/runners/embedded/Makefile @@ -29,6 +29,7 @@ CFG_VAL_SOC = $(shell python3 -c 'import toml; print(toml.load("$(CFG_PATH)")["p ARTIFACTS = artifacts LOG ?= build-$(BUILD_ID).err.log SYMBOLS ?= symbols-$(BUILD_ID).txt +EXTRA ?= extra-$(BUILD_ID).txt # input files and outputs SRCS = $(shell find src -name "*.rs" ) @@ -138,15 +139,24 @@ build: build-banner $(SRCS) check-var-BOARD check-var-BUILD_PROFILE check-var-SO --features $(COMMA_FEATURES) \ --quiet + # cargo install cargo-bloat + cargo bloat --release --target $(TARGET) \ + --features $(COMMA_FEATURES) \ + -n 50 > $(EXTRA)-cargo-bloat + cargo bloat --release --target $(TARGET) \ + --features $(COMMA_FEATURES) \ + --crates -n 50 >> $(EXTRA)-cargo-bloat + cp $(RAW_OUT) ./$(OUT_ELF) - $(GNU_TARGET)-nm ./$(OUT_ELF) | grep ' [ABbDdRrTt] ' | sort | c++filt > $(SYMBOLS) - + $(GNU_TARGET)-nm ./$(OUT_ELF) | grep ' [ABbDdRrTt] ' | sort | c++filt > $(SYMBOLS) + $(GNU_TARGET)-nm -B -l -r --size-sort --print-size -t d ./$(OUT_ELF) | grep ' [ABbDdRrTt] ' | sort -k2rn | c++filt > $(EXTRA)-sizes + arm-none-eabi-objdump -t ./$(OUT_ELF) | grep -Fv ".text" | sort -k5rn | c++filt > $(EXTRA)-objdump-sizes cat $(GNU_TARGET)-arch-symbols.txt >> $(SYMBOLS) $(GNU_TARGET)-objcopy -O binary ./$(OUT_ELF) ./$(OUT_BIN) $(GNU_TARGET)-objcopy -O ihex ./$(OUT_ELF) ./$(OUT_IHEX) - $(GNU_TARGET)-readelf -l ./$(OUT_ELF) | grep LOAD + $(GNU_TARGET)-readelf -l ./$(OUT_ELF) | grep LOAD | tee $(EXTRA)-load clean: clean-banner check-var-BOARD check-var-BUILD_PROFILE rm -f ./$(OUT_BIN) ./$(OUT_ELF) ./$(OUT_IHEX) $(RAW_OUT) $(SYMBOLS) $(LOG) From a81c1ba583384a865e1357a67afa85d9cc44e6d8 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 26 Nov 2022 14:34:27 +0100 Subject: [PATCH 3/9] Add elf-size-analyze tool for size summary --- runners/embedded/Makefile | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/runners/embedded/Makefile b/runners/embedded/Makefile index e0aeb249..909e3280 100644 --- a/runners/embedded/Makefile +++ b/runners/embedded/Makefile @@ -121,11 +121,14 @@ clean-all: rm -f firmware.hex mbr.hex bootloader.hex rm -rf artifacts cfg.toml +elf-size-analyze.py: + wget https://raw.githubusercontent.com/jedrzejboczar/elf-size-analyze/master/elf-size-analyze.py + ############################################################################### #### actual build, clean, reset, program targets ############################################################################### -build: build-banner $(SRCS) check-var-BOARD check-var-BUILD_PROFILE check-var-SOC +build: build-banner $(SRCS) check-var-BOARD check-var-BUILD_PROFILE check-var-SOC elf-size-analyze.py cargo --version @@ -149,6 +152,8 @@ build: build-banner $(SRCS) check-var-BOARD check-var-BUILD_PROFILE check-var-SO cp $(RAW_OUT) ./$(OUT_ELF) + python3 ./elf-size-analyze.py -t arm-none-eabi- ./$(OUT_ELF) -R -H -m 50 -w 0 > $(EXTRA)-sizes-detailed + python3 ./elf-size-analyze.py -t arm-none-eabi- ./$(OUT_ELF) -F -H -m 100 -w 0 >> $(EXTRA)-sizes-detailed $(GNU_TARGET)-nm ./$(OUT_ELF) | grep ' [ABbDdRrTt] ' | sort | c++filt > $(SYMBOLS) $(GNU_TARGET)-nm -B -l -r --size-sort --print-size -t d ./$(OUT_ELF) | grep ' [ABbDdRrTt] ' | sort -k2rn | c++filt > $(EXTRA)-sizes arm-none-eabi-objdump -t ./$(OUT_ELF) | grep -Fv ".text" | sort -k5rn | c++filt > $(EXTRA)-objdump-sizes From 671bcb829594855a9b4d4b772b6003e214cde6a2 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 26 Nov 2022 14:41:19 +0100 Subject: [PATCH 4/9] fixup cleanup --- runners/embedded/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runners/embedded/Makefile b/runners/embedded/Makefile index 909e3280..7a1f6210 100644 --- a/runners/embedded/Makefile +++ b/runners/embedded/Makefile @@ -164,5 +164,5 @@ build: build-banner $(SRCS) check-var-BOARD check-var-BUILD_PROFILE check-var-SO $(GNU_TARGET)-readelf -l ./$(OUT_ELF) | grep LOAD | tee $(EXTRA)-load clean: clean-banner check-var-BOARD check-var-BUILD_PROFILE - rm -f ./$(OUT_BIN) ./$(OUT_ELF) ./$(OUT_IHEX) $(RAW_OUT) $(SYMBOLS) $(LOG) + rm -f ./$(OUT_BIN) ./$(OUT_ELF) ./$(OUT_IHEX) $(RAW_OUT) $(SYMBOLS) $(LOG) $(EXTRA)* From 39535097cace46f88d1f622112d81c534f79e9aa Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 26 Nov 2022 14:47:07 +0100 Subject: [PATCH 5/9] REMOVE Install cargo bloat for now --- runners/embedded/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runners/embedded/Makefile b/runners/embedded/Makefile index 7a1f6210..ebd880f0 100644 --- a/runners/embedded/Makefile +++ b/runners/embedded/Makefile @@ -142,7 +142,7 @@ build: build-banner $(SRCS) check-var-BOARD check-var-BUILD_PROFILE check-var-SO --features $(COMMA_FEATURES) \ --quiet - # cargo install cargo-bloat + cargo install cargo-bloat cargo bloat --release --target $(TARGET) \ --features $(COMMA_FEATURES) \ -n 50 > $(EXTRA)-cargo-bloat From 9c1635b26d066dc41cf3a8c4c94d8aa4f6083e89 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 26 Nov 2022 15:09:56 +0100 Subject: [PATCH 6/9] Add debug scripts --- .../lpc55-builder/scripts/usbwait.sh | 20 +++++++++ update/Robin-scripts/lpc55-builder/test.mk | 43 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100755 update/Robin-scripts/lpc55-builder/scripts/usbwait.sh create mode 100755 update/Robin-scripts/lpc55-builder/test.mk diff --git a/update/Robin-scripts/lpc55-builder/scripts/usbwait.sh b/update/Robin-scripts/lpc55-builder/scripts/usbwait.sh new file mode 100755 index 00000000..975d9949 --- /dev/null +++ b/update/Robin-scripts/lpc55-builder/scripts/usbwait.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +echo -n "Waiting for USB device: $@ " +sleep 1 +for i in `seq 1 10` +do + for id in $@ + do + if lsusb -d $id + then + exit 0 + fi + echo -n "." + sleep 1 + done +done + +echo +echo "Error: Device not found" >&2 +exit 1 diff --git a/update/Robin-scripts/lpc55-builder/test.mk b/update/Robin-scripts/lpc55-builder/test.mk new file mode 100755 index 00000000..5cb16ace --- /dev/null +++ b/update/Robin-scripts/lpc55-builder/test.mk @@ -0,0 +1,43 @@ +# test.sh is the relevant script +# you’ll have to update the variables +# and you have to update the jlink-erase-reset.txt to point to the provisioner + + +# fully erase via debugger +# flash old v1.2.2 provisioner via debugger +# reset via debugger and boot into firmware +# provisioner firmware sets up file system +# reboot to bootloader +# flash test firmware via bootloader +# reboot into firmware mode + +RUNNER=/home/sz/work/nitrokey-3-firmware/runners/embedded +BINARY=$(RUNNER)/artifacts/runner-lpc55-nk3xn.bin +# LPC55_BUILDER=/tmp/lpc55-builder +LPC55_BUILDER=$(PWD) + +all: prov + -rm "$(BINARY)" + cd $(RUNNER) && git checkout -- Cargo.lock + # cd $(RUNNER) && cargo update -p trussed +# cd $(RUNNER) && cargo update + make -C "$(RUNNER)" build-nk3xn FEATURES=alpha + # make -C "$(RUNNER)" build-nk3xn FEATURES=opcard,trussed/clients-3 + + ls -lh /tmp/provisioner-nk3xn-lpc55-v1.2.2.bin + JLinkExe -device LPC55S69_M33_0 -if SWD -autoconnect 1 -speed 4000 -CommandFile "$(LPC55_BUILDER)"/jlink-erase-reset.txt + ./scripts/usbwait.sh 20a0:42b2 + + nitropy nk3 reboot --bootloader + ./scripts/usbwait.sh 20a0:42dd 1fc9:0021 + + lpc55 write-flash "$(BINARY)" + sha256sum "$(BINARY)" + lpc55 reboot + ./scripts/usbwait.sh 20a0:42b2 + +# alias for the full path +prov: /tmp/provisioner-nk3xn-lpc55-v1.2.2.bin + +/tmp/provisioner-nk3xn-lpc55-v1.2.2.bin: + cp ../provisioner-nk3xn-lpc55-v1.2.2.bin /tmp/ From 2b15809756943f3b093f044271aa6742025ce8e4 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 28 Nov 2022 14:26:52 +0100 Subject: [PATCH 7/9] CI: archive generated static analysis reports --- .gitlab-ci.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 6576056f..e3f634af 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -46,6 +46,7 @@ build-lpc55-nk3xn: - docker stage: build script: + - echo "This script should not be that long. These commands should be kept in Makefile or other runner" - rustup target add thumbv8m.main-none-eabi - rustup +nightly-2022-11-13 target add thumbv8m.main-none-eabi - mkdir -p artifacts nk3xn @@ -60,6 +61,7 @@ build-lpc55-nk3xn: - make -C runners/embedded build-nk3xn - cp ./runners/embedded/artifacts/runner-lpc55-nk3xn.bin nk3xn/firmware-nk3xn-lpc55-$VERSION.bin - zip nk3xn-raw.zip nk3xn/* + - zip nk3xn-report.zip ./runners/embedded/artifacts/extra* ./runners/embedded/artifacts/symbols* - cp nk3xn-raw.zip artifacts after_script: - git archive --format zip --output artifacts/nitrokey-3-firmware.zip --prefix nitrokey-3-firmware/ HEAD @@ -78,6 +80,7 @@ build-nrf52-nk3mini: - docker stage: build script: + - echo "This script should not be that long. These commands should be kept in Makefile or other runner" - apt-get install -y python3 python3-toml zip - rustup target add thumbv7em-none-eabihf - rustup +nightly-2022-11-13 target add thumbv7em-none-eabihf From 92aea175342c59d58d1686c644d2974b34c18ac5 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 28 Nov 2022 14:27:14 +0100 Subject: [PATCH 8/9] CI: archive generated static analysis reports (2) --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index e3f634af..26ce9634 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -61,7 +61,7 @@ build-lpc55-nk3xn: - make -C runners/embedded build-nk3xn - cp ./runners/embedded/artifacts/runner-lpc55-nk3xn.bin nk3xn/firmware-nk3xn-lpc55-$VERSION.bin - zip nk3xn-raw.zip nk3xn/* - - zip nk3xn-report.zip ./runners/embedded/artifacts/extra* ./runners/embedded/artifacts/symbols* + - zip artifacts/nk3xn-report.zip ./runners/embedded/artifacts/extra* ./runners/embedded/artifacts/symbols* - cp nk3xn-raw.zip artifacts after_script: - git archive --format zip --output artifacts/nitrokey-3-firmware.zip --prefix nitrokey-3-firmware/ HEAD From 96f4720bac6b66d294b5a07bc452cd97e614f066 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 28 Nov 2022 15:27:54 +0100 Subject: [PATCH 9/9] CI: archive generated static analysis reports (3) --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 26ce9634..71b90676 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -61,7 +61,7 @@ build-lpc55-nk3xn: - make -C runners/embedded build-nk3xn - cp ./runners/embedded/artifacts/runner-lpc55-nk3xn.bin nk3xn/firmware-nk3xn-lpc55-$VERSION.bin - zip nk3xn-raw.zip nk3xn/* - - zip artifacts/nk3xn-report.zip ./runners/embedded/artifacts/extra* ./runners/embedded/artifacts/symbols* + - zip artifacts/nk3xn-report.zip ./runners/embedded/artifacts/*extra* ./runners/embedded/artifacts/*symbols* - cp nk3xn-raw.zip artifacts after_script: - git archive --format zip --output artifacts/nitrokey-3-firmware.zip --prefix nitrokey-3-firmware/ HEAD