-
Notifications
You must be signed in to change notification settings - Fork 47
/
entry_base.go
63 lines (50 loc) · 1.98 KB
/
entry_base.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
// Copyright 2017-2021 the LinuxBoot 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 fit
import (
"fmt"
"io"
)
// EntryBase is the common information for any FIT entry
type EntryBase struct {
// Headers is FIT entry headers.
//
// See "Table 1-1" in "1.2 Firmware Interface Table" in "Firmware Interface Table" specification:
// * https://www.intel.com/content/dam/www/public/us/en/documents/guides/fit-bios-specification.pdf
Headers EntryHeaders
// DataSegmentBytes is the raw bytes of the special data segment referenced by the headers.
//
// Is not nil only if FIT entry really references to a data segment. If FIT entry
// stores data directly in headers then DataSegmentBytes is nil.
DataSegmentBytes []byte `json:",omitempty"`
// HeadersErrors is the list of errors occurred while parsing and interpreting FIT entry headers.
HeadersErrors []error `json:",omitempty"`
}
// GetEntryBase returns EntryBase (which contains metadata of the Entry).
func (entry *EntryBase) GetEntryBase() *EntryBase {
return entry
}
// GoString implements fmt.GoStringer
func (entry *EntryBase) GoString() string {
return entry.Headers.GoString()
}
// injectDataSectionTo does the same as InjectData, but for io.WriteSeeker.
func (entry EntryBase) injectDataSectionTo(w io.WriteSeeker) error {
base := entry.GetEntryBase()
if len(base.DataSegmentBytes) == 0 {
return nil
}
firmwareSize, err := w.Seek(0, io.SeekEnd)
if err != nil {
return fmt.Errorf("unable to detect firmware size: %w", err)
}
dataSectionOffset := base.Headers.Address.Offset(uint64(firmwareSize))
if _, err := w.Seek(int64(dataSectionOffset), io.SeekStart); err != nil {
return fmt.Errorf("unable to Seek(%d, %d) to write the data section: %w", int64(dataSectionOffset), io.SeekStart, err)
}
if _, err := w.Write(entry.DataSegmentBytes); err != nil {
return fmt.Errorf("unable to write the data section: %w", err)
}
return nil
}