Files
Reims-Go/internal/app/opencore_flow.go
T

131 lines
4.5 KiB
Go

package app
import (
"fmt"
"github.com/Nwflower/reims-go/internal/macos/opencore"
"github.com/Nwflower/reims-go/internal/macos/reims"
"github.com/Nwflower/reims-go/internal/macos/startprep"
rt "github.com/Nwflower/reims-go/internal/runtime"
)
// populateGuestLangDetail fills VMDetail.GuestLang from the VM's persisted
// sidecar preference, falling back to reading prev-lang:kbd directly from its
// OpenCore image so existing VMs accurately reflect their boot/recovery locale.
func (a *App) populateGuestLangDetail(d *reims.VMDetail) {
if d == nil || d.Directory == "" {
return
}
if saved := opencore.ReadSavedGuestLang(d.Directory); saved != "" {
d.GuestLang = saved
return
}
if ocPath := opencore.ResolveOpenCorePath(d.Directory); ocPath != "" {
if lang, err := opencore.ReadImagePrevLang(ocPath); err == nil && lang != "" {
d.GuestLang = lang
}
}
}
// prep returns the shared start/OpenCore helpers (internal/vmstart) for one VM,
// logging to that VM's log panel.
func (a *App) prep(name string) (*startprep.Prep, error) {
if err := a.requireMacOSEnabled(); err != nil {
return nil, err
}
return startprep.New(a.runtimeConfig(), func(line string) { a.emitVMLog(name, line) })
}
// GetVMSMBIOS reads the VM's OpenCore PlatformInfo > Generic identity
// (SystemProductName, SystemSerialNumber, MLB, SystemUUID, and read-only ROM).
// Values are returned directly to the UI and never written to app or VM logs.
func (a *App) GetVMSMBIOS(name string) (opencore.SMBIOSIdentity, error) {
if err := a.requireMacOSEnabled(); err != nil {
return opencore.SMBIOSIdentity{}, err
}
cfgCopy := a.runtimeConfig()
active, err := rt.ResolveActive(&cfgCopy)
if err != nil {
return opencore.SMBIOSIdentity{}, err
}
c := reims.NewClient(&cfgCopy, *active)
d, err := c.Show(a.baseCtx(), name)
if err != nil {
return opencore.SMBIOSIdentity{}, err
}
ocPath := opencore.ResolveOpenCorePath(d.Directory)
if ocPath == "" {
return opencore.SMBIOSIdentity{}, fmt.Errorf("VM %s has no OpenCore image", name)
}
return opencore.ReadImageSMBIOS(ocPath)
}
// GenerateVMSMBIOS creates a cryptographically random, format-valid Mac SMBIOS
// candidate (Serial, Base-34 checksummed MLB, and UUID v4) for the given model
// (defaulting to iMac20,1) without modifying disk state.
func (a *App) GenerateVMSMBIOS(model string) (opencore.SMBIOSIdentity, error) {
return opencore.GenerateSMBIOS(model)
}
// ApplyVMSMBIOS writes new SystemSerialNumber, MLB, and SystemUUID values into
// a stopped VM's OpenCore image while keeping ROM untouched so it stays synced
// with the VM's network MAC address.
func (a *App) ApplyVMSMBIOS(name string, id opencore.SMBIOSIdentity) (opencore.SMBIOSIdentity, error) {
if err := a.requireMacOSEnabled(); err != nil {
return opencore.SMBIOSIdentity{}, err
}
if err := a.requireWritableDataRoot(); err != nil {
return opencore.SMBIOSIdentity{}, err
}
norm, err := opencore.NormalizeAndValidateSMBIOS(id)
if err != nil {
return opencore.SMBIOSIdentity{}, err
}
cfgCopy := a.runtimeConfig()
active, err := rt.ResolveActive(&cfgCopy)
if err != nil {
return opencore.SMBIOSIdentity{}, err
}
c := reims.NewClient(&cfgCopy, *active)
ctx := a.baseCtx()
d, release, err := startprep.LockVM(ctx, c, name)
if err != nil {
return opencore.SMBIOSIdentity{}, err
}
defer release()
if c.IsRunning(ctx, d) {
return opencore.SMBIOSIdentity{}, fmt.Errorf("stop %s before updating its SMBIOS identity", name)
}
if d.State == "suspended" {
return opencore.SMBIOSIdentity{}, fmt.Errorf("resume or discard suspend state for %s before updating its SMBIOS identity", name)
}
if info, serr := reims.ReadSuspendInfo(d.Directory); serr == nil && info != nil {
return opencore.SMBIOSIdentity{}, fmt.Errorf("resume or discard suspend state for %s before updating its SMBIOS identity", name)
}
ocPath := opencore.ResolveOpenCorePath(d.Directory)
if ocPath == "" {
return opencore.SMBIOSIdentity{}, fmt.Errorf("VM %s has no OpenCore image", name)
}
env := rt.BuildChildEnv(&cfgCopy, *active)
changed, err := opencore.PatchImageSMBIOS(ctx, ocPath, norm, active.QemuImgPath(), env)
if err != nil {
return opencore.SMBIOSIdentity{}, err
}
updated, err := opencore.ReadImageSMBIOS(ocPath)
if err != nil {
return opencore.SMBIOSIdentity{}, err
}
if changed {
modelLabel := updated.Model
if modelLabel == "" {
modelLabel = "default"
}
// Never log Serial, MLB, UUID, or ROM into VM/app logs.
a.emitVMLog(name, fmt.Sprintf("Updated OpenCore SMBIOS identity (%s)", modelLabel))
a.emitStateChanged()
}
return updated, nil
}