forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 2
/
grub.go
595 lines (544 loc) · 17.2 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
// 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
//
// See parser.append function for list of commands that are supported.
package grub
import (
"context"
"errors"
"fmt"
"io"
"log"
"net/url"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/spf13/pflag"
"github.com/u-root/u-root/pkg/boot"
"github.com/u-root/u-root/pkg/boot/bls"
"github.com/u-root/u-root/pkg/boot/multiboot"
"github.com/u-root/u-root/pkg/curl"
"github.com/u-root/u-root/pkg/mount"
"github.com/u-root/u-root/pkg/mount/block"
"github.com/u-root/u-root/pkg/shlex"
"github.com/u-root/u-root/pkg/uio"
"github.com/u-root/u-root/pkg/ulog"
)
var probeGrubFiles = []string{
"boot/grub/grub.cfg",
"grub/grub.cfg",
"grub2/grub.cfg",
"boot/grub2/grub.cfg",
}
var probeGrubEnvFiles = []string{
"EFI/*/grubenv",
"boot/grub/grubenv",
"grub/grubenv",
"grub2/grubenv",
"boot/grub2/grubenv",
}
// Grub syntax for OpenSUSE/Fedora/RHEL has some undocumented quirks. You
// won't find it on the master branch, but instead look at the rhel and fedora
// branches for these commits:
//
// * https://github.com/rhboot/grub2/commit/7e6775e6d4a8de9baf3f4676d4e021cc2f5dd761
// * https://github.com/rhboot/grub2/commit/0c26c6f7525737962d1389ebdfbb918f52d1b3b6
//
// They add a special case to not escape hex sequences:
//
// grub> echo hello \xff \xfg
// hello \xff xfg
//
// Their default installations depend on this functionality.
var hexEscape = regexp.MustCompile(`\\x[0-9a-fA-F]{2}`)
var anyEscape = regexp.MustCompile(`\\.{0,3}`)
// mountFlags are the flags this grub interpreter uses to mount partitions.
var mountFlags = uintptr(mount.ReadOnly)
var errMissingKey = errors.New("key is not found")
// absFileScheme creates a file:/// scheme with an absolute path. Technically,
// file schemes must be absolute paths and Go makes that assumption.
func absFileScheme(path string) (*url.URL, error) {
path, err := filepath.Abs(path)
if err != nil {
return nil, err
}
return &url.URL{
Scheme: "file",
Path: path,
}, nil
}
// ParseLocalConfig looks for a GRUB config in the disk partition mounted at
// diskDir and parses out OSes to boot.
func ParseLocalConfig(ctx context.Context, diskDir string, devices block.BlockDevices, mountPool *mount.Pool) ([]boot.OSImage, error) {
root, err := absFileScheme(diskDir)
if err != nil {
return nil, err
}
// This is a hack. GRUB should stop caring about URLs at least in the
// way we use them today, because GRUB has additional path encoding
// methods. Sorry.
//
// Normally, stuff like this will be in EFI/BOOT/grub.cfg, but some
// distro's have their own directory in this EFI namespace. Just check
// 'em all.
files, err := filepath.Glob(filepath.Join(diskDir, "EFI", "*", "grub.cfg"))
if err != nil {
log.Printf("[grub] Could not glob for %s/EFI/*/grub.cfg: %v", diskDir, err)
}
var relNames []string
for _, file := range files {
base, err := filepath.Rel(diskDir, file)
if err == nil {
relNames = append(relNames, base)
}
}
for _, relname := range append(relNames, probeGrubFiles...) {
c, err := ParseConfigFile(ctx, curl.DefaultSchemes, relname, root, devices, mountPool)
if curl.IsURLError(err) {
continue
}
return c, err
}
return nil, fmt.Errorf("no valid grub config found")
}
func grubScanBLSEntries(mountPool *mount.Pool, variables map[string]string, grubDefaultSavedEntry string) ([]boot.OSImage, error) {
var images []boot.OSImage
// Scan each mounted partition for BLS entries
for _, m := range mountPool.MountPoints {
imgs, _ := bls.ScanBLSEntries(ulog.Null, m.Path, variables, grubDefaultSavedEntry)
images = append(images, imgs...)
}
if len(images) == 0 {
return nil, fmt.Errorf("no valid BLS entry found")
}
return images, nil
}
// Find and return the value of the key from a grubenv file
func findkeywordGrubEnv(file string, fsRoot string, key string) (string, error) {
FileGrubenv, err := filepath.Glob(filepath.Join(fsRoot, file))
if FileGrubenv == nil || err != nil {
return "", err
}
relNamesFile, err := os.Open(FileGrubenv[0])
if err != nil {
return "", fmt.Errorf("[grubenv]:%w", err)
}
grubenv, err := uio.ReadAll(relNamesFile)
if err != nil {
return "", fmt.Errorf("[grubenv]:%w", err)
}
for _, line := range strings.Split(string(grubenv), "\n") {
vals := strings.SplitN(line, "=", 2)
if vals[0] == key {
return vals[1], nil
}
}
return "", fmt.Errorf("%q:%w", key, errMissingKey)
}
// ParseConfigFile parses a grub configuration as specified in
// https://www.gnu.org/software/grub/manual/grub/
//
// See parser.append function for list of commands that are supported.
//
// `root` is the default scheme, host, and path for any files named as a
// relative path - e.g. kernel and initramfs paths are requested relative to
// the root.
func ParseConfigFile(ctx context.Context, s curl.Schemes, configFile string, root *url.URL, devices block.BlockDevices, mountPool *mount.Pool) ([]boot.OSImage, error) {
p := newParser(root, devices, mountPool, s)
if err := p.appendFile(ctx, configFile); err != nil {
return nil, err
}
// Don't add entries twice.
//
// Multiple labels can refer to the same image, so we have to dedup by pointer.
seenLinux := make(map[*boot.LinuxImage]struct{})
seenMB := make(map[*boot.MultibootImage]struct{})
var grubDefaultSavedEntry string
// If the value of keyword "default_saved_entry" exists, find the value of "save_entry" from grubenv files from all possible paths.
if _, ok := p.variables["default_saved_entry"]; ok {
for _, m := range mountPool.MountPoints {
for _, file := range probeGrubEnvFiles {
// Parse grubenv and return the value of 'saved_entry'.
val, _ := findkeywordGrubEnv(file, m.Path, "saved_entry")
if val != "" {
grubDefaultSavedEntry = val
}
}
}
}
if defaultEntry, ok := p.variables["default"]; ok {
p.labelOrder = append([]string{defaultEntry}, p.labelOrder...)
}
var images []boot.OSImage
if p.blscfgFound {
if imgs, err := grubScanBLSEntries(p.mountPool, p.variables, grubDefaultSavedEntry); err == nil {
images = append(images, imgs...)
}
}
for _, label := range p.labelOrder {
if img, ok := p.linuxEntries[label]; ok {
if _, ok := seenLinux[img]; !ok {
images = append(images, img)
seenLinux[img] = struct{}{}
}
}
if img, ok := p.mbEntries[label]; ok {
if _, ok := seenMB[img]; !ok {
images = append(images, img)
seenMB[img] = struct{}{}
}
}
}
return images, nil
}
type parser struct {
linuxEntries map[string]*boot.LinuxImage
mbEntries map[string]*boot.MultibootImage
labelOrder []string
W io.Writer
// parser internals.
numEntry int
// Special variables:
// * default: Default boot option.
// * root: Root "partition" as a URL.
variables map[string]string
// curEntry is the current entry number as a string.
curEntry string
// curLabel is the last parsed label from a "menuentry".
curLabel string
devices block.BlockDevices
mountPool *mount.Pool
schemes curl.Schemes
// blscfgFound is set to true when blscfg is found
blscfgFound bool
}
// newParser returns a new grub parser using `root` and schemes `s`.
//
// We are going off script here by using URLs instead of grub's device syntax.
//
// Typically, the default value for root should be the mount point containing
// the grub config, for example: "file:///tmp/sda1/". Kernel and initramfs
// files are opened relative to this path.
//
// Some grub configs may set a different local root. For this, all partitions
// must be mounted beforehand and made available to grub through `mounts`.
//
// For example, if the grub config contains `search --by-label LINUX`, this
// resolves to the device node "/dev/disk/by-partlabel/LINUX". This grub parser
// looks through mounts for a matching device number.
func newParser(root *url.URL, devices block.BlockDevices, mountPool *mount.Pool, s curl.Schemes) *parser {
return &parser{
linuxEntries: make(map[string]*boot.LinuxImage),
mbEntries: make(map[string]*boot.MultibootImage),
variables: map[string]string{
"root": root.String(),
},
devices: devices,
mountPool: mountPool,
schemes: s,
blscfgFound: false,
}
}
func parseURL(surl string, root string) (*url.URL, error) {
u, err := url.Parse(surl)
if err != nil {
return nil, fmt.Errorf("could not parse URL %q: %v", surl, err)
}
ru, err := url.Parse(root)
if err != nil {
return nil, fmt.Errorf("could not parse URL %q: %v", root, err)
}
if len(u.Scheme) == 0 {
u.Scheme = ru.Scheme
if len(u.Host) == 0 {
// If this is not there, it was likely just a path.
u.Host = ru.Host
u.Path = filepath.Join(ru.Path, filepath.Clean(u.Path))
}
}
return u, nil
}
// getFile parses `url` relative to the current root and returns an io.Reader
// for the requested url.
//
// If url is just a relative path and not a full URL, c.root is used for the
// relative path; the resulting URL is roughly path.Join(root, url).
func (c *parser) getFile(url string) (io.ReaderAt, error) {
u, err := parseURL(url, c.variables["root"])
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(ctx context.Context, url string) error {
u, err := parseURL(url, c.variables["root"])
if err != nil {
return err
}
r, err := c.schemes.Fetch(ctx, u)
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("[grub] Got config file %s", r)
} else {
log.Printf("[grub] Got config file %s:\n%s\n", r, string(config))
}
return c.append(ctx, 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 {
// Replace \ with \\ unless it matches \xXX
s = anyEscape.ReplaceAllStringFunc(s, func(match string) string {
if hexEscape.MatchString(match) {
return match
}
return strings.Replace(match, `\`, `\\`, -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`.
//
// NOTE: This parser has outlived its usefulness already, given that it doesn't
// even understand the {} scoping in GRUB. But let's get the tests to pass, and
// then we can do a rewrite.
func (c *parser) append(ctx context.Context, config string) error {
// Here's a shitty parser.
for _, line := range strings.Split(config, "\n") {
// Add extra backslash for OpenSUSE/Fedora/RHEL use case. shlex
// will convert it back to a single backslash.
line = hexEscape.ReplaceAllString(line, `\\$0`)
kv := shlex.Argv(line)
if len(kv) < 1 {
continue
}
directive := strings.ToLower(kv[0])
// blscfg len(kv) is 1 so need to be checked here
if directive == "blscfg" {
c.blscfgFound = true
}
// 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 "search.file", "search.fs_label", "search.fs_uuid":
// Alias to regular search directive.
kv = append(
[]string{"search", map[string]string{
"search.file": "--file",
"search.fs_label": "--fs-label",
"search.fs_uuid": "--fs-uuid",
}[directive]},
kv[1:]...,
)
fallthrough
case "search":
// Parses a line with this format:
// search [--file|--label|--fs-uuid] [--set [var]] [--no-floppy] name
fs := pflag.NewFlagSet("grub.search", pflag.ContinueOnError)
searchUUID := fs.BoolP("fs-uuid", "u", false, "")
searchLabel := fs.BoolP("fs-label", "l", false, "")
searchFile := fs.BoolP("file", "f", false, "")
setVar := fs.String("set", "root", "")
// Ignored flags
fs.String("no-floppy", "", "ignored")
fs.String("hint", "", "ignored")
fs.SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName {
// Everything that begins with "hint" is ignored.
if strings.HasPrefix(name, "hint") {
name = "hint"
}
return pflag.NormalizedName(name)
})
if err := fs.Parse(kv[1:]); err != nil || fs.NArg() != 1 {
log.Printf("Warning: Grub parser could not parse %q", kv)
continue
}
searchName := fs.Arg(0)
if *searchUUID && *searchLabel || *searchUUID && *searchFile || *searchLabel && *searchFile {
log.Printf("Warning: Grub parser found more than one search option in %q, skipping line", line)
continue
}
if !*searchUUID && !*searchLabel && !*searchFile {
// defaults to searchUUID
*searchUUID = true
}
switch {
case *searchUUID:
d := c.devices.FilterFSUUID(searchName)
if len(d) != 1 {
log.Printf("Error: Expected 1 device with UUID %q, found %d", searchName, len(d))
continue
}
mp, err := c.mountPool.Mount(d[0], mountFlags)
if err != nil {
log.Printf("Error: Could not mount %v: %v", d[0], err)
continue
}
setVal, err := absFileScheme(mp.Path)
if err != nil {
continue
}
c.variables[*setVar] = setVal.String()
case *searchLabel:
d := c.devices.FilterPartLabel(searchName)
if len(d) != 1 {
log.Printf("Error: Expected 1 device with label %q, found %d", searchName, len(d))
continue
}
mp, err := c.mountPool.Mount(d[0], mountFlags)
if err != nil {
log.Printf("Error: Could not mount %v: %v", d[0], err)
continue
}
setVal, err := absFileScheme(mp.Path)
if err != nil {
continue
}
c.variables[*setVar] = setVal.String()
case *searchFile:
// Make sure searchName stays in mountpoint. Remove "../" components.
cleanPath, err := filepath.Rel("/", filepath.Clean(filepath.Join("/", searchName)))
if err != nil {
log.Printf("Error: Could not clean path %q: %v", searchName, err)
continue
}
// Search through all the devices for the file.
for _, d := range c.devices {
mp, err := c.mountPool.Mount(d, mountFlags)
if err != nil {
log.Printf("Warning: Could not mount %v: %v", mp, err)
continue
}
file := filepath.Join(mp.Path, cleanPath)
if _, err := os.Stat(file); err == nil {
setVal, err := absFileScheme(mp.Path)
if err != nil {
continue
}
c.variables[*setVar] = setVal.String()
break
}
}
}
case "set":
vals := strings.SplitN(arg, "=", 2)
if len(vals) == 2 {
// TODO: We cannot parse grub device syntax.
if vals[0] == "root" {
continue
}
// here we only add the support for the case: set default="${saved_entry}".
if vals[0] == "default" {
if vals[1] == "${saved_entry}" {
c.variables["default_saved_entry"] = vals[1]
} else {
c.variables[vals[0]] = vals[1]
}
} else {
c.variables[vals[0]] = vals[1]
}
}
case "configfile":
// TODO test that
if err := c.appendFile(ctx, arg); err != nil {
return err
}
case "menuentry":
c.curEntry = strconv.Itoa(c.numEntry)
c.curLabel = arg
c.numEntry++
c.labelOrder = append(c.labelOrder, c.curEntry, c.curLabel)
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.linuxEntries[c.curEntry] = entry
c.linuxEntries[c.curLabel] = entry
case "initrd", "initrd16", "initrdefi":
if e, ok := c.linuxEntries[c.curEntry]; ok {
i, err := c.getFile(arg)
if err != nil {
return err
}
e.Initrd = i
}
case "multiboot", "multiboot2":
// 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.mbEntries[c.curEntry] = entry
c.mbEntries[c.curLabel] = entry
case "module", "module2":
// TODO handle --nounzip arguments ? (change parsing)
if e, ok := c.mbEntries[c.curEntry]; ok {
// The only allowed arg
cmdline := kv[1:]
if arg == "--nounzip" {
if len(kv) < 3 {
return fmt.Errorf("no file argument given: %v", kv)
}
arg = kv[2]
cmdline = kv[2:]
}
m, err := c.getFile(arg)
if err != nil {
return err
}
// TODO: Lasy tryGzipFilter(m)
mod := multiboot.Module{
Module: m,
Cmdline: cmdlineQuote(cmdline),
}
e.Modules = append(e.Modules, mod)
}
}
}
return nil
}