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::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 // Extended memory from 16 MB to 4 GB in units of 64 KB 30 let ext_mem = min( 31 0xFFFF, 32 mem_below_4g.saturating_sub(16 * 1024 * 1024) / (64 * 1024), 33 ); 34 data[0x34] = ext_mem as u8; 35 data[0x35] = (ext_mem >> 8) as u8; 36 37 // High memory (> 4GB) in units of 64 KB 38 let high_mem = min(0xFFFFFF, mem_above_4g / (64 * 1024)); 39 data[0x5b] = high_mem as u8; 40 data[0x5c] = (high_mem >> 8) as u8; 41 data[0x5d] = (high_mem >> 16) as u8; 42 43 Cmos { index: 0, data } 44 } 45 } 46 47 impl BusDevice for Cmos { debug_label(&self) -> String48 fn debug_label(&self) -> String { 49 "cmos".to_owned() 50 } 51 write(&mut self, offset: u64, data: &[u8])52 fn write(&mut self, offset: u64, data: &[u8]) { 53 if data.len() != 1 { 54 return; 55 } 56 57 match offset { 58 INDEX_OFFSET => self.index = data[0] & INDEX_MASK, 59 DATA_OFFSET => self.data[self.index as usize] = data[0], 60 o => panic!("bad write offset on CMOS device: {}", o), 61 } 62 } 63 read(&mut self, offset: u64, data: &mut [u8])64 fn read(&mut self, offset: u64, data: &mut [u8]) { 65 fn to_bcd(v: u8) -> u8 { 66 assert!(v < 100); 67 ((v / 10) << 4) | (v % 10) 68 } 69 70 if data.len() != 1 { 71 return; 72 } 73 74 data[0] = match offset { 75 INDEX_OFFSET => self.index, 76 DATA_OFFSET => { 77 let seconds; 78 let minutes; 79 let hours; 80 let week_day; 81 let day; 82 let month; 83 let year; 84 // The time and gmtime_r calls are safe as long as the structs they are given are 85 // large enough, and neither of them fail. It is safe to zero initialize the tm 86 // struct because it contains only plain data. 87 unsafe { 88 let mut tm: tm = mem::zeroed(); 89 let mut now: time_t = 0; 90 time(&mut now as *mut _); 91 gmtime_r(&now, &mut tm as *mut _); 92 // The following lines of code are safe but depend on tm being in scope. 93 seconds = tm.tm_sec; 94 minutes = tm.tm_min; 95 hours = tm.tm_hour; 96 week_day = tm.tm_wday + 1; 97 day = tm.tm_mday; 98 month = tm.tm_mon + 1; 99 year = tm.tm_year; 100 }; 101 match self.index { 102 0x00 => to_bcd(seconds as u8), 103 0x02 => to_bcd(minutes as u8), 104 0x04 => to_bcd(hours as u8), 105 0x06 => to_bcd(week_day as u8), 106 0x07 => to_bcd(day as u8), 107 0x08 => to_bcd(month as u8), 108 0x09 => to_bcd((year % 100) as u8), 109 0x32 => to_bcd(((year + 1900) / 100) as u8), 110 _ => { 111 // self.index is always guaranteed to be in range via INDEX_MASK. 112 self.data[(self.index & INDEX_MASK) as usize] 113 } 114 } 115 } 116 o => panic!("bad read offset on CMOS device: {}", o), 117 } 118 } 119 } 120