summaryrefslogtreecommitdiff
path: root/util/board_status/go/src/cbfs/cbfs.go
blob: 042d35fa72056f7906a7959e8d872b9e37da2bef (plain)
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
package cbfs

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"os"
	"sort"
	"strings"
	"text/tabwriter"
)

type CBFSReader interface {
	GetFile(name string) ([]byte, error)
	ListFiles() ([]string, error)
}

type ArchType uint32
type FileType uint32

type CBFSHeader struct {
	Magic         uint32
	Version       uint32
	ROMSize       uint32
	BootBlockSize uint32
	Align         uint32
	Offset        uint32
	Architecture  ArchType
	Pad           [1]uint32
}

func (a ArchType) String() string {
	switch a {
	case 0xFFFFFFFF:
		return "unknown"
	case 0x00000001:
		return "x86"
	case 0x00000010:
		return "arm"
	default:
		return fmt.Sprintf("0x%x", a)
	}
}

func (f FileType) String() string {
	switch f {
	case 0xffffffff:
		return "null"
	case 0x10:
		return "stage"
	case 0x20:
		return "payload"
	case 0x30:
		return "optionrom"
	case 0x40:
		return "bootsplash"
	case 0x50:
		return "raw"
	case 0x51:
		return "vsa"
	case 0x52:
		return "mbi"
	case 0x53:
		return "microcode"
	case 0xaa:
		return "cmos_default"
	case 0x1aa:
		return "cmos_layout"
	default:
		return fmt.Sprintf("0x%x", uint32(f))
	}
}

func (c CBFSHeader) String() (ret string) {
	ret = fmt.Sprintf("bootblocksize: %d\n", c.BootBlockSize)
	ret += fmt.Sprintf("romsize: %d\n", c.ROMSize)
	ret += fmt.Sprintf("offset: 0x%x\n", c.Offset)
	ret += fmt.Sprintf("alignment: %d bytes\n", c.Align)
	ret += fmt.Sprintf("architecture: %v\n", c.Architecture)
	ret += fmt.Sprintf("version: 0x%x\n", c.Version)
	return ret
}

const sizeofFileHeader = 24
const CBFSHeaderMagic = 0x4F524243

type CBFSFileHeader struct {
	Magic    [8]byte
	Len      uint32
	Type     FileType
	CheckSum uint32
	Offset   uint32
}

type cBFSFile struct {
	headerOffset uint64
	header       CBFSFileHeader
	name         string
}

type cBFSDesc struct {
	file      *os.File
	end       uint64
	headerPos uint64
	rOMStart  uint64
	fileNames map[string]cBFSFile
	files     []cBFSFile
	header    CBFSHeader
}

func (c cBFSDesc) align(offset uint32) uint32 {
	a := uint32(c.header.Align)
	return (a + offset - 1) & ^(a - 1)
}

func (c cBFSDesc) ListFiles() (files []string, err error) {
	for name, _ := range c.fileNames {
		files = append(files, name)
	}
	sort.Strings(files)
	return files, nil
}

func (c cBFSDesc) GetFile(name string) ([]byte, error) {
	file, ok := c.fileNames[name]
	if !ok {
		return nil, fmt.Errorf("file not found: %s", name)
	}
	_, err := c.file.Seek(int64(file.headerOffset)+int64(file.header.Offset), 0)
	if err != nil {
		return nil, err
	}
	ret := make([]byte, file.header.Len, file.header.Len)
	r, err := c.file.Read(ret)
	if err != nil {
		return nil, err
	}
	if r != len(ret) {
		return nil, fmt.Errorf("incomplete read")
	}
	return ret, nil
}

func (c cBFSDesc) String() (ret string) {
	ret = c.header.String()
	ret += "\n"
	buf := bytes.NewBuffer([]byte{})
	w := new(tabwriter.Writer)
	w.Init(buf, 15, 0, 1, ' ', 0)
	fmt.Fprintln(w, "Name\tOffset\tType\tSize\t")
	for _, file := range c.files {
		name := file.name
		if file.header.Type == 0xffffffff {
			name = "(empty)"
		}
		fmt.Fprintf(w, "%s\t0x%x\t%v\t%d\t\n",
			name, file.headerOffset-c.rOMStart,
			file.header.Type, file.header.Len)
	}
	w.Flush()
	ret += buf.String()
	return ret
}

func openGeneric(cbfs *cBFSDesc) (CBFSReader, error) {
	_, err := cbfs.file.Seek(int64(cbfs.end-4), 0)
	if err != nil {
		return nil, err
	}
	headerPos := int32(0)
	binary.Read(cbfs.file, binary.LittleEndian, &headerPos)
	if headerPos < 0 {
		cbfs.headerPos = cbfs.end - uint64(-headerPos)
	} else {
		cbfs.headerPos = uint64(headerPos)
	}
	_, err = cbfs.file.Seek(int64(cbfs.headerPos), 0)
	if err != nil {
		return nil, err
	}
	err = binary.Read(cbfs.file, binary.BigEndian, &cbfs.header)
	if err != nil {
		return nil, err
	}
	if cbfs.header.Magic != CBFSHeaderMagic {
		return nil, fmt.Errorf("invalid header magic")
	}

	cbfs.fileNames = map[string]cBFSFile{}

	curptr := cbfs.end - uint64(cbfs.header.ROMSize) + uint64(cbfs.header.Offset)
	cbfs.rOMStart = cbfs.end - uint64(cbfs.header.ROMSize)
	for {
		file := cBFSFile{headerOffset: curptr}
		_, err = cbfs.file.Seek(int64(curptr), 0)
		if err != nil {
			return nil, err
		}
		err = binary.Read(cbfs.file, binary.BigEndian, &file.header)
		if err != nil {
			return nil, err
		}
		if string(file.header.Magic[:]) != "LARCHIVE" {
			return *cbfs, nil
		}
		name := make([]byte, file.header.Offset-sizeofFileHeader, file.header.Offset-sizeofFileHeader)
		_, err = cbfs.file.Read(name)
		if err != nil {
			return nil, err
		}
		nameStr := string(name)
		idx := strings.Index(nameStr, "\000")
		if idx >= 0 {
			nameStr = nameStr[0:idx]
		}
		file.name = nameStr
		cbfs.fileNames[nameStr] = file
		cbfs.files = append(cbfs.files, file)
		curptr += uint64(cbfs.align(file.header.Offset + file.header.Len))
	}
}

func OpenFile(file *os.File) (CBFSReader, error) {
	stat, err := file.Stat()
	if err != nil {
		return nil, err
	}
	cbfs := cBFSDesc{file: file, end: uint64(stat.Size())}
	return openGeneric(&cbfs)
}

func OpenROM() (CBFSReader, error) {
	file, err := os.Open("/dev/mem")
	if err != nil {
		return nil, err
	}
	cbfs := cBFSDesc{file: file, end: 0x100000000}
	return openGeneric(&cbfs)
}