Skip to content

Commit

Permalink
b
Browse files Browse the repository at this point in the history
  • Loading branch information
telecomadm1145 committed Aug 3, 2024
1 parent a204467 commit cfa80c2
Showing 1 changed file with 73 additions and 37 deletions.
110 changes: 73 additions & 37 deletions cw2kbd/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,42 @@ dc ce
Console.WriteLine($"[ * ] animate: {animate_func - rom:X6}");
var bit_blit = ((animate_func[0x29] & 0xf) << 16) | ((animate_func[0x29 + 1] & 0xff)) | ((animate_func[0x29 + 2] & 0xff) << 8);
Console.WriteLine($"[ * ] bit_blit: {bit_blit:X6}");
BitmapImage bitmap = new();
using var fs4 = File.OpenRead("shutdown.png");
bitmap.BeginInit();
bitmap.StreamSource = fs4;
bitmap.EndInit();
const int bs = 0x5f000;
Set3(bs + 0x10, bs + 0x10 + 0x5ea, 192, 63, bitmap);
var table = (int*)(rom + bs);
table[0] = bs + 0x10;
table[1] = bs + 0x10 + 0x5ea;
ApplyPatch(rom + bs + 0x8, [0, 0, 192, 63]);
var xx = (bs >> 16) & 0xf;
var yy = (bs >> 8) & 0xff;
var zz = bs & 0xff;
Console.WriteLine($"[ * ] Written shutdown logo to {bs:X6}.");
var shutdown_func = ConvertHexStringToBytes($"""
var emu_scan_key_1 = FindSignature(rom, 0x60000, $"e5 f0 fa 10 fc 61 {BL(tick)} a0 00 0f 01 {BL(delay)}");
Console.WriteLine($"[ * ] emu_scan_key patch point: {(byte*)emu_scan_key_1 - rom:X4}");
var emu_scan_key_real = emu_scan_key_1 - 0x114;
Console.WriteLine($"[ * ] emu_scan_key: {(byte*)emu_scan_key_real - rom:X4}");
var wait_key = (byte*)FindSignature(rom, 0x60000, $"e5 f0 ca e0 {BL(emu_scan_key_real - (nint)rom)} e8 90 cb ff");
if(wait_key == null)
{
wait_key = (byte*)FindSignature(rom, 0x60000, $"e5 f0 ca e0 01 ?? ?? ?? e8 90 cb ff");
Console.WriteLine($"[ + ] Warn: fall back to magic pattern.");
}
Console.WriteLine($"[ * ] wait_key part: {wait_key - rom:X4}");
var emu_report_status = ((wait_key[0x25] & 0xf) << 16) | ((wait_key[0x26] & 0xff)) | ((wait_key[0x27] & 0xff) << 8);
Console.WriteLine($"[ * ] emu_report_status: {emu_report_status:X4}");
ApplyPatch(rom + emu_report_status, [0x1f, 0xfe]);
var wait_kiko_v2 = emu_report_status + 2;
ApplyPatch(rom + wait_kiko_v2, patch);
Console.WriteLine($"[ + ] Written wait_kiko_v2 to {wait_kiko_v2:X6}");

if (File.Exists("shutdown.png"))
{
BitmapImage bitmap = new();
using var fs4 = File.OpenRead("shutdown.png");
bitmap.BeginInit();
bitmap.StreamSource = fs4;
bitmap.EndInit();
const int bs = 0x5cc70;
Set3(bs + 0x10, bs + 0x10 + 0x5ea, 192, 63, bitmap);
var table = (int*)(rom + bs);
table[0] = bs + 0x10;
table[1] = bs + 0x10 + 0x5ea;
ApplyPatch(rom + bs + 0x8, [0, 0, 192, 63]);
var xx = (bs >> 16) & 0xf;
var yy = (bs >> 8) & 0xff;
var zz = bs & 0xff;
Console.WriteLine($"[ * ] Written shutdown logo to {bs:X6}.");
var shutdown_func = ConvertHexStringToBytes($"""
00 00
11 90 d1 f0
0c f0 {zz:X2} {yy:X2}
Expand All @@ -148,34 +168,50 @@ 03 00 11 90 31 f0 00 00 11 90 3d f0 11 90 0a f0 11 90 10 f0 11 90 11 f0 11 90 12
{BL(ko_0)}
{B(STOP)}
""");
const int shutdown_ = 0x5de00;
ApplyPatch(rom + shutdown_, shutdown_func);
Console.WriteLine($"[ * ] Written shutdown function to {shutdown_:X6}.");

var emu_scan_key_1 = FindSignature(rom, 0x60000, $"e5 f0 fa 10 fc 61 {BL(tick)} a0 00 0f 01 {BL(delay)}");
Console.WriteLine($"[ * ] emu_scan_key patch point: {(byte*)emu_scan_key_1 - rom:X4}");
var emu_scan_key_real = emu_scan_key_1 - 0x114;
Console.WriteLine($"[ * ] emu_scan_key: {(byte*)emu_scan_key_real - rom:X4}");
var wait_key = (byte*)FindSignature(rom, 0x60000, $"e5 f0 ca e0 {BL(emu_scan_key_real - (nint)rom)} e8 90 cb ff");
Console.WriteLine($"[ * ] wait_key part: {wait_key - rom:X4}");

var wait_key_shutdown_routine = wait_key + 0x9C;
ApplyPatch(wait_key_shutdown_routine, ConvertHexStringToBytes($"""
const int shutdown_ = 0x5cbb0;
ApplyPatch(rom + shutdown_, shutdown_func);
Console.WriteLine($"[ * ] Written shutdown function to {shutdown_:X6}.");
var wait_key_shutdown_routine = wait_key + 0x9C;
ApplyPatch(wait_key_shutdown_routine, ConvertHexStringToBytes($"""
{BL(shutdown_)}
00 30 00 30
00 30
00 30 00 30
00 30
00 30 00 30
"""));
Console.WriteLine($"[ * ] Patched shutdown call.");

var emu_report_status = ((wait_key[0x25] & 0xf) << 16) | ((wait_key[0x26] & 0xff)) | ((wait_key[0x27] & 0xff) << 8);
Console.WriteLine($"[ * ] emu_report_status: {emu_report_status:X4}");
ApplyPatch(rom + emu_report_status, [0x1f, 0xfe]);
var wait_kiko_v2 = emu_report_status + 2;
ApplyPatch(rom + wait_kiko_v2, patch);
Console.WriteLine($"[ + ] Written wait_kiko_v2 to {wait_kiko_v2:X6}");
Console.WriteLine($"[ * ] Patched shutdown call.");
}
else
{
var shutdown_func = ConvertHexStringToBytes($"""
03 00
11 90 31 f0
00 00
11 90 d1 f0
11 90 3d f0
11 90 0a f0
11 90 10 f0
11 90 11 f0
11 90 12 f0
{BL(ki_mask_0)}
{BL(ko_0)}
{B(STOP)}
""");
var shutdown_ = emu_report_status + 2 + patch.Length;
ApplyPatch(rom + shutdown_, shutdown_func);
Console.WriteLine($"[ * ] Written shutdown function to {shutdown_:X6}.");
var wait_key_shutdown_routine = wait_key + 0x9C;
ApplyPatch(wait_key_shutdown_routine, ConvertHexStringToBytes($"""
{BL(shutdown_)}
00 30 00 30
00 30
00 30 00 30
00 30
00 30 00 30
"""));
Console.WriteLine($"[ * ] Patched shutdown call.");
}

if (emu_scan_key_1 != 0)
{
Expand Down

0 comments on commit cfa80c2

Please sign in to comment.