Skip to content

Commit dd95f66

Browse files
committed
Revert "Improve patch error messages and kernel detection"
1 parent 1ad3580 commit dd95f66

10 files changed

Lines changed: 64 additions & 191 deletions

File tree

UotanToolbox/Assets/Resources.resx

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,42 +1310,6 @@ Fill ID in ADB, one at a time!</value>
13101310
<data name="Basicflash_ZipError" xml:space="preserve">
13111311
<value>An error occurred while parsing the zip format</value>
13121312
</data>
1313-
<data name="Patch_KernelSUPackFailed" xml:space="preserve">
1314-
<value>KernelSU pack failed: </value>
1315-
</data>
1316-
<data name="Patch_KernelSUNotFound" xml:space="preserve">
1317-
<value>kernelsu.ko not found in patch source.</value>
1318-
</data>
1319-
<data name="Patch_KernelSUPatchFailed" xml:space="preserve">
1320-
<value>KernelSU patch failed: </value>
1321-
</data>
1322-
<data name="Patch_BootCleanFailed" xml:space="preserve">
1323-
<value>Boot clean failed: </value>
1324-
</data>
1325-
<data name="Patch_MagiskCopyFailed" xml:space="preserve">
1326-
<value>Magisk component copy failed: </value>
1327-
</data>
1328-
<data name="Patch_MagiskRamdiskPatchFailed" xml:space="preserve">
1329-
<value>Magisk ramdisk patch failed: </value>
1330-
</data>
1331-
<data name="Patch_MagiskPatchFailed" xml:space="preserve">
1332-
<value>Magisk patch failed.</value>
1333-
</data>
1334-
<data name="Patch_MagiskDTBPatchFailed" xml:space="preserve">
1335-
<value>Magisk DTB patch failed: </value>
1336-
</data>
1337-
<data name="Patch_KSUOnlyArm64" xml:space="preserve">
1338-
<value>KernelSU only supports ARM64 architecture.</value>
1339-
</data>
1340-
<data name="Patch_KSUKsudNotFound" xml:space="preserve">
1341-
<value>Could not find libksud.so for architecture {0} in APK.</value>
1342-
</data>
1343-
<data name="Patch_KSUKoNotFound" xml:space="preserve">
1344-
<value>Cannot find a matching kernelsu.ko for kernel version {0} in this APK.</value>
1345-
</data>
1346-
<data name="Common_PlatformNotSupported" xml:space="preserve">
1347-
<value>This function only supports Windows, macOS and Linux.</value>
1348-
</data>
13491313
<data name="Common_ExeFailed" xml:space="preserve">
13501314
<value>Execution failed!</value>
13511315
</data>

UotanToolbox/Assets/Resources.zh-CN.resx

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,42 +1310,6 @@ ADB下填写序号,一次一个!</value>
13101310
<data name="Basicflash_ZipError" xml:space="preserve">
13111311
<value>解析zip格式时发生错误</value>
13121312
</data>
1313-
<data name="Patch_KernelSUPackFailed" xml:space="preserve">
1314-
<value>KernelSU 打包失败:</value>
1315-
</data>
1316-
<data name="Patch_KernelSUNotFound" xml:space="preserve">
1317-
<value>补丁源中未找到 kernelsu.ko。</value>
1318-
</data>
1319-
<data name="Patch_KernelSUPatchFailed" xml:space="preserve">
1320-
<value>KernelSU 修补失败:</value>
1321-
</data>
1322-
<data name="Patch_BootCleanFailed" xml:space="preserve">
1323-
<value>引导清理失败:</value>
1324-
</data>
1325-
<data name="Patch_MagiskCopyFailed" xml:space="preserve">
1326-
<value>Magisk 组件复制失败:</value>
1327-
</data>
1328-
<data name="Patch_MagiskRamdiskPatchFailed" xml:space="preserve">
1329-
<value>Magisk RAMDISK 修补失败:</value>
1330-
</data>
1331-
<data name="Patch_MagiskPatchFailed" xml:space="preserve">
1332-
<value>Magisk 修补失败。</value>
1333-
</data>
1334-
<data name="Patch_MagiskDTBPatchFailed" xml:space="preserve">
1335-
<value>Magisk DTB 修补失败:</value>
1336-
</data>
1337-
<data name="Patch_KSUOnlyArm64" xml:space="preserve">
1338-
<value>KernelSU 仅支持 ARM64 架构。</value>
1339-
</data>
1340-
<data name="Patch_KSUKsudNotFound" xml:space="preserve">
1341-
<value>在 APK 中找不到架构为 {0} 的 libksud.so。</value>
1342-
</data>
1343-
<data name="Patch_KSUKoNotFound" xml:space="preserve">
1344-
<value>在此 APK 中找不到与内核版本 {0} 匹配的 kernelsu.ko。</value>
1345-
</data>
1346-
<data name="Common_PlatformNotSupported" xml:space="preserve">
1347-
<value>此功能仅支持 Windows、macOS 和 Linux。</value>
1348-
</data>
13491313
<data name="Common_ExeFailed" xml:space="preserve">
13501314
<value>执行失败!</value>
13511315
</data>

UotanToolbox/Common/FileHelper.cs

Lines changed: 25 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -349,65 +349,44 @@ public static bool TestPermission(string directoryPath)
349349
/// <returns>内核编译签名信息</returns>
350350
public static string ReadKernelVersion(string filePath)
351351
{
352-
if (!File.Exists(filePath)) return "";
353-
try
352+
byte[] Signature = [0x69, 0x6e, 0x69, 0x74, 0x63, 0x61, 0x6c, 0x6c, 0x5f, 0x64, 0x65, 0x62, 0x75, 0x67, 0x00];
353+
using FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
354+
using BinaryReader br = new BinaryReader(fs);
355+
long signaturePosition = FindSignaturePosition(br, Signature);
356+
if (signaturePosition == -1)
354357
{
355-
356-
byte[] initcallSignature = [0x69, 0x6e, 0x69, 0x74, 0x63, 0x61, 0x6c, 0x6c, 0x5f, 0x64, 0x65, 0x62, 0x75, 0x67, 0x00];
357-
byte[] linuxVersionSignature = Encoding.ASCII.GetBytes("Linux version ");
358-
359-
using FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
360-
byte[] data = new byte[fs.Length];
361-
fs.ReadExactly(data);
362-
363-
// initcall_debug
364-
int index = IndexOf(data, initcallSignature);
365-
if (index != -1)
366-
{
367-
return ReadNullTerminatedString(data, index + initcallSignature.Length);
368-
}
369-
370-
// Linux version
371-
index = IndexOf(data, linuxVersionSignature);
372-
if (index != -1)
373-
{
374-
return ReadNullTerminatedString(data, index);
375-
}
358+
return "";
376359
}
377-
catch { }
378-
return "";
360+
_ = fs.Seek(signaturePosition + Signature.Length, SeekOrigin.Begin);
361+
return ReadUntilTerminator(br);
379362
}
380-
381-
private static int IndexOf(byte[] data, byte[] pattern)
363+
private static long FindSignaturePosition(BinaryReader reader, byte[] signature)
382364
{
383-
if (pattern.Length > data.Length) return -1;
384-
for (int i = 0; i <= data.Length - pattern.Length; i++)
365+
byte[] buffer = new byte[signature.Length];
366+
long position = 0;
367+
while (position + signature.Length <= reader.BaseStream.Length)
385368
{
386-
bool found = true;
387-
for (int j = 0; j < pattern.Length; j++)
369+
_ = reader.BaseStream.Seek(position, SeekOrigin.Begin);
370+
_ = reader.Read(buffer, 0, signature.Length);
371+
if (buffer.SequenceEqual(signature))
388372
{
389-
if (data[i + j] != pattern[j])
390-
{
391-
found = false;
392-
break;
393-
}
373+
return position;
394374
}
395-
if (found) return i;
375+
position++;
396376
}
397377
return -1;
398378
}
399-
400-
private static string ReadNullTerminatedString(byte[] data, int startIndex)
379+
private static string ReadUntilTerminator(BinaryReader reader)
401380
{
402381
StringBuilder sb = new StringBuilder();
403-
for (int i = startIndex; i < data.Length; i++)
382+
int b;
383+
while ((b = reader.ReadByte()) != 0x00)
404384
{
405-
if (data[i] == 0)
406-
{
407-
if (sb.Length == 0) continue;
408-
break;
409-
}
410-
sb.Append((char)data[i]);
385+
_ = sb.Append((char)b);
386+
}
387+
while ((b = reader.ReadByte()) != 0x00)
388+
{
389+
_ = sb.Append((char)b);
411390
}
412391
return sb.ToString();
413392
}

UotanToolbox/Common/PatchHelper/ImageDetect.cs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,16 @@ private static (bool, string) dtb_detect(string temp)
5656
{
5757
string kmi = "", version = "", arch = "";
5858
bool have_kernel = false, gki2 = false;
59-
string kernelPath = Path.Combine(temp, "kernel");
60-
if (File.Exists(kernelPath))
59+
if (File.Exists(Path.Combine(temp, "kernel")))
6160
{
6261
have_kernel = true;
63-
version = FileHelper.ReadKernelVersion(kernelPath);
62+
version = FileHelper.ReadKernelVersion(Path.Combine(temp, "kernel"));
6463
kmi = StringHelper.ExtractKMI(version);
65-
if (string.IsNullOrEmpty(kmi))
66-
{
67-
try
68-
{
69-
byte[] data = await File.ReadAllBytesAsync(kernelPath);
70-
string rawContent = System.Text.Encoding.ASCII.GetString(System.Linq.Enumerable.ToArray(System.Linq.Enumerable.Select(data, b => (b >= 32 && b <= 126) ? b : (byte)'.')));
71-
kmi = StringHelper.ExtractKMI(rawContent);
72-
}
73-
catch { }
74-
}
75-
7664
if (!string.IsNullOrEmpty(kmi))
7765
{
7866
gki2 = true;
7967
}
80-
string kernel_info = await CallExternalProgram.File($"\"{kernelPath}\"");
68+
string kernel_info = await CallExternalProgram.File($"\"{Path.Combine(temp, "kernel")}\"");
8169
arch = ArchDetect(kernel_info);
8270
}
8371
return (version, kmi, have_kernel, gki2, arch);

UotanToolbox/Common/PatchHelper/KernelSUPatch.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static async Task<string> GKI_Patch(PatchInfo zipInfo, BootInfo bootInfo)
2323
(string mb_output, int exitcode) = await CallExternalProgram.MagiskBoot($"repack \"{bootInfo.Path}\"", bootInfo.TempPath);
2424
if (exitcode != 0)
2525
{
26-
throw new Exception(GetTranslation("Patch_KernelSUPackFailed") + mb_output);
26+
throw new Exception("ksu_pack_1 failed: " + mb_output);
2727
}
2828
string allowedChars = "abcdef0123456789";
2929
Random random = new Random();
@@ -40,7 +40,7 @@ public static async Task<string> LKM_Patch(PatchInfo zipInfo, BootInfo bootInfo)
4040
}
4141
if (bootInfo.Arch != "aarch64")
4242
{
43-
throw new Exception(GetTranslation("Patch_KSUOnlyArm64"));
43+
throw new Exception("unsupported arch" + bootInfo.Arch);
4444
}
4545

4646
string koPath = Path.Combine(zipInfo.TempPath, "kernelsu.ko");
@@ -58,7 +58,7 @@ public static async Task<string> LKM_Patch(PatchInfo zipInfo, BootInfo bootInfo)
5858

5959
if (!File.Exists(koPath))
6060
{
61-
throw new Exception(GetTranslation("Patch_KernelSUNotFound"));
61+
throw new Exception("kernelsu.ko not found in patch source.");
6262
}
6363

6464
File.Copy(koPath, Path.Combine(bootInfo.TempPath, "kernelsu.ko"), true);
@@ -72,13 +72,13 @@ public static async Task<string> LKM_Patch(PatchInfo zipInfo, BootInfo bootInfo)
7272
(string mb_output, int exitcode) = await CallExternalProgram.MagiskBoot($"cpio ramdisk.cpio \"cp init init.real\" \"add 0755 ksuinit init\" \"add 0755 kernelsu.ko kernelsu.ko\"", bootInfo.TempPath);
7373
if (exitcode != 0)
7474
{
75-
throw new Exception(GetTranslation("Patch_KernelSUPatchFailed") + mb_output);
75+
throw new Exception("lkm_patch failed: " + mb_output);
7676
}
7777
CleanBoot(bootInfo.TempPath);
7878
(mb_output, exitcode) = await CallExternalProgram.MagiskBoot($"repack \"{bootInfo.Path}\"", bootInfo.TempPath);
7979
if (exitcode != 0)
8080
{
81-
throw new Exception(GetTranslation("Patch_KernelSUPackFailed") + mb_output);
81+
throw new Exception("lkm_pack failed: " + mb_output);
8282
}
8383
string allowedChars = "abcdef0123456789";
8484
Random random = new Random();
@@ -118,7 +118,7 @@ private static void CleanBoot(string path)
118118
}
119119
catch (Exception ex)
120120
{
121-
throw new Exception(GetTranslation("Patch_BootCleanFailed") + ex.Message);
121+
throw new Exception("bootclean failed: " + ex.Message);
122122
}
123123
}
124124
}

UotanToolbox/Common/PatchHelper/KsuAssetExtractor.cs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static string ExtractKoFromApk(string apkPath, string outputDir, string a
1616
{
1717
"aarch64" => "arm64-v8a",
1818
//"X86-64" => "x86_64",
19-
_ => throw new ArgumentException(FeaturesHelper.GetTranslation("Patch_KSUOnlyArm64"))
19+
_ => throw new ArgumentException($"Unsupported architecture: {arch}")
2020
};
2121

2222
using (ZipArchive archive = ZipFile.OpenRead(apkPath))
@@ -29,7 +29,7 @@ public static string ExtractKoFromApk(string apkPath, string outputDir, string a
2929

3030
if (ksudEntry == null)
3131
{
32-
throw new Exception(string.Format(FeaturesHelper.GetTranslation("Patch_KSUKsudNotFound"), archSubfolder));
32+
throw new Exception($"Could not find libksud.so for architecture {archSubfolder} in APK.");
3333
}
3434

3535
using (Stream s = ksudEntry.Open())
@@ -46,8 +46,8 @@ public static string ExtractKoFromApk(string apkPath, string outputDir, string a
4646
{
4747
return Path.Combine(outputDir, match);
4848
}
49-
50-
throw new Exception(string.Format(FeaturesHelper.GetTranslation("Patch_KSUKoNotFound"), kernelVersion));
49+
50+
throw new Exception($"Cannot find a matching kernelsu.ko for kernel version {kernelVersion} in this APK.");
5151
}
5252

5353
var koFile = extractedFiles.Keys.FirstOrDefault(k => k.EndsWith(".ko"));
@@ -61,22 +61,7 @@ public static string ExtractKoFromApk(string apkPath, string outputDir, string a
6161
return null;
6262
}
6363

64-
public static bool IsKsudHaveKo(string ksudPath)
65-
{
66-
try
67-
{
68-
if (!File.Exists(ksudPath)) return false;
69-
byte[] data = File.ReadAllBytes(ksudPath);
70-
var results = ProcessData(data, null);
71-
return results.Keys.Any(k => k.EndsWith(".ko"));
72-
}
73-
catch
74-
{
75-
return false;
76-
}
77-
}
78-
79-
internal static Dictionary<string, byte[]> ProcessData(byte[] data, string outputDir)
64+
private static Dictionary<string, byte[]> ProcessData(byte[] data, string outputDir)
8065
{
8166
var results = new Dictionary<string, byte[]>();
8267

UotanToolbox/Common/PatchHelper/MagiskPatch.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static async Task<string> Magisk_Patch_Mouzei(PatchInfo zipInfo, BootInfo
5757
? Path.Combine(Global.bin_path, "7z", "7za.exe")
5858
: RuntimeInformation.IsOSPlatform(OSPlatform.Linux) | (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
5959
? Path.Combine(Global.bin_path, "7zza")
60-
: throw new PlatformNotSupportedException(GetTranslation("Common_PlatformNotSupported")),
60+
: throw new PlatformNotSupportedException("This function only supports Windows,macOS and Linux."),
6161
MagiskbootPath = Path.Combine(Global.bin_path, "magiskboot"),
6262
CsvConfPath = csvConfPath,
6363
CpuType = cpuType,
@@ -81,7 +81,7 @@ public static async Task<string> Magisk_Patch_Mouzei(PatchInfo zipInfo, BootInfo
8181

8282
if (!patchResult.IsSuccess)
8383
{
84-
string fallbackMessage = GetTranslation("Patch_MagiskPatchFailed");
84+
string fallbackMessage = $"{GetTranslation("Common_Error")}: Magisk patch failed.";
8585
string message = string.IsNullOrWhiteSpace(patchResult.ErrorMessage) ? fallbackMessage : patchResult.ErrorMessage!;
8686
if (patchResult.Exception != null)
8787
{
@@ -177,7 +177,7 @@ private static async Task comp_copy(PatchInfo zipInfo, BootInfo bootinfo)
177177
}
178178
catch (Exception ex)
179179
{
180-
throw new Exception(GetTranslation("Patch_MagiskCopyFailed") + ex.Message);
180+
throw new Exception("comp_copy failed: " + ex.Message);
181181
}
182182

183183
}
@@ -188,20 +188,20 @@ private static async Task ramdisk_patch(BootInfo bootInfo)
188188
(mb_output, exitcode) = await CallExternalProgram.MagiskBoot("cpio ramdisk.cpio \"add 0750 init init\" \"mkdir 0750 overlay.d\" \"mkdir 0750 overlay.d/sbin\" \"add 0644 overlay.d/sbin/magisk32.xz magisk32.xz\" ", bootInfo.TempPath);
189189
if (exitcode != 0)
190190
{
191-
throw new Exception(GetTranslation("Patch_MagiskRamdiskPatchFailed") + mb_output);
191+
throw new Exception("ramdisk_patch_1 failed: " + mb_output);
192192
}
193193
(mb_output, exitcode) = await CallExternalProgram.MagiskBoot("cpio ramdisk.cpio \"add 0644 overlay.d/sbin/stub.xz stub.xz\" \"patch\" \"backup ramdisk.cpio.orig\" \"mkdir 000 .backup\" \"add 000 .backup/.magisk config\"", bootInfo.TempPath);
194194
if (exitcode != 0)
195195
{
196-
throw new Exception(GetTranslation("Patch_MagiskRamdiskPatchFailed") + mb_output);
196+
throw new Exception("ramdisk_patch_2 failed: " + mb_output);
197197
}
198198

199199
if (File.Exists(Path.Combine(bootInfo.TempPath, "magisk64.xz")))
200200
{
201201
(mb_output, exitcode) = await CallExternalProgram.MagiskBoot("cpio ramdisk.cpio \"add 0644 overlay.d/sbin/magisk64.xz magisk64.xz\"", bootInfo.TempPath);
202202
if (exitcode != 0)
203203
{
204-
throw new Exception(GetTranslation("Patch_MagiskRamdiskPatchFailed") + mb_output);
204+
throw new Exception("ramdisk_patch_3 failed: " + mb_output);
205205
}
206206
}
207207
}
@@ -241,7 +241,7 @@ private static async Task dtb_patch(BootInfo bootInfo)
241241
(string mb_output, int exitcode) = await CallExternalProgram.MagiskBoot($"dtb {bootInfo.DTBName} test", bootInfo.TempPath);
242242
if (exitcode != 0)
243243
{
244-
throw new Exception(GetTranslation("Patch_MagiskDTBPatchFailed") + mb_output);
244+
throw new Exception("dtb_patch_1 failed: " + mb_output);
245245
}
246246
(_, _) = await CallExternalProgram.MagiskBoot($"dtb {bootInfo.DTBName} patch", bootInfo.TempPath);
247247
}
@@ -277,7 +277,7 @@ private static void CleanBoot(string path)
277277
}
278278
catch (Exception ex)
279279
{
280-
throw new Exception(GetTranslation("Patch_BootCleanFailed") + ex.Message);
280+
throw new Exception("bootclean failed: " + ex.Message);
281281
}
282282
}
283283
}

0 commit comments

Comments
 (0)