forked from christinaa/rpi-open-firmware
-
Notifications
You must be signed in to change notification settings - Fork 18
/
default.nix
345 lines (343 loc) · 12.1 KB
/
default.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
{ kernelOverride ? null
}:
let
sources = import ./nix/sources.nix;
haskellNix = import sources."haskell.nix" {
system = "x86_64-linux";
};
pkgs = import sources.nixpkgs {
inherit (haskellNix.nixpkgsArgs) system config;
overlays = haskellNix.nixpkgsArgs.overlays ++ [ overlay ];
};
lib = pkgs.lib;
vc4 = pkgs.pkgsCross.vc4;
arm = pkgs.pkgsCross.arm-embedded;
arm7 = pkgs.pkgsCross.armv7l-hf-multiplatform;
arm6 = pkgs.pkgsCross.raspberryPi;
aarch64 = pkgs.pkgsCross.aarch64-multiplatform;
arm64 = pkgs.pkgsCross.aarch64-embedded;
x86_64 = pkgs;
kernels = {
linux_rpi1 = arm6.linux_rpi1;
linux_rpi2 = arm7.linux_rpi2;
linux_rpi3 = aarch64.linux_rpi3;
linux_rpi4 = aarch64.linux_rpi4;
};
trimHaskellNixTree = input: filter:
let
f1 = k: v: {
cexes = input.config.hsPkgs.${k}.components.exes;
};
in lib.mapAttrs f1 filter;
overlay = self: super: {
bcm2835 = self.callPackage ./bcm2835.nix {};
tlsf = self.stdenv.mkDerivation {
name = "tlsf";
src = lib.cleanSource ./tlsf;
};
bash-completion = super.bash-completion.overrideAttrs (drv: {
doCheck = false;
});
#linux_rpi2 = super.linux_rpi2.overrideAttrs (drv: {
#nativeBuildInputs = drv.nativeBuildInputs ++ (with self; [ ncurses pkgconfig ]);
#});
common = self.callPackage ./common {};
# nix-shell -p haskellPackages.cabal-install haskellPackages.ghc --run "cabal new-configure"
# nt/bin/plan-to-nix --output . --plan-json dist-newstyle/cache/plan.json --cabal-project cabal.project
pkgSet = self.haskell-nix.mkCabalProjectPkgSet {
plan-pkgs = import ./pkgs.nix;
modules = [
{
nonReinstallablePkgs = [
"rts" "ghc-heap" "ghc-prim" "integer-gmp" "integer-simple" "base"
"deepseq" "array" "ghc-boot-th" "pretty" "template-haskell"
"ghcjs-prim" "ghcjs-th"
"ghc-boot"
"ghc" "array" "binary" "bytestring" "containers"
"filepath" "ghc-boot" "ghc-compact" "ghc-prim"
"hpc"
"mtl" "parsec" "text" "transformers"
];
}
];
};
uart-manager = self.stdenv.mkDerivation {
name = "uart-manager";
src = ./uart-manager;
};
utils = self.callPackage ./utils {};
notc = self.stdenv.mkDerivation {
name = "notc";
src = lib.cleanSource ./notc;
buildInputs = [ self.common ];
propagatedBuildInputs = [];
enableParallelBuilding = true;
hardeningDisable = [ "fortify" "stackprotector" ];
dontStrip = true;
};
libdrm = super.libdrm.override { withValgrind = false; };
libgpgerror = self.callPackage "${sources.nixpkgs-old}/pkgs/development/libraries/libgpg-error/default.nix" {};
chainloader = arm.stdenv.mkDerivation {
name = "chainloader";
src = lib.cleanSource ./arm_chainloader;
buildInputs = [ self.tlsf self.common self.notc ];
enableParallelBuilding = true;
installPhase = ''
#$OBJDUMP -t build/arm_chainloader.bin.elf | sort -rk4
mkdir -p $out/nix-support
cp build/arm_chainloader.{bin,elf,map} $out/
$OBJDUMP -S build/arm_chainloader.elf > $out/chainloader.S
cat <<EOF > $out/nix-support/hydra-metrics
arm_chainloader.bin $(stat --printf=%s $out/arm_chainloader.bin) bytes
EOF
'';
};
chainloader64 = arm64.stdenv.mkDerivation {
name = "chainloader64";
src = lib.cleanSource ./arm64;
buildInputs = [ self.common self.notc ];
hardeningDisable = [ "fortify" "stackprotector" ];
enableParallelBuilding = true;
dontStrip = true;
};
firmware = vc4.stdenv.mkDerivation {
name = "firmware";
src = lib.cleanSource ./firmware;
buildInputs = [ self.common self.notc ];
nativeBuildInputs = [ x86_64.uart-manager ];
preBuild = ''
mkdir -p arm_chainloader
rm arm_chainloader/build || true
ln -s ${arm.chainloader} arm_chainloader/build
'';
shellHook = ''
rm -v ${toString ./firmware}/arm_chainloader.o || true
mkdir -vp ${toString ./firmware}/arm_chainloader
rm -v ${toString ./firmware}/arm_chainloader/build || true
ln -sv ${arm.chainloader} ${toString ./firmware}/arm_chainloader/build
'';
enableParallelBuilding = true;
dontPatchELF = true;
dontStrip = true;
installPhase = ''
mkdir -p $out/nix-support
cp build/bootcode.{bin,elf,map} $out/
ln -s ${arm.chainloader} $out/arm
$OBJDUMP -d $out/bootcode.elf > $out/bootcode.S
cat <<EOF > $out/nix-support/hydra-metrics
bootcode.bin $(stat --printf=%s $out/bootcode.bin) bytes
bootcode.elf $(stat --printf=%s $out/bootcode.elf) bytes
EOF
'';
};
script = pkgs.writeTextFile {
name = "init";
text = ''
#!${self.busybox}/bin/ash
export PATH=/bin
mknod /dev/kmsg c 1 11
exec > /dev/kmsg 2>&1
mount -t proc proc proc
mount -t sysfs sys sys
mount -t devtmpfs dev dev
mount -t debugfs debugfs /sys/kernel/debug
exec > /dev/ttyAMA0 2>&1 < /dev/ttyAMA0
/bin/sh > /dev/ttyAMA0 < /dev/ttyAMA0
echo sh failed
'';
executable = true;
};
myinit = self.stdenv.mkDerivation {
name = "myinit";
nativeBuildInputs = [ x86_64.nukeReferences ];
buildCommand = ''
$CC ${./my-init.c} -o $out
nuke-refs -e ${self.stdenv.cc.libc.out} $out
'';
};
raspi-gpio = self.stdenv.mkDerivation {
name = "raspi-gpio";
src = self.fetchFromGitHub {
owner = "RPi-Distro";
repo = "raspi-gpio";
rev = "4edfde183ff3ac9ed66cdc015ae25e45f3a5502d";
sha256 = "0246m7sh04nbdqmvfgkf456ah0c07qhy0ij99dyqy906df3rvjgy";
};
};
initrd-tools = self.buildEnv {
name = "initrd-tools";
paths = [ self.raspi-gpio self.busybox ];
};
initrd = self.makeInitrd {
contents = [
{
object = "${self.initrd-tools}/bin";
symlink = "/bin";
}
{
object = self.script;
symlink = "/init";
}
];
};
bootdir = self.buildPackages.runCommandCC "bootdir" { buildInputs = [ pkgs.dtc ]; } ''
mkdir $out
cd $out
cp ${vc4.firmware}/bootcode.bin bootcode.bin
echo print-fatal-signals=1 console=ttyAMA0,115200 earlyprintk loglevel=7 root=/dev/mmcblk0p2 printk.devkmsg=on > cmdline.txt
builddtb() {
cc -x assembler-with-cpp -E $1 -o temp
egrep -v '^#' < temp > temp2
dtc temp2 -o $2
rm temp temp2
}
builddtb ${./rpi1.dts} rpi1.dtb
builddtb ${./rpi2.dts} rpi2.dtb
builddtb ${./rpi3.dts} rpi3.dtb
${if kernelOverride == null then ''
cp ${arm7.linux_rpi2}/zImage zImage
'' else ''
cp ${kernelOverride} zImage
''}
echo bootdir is $out
'';
diskImage = let
zImage = pkgs.fetchurl {
url = "https://ext.earthtools.ca/videos/private/rpi/zImage-2020-05-19";
sha256 = "09kijy3rrwzf6zgrq3pbww9267b1dr0s9rippz7ygk354lr3g7c8";
};
eval = import (self.path + "/nixos") {
configuration = {
imports = [
./nixos.nix
(pkgs.path + "/nixos/modules/installer/cd-dvd/sd-image.nix")
];
users.users.root.initialPassword = "password";
sdImage = {
compressImage = true;
firmwareSize = 128;
populateRootCommands = ''
touch files/dummy.txt
'';
populateFirmwareCommands = ''
for file in rpi2.dtb rpi3.dtb bootcode.bin; do
cp -v ${self.bootdir}/$file firmware/
done
cp -vL ${zImage} firmware/zImage
cp -vL ${eval.config.system.build.toplevel}/initrd firmware/initrd
echo "systemConfig=${eval.config.system.build.toplevel} init=${eval.config.system.build.toplevel}/init $(cat ${eval.config.system.build.toplevel}/kernel-params)" > firmware/cmdline.txt
'';
};
};
};
in eval.config.system.build.sdImage;
helper = pkgs.writeShellScript "helper" ''
set -e
set -x
mount -v /dev/mmcblk0p1 /mnt
cp -v ${self.bootdir}/* /mnt/
ls -ltrh /mnt/
umount /mnt
'';
test-script = pkgs.writeShellScript "test-script" ''
#!${self.stdenv.shell}
${self.qemu}/bin/qemu-system-x86_64 -kernel ${self.linux}/bzImage -initrd ${self.initrd}/initrd -nographic -append 'console=ttyS0,115200'
'';
};
dtbFiles = pkgs.runCommand "dtb-files" { buildInputs = [ pkgs.dtc pkgs.strace pkgs.stdenv.cc.cc ]; src = ./dts; } ''
unpackPhase
cd $sourceRoot
mkdir $out
gcc -v -E -x assembler-with-cpp bcm2837-rpi-3-b.dts -o preprocessed.dts -I ${arm7.linux_rpi2.src}/include/
cat preprocessed.dts | egrep -v '^#' > $out/rpi3b.dts
echo compiling
dtc $out/rpi3b.dts -o $out/rpi3b.dtb
'';
nixos = (import (sources.nixpkgs + "/nixos") { configuration = ./nixos-with-fs.nix; });
testcycle = pkgs.writeShellScript "testcycle" ''
set -e
scp ${vc4.firmware}/bootcode.bin [email protected]:/tftproot/open-firmware/bootcode.bin
exec ${x86_64.uart-manager}/bin/uart-manager
'';
filterArmUserlandPackages = input: {
inherit (input) initrd bcm2835 busybox openssl diskImage bootdir utils nix raspberrypi-tools systemd libdrm;
#inherit (aarch64) ubootRaspberryPi3_64bit linux_rpi3 bcm2835;
hs = trimHaskellNixTree input.pkgSet { hs-gpio = true; };
};
in pkgs.lib.fix (self: {
inherit dtbFiles testcycle kernels;
arm64 = {
inherit (arm64) common; #chainloader64;
};
vc4 = {
inherit (vc4) tlsf firmware common notc;
#gdb = vc4.buildPackages.gdb;
};
arm = {
inherit (arm) tlsf chainloader common notc;
};
arm6 = filterArmUserlandPackages arm6;
arm7 = filterArmUserlandPackages arm7;
aarch64 = filterArmUserlandPackages aarch64;
x86_64 = {
inherit (x86_64) test-script uart-manager;
#haskell-nix = {
# inherit (x86_64.haskell-nix) nix-tools;
#};
};
# make $makeFlags menuconfig
# time make $makeFlags zImage -j8
kernelShell = arm7.linux_rpi2.overrideDerivation (drv: {
nativeBuildInputs = drv.nativeBuildInputs ++ (with x86_64; [ ncurses pkgconfig ]);
makeFlags = drv.makeFlags ++ [ "O=build-v7" ];
shellHook = ''
addToSearchPath PKG_CONFIG_PATH ${x86_64.ncurses.dev}/lib/pkgconfig
echo to configure: 'make $makeFlags menuconfig'
echo to build: 'time make $makeFlags zImage -j8'
'';
});
kernelShellv6 = arm6.linux_rpi1.overrideDerivation (drv: {
nativeBuildInputs = drv.nativeBuildInputs ++ (with x86_64; [ ncurses pkgconfig ]);
shellHook = ''
addToSearchPath PKG_CONFIG_PATH ${x86_64.ncurses.dev}/lib/pkgconfig
echo to configure: 'make $makeFlags menuconfig'
echo to build: 'time make $makeFlags zImage -j8'
'';
});
# bcmrpi3_defconfig aarch64 pi3 base config
kernelShell64 = aarch64.linux_rpi3.overrideDerivation (drv: {
nativeBuildInputs = drv.nativeBuildInputs ++ (with x86_64; [ ncurses pkgconfig ]);
makeFlags = drv.makeFlags ++ [ "O=build-64" ];
shellHook = ''
addToSearchPath PKG_CONFIG_PATH ${x86_64.ncurses.dev}/lib/pkgconfig
echo to configure: 'make $makeFlags menuconfig'
echo to build: 'time make $makeFlags Image -j8'
'';
});
aarch64-shell = x86_64.stdenv.mkDerivation {
name = "aarch64-shell";
buildInputs = [
x86_64.ddd
arm7.buildPackages.gdb
aarch64.buildPackages.gdb
x86_64.telnet
];
};
uboot-shell = aarch64.ubootRaspberryPi3_64bit.overrideAttrs (drv: {
# export NIX_BUILD_LDFLAGS="${NIX_BUILD_LDFLAGS} -lncursesw"
# make $makeFlags
nativeBuildInputs = drv.nativeBuildInputs ++ (with x86_64; [ ncurses pkgconfig ]);
});
nixos = {
inherit (nixos) system;
inherit (nixos.config.system.build) initialRamdisk kernel;
kernelConfig = nixos.config.system.build.kernel.configfile;
};
test1 = pkgs.callPackage (pkgs.path + "/nixos/lib/make-system-tarball.nix") {
contents = [];
storeContents = [
{ symlink = "/gdb"; object = arm6.gdb; }
{ symlink = "/strace"; object = arm6.strace; }
];
};
})