forked from u-root/u-root
/
chips.go
100 lines (85 loc) · 2.37 KB
/
chips.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
// Copyright 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 mtd
import "fmt"
// VendorFromID returns a Vendor or error given a VendorID.
func VendorFromID(v VendorID) (Vendor, error) {
for _, vendor := range vendors {
if vendor.id == v {
return &vendor, nil
}
}
return nil, fmt.Errorf("%v: not a known vendor", v)
}
// VendorFromName returns a Vendor or error given a VendorName.
func VendorFromName(v VendorName) (Vendor, error) {
for _, vendor := range vendors {
for _, n := range vendor.names {
if n == v {
return &vendor, nil
}
}
}
return nil, fmt.Errorf("%v: not a known vendor", v)
}
// Chip returns a Chip or error given a ChipID.
func (v *vendor) Chip(id ChipID) (Chip, error) {
for _, d := range devices {
if d.vendor == v.names[0] && d.id == id {
return &d, nil
}
}
return nil, fmt.Errorf("No chip with id %#x for vendor %q", id, v.Synonyms())
}
// ID returns a VendorID.
func (v *vendor) ID() VendorID {
return v.id
}
// Name returns the preferred VendorName.
func (v *vendor) Name() VendorName {
return v.names[0]
}
// Synonyms returns all the synonyms.
func (v *vendor) Synonyms() []VendorName {
return v.names[1:]
}
// ChipFromVIDDID will return a Chip struct, given a Vendor and Device ID.
func ChipFromVIDDID(vid VendorID, did ChipID) (Chip, error) {
v, err := VendorFromID(vid)
if err != nil {
return nil, err
}
return v.Chip(did)
}
// ID returns the ChipID.
func (c *ChipDevice) ID() ChipID {
return c.id
}
// Name returns the canonical chip name.
func (c *ChipDevice) Name() ChipName {
return c.devices[0]
}
// Synonyms returns all synonyms for a chip.
func (c *ChipDevice) Synonyms() []ChipName {
return c.devices[1:]
}
// Size returns a ChipSize in bytes.
func (c *ChipDevice) Size() ChipSize {
return ChipSize(c.pageSize * c.numPages)
}
// String is a stringer for a ChipDevice.
func (c *ChipDevice) String() string {
s := fmt.Sprintf("%s/%s: %d pages, %d pagesize, %#x bytes", c.vendor, c.devices[0], c.numPages, c.pageSize, c.Size())
if len(c.devices) > 1 {
s = s + fmt.Sprintf(", synonyms %v", c.devices[1:])
}
if len(c.remarks) > 0 {
s = s + fmt.Sprintf(", remarks: %s", c.remarks)
}
return s
}
// Supported returns true if a chip is supported by this package.
func Supported(c Chip) bool {
return c.Size() != 0
}