gitee.com/mirrors_u-root/u-root@v7.0.0+incompatible/pkg/mount/mtd/chips.go (about) 1 // Copyright 2019 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package mtd 6 7 import "fmt" 8 9 // VendorFromID returns a Vendor or error given a VendorID. 10 func VendorFromID(v VendorID) (Vendor, error) { 11 for _, vendor := range vendors { 12 if vendor.id == v { 13 return &vendor, nil 14 } 15 } 16 return nil, fmt.Errorf("%v: not a known vendor", v) 17 } 18 19 // VendorFromName returns a Vendor or error given a VendorName. 20 func VendorFromName(v VendorName) (Vendor, error) { 21 for _, vendor := range vendors { 22 for _, n := range vendor.names { 23 if n == v { 24 return &vendor, nil 25 } 26 } 27 } 28 return nil, fmt.Errorf("%v: not a known vendor", v) 29 } 30 31 // Chip returns a Chip or error given a ChipID. 32 func (v *vendor) Chip(id ChipID) (Chip, error) { 33 for _, d := range devices { 34 if d.vendor == v.names[0] && d.id == id { 35 return &d, nil 36 } 37 } 38 return nil, fmt.Errorf("no chip with id %#x for vendor %q", id, v.Synonyms()) 39 } 40 41 // ID returns a VendorID. 42 func (v *vendor) ID() VendorID { 43 return v.id 44 } 45 46 // Name returns the preferred VendorName. 47 func (v *vendor) Name() VendorName { 48 return v.names[0] 49 } 50 51 // Synonyms returns all the synonyms. 52 func (v *vendor) Synonyms() []VendorName { 53 return v.names[1:] 54 } 55 56 // ChipFromVIDDID will return a Chip struct, given a Vendor and Device ID. 57 func ChipFromVIDDID(vid VendorID, did ChipID) (Chip, error) { 58 v, err := VendorFromID(vid) 59 if err != nil { 60 return nil, err 61 } 62 return v.Chip(did) 63 } 64 65 // ID returns the ChipID. 66 func (c *ChipDevice) ID() ChipID { 67 return c.id 68 } 69 70 // Name returns the canonical chip name. 71 func (c *ChipDevice) Name() ChipName { 72 return c.devices[0] 73 } 74 75 // Synonyms returns all synonyms for a chip. 76 func (c *ChipDevice) Synonyms() []ChipName { 77 return c.devices[1:] 78 } 79 80 // Size returns a ChipSize in bytes. 81 func (c *ChipDevice) Size() ChipSize { 82 return ChipSize(c.pageSize * c.numPages) 83 } 84 85 // String is a stringer for a ChipDevice. 86 func (c *ChipDevice) String() string { 87 s := fmt.Sprintf("%s/%s: %d pages, %d pagesize, %#x bytes", c.vendor, c.devices[0], c.numPages, c.pageSize, c.Size()) 88 if len(c.devices) > 1 { 89 s = s + fmt.Sprintf(", synonyms %v", c.devices[1:]) 90 } 91 if len(c.remarks) > 0 { 92 s = s + fmt.Sprintf(", remarks: %s", c.remarks) 93 } 94 return s 95 } 96 97 // Supported returns true if a chip is supported by this package. 98 func Supported(c Chip) bool { 99 return c.Size() != 0 100 }