Updated formatting of gpt.c and boot.c.

This commit is contained in:
Jacob Pease 2024-07-31 11:12:05 -05:00
parent 0396181d1e
commit 906fa73747
3 changed files with 34 additions and 30 deletions

View File

@ -34,6 +34,8 @@
/* return 0;; */ /* return 0;; */
/* } */ /* } */
#define SYSTEMCLOCK 20000000
int disk_read(BYTE * buf, LBA_t sector, UINT count) { int disk_read(BYTE * buf, LBA_t sector, UINT count) {
uint64_t r; uint64_t r;
UINT i; UINT i;
@ -46,25 +48,29 @@ int disk_read(BYTE * buf, LBA_t sector, UINT count) {
crc = crc7(crc, sector & 0xff); crc = crc7(crc, sector & 0xff);
crc = crc | 1; crc = crc | 1;
if (sd_cmd(18, sector & 0xffffffff, crc) != 0x00) { if ((r = sd_cmd(18, sector & 0xffffffff, crc) & 0xff) != 0x00) {
print_uart("disk_read: CMD18 failed. r = "); print_uart("disk_read: CMD18 failed. r = 0x");
print_uart_byte(r & 0xff); print_uart_byte(r);
print_uart("\r\n"); print_uart("\r\n");
return -1; return -1;
} }
// write_reg(SPI_CSMODE, SIFIVE_SPI_CSMODE_MODE_HOLD);
// Begin reading blocks // Begin reading blocks
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
uint16_t crc, crc_exp; uint16_t crc, crc_exp;
// Read the data token // Read the data token
r = spi_readbyte(); r = spi_readbyte();
if (r != SD_DATA_TOKEN) { /* if (r != SD_DATA_TOKEN) { */
print_uart("Didn't receive data token first thing. Shoot: "); /* print_uart("Didn't receive data token first thing. Shoot: "); */
print_uart_byte(r & 0xff); /* print_uart_byte(r & 0xff); */
print_uart("\r\n"); /* print_uart("\r\n"); */
return -1; /* return -1; */
} /* } */
// Wait for data token
while((r & 0xff) != SD_DATA_TOKEN);
// Read block into memory. // Read block into memory.
for (int j = 0; j < 8; j++) { 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); crc_exp |= spi_txrx(0xff);
if (crc != crc_exp) { 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_int(i);
print_uart("\r\n"); print_uart("\r\n");
return -1; return -1;
@ -86,7 +92,8 @@ int disk_read(BYTE * buf, LBA_t sector, UINT count) {
} }
sd_cmd(SD_CMD_STOP_TRANSMISSION, 0, 0x01); 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; return 0;
} }
@ -100,12 +107,17 @@ void copyFlash(QWORD address, QWORD * Dst, DWORD numBlocks) {
// Initialize UART for messages // Initialize UART for messages
init_uart(20000000, 115200); init_uart(20000000, 115200);
print_uart("Booting wally.\r\n");
// Print the wally banner // Print the wally banner
print_uart(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 // Intialize the SD card
init_sd(); init_sd(SYSTEMCLOCK, SYSTEMCLOCK/2);
ret = gpt_load_partitions(); ret = gpt_load_partitions();
} }

View File

@ -3,18 +3,7 @@
#include "uart.h" #include "uart.h"
#include <stddef.h> #include <stddef.h>
/* 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() { 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; // size_t block_size = 512/8;
// long int lba1_buf[block_size]; // long int lba1_buf[block_size];
@ -22,15 +11,12 @@ int gpt_load_partitions() {
int ret = 0; int ret = 0;
//ret = disk_read(/* BYTE * buf, LBA_t sector, UINT count, BYTE card_type */); //ret = disk_read(/* BYTE * buf, LBA_t sector, UINT count, BYTE card_type */);
println("Getting GPT information.");
ret = disk_read(lba1_buf, 1, 1); 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; gpt_pth_t *lba1 = (gpt_pth_t *)lba1_buf;
println("Getting partition entries.");
BYTE lba2_buf[512]; BYTE lba2_buf[512];
ret = disk_read(lba2_buf, (LBA_t)lba1->partition_entries_lba, 1); 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); partition_entries_t *kernel = (partition_entries_t *)(lba2_buf + 256);
// Load device tree // 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); ret = disk_read((BYTE *)FDT_ADDRESS, fdt->first_lba, fdt->last_lba - fdt->first_lba + 1);
if (ret < 0) { if (ret < 0) {
print_uart("Failed to load device tree!\r\n"); print_uart("Failed to load device tree!\r\n");
@ -47,6 +34,7 @@ int gpt_load_partitions() {
} }
// Load OpenSBI // 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); ret = disk_read((BYTE *)OPENSBI_ADDRESS, opensbi->first_lba, opensbi->last_lba - opensbi->first_lba + 1);
if (ret < 0) { if (ret < 0) {
print_uart("Failed to load OpenSBI!\r\n"); print_uart("Failed to load OpenSBI!\r\n");
@ -54,6 +42,7 @@ int gpt_load_partitions() {
} }
// Load Linux // 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); ret = disk_read((BYTE *)KERNEL_ADDRESS, kernel->first_lba,kernel->last_lba - kernel->first_lba + 1);
if (ret < 0) { if (ret < 0) {
print_uart("Failed to load Linux!\r\n"); print_uart("Failed to load Linux!\r\n");

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <stdint.h> #include <stdint.h>
// UART register addresses
#define UART_BASE 0x10000000 #define UART_BASE 0x10000000
#define UART_RBR UART_BASE + 0x00 #define UART_RBR UART_BASE + 0x00
@ -16,6 +17,7 @@
#define UART_DLL UART_BASE + 0x00 #define UART_DLL UART_BASE + 0x00
#define UART_DLM UART_BASE + 0x01 #define UART_DLM UART_BASE + 0x01
// Primary function prototypes
void init_uart(uint32_t freq, uint32_t baud); void init_uart(uint32_t freq, uint32_t baud);
void write_reg_u8(uintptr_t addr, uint8_t value); void write_reg_u8(uintptr_t addr, uint8_t value);
uint8_t read_reg_u8(uintptr_t addr); 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_hex(uint64_t addr, int n);
void print_uart_byte(uint8_t byte); 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_int(addr) print_uart_hex(addr, 4)
#define print_uart_addr(addr) print_uart_hex(addr, 8) #define print_uart_addr(addr) print_uart_hex(addr, 8)
#define print_uart_byte(addr) print_uart_hex(addr, 1) #define print_uart_byte(addr) print_uart_hex(addr, 1)