Serenity Operating System
at portability 169 lines 4.5 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/Assertions.h> 28#include <Kernel/CMOS.h> 29#include <Kernel/RTC.h> 30 31namespace RTC { 32 33static time_t s_boot_time; 34 35void initialize() 36{ 37 s_boot_time = now(); 38} 39 40time_t boot_time() 41{ 42 return s_boot_time; 43} 44 45static bool update_in_progress() 46{ 47 return CMOS::read(0x0a) & 0x80; 48} 49 50inline bool is_leap_year(unsigned year) 51{ 52 return ((year % 4 == 0) && ((year % 100 != 0) || (year % 400) == 0)); 53} 54 55static unsigned days_in_months_since_start_of_year(unsigned month, unsigned year) 56{ 57 ASSERT(month <= 11); 58 unsigned days = 0; 59 switch (month) { 60 case 11: 61 days += 30; 62 [[fallthrough]]; 63 case 10: 64 days += 31; 65 [[fallthrough]]; 66 case 9: 67 days += 30; 68 [[fallthrough]]; 69 case 8: 70 days += 31; 71 [[fallthrough]]; 72 case 7: 73 days += 31; 74 [[fallthrough]]; 75 case 6: 76 days += 30; 77 [[fallthrough]]; 78 case 5: 79 days += 31; 80 [[fallthrough]]; 81 case 4: 82 days += 30; 83 [[fallthrough]]; 84 case 3: 85 days += 31; 86 [[fallthrough]]; 87 case 2: 88 if (is_leap_year(year)) 89 days += 29; 90 else 91 days += 28; 92 [[fallthrough]]; 93 case 1: 94 days += 31; 95 } 96 return days; 97} 98 99static unsigned days_in_years_since_epoch(unsigned year) 100{ 101 unsigned days = 0; 102 while (year > 1969) { 103 days += 365; 104 if (is_leap_year(year)) 105 ++days; 106 --year; 107 } 108 return days; 109} 110 111u8 bcd_to_binary(u8 bcd) 112{ 113 return (bcd & 0x0F) + ((bcd >> 4) * 10); 114} 115 116void read_registers(unsigned& year, unsigned& month, unsigned& day, unsigned& hour, unsigned& minute, unsigned& second) 117{ 118 while (update_in_progress()) 119 ; 120 121 u8 status_b = CMOS::read(0x0b); 122 123 second = CMOS::read(0x00); 124 minute = CMOS::read(0x02); 125 hour = CMOS::read(0x04); 126 day = CMOS::read(0x07); 127 month = CMOS::read(0x08); 128 year = CMOS::read(0x09); 129 130 if (!(status_b & 0x04)) { 131 second = bcd_to_binary(second); 132 minute = bcd_to_binary(minute); 133 hour = bcd_to_binary(hour & 0x70); 134 day = bcd_to_binary(day); 135 month = bcd_to_binary(month); 136 year = bcd_to_binary(year); 137 } 138 139 if (!(status_b & 0x02) && (hour & 0x80)) { 140 hour = ((hour & 0x7F) + 12) % 24; 141 } 142 143 year += 2000; 144} 145 146time_t now() 147{ 148 // FIXME: We should probably do something more robust here. 149 // Perhaps read all the values twice and verify that they were identical. 150 // We don't want to be caught in the middle of an RTC register update. 151 while (update_in_progress()) 152 ; 153 154 unsigned year, month, day, hour, minute, second; 155 read_registers(year, month, day, hour, minute, second); 156 157 kprintf("year: %d, month: %d, day: %d\n", year, month, day); 158 159 ASSERT(year >= 2018); 160 161 return days_in_years_since_epoch(year - 1) * 86400 162 + days_in_months_since_start_of_year(month - 1, year) * 86400 163 + (day - 1) * 86400 164 + hour * 3600 165 + minute * 60 166 + second; 167} 168 169}