| 1 | Subject: [PATCH] Open Windows pflash firmware through its Unicode working directory |
| 2 | |
| 3 | |
| 4 | new file mode 100644 |
| 5 | |
| 6 | |
| 7 | |
| 8 | @@ -0,0 +1,22 @@ |
| 9 | +// SPDX-FileCopyrightText: Copyright The Lima Authors |
| 10 | +// SPDX-License-Identifier: Apache-2.0 |
| 11 | + |
| 12 | +package qemu |
| 13 | + |
| 14 | +import ( |
| 15 | + "path/filepath" |
| 16 | + "unicode/utf8" |
| 17 | +) |
| 18 | + |
| 19 | +// The Windows QEMU build cannot reliably open a non-ASCII pflash argument. |
| 20 | +// CreateProcessW takes the working directory as UTF-16, so QEMU can open |
| 21 | +// the original firmware with its ASCII basename without making a stale copy. |
| 22 | +func windowsFirmwareArgument(path string) (argument, workingDir string) { |
| 23 | + dir := filepath.Dir(path) |
| 24 | + for _, r := range dir { |
| 25 | + if r >= utf8.RuneSelf && filepath.IsAbs(path) { |
| 26 | + return filepath.Base(path), dir |
| 27 | + } |
| 28 | + } |
| 29 | + return path, "" |
| 30 | +} |
| 31 | |
| 32 | new file mode 100644 |
| 33 | |
| 34 | |
| 35 | |
| 36 | @@ -0,0 +1,60 @@ |
| 37 | +// SPDX-FileCopyrightText: Copyright The Lima Authors |
| 38 | +// SPDX-License-Identifier: Apache-2.0 |
| 39 | + |
| 40 | +package qemu |
| 41 | + |
| 42 | +import ( |
| 43 | + "os" |
| 44 | + "path/filepath" |
| 45 | + "runtime" |
| 46 | + "testing" |
| 47 | + |
| 48 | + "github.com/lima-vm/lima/v2/pkg/limatype" |
| 49 | +) |
| 50 | + |
| 51 | +func TestWindowsFirmwareArgument(t *testing.T) { |
| 52 | + dir := filepath.Join(t.TempDir(), "测试安装, Cage", "share") |
| 53 | + if err := os.MkdirAll(dir, 0o700); err != nil { |
| 54 | + t.Fatal(err) |
| 55 | + } |
| 56 | + const name = "edk2-x86_64-code.fd" |
| 57 | + path := filepath.Join(dir, name) |
| 58 | + arg, cwd := windowsFirmwareArgument(path) |
| 59 | + if arg != name || cwd != dir { |
| 60 | + t.Fatalf("got argument %q and working directory %q", arg, cwd) |
| 61 | + } |
| 62 | + // Replacing the packaged firmware must also replace what QEMU reads. |
| 63 | + for _, revision := range []string{"first release", "updated release"} { |
| 64 | + if err := os.WriteFile(path, []byte(revision), 0o600); err != nil { |
| 65 | + t.Fatal(err) |
| 66 | + } |
| 67 | + data, err := os.ReadFile(filepath.Join(cwd, arg)) |
| 68 | + if err != nil || string(data) != revision { |
| 69 | + t.Fatalf("reading %q: got %q, %v", revision, data, err) |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestWindowsPackagedFirmwareBeatsOldUserCopy(t *testing.T) { |
| 75 | + if runtime.GOOS != "windows" { |
| 76 | + t.Skip("Windows QEMU distribution layout") |
| 77 | + } |
| 78 | + root := t.TempDir() |
| 79 | + home := filepath.Join(root, "用户") |
| 80 | + t.Setenv("USERPROFILE", home) |
| 81 | + binDir := filepath.Join(root, "安装目录", "qemu") |
| 82 | + const name = "edk2-x86_64-code.fd" |
| 83 | + bundled := filepath.Join(binDir, "share", name) |
| 84 | + for _, path := range []string{filepath.Join(home, ".local", "share", "qemu", name), bundled} { |
| 85 | + if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { |
| 86 | + t.Fatal(err) |
| 87 | + } |
| 88 | + if err := os.WriteFile(path, []byte(path), 0o600); err != nil { |
| 89 | + t.Fatal(err) |
| 90 | + } |
| 91 | + } |
| 92 | + got, err := getFirmware(filepath.Join(binDir, "qemu-system-x86_64.exe"), limatype.X8664) |
| 93 | + if err != nil || got != bundled { |
| 94 | + t.Fatalf("selected %q instead of the packaged %q: %v", got, bundled, err) |
| 95 | + } |
| 96 | +} |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | @@ -480,6 +480,10 @@ func resolveCPUType(y *limatype.LimaYAML, qemuVersion *semver.Version) string { |
| 102 | } |
| 103 | |
| 104 | func Cmdline(ctx context.Context, cfg Config) (exe string, args []string, err error) { |
| 105 | + return cmdline(ctx, cfg, nil) |
| 106 | +} |
| 107 | + |
| 108 | +func cmdline(ctx context.Context, cfg Config, workingDir *string) (exe string, args []string, err error) { |
| 109 | y := cfg.LimaYAML |
| 110 | exe, args, err = Exe(*y.Arch) |
| 111 | if err != nil { |
| 112 | @@ -647,7 +651,11 @@ func Cmdline(ctx context.Context, cfg Config) (exe string, args []string, err er |
| 113 | logrus.Infof("Using system firmware (%#q)", firmware) |
| 114 | } |
| 115 | if firmware != "" { |
| 116 | - args = append(args, "-drive", fmt.Sprintf("if=pflash,format=raw,readonly=on,file=%s", firmware)) |
| 117 | + if runtime.GOOS == "windows" && workingDir != nil { |
| 118 | + firmware, *workingDir = windowsFirmwareArgument(firmware) |
| 119 | + } |
| 120 | + // Commas in -drive filenames are escaped by doubling them. |
| 121 | + args = append(args, "-drive", "if=pflash,format=raw,readonly=on,file="+strings.ReplaceAll(firmware, ",", ",,")) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | @@ -1315,6 +1323,14 @@ func getFirmware(qemuExe string, arch limatype.Arch) (string, error) { |
| 126 | |
| 127 | relativePath := fmt.Sprintf("share/qemu/edk2-%s-code.fd", qemuEdk2Arch(arch)) |
| 128 | relativePathWin := fmt.Sprintf("share/edk2-%s-code.fd", qemuEdk2Arch(arch)) |
| 129 | + if runtime.GOOS == "windows" { |
| 130 | + // The firmware shipped beside QEMU belongs to that QEMU version. |
| 131 | + // Prefer it over an old manual copy in the user's .local directory. |
| 132 | + bundled := filepath.Join(binDir, relativePathWin) |
| 133 | + if _, err := os.Stat(bundled); err == nil || !errors.Is(err, os.ErrNotExist) { |
| 134 | + return bundled, err |
| 135 | + } |
| 136 | + } |
| 137 | candidates := []string{ |
| 138 | filepath.Join(userLocalDir, relativePath), // XDG-like |
| 139 | filepath.Join(localDir, relativePath), // macOS (homebrew) |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | @@ -422,7 +422,8 @@ func (l *LimaQemuDriver) Start(ctx context.Context) (chan error, error) { |
| 145 | socks.dgramConn.Close() |
| 146 | } |
| 147 | }() |
| 148 | - qExe, qArgs, err := Cmdline(ctx, qCfg) |
| 149 | + var workingDir string |
| 150 | + qExe, qArgs, err := cmdline(ctx, qCfg, &workingDir) |
| 151 | if err != nil { |
| 152 | return nil, err |
| 153 | } |
| 154 | @@ -468,6 +469,7 @@ func (l *LimaQemuDriver) Start(ctx context.Context) (chan error, error) { |
| 155 | qArgsFinal = append(qArgsFinal, applied) |
| 156 | } |
| 157 | qCmd := exec.CommandContext(ctx, qExe, qArgsFinal...) |
| 158 | + qCmd.Dir = workingDir |
| 159 | qCmd.ExtraFiles = append(qCmd.ExtraFiles, applier.files...) |
| 160 | qCmd.SysProcAttr = executil.BackgroundSysProcAttr |
| 161 | qStdout, err := qCmd.StdoutPipe() |