duckstation

duckstation, but archived from the revision just before upstream changed it to a proprietary software project, this version is the libre one
git clone https://git.neptards.moe/u3shit/duckstation.git
Log | Files | Refs | README | LICENSE

bios.h (2776B)


      1 // SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>.
      2 // SPDX-License-Identifier: (GPL-3.0 OR PolyForm-Strict-1.0.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   Hash hash;
     44   FastBootPatch fastboot_patch;
     45   u8 priority;
     46 
     47   bool SupportsFastBoot() const { return (fastboot_patch != FastBootPatch::Unsupported); }
     48 
     49   static TinyString GetHashString(const Hash& hash);
     50 };
     51 
     52 struct Image
     53 {
     54   const ImageInfo* info;
     55   ImageInfo::Hash hash;
     56   DynamicHeapArray<u8> data;
     57 };
     58 
     59 #pragma pack(push, 1)
     60 struct PSEXEHeader
     61 {
     62   char id[8];            // 0x000-0x007 PS-X EXE
     63   char pad1[8];          // 0x008-0x00F
     64   u32 initial_pc;        // 0x010
     65   u32 initial_gp;        // 0x014
     66   u32 load_address;      // 0x018
     67   u32 file_size;         // 0x01C excluding 0x800-byte header
     68   u32 unk0;              // 0x020
     69   u32 unk1;              // 0x024
     70   u32 memfill_start;     // 0x028
     71   u32 memfill_size;      // 0x02C
     72   u32 initial_sp_base;   // 0x030
     73   u32 initial_sp_offset; // 0x034
     74   u32 reserved[5];       // 0x038-0x04B
     75   char marker[0x7B4];    // 0x04C-0x7FF
     76 };
     77 static_assert(sizeof(PSEXEHeader) == 0x800);
     78 #pragma pack(pop)
     79 
     80 std::optional<Image> LoadImageFromFile(const char* filename, Error* error);
     81 
     82 bool IsValidBIOSForRegion(ConsoleRegion console_region, ConsoleRegion bios_region);
     83 
     84 bool PatchBIOSFastBoot(u8* image, u32 image_size, ImageInfo::FastBootPatch type);
     85 
     86 bool IsValidPSExeHeader(const PSEXEHeader& header, size_t file_size);
     87 DiscRegion GetPSExeDiscRegion(const PSEXEHeader& header);
     88 
     89 /// Loads the BIOS image for the specified region.
     90 std::optional<Image> GetBIOSImage(ConsoleRegion region, Error* error);
     91 
     92 /// Searches for a BIOS image for the specified region in the specified directory. If no match is found, the first
     93 /// BIOS image within 512KB and 4MB will be used.
     94 std::optional<Image> FindBIOSImageInDirectory(ConsoleRegion region, const char* directory, Error* error);
     95 
     96 /// Returns a list of filenames and descriptions for BIOS images in a directory.
     97 std::vector<std::pair<std::string, const BIOS::ImageInfo*>> FindBIOSImagesInDirectory(const char* directory);
     98 
     99 /// Returns true if any BIOS images are found in the configured BIOS directory.
    100 bool HasAnyBIOSImages();
    101 } // namespace BIOS