This repository has been archived by the owner on Feb 20, 2024. It is now read-only.
/
fwupdater.go
146 lines (121 loc) · 3.7 KB
/
fwupdater.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2021 Intel Corporation
package daemon
import (
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"syscall"
"github.com/go-logr/logr"
ethernetv1 "github.com/smart-edge-open/intel-ethernet-operator/apis/ethernet/v1"
"github.com/smart-edge-open/intel-ethernet-operator/pkg/utils"
)
const (
nvmupdate64e = "./nvmupdate64e"
nvmupdatePackageFilename = "nvmupdate.tar.gz"
updateOutFile = "update.xml"
)
var (
findFw = findFwExec
nvmupdateExec = utils.RunExecWithLog
)
type fwUpdater struct {
log logr.Logger
}
func (f *fwUpdater) prepareFirmware(config ethernetv1.DeviceNodeConfig) (string, error) {
log := f.log.WithName("prepareFirmware")
if config.DeviceConfig.FWURL == "" {
log.V(4).Info("Empty FWURL")
return "", nil
}
targetPath := path.Join(artifactsFolder, config.PCIAddress)
err := utils.CreateFolder(targetPath, log)
if err != nil {
return "", err
}
fullPath := path.Join(targetPath, nvmupdatePackageFilename)
log.V(4).Info("Downloading", "url", config.DeviceConfig.FWURL, "dstPath", fullPath)
err = downloadFile(fullPath, config.DeviceConfig.FWURL, config.DeviceConfig.FWChecksum)
if err != nil {
return "", err
}
log.V(4).Info("FW file downloaded - extracting")
err = untarFile(fullPath, targetPath, log)
if err != nil {
return "", err
}
return findFw(targetPath)
}
func (f *fwUpdater) handleFWUpdate(pciAddr, fwPath string) (bool, error) {
log := f.log.WithName("handleFWUpdate")
rebootRequired := false
if fwPath == "" {
return false, nil
}
err := f.updateFirmware(pciAddr, fwPath)
if err != nil {
log.Error(err, "Failed to update firmware", "device", pciAddr)
return false, err
}
reboot, err := isRebootRequired(updateResultPath(fwPath))
if err != nil {
log.Error(err, "Failed to extract reboot required flag from file")
rebootRequired = true // failsafe
} else if reboot {
log.V(4).Info("Node reboot required to complete firmware update", "device", pciAddr)
rebootRequired = true
}
return rebootRequired, nil
}
func (f *fwUpdater) updateFirmware(pciAddr, fwPath string) error {
log := f.log.WithName("updateFirmware")
rootAttr := &syscall.SysProcAttr{
Credential: &syscall.Credential{Uid: 0, Gid: 0},
}
// Call nvmupdate64 -i first to refresh devices
log.V(2).Info("Refreshing nvmupdate inventory")
cmd := exec.Command(nvmupdate64e, "-i")
cmd.SysProcAttr = rootAttr
cmd.Dir = fwPath
err := nvmupdateExec(cmd, log)
if err != nil {
return err
}
mac, err := getDeviceMAC(pciAddr, log)
if err != nil {
log.Error(err, "Failed to get MAC for", "device", pciAddr)
return err
}
log.V(2).Info("Updating", "MAC", mac)
cmd = exec.Command(nvmupdate64e, "-u", "-m", mac, "-c", nvmupdate64eCfgPath(fwPath), "-o", updateResultPath(fwPath), "-l")
cmd.SysProcAttr = rootAttr
cmd.Dir = fwPath
err = nvmupdateExec(cmd, log)
if err != nil {
return err
}
return nil
}
func findFwExec(targetPath string) (string, error) {
var fwPaths []string
walkFunction := func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(info.Name(), "nvmupdate64e") && isExecutable(info) {
fwPaths = append(fwPaths, strings.TrimSuffix(path, "nvmupdate64e"))
}
return nil
}
err := filepath.Walk(targetPath, walkFunction)
if err != nil {
return "", err
}
if len(fwPaths) != 1 {
return "", fmt.Errorf("expected to find exactly 1 file starting with 'nvmupdate64e', but found %v - %v", len(fwPaths), fwPaths)
}
return fwPaths[0], err
}
func nvmupdate64eCfgPath(p string) string { return path.Join(p, "nvmupdate.cfg") }
func updateResultPath(p string) string { return path.Join(p, updateOutFile) }
func isExecutable(info os.FileInfo) bool { return info.Mode()&0100 != 0 }