From ea232fc53aaefc822a3a073c88e48243b2cf7e48 Mon Sep 17 00:00:00 2001 From: rprinz08 Date: Sat, 9 May 2020 19:20:32 +0200 Subject: [PATCH 1/3] BIOS boot firmware from SPI with address offset --- litex/soc/software/bios/boot.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/litex/soc/software/bios/boot.c b/litex/soc/software/bios/boot.c index 8f53fcca3..ba2376cb7 100644 --- a/litex/soc/software/bios/boot.c +++ b/litex/soc/software/bios/boot.c @@ -366,6 +366,11 @@ void netboot(void) #endif #ifdef FLASH_BOOT_ADDRESS +#ifdef FLASH_BOOT_OFFSET +unsigned int flash_boot_address = FLASH_BOOT_ADDRESS + FLASH_BOOT_OFFSET; +#else +unsigned int flash_boot_address = FLASH_BOOT_ADDRESS; +#endif /* On systems with exernal SDRAM we copy out of the SPI flash into the SDRAM before running, as it is faster. If we have no SDRAM then we have to @@ -374,7 +379,7 @@ void netboot(void) #define FIRMWARE_BASE_ADDRESS MAIN_RAM_BASE #else /* Firmware code starts after (a) length and (b) CRC -- both unsigned ints */ -#define FIRMWARE_BASE_ADDRESS (FLASH_BOOT_ADDRESS + 2 * sizeof(unsigned int)) +#define FIRMWARE_BASE_ADDRESS (flash_boot_address + 2 * sizeof(unsigned int)) #endif static unsigned int check_image_in_flash(unsigned int base_address) @@ -399,7 +404,7 @@ static unsigned int check_image_in_flash(unsigned int base_address) return length; } -#if defined(MAIN_RAM_BASE) && defined(CONFIG_CPU_TYPE_VEXRISCV) && defined(CONFIG_CPU_VARIANT_LINUX) +#if defined(MAIN_RAM_BASE) && defined(CONFIG_CPU_TYPE_VEXRISCV) && defined(FLASH_BOOT_ADDRESS) static int copy_image_from_flash_to_ram(unsigned int flash_address, unsigned int ram_address) { unsigned int length; @@ -408,7 +413,7 @@ static int copy_image_from_flash_to_ram(unsigned int flash_address, unsigned int if(length > 0) { printf("Copying %d bytes from 0x%08x to 0x%08x...\n", length, flash_address, ram_address); // skip length and crc - memcpy((void *)ram_address, (void *)flash_address + 8, length); + memcpy((void *)ram_address, (unsigned int *)(flash_address + 2 * sizeof(unsigned int)), length); return 1; } @@ -432,34 +437,34 @@ static int copy_image_from_flash_to_ram(unsigned int flash_address, unsigned int void flashboot(void) { unsigned int length; + unsigned int result; #if defined(MAIN_RAM_BASE) && defined(CONFIG_CPU_TYPE_VEXRISCV) && defined(CONFIG_CPU_VARIANT_LINUX) - unsigned int result; printf("Loading Image from flash...\n"); result = copy_image_from_flash_to_ram( - (FLASH_BOOT_ADDRESS + KERNEL_IMAGE_FLASH_OFFSET), + (flash_boot_address + KERNEL_IMAGE_FLASH_OFFSET), (MAIN_RAM_BASE + KERNEL_IMAGE_RAM_OFFSET)); if(result) { printf("Loading rootfs.cpio from flash...\n"); result &= copy_image_from_flash_to_ram( - (FLASH_BOOT_ADDRESS + ROOTFS_IMAGE_FLASH_OFFSET), + (flash_boot_address + ROOTFS_IMAGE_FLASH_OFFSET), (MAIN_RAM_BASE + ROOTFS_IMAGE_RAM_OFFSET)); } if(result) { printf("Loading rv32.dtb from flash...\n"); result &= copy_image_from_flash_to_ram( - (FLASH_BOOT_ADDRESS + DEVICE_TREE_IMAGE_FLASH_OFFSET), + (flash_boot_address + DEVICE_TREE_IMAGE_FLASH_OFFSET), (MAIN_RAM_BASE + DEVICE_TREE_IMAGE_RAM_OFFSET)); } if(result) { printf("Loading emulator.bin from flash...\n"); result &= copy_image_from_flash_to_ram( - (FLASH_BOOT_ADDRESS + EMULATOR_IMAGE_FLASH_OFFSET), + (flash_boot_address + EMULATOR_IMAGE_FLASH_OFFSET), (MAIN_RAM_BASE + EMULATOR_IMAGE_RAM_OFFSET)); } @@ -469,15 +474,15 @@ void flashboot(void) } #endif - printf("Booting from flash...\n"); - length = check_image_in_flash(FLASH_BOOT_ADDRESS); + printf("Booting from flash addr 0x%08x...\n", flash_boot_address); + length = check_image_in_flash(flash_boot_address); if(!length) return; #ifdef MAIN_RAM_BASE - printf("Loading %d bytes from flash...\n", length); - // skip length and crc - memcpy((void *)MAIN_RAM_BASE, (unsigned int *)(FLASH_BOOT_ADDRESS + 2 * sizeof(unsigned int)), length); + result = copy_image_from_flash_to_ram(flash_boot_address, MAIN_RAM_BASE); + if(!result) + return; #endif boot(0, 0, 0, FIRMWARE_BASE_ADDRESS); From f062c0c44b63e63add017296151d87b7c9955b1a Mon Sep 17 00:00:00 2001 From: rprinz08 Date: Tue, 12 May 2020 16:57:21 +0200 Subject: [PATCH 2/3] removed FLASH_BOOT_OFFSET, replaced memcyp with copy_image_from_flash_to_ram --- litex/soc/software/bios/boot.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/litex/soc/software/bios/boot.c b/litex/soc/software/bios/boot.c index ba2376cb7..c2fa44924 100644 --- a/litex/soc/software/bios/boot.c +++ b/litex/soc/software/bios/boot.c @@ -366,11 +366,6 @@ void netboot(void) #endif #ifdef FLASH_BOOT_ADDRESS -#ifdef FLASH_BOOT_OFFSET -unsigned int flash_boot_address = FLASH_BOOT_ADDRESS + FLASH_BOOT_OFFSET; -#else -unsigned int flash_boot_address = FLASH_BOOT_ADDRESS; -#endif /* On systems with exernal SDRAM we copy out of the SPI flash into the SDRAM before running, as it is faster. If we have no SDRAM then we have to @@ -379,7 +374,7 @@ unsigned int flash_boot_address = FLASH_BOOT_ADDRESS; #define FIRMWARE_BASE_ADDRESS MAIN_RAM_BASE #else /* Firmware code starts after (a) length and (b) CRC -- both unsigned ints */ -#define FIRMWARE_BASE_ADDRESS (flash_boot_address + 2 * sizeof(unsigned int)) +#define FIRMWARE_BASE_ADDRESS (FLASH_BOOT_ADDRESS + 2 * sizeof(unsigned int)) #endif static unsigned int check_image_in_flash(unsigned int base_address) @@ -443,28 +438,28 @@ void flashboot(void) printf("Loading Image from flash...\n"); result = copy_image_from_flash_to_ram( - (flash_boot_address + KERNEL_IMAGE_FLASH_OFFSET), + (FLASH_BOOT_ADDRESS + KERNEL_IMAGE_FLASH_OFFSET), (MAIN_RAM_BASE + KERNEL_IMAGE_RAM_OFFSET)); if(result) { printf("Loading rootfs.cpio from flash...\n"); result &= copy_image_from_flash_to_ram( - (flash_boot_address + ROOTFS_IMAGE_FLASH_OFFSET), + (FLASH_BOOT_ADDRESS + ROOTFS_IMAGE_FLASH_OFFSET), (MAIN_RAM_BASE + ROOTFS_IMAGE_RAM_OFFSET)); } if(result) { printf("Loading rv32.dtb from flash...\n"); result &= copy_image_from_flash_to_ram( - (flash_boot_address + DEVICE_TREE_IMAGE_FLASH_OFFSET), + (FLASH_BOOT_ADDRESS + DEVICE_TREE_IMAGE_FLASH_OFFSET), (MAIN_RAM_BASE + DEVICE_TREE_IMAGE_RAM_OFFSET)); } if(result) { printf("Loading emulator.bin from flash...\n"); result &= copy_image_from_flash_to_ram( - (flash_boot_address + EMULATOR_IMAGE_FLASH_OFFSET), + (FLASH_BOOT_ADDRESS + EMULATOR_IMAGE_FLASH_OFFSET), (MAIN_RAM_BASE + EMULATOR_IMAGE_RAM_OFFSET)); } @@ -474,13 +469,13 @@ void flashboot(void) } #endif - printf("Booting from flash addr 0x%08x...\n", flash_boot_address); - length = check_image_in_flash(flash_boot_address); + printf("Booting from flash addr 0x%08x...\n", FLASH_BOOT_ADDRESS); + length = check_image_in_flash(FLASH_BOOT_ADDRESS); if(!length) return; #ifdef MAIN_RAM_BASE - result = copy_image_from_flash_to_ram(flash_boot_address, MAIN_RAM_BASE); + result = copy_image_from_flash_to_ram(FLASH_BOOT_ADDRESS, MAIN_RAM_BASE); if(!result) return; #endif From 1f55fcf4496652497f717c6407922ad5bba49d82 Mon Sep 17 00:00:00 2001 From: rprinz08 Date: Tue, 12 May 2020 16:58:42 +0200 Subject: [PATCH 3/3] fixed bug in BIOS spi flash "fw" command --- litex/soc/software/bios/commands/cmd_spi_flash.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/litex/soc/software/bios/commands/cmd_spi_flash.c b/litex/soc/software/bios/commands/cmd_spi_flash.c index e607a89df..8ca18c87f 100644 --- a/litex/soc/software/bios/commands/cmd_spi_flash.c +++ b/litex/soc/software/bios/commands/cmd_spi_flash.c @@ -43,7 +43,7 @@ static void fw(int nb_params, char **params) if (nb_params == 2) { count = 1; } else { - count = strtoul(count, &c, 0); + count = strtoul(params[2], &c, 0); if (*c != 0) { printf("Incorrect count"); return;