55 lines
2.4 KiB
PowerShell
55 lines
2.4 KiB
PowerShell
# Замена пункта Grub2 FileManager на Ventoy в основном меню grub4dos (g4d\grldr).
|
|
$ErrorActionPreference = 'Stop'
|
|
$f = 'F:\efi\Paravis\Boot\g4d\grldr'
|
|
$bak = 'C:\PF\Ventoy\backup\g4d-grldr-GFM.bin'
|
|
|
|
$b = [IO.File]::ReadAllBytes($f)
|
|
$text = [Text.Encoding]::UTF8.GetString($b)
|
|
|
|
$startMarker = 'title Grub2 FileManager'
|
|
$start = $text.IndexOf($startMarker)
|
|
if ($start -lt 0) { throw 'GFM-блок не найден' }
|
|
|
|
# конец блока: первый "\nboot\n" после начала
|
|
$bootIdx = $text.IndexOf("`nboot`n", $start)
|
|
if ($bootIdx -lt 0) { throw 'конец GFM-блока не найден' }
|
|
$blockEnd = $bootIdx + ("`nboot`n").Length
|
|
$oldBlock = $text.Substring($start, $blockEnd - $start)
|
|
"--- старый блок ({0} байт):" -f ([Text.Encoding]::UTF8.GetByteCount($oldBlock))
|
|
($oldBlock -split "`n") | ForEach-Object { ' |' + $_ }
|
|
|
|
$newText = @(
|
|
'title Ventoy (ISO образы)',
|
|
'map --unmap=0:0x03 > nul',
|
|
'map --unhook',
|
|
'find --set-root /ventoy/core.img',
|
|
'kernel /ventoy/core.img',
|
|
'boot'
|
|
) -join "`n"
|
|
$newText += "`n"
|
|
|
|
$oldLen = [Text.Encoding]::UTF8.GetByteCount($oldBlock)
|
|
$newBytes = [Text.Encoding]::UTF8.GetBytes($newText)
|
|
if ($newBytes.Length -gt $oldLen) { throw ("новый блок длиннее: {0} > {1}" -f $newBytes.Length, $oldLen) }
|
|
$pad = New-Object byte[] ($oldLen - $newBytes.Length)
|
|
for ($i = 0; $i -lt $pad.Length; $i++) { $pad[$i] = 0x20 }
|
|
|
|
$startByte = [Text.Encoding]::UTF8.GetByteCount($text.Substring(0, $start))
|
|
$endByte = $startByte + $oldLen
|
|
|
|
if (-not (Test-Path $bak)) { Copy-Item $f $bak -Force; ' бэкап: ' + $bak }
|
|
Set-ItemProperty -Path $f -Name IsReadOnly -Value $false -ErrorAction SilentlyContinue
|
|
|
|
$out = New-Object System.Collections.Generic.List[byte]
|
|
$out.AddRange([byte[]]$b[0..($startByte-1)])
|
|
$out.AddRange($newBytes); $out.AddRange($pad)
|
|
$out.AddRange([byte[]]$b[$endByte..($b.Length-1)])
|
|
[IO.File]::WriteAllBytes($f, $out.ToArray())
|
|
|
|
"--- размер: {0} (было {1})" -f (Get-Item $f).Length, $b.Length
|
|
$chk = [Text.Encoding]::UTF8.GetString([IO.File]::ReadAllBytes($f))
|
|
$i2 = $chk.IndexOf('title Ventoy')
|
|
"--- новый блок:"
|
|
$chk.Substring($i2, 180) -split "`n" | Select-Object -First 7 | ForEach-Object { ' |' + $_ }
|
|
"--- остались ссылки: GFM={0}, gfm.iso={1}" -f ([regex]::Matches($chk,'Grub2 FileManager').Count), ([regex]::Matches($chk,'gfm\.iso').Count)
|