Skip to content

Commit e2d0822

Browse files
committed
[no-relnote] Move firmware functions to separate file
Signed-off-by: Evan Lezar <[email protected]>
1 parent f44e31f commit e2d0822

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

pkg/nvcdi/firmware.go

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
# Copyright 2024 NVIDIA CORPORATION
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
**/
16+
17+
package nvcdi
18+
19+
import (
20+
"fmt"
21+
"os"
22+
"path/filepath"
23+
"strings"
24+
25+
"golang.org/x/sys/unix"
26+
27+
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
28+
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
29+
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup"
30+
)
31+
32+
// newDriverFirmwareDiscoverer creates a discoverer for GSP firmware associated with the specified driver version.
33+
func (l *nvcdilib) newDriverFirmwareDiscoverer(logger logger.Interface, driverRoot string, version string) (discover.Discover, error) {
34+
if !l.optInFeatures["allow-gsp-firmware"] {
35+
return discover.None{}, nil
36+
}
37+
38+
gspFirmwareSearchPaths, err := getFirmwareSearchPaths(logger)
39+
if err != nil {
40+
return nil, fmt.Errorf("failed to get firmware search paths: %v", err)
41+
}
42+
gspFirmwarePaths := filepath.Join("nvidia", version, "gsp*.bin")
43+
return discover.NewMounts(
44+
logger,
45+
lookup.NewFileLocator(
46+
lookup.WithLogger(logger),
47+
lookup.WithRoot(driverRoot),
48+
lookup.WithSearchPaths(gspFirmwareSearchPaths...),
49+
),
50+
driverRoot,
51+
[]string{gspFirmwarePaths},
52+
), nil
53+
}
54+
55+
func getUTSRelease() (string, error) {
56+
utsname := &unix.Utsname{}
57+
if err := unix.Uname(utsname); err != nil {
58+
return "", err
59+
}
60+
return unix.ByteSliceToString(utsname.Release[:]), nil
61+
}
62+
63+
func getFirmwareSearchPaths(logger logger.Interface) ([]string, error) {
64+
65+
var firmwarePaths []string
66+
if p := getCustomFirmwareClassPath(logger); p != "" {
67+
logger.Debugf("using custom firmware class path: %s", p)
68+
firmwarePaths = append(firmwarePaths, p)
69+
}
70+
71+
utsRelease, err := getUTSRelease()
72+
if err != nil {
73+
return nil, fmt.Errorf("failed to get UTS_RELEASE: %v", err)
74+
}
75+
76+
standardPaths := []string{
77+
filepath.Join("/lib/firmware/updates/", utsRelease),
78+
"/lib/firmware/updates/",
79+
filepath.Join("/lib/firmware/", utsRelease),
80+
"/lib/firmware/",
81+
}
82+
83+
return append(firmwarePaths, standardPaths...), nil
84+
}
85+
86+
// getCustomFirmwareClassPath returns the custom firmware class path if it exists.
87+
func getCustomFirmwareClassPath(logger logger.Interface) string {
88+
customFirmwareClassPath, err := os.ReadFile("/sys/module/firmware_class/parameters/path")
89+
if err != nil {
90+
logger.Warningf("failed to get custom firmware class path: %v", err)
91+
return ""
92+
}
93+
94+
return strings.TrimSpace(string(customFirmwareClassPath))
95+
}

0 commit comments

Comments
 (0)