Skip to content

Commit 9f5a290

Browse files
authored
feat: Seek firmware from path specified in kernel parameter path filesystem (#2)
Signed-off-by: Yejin Seo <yejseo01@arm.com>
1 parent c5a596c commit 9f5a290

4 files changed

Lines changed: 57 additions & 14 deletions

File tree

cmd/remoteproc-simulator/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Example usage:
8888

8989
rootCmd.Flags().UintVar(&index, "index", 0, "is the N in /sys/class/remoteproc/remoteprocN/.../ (default 0)")
9090
rootCmd.Flags().StringVar(&name, "name", "dsp0", "remote processor name written to /sys/class/remoteproc/.../name")
91-
rootCmd.Flags().StringVar(&rootDir, "root-dir", "", "location where /sys and /lib will be created")
91+
rootCmd.Flags().StringVar(&rootDir, "root-dir", "", "location where /sys will be created")
9292
rootCmd.Flags().BoolVar(&showVersion, "version", false, "show version information")
9393

9494
if err := rootCmd.Execute(); err != nil {

e2e/boostrapping_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package e2e
22

33
import (
4+
"os"
45
"path/filepath"
6+
"strings"
57
"testing"
68

79
"github.com/stretchr/testify/assert"
@@ -50,7 +52,8 @@ func TestBootstrapping(t *testing.T) {
5052

5153
runSimulator(t, "--root-dir", root, "--index", "99")
5254

53-
firmwareDir := filepath.Join(root, "lib", "firmware")
54-
assert.DirExists(t, firmwareDir)
55+
firmwareDir, err := os.ReadFile(filepath.Join(root, "sys", "module", "firmware_class", "parameters", "path"))
56+
assert.NoError(t, err)
57+
assert.DirExists(t, strings.TrimSpace(string(firmwareDir)))
5558
})
5659
}

e2e/running_firmware_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package e2e
33
import (
44
"os"
55
"path/filepath"
6+
"strings"
67
"testing"
78

89
"github.com/stretchr/testify/require"
@@ -25,7 +26,7 @@ func TestRunningFirmware(t *testing.T) {
2526
requireState(t, instanceDir, "offline")
2627
})
2728

28-
t.Run("firmware file must exist in /lib/firmware", func(t *testing.T) {
29+
t.Run("firmware file must exist in folder indicated inside <fake-root>/sys/module/firmware_class/parameters/path", func(t *testing.T) {
2930
root := t.TempDir()
3031
runSimulator(t, "--root-dir", root, "--index", "0")
3132
instanceDir := filepath.Join(root, "sys", "class", "remoteproc", "remoteproc0")
@@ -38,7 +39,12 @@ func TestRunningFirmware(t *testing.T) {
3839
}
3940

4041
func createFirmwareFile(t *testing.T, root, firmwareName string) {
41-
firmwarePath := filepath.Join(root, "lib", "firmware", firmwareName)
42+
firmwareDirPath, err := os.ReadFile(filepath.Join(root, "sys", "module", "firmware_class", "parameters", "path"))
43+
require.NoError(t, err)
44+
firmwareDirPathStr := strings.TrimSpace(string(firmwareDirPath))
45+
err = os.MkdirAll(firmwareDirPathStr, 0755)
46+
require.NoError(t, err)
47+
firmwarePath := filepath.Join(firmwareDirPathStr, firmwareName)
4248
require.NoError(t, writeFile(firmwarePath, ""))
4349
}
4450

pkg/simulator/filesystem.go

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@ import (
77
)
88

99
type FileSystemManager struct {
10-
instanceDir string
11-
firmwareDir string
12-
createdDirs []string
10+
instanceDir string
11+
firmwareDirPathFile string
12+
firmwareDir string
13+
createdDirs []string
1314
}
1415

1516
func NewFileSystemManager(rootDir string, index uint) *FileSystemManager {
1617
instanceName := fmt.Sprintf("remoteproc%d", index)
1718
return &FileSystemManager{
18-
instanceDir: filepath.Join(rootDir, "sys", "class", "remoteproc", instanceName),
19-
firmwareDir: filepath.Join(rootDir, "lib", "firmware"),
20-
createdDirs: []string{},
19+
instanceDir: filepath.Join(rootDir, "sys", "class", "remoteproc", instanceName),
20+
firmwareDirPathFile: filepath.Join(rootDir, "sys", "module", "firmware_class", "parameters", "path"),
21+
firmwareDir: filepath.Join(rootDir, "firmware"),
22+
createdDirs: []string{},
2123
}
2224
}
2325

@@ -30,13 +32,27 @@ func (fs *FileSystemManager) BootstrapDirectories() error {
3032
fs.createdDirs = append(fs.createdDirs, createdInstancePath)
3133
}
3234

33-
createdFirmwarePath, err := mkdirAll(fs.firmwareDir, 0755)
35+
createdFirmwareDirSpecPath, err := mkfile(fs.firmwareDirPathFile, 0755)
36+
if err != nil {
37+
fs.Cleanup()
38+
return fmt.Errorf("failed to create firmware path directory: %w", err)
39+
}
40+
if createdFirmwareDirSpecPath != "" {
41+
fs.createdDirs = append(fs.createdDirs, createdFirmwareDirSpecPath)
42+
}
43+
44+
if err := os.WriteFile(fs.firmwareDirPathFile, []byte(fs.firmwareDir), 0644); err != nil {
45+
fs.Cleanup()
46+
return fmt.Errorf("failed to write firmware path file: %w", err)
47+
}
48+
49+
createdFirmwareDir, err := mkdirAll(fs.firmwareDir, 0755)
3450
if err != nil {
3551
fs.Cleanup()
3652
return fmt.Errorf("failed to create firmware directory: %w", err)
3753
}
38-
if createdFirmwarePath != "" {
39-
fs.createdDirs = append(fs.createdDirs, createdFirmwarePath)
54+
if createdFirmwareDir != "" {
55+
fs.createdDirs = append(fs.createdDirs, createdFirmwareDir)
4056
}
4157

4258
return nil
@@ -75,6 +91,24 @@ func (fs *FileSystemManager) Cleanup() error {
7591
return nil
7692
}
7793

94+
func mkfile(path string, perm os.FileMode) (string, error) {
95+
dir, err := mkdirAll(filepath.Dir(path), perm)
96+
if err != nil {
97+
return "", err
98+
}
99+
file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, perm)
100+
if err != nil {
101+
return "", err
102+
}
103+
defer file.Close()
104+
if dir == "" {
105+
return path, nil
106+
} else {
107+
return dir, nil
108+
}
109+
110+
}
111+
78112
func mkdirAll(path string, perm os.FileMode) (string, error) {
79113
current := path
80114
var topmostMissing string

0 commit comments

Comments
 (0)