forked from TrenchBoot/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syslinux.go
470 lines (426 loc) · 12.3 KB
/
syslinux.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
// 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 syslinux implements a syslinux config file parser.
//
// See http://www.syslinux.org/wiki/index.php?title=Config for general syslinux
// config features.
//
// Currently, only the APPEND, INCLUDE, KERNEL, LABEL, DEFAULT, and INITRD
// directives are partially supported.
package syslinux
import (
"context"
"fmt"
"io"
"log"
"net/url"
"path"
"path/filepath"
"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/uio"
)
func probeIsolinuxFiles() []string {
files := make([]string, 0, 10)
// search order from the syslinux wiki
// http://wiki.syslinux.org/wiki/index.php?title=Config
// TODO: do we want to handle extlinux too ?
dirs := []string{
"boot/isolinux",
"isolinux",
"boot/syslinux",
"syslinux",
"",
}
confs := []string{
"isolinux.cfg",
"syslinux.cfg",
}
for _, dir := range dirs {
for _, conf := range confs {
if dir == "" {
files = append(files, conf)
} else {
files = append(files, filepath.Join(dir, conf))
}
}
}
return files
}
// ParseLocalConfig treats diskDir like a mount point on the local file system
// and finds an isolinux config under there.
func ParseLocalConfig(ctx context.Context, diskDir string) ([]boot.OSImage, error) {
rootdir := &url.URL{
Scheme: "file",
Path: diskDir,
}
for _, relname := range probeIsolinuxFiles() {
dir, name := filepath.Split(relname)
// "When booting, the initial working directory for SYSLINUX /
// ISOLINUX will be the directory containing the initial
// configuration file."
//
// https://wiki.syslinux.org/wiki/index.php?title=Config#Working_directory
imgs, err := ParseConfigFile(ctx, curl.DefaultSchemes, name, rootdir, dir)
if curl.IsURLError(err) {
continue
}
return imgs, err
}
return nil, fmt.Errorf("no valid syslinux config found on %s", diskDir)
}
// ParseConfigFile parses a Syslinux configuration as specified in
// http://www.syslinux.org/wiki/index.php?title=Config
//
// Currently, only the APPEND, INCLUDE, KERNEL, LABEL, DEFAULT, and INITRD
// directives are partially supported.
//
// `s` is used to fetch any files that must be parsed or provided.
//
// rootdir is the partition mount point that syslinux is operating under.
// Parsed absolute paths will be interpreted relative to the rootdir.
//
// wd is a directory within rootdir that is the current working directory.
// Parsed relative paths will be interpreted relative to rootdir + "/" + wd.
//
// For PXE clients, rootdir will be the the URL without the path, and wd the
// path component of the URL (e.g. rootdir = http://foobar.com, wd =
// barfoo/pxelinux.cfg/).
func ParseConfigFile(ctx context.Context, s curl.Schemes, configFile string, rootdir *url.URL, wd string) ([]boot.OSImage, error) {
p := newParser(rootdir, wd, s)
if err := p.appendFile(ctx, configFile); err != nil {
return nil, err
}
// Assign the right label to display to users.
for label, displayLabel := range p.menuLabel {
if e, ok := p.linuxEntries[label]; ok {
e.Name = displayLabel
}
if e, ok := p.mbEntries[label]; ok {
e.Name = displayLabel
}
}
// Intended order:
//
// 1. nerfDefaultEntry
// 2. defaultEntry
// 3. labels in order they appeared in config
if len(p.labelOrder) == 0 {
return nil, nil
}
if len(p.defaultEntry) > 0 {
p.labelOrder = append([]string{p.defaultEntry}, p.labelOrder...)
}
if len(p.nerfDefaultEntry) > 0 {
p.labelOrder = append([]string{p.nerfDefaultEntry}, p.labelOrder...)
}
p.labelOrder = dedupStrings(p.labelOrder)
var images []boot.OSImage
for _, label := range p.labelOrder {
if img, ok := p.linuxEntries[label]; ok && img.Kernel != nil {
images = append(images, img)
}
if img, ok := p.mbEntries[label]; ok && img.Kernel != nil {
images = append(images, img)
}
}
return images, nil
}
func dedupStrings(list []string) []string {
var newList []string
seen := make(map[string]struct{})
for _, s := range list {
if _, ok := seen[s]; !ok {
seen[s] = struct{}{}
newList = append(newList, s)
}
}
return newList
}
type parser struct {
// linuxEntries is a map of label name -> label configuration.
linuxEntries map[string]*boot.LinuxImage
mbEntries map[string]*boot.MultibootImage
// labelOrder is the order of label entries in linuxEntries.
labelOrder []string
// menuLabel are human-readable labels defined by the "menu label" directive.
menuLabel map[string]string
defaultEntry string
nerfDefaultEntry string
// parser internals.
globalAppend string
scope scope
curEntry string
wd string
rootdir *url.URL
schemes curl.Schemes
}
type scope uint8
const (
scopeGlobal scope = iota
scopeEntry
)
// newParser returns a new PXE 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 newParser(rootdir *url.URL, wd string, s curl.Schemes) *parser {
return &parser{
linuxEntries: make(map[string]*boot.LinuxImage),
mbEntries: make(map[string]*boot.MultibootImage),
scope: scopeGlobal,
wd: wd,
rootdir: rootdir,
schemes: s,
menuLabel: make(map[string]string),
}
}
func parseURL(name string, rootdir *url.URL, wd string) (*url.URL, error) {
u, err := url.Parse(name)
if err != nil {
return nil, fmt.Errorf("could not parse URL %q: %v", name, err)
}
// If it parsed, but it didn't have a Scheme or Host, use the working
// directory's values.
if len(u.Scheme) == 0 && rootdir != nil {
u.Scheme = rootdir.Scheme
if len(u.Host) == 0 {
// If this is not there, it was likely just a path.
u.Host = rootdir.Host
// Absolute file names don't get the parent
// directories, just the host and scheme.
//
// "All (paths to) file names inside the configuration
// file are relative to the Working Directory, unless
// preceded with a slash."
//
// https://wiki.syslinux.org/wiki/index.php?title=Config#Working_directory
if path.IsAbs(name) {
u.Path = path.Join(rootdir.Path, path.Clean(u.Path))
} else {
u.Path = path.Join(rootdir.Path, wd, path.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.rootdir, 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(ctx context.Context, url string) error {
u, err := parseURL(url, c.rootdir, c.wd)
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
}
log.Printf("Got config file %s:\n%s\n", r, string(config))
return c.append(ctx, string(config))
}
// Append parses `config` and adds the respective configuration to `c`.
func (c *parser) append(ctx context.Context, config string) error {
// Here's a shitty parser.
for _, line := range strings.Split(config, "\n") {
// This is stupid. There should be a FieldsN(...).
kv := strings.Fields(line)
if len(kv) <= 1 {
continue
}
directive := strings.ToLower(kv[0])
var arg string
if len(kv) == 2 {
arg = kv[1]
} else {
arg = strings.Join(kv[1:], " ")
}
switch directive {
case "default":
c.defaultEntry = arg
case "nerfdefault":
c.nerfDefaultEntry = arg
case "include":
if err := c.appendFile(ctx, arg); curl.IsURLError(err) {
log.Printf("failed to parse %s: %v", arg, err)
// Means we didn't find the file. Just ignore
// it.
// TODO(hugelgupf): plumb a logger through here.
continue
} else if err != nil {
return err
}
case "menu":
opt := strings.Fields(arg)
if len(opt) < 1 {
continue
}
switch strings.ToLower(opt[0]) {
case "label":
// Note that "menu label" only changes the
// displayed label, not the identifier for this
// entry.
//
// We track these separately because "menu
// label" directives may happen before we know
// whether this is a Linux or Multiboot entry.
c.menuLabel[c.curEntry] = strings.Join(opt[1:], " ")
case "default":
// Are we in label scope?
//
// "Only valid after a LABEL statement" -syslinux wiki.
if c.scope == scopeEntry {
c.defaultEntry = c.curEntry
}
}
case "label":
// We forever enter label scope.
c.scope = scopeEntry
c.curEntry = arg
c.linuxEntries[c.curEntry] = &boot.LinuxImage{
Cmdline: c.globalAppend,
Name: c.curEntry,
}
c.labelOrder = append(c.labelOrder, c.curEntry)
case "kernel":
// I hate special cases like these, but we aren't gonna
// implement syslinux modules.
if arg == "mboot.c32" {
// Prepare for a multiboot kernel.
delete(c.linuxEntries, c.curEntry)
c.mbEntries[c.curEntry] = &boot.MultibootImage{
Name: c.curEntry,
}
}
fallthrough
case "linux":
if e, ok := c.linuxEntries[c.curEntry]; ok {
k, err := c.getFile(arg)
if err != nil {
return err
}
e.Kernel = k
}
case "initrd":
if e, ok := c.linuxEntries[c.curEntry]; ok {
// TODO: support multiple comma-separated initrds.
// TODO: append "initrd=$arg" to the cmdline.
//
// For how this interacts with global appends,
// read
// https://wiki.syslinux.org/wiki/index.php?title=Directives/append
i, err := c.getFile(arg)
if err != nil {
return err
}
e.Initrd = i
}
case "append":
switch c.scope {
case scopeGlobal:
c.globalAppend = arg
case scopeEntry:
if e, ok := c.mbEntries[c.curEntry]; ok {
modules := strings.Split(arg, "---")
// The first module is special -- the kernel.
if len(modules) > 0 {
kernel := strings.Fields(modules[0])
k, err := c.getFile(kernel[0])
if err != nil {
return err
}
e.Kernel = k
if len(kernel) > 1 {
e.Cmdline = strings.Join(kernel[1:], " ")
}
modules = modules[1:]
}
for _, cmdline := range modules {
m := strings.Fields(cmdline)
if len(m) == 0 {
continue
}
name := m[0]
file, err := c.getFile(name)
if err != nil {
return err
}
e.Modules = append(e.Modules, multiboot.Module{
CmdLine: strings.TrimSpace(cmdline),
Name: name,
Module: file,
})
}
}
if e, ok := c.linuxEntries[c.curEntry]; ok {
if arg == "-" {
e.Cmdline = ""
} else {
// Yes, we explicitly _override_, not
// concatenate. If a specific append
// directive is present, a global
// append directive is ignored.
//
// Also, "If you enter multiple APPEND
// statements in a single LABEL entry,
// only the last one will be used".
//
// https://wiki.syslinux.org/wiki/index.php?title=Directives/append
e.Cmdline = arg
}
}
}
}
}
// Go through all labels and download the initrds.
for _, label := range c.linuxEntries {
// If the initrd was set via the INITRD directive, don't
// overwrite that.
//
// TODO(hugelgupf): Is this really what syslinux does? Does
// INITRD trump cmdline? Does it trump global? What if both the
// directive and cmdline initrd= are set? Does it depend on the
// order in the config file? (My current best guess: order.)
//
// Answer: Normally, the INITRD directive appends to the
// cmdline, and the _last_ effective initrd= parameter is used
// for loading initrd files.
if label.Initrd != nil {
continue
}
for _, opt := range strings.Fields(label.Cmdline) {
optkv := strings.Split(opt, "=")
if optkv[0] != "initrd" {
continue
}
i, err := c.getFile(optkv[1])
if err != nil {
return err
}
label.Initrd = i
}
}
return nil
}