Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Research booting NanoPi R6C #3

Closed
jakubgs opened this issue Jun 3, 2023 · 148 comments
Closed

Research booting NanoPi R6C #3

jakubgs opened this issue Jun 3, 2023 · 148 comments
Assignees

Comments

@jakubgs
Copy link
Owner

jakubgs commented Jun 3, 2023

Research into running NixOS on a NanoPi R6C device.

image

Links:

@jakubgs jakubgs self-assigned this Jun 3, 2023
@jakubgs
Copy link
Owner Author

jakubgs commented Jun 3, 2023

I installed Ubuntu using rk3588-eflasher-ubuntu-jammy-minimal-5.10-arm64-20230527.img image. Here's the storage:

pi@NanoPi-R6C:~$ sudo fdisk -l /dev/mmcblk2 /dev/nvme0n1 
Disk /dev/mmcblk2: 28.91 GiB, 31037849600 bytes, 60620800 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 73987B6B-4974-4C94-A3E8-58AB2EB7A946

Device           Start      End  Sectors  Size Type
/dev/mmcblk2p1   16384    24575     8192    4M unknown
/dev/mmcblk2p2   24576    32767     8192    4M unknown
/dev/mmcblk2p3   32768    40959     8192    4M unknown
/dev/mmcblk2p4   40960    73727    32768   16M unknown
/dev/mmcblk2p5   73728   155647    81920   40M unknown
/dev/mmcblk2p6  155648   221183    65536   32M unknown
/dev/mmcblk2p7  221184   286719    65536   32M unknown
/dev/mmcblk2p8  286720  5267455  4980736  2.4G unknown
/dev/mmcblk2p9 5267456 60620766 55353311 26.4G unknown


Disk /dev/nvme0n1: 476.94 GiB, 512110190592 bytes, 1000215216 sectors
Disk model: SAMSUNG MZVLB512HAJQ-000L7              
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

@jakubgs
Copy link
Owner Author

jakubgs commented Jun 3, 2023

aWe can get more insight into the partitions on the eMMC by using parted:

pi@NanoPi-R6C:~$ sudo parted /dev/mmcblk2 print
Model: MMC A3A551 (sd/mmc)
Disk /dev/mmcblk2: 31.0GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name      Flags
 1      8389kB  12.6MB  4194kB               uboot
 2      12.6MB  16.8MB  4194kB               misc
 3      16.8MB  21.0MB  4194kB               dtbo
 4      21.0MB  37.7MB  16.8MB               resource
 5      37.7MB  79.7MB  41.9MB               kernel
 6      79.7MB  113MB   33.6MB               boot
 7      113MB   147MB   33.6MB               recovery
 8      147MB   2697MB  2550MB  ext4         rootfs
 9      2697MB  31.0GB  28.3GB  ext4         userdata

https://wiki.friendlyelec.com/wiki/index.php/NanoPi_R6C#Install_to_target_board

@jakubgs
Copy link
Owner Author

jakubgs commented Jun 3, 2023

Apparently the kernel needs to be written to the 5th partition based on instructions:

dd if=resource.img of=/dev/mmcblk2p4 bs=1M
dd if=kernel.img of=/dev/mmcblk2p5 bs=1M
dd if=uboot.img of=/dev/mmcblk2p1 bs=1M

And the Ubuntu image actually has no kernel or configuration in /boot:

pi@NanoPi-R6C:~$ ls -l /boot
total 0

Which is different from how NanoPC-T4 works, since there you actually have an extlinux config file in /boot and kernels.

@jakubgs
Copy link
Owner Author

jakubgs commented Jun 3, 2023

I put in it a Samsung PM981 512 GB M.2 2280 (MZVLB512HAJQ) NVMe and here's some very basic benchmarks:

 > sudo hdparm -tT /dev/nvme0n1
/dev/nvme0n1:
 Timing cached reads:   8034 MB in  2.00 seconds = 4020.01 MB/sec
 Timing buffered disk reads: 1030 MB in  3.00 seconds = 343.10 MB/sec

/dev/nvme0n1:
 Timing cached reads:   8342 MB in  2.00 seconds = 4174.52 MB/sec
 Timing buffered disk reads: 1032 MB in  3.00 seconds = 343.92 MB/sec

/dev/nvme0n1:
 Timing cached reads:   8054 MB in  2.00 seconds = 4029.94 MB/sec
 Timing buffered disk reads: 1032 MB in  3.00 seconds = 343.72 MB/sec
 > sudo dd if=/dev/zero of=/mnt/test.img bs=1M count=2048
2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB, 2.0 GiB) copied, 4.06961 s, 528 MB/s

2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB, 2.0 GiB) copied, 4.77926 s, 449 MB/s

2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB, 2.0 GiB) copied, 4.48503 s, 479 MB/s

@jakubgs
Copy link
Owner Author

jakubgs commented Jun 3, 2023

Some potentially interesting resources:

@jakubgs
Copy link
Owner Author

jakubgs commented Jun 3, 2023

I have tried booting a generic SD card image of Nixos, since it works on NanoPC-T4, but it does not work:
https://hydra.nixos.org/job/nixos/trunk-combined/nixos.sd_image.aarch64-linux

@jakubgs
Copy link
Owner Author

jakubgs commented Jun 3, 2023

I built the image:

 > ls -l $(nix-build -A radxa-rock5b.config.build.firmwareSPI)/binaries
trace: Using default Nixpkgs revision '1905f5f2e55e0db0bb6244cfe62cb6c0dbda391d'...
trace: ***********************************
trace: * Evaluating device: radxa-rock5b *
trace: ***********************************
trace: Building with crossSystem?: aarch64-linux != x86_64-linux → we are.
trace:     crossSystem: config: aarch64-unknown-linux-gnu
trace: Building with crossSystem?: aarch64-linux != x86_64-linux → we are.
trace:     crossSystem: config: aarch64-unknown-linux-gnu
total 1796
-r--r--r-- 1 root root 1866752 Jan  1  1970 Tow-Boot.spi.bi

But it's not clear how I'm supposed to install it. But these instructions for ROCK 5B just use dd:

sudo dd if=spi-image.img of=/dev/mtdblock0

https://wiki.radxa.com/Rock5/install/spi

@jakubgs
Copy link
Owner Author

jakubgs commented Jun 3, 2023

I tried installing it in the U-Boot partition to see if I can maybe get the generic SD image to boot with it:

pi@NanoPi-R6C:~$ sudo dd if=/dev/zero of=/dev/mmcblk2p1
dd: writing to '/dev/mmcblk2p1': No space left on device
8193+0 records in
8192+0 records out
4194304 bytes (4.2 MB, 4.0 MiB) copied, 0.410807 s, 10.2 MB/s

pi@NanoPi-R6C:~$ sudo dd if=Tow-Boot.spi.bin of=/dev/mmcblk2p1
3646+0 records in
3646+0 records out
1866752 bytes (1.9 MB, 1.8 MiB) copied, 0.153187 s, 12.2 MB/s

But of course that did not work. And now nothing boots. That was pure wishful thinking.

@jakubgs
Copy link
Owner Author

jakubgs commented Jun 3, 2023

Using picocom with baud 1500000 via a USB debug port I was able to get the UART to work:

 > sudo minicom -b 1500000 -D /dev/ttyUSB0
...
DDR V1.10 75d050770f typ 23/02/28-20:47:10
LPDDR4X, 2112MHz
channel[0] BW=16 Col=10 Bk=8 CS0 Row=16 CS1 Row=16 CS=2 Die BW=16 Size=2048MB
channel[1] BW=16 Col=10 Bk=8 CS0 Row=16 CS1 Row=16 CS=2 Die BW=16 Size=2048MB
channel[2] BW=16 Col=10 Bk=8 CS0 Row=16 CS1 Row=16 CS=2 Die BW=16 Size=2048MB
channel[3] BW=16 Col=10 Bk=8 CS0 Row=16 CS1 Row=16 CS=2 Die BW=16 Size=2048MB
Manufacturer ID:0x1
CH0 RX Vref:26.3%, TX Vref:21.8%,21.8%
CH1 RX Vref:26.3%, TX Vref:22.8%,21.8%
CH2 RX Vref:27.5%, TX Vref:19.8%,18.8%
CH3 RX Vref:26.7%, TX Vref:20.8%,21.8%
change to F1: 528MHz
change to F2: 1068MHz
change to F3: 1560MHz
change to F0: 2112MHz
out
U-Boot SPL board init
U-Boot SPL 2017.09-gd91b448214-221107 #root (Apr 10 2023 - 14:05:21)
unknown raw ID 0 0 0
unrecognized JEDEC id bytes: 00, 00, 00
Trying to boot from MMC2
part_get_info_efi: *** ERROR: Invalid GPT ***
part_get_info_efi: ***        Using Backup GPT ***
Trying fit image at 0x4000 sector
Not fit magic
Trying fit image at 0x5000 sector
Not fit magic
Trying fit image at 0x4000 sector
Not fit magic
Trying fit image at 0x5000 sector
Not fit magic
Trying to boot from MMC1
Trying fit image at 0x4000 sector
Not fit magic
Trying fit image at 0x5000 sector
Not fit magic
Trying fit image at 0x4000 sector
Not fit magic
Trying fit image at 0x5000 sector
Not fit magic
SPL: failed to boot from all boot devices
### ERROR ### Please RESET the board ###
# Reset the board to bootrom #

As we can see it fails to boot: SPL: failed to boot from all boot devices
I wonder how this looked when I had a working Ubuntu image. Lets see.

@jakubgs
Copy link
Owner Author

jakubgs commented Jun 3, 2023

There are instructions on how to build U-Boot from FriendlyELEC source:

git clone https://github.com/friendlyarm/uboot-rockchip --depth 1 -b nanopi6-v2017.09
UBOOT_SRC=$PWD/uboot-rockchip ./build-uboot.sh friendlycore-focal-arm64

https://wiki.friendlyelec.com/wiki/index.php/NanoPi_R6C#Compile_the_uboot

@jakubgs
Copy link
Owner Author

jakubgs commented Jun 3, 2023

The Ubuntu flash image boots without issues:

U-Boot SPL board init
U-Boot SPL 2017.09-gd91b448214-221107 #root (Apr 10 2023 - 14:05:21)
unknown raw ID 0 0 0
unrecognized JEDEC id bytes: 00, 00, 00
Trying to boot from MMC2
Trying fit image at 0x4000 sector
## Verified-boot: 0
## Checking atf-1 0x00040000 ... sha256(ac799e4aa0...) + OK
## Checking uboot 0x00200000 ... sha256(ecc2ca052f...) + OK
## Checking fdt 0x0032ce40 ... sha256(e936f08b25...) + OK
## Checking atf-2 0xff100000 ... sha256(c0f2f7769f...) + OK
## Checking atf-3 0x000f0000 ... sha256(338cd7cb83...) + OK
## Checking optee 0x08400000 ... sha256(fde0860845...) + OK
Jumping to U-Boot(0x00200000) via ARM Trusted Firmware(0x00040000)

boot.log

@jakubgs
Copy link
Owner Author

jakubgs commented Jun 3, 2023

At least we can see the NVMe shows up properly at boot here, so maybe NixOS won't have same issue as on NanoPC-T4.

[    4.475674] nvme nvme0: pci function 0004:41:00.0
[    4.475754] nvme 0004:41:00.0: enabling device (0000 -> 0002)
[    4.478778] nvme nvme0: Shutdown timeout set to 8 seconds
[    4.497511] nvme nvme0: 8/0/0 default/read/poll queues
[    4.524958] rk-pcie fe180000.pcie: PCIe Link up, LTSSM is 0x130011
[    4.525205] rk-pcie fe180000.pcie: PCI host bridge to bus 0003:30

boot.log

@jakubgs
Copy link
Owner Author

jakubgs commented Jun 3, 2023

If we take a look at an SD card image for Ubuntu we can see the same layout as on the eMMC:

rk3588-eflasher-ubuntu-jammy-minimal-5.10-arm64-20230527.img

 > sudo fdisk -l /dev/loop3 
Disk /dev/loop3: 7.26 GiB, 7799999488 bytes, 15234374 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 73987B6B-4974-4C94-A3E8-58AB2EB7A946

Device         Start      End  Sectors  Size Type
/dev/loop3p1 3563520 15228927 11665408  5.6G unknown
/dev/loop3p2   16384    24575     8192    4M unknown
/dev/loop3p3   24576    32767     8192    4M unknown
/dev/loop3p4   32768    40959     8192    4M unknown
/dev/loop3p5   40960    73727    32768   16M unknown
/dev/loop3p6   73728   155647    81920   40M unknown
/dev/loop3p7  155648   221183    65536   32M unknown
/dev/loop3p8  221184   286719    65536   32M unknown
/dev/loop3p9  286720  3563519  3276800  1.6G unknown

Partition table entries are not in disk order.

While the NixOS image uses a FAT32 partition for the boot files, which I guess is why it doesn't work:

 > sudo fdisk -l /dev/loop2 
Disk /dev/loop2: 2.54 GiB, 2723512320 bytes, 5319360 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x2178694e

Device       Boot Start     End Sectors  Size Id Type
/dev/loop2p1      16384   77823   61440   30M  b W95 FAT32
/dev/loop2p2 *    77824 5319359 5241536  2.5G 83 Linux

@jakubgs
Copy link
Owner Author

jakubgs commented Jun 3, 2023

Here are official RockChip docs about the boot process: https://opensource.rock-chips.com/wiki_Boot_option

@jakubgs
Copy link
Owner Author

jakubgs commented Jun 3, 2023

The docs state that:

By default, the system will be booted from the TF card first, but this is not the case under all conditions. This section will explain all situations in detail;

Refer to rockchip official document [1], there are two types of loader program:

  1. U-Boot TPL/SPL (i.e. upsream U-Boot, also called mainline U-Boot)
  2. Rockchip MiniLoader

Things to note:

  1. FriendlyELEC's image uses Rockchip MiniLoader
  2. The third-party image usually uses U-Boot TPL/SPL

https://wiki.friendlyelec.com/wiki/index.php/NanoPi_R6C#The_Boot_order_between_eMMC_and_SD_card

@jakubgs
Copy link
Owner Author

jakubgs commented Jun 3, 2023

There are instructions for building an U-Boot using FriendlyELEC scripts:

git clone https://github.com/friendlyarm/rkbin --single-branch --depth 1 -b nanopi6
git clone https://github.com/friendlyarm/uboot-rockchip --single-branch --depth 1 -b nanopi6-v2017.09
export PATH=/opt/FriendlyARM/toolchain/11.3-aarch64/bin/:$PATH
cd uboot-rockchip/
./make.sh nanopi6

https://wiki.friendlyelec.com/wiki/index.php/NanoPi_R6C#Build_u-boot_v2017.09

But I'm still don't know how that could be modified to boot NixOS. I might have to read up on U-Boot itself.

@jakubgs
Copy link
Owner Author

jakubgs commented Jun 3, 2023

I found some interesting info in this issue. For example this comment describes how to load kernel from Ext4 partition:

setenv m "1080p60hz"
setenv m_bpp "32"
setenv condev "console=ttyS0,115200n8 console=tty0"   # on both
setenv bootargs "root=/dev/mmcblk0p2 rootwait rw ${condev} no_console_suspend hdmimode=${m} m_bpp=${m_bpp} vout=${vout} fsck.repair=yes"
setenv loadaddr "0x11000000"
setenv dtb_loadaddr "0x1000000"
setenv initrd_loadaddr "0x13000000"
ext4load mmc 0:2 ${initrd_loadaddr} /boot/uInitrd
ext4load mmc 0:2 ${loadaddr} /boot/Image
ext4load mmc 0:2 ${dtb_loadaddr} /boot/meson64_odroidc2.dtb
booti ${loadaddr} ${initrd_loadaddr} ${dtb_loadaddr}

@jakubgs
Copy link
Owner Author

jakubgs commented Jun 3, 2023

And indeed, the NanoPi branch in uboot-rockchip repo has a README about handling Ext4 filesystems:
https://github.com/friendlyarm/uboot-rockchip/blob/nanopi6-v2017.09/doc/README.ext4

@jakubgs
Copy link
Owner Author

jakubgs commented Jun 3, 2023

I notice that ext4 support is not enabled in the NanoPi 6 build config:

~/soft/uboot-rockchip nanopi6-v2017.09
 > grep ext4 configs/nanopi*          

~/soft/uboot-rockchip nanopi6-v2017.09
 > grep ext4 configs/odroid_defconfig                       
CONFIG_CMD_EXT4=y
CONFIG_CMD_EXT4_WRITE=y

@jakubgs
Copy link
Owner Author

jakubgs commented Jun 3, 2023

In theory this line suggests I could provide U-Boot with custom commands at startup via UART console:

Hit key to stop autoboot('CTRL+C'):  0 

But the real question is how can I modify the config to read extlinux.conf file from an Ext4 /boot partition.

@jakubgs
Copy link
Owner Author

jakubgs commented Jun 4, 2023

And it doesn't seem like it supports FAT filesystem either:

~/soft/uboot-rockchip nanopi6-v2017.09
 > grep fat configs/nanopi*          

~/soft/uboot-rockchip nanopi6-v2017.09
 > grep fat configs/odroid_defconfig
CONFIG_CMD_FAT=y

But there appears to be several FAT filestem flags:

 > grep 'CONFIG_.*FAT' fs/fs.c cmd/pxe.c 
fs/fs.c:#ifdef CONFIG_FS_FAT
fs/fs.c:#ifdef CONFIG_FAT_WRITE
cmd/pxe.c:#ifdef CONFIG_CMD_FAT

For example some usage can be seen in fs/fs.c:
https://github.com/friendlyarm/uboot-rockchip/blob/93ceeb4da7efbabefe9b88b57093f90ea731d502/fs/fs.c#L147-L163

Not yet sure how the CMD ones are different.

@jakubgs
Copy link
Owner Author

jakubgs commented Jun 4, 2023

According to the README:

/cmd			U-Boot commands functions

So it seems to me that just means you can enable FAT filesystem support with CONFIG_FS_FAT, but if you want interactive commands at boot time like fatload you need CONFIG_CMD_FAT.

@jakubgs
Copy link
Owner Author

jakubgs commented Jun 4, 2023

So I think a pretty good assumption would be that current U-Boot can't load the config from generic NixOS sd card because it lacks CONFIG_FS_FAT enabled. So lets try that first.

@jakubgs
Copy link
Owner Author

jakubgs commented Jun 4, 2023

Building U-Boot from Rockchip was failing due to lack of cross platform GCC:

 > ./make.sh nanopi6
## make nanopi6_defconfig -j24
#
# configuration written to .config
#
which: no aarch64-linux-gnu-gcc in (/nix/store/fwc0m6fsccir8zznkl7m8iznv5wsjznw-bash-interactive-5.1-p16/bin:/nix/store/n845kl3915ri14zlkqq7wf3n3xabmgal-patchelf-0.14.5/bin:/nix/store/ykcrnkiicqg1pwls9kgnmf0hd9qjqp4x-gcc-wrapper-11.3.0/bin:/nix/store/65v2c245h5qa9mpc7dxhqkfjinl6phx0-gcc-11.3.0/bin:/nix/store/rvgp96bwfmgz4b163flszfaygmhx8wl4-glibc-2.34-210-bin/bin:/nix/store/qarssrazji0q9xp80xg8shsm2crckfr0-coreutils-9.0/bin:/nix/store/n5ypzvl7dijcg24isyngvw8fx0ri6hff-binutils-wrapper-2.38/bin:/nix/store/0q9hm42fapihzj1d64nxqmbml7fpb2d6-binutils-2.38/bin:/nix/store/9l2pdqd2cs9hqyk53w543lpnp0vqaysd-ncurses-6.3-p20220507-dev/bin:/nix/store/sb1wx6xz9yj05d13w5lpdljspllarbim-ncurses-6.3-p20220507/bin:/nix/store/qarssrazji0q9xp80xg8shsm2crckfr0-coreutils-9.0/bin:/nix/store/j25abvpcbappy74w23l8lfcz7gkrsjhy-findutils-4.9.0/bin:/nix/store/2kz1pihzg1jfif46mdm917xmj6r9xyz6-diffutils-3.8/bin:/nix/store/yyg26p5j2mrjwpkbk1djh4nxlsm2p4rw-gnused-4.8/bin:/nix/store/p7pqs5c4zfc4y977p626zch11msmmpj8-gnugrep-3.7/bin:/nix/store/pmh9q9k0g9s189v0iqrxpdp8j1g77gmd-gawk-5.1.1/bin:/nix/store/3fw5n1g3bb925hfll7fj7x94bd0q6k0r-gnutar-1.34/bin:/nix/store/nrvhb0yvawiqgrwbmbfmhjrmy934hhs5-gzip-1.12/bin:/nix/store/n7208v30hf3z4sz6127947vmzpfl46nb-bzip2-1.0.6.0.2-bin/bin:/nix/store/c5myz1zs9bsaq3y4s0rcxgkzb11irwra-gnumake-4.3/bin:/nix/store/9zm6br2ri10a0b71dll2wrim5bnhg6b6-bash-5.1-p16/bin:/nix/store/8636bkd1gg5s8675ipb9wf98wrc5mdpw-patch-2.7.6/bin:/nix/store/5ywhaxrasrp3pj72dnyy40mimx13fi0p-xz-5.2.5-bin/bin:/run/wrappers/bin:/home/jakubgs/.nix-profile/bin:/etc/profiles/per-user/jakubgs/bin:/nix/var/nix/profiles/default/bin:/run/current-system/sw/bin:/home/jakubgs/go/bin:/home/jakubgs/bin:/home/jakubgs/go/bin:/home/jakubgs/bin:/home/jakubgs/go/bin:/home/jakubgs/bin)
ERROR: No find aarch64-linux-gnu-gcc

So I will need to create Nix shell for this.

@jakubgs
Copy link
Owner Author

jakubgs commented Jun 4, 2023

So I have created a Nix shell using this example:

{ pkgs ? import <nixpkgs> {
    crossSystem = { config = "aarch64-unknown-linux-gnu"; };
  }
}:

pkgs.mkShell {
  buildInputs = [ gcc ];
}

https://nixos.wiki/wiki/Cross_Compiling#How_to_obtain_a_shell_with_a_cross_compiler

But I'm getting some issues, for example:

aarch64-unknown-linux-gnu-ld: cannot find rcs: No such file or directory
aarch64-unknown-linux-gnu-ld: cannot find arch/arm/cpu/built-in.o: No such file or directory
aarch64-unknown-linux-gnu-ld: cannot find arch/arm/lib/lib.a: No such file or directory

@jakubgs
Copy link
Owner Author

jakubgs commented Jun 4, 2023

This link shows build dependencies for U-Boot on Debian:
https://u-boot.readthedocs.io/en/latest/build/gcc.html#debian-based

@jakubgs
Copy link
Owner Author

jakubgs commented Jun 4, 2023

I tried building with a derivation:

{ pkgs ? import <nixpkgs> {
    crossSystem = { config = "aarch64-unknown-linux-gnu"; };
  }
}:

pkgs.stdenv.mkDerivation {
  name = "uboot-nanopi6";
  version = "v2017.09";

  src = pkgs.fetchFromGitHub {
    owner = "friendlyarm";
    repo = "uboot-rockchip";
    rev = "nanopi6-v2017.09";
    sha256 = "sha256-8zg/7py9FXn+6GY+D5zSAoOk/3NgQj1FMLWBYzZJKZM=";
  };
  srcs = [
    (pkgs.fetchFromGitHub {
      owner = "friendlyarm";
      repo = "rkbin";
      rev = "nanopi6";
      sha256 = "sha256-XGnRUW5Foz0U1V08Kd6Cr/OkwY81xzCzS05irdawWjo=";
    })
  ];

  buildInputs = with pkgs; [ gcc linuxHeaders ];
  nativeBuildInputs = with pkgs; [ which glibc autoPatchelfHook ];

  unpackPhase = ''
    cp -r $srcs rkbin
    cp -r $src uboot-rockchip
    find ./ -name 'built-in.o'
    cd uboot-rockchip
  '';

  preBuild = ''
    chmod -R +w ./ ../rkbin
    autoPatchelf ./ ../rkbin
    patchShebangs make.sh scripts arch

    function makeWrapper() {
      AR=$(which $1) 
      echo -e "#!$SHELL\nexec $CC \$@" > $TMP_BIN/$2
      chmod +x $TMP_BIN/$2
    }
    TMP_BIN=$(mktemp -d)
    export PATH="$TMP_BIN:$PATH"
    makeWrapper aarch64-unknown-linux-gnu-gcc cc
    makeWrapper aarch64-unknown-linux-gnu-gcc aarch64-linux-gnu-gcc
    makeWrapper aarch64-unknown-linux-gnu-ld aarch64-linux-gnu-ld
    makeWrapper aarch64-unknown-linux-gnu-ar aarch64-linux-gnu-ar
  '';

  buildPhase = ''
    runHook preBuild
    ./make.sh nanopi6
    runHook postBuild
  '';
}

But it just fails with the same errors:

aarch64-unknown-linux-gnu-gcc: error: rcs: No such file or directory
aarch64-unknown-linux-gnu-gcc: error: arch/arm/cpu/built-in.o: No such file or directory

I have no idea what the rcs part is about, but the arch/arm/cpu/built-in.o part seems like it should come from Linux kernel.

@jakubgs
Copy link
Owner Author

jakubgs commented Jun 4, 2023

I thought that the rcs thing might come from the toolchain provided by FriendlyELC:
https://drive.google.com/file/d/16sJLrExcaCnCba13cc22Sw2k9pEJdEMw/view?usp=share_link
But when I search the tarball I can't find anything like that:

 > tar tvf cross_compiler.tgz | grep rcs  

@jakubgs
Copy link
Owner Author

jakubgs commented Jun 4, 2023

But the build instructions in the wiki do say we need the toolchain in the PATH anyway:

export PATH=/opt/FriendlyARM/toolchain/11.3-aarch64/bin/:$PATH

So maybe trying to build this with Nix is a bad idea, if the only easy way is using their toolchain.

@jakubgs
Copy link
Owner Author

jakubgs commented Oct 21, 2023

Aaaaand it worked:

<<< NixOS Stage 1 >>>

loading module dm_mod...
running udev...
Starting systemd-udevd version 254.3
kbd_mode: KDSKBMODE: Inappropriate ioctl for device
Gstarting device mapper and LVM...
checking /dev/disk/by-uuid/4559e7f3-1fd3-426b-8550-50c4e0ecb49d...
fsck (busybox 1.36.1)
[fsck.ext4 (1) -- /mnt-root/] fsck.ext4 -a /dev/disk/by-uuid/4559e7f3-1fd3-426b-8550-50c4e0ecb49d
/dev/disk/by-uuid/4559e7f3-1fd3-426b-8550-50c4e0ecb49d: clean, 72495/31260672 files, 2729604/125026901 blocks
mounting /dev/disk/by-uuid/4559e7f3-1fd3-426b-8550-50c4e0ecb49d on /...

<<< NixOS Stage 2 >>>

running activation script...
setting up /etc...
3starting systemd...

Welcome to NixOS 23.11 (Tapir)!

Thank fuck.

@jakubgs
Copy link
Owner Author

jakubgs commented Oct 21, 2023

This error can be fixed by removing /boot/EFI and recreating it as /boot/efi:

Not booted with EFI, skipping EFI variable setup.
Traceback (most recent call last):
  File "/nix/store/8hryga86n7x5cw7ik7iphpm778gpmp0l-systemd-boot", line 334, in <module>
    main()
  File "/nix/store/8hryga86n7x5cw7ik7iphpm778gpmp0l-systemd-boot", line 322, in main
    install_bootloader(args)
  File "/nix/store/8hryga86n7x5cw7ik7iphpm778gpmp0l-systemd-boot", line 273, in install_bootloader
    os.makedirs("/boot/efi/nixos", exist_ok=True)
  File "/nix/store/idpjrasbr9n8kij11s5mphrw770sf13s-python3-3.10.12/lib/python3.10/os.py", line 225, in makedirs
    mkdir(name, mode)
FileNotFoundError: [Errno 2] No such file or directory: '/boot/efi/nixos'

@jakubgs
Copy link
Owner Author

jakubgs commented Oct 21, 2023

It does have trouble mounting the eMMC:

[ TIME ] Timed out waiting for device /dev/disk/by-uuid/364E-B75E.
[DEPEND] Dependency failed for File System …eck on /dev/disk/by-uuid/364E-B75E.
[DEPEND] Dependency failed for /boot.
[DEPEND] Dependency failed for Local File Systems.

But that's a minor issue that can probably be solved by moving the EFI partition to the NVMe.

@jakubgs
Copy link
Owner Author

jakubgs commented Oct 21, 2023

It seems after booting it just doesn't see the eMMC:

[root@arael:~]# ls -l /dev/disk/by-uuid/364E-B75E 
ls: cannot access '/dev/disk/by-uuid/364E-B75E': No such file or directory

[root@arael:~]# fdisk -l
Disk /dev/nvme0n1: 476.94 GiB, 512110190592 bytes, 1000215216 sectors
Disk model: SAMSUNG MZVLB512HAJQ-000L7              
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xe43feb0f

Device         Boot Start        End    Sectors   Size Id Type
/dev/nvme0n1p1          1 1000215215 1000215215 476.9G 83 Linux

Unlike Armbian. Probably due to lack of DTS for Rock 5B:

nanopi-r6s:~:# sudo fdisk -l /dev/mmcblk0 
Disk /dev/mmcblk0: 28.91 GiB, 31037849600 bytes, 60620800 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 25A6920B-1CE1-4A88-9FA7-72ECBF641DC7

Device         Start      End  Sectors  Size Type
/dev/mmcblk0p1  2048    18431    16384    8M Linux filesystem
/dev/mmcblk0p2 18432 60620766 60602335 28.9G EFI System

@jakubgs
Copy link
Owner Author

jakubgs commented Oct 21, 2023

And there we have it:

<<< Welcome to NixOS 23.11pre537863.038b2922be3f (aarch64) - ttyS0 >>>

Run 'nixos-help' for the NixOS manual.

arael login:

Though it seems there's issues with the network interface:

[root@arael:~]# ip addr show enP3p1s0
2: enP3p1s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
    link/ether 72:86:70:41:d0:ee brd ff:ff:ff:ff:ff:ff

@jakubgs
Copy link
Owner Author

jakubgs commented Oct 21, 2023

Partitioning used:

format() {
  parted -s --align optimal "$1" -- mklabel gpt;
  parted -s --align optimal "$1" -- mkpart 'EFI'  2MB   6GiB  set 1 esp on;
  parted -s --align optimal "$1" -- mkpart 'SWAP' 6GiB  16GiB;
  parted -s --align optimal "$1" -- mkpart 'ROOT' 16GiB '100%';
  parted -s --align optimal "$1" -- print;
  mkswap /dev/nvme0n1p2;
  mkfs.vfat /dev/nvme0n1p1;
  mkfs.ext4 /dev/nvme0n1p2;
}

@jakubgs
Copy link
Owner Author

jakubgs commented Oct 21, 2023

Looks like the WAN interface isn't recognixed, the enP3p1s0 is the 2.5 Gbit LAN interface:

[root@arael:~]# ip addr show enP3p1s0
2: enP3p1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 72:86:70:41:d0:ee brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.163/24 brd 192.168.1.255 scope global dynamic noprefixroute enP3p1s0
       valid_lft 43198sec preferred_lft 37798sec

@jakubgs
Copy link
Owner Author

jakubgs commented Oct 21, 2023

This is a weird issue I have not seen before:

warning: Nix search path entry '/nix/var/nix/profiles/per-user/root/channels/nixos' does not exist, ignoring
warning: Nix search path entry '/nix/var/nix/profiles/per-user/root/channels' does not exist, ignoring
error: file 'nixpkgs' was not found in the Nix search path (add it using $NIX_PATH or -I)

       at «none»:0: (source not available)

You need to run nix-channels --update to fix it.

@jakubgs
Copy link
Owner Author

jakubgs commented Oct 21, 2023

I though by using pkgs.linuxPackages_latest which is 6.5.7 we can see the eMMC and the WAN interface:

[root@arael:~]# uname -a
Linux arael 6.5.7 #1-NixOS SMP Tue Oct 10 20:03:06 UTC 2023 aarch64 GNU/Linux

[root@arael:~]# lsblk
NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
nvme0n1     259:0    0 476.9G  0 disk 
├─nvme0n1p1 259:1    0     6G  0 part /boot
├─nvme0n1p2 259:2    0    10G  0 part 
└─nvme0n1p3 259:3    0 460.9G  0 part /nix/store
                                      /

[root@arael:~]# ip --brief addr show
lo               UNKNOWN        127.0.0.1/8 ::1/128 
enP3p1s0         UP             192.168.1.163/24 fdc9:185f:c797::26c/128 fdc9:185f:c797:0:b030:3046:6fc7:5ca/64 fdc9:185f:c797:0:7086:70ff:fe41:d0ee/64 fe80::7086:70ff:fe41:d0ee/64 

But unfortunately we can't. Might require the DST patches anyway.

@jakubgs
Copy link
Owner Author

jakubgs commented Oct 21, 2023

Added docs and config:

Will migrate NVMe from sachiel tomorrow.

@jakubgs
Copy link
Owner Author

jakubgs commented Oct 22, 2023

I have found a working slimmed down 6.6-rc6 kernel config:

But it doesn't seem like it actually adds anything, since both eMMC and the WAN interface still don't show up.

@jakubgs
Copy link
Owner Author

jakubgs commented Oct 22, 2023

Some cleanup of no longer necessary files:

Can always be restored if necessary.

@jakubgs
Copy link
Owner Author

jakubgs commented Oct 22, 2023

Fixed iptables failures in firewall service:

Fixes errors like:

iptables: Failed to initialize nft: Protocol not supported
RULE_APPEND failed (No such file or directory): rule in chain nixos-fw-log-refuse

@jakubgs
Copy link
Owner Author

jakubgs commented Oct 25, 2023

Thanks to a suggestion in this Discourse post I made:

I decided to write a stock aarch64 ISO image to a USB drive, and that turned out to work:

image

And the NVMe is detected without issues:

[nixos@nixos:~]$ sudo fdisk -l /dev/nvme0n1
Disk /dev/nvme0n1: 1.82 TiB, 2000398934016 bytes, 3907029168 sectors
Disk model: KINGSTON SNV2S2000G                     
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 0F958345-CBF2-42E7-8C1A-D5945F1F57AF

Device            Start        End    Sectors  Size Type
/dev/nvme0n1p1     4096   10485759   10481664    5G EFI System
/dev/nvme0n1p2 10485760   20971519   10485760    5G Linux filesystem
/dev/nvme0n1p3 20971520   62914559   41943040   20G Linux filesystem
/dev/nvme0n1p4 62914560 3907028991 3844114432  1.8T Linux filesystem

@jakubgs
Copy link
Owner Author

jakubgs commented Oct 25, 2023

Fuck, I can't use unstable NixOS and then switch to stable because mainline ZFS has extra features:

cannot import 'rpool': unsupported version or feature
This pool uses the following feature(s) not supported by this system:
	com.klarasystems:vdev_zaps_v2

So I have to use 23.05.

@jakubgs
Copy link
Owner Author

jakubgs commented Oct 25, 2023

But when i use 23.05 even with 6.5.5 kernel it cannot find NVMe devices at boot:

<<< NixOS Stage 1 >>>

loading module dm_mod...
running udev...
Starting systemd-udevd version 253.6
kbd_mode: KDSKBMODE: Inappropriate ioctl for device
Gstarting device mapper and LVM...
waiting for device /dev/disk/by-uuid/f79e6602-3cf9-4b40-8669-ca73d0c940a3 to appear.......................
waiting for device /dev/disk/by-uuid/5b592e10-8881-4dcf-a1ec-33117bdc248c to appear.......................
Timed out waiting for device /dev/disk/by-uuid/5b592e10-8881-4dcf-a1ec-33117bdc248c, trying to mount anyway.
mounting /dev/disk/by-uuid/5b592e10-8881-4dcf-a1ec-33117bdc248c on /...
[   65.123463] /dev/disk/by-uuid/5b592e10-8881-4dcf-a1ec-33117bdc248c: Can't open blockdev
mount: mounting /dev/disk/by-uuid/5b592e10-8881-4dcf-a1ec-33117bdc248c on /mnt-root/ failed: No such file or directory

@jakubgs
Copy link
Owner Author

jakubgs commented Oct 25, 2023

And there we have it. NanoPi R6C running NixOS 23.05 with 6.1.55 kernel:

jakubgs@arael /mnt
 > sudo dmidecode | grep 'Version:' | sort -u
	Firmware Version: Not Specified
	Version: 0
	Version: NanoPi R6C
	Version: Rockchip RK3588S
	Version: v0.9.1

jakubgs@arael /mnt
 > lsb_release -a
No LSB modules are available.
Distributor ID:	NixOS
Description:	NixOS 23.05 (Stoat)
Release:	23.05
Codename:	stoat

Changes:

Might write up a wiki page for this device on the weekend: https://nixos.wiki/wiki/NixOS_Wiki:Contributing

@amelchio
Copy link

amelchio commented Jan 4, 2024

This is helpful, thank you. Did you get the WAN port working?

@jakubgs
Copy link
Owner Author

jakubgs commented Jan 4, 2024

No, I'm running NixOS 23.11 with 6.1.69 Linux kernel and I see only the PCI-e RTL8125BG 2.5 Gbps LAN port:

 > sudo lspci 
0003:01:00.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL8125 2.5GbE Controller (rev 05)
0004:01:00.0 Non-Volatile memory controller: Kingston Technology Company, Inc. Device 5017 (rev 03)

 > sudo dmesg | grep RTL  
[   16.739821] r8169 0003:01:00.0 eth0: RTL8125B, 11:22:33:44:55:66, XID 641, IRQ 24
[   90.634885] RTL8226B_RTL8221B 2.5Gbps PHY r8169-3-100:00: attached PHY driver (mii_bus:phy_addr=r8169-3-100:00, irq=MAC)

I don't need both ports so I have not looked into it.

@jakubgs jakubgs closed this as completed Jan 4, 2024
@msgilligan
Copy link

msgilligan commented Sep 24, 2024

I followed your instructions here, but I used a newer EFI:

https://github.com/edk2-porting/edk2-rk3588/releases/download/v0.11.2/nanopi-r6c_UEFI_Release_v0.11.2.img

and a newer NixOS ISO:

https://hydra.nixos.org/build/273422643/download/1/nixos-minimal-24.11pre684053

Everything seems the be working but reads (and I assume writes) to the NVMe are very slow:

sudo hdparm -tT /dev/nvme0n1

/dev/nvme0n1:
 Timing cached reads:   2144 MB in  2.00 seconds = 1072.86 MB/sec
 Timing buffered disk reads:  16 MB in  3.34 seconds =   4.78 MB/sec

Any ideas for something I could have missed in configuration. Or should I reinstall with the older NixOS and/or EFI?

@jakubgs
Copy link
Owner Author

jakubgs commented Sep 24, 2024

I'm still on 24.05 using Linux kernel 6.6:

 > uname -a
Linux arael 6.6.46 #1-NixOS SMP Wed Aug 14 11:59:04 UTC 2024 aarch64 GNU/Linux

 > sudo hdparm -tT /dev/nvme0n1

/dev/nvme0n1:
 Timing cached reads:   4776 MB in  2.00 seconds = 2390.35 MB/sec
 Timing buffered disk reads: 1122 MB in  3.00 seconds = 373.94 MB/sec

And as far as I know I've installed 0.9.1 version of edk2-rk3588. Not sure what your problem is, but in my experience sometimes there are weird incompatibilities between the PCIe chip and the NVMe device. I've had this happen with my NanoPC T4, where some NVMe devices would refuse to be detected for no clear reason, while others would work flawlessly.

Might be more of a firmware issue than software one.

@msgilligan
Copy link

msgilligan commented Sep 24, 2024

Thanks for the response!

It appears to be an OS/software issue. I booted (using the 0.11.2 EFI) from an SDCard with the vendor's Debian 12 image and installed and ran hdparm:

sudo hdparm -tT /dev/nvme0n1

/dev/nvme0n1:
 Timing cached reads:   9840 MB in  2.00 seconds = 4923.82 MB/sec
 Timing buffered disk reads: 1160 MB in  3.00 seconds = 386.19 MB/sec

I guess I'll try it with 24.05 next.

@msgilligan
Copy link

Update: I'm seeing the same issue when booting from nixos-minimal-24.05.5218.23cbb250f3bf-aarch64-linux.iso

@jakubgs
Copy link
Owner Author

jakubgs commented Sep 25, 2024

I actually lock kernel at 6.6 for this device:

# Lock kernel version.
boot.kernelPackages = pkgs.linuxPackages_6_6;

Maybe it's worth trying a downgrade of the kernel? Though an older edk2-rk3588 might also be worth checking.

You issues suggest to me that I might not want to try to upgrade my version of edk2-rk3588.

@msgilligan
Copy link

I must confess that I am a NixOS beginner. I use NixPkgs on macOS and Linux, I've played with NixOS VMs, and I've booted a Raspberry Pi with a standard NixOS SDCard image, but this is my first time doing a NixOS install on "real" hardware.

I technically had the system running, but every time a did a nixos-rebuild switch it took about 1/2 hour, so it was very painful.

I have wiped the SSD and will start all over with 0.9.1 of edk2-rk3588 and NixOS 24.05. I don't remember which kernel version 24.05 comes with, but if it is later than 6.6, I will try to downgrade. I won't be able to try again till the weekend, though.

Thanks, again for your help.

@jakubgs
Copy link
Owner Author

jakubgs commented Sep 26, 2024

Yeah, since this is not a very powerful device building anything is going to take a while, especially if you have that terrible read performance from the NVMe, and Nix makes a LOT of small reads when re-evaluating configuration.

If you have general NixOS questions you can just wite to me over email, it's in my GitHub profile.

@msgilligan
Copy link

I tried edk2-rk3588 0.9.1 and I tried a really early version of NixOS 23.11 -- neither made a difference. I tried a different SSD (1TB instead of 2TB) by the same manufacturer (but with the same controller, I believe) and none of those things helped.

I order a new Samsung 1TB SSD yesterday (I couldn't find your exact model) and it arrived today. I installed it and everything is working great! Thanks for your help!

Now it's time to tempt fate and try reinstalling but formatting with BTRFS this time...

@jakubgs
Copy link
Owner Author

jakubgs commented Oct 1, 2024

Nice. So as I thought it was some kind of incompatibility between PCIe controller and the NVMe.

Also, ZFS is better.

@msgilligan
Copy link

So as I thought it was some kind of incompatibility between PCIe controller and the NVMe.

Yes, thank you for suggesting that. Apparently you can never have too many (working) SSDs...

I believe you that ZFS is better. But BTRFS seems to do everything I need for a SBC with a single NVMe SSD. I've also noticed that some builds of NixOS (latest kernels, I think) don't have ZFS. I will give a try eventually, though.

I will send you an email. Thanks, again for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants