github.com/linuxboot/fiano@v1.2.0/pkg/uefi/flashregionsection.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 "bytes" 9 "encoding/binary" 10 "fmt" 11 "strings" 12 ) 13 14 // FlashRegionSectionSize is the size of the Region descriptor. It is made up by 16 fields, each 2x16-bits large. 15 const FlashRegionSectionSize = 64 16 17 // FlashRegionSection holds the metadata of all the different flash regions like PDR, Gbe and the Bios region. 18 type FlashRegionSection struct { 19 _ uint16 20 FlashBlockEraseSize uint16 21 22 // This isn't documented anywhere, but I've only seen images with 16 slots for FlashRegion entries, with the 23 // FlashMasterSection coming immediately after, so I'm assuming that's the max for now. 24 FlashRegions [15]FlashRegion 25 } 26 27 // ValidRegions returns a list of names of the regions with non-zero size. 28 func (f *FlashRegionSection) ValidRegions() []string { 29 var regions []string 30 for i, r := range f.FlashRegions { 31 if r.Valid() { 32 regions = append(regions, flashRegionTypeNames[FlashRegionType(i)]) 33 } 34 } 35 return regions 36 } 37 38 func (f *FlashRegionSection) String() string { 39 return fmt.Sprintf("FlashRegionSection{Regions=%v}", 40 strings.Join(f.ValidRegions(), ","), 41 ) 42 } 43 44 // NewFlashRegionSection initializes a FlashRegionSection from a slice of bytes 45 func NewFlashRegionSection(data []byte) (*FlashRegionSection, error) { 46 if len(data) < FlashRegionSectionSize { 47 return nil, fmt.Errorf("flash Region Section size too small: expected %v bytes, got %v", 48 FlashRegionSectionSize, 49 len(data), 50 ) 51 } 52 var region FlashRegionSection 53 reader := bytes.NewReader(data) 54 if err := binary.Read(reader, binary.LittleEndian, ®ion); err != nil { 55 return nil, err 56 } 57 return ®ion, nil 58 }