forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grub.go
301 lines (269 loc) · 8.02 KB
/
grub.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright 2017-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.
// Package grub implements a grub config file parser.
//
// See the grub manual https://www.gnu.org/software/grub/manual/grub/ for
// a reference of the configuration format
// In particular the following pages:
// - https://www.gnu.org/software/grub/manual/grub/html_node/Shell_002dlike-scripting.html
// - https://www.gnu.org/software/grub/manual/grub/html_node/Commands.html
//
// Currently, only the linux[16|efi], initrd[16|efi], menuentry and set
// directives are partially supported.
package grub
import (
"errors"
"fmt"
"io"
"log"
"net/url"
"path/filepath"
"strconv"
"strings"
"github.com/u-root/u-root/pkg/boot"
"github.com/u-root/u-root/pkg/boot/multiboot"
"github.com/u-root/u-root/pkg/curl"
"github.com/u-root/u-root/pkg/shlex"
"github.com/u-root/u-root/pkg/uio"
)
var (
// ErrDefaultEntryNotFound is returned when the configuration file
// names a default label that is not part of the configuration.
ErrDefaultEntryNotFound = errors.New("default variable not set in configuration")
// ErrInitrdUsedWithoutLinux is returned when an initrd directive is
// not following a linux directive in the same menu entry
ErrInitrdUsedWithoutLinux = errors.New("missing linux directive before initrd")
// ErrModuleUsedWithoutMultiboot is returned when a module directive is
// not following a multiboot directive in the same menu entry
ErrModuleUsedWithoutMultiboot = errors.New("missing multiboot directive before module")
)
// Config encapsulates a parsed grub configuration file.
type Config struct {
// Entries is a map of label name -> label configuration.
Entries map[string]boot.OSImage
// DefaultEntry is the default label key to use.
//
// If DefaultEntry is non-empty, the label is guaranteed to exist in
// `Entries`.
DefaultEntry string
}
// ParseConfigFile parses a grub configuration as specified in
// https://www.gnu.org/software/grub/manual/grub/
//
// Currently, only the linux[16|efi], initrd[16|efi], menuentry and set
// directives are partially supported.
//
// curl.DefaultSchemes is used to fetch any files that must be parsed or
// provided.
//
// `wd` is the default scheme, host, and path for any files named as a
// relative path - e.g. kernel, include, and initramfs paths are requested
// relative to the wd.
func ParseConfigFile(url string, wd *url.URL) (*Config, error) {
return ParseConfigFileWithSchemes(curl.DefaultSchemes, url, wd)
}
// ParseConfigFileWithSchemes is like ParseConfigFile, but uses the given
// schemes explicitly.
func ParseConfigFileWithSchemes(s curl.Schemes, url string, wd *url.URL) (*Config, error) {
p := newParserWithSchemes(wd, s)
if err := p.appendFile(url); err != nil {
return nil, err
}
return p.config, nil
}
type parser struct {
config *Config
W io.Writer
// parser internals.
scope scope
numEntry int
curEntry string
curLabel string
wd *url.URL
schemes curl.Schemes
}
type scope uint8
const (
scopeGlobal scope = iota
scopeEntry
)
// newParserWithSchemes returns a new grub parser using working directory `wd`
// and schemes `s`.
//
// If a path encountered in a configuration file is relative instead of a full
// URL, `wd` is used as the "working directory" of that relative path; the
// resulting URL is roughly `wd.String()/path`.
//
// `s` is used to get files referred to by URLs.
func newParserWithSchemes(wd *url.URL, s curl.Schemes) *parser {
return &parser{
config: &Config{
Entries: make(map[string]boot.OSImage),
},
scope: scopeGlobal,
wd: wd,
schemes: s,
}
}
func parseURL(surl string, wd *url.URL) (*url.URL, error) {
u, err := url.Parse(surl)
if err != nil {
return nil, fmt.Errorf("could not parse URL %q: %v", surl, err)
}
if len(u.Scheme) == 0 {
u.Scheme = wd.Scheme
if len(u.Host) == 0 {
// If this is not there, it was likely just a path.
u.Host = wd.Host
u.Path = filepath.Join(wd.Path, filepath.Clean(u.Path))
}
}
return u, nil
}
// getFile parses `url` relative to the config's working directory and returns
// an io.Reader for the requested url.
//
// If url is just a relative path and not a full URL, c.wd is used as the
// "working directory" of that relative path; the resulting URL is roughly
// path.Join(wd.String(), url).
func (c *parser) getFile(url string) (io.ReaderAt, error) {
u, err := parseURL(url, c.wd)
if err != nil {
return nil, err
}
return c.schemes.LazyFetch(u)
}
// appendFile parses the config file downloaded from `url` and adds it to `c`.
func (c *parser) appendFile(url string) error {
r, err := c.getFile(url)
if err != nil {
return err
}
config, err := uio.ReadAll(r)
if err != nil {
return err
}
if len(config) > 500 {
// Avoid flooding the console on real systems
// TODO: do we want to pass a verbose flag or a logger?
log.Printf("Got config file %s", r)
} else {
log.Printf("Got config file %s:\n%s\n", r, string(config))
}
return c.append(string(config))
}
// CmdlineQuote quotes the command line as grub-core/lib/cmdline.c does
func cmdlineQuote(args []string) string {
q := make([]string, len(args))
for i, s := range args {
s = strings.Replace(s, `\`, `\\`, -1)
s = strings.Replace(s, `'`, `\'`, -1)
s = strings.Replace(s, `"`, `\"`, -1)
if strings.ContainsRune(s, ' ') {
s = `"` + s + `"`
}
q[i] = s
}
return strings.Join(q, " ")
}
// Append parses `config` and adds the respective configuration to `c`.
func (c *parser) append(config string) error {
// Here's a shitty parser.
for _, line := range strings.Split(config, "\n") {
kv := shlex.Argv(line)
if len(kv) < 1 {
continue
}
directive := strings.ToLower(kv[0])
// Used by tests (allow no parameters here)
if c.W != nil && directive == "echo" {
fmt.Fprintf(c.W, "echo:%#v\n", kv[1:])
}
if len(kv) <= 1 {
continue
}
arg := kv[1]
switch directive {
case "set":
vals := strings.SplitN(arg, "=", 2)
if len(vals) == 2 {
//TODO handle vars? bootVars[vals[0]] = vals[1]
//log.Printf("grubvar: %s=%s", vals[0], vals[1])
if vals[0] == "default" {
c.config.DefaultEntry = vals[1]
}
}
case "configfile":
// TODO test that
if err := c.appendFile(arg); err != nil {
return err
}
case "menuentry":
c.scope = scopeEntry
c.curEntry = strconv.Itoa(c.numEntry)
c.curLabel = arg
c.numEntry++
case "linux", "linux16", "linuxefi":
k, err := c.getFile(arg)
if err != nil {
return err
}
// from grub manual: "Any initrd must be reloaded after using this command" so we can replace the entry
entry := &boot.LinuxImage{
Name: c.curLabel,
Kernel: k,
Cmdline: cmdlineQuote(kv[2:]),
}
c.config.Entries[c.curEntry] = entry
c.config.Entries[c.curLabel] = entry
case "initrd", "initrd16", "initrdefi":
i, err := c.getFile(arg)
if err != nil {
return err
}
entry, ok := c.config.Entries[c.curEntry].(*boot.LinuxImage)
if !ok {
return ErrInitrdUsedWithoutLinux
}
entry.Initrd = i
case "multiboot":
// TODO handle --quirk-* arguments ? (change parsing)
k, err := c.getFile(arg)
if err != nil {
return err
}
// from grub manual: "Any initrd must be reloaded after using this command" so we can replace the entry
entry := &boot.MultibootImage{
Name: c.curLabel,
Kernel: k,
Cmdline: cmdlineQuote(kv[2:]),
}
c.config.Entries[c.curEntry] = entry
c.config.Entries[c.curLabel] = entry
case "module":
// TODO handle --nounzip arguments ? (change parsing)
m, err := c.getFile(arg)
if err != nil {
return err
}
entry, ok := c.config.Entries[c.curEntry].(*boot.MultibootImage)
if !ok {
return ErrModuleUsedWithoutMultiboot
}
// TODO: Lasy tryGzipFilter(m)
mod := multiboot.Module{
Module: m,
Name: arg,
CmdLine: cmdlineQuote(kv[2:]),
}
entry.Modules = append(entry.Modules, mod)
}
}
if len(c.config.DefaultEntry) > 0 {
if _, ok := c.config.Entries[c.config.DefaultEntry]; !ok {
return ErrDefaultEntryNotFound
}
}
return nil
}