github.com/platinasystems/nvram@v1.0.1-0.20190709235807-51a23abd5aec/cmos_checksum.go (about)

     1  // Copyright © 2019 Platina Systems, Inc. All rights reserved.
     2  // Use of this source code is governed by the GPL-2 license described in the
     3  // LICENSE file.
     4  
     5  package nvram
     6  
     7  import (
     8  	"fmt"
     9  	"github.com/platinasystems/nvram/debug"
    10  )
    11  
    12  type CMOSChecksum struct {
    13  	start, end, index uint
    14  }
    15  
    16  func (c CMOSChecksum) String() string {
    17  	return fmt.Sprintf("%d %d %d", c.start*8, c.end*8, c.index*8)
    18  }
    19  
    20  func NewCMOSChecksum(start, end, index uint) (c *CMOSChecksum, err error) {
    21  
    22  	debug.Trace(debug.LevelMSG3, "New CMOS Checksum %d %d %d\n", start, end, index)
    23  
    24  	// Check that checksum area is aligned
    25  	if start%8 != 0 {
    26  		err = fmt.Errorf("Checksum area start not aligned")
    27  		return
    28  	}
    29  
    30  	if end%8 != 7 {
    31  		err = fmt.Errorf("Checksum area end not aligned")
    32  		return
    33  	}
    34  
    35  	// Check that checksum location is aligned
    36  	if index%8 != 0 {
    37  		err = fmt.Errorf("Checksum location not aligned")
    38  		return
    39  	}
    40  
    41  	// Check that checksum area is valid.
    42  	if end <= start {
    43  		err = fmt.Errorf("Checksum area invalid.")
    44  		return
    45  	}
    46  
    47  	// Convert to bytes
    48  	start /= 8
    49  	end /= 8
    50  	index /= 8
    51  
    52  	// Verify checksum area range
    53  	if !verifyCMOSByteIndex(start) || !verifyCMOSByteIndex(end) {
    54  		err = fmt.Errorf("Checksum area out of range.")
    55  		return
    56  	}
    57  
    58  	// Verify checksum location range
    59  	if !verifyCMOSByteIndex(index) {
    60  		err = fmt.Errorf("Checksum location out of range.")
    61  		return
    62  	}
    63  
    64  	// Make sure checksum area does not overlap location
    65  	if checkAreaOverLap(start, end-start+1, index, index+1) {
    66  		err = fmt.Errorf("Checksum overlaps summed area.")
    67  		return
    68  	}
    69  
    70  	debug.Trace(debug.LevelMSG3, "Valid checksum %d %d %d\n", start, end, index)
    71  
    72  	// Create new checksum with byte values
    73  	c = &CMOSChecksum{start: start, end: end, index: index}
    74  
    75  	return
    76  }