From 95ed5766d6da53d0786d0d84e7a694d9da4c8b24 Mon Sep 17 00:00:00 2001
From: Stefan Junker <mail@stefanjunker.de>
Date: Tue, 3 Nov 2015 00:53:10 +0100
Subject: [PATCH 1/6] U-Boot: refactor to allow easier extendability

[dezgeg: minor stylistic tweaks, export buildUBoot for out-of-tree users]
---
 pkgs/misc/uboot/default.nix     | 129 +++++++++++++++++++-------------
 pkgs/top-level/all-packages.nix |  37 ++-------
 2 files changed, 85 insertions(+), 81 deletions(-)

diff --git a/pkgs/misc/uboot/default.nix b/pkgs/misc/uboot/default.nix
index 443841363bf3..981fbce0a3ab 100644
--- a/pkgs/misc/uboot/default.nix
+++ b/pkgs/misc/uboot/default.nix
@@ -1,62 +1,89 @@
-{ stdenv, fetchurl, bc, dtc
-, toolsOnly ? false
-, defconfig ? "allnoconfig"
-, targetPlatforms
-, filesToInstall
-}:
+{ stdenv, fetchurl, bc, dtc }:
 
 let
-  platform = stdenv.platform;
-  crossPlatform = stdenv.cross.platform;
-  makeTarget = if toolsOnly then "tools NO_SDL=1" else "all";
-  installDir = if toolsOnly then "$out/bin" else "$out";
-  buildFun = kernelArch:
-    ''
-      if test -z "$crossConfig"; then
-          make ${makeTarget}
-      else
-          make ${makeTarget} ARCH=${kernelArch} CROSS_COMPILE=$crossConfig-
-      fi
+  buildUBoot = { targetPlatforms
+            , filesToInstall
+            , installDir ? "$out"
+            , defconfig
+            , extraMeta ? {}
+            , ... } @ args:
+           stdenv.mkDerivation (rec {
+
+    name = "uboot-${defconfig}-${version}";
+    version = "2015.10";
+
+    nativeBuildInputs = [ bc dtc ];
+
+    src = fetchurl {
+      url = "ftp://ftp.denx.de/pub/u-boot/u-boot-${version}.tar.bz2";
+      sha256 = "0m8r08izci0lzzjn5c5g5manp2rc7yc5swww0lxr7bamjigqvimx";
+    };
+
+    configurePhase = ''
+      make ${defconfig}
     '';
-in
 
-stdenv.mkDerivation rec {
-  name = "uboot-${defconfig}-${version}";
-  version = "2015.10";
+    installPhase = ''
+      runHook preInstall
 
-  src = fetchurl {
-    url = "ftp://ftp.denx.de/pub/u-boot/u-boot-${version}.tar.bz2";
-    sha256 = "0m8r08izci0lzzjn5c5g5manp2rc7yc5swww0lxr7bamjigqvimx";
+      mkdir -p ${installDir}
+      cp ${stdenv.lib.concatStringsSep " " filesToInstall} ${installDir}
+
+      runHook postInstall
+    '';
+
+    dontStrip = true;
+
+    crossAttrs = {
+      makeFlags = [
+        "ARCH=${stdenv.cross.platform.kernelArch}"
+        "CROSS_COMPILE=${stdenv.cross.config}-"
+      ];
+    };
+
+    meta = with stdenv.lib; {
+      homepage = "http://www.denx.de/wiki/U-Boot/";
+      description = "Boot loader for embedded systems";
+      license = licenses.gpl2;
+      maintainers = [ maintainers.dezgeg ];
+      platforms = targetPlatforms;
+    } // extraMeta;
+  } // args);
+
+in rec {
+  inherit buildUBoot;
+
+  ubootTools = buildUBoot rec {
+    installDir = "$out/bin";
+    buildFlags = "tools NO_SDL=1";
+    dontStrip = false;
+    targetPlatforms = stdenv.lib.platforms.linux;
+    filesToInstall = ["tools/dumpimage" "tools/mkenvimage" "tools/mkimage"];
   };
 
-  patches = [ ./vexpress-Use-config_distro_bootcmd.patch ];
-
-  nativeBuildInputs = [ bc dtc ];
-
-  configurePhase = ''
-    make ${defconfig}
-  '';
-
-  buildPhase = assert (platform ? kernelArch);
-    buildFun platform.kernelArch;
-
-  installPhase = ''
-    mkdir -p ${installDir}
-    cp ${stdenv.lib.concatStringsSep " " filesToInstall} ${installDir}
-  '';
-
-  dontStrip = !toolsOnly;
-
-  crossAttrs = {
-    buildPhase = assert (crossPlatform ? kernelArch);
-      buildFun crossPlatform.kernelArch;
+  ubootJetsonTK1 = buildUBoot rec {
+    defconfig = "jetson-tk1_defconfig";
+    targetPlatforms = ["armv7l-linux"];
+    filesToInstall = ["u-boot" "u-boot.dtb" "u-boot-dtb-tegra.bin" "u-boot-nodtb-tegra.bin"];
   };
 
-  meta = with stdenv.lib; {
-    homepage = "http://www.denx.de/wiki/U-Boot/";
-    description = "Boot loader for embedded systems";
-    license = licenses.gpl2;
-    maintainers = [ maintainers.dezgeg ];
-    platforms = targetPlatforms;
+  ubootPcduino3Nano = buildUBoot rec {
+    defconfig = "Linksprite_pcDuino3_Nano_defconfig";
+    targetPlatforms = ["armv7l-linux"];
+    filesToInstall = ["u-boot-sunxi-with-spl.bin"];
+  };
+
+  ubootRaspberryPi = buildUBoot rec {
+    defconfig = "rpi_defconfig";
+    targetPlatforms = ["armv6l-linux"];
+    filesToInstall = ["u-boot.bin"];
+  };
+
+  # Intended only for QEMU's vexpress-a9 emulation target!
+  ubootVersatileExpressCA9 = buildUBoot rec {
+    defconfig = "vexpress_ca9x4_defconfig";
+    targetPlatforms = ["armv7l-linux"];
+    filesToInstall = ["u-boot"];
+    patches = [ ./vexpress-Use-config_distro_bootcmd.patch ];
   };
 }
diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index 1c283e4acb10..bac0f0dd8b6f 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -10574,36 +10574,13 @@ let
   ubootChooser = name: ubootTools;
 
   # Upstream U-Boots:
-  ubootTools = callPackage ../misc/uboot {
-    toolsOnly = true;
-    targetPlatforms = lib.platforms.linux;
-    filesToInstall = ["tools/dumpimage" "tools/mkenvimage" "tools/mkimage"];
-  };
-
-  ubootJetsonTK1 = callPackage ../misc/uboot {
-    defconfig = "jetson-tk1_defconfig";
-    targetPlatforms = ["armv7l-linux"];
-    filesToInstall = ["u-boot" "u-boot.dtb" "u-boot-dtb-tegra.bin" "u-boot-nodtb-tegra.bin"];
-  };
-
-  ubootPcduino3Nano = callPackage ../misc/uboot {
-    defconfig = "Linksprite_pcDuino3_Nano_defconfig";
-    targetPlatforms = ["armv7l-linux"];
-    filesToInstall = ["u-boot-sunxi-with-spl.bin"];
-  };
-
-  ubootRaspberryPi = callPackage ../misc/uboot {
-    defconfig = "rpi_defconfig";
-    targetPlatforms = ["armv6l-linux"];
-    filesToInstall = ["u-boot.bin"];
-  };
-
-  # Intended only for QEMU's vexpress-a9 emulation target!
-  ubootVersatileExpressCA9 = callPackage ../misc/uboot {
-    defconfig = "vexpress_ca9x4_defconfig";
-    targetPlatforms = ["armv7l-linux"];
-    filesToInstall = ["u-boot"];
-  };
+  inherit (callPackage ../misc/uboot {})
+    buildUBoot
+    ubootJetsonTK1
+    ubootPcduino3Nano
+    ubootRaspberryPi
+    ubootVersatileExpressCA9
+    ;
 
   # Non-upstream U-Boots:
   ubootSheevaplug = callPackage ../misc/uboot/sheevaplug.nix { };

From f80a5634612807befc5c6ea012c7063bb7efed06 Mon Sep 17 00:00:00 2001
From: Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi>
Date: Fri, 25 Dec 2015 20:31:43 +0200
Subject: [PATCH 2/6] U-Boot: Build for the Banana Pi board

I don't own this board, but someone on IRC reported this as working (and
anyway it's practically identical to ubootPcduino3Nano since they're both
Allwinner boards)
---
 pkgs/misc/uboot/default.nix     | 6 ++++++
 pkgs/top-level/all-packages.nix | 1 +
 2 files changed, 7 insertions(+)

diff --git a/pkgs/misc/uboot/default.nix b/pkgs/misc/uboot/default.nix
index 981fbce0a3ab..b592005e7948 100644
--- a/pkgs/misc/uboot/default.nix
+++ b/pkgs/misc/uboot/default.nix
@@ -61,6 +61,12 @@ in rec {
     filesToInstall = ["tools/dumpimage" "tools/mkenvimage" "tools/mkimage"];
   };
 
+  ubootBananaPi = buildUBoot rec {
+    defconfig = "Bananapi_defconfig";
+    targetPlatforms = ["armv7l-linux"];
+    filesToInstall = ["u-boot-sunxi-with-spl.bin"];
+  };
+
   ubootJetsonTK1 = buildUBoot rec {
     defconfig = "jetson-tk1_defconfig";
     targetPlatforms = ["armv7l-linux"];
diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index bac0f0dd8b6f..2472e129a9a5 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -10576,6 +10576,7 @@ let
   # Upstream U-Boots:
   inherit (callPackage ../misc/uboot {})
     buildUBoot
+    ubootBananaPi
     ubootJetsonTK1
     ubootPcduino3Nano
     ubootRaspberryPi

From 5d3642eba9e8ea4bebf25aa19b2a7320a3666974 Mon Sep 17 00:00:00 2001
From: Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi>
Date: Sat, 26 Dec 2015 07:24:17 +0200
Subject: [PATCH 3/6] U-Boot: 2015.10 -> 2016.01

---
 pkgs/misc/uboot/default.nix | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/pkgs/misc/uboot/default.nix b/pkgs/misc/uboot/default.nix
index b592005e7948..50b676aff1ea 100644
--- a/pkgs/misc/uboot/default.nix
+++ b/pkgs/misc/uboot/default.nix
@@ -10,13 +10,13 @@ let
            stdenv.mkDerivation (rec {
 
     name = "uboot-${defconfig}-${version}";
-    version = "2015.10";
+    version = "2016.01";
 
     nativeBuildInputs = [ bc dtc ];
 
     src = fetchurl {
       url = "ftp://ftp.denx.de/pub/u-boot/u-boot-${version}.tar.bz2";
-      sha256 = "0m8r08izci0lzzjn5c5g5manp2rc7yc5swww0lxr7bamjigqvimx";
+      sha256 = "1md5jpq5n9jh08s7sdkjrvg2q7kpzwa7yrpgl9581ncrjfx2yyg5";
     };
 
     configurePhase = ''

From e9b80f39721965ecbcd384ef6faeb3b661d3a3a7 Mon Sep 17 00:00:00 2001
From: Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi>
Date: Fri, 25 Dec 2015 20:55:22 +0200
Subject: [PATCH 4/6] U-Boot: Build for the Wandboard board

From v2016.01 onwards, the Wandboards boot using the distro bootcmd
stuff as well.

Tested on a Wandboard Quad.
---
 pkgs/misc/uboot/default.nix     | 6 ++++++
 pkgs/top-level/all-packages.nix | 1 +
 2 files changed, 7 insertions(+)

diff --git a/pkgs/misc/uboot/default.nix b/pkgs/misc/uboot/default.nix
index 50b676aff1ea..4d1d3d73990e 100644
--- a/pkgs/misc/uboot/default.nix
+++ b/pkgs/misc/uboot/default.nix
@@ -92,4 +92,10 @@ in rec {
     filesToInstall = ["u-boot"];
     patches = [ ./vexpress-Use-config_distro_bootcmd.patch ];
   };
+
+  ubootWandboard = buildUBoot rec {
+    defconfig = "wandboard_defconfig";
+    targetPlatforms = ["armv7l-linux"];
+    filesToInstall = ["u-boot.img" "SPL"];
+  };
 }
diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index 2472e129a9a5..8fc516621d7a 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -10581,6 +10581,7 @@ let
     ubootPcduino3Nano
     ubootRaspberryPi
     ubootVersatileExpressCA9
+    ubootWandboard
     ;
 
   # Non-upstream U-Boots:

From b6621196e07429ed416ff426cc1e398cfc70a5d6 Mon Sep 17 00:00:00 2001
From: Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi>
Date: Fri, 25 Dec 2015 20:35:59 +0200
Subject: [PATCH 5/6] sd-image-armv7l-multiplatform.nix: Add ttymxc0 to the
 list of consoles

Needed for the RS-232 port on Wandboard Quad (and presumably other boards
using the i.MX6 SoC).
---
 .../modules/installer/cd-dvd/sd-image-armv7l-multiplatform.nix  | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/nixos/modules/installer/cd-dvd/sd-image-armv7l-multiplatform.nix b/nixos/modules/installer/cd-dvd/sd-image-armv7l-multiplatform.nix
index 15e22fb50d48..957a8ff9ce6d 100644
--- a/nixos/modules/installer/cd-dvd/sd-image-armv7l-multiplatform.nix
+++ b/nixos/modules/installer/cd-dvd/sd-image-armv7l-multiplatform.nix
@@ -23,7 +23,7 @@ in
   boot.loader.generic-extlinux-compatible.enable = true;
 
   boot.kernelPackages = pkgs.linuxPackages_latest;
-  boot.kernelParams = ["console=ttyS0,115200n8" "console=ttyAMA0,115200n8" "console=tty0"];
+  boot.kernelParams = ["console=ttyS0,115200n8" "console=ttymxc0,115200n8" "console=ttyAMA0,115200n8" "console=tty0"];
 
   # FIXME: this probably should be in installation-device.nix
   users.extraUsers.root.initialHashedPassword = "";

From 4cf9bf9eb00e85ab1b2d117b767aff81cb1c39b2 Mon Sep 17 00:00:00 2001
From: Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi>
Date: Sat, 26 Dec 2015 07:28:19 +0200
Subject: [PATCH 6/6] sd-image.nix: Move the /boot partition up to 8M

Reportedly some ARM boards need some boot code at the start of a SD card
that could be larger than a megabyte. Change it to 8M, and while at it
reduce the /boot size such that the root partition should now start on a
128M boundary (the flash on SD cards really don't like non-aligned
writes these days).
---
 nixos/modules/installer/cd-dvd/sd-image.nix | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/nixos/modules/installer/cd-dvd/sd-image.nix b/nixos/modules/installer/cd-dvd/sd-image.nix
index 12b4f3045614..9eba542d8c91 100644
--- a/nixos/modules/installer/cd-dvd/sd-image.nix
+++ b/nixos/modules/installer/cd-dvd/sd-image.nix
@@ -30,7 +30,7 @@ in
 
     bootSize = mkOption {
       type = types.int;
-      default = 128;
+      default = 120;
       description = ''
         Size of the /boot partition, in megabytes.
       '';
@@ -66,10 +66,10 @@ in
       buildInputs = with pkgs; [ dosfstools e2fsprogs mtools libfaketime utillinux ];
 
       buildCommand = ''
-        # Create the image file sized to fit /boot and /, plus 4M of slack
+        # Create the image file sized to fit /boot and /, plus 20M of slack
         rootSizeBlocks=$(du -B 512 --apparent-size ${rootfsImage} | awk '{ print $1 }')
         bootSizeBlocks=$((${toString config.sdImage.bootSize} * 1024 * 1024 / 512))
-        imageSize=$((rootSizeBlocks * 512 + bootSizeBlocks * 512 + 4096 * 1024))
+        imageSize=$((rootSizeBlocks * 512 + bootSizeBlocks * 512 + 20 * 1024 * 1024))
         truncate -s $imageSize $out
 
         # type=b is 'W95 FAT32', type=83 is 'Linux'.
@@ -77,8 +77,8 @@ in
             label: dos
             label-id: 0x2178694e
 
-            start=1M, size=$bootSizeBlocks, type=b, bootable
-            type=83
+            start=8M, size=$bootSizeBlocks, type=b, bootable
+            start=${toString (8 + config.sdImage.bootSize)}M, type=83
         EOF
 
         # Copy the rootfs into the SD image