LCOV - code coverage report
Current view: top level - core/test - run-nvram-format.c (source / functions) Hit Total Coverage
Test: skiboot.info Lines: 88 90 97.8 %
Date: 2024-01-02 21:04:04 Functions: 4 5 80.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
       2                 :            : /*
       3                 :            :  * Copyright 2013-2019 IBM Corp.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include <stdlib.h>
       7                 :            : 
       8                 :            : #include "../nvram-format.c"
       9                 :            : 
      10                 :          0 : bool nvram_wait_for_load(void)
      11                 :            : {
      12                 :          0 :         return true;
      13                 :            : }
      14                 :            : 
      15                 :          5 : bool nvram_validate(void)
      16                 :            : {
      17                 :          5 :         return true;
      18                 :            : }
      19                 :            : 
      20                 :          5 : bool nvram_has_loaded(void)
      21                 :            : {
      22                 :          5 :         return true;
      23                 :            : }
      24                 :            : 
      25                 :          5 : static char *nvram_reset(void *nvram_image, int size)
      26                 :            : {
      27                 :          5 :         struct chrp_nvram_hdr *h = nvram_image;
      28                 :            : 
      29                 :            :         /* entire partition used by one key */
      30                 :          5 :         assert(nvram_format(nvram_image, size) == 0);
      31                 :          5 :         memset((char *) h + sizeof(*h), 0, NVRAM_SIZE_FW_PRIV - sizeof(*h));
      32                 :          5 :         assert(nvram_check(nvram_image, size) == 0);
      33                 :            : 
      34                 :          5 :         return (char *) h + sizeof(*h);
      35                 :            : }
      36                 :            : 
      37                 :          1 : int main(void)
      38                 :            : {
      39                 :            :         char *nvram_image;
      40                 :            :         size_t sz;
      41                 :            :         struct chrp_nvram_hdr *h;
      42                 :            :         char *data;
      43                 :            :         const char *result;
      44                 :            : 
      45                 :            :         /* 1024 bytes is too small for our NVRAM */
      46                 :          1 :         nvram_image = malloc(1024);
      47                 :          1 :         assert(nvram_format(nvram_image, 1024)!=0);
      48                 :          1 :         free(nvram_image);
      49                 :            : 
      50                 :            :         /* 4096 bytes is too small for our NVRAM */
      51                 :          1 :         nvram_image = malloc(4096);
      52                 :          1 :         assert(nvram_format(nvram_image, 4096)!=0);
      53                 :          1 :         free(nvram_image);
      54                 :            : 
      55                 :            :         /* 64k is too small for our NVRAM */
      56                 :          1 :         nvram_image = malloc(0x10000);
      57                 :          1 :         assert(nvram_format(nvram_image, 0x10000)!=0);
      58                 :          1 :         free(nvram_image);
      59                 :            : 
      60                 :            :         /* 68k is too small for our NVRAM */
      61                 :          1 :         nvram_image = malloc(68*1024);
      62                 :          1 :         assert(nvram_format(nvram_image, 68*1024)!=0);
      63                 :          1 :         free(nvram_image);
      64                 :            : 
      65                 :            :         /* 68k+16 bytes (nvram header) should generate empty free space */
      66                 :          1 :         sz = NVRAM_SIZE_COMMON + NVRAM_SIZE_FW_PRIV
      67                 :            :                 + sizeof(struct chrp_nvram_hdr);
      68                 :          1 :         nvram_image = malloc(sz);
      69                 :          1 :         assert(nvram_format(nvram_image, sz)==0);
      70                 :          1 :         assert(nvram_check(nvram_image, sz)==0);
      71                 :          1 :         assert(nvram_image[sz-14]==0);
      72                 :          1 :         assert(nvram_image[sz-13]==1);
      73                 :          1 :         h = (struct chrp_nvram_hdr*)(&nvram_image[NVRAM_SIZE_COMMON + NVRAM_SIZE_FW_PRIV]);
      74                 :          1 :         assert(memcmp(h->name, "wwwwwwwwwwww", 12)==0);
      75                 :          1 :         free(nvram_image);
      76                 :            : 
      77                 :            :         /* 128k NVRAM check */
      78                 :          1 :         nvram_image = malloc(128*1024);
      79                 :          1 :         assert(nvram_format(nvram_image, 128*1024)==0);
      80                 :          1 :         assert(nvram_check(nvram_image,128*1024)==0);
      81                 :            : 
      82                 :            :         /* Now, we corrupt it */
      83                 :          1 :         nvram_image[0] = 0;
      84                 :          1 :         assert(nvram_check(nvram_image,128*1024) != 0);
      85                 :            : 
      86                 :            :         /* Does our NUL checking work? */
      87                 :          1 :         assert(nvram_format(nvram_image, 128 * 1024) == 0);
      88                 :          1 :         h = (struct chrp_nvram_hdr *) nvram_image;
      89                 :          1 :         memset((char *) h + sizeof(*h), 0xFF, be16_to_cpu(h->len) * 16 - sizeof(*h));
      90                 :          1 :         assert(nvram_check(nvram_image, 128 * 1024) != 0);
      91                 :            : 
      92                 :          1 :         assert(nvram_format(nvram_image, 128*1024)==0);
      93                 :            :         /* corrupt the length of the partition */
      94                 :          1 :         nvram_image[2] = 0;
      95                 :          1 :         nvram_image[3] = 0;
      96                 :          1 :         assert(nvram_check(nvram_image,128*1024) != 0);
      97                 :            : 
      98                 :          1 :         assert(nvram_format(nvram_image, 128*1024)==0);
      99                 :            :         /* corrupt the length of the partition */
     100                 :          1 :         nvram_image[2] = 0;
     101                 :          1 :         nvram_image[3] = 0;
     102                 :            :         /* but reset checksum! */
     103                 :          1 :         h = (struct chrp_nvram_hdr*)nvram_image;
     104                 :          1 :         h->cksum = chrp_nv_cksum(h);
     105                 :          1 :         assert(nvram_check(nvram_image,128*1024) != 0);
     106                 :            : 
     107                 :          1 :         assert(nvram_format(nvram_image, 128*1024)==0);
     108                 :            :         /* make the length insanely beyond end of nvram  */
     109                 :          1 :         nvram_image[2] = 42;
     110                 :          1 :         nvram_image[3] = 32;
     111                 :            :         /* but reset checksum! */
     112                 :          1 :         h = (struct chrp_nvram_hdr*)nvram_image;
     113                 :          1 :         h->cksum = chrp_nv_cksum(h);
     114                 :          1 :         assert(nvram_check(nvram_image,128*1024) != 0);
     115                 :            : 
     116                 :          1 :         assert(nvram_format(nvram_image, 128*1024)==0);
     117                 :            :         /* remove skiboot partition */
     118                 :          1 :         nvram_image[12] = '\0';
     119                 :            :         /* but reset checksum! */
     120                 :          1 :         h = (struct chrp_nvram_hdr*)nvram_image;
     121                 :          1 :         h->cksum = chrp_nv_cksum(h);
     122                 :          1 :         assert(nvram_check(nvram_image,128*1024) != 0);
     123                 :            : 
     124                 :          1 :         assert(nvram_format(nvram_image, 128*1024)==0);
     125                 :            :         /* remove common partition */
     126                 :          1 :         nvram_image[NVRAM_SIZE_FW_PRIV+5] = '\0';
     127                 :            :         /* but reset checksum! */
     128                 :          1 :         h = (struct chrp_nvram_hdr*)(&nvram_image[NVRAM_SIZE_FW_PRIV]);
     129                 :          1 :         h->cksum = chrp_nv_cksum(h);
     130                 :          1 :         assert(nvram_check(nvram_image,128*1024) != 0);
     131                 :            : 
     132                 :            :         /* test nvram_query() */
     133                 :            : 
     134                 :            :         /* does an empty partition break us? */
     135                 :          1 :         data = nvram_reset(nvram_image, 128*1024);
     136                 :          1 :         assert(nvram_query_safe("test") == NULL);
     137                 :            : 
     138                 :            :         /* does a zero length key break us? */
     139                 :          1 :         data = nvram_reset(nvram_image, 128*1024);
     140                 :          1 :         data[0] = '=';
     141                 :          1 :         assert(nvram_query_safe("test") == NULL);
     142                 :            : 
     143                 :            :         /* does a missing = break us? */
     144                 :          1 :         data = nvram_reset(nvram_image, 128*1024);
     145                 :          1 :         data[0] = 'a';
     146                 :          1 :         assert(nvram_query_safe("test") == NULL);
     147                 :            : 
     148                 :            :         /* does an empty value break us? */
     149                 :          1 :         data = nvram_reset(nvram_image, 128*1024);
     150                 :          1 :         data[0] = 'a';
     151                 :          1 :         data[1] = '=';
     152                 :          1 :         result = nvram_query_safe("a");
     153                 :          1 :         assert(result);
     154                 :          1 :         assert(strlen(result) == 0);
     155                 :            : 
     156                 :            :         /* do we trip over malformed keys? */
     157                 :          1 :         data = nvram_reset(nvram_image, 128*1024);
     158                 :            : #define TEST_1 "a\0a=\0test=test\0"
     159                 :          1 :         memcpy(data, TEST_1, sizeof(TEST_1));
     160                 :          1 :         result = nvram_query_safe("test");
     161                 :          1 :         assert(result);
     162                 :          1 :         assert(strcmp(result, "test") == 0);
     163                 :            : 
     164                 :          1 :         free(nvram_image);
     165                 :            : 
     166                 :          1 :         return 0;
     167                 :            : }

Generated by: LCOV version 1.14