func (rr *MERegion) SetFlashRegion(fr *FlashRegion) {
	rr.FRegion = fr
}
SetFlashRegion sets the flash region.
package uefi
import (
	"bytes"
	"encoding/binary"
	"encoding/hex"
	"fmt"
	"github.com/linuxboot/fiano/pkg/log"
)
var (
	MEFTPSignature = []byte{0x24, 0x46, 0x50, 0x54}
)
const (
	MEPartitionDescriptorMinLength = 28
	MEPartitionTableEntryLength = 32
)
type MEFPT struct {
	buf []byte
	PartitionCount    uint32
	PartitionMapStart int
	Entries           []MEPartitionEntry
	ExtractPath string
}
type MEPartitionEntry struct {
	Name     MEName
	Owner    [4]byte
	Offset   uint32
	Length   uint32
	Reserved [3]uint32
	Flags    uint32
}
type MEName [4]byte
func (n MEName) MarshalText() ([]byte, error) {
	return bytes.TrimRight(n[:], "\x00"), nil
}
func (n *MEName) UnmarshalText(b []byte) error {
	var m MEName
	copy(m[:], b)
	*n = m
	if len(b) > len(m) {
		return fmt.Errorf("can’t unmarshal %q to MEName, %d > %d", b, len(b), len(m))
	}
	return nil
}
func (n MEName) String() string {
	b, _ := n.MarshalText()
	return string(b)
}
func (e MEPartitionEntry) OffsetIsValid() bool {
	return e.Offset != 0 && e.Offset != 0xffffffff
}
var mePartitionEntryTypeNames = map[byte]string{0: "Code", 1: "Data", 2: "NVRAM", 3: "Generic", 4: "EFFS", 5: "ROM"}
func (e MEPartitionEntry) Type() string {
	t := byte(e.Flags & 0x7f)
	if s, ok := mePartitionEntryTypeNames[t]; ok {
		return s
	}
	return fmt.Sprintf("Unknown (%d)", t)
}
func FindMEDescriptor(buf []byte) (int, error) {
	if bytes.Equal(buf[16:16+len(MEFTPSignature)], MEFTPSignature) {
		return 16 + len(MEFTPSignature), nil
	}
	if bytes.Equal(buf[:len(MEFTPSignature)], MEFTPSignature) {
		return len(MEFTPSignature), nil
	}
	return -1, fmt.Errorf("ME Flash Partition Table signature %#02x not found: first 20 bytes are:\n%s", MEFTPSignature, hex.Dump(buf[:20]))
}
func (fp *MEFPT) Buf() []byte {
	return fp.buf
}
func (fp *MEFPT) SetBuf(buf []byte) {
	fp.buf = buf
}
func (fp *MEFPT) Apply(v Visitor) error {
	return v.Visit(fp)
}
func (fp *MEFPT) ApplyChildren(v Visitor) error {
	return nil
}
func NewMEFPT(buf []byte) (*MEFPT, error) {
	o, err := FindMEDescriptor(buf)
	if err != nil {
		return nil, err
	}
	if len(buf) < o+MEPartitionDescriptorMinLength {
		return nil, fmt.Errorf("ME section (%#x) too small for ME Flash Partition Table (%#x)", len(buf), o+MEPartitionDescriptorMinLength)
	}
	fp := &MEFPT{PartitionMapStart: o + MEPartitionDescriptorMinLength}
	r := bytes.NewReader(buf[o:])
	if err := binary.Read(r, binary.LittleEndian, &fp.PartitionCount); err != nil {
		return nil, err
	}
	l := fp.PartitionMapStart + MEPartitionTableEntryLength*int(fp.PartitionCount)
	if len(buf) < l {
		return nil, fmt.Errorf("ME section (%#x) too small for %d entries in ME Flash Partition Table (%#x)", len(buf), fp.PartitionCount, l)
	}
	fp.buf = make([]byte, l)
	copy(fp.buf, buf[:l])
	if err := fp.parsePartitions(); err != nil {
		return nil, err
	}
	return fp, nil
}
func (fp *MEFPT) parsePartitions() error {
	fp.Entries = make([]MEPartitionEntry, fp.PartitionCount)
	r := bytes.NewReader(fp.buf[fp.PartitionMapStart:])
	return binary.Read(r, binary.LittleEndian, fp.Entries)
}
type MERegion struct {
	FPT *MEFPT
	buf []byte
	ExtractPath string
	FRegion *FlashRegion
	RegionType FlashRegionType
	FreeSpaceOffset uint64
}