Andrey Rys пре 1 година
родитељ
комит
0899e0c796
5 измењених фајлова са 176 додато и 0 уклоњено
  1. 10 0
      spl_tool/.gitignore
  2. 15 0
      spl_tool/Makefile
  3. 19 0
      spl_tool/README
  4. 34 0
      spl_tool/crc32.c
  5. 98 0
      spl_tool/jh7110_uboot_spl.c

+ 10 - 0
spl_tool/.gitignore

@@ -0,0 +1,10 @@
+_*
+*.swp
+*.o
+*.out
+*.key
+*.diff
+*.patch
+*.out
+tags
+jh7110_uboot_spl

+ 15 - 0
spl_tool/Makefile

@@ -0,0 +1,15 @@
+override CFLAGS=-Wall -O2
+
+SRCS = $(wildcard *.c)
+OBJS = $(SRCS:.c=.o)
+
+all: jh7110_uboot_spl
+
+%.o: %.c
+	$(CC) $(CFLAGS) -c -o $@ $<
+
+jh7110_uboot_spl: $(OBJS)
+	$(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) -o $@
+
+clean:
+	rm -f *.o jh7110_uboot_spl

+ 19 - 0
spl_tool/README

@@ -0,0 +1,19 @@
+DESCRIPTION
+
+A replacement for VisionFive2 SDK spl_tool which happened to be proprietary.
+Use only with VisionFive2 boards to make u-boot-spl.bin.normal.out image!
+
+DISCLAIMER
+
+Use at your own risk! I, Rys Andrey, don't bear any responsibility for
+your broken hardware. Check the image carefully with your eyes before
+flashing it to real device!
+
+WHY
+
+Cause I want it to run on RISC-V when building U-Boot there. I don't want
+to resort to qemu-x86_64 to run a tool which does something very simple.
+
+LICENSE
+
+Public domain.

+ 34 - 0
spl_tool/crc32.c

@@ -0,0 +1,34 @@
+#include <sys/types.h>
+#include <stdlib.h>
+#include <stdint.h>
+
+static uint32_t crc32_reverse(uint32_t x)
+{
+	x = ((x & 0x55555555) << 1) | ((x >> 1) & 0x55555555);
+	x = ((x & 0x33333333) << 2) | ((x >> 2) & 0x33333333);
+	x = ((x & 0x0F0F0F0F) << 4) | ((x >> 4) & 0x0F0F0F0F);
+	x = (x << 24) | ((x & 0xFF00) << 8) | ((x >> 8) & 0xFF00) | (x >> 24);
+	return x;
+}
+
+uint32_t crc32(uint32_t iv, uint32_t sv, const void *data, size_t n)
+{
+	const unsigned char *ptr;
+	unsigned x;
+	uint32_t byte, crc;
+
+	crc = iv;
+	ptr = data;
+	while (n--) {
+		byte = *ptr++;
+		byte = crc32_reverse(byte);
+		for (x = 0; x < 8; x++, byte <<= 1) crc = ((crc ^ byte) & 0x80000000U) ? (crc << 1) ^ sv : (crc << 1);
+	}
+
+	return crc;
+}
+
+uint32_t crc32_final(uint32_t iv)
+{
+	return crc32_reverse(iv ^ ~0U);
+}

+ 98 - 0
spl_tool/jh7110_uboot_spl.c

@@ -0,0 +1,98 @@
+#define _GNU_SOURCE
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+#include <endian.h>
+#include <errno.h>
+#include <limits.h>
+
+#define NOSIZE ((size_t)-1)
+
+extern uint32_t crc32(uint32_t iv, uint32_t sv, const void *data, size_t n);
+extern uint32_t crc32_final(uint32_t iv);
+
+struct __attribute__((__packed__)) ubootsplhdr {
+	uint8_t  res1[  2];
+	uint8_t  zro1[  4];
+	uint8_t  res2[  4];
+	uint8_t  zro2[634];
+	uint32_t vers;
+	uint32_t fsiz;
+	uint8_t  res3[  4];
+	uint32_t crcs;
+	uint8_t  zro3[364];
+};
+
+static struct ubootsplhdr ubsplhdr = {
+	.res1[0] = 0x40, .res1[1] = 0x02,
+	/* res2 seems to be "# Offset of backup SBL from Flash info start"
+	 * from SBL_BAK_OFFSET @ input_files/input_sbl_normal.cfg */
+	.res2[0] = 0x20, .res2[1] = 0x00, .res2[2] = 0x00, .res2[3] = 0x00,
+	.res3[0] = 0x00, .res3[1] = 0x04, .res3[2] = 0x00, .res3[3] = 0x00,
+};
+
+static char ubootspl[131072-0x400];
+static char outpath[PATH_MAX];
+
+static void xerror(int errnoval, const char *s)
+{
+	if (errnoval) perror(s);
+	else fprintf(stderr, "%s\n", s);
+	exit(2);
+}
+
+static void usage(void)
+{
+	printf("usage: jh7110_uboot_spl u-boot-spl.bin version\n");
+	printf("u-boot-spl.bin is path to input U-Boot SPL file\n");
+	printf("version shall be 0x01010101\n");
+	printf("output will be written to u-boot-spl.bin.out\n");
+	exit(1);
+}
+
+int main(int argc, char **argv)
+{
+	int fd;
+	uint32_t v;
+	size_t sz;
+
+	if (argc < 3) usage();
+
+	fd = open(argv[1], O_RDONLY);
+	if (fd == -1) xerror(errno, argv[1]);
+
+	v = (uint32_t)strtoul(argv[2], NULL, 16);
+	v = htole32(v);
+	ubsplhdr.vers = v;
+
+	sz = (size_t)read(fd, ubootspl, sizeof(ubootspl));
+	if (sz == NOSIZE) xerror(errno, argv[1]);
+	if (sz >= (sizeof(ubootspl) - sizeof(struct ubootsplhdr)))
+		xerror(0, "File too large! Please rebuild your SPL with -Os. Maximum allowed size is 130048 bytes.");
+	v = htole32((uint32_t)sz);
+	ubsplhdr.fsiz = v;
+
+	close(fd);
+	snprintf(outpath, sizeof(outpath), "%s.out", argv[1]);
+	fd = creat(outpath, 0666);
+	if (fd == -1) xerror(errno, outpath);
+
+	v = crc32(~0U, 0x04c11db7U, ubootspl, sz);
+	v = crc32_final(v);
+	v = htole32(v);
+	ubsplhdr.crcs = v;
+
+	write(fd, &ubsplhdr, sizeof(struct ubootsplhdr));
+	write(fd, ubootspl, sz);
+
+	close(fd);
+
+	printf("SPL written to %s successfully.\n", outpath);
+
+	return 0;
+}