Files
flash-build/backup-flash-sectors.ps1
T
2026-11-13 07:52:38 +03:00

63 lines
2.7 KiB
PowerShell

# Часть 2: дамп загрузочных областей флешки (MBR, таблица разделов, VBR).
# Требует прав администратора (чтение \\.\PhysicalDriveN).
$ErrorActionPreference = 'Stop'
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
throw 'Нужны права администратора.'
}
$dest = 'C:\PF\flash-backup-2026-10-30\00-disk'
New-Item -ItemType Directory -Force $dest | Out-Null
Start-Transcript -Path (Join-Path $dest '_dump.log') -Force | Out-Null
$diskNo = 2
$dev = '\\.\PhysicalDrive' + $diskNo
$parts = Get-Partition -DiskNumber $diskNo | Sort-Object Offset
$info = @()
$info += 'Диск: PhysicalDrive' + $diskNo
$info += (Get-Disk -Number $diskNo | Format-List * | Out-String)
$info += ''
$info += 'Разделы:'
foreach ($p in $parts) {
$info += (' #{0} {1} offset={2} байт ({3:N0} МБ) размер={4:N0} МБ тип={5} active={6}' -f `
$p.PartitionNumber, $p.DriveLetter, $p.Offset, ($p.Offset/1MB), ($p.Size/1MB), $p.Type, $p.IsActive)
}
$info | Out-File -Encoding utf8 (Join-Path $dest 'diskinfo.txt')
function Dump([string]$file, [long]$offset, [int]$bytes) {
$fs = [IO.File]::Open($dev, 'Open', 'Read', 'ReadWrite')
try {
$fs.Seek($offset, 'Begin') | Out-Null
$buf = New-Object byte[] $bytes
$read = 0
while ($read -lt $bytes) {
$n = $fs.Read($buf, $read, $bytes - $read)
if ($n -le 0) { break }
$read += $n
}
[IO.File]::WriteAllBytes((Join-Path $dest $file), $buf[0..($read-1)])
$h = (Get-FileHash (Join-Path $dest $file) -Algorithm SHA256).Hash
' {0,-18} {1,10:N0} байт sha256={2}' -f $file, $read, $h
} finally { $fs.Close() }
}
'--- дамп загрузочных областей:'
Dump 'mbr-1MiB.bin' 0 (1MB) # MBR + код загрузчика в зазоре
foreach ($p in $parts) {
$name = 'p{0}-vbr-1MiB.bin' -f $p.PartitionNumber
Dump $name $p.Offset (1MB)
}
# расшифровка MBR
$mbr = [IO.File]::ReadAllBytes((Join-Path $dest 'mbr-1MiB.bin'))
$sig = '{0:X2}{1:X2}' -f $mbr[510], $mbr[511]
$diskSig = '{0:X2}{1:X2}{2:X2}{3:X2}' -f $mbr[440],$mbr[441],$mbr[442],$mbr[443]
$tbl = @('MBR подпись: 0x' + $sig, 'Disk signature: 0x' + $diskSig, '',
'Таблица разделов (hex):')
for ($i = 0; $i -lt 4; $i++) {
$o = 446 + $i*16
$tbl += (' #{0}: ' -f ($i+1)) + (($mbr[$o..($o+15)] | ForEach-Object { '{0:X2}' -f $_ }) -join ' ')
}
$tbl | Out-File -Encoding utf8 (Join-Path $dest 'mbr-info.txt')
$tbl
Stop-Transcript | Out-Null