This commit is contained in:
Peder Bergebakken Sundt 2023-07-07 01:47:40 +02:00
parent bb8db59534
commit 900e2d1b8f
4 changed files with 156 additions and 38 deletions

67
README.md Normal file
View File

@ -0,0 +1,67 @@
# Personal NixOS RISC-V 64 flake
Goals:
* I want to flash NixOS on to the M.2 NVMe SSD of a VisionFive2
* I want a flake with a neat cross story / UX
# Nix?
* https://nixos.org/
* https://determinate.systems/posts/determinate-nix-installer
# How?
The VisionFive2 shipped with a stinky firmware, which does not support booting from the SSD.
This capability was later added in [starfive uboot v3.0.4](https://github.com/starfive-tech/u-boot/commit/2139bf20ea67250c9dc426735fb9daf14e21bb9f).
As such we have to flash an SD card, use it to flash new firmware to the VisionFive2 SPI flash, then finally boot from the NVMe, all the while [fiddling with DIP switches](dip-pics.md).
## Building an image
Pick a host, then pick your poison:
| Hostname | Description |
| -------------- | ------------------- |
| `demo-r12a` | Board rev 1.2A |
| `demo-r12a-8g` | Board rev 1.2A, 8GB |
| `demo-r13b` | Board rev 1.3B |
| `demo-r13b-8g` | Board rev 1.3B, 8GB |
| `asgaut` | Asgaut |
| `gunder` | Gunder |
```shell
nix build .#asgaut-qemu # emulation go brr
nix build .#asgaut-nvme-image
nix build .#asgaut-sd-image
nix build .#asgaut-sd-installer
```
## Building without cross compilation
Either use a `riscv64-linux` host, or run the following command with `riscv64-linux` [binfmt emulation](https://search.nixos.org/options?query=boot.binfmt.emulatedSystems) configured:
```shell
nix build .#packages.riscv64-linux.demo-nvme-image
```
## Flashing an image
```shell
nix build .#something
lsblk # locate your SD card
umount /run/media/pbsds/9C33-6BBD # example mounts
sudo dd if=result/sd-image/*.img of=/dev/sdX bs=4M conf=fsync status=progress
sync
```
## Booting
Here are some sweet [dip pics](https://doc-en.rvspace.org/VisionFive2/Boot_UG/VisionFive2_SDK_QSG/boot_mode_settings.html).
After flashing the firmware you can boot using quad SPI. Use SDIO3.0 to boot using the u-boot SPL and FIT firmware on the sd card.
## Updating SPI firmware
```shell
sudo update-firmware
```

View File

@ -1,9 +1,9 @@
{ {
description = "My visionfive 2 setup"; description = "My visionfive 2 setup";
#inputs.nixpkgs.url = "github:NickCao/nixpkgs/riscv"; # https://github.com/NickCao/nixpkgs/tree/riscv #inputs.nixpkgs.url = "github:NickCao/nixpkgs/riscv"; # https://github.com/NickCao/nixpkgs/tree/riscv
#inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.05"; #inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.05";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
inputs.nixos-hardware.url = "github:nixos/nixos-hardware"; inputs.nixos-hardware.url = "github:nixos/nixos-hardware";
#inputs.nickcao-rv64.url = "github:NickCao/nixos-riscv"; # https://github.com/NickCao/nixos-riscv #inputs.nickcao-rv64.url = "github:NickCao/nixos-riscv"; # https://github.com/NickCao/nixos-riscv
@ -17,19 +17,17 @@
#nickcao-rv64, #nickcao-rv64,
...} @ inputs: ...} @ inputs:
let let
#systems = nixpkgs.lib.systems.flakeExposed; forSystems = systems: f: nixpkgs.lib.genAttrs systems (system: f rec {
systems = [ inherit system;
pkgs = nixpkgs.legacyPackages.${system};
lib = nixpkgs.legacyPackages.${system}.lib;
});
#forAllSystems = forSystems nixpkgs.lib.systems.flakeExposed;
forAllSystems = forSystems [
"x86_64-linux" "x86_64-linux"
"aarch64-linux" "aarch64-linux"
"riscv64-linux" "riscv64-linux"
]; ];
forAllSystems = f: nixpkgs.lib.genAttrs systems (system: f rec {
inherit system;
pkgs = nixpkgs.legacyPackages.${system};
inherit (pkgs) liv;
mkNixos = modules: nixpkgs.lib.nixosSystem { inherit system modules; };
vf2-firmware = pkgs.callPackage "${nixos-hardware}/starfive/visionfive/v2/firmware.nix" { };
});
#overlays = nixpkgs.lib.mapAttrsToList (name: val: val) self.overlays; #overlays = nixpkgs.lib.mapAttrsToList (name: val: val) self.overlays;
in { in {
@ -41,14 +39,16 @@
#}; #};
nixosModules = rec { nixosModules = rec {
# hardware # cross
rv64 = { config, lib, ... }: lib.mkIf (config.nixpkgs.system != "riscv64-linux") { rv64-maybe-cross = { config, lib, ... }: lib.mkIf (config.nixpkgs.system != "riscv64-linux") {
# (system != "riscv64-linux") => do cross # (system != "riscv64-linux") => do cross
nixpkgs.crossSystem.config = "riscv64-unknown-linux-gnu"; nixpkgs.crossSystem.config = "riscv64-unknown-linux-gnu";
nixpkgs.crossSystem.system = "riscv64-linux"; nixpkgs.crossSystem.system = "riscv64-linux";
}; };
# hardware
vf2-r12a = { lib, ...}: { vf2-r12a = { lib, ...}: {
imports = [ rv64 ]; imports = [ rv64-maybe-cross ];
hardware.deviceTree.name = "starfive/jh7110-starfive-visionfive-2-v1.2a.dtb"; hardware.deviceTree.name = "starfive/jh7110-starfive-visionfive-2-v1.2a.dtb";
}; };
vf2-r12a-8gb = { vf2-r12a-8gb = {
@ -59,7 +59,7 @@
}]; }];
}; };
vf2-r13b = { lib, ...}: { vf2-r13b = { lib, ...}: {
imports = [ rv64 ]; imports = [ rv64-maybe-cross ];
hardware.deviceTree.name = "starfive/jh7110-starfive-visionfive-2-v1.3b.dtb"; # default hardware.deviceTree.name = "starfive/jh7110-starfive-visionfive-2-v1.3b.dtb"; # default
}; };
vf2-r13b-8gb = { vf2-r13b-8gb = {
@ -119,15 +119,24 @@
# components # components
minimal = { lib, ...}: { minimal = { lib, ...}: with lib; {
boot.enableContainers = false; #boot.enableContainers = mkDefault false;
programs.command-not-found.enable = false; documentation.enable = mkDefault false;
documentation.man.enable = false; documentation.doc.enable = mkDefault false;
documentation.info.enable = false; documentation.info.enable = mkDefault false;
security.polkit.enable = lib.mkForce false; documentation.man.enable = mkDefault false;
security.audit.enable = false; documentation.nixos.enable = mkDefault false;
#environment.noXlibs = mkDefault true;
networking.firewall.enable = lib.mkDefault false; networking.firewall.enable = lib.mkDefault false;
programs.command-not-found.enable = false;
security.audit.enable = mkDefault false;
security.polkit.enable = lib.mkForce false;
services.logrotate.enable = mkDefault false;
services.udisks2.enable = mkDefault false;
#xdg.autostart.enable = mkDefault false;
#xdg.icons.enable = mkDefault false;
#xdg.mime.enable = mkDefault false;
#xdg.sounds.enable = mkDefault false;
}; };
dhcp = { dhcp = {
networking.interfaces.end0.useDHCP = true; networking.interfaces.end0.useDHCP = true;
@ -155,6 +164,7 @@
networking.hostName = "asgaut"; networking.hostName = "asgaut";
environment.systemPackages = with pkgs;[ neofetch htop fd ripgrep ]; environment.systemPackages = with pkgs;[ neofetch htop fd ripgrep ];
system.stateVersion = "23.05"; system.stateVersion = "23.05";
virtualisation.docker.enable = true;
}; };
gunder = { pkgs, ... }: { gunder = { pkgs, ... }: {
imports = [ minimal dhcp locale-no_nb ]; imports = [ minimal dhcp locale-no_nb ];
@ -164,19 +174,31 @@
}; };
}; };
packages = forAllSystems ({ system, mkNixos, ... }: let packages = forAllSystems ({ system, ... }: let
mkAll = hostname: modules: with self.nixosModules; { m = self.nixosModules;
"${hostname}-sd-installer" = (mkNixos (modules ++ [ vf2-r12a-8gb vf2-sd-installer ])).config.system.build.sdImage; mkNixos = modules: nixpkgs.lib.nixosSystem { inherit system modules; };
"${hostname}-sd-image" = (mkNixos (modules ++ [ vf2-r12a-8gb vf2-sd-image ])).config.system.build.sdImage; mkAll = hostname: modules: with m; {
"${hostname}-nvme-image" = (mkNixos (modules ++ [ vf2-r12a-8gb vf2-nvme-image ])).config.system.build.efiImage; "${hostname}-sd-installer" = (mkNixos (modules ++ [ vf2-sd-installer ])).config.system.build.sdImage;
"${hostname}-qemu" = (mkNixos (modules ++ [ rv64 vf2-qemu ])).config.system.build.vm; "${hostname}-sd-image" = (mkNixos (modules ++ [ vf2-sd-image ])).config.system.build.sdImage;
"${hostname}-nvme-image" = (mkNixos (modules ++ [ vf2-nvme-image ])).config.system.build.efiImage;
"${hostname}-qemu" = (mkNixos (modules ++ [ vf2-qemu ])).config.system.build.vm;
}; };
in (mkAll "demo" [ self.nixosModules.demo ]) in (mkAll "demo-r12a" [ m.demo m.vf2-r12a ])
// (mkAll "asgaut" [ self.nixosModules.asgaut ]) // (mkAll "demo-r12a-8gb" [ m.demo m.vf2-r12a-8gb ])
// (mkAll "gunder" [ self.nixosModules.gunder ])); // (mkAll "demo-r13b" [ m.demo m.vf2-r13b ])
// (mkAll "demo-r13b-8gb" [ m.demo m.vf2-r13b-8gb ])
// (mkAll "asgaut" [ m.asgaut m.vf2-r12a-8gb ])
// (mkAll "gunder" [ m.gunder m.vf2-r12a-8gb ])
// {
default = self.packages.${system}.demo-r12a-qemu;
});
nixosConfigurations.demo = nixpkgs.lib.nixosSystem { system = "x86_64-linux"; modules = with self.nixosModules; [ demo vf2-nvme ]; }; nixosConfigurations = let
nixosConfigurations.asgaut = nixpkgs.lib.nixosSystem { system = "x86_64-linux"; modules = with self.nixosModules; [ asgaut vf2-nvme ]; }; m = self.nixosModules;
nixosConfigurations.gunder = nixpkgs.lib.nixosSystem { system = "x86_64-linux"; modules = with self.nixosModules; [ gunder vf2-nvme ]; }; mkSystem = modules: nixpkgs.lib.nixosSystem { system = "x86_64-linux"; inherit modules; };
in {
asgaut = mkSystem (with m; [ asgaut vf2-r12a-8gb vf2-nvme ]);
gunder = mkSystem (with m; [ gunder vf2-r12a-8gb vf2-nvme ]);
};
}; };
} }

View File

@ -140,3 +140,34 @@ github:zhaofengli/nixos-riscv64/def02c67833bf551288c5cbd8ed6652b255d2fe8
│ └───visionfive: NixOS module │ └───visionfive: NixOS module
└───overlay: Nixpkgs overlay └───overlay: Nixpkgs overlay
``` ```
# BARF
https://www.waveshare.com/wiki/VisionFive2
https://doc-en.rvspace.org/Doc_Center/visionfive_2.html
https://github.com/starfive-tech/VisionFive2
https://github.com/starfive-tech/u-boot
https://github.com/u-boot/u-boot/blob/master/doc/board/starfive/visionfive2.rst
http://forum.rvspace.org/t/usb-serial-needed/897/4
https://reddit.com/r/RISCV/comments/zy30vl/comment/j3zhzkx/
https://forum.rvspace.org/t/nvme-boot-using-visionfive2-software-v2-11-5/2464/1
https://forum.rvspace.org/t/i-tried-the-v3-0-4-sdcard-img-from-sdcard-and-from-nvme/2965
https://u-boot.readthedocs.io/en/latest/board/starfive/visionfive2.html
* base: https://github.com/NixOS/nixos-hardware/blob/master/starfive/visionfive/v2/
* make-sd-image: https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/installer/sd-card/sd-image.nix
* make-disk-image:
* https://github.com/NixOS/nixpkgs/blob/master/nixos/lib/make-disk-image.nix
* https://github.com/nix-community/nixos-generators/blob/master/formats/raw.nix
* https://github.com/nix-community/nixos-generators/blob/master/formats/raw-efi.nix
* riscv efi: https://github.com/dramforever/nixos-riscv-efi/
* https://github.com/dramforever/nixos-riscv-efi
* gpu?: https://github.com/dramforever/nixos-visionfive
* qemu: https://github.com/NickCao/nixos-riscv
* Distroboot u-boot: https://github.com/NickCao/u-boot-starfive
* ?? https://github.com/zhaofengli/nixos-riscv64

View File

@ -3,8 +3,6 @@
{ {
boot.loader.generic-extlinux-compatible.enable = false; boot.loader.generic-extlinux-compatible.enable = false;
#boot.kernelPackages = pkgs.linuxPackages_latest;
boot.kernelParams = [ "console=ttyS0" "earlycon" ]; boot.kernelParams = [ "console=ttyS0" "earlycon" ];
boot.initrd.availableKernelModules = [ "pci_host_generic" "virtio_pci" "9p" "9pnet_virtio" ]; boot.initrd.availableKernelModules = [ "pci_host_generic" "virtio_pci" "9p" "9pnet_virtio" ];