56 lines
2.6 KiB
PowerShell
56 lines
2.6 KiB
PowerShell
# Восстановление grldr из бэкапа + корректная замена GFM -> Ventoy (поиск по байтам).
|
|
$ErrorActionPreference = 'Stop'
|
|
$f = 'F:\efi\Paravis\Boot\g4d\grldr'
|
|
$bak = 'C:\PF\Ventoy\backup\g4d-grldr-GFM.bin'
|
|
|
|
function Find-Bytes([byte[]]$hay, [byte[]]$needle, [int]$from = 0) {
|
|
$limit = $hay.Length - $needle.Length
|
|
for ($i = $from; $i -le $limit; $i++) {
|
|
$ok = $true
|
|
for ($j = 0; $j -lt $needle.Length; $j++) { if ($hay[$i+$j] -ne $needle[$j]) { $ok = $false; break } }
|
|
if ($ok) { return $i }
|
|
}
|
|
return -1
|
|
}
|
|
|
|
# 1. восстановление
|
|
Copy-Item $bak $f -Force
|
|
Set-ItemProperty -Path $f -Name IsReadOnly -Value $false -ErrorAction SilentlyContinue
|
|
$b = [IO.File]::ReadAllBytes($f)
|
|
"--- восстановлен: {0} байт" -f $b.Length
|
|
|
|
# 2. поиск блока
|
|
$startMarker = [Text.Encoding]::ASCII.GetBytes('title Grub2 FileManager')
|
|
$start = Find-Bytes $b $startMarker
|
|
if ($start -lt 0) { throw 'GFM-блок не найден' }
|
|
$bootMarker = [Text.Encoding]::ASCII.GetBytes("`nboot`n")
|
|
$bootPos = Find-Bytes $b $bootMarker $start
|
|
if ($bootPos -lt 0) { throw 'конец блока не найден' }
|
|
$blockEnd = $bootPos + $bootMarker.Length
|
|
$oldLen = $blockEnd - $start
|
|
"--- блок найден: 0x{0:X}, длина {1} байт" -f $start, $oldLen
|
|
|
|
$newText = (@(
|
|
'title Ventoy (ISO образы)',
|
|
'chainloader (bd)/efi/Paravis/boot/g4d/Ventoy'
|
|
) -join "`n") + "`n"
|
|
$newBytes = [Text.Encoding]::UTF8.GetBytes($newText)
|
|
"--- новый блок: {0} байт" -f $newBytes.Length
|
|
if ($newBytes.Length -gt $oldLen) { throw "новый блок длиннее ($($newBytes.Length) > $oldLen)" }
|
|
$pad = New-Object byte[] ($oldLen - $newBytes.Length)
|
|
for ($i = 0; $i -lt $pad.Length; $i++) { $pad[$i] = 0x20 }
|
|
|
|
$out = New-Object System.Collections.Generic.List[byte]
|
|
$out.AddRange([byte[]]$b[0..($start-1)])
|
|
$out.AddRange($newBytes)
|
|
$out.AddRange($pad)
|
|
$out.AddRange([byte[]]$b[$blockEnd..($b.Length-1)])
|
|
[IO.File]::WriteAllBytes($f, $out.ToArray())
|
|
|
|
$nb = [IO.File]::ReadAllBytes($f)
|
|
"--- итог: {0} байт (было {1}, дельта {2})" -f $nb.Length, $b.Length, ($nb.Length - $b.Length)
|
|
"--- префикс до блока совпадает: " + $((0..($start-1) | ForEach-Object { $nb[$_] -ne $b[$_] } | Where-Object { $_ } | Measure-Object).Count -eq 0)
|
|
$t = [Text.Encoding]::UTF8.GetString($nb[$start..($blockEnd-1)])
|
|
($t -split "`n") | ForEach-Object { ' |' + $_ }
|
|
"--- GFM-ссылок осталось: " + ([regex]::Matches([Text.Encoding]::UTF8.GetString($nb),'Grub2 FileManager').Count)
|