-
Notifications
You must be signed in to change notification settings - Fork 2
/
boot.go
194 lines (169 loc) · 5.74 KB
/
boot.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
//go:generate stringer -type=bootConfigRole -trimprefix=bootConfigRole -linecomment
package amt
import (
"fmt"
"strconv"
"github.com/VictorLowther/simplexml/dom"
"github.com/VictorLowther/simplexml/search"
"github.com/ammmze/go-amt/cim"
"github.com/ammmze/wsman"
)
type bootConfigRole int
const (
bootConfigRoleIsNext bootConfigRole = 0
bootConfigRoleIsNextSingleUse bootConfigRole = 1
bootConfigRoleIsDefault bootConfigRole = 2
bootConfigRoleDMTFReserved bootConfigRole = 3 // 3..32767
bootConfigRoleVendorSpecified bootConfigRole = 32768 // 32768..65535
)
// SetPXE makes sure the node will pxe boot next time.
func (c *Client) SetPXE() error {
// clear existing boot order per meshcommander's implementation...
// "Set the boot order to null, this is needed for some AMT versions that don't clear this automatically."
// err := changeBootOrder(client, []string{})
// if err != nil {
// return err
// }
if err := setBootSettingData(c); err != nil {
return err
}
if err := setBootConfigRole(c, bootConfigRoleIsNextSingleUse); err != nil {
return err
}
if err := changeBootOrder(c, []string{"Intel(r) AMT: Force PXE Boot"}); err != nil {
return err
}
return nil
}
func setBootConfigRole(client *Client, role bootConfigRole) error {
bootConfigRef, err := getBootConfigSettingRef(client, "Intel(r) AMT: Boot Configuration 0")
if err != nil {
return err
}
message := client.wsManClient.Invoke(resourceCIMBootService, "SetBootConfigRole")
bootConfigSetting := message.MakeParameter("BootConfigSetting")
bootConfigSetting.AddChildren(bootConfigRef.Children()...)
message.AddParameter(bootConfigSetting)
message.Parameters("Role", strconv.Itoa(int(role)))
_, err = sendMessageForReturnValueInt(message)
if err != nil {
return err
}
return nil
}
func changeBootOrder(client *Client, items []string) error {
message := client.wsManClient.Invoke(resourceCIMBootConfigSetting, "ChangeBootOrder")
if len(items) > 0 {
// TODO: multiple?
pxeEndpointRef, err := getBootSourceRef(client, "Intel(r) AMT: Force PXE Boot")
if err != nil {
return err
}
sourceParam := message.MakeParameter("Source")
sourceParam.AddChildren(pxeEndpointRef.Children()...)
message.AddParameter(sourceParam)
}
_, err := sendMessageForReturnValueInt(message)
if err != nil {
return err
}
return nil
}
func getBootSettingData(client *Client) ([]*dom.Element, error) {
msg := client.wsManClient.Get(resourceAMTBootSettingData)
response, err := msg.Send()
if err != nil {
return nil, err
}
data := search.FirstTag("AMT_BootSettingData", resourceAMTBootSettingData, response.Body())
if data == nil {
return nil, fmt.Errorf("response was missing the AMT_BootSettingData")
}
return data.Children(), nil
}
func setBootSettingData(client *Client) error {
bootSettings, err := getBootSettingData(client)
if err != nil {
return err
}
settingsToKeep := []*dom.Element{}
for _, setting := range bootSettings {
switch setting.Name.Local {
// omit these ones ... they are read-only parameters (per meshcommand implementation)
case "WinREBootEnabled",
"UEFILocalPBABootEnabled",
"UEFIHTTPSBootEnabled",
"SecureBootControlEnabled",
"BootguardStatus",
"OptionsCleared",
"BIOSLastStatus",
"UefiBootParametersArray":
continue
// gonna make sure these are set to "false"
case "BIOSPause", "BIOSSetup":
setting.Content = []byte("false")
settingsToKeep = append(settingsToKeep, setting)
// gonna make sure these are set to "0"
case "BootMediaIndex":
setting.Content = []byte("0")
settingsToKeep = append(settingsToKeep, setting)
default:
settingsToKeep = append(settingsToKeep, setting)
}
}
msg := client.wsManClient.Put(resourceAMTBootSettingData)
data := dom.Elem("AMT_BootSettingData", resourceAMTBootSettingData)
data.AddChildren(settingsToKeep...)
msg.SetBody(data)
_, err = msg.Send()
return err
}
func getBootConfigSettingRef(client *Client, name string) (*dom.Element, error) {
return getEndpointReferenceByInstanceID(client, resourceCIMBootConfigSetting, name)
}
func getBootSourceRef(client *Client, name string) (*dom.Element, error) {
return getEndpointReferenceByInstanceID(client, resourceCIMBootSourceSetting, name)
}
func getStructuredBootOrder(client *Client) ([]string, error) {
msg, err := client.wsManClient.EnumerateObjectAndEPR("http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_BootConfigSetting").Send()
if err != nil {
return nil, err
}
bootConfigs := []cim.BootConfigSettingTypeAndEPRItem{}
err = msg.UnmarshalEnumItems(&bootConfigs)
if err != nil {
return nil, err
}
if len(bootConfigs) == 0 {
return nil, fmt.Errorf("no boot config was found. cannot get boot order")
}
msg, err = client.wsManClient.EnumerateObjectAndEPR("http://schemas.dmtf.org/wbem/wscim/1/*").
AddFilterWithDialect("http://schemas.dmtf.org/wbem/wsman/1/cimbinding/associationFilter", wsman.MarshalToElementOrPanic(
cim.AssociationInstances{
Object: bootConfigs[0].EndpointReference,
Role: "GroupComponent",
ResultClassName: "CIM_OrderedComponent",
})).
Send()
if err != nil {
return nil, err
}
bootSourceSettings := []cim.OrderedComponentTypeAndEPRItem{}
msg.UnmarshalEnumItems(&bootSourceSettings)
bootSources := []string{}
for _, src := range bootSourceSettings {
msg, err := client.wsManClient.Get(src.OrderedComponent.PartComponent.ReferenceParameters.ResourceURI).
AddSelector(wsman.MarshalToElementOrPanic(src.OrderedComponent.PartComponent.ReferenceParameters.SelectorSet).Children()...).
Send()
if err != nil {
return nil, err
}
settings := &cim.BootSourceSettingType{}
err = msg.UnmarshalBody(settings)
if err != nil {
return nil, err
}
bootSources = append(bootSources, settings.StructuredBootString)
}
return bootSources, nil
}