forked from snapcore/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uboot.go
111 lines (89 loc) · 2.38 KB
/
uboot.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2014-2015 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package bootloader
import (
"path/filepath"
"github.com/snapcore/snapd/bootloader/ubootenv"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/snap"
)
type uboot struct {
rootdir string
}
// newUboot create a new Uboot bootloader object
func newUboot(rootdir string) Bootloader {
u := &uboot{rootdir: rootdir}
if !osutil.FileExists(u.envFile()) {
return nil
}
return u
}
func (u *uboot) Name() string {
return "uboot"
}
func (u *uboot) setRootDir(rootdir string) {
u.rootdir = rootdir
}
func (u *uboot) dir() string {
if u.rootdir == "" {
panic("internal error: unset rootdir")
}
return filepath.Join(u.rootdir, "/boot/uboot")
}
func (u *uboot) ConfigFile() string {
return u.envFile()
}
func (u *uboot) envFile() string {
return filepath.Join(u.dir(), "uboot.env")
}
func (u *uboot) SetBootVars(values map[string]string) error {
env, err := ubootenv.OpenWithFlags(u.envFile(), ubootenv.OpenBestEffort)
if err != nil {
return err
}
dirty := false
for k, v := range values {
// already set to the right value, nothing to do
if env.Get(k) == v {
continue
}
env.Set(k, v)
dirty = true
}
if dirty {
return env.Save()
}
return nil
}
func (u *uboot) GetBootVars(names ...string) (map[string]string, error) {
out := map[string]string{}
env, err := ubootenv.OpenWithFlags(u.envFile(), ubootenv.OpenBestEffort)
if err != nil {
return nil, err
}
for _, name := range names {
out[name] = env.Get(name)
}
return out, nil
}
func (u *uboot) ExtractKernelAssets(s snap.PlaceInfo, snapf snap.Container) error {
return extractKernelAssetsToBootDir(u.dir(), s, snapf)
}
func (u *uboot) RemoveKernelAssets(s snap.PlaceInfo) error {
return removeKernelAssetsFromBootDir(u.dir(), s)
}