| 2421 | } |
| 2422 | |
| 2423 | BOOLEAN |
| 2424 | LOADER_ENTRY::BooterPatch(IN UINT8 *BooterData, IN UINT64 BooterSize) |
| 2425 | { |
| 2426 | INTN Num, y = 0; |
| 2427 | |
| 2428 | |
| 2429 | for (size_t i = 0 ; i < KernelAndKextPatches.BootPatches.size(); ++i) |
| 2430 | { |
| 2431 | // if we modify directly KernelAndKextPatches.BootPatches[i].SearchLen, it will wrong for next driver |
| 2432 | UINTN SearchLen = KernelAndKextPatches.BootPatches[i].SearchLen; |
| 2433 | if (!SearchLen) { |
| 2434 | SearchLen = BooterSize; |
| 2435 | } |
| 2436 | |
| 2437 | DBG( "Patch[%zu]: %s\n", i, KernelAndKextPatches.BootPatches[i].Label.c_str()); |
| 2438 | if (!KernelAndKextPatches.BootPatches[i].MenuItem.BValue) { |
| 2439 | DBG( "==> disabled\n"); |
| 2440 | continue; |
| 2441 | } |
| 2442 | UINT8 * curs = BooterData; |
| 2443 | UINTN j = 0; |
| 2444 | while (j < BooterSize) { |
| 2445 | if (KernelAndKextPatches.BootPatches[i].StartPattern.isEmpty() || //old behavior |
| 2446 | CompareMemMask((const UINT8*)curs, |
| 2447 | (const UINT8*)KernelAndKextPatches.BootPatches[i].StartPattern.data(), |
| 2448 | KernelAndKextPatches.BootPatches[i].StartPattern.size(), |
| 2449 | (const UINT8*)KernelAndKextPatches.BootPatches[i].StartMask.data(), |
| 2450 | KernelAndKextPatches.BootPatches[i].StartPattern.size())) { |
| 2451 | DBG( " StartPattern found\n"); |
| 2452 | |
| 2453 | Num = SearchAndReplaceMask(curs, |
| 2454 | SearchLen, |
| 2455 | (const UINT8*)KernelAndKextPatches.BootPatches[i].Data.data(), |
| 2456 | (const UINT8*)KernelAndKextPatches.BootPatches[i].MaskFind.data(), |
| 2457 | KernelAndKextPatches.BootPatches[i].Data.size(), |
| 2458 | (const UINT8*)KernelAndKextPatches.BootPatches[i].Patch.data(), |
| 2459 | (const UINT8*)KernelAndKextPatches.BootPatches[i].MaskReplace.data(), |
| 2460 | KernelAndKextPatches.BootPatches[i].Count |
| 2461 | ); |
| 2462 | if (Num) { |
| 2463 | y++; |
| 2464 | curs += SearchLen - 1; |
| 2465 | j += SearchLen - 1; |
| 2466 | } |
| 2467 | |
| 2468 | DBG( "==> %s : %lld replaces done\n", Num ? "Success" : "Error", Num); |
| 2469 | if ( KernelAndKextPatches.BootPatches[i].StartPattern.isEmpty() ) { |
| 2470 | break; |
| 2471 | } |
| 2472 | } |
| 2473 | j++; curs++; |
| 2474 | } |
| 2475 | } |
| 2476 | // if (KernelAndKextPatches.KPDebug) { |
| 2477 | // gBS->Stall(2000000); |
| 2478 | // } |
| 2479 | Stall(2000000); |
| 2480 | return (y != 0); |
nothing calls this directly
no test coverage detected