-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.nix
362 lines (358 loc) · 11.2 KB
/
options.nix
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
{
config,
pkgs,
lib,
...
}:
let
boardsDir = builtins.readDir ./boards;
testStartupScript = pkgs.writeScript "installer-startup-script" ''
#!/bin/sh
mkdir -p /proc && mount -t proc proc /proc
mkdir -p /sys && mount -t sysfs sysfs /sys
mkdir -p /dev && mount -t devtmpfs devtmpfs /dev
mkdir -p /dev/pts && mount -t devpts devpts /dev/pts
mkdir -p /run && mount -t tmpfs tmpfs /run
ln -sfn /proc/self/fd /dev/fd
ln -sfn /proc/self/fd/0 /dev/stdin
ln -sfn /proc/self/fd/1 /dev/stdout
ln -sfn /proc/self/fd/2 /dev/stderr
touch /etc/fstab
'';
testInittab = pkgs.writeText "inittab" ''
::sysinit:/etc/init.d/rcS
::askfirst:/bin/sh
::ctrlaltdel:/bin/reboot
::shutdown:/bin/swapoff -a
::shutdown:/bin/umount -a -r
::restart:/bin/init
'';
testInitrd = pkgs.makeInitrdNG {
compressor = "xz";
contents = [
{
source = "${pkgs.busybox}/bin/busybox";
target = "/init";
}
{
source = "${pkgs.busybox}/bin";
target = "/bin";
}
{
source = testStartupScript;
target = "/etc/init.d/rcS";
}
{
source = testInittab;
target = "/etc/inittab";
}
] ++ config.extraInitrdContents;
};
kconfigOption = lib.mkOption {
type = lib.types.attrsOf lib.types.anything;
default = { };
apply =
attrs:
lib.concatLines (
lib.mapAttrsToList (
kconfOption: answer:
let
optionName = "CONFIG_${kconfOption}";
kconfLine =
if answer ? freeform then
if
(
(lib.isPath answer.freeform || lib.isDerivation answer.freeform)
|| (lib.isString answer.freeform && builtins.match "[0-9]+" answer.freeform == null)
)
&& !(lib.hasPrefix "0x" answer.freeform)
then
"${optionName}=\"${answer.freeform}\""
else
"${optionName}=${toString answer.freeform}"
else
assert answer ? tristate;
assert answer.tristate != "m";
if answer.tristate == null then
"# ${optionName} is not set"
else
"${optionName}=${toString answer.tristate}";
in
kconfLine
) attrs
);
};
vpdOption = lib.mkOption {
type = lib.types.attrsOf (lib.types.either lib.types.str lib.types.path);
default = { };
};
applyVpd =
vpdPartition: key: value:
if lib.isPath value then
''
base64 <${value} >tmp.base64
vpd -f $out -i ${vpdPartition} -S ${key}=tmp.base64
rm tmp.base64
''
else
''
vpd -f $out -i ${vpdPartition} -s ${key}=${value}
'';
in
{
imports = [
./kernel-configs
] ++ (lib.mapAttrsToList (board: _: ./boards/${board}/config.nix) boardsDir);
options = with lib; {
board = mkOption { type = types.enum (builtins.attrNames boardsDir); };
build = mkOption {
default = { };
type = types.submoduleWith {
modules = [ { freeformType = with types; lazyAttrsOf (uniq unspecified); } ];
};
};
flashrom = {
package = mkPackageOption pkgs "flashrom-cros" { };
programmer = mkOption {
type = types.str;
default = "internal";
};
};
debug = mkEnableOption "debug";
video = mkEnableOption "video";
network = mkEnableOption "network";
chromebook = mkEnableOption "chromebook";
platform = mkOption {
type = types.nullOr (
types.attrTag {
alderlake = mkEnableOption "alderlake";
mediatek = mkEnableOption "mediatek";
qemu = mkEnableOption "qemu";
qualcomm = mkEnableOption "qualcomm";
tigerlake = mkEnableOption "tigerlake";
}
);
default = null;
};
linux = {
package = mkOption {
type = types.package;
default = pkgs.linux_6_12;
};
consoles = mkOption {
type = types.listOf types.str;
default = [ ];
};
kconfig = kconfigOption;
dtb = mkOption {
type = types.nullOr types.path;
default = null;
};
dtbPattern = mkOption {
type = types.nullOr types.str;
default = null;
};
firmware = mkOption {
type = types.listOf (
types.submodule {
options.dir = mkOption {
type = types.str;
default = ".";
};
options.pattern = mkOption { type = types.str; };
}
);
default = [ ];
};
};
coreboot = {
enable = mkEnableOption "coreboot integration" // {
default = config.chromebook;
};
wpRange.start = mkOption { type = types.str; };
wpRange.length = mkOption { type = types.str; };
kconfig = kconfigOption;
vpd.ro = vpdOption;
vpd.rw = vpdOption;
};
verifiedBoot = {
requiredSystemFeatures = mkOption {
type = types.listOf types.str;
default = [ ];
};
tbootPublicCertificate = mkOption {
type = types.path;
default = ./tests/keys/tboot/key.der;
};
tbootPrivateKey = mkOption {
type = types.path;
default = ./tests/keys/tboot/key.pem;
};
vbootRootKey = mkOption {
type = types.path;
default = ./tests/keys/root/key.vbpubk;
};
vbootFirmwarePrivkey = mkOption {
type = types.path;
default = ./tests/keys/firmware/key.vbprivk;
};
vbootFirmwareKey = mkOption {
type = types.path;
default = ./tests/keys/firmware/key.vbpubk;
};
vbootKeyblock = mkOption {
type = types.path;
default = ./tests/keys/firmware/key.keyblock;
};
};
extraInitrdContents = mkOption {
type = types.listOf (
types.submodule {
options.source = mkOption { type = types.path; };
options.target = mkOption { type = types.str; };
}
);
default = [ ];
};
};
config = {
linux.kconfig.CMDLINE = lib.kernel.freeform (
toString (
lib.optionals config.video [ "fbcon=logo-count:1" ]
++ [
"printk.devkmsg=on"
"loglevel=${if config.debug then "8" else "5"}"
]
++ map (c: "console=${c}") config.linux.consoles
)
);
extraInitrdContents = lib.optional (config.linux.firmware != [ ]) {
target = "/lib/firmware";
source = pkgs.buildPackages.runCommand "linux-firmware" { } (
lib.concatLines (
map (
{ dir, pattern }:
''
pushd ${pkgs.linux-firmware}/lib/firmware
find ${dir} -type f -name "${pattern}" -exec install -Dm0444 --target-directory=$out/${dir} {} \;
popd
''
) config.linux.firmware
)
);
};
coreboot.vpd.ro.pubkey = config.verifiedBoot.tbootPublicCertificate;
coreboot.kconfig =
with lib.kernel;
{
"DEFAULT_CONSOLE_LOGLEVEL_${if config.debug then "7" else "6"}" = yes;
PAYLOAD_NONE = unset;
VBOOT = yes;
VBOOT_ALWAYS_ENABLE_DISPLAY = yes;
VBOOT_FIRMWARE_PRIVKEY = freeform config.verifiedBoot.vbootFirmwarePrivkey;
VBOOT_KERNEL_KEY = freeform config.verifiedBoot.vbootFirmwareKey;
VBOOT_KEYBLOCK = freeform config.verifiedBoot.vbootKeyblock;
VBOOT_RECOVERY_KEY = freeform config.verifiedBoot.vbootFirmwareKey;
VBOOT_ROOT_KEY = freeform config.verifiedBoot.vbootRootKey;
VBOOT_SIGN = unset; # don't sign during build
VPD = yes;
}
// lib.optionalAttrs pkgs.hostPlatform.isx86_64 {
LINUX_INITRD = freeform "${config.build.initrd}/initrd";
PAYLOAD_FILE = freeform "${config.build.linux}/${pkgs.stdenv.hostPlatform.linux-kernel.target}";
PAYLOAD_LINUX = yes;
VBOOT_SLOTS_RW_AB = lib.mkDefault yes; # x86_64 spi flash is usually large enough for 3 vboot slots
VBOOT_X86_SHA256_ACCELERATION = yes;
}
// lib.optionalAttrs pkgs.hostPlatform.isAarch64 {
PAYLOAD_FILE = freeform "${config.build.fitImage}/uImage";
PAYLOAD_FIT = yes;
PAYLOAD_FIT_SUPPORT = yes;
VBOOT_ARMV8_CE_SHA256_ACCELERATION = yes;
VBOOT_SLOTS_RW_A = lib.mkDefault yes; # spi flash on aarch64 chromebooks is usually not large enough for 3 vboot slots
};
build = {
inherit testInitrd;
initrd = pkgs.makeInitrdNG {
prepend = [ "${pkgs.tinybootLoader}/tboot-loader.cpio.xz" ];
compressor = "xz";
contents = config.extraInitrdContents ++ [
# TODO(jared): Hack making makeInitrdNG not working with contents
# being an empty list.
{
target = "/empty";
source = pkgs.emptyFile;
}
];
};
linux = (
pkgs.callPackage ./pkgs/linux {
linux = config.linux.package;
inherit (config.linux) kconfig;
}
);
fitImage = (pkgs.callPackage ./fitimage { }) {
inherit (config) board;
inherit (config.build) linux initrd;
inherit (config.linux) dtb dtbPattern;
};
coreboot = pkgs.callPackage ./pkgs/coreboot {
inherit (config) board;
inherit (config.coreboot) kconfig;
};
firmware =
pkgs.runCommand "tinyboot-${config.board}"
{
inherit (config.verifiedBoot) requiredSystemFeatures;
nativeBuildInputs = with pkgs.buildPackages; [
cbfstool
vboot_reference # vpd
];
passthru = {
inherit (config.build)
linux
initrd
testInitrd
coreboot
;
};
env.CBFSTOOL = "${pkgs.buildPackages.cbfstool}/bin/cbfstool"; # needed by futility
}
''
dd status=none if=${config.build.coreboot}/coreboot.rom of=$out
${lib.optionalString (lib.trace "TODO: create vpd image in zig" false) ''
vpd -f $out -i RO_VPD -O
${lib.concatLines (lib.mapAttrsToList (applyVpd "RO_VPD") config.coreboot.vpd.ro)}
vpd -f $out -i RW_VPD -O
${lib.concatLines (lib.mapAttrsToList (applyVpd "RW_VPD") config.coreboot.vpd.rw)}
''}
futility sign \
--signprivate "${config.verifiedBoot.vbootFirmwarePrivkey}" \
--keyblock "${config.verifiedBoot.vbootKeyblock}" \
--kernelkey "${config.verifiedBoot.vbootFirmwareKey}" \
$out
'';
# useful for testing kernel configurations
testScript =
let
commonConsoles = map (console: "console=${console}") (
(map (serial: "${serial},115200") [
"ttyS0"
"ttyAMA0"
"ttyMSM0"
"ttymxc0"
"ttyO0"
"ttySAC2"
])
++ [ "tty0" ]
);
in
pkgs.writeShellScriptBin "tboot-test" ''
kexec -l ${config.build.linux}/${pkgs.stdenv.hostPlatform.linux-kernel.target} \
--initrd=${testInitrd}/initrd \
--command-line="${toString commonConsoles}"
systemctl kexec
'';
};
};
}