forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
efiDppResolver.go
107 lines (85 loc) · 2.51 KB
/
efiDppResolver.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
// Copyright 2020 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// SPDX-License-Identifier: BSD-3-Clause
//
package boot
import (
"log"
"os"
fp "path/filepath"
"github.com/u-root/u-root/pkg/mount"
"github.com/u-root/u-root/pkg/mount/block"
"golang.org/x/sys/unix"
)
type EfiPathSegmentResolver interface {
// Returns description, does not require cleanup
String() string
// Mount fs, etc. You must call Cleanup() eventually.
Resolve(suggestedBasePath string) (string, error)
// For devices, returns BlockDev. Returns nil otherwise.
BlockInfo() *block.BlockDev
// For mounted devices, returns MountPoint. Returns nil otherwise.
MntPoint() *mount.MountPoint
// Unmount fs, free resources, etc
Cleanup() error
}
// HddResolver can identify and mount a partition.
type HddResolver struct {
*block.BlockDev
*mount.MountPoint
}
var _ EfiPathSegmentResolver = (*HddResolver)(nil)
func (r *HddResolver) String() string { return "/dev/" + r.BlockDev.Name }
func (r *HddResolver) Resolve(basePath string) (string, error) {
if r.MountPoint != nil {
return r.MountPoint.Path, nil
}
var err error
if len(basePath) == 0 {
basePath, err = os.MkdirTemp("", "uefiPath")
if err != nil {
return "", err
}
} else {
fi, err := os.Stat(basePath)
if err != nil || !fi.IsDir() {
err = os.RemoveAll(basePath)
if err != nil {
return "", err
}
err = os.MkdirAll(basePath, 0o755)
if err != nil {
return "", err
}
}
}
r.MountPoint, err = r.BlockDev.Mount(basePath, 0)
return r.MountPoint.Path, err
}
func (r *HddResolver) BlockInfo() *block.BlockDev { return r.BlockDev }
func (r *HddResolver) MntPoint() *mount.MountPoint { return r.MountPoint }
func (r *HddResolver) Cleanup() error {
if r.MountPoint != nil {
err := r.MountPoint.Unmount(unix.UMOUNT_NOFOLLOW | unix.MNT_DETACH)
if err == nil {
r.MountPoint = nil
}
return err
}
return nil
}
// PathResolver outputs a file path.
type PathResolver string
var _ EfiPathSegmentResolver = (*PathResolver)(nil)
func (r *PathResolver) String() string { return string(*r) }
func (r *PathResolver) Resolve(basePath string) (string, error) {
if len(basePath) == 0 {
log.Printf("uefi.PathResolver: empty base path")
}
return fp.Join(basePath, string(*r)), nil
}
func (r *PathResolver) BlockInfo() *block.BlockDev { return nil }
func (r *PathResolver) MntPoint() *mount.MountPoint { return nil }
func (r *PathResolver) Cleanup() error { return nil }