返回 CodeWhale
ohos-sign-binary.sh
根目录 / scripts / ohos / ohos-sign-binary.sh
1 #!/usr/bin/env sh
2 # Cross-build and self-sign the Codewhale Engine for a HarmonyOS / OpenHarmony
3 # device, using the signing tools inside the DevEco Studio SDK.
4 #
5 # A binary only runs on device when it carries a code signature: the loader
6 # refuses an unsigned ELF, and the failure it reports ("Permission denied") is
7 # indistinguishable from a plain missing exec bit. Native HarmonyOS therefore
8 # needs both this step and `strip` before the file is useful.
9 #
10 # The signature is SELF-SIGNED. `binary-sign-tool` writes a descriptor digest
11 # and sets FLAG_SELF_SIGN; it does not embed a certificate and needs no private
12 # key. That is the same shape as the binary already running on the device —
13 # `display-sign` reports "code signature is self-sign" for both. It is not an
14 # AppGallery-signed build and carries no bundle identity.
15 #
16 # Usage:
17 # scripts/ohos/ohos-sign-binary.sh [--strip] [--out PATH]
18 #
19 # Requires:
20 # OHOS_NATIVE_SDK the OpenHarmony native SDK (llvm/, sysroot/, build-tools/)
21 #
22 # Steps this runs:
23 # 1. cargo build --release --target aarch64-unknown-linux-ohos -p codewhale-cli
24 # 2. llvm-strip the copy (optional, ~2.6 MiB smaller)
25 # 3. binary-sign-tool sign -selfSign 1
26 # 4. binary-sign-tool display-sign, so a failure is not silent
27 set -eu
28
29 repo_root=$(CDPATH='' cd -- "$(dirname -- "$0")/../.." && pwd)
30 cd "$repo_root"
31
32 strip_binary=no
33 out=
34 while [ $# -gt 0 ]; do
35 case $1 in
36 --strip) strip_binary=yes; shift ;;
37 --out) out=${2:?--out needs a path}; shift 2 ;;
38 -h|--help) sed -n '2,26p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;;
39 *) echo "unknown argument: $1" >&2; exit 2 ;;
40 esac
41 done
42
43 if [ -z "${OHOS_NATIVE_SDK:-}" ]; then
44 echo "error: set OHOS_NATIVE_SDK to the OpenHarmony native SDK directory." >&2
45 echo " e.g. /Applications/DevEco-Studio.app/Contents/sdk/default/openharmony/native" >&2
46 exit 1
47 fi
48
49 sdk=$(cd "$OHOS_NATIVE_SDK" && pwd)
50 toolchains=$sdk/../toolchains
51 sign_jar=$toolchains/lib/binary-sign-tool.jar
52 # The keystore is only consulted for its alias name in self-sign mode; the
53 # password is the published one that ships with the SDK sample material.
54 keystore=$toolchains/lib/OpenHarmony.p12
55 keystore_pwd=123456
56 key_alias='openharmony application release'
57 strip_tool=$sdk/llvm/bin/llvm-strip
58
59 for file in "$sign_jar" "$keystore" "$strip_tool"; do
60 [ -f "$file" ] || { echo "error: missing SDK file: $file" >&2; exit 1; }
61 done
62 command -v java >/dev/null 2>&1 || { echo "error: java not found; install a JDK or use DevEco's jbr." >&2; exit 1; }
63
64 # 1. Build. The environment is shared with the documented manual flow so there
65 # is one place that decides the target flags.
66 # shellcheck disable=SC1091
67 . ./scripts/ohos/ohos-env.sh >/dev/null
68
69 echo "== building codewhale-cli for aarch64-unknown-linux-ohos"
70 cargo build --locked --release --target aarch64-unknown-linux-ohos -p codewhale-cli
71
72 built=target/aarch64-unknown-linux-ohos/release/codewhale
73 [ -f "$built" ] || { echo "error: build produced no binary at $built" >&2; exit 1; }
74
75 out=${out:-target/aarch64-unknown-linux-ohos/release/codewhale-signed}
76 work=$(mktemp -d)
77 trap 'rm -rf "$work"' EXIT HUP INT TERM
78
79 # 2. Strip a copy. Never strip the build output in place: cargo would consider
80 # the artifact current and leave a stripped binary where it expects an
81 # unstripped one.
82 cp "$built" "$work/unsigned"
83 if [ "$strip_binary" = yes ]; then
84 echo "== stripping"
85 "$strip_tool" --strip-unneeded "$work/unsigned"
86 fi
87
88 # 3. Sign.
89 echo "== signing (self-sign)"
90 java -jar "$sign_jar" sign \
91 -mode localSign \
92 -keyAlias "$key_alias" \
93 -keystoreFile "$keystore" \
94 -keystorePwd "$keystore_pwd" \
95 -inFile "$work/unsigned" \
96 -signAlg SHA256withECDSA \
97 -outFile "$work/signed" \
98 -selfSign 1 2>&1 | sed -n 's/^.*INFO - / /p'
99
100 # 4. Verify, and refuse to hand back a file that did not sign.
101 echo "== verifying"
102 verify=$(java -jar "$sign_jar" display-sign -inFile "$work/signed" 2>&1) || {
103 echo "$verify" >&2
104 echo "error: display-sign failed" >&2
105 exit 1
106 }
107 case $verify in
108 *"code signature is self-sign"*) echo " code signature is self-sign" ;;
109 *) echo "$verify" >&2; echo "error: the output is not self-signed" >&2; exit 1 ;;
110 esac
111
112 cp "$work/signed" "$out"
113 chmod +x "$out"
114 echo
115 echo "signed: $out"
116 echo "size : $(wc -c < "$out") bytes"
117 echo "sha256: $(shasum -a 256 "$out" | cut -d' ' -f1)"
118 cat <<'NOTE'
119
120 To run it on a device, note that two separate gates apply:
121
122 * the file needs its exec bits. HarmonyOS hmdfs (the user directory) refuses
123 `chmod`, and `hdc file send` creates files at 0660, so copy on device from
124 something already executable:
125 cp ~/.cargo/bin/codewhale ~/codewhale-new # keeps 0750
126 # then overwrite its content with the new build
127 * it must be executed from an app process (HiShell, or the CodeArts
128 integrated terminal). The hdc debug shell cannot exec a file on hmdfs at
129 all — it reports "Permission denied" even for a binary that runs fine
130 elsewhere.
131 NOTE
132
132 lines BASH