Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 1 | /*****************************************************************************\ |
| 2 | * cmos_ops.c |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 3 | ***************************************************************************** |
| 4 | * Copyright (C) 2002-2005 The Regents of the University of California. |
| 5 | * Produced at the Lawrence Livermore National Laboratory. |
| 6 | * Written by David S. Peterson <dsp@llnl.gov> <dave_peterson@pobox.com>. |
| 7 | * UCRL-CODE-2003-012 |
| 8 | * All rights reserved. |
| 9 | * |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame] | 10 | * This file is part of nvramtool, a utility for reading/writing coreboot |
Stefan Reinauer | f527e70 | 2008-01-18 15:33:49 +0000 | [diff] [blame] | 11 | * parameters and displaying information from the coreboot table. |
Paul Menzel | a8843de | 2017-06-05 12:33:23 +0200 | [diff] [blame] | 12 | * For details, see https://coreboot.org/nvramtool. |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 13 | * |
| 14 | * Please also read the file DISCLAIMER which is included in this software |
| 15 | * distribution. |
| 16 | * |
| 17 | * This program is free software; you can redistribute it and/or modify it |
| 18 | * under the terms of the GNU General Public License (as published by the |
| 19 | * Free Software Foundation) version 2, dated June 1991. |
| 20 | * |
| 21 | * This program is distributed in the hope that it will be useful, but |
| 22 | * WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF |
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and |
| 24 | * conditions of the GNU General Public License for more details. |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 25 | \*****************************************************************************/ |
| 26 | |
| 27 | #include "common.h" |
| 28 | #include "cmos_ops.h" |
| 29 | #include "cmos_lowlevel.h" |
| 30 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 31 | static int prepare_cmos_op_common(const cmos_entry_t * e); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 32 | |
| 33 | /**************************************************************************** |
| 34 | * prepare_cmos_op_common |
| 35 | * |
| 36 | * Perform a few checks common to both reads and writes. |
| 37 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 38 | static int prepare_cmos_op_common(const cmos_entry_t * e) |
| 39 | { |
| 40 | int result; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 41 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 42 | if (e->config == CMOS_ENTRY_RESERVED) |
| 43 | /* Access to reserved parameters is not permitted. */ |
| 44 | return CMOS_OP_RESERVED; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 45 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 46 | if ((result = verify_cmos_op(e->bit, e->length, e->config)) != OK) |
| 47 | return result; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 48 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 49 | assert(e->length > 0); |
| 50 | return OK; |
| 51 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 52 | |
| 53 | /**************************************************************************** |
| 54 | * prepare_cmos_read |
| 55 | * |
| 56 | * The caller wishes to read a CMOS parameter represented by 'e'. Perform |
| 57 | * sanity checking on 'e'. If a problem was found with e, return an error |
| 58 | * code. Else return OK. |
| 59 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 60 | int prepare_cmos_read(const cmos_entry_t * e) |
| 61 | { |
| 62 | int result; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 63 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 64 | if ((result = prepare_cmos_op_common(e)) != OK) |
| 65 | return result; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 66 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 67 | switch (e->config) { |
| 68 | case CMOS_ENTRY_ENUM: |
| 69 | case CMOS_ENTRY_HEX: |
| 70 | case CMOS_ENTRY_STRING: |
| 71 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 72 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 73 | default: |
| 74 | BUG(); |
| 75 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 76 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 77 | return OK; |
| 78 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 79 | |
| 80 | /**************************************************************************** |
| 81 | * prepare_cmos_write |
| 82 | * |
| 83 | * The caller wishes to set a CMOS parameter represented by 'e' to a value |
| 84 | * whose string representation is stored in 'value_str'. Perform sanity |
| 85 | * checking on 'value_str'. On error, return an error code. Else store the |
| 86 | * numeric equivalent of 'value_str' in '*value' and return OK. |
| 87 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 88 | int prepare_cmos_write(const cmos_entry_t * e, const char value_str[], |
| 89 | unsigned long long *value) |
| 90 | { |
| 91 | const cmos_enum_t *q; |
| 92 | unsigned long long out; |
| 93 | const char *p; |
Patrick Georgi | dd1aab9 | 2014-08-09 17:06:20 +0200 | [diff] [blame] | 94 | char *memory = NULL; |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 95 | int negative, result, found_one; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 96 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 97 | if ((result = prepare_cmos_op_common(e)) != OK) |
| 98 | return result; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 99 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 100 | switch (e->config) { |
| 101 | case CMOS_ENTRY_ENUM: |
| 102 | /* Make sure the user's input corresponds to a valid option. */ |
| 103 | for (q = first_cmos_enum_id(e->config_id), found_one = 0; |
| 104 | q != NULL; q = next_cmos_enum_id(q)) { |
| 105 | found_one = 1; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 106 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 107 | if (!strncmp(q->text, value_str, CMOS_MAX_TEXT_LENGTH)) |
| 108 | break; |
| 109 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 110 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 111 | if (!found_one) |
| 112 | return CMOS_OP_NO_MATCHING_ENUM; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 113 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 114 | if (q == NULL) |
| 115 | return CMOS_OP_BAD_ENUM_VALUE; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 116 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 117 | out = q->value; |
| 118 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 119 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 120 | case CMOS_ENTRY_HEX: |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 121 | /* See if the first character of 'value_str' (excluding |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 122 | * any initial whitespace) is a minus sign. |
| 123 | */ |
Jonathan Kollasch | 1571dc9 | 2011-04-19 19:34:25 +0000 | [diff] [blame] | 124 | for (p = value_str; isspace((int)(unsigned char)*p); p++) ; |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 125 | negative = (*p == '-'); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 126 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 127 | out = strtoull(value_str, (char **)&p, 0); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 128 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 129 | if (*p) |
| 130 | return CMOS_OP_INVALID_INT; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 131 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 132 | /* If we get this far, the user specified a valid integer. |
| 133 | * However we do not currently support the use of negative |
| 134 | * numbers as CMOS parameter values. |
| 135 | */ |
| 136 | if (negative) |
| 137 | return CMOS_OP_NEGATIVE_INT; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 138 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 139 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 140 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 141 | case CMOS_ENTRY_STRING: |
| 142 | if (e->length < (8 * strlen(value_str))) |
| 143 | return CMOS_OP_VALUE_TOO_WIDE; |
| 144 | memory = malloc(e->length / 8); |
| 145 | memset(memory, 0, e->length / 8); |
| 146 | strcpy(memory, value_str); |
| 147 | out = (unsigned long)memory; |
| 148 | break; |
Stefan Reinauer | a67aab7 | 2008-09-27 10:08:28 +0000 | [diff] [blame] | 149 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 150 | default: |
| 151 | BUG(); |
| 152 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 153 | |
Patrick Georgi | dd1aab9 | 2014-08-09 17:06:20 +0200 | [diff] [blame] | 154 | if ((e->length < (8 * sizeof(*value))) && (out >= (1ull << e->length))) { |
| 155 | if (memory) free(memory); |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 156 | return CMOS_OP_VALUE_TOO_WIDE; |
Patrick Georgi | dd1aab9 | 2014-08-09 17:06:20 +0200 | [diff] [blame] | 157 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 158 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 159 | *value = out; |
| 160 | return OK; |
| 161 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 162 | |
| 163 | /**************************************************************************** |
| 164 | * cmos_checksum_read |
| 165 | * |
Stefan Reinauer | f527e70 | 2008-01-18 15:33:49 +0000 | [diff] [blame] | 166 | * Read the checksum for the coreboot parameters stored in CMOS and return |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 167 | * this value. |
| 168 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 169 | uint16_t cmos_checksum_read(void) |
| 170 | { |
| 171 | uint16_t lo, hi; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 172 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 173 | /* The checksum is stored in a big-endian format. */ |
| 174 | hi = cmos_read_byte(cmos_checksum_index); |
| 175 | lo = cmos_read_byte(cmos_checksum_index + 1); |
| 176 | return (hi << 8) + lo; |
| 177 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 178 | |
| 179 | /**************************************************************************** |
| 180 | * cmos_checksum_write |
| 181 | * |
Stefan Reinauer | f527e70 | 2008-01-18 15:33:49 +0000 | [diff] [blame] | 182 | * Set the checksum for the coreboot parameters stored in CMOS to |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 183 | * 'checksum'. |
| 184 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 185 | void cmos_checksum_write(uint16_t checksum) |
| 186 | { |
| 187 | unsigned char lo, hi; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 188 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 189 | /* The checksum is stored in a big-endian format. */ |
| 190 | hi = (unsigned char)(checksum >> 8); |
| 191 | lo = (unsigned char)(checksum & 0x00ff); |
| 192 | cmos_write_byte(cmos_checksum_index, hi); |
| 193 | cmos_write_byte(cmos_checksum_index + 1, lo); |
| 194 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 195 | |
| 196 | /**************************************************************************** |
| 197 | * cmos_checksum_compute |
| 198 | * |
Stefan Reinauer | f527e70 | 2008-01-18 15:33:49 +0000 | [diff] [blame] | 199 | * Compute a checksum for the coreboot parameter values currently stored in |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 200 | * CMOS and return this checksum. |
| 201 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 202 | uint16_t cmos_checksum_compute(void) |
| 203 | { |
| 204 | unsigned i, sum; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 205 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 206 | sum = 0; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 207 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 208 | for (i = cmos_checksum_start; i <= cmos_checksum_end; i++) |
| 209 | sum += cmos_read_byte(i); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 210 | |
Stefan Reinauer | c31c4de | 2011-10-17 08:58:27 -0700 | [diff] [blame] | 211 | return (uint16_t)(sum & 0xffff); |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 212 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 213 | |
| 214 | /**************************************************************************** |
| 215 | * cmos_checksum_verify |
| 216 | * |
Stefan Reinauer | f527e70 | 2008-01-18 15:33:49 +0000 | [diff] [blame] | 217 | * Verify that the coreboot CMOS checksum is valid. If checksum is not |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 218 | * valid then print warning message and exit. |
| 219 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 220 | void cmos_checksum_verify(void) |
| 221 | { |
| 222 | uint16_t computed, actual; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 223 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 224 | set_iopl(3); |
| 225 | computed = cmos_checksum_compute(); |
| 226 | actual = cmos_checksum_read(); |
| 227 | set_iopl(0); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 228 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 229 | if (computed != actual) { |
Martin Roth | a564811 | 2017-06-03 20:05:42 -0600 | [diff] [blame] | 230 | fprintf(stderr, "%s: Warning: coreboot CMOS checksum is bad.\n", |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 231 | prog_name); |
Denis 'GNUtoo' Carikli | 9c44b25 | 2016-02-20 18:49:13 +0100 | [diff] [blame] | 232 | fprintf(stderr, "Computed checksum: 0x%x. Stored checksum: 0x%x\n", |
| 233 | computed, actual); |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 234 | exit(1); |
| 235 | } |
| 236 | } |