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