LCOV - code coverage report
Current view: top level - core - nvram-format.c (source / functions) Hit Total Coverage
Test: skiboot.info Lines: 113 147 76.9 %
Date: 2024-01-02 21:04:04 Functions: 6 11 54.5 %
Branches: 0 0 -

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

Generated by: LCOV version 1.14