Skip to content

Commit 688ee55

Browse files
committed
refactor(nxp): parameterize i.MX platform builders to reduce duplication
This refactoring reduces code duplication across i.MX93, i.MX8MP, and i.MX8MQ platforms by extracting common build logic into parameterized shared builders. This makes it easier to maintain and add new i.MX platforms while ensuring consistency across all platforms.
1 parent 8b405e9 commit 688ee55

File tree

14 files changed

+397
-597
lines changed

14 files changed

+397
-597
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Parameterized Linux kernel builder for i.MX platforms
2+
# This builder is used across i.MX93, i.MX8MP, i.MX8MQ and similar platforms
3+
{ lib, pkgs, ... }@args:
4+
let
5+
inherit (pkgs) buildLinux;
6+
7+
# Import common kernel configuration
8+
kernelConfig = import ../lib/kernel-config.nix;
9+
in
10+
# Platform-specific parameters
11+
{
12+
pname,
13+
version,
14+
src,
15+
defconfig ? "imx_v8_defconfig",
16+
# Optional parameters
17+
extraConfig ? "",
18+
kernelPatches ? [ ],
19+
autoModules ? false,
20+
ignoreConfigErrors ? true,
21+
extraMeta ? { },
22+
}:
23+
let
24+
# Combine common i.MX kernel config with platform-specific config
25+
finalExtraConfig = kernelConfig.imxCommonKernelConfig + extraConfig;
26+
in
27+
buildLinux (
28+
args
29+
// rec {
30+
inherit
31+
version
32+
defconfig
33+
kernelPatches
34+
autoModules
35+
ignoreConfigErrors
36+
;
37+
name = pname;
38+
39+
# modDirVersion needs to be x.y.z, will automatically add .0 if needed
40+
modDirVersion = version;
41+
42+
extraConfig = finalExtraConfig;
43+
44+
inherit src;
45+
46+
meta =
47+
with lib;
48+
{
49+
homepage = "https://github.com/nxp-imx/linux-imx";
50+
license = [ licenses.gpl2Only ];
51+
platforms = [ "aarch64-linux" ];
52+
}
53+
// extraMeta;
54+
}
55+
// (args.argsOverride or { })
56+
)
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Parameterized OP-TEE OS builder for i.MX platforms
2+
# This builder is used across i.MX93, i.MX8MP, i.MX8MQ and similar platforms
3+
{
4+
lib,
5+
pkgs,
6+
# Platform-specific parameters
7+
pname,
8+
version,
9+
platformFlavor,
10+
src,
11+
# Optional parameters
12+
meta ? { },
13+
}:
14+
let
15+
inherit (pkgs.buildPackages) python3;
16+
toolchain = pkgs.gccStdenv.cc;
17+
binutils = pkgs.gccStdenv.cc.bintools.bintools_bin;
18+
cpp = pkgs.gcc;
19+
20+
# Determine PLATFORM and PLATFORM_FLAVOR from platformFlavor
21+
# Format can be either "imx-mx93evk" (full platform string) or "mx8mpevk" (just flavor, platform is "imx")
22+
# Check if it starts with "imx-" to determine if it's a full platform string or just a flavor
23+
hasFullPlatform = lib.hasPrefix "imx-" platformFlavor;
24+
platform = if hasFullPlatform then platformFlavor else "imx";
25+
flavor = if hasFullPlatform then null else platformFlavor;
26+
in
27+
pkgs.stdenv.mkDerivation {
28+
inherit pname version src;
29+
30+
nativeBuildInputs = [
31+
python3
32+
];
33+
34+
enableParallelBuilding = true;
35+
36+
propagatedBuildInputs = with python3.pkgs; [
37+
pycryptodomex
38+
pyelftools
39+
cryptography
40+
];
41+
42+
# Common postPatch for all i.MX platforms
43+
# This is the major source of code duplication - ~60 lines of identical substitutions
44+
postPatch = ''
45+
# Patch all script shebangs automatically
46+
patchShebangs scripts/
47+
patchShebangs ta/
48+
49+
# Patch toolchain paths in mk/gcc.mk
50+
substituteInPlace mk/gcc.mk \
51+
--replace "\$(CROSS_COMPILE_\$(sm))objcopy" ${binutils}/bin/${toolchain.targetPrefix}objcopy
52+
substituteInPlace mk/gcc.mk \
53+
--replace "\$(CROSS_COMPILE_\$(sm))objdump" ${binutils}/bin/${toolchain.targetPrefix}objdump
54+
substituteInPlace mk/gcc.mk \
55+
--replace "\$(CROSS_COMPILE_\$(sm))nm" ${binutils}/bin/${toolchain.targetPrefix}nm
56+
substituteInPlace mk/gcc.mk \
57+
--replace "\$(CROSS_COMPILE_\$(sm))readelf" ${binutils}/bin/${toolchain.targetPrefix}readelf
58+
substituteInPlace mk/gcc.mk \
59+
--replace "\$(CROSS_COMPILE_\$(sm))ar" ${binutils}/bin/${toolchain.targetPrefix}ar
60+
substituteInPlace mk/gcc.mk \
61+
--replace "\$(CROSS_COMPILE_\$(sm))cpp" ${cpp}/bin/cpp
62+
'';
63+
64+
makeFlags =
65+
[
66+
"PLATFORM=${platform}"
67+
]
68+
++ lib.optionals (!hasFullPlatform) [
69+
"PLATFORM_FLAVOR=${flavor}"
70+
]
71+
++ [
72+
"CFG_ARM64_core=y"
73+
"CFG_TEE_TA_LOG_LEVEL=0"
74+
"CFG_TEE_CORE_LOG_LEVEL=0"
75+
"CROSS_COMPILE=${toolchain}/bin/${toolchain.targetPrefix}"
76+
"CROSS_COMPILE64=${toolchain}/bin/${toolchain.targetPrefix}"
77+
];
78+
79+
installPhase = ''
80+
mkdir -p $out
81+
cp ./out/arm-plat-imx/core/tee-raw.bin $out/tee.bin
82+
'';
83+
84+
meta = {
85+
homepage = "https://github.com/nxp-imx/imx-optee-os";
86+
license = [ lib.licenses.bsd2 ];
87+
platforms = [ "aarch64-linux" ];
88+
} // meta;
89+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Parameterized U-Boot builder for i.MX platforms
2+
# This builder is used across i.MX93, i.MX8MP, i.MX8MQ and similar platforms
3+
{
4+
lib,
5+
stdenv,
6+
buildPackages,
7+
# Required dependencies
8+
bison,
9+
dtc,
10+
flex,
11+
gnutls,
12+
libuuid,
13+
ncurses,
14+
openssl,
15+
perl,
16+
efitools,
17+
which,
18+
# Platform-specific parameters
19+
pname,
20+
version,
21+
src,
22+
defconfig,
23+
ramdiskAddr,
24+
fdtAddr,
25+
dtbPath,
26+
# Optional parameters
27+
extraConfig ? "",
28+
extraNativeBuildInputs ? [ ],
29+
}:
30+
let
31+
# Import common U-Boot configuration
32+
ubootConfig = import ../lib/uboot-config.nix;
33+
34+
# Generate the common config with platform-specific memory addresses
35+
commonConfig = ubootConfig.imxCommonUbootConfig {
36+
inherit ramdiskAddr fdtAddr;
37+
};
38+
39+
# Combine common config with any platform-specific extra config
40+
finalExtraConfig = commonConfig + extraConfig;
41+
in
42+
stdenv.mkDerivation {
43+
inherit pname version src;
44+
45+
postPatch = ''
46+
patchShebangs tools
47+
patchShebangs scripts
48+
'';
49+
50+
nativeBuildInputs = [
51+
bison
52+
flex
53+
openssl
54+
which
55+
ncurses
56+
libuuid
57+
gnutls
58+
perl
59+
efitools
60+
] ++ extraNativeBuildInputs;
61+
62+
depsBuildBuild = [ buildPackages.stdenv.cc ];
63+
hardeningDisable = [ "all" ];
64+
enableParallelBuilding = true;
65+
66+
makeFlags = [
67+
"DTC=${lib.getExe buildPackages.dtc}"
68+
"CROSS_COMPILE=${stdenv.cc.targetPrefix}"
69+
];
70+
71+
extraConfig = finalExtraConfig;
72+
73+
passAsFile = [ "extraConfig" ];
74+
75+
configurePhase = ''
76+
runHook preConfigure
77+
78+
make ${defconfig}
79+
cat $extraConfigPath >> .config
80+
81+
runHook postConfigure
82+
'';
83+
84+
installPhase = ''
85+
runHook preInstall
86+
87+
mkdir -p $out
88+
cp ./u-boot-nodtb.bin $out
89+
cp ./spl/u-boot-spl.bin $out
90+
cp ${dtbPath} $out
91+
cp .config $out
92+
93+
runHook postInstall
94+
'';
95+
96+
dontStrip = true;
97+
}

nxp/common/lib/kernel-config.nix

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Shared kernel configuration for i.MX platforms
2+
# This configuration is used across i.MX93, i.MX8MP, i.MX8MQ and similar platforms
3+
{
4+
# Common kernel extra configuration for i.MX platforms
5+
# Includes: virtualization support, EFI boot, RAID, USB/IP, framebuffer settings
6+
imxCommonKernelConfig = ''
7+
CRYPTO_TLS m
8+
TLS y
9+
MD_RAID0 m
10+
MD_RAID1 m
11+
MD_RAID10 m
12+
MD_RAID456 m
13+
DM_VERITY m
14+
LOGO y
15+
FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER n
16+
FB_EFI n
17+
EFI_STUB y
18+
EFI y
19+
VIRTIO y
20+
VIRTIO_PCI y
21+
VIRTIO_BLK y
22+
DRM_VIRTIO_GPU y
23+
EXT4_FS y
24+
USBIP_CORE m
25+
USBIP_VHCI_HCD m
26+
USBIP_HOST m
27+
USBIP_VUDC m
28+
'';
29+
}

nxp/common/lib/uboot-config.nix

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Shared U-Boot configuration for i.MX platforms
2+
# This configuration is used across i.MX93, i.MX8MP, i.MX8MQ and similar platforms
3+
{
4+
# Generate common U-Boot extra configuration for i.MX platforms
5+
# ramdiskAddr and fdtAddr are platform-specific memory addresses
6+
imxCommonUbootConfig =
7+
{ ramdiskAddr, fdtAddr }:
8+
''
9+
CONFIG_USE_BOOTCOMMAND=y
10+
CONFIG_BOOTCOMMAND="setenv ramdisk_addr_r ${ramdiskAddr}; setenv fdt_addr_r ${fdtAddr}; run distro_bootcmd; "
11+
CONFIG_CMD_BOOTEFI_SELFTEST=y
12+
CONFIG_CMD_BOOTEFI=y
13+
CONFIG_EFI_LOADER=y
14+
CONFIG_BLK=y
15+
CONFIG_PARTITIONS=y
16+
CONFIG_DM_DEVICE_REMOVE=n
17+
CONFIG_CMD_CACHE=y
18+
'';
19+
20+
# Common U-Boot native build inputs for i.MX platforms
21+
imxCommonUbootNativeBuildInputs = [
22+
"bison"
23+
"flex"
24+
"openssl"
25+
"which"
26+
"ncurses"
27+
"libuuid"
28+
"gnutls"
29+
"openssl"
30+
"perl"
31+
"efitools"
32+
];
33+
}
Lines changed: 16 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,20 @@
11
{ pkgs, ... }@args:
2-
with pkgs;
3-
buildLinux (
4-
args
5-
// rec {
6-
version = "6.12.20";
7-
name = "imx8mp-linux";
2+
(pkgs.callPackage ../../common/bsp/imx-linux-builder.nix args) {
3+
pname = "imx8mp-linux";
4+
version = "6.12.20";
85

9-
# modDirVersion needs to be x.y.z, will automatically add .0 if needed
10-
modDirVersion = version;
6+
src = pkgs.fetchFromGitHub {
7+
owner = "nxp-imx";
8+
repo = "linux-imx";
9+
# tag: lf-6.12.20-2.0.0
10+
rev = "dfaf2136deb2af2e60b994421281ba42f1c087e0";
11+
sha256 = "sha256-ITrmj3a5YfXh/PSRTi+Rlto5uEBIAWFWtkTsO1ATXIo=";
12+
};
1113

12-
defconfig = "imx_v8_defconfig";
14+
# Platform-specific configuration (if any)
15+
extraConfig = "";
1316

14-
# https://github.com/NixOS/nixpkgs/pull/366004
15-
# introduced a breaking change that if a module is declared but it is not being used it will faill.
16-
ignoreConfigErrors = true;
17-
18-
kernelPatches = [
19-
];
20-
21-
autoModules = false;
22-
23-
extraConfig = ''
24-
CRYPTO_TLS m
25-
TLS y
26-
MD_RAID0 m
27-
MD_RAID1 m
28-
MD_RAID10 m
29-
MD_RAID456 m
30-
DM_VERITY m
31-
LOGO y
32-
FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER n
33-
FB_EFI n
34-
EFI_STUB y
35-
EFI y
36-
VIRTIO y
37-
VIRTIO_PCI y
38-
VIRTIO_BLK y
39-
DRM_VIRTIO_GPU y
40-
EXT4_FS y
41-
USBIP_CORE m
42-
USBIP_VHCI_HCD m
43-
USBIP_HOST m
44-
USBIP_VUDC m
45-
'';
46-
47-
src = fetchFromGitHub {
48-
owner = "nxp-imx";
49-
repo = "linux-imx";
50-
# tag: lf-6.12.20-2.0.0
51-
rev = "dfaf2136deb2af2e60b994421281ba42f1c087e0";
52-
sha256 = "sha256-ITrmj3a5YfXh/PSRTi+Rlto5uEBIAWFWtkTsO1ATXIo=";
53-
};
54-
}
55-
// (args.argsOverride or { })
56-
)
17+
# https://github.com/NixOS/nixpkgs/pull/366004
18+
# introduced a breaking change that if a module is declared but it is not being used it will faill.
19+
ignoreConfigErrors = true;
20+
}

0 commit comments

Comments
 (0)