github.com/linuxboot/fiano@v1.2.0/pkg/cbfs/fns.go (about) 1 // Copyright 2018-2021 the LinuxBoot 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 cbfs 6 7 import ( 8 "encoding/binary" 9 "fmt" 10 "io" 11 "strings" 12 "unicode" 13 ) 14 15 var Debug = func(format string, v ...interface{}) {} 16 17 // Read reads things in in BE format, which they are supposed to be in. 18 func Read(r io.Reader, f interface{}) error { 19 // NOTE: THIS is the `Read` you are looking for! 20 if err := binary.Read(r, Endian, f); err != nil { 21 if err == io.EOF { 22 Debug("Read %v: reached EOF", f) 23 } 24 return err 25 } 26 return nil 27 } 28 29 // ReadLE reads things in LE format, which the spec says it is not in. 30 func ReadLE(r io.Reader, f interface{}) error { 31 if err := binary.Read(r, binary.LittleEndian, f); err != nil { 32 return err 33 } 34 return nil 35 } 36 37 // Write reads things in in BE format, which they are supposed to be in. 38 func Write(w io.Writer, f interface{}) error { 39 if err := binary.Write(w, Endian, f); err != nil { 40 return err 41 } 42 return nil 43 } 44 45 // WriteLE reads things in LE format, which the spec says it is not in. 46 func WriteLE(r io.Writer, f interface{}) error { 47 if err := binary.Write(r, binary.LittleEndian, f); err != nil { 48 return err 49 } 50 return nil 51 } 52 53 func (c Compression) String() string { 54 switch c { 55 case None: 56 return "none" 57 case LZMA: 58 return "lzma" 59 case LZ4: 60 return "lz4" 61 } 62 return "unknown" 63 } 64 65 func (f FileType) String() string { 66 switch f { 67 case TypeDeleted2: 68 return "Deleted2" 69 case TypeDeleted: 70 return "Deleted" 71 case TypeMaster: 72 return "cbfs header" 73 case TypeBootBlock: 74 return "BootBlock" 75 case TypeLegacyStage: 76 return "LegacyStage" 77 case TypeStage: 78 return "Stage" 79 case TypeSELF: 80 return "SELF" 81 case TypeFIT: 82 return "FIT" 83 case TypeOptionRom: 84 return "OptionRom" 85 case TypeBootSplash: 86 return "BootSplash" 87 case TypeRaw: 88 return "Raw" 89 case TypeVSA: 90 return "VSA" 91 case TypeMBI: 92 return "MBI" 93 case TypeMicroCode: 94 return "MicroCode" 95 case TypeFSP: 96 return "FSP" 97 case TypeMRC: 98 return "MRC" 99 case TypeMMA: 100 return "MMA" 101 case TypeEFI: 102 return "EFI" 103 case TypeStruct: 104 return "Struct" 105 case TypeCMOS: 106 return "CMOS" 107 case TypeSPD: 108 return "SPD" 109 case TypeMRCCache: 110 return "MRCCache" 111 case TypeCMOSLayout: 112 return "CMOSLayout" 113 } 114 return fmt.Sprintf("%#x", uint32(f)) 115 } 116 117 func recString(n string, off uint32, typ string, sz uint32, compress string) string { 118 return fmt.Sprintf("%-32s 0x%-8x %-24s 0x%-8x %-4s", n, off, typ, sz, compress) 119 } 120 121 // Clean up non-printable and other characters. 0xfffd is the Unicode tofu char, 122 // aka 'REPLACEMENT CHARACTER': https://unicodemap.org/details/0xFFFD/index.html 123 // Would occur e.g. from `0x66616c6c6261636b2f726f6d737461676500...00ff...ff`. 124 func cleanString(n string) string { 125 return strings.Map(func(r rune) rune { 126 if r != 0xfffd && (unicode.IsPrint(r) || unicode.IsGraphic(r)) { 127 return r 128 } 129 return -1 130 }, n) 131 } 132 133 func ffbyte(s uint32) []byte { 134 b := make([]byte, s) 135 for i := range b { 136 b[i] = 0xff 137 } 138 return b 139 }