Saturday, September 30, 2023

U-Boot for RK356x SoCs

So here is the start of a port for a few RK3566 and RK3568 boards.
These boards currently need some binary blobs to run. And a LICENSE
file was added to the relevant repository that explicitly allows
redistribution of these blobs.

The default baudrate for the serial console is embedded in one of the
blobs. Since the default rate is unusable, the port changes this to
115200, which is the de-facto standard for single-board computers
these days. This requires a small modification to the blobs, for
which I write a small C program.

At this point I'm primarily interested in suggestions about how I can
improve the way I handle these blobs.



diff --git a/sysutils/u-boot/Makefile b/sysutils/u-boot/Makefile
index 857155ac9f6..9da1b1599d5 100644
--- a/sysutils/u-boot/Makefile
+++ b/sysutils/u-boot/Makefile
@@ -2,5 +2,6 @@
SUBDIR += aarch64
SUBDIR += arm
SUBDIR += riscv64
+ SUBDIR += rk356x

.include <bsd.port.subdir.mk>
diff --git a/sysutils/u-boot/Makefile.inc b/sysutils/u-boot/Makefile.inc
index 34055a7e502..51e0582edc7 100644
--- a/sysutils/u-boot/Makefile.inc
+++ b/sysutils/u-boot/Makefile.inc
@@ -74,6 +74,16 @@ do-build:
${SETENV} ${MAKE_ENV} BL31=${RK3328_BL31} ${MAKE_PROGRAM} \
${MAKE_FLAGS} O="build/${BOARD}" \
-f ${MAKE_FILE} ${ALL_TARGET}
+.elif "${BOARD:M*-rk3566*}"
+ cd ${WRKSRC} && \
+ ${SETENV} ${MAKE_ENV} ROCKCHIP_TPL=${RK3566_TPL} ${MAKE_PROGRAM} \
+ ${MAKE_FLAGS} O="build/${BOARD}" \
+ -f ${MAKE_FILE} ${ALL_TARGET}
+.elif "${BOARD:M*-rk3568*}"
+ cd ${WRKSRC} && \
+ ${SETENV} ${MAKE_ENV} ROCKCHIP_TPL=${RK3568_TPL} ${MAKE_PROGRAM} \
+ ${MAKE_FLAGS} O="build/${BOARD}" \
+ -f ${MAKE_FILE} ${ALL_TARGET}
.elif "${BOARD:M*sifive_*}"
cd ${WRKSRC} && \
${SETENV} ${MAKE_ENV} OPENSBI=${FW_DYNAMIC} ${MAKE_PROGRAM} \
diff --git a/sysutils/u-boot/rk356x/Makefile b/sysutils/u-boot/rk356x/Makefile
new file mode 100644
index 00000000000..2de3b5c138c
--- /dev/null
+++ b/sysutils/u-boot/rk356x/Makefile
@@ -0,0 +1,33 @@
+VERSION= 2023.10-rc4
+
+SOC= rk356x
+
+BOARDS=\
+ quartz64-b-rk3566 \
+ rock-3a-rk3568
+
+BUILD_DEPENDS+= devel/arm-none-eabi/gcc,aarch64 \
+ devel/py-elftools${MODPY_FLAVOR} \
+ sysutils/e2fsprogs
+CROSS_COMPILE= aarch64-none-elf-
+
+RK3566_TPL= rk3566_ddr_1056MHz_v1.18.bin
+RK3568_TPL= rk3568_ddr_1560MHz_v1.18.bin
+RK356X_BL31= rk3568_bl31_v1.43.elf
+
+DISTFILES.rkbin= ${RK3566_TPL} ${RK3568_TPL} ${RK356X_BL31}
+SITES.rkbin= https://github.com/rockchip-linux/rkbin/raw/master/bin/rk35/
+EXTRACT_ONLY= ${DISTNAME}${EXTRACT_SUFX}
+
+MAKE_ENV+= BL31=${FULLDISTDIR}/${RK356X_BL31}
+
+pre-build:
+ cc -o ${WRKSRC}/rkbinpatch ${FILESDIR}/rkbinpatch.c
+ cd ${WRKSRC} && \
+ cp ${FULLDISTDIR}/${RK3566_TPL} . && \
+ ./rkbinpatch ${RK3566_TPL}
+ cd ${WRKSRC} && \
+ cp ${FULLDISTDIR}/${RK3568_TPL} . && \
+ ./rkbinpatch ${RK3568_TPL}
+
+.include <bsd.port.mk>
diff --git a/sysutils/u-boot/rk356x/distinfo b/sysutils/u-boot/rk356x/distinfo
new file mode 100644
index 00000000000..3b057558d11
--- /dev/null
+++ b/sysutils/u-boot/rk356x/distinfo
@@ -0,0 +1,8 @@
+SHA256 (rk3566_ddr_1056MHz_v1.18.bin) = 3ABjVq4NHR9mhYo3Yub1pSlMruHKFz7lsMRTG9QgjU8=
+SHA256 (rk3568_bl31_v1.43.elf) = U7k3G+6qDGo8AjWg8Gmtxxn/kCinhjdyzl7vJBVqsHw=
+SHA256 (rk3568_ddr_1560MHz_v1.18.bin) = nmIAyhP4RjebrnA7A21C4oCIirOoFDmZOAvcmJjQQyI=
+SHA256 (u-boot-2023.10-rc4.tar.bz2) = khjrF3FCF8EAaCXXLhYO65kWh5yjZa/MQaa4UcI43ps=
+SIZE (rk3566_ddr_1056MHz_v1.18.bin) = 55296
+SIZE (rk3568_bl31_v1.43.elf) = 402376
+SIZE (rk3568_ddr_1560MHz_v1.18.bin) = 55296
+SIZE (u-boot-2023.10-rc4.tar.bz2) = 19677302
diff --git a/sysutils/u-boot/rk356x/files/rkbinpatch.c b/sysutils/u-boot/rk356x/files/rkbinpatch.c
new file mode 100644
index 00000000000..0ec5688d394
--- /dev/null
+++ b/sysutils/u-boot/rk356x/files/rkbinpatch.c
@@ -0,0 +1,46 @@
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <err.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+int
+main(int argc, char *argv[])
+{
+ struct stat st;
+ void *start, *end;
+ uint32_t *word;
+ uint32_t data;
+ int fd;
+
+ fd = open(argv[1], O_RDWR);
+ if (fd == -1)
+ err(1, "%s", argv[1]);
+
+ if (fstat(fd, &st) == -1)
+ err(1, "%s: stat", argv[1]);
+
+ start = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE,
+ MAP_SHARED, fd, 0);
+ if (start == MAP_FAILED)
+ err(1, "%s: mmap", argv[1]);
+
+ end = (char *)start + st.st_size;
+ for (word = start; (void *)word < end; word++) {
+ if (*word == 0x12345678 && (void *)(word + 10) < end) {
+ data = *(word + 9);
+ if ((data & 0xffffff) == 1500000) {
+ data &= 0xff000000;
+ data |= 115200;
+ *(word + 9) = data;
+ close(fd);
+ return 0;
+ }
+ }
+ }
+
+ warnx("%s: can't find parameters", argv[1]);
+
+ close(fd);
+ return 1;
+}
diff --git a/sysutils/u-boot/rk356x/patches/patch-configs_quartz64-b-rk3566_defconfig b/sysutils/u-boot/rk356x/patches/patch-configs_quartz64-b-rk3566_defconfig
new file mode 100644
index 00000000000..c5cf259f07e
--- /dev/null
+++ b/sysutils/u-boot/rk356x/patches/patch-configs_quartz64-b-rk3566_defconfig
@@ -0,0 +1,12 @@
+Index: configs/quartz64-b-rk3566_defconfig
+--- configs/quartz64-b-rk3566_defconfig.orig
++++ configs/quartz64-b-rk3566_defconfig
+@@ -90,7 +90,7 @@ CONFIG_PWM_ROCKCHIP=y
+ CONFIG_SPL_RAM=y
+ CONFIG_SCSI=y
+ CONFIG_DM_SCSI=y
+-CONFIG_BAUDRATE=1500000
++CONFIG_BAUDRATE=115200
+ CONFIG_DEBUG_UART_SHIFT=2
+ CONFIG_SYS_NS16550_MEM32=y
+ CONFIG_ROCKCHIP_SFC=y
diff --git a/sysutils/u-boot/rk356x/patches/patch-configs_rock-3a-rk3568_defconfig b/sysutils/u-boot/rk356x/patches/patch-configs_rock-3a-rk3568_defconfig
new file mode 100644
index 00000000000..548f36030fb
--- /dev/null
+++ b/sysutils/u-boot/rk356x/patches/patch-configs_rock-3a-rk3568_defconfig
@@ -0,0 +1,12 @@
+Index: configs/rock-3a-rk3568_defconfig
+--- configs/rock-3a-rk3568_defconfig.orig
++++ configs/rock-3a-rk3568_defconfig
+@@ -90,7 +90,7 @@ CONFIG_PWM_ROCKCHIP=y
+ CONFIG_SPL_RAM=y
+ CONFIG_SCSI=y
+ CONFIG_DM_SCSI=y
+-CONFIG_BAUDRATE=1500000
++CONFIG_BAUDRATE=115200
+ CONFIG_DEBUG_UART_SHIFT=2
+ CONFIG_SYS_NS16550_MEM32=y
+ CONFIG_ROCKCHIP_SFC=y
diff --git a/sysutils/u-boot/rk356x/pkg/DESCR b/sysutils/u-boot/rk356x/pkg/DESCR
new file mode 100644
index 00000000000..2d1b3c1f5cc
--- /dev/null
+++ b/sysutils/u-boot/rk356x/pkg/DESCR
@@ -0,0 +1,9 @@
+U-Boot is a firmware for embedded boards based on PowerPC, ARM, MIPS and
+several other processors, which can be installed in a boot ROM and used to
+initialize and test the hardware or to download and run application code.
+
+As single-board computers often do not come with firmware it must be
+supplied on an SD card or MMC device to have a bootable system.
+
+This package provides U-Boot for various boards using Rockchip RK3566
+and RK3568 SoCs.
diff --git a/sysutils/u-boot/rk356x/pkg/PLIST b/sysutils/u-boot/rk356x/pkg/PLIST
new file mode 100644
index 00000000000..69e80e59081
--- /dev/null
+++ b/sysutils/u-boot/rk356x/pkg/PLIST
@@ -0,0 +1,17 @@
+share/u-boot/
+share/u-boot/quartz64-b-rk3566/
+@comment share/u-boot/quartz64-b-rk3566/idbloader.img
+@comment share/u-boot/quartz64-b-rk3566/u-boot
+share/u-boot/quartz64-b-rk3566/u-boot-rockchip.bin
+@comment share/u-boot/quartz64-b-rk3566/u-boot-spl.bin
+@comment share/u-boot/quartz64-b-rk3566/u-boot.bin
+@comment share/u-boot/quartz64-b-rk3566/u-boot.img
+@comment share/u-boot/quartz64-b-rk3566/u-boot.itb
+share/u-boot/rock-3a-rk3568/
+@comment share/u-boot/rock-3a-rk3568/idbloader.img
+@comment share/u-boot/rock-3a-rk3568/u-boot
+share/u-boot/rock-3a-rk3568/u-boot-rockchip.bin
+@comment share/u-boot/rock-3a-rk3568/u-boot-spl.bin
+@comment share/u-boot/rock-3a-rk3568/u-boot.bin
+@comment share/u-boot/rock-3a-rk3568/u-boot.img
+@comment share/u-boot/rock-3a-rk3568/u-boot.itb

No comments:

Post a Comment