- Pure-Go HFS+ offline helper disk (REIMSHELP) bypassing Recovery's missing pkgutil & read-only installer target, with automatic blank-disk APFS formatting and non-blank safety confirmation - Recovery mode UX: rename Finish install to Exit recovery mode, add Enter recovery mode via OpenCore boot picker (--boot-selection), show Attach offline image on VM page with ~1 GB Apple online recovery fallback download - Pre-apply WHPX kernel-irqchip=off when nested-virt probe fails and filter benign TSC deadline timer warnings - QMP keyboard typing (type clipboard / one-click Recovery command) and offline-install network toggle
188 lines
5.2 KiB
Go
188 lines
5.2 KiB
Go
package qmp
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// Typing text into a guest as key presses ("paste as keystrokes"). The guest
|
|
// sees a USB keyboard (`usb-kbd` in `reims-ctl command`, upstream 3731afd), so
|
|
// what each key produces depends on the guest's input source: the mapping
|
|
// below is the US / "ABC" layout macOS Recovery and a new install start with.
|
|
|
|
// Stroke is one key, pressed with or without Shift.
|
|
type Stroke struct {
|
|
Key string // QKeyCode name, as QMP's input-send-event takes it
|
|
Shift bool
|
|
}
|
|
|
|
// MaxTypedRunes caps one job: at ~30 ms a key, 4000 keys take two minutes.
|
|
const MaxTypedRunes = 4000
|
|
|
|
// UntypeableError names the first character a US keyboard can't type.
|
|
type UntypeableError struct {
|
|
Rune rune
|
|
Pos int // 1-based character position
|
|
}
|
|
|
|
func (e *UntypeableError) Error() string {
|
|
return fmt.Sprintf("character %q (U+%04X) at position %d can't be typed on a US keyboard", e.Rune, e.Rune, e.Pos)
|
|
}
|
|
|
|
var plainKeys = map[rune]string{
|
|
' ': "spc", '\n': "ret", '\t': "tab",
|
|
'-': "minus", '=': "equal", '[': "bracket_left", ']': "bracket_right",
|
|
'\\': "backslash", ';': "semicolon", '\'': "apostrophe", '`': "grave_accent",
|
|
',': "comma", '.': "dot", '/': "slash",
|
|
}
|
|
|
|
var shiftedKeys = map[rune]string{
|
|
'!': "1", '@': "2", '#': "3", '$': "4", '%': "5", '^': "6", '&': "7", '*': "8", '(': "9", ')': "0",
|
|
'_': "minus", '+': "equal", '{': "bracket_left", '}': "bracket_right", '|': "backslash",
|
|
':': "semicolon", '"': "apostrophe", '~': "grave_accent", '<': "comma", '>': "dot", '?': "slash",
|
|
}
|
|
|
|
// StrokeFor maps one character to a key on a US layout.
|
|
func StrokeFor(r rune) (Stroke, bool) {
|
|
switch {
|
|
case r >= 'a' && r <= 'z', r >= '0' && r <= '9':
|
|
return Stroke{Key: string(r)}, true
|
|
case r >= 'A' && r <= 'Z':
|
|
return Stroke{Key: string(r - 'A' + 'a'), Shift: true}, true
|
|
}
|
|
if k, ok := plainKeys[r]; ok {
|
|
return Stroke{Key: k}, true
|
|
}
|
|
if k, ok := shiftedKeys[r]; ok {
|
|
return Stroke{Key: k, Shift: true}, true
|
|
}
|
|
return Stroke{}, false
|
|
}
|
|
|
|
// Strokes turns text into key presses. It refuses the whole text if any
|
|
// character can't be typed, so a command is never typed half-way. Windows line
|
|
// ends become one Return.
|
|
func Strokes(text string) ([]Stroke, error) {
|
|
text = strings.ReplaceAll(text, "\r\n", "\n")
|
|
text = strings.ReplaceAll(text, "\r", "\n")
|
|
out := make([]Stroke, 0, len(text))
|
|
pos := 0
|
|
for _, r := range text {
|
|
pos++
|
|
if pos > MaxTypedRunes {
|
|
return nil, fmt.Errorf("text is longer than %d characters", MaxTypedRunes)
|
|
}
|
|
s, ok := StrokeFor(r)
|
|
if !ok {
|
|
return nil, &UntypeableError{Rune: r, Pos: pos}
|
|
}
|
|
out = append(out, s)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// Executor is the part of a QMP session typing needs.
|
|
type Executor interface {
|
|
Execute(command string, args any) error
|
|
}
|
|
|
|
func keyEvent(key string, down bool) map[string]any {
|
|
return map[string]any{
|
|
"type": "key",
|
|
"data": map[string]any{
|
|
"down": down,
|
|
"key": map[string]any{"type": "qcode", "data": key},
|
|
},
|
|
}
|
|
}
|
|
|
|
func sendKeys(q Executor, down bool, keys ...string) error {
|
|
events := make([]map[string]any, 0, len(keys))
|
|
for _, k := range keys {
|
|
events = append(events, keyEvent(k, down))
|
|
}
|
|
return q.Execute("input-send-event", map[string]any{"events": events})
|
|
}
|
|
|
|
// TypeStrokes presses and releases each key in turn, holding it for hold and
|
|
// waiting gap before the next, so the guest's USB keyboard queue never fills.
|
|
// On any error or cancel it releases Shift so no modifier is left held down.
|
|
func TypeStrokes(ctx context.Context, q Executor, strokes []Stroke, hold, gap time.Duration, progress func(done int)) (err error) {
|
|
shiftDown := false
|
|
defer func() {
|
|
if shiftDown {
|
|
_ = sendKeys(q, false, "shift")
|
|
}
|
|
}()
|
|
for i, s := range strokes {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
if s.Shift != shiftDown {
|
|
if err := sendKeys(q, s.Shift, "shift"); err != nil {
|
|
return err
|
|
}
|
|
shiftDown = s.Shift
|
|
}
|
|
if err := sendKeys(q, true, s.Key); err != nil {
|
|
return err
|
|
}
|
|
sleep(ctx, hold)
|
|
if err := sendKeys(q, false, s.Key); err != nil {
|
|
return err
|
|
}
|
|
sleep(ctx, gap)
|
|
if progress != nil {
|
|
progress(i + 1)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SocketPath is the QMP socket reims-ctl gives every VM it starts: `-qmp
|
|
// unix:<dir>\qmp.sock,server=on,wait=off` (`reims-ctl command`, upstream
|
|
// 3731afd). It exists on every host, unlike the Win10 cold-reboot TCP monitor.
|
|
func SocketPath(vmDir string) string {
|
|
return filepath.Join(vmDir, "qmp.sock")
|
|
}
|
|
|
|
// TypeIntoVM types text into a running VM through its QMP socket and returns
|
|
// how many keys it pressed.
|
|
func TypeIntoVM(ctx context.Context, vmDir, text string, progress func(done, total int)) (int, error) {
|
|
strokes, err := Strokes(text)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if len(strokes) == 0 {
|
|
return 0, fmt.Errorf("nothing to type")
|
|
}
|
|
q, err := Dial(ctx, "unix", SocketPath(vmDir))
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer q.Close()
|
|
done := 0
|
|
err = TypeStrokes(ctx, q, strokes, 20*time.Millisecond, 15*time.Millisecond, func(n int) {
|
|
done = n
|
|
if progress != nil {
|
|
progress(n, len(strokes))
|
|
}
|
|
})
|
|
return done, err
|
|
}
|
|
|
|
func sleep(ctx context.Context, d time.Duration) {
|
|
if d <= 0 {
|
|
return
|
|
}
|
|
t := time.NewTimer(d)
|
|
defer t.Stop()
|
|
select {
|
|
case <-ctx.Done():
|
|
case <-t.C:
|
|
}
|
|
}
|