2023-06-02 11:31:54 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-07-17 14:07:31 +00:00
|
|
|
import subprocess
|
2023-09-26 14:28:38 +00:00
|
|
|
import zlib
|
2023-04-05 11:03:47 +00:00
|
|
|
from pathlib import Path
|
2023-01-10 21:28:57 +00:00
|
|
|
|
2023-07-17 14:07:31 +00:00
|
|
|
from boards import (
|
|
|
|
discovery,
|
2023-06-28 08:51:37 +00:00
|
|
|
discovery2,
|
2023-07-17 14:07:31 +00:00
|
|
|
trezor_1,
|
|
|
|
trezor_r_v3,
|
|
|
|
trezor_r_v4,
|
|
|
|
trezor_r_v6,
|
|
|
|
trezor_r_v10,
|
|
|
|
trezor_t,
|
2024-02-20 13:54:37 +00:00
|
|
|
trezor_t3t1_revE,
|
2023-08-24 09:33:50 +00:00
|
|
|
trezor_t3t1_v4,
|
2023-07-17 14:07:31 +00:00
|
|
|
)
|
2023-02-15 12:57:54 +00:00
|
|
|
|
2023-04-05 11:03:47 +00:00
|
|
|
HERE = Path(__file__).parent.resolve()
|
|
|
|
|
|
|
|
# go up from site_scons to core/
|
|
|
|
PROJECT_ROOT = HERE.parent.resolve()
|
2023-01-10 21:28:57 +00:00
|
|
|
|
|
|
|
|
2023-06-02 11:31:54 +00:00
|
|
|
def add_font(
|
|
|
|
font_name: str, font: str | None, defines: list[str], sources: list[str]
|
|
|
|
) -> None:
|
2022-08-18 13:12:02 +00:00
|
|
|
if font is not None:
|
2024-04-09 11:17:05 +00:00
|
|
|
font_filename = font.replace("_upper", "").lower()
|
2022-08-18 13:12:02 +00:00
|
|
|
defines += [
|
2023-06-02 11:31:54 +00:00
|
|
|
"TREZOR_FONT_" + font_name + "_ENABLE=" + font,
|
2024-04-09 11:17:05 +00:00
|
|
|
"TREZOR_FONT_" + font_name + '_INCLUDE=\\"' + font_filename + '.h\\"',
|
2022-10-14 10:43:37 +00:00
|
|
|
]
|
2024-04-09 11:17:05 +00:00
|
|
|
sourcefile = "embed/lib/fonts/" + font_filename + ".c"
|
2022-08-18 13:12:02 +00:00
|
|
|
if sourcefile not in sources:
|
|
|
|
sources.append(sourcefile)
|
|
|
|
|
2022-10-14 10:43:37 +00:00
|
|
|
|
2023-06-02 11:31:54 +00:00
|
|
|
def configure_board(
|
|
|
|
model: str,
|
|
|
|
features_wanted: list[str],
|
|
|
|
env: dict, # type: ignore
|
|
|
|
defines: list[str | tuple[str, str]],
|
|
|
|
sources: list[str],
|
2023-06-23 14:50:13 +00:00
|
|
|
paths: list[str],
|
2023-07-17 14:15:20 +00:00
|
|
|
) -> list[str]:
|
2023-06-22 19:56:48 +00:00
|
|
|
model_r_version = 10
|
2024-02-20 13:54:37 +00:00
|
|
|
model_t3t1_version = "E"
|
2022-10-14 10:43:37 +00:00
|
|
|
|
2023-06-02 11:31:54 +00:00
|
|
|
if model in ("1",):
|
2023-06-23 14:50:13 +00:00
|
|
|
return trezor_1.configure(env, features_wanted, defines, sources, paths)
|
2023-06-02 11:31:54 +00:00
|
|
|
elif model in ("T",):
|
2023-06-23 14:50:13 +00:00
|
|
|
return trezor_t.configure(env, features_wanted, defines, sources, paths)
|
2023-06-02 11:31:54 +00:00
|
|
|
elif model in ("R",):
|
2022-10-14 10:43:37 +00:00
|
|
|
if model_r_version == 3:
|
2023-06-23 14:50:13 +00:00
|
|
|
return trezor_r_v3.configure(env, features_wanted, defines, sources, paths)
|
2023-04-17 07:51:57 +00:00
|
|
|
elif model_r_version == 4:
|
2023-06-23 14:50:13 +00:00
|
|
|
return trezor_r_v4.configure(env, features_wanted, defines, sources, paths)
|
2023-07-17 14:07:31 +00:00
|
|
|
elif model_r_version == 6:
|
2023-06-23 14:50:13 +00:00
|
|
|
return trezor_r_v6.configure(env, features_wanted, defines, sources, paths)
|
2023-06-22 19:56:48 +00:00
|
|
|
elif model_r_version == 10:
|
2023-06-23 14:50:13 +00:00
|
|
|
return trezor_r_v10.configure(env, features_wanted, defines, sources, paths)
|
2023-07-17 14:15:20 +00:00
|
|
|
raise Exception("Unknown model_r_version")
|
2023-08-24 09:33:50 +00:00
|
|
|
elif model in ("T3T1",):
|
2024-02-20 13:54:37 +00:00
|
|
|
if model_t3t1_version == 4:
|
|
|
|
return trezor_t3t1_v4.configure(
|
|
|
|
env, features_wanted, defines, sources, paths
|
|
|
|
)
|
|
|
|
elif model_t3t1_version == "E":
|
|
|
|
return trezor_t3t1_revE.configure(
|
|
|
|
env, features_wanted, defines, sources, paths
|
|
|
|
)
|
|
|
|
raise Exception("Unknown model_t3t1_version")
|
2023-07-17 14:07:31 +00:00
|
|
|
elif model in ("DISC1",):
|
2023-06-23 14:50:13 +00:00
|
|
|
return discovery.configure(env, features_wanted, defines, sources, paths)
|
2023-06-28 08:51:37 +00:00
|
|
|
elif model in ("DISC2",):
|
|
|
|
return discovery2.configure(env, features_wanted, defines, sources, paths)
|
2023-07-17 14:15:20 +00:00
|
|
|
raise Exception("Unknown model")
|
2022-10-14 10:43:37 +00:00
|
|
|
|
2023-01-10 15:47:23 +00:00
|
|
|
|
2023-06-02 11:31:54 +00:00
|
|
|
def get_model_identifier(model: str) -> str:
|
|
|
|
if model == "1":
|
2023-01-10 15:47:23 +00:00
|
|
|
return "T1B1"
|
2023-06-02 11:31:54 +00:00
|
|
|
elif model == "T":
|
2023-01-10 15:47:23 +00:00
|
|
|
return "T2T1"
|
2023-06-02 11:31:54 +00:00
|
|
|
elif model == "R":
|
2023-01-10 15:47:23 +00:00
|
|
|
return "T2B1"
|
2023-08-24 09:33:50 +00:00
|
|
|
elif model == "T3T1":
|
|
|
|
return "T3T1"
|
2023-07-17 14:07:31 +00:00
|
|
|
elif model == "DISC1":
|
2023-02-09 22:56:03 +00:00
|
|
|
return "D001"
|
2023-06-28 08:51:37 +00:00
|
|
|
elif model == "DISC2":
|
|
|
|
return "D002"
|
2023-01-10 15:47:23 +00:00
|
|
|
else:
|
|
|
|
raise Exception("Unknown model")
|
2023-01-10 21:28:57 +00:00
|
|
|
|
|
|
|
|
2023-06-02 11:31:54 +00:00
|
|
|
def get_version(file: str) -> str:
|
2023-01-10 21:28:57 +00:00
|
|
|
major = 0
|
|
|
|
minor = 0
|
|
|
|
patch = 0
|
|
|
|
|
2023-06-02 11:31:54 +00:00
|
|
|
file_path = PROJECT_ROOT / file
|
|
|
|
with open(file_path, "r") as f:
|
2023-01-10 21:28:57 +00:00
|
|
|
for line in f:
|
2023-06-02 11:31:54 +00:00
|
|
|
if line.startswith("#define VERSION_MAJOR "):
|
|
|
|
major = line.split("VERSION_MAJOR")[1].strip()
|
|
|
|
if line.startswith("#define VERSION_MINOR "):
|
|
|
|
minor = line.split("VERSION_MINOR")[1].strip()
|
|
|
|
if line.startswith("#define VERSION_PATCH "):
|
|
|
|
patch = line.split("VERSION_PATCH")[1].strip()
|
|
|
|
return f"{major}.{minor}.{patch}"
|
2023-01-10 21:28:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_git_revision_hash() -> str:
|
2023-06-02 11:31:54 +00:00
|
|
|
return subprocess.check_output(["git", "rev-parse", "HEAD"]).decode("ascii").strip()
|
2023-01-10 21:28:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_git_revision_short_hash() -> str:
|
2023-06-02 11:31:54 +00:00
|
|
|
return (
|
|
|
|
subprocess.check_output(["git", "rev-parse", "--short", "HEAD"])
|
|
|
|
.decode("ascii")
|
|
|
|
.strip()
|
|
|
|
)
|
2023-01-10 21:28:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_git_modified() -> bool:
|
2023-06-02 11:31:54 +00:00
|
|
|
return (
|
|
|
|
subprocess.check_output(["git", "diff", "--name-status"])
|
|
|
|
.decode("ascii")
|
|
|
|
.strip()
|
|
|
|
!= ""
|
|
|
|
)
|
2023-01-30 14:36:22 +00:00
|
|
|
|
|
|
|
|
2023-06-02 11:31:54 +00:00
|
|
|
def get_defs_for_cmake(defs: list[str | tuple[str, str]]) -> list[str]:
|
|
|
|
result: list[str] = []
|
2023-01-30 14:36:22 +00:00
|
|
|
for d in defs:
|
|
|
|
if type(d) is tuple:
|
|
|
|
result.append(d[0] + "=" + d[1])
|
|
|
|
else:
|
|
|
|
result.append(d)
|
|
|
|
return result
|
2023-09-26 14:28:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _compress(data: bytes) -> bytes:
|
|
|
|
z = zlib.compressobj(level=9, wbits=-10)
|
|
|
|
return z.compress(data) + z.flush()
|
|
|
|
|
|
|
|
|
2024-04-12 20:39:30 +00:00
|
|
|
def get_bindgen_defines(
|
|
|
|
defines: list[str | tuple[str, str]], paths: list[str]
|
|
|
|
) -> tuple(str, str):
|
|
|
|
rest_defs = []
|
|
|
|
for d in defines:
|
2024-04-25 14:44:42 +00:00
|
|
|
if type(d) is tuple:
|
|
|
|
d = f"-D{d[0]}={d[1]}"
|
|
|
|
else:
|
|
|
|
d = f"-D{d}"
|
|
|
|
d = (
|
|
|
|
d.replace('\\"', '"')
|
|
|
|
.replace("'", "'\"'\"'")
|
|
|
|
.replace('"<', "<")
|
|
|
|
.replace('>"', ">")
|
2024-04-12 20:39:30 +00:00
|
|
|
)
|
2024-04-25 14:44:42 +00:00
|
|
|
rest_defs.append(d)
|
2024-04-12 20:39:30 +00:00
|
|
|
for d in paths:
|
|
|
|
rest_defs.append(f"-I../../{d}")
|
|
|
|
|
|
|
|
return ",".join(rest_defs)
|
|
|
|
|
|
|
|
|
2023-09-26 14:28:38 +00:00
|
|
|
def embed_binary(obj_program, env, section, target_, file):
|
|
|
|
_in = f"embedded_{section}.bin.deflated"
|
|
|
|
|
|
|
|
def redefine_sym(name):
|
|
|
|
src = (
|
|
|
|
"_binary_build_firmware_"
|
|
|
|
+ _in.replace("/", "_").replace(".", "_")
|
|
|
|
+ "_"
|
|
|
|
+ name
|
|
|
|
)
|
|
|
|
dest = (
|
|
|
|
"_binary_"
|
|
|
|
+ target_.replace("/", "_").replace(".o", "_bin_deflated")
|
|
|
|
+ "_"
|
|
|
|
+ name
|
|
|
|
)
|
|
|
|
return f" --redefine-sym {src}={dest}"
|
|
|
|
|
|
|
|
def compress_action(target, source, env):
|
|
|
|
srcf = Path(str(source[0]))
|
|
|
|
dstf = Path(str(target[0]))
|
|
|
|
compressed = _compress(srcf.read_bytes())
|
|
|
|
dstf.write_bytes(compressed)
|
|
|
|
return 0
|
|
|
|
|
|
|
|
compress = env.Command(target=_in, source=file, action=compress_action)
|
|
|
|
|
|
|
|
obj_program.extend(
|
|
|
|
env.Command(
|
|
|
|
target=target_,
|
|
|
|
source=_in,
|
|
|
|
action="$OBJCOPY -I binary -O elf32-littlearm -B arm"
|
|
|
|
f" --rename-section .data=.{section}"
|
|
|
|
+ redefine_sym("start")
|
|
|
|
+ redefine_sym("end")
|
|
|
|
+ redefine_sym("size")
|
|
|
|
+ " $SOURCE $TARGET",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
env.Depends(obj_program, compress)
|