From 906fa73747ed709dbd8f96ff3368ee6e620a8a0d Mon Sep 17 00:00:00 2001 From: Jacob Pease Date: Wed, 31 Jul 2024 11:12:05 -0500 Subject: [PATCH] Updated formatting of gpt.c and boot.c. --- fpga/zsbl/boot.c | 40 ++++++++++++++++++++++++++-------------- fpga/zsbl/gpt.c | 21 +++++---------------- fpga/zsbl/uart.h | 3 +++ 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/fpga/zsbl/boot.c b/fpga/zsbl/boot.c index 5b022cea3..7b2e9af7d 100644 --- a/fpga/zsbl/boot.c +++ b/fpga/zsbl/boot.c @@ -34,10 +34,12 @@ /* return 0;; */ /* } */ +#define SYSTEMCLOCK 20000000 + int disk_read(BYTE * buf, LBA_t sector, UINT count) { uint64_t r; UINT i; - + uint8_t crc = 0; crc = crc7(crc, 0x40 | SD_CMD_READ_BLOCK_MULTIPLE); crc = crc7(crc, (sector >> 24) & 0xff); @@ -46,25 +48,29 @@ int disk_read(BYTE * buf, LBA_t sector, UINT count) { crc = crc7(crc, sector & 0xff); crc = crc | 1; - if (sd_cmd(18, sector & 0xffffffff, crc) != 0x00) { - print_uart("disk_read: CMD18 failed. r = "); - print_uart_byte(r & 0xff); + if ((r = sd_cmd(18, sector & 0xffffffff, crc) & 0xff) != 0x00) { + print_uart("disk_read: CMD18 failed. r = 0x"); + print_uart_byte(r); print_uart("\r\n"); return -1; } + // write_reg(SPI_CSMODE, SIFIVE_SPI_CSMODE_MODE_HOLD); // Begin reading blocks for (i = 0; i < count; i++) { uint16_t crc, crc_exp; // Read the data token r = spi_readbyte(); - if (r != SD_DATA_TOKEN) { - print_uart("Didn't receive data token first thing. Shoot: "); - print_uart_byte(r & 0xff); - print_uart("\r\n"); - return -1; - } + /* if (r != SD_DATA_TOKEN) { */ + /* print_uart("Didn't receive data token first thing. Shoot: "); */ + /* print_uart_byte(r & 0xff); */ + /* print_uart("\r\n"); */ + /* return -1; */ + /* } */ + + // Wait for data token + while((r & 0xff) != SD_DATA_TOKEN); // Read block into memory. for (int j = 0; j < 8; j++) { @@ -77,7 +83,7 @@ int disk_read(BYTE * buf, LBA_t sector, UINT count) { crc_exp |= spi_txrx(0xff); if (crc != crc_exp) { - print_uart("Stinking CRC16 didn't match on block "); + print_uart("Stinking CRC16 didn't match on block read.\r\n"); print_uart_int(i); print_uart("\r\n"); return -1; @@ -86,7 +92,8 @@ int disk_read(BYTE * buf, LBA_t sector, UINT count) { } sd_cmd(SD_CMD_STOP_TRANSMISSION, 0, 0x01); - spi_txrx(0xff); + // write_reg(SPI_CSMODE, SIFIVE_SPI_CSMODE_MODE_AUTO); + //spi_txrx(0xff); return 0; } @@ -100,12 +107,17 @@ void copyFlash(QWORD address, QWORD * Dst, DWORD numBlocks) { // Initialize UART for messages init_uart(20000000, 115200); - print_uart("Booting wally.\r\n"); // Print the wally banner print_uart(BANNER); + /* print_uart("System clock speed: "); */ + /* print_uart_dec(SYSTEMCLOCK); */ + /* print_uart("\r\n"); */ + + println_with_dec("Hello, does this work? Here's the clock: ", SYSTEMCLOCK); + // Intialize the SD card - init_sd(); + init_sd(SYSTEMCLOCK, SYSTEMCLOCK/2); ret = gpt_load_partitions(); } diff --git a/fpga/zsbl/gpt.c b/fpga/zsbl/gpt.c index a52dc9048..9af396469 100644 --- a/fpga/zsbl/gpt.c +++ b/fpga/zsbl/gpt.c @@ -3,18 +3,7 @@ #include "uart.h" #include -/* PSUEDOCODE - - Need to load GPT LBA 1 and read through the partition entries. - I need to find each of the relevant partition entries, possibly - by their partition names. - -*/ - int gpt_load_partitions() { - // In this version of the GPT partition code - // I'm going to assume that the SD card is already initialized. - // size_t block_size = 512/8; // long int lba1_buf[block_size]; @@ -22,15 +11,12 @@ int gpt_load_partitions() { int ret = 0; //ret = disk_read(/* BYTE * buf, LBA_t sector, UINT count, BYTE card_type */); + println("Getting GPT information."); ret = disk_read(lba1_buf, 1, 1); - /* Possible error handling with UART message - if ( ret != 0 ) { - - }*/ - gpt_pth_t *lba1 = (gpt_pth_t *)lba1_buf; + println("Getting partition entries."); BYTE lba2_buf[512]; ret = disk_read(lba2_buf, (LBA_t)lba1->partition_entries_lba, 1); @@ -40,6 +26,7 @@ int gpt_load_partitions() { partition_entries_t *kernel = (partition_entries_t *)(lba2_buf + 256); // Load device tree + println_with_int("Loading device tree at: 0x", FDT_ADDRESS); ret = disk_read((BYTE *)FDT_ADDRESS, fdt->first_lba, fdt->last_lba - fdt->first_lba + 1); if (ret < 0) { print_uart("Failed to load device tree!\r\n"); @@ -47,6 +34,7 @@ int gpt_load_partitions() { } // Load OpenSBI + println_with_int("Loading OpenSBI at: 0x", OPENSBI_ADDRESS); ret = disk_read((BYTE *)OPENSBI_ADDRESS, opensbi->first_lba, opensbi->last_lba - opensbi->first_lba + 1); if (ret < 0) { print_uart("Failed to load OpenSBI!\r\n"); @@ -54,6 +42,7 @@ int gpt_load_partitions() { } // Load Linux + println_with_int("Loading Linux Kernel at: 0x", KERNEL_ADDRESS); ret = disk_read((BYTE *)KERNEL_ADDRESS, kernel->first_lba,kernel->last_lba - kernel->first_lba + 1); if (ret < 0) { print_uart("Failed to load Linux!\r\n"); diff --git a/fpga/zsbl/uart.h b/fpga/zsbl/uart.h index ee399885f..515a9dccd 100644 --- a/fpga/zsbl/uart.h +++ b/fpga/zsbl/uart.h @@ -1,6 +1,7 @@ #pragma once #include +// UART register addresses #define UART_BASE 0x10000000 #define UART_RBR UART_BASE + 0x00 @@ -16,6 +17,7 @@ #define UART_DLL UART_BASE + 0x00 #define UART_DLM UART_BASE + 0x01 +// Primary function prototypes void init_uart(uint32_t freq, uint32_t baud); void write_reg_u8(uintptr_t addr, uint8_t value); uint8_t read_reg_u8(uintptr_t addr); @@ -27,6 +29,7 @@ void print_uart_addr(uint64_t addr); void print_uart_hex(uint64_t addr, int n); void print_uart_byte(uint8_t byte); +// Print numbers in hex with specified widths #define print_uart_int(addr) print_uart_hex(addr, 4) #define print_uart_addr(addr) print_uart_hex(addr, 8) #define print_uart_byte(addr) print_uart_hex(addr, 1)