-
Notifications
You must be signed in to change notification settings - Fork 0
/
coreboot.go
346 lines (309 loc) · 11 KB
/
coreboot.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
// SPDX-License-Identifier: MIT
// Package recipes / coreboot
package recipes
import (
"context"
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
"dagger.io/dagger"
"github.com/9elements/firmware-action/action/container"
"github.com/9elements/firmware-action/action/filesystem"
"github.com/9elements/firmware-action/action/logging"
)
// BlobDef is used to store information about a single blob.
// This structure is not exposed to the user, it is filled in automatically based on user input.
type BlobDef struct {
// Path to the blob (either file or directory)
Path string `validate:"required"`
// Blobs get renamed when moved to this string
DestinationFilename string `validate:"required"`
// Kconfig key specifying the filepath to the blob in defconfig
KconfigKey string `validate:"required"`
// Is blob a directory? If blob is file, set to FALSE
IsDirectory bool `validate:"required,boolean"`
}
// CorebootBlobs is used to store data specific to coreboot.
// ANCHOR: CorebootBlobs
type CorebootBlobs struct {
// ** List of supported blobs **
// NOTE: The blobs may not be added to the ROM, depends on provided defconfig.
//
// Gives the (relative) path to the payload.
// In a 'coreboot' build, the file will be placed at
// `3rdparty/blobs/mainboard/$(MAINBOARDDIR)/payload`.
// The Kconfig `CONFIG_PAYLOAD_FILE` will be changed to point to the same path.
PayloadFilePath string `json:"payload_file_path" type:"blob"`
// Gives the (relative) path to the Intel Flash descriptor binary.
// In a 'coreboot' build, the file will be placed at
// `3rdparty/blobs/mainboard/$(CONFIG_MAINBOARD_DIR)/descriptor.bin`.
// The Kconfig `CONFIG_IFD_BIN_PATH` will be changed to point to the same path.
IntelIfdPath string `json:"intel_ifd_path" type:"blob"`
// Gives the (relative) path to the Intel Management engine binary.
// In a 'coreboot' build, the file will be placed at
// `3rdparty/blobs/mainboard/$(CONFIG_MAINBOARD_DIR)/me.bin`.
// The Kconfig `CONFIG_ME_BIN_PATH` will be changed to point to the same path.
IntelMePath string `json:"intel_me_path" type:"blob"`
// Gives the (relative) path to the Intel Gigabit Ethernet engine binary.
// In a 'coreboot' build, the file will be placed at
// `3rdparty/blobs/mainboard/$(CONFIG_MAINBOARD_DIR)/gbe.bin`.
// The Kconfig `CONFIG_GBE_BIN_PATH` will be changed to point to the same path.
IntelGbePath string `json:"intel_gbe_path" type:"blob"`
// Gives the (relative) path to the Intel FSP binary.
// In a 'coreboot' build, the file will be placed at
// `3rdparty/blobs/mainboard/$(CONFIG_MAINBOARD_DIR)/Fsp.fd`.
// The Kconfig `CONFIG_FSP_FD_PATH` will be changed to point to the same path.
FspBinaryPath string `json:"fsp_binary_path" type:"blob"`
// Gives the (relative) path to the Intel FSP header folder.
// In a 'coreboot' build, the files will be placed at
// `3rdparty/blobs/mainboard/$(CONFIG_MAINBOARD_DIR)/Include`.
// The Kconfig `CONFIG_FSP_HEADER_PATH` will be changed to point to the same path.
FspHeaderPath string `json:"fsp_header_path" type:"blob"`
// Gives the (relative) path to the Video BIOS Table binary.
// In a 'coreboot' build, the files will be placed at
// `3rdparty/blobs/mainboard/$(CONFIG_MAINBOARD_DIR)/vbt.bin`.
// The Kconfig `CONFIG_INTEL_GMA_VBT_FILE` will be changed to point to the same path.
VbtPath string `json:"vbt_path" type:"blob"`
// Gives the (relative) path to the Embedded Controller binary.
// In a 'coreboot' build, the files will be placed at
// `3rdparty/blobs/mainboard/$(CONFIG_MAINBOARD_DIR)/ec.bin`.
// The Kconfig `CONFIG_EC_BIN_PATH` will be changed to point to the same path.
EcPath string `json:"ec_path" type:"blob"`
}
// ANCHOR_END: CorebootBlobs
// CorebootOpts is used to store all data needed to build coreboot.
// ANCHOR: CorebootOpts
type CorebootOpts struct {
// List of IDs this instance depends on
Depends []string `json:"depends"`
// Common options like paths etc.
CommonOpts
// Gives the (relative) path to the defconfig that should be used to build the target.
DefconfigPath string `json:"defconfig_path" validate:"required,filepath"`
// Coreboot specific options
Blobs CorebootBlobs `json:"blobs"`
}
// ANCHOR_END: CorebootOpts
// GetDepends is used to return list of dependencies
func (opts CorebootOpts) GetDepends() []string {
return opts.Depends
}
// GetArtifacts returns list of wanted artifacts from container
func (opts CorebootOpts) GetArtifacts() *[]container.Artifacts {
return opts.CommonOpts.GetArtifacts()
}
// corebootProcessBlobs is used to fill figure out blobs from provided data.
func corebootProcessBlobs(opts CorebootBlobs) ([]BlobDef, error) {
blobMap := map[string]BlobDef{
// Payload
// docs: https://doc.coreboot.org/payloads.html
"payload_file_path": {
DestinationFilename: "payload",
KconfigKey: "CONFIG_PAYLOAD_FILE",
IsDirectory: false,
},
// Intel IFD (Intel Flash Descriptor)
// docs: https://doc.coreboot.org/util/ifdtool/layout.html
"intel_ifd_path": {
DestinationFilename: "descriptor.bin",
KconfigKey: "CONFIG_IFD_BIN_PATH",
IsDirectory: false,
},
// Intel ME (Intel Management Engine)
"intel_me_path": {
DestinationFilename: "me.bin",
KconfigKey: "CONFIG_ME_BIN_PATH",
IsDirectory: false,
},
// Intel GbE (Intel Gigabit Ethernet)
"intel_gbe_path": {
DestinationFilename: "gbe.bin",
KconfigKey: "CONFIG_GBE_BIN_PATH",
IsDirectory: false,
},
// Intel FSP binary (Intel Firmware Support Package)
"fsp_binary_path": {
DestinationFilename: "Fsp.fd",
KconfigKey: "CONFIG_FSP_FD_PATH",
IsDirectory: false,
},
// Intel FSP header (Intel Firmware Support Package)
"fsp_header_path": {
DestinationFilename: "Include",
KconfigKey: "CONFIG_FSP_HEADER_PATH",
IsDirectory: true,
},
// VBT (Video BIOS Table)
"vbt_path": {
DestinationFilename: "vbt.bin",
KconfigKey: "CONFIG_INTEL_GMA_VBT_FILE",
IsDirectory: false,
},
// EC (Embedded Controller)
"ec_path": {
DestinationFilename: "ec.bin",
KconfigKey: "CONFIG_EC_BIN_PATH",
IsDirectory: false,
},
}
blobs := []BlobDef{}
blob := reflect.ValueOf(opts)
for i := 0; i < blob.Type().NumField(); i++ {
t := blob.Type().Field(i)
jsonTag := t.Tag.Get("json")
jsonType := t.Tag.Get("type")
if jsonTag != "" && jsonType == "blob" {
newBlob := blobMap[jsonTag]
newBlob.Path = blob.Field(i).Interface().(string)
if newBlob.Path != "" {
blobs = append(blobs, newBlob)
}
}
}
return blobs, nil
}
// buildFirmware builds coreboot with all blobs and stuff
func (opts CorebootOpts) buildFirmware(ctx context.Context, client *dagger.Client, dockerfileDirectoryPath string) (*dagger.Container, error) {
// Spin up container
containerOpts := container.SetupOpts{
ContainerURL: opts.SdkURL,
MountContainerDir: ContainerWorkDir,
MountHostDir: opts.RepoPath,
WorkdirContainer: ContainerWorkDir,
}
myContainer, err := container.Setup(ctx, client, &containerOpts, dockerfileDirectoryPath)
if err != nil {
slog.Error(
"Failed to start a container",
slog.Any("error", err),
)
return nil, err
}
// Copy over the defconfig file
defconfigBasename := filepath.Base(opts.DefconfigPath)
// not sure why, but without the 'pwd' I am getting different results between CI and 'go test'
pwd, err := os.Getwd()
if err != nil {
slog.Error(
"Could not get working directory, should not happen",
slog.String("suggestion", logging.ThisShouldNotHappenMessage),
slog.Any("error", err),
)
return nil, err
}
myContainer = myContainer.WithFile(
filepath.Join(ContainerWorkDir, defconfigBasename),
client.Host().File(filepath.Join(pwd, opts.DefconfigPath)),
)
// Get value of CONFIG_MAINBOARD_DIR / MAINBOARD_DIR variable from dotconfig
// to extract value of 'CONFIG_MAINBOARD_DIR', there must be '.config'
generateDotConfigCmd := []string{"make", fmt.Sprintf("KBUILD_DEFCONFIG=%s", defconfigBasename), "defconfig"}
myContainerPrevious := myContainer
mainboardDir, err := myContainer.
WithExec(generateDotConfigCmd).
WithExec([]string{"./util/scripts/config", "-s", "CONFIG_MAINBOARD_DIR"}).
Stdout(ctx)
if err != nil {
slog.Error(
"Failed to get value of MAINBOARD_DIR from .config",
slog.Any("error", err),
)
return myContainerPrevious, err
}
// strip newline from mainboardDir
mainboardDir = strings.Replace(mainboardDir, "\n", "", -1)
// Assemble commands to build
buildSteps := [][]string{
// remove existing config if exists
// -f: ignore nonexistent files
{"rm", "-f", ".config"},
// generate dotconfig from defconfig
generateDotConfigCmd,
}
// Handle blobs
// Firstly copy all the blobs into building container.
// Then use './util/scripts/config' script in coreboot repository to update configuration
// options for said blobs (this must run inside container).
blobs, err := corebootProcessBlobs(opts.Blobs)
if err != nil {
slog.Error(
"Failed to process all blobs",
slog.Any("error", err),
)
return nil, err
}
for blob := range blobs {
// Path to local file on host
src := filepath.Join(
pwd,
blobs[blob].Path,
)
// Path to file in container
dst := filepath.Join(
filepath.Join("3rdparty/blobs/mainboard", mainboardDir),
blobs[blob].DestinationFilename,
)
// Copy into container
if err = filesystem.CheckFileExists(src); !errors.Is(err, os.ErrExist) {
return nil, err
}
if blobs[blob].IsDirectory {
// Directory
slog.Info(fmt.Sprintf("Copying directory '%s' to container at '%s'", src, dst))
myContainer = myContainer.WithExec([]string{"mkdir", "-p", dst})
// myContainer = myContainer.WithMountedDirectory(
// can't use WithMountedDirectory because the repo (aka working directory)
// is already mounted with WithMountedDirectory
// this nesting causes problems
myContainer = myContainer.WithDirectory(
dst,
client.Host().Directory(src),
)
} else {
// File
myContainer = myContainer.WithFile(
dst,
client.Host().File(src),
)
}
// Fix defconfig
buildSteps = append(
buildSteps,
// update coreboot config value related to blob to actual path of the blob
[]string{"./util/scripts/config", "--set-str", blobs[blob].KconfigKey, dst},
)
}
buildSteps = append(
buildSteps,
// compile
[]string{"make", "-j", fmt.Sprintf("%d", runtime.NumCPU())},
// for documenting purposes
[]string{"make", "savedefconfig"},
)
// Setup environment variables in the container
envVars := map[string]string{}
for key, value := range envVars {
myContainer = myContainer.WithEnvVariable(key, value)
}
// Build
for step := range buildSteps {
myContainerPrevious := myContainer
myContainer, err = myContainer.
WithExec(buildSteps[step]).
Sync(ctx)
if err != nil {
slog.Error(
"Failed to build coreboot",
slog.Any("error", err),
)
return myContainerPrevious, fmt.Errorf("coreboot build failed: %w", err)
}
}
// Extract artifacts
return myContainer, container.GetArtifacts(ctx, myContainer, opts.CommonOpts.GetArtifacts())
}