libWiiSharp/IosPatcher.cs

375 lines
13 KiB
C#
Raw Normal View History

2021-02-06 21:19:03 -06:00
/* This file is part of libWiiSharp
* Copyright(C) 2009 Leathl
* Copyright (C) 2020 - 2022 TheShadowEevee, Github Contributors
2021-02-06 21:19:03 -06:00
*
* libWiiSharp is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libWiiSharp is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.ComponentModel;
using System.Text;
namespace libWiiSharp
{
2021-02-06 21:19:03 -06:00
/// <summary>
/// An IOS patcher which can patch fakesigning, es_identify and nand permissions.
/// </summary>
public class IosPatcher
{
2021-02-06 21:19:03 -06:00
private WAD wadFile;
private int esIndex = -1;
2021-02-06 21:19:03 -06:00
#region Public Functions
/// <summary>
/// Loads an IOS wad to patch the es module.
/// </summary>
/// <param name="iosWad"></param>
public void LoadIOS(ref WAD iosWad)
{
wadFile = iosWad;
2021-02-10 13:32:35 -06:00
PrivGetEsIndex();
2021-02-06 21:19:03 -06:00
}
2021-02-06 21:19:03 -06:00
/// <summary>
/// Patches fakesigning.
/// Returns the number of applied patches.
/// </summary>
/// <returns></returns>
public int PatchFakeSigning()
{
return esIndex < 0 ? -1 : PrivPatchFakeSigning(ref wadFile.Contents[esIndex]);
}
2021-02-06 21:19:03 -06:00
/// <summary>
/// Patches es_identify.
/// Returns the number of applied patches.
/// </summary>
/// <returns></returns>
public int PatchEsIdentify()
{
return esIndex < 0 ? -1 : PrivPatchEsIdentify(ref wadFile.Contents[esIndex]);
}
2021-02-06 21:19:03 -06:00
/// <summary>
/// Patches nand permissions.
/// Returns the number of applied patches.
/// </summary>
/// <returns></returns>
public int PatchNandPermissions()
{
return esIndex < 0 ? -1 : PrivPatchNandPermissions(ref wadFile.Contents[esIndex]);
}
public int PatchVP()
{
return esIndex < 0 ? -1 : PrivPatchVP(ref wadFile.Contents[esIndex]);
}
2021-02-06 21:19:03 -06:00
/// <summary>
/// Patches fakesigning, es_identify and nand permissions.
/// Returns the number of applied patches.
/// </summary>
/// <returns></returns>
public int PatchAll()
{
return esIndex < 0 ? -1 : PrivPatchAll(ref wadFile.Contents[esIndex]);
}
public int PatchFakeSigning(ref byte[] esModule)
{
2021-02-10 13:32:35 -06:00
return PrivPatchFakeSigning(ref esModule);
}
public int PatchEsIdentify(ref byte[] esModule)
{
2021-02-10 13:32:35 -06:00
return PrivPatchEsIdentify(ref esModule);
}
public int PatchNandPermissions(ref byte[] esModule)
{
return PrivPatchNandPermissions(ref esModule);
}
public int PatchVP(ref byte[] esModule)
{
return PrivPatchVP(ref esModule);
}
public int PatchAll(ref byte[] esModule)
{
return PrivPatchAll(ref esModule);
}
2021-02-06 21:19:03 -06:00
#endregion
2021-02-06 21:19:03 -06:00
#region Private Functions
private int PrivPatchFakeSigning(ref byte[] esModule)
{
FireDebug("Patching Fakesigning...");
2021-02-06 21:19:03 -06:00
int num = 0;
byte[] second1 = new byte[4]
{
32,
7,
35,
162
2021-02-06 21:19:03 -06:00
};
byte[] second2 = new byte[4]
{
32,
7,
75,
11
2021-02-06 21:19:03 -06:00
};
for (int firstIndex = 0; firstIndex < esModule.Length - 4; ++firstIndex)
{
FireProgress((firstIndex + 1) * 100 / esModule.Length);
2021-02-06 21:19:03 -06:00
if (Shared.CompareByteArrays(esModule, firstIndex, second1, 0, 4) || Shared.CompareByteArrays(esModule, firstIndex, second2, 0, 4))
{
FireDebug(" Patching at Offset: 0x{0}", (object)firstIndex.ToString("x8").ToUpper());
esModule[firstIndex + 1] = 0;
2021-02-06 21:19:03 -06:00
firstIndex += 4;
++num;
}
}
FireDebug("Patching Fakesigning Finished... (Patches applied: {0})", (object)num);
return num;
}
private int PrivPatchEsIdentify(ref byte[] esModule)
{
FireDebug("Patching ES_Identify...");
2021-02-06 21:19:03 -06:00
int num = 0;
byte[] second = new byte[4]
{
40,
3,
209,
35
2021-02-06 21:19:03 -06:00
};
for (int firstIndex = 0; firstIndex < esModule.Length - 4; ++firstIndex)
{
FireProgress((firstIndex + 1) * 100 / esModule.Length);
2021-02-06 21:19:03 -06:00
if (Shared.CompareByteArrays(esModule, firstIndex, second, 0, 4))
{
FireDebug(" Patching at Offset: 0x{0}", (object)firstIndex.ToString("x8").ToUpper());
esModule[firstIndex + 2] = 0;
esModule[firstIndex + 3] = 0;
2021-02-06 21:19:03 -06:00
firstIndex += 4;
++num;
}
}
FireDebug("Patching ES_Identify Finished... (Patches applied: {0})", (object)num);
2021-02-06 21:19:03 -06:00
return num;
}
private int PrivPatchNandPermissions(ref byte[] esModule)
{
FireDebug("Patching NAND Permissions...");
2021-02-06 21:19:03 -06:00
int num = 0;
byte[] second = new byte[6]
{
66,
139,
208,
1,
37,
102
2021-02-06 21:19:03 -06:00
};
for (int firstIndex = 0; firstIndex < esModule.Length - 6; ++firstIndex)
{
FireProgress((firstIndex + 1) * 100 / esModule.Length);
2021-02-06 21:19:03 -06:00
if (Shared.CompareByteArrays(esModule, firstIndex, second, 0, 6))
{
FireDebug(" Patching at Offset: 0x{0}", (object)firstIndex.ToString("x8").ToUpper());
esModule[firstIndex + 2] = 224;
2021-02-06 21:19:03 -06:00
firstIndex += 6;
++num;
}
}
FireDebug("Patching NAND Permissions Finished... (Patches applied: {0})", (object)num);
2021-02-06 21:19:03 -06:00
return num;
}
private int PrivPatchVP(ref byte[] esModule)
{
FireDebug("Patching VP...");
2021-02-06 21:19:03 -06:00
int num = 0;
byte[] second = new byte[4]
{
210,
1,
78,
86
2021-02-06 21:19:03 -06:00
};
for (int firstIndex = 0; firstIndex < esModule.Length - 4; ++firstIndex)
{
FireProgress((firstIndex + 1) * 100 / esModule.Length);
2021-02-06 21:19:03 -06:00
if (Shared.CompareByteArrays(esModule, firstIndex, second, 0, 4))
{
FireDebug(" Patching for VP at Offset: 0x{0}", (object)firstIndex.ToString("x8").ToUpper());
esModule[firstIndex] = 224;
2021-02-06 21:19:03 -06:00
firstIndex += 4;
++num;
}
}
FireDebug("Patching VP Finished... (Patches applied: {0})", (object)num);
2021-02-06 21:19:03 -06:00
return num;
}
2021-02-06 21:19:03 -06:00
private int PrivPatchAll(ref byte[] esModule)
{
FireDebug("Patching Fakesigning, ES_Identify, NAND Permissions and VP ...");
2021-02-06 21:19:03 -06:00
int num = 0;
byte[] second1 = new byte[4]
{
32,
7,
35,
162
2021-02-06 21:19:03 -06:00
};
byte[] second2 = new byte[4]
{
32,
7,
75,
11
2021-02-06 21:19:03 -06:00
};
byte[] second3 = new byte[4]
{
40,
3,
209,
35
2021-02-06 21:19:03 -06:00
};
byte[] second4 = new byte[6]
{
66,
139,
208,
1,
37,
102
2021-02-06 21:19:03 -06:00
};
byte[] second5 = new byte[4]
{
210,
1,
78,
86
2021-02-06 21:19:03 -06:00
};
for (int firstIndex = 0; firstIndex < esModule.Length - 6; ++firstIndex)
{
FireProgress((firstIndex + 1) * 100 / esModule.Length);
2021-02-06 21:19:03 -06:00
if (Shared.CompareByteArrays(esModule, firstIndex, second1, 0, 4) || Shared.CompareByteArrays(esModule, firstIndex, second2, 0, 4))
{
FireDebug(" Patching Fakesigning at Offset: 0x{0}", (object)firstIndex.ToString("x8").ToUpper());
esModule[firstIndex + 1] = 0;
2021-02-06 21:19:03 -06:00
firstIndex += 4;
++num;
}
else if (Shared.CompareByteArrays(esModule, firstIndex, second3, 0, 4))
{
FireDebug(" Patching ES_Identify at Offset: 0x{0}", (object)firstIndex.ToString("x8").ToUpper());
esModule[firstIndex + 2] = 0;
esModule[firstIndex + 3] = 0;
2021-02-06 21:19:03 -06:00
firstIndex += 4;
++num;
}
else if (Shared.CompareByteArrays(esModule, firstIndex, second4, 0, 6))
{
FireDebug(" Patching NAND Permissions at Offset: 0x{0}", (object)firstIndex.ToString("x8").ToUpper());
esModule[firstIndex + 2] = 224;
2021-02-06 21:19:03 -06:00
firstIndex += 6;
++num;
}
else if (Shared.CompareByteArrays(esModule, firstIndex, second5, 0, 4))
{
FireDebug(" Patching VP at Offset: 0x{0}", (object)firstIndex.ToString("x8").ToUpper());
esModule[firstIndex] = 224;
2021-02-06 21:19:03 -06:00
firstIndex += 4;
++num;
}
}
FireDebug("Patching Fakesigning, ES_Identify, NAND Permissions and VP Finished... (Patches applied: {0})", (object)num);
2021-02-06 21:19:03 -06:00
return num;
}
2021-02-10 13:32:35 -06:00
private void PrivGetEsIndex()
{
FireDebug("Scanning for ES Module...");
2021-02-06 21:19:03 -06:00
string str = "$IOSVersion:";
for (int index1 = wadFile.NumOfContents - 1; index1 >= 0; --index1)
{
FireDebug(" Scanning Content #{0} of {1}...", index1 + 1, wadFile.NumOfContents);
FireProgress((index1 + 1) * 100 / wadFile.NumOfContents);
for (int index2 = 0; index2 < wadFile.Contents[index1].Length - 64; ++index2)
2021-02-06 21:19:03 -06:00
{
if (Encoding.ASCII.GetString(wadFile.Contents[index1], index2, 12) == str)
2021-02-06 21:19:03 -06:00
{
int index3 = index2 + 12;
while (wadFile.Contents[index1][index3] == 32)
2021-02-06 21:19:03 -06:00
{
++index3;
}
if (Encoding.ASCII.GetString(wadFile.Contents[index1], index3, 3) == "ES:")
{
FireDebug(" -> ES Module found!");
FireDebug("Scanning for ES Module Finished...");
esIndex = index1;
FireProgress(100);
2021-02-06 21:19:03 -06:00
return;
}
}
}
}
FireDebug("/!\\/!\\/!\\ ES Module wasn't found! /!\\/!\\/!\\");
2021-02-06 21:19:03 -06:00
throw new Exception("ES module wasn't found!");
}
2021-02-06 21:19:03 -06:00
#endregion
2021-02-06 21:19:03 -06:00
#region Events
/// <summary>
/// Fires the Progress of various operations
/// </summary>
public event EventHandler<ProgressChangedEventArgs> Progress;
/// <summary>
/// Fires debugging messages. You may write them into a log file or log textbox.
/// </summary>
public event EventHandler<MessageEventArgs> Debug;
private void FireDebug(string debugMessage, params object[] args)
2021-02-06 21:19:03 -06:00
{
EventHandler<MessageEventArgs> debug = Debug;
2021-02-06 21:19:03 -06:00
if (debug == null)
{
2021-02-06 21:19:03 -06:00
return;
}
2021-02-06 21:19:03 -06:00
debug(new object(), new MessageEventArgs(string.Format(debugMessage, args)));
}
private void FireProgress(int progressPercentage)
2021-02-06 21:19:03 -06:00
{
EventHandler<ProgressChangedEventArgs> progress = Progress;
2021-02-06 21:19:03 -06:00
if (progress == null)
{
2021-02-06 21:19:03 -06:00
return;
}
progress(new object(), new ProgressChangedEventArgs(progressPercentage, string.Empty));
2021-02-06 21:19:03 -06:00
}
#endregion
}
}