Marc Jones | 3cc685f | 2014-12-29 21:31:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2014 The Chromium OS Authors. All rights reserved. |
Timothy Pearson | 7b22d84 | 2015-08-28 19:52:05 -0500 | [diff] [blame] | 5 | * Copyright (C) 2015 Timothy Pearson <tpearson@raptorengineeringinc.com>, Raptor Engineering |
Marc Jones | 3cc685f | 2014-12-29 21:31:44 -0700 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; version 2 of the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
Marc Jones | 3cc685f | 2014-12-29 21:31:44 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <arch/acpi.h> |
Kyösti Mälkki | 7ce1a75 | 2016-12-11 12:56:37 +0200 | [diff] [blame] | 18 | #include <arch/io.h> |
Marc Jones | 3cc685f | 2014-12-29 21:31:44 -0700 | [diff] [blame] | 19 | #include <bcd.h> |
Kyösti Mälkki | 7ce1a75 | 2016-12-11 12:56:37 +0200 | [diff] [blame] | 20 | #include <fallback.h> |
Stefan Reinauer | ea5c2b6 | 2011-10-27 18:42:53 +0200 | [diff] [blame] | 21 | #include <stdint.h> |
Kyösti Mälkki | c36af7b | 2014-11-18 12:41:16 +0200 | [diff] [blame] | 22 | #include <version.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 23 | #include <console/console.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 24 | #include <pc80/mc146818rtc.h> |
Stefan Reinauer | ca374d4 | 2008-01-18 16:16:45 +0000 | [diff] [blame] | 25 | #include <boot/coreboot_tables.h> |
Marc Jones | 3cc685f | 2014-12-29 21:31:44 -0700 | [diff] [blame] | 26 | #include <rtc.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 27 | #include <string.h> |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 28 | #include <cbfs.h> |
Philipp Deppenwiese | fea2429 | 2017-10-17 17:02:29 +0200 | [diff] [blame] | 29 | #include <security/vboot/vbnv.h> |
| 30 | #include <security/vboot/vbnv_layout.h> |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 31 | |
| 32 | /* There's no way around this include guard. option_table.h is autogenerated */ |
Martin Roth | 32c27c2 | 2017-06-24 14:07:25 -0600 | [diff] [blame] | 33 | #if IS_ENABLED(CONFIG_USE_OPTION_TABLE) |
Stefan Reinauer | 10ec0fe | 2010-09-25 10:40:47 +0000 | [diff] [blame] | 34 | #include "option_table.h" |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 35 | #else |
| 36 | #define LB_CKS_RANGE_START 0 |
| 37 | #define LB_CKS_RANGE_END 0 |
| 38 | #define LB_CKS_LOC 0 |
Stefan Reinauer | 10ec0fe | 2010-09-25 10:40:47 +0000 | [diff] [blame] | 39 | #endif |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 40 | |
Timothy Pearson | 7b22d84 | 2015-08-28 19:52:05 -0500 | [diff] [blame] | 41 | #include <smp/spinlock.h> |
| 42 | |
| 43 | #if (defined(__PRE_RAM__) && \ |
| 44 | IS_ENABLED(CONFIG_HAVE_ROMSTAGE_NVRAM_CBFS_SPINLOCK)) |
Stefan Reinauer | 86ddd73 | 2016-03-11 20:22:28 -0800 | [diff] [blame] | 45 | #define LOCK_NVRAM_CBFS_SPINLOCK() spin_lock(romstage_nvram_cbfs_lock()) |
| 46 | #define UNLOCK_NVRAM_CBFS_SPINLOCK() spin_unlock(romstage_nvram_cbfs_lock()) |
Timothy Pearson | 7b22d84 | 2015-08-28 19:52:05 -0500 | [diff] [blame] | 47 | #else |
Stefan Reinauer | 86ddd73 | 2016-03-11 20:22:28 -0800 | [diff] [blame] | 48 | #define LOCK_NVRAM_CBFS_SPINLOCK() { } |
| 49 | #define UNLOCK_NVRAM_CBFS_SPINLOCK() { } |
Timothy Pearson | 7b22d84 | 2015-08-28 19:52:05 -0500 | [diff] [blame] | 50 | #endif |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 51 | |
Gabe Black | 03abaee21 | 2014-04-30 21:31:44 -0700 | [diff] [blame] | 52 | static void cmos_reset_date(void) |
zbao | a1e6a9c | 2012-08-02 19:02:26 +0800 | [diff] [blame] | 53 | { |
Vincent Palatin | fa90fd4 | 2012-08-07 17:03:40 -0700 | [diff] [blame] | 54 | /* Now setup a default date equals to the build date */ |
Marc Jones | 3cc685f | 2014-12-29 21:31:44 -0700 | [diff] [blame] | 55 | struct rtc_time time = { |
| 56 | .sec = 0, |
| 57 | .min = 0, |
| 58 | .hour = 1, |
| 59 | .mday = bcd2bin(coreboot_build_date.day), |
| 60 | .mon = bcd2bin(coreboot_build_date.month), |
| 61 | .year = (bcd2bin(coreboot_build_date.century) * 100) + |
| 62 | bcd2bin(coreboot_build_date.year), |
| 63 | .wday = bcd2bin(coreboot_build_date.weekday) |
| 64 | }; |
Gabe Black | 03abaee21 | 2014-04-30 21:31:44 -0700 | [diff] [blame] | 65 | rtc_set(&time); |
zbao | a1e6a9c | 2012-08-02 19:02:26 +0800 | [diff] [blame] | 66 | } |
| 67 | |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 68 | static int cmos_checksum_valid(int range_start, int range_end, int cks_loc) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 69 | { |
Stefan Reinauer | 86ddd73 | 2016-03-11 20:22:28 -0800 | [diff] [blame] | 70 | int i; |
| 71 | u16 sum, old_sum; |
| 72 | |
Timothy Pearson | f20c6e8 | 2015-02-14 16:15:31 -0600 | [diff] [blame] | 73 | if (IS_ENABLED(CONFIG_STATIC_OPTION_TABLE)) |
| 74 | return 1; |
| 75 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 76 | sum = 0; |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 77 | for (i = range_start; i <= range_end; i++) |
Stefan Reinauer | 73d5daa | 2009-04-22 09:03:08 +0000 | [diff] [blame] | 78 | sum += cmos_read(i); |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 79 | old_sum = ((cmos_read(cks_loc) << 8) | cmos_read(cks_loc + 1)) & |
| 80 | 0x0ffff; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 81 | return sum == old_sum; |
| 82 | } |
| 83 | |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 84 | static void cmos_set_checksum(int range_start, int range_end, int cks_loc) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 85 | { |
| 86 | int i; |
Stefan Reinauer | ea5c2b6 | 2011-10-27 18:42:53 +0200 | [diff] [blame] | 87 | u16 sum; |
Stefan Reinauer | 86ddd73 | 2016-03-11 20:22:28 -0800 | [diff] [blame] | 88 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 89 | sum = 0; |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 90 | for (i = range_start; i <= range_end; i++) |
Stefan Reinauer | 73d5daa | 2009-04-22 09:03:08 +0000 | [diff] [blame] | 91 | sum += cmos_read(i); |
Stefan Reinauer | 73d5daa | 2009-04-22 09:03:08 +0000 | [diff] [blame] | 92 | cmos_write(((sum >> 8) & 0x0ff), cks_loc); |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 93 | cmos_write(((sum >> 0) & 0x0ff), cks_loc + 1); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | #define RTC_CONTROL_DEFAULT (RTC_24H) |
| 97 | #define RTC_FREQ_SELECT_DEFAULT (RTC_REF_CLCK_32KHZ | RTC_RATE_1024HZ) |
Li-Ta Lo | 84a8028 | 2005-01-07 02:42:53 +0000 | [diff] [blame] | 98 | |
Duncan Laurie | 4b1610d | 2012-09-05 10:52:44 -0700 | [diff] [blame] | 99 | #ifndef __SMM__ |
Aaron Durbin | 9fde0d7 | 2017-09-15 11:01:17 -0600 | [diff] [blame] | 100 | static bool __cmos_init(bool invalid) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 101 | { |
Marshall Dawson | f8a274a | 2016-11-05 18:47:51 -0600 | [diff] [blame] | 102 | bool cmos_invalid; |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 103 | bool checksum_invalid = false; |
| 104 | bool clear_cmos; |
| 105 | size_t i; |
| 106 | uint8_t x; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 107 | |
Martin Roth | 7312c54 | 2014-05-12 21:52:54 -0600 | [diff] [blame] | 108 | #ifndef __PRE_RAM__ |
Stefan Reinauer | 86ce7f9 | 2013-05-28 12:37:08 -0700 | [diff] [blame] | 109 | /* |
| 110 | * Avoid clearing pending interrupts and resetting the RTC control |
| 111 | * register in the resume path because the Linux kernel relies on |
| 112 | * this to know if it should restart the RTC timer queue if the wake |
| 113 | * was due to the RTC alarm. |
| 114 | */ |
Kyösti Mälkki | 9d9eb1e | 2014-06-20 05:38:07 +0300 | [diff] [blame] | 115 | if (acpi_is_wakeup_s3()) |
Aaron Durbin | 9fde0d7 | 2017-09-15 11:01:17 -0600 | [diff] [blame] | 116 | return false; |
Martin Roth | 7312c54 | 2014-05-12 21:52:54 -0600 | [diff] [blame] | 117 | #endif /* __PRE_RAM__ */ |
Stefan Reinauer | 86ce7f9 | 2013-05-28 12:37:08 -0700 | [diff] [blame] | 118 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 119 | printk(BIOS_DEBUG, "RTC Init\n"); |
Steven J. Magnani | 8cbc475 | 2005-09-12 18:43:27 +0000 | [diff] [blame] | 120 | |
Marshall Dawson | f8a274a | 2016-11-05 18:47:51 -0600 | [diff] [blame] | 121 | /* See if there has been a CMOS power problem. */ |
| 122 | x = cmos_read(RTC_VALID); |
| 123 | cmos_invalid = !(x & RTC_VRT); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 124 | |
Marshall Dawson | f8a274a | 2016-11-05 18:47:51 -0600 | [diff] [blame] | 125 | if (IS_ENABLED(CONFIG_USE_OPTION_TABLE)) { |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 126 | /* See if there is a CMOS checksum error */ |
| 127 | checksum_invalid = !cmos_checksum_valid(PC_CKS_RANGE_START, |
| 128 | PC_CKS_RANGE_END, PC_CKS_LOC); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 129 | |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 130 | clear_cmos = false; |
| 131 | } else { |
| 132 | clear_cmos = true; |
| 133 | } |
Vincent Palatin | fa90fd4 | 2012-08-07 17:03:40 -0700 | [diff] [blame] | 134 | |
Marshall Dawson | f8a274a | 2016-11-05 18:47:51 -0600 | [diff] [blame] | 135 | if (cmos_invalid || invalid) |
| 136 | cmos_write(cmos_read(RTC_CONTROL) | RTC_SET, RTC_CONTROL); |
| 137 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 138 | if (invalid || cmos_invalid || checksum_invalid) { |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 139 | if (clear_cmos) { |
| 140 | cmos_write(0, 0x01); |
| 141 | cmos_write(0, 0x03); |
| 142 | cmos_write(0, 0x05); |
| 143 | for (i = 10; i < 128; i++) |
| 144 | cmos_write(0, i); |
| 145 | } |
Vincent Palatin | fa90fd4 | 2012-08-07 17:03:40 -0700 | [diff] [blame] | 146 | |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 147 | if (cmos_invalid) |
Gabe Black | 03abaee21 | 2014-04-30 21:31:44 -0700 | [diff] [blame] | 148 | cmos_reset_date(); |
Stefan Reinauer | feadfb7 | 2012-11-06 18:39:41 -0800 | [diff] [blame] | 149 | |
Vincent Palatin | fa90fd4 | 2012-08-07 17:03:40 -0700 | [diff] [blame] | 150 | printk(BIOS_WARNING, "RTC:%s%s%s%s\n", |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 151 | invalid ? " Clear requested":"", |
| 152 | cmos_invalid ? " Power Problem":"", |
| 153 | checksum_invalid ? " Checksum invalid":"", |
| 154 | clear_cmos ? " zeroing cmos":""); |
Aaron Durbin | 9fde0d7 | 2017-09-15 11:01:17 -0600 | [diff] [blame] | 155 | } else |
| 156 | clear_cmos = false; |
Steven J. Magnani | 8cbc475 | 2005-09-12 18:43:27 +0000 | [diff] [blame] | 157 | |
| 158 | /* Setup the real time clock */ |
Stefan Reinauer | 73d5daa | 2009-04-22 09:03:08 +0000 | [diff] [blame] | 159 | cmos_write(RTC_CONTROL_DEFAULT, RTC_CONTROL); |
Steven J. Magnani | 8cbc475 | 2005-09-12 18:43:27 +0000 | [diff] [blame] | 160 | /* Setup the frequency it operates at */ |
Stefan Reinauer | 73d5daa | 2009-04-22 09:03:08 +0000 | [diff] [blame] | 161 | cmos_write(RTC_FREQ_SELECT_DEFAULT, RTC_FREQ_SELECT); |
Vincent Palatin | fc1b9ee | 2012-08-07 16:05:14 -0700 | [diff] [blame] | 162 | /* Ensure all reserved bits are 0 in register D */ |
| 163 | cmos_write(RTC_VRT, RTC_VALID); |
Steven J. Magnani | 8cbc475 | 2005-09-12 18:43:27 +0000 | [diff] [blame] | 164 | |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 165 | if (IS_ENABLED(CONFIG_USE_OPTION_TABLE)) { |
| 166 | /* See if there is a LB CMOS checksum error */ |
| 167 | checksum_invalid = !cmos_checksum_valid(LB_CKS_RANGE_START, |
Stefan Reinauer | 86ddd73 | 2016-03-11 20:22:28 -0800 | [diff] [blame] | 168 | LB_CKS_RANGE_END, LB_CKS_LOC); |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 169 | if (checksum_invalid) |
| 170 | printk(BIOS_DEBUG, "RTC: coreboot checksum invalid\n"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 171 | |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 172 | /* Make certain we have a valid checksum */ |
| 173 | cmos_set_checksum(PC_CKS_RANGE_START, PC_CKS_RANGE_END, PC_CKS_LOC); |
| 174 | } |
Steven J. Magnani | 8cbc475 | 2005-09-12 18:43:27 +0000 | [diff] [blame] | 175 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 176 | /* Clear any pending interrupts */ |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 177 | cmos_read(RTC_INTR_FLAGS); |
Aaron Durbin | 9fde0d7 | 2017-09-15 11:01:17 -0600 | [diff] [blame] | 178 | |
| 179 | return clear_cmos; |
| 180 | } |
| 181 | |
| 182 | static void cmos_init_vbnv(bool invalid) |
| 183 | { |
| 184 | uint8_t vbnv[VBOOT_VBNV_BLOCK_SIZE]; |
| 185 | |
| 186 | /* __cmos_init() will clear vbnv contents when a known rtc failure |
| 187 | occurred with !CONFIG_USE_OPTION_TABLE. However, __cmos_init() may |
| 188 | clear vbnv data for other internal reasons. For that, always back up |
| 189 | the vbnv contents and conditionally save them when __cmos_init() |
| 190 | indicates cmos was cleared. */ |
| 191 | read_vbnv_cmos(vbnv); |
| 192 | |
| 193 | if (__cmos_init(invalid)) |
| 194 | save_vbnv_cmos(vbnv); |
| 195 | } |
| 196 | |
| 197 | void cmos_init(bool invalid) |
| 198 | { |
| 199 | if (IS_ENABLED(CONFIG_VBOOT_VBNV_CMOS)) |
| 200 | cmos_init_vbnv(invalid); |
| 201 | else |
| 202 | __cmos_init(invalid); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 203 | } |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 204 | #endif /* __SMM__ */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 205 | |
| 206 | |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 207 | /* |
| 208 | * This routine returns the value of the requested bits. |
| 209 | * input bit = bit count from the beginning of the cmos image |
| 210 | * length = number of bits to include in the value |
| 211 | * ret = a character pointer to where the value is to be returned |
| 212 | * returns CB_SUCCESS = successful, cb_err code if an error occurred |
| 213 | */ |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 214 | static enum cb_err get_cmos_value(unsigned long bit, unsigned long length, |
| 215 | void *vret) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 216 | { |
Luc Verhaegen | a9c5ea0 | 2009-06-03 14:19:33 +0000 | [diff] [blame] | 217 | unsigned char *ret; |
Stefan Reinauer | 86ddd73 | 2016-03-11 20:22:28 -0800 | [diff] [blame] | 218 | unsigned long byte, byte_bit; |
Luc Verhaegen | a9c5ea0 | 2009-06-03 14:19:33 +0000 | [diff] [blame] | 219 | unsigned long i; |
| 220 | unsigned char uchar; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 221 | |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 222 | /* |
| 223 | * The table is checked when it is built to ensure all |
| 224 | * values are valid. |
| 225 | */ |
Luc Verhaegen | a9c5ea0 | 2009-06-03 14:19:33 +0000 | [diff] [blame] | 226 | ret = vret; |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 227 | byte = bit / 8; /* find the byte where the data starts */ |
| 228 | byte_bit = bit % 8; /* find the bit in the byte where the data starts */ |
| 229 | if (length < 9) { /* one byte or less */ |
Luc Verhaegen | a9c5ea0 | 2009-06-03 14:19:33 +0000 | [diff] [blame] | 230 | uchar = cmos_read(byte); /* load the byte */ |
| 231 | uchar >>= byte_bit; /* shift the bits to byte align */ |
| 232 | /* clear unspecified bits */ |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 233 | ret[0] = uchar & ((1 << length) - 1); |
Stefan Reinauer | 24850cc | 2016-05-04 16:16:47 -0700 | [diff] [blame] | 234 | } else { /* more than one byte so transfer the whole bytes */ |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 235 | for (i = 0; length; i++, length -= 8, byte++) { |
Luc Verhaegen | a9c5ea0 | 2009-06-03 14:19:33 +0000 | [diff] [blame] | 236 | /* load the byte */ |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 237 | ret[i] = cmos_read(byte); |
Luc Verhaegen | a9c5ea0 | 2009-06-03 14:19:33 +0000 | [diff] [blame] | 238 | } |
| 239 | } |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 240 | return CB_SUCCESS; |
Luc Verhaegen | 9ceae90 | 2009-06-03 10:47:19 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 243 | enum cb_err get_option(void *dest, const char *name) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 244 | { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 245 | struct cmos_option_table *ct; |
| 246 | struct cmos_entries *ce; |
| 247 | size_t namelen; |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 248 | int found = 0; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 249 | |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 250 | if (!IS_ENABLED(CONFIG_USE_OPTION_TABLE)) |
| 251 | return CB_CMOS_OTABLE_DISABLED; |
| 252 | |
Stefan Reinauer | 86ddd73 | 2016-03-11 20:22:28 -0800 | [diff] [blame] | 253 | LOCK_NVRAM_CBFS_SPINLOCK(); |
Timothy Pearson | 7b22d84 | 2015-08-28 19:52:05 -0500 | [diff] [blame] | 254 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 255 | /* Figure out how long name is */ |
| 256 | namelen = strnlen(name, CMOS_MAX_NAME_LENGTH); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 257 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 258 | /* find the requested entry record */ |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 259 | ct = cbfs_boot_map_with_leak("cmos_layout.bin", |
| 260 | CBFS_COMPONENT_CMOS_LAYOUT, NULL); |
Patrick Georgi | cef3b89 | 2011-01-18 14:28:45 +0000 | [diff] [blame] | 261 | if (!ct) { |
Stefan Reinauer | 1babddb | 2011-10-14 15:22:52 -0700 | [diff] [blame] | 262 | printk(BIOS_ERR, "RTC: cmos_layout.bin could not be found. " |
| 263 | "Options are disabled\n"); |
Timothy Pearson | 7b22d84 | 2015-08-28 19:52:05 -0500 | [diff] [blame] | 264 | |
Stefan Reinauer | 86ddd73 | 2016-03-11 20:22:28 -0800 | [diff] [blame] | 265 | UNLOCK_NVRAM_CBFS_SPINLOCK(); |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 266 | return CB_CMOS_LAYOUT_NOT_FOUND; |
Patrick Georgi | cef3b89 | 2011-01-18 14:28:45 +0000 | [diff] [blame] | 267 | } |
Stefan Reinauer | 86ddd73 | 2016-03-11 20:22:28 -0800 | [diff] [blame] | 268 | ce = (struct cmos_entries *)((unsigned char *)ct + ct->header_length); |
| 269 | for (; ce->tag == LB_TAG_OPTION; |
| 270 | ce = (struct cmos_entries *)((unsigned char *)ce + ce->size)) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 271 | if (memcmp(ce->name, name, namelen) == 0) { |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 272 | found = 1; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 273 | break; |
| 274 | } |
| 275 | } |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 276 | if (!found) { |
Stefan Reinauer | 8e726b7 | 2010-03-29 23:01:35 +0000 | [diff] [blame] | 277 | printk(BIOS_DEBUG, "WARNING: No CMOS option '%s'.\n", name); |
Stefan Reinauer | 86ddd73 | 2016-03-11 20:22:28 -0800 | [diff] [blame] | 278 | UNLOCK_NVRAM_CBFS_SPINLOCK(); |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 279 | return CB_CMOS_OPTION_NOT_FOUND; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 280 | } |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 281 | |
Timothy Pearson | 7b22d84 | 2015-08-28 19:52:05 -0500 | [diff] [blame] | 282 | if (!cmos_checksum_valid(LB_CKS_RANGE_START, LB_CKS_RANGE_END, LB_CKS_LOC)) { |
Stefan Reinauer | 86ddd73 | 2016-03-11 20:22:28 -0800 | [diff] [blame] | 283 | UNLOCK_NVRAM_CBFS_SPINLOCK(); |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 284 | return CB_CMOS_CHECKSUM_INVALID; |
Timothy Pearson | 7b22d84 | 2015-08-28 19:52:05 -0500 | [diff] [blame] | 285 | } |
Martin Roth | 0e7a93f | 2017-01-25 11:00:18 -0700 | [diff] [blame] | 286 | if (get_cmos_value(ce->bit, ce->length, dest) != CB_SUCCESS) { |
| 287 | UNLOCK_NVRAM_CBFS_SPINLOCK(); |
| 288 | return CB_CMOS_ACCESS_ERROR; |
| 289 | } |
Stefan Reinauer | 86ddd73 | 2016-03-11 20:22:28 -0800 | [diff] [blame] | 290 | UNLOCK_NVRAM_CBFS_SPINLOCK(); |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 291 | return CB_SUCCESS; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 292 | } |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 293 | |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 294 | static enum cb_err set_cmos_value(unsigned long bit, unsigned long length, |
| 295 | void *vret) |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 296 | { |
| 297 | unsigned char *ret; |
Stefan Reinauer | 86ddd73 | 2016-03-11 20:22:28 -0800 | [diff] [blame] | 298 | unsigned long byte, byte_bit; |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 299 | unsigned long i; |
| 300 | unsigned char uchar, mask; |
| 301 | unsigned int chksum_update_needed = 0; |
| 302 | |
| 303 | ret = vret; |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 304 | byte = bit / 8; /* find the byte where the data starts */ |
| 305 | byte_bit = bit % 8; /* find the bit where the data starts */ |
| 306 | if (length <= 8) { /* one byte or less */ |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 307 | mask = (1 << length) - 1; |
| 308 | mask <<= byte_bit; |
| 309 | |
| 310 | uchar = cmos_read(byte); |
| 311 | uchar &= ~mask; |
| 312 | uchar |= (ret[0] << byte_bit); |
| 313 | cmos_write(uchar, byte); |
| 314 | if (byte >= LB_CKS_RANGE_START && byte <= LB_CKS_RANGE_END) |
| 315 | chksum_update_needed = 1; |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 316 | } else { /* more that one byte so transfer the whole bytes */ |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 317 | if (byte_bit || length % 8) |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 318 | return CB_ERR_ARG; |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 319 | |
Stefan Reinauer | 24850cc | 2016-05-04 16:16:47 -0700 | [diff] [blame] | 320 | for (i = 0; length; i++, length -= 8, byte++) { |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 321 | cmos_write(ret[i], byte); |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 322 | if (byte >= LB_CKS_RANGE_START && |
| 323 | byte <= LB_CKS_RANGE_END) |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 324 | chksum_update_needed = 1; |
Stefan Reinauer | 24850cc | 2016-05-04 16:16:47 -0700 | [diff] [blame] | 325 | } |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | if (chksum_update_needed) { |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 329 | cmos_set_checksum(LB_CKS_RANGE_START, LB_CKS_RANGE_END, |
| 330 | LB_CKS_LOC); |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 331 | } |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 332 | return CB_SUCCESS; |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 333 | } |
| 334 | |
Nico Huber | c3da3fe | 2017-05-11 16:40:40 +0200 | [diff] [blame] | 335 | unsigned int read_option_lowlevel(unsigned int start, unsigned int size, |
| 336 | unsigned int def) |
| 337 | { |
| 338 | printk(BIOS_NOTICE, "NOTICE: read_option() used to access CMOS " |
| 339 | "from non-ROMCC code, please use get_option() instead.\n"); |
| 340 | if (IS_ENABLED(CONFIG_USE_OPTION_TABLE)) { |
| 341 | const unsigned char byte = cmos_read(start / 8); |
| 342 | return (byte >> (start & 7U)) & ((1U << size) - 1U); |
| 343 | } |
| 344 | return def; |
| 345 | } |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 346 | |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 347 | enum cb_err set_option(const char *name, void *value) |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 348 | { |
| 349 | struct cmos_option_table *ct; |
| 350 | struct cmos_entries *ce; |
| 351 | unsigned long length; |
| 352 | size_t namelen; |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 353 | int found = 0; |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 354 | |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 355 | if (!IS_ENABLED(CONFIG_USE_OPTION_TABLE)) |
| 356 | return CB_CMOS_OTABLE_DISABLED; |
| 357 | |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 358 | /* Figure out how long name is */ |
| 359 | namelen = strnlen(name, CMOS_MAX_NAME_LENGTH); |
| 360 | |
| 361 | /* find the requested entry record */ |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 362 | ct = cbfs_boot_map_with_leak("cmos_layout.bin", |
| 363 | CBFS_COMPONENT_CMOS_LAYOUT, NULL); |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 364 | if (!ct) { |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 365 | printk(BIOS_ERR, "cmos_layout.bin could not be found. " |
| 366 | "Options are disabled\n"); |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 367 | return CB_CMOS_LAYOUT_NOT_FOUND; |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 368 | } |
Stefan Reinauer | 86ddd73 | 2016-03-11 20:22:28 -0800 | [diff] [blame] | 369 | ce = (struct cmos_entries *)((unsigned char *)ct + ct->header_length); |
| 370 | for (; ce->tag == LB_TAG_OPTION; |
| 371 | ce = (struct cmos_entries *)((unsigned char *)ce + ce->size)) { |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 372 | if (memcmp(ce->name, name, namelen) == 0) { |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 373 | found = 1; |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 374 | break; |
| 375 | } |
| 376 | } |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 377 | if (!found) { |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 378 | printk(BIOS_DEBUG, "WARNING: No CMOS option '%s'.\n", name); |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 379 | return CB_CMOS_OPTION_NOT_FOUND; |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | length = ce->length; |
| 383 | if (ce->config == 's') { |
| 384 | length = MAX(strlen((const char *)value) * 8, ce->length - 8); |
| 385 | /* make sure the string is null terminated */ |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 386 | if (set_cmos_value(ce->bit + ce->length - 8, 8, &(u8[]){0}) |
| 387 | != CB_SUCCESS) |
Stefan Reinauer | 86ddd73 | 2016-03-11 20:22:28 -0800 | [diff] [blame] | 388 | return CB_CMOS_ACCESS_ERROR; |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 389 | } |
| 390 | |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 391 | if (set_cmos_value(ce->bit, length, value) != CB_SUCCESS) |
Stefan Reinauer | 86ddd73 | 2016-03-11 20:22:28 -0800 | [diff] [blame] | 392 | return CB_CMOS_ACCESS_ERROR; |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 393 | |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 394 | return CB_SUCCESS; |
Sven Schnelle | d29e5bb | 2011-06-06 15:58:54 +0200 | [diff] [blame] | 395 | } |
zbao | a1e6a9c | 2012-08-02 19:02:26 +0800 | [diff] [blame] | 396 | |
| 397 | /* |
| 398 | * If the CMOS is cleared, the rtc_reg has the invalid date. That |
| 399 | * hurts some OSes. Even if we don't set USE_OPTION_TABLE, we need |
| 400 | * to make sure the date is valid. |
| 401 | */ |
Stefan Reinauer | 86ddd73 | 2016-03-11 20:22:28 -0800 | [diff] [blame] | 402 | void cmos_check_update_date(void) |
zbao | a1e6a9c | 2012-08-02 19:02:26 +0800 | [diff] [blame] | 403 | { |
| 404 | u8 year, century; |
| 405 | |
Gabe Black | 03abaee21 | 2014-04-30 21:31:44 -0700 | [diff] [blame] | 406 | /* Assume hardware always supports RTC_CLK_ALTCENTURY. */ |
Marshall Dawson | 5a043fe | 2016-11-05 18:31:33 -0600 | [diff] [blame] | 407 | wait_uip(); |
Gabe Black | 03abaee21 | 2014-04-30 21:31:44 -0700 | [diff] [blame] | 408 | century = cmos_read(RTC_CLK_ALTCENTURY); |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 409 | year = cmos_read(RTC_CLK_YEAR); |
zbao | a1e6a9c | 2012-08-02 19:02:26 +0800 | [diff] [blame] | 410 | |
Gabe Black | b3f08c6 | 2014-04-30 17:12:25 -0700 | [diff] [blame] | 411 | /* |
| 412 | * TODO: If century is 0xFF, 100% that the cmos is cleared. |
| 413 | * Other than that, so far rtc_year is the only entry to check |
| 414 | * if the date is valid. |
| 415 | */ |
| 416 | if (century > 0x99 || year > 0x99) /* Invalid date */ |
Gabe Black | 03abaee21 | 2014-04-30 21:31:44 -0700 | [diff] [blame] | 417 | cmos_reset_date(); |
Marc Jones | 3cc685f | 2014-12-29 21:31:44 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Stefan Reinauer | 86ddd73 | 2016-03-11 20:22:28 -0800 | [diff] [blame] | 420 | int rtc_set(const struct rtc_time *time) |
| 421 | { |
Marc Jones | 3cc685f | 2014-12-29 21:31:44 -0700 | [diff] [blame] | 422 | cmos_write(bin2bcd(time->sec), RTC_CLK_SECOND); |
| 423 | cmos_write(bin2bcd(time->min), RTC_CLK_MINUTE); |
| 424 | cmos_write(bin2bcd(time->hour), RTC_CLK_HOUR); |
| 425 | cmos_write(bin2bcd(time->mday), RTC_CLK_DAYOFMONTH); |
| 426 | cmos_write(bin2bcd(time->mon), RTC_CLK_MONTH); |
| 427 | cmos_write(bin2bcd(time->year % 100), RTC_CLK_YEAR); |
Gabe Black | 03abaee21 | 2014-04-30 21:31:44 -0700 | [diff] [blame] | 428 | /* Same assumption as above: We always have RTC_CLK_ALTCENTURY */ |
| 429 | cmos_write(bin2bcd(time->year / 100), RTC_CLK_ALTCENTURY); |
Marc Jones | 3cc685f | 2014-12-29 21:31:44 -0700 | [diff] [blame] | 430 | cmos_write(bin2bcd(time->wday + 1), RTC_CLK_DAYOFWEEK); |
| 431 | return 0; |
| 432 | } |
| 433 | |
Gabe Black | 03abaee21 | 2014-04-30 21:31:44 -0700 | [diff] [blame] | 434 | int rtc_get(struct rtc_time *time) |
Marc Jones | 3cc685f | 2014-12-29 21:31:44 -0700 | [diff] [blame] | 435 | { |
Marshall Dawson | 5a043fe | 2016-11-05 18:31:33 -0600 | [diff] [blame] | 436 | wait_uip(); |
Marc Jones | 3cc685f | 2014-12-29 21:31:44 -0700 | [diff] [blame] | 437 | time->sec = bcd2bin(cmos_read(RTC_CLK_SECOND)); |
| 438 | time->min = bcd2bin(cmos_read(RTC_CLK_MINUTE)); |
| 439 | time->hour = bcd2bin(cmos_read(RTC_CLK_HOUR)); |
| 440 | time->mday = bcd2bin(cmos_read(RTC_CLK_DAYOFMONTH)); |
| 441 | time->mon = bcd2bin(cmos_read(RTC_CLK_MONTH)); |
| 442 | time->year = bcd2bin(cmos_read(RTC_CLK_YEAR)); |
Gabe Black | 03abaee21 | 2014-04-30 21:31:44 -0700 | [diff] [blame] | 443 | /* Same assumption as above: We always have RTC_CLK_ALTCENTURY */ |
| 444 | time->year += bcd2bin(cmos_read(RTC_CLK_ALTCENTURY)) * 100; |
Marc Jones | 3cc685f | 2014-12-29 21:31:44 -0700 | [diff] [blame] | 445 | time->wday = bcd2bin(cmos_read(RTC_CLK_DAYOFWEEK)) - 1; |
| 446 | return 0; |
zbao | a1e6a9c | 2012-08-02 19:02:26 +0800 | [diff] [blame] | 447 | } |
Kyösti Mälkki | 7ce1a75 | 2016-12-11 12:56:37 +0200 | [diff] [blame] | 448 | |
| 449 | /* |
| 450 | * Signal coreboot proper completed -- just before running payload |
| 451 | * or jumping to ACPI S3 wakeup vector. |
| 452 | */ |
| 453 | void set_boot_successful(void) |
| 454 | { |
| 455 | uint8_t index, byte; |
| 456 | |
| 457 | index = inb(RTC_PORT(0)) & 0x80; |
| 458 | index |= RTC_BOOT_BYTE; |
| 459 | outb(index, RTC_PORT(0)); |
| 460 | |
| 461 | byte = inb(RTC_PORT(1)); |
| 462 | |
| 463 | if (IS_ENABLED(CONFIG_SKIP_MAX_REBOOT_CNT_CLEAR)) { |
| 464 | /* |
| 465 | * Set the fallback boot bit to allow for recovery if |
| 466 | * the payload fails to boot. |
| 467 | * It is the responsibility of the payload to reset |
| 468 | * the normal boot bit to 1 if desired |
| 469 | */ |
| 470 | byte &= ~RTC_BOOT_NORMAL; |
| 471 | } else { |
| 472 | /* If we are in normal mode set the boot count to 0 */ |
| 473 | if (byte & RTC_BOOT_NORMAL) |
| 474 | byte &= 0x0f; |
| 475 | |
| 476 | } |
| 477 | |
| 478 | outb(byte, RTC_PORT(1)); |
| 479 | } |