#pragma once
#include "types.h"
#include "common/heap_array.h"
#include "common/small_string.h"
#include <array>
#include <optional>
#include <span>
#include <string>
#include <string_view>
#include <vector>
class Error;
namespace BIOS {
enum : u32
{
BIOS_BASE = 0x1FC00000,
BIOS_SIZE = 0x80000,
BIOS_SIZE_PS2 = 0x400000,
BIOS_SIZE_PS3 = 0x3E66F0
};
struct ImageInfo
{
static constexpr u32 HASH_SIZE = 16;
using Hash = std::array<u8, HASH_SIZE>;
enum class FastBootPatch : u8
{
Unsupported,
Type1,
Type2,
};
const char* description;
ConsoleRegion region;
bool region_check;
FastBootPatch fastboot_patch;
u8 priority;
alignas(16) Hash hash;
bool SupportsFastBoot() const { return (fastboot_patch != FastBootPatch::Unsupported); }
bool CanSlowBootDisc(DiscRegion disc_region) const;
static TinyString GetHashString(const Hash& hash);
};
struct Image
{
const ImageInfo* info;
ImageInfo::Hash hash;
DynamicHeapArray<u8> data;
};
#pragma pack(push, 1)
struct PSEXEHeader
{
char id[8];
char pad1[8];
u32 initial_pc;
u32 initial_gp;
u32 load_address;
u32 file_size;
u32 unk0;
u32 unk1;
u32 memfill_start;
u32 memfill_size;
u32 initial_sp_base;
u32 initial_sp_offset;
u32 reserved[5];
char marker[0x7B4];
};
static_assert(sizeof(PSEXEHeader) == 0x800);
#pragma pack(pop)
inline constexpr u32 CPE_MAGIC = 0x01455043;
std::optional<Image> LoadImageFromFile(const char* filename, Error* error);
const ImageInfo* GetInfoForHash(const std::span<const u8> image, const ImageInfo::Hash& hash);
bool IsValidBIOSForRegion(ConsoleRegion console_region, ConsoleRegion bios_region);
bool PatchBIOSFastBoot(u8* image, u32 image_size, ImageInfo::FastBootPatch type);
bool IsValidPSExeHeader(const PSEXEHeader& header, size_t file_size);
DiscRegion GetPSExeDiscRegion(const PSEXEHeader& header);
std::optional<Image> GetBIOSImage(ConsoleRegion region, Error* error);
std::optional<Image> FindBIOSImageInDirectory(ConsoleRegion region, const char* directory, Error* error);
std::vector<std::pair<std::string, const BIOS::ImageInfo*>> FindBIOSImagesInDirectory(const char* directory);
bool HasAnyBIOSImages();
}