Skip to content

Commit

Permalink
Refactor parse inf driver class guid
Browse files Browse the repository at this point in the history
Signed-off-by: JUN JIE NAN <[email protected]>
  • Loading branch information
nanjj committed May 13, 2024
1 parent 57ef557 commit 63db092
Show file tree
Hide file tree
Showing 15 changed files with 166 additions and 45 deletions.
57 changes: 14 additions & 43 deletions distrobuilder/main_repack-windows.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"bufio"
"bytes"
"context"
"encoding/hex"
Expand All @@ -11,7 +10,6 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"slices"
"strconv"
"strings"
Expand Down Expand Up @@ -101,9 +99,9 @@ func (c *cmdRepackWindows) command() *cobra.Command {
c.defaultDrivers = "virtio-win.iso"
cmd.Flags().StringVar(&c.flagDrivers, "drivers", c.defaultDrivers, "Path to virtio windowns drivers ISO file"+"``")
cmd.Flags().StringVar(&c.flagWindowsVersion, "windows-version", "",
"Windows version to repack, must be one of ["+strings.Join(shared.SupportedWindowsVersions, ", ")+"]``")
"Windows version to repack, must be one of ["+strings.Join(windows.SupportedWindowsVersions, ", ")+"]``")
cmd.Flags().StringVar(&c.flagWindowsArchitecture, "windows-arch", "",
"Windows architecture to repack, must be one of ["+strings.Join(shared.SupportedWindowsArchitectures, ", ")+"]``")
"Windows architecture to repack, must be one of ["+strings.Join(windows.SupportedWindowsArchitectures, ", ")+"]``")

return cmd
}
Expand All @@ -113,18 +111,18 @@ func (c *cmdRepackWindows) preRun(cmd *cobra.Command, args []string) error {
logger := c.global.logger

if c.flagWindowsVersion == "" {
c.flagWindowsVersion = shared.DetectWindowsVersion(filepath.Base(args[0]))
c.flagWindowsVersion = windows.DetectWindowsVersion(filepath.Base(args[0]))
} else {
if !slices.Contains(shared.SupportedWindowsVersions, c.flagWindowsVersion) {
return fmt.Errorf("Version must be one of %v", shared.SupportedWindowsVersions)
if !slices.Contains(windows.SupportedWindowsVersions, c.flagWindowsVersion) {
return fmt.Errorf("Version must be one of %v", windows.SupportedWindowsVersions)
}
}

if c.flagWindowsArchitecture == "" {
c.flagWindowsArchitecture = shared.DetectWindowsArchitecture(filepath.Base(args[0]))
c.flagWindowsArchitecture = windows.DetectWindowsArchitecture(filepath.Base(args[0]))
} else {
if !slices.Contains(shared.SupportedWindowsArchitectures, c.flagWindowsArchitecture) {
return fmt.Errorf("Architecture must be one of %v", shared.SupportedWindowsArchitectures)
if !slices.Contains(windows.SupportedWindowsArchitectures, c.flagWindowsArchitecture) {
return fmt.Errorf("Architecture must be one of %v", windows.SupportedWindowsArchitectures)
}
}

Expand Down Expand Up @@ -271,11 +269,11 @@ func (c *cmdRepackWindows) run(cmd *cobra.Command, args []string, overlayDir str
}

if c.flagWindowsVersion == "" {
c.flagWindowsVersion = shared.DetectWindowsVersion(installWimInfo.Name(1))
c.flagWindowsVersion = windows.DetectWindowsVersion(installWimInfo.Name(1))
}

if c.flagWindowsArchitecture == "" {
c.flagWindowsArchitecture = shared.DetectWindowsArchitecture(installWimInfo.Architecture(1))
c.flagWindowsArchitecture = windows.DetectWindowsArchitecture(installWimInfo.Architecture(1))
}

if c.flagWindowsVersion == "" {
Expand Down Expand Up @@ -342,7 +340,7 @@ func (c *cmdRepackWindows) run(cmd *cobra.Command, args []string, overlayDir str
return nil
}

func (c *cmdRepackWindows) getWimInfo(wimFile string) (info shared.WimInfo, err error) {
func (c *cmdRepackWindows) getWimInfo(wimFile string) (info windows.WimInfo, err error) {
wimName := filepath.Base(wimFile)
var buf bytes.Buffer
err = shared.RunCommand(c.global.ctx, nil, &buf, "wimlib-imagex", "info", wimFile)
Expand All @@ -351,7 +349,7 @@ func (c *cmdRepackWindows) getWimInfo(wimFile string) (info shared.WimInfo, err
return
}

info, err = shared.ParseWimInfo(&buf)
info, err = windows.ParseWimInfo(&buf)
if err != nil {
err = fmt.Errorf("Failed to parse wim info %s: %w", wimFile, err)
return
Expand All @@ -360,7 +358,7 @@ func (c *cmdRepackWindows) getWimInfo(wimFile string) (info shared.WimInfo, err
return
}

func (c *cmdRepackWindows) modifyWim(wimFile string, info shared.WimInfo) (err error) {
func (c *cmdRepackWindows) modifyWim(wimFile string, info windows.WimInfo) (err error) {
wimName := filepath.Base(wimFile)
// Injects the drivers
for idx := 1; idx <= info.ImageCount(); idx++ {
Expand Down Expand Up @@ -505,7 +503,7 @@ func (c *cmdRepackWindows) injectDrivers(infDir, driversDir, filerepositoryDir,
}
}

classGuid, err := parseDriverClassGuid(driverName, filepath.Join(infDir, infFilename))
classGuid, err := windows.ParseDriverClassGuid(driverName, filepath.Join(infDir, infFilename))
if err != nil {
return err
}
Expand Down Expand Up @@ -589,33 +587,6 @@ func (c *cmdRepackWindows) injectDrivers(infDir, driversDir, filerepositoryDir,
return nil
}

func parseDriverClassGuid(driverName, infPath string) (classGuid string, err error) {
// Retrieve the ClassGuid which is needed for the Windows registry entries.
file, err := os.Open(infPath)
if err != nil {
err = fmt.Errorf("Failed to open driver %s inf %s: %w", driverName, infPath, err)
return
}

defer func() {
file.Close()
if classGuid == "" {
err = fmt.Errorf("Failed to parse driver %s classGuid %s", driverName, infPath)
}
}()
re := regexp.MustCompile(`(?i)^ClassGuid[ ]*=[ ]*(.+)$`)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
matches := re.FindStringSubmatch(scanner.Text())
if len(matches) > 1 {
classGuid = strings.TrimSpace(matches[1])
return
}
}

return
}

// toHex is a pongo2 filter which converts the provided value to a hex value understood by the Windows registry.
func toHex(in *pongo2.Value, param *pongo2.Value) (out *pongo2.Value, err *pongo2.Error) {
dst := make([]byte, hex.EncodedLen(len(in.String())))
Expand Down
57 changes: 57 additions & 0 deletions windows/inf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package windows

import (
"bufio"
"fmt"
"io"
"os"
"regexp"
"strings"
)

var (
versionRe = regexp.MustCompile(`(?i)^\[Version\][ ]*$`)
classGuidRe = regexp.MustCompile(`(?i)^ClassGuid[ ]*=[ ]*(.+)$`)
)

func ParseDriverClassGuid(driverName, infPath string) (classGuid string, err error) {
// Retrieve the ClassGuid which is needed for the Windows registry entries.
file, err := os.Open(infPath)
if err != nil {
err = fmt.Errorf("Failed to open driver %s inf %s: %w", driverName, infPath, err)
return
}

defer func() {
file.Close()
if classGuid == "" {
err = fmt.Errorf("Failed to parse driver %s classGuid %s", driverName, infPath)
}
}()

classGuid = MatchClassGuid(file)
return
}

func MatchClassGuid(r io.Reader) (classGuid string) {
scanner := bufio.NewScanner(r)
versionFlag := false
for scanner.Scan() {
line := scanner.Text()
if !versionFlag {
if versionRe.MatchString(line) {
versionFlag = true
}
continue
}
matches := classGuidRe.FindStringSubmatch(line)
if len(matches) > 1 {
classGuid = strings.TrimSpace(matches[1])
if classGuid != "" {
return
}
}
}

return
}
29 changes: 29 additions & 0 deletions windows/inf_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package windows

import (
"os"
"testing"
)

func TestMatchClassGuid(t *testing.T) {
tcs := []struct {
filename string
want string
}{
{"testdata/inf01.txt", "{4D36E97B-E325-11CE-BFC1-08002BE10318}"},
{"testdata/inf02.txt", "{4D36E97B-E325-11CE-BFC1-08002BE10318}"},
{"testdata/inf03.txt", ""},
}
for _, tc := range tcs {
t.Run("", func(t *testing.T) {
file, err := os.Open(tc.filename)
if err != nil {
t.Fatal(err)
}
actual := MatchClassGuid(file)
if actual != tc.want {
t.Fatal(actual, tc.want)
}
})
}
}
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions windows/testdata/inf01.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[Version]
Signature="$Windows NT$"
Class=SCSIAdapter
ClassGUID={4D36E97B-E325-11CE-BFC1-08002BE10318}
Provider=%VENDOR%
DriverVer = 02/25/2024,100.94.104.24800
CatalogFile=viostor.cat
DriverPackageType = PlugAndPlay
DriverPackageDisplayName = %VioStorScsi.DeviceDesc%
PnpLockdown=1
27 changes: 27 additions & 0 deletions windows/testdata/inf02.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
;/*++
;
;Copyright (c) 2008-2023 Red Hat Inc.
;
;
;Module Name:
; viostor.inf
;
;Abstract:
;
;Installation Notes:
; Step by step driver installation wiki:
; https://github.com/virtio-win/kvm-guest-drivers-windows/wiki/Driver-installation
;
;--*/

[Version]
Signature="$Windows NT$"
Class=SCSIAdapter
;ClassGUID={4d36e97b-e325-11ce-bfc1-08002be10318}
ClassGUID={4D36E97B-E325-11CE-BFC1-08002BE10318}
Provider=%VENDOR%
DriverVer = 02/25/2024,100.94.104.24800
CatalogFile=viostor.cat
DriverPackageType = PlugAndPlay
DriverPackageDisplayName = %VioStorScsi.DeviceDesc%
PnpLockdown=1
27 changes: 27 additions & 0 deletions windows/testdata/inf03.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
;/*++
;
;Copyright (c) 2008-2023 Red Hat Inc.
;
;
;Module Name:
; viostor.inf
;
;Abstract:
;
;Installation Notes:
; Step by step driver installation wiki:
; https://github.com/virtio-win/kvm-guest-drivers-windows/wiki/Driver-installation
;
;--*/

;[Version]
Signature="$Windows NT$"
Class=SCSIAdapter
;ClassGUID={4d36e97b-e325-11ce-bfc1-08002be10318}
ClassGUID={4D36E97B-E325-11CE-BFC1-08002BE10318}
Provider=%VENDOR%
DriverVer = 02/25/2024,100.94.104.24800
CatalogFile=viostor.cat
DriverPackageType = PlugAndPlay
DriverPackageDisplayName = %VioStorScsi.DeviceDesc%
PnpLockdown=1
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion shared/wiminfo.go → windows/wiminfo.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package shared
package windows

import (
"bufio"
Expand Down
2 changes: 1 addition & 1 deletion shared/wiminfo_test.go → windows/wiminfo_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package shared
package windows

import (
"os"
Expand Down

0 comments on commit 63db092

Please sign in to comment.