1 // Copyright 2017 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 use libc::{gmtime_r, time, time_t, tm};
6 use std::cmp::min;
7 use std::mem;
8 
9 use crate::{BusAccessInfo, BusDevice};
10 
11 const INDEX_MASK: u8 = 0x7f;
12 const INDEX_OFFSET: u64 = 0x0;
13 const DATA_OFFSET: u64 = 0x1;
14 const DATA_LEN: usize = 128;
15 
16 /// A CMOS/RTC device commonly seen on x86 I/O port 0x70/0x71.
17 pub struct Cmos {
18     index: u8,
19     data: [u8; DATA_LEN],
20 }
21 
22 impl Cmos {
23     /// Constructs a CMOS/RTC device with initial data.
24     /// `mem_below_4g` is the size of memory in bytes below the 32-bit gap.
25     /// `mem_above_4g` is the size of memory in bytes above the 32-bit gap.
new(mem_below_4g: u64, mem_above_4g: u64) -> Cmos26     pub fn new(mem_below_4g: u64, mem_above_4g: u64) -> Cmos {
27         let mut data = [0u8; DATA_LEN];
28 
29         data[0x0B] = 0x02; // Status Register B: 24-hour mode
30 
31         // Extended memory from 16 MB to 4 GB in units of 64 KB
32         let ext_mem = min(
33             0xFFFF,
34             mem_below_4g.saturating_sub(16 * 1024 * 1024) / (64 * 1024),
35         );
36         data[0x34] = ext_mem as u8;
37         data[0x35] = (ext_mem >> 8) as u8;
38 
39         // High memory (> 4GB) in units of 64 KB
40         let high_mem = min(0xFFFFFF, mem_above_4g / (64 * 1024));
41         data[0x5b] = high_mem as u8;
42         data[0x5c] = (high_mem >> 8) as u8;
43         data[0x5d] = (high_mem >> 16) as u8;
44 
45         Cmos { index: 0, data }
46     }
47 }
48 
49 impl BusDevice for Cmos {
debug_label(&self) -> String50     fn debug_label(&self) -> String {
51         "cmos".to_owned()
52     }
53 
write(&mut self, info: BusAccessInfo, data: &[u8])54     fn write(&mut self, info: BusAccessInfo, data: &[u8]) {
55         if data.len() != 1 {
56             return;
57         }
58 
59         match info.offset {
60             INDEX_OFFSET => self.index = data[0] & INDEX_MASK,
61             DATA_OFFSET => self.data[self.index as usize] = data[0],
62             o => panic!("bad write offset on CMOS device: {}", o),
63         }
64     }
65 
read(&mut self, info: BusAccessInfo, data: &mut [u8])66     fn read(&mut self, info: BusAccessInfo, data: &mut [u8]) {
67         fn to_bcd(v: u8) -> u8 {
68             assert!(v < 100);
69             ((v / 10) << 4) | (v % 10)
70         }
71 
72         if data.len() != 1 {
73             return;
74         }
75 
76         data[0] = match info.offset {
77             INDEX_OFFSET => self.index,
78             DATA_OFFSET => {
79                 let seconds;
80                 let minutes;
81                 let hours;
82                 let week_day;
83                 let day;
84                 let month;
85                 let year;
86                 // The time and gmtime_r calls are safe as long as the structs they are given are
87                 // large enough, and neither of them fail. It is safe to zero initialize the tm
88                 // struct because it contains only plain data.
89                 unsafe {
90                     let mut tm: tm = mem::zeroed();
91                     let mut now: time_t = 0;
92                     time(&mut now as *mut _);
93                     gmtime_r(&now, &mut tm as *mut _);
94                     // The following lines of code are safe but depend on tm being in scope.
95                     seconds = tm.tm_sec;
96                     minutes = tm.tm_min;
97                     hours = tm.tm_hour;
98                     week_day = tm.tm_wday + 1;
99                     day = tm.tm_mday;
100                     month = tm.tm_mon + 1;
101                     year = tm.tm_year;
102                 };
103                 match self.index {
104                     0x00 => to_bcd(seconds as u8),
105                     0x02 => to_bcd(minutes as u8),
106                     0x04 => to_bcd(hours as u8),
107                     0x06 => to_bcd(week_day as u8),
108                     0x07 => to_bcd(day as u8),
109                     0x08 => to_bcd(month as u8),
110                     0x09 => to_bcd((year % 100) as u8),
111                     0x32 => to_bcd(((year + 1900) / 100) as u8),
112                     _ => {
113                         // self.index is always guaranteed to be in range via INDEX_MASK.
114                         self.data[(self.index & INDEX_MASK) as usize]
115                     }
116                 }
117             }
118             o => panic!("bad read offset on CMOS device: {}", o),
119         }
120     }
121 }
122