forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
271 lines (243 loc) · 6.01 KB
/
parser.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
// Copyright 2017-2018 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 diskboot
import (
"log"
"path/filepath"
"strconv"
"strings"
)
type parserState int
const (
search parserState = iota // searching for a valid entry
grub // building a grub entry
syslinux // building a syslinux entry
)
type parser struct {
state parserState
config *Config
entry *Entry
defaultName string
defaultIndex int
}
func (p *parser) parseSearch(line string) {
trimmedLine := strings.TrimSpace(line)
f := strings.Fields(trimmedLine)
if len(f) == 0 {
return
}
newEntry := false
var name string
switch strings.ToUpper(f[0]) {
case "MENUENTRY": // grub
p.state = grub
newEntry = true
repNames := strings.Replace(trimmedLine, "'", "\"", -1)
names := strings.Split(repNames, "\"")
if len(names) > 1 {
name = names[1]
}
case "SET": // grub
if len(f) > 1 {
p.parseSearchHandleSet(f[1])
}
case "LABEL": // syslinux
p.state = syslinux
newEntry = true
name = trimmedLine[6:]
if name == "" {
name = "linux" // default for syslinux
}
case "DEFAULT": // syslinux
if len(f) > 1 {
label := strings.Join(f[1:], " ")
if !strings.HasSuffix(label, ".c32") {
p.defaultName = label
}
}
}
if newEntry {
p.entry = &Entry{
Name: name,
Type: Elf,
}
}
}
func (p *parser) parseSearchHandleSet(val string) {
val = strings.Replace(val, "\"", "", -1)
expr := strings.Split(val, "=")
if len(expr) != 2 {
return
}
switch expr[0] {
case "default":
index, err := strconv.Atoi(expr[1])
if err == nil {
p.defaultIndex = index
}
default:
// TODO: handle variables when grub conditionals are implemented
return
}
}
func (p *parser) parseGrubEntry(line string) {
trimmedLine := strings.TrimSpace(line)
f := strings.Fields(trimmedLine)
if len(f) == 0 {
return
}
switch f[0] {
case "}":
p.finishEntry()
case "multiboot":
p.entry.Type = Multiboot
p.entry.Modules = append(p.entry.Modules, NewModule(f[1], f[2:]))
case "module":
var filteredParams []string
for _, param := range f {
if param != "--nounzip" {
filteredParams = append(filteredParams, param)
}
}
p.entry.Modules = append(p.entry.Modules,
NewModule(filteredParams[1], filteredParams[2:]))
case "linux":
p.entry.Modules = append(p.entry.Modules, NewModule(f[1], f[2:]))
case "initrd":
p.entry.Modules = append(p.entry.Modules, NewModule(f[1], nil))
}
}
func (p *parser) parseSyslinuxEntry(line string) {
trimmedLine := strings.TrimSpace(line)
if len(trimmedLine) == 0 {
p.finishEntry()
return
}
f := strings.Fields(trimmedLine)
val := strings.Join(f[1:], " ")
switch strings.ToUpper(f[0]) {
case "LABEL": // new entry, finish this one and start a new one
p.finishEntry()
p.parseSearch(line)
case "MENU":
if len(f) > 1 {
switch strings.ToUpper(f[1]) {
case "LABEL":
tempName := strings.Join(f[2:], " ")
p.entry.Name = strings.Replace(tempName, "^", "", -1)
case "DEFAULT":
p.defaultName = p.entry.Name
}
}
case "LINUX", "KERNEL":
p.parseSyslinuxKernel(val)
case "INITRD":
p.entry.Modules = append(p.entry.Modules, NewModule(f[1], nil))
case "APPEND":
p.parseSyslinuxAppend(val)
}
}
func (p *parser) parseSyslinuxKernel(val string) {
if strings.HasSuffix(val, "mboot.c32") {
p.entry.Type = Multiboot
} else if strings.HasSuffix(val, ".c32") {
// skip this entry - not valid for kexec
p.state = search
} else {
p.entry.Modules = append(p.entry.Modules, NewModule(val, nil))
}
}
func (p *parser) parseSyslinuxAppend(val string) {
if p.entry.Type == Multiboot {
// split params by "---" for each module
modules := strings.Split(val, " --- ")
for _, module := range modules {
moduleFields := strings.Fields(module)
p.entry.Modules = append(p.entry.Modules,
NewModule(moduleFields[0], moduleFields[1:]))
}
} else {
if len(p.entry.Modules) == 0 {
// TODO: log error
return
}
p.entry.Modules[0].Params = val
}
}
func (p *parser) finishEntry() {
// skip empty entries
if len(p.entry.Modules) == 0 {
return
}
// try to fix up initrd from kernel params
if len(p.entry.Modules) == 1 && p.entry.Type == Elf {
var initrd string
var newParams []string
params := strings.Fields(p.entry.Modules[0].Params)
for _, param := range params {
if strings.HasPrefix(param, "initrd=") {
initrd = param[7:]
} else {
newParams = append(newParams, param)
}
}
if initrd != "" {
p.entry.Modules = append(p.entry.Modules, NewModule(initrd, nil))
p.entry.Modules[0].Params = strings.Join(newParams, " ")
}
}
appendPath, err := filepath.Rel(p.config.MountPath, filepath.Dir(p.config.ConfigPath))
if err != nil {
log.Fatal("Config file path not relative to mount path")
}
for i, module := range p.entry.Modules {
if !strings.HasPrefix(module.Path, "/") {
module.Path = filepath.Join("/"+appendPath, module.Path)
}
module.Path = filepath.Clean(module.Path)
p.entry.Modules[i] = module
}
p.state = search
p.config.Entries = append(p.config.Entries, *p.entry)
}
func (p *parser) parseLines(lines []string) {
p.state = search
for _, line := range lines {
switch p.state {
case search:
p.parseSearch(line)
case grub:
p.parseGrubEntry(line)
case syslinux:
p.parseSyslinuxEntry(line)
}
}
if p.state == syslinux {
p.finishEntry()
}
}
// ParseConfig attempts to construct a valid boot Config from the location
// and lines contents passed in.
func ParseConfig(mountPath, configPath string, lines []string) *Config {
p := &parser{
config: &Config{
MountPath: mountPath,
ConfigPath: configPath,
DefaultEntry: -1,
},
defaultIndex: -1,
}
p.parseLines(lines)
if p.defaultName != "" {
for i, entry := range p.config.Entries {
if entry.Name == p.defaultName {
p.config.DefaultEntry = i
}
}
}
if p.defaultIndex >= 0 && len(p.config.Entries) > p.defaultIndex {
p.config.DefaultEntry = p.defaultIndex
}
return p.config
}