Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/src/core/bios.h
4214 views
1
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <[email protected]>.
2
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
3
4
#pragma once
5
6
#include "types.h"
7
8
#include "common/heap_array.h"
9
#include "common/small_string.h"
10
11
#include <array>
12
#include <optional>
13
#include <span>
14
#include <string>
15
#include <string_view>
16
#include <vector>
17
18
class Error;
19
20
namespace BIOS {
21
enum : u32
22
{
23
BIOS_BASE = 0x1FC00000,
24
BIOS_SIZE = 0x80000,
25
BIOS_SIZE_PS2 = 0x400000,
26
BIOS_SIZE_PS3 = 0x3E66F0
27
};
28
29
struct ImageInfo
30
{
31
static constexpr u32 HASH_SIZE = 16;
32
using Hash = std::array<u8, HASH_SIZE>;
33
34
enum class FastBootPatch : u8
35
{
36
Unsupported,
37
Type1,
38
Type2,
39
};
40
41
const char* description;
42
ConsoleRegion region;
43
bool region_check;
44
FastBootPatch fastboot_patch;
45
u8 priority;
46
alignas(16) Hash hash;
47
48
bool SupportsFastBoot() const { return (fastboot_patch != FastBootPatch::Unsupported); }
49
bool CanSlowBootDisc(DiscRegion disc_region) const;
50
51
static TinyString GetHashString(const Hash& hash);
52
};
53
54
struct Image
55
{
56
const ImageInfo* info;
57
ImageInfo::Hash hash;
58
DynamicHeapArray<u8> data;
59
};
60
61
#pragma pack(push, 1)
62
struct PSEXEHeader
63
{
64
char id[8]; // 0x000-0x007 PS-X EXE
65
char pad1[8]; // 0x008-0x00F
66
u32 initial_pc; // 0x010
67
u32 initial_gp; // 0x014
68
u32 load_address; // 0x018
69
u32 file_size; // 0x01C excluding 0x800-byte header
70
u32 unk0; // 0x020
71
u32 unk1; // 0x024
72
u32 memfill_start; // 0x028
73
u32 memfill_size; // 0x02C
74
u32 initial_sp_base; // 0x030
75
u32 initial_sp_offset; // 0x034
76
u32 reserved[5]; // 0x038-0x04B
77
char marker[0x7B4]; // 0x04C-0x7FF
78
};
79
static_assert(sizeof(PSEXEHeader) == 0x800);
80
#pragma pack(pop)
81
82
// .cpe files
83
inline constexpr u32 CPE_MAGIC = 0x01455043;
84
85
std::optional<Image> LoadImageFromFile(const char* filename, Error* error);
86
87
const ImageInfo* GetInfoForHash(const std::span<const u8> image, const ImageInfo::Hash& hash);
88
89
bool IsValidBIOSForRegion(ConsoleRegion console_region, ConsoleRegion bios_region);
90
91
bool PatchBIOSFastBoot(u8* image, u32 image_size, ImageInfo::FastBootPatch type);
92
93
bool IsValidPSExeHeader(const PSEXEHeader& header, size_t file_size);
94
DiscRegion GetPSExeDiscRegion(const PSEXEHeader& header);
95
96
/// Loads the BIOS image for the specified region.
97
std::optional<Image> GetBIOSImage(ConsoleRegion region, Error* error);
98
99
/// Searches for a BIOS image for the specified region in the specified directory. If no match is found, the first
100
/// BIOS image within 512KB and 4MB will be used.
101
std::optional<Image> FindBIOSImageInDirectory(ConsoleRegion region, const char* directory, Error* error);
102
103
/// Returns a list of filenames and descriptions for BIOS images in a directory.
104
std::vector<std::pair<std::string, const BIOS::ImageInfo*>> FindBIOSImagesInDirectory(const char* directory);
105
106
/// Returns true if any BIOS images are found in the configured BIOS directory.
107
bool HasAnyBIOSImages();
108
} // namespace BIOS
109
110