LCOV - code coverage report
Current view: top level - core - flash-subpartition.c (source / functions) Coverage Total Hit
Test: skiboot.info Lines: 68.8 % 48 33
Test Date: 2026-04-23 19:55:58 Functions: 100.0 % 1 1
Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
       2                 :             : /*
       3                 :             :  * Parse flash sub-partitions
       4                 :             :  *
       5                 :             :  * Copyright 2013-2018 IBM Corp.
       6                 :             :  */
       7                 :             : 
       8                 :             : #include <skiboot.h>
       9                 :             : #include <opal-api.h>
      10                 :             : 
      11                 :             : struct flash_hostboot_toc {
      12                 :             :         be32 ec;
      13                 :             :         be32 offset; /* From start of header.  4K aligned */
      14                 :             :         be32 size;
      15                 :             : };
      16                 :             : #define FLASH_HOSTBOOT_TOC_MAX_ENTRIES ((FLASH_SUBPART_HEADER_SIZE - 8) \
      17                 :             :                 /sizeof(struct flash_hostboot_toc))
      18                 :             : 
      19                 :             : struct flash_hostboot_header {
      20                 :             :         char eyecatcher[4];
      21                 :             :         be32 version;
      22                 :             :         struct flash_hostboot_toc toc[FLASH_HOSTBOOT_TOC_MAX_ENTRIES];
      23                 :             : };
      24                 :             : 
      25                 :           5 : int flash_subpart_info(void *part_header, uint32_t header_len,
      26                 :             :                        uint32_t part_size, uint32_t *part_actualp,
      27                 :             :                        uint32_t subid, uint32_t *offset, uint32_t *size)
      28                 :             : {
      29                 :             :         struct flash_hostboot_header *header;
      30                 :             :         char eyecatcher[5];
      31                 :             :         uint32_t i, ec, o, s;
      32                 :             :         uint32_t part_actual;
      33                 :             :         bool subpart_found;
      34                 :             : 
      35                 :           5 :         if (!part_header || ( !offset && !size && !part_actualp)) {
      36                 :           0 :                 prlog(PR_ERR, "FLASH: invalid parameters: ph %p of %p sz %p "
      37                 :             :                       "tsz %p\n", part_header, offset, size, part_actualp);
      38                 :           0 :                 return OPAL_PARAMETER;
      39                 :             :         }
      40                 :             : 
      41                 :           5 :         if (header_len < FLASH_SUBPART_HEADER_SIZE) {
      42                 :           0 :                 prlog(PR_ERR, "FLASH: subpartition header too small 0x%x\n",
      43                 :             :                       header_len);
      44                 :           0 :                 return OPAL_PARAMETER;
      45                 :             :         }
      46                 :             : 
      47                 :           5 :         header = (struct flash_hostboot_header*) part_header;
      48                 :             : 
      49                 :             :         /* Perform sanity */
      50                 :           5 :         i = be32_to_cpu(header->version);
      51                 :           5 :         if (i != 1) {
      52                 :           0 :                 prerror("FLASH: flash subpartition TOC version unknown %i\n", i);
      53                 :           0 :                 return OPAL_RESOURCE;
      54                 :             :         }
      55                 :             : 
      56                 :             :         /* NULL terminate eyecatcher */
      57                 :           5 :         strncpy(eyecatcher, header->eyecatcher, 4);
      58                 :           5 :         eyecatcher[4] = '\0';
      59                 :           5 :         prlog(PR_DEBUG, "FLASH: flash subpartition eyecatcher %s\n",
      60                 :             :               eyecatcher);
      61                 :             : 
      62                 :             :         /*
      63                 :             :          * P11 IMA Catalog partition require subid 0x800200.
      64                 :             :          * Override the subid when we detect an IMCC partition.
      65                 :             :          */
      66                 :           5 :         if (strcmp(eyecatcher, "IMCC") == 0 && subid == 0x820200) {
      67                 :           0 :                 subid = 0x800200;
      68                 :             :         }
      69                 :             : 
      70                 :           5 :         subpart_found = false;
      71                 :           5 :         part_actual = 0;
      72                 :          30 :         for (i = 0; i < FLASH_HOSTBOOT_TOC_MAX_ENTRIES; i++) {
      73                 :             : 
      74                 :          30 :                 ec = be32_to_cpu(header->toc[i].ec);
      75                 :          30 :                 o = be32_to_cpu(header->toc[i].offset);
      76                 :          30 :                 s = be32_to_cpu(header->toc[i].size);
      77                 :             : 
      78                 :             :                 /* Check for null terminating entry */
      79                 :          30 :                 if (!ec && !o && !s)
      80                 :           5 :                         break;
      81                 :             : 
      82                 :             :                 /* Sanity check the offset and size. */
      83                 :          25 :                 if (o + s > part_size) {
      84                 :           0 :                         prerror("FLASH: flash subpartition too big: %i\n", i);
      85                 :           0 :                         return OPAL_RESOURCE;
      86                 :             :                 }
      87                 :          25 :                 if (!s) {
      88                 :           0 :                         prerror("FLASH: flash subpartition zero size: %i\n", i);
      89                 :           0 :                         return OPAL_RESOURCE;
      90                 :             :                 }
      91                 :          25 :                 if (o < FLASH_SUBPART_HEADER_SIZE) {
      92                 :           0 :                         prerror("FLASH: flash subpartition offset too small: "
      93                 :             :                                 "%i\n", i);
      94                 :           0 :                         return OPAL_RESOURCE;
      95                 :             :                 }
      96                 :             :                 /*
      97                 :             :                  * Subpartitions content are different, but multiple toc entries
      98                 :             :                  * may point to the same subpartition.
      99                 :             :                  */
     100                 :          25 :                 if (ALIGN_UP(o + s, FLASH_SUBPART_HEADER_SIZE) > part_actual)
     101                 :           5 :                         part_actual = ALIGN_UP(o + s, FLASH_SUBPART_HEADER_SIZE);
     102                 :             : 
     103                 :          25 :                 if (ec == subid) {
     104                 :           5 :                         if (offset)
     105                 :           5 :                                 *offset += o;
     106                 :           5 :                         if (size)
     107                 :           5 :                                 *size = s;
     108                 :           5 :                         subpart_found = true;
     109                 :             :                 }
     110                 :             :         }
     111                 :           5 :         if (!subpart_found && (offset || size)) {
     112                 :           0 :                 prerror("FLASH: flash subpartition not found.\n");
     113                 :           0 :                 return OPAL_RESOURCE;
     114                 :             :         }
     115                 :           5 :         if (part_actualp)
     116                 :           5 :                 *part_actualp = part_actual;
     117                 :           5 :         return OPAL_SUCCESS;
     118                 :             : }
        

Generated by: LCOV version 2.0-1