github.com/linuxboot/fiano@v1.2.0/pkg/uefi/region.go (about) 1 // Copyright 2018 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 uefi 6 7 import ( 8 "fmt" 9 ) 10 11 const ( 12 // RegionBlockSize assumes the region struct values correspond to blocks of 0x1000 in size 13 RegionBlockSize = 0x1000 14 ) 15 16 // FlashRegionType represents the different types possible in a flash region. 17 type FlashRegionType int 18 19 // IFD Region types. 20 // This also corresponds to their index in the flash region section. 21 // Referenced from github.com/LongSoft/UEFITool, common/descriptor.h. 22 const ( 23 RegionTypeBIOS FlashRegionType = iota 24 RegionTypeME 25 RegionTypeGBE 26 RegionTypePD 27 RegionTypeDevExp1 28 RegionTypeBIOS2 29 RegionTypeMicrocode 30 RegionTypeEC 31 RegionTypeDevExp2 32 RegionTypeIE 33 RegionTypeTGBE1 34 RegionTypeTGBE2 35 RegionTypeReserved1 36 RegionTypeReserved2 37 RegionTypePTT 38 39 RegionTypeUnknown FlashRegionType = -1 40 ) 41 42 var flashRegionTypeNames = map[FlashRegionType]string{ 43 RegionTypeBIOS: "BIOS", 44 RegionTypeME: "ME", 45 RegionTypeGBE: "GbE", 46 RegionTypePD: "PD", 47 RegionTypeDevExp1: "DevExp1", 48 RegionTypeBIOS2: "BIOS2", 49 RegionTypeMicrocode: "Microcode", 50 RegionTypeEC: "EC", 51 RegionTypeDevExp2: "DevExp2", 52 RegionTypeIE: "IE", 53 RegionTypeTGBE1: "10GbE1", 54 RegionTypeTGBE2: "10GbE2", 55 RegionTypeReserved1: "Reserved1", 56 RegionTypeReserved2: "Reserved2", 57 RegionTypePTT: "PTT", 58 // RegionTypeUnknown doesn't have a string name, we want it 59 // to fallback and print the number 60 } 61 62 func (rt FlashRegionType) String() string { 63 if s, ok := flashRegionTypeNames[rt]; ok { 64 return s 65 } 66 return fmt.Sprintf("Unknown Region (%d)", rt) 67 } 68 69 // FlashRegion holds the base and limit of every type of region. Each region such as the bios region 70 // should point back to it. 71 // TODO: figure out of block sizes are read from some location on flash or fixed. 72 // Right now we assume they're fixed to 4KiB 73 type FlashRegion struct { 74 Base uint16 // Index of first 4KiB block 75 Limit uint16 // Index of last block 76 } 77 78 // Valid checks to see if a region is valid 79 func (r *FlashRegion) Valid() bool { 80 // The ODROID bios seems to be different from all other bioses, and seems to not report 81 // invalid regions correctly. They report a limit and base of 0xFFFF instead of a limit of 0 82 return r.Limit > 0 && r.Limit >= r.Base && r.Limit != 0xFFFF && r.Base != 0xFFFF 83 } 84 85 func (r *FlashRegion) String() string { 86 return fmt.Sprintf("[%#x, %#x)", r.Base, r.Limit) 87 } 88 89 // BaseOffset calculates the offset into the flash image where the Region begins 90 func (r *FlashRegion) BaseOffset() uint32 { 91 return uint32(r.Base) * RegionBlockSize 92 } 93 94 // EndOffset calculates the offset into the flash image where the Region ends 95 func (r *FlashRegion) EndOffset() uint32 { 96 return (uint32(r.Limit) + 1) * RegionBlockSize 97 } 98 99 var regionConstructors = map[FlashRegionType]func(buf []byte, r *FlashRegion, rt FlashRegionType) (Region, error){ 100 RegionTypeBIOS: NewBIOSRegion, 101 RegionTypeME: NewMERegion, 102 RegionTypeGBE: NewRawRegion, 103 RegionTypePD: NewRawRegion, 104 RegionTypeDevExp1: NewRawRegion, 105 RegionTypeBIOS2: NewRawRegion, 106 RegionTypeMicrocode: NewRawRegion, 107 RegionTypeEC: NewRawRegion, 108 RegionTypeDevExp2: NewRawRegion, 109 RegionTypeIE: NewRawRegion, 110 RegionTypeTGBE1: NewRawRegion, 111 RegionTypeTGBE2: NewRawRegion, 112 RegionTypeReserved1: NewRawRegion, 113 RegionTypeReserved2: NewRawRegion, 114 RegionTypePTT: NewRawRegion, 115 RegionTypeUnknown: NewRawRegion, 116 } 117 118 // Region contains the start and end of a region in flash. This can be a BIOS, ME, PDR or GBE region. 119 type Region interface { 120 Firmware 121 Type() FlashRegionType 122 FlashRegion() *FlashRegion 123 SetFlashRegion(fr *FlashRegion) 124 }