74 lines
3.4 KiB
PowerShell
74 lines
3.4 KiB
PowerShell
# Полная пересборка меню g4d из чистого бэкапа: KRD 24 + Ventoy (13 пробелов).
|
|
# Результат пишется в рабочий F:\grldr и в шаблон efi\Paravis\Boot\g4d\grldr.
|
|
$ErrorActionPreference = 'Stop'
|
|
$base = 'C:\PF\Ventoy\backup\g4d-grldr-KRD18.bin'
|
|
$targets = @('F:\grldr', 'F:\efi\Paravis\Boot\g4d\grldr')
|
|
|
|
function Find-Bytes([byte[]]$hay, [byte[]]$needle, [int]$from = 0) {
|
|
for ($i = $from; $i -le $hay.Length - $needle.Length; $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
|
|
}
|
|
function Replace-Block([byte[]]$data, [byte[]]$startNeedle, [byte[]]$endNeedle, [string]$newText) {
|
|
$s = Find-Bytes $data $startNeedle
|
|
if ($s -lt 0) { throw 'начало блока не найдено' }
|
|
$e = Find-Bytes $data $endNeedle $s
|
|
if ($e -lt 0) { throw 'конец блока не найден' }
|
|
$endPos = $e + $endNeedle.Length
|
|
$oldLen = $endPos - $s
|
|
$nb = [Text.Encoding]::UTF8.GetBytes($newText)
|
|
if ($nb.Length -gt $oldLen) { throw ("блок не влезает: {0} > {1}" -f $nb.Length, $oldLen) }
|
|
$pad = New-Object byte[] ($oldLen - $nb.Length)
|
|
for ($i = 0; $i -lt $pad.Length; $i++) { $pad[$i] = 0x20 }
|
|
$out = New-Object System.Collections.Generic.List[byte]
|
|
$out.AddRange([byte[]]$data[0..($s-1)])
|
|
$out.AddRange($nb); $out.AddRange($pad)
|
|
$out.AddRange([byte[]]$data[$endPos..($data.Length-1)])
|
|
return ,$out.ToArray()
|
|
}
|
|
|
|
$b = [IO.File]::ReadAllBytes($base)
|
|
"--- база: {0} байт" -f $b.Length
|
|
|
|
# 1) KRD 18 -> 24
|
|
$krdNew = "title" + (' ' * 10) + "Kaspersky Rescue Disk 24`n" +
|
|
"set path=/efi/Paravis/KRD`n" +
|
|
"set lang=ru`n" +
|
|
"find --set-root %path%/live/vmlinuz`n" +
|
|
"kernel`t%path%/live/vmlinuz boot=live components live-media-path=%path%/live locales=ru_RU.UTF-8`n" +
|
|
"initrd`t%path%/live/initrd.img`n"
|
|
$b = Replace-Block $b ([Text.Encoding]::UTF8.GetBytes('title Kaspersky Rescue Disk 18')) ([Text.Encoding]::UTF8.GetBytes("initrd`t%path%/initrd.xz`n")) $krdNew
|
|
"--- после KRD: {0} байт" -f $b.Length
|
|
|
|
# 2) Grub2 FileManager -> Ventoy
|
|
$vtNew = "title" + (' ' * 13) + "Ventoy (ISO образы)`n" +
|
|
"chainloader (bd)/efi/Paravis/boot/g4d/Ventoy`n"
|
|
$b = Replace-Block $b ([Text.Encoding]::UTF8.GetBytes('title Grub2 FileManager')) ([Text.Encoding]::UTF8.GetBytes("`nboot`n")) $vtNew
|
|
"--- после Ventoy: {0} байт" -f $b.Length
|
|
|
|
if (Get-Process -Name vmware-vmx -ErrorAction SilentlyContinue) { throw 'VM запущена — отмена' }
|
|
foreach ($t in $targets) {
|
|
[IO.File]::WriteAllBytes($t, $b)
|
|
"--- записан: {0} ({1} байт)" -f $t, (Get-Item $t).Length
|
|
}
|
|
|
|
# 3) проверка пар "title -> команда"
|
|
$s = [Text.Encoding]::UTF8.GetString($b)
|
|
$lines = $s -split "`n"
|
|
$res = @()
|
|
for ($i = 0; $i -lt $lines.Count; $i++) {
|
|
if ($lines[$i] -match '^title\s*(.*)$') {
|
|
$ttl = $Matches[1].Trim()
|
|
$cmd = ''
|
|
for ($k = $i + 1; $k -lt [Math]::Min($i + 4, $lines.Count); $k++) {
|
|
if ($lines[$k] -match '^\s*(chainloader|kernel|map|halt|reboot|set|configfile|find)') { $cmd = $lines[$k].Trim(); break }
|
|
}
|
|
$res += (' {0,-42} -> {1}' -f $ttl, $cmd)
|
|
}
|
|
}
|
|
'--- итоговое меню:'
|
|
$res | Select-Object -First 20
|