Skip to content

Commit

Permalink
Merge pull request 'feat: add proper A/B schema' (telent#1) from prei…
Browse files Browse the repository at this point in the history
…nit-ab into main

Reviewed-on: https://git.dgnum.eu/DGNum/liminix/pulls/1
  • Loading branch information
Ryan Lahfa committed May 24, 2024
2 parents 471c63b + 9513452 commit 1e9204f
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 186 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ _build
*-secrets.nix
examples/static-leases.nix
/doc/hardware.rst
.ccls-cache
14 changes: 8 additions & 6 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
, liminix-config ? <liminix-config>
, nixpkgs ? <nixpkgs>
, borderVmConf ? ./bordervm.conf.nix
, imageType ? "primary"
}:

let
Expand All @@ -14,8 +13,7 @@ let
config = {
allowUnsupportedSystem = true; # mipsel
permittedInsecurePackages = [
"python-2.7.18.6" # kernel backports needs python <3
"python-2.7.18.7"
"python-2.7.18.8" # kernel backports needs python <3
];
};
});
Expand All @@ -36,9 +34,6 @@ let
./modules/s6
./modules/users.nix
./modules/outputs.nix
{
boot.imageType = imageType;
}
];
};
config = eval.config;
Expand Down Expand Up @@ -66,6 +61,13 @@ in {
# cross-compiling nix-shell for any package we're customizing
inherit pkgs;

deployEnv = pkgs.mkShell {
packages = with pkgs.pkgsBuildBuild; [
tufted
min-copy-closure
];
};

buildEnv = pkgs.mkShell {
packages = with pkgs.pkgsBuildBuild; [
tufted
Expand Down
155 changes: 0 additions & 155 deletions devices/zyxel-nwa50ax/b_image/mt7621_zyxel_nwa-ax-for-ab.dtsi

This file was deleted.

25 changes: 11 additions & 14 deletions devices/zyxel-nwa50ax/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
- Mount the logs partition, mount / as overlayfs of firmware ? rootfs and rootfs_data for extended data.
- Jitter-based entropy injection? Device can be slow to initialize its CRNG and hostapd will reject few clients at the start because of that.
- Defaults for hostapd based on MT7915 capabilities? See the example for one possible list.
- Remove primary/secondary hack and put it in preinit.
- Offer ways to reflash the *bootloader* itself to support direct boot via UBI and kernel upgrades via filesystem rewrite.
Vendor web page: https://www.zyxel.com/fr/fr/products/wireless/ax1800-wifi-6-dual-radio-nebulaflex-access-point-nwa50ax
Expand Down Expand Up @@ -171,6 +170,8 @@
maxLEBcount = "256";
};

flash.eraseBlockSize = 65536;

# This is a FIT containing a kernel padded and
# a UBI volume rootfs.
defaultOutput = "zyxel-nwa-fit";
Expand All @@ -181,22 +182,18 @@
alignment = 2048;

rootDevice = "ubi:rootfs";
alternativeRootDevice = "ubi:rootfs";

# Auto-attach MTD devices: ubi_a then ubi_b.
ubi.mtds = [ "ubi_a" "ubi_b" ];

dts = {
# Actually, this is not what we want.
# This DTS is insufficient.
src = ./mt7621_zyxel_nwa50ax.dtsi;
# Strong DTB assumptions:
# ubi_a and ubi_b are two MTD devices.
# If those changes, disaster will occur.
src = ./dtb/mt7621_zyxel_nwa50ax.dtsi;
includes = [
# Here's one weird trick to make `ubi` detection
# out of the box.
# We will write ubi on /dev/firmware_a:rootfs location
# and same for /dev/firmware_b:rootfs.
# How do we distinguish both?
# We can just use the DTS to point ubi at A or B.
# This, unfortunately, means that we have "two images".
# But they are really just 1 image with 2 different DTS.
# TODO: improve this hack in preinit?
(if config.boot.imageType == "primary" then "${./a_image}" else "${./b_image}")
"${./dtb}"
"${openwrt.src}/target/linux/ramips/dts"
];
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
};

partition@400000 {
label = "ubi";
label = "ubi_a";
reg = <0x800000 0x2000000>;
};
};
Expand Down
9 changes: 4 additions & 5 deletions modules/base.nix
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ in {
default = "bootargs";
description = "Kernel command line's devicetree node";
};
imageType = mkOption {
type = types.enum [ "primary" "secondary" ];
default = "primary";
};
imageFormat = mkOption {
type = types.enum ["fit" "uimage"];
default = "uimage";
Expand Down Expand Up @@ -110,7 +106,10 @@ in {
"root=${config.hardware.rootDevice}"
"rootfstype=${config.rootfsType}"
"fw_devlink=off"
] ++ lib.optional (config.rootOptions != null) "rootflags=${config.rootOptions}";
]
++ (map (mtd: "ubi.mtd=${mtd}") config.hardware.ubi.mtds)
++ lib.optional (config.rootOptions != null) "rootflags=${config.rootOptions}"
++ lib.optional (config.hardware.alternativeRootDevice != null) "altroot=${config.hardware.alternativeRootDevice}";

system.callService = path : parameters :
let
Expand Down
12 changes: 12 additions & 0 deletions modules/hardware.nix
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ in {
type = types.str;
example = "/dev/mtdblock3";
};
alternativeRootDevice = mkOption {
description = "Full path to alternative preferred root device (the B partition of your rootfs)";
type = types.nullOr types.str;
example = "ubi_b:rootfs";
default = null;
};
ubi.mtds = mkOption {
description = "List of MTD device to attach";
type = types.listOf types.str;
example = [ "ubi_a" "ubi_b" "data" ];
default = null;
};
networkInterfaces = mkOption {
type = types.attrsOf types.anything;
};
Expand Down
2 changes: 1 addition & 1 deletion modules/outputs/zyxel-nwa-fit.nix
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ on a system with pre-existing firmware and OS.
};
'';
in
pkgs.runCommand "zyxel-nwa-fit-${config.boot.imageType}" {
pkgs.runCommand "zyxel-nwa-fit" {
nativeBuildInputs = [ pkgs.pkgsBuildBuild.ubootTools pkgs.pkgsBuildBuild.dtc ];
} ''
mkimage -f ${dts} $out
Expand Down
1 change: 1 addition & 0 deletions pkgs/preinit/opts.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
struct root_opts {
char *device;
char *altdevice; /* For A/B schemas */
char *fstype;
char *mount_opts;
};
Expand Down
15 changes: 13 additions & 2 deletions pkgs/preinit/parseopts.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ void parseopts(char * cmdline, struct root_opts *opts) {
p = eat_param(p, "root=", &(opts->device));
p = eat_param(p, "rootfstype=", &(opts->fstype));
p = eat_param(p, "rootflags=", &(opts->mount_opts));
p = eat_param(p, "altroot=", &(opts->altdevice));
};
}

Expand Down Expand Up @@ -102,6 +103,14 @@ int main()
expect_equal(opts.fstype, "ubifs");
expect_equal(opts.mount_opts, "subvol=1");

// finds altroot= options
buf = strdup("liminix console=ttyS0,115200 panic=10 oops=panic init=/bin/init loglevel=8 root=/dev/ubi0_4 rootfstype=ubifs rootflags=subvol=1 fw_devlink=off mtdparts=phram0:18M(rootfs) phram.phram=phram0,0x40400000,18874368,65536 root=/dev/mtdblock0 altroot=/dev/mtdblock6 foo");
memset(&opts, '\0', sizeof opts); parseopts(buf, &opts);
expect_equal(opts.device, "/dev/mtdblock0");
expect_equal(opts.altdevice, "/dev/mtdblock6");
expect_equal(opts.fstype, "ubifs");
expect_equal(opts.mount_opts, "subvol=1");

// in case of duplicates, chooses the latter
// also: works if the option is end of string
buf = strdup("liminix console=ttyS0,115200 panic=10 oops=panic init=/bin/init loglevel=8 root=/dev/ubi0_4 rootfstype=ubifs fw_devlink=off mtdparts=phram0:18M(rootfs) phram.phram=phram0,0x40400000,18874368,65536 root=/dev/mtdblock0");
Expand Down Expand Up @@ -134,13 +143,15 @@ int main()
if(opts.fstype) die("expected null rootfstype, got \"%s\"", opts.fstype);
if(opts.device) die("expected null root, got \"%s\"", opts.device);
if(opts.mount_opts) die("expected null mount_opts, got \"%s\"", opts.mount_opts);
if(opts.altdevice) die("expected null altdevice, got \"%s\"", opts.altdevice);

// provides empty strings for empty options
buf = strdup("liminix rootfstype= fw_devlink=off root= /dev/hda1");
buf = strdup("liminix rootfstype= fw_devlink=off root= altroot= /dev/hda1");
memset(&opts, '\0', sizeof opts); parseopts(buf, &opts);

if(strlen(opts.fstype)) die("expected empty rootfstype, got \"%s\"", opts.fstype);
if(strlen(opts.device)) die("expected null root, got \"%s\"", opts.device);
if(strlen(opts.device)) die("expected empty root, got \"%s\"", opts.device);
if(strlen(opts.altdevice)) die("expected empty altroot, got \"%s\"", opts.altdevice);

expect_equal("01", pr_u32(1));
expect_equal("ab", pr_u32(0xab));
Expand Down
18 changes: 16 additions & 2 deletions pkgs/preinit/preinit.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "opts.h"

#define ERR(x) write(2, x, strlen(x))
#define AVER(c) do { if(c < 0) { ERR("failed: " #c ": error=0x" ); pr_u32(errno); ERR("\n"); } } while(0)
#define AVER(c) do { if(c < 0) { ERR("failed: " #c ": error=0x" ); pr_u32(errno); ERR ( " - "); ERR(strerror(errno)); ERR("\n"); } } while(0)

char * pr_u32(int32_t input);

Expand Down Expand Up @@ -88,8 +88,21 @@ int main(int argc, char *argv[], char *envp[])
write(1, ", opts=", 7);
write(1, opts.mount_opts, strlen(opts.mount_opts));
}
if(opts.altdevice) {
write(1, ", altdevice=", 12);
write(1, opts.altdevice, strlen(opts.altdevice));
}
write(1, ")\n", 2);
AVER(mount(opts.device, "/target/persist", opts.fstype, 0, opts.mount_opts));

if(!opts.altdevice) {
AVER(mount(opts.device, "/target/persist", opts.fstype, 0, opts.mount_opts));
} else {
if(mount(opts.device, "/target/persist", opts.fstype, 0, opts.mount_opts) < 0) {
AVER(mount(opts.altdevice, "/target/persist", opts.fstype, 0, opts.mount_opts));
}
}

// FUTUREWORK: any failure using `opts.device` should force us to consider rerunning this with the alternative rootfs.
AVER(mount("/target/persist/nix", "/target/nix",
"bind", MS_BIND, NULL));

Expand All @@ -102,6 +115,7 @@ int main(int argc, char *argv[], char *envp[])

argv[0] = "init";
argv[1] = NULL;

AVER(execve("/persist/init", argv, envp));
}
die();
Expand Down

0 comments on commit 1e9204f

Please sign in to comment.