1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
/*
* NVRAM Format as specified in PAPR
*
* Copyright 2013-2019 IBM Corp.
*/
#include <skiboot.h>
#include <nvram.h>
struct chrp_nvram_hdr {
uint8_t sig;
uint8_t cksum;
be16 len;
char name[12];
};
static struct chrp_nvram_hdr *skiboot_part_hdr;
#define NVRAM_SIG_FW_PRIV 0x51
#define NVRAM_SIG_SYSTEM 0x70
#define NVRAM_SIG_FREE 0x7f
#define NVRAM_NAME_COMMON "common"
#define NVRAM_NAME_FW_PRIV "ibm,skiboot"
#define NVRAM_NAME_FREE "wwwwwwwwwwww"
/* 64k should be enough, famous last words... */
#define NVRAM_SIZE_COMMON 0x10000
/* 4k should be enough, famous last words... */
#define NVRAM_SIZE_FW_PRIV 0x1000
static uint8_t chrp_nv_cksum(struct chrp_nvram_hdr *hdr)
{
struct chrp_nvram_hdr h_copy = *hdr;
uint8_t b_data, i_sum, c_sum;
uint8_t *p = (uint8_t *)&h_copy;
unsigned int nbytes = sizeof(h_copy);
h_copy.cksum = 0;
for (c_sum = 0; nbytes; nbytes--) {
b_data = *(p++);
i_sum = c_sum + b_data;
if (i_sum < c_sum)
i_sum++;
c_sum = i_sum;
}
return c_sum;
}
int nvram_format(void *nvram_image, uint32_t nvram_size)
{
struct chrp_nvram_hdr *h;
unsigned int offset = 0;
prerror("NVRAM: Re-initializing (size: 0x%08x)\n", nvram_size);
memset(nvram_image, 0, nvram_size);
/* Create private partition */
if (nvram_size - offset < NVRAM_SIZE_FW_PRIV)
return -1;
h = nvram_image + offset;
h->sig = NVRAM_SIG_FW_PRIV;
h->len = cpu_to_be16(NVRAM_SIZE_FW_PRIV >> 4);
strcpy(h->name, NVRAM_NAME_FW_PRIV);
h->cksum = chrp_nv_cksum(h);
prlog(PR_DEBUG, "NVRAM: Created '%s' partition at 0x%08x"
" for size 0x%08x with cksum 0x%02x\n",
NVRAM_NAME_FW_PRIV, offset,
be16_to_cpu(h->len), h->cksum);
offset += NVRAM_SIZE_FW_PRIV;
/* Create common partition */
if (nvram_size - offset < NVRAM_SIZE_COMMON)
return -1;
h = nvram_image + offset;
h->sig = NVRAM_SIG_SYSTEM;
h->len = cpu_to_be16(NVRAM_SIZE_COMMON >> 4);
strcpy(h->name, NVRAM_NAME_COMMON);
h->cksum = chrp_nv_cksum(h);
prlog(PR_DEBUG, "NVRAM: Created '%s' partition at 0x%08x"
" for size 0x%08x with cksum 0x%02x\n",
NVRAM_NAME_COMMON, offset,
be16_to_cpu(h->len), h->cksum);
offset += NVRAM_SIZE_COMMON;
/* Create free space partition */
if (nvram_size - offset < sizeof(struct chrp_nvram_hdr))
return -1;
h = nvram_image + offset;
h->sig = NVRAM_SIG_FREE;
h->len = cpu_to_be16((nvram_size - offset) >> 4);
/* We have the full 12 bytes here */
memcpy(h->name, NVRAM_NAME_FREE, 12);
h->cksum = chrp_nv_cksum(h);
prlog(PR_DEBUG, "NVRAM: Created '%s' partition at 0x%08x"
" for size 0x%08x with cksum 0x%02x\n",
NVRAM_NAME_FREE, offset, be16_to_cpu(h->len), h->cksum);
return 0;
}
/*
* Check that the nvram partition layout is sane and that it
* contains our required partitions. If not, we re-format the
* lot of it
*/
int nvram_check(void *nvram_image, const uint32_t nvram_size)
{
unsigned int offset = 0;
bool found_common = false;
skiboot_part_hdr = NULL;
while (offset + sizeof(struct chrp_nvram_hdr) < nvram_size) {
struct chrp_nvram_hdr *h = nvram_image + offset;
if (chrp_nv_cksum(h) != h->cksum) {
prerror("NVRAM: Partition at offset 0x%x"
" has bad checksum: 0x%02x vs 0x%02x\n",
offset, h->cksum, chrp_nv_cksum(h));
goto failed;
}
if (be16_to_cpu(h->len) < 1) {
prerror("NVRAM: Partition at offset 0x%x"
" has incorrect 0 length\n", offset);
goto failed;
}
if (h->sig == NVRAM_SIG_SYSTEM &&
strcmp(h->name, NVRAM_NAME_COMMON) == 0)
found_common = true;
if (h->sig == NVRAM_SIG_FW_PRIV &&
strcmp(h->name, NVRAM_NAME_FW_PRIV) == 0)
skiboot_part_hdr = h;
offset += be16_to_cpu(h->len) << 4;
if (offset > nvram_size) {
prerror("NVRAM: Partition at offset 0x%x"
" extends beyond end of nvram !\n", offset);
goto failed;
}
}
if (!found_common) {
prlog_once(PR_ERR, "NVRAM: Common partition not found !\n");
goto failed;
}
if (!skiboot_part_hdr) {
prlog_once(PR_ERR, "NVRAM: Skiboot private partition not found !\n");
goto failed;
} else {
/*
* The OF NVRAM format requires config strings to be NUL
* terminated and unused memory to be set to zero. Well behaved
* software should ensure this is done for us, but we should
* always check.
*/
const char *last_byte = (const char *) skiboot_part_hdr +
be16_to_cpu(skiboot_part_hdr->len) * 16 - 1;
if (*last_byte != 0) {
prerror("NVRAM: Skiboot private partition is not NUL terminated");
goto failed;
}
}
prlog(PR_INFO, "NVRAM: Layout appears sane\n");
assert(skiboot_part_hdr);
return 0;
failed:
return -1;
}
static const char *find_next_key(const char *start, const char *end)
{
/*
* Unused parts of the partition are set to NUL. If we hit two
* NULs in a row then we assume that we have hit the end of the
* partition.
*/
if (*start == 0)
return NULL;
while (start < end) {
if (*start == 0)
return start + 1;
start++;
}
return NULL;
}
static void nvram_dangerous(const char *key)
{
prlog(PR_ERR, " ___________________________________________________________\n");
prlog(PR_ERR, "< Dangerous NVRAM option: %s\n", key);
prlog(PR_ERR, " -----------------------------------------------------------\n");
prlog(PR_ERR, " \\ \n");
prlog(PR_ERR, " \\ WW \n");
prlog(PR_ERR, " <^ \\___/| \n");
prlog(PR_ERR, " \\ / \n");
prlog(PR_ERR, " \\_ _/ \n");
prlog(PR_ERR, " }{ \n");
}
/*
* nvram_query_safe/dangerous() - Searches skiboot NVRAM partition
* for a key=value pair.
*
* Dangerous means it should only be used for testing as it may
* mask issues. Safe is ok for long term use.
*
* Returns a pointer to a NUL terminated string that contains the value
* associated with the given key.
*/
static const char *__nvram_query(const char *key, bool dangerous)
{
const char *part_end, *start;
int key_len = strlen(key);
assert(key);
if (!nvram_has_loaded()) {
prlog(PR_DEBUG,
"NVRAM: Query for '%s' must wait for NVRAM to load\n",
key);
if (!nvram_wait_for_load()) {
prlog(PR_CRIT, "NVRAM: Failed to load\n");
return NULL;
}
}
/*
* The running OS can modify the NVRAM as it pleases so we need to be
* a little paranoid and check that it's ok before we try parse it.
*
* NB: nvram_validate() can update skiboot_part_hdr
*/
if (!nvram_validate())
return NULL;
assert(skiboot_part_hdr);
part_end = (const char *) skiboot_part_hdr
+ be16_to_cpu(skiboot_part_hdr->len) * 16 - 1;
start = (const char *) skiboot_part_hdr
+ sizeof(*skiboot_part_hdr);
if (!key_len) {
prlog(PR_WARNING, "NVRAM: search key is empty!\n");
return NULL;
}
if (key_len > 32)
prlog(PR_WARNING, "NVRAM: search key '%s' is longer than 32 chars\n", key);
while (start) {
int remaining = part_end - start;
prlog(PR_TRACE, "NVRAM: '%s' (%lu)\n",
start, strlen(start));
if (key_len + 1 > remaining)
return NULL;
if (!strncmp(key, start, key_len) && start[key_len] == '=') {
const char *value = &start[key_len + 1];
prlog(PR_DEBUG, "NVRAM: Searched for '%s' found '%s'\n",
key, value);
if (dangerous)
nvram_dangerous(start);
return value;
}
start = find_next_key(start, part_end);
}
prlog(PR_DEBUG, "NVRAM: '%s' not found\n", key);
return NULL;
}
const char *nvram_query_safe(const char *key)
{
return __nvram_query(key, false);
}
const char *nvram_query_dangerous(const char *key)
{
return __nvram_query(key, true);
}
/*
* nvram_query_eq_safe/dangerous() - Check if the given 'key' exists
* and is set to 'value'.
*
* Dangerous means it should only be used for testing as it may
* mask issues. Safe is ok for long term use.
*
* Note: Its an error to check for non-existence of a key
* by passing 'value == NULL' as a key's value can never be
* NULL in nvram.
*/
static bool __nvram_query_eq(const char *key, const char *value, bool dangerous)
{
const char *s = __nvram_query(key, dangerous);
if (!s)
return false;
assert(value != NULL);
return !strcmp(s, value);
}
bool nvram_query_eq_safe(const char *key, const char *value)
{
return __nvram_query_eq(key, value, false);
}
bool nvram_query_eq_dangerous(const char *key, const char *value)
{
return __nvram_query_eq(key, value, true);
}
|