1{
2 dtc,
3 fetchFromGitHub,
4 lib,
5 pkgsBuildBuild,
6 stdenv,
7}:
8
9let
10 defaultVersion = "4.6.0";
11
12 defaultSrc = fetchFromGitHub {
13 owner = "OP-TEE";
14 repo = "optee_os";
15 rev = defaultVersion;
16 hash = "sha256-4z706DNfZE+CAPOa362CNSFhAN1KaNyKcI9C7+MRccs=";
17 };
18
19 buildOptee = lib.makeOverridable (
20 {
21 version ? null,
22 src ? null,
23 platform,
24 extraMakeFlags ? [ ],
25 extraMeta ? { },
26 ...
27 }@args:
28
29 let
30 inherit (stdenv.hostPlatform) is32bit is64bit;
31
32 taTarget =
33 {
34 "arm" = "ta_arm32";
35 "arm64" = "ta_arm64";
36 }
37 .${stdenv.hostPlatform.linuxArch};
38 in
39 stdenv.mkDerivation (
40 {
41 pname = "optee-os-${platform}";
42
43 version = if src == null then defaultVersion else version;
44
45 src = if src == null then defaultSrc else src;
46
47 postPatch = ''
48 patchShebangs $(find -type d -name scripts -printf '%p ')
49 '';
50
51 outputs = [
52 "out"
53 "devkit"
54 ];
55
56 strictDeps = true;
57
58 enableParallelBuilding = true;
59
60 depsBuildBuild = [ pkgsBuildBuild.stdenv.cc ];
61
62 nativeBuildInputs = [
63 dtc
64 (pkgsBuildBuild.python3.withPackages (
65 p: with p; [
66 pyelftools
67 cryptography
68 ]
69 ))
70 ];
71
72 makeFlags = [
73 "O=out"
74 "PLATFORM=${platform}"
75 "CFG_USER_TA_TARGETS=${taTarget}"
76 ]
77 ++ (lib.optionals (is32bit) [
78 "CFG_ARM32_core=y"
79 "CROSS_COMPILE32=${stdenv.cc.targetPrefix}"
80 ])
81 ++ (lib.optionals (is64bit) [
82 "CFG_ARM64_core=y"
83 "CROSS_COMPILE64=${stdenv.cc.targetPrefix}"
84 ])
85 ++ extraMakeFlags;
86
87 installPhase = ''
88 runHook preInstall
89
90 mkdir -p $out
91 cp out/core/{tee.elf,tee-pageable_v2.bin,tee.bin,tee-header_v2.bin,tee-pager_v2.bin,tee-raw.bin} $out
92 cp -r out/export-${taTarget} $devkit
93
94 runHook postInstall
95 '';
96
97 meta =
98 with lib;
99 {
100 description = "Trusted Execution Environment for ARM";
101 homepage = "https://github.com/OP-TEE/optee_os";
102 changelog = "https://github.com/OP-TEE/optee_os/blob/${defaultVersion}/CHANGELOG.md";
103 license = licenses.bsd2;
104 maintainers = [ maintainers.jmbaur ];
105 }
106 // extraMeta;
107 }
108 // removeAttrs args [ "extraMeta" ]
109 )
110 );
111in
112{
113 inherit buildOptee;
114
115 opteeQemuArm = buildOptee {
116 platform = "vexpress";
117 extraMakeFlags = [ "PLATFORM_FLAVOR=qemu_virt" ];
118 extraMeta.platforms = [ "armv7l-linux" ];
119 };
120
121 opteeQemuAarch64 = buildOptee {
122 platform = "vexpress";
123 extraMakeFlags = [ "PLATFORM_FLAVOR=qemu_armv8a" ];
124 extraMeta.platforms = [ "aarch64-linux" ];
125 };
126}