Skip to content

Commit

Permalink
Implemented smbus cycles + connected eeprom to smbus
Browse files Browse the repository at this point in the history
  • Loading branch information
ergo720 committed Aug 19, 2024
1 parent 46b9891 commit cac11f5
Show file tree
Hide file tree
Showing 8 changed files with 311 additions and 58 deletions.
2 changes: 1 addition & 1 deletion src/files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,12 @@ open_file(std::filesystem::path path)
std::optional<std::fstream>
open_file(std::filesystem::path path, std::uintmax_t *size)
{
*size = 0;
if (auto opt = open_file(path)) {
try {
*size = std::filesystem::file_size(path);
}
catch (const std::exception &e) {
*size = 0;
logger_en(info, "Failed to determine the file size of path %s, the error was %s", path.string().c_str(), e.what());
}
return opt;
Expand Down
86 changes: 78 additions & 8 deletions src/hw/eeprom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
// SPDX-FileCopyrightText: 2023 ergo720

#include "eeprom.hpp"
#include "../files.hpp"
#include <cstdint>
#include <cinttypes>
#include <cstring>

#define MODULE_NAME eeprom


// This is bunnie's eeprom, except that it stores the encrypted settings unencrypted, because nboxkrnl cannot decrypt them yet
constexpr uint8_t default_eeprom[] = {
static constexpr uint8_t g_default_eeprom[] = {
0xe3, 0x1c, 0x5c, 0x23, 0x6a, 0x58, 0x68, 0x37,
0xb7, 0x12, 0x26, 0x6c, 0x99, 0x11, 0x30, 0xd1,
0xe2, 0x3e, 0x4d, 0x56, 0x00, 0x00, 0x00, 0x00,
Expand Down Expand Up @@ -41,17 +46,82 @@ constexpr uint8_t default_eeprom[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
static_assert(sizeof(g_default_eeprom) == 256);


std::optional<uint16_t>
eeprom::read_byte(uint8_t command)
{
return m_eeprom[command];
}

std::optional<uint16_t>
eeprom::write_byte(uint8_t command, uint8_t data)
{
m_eeprom[command] = data;
return 0;
}

std::optional<uint16_t>
eeprom::read_word(uint8_t command)
{
return m_eeprom[command] | (((uint16_t)m_eeprom[command + 1]) << 8);
}

std::optional<uint16_t>
eeprom::write_word(uint8_t command, uint16_t data)
{
m_eeprom[command] = data & 0xFF;
m_eeprom[command + 1] = data >> 8;
return 0;
}

void
eeprom::deinit()
{
m_fs.seekg(0);
m_fs.write((const char *)m_eeprom, 256);
if (!m_fs.good()) {
logger_en(error, "Failed to flush eeprom file to disk");
}
}

bool
gen_eeprom(std::fstream fs)
eeprom::init(std::filesystem::path eeprom_dir)
{
fs.seekg(0, fs.beg);
fs.write((const char *)default_eeprom, sizeof(default_eeprom));
if (fs.rdstate() != std::ios_base::goodbit) {
fs.clear();
uintmax_t size;
eeprom_dir = eeprom_dir.remove_filename();
eeprom_dir /= "eeprom.bin";
eeprom_dir.make_preferred();
if (auto opt = open_file(eeprom_dir, &size); !opt) {
if (auto opt = create_file(eeprom_dir); !opt) {
logger_en(error, "Failed to create eeprom file");
return false;
}
else {
m_fs = std::move(*opt);
m_fs.seekg(0);
m_fs.write((const char *)g_default_eeprom, 256);
if (m_fs.good()) {
std::memcpy(m_eeprom, g_default_eeprom, 256);
return true;
}
logger_en(error, "Failed to update eeprom file");
return false;
}
}
else {
if (size != 256) {
logger_en(error, "Unexpected eeprom file size (it was %" PRIuMAX ")", size);
return false;
}
m_fs = std::move(*opt);
m_fs.seekg(0);
m_fs.read((char *)m_eeprom, 256);
if (m_fs.good()) {
return true;
}
logger_en(error, "Failed to copy eeprom file to memory");
return false;
}

return true;
}
17 changes: 16 additions & 1 deletion src/hw/eeprom.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,22 @@

// SPDX-FileCopyrightText: 2023 ergo720

#include "smbus.hpp"
#include <fstream>
#include <filesystem>


bool gen_eeprom(std::fstream fs);
class eeprom : public smbus_device {
public:
eeprom(log_module module_name) : smbus_device(module_name) {}
bool init(std::filesystem::path eeprom_dir);
void deinit() override;
std::optional<uint16_t> read_byte(uint8_t command) override;
std::optional<uint16_t> write_byte(uint8_t command, uint8_t data) override;
std::optional<uint16_t> read_word(uint8_t command) override;
std::optional<uint16_t> write_word(uint8_t command, uint16_t data) override;

private:
std::fstream m_fs;
uint8_t m_eeprom[256];
};
10 changes: 9 additions & 1 deletion src/hw/machine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "cmos.hpp"
#include "pci.hpp"
#include "smbus.hpp"
#include "eeprom.hpp"
#include "video/vga.hpp"
#include "video/gpu/nv2a.hpp"

Expand All @@ -21,7 +22,7 @@ concept is_cpu_t = std::is_same_v<T, cpu_t *>;
class machine {
public:
machine() : m_cpu(this), m_pit(this), m_pic{ {this, 0, "MASTER PIC"}, {this, 1, "SLAVE PIC"} }, m_pci(this), m_cmos(this), m_nv2a(this),
m_vga(this), m_smbus(this) {}
m_vga(this), m_smbus(this), m_eeprom(log_module::eeprom) {}
bool init(const init_info_t &init_info)
{
if (!m_cpu.init(init_info)) {
Expand Down Expand Up @@ -51,6 +52,9 @@ class machine {
if (!m_smbus.init()) {
return false;
}
if (!m_eeprom.init(init_info.m_nxbx_path)) {
return false;
}
return true;
}
void deinit()
Expand Down Expand Up @@ -91,6 +95,9 @@ class machine {
else if constexpr (std::is_same_v<T, smbus>) {
return m_smbus;
}
else if constexpr (std::is_same_v<T, eeprom>) {
return m_eeprom;
}
else if constexpr (std::is_same_v<T, nv2a>) {
return m_nv2a;
}
Expand Down Expand Up @@ -171,4 +178,5 @@ class machine {
nv2a m_nv2a;
vga m_vga;
smbus m_smbus;
eeprom m_eeprom;
};
Loading

0 comments on commit cac11f5

Please sign in to comment.