github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/cmos/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 cmos 9 10 import ( 11 "github.com/mvdan/u-root-coreutils/pkg/memio" 12 ) 13 14 const ( 15 regPort = 0x70 16 dataPort = 0x71 17 ) 18 19 type Chip struct { 20 memio.PortReadWriter 21 } 22 23 // Read reads a register reg from CMOS into data. 24 func (c *Chip) Read(reg memio.Uint8, data memio.UintN) error { 25 if err := c.PortReadWriter.Out(regPort, ®); err != nil { 26 return err 27 } 28 return c.PortReadWriter.In(dataPort, data) 29 } 30 31 // Write writes value data into CMOS register reg. 32 func (c *Chip) Write(reg memio.Uint8, data memio.UintN) error { 33 if err := c.PortReadWriter.Out(regPort, ®); err != nil { 34 return err 35 } 36 return c.PortReadWriter.Out(dataPort, data) 37 } 38 39 // GetCMOS() returns the struct to call Read and Write functions for CMOS 40 // associated with the correct functions of memio.In and memio.Out 41 func New() (*Chip, error) { 42 pr, err := memio.NewPort() 43 if err != nil { 44 return nil, err 45 } 46 return &Chip{ 47 PortReadWriter: pr, 48 }, nil 49 }