forked from u-root/u-root
/
main.go
262 lines (232 loc) · 7.16 KB
/
main.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
// Copyright 2017-2019 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 main
import (
"flag"
"log"
"os"
"os/exec"
"os/signal"
"strings"
"time"
"github.com/u-root/u-root/pkg/boot/systembooter"
"github.com/u-root/u-root/pkg/ipmi"
"github.com/u-root/u-root/pkg/ipmi/ocp"
"github.com/u-root/u-root/pkg/smbios"
)
var (
allowInteractive = flag.Bool("i", true, "Allow user to interrupt boot process and run commands")
doQuiet = flag.Bool("q", false, "Disable verbose output")
interval = flag.Int("I", 1, "Interval in seconds before looping to the next boot command")
noDefaultBoot = flag.Bool("nodefault", false, "Do not attempt default boot entries if regular ones fail")
)
var defaultBootsequence = [][]string{
{"fbnetboot", "-userclass", "linuxboot"},
{"localboot", "-grub"},
}
// Product list for running IPMI OEM commands
var productList = [3]string{"Tioga Pass", "Mono Lake", "Delta Lake"}
var selRecorded bool
func isMatched(productName string) bool {
for _, v := range productList {
if strings.HasPrefix(productName, v) {
return true
}
}
return false
}
func getBaseboardProductName(si *smbios.Info) (string, error) {
t2, err := si.GetBaseboardInfo()
if err != nil {
log.Printf("Error getting Baseboard Information: %v", err)
return "", err
}
return t2[0].Product, nil
}
func getSystemFWVersion(si *smbios.Info) (string, error) {
t0, err := si.GetBIOSInfo()
if err != nil {
log.Printf("Error getting BIOS Information: %v", err)
return "", err
}
return t0.Version, nil
}
func checkCMOSClear(ipmi *ipmi.IPMI) error {
if cmosclear, bootorder, err := ocp.IsCMOSClearSet(ipmi); cmosclear {
log.Printf("CMOS clear starts")
if err = cmosClear(); err != nil {
return err
}
// ToDo: Reset RW_VPD to default values
if err = ocp.ClearCMOSClearValidBits(ipmi, bootorder); err != nil {
return err
}
addSEL("cmosclear")
if err = reboot(); err != nil {
return err
}
} else if err != nil {
return err
}
return nil
}
func runIPMICommands() {
i, err := ipmi.Open(0)
if err != nil {
log.Printf("Failed to open ipmi device %v, watchdog may still be running", err)
return
}
defer i.Close()
if err = i.ShutoffWatchdog(); err != nil {
log.Printf("Failed to stop watchdog %v.", err)
} else {
log.Printf("Watchdog is stopped.")
}
// Below IPMI commands would require SMBIOS data
si, err := smbios.FromSysfs()
if err != nil {
log.Printf("Error reading SMBIOS info: %v", err)
return
}
if fwVersion, err := getSystemFWVersion(si); err == nil {
log.Printf("System firmware version: %s", fwVersion)
if err = i.SetSystemFWVersion(fwVersion); err != nil {
log.Printf("Failed to set system firmware version to BMC %v.", err)
}
}
if productName, err := getBaseboardProductName(si); err == nil {
if isMatched(productName) {
log.Printf("Running OEM IPMI commands.")
if err = checkCMOSClear(i); err != nil {
log.Printf("IPMI CMOS clear err: %v", err)
}
dimmInfo, err := ocp.GetOemIpmiDimmInfo(si)
if err == nil {
if err = ocp.SendOemIpmiDimmInfo(i, dimmInfo); err == nil {
log.Printf("Send the information of DIMMs to BMC.")
} else {
log.Printf("Failed to send the information of DIMMs to BMC: %v.", err)
}
} else {
log.Printf("Failed to get the information of DIMMs: %v.", err)
}
processorInfo, err := ocp.GetOemIpmiProcessorInfo(si)
if err == nil {
if err = ocp.SendOemIpmiProcessorInfo(i, processorInfo); err == nil {
log.Printf("Send the information of processors to BMC.")
} else {
log.Printf("Failed to send the information of processors to BMC: %v.", err)
}
} else {
log.Printf("Failed to get the information of Processors: %v.", err)
}
if err = ocp.SetOemIpmiPostEnd(i); err == nil {
log.Printf("Send IPMI POST end to BMC")
} else {
log.Printf("Failed to send IPMI POST end to BMC: %v.", err)
}
} else {
log.Printf("No product name is matched for OEM commands.")
}
}
}
func addSEL(sequence string) {
var bootErr ipmi.Event
i, err := ipmi.Open(0)
if err != nil {
log.Printf("Failed to open ipmi device to send SEL %v", err)
return
}
defer i.Close()
switch sequence {
case "fbnetboot":
bootErr.RecordID = 0
bootErr.RecordType = ipmi.OEM_NTS_TYPE
bootErr.OEMNontsDefinedData[0] = 0x28
bootErr.OEMNontsDefinedData[5] = 0xf0
for idx := 6; idx < 13; idx++ {
bootErr.OEMNontsDefinedData[idx] = 0xff
}
if err := i.LogSystemEvent(&bootErr); err != nil {
log.Printf("SEL recorded: %s fail\n", sequence)
}
case "cmosclear":
bootErr.RecordID = 0
bootErr.RecordType = ipmi.OEM_NTS_TYPE
bootErr.OEMNontsDefinedData[0] = 0x28
bootErr.OEMNontsDefinedData[5] = 0xf1
for idx := 6; idx < 13; idx++ {
bootErr.OEMNontsDefinedData[idx] = 0xff
}
if err := i.LogSystemEvent(&bootErr); err != nil {
log.Printf("SEL recorded: %s fail\n", sequence)
}
default:
}
}
func main() {
flag.Parse()
log.Print(`
____ _ _ _
/ ___| _ _ ___| |_ ___ _ __ ___ | |__ ___ ___ | |_
\___ \| | | / __| __/ _ \ '_ ` + "`" + ` _ \| '_ \ / _ \ / _ \| __|
___) | |_| \__ \ || __/ | | | | | |_) | (_) | (_) | |_
|____/ \__, |___/\__\___|_| |_| |_|_.__/ \___/ \___/ \__|
|___/
`)
runIPMICommands()
sleepInterval := time.Duration(*interval) * time.Second
if *allowInteractive {
log.Printf("**************************************************************************")
log.Print("Starting boot sequence, press CTRL-C within 5 seconds to drop into a shell")
log.Printf("**************************************************************************")
time.Sleep(5 * time.Second)
} else {
signal.Ignore()
}
// Get and show boot entries
bootEntries := systembooter.GetBootEntries()
log.Printf("BOOT ENTRIES:")
for _, entry := range bootEntries {
log.Printf(" %v) %+v", entry.Name, string(entry.Config))
}
for _, entry := range bootEntries {
log.Printf("Trying boot entry %s: %s", entry.Name, string(entry.Config))
if err := entry.Booter.Boot(); err != nil {
log.Printf("Warning: failed to boot with configuration: %+v", entry)
addSEL(entry.Booter.TypeName())
}
if !*doQuiet {
log.Printf("Sleeping %v before attempting next boot command", sleepInterval)
}
time.Sleep(sleepInterval)
}
// if boot entries failed, use the default boot sequence
log.Printf("Boot entries failed")
if !*noDefaultBoot {
log.Print("Falling back to the default boot sequence")
for {
for _, bootcmd := range defaultBootsequence {
if !*doQuiet {
bootcmd = append(bootcmd, "-d")
}
log.Printf("Running boot command: %v", bootcmd)
cmd := exec.Command(bootcmd[0], bootcmd[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Printf("Error executing %v: %v", cmd, err)
if !selRecorded {
addSEL(bootcmd[0])
}
}
}
selRecorded = true
if !*doQuiet {
log.Printf("Sleeping %v before attempting next boot command", sleepInterval)
}
time.Sleep(sleepInterval)
}
}
}