mirror of
https://github.com/LNH-team/pico-loader.git
synced 2026-06-02 09:16:49 +02:00
Initial work on patch list
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
#include "common.h"
|
||||
#include "ApListFactory.h"
|
||||
|
||||
ApList* ApListFactory::CreateFromFile(const TCHAR *path)
|
||||
std::unique_ptr<ApList> ApListFactory::CreateFromFile(const TCHAR* path)
|
||||
{
|
||||
FIL file;
|
||||
if (f_open(&file, path, FA_OPEN_EXISTING | FA_READ) != FR_OK)
|
||||
@@ -20,5 +20,5 @@ ApList* ApListFactory::CreateFromFile(const TCHAR *path)
|
||||
}
|
||||
f_close(&file);
|
||||
|
||||
return new ApList(std::move(entries), entryCount);
|
||||
return std::make_unique<ApList>(std::move(entries), entryCount);
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#include <memory>
|
||||
#include "ApList.h"
|
||||
|
||||
/// @brief Factory for creating \see ApList instances.
|
||||
@@ -8,5 +9,5 @@ public:
|
||||
/// @brief Creates an \see ApList instance from the file at the given \p path.
|
||||
/// @param path The ap list file path.
|
||||
/// @return A pointer to the constructed \see ApList instance, or \c nullptr if construction failed.
|
||||
ApList* CreateFromFile(const TCHAR* path);
|
||||
std::unique_ptr<ApList> CreateFromFile(const TCHAR* path);
|
||||
};
|
||||
|
||||
@@ -29,7 +29,7 @@ bool CardSaveArranger::SetupCardSave(const nds_header_ntr_t* header, const TCHAR
|
||||
}
|
||||
else
|
||||
{
|
||||
SaveList* saveList = SaveListFactory().CreateFromFile(SAVE_LIST_PATH);
|
||||
auto saveList = SaveListFactory().CreateFromFile(SAVE_LIST_PATH);
|
||||
if (saveList)
|
||||
{
|
||||
const auto saveListEntry = saveList->FindEntry(header->gameCode);
|
||||
@@ -44,7 +44,6 @@ bool CardSaveArranger::SetupCardSave(const nds_header_ntr_t* header, const TCHAR
|
||||
saveSize = saveListEntry->GetSaveSize();
|
||||
saveListEntry->Dump();
|
||||
}
|
||||
delete saveList;
|
||||
}
|
||||
}
|
||||
if (saveSize == 0)
|
||||
|
||||
@@ -26,9 +26,11 @@
|
||||
#include "TwlAes.h"
|
||||
#include "DSMode.h"
|
||||
#include "Arm7IoRegisterClearer.h"
|
||||
#include "PatchListFactory.h"
|
||||
#include "NdsLoader.h"
|
||||
|
||||
#define AP_LIST_PATH "/_pico/aplist.bin"
|
||||
#define PATCH_LIST_PATH "/_pico/patchlist.bin"
|
||||
#define BIOS_NDS7_PATH "/_pico/biosnds7.rom"
|
||||
|
||||
typedef void (*entrypoint_t)(void);
|
||||
@@ -290,6 +292,8 @@ void NdsLoader::Load(BootMode bootMode)
|
||||
// wait for arm9 patches to be ready
|
||||
receiveFromArm9();
|
||||
|
||||
HandleGameSpecificPatches();
|
||||
|
||||
LOG_DEBUG("Arm9 patches done\n");
|
||||
|
||||
ApplyArm7Patches();
|
||||
@@ -438,6 +442,63 @@ void NdsLoader::InsertArgv()
|
||||
HOMEBREW_ARGV->length = argSize;
|
||||
}
|
||||
|
||||
void NdsLoader::HandleGameSpecificPatches()
|
||||
{
|
||||
auto patchList = PatchListFactory().CreateFromFile(PATCH_LIST_PATH);
|
||||
if (patchList)
|
||||
{
|
||||
auto entry = patchList->FindEntry(_romHeader.gameCode, _romHeader.softwareVersion);
|
||||
if (entry)
|
||||
{
|
||||
auto patch = &entry->firstPatch;
|
||||
for (int patchIndex = 0; patchIndex < entry->patchCount; patchIndex++)
|
||||
{
|
||||
switch (patch->common.patchType)
|
||||
{
|
||||
case PatchListPatchType::Replace:
|
||||
{
|
||||
LOG_DEBUG("Applying replace patch. %d bytes to 0x%08X\n",
|
||||
patch->replacePatch.dataLength, patch->replacePatch.address);
|
||||
memcpy((void*)patch->replacePatch.address, patch->replacePatch.data, patch->replacePatch.dataLength);
|
||||
patch = (const PatchListEntryPatch*)&patch->replacePatch.data[(patch->replacePatch.dataLength + 3) & ~3];
|
||||
break;
|
||||
}
|
||||
case PatchListPatchType::Metafortress:
|
||||
{
|
||||
LOG_DEBUG("Applying Metafortress patch. %d addresses\n", patch->metafortressPatch.addressCount);
|
||||
auto addresses = patch->metafortressPatch.addresses;
|
||||
for (uint32_t addressIndex = 0; addressIndex < patch->metafortressPatch.addressCount; addressIndex++)
|
||||
{
|
||||
uint32_t address = addresses[addressIndex];
|
||||
if ((address & 1) != 0)
|
||||
{
|
||||
// thumb
|
||||
*(u16*)(address & ~1) = 0x4280; // cmp r0, r0
|
||||
}
|
||||
else
|
||||
{
|
||||
// arm
|
||||
*(u32*)address = 0xE1500000; // cmp r0, r0
|
||||
}
|
||||
}
|
||||
patch = (const PatchListEntryPatch*)&addresses[patch->metafortressPatch.addressCount];
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
LOG_ERROR("Unknown patch type\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_DEBUG("No entry found in patch list\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NdsLoader::HandleHomebrewPatching()
|
||||
{
|
||||
if (_launcherPath != nullptr && _launcherPath[0] != 0)
|
||||
@@ -660,8 +721,6 @@ void NdsLoader::HandleAntiPiracy()
|
||||
{
|
||||
LOG_DEBUG("No entry found in ap list\n");
|
||||
}
|
||||
|
||||
delete apList;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -79,6 +79,7 @@ private:
|
||||
char driveLetter, const char* deviceName, const char* path, u8 flags, u8 accessRights);
|
||||
void SetupDsiDeviceList();
|
||||
void InsertArgv();
|
||||
void HandleGameSpecificPatches();
|
||||
void HandleHomebrewPatching();
|
||||
bool TrySetupDsiWareSave();
|
||||
bool TryDecryptSecureArea();
|
||||
|
||||
32
arm7/source/loader/PatchList.cpp
Normal file
32
arm7/source/loader/PatchList.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include "common.h"
|
||||
#include <algorithm>
|
||||
#include "PatchList.h"
|
||||
|
||||
const PatchListEntry* PatchList::FindEntry(u32 gameCode, u8 gameVersion)
|
||||
{
|
||||
auto header = reinterpret_cast<const PatchListHeader*>(_fileContents.get());
|
||||
u32 count = header->entryCount;
|
||||
auto entries = header->headerEntries;
|
||||
if (count != 0)
|
||||
{
|
||||
const auto gameEntry = std::lower_bound(entries, entries + count, gameCode,
|
||||
[gameVersion] (const PatchListHeaderEntry& entry, u32 value)
|
||||
{
|
||||
if (entry.gameCode == value)
|
||||
{
|
||||
return entry.gameVersion < gameVersion;
|
||||
}
|
||||
else
|
||||
{
|
||||
return entry.gameCode < value;
|
||||
}
|
||||
});
|
||||
|
||||
if (gameEntry != entries + count && gameEntry->gameCode == gameCode && gameEntry->gameVersion == gameVersion)
|
||||
{
|
||||
return reinterpret_cast<const PatchListEntry*>(_fileContents.get() + gameEntry->offset);
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
71
arm7/source/loader/PatchList.h
Normal file
71
arm7/source/loader/PatchList.h
Normal file
@@ -0,0 +1,71 @@
|
||||
#pragma once
|
||||
#include <memory>
|
||||
|
||||
enum class PatchListPatchType : u8
|
||||
{
|
||||
Replace,
|
||||
Metafortress
|
||||
};
|
||||
|
||||
struct PatchListHeaderEntry
|
||||
{
|
||||
u32 gameCode;
|
||||
u32 gameVersion : 8;
|
||||
u32 offset : 24;
|
||||
};
|
||||
|
||||
struct PatchListHeader
|
||||
{
|
||||
u32 entryCount;
|
||||
PatchListHeaderEntry headerEntries[1];
|
||||
};
|
||||
|
||||
union PatchListEntryPatch
|
||||
{
|
||||
struct
|
||||
{
|
||||
PatchListPatchType patchType;
|
||||
u8 reserved;
|
||||
} common;
|
||||
struct
|
||||
{
|
||||
PatchListPatchType patchType;
|
||||
u8 reserved;
|
||||
u16 dataLength;
|
||||
u32 address;
|
||||
u8 data[1];
|
||||
} replacePatch;
|
||||
struct
|
||||
{
|
||||
PatchListPatchType patchType;
|
||||
u8 reserved;
|
||||
u16 addressCount;
|
||||
u32 addresses[1];
|
||||
} metafortressPatch;
|
||||
};
|
||||
|
||||
static_assert(offsetof(PatchListEntryPatch, replacePatch.dataLength) == 2);
|
||||
static_assert(offsetof(PatchListEntryPatch, replacePatch.address) == 4);
|
||||
static_assert(offsetof(PatchListEntryPatch, replacePatch.data) == 8);
|
||||
static_assert(offsetof(PatchListEntryPatch, metafortressPatch.addressCount) == 2);
|
||||
static_assert(offsetof(PatchListEntryPatch, metafortressPatch.addresses) == 4);
|
||||
|
||||
struct PatchListEntry
|
||||
{
|
||||
u16 length;
|
||||
u16 patchCount;
|
||||
PatchListEntryPatch firstPatch;
|
||||
};
|
||||
|
||||
/// @brief Class representing a patch list.
|
||||
class PatchList
|
||||
{
|
||||
public:
|
||||
PatchList(std::unique_ptr<const u8[]> fileContents)
|
||||
: _fileContents(std::move(fileContents)) { }
|
||||
|
||||
const PatchListEntry* FindEntry(u32 gameCode, u8 gameVersion);
|
||||
|
||||
private:
|
||||
std::unique_ptr<const u8[]> _fileContents;
|
||||
};
|
||||
24
arm7/source/loader/PatchListFactory.cpp
Normal file
24
arm7/source/loader/PatchListFactory.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include "common.h"
|
||||
#include "PatchListFactory.h"
|
||||
|
||||
std::unique_ptr<PatchList> PatchListFactory::CreateFromFile(const TCHAR* path)
|
||||
{
|
||||
FIL file;
|
||||
if (f_open(&file, path, FA_OPEN_EXISTING | FA_READ) != FR_OK)
|
||||
{
|
||||
LOG_FATAL("Failed to open patch list file\n");
|
||||
return nullptr;
|
||||
}
|
||||
u32 fileSize = f_size(&file);
|
||||
auto fileContents = std::make_unique_for_overwrite<u8[]>(fileSize);
|
||||
UINT bytesRead = 0;
|
||||
FRESULT result = f_read(&file, fileContents.get(), fileSize, &bytesRead);
|
||||
if (result != FR_OK || bytesRead != fileSize)
|
||||
{
|
||||
LOG_FATAL("Failed to read patch list file\n");
|
||||
return nullptr;
|
||||
}
|
||||
f_close(&file);
|
||||
|
||||
return std::make_unique<PatchList>(std::move(fileContents));
|
||||
}
|
||||
12
arm7/source/loader/PatchListFactory.h
Normal file
12
arm7/source/loader/PatchListFactory.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
#include "PatchList.h"
|
||||
|
||||
/// @brief Factory for creating \see ApList instances.
|
||||
class PatchListFactory
|
||||
{
|
||||
public:
|
||||
/// @brief Creates an \see PatchList instance from the file at the given \p path.
|
||||
/// @param path The patch list file path.
|
||||
/// @return A pointer to the constructed \see PatchList instance, or \c nullptr if construction failed.
|
||||
std::unique_ptr<PatchList> CreateFromFile(const TCHAR* path);
|
||||
};
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "common.h"
|
||||
#include "SaveListFactory.h"
|
||||
|
||||
SaveList* SaveListFactory::CreateFromFile(const TCHAR *path)
|
||||
std::unique_ptr<SaveList> SaveListFactory::CreateFromFile(const TCHAR* path)
|
||||
{
|
||||
FIL file;
|
||||
if (f_open(&file, path, FA_OPEN_EXISTING | FA_READ) != FR_OK)
|
||||
@@ -20,5 +20,5 @@ SaveList* SaveListFactory::CreateFromFile(const TCHAR *path)
|
||||
}
|
||||
f_close(&file);
|
||||
|
||||
return new SaveList(std::move(entries), entryCount);
|
||||
return std::make_unique<SaveList>(std::move(entries), entryCount);
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#include <memory>
|
||||
#include "SaveList.h"
|
||||
|
||||
/// @brief Factory for creating \see SaveList instances.
|
||||
@@ -8,5 +9,5 @@ public:
|
||||
/// @brief Creates a \see SaveList instance from the file at the given \p path.
|
||||
/// @param path The save list file path.
|
||||
/// @return A pointer to the constructed \see SaveList instance, or \c nullptr if construction failed.
|
||||
SaveList* CreateFromFile(const TCHAR* path);
|
||||
std::unique_ptr<SaveList> CreateFromFile(const TCHAR* path);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user