coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
option.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <console/console.h>
4 #include <string.h>
5 #include <cbfs.h>
6 #include <option.h>
7 #include <pc80/mc146818rtc.h>
8 #include <types.h>
9 
10 /* option_table.h is autogenerated */
11 #include "option_table.h"
12 
13 /* Don't warn for checking >= LB_CKS_RANGE_START even though it may be 0. */
14 #pragma GCC diagnostic ignored "-Wtype-limits"
15 
16 /*
17  * This routine returns the value of the requested bits.
18  * input bit = bit count from the beginning of the CMOS image
19  * length = number of bits to include in the value
20  * ret = a character pointer to where the value is to be returned
21  * returns CB_SUCCESS = successful, cb_err code if an error occurred
22  */
23 static enum cb_err get_cmos_value(unsigned long bit, unsigned long length,
24  void *vret)
25 {
26  unsigned char *ret;
27  unsigned long byte, byte_bit;
28  unsigned long i;
29  unsigned char uchar;
30 
31  /*
32  * The table is checked when it is built to ensure all
33  * values are valid.
34  */
35  ret = vret;
36  byte = bit / 8; /* find the byte where the data starts */
37  byte_bit = bit % 8; /* find the bit in the byte where the data starts */
38  if (length < 9) { /* one byte or less */
39  uchar = cmos_read(byte); /* load the byte */
40  uchar >>= byte_bit; /* shift the bits to byte align */
41  /* clear unspecified bits */
42  ret[0] = uchar & ((1 << length) - 1);
43  } else { /* more than one byte so transfer the whole bytes */
44  for (i = 0; length; i++, length -= 8, byte++) {
45  /* load the byte */
46  ret[i] = cmos_read(byte);
47  }
48  }
49  return CB_SUCCESS;
50 }
51 
52 static struct cmos_option_table *get_cmos_layout(void)
53 {
54  static struct cmos_option_table *ct = NULL;
55 
56  /*
57  * In case VBOOT is enabled and this function is called from SMM,
58  * we have multiple CMOS layout files and to locate them we'd need to
59  * include VBOOT into SMM...
60  *
61  * Support only one CMOS layout in the RO CBFS for now.
62  */
63  if (!ct)
64  ct = cbfs_ro_map("cmos_layout.bin", NULL);
65  if (!ct)
66  printk(BIOS_ERR, "RTC: cmos_layout.bin could not be found. "
67  "Options are disabled\n");
68  return ct;
69 }
70 
71 static struct cmos_entries *find_cmos_entry(struct cmos_option_table *ct, const char *name)
72 {
73  /* Figure out how long name is */
74  const size_t namelen = strnlen(name, CMOS_MAX_NAME_LENGTH);
75  struct cmos_entries *ce;
76 
77  /* Find the requested entry record */
78  ce = (struct cmos_entries *)((unsigned char *)ct + ct->header_length);
79  for (; ce->tag == LB_TAG_OPTION;
80  ce = (struct cmos_entries *)((unsigned char *)ce + ce->size)) {
81  if (memcmp(ce->name, name, namelen) == 0)
82  return ce;
83  }
84  return NULL;
85 }
86 
87 static enum cb_err cmos_get_uint_option(unsigned int *dest, const char *name)
88 {
89  struct cmos_option_table *ct;
90  struct cmos_entries *ce;
91 
92  ct = get_cmos_layout();
93  if (!ct)
95 
96  ce = find_cmos_entry(ct, name);
97  if (!ce) {
98  printk(BIOS_DEBUG, "No CMOS option '%s'.\n", name);
100  }
101 
102  if (ce->config != 'e' && ce->config != 'h') {
103  printk(BIOS_ERR, "CMOS option '%s' is not of integer type.\n", name);
104  return CB_ERR_ARG;
105  }
106 
107  if (!cmos_checksum_valid(LB_CKS_RANGE_START, LB_CKS_RANGE_END, LB_CKS_LOC))
109 
110  if (get_cmos_value(ce->bit, ce->length, dest) != CB_SUCCESS)
111  return CB_CMOS_ACCESS_ERROR;
112 
113  return CB_SUCCESS;
114 }
115 
116 unsigned int get_uint_option(const char *name, const unsigned int fallback)
117 {
118  unsigned int value = 0;
119  return cmos_get_uint_option(&value, name) == CB_SUCCESS ? value : fallback;
120 }
121 
122 static enum cb_err set_cmos_value(unsigned long bit, unsigned long length,
123  void *vret)
124 {
125  unsigned char *ret;
126  unsigned long byte, byte_bit;
127  unsigned long i;
128  unsigned char uchar, mask;
129  unsigned int chksum_update_needed = 0;
130 
131  ret = vret;
132  byte = bit / 8; /* find the byte where the data starts */
133  byte_bit = bit % 8; /* find the bit where the data starts */
134  if (length <= 8) { /* one byte or less */
135  mask = (1 << length) - 1;
136  mask <<= byte_bit;
137 
138  uchar = cmos_read(byte);
139  uchar &= ~mask;
140  uchar |= (ret[0] << byte_bit);
141  cmos_write(uchar, byte);
142  if (byte >= LB_CKS_RANGE_START && byte <= LB_CKS_RANGE_END)
143  chksum_update_needed = 1;
144  } else { /* more that one byte so transfer the whole bytes */
145  if (byte_bit || length % 8)
146  return CB_ERR_ARG;
147 
148  for (i = 0; length; i++, length -= 8, byte++) {
149  cmos_write(ret[i], byte);
150  if (byte >= LB_CKS_RANGE_START &&
151  byte <= LB_CKS_RANGE_END)
152  chksum_update_needed = 1;
153  }
154  }
155 
156  if (chksum_update_needed) {
157  cmos_set_checksum(LB_CKS_RANGE_START, LB_CKS_RANGE_END,
158  LB_CKS_LOC);
159  }
160  return CB_SUCCESS;
161 }
162 
163 static enum cb_err cmos_set_uint_option(const char *name, unsigned int *value)
164 {
165  struct cmos_option_table *ct;
166  struct cmos_entries *ce;
167 
168  ct = get_cmos_layout();
169  if (!ct)
171 
172  ce = find_cmos_entry(ct, name);
173  if (!ce) {
174  printk(BIOS_DEBUG, "WARNING: No CMOS option '%s'.\n", name);
176  }
177 
178  if (ce->config != 'e' && ce->config != 'h') {
179  printk(BIOS_ERR, "CMOS option '%s' is not of integer type.\n", name);
180  return CB_ERR_ARG;
181  }
182 
183  if (set_cmos_value(ce->bit, ce->length, value) != CB_SUCCESS)
184  return CB_CMOS_ACCESS_ERROR;
185 
186  return CB_SUCCESS;
187 }
188 
189 enum cb_err set_uint_option(const char *name, unsigned int value)
190 {
191  return cmos_set_uint_option(name, &value);
192 }
193 
195 {
196  return cmos_checksum_valid(LB_CKS_RANGE_START, LB_CKS_RANGE_END, LB_CKS_LOC);
197 }
198 
199 void sanitize_cmos(void)
200 {
201  const unsigned char *cmos_default;
202  const bool cmos_need_reset =
203  CONFIG(STATIC_OPTION_TABLE) || cmos_error() || !cmos_lb_cks_valid();
204  size_t length = 128;
205  size_t i;
206 
207  if (CONFIG(TPM_MEASURED_BOOT) || cmos_need_reset) {
208  cmos_default = cbfs_map("cmos.default", &length);
209 
210  if (!cmos_default || !cmos_need_reset)
211  return;
212 
213  u8 control_state = cmos_disable_rtc();
214  for (i = 14; i < MIN(128, length); i++)
215  cmos_write_inner(cmos_default[i], i);
216  cmos_restore_rtc(control_state);
217  }
218 }
pte_t value
Definition: mmu.c:91
const char * name
Definition: mmu.c:92
#define MIN(a, b)
Definition: helpers.h:37
cb_err
coreboot error codes
Definition: cb_err.h:15
@ CB_ERR_ARG
Invalid argument.
Definition: cb_err.h:18
@ CB_CMOS_OPTION_NOT_FOUND
Option string not found.
Definition: cb_err.h:23
@ CB_SUCCESS
Call completed successfully.
Definition: cb_err.h:16
@ CB_CMOS_CHECKSUM_INVALID
CMOS checksum is invalid.
Definition: cb_err.h:25
@ CB_CMOS_ACCESS_ERROR
CMOS access error.
Definition: cb_err.h:24
@ CB_CMOS_LAYOUT_NOT_FOUND
Layout file not found.
Definition: cb_err.h:22
static void * cbfs_ro_map(const char *name, size_t *size_out)
Definition: cbfs.h:251
static void * cbfs_map(const char *name, size_t *size_out)
Definition: cbfs.h:246
@ LB_TAG_OPTION
#define CMOS_MAX_NAME_LENGTH
#define printk(level,...)
Definition: stdlib.h:16
@ CONFIG
Definition: dsi_common.h:201
uint64_t length
Definition: fw_cfg_if.h:1
#define BIOS_DEBUG
BIOS_DEBUG - Verbose output.
Definition: loglevel.h:128
#define BIOS_ERR
BIOS_ERR - System in incomplete state.
Definition: loglevel.h:72
void cmos_set_checksum(int range_start, int range_end, int cks_loc)
Definition: mc146818rtc.c:47
int cmos_error(void)
Definition: mc146818rtc.c:60
int cmos_checksum_valid(int range_start, int range_end, int cks_loc)
Definition: mc146818rtc.c:31
static void cmos_restore_rtc(u8 control_state)
Definition: mc146818rtc.h:135
static void cmos_write(unsigned char val, unsigned char addr)
Definition: mc146818rtc.h:141
static unsigned char cmos_read(unsigned char addr)
Definition: mc146818rtc.h:105
static u8 cmos_disable_rtc(void)
Definition: mc146818rtc.h:127
static void cmos_write_inner(unsigned char val, unsigned char addr)
Definition: mc146818rtc.h:116
int cmos_lb_cks_valid(void)
Definition: option.c:194
void sanitize_cmos(void)
Definition: option.c:199
enum cb_err set_uint_option(const char *name, unsigned int value)
Definition: option.c:189
static enum cb_err set_cmos_value(unsigned long bit, unsigned long length, void *vret)
Definition: option.c:122
static enum cb_err get_cmos_value(unsigned long bit, unsigned long length, void *vret)
Definition: option.c:23
static struct cmos_entries * find_cmos_entry(struct cmos_option_table *ct, const char *name)
Definition: option.c:71
unsigned int get_uint_option(const char *name, const unsigned int fallback)
Definition: option.c:116
static struct cmos_option_table * get_cmos_layout(void)
Definition: option.c:52
static enum cb_err cmos_set_uint_option(const char *name, unsigned int *value)
Definition: option.c:163
static enum cb_err cmos_get_uint_option(unsigned int *dest, const char *name)
Definition: option.c:87
static const int mask[4]
Definition: gpio.c:308
#define NULL
Definition: stddef.h:19
uint8_t u8
Definition: stdint.h:45
size_t strnlen(const char *src, size_t max)
Definition: string.c:34
int memcmp(const void *s1, const void *s2, size_t n)
Definition: memcmp.c:3
uint8_t name[CMOS_MAX_NAME_LENGTH]