2024-08-06 22:28:50 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
// gpt.c
|
|
|
|
//
|
|
|
|
// Written: Jaocb Pease jacob.pease@okstate.edu 7/22/2024
|
|
|
|
//
|
|
|
|
// Purpose: Code to read GPT Partitions off of an SD card.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// A component of the Wally configurable RISC-V project.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2021-23 Harvey Mudd College & Oklahoma State University
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH SHL-2.1
|
|
|
|
//
|
|
|
|
// Licensed under the Solderpad Hardware License v 2.1 (the
|
|
|
|
// “License”); you may not use this file except in compliance with the
|
|
|
|
// License, or, at your option, the Apache License version 2.0. You
|
|
|
|
// may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// https://solderpad.org/licenses/SHL-2.1/
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, any work
|
|
|
|
// distributed under the License is distributed on an “AS IS” BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
|
|
// implied. See the License for the specific language governing
|
|
|
|
// permissions and limitations under the License.
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
|
2022-11-03 22:35:57 +00:00
|
|
|
#include "gpt.h"
|
2024-05-31 20:28:25 +00:00
|
|
|
#include "boot.h"
|
2024-07-25 03:43:16 +00:00
|
|
|
#include "uart.h"
|
2022-11-03 22:35:57 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
|
2024-07-23 20:46:18 +00:00
|
|
|
int gpt_load_partitions() {
|
2024-05-31 20:28:25 +00:00
|
|
|
// size_t block_size = 512/8;
|
|
|
|
// long int lba1_buf[block_size];
|
2022-11-03 22:35:57 +00:00
|
|
|
|
2024-05-31 20:28:25 +00:00
|
|
|
BYTE lba1_buf[512];
|
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
//ret = disk_read(/* BYTE * buf, LBA_t sector, UINT count, BYTE card_type */);
|
2024-08-02 20:19:52 +00:00
|
|
|
print_time();
|
2024-07-31 16:12:05 +00:00
|
|
|
println("Getting GPT information.");
|
2024-07-23 20:46:18 +00:00
|
|
|
ret = disk_read(lba1_buf, 1, 1);
|
2022-11-03 22:35:57 +00:00
|
|
|
|
2024-05-31 20:28:25 +00:00
|
|
|
gpt_pth_t *lba1 = (gpt_pth_t *)lba1_buf;
|
2022-11-03 22:35:57 +00:00
|
|
|
|
2024-08-02 20:19:52 +00:00
|
|
|
print_time();
|
2024-07-31 16:12:05 +00:00
|
|
|
println("Getting partition entries.");
|
2024-05-31 20:28:25 +00:00
|
|
|
BYTE lba2_buf[512];
|
2024-07-23 20:46:18 +00:00
|
|
|
ret = disk_read(lba2_buf, (LBA_t)lba1->partition_entries_lba, 1);
|
2022-11-03 22:35:57 +00:00
|
|
|
|
2024-05-31 20:28:25 +00:00
|
|
|
// Load parition entries for the relevant boot partitions.
|
|
|
|
partition_entries_t *fdt = (partition_entries_t *)(lba2_buf);
|
|
|
|
partition_entries_t *opensbi = (partition_entries_t *)(lba2_buf + 128);
|
|
|
|
partition_entries_t *kernel = (partition_entries_t *)(lba2_buf + 256);
|
2022-11-03 22:35:57 +00:00
|
|
|
|
2024-07-23 21:32:52 +00:00
|
|
|
// Load device tree
|
2024-08-02 20:19:52 +00:00
|
|
|
print_time();
|
2024-07-31 16:12:05 +00:00
|
|
|
println_with_int("Loading device tree at: 0x", FDT_ADDRESS);
|
2024-07-23 20:46:18 +00:00
|
|
|
ret = disk_read((BYTE *)FDT_ADDRESS, fdt->first_lba, fdt->last_lba - fdt->first_lba + 1);
|
2024-07-23 21:32:52 +00:00
|
|
|
if (ret < 0) {
|
2024-07-25 18:05:57 +00:00
|
|
|
print_uart("Failed to load device tree!\r\n");
|
2024-07-23 21:32:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load OpenSBI
|
2024-08-02 20:19:52 +00:00
|
|
|
print_time();
|
2024-07-31 16:12:05 +00:00
|
|
|
println_with_int("Loading OpenSBI at: 0x", OPENSBI_ADDRESS);
|
2024-07-23 20:46:18 +00:00
|
|
|
ret = disk_read((BYTE *)OPENSBI_ADDRESS, opensbi->first_lba, opensbi->last_lba - opensbi->first_lba + 1);
|
2024-07-23 21:32:52 +00:00
|
|
|
if (ret < 0) {
|
2024-07-25 18:05:57 +00:00
|
|
|
print_uart("Failed to load OpenSBI!\r\n");
|
2024-07-23 21:32:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load Linux
|
2024-08-02 20:19:52 +00:00
|
|
|
print_time();
|
2024-07-31 16:12:05 +00:00
|
|
|
println_with_int("Loading Linux Kernel at: 0x", KERNEL_ADDRESS);
|
2024-07-23 20:46:18 +00:00
|
|
|
ret = disk_read((BYTE *)KERNEL_ADDRESS, kernel->first_lba,kernel->last_lba - kernel->first_lba + 1);
|
2024-07-23 21:32:52 +00:00
|
|
|
if (ret < 0) {
|
2024-07-25 18:05:57 +00:00
|
|
|
print_uart("Failed to load Linux!\r\n");
|
2024-07-23 21:32:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2022-11-03 22:35:57 +00:00
|
|
|
|
2024-08-02 20:19:52 +00:00
|
|
|
print_time();
|
|
|
|
println("Done! Flashing LEDs and jumping to OpenSBI...");
|
|
|
|
|
2024-05-31 20:28:25 +00:00
|
|
|
return 0;
|
2022-11-03 22:35:57 +00:00
|
|
|
}
|