2019-12-24 08:17:14 +03:00
|
|
|
{ config, pkgs, lib, utils, ... }:
|
2019-09-22 05:40:24 +03:00
|
|
|
|
|
|
|
let
|
2019-12-21 08:37:24 +03:00
|
|
|
inherit (pkgs)
|
|
|
|
busybox
|
|
|
|
makeInitrd
|
|
|
|
mkExtraUtils
|
|
|
|
runCommandNoCC
|
|
|
|
udev
|
2019-12-23 22:06:57 +03:00
|
|
|
writeText
|
|
|
|
;
|
|
|
|
inherit (lib)
|
2019-12-24 08:17:14 +03:00
|
|
|
concatMap
|
|
|
|
concatStringsSep
|
|
|
|
filter
|
2019-12-23 22:06:57 +03:00
|
|
|
flatten
|
2019-12-25 03:49:54 +03:00
|
|
|
mkOption
|
2019-12-23 22:06:57 +03:00
|
|
|
optionalString
|
|
|
|
optionals
|
2019-12-25 03:49:54 +03:00
|
|
|
types
|
2019-12-23 22:06:57 +03:00
|
|
|
;
|
|
|
|
inherit (builtins)
|
2019-12-24 08:17:14 +03:00
|
|
|
listToAttrs
|
2019-12-23 22:06:57 +03:00
|
|
|
toJSON
|
2019-12-21 08:37:24 +03:00
|
|
|
;
|
2019-12-23 06:36:17 +03:00
|
|
|
|
2020-01-13 08:56:52 +03:00
|
|
|
JSONValue = with lib.types; let
|
|
|
|
valueType = nullOr (oneOf [
|
|
|
|
bool
|
|
|
|
int
|
|
|
|
float
|
|
|
|
str
|
|
|
|
(lazyAttrsOf valueType)
|
|
|
|
(listOf valueType)
|
|
|
|
]) // {
|
|
|
|
description = "JSON value";
|
|
|
|
emptyValue.value = {};
|
|
|
|
};
|
|
|
|
in valueType;
|
|
|
|
|
2020-02-03 08:16:13 +03:00
|
|
|
inherit (config.mobile.boot.stage-1) earlyInitScripts;
|
2019-12-23 06:36:17 +03:00
|
|
|
|
2020-02-28 07:05:36 +03:00
|
|
|
# The script loader
|
2020-03-02 06:22:09 +03:00
|
|
|
loader = "${mobile-nixos-script-loader}/bin/loader";
|
2020-02-28 07:05:36 +03:00
|
|
|
|
|
|
|
# The init "script"
|
|
|
|
initScript = "${mobile-nixos-init}/libexec/init.mrb";
|
2020-02-03 08:16:13 +03:00
|
|
|
|
|
|
|
# Where we install the "real" init program.
|
|
|
|
# We are wrapping it with a minimal shell script to do some early accounting,
|
|
|
|
# mainly for the boot logs.
|
2020-02-28 07:05:36 +03:00
|
|
|
loaderPath = "/loader";
|
2019-12-23 06:36:48 +03:00
|
|
|
|
2019-12-23 06:36:17 +03:00
|
|
|
# TODO: define as an option
|
|
|
|
withStrace = false;
|
|
|
|
|
2019-09-22 05:40:24 +03:00
|
|
|
device_config = config.mobile.device;
|
2019-12-21 08:37:24 +03:00
|
|
|
device_name = device_config.name;
|
|
|
|
|
2019-09-22 05:40:24 +03:00
|
|
|
stage-1 = config.mobile.boot.stage-1;
|
2019-12-21 08:37:24 +03:00
|
|
|
|
2020-03-02 06:22:09 +03:00
|
|
|
mobile-nixos-script-loader = pkgs.pkgsStatic.callPackage ../boot/script-loader {};
|
2019-12-25 03:49:54 +03:00
|
|
|
mobile-nixos-init = pkgs.pkgsStatic.callPackage ../boot/init {
|
2020-03-02 06:22:09 +03:00
|
|
|
script-loader = mobile-nixos-script-loader;
|
2019-12-25 03:49:54 +03:00
|
|
|
inherit (config.mobile.boot.stage-1) tasks;
|
|
|
|
};
|
|
|
|
|
2019-12-23 06:36:17 +03:00
|
|
|
shell = "${extraUtils}/bin/sh";
|
2020-02-28 07:05:36 +03:00
|
|
|
initWrapper = pkgs.writeScript "init-wrapper" ''
|
2019-12-23 06:36:17 +03:00
|
|
|
#!${shell}
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo "***************************************"
|
|
|
|
echo "* Mobile NixOS stage-0 script wrapper *"
|
|
|
|
echo "***************************************"
|
|
|
|
echo
|
|
|
|
|
2020-02-03 08:16:13 +03:00
|
|
|
${earlyInitScripts}
|
2019-12-23 06:36:48 +03:00
|
|
|
|
2020-02-28 07:05:36 +03:00
|
|
|
exec ${optionalString withStrace "${extraUtils}/bin/strace -f"} \
|
|
|
|
${loaderPath} \
|
|
|
|
/init.mrb
|
2019-12-23 06:36:17 +03:00
|
|
|
'';
|
|
|
|
|
2020-01-12 03:36:23 +03:00
|
|
|
bootConfigFile = writeText "${device_name}-boot-config" (toJSON config.mobile.boot.stage-1.bootConfig);
|
2019-12-23 22:06:57 +03:00
|
|
|
|
2019-12-21 08:37:24 +03:00
|
|
|
contents =
|
|
|
|
(optionals (stage-1 ? contents) (flatten stage-1.contents))
|
|
|
|
++ [
|
|
|
|
# Populate /bin/sh to stay POSIXLY compliant.
|
2019-12-23 22:08:15 +03:00
|
|
|
{ object = "${extraUtils}/bin/sh"; symlink = "/bin/sh"; }
|
2019-12-21 08:37:24 +03:00
|
|
|
|
2019-12-23 22:06:57 +03:00
|
|
|
# The mostly device-specific configuration for the bootloader.
|
|
|
|
{ object = bootConfigFile; symlink = "/etc/boot/config"; }
|
|
|
|
|
2019-12-21 08:37:24 +03:00
|
|
|
# FIXME: udev/udevRules module.
|
|
|
|
{ object = udevRules; symlink = "/etc/udev/rules.d"; }
|
2020-02-28 07:05:36 +03:00
|
|
|
|
|
|
|
# Init components
|
|
|
|
{ object = loader; symlink = loaderPath; }
|
|
|
|
{ object = initWrapper; symlink = "/init"; }
|
|
|
|
{ object = initScript; symlink = "/init.mrb"; }
|
2019-12-23 06:36:17 +03:00
|
|
|
]
|
2019-12-21 08:37:24 +03:00
|
|
|
;
|
|
|
|
|
2019-12-24 08:17:14 +03:00
|
|
|
# The initrd only has to mount `/` or any FS marked as necessary for
|
|
|
|
# booting (such as the FS containing `/nix/store`, or an FS needed for
|
|
|
|
# mounting `/`, like `/` on a loopback).
|
|
|
|
bootFileSystems = listToAttrs (map (item: { inherit (item._module.args) name; value = item; })
|
|
|
|
(filter utils.fsNeededForBoot config.system.build.fileSystems)
|
|
|
|
);
|
|
|
|
|
2019-12-21 08:37:24 +03:00
|
|
|
udevRules = runCommandNoCC "udev-rules" {
|
|
|
|
allowedReferences = [ extraUtils ];
|
|
|
|
preferLocalBuild = true;
|
|
|
|
} ''
|
|
|
|
mkdir -p $out
|
|
|
|
|
|
|
|
# These 00-env rules are used both by udev to set the environment, and
|
|
|
|
# by our bespoke init.
|
|
|
|
# This makes it a one-stop-shop for preparing the init environment.
|
|
|
|
echo 'ENV{LD_LIBRARY_PATH}="${extraUtils}/lib"' > $out/00-env.rules
|
|
|
|
echo 'ENV{PATH}="${extraUtils}/bin"' >> $out/00-env.rules
|
|
|
|
|
|
|
|
cp -v ${udev}/lib/udev/rules.d/60-cdrom_id.rules $out/
|
|
|
|
cp -v ${udev}/lib/udev/rules.d/60-persistent-storage.rules $out/
|
|
|
|
cp -v ${udev}/lib/udev/rules.d/80-drivers.rules $out/
|
|
|
|
cp -v ${pkgs.lvm2}/lib/udev/rules.d/*.rules $out/
|
|
|
|
|
|
|
|
for i in $out/*.rules; do
|
|
|
|
substituteInPlace $i \
|
|
|
|
--replace ata_id ${extraUtils}/bin/ata_id \
|
|
|
|
--replace scsi_id ${extraUtils}/bin/scsi_id \
|
|
|
|
--replace cdrom_id ${extraUtils}/bin/cdrom_id \
|
|
|
|
--replace ${pkgs.coreutils}/bin/basename ${extraUtils}/bin/basename \
|
|
|
|
--replace ${pkgs.utillinux}/bin/blkid ${extraUtils}/bin/blkid \
|
|
|
|
--replace ${pkgs.lvm2}/sbin ${extraUtils}/bin \
|
|
|
|
--replace ${pkgs.mdadm}/sbin ${extraUtils}/sbin \
|
|
|
|
--replace ${pkgs.bash}/bin/sh ${extraUtils}/bin/sh \
|
|
|
|
--replace ${udev}/bin/udevadm ${extraUtils}/bin/udevadm
|
|
|
|
done
|
|
|
|
|
|
|
|
# Work around a bug in QEMU, which doesn't implement the "READ
|
|
|
|
# DISC INFORMATION" SCSI command:
|
|
|
|
# https://bugzilla.redhat.com/show_bug.cgi?id=609049
|
|
|
|
# As a result, `cdrom_id' doesn't print
|
|
|
|
# ID_CDROM_MEDIA_TRACK_COUNT_DATA, which in turn prevents the
|
|
|
|
# /dev/disk/by-label symlinks from being created. We need these
|
|
|
|
# in the NixOS installation CD, so use ID_CDROM_MEDIA in the
|
|
|
|
# corresponding udev rules for now. This was the behaviour in
|
|
|
|
# udev <= 154. See also
|
|
|
|
# http://www.spinics.net/lists/hotplug/msg03935.html
|
|
|
|
substituteInPlace $out/60-persistent-storage.rules \
|
|
|
|
--replace ID_CDROM_MEDIA_TRACK_COUNT_DATA ID_CDROM_MEDIA
|
|
|
|
''; # */
|
|
|
|
|
|
|
|
extraUtils = mkExtraUtils {
|
|
|
|
name = "${device_name}-extra-utils";
|
|
|
|
packages = [
|
|
|
|
busybox
|
|
|
|
]
|
2019-12-23 06:36:17 +03:00
|
|
|
++ optionals (stage-1 ? extraUtils) stage-1.extraUtils
|
|
|
|
++ [{
|
2019-12-21 08:37:24 +03:00
|
|
|
package = runCommandNoCC "empty" {} "mkdir -p $out";
|
|
|
|
extraCommand =
|
2019-12-23 06:36:17 +03:00
|
|
|
let
|
|
|
|
inherit (pkgs) udev;
|
|
|
|
in
|
2019-12-21 08:37:24 +03:00
|
|
|
''
|
|
|
|
# Copy udev.
|
|
|
|
copy_bin_and_libs ${udev}/lib/systemd/systemd-udevd
|
|
|
|
copy_bin_and_libs ${udev}/bin/udevadm
|
|
|
|
for BIN in ${udev}/lib/udev/*_id; do
|
|
|
|
copy_bin_and_libs $BIN
|
|
|
|
done
|
|
|
|
''
|
|
|
|
;
|
|
|
|
}]
|
2019-12-23 06:36:17 +03:00
|
|
|
++ optionals withStrace [
|
|
|
|
{
|
2020-01-28 04:38:11 +03:00
|
|
|
# Remove libunwind, allows us to skip requiring libgcc_s
|
|
|
|
package = pkgs.strace.overrideAttrs(old: { buildInputs = []; });
|
2019-12-23 06:36:17 +03:00
|
|
|
}
|
|
|
|
]
|
2019-12-21 08:37:24 +03:00
|
|
|
;
|
|
|
|
};
|
|
|
|
|
|
|
|
initrd = makeInitrd {
|
|
|
|
name = "initrd-${device_config.name}";
|
|
|
|
inherit contents;
|
|
|
|
};
|
2019-09-22 05:40:24 +03:00
|
|
|
in
|
2019-12-21 08:37:24 +03:00
|
|
|
{
|
2019-12-25 03:49:54 +03:00
|
|
|
options = {
|
|
|
|
mobile.boot.stage-1.tasks = mkOption {
|
|
|
|
type = with types; listOf (either package path);
|
|
|
|
default = [];
|
|
|
|
internal = true;
|
|
|
|
description = "
|
|
|
|
Add tasks to the boot/init program.
|
|
|
|
The build system for boot/init will `find -iname '*.rb'` the given paths.
|
|
|
|
";
|
|
|
|
};
|
2020-01-12 03:36:23 +03:00
|
|
|
mobile.boot.stage-1.bootConfig = mkOption {
|
2020-01-13 08:56:52 +03:00
|
|
|
type = JSONValue;
|
2020-01-12 03:36:23 +03:00
|
|
|
default = {};
|
|
|
|
internal = true;
|
|
|
|
description = ''
|
|
|
|
The things being put in the JSON configuration file in stage-1.
|
|
|
|
'';
|
|
|
|
};
|
2020-01-14 00:33:40 +03:00
|
|
|
mobile.boot.stage-1.crashToBootloader = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
When the stage-1 bootloader crashes, prefer rebooting directly to
|
|
|
|
bootloader rather than panic by killing init.
|
|
|
|
|
|
|
|
This may be preferrable for devices with direct serial access.
|
|
|
|
|
|
|
|
Note that console ramoops requires the kernel to panic, this should
|
|
|
|
be set to false if you rely on console ramoops to debug issues.
|
|
|
|
'';
|
|
|
|
};
|
2020-02-03 08:16:13 +03:00
|
|
|
mobile.boot.stage-1.earlyInitScripts = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
Additional shell commands to run before the actual init.
|
|
|
|
|
|
|
|
Prefer writing a task. This should be used mainly to redirect logging,
|
|
|
|
or do setup that is otherwise impossible in the init, like running it
|
|
|
|
against strace.
|
|
|
|
'';
|
|
|
|
internal = true;
|
|
|
|
};
|
2019-12-25 03:49:54 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
2020-02-03 08:15:39 +03:00
|
|
|
system.build.extraUtils = extraUtils;
|
2019-12-25 03:49:54 +03:00
|
|
|
system.build.initrd = "${initrd}/initrd";
|
|
|
|
boot.specialFileSystems = {
|
2020-01-12 03:36:23 +03:00
|
|
|
# HACK: as we're using isContainer to bypass some NixOS stuff
|
|
|
|
# See <nixpkgs/nixos/modules/tasks/filesystems.nix>
|
2019-12-25 03:49:54 +03:00
|
|
|
"/sys" = { fsType = "sysfs"; options = [ "nosuid" "noexec" "nodev" ]; };
|
|
|
|
};
|
2020-01-12 03:36:23 +03:00
|
|
|
|
|
|
|
mobile.boot.stage-1.bootConfig = {
|
|
|
|
device = {
|
|
|
|
inherit (device_config) name;
|
2020-02-29 06:01:11 +03:00
|
|
|
boot_as_recovery = if device_config.info ? boot_as_recovery
|
|
|
|
then device_config.info.boot_as_recovery
|
|
|
|
else false;
|
2020-01-12 03:36:23 +03:00
|
|
|
};
|
|
|
|
kernel = {
|
|
|
|
inherit (config.mobile.boot.stage-1.kernel) modules;
|
|
|
|
};
|
|
|
|
|
|
|
|
# Literally transmit some nixos configurations.
|
|
|
|
nixos = {
|
|
|
|
boot.specialFileSystems = config.boot.specialFileSystems;
|
|
|
|
};
|
|
|
|
|
|
|
|
inherit bootFileSystems;
|
|
|
|
|
|
|
|
boot = {
|
2020-01-14 00:33:40 +03:00
|
|
|
inherit (config.mobile.boot.stage-1) fail crashToBootloader;
|
2020-01-12 03:36:23 +03:00
|
|
|
};
|
|
|
|
};
|
2019-12-24 05:53:00 +03:00
|
|
|
};
|
2019-12-21 08:37:24 +03:00
|
|
|
}
|