LCOV - code coverage report
Current view: top level - libstb/secvar/test - secvar-test-secboot-tpm.c (source / functions) Hit Total Coverage
Test: skiboot.info Lines: 55 56 98.2 %
Date: 2024-01-02 21:04:04 Functions: 6 6 100.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
       2                 :            : /* Copyright 2020 IBM Corp. */
       3                 :            : #define TPM_SKIBOOT
       4                 :            : #include "secvar_common_test.c"
       5                 :            : #include "../storage/secboot_tpm.c"
       6                 :            : #include "../storage/fakenv_ops.c"
       7                 :            : #include "../secvar_util.c"
       8                 :            : 
       9                 :            : char *secboot_buffer;
      10                 :            : 
      11                 :            : #define ARBITRARY_SECBOOT_SIZE 128000
      12                 :            : 
      13                 :            : const char *secvar_test_name = "secboot_tpm";
      14                 :            : 
      15                 :          2 : int flash_secboot_read(void *dst, uint32_t src, uint32_t len)
      16                 :            : {
      17                 :          2 :         memcpy(dst, secboot_buffer + src, len);
      18                 :          2 :         return 0;
      19                 :            : }
      20                 :            : 
      21                 :         12 : int flash_secboot_write(uint32_t dst, void *src, uint32_t len)
      22                 :            : {
      23                 :         12 :         memcpy(secboot_buffer + dst, src, len);
      24                 :         12 :         return 0;
      25                 :            : }
      26                 :            : 
      27                 :          1 : int flash_secboot_info(uint32_t *total_size)
      28                 :            : {
      29                 :          1 :         *total_size = ARBITRARY_SECBOOT_SIZE;
      30                 :          1 :         return 0;
      31                 :            : }
      32                 :            : 
      33                 :            : /* Toggle this to test the physical presence resetting */
      34                 :            : bool phys_presence = false;
      35                 :          1 : bool secvar_check_physical_presence(void)
      36                 :            : {
      37                 :          1 :         return phys_presence;
      38                 :            : }
      39                 :            : 
      40                 :            : struct platform platform;
      41                 :            : 
      42                 :          1 : int run_test(void)
      43                 :            : {
      44                 :            :         int rc;
      45                 :            :         struct secvar *tmp;
      46                 :            : 
      47                 :          1 :         secboot_buffer = zalloc(ARBITRARY_SECBOOT_SIZE);
      48                 :            : 
      49                 :            :         // Initialize and format the storage
      50                 :          1 :         rc = secboot_tpm_store_init();
      51                 :          1 :         ASSERT(OPAL_SUCCESS == rc);
      52                 :            : 
      53                 :            :         // Load the just-formatted empty section
      54                 :          1 :         rc = secboot_tpm_load_bank(&variable_bank, SECVAR_VARIABLE_BANK);
      55                 :          1 :         ASSERT(OPAL_SUCCESS == rc);
      56                 :          1 :         ASSERT(0 == list_length(&variable_bank));
      57                 :            : 
      58                 :            :         // Add some test variables
      59                 :          1 :         tmp = new_secvar("test", 5, "testdata", 8, 0);
      60                 :          1 :         list_add_tail(&variable_bank, &tmp->link);
      61                 :            : 
      62                 :          1 :         tmp = new_secvar("foo", 3, "moredata", 8, 0);
      63                 :          1 :         list_add_tail(&variable_bank, &tmp->link);
      64                 :            : 
      65                 :            :         // Add a priority variable, ensure that works
      66                 :          1 :         tmp = new_secvar("priority", 9, "meep", 4, SECVAR_FLAG_PROTECTED);
      67                 :          1 :         list_add_tail(&variable_bank, &tmp->link);
      68                 :            : 
      69                 :            :         // Add another one
      70                 :          1 :         tmp = new_secvar("priority2", 9, "meep", 4, SECVAR_FLAG_PROTECTED);
      71                 :          1 :         list_add_tail(&variable_bank, &tmp->link);
      72                 :            : 
      73                 :          1 :         ASSERT(4 == list_length(&variable_bank));
      74                 :            : 
      75                 :            :         // Write the bank
      76                 :          1 :         rc = secboot_tpm_write_bank(&variable_bank, SECVAR_VARIABLE_BANK);
      77                 :          1 :         ASSERT(OPAL_SUCCESS == rc);
      78                 :            :         // should write to bank 1 first
      79                 :          1 :         ASSERT(*((uint64_t*) secboot_image->bank[1]) != 0llu);
      80                 :          1 :         ASSERT(*((uint64_t*) secboot_image->bank[0]) == 0llu);
      81                 :            : 
      82                 :            :         // Clear the variable list
      83                 :          1 :         clear_bank_list(&variable_bank);
      84                 :          1 :         ASSERT(0 == list_length(&variable_bank));
      85                 :            : 
      86                 :            :         // Load the bank
      87                 :          1 :         rc = secboot_tpm_load_bank(&variable_bank, SECVAR_VARIABLE_BANK);
      88                 :          1 :         ASSERT(OPAL_SUCCESS == rc);
      89                 :          1 :         ASSERT(4 == list_length(&variable_bank));
      90                 :            : 
      91                 :            :         // Change a variable
      92                 :          1 :         tmp = list_tail(&variable_bank, struct secvar, link);
      93                 :          1 :         memcpy(tmp->data, "somethin", 8);
      94                 :            : 
      95                 :            :         // Write the bank
      96                 :          1 :         rc = secboot_tpm_write_bank(&variable_bank, SECVAR_VARIABLE_BANK);
      97                 :          1 :         ASSERT(OPAL_SUCCESS == rc);
      98                 :            :         // should have data in both now
      99                 :          1 :         ASSERT(*((uint64_t*) secboot_image->bank[0]) != 0llu);
     100                 :          1 :         ASSERT(*((uint64_t*) secboot_image->bank[1]) != 0llu);
     101                 :            : 
     102                 :          1 :         clear_bank_list(&variable_bank);
     103                 :          1 :         free(secboot_buffer);
     104                 :            : 
     105                 :          1 :         return 0;
     106                 :            : }
     107                 :            : 
     108                 :          1 : int main(void)
     109                 :            : {
     110                 :          1 :         int rc = 0;
     111                 :            : 
     112                 :          1 :         list_head_init(&variable_bank);
     113                 :            : 
     114                 :          1 :         rc = run_test();
     115                 :            : 
     116                 :          1 :         if (rc)
     117                 :          0 :                 printf(COLOR_RED "FAILED" COLOR_RESET "\n");
     118                 :            :         else
     119                 :          1 :                 printf(COLOR_GREEN "OK" COLOR_RESET "\n");
     120                 :            : 
     121                 :          1 :         free(tpmnv_vars_image);
     122                 :          1 :         free(tpmnv_control_image);
     123                 :          1 :         free(secboot_image);
     124                 :            : 
     125                 :          1 :         return rc;
     126                 :            : }

Generated by: LCOV version 1.14