github.com/linuxboot/fiano@v1.2.0/pkg/uefi/flashparams.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  	// FlashParamsSize is the size of a FlashParams struct
    13  	FlashParamsSize = 4
    14  )
    15  
    16  // FlashFrequency is the type used for Frequency fields
    17  type FlashFrequency uint
    18  
    19  // Flash frequency constants
    20  const (
    21  	Freq20MHz      FlashFrequency = 0
    22  	Freq33MHz      FlashFrequency = 1
    23  	Freq48MHz      FlashFrequency = 2
    24  	Freq50MHz30MHz FlashFrequency = 4
    25  	Freq17MHz      FlashFrequency = 6
    26  )
    27  
    28  // FlashFrequencyStringMap maps frequency constants to strings
    29  var FlashFrequencyStringMap = map[FlashFrequency]string{
    30  	Freq20MHz:      "20MHz",
    31  	Freq33MHz:      "33MHz",
    32  	Freq48MHz:      "48MHz",
    33  	Freq50MHz30MHz: "50Mhz30MHz",
    34  	Freq17MHz:      "17MHz",
    35  }
    36  
    37  // FlashParams is a 4-byte object that holds the flash parameters information.
    38  type FlashParams [4]byte
    39  
    40  // FirstChipDensity returns the size of the first chip.
    41  func (p *FlashParams) FirstChipDensity() uint {
    42  	return uint(p[0] & 0x0f)
    43  }
    44  
    45  // SecondChipDensity returns the size of the second chip.
    46  func (p *FlashParams) SecondChipDensity() uint {
    47  	return uint((p[0] >> 4) & 0x0f)
    48  }
    49  
    50  // ReadClockFrequency returns the chip frequency while reading from the flash.
    51  func (p *FlashParams) ReadClockFrequency() FlashFrequency {
    52  	return FlashFrequency((p[2] >> 1) & 0x07)
    53  }
    54  
    55  // FastReadEnabled returns if FastRead is enabled.
    56  func (p *FlashParams) FastReadEnabled() uint {
    57  	return uint((p[2] >> 4) & 0x01)
    58  }
    59  
    60  // FastReadFrequency returns the frequency under FastRead.
    61  func (p *FlashParams) FastReadFrequency() FlashFrequency {
    62  	return FlashFrequency((p[2] >> 5) & 0x07)
    63  }
    64  
    65  // FlashWriteFrequency returns the chip frequency for writing.
    66  func (p *FlashParams) FlashWriteFrequency() FlashFrequency {
    67  	return FlashFrequency(p[3] & 0x07)
    68  }
    69  
    70  // FlashReadStatusFrequency returns the chip frequency while reading the flash status.
    71  func (p *FlashParams) FlashReadStatusFrequency() FlashFrequency {
    72  	return FlashFrequency((p[3] >> 3) & 0x07)
    73  }
    74  
    75  // DualOutputFastReadSupported returns if Dual Output Fast Read is supported.
    76  func (p *FlashParams) DualOutputFastReadSupported() uint {
    77  	return uint(p[3] >> 7)
    78  }
    79  
    80  func (p *FlashParams) String() string {
    81  	return "FlashParams{...}"
    82  }
    83  
    84  // NewFlashParams initializes a FlashParam struct from a slice of bytes
    85  func NewFlashParams(buf []byte) (*FlashParams, error) {
    86  	if len(buf) != FlashParamsSize {
    87  		return nil, fmt.Errorf("invalid image size: expected %v bytes, got %v",
    88  			FlashParamsSize,
    89  			len(buf),
    90  		)
    91  	}
    92  	var p FlashParams
    93  	copy(p[:], buf[0:FlashParamsSize])
    94  	return &p, nil
    95  }