Initial import of ViperOS
This commit is contained in:
91
Scripts/check_disk.py
Normal file
91
Scripts/check_disk.py
Normal file
@@ -0,0 +1,91 @@
|
||||
import struct
|
||||
f = open(r'd:\Users\TermiNexus\Desktop\TransPyC\ViperOS\disk.img', 'rb')
|
||||
f.seek(0)
|
||||
first_sector = f.read(512)
|
||||
first_byte = first_sector[0]
|
||||
|
||||
if first_byte == 0xEB or first_byte == 0xE9:
|
||||
lba_start = 0
|
||||
bpb = first_sector
|
||||
else:
|
||||
ptype = struct.unpack_from('<B', first_sector, 450)[0]
|
||||
lba_start = struct.unpack_from('<I', first_sector, 454)[0]
|
||||
f.seek(lba_start * 512)
|
||||
bpb = f.read(512)
|
||||
|
||||
bytes_per_sec = struct.unpack_from('<H', bpb, 11)[0]
|
||||
sec_per_clus = struct.unpack_from('<B', bpb, 13)[0]
|
||||
rsvd = struct.unpack_from('<H', bpb, 14)[0]
|
||||
num_fats = struct.unpack_from('<B', bpb, 16)[0]
|
||||
root_clus = struct.unpack_from('<I', bpb, 44)[0]
|
||||
fat_size = struct.unpack_from('<I', bpb, 36)[0]
|
||||
data_start = rsvd + num_fats * fat_size
|
||||
|
||||
print(f'BPS={bytes_per_sec} SPC={sec_per_clus} rsvd={rsvd} NF={num_fats} root_clus={root_clus} fat_size={fat_size} data_start={data_start}')
|
||||
|
||||
def read_dir(f, lba_start, data_start, sec_per_clus, bytes_per_sec, cluster, indent=" ", depth=0):
|
||||
if depth > 5:
|
||||
return
|
||||
cluster_sec = data_start + (cluster - 2) * sec_per_clus
|
||||
f.seek((lba_start + cluster_sec) * 512)
|
||||
dir_data = f.read(sec_per_clus * bytes_per_sec)
|
||||
off = 0
|
||||
lfn_parts = {}
|
||||
while off < len(dir_data):
|
||||
first = dir_data[off]
|
||||
if first == 0:
|
||||
break
|
||||
if first == 0xE5:
|
||||
off += 32
|
||||
lfn_parts = {}
|
||||
continue
|
||||
attr = dir_data[off + 11]
|
||||
if attr == 0x0F: # LFN entry
|
||||
seq = first & 0x3F
|
||||
lfn_chars = []
|
||||
for i in range(5):
|
||||
c = struct.unpack_from('<H', dir_data, off + 2 + i * 2)[0]
|
||||
if c != 0xFFFF and c != 0:
|
||||
lfn_chars.append(chr(c))
|
||||
for i in range(6):
|
||||
c = struct.unpack_from('<H', dir_data, off + 14 + i * 2)[0]
|
||||
if c != 0xFFFF and c != 0:
|
||||
lfn_chars.append(chr(c))
|
||||
for i in range(2):
|
||||
c = struct.unpack_from('<H', dir_data, off + 28 + i * 2)[0]
|
||||
if c != 0xFFFF and c != 0:
|
||||
lfn_chars.append(chr(c))
|
||||
lfn_parts[seq] = ''.join(lfn_chars)
|
||||
off += 32
|
||||
continue
|
||||
if attr == 8: # Volume label
|
||||
off += 32
|
||||
lfn_parts = {}
|
||||
continue
|
||||
name_bytes = dir_data[off:off + 11]
|
||||
name_str = name_bytes.decode('ascii', errors='replace')
|
||||
nt_case = dir_data[off + 12]
|
||||
clus_hi = struct.unpack_from('<H', dir_data, off + 20)[0]
|
||||
clus_lo = struct.unpack_from('<H', dir_data, off + 26)[0]
|
||||
clus = (clus_hi << 16) | clus_lo
|
||||
fsize = struct.unpack_from('<I', dir_data, off + 28)[0]
|
||||
# Reconstruct LFN
|
||||
lfn = ''
|
||||
if lfn_parts:
|
||||
lfn = ''.join(lfn_parts.get(i, '') for i in sorted(lfn_parts.keys()))
|
||||
case_info = ""
|
||||
if nt_case & 0x08:
|
||||
case_info += " [base_lower]"
|
||||
if nt_case & 0x10:
|
||||
case_info += " [ext_lower]"
|
||||
print(f'{indent}SFN=[{name_str}] NT=0x{nt_case:02X}{case_info} LFN="{lfn}" attr=0x{attr:02X} clus={clus} size={fsize}')
|
||||
if attr & 0x10 and clus >= 2:
|
||||
# Skip . and .. entries
|
||||
sfn_name = name_str.rstrip()
|
||||
if sfn_name != '.' and sfn_name != '..':
|
||||
read_dir(f, lba_start, data_start, sec_per_clus, bytes_per_sec, clus, indent + " ", depth + 1)
|
||||
off += 32
|
||||
lfn_parts = {}
|
||||
|
||||
read_dir(f, lba_start, data_start, sec_per_clus, bytes_per_sec, root_clus)
|
||||
f.close()
|
||||
90
Scripts/disk.ps1
Normal file
90
Scripts/disk.ps1
Normal file
@@ -0,0 +1,90 @@
|
||||
# Auto-elevate to administrator
|
||||
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
|
||||
$maxRetries = 5
|
||||
$retryCount = 0
|
||||
$launched = $false
|
||||
|
||||
while ($retryCount -lt $maxRetries -and -not $launched) {
|
||||
$retryCount++
|
||||
try {
|
||||
Write-Host "[Elevate $retryCount/$maxRetries] Requesting admin privileges..." -ForegroundColor Cyan
|
||||
$proc = Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs -Wait -PassThru -ErrorAction Stop
|
||||
if ($null -ne $proc) {
|
||||
$launched = $true
|
||||
}
|
||||
}
|
||||
catch {
|
||||
Write-Host "Elevation failed: user cancelled or permission error" -ForegroundColor Yellow
|
||||
if ($retryCount -ge $maxRetries) {
|
||||
Write-Host "Max retries ($maxRetries) reached, exiting." -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
Start-Sleep -Milliseconds 300
|
||||
}
|
||||
}
|
||||
|
||||
if ($launched) {
|
||||
exit 0
|
||||
}
|
||||
exit 1
|
||||
}
|
||||
|
||||
Write-Host "Running as administrator!" -ForegroundColor Green
|
||||
|
||||
# ==========================================================
|
||||
# ImDisk-based FAT32 disk image builder
|
||||
# ==========================================================
|
||||
|
||||
$SCRIPT_DIR = Split-Path -Parent $MyInvocation.MyCommand.Definition
|
||||
$PROJECT_DIR = Split-Path -Parent $SCRIPT_DIR
|
||||
$DISK_IMG = Join-Path $PROJECT_DIR "disk.img"
|
||||
$DISK_DIR = Join-Path $PROJECT_DIR "disk"
|
||||
$MOUNT_LETTER = "Z:"
|
||||
|
||||
$IMAGE_SIZE = 64 * 1024 * 1024
|
||||
|
||||
Write-Host "=== Creating disk image ===" -ForegroundColor Cyan
|
||||
|
||||
if (-not (Get-Command "imdisk.exe" -ErrorAction SilentlyContinue)) {
|
||||
Write-Host "Error: Cannot find imdisk.exe. Please ensure ImDisk is installed and added to system PATH." -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
|
||||
if (-not (Test-Path $DISK_DIR)) {
|
||||
Write-Host "Error: Source directory not found $DISK_DIR" -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
|
||||
if (Test-Path $DISK_IMG) {
|
||||
Write-Host "Removing existing disk image..."
|
||||
Remove-Item -Force $DISK_IMG
|
||||
}
|
||||
|
||||
Write-Host "Creating $IMAGE_SIZE byte disk image..."
|
||||
$fsutilFile = [System.IO.File]::Create($DISK_IMG)
|
||||
$fsutilFile.SetLength($IMAGE_SIZE)
|
||||
$fsutilFile.Close()
|
||||
|
||||
if (Test-Path $MOUNT_LETTER) {
|
||||
Write-Host "Warning: $MOUNT_LETTER already exists. Attempting to detach..." -ForegroundColor Yellow
|
||||
imdisk -D -m $MOUNT_LETTER
|
||||
Start-Sleep -Seconds 1
|
||||
}
|
||||
|
||||
Write-Host "Mounting and formatting disk image to $MOUNT_LETTER ..."
|
||||
imdisk -a -f $DISK_IMG -m $MOUNT_LETTER -o hd --% -p "/fs:fat32 /q /y"
|
||||
|
||||
Start-Sleep -Seconds 2
|
||||
|
||||
Write-Host "Copying files to disk image..."
|
||||
robocopy $DISK_DIR "$MOUNT_LETTER\" /E /NJH /NJS /np
|
||||
if ($LASTEXITCODE -le 7) {
|
||||
Write-Host "Files copied successfully." -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "Warning: File copy encountered an error. Robocopy exit code: $LASTEXITCODE" -ForegroundColor Yellow
|
||||
}
|
||||
|
||||
Write-Host "Unmounting disk image..."
|
||||
imdisk -D -m $MOUNT_LETTER
|
||||
|
||||
Write-Host "Disk image created: $DISK_IMG" -ForegroundColor Green
|
||||
40
Scripts/disk.sh
Normal file
40
Scripts/disk.sh
Normal file
@@ -0,0 +1,40 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
|
||||
DISK_IMG="$PROJECT_DIR/disk.img"
|
||||
DISK_DIR="$PROJECT_DIR/disk"
|
||||
MOUNT_POINT="/tmp/diskimg"
|
||||
|
||||
SUDO_PASSWORD="198314qwe"
|
||||
|
||||
echo "=== Creating disk image ==="
|
||||
|
||||
cd "$PROJECT_DIR"
|
||||
|
||||
IMAGE_SIZE=$((64 * 1024 * 1024))
|
||||
|
||||
if [ -f "$DISK_IMG" ]; then
|
||||
echo "Removing existing disk image..."
|
||||
rm -f "$DISK_IMG"
|
||||
fi
|
||||
|
||||
echo "Creating $IMAGE_SIZE byte disk image..."
|
||||
dd if=/dev/zero of="$DISK_IMG" bs=1M count=64 status=none
|
||||
|
||||
echo "Formatting disk image as FAT32..."
|
||||
mkfs.vfat -F 32 "$DISK_IMG" > /dev/null
|
||||
|
||||
echo "Mounting disk image..."
|
||||
mkdir -p "$MOUNT_POINT"
|
||||
echo "$SUDO_PASSWORD" | sudo -S mount -o loop "$DISK_IMG" "$MOUNT_POINT"
|
||||
|
||||
echo "Copying files to disk image..."
|
||||
echo "$SUDO_PASSWORD" | sudo -S cp -r "$DISK_DIR"/* "$MOUNT_POINT/"
|
||||
|
||||
echo "Unmounting disk image..."
|
||||
echo "$SUDO_PASSWORD" | sudo -S umount "$MOUNT_POINT"
|
||||
rmdir "$MOUNT_POINT"
|
||||
|
||||
echo "Disk image created: $DISK_IMG"
|
||||
Reference in New Issue
Block a user