modules/hal/samsung: init

this can be used to get baseline support for samsung exynos5 chromebook

i should probably rename it, in time
This commit is contained in:
Colin 2024-06-07 07:33:46 +00:00
parent 50450fe7fe
commit 52a0e8cf53
5 changed files with 486 additions and 0 deletions

View File

@ -2,6 +2,7 @@
{
imports = [
./pine64.nix
./samsung
./x86_64.nix
];
}

View File

@ -0,0 +1,339 @@
# device support for samsung XE303C12 "google-snow" model, specifically.
# see: <https://wiki.postmarketos.org/wiki/Samsung_Chromebook_(google-snow)>
# - build logs: <https://images.postmarketos.org/bpo/edge/google-snow/console/>
# see: <https://github.com/thefloweringash/kevin-nix>
# - related "depthcharge" chromebook, built with nix
# see: <https://mobile.nixos.org/devices/lenovo-wormdingler.html>
# - above module, integrated into an image builder
# - implementation in modules/system-types/depthcharge
# see: <https://web.archive.org/web/20191103000916/http://www.chromium.org/chromium-os/firmware-porting-guide/using-nv-u-boot-on-the-samsung-arm-chromebook>
# - referenced from u-boot `doc/` directory
# - <https://web.archive.org/web/20220813062811/https://www.chromium.org/chromium-os/how-tos-and-troubleshooting/using-an-upstream-kernel-on-snow/>
# - <https://web.archive.org/web/20240119111314/https://www.chromium.org/chromium-os/developer-information-for-chrome-os-devices/custom-firmware/>
# - google exynos5_defconfig: <https://chromium.googlesource.com/chromiumos/overlays/chromiumos-overlay/%2B/HEAD/eclass/cros-kernel>
# see: <repo:postmarketOS/pmaports:device/community/device-google-snow>
# - <https://gitlab.com/postmarketOS/boot-deploy/-/blob/5f08ebb05a520d0e6bccfcda324f12e4aac1623f/boot-deploy-functions.sh#L872>
# - deviceinfo:
# - deviceinfo_flash_method="none"
# - deviceinfo_cgpt_kpart="/boot/vmlinuz.kpart"
# - deviceinfo_cgpt_kpart_start="8192"
# - deviceinfo_cgpt_kpart_size="16384"
# - deviceinfo_kernel_cmdline="console=null"
# - deviceinfo_depthcharge_board="snow"
# - deviceinfo_generate_depthcharge_image="true"
# - deviceinfo_generate_extlinux_config="true"
# - modules-initfs:
# - drm-dp-aux-bus
# - panel-edp
# - drm-kms-helper
# - cros-ec-keyb
# - sbs-battery
# - tps65090-charger
# - uas
# - sd-mod
# - pmOS also uses a custom alsa UCM config
# - pmOS kernel package: linux-postmarketos-exynos5
# - pmOS firmware packages (for WiFi/Bluetooth): linux-firmware-mrvl linux-firmware-s5p-mfc
#
# pmOS image has disk layout:
# /dev/sdb1 8192 24575 16384 8M ChromeOS kernel
# /dev/sdb2 24576 548863 524288 256M EFI System
# /dev/sdb3 548864 31336414 30787551 14.7G Microsoft basic data
# - built using `depthcharge-tools`: <https://github.com/alpernebbi/depthcharge-tools>
# - expected chromeos disk layout documented: <https://www.chromium.org/chromium-os/developer-library/reference/device/disk-format/>
#
# typical boot process:
# - BIOS searches for a partition `ChromeOS Kernel Type GUID (fe3a2a5d-4f32-41a7-b725-accc3285a309)`
# - first 64K are reserved for sigantures (when verified boot is active)
# - then kernel, some datastructures (i.e. config.txt, the command line passed to the kernel), bootloader stub
# - BIOS loads kernel blob into RAM, then invokes the bootstub
# - bootloader stub is an EFI application. it setups up tables and jumps into the kernel.
# - so potentially i could put any EFI application here, and load the kernel myself from somewhere else?
# - partitions are all 2MiB-aligned
# according to depthcharge-tools, max image size is 8 MiB, though i don't know how strict that is.
{ config, lib, pkgs, ... }:
let
cfg = config.sane.hal.samsung;
# dtbs = "${config.system.build.kernel}/dtbs/exynos";
# dtbs = "${config.system.build.kernel}/dtbs";
# initrd = "${config.system.build.initialRamdisk}";
# kernel_file = "${config.system.build.kernel}/zImage"; #< TODO: `zImage` is specific to the `stdenv` config
# kernel-lzma = pkgs.runCommand "kernel-lzma" { nativeBuildInputs = [ pkgs.xz ]; } ''
# lzma --threads 0 < ${kernel_file} > "$out"
# '';
# cmdlineText = lib.concatStringsSep " " (config.boot.kernelParams ++ [
# "init=${config.system.build.toplevel}/init"
# ]);
# cmdlineFile = pkgs.writeTextFile {
# name = "kernel-cmdline";
# text = cmdlineText;
# };
u-boot = pkgs.buildUBoot {
defconfig = "snow_defconfig";
extraMeta.platforms = [ "armv7l-linux" ];
filesToInstall = [
"u-boot" #< ELF file
"u-boot.bin" #< raw binary, load it into RAM and jump toit
"u-boot.cfg" #< copy of Kconfig which this u-boot was compiled with
"u-boot.dtb"
"u-boot.map"
"u-boot-nodtb.bin"
"u-boot.sym"
];
};
in
{
options = {
sane.hal.samsung.enable = lib.mkEnableOption "samsung-specific hardware support";
};
config = lib.mkIf cfg.enable {
# we're limited to 8 MiB: omit the debugging utilities
# boot.initrd.availableKernelModules = lib.mkForce [];
boot.initrd.compressor = "gzip";
# boot.initrd.compressorArgs = [ "--ultra" "-22" ];
# boot.initrd.extraUtilsCommands = lib.mkForce ''
# rm -f $out/bin/{ata_id,cdrom_id,dmsetup,fido_id,lvm,scsi_id,v4l_id}
# '';
# boot.initrd.supportedFilesystems = lib.mkForce [ "btrfs" ]; #< vfat should technically be optional?
# boot.initrd.availableKernelModules = lib.mkForce [ ];
# boot.initrd.extraUdevRulesCommands = lib.mkForce "";
# boot.initrd.extraUtilsCommandsTest = lib.mkForce "";
# boot.initrd.luks.cryptoModules = lib.mkForce [];
# boot.initrd.services.udev.packages = lib.mkForce [];
# boot.initrd.services.udev.rules = lib.mkForce "";
# # boot.initrd.systemd.enable = true;
# boot.initrd.systemd.enableTpm2 = false;
# # # build systemd w/o efivarfs support, so it won't complain about that missing module
# boot.initrd.systemd.package = pkgs.systemd.override { withEfi = false; };
# boot.initrd.systemd.extraBin = lib.mkForce {};
# boot.initrd.systemd.suppressedUnits = [
# "ctrl-alt-del.target"
# "debug-shell.service"
# "emergency.service"
# "emergency.target"
# "rpcbind.target"
# # "swap.target"
# "systemd-fsck@.service"
# "systemd-bsod.service"
# ];
boot.initrd.availableKernelModules = [
# boot.initrd.kernelModules = [
# from postmarketOS
"drm-dp-aux-bus"
"panel-edp"
"drm-kms-helper"
"cros-ec-keyb"
"sbs-battery"
"tps65090-charger"
"uas"
"sd-mod"
];
# TODO: mobile-nixos says these modules break udev, if builtin or run before udev:
# "sbs-battery"
# "sbs-charger"
# "sbs-manager"
boot.kernelPackages = pkgs.linuxPackagesFor pkgs.linux-postmarketos-exynos5;
# boot.kernelPackages = pkgs.linuxPackagesFor (pkgs.linux_latest.override {
# structuredExtraConfig = with lib.kernel; {
# CC_OPTIMIZE_FOR_SIZE = lib.mkForce yes;
# };
# });
# nixpkgs.overlays = [(self: super: {
# # optimize for size, especially to get a initrd which fits within the platform size constraints
# stdenv = super.withCFlags [ "-Os" ] super.stdenv;
# libgcrypt = super.libgcrypt.overrideAttrs (base: {
# postPatch = (base.postPatch or "") + ''
# export NIX_CFLAGS_COMPILE=
# '';
# });
# })];
system.build.u-boot = u-boot;
# system.build.kernel-its = pkgs.runCommand "kernel-its-samsung" {
# nativeBuildInputs = with pkgs; [
# dtc
# ubootTools
# vboot_reference
# xz
# ];
# meta.description = ''
# ITS file is Image Tree Source: <https://gibbard.me/linux_fit_images/>
# DTS is to ITS
# as DTB is to FIT.
# '';
# } ''
# ln -s "${kernel-lzma}" kernel.lzma
# ln -s "${dtbs}" dtbs
# ln -s "${initrd}/initrd" initrd
# bash "${./make-kernel-its}" "$PWD" > $out
# '';
# system.build.kernel-lzma = kernel-lzma;
# system.build.kernel-uimg = pkgs.runCommand "kernel-uimg" {
# nativeBuildInputs = with pkgs; [
# dtc
# ubootTools
# ];
# } ''
# ln -s "${config.system.build.kernel-lzma}" kernel.lzma
# ln -s "${dtbs}" dtbs
# ln -s "${initrd}/initrd" initrd
# ln -s "${config.system.build.kernel-its}" kernel.its
# mkimage \
# -D "-I dts -O dtb -p 2048" \
# -f kernel.its \
# $out
# '';
system.build.platformPartition = pkgs.runCommandLocal "kernel-partition" {
nativeBuildInputs = with pkgs; [
vboot_reference
dtc
ubootTools
];
} ''
# according to depthcharge-tools, bootloader.bin is legacy, was used by the earliest
# chromebooks (H2C) *only*.
dd if=/dev/zero of=dummy_bootloader.bin bs=512 count=1
echo auto > dummy_config.txt
# from uboot snow_defconfig, also == CONFIG_SYS_LOAD_ADDR
CONFIG_TEXT_BASE=0x43e00000
cp ${config.system.build.u-boot}/u-boot.bin .
ubootFlags=(
-A arm # architecture
-O linux # operating system
-T kernel # image type
-C none # compression
-a $CONFIG_TEXT_BASE # load address (CONFIG_TEXT_BASE)
-e $CONFIG_TEXT_BASE # entry point (CONFIG_SYS_LOAD_ADDR), i.e. where u-boot `bootm` should jump to to execute the kernel
-n nixos-uboot # image name
-d u-boot.bin # image data
u-boot.fit # output
)
mkimage "''${ubootFlags[@]}"
futility \
--debug \
vbutil_kernel \
--version 1 \
--bootloader ./dummy_bootloader.bin \
--vmlinuz u-boot.fit \
--arch arm \
--keyblock ${pkgs.buildPackages.vboot_reference}/share/vboot/devkeys/kernel.keyblock \
--signprivate ${pkgs.buildPackages.vboot_reference}/share/vboot/devkeys/kernel_data_key.vbprivk \
--config ./dummy_config.txt \
--pack $out
'';
# system.build.platformPartition = pkgs.runCommandLocal "kernel-partition" {
# nativeBuildInputs = with pkgs; [
# dtc # for fdtget
# python3Packages.depthcharge-tools
# ubootTools # for mkimage
# vboot_reference # for futility
# ];
# } ''
# cp -R "${dtbs}" dtbs
# cp ${u-boot}/u-boot .
# depthchargectl build \
# --verbose \
# --vboot-keyblock ${pkgs.buildPackages.vboot_reference}/share/vboot/devkeys/kernel.keyblock \
# --vboot-private-key ${pkgs.buildPackages.vboot_reference}/share/vboot/devkeys/kernel_data_key.vbprivk \
# --root none \
# --ignore-initramfs \
# --board "snow" \
# --kernel ./u-boot \
# --fdtdir ./dtbs \
# --output "$out"
# '';
# system.build.platformPartition = pkgs.runCommandLocal "kernel-partition" {
# nativeBuildInputs = with pkgs; [
# dtc # for fdtget
# python3Packages.depthcharge-tools
# ubootTools # for mkimage
# vboot_reference # for futility
# ];
# # TODO: try --compress lz4 --compress lzma
# } ''
# # override default board settings (pray that the 8 MiB kpart limit isn't firmware enforced)
# echo '[boards/arm/exynos5/daisy/snow]' > boardcfg
# echo 'image-max-size = 16 MiB' >> boardcfg
# cp "${kernel_file}" zImage
# cp "${initrd}/initrd" initrd
# cp -R "${dtbs}" dtbs
# # --config ./boardcfg
# # --no-kern-guid
# depthchargectl build \
# --verbose \
# --vboot-keyblock ${pkgs.buildPackages.vboot_reference}/share/vboot/devkeys/kernel.keyblock \
# --vboot-private-key ${pkgs.buildPackages.vboot_reference}/share/vboot/devkeys/kernel_data_key.vbprivk \
# --root none \
# --ignore-initramfs \
# --board "snow" \
# --kernel ./zImage \
# --kernel-cmdline ${lib.escapeShellArg cmdlineText} \
# --initramfs ./initrd \
# --fdtdir ./dtbs \
# --output "$out"
# '';
# ArchLinuxArm method:
# system.build.platformPartition = pkgs.runCommandLocal "kernel-partition" {
# nativeBuildInputs = with pkgs; [
# vboot_reference
# ];
# } ''
# # according to depthcharge-tools, bootloader.bin is legacy, was used by the earliest
# # chromebooks (H2C) *only*.
# dd if=/dev/zero of=bootloader.bin bs=512 count=1
# futility \
# --debug \
# vbutil_kernel \
# --version 1 \
# --bootloader bootloader.bin \
# --vmlinuz ${config.system.build.kernel-uimg} \
# --arch arm7l \
# --keyblock ${pkgs.buildPackages.vboot_reference}/share/vboot/devkeys/kernel.keyblock \
# --signprivate ${pkgs.buildPackages.vboot_reference}/share/vboot/devkeys/kernel_data_key.vbprivk \
# --config ${cmdlineFile} \
# --pack $out
# '';
# nixos kernels + initrd can be hilariously large, so allocate a ton of space
sane.image.platformPartSize = 256 * 1024 * 1024;
# depthcharge firmware is designed for an A/B partition style,
# where partition A holds a kernel and partion B holds a different kernel.
# an update is to flash the currently inactive partition and then mark that one as active,
# either switching the default boot from partition A to partition B, or from B to A.
# anyway, this relies on the partitions having some extra metadata, which we add here.
# i believe this metadata is stored in a depthcharge-specific format, not anything
# which can be generalized.
sane.image.installBootloader = ''
${lib.getExe' pkgs.buildPackages.vboot_reference "cgpt"} add ${lib.concatStringsSep " " [
"-i 1" # work on the first partition (instead of adding)
"-S 1" # mark as successful (so it'll be booted from)
"-T 5" # tries remaining
"-P 10" # priority
"$out"
]}
'';
};
}

View File

@ -0,0 +1,85 @@
#!/bin/sh
# source: <https://github.com/thefloweringash/kevin-nix/blob/master/modules/make-kernel-its.sh>
toplevel=$1
cd $toplevel
dtb_files=($(find -L dtbs -type f -name '*.dtb'))
fdt_definition() {
local idx=$1
local filename=$2
local basename=$(basename $filename)
cat <<EOF
fdt-${idx}{
description = "${basename}";
data = /incbin/("${filename}");
type = "flat_dt";
arch = "arm64";
compression = "none";
hash-1{
algo = "sha1";
};
};
EOF
}
fdt_reference() {
local idx=$1
cat <<EOF
conf-${idx}{
kernel = "kernel-1";
fdt = "fdt-${idx}";
ramdisk = "ramdisk-1";
};
EOF
}
cat <<EOF
/dts-v1/;
/ {
description = "Chrome OS kernel image with one or more FDT blobs";
images {
kernel-1{
description = "kernel";
data = /incbin/("kernel.lzma");
type = "kernel_noload";
arch = "arm64";
os = "linux";
compression = "lzma";
load = <0>;
entry = <0>;
};
ramdisk-1 {
description = "ramdisk";
data = /incbin/("initrd");
type = "ramdisk";
arch = "arm64";
os = "linux";
compression = "none";
hash-1 {
algo = "sha1";
};
};
EOF
for index in "${!dtb_files[@]}"; do
fdt_definition $index ${dtb_files[$index]}
done
cat <<EOF
};
configurations {
default = "conf-0";
EOF
for index in "${!dtb_files[@]}"; do
fdt_reference $index
done
cat <<EOF
};
};
EOF

View File

@ -0,0 +1,43 @@
/dts-v1/;
/ {
description = "U-Boot mainline";
#address-cells = <1>;
images {
kernel {
description = "U-Boot mainline ";
type = "kernel_noload";
arch = "arm";
os = "linux";
data = /incbin/("u-boot");
compression = "none";
load = <0>;
entry = <0>;
hash-2 {
algo = "sha256";
};
};
fdt-1{
description = "u-boot.dtb";
data = /incbin/("u-boot.dtb");
type = "flat_dt";
arch = "arm";
compression = "none";
hash-1{
algo = "sha256";
};
};
};
configurations {
default = "config-1";
config-1 {
description = "Boot U-Boot";
kernel = "kernel";
fdt = "fdt-1";
};
};
};

View File

@ -58,6 +58,15 @@ in
default = (16 * 1024 * 1024 - 34 * 512) * 1024 * 1024 - 1;
type = types.nullOr types.int;
};
sane.image.platformPartSize = mkOption {
default = null;
type = types.nullOr types.int;
description = ''
size of the platform firmware (or, bootloader) partition, in bytes.
most platforms don't need this. the primary user is "depthcharge" chromebooks.
the partition contents is taken from `config.system.build.platformPartition`.
'';
};
sane.image.bootPartSize = mkOption {
default = 1024 * 1024 * 1024;
type = types.int;
@ -168,6 +177,15 @@ in
headerHole = cfg.extraGPTPadding;
partitions = [
(pkgs.imageBuilder.gap cfg.firstPartGap)
] ++ lib.optionals (cfg.platformPartSize != null) [
{
name = "kernel"; #< TODO: is it safe to rename this?
filename = "${config.system.build.platformPartition}";
# from: <https://www.chromium.org/chromium-os/chromiumos-design-docs/disk-format>
partitionType = "FE3A2A5D-4F32-41A7-B725-ACCC3285A309";
length = cfg.platformPartSize;
}
] ++ [
bootFsImg
nixFsImg
];