github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/cmds/core/io/cmos.go (about)

     1  // Copyright 2012-2020 the u-root 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  //go:build amd64 || 386
     6  // +build amd64 386
     7  
     8  package main
     9  
    10  import (
    11  	"fmt"
    12  
    13  	"github.com/mvdan/u-root-coreutils/pkg/cmos"
    14  	"github.com/mvdan/u-root-coreutils/pkg/memio"
    15  )
    16  
    17  func init() {
    18  	usageMsg += `io (cr index)... # read from CMOS register index [14-127]
    19  io (cw index value)... # write value to CMOS register index [14-127]
    20  io (rtcr index)... # read from RTC register index [0-13]
    21  io (rtcw index value)... # write value to RTC register index [0-13]
    22  `
    23  	addCmd(readCmds, "cr", &cmd{cmosRead, 7, 8})
    24  	addCmd(readCmds, "rtcr", &cmd{rtcRead, 7, 8})
    25  	addCmd(writeCmds, "cw", &cmd{cmosWrite, 7, 8})
    26  	addCmd(writeCmds, "rtcw", &cmd{rtcWrite, 7, 8})
    27  }
    28  
    29  func cmosRead(reg int64, data memio.UintN) error {
    30  	c, err := cmos.New()
    31  	if err != nil {
    32  		return err
    33  	}
    34  	defer c.Close()
    35  	regVal := memio.Uint8(reg)
    36  	if regVal < 14 {
    37  		return fmt.Errorf("byte %d is inside the range 0-13 which is reserved for RTC", regVal)
    38  	}
    39  	return c.Read(regVal, data)
    40  }
    41  
    42  func cmosWrite(reg int64, data memio.UintN) error {
    43  	c, err := cmos.New()
    44  	if err != nil {
    45  		return err
    46  	}
    47  	defer c.Close()
    48  	regVal := memio.Uint8(reg)
    49  	if regVal < 14 {
    50  		return fmt.Errorf("byte %d is inside the range 0-13 which is reserved for RTC", regVal)
    51  	}
    52  	return c.Write(regVal, data)
    53  }
    54  
    55  func rtcRead(reg int64, data memio.UintN) error {
    56  	c, err := cmos.New()
    57  	if err != nil {
    58  		return err
    59  	}
    60  	defer c.Close()
    61  	regVal := memio.Uint8(reg)
    62  	if regVal > 13 {
    63  		return fmt.Errorf("byte %d is outside the range 0-13 reserved for RTC", regVal)
    64  	}
    65  	return c.Read(regVal, data)
    66  }
    67  
    68  func rtcWrite(reg int64, data memio.UintN) error {
    69  	c, err := cmos.New()
    70  	if err != nil {
    71  		return err
    72  	}
    73  	defer c.Close()
    74  	regVal := memio.Uint8(reg)
    75  	if regVal > 13 {
    76  		return fmt.Errorf("byte %d is outside the range 0-13 reserved for RTC", regVal)
    77  	}
    78  	return c.Write(regVal, data)
    79  }