blob: 928b403239f0e6625d883b429e9df9e6eb541217 [file] [log] [blame]
Marc Jones3cc685f2014-12-29 21:31:44 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2014 The Chromium OS Authors. All rights reserved.
Timothy Pearson7b22d842015-08-28 19:52:05 -05005 * Copyright (C) 2015 Timothy Pearson <tpearson@raptorengineeringinc.com>, Raptor Engineering
Marc Jones3cc685f2014-12-29 21:31:44 -07006 *
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 Jones3cc685f2014-12-29 21:31:44 -070015 */
16
17#include <arch/acpi.h>
Kyösti Mälkki7ce1a752016-12-11 12:56:37 +020018#include <arch/io.h>
Marc Jones3cc685f2014-12-29 21:31:44 -070019#include <bcd.h>
Kyösti Mälkki7ce1a752016-12-11 12:56:37 +020020#include <fallback.h>
Stefan Reinauerea5c2b62011-10-27 18:42:53 +020021#include <stdint.h>
Kyösti Mälkkic36af7b2014-11-18 12:41:16 +020022#include <version.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000023#include <console/console.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000024#include <pc80/mc146818rtc.h>
Stefan Reinauerca374d42008-01-18 16:16:45 +000025#include <boot/coreboot_tables.h>
Marc Jones3cc685f2014-12-29 21:31:44 -070026#include <rtc.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000027#include <string.h>
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -060028#include <cbfs.h>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020029#include <security/vboot/vbnv.h>
30#include <security/vboot/vbnv_layout.h>
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -060031
32/* There's no way around this include guard. option_table.h is autogenerated */
Martin Roth32c27c22017-06-24 14:07:25 -060033#if IS_ENABLED(CONFIG_USE_OPTION_TABLE)
Stefan Reinauer10ec0fe2010-09-25 10:40:47 +000034#include "option_table.h"
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -060035#else
36#define LB_CKS_RANGE_START 0
37#define LB_CKS_RANGE_END 0
38#define LB_CKS_LOC 0
Stefan Reinauer10ec0fe2010-09-25 10:40:47 +000039#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +000040
Timothy Pearson7b22d842015-08-28 19:52:05 -050041#include <smp/spinlock.h>
42
43#if (defined(__PRE_RAM__) && \
44IS_ENABLED(CONFIG_HAVE_ROMSTAGE_NVRAM_CBFS_SPINLOCK))
Stefan Reinauer86ddd732016-03-11 20:22:28 -080045 #define LOCK_NVRAM_CBFS_SPINLOCK() spin_lock(romstage_nvram_cbfs_lock())
46 #define UNLOCK_NVRAM_CBFS_SPINLOCK() spin_unlock(romstage_nvram_cbfs_lock())
Timothy Pearson7b22d842015-08-28 19:52:05 -050047#else
Stefan Reinauer86ddd732016-03-11 20:22:28 -080048 #define LOCK_NVRAM_CBFS_SPINLOCK() { }
49 #define UNLOCK_NVRAM_CBFS_SPINLOCK() { }
Timothy Pearson7b22d842015-08-28 19:52:05 -050050#endif
Gabe Blackb3f08c62014-04-30 17:12:25 -070051
Gabe Black03abaee212014-04-30 21:31:44 -070052static void cmos_reset_date(void)
zbaoa1e6a9c2012-08-02 19:02:26 +080053{
Vincent Palatinfa90fd42012-08-07 17:03:40 -070054 /* Now setup a default date equals to the build date */
Marc Jones3cc685f2014-12-29 21:31:44 -070055 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 Black03abaee212014-04-30 21:31:44 -070065 rtc_set(&time);
zbaoa1e6a9c2012-08-02 19:02:26 +080066}
67
Gabe Blackb3f08c62014-04-30 17:12:25 -070068static int cmos_checksum_valid(int range_start, int range_end, int cks_loc)
Eric Biederman8ca8d762003-04-22 19:02:15 +000069{
Stefan Reinauer86ddd732016-03-11 20:22:28 -080070 int i;
71 u16 sum, old_sum;
72
Timothy Pearsonf20c6e82015-02-14 16:15:31 -060073 if (IS_ENABLED(CONFIG_STATIC_OPTION_TABLE))
74 return 1;
75
Eric Biederman8ca8d762003-04-22 19:02:15 +000076 sum = 0;
Gabe Blackb3f08c62014-04-30 17:12:25 -070077 for (i = range_start; i <= range_end; i++)
Stefan Reinauer73d5daa2009-04-22 09:03:08 +000078 sum += cmos_read(i);
Gabe Blackb3f08c62014-04-30 17:12:25 -070079 old_sum = ((cmos_read(cks_loc) << 8) | cmos_read(cks_loc + 1)) &
80 0x0ffff;
Eric Biederman8ca8d762003-04-22 19:02:15 +000081 return sum == old_sum;
82}
83
Gabe Blackb3f08c62014-04-30 17:12:25 -070084static void cmos_set_checksum(int range_start, int range_end, int cks_loc)
Eric Biederman8ca8d762003-04-22 19:02:15 +000085{
86 int i;
Stefan Reinauerea5c2b62011-10-27 18:42:53 +020087 u16 sum;
Stefan Reinauer86ddd732016-03-11 20:22:28 -080088
Eric Biederman8ca8d762003-04-22 19:02:15 +000089 sum = 0;
Gabe Blackb3f08c62014-04-30 17:12:25 -070090 for (i = range_start; i <= range_end; i++)
Stefan Reinauer73d5daa2009-04-22 09:03:08 +000091 sum += cmos_read(i);
Stefan Reinauer73d5daa2009-04-22 09:03:08 +000092 cmos_write(((sum >> 8) & 0x0ff), cks_loc);
Gabe Blackb3f08c62014-04-30 17:12:25 -070093 cmos_write(((sum >> 0) & 0x0ff), cks_loc + 1);
Eric Biederman8ca8d762003-04-22 19:02:15 +000094}
95
96#define RTC_CONTROL_DEFAULT (RTC_24H)
97#define RTC_FREQ_SELECT_DEFAULT (RTC_REF_CLCK_32KHZ | RTC_RATE_1024HZ)
Li-Ta Lo84a80282005-01-07 02:42:53 +000098
Duncan Laurie4b1610d2012-09-05 10:52:44 -070099#ifndef __SMM__
Aaron Durbin9fde0d72017-09-15 11:01:17 -0600100static bool __cmos_init(bool invalid)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000101{
Marshall Dawsonf8a274a2016-11-05 18:47:51 -0600102 bool cmos_invalid;
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600103 bool checksum_invalid = false;
104 bool clear_cmos;
105 size_t i;
106 uint8_t x;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000107
Martin Roth7312c542014-05-12 21:52:54 -0600108#ifndef __PRE_RAM__
Stefan Reinauer86ce7f92013-05-28 12:37:08 -0700109 /*
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älkki9d9eb1e2014-06-20 05:38:07 +0300115 if (acpi_is_wakeup_s3())
Aaron Durbin9fde0d72017-09-15 11:01:17 -0600116 return false;
Martin Roth7312c542014-05-12 21:52:54 -0600117#endif /* __PRE_RAM__ */
Stefan Reinauer86ce7f92013-05-28 12:37:08 -0700118
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000119 printk(BIOS_DEBUG, "RTC Init\n");
Steven J. Magnani8cbc4752005-09-12 18:43:27 +0000120
Marshall Dawsonf8a274a2016-11-05 18:47:51 -0600121 /* See if there has been a CMOS power problem. */
122 x = cmos_read(RTC_VALID);
123 cmos_invalid = !(x & RTC_VRT);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000124
Marshall Dawsonf8a274a2016-11-05 18:47:51 -0600125 if (IS_ENABLED(CONFIG_USE_OPTION_TABLE)) {
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600126 /* 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 Biederman8ca8d762003-04-22 19:02:15 +0000129
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600130 clear_cmos = false;
131 } else {
132 clear_cmos = true;
133 }
Vincent Palatinfa90fd42012-08-07 17:03:40 -0700134
Marshall Dawsonf8a274a2016-11-05 18:47:51 -0600135 if (cmos_invalid || invalid)
136 cmos_write(cmos_read(RTC_CONTROL) | RTC_SET, RTC_CONTROL);
137
Eric Biederman8ca8d762003-04-22 19:02:15 +0000138 if (invalid || cmos_invalid || checksum_invalid) {
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600139 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 Palatinfa90fd42012-08-07 17:03:40 -0700146
Gabe Blackb3f08c62014-04-30 17:12:25 -0700147 if (cmos_invalid)
Gabe Black03abaee212014-04-30 21:31:44 -0700148 cmos_reset_date();
Stefan Reinauerfeadfb72012-11-06 18:39:41 -0800149
Vincent Palatinfa90fd42012-08-07 17:03:40 -0700150 printk(BIOS_WARNING, "RTC:%s%s%s%s\n",
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600151 invalid ? " Clear requested":"",
152 cmos_invalid ? " Power Problem":"",
153 checksum_invalid ? " Checksum invalid":"",
154 clear_cmos ? " zeroing cmos":"");
Aaron Durbin9fde0d72017-09-15 11:01:17 -0600155 } else
156 clear_cmos = false;
Steven J. Magnani8cbc4752005-09-12 18:43:27 +0000157
158 /* Setup the real time clock */
Stefan Reinauer73d5daa2009-04-22 09:03:08 +0000159 cmos_write(RTC_CONTROL_DEFAULT, RTC_CONTROL);
Steven J. Magnani8cbc4752005-09-12 18:43:27 +0000160 /* Setup the frequency it operates at */
Stefan Reinauer73d5daa2009-04-22 09:03:08 +0000161 cmos_write(RTC_FREQ_SELECT_DEFAULT, RTC_FREQ_SELECT);
Vincent Palatinfc1b9ee2012-08-07 16:05:14 -0700162 /* Ensure all reserved bits are 0 in register D */
163 cmos_write(RTC_VRT, RTC_VALID);
Steven J. Magnani8cbc4752005-09-12 18:43:27 +0000164
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600165 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 Reinauer86ddd732016-03-11 20:22:28 -0800168 LB_CKS_RANGE_END, LB_CKS_LOC);
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600169 if (checksum_invalid)
170 printk(BIOS_DEBUG, "RTC: coreboot checksum invalid\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000171
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600172 /* 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. Magnani8cbc4752005-09-12 18:43:27 +0000175
Eric Biederman8ca8d762003-04-22 19:02:15 +0000176 /* Clear any pending interrupts */
Gabe Blackb3f08c62014-04-30 17:12:25 -0700177 cmos_read(RTC_INTR_FLAGS);
Aaron Durbin9fde0d72017-09-15 11:01:17 -0600178
179 return clear_cmos;
180}
181
182static 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
197void 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 Biederman8ca8d762003-04-22 19:02:15 +0000203}
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600204#endif /* __SMM__ */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000205
206
Gabe Blackb3f08c62014-04-30 17:12:25 -0700207/*
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 Gagniucd7134e02013-11-23 18:54:44 -0600214static enum cb_err get_cmos_value(unsigned long bit, unsigned long length,
215 void *vret)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000216{
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000217 unsigned char *ret;
Stefan Reinauer86ddd732016-03-11 20:22:28 -0800218 unsigned long byte, byte_bit;
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000219 unsigned long i;
220 unsigned char uchar;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000221
Gabe Blackb3f08c62014-04-30 17:12:25 -0700222 /*
223 * The table is checked when it is built to ensure all
224 * values are valid.
225 */
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000226 ret = vret;
Gabe Blackb3f08c62014-04-30 17:12:25 -0700227 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 Verhaegena9c5ea02009-06-03 14:19:33 +0000230 uchar = cmos_read(byte); /* load the byte */
231 uchar >>= byte_bit; /* shift the bits to byte align */
232 /* clear unspecified bits */
Gabe Blackb3f08c62014-04-30 17:12:25 -0700233 ret[0] = uchar & ((1 << length) - 1);
Stefan Reinauer24850cc2016-05-04 16:16:47 -0700234 } else { /* more than one byte so transfer the whole bytes */
Gabe Blackb3f08c62014-04-30 17:12:25 -0700235 for (i = 0; length; i++, length -= 8, byte++) {
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000236 /* load the byte */
Gabe Blackb3f08c62014-04-30 17:12:25 -0700237 ret[i] = cmos_read(byte);
Luc Verhaegena9c5ea02009-06-03 14:19:33 +0000238 }
239 }
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600240 return CB_SUCCESS;
Luc Verhaegen9ceae902009-06-03 10:47:19 +0000241}
242
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600243enum cb_err get_option(void *dest, const char *name)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000244{
Eric Biederman8ca8d762003-04-22 19:02:15 +0000245 struct cmos_option_table *ct;
246 struct cmos_entries *ce;
247 size_t namelen;
Gabe Blackb3f08c62014-04-30 17:12:25 -0700248 int found = 0;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000249
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600250 if (!IS_ENABLED(CONFIG_USE_OPTION_TABLE))
251 return CB_CMOS_OTABLE_DISABLED;
252
Stefan Reinauer86ddd732016-03-11 20:22:28 -0800253 LOCK_NVRAM_CBFS_SPINLOCK();
Timothy Pearson7b22d842015-08-28 19:52:05 -0500254
Eric Biederman8ca8d762003-04-22 19:02:15 +0000255 /* Figure out how long name is */
256 namelen = strnlen(name, CMOS_MAX_NAME_LENGTH);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000257
Eric Biederman8ca8d762003-04-22 19:02:15 +0000258 /* find the requested entry record */
Aaron Durbin899d13d2015-05-15 23:39:23 -0500259 ct = cbfs_boot_map_with_leak("cmos_layout.bin",
260 CBFS_COMPONENT_CMOS_LAYOUT, NULL);
Patrick Georgicef3b892011-01-18 14:28:45 +0000261 if (!ct) {
Stefan Reinauer1babddb2011-10-14 15:22:52 -0700262 printk(BIOS_ERR, "RTC: cmos_layout.bin could not be found. "
263 "Options are disabled\n");
Timothy Pearson7b22d842015-08-28 19:52:05 -0500264
Stefan Reinauer86ddd732016-03-11 20:22:28 -0800265 UNLOCK_NVRAM_CBFS_SPINLOCK();
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600266 return CB_CMOS_LAYOUT_NOT_FOUND;
Patrick Georgicef3b892011-01-18 14:28:45 +0000267 }
Stefan Reinauer86ddd732016-03-11 20:22:28 -0800268 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 Biederman8ca8d762003-04-22 19:02:15 +0000271 if (memcmp(ce->name, name, namelen) == 0) {
Gabe Blackb3f08c62014-04-30 17:12:25 -0700272 found = 1;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000273 break;
274 }
275 }
Gabe Blackb3f08c62014-04-30 17:12:25 -0700276 if (!found) {
Stefan Reinauer8e726b72010-03-29 23:01:35 +0000277 printk(BIOS_DEBUG, "WARNING: No CMOS option '%s'.\n", name);
Stefan Reinauer86ddd732016-03-11 20:22:28 -0800278 UNLOCK_NVRAM_CBFS_SPINLOCK();
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600279 return CB_CMOS_OPTION_NOT_FOUND;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000280 }
Stefan Reinauer14e22772010-04-27 06:56:47 +0000281
Timothy Pearson7b22d842015-08-28 19:52:05 -0500282 if (!cmos_checksum_valid(LB_CKS_RANGE_START, LB_CKS_RANGE_END, LB_CKS_LOC)) {
Stefan Reinauer86ddd732016-03-11 20:22:28 -0800283 UNLOCK_NVRAM_CBFS_SPINLOCK();
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600284 return CB_CMOS_CHECKSUM_INVALID;
Timothy Pearson7b22d842015-08-28 19:52:05 -0500285 }
Martin Roth0e7a93f2017-01-25 11:00:18 -0700286 if (get_cmos_value(ce->bit, ce->length, dest) != CB_SUCCESS) {
287 UNLOCK_NVRAM_CBFS_SPINLOCK();
288 return CB_CMOS_ACCESS_ERROR;
289 }
Stefan Reinauer86ddd732016-03-11 20:22:28 -0800290 UNLOCK_NVRAM_CBFS_SPINLOCK();
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600291 return CB_SUCCESS;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000292}
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200293
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600294static enum cb_err set_cmos_value(unsigned long bit, unsigned long length,
295 void *vret)
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200296{
297 unsigned char *ret;
Stefan Reinauer86ddd732016-03-11 20:22:28 -0800298 unsigned long byte, byte_bit;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200299 unsigned long i;
300 unsigned char uchar, mask;
301 unsigned int chksum_update_needed = 0;
302
303 ret = vret;
Gabe Blackb3f08c62014-04-30 17:12:25 -0700304 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 Schnelled29e5bb2011-06-06 15:58:54 +0200307 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 Blackb3f08c62014-04-30 17:12:25 -0700316 } else { /* more that one byte so transfer the whole bytes */
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200317 if (byte_bit || length % 8)
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600318 return CB_ERR_ARG;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200319
Stefan Reinauer24850cc2016-05-04 16:16:47 -0700320 for (i = 0; length; i++, length -= 8, byte++) {
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200321 cmos_write(ret[i], byte);
Gabe Blackb3f08c62014-04-30 17:12:25 -0700322 if (byte >= LB_CKS_RANGE_START &&
323 byte <= LB_CKS_RANGE_END)
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200324 chksum_update_needed = 1;
Stefan Reinauer24850cc2016-05-04 16:16:47 -0700325 }
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200326 }
327
328 if (chksum_update_needed) {
Gabe Blackb3f08c62014-04-30 17:12:25 -0700329 cmos_set_checksum(LB_CKS_RANGE_START, LB_CKS_RANGE_END,
330 LB_CKS_LOC);
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200331 }
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600332 return CB_SUCCESS;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200333}
334
Nico Huberc3da3fe2017-05-11 16:40:40 +0200335unsigned 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 Schnelled29e5bb2011-06-06 15:58:54 +0200346
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600347enum cb_err set_option(const char *name, void *value)
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200348{
349 struct cmos_option_table *ct;
350 struct cmos_entries *ce;
351 unsigned long length;
352 size_t namelen;
Gabe Blackb3f08c62014-04-30 17:12:25 -0700353 int found = 0;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200354
Alexandru Gagniucb5669ba2015-01-30 00:07:12 -0600355 if (!IS_ENABLED(CONFIG_USE_OPTION_TABLE))
356 return CB_CMOS_OTABLE_DISABLED;
357
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200358 /* Figure out how long name is */
359 namelen = strnlen(name, CMOS_MAX_NAME_LENGTH);
360
361 /* find the requested entry record */
Aaron Durbin899d13d2015-05-15 23:39:23 -0500362 ct = cbfs_boot_map_with_leak("cmos_layout.bin",
363 CBFS_COMPONENT_CMOS_LAYOUT, NULL);
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200364 if (!ct) {
Gabe Blackb3f08c62014-04-30 17:12:25 -0700365 printk(BIOS_ERR, "cmos_layout.bin could not be found. "
366 "Options are disabled\n");
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600367 return CB_CMOS_LAYOUT_NOT_FOUND;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200368 }
Stefan Reinauer86ddd732016-03-11 20:22:28 -0800369 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 Schnelled29e5bb2011-06-06 15:58:54 +0200372 if (memcmp(ce->name, name, namelen) == 0) {
Gabe Blackb3f08c62014-04-30 17:12:25 -0700373 found = 1;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200374 break;
375 }
376 }
Gabe Blackb3f08c62014-04-30 17:12:25 -0700377 if (!found) {
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200378 printk(BIOS_DEBUG, "WARNING: No CMOS option '%s'.\n", name);
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600379 return CB_CMOS_OPTION_NOT_FOUND;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200380 }
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 Gagniucd7134e02013-11-23 18:54:44 -0600386 if (set_cmos_value(ce->bit + ce->length - 8, 8, &(u8[]){0})
387 != CB_SUCCESS)
Stefan Reinauer86ddd732016-03-11 20:22:28 -0800388 return CB_CMOS_ACCESS_ERROR;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200389 }
390
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600391 if (set_cmos_value(ce->bit, length, value) != CB_SUCCESS)
Stefan Reinauer86ddd732016-03-11 20:22:28 -0800392 return CB_CMOS_ACCESS_ERROR;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200393
Alexandru Gagniucd7134e02013-11-23 18:54:44 -0600394 return CB_SUCCESS;
Sven Schnelled29e5bb2011-06-06 15:58:54 +0200395}
zbaoa1e6a9c2012-08-02 19:02:26 +0800396
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 Reinauer86ddd732016-03-11 20:22:28 -0800402void cmos_check_update_date(void)
zbaoa1e6a9c2012-08-02 19:02:26 +0800403{
404 u8 year, century;
405
Gabe Black03abaee212014-04-30 21:31:44 -0700406 /* Assume hardware always supports RTC_CLK_ALTCENTURY. */
Marshall Dawson5a043fe2016-11-05 18:31:33 -0600407 wait_uip();
Gabe Black03abaee212014-04-30 21:31:44 -0700408 century = cmos_read(RTC_CLK_ALTCENTURY);
Gabe Blackb3f08c62014-04-30 17:12:25 -0700409 year = cmos_read(RTC_CLK_YEAR);
zbaoa1e6a9c2012-08-02 19:02:26 +0800410
Gabe Blackb3f08c62014-04-30 17:12:25 -0700411 /*
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 Black03abaee212014-04-30 21:31:44 -0700417 cmos_reset_date();
Marc Jones3cc685f2014-12-29 21:31:44 -0700418}
419
Stefan Reinauer86ddd732016-03-11 20:22:28 -0800420int rtc_set(const struct rtc_time *time)
421{
Marc Jones3cc685f2014-12-29 21:31:44 -0700422 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 Black03abaee212014-04-30 21:31:44 -0700428 /* Same assumption as above: We always have RTC_CLK_ALTCENTURY */
429 cmos_write(bin2bcd(time->year / 100), RTC_CLK_ALTCENTURY);
Marc Jones3cc685f2014-12-29 21:31:44 -0700430 cmos_write(bin2bcd(time->wday + 1), RTC_CLK_DAYOFWEEK);
431 return 0;
432}
433
Gabe Black03abaee212014-04-30 21:31:44 -0700434int rtc_get(struct rtc_time *time)
Marc Jones3cc685f2014-12-29 21:31:44 -0700435{
Marshall Dawson5a043fe2016-11-05 18:31:33 -0600436 wait_uip();
Marc Jones3cc685f2014-12-29 21:31:44 -0700437 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 Black03abaee212014-04-30 21:31:44 -0700443 /* Same assumption as above: We always have RTC_CLK_ALTCENTURY */
444 time->year += bcd2bin(cmos_read(RTC_CLK_ALTCENTURY)) * 100;
Marc Jones3cc685f2014-12-29 21:31:44 -0700445 time->wday = bcd2bin(cmos_read(RTC_CLK_DAYOFWEEK)) - 1;
446 return 0;
zbaoa1e6a9c2012-08-02 19:02:26 +0800447}
Kyösti Mälkki7ce1a752016-12-11 12:56:37 +0200448
449/*
450 * Signal coreboot proper completed -- just before running payload
451 * or jumping to ACPI S3 wakeup vector.
452 */
453void 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}