summaryrefslogtreecommitdiffstats
path: root/src/usr/pnor/pnor_utils.C
blob: 5af2993cb42bede4569f1196dfdf63fd6bf1d249 (plain)
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/pnor/pnor_utils.C $                                   */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2011,2017                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */


/****************** Description *****************************************/
//This file provides a variety of utility functions primarily interacting
//with the table of contents(TOC) of pnor flash. This code is shared by HB
//with the bootloader code (/src/bootloader) so traces can be tricky,
//use the PNOR_UTIL macros to record logs.
/************************************************************************/

#include "pnor_utils.H"
#include <pnor/pnor_const.H>

#if  !defined(__BOOT_LOADER_H)
#include <errl/errlmanager.H>
extern trace_desc_t* g_trac_pnor;
#define PNOR_UTIL_TRACE(arg0, args...) TRACFCOMP(g_trac_pnor, args)
#define PNOR_UTIL_TRACE_W_BRK(arg0, args...) TRACFCOMP(g_trac_pnor, args)
#else
#include <bootloader/bootloader_trace.H>
#define PNOR_UTIL_TRACE(arg0, args...) BOOTLOADER_TRACE(arg0)
#define PNOR_UTIL_TRACE_W_BRK(arg0, args...) BOOTLOADER_TRACE_W_BRK(arg0)
#endif


#include "common/ffs_hb.H"
#include <util/align.H>

/**
 * Eyecatcher strings for PNOR TOC entries
 */
const char* cv_EYECATCHER[] = {
    "part",      /**< PNOR::TOC              : Table of Contents */
    "HBI",       /**< PNOR::HB_EXT_CODE      : Hostboot Extended Image */
    "GLOBAL",    /**< PNOR::GLOBAL_DATA      : Global Data */
    "HBB",       /**< PNOR::HB_BASE_CODE     : Hostboot Base Image */
    "SBEC",      /**< PNOR::CENTAUR_SBE      : Centaur Self-Boot Engine image */
    "SBE",       /**< PNOR::SBE_IPL          : Self-Boot Enginer IPL image */
    "HCODE",     /**< PNOR::HCODE            : HCODE Reference image */
    "PAYLOAD",   /**< PNOR::PAYLOAD          : HAL/OPAL */
    "HBRT",      /**< PNOR::HB_RUNTIME       : Hostboot Runtime(for Sapphire)*/
    "HBD",       /**< PNOR::HB_DATA          : Hostboot Data */
    "GUARD",     /**< PNOR::GUARD_DATA       : Hostboot Data */
    "HBEL",      /**< PNOR::HB_ERRLOGS       : Hostboot Error log Repository */
    "DJVPD",     /**< PNOR::DIMM_JEDEC_VPD   : Dimm JEDEC VPD */
    "MVPD",      /**< PNOR::MODULE_VPD       : Module VPD */
    "CVPD",      /**< PNOR::CENTAUR_VPD      : Centaur VPD */
    "NVRAM",     /**< PNOR::NVRAM            : OPAL Storage */
    "OCC",       /**< PNOR::OCC              : OCC LID */
    "FIRDATA",   /**< PNOR::FIRDATA          : FIRs for checkstop analysis */
    "ATTR_TMP",  /**< PNOR::ATTR_TMP         : Temporary Attribute Overrides */
    "ATTR_PERM", /**< PNOR::ATTR_PERM        : Permanent Attribute Overrides */
    "CAPP",      /**< PNOR::CAPP             : CAPP LID */
    "VERSION",   /**< PNOR::VERSION          : PNOR Version string */
    "HBBL",      /**<PNOR::HB_BOOTLOADER     : Hostboot Bootloader image */
    "TEST",      /**< PNOR::TEST             : Test space for PNOR*/
    "TESTRO",    /**< PNOR::TESTRO           : ReadOnly Test space for PNOR */
    "BACKUP_PART", /**PNOR::BACKUP_PART      : Backup of PART*/
    "POWERVM",   /**< PNOR::POWERVM          : Power VM data */
    "RINGOVD",   /**< PNOR::RINGOVD          : Ring overrides */
    "WOFDATA",   /**< PNOR::WOFDATA          : VFRT data tables for WOF */
};

/**
 * @brief calculates the checksum on data(ffs header/entry) and will return
 *    0 if the checksums match
 */
uint32_t PNOR::pnor_ffs_checksum(void* i_data, size_t i_size)
{
    uint32_t checksum = 0;
    for (size_t i = 0; i < (i_size/4); i++)
    {
        checksum ^= ((uint32_t*)i_data)[i];
    }
    checksum = htobe32(checksum);

    return checksum;
}

//RTC: 147939 Refactor Local Memset
void * localMemset(void *i_dest, int8_t i_value, uint8_t i_size)
{
    unsigned char *buf = (unsigned char *)i_dest;
    while (i_size--)
    {
        *(buf++) = (unsigned char)i_value;
    }
    return i_dest;
}

/**
* @brief Set up some initial information about the sections of the TOC.
*        The section's ID and flashAddr are set to specific values while
*        the rest of the data for the struct is set to 0.
*/
void PNOR::initializeSections(PNOR::SectionData_t io_toc[NUM_SECTIONS])
{
    for( size_t id = PNOR::FIRST_SECTION;
          id <= PNOR::NUM_SECTIONS; //include extra entry for error paths
          ++id )
    {
        localMemset(&io_toc[id], 0, sizeof(io_toc[id]));

        //Set the id to be the name corresponding to this section
        io_toc[id].id = (PNOR::SectionId)id;

        //All sections are initially set to INVALID_FLASH_OFFSET
        //this is large value that is not in the valid addressing range.
        //We chose a large value instead of 0 because the first section
        //has a flash addr of 0 and we want to avoid confusion.
        io_toc[id].flashAddr = INVALID_FLASH_OFFSET;

    }
}



/**
  * @brief Ensure the buffer is not NULL, if it is, then return
  *        the appropriate err code from the o_errCode param.
  *        if the buffer is not NULL then cast it to a ffs_hdr
  *        and return that out through the respective o_param
  */
void PNOR::checkForNullBuffer(uint8_t* i_tocBuffer,
                              uint32_t& o_errCode,
                              ffs_hdr*& o_ffs_hdr)
{
    if(!i_tocBuffer)
    {
        o_errCode |= BUFF_IS_NULL;
        o_ffs_hdr = NULL;
    }
    else
    {
        o_ffs_hdr = (ffs_hdr*)i_tocBuffer;
    }
}


/**
  * @brief Perform a series of checks on the header of the table of contents
  *        These checks include: looking for valid magic #, valid block size,
  *        valid block count, valid entry size, valid entry count, version and
  *        total size.
  *
  */
void PNOR::checkHeader (ffs_hdr* i_ffs_hdr,
                        uint32_t& io_errCode)
{
    uint64_t spaceUsed = (sizeof(ffs_entry))*i_ffs_hdr->entry_count;

    // Checking FFS Header to make sure it looks valid
    // Not breaking after one error, want to collect them all
    if(i_ffs_hdr->magic != FFS_MAGIC)
    {
        PNOR_UTIL_TRACE_W_BRK(BTLDR_TRC_UTILS_CHECKHEADER_MAGIC,
                               "E>PNOR::parseTOC: Invalid magic"
                " number in FFS header: 0x%.4X",i_ffs_hdr->magic);
        io_errCode |= INVALID_MAGIC;
    }
    if(i_ffs_hdr->version != SUPPORTED_FFS_VERSION)
    {
        PNOR_UTIL_TRACE_W_BRK(BTLDR_TRC_UTILS_CHECKHEADER_VERSION,
          "E>PNOR::parseTOC:Unsupported FFS"
                " Header version: 0x%.4X", i_ffs_hdr->version);
        io_errCode |= UNSUPPORTED_FFS;
    }
    if(i_ffs_hdr->entry_size != sizeof(ffs_entry))
    {
        PNOR_UTIL_TRACE_W_BRK(BTLDR_TRC_UTILS_CHECKHEADER_ENTRYSIZE,
                               "E>PNOR::parseTOC: Unexpected"
                " entry_size(0x%.8x) in FFS header: 0x%.4X",
                i_ffs_hdr->entry_size);
        io_errCode |= INVALID_ENTRY_SIZE;
    }
    if(i_ffs_hdr->entry_count == 0)
    {
        PNOR_UTIL_TRACE_W_BRK(BTLDR_TRC_UTILS_CHECKHEADER_ENTRYCNT,
                               "E>PNOR::parseTOC:"
                                " FFS Header pointer to entries is NULL.");
        io_errCode |= NO_ENTRIES;
    }
    if(i_ffs_hdr->block_size != PAGESIZE)
    {
        PNOR_UTIL_TRACE_W_BRK(BTLDR_TRC_UTILS_CHECKHEADER_BLOCKSIZE,
                              "E>PNOR::parseTOC:  Unsupported"
                              " Block Size(0x%.4X). PNOR Blocks must be 4k",
                              i_ffs_hdr->block_size);
        io_errCode |= INVALID_BLOCK_SIZE;
    }
    if(i_ffs_hdr->block_count == 0)
    {
        PNOR_UTIL_TRACE_W_BRK(BTLDR_TRC_UTILS_CHECKHEADER_BLOCKCNT,
                              "E>PNOR::parseTOC:  Unsupported"
                " Block COunt(0x%.4X). Device cannot be zero"
                " blocks in length.",i_ffs_hdr->block_count);
        io_errCode |= INVALID_BLOCK_COUNT;
    }
    //Make sure all the entries fit in specified partition
    //table size
    if(spaceUsed >
        ((i_ffs_hdr->block_size*i_ffs_hdr->size)-sizeof(ffs_hdr)))
    {
        PNOR_UTIL_TRACE_W_BRK(BTLDR_TRC_UTILS_CHECKHEADER_HDRSIZE,
                              "E>PNOR::parseTOC:  FFS Entries"
                        " (0x%.16X) go past end of FFS Table.",spaceUsed);
        io_errCode |= INVALID_HEADER_SIZE;

    }
    if(io_errCode != NO_ERROR)
    {
        io_errCode |= HEADER_ERR;
    }
}

/**
  * @brief Takes in an ffs_entry and returns the enum version of the section
  *         title.
  */
void PNOR::getSectionEnum (ffs_entry* i_entry,
                           uint32_t* o_secId)
{
    *o_secId = PNOR::INVALID_SECTION;
    //Figure out section enum
    for(uint32_t eyeIndex=PNOR::TOC;eyeIndex<PNOR::NUM_SECTIONS;
                      eyeIndex++)
    {
        if(strcmp(cv_EYECATCHER[eyeIndex],i_entry->name) == 0)
        {
            *o_secId = eyeIndex;
            break;
        }
    }
}

/**
  * @brief Iterate through the entries, each which represent a section in pnor.
  *         During the iteration we are checking that the entries are valid
  *        and we set the sectionData_t for each section in the TOC.
  */
void PNOR::parseEntries (ffs_hdr* i_ffs_hdr,
                         uint32_t& io_errCode,
                         SectionData_t * io_TOC,
                         ffs_entry*& o_err_entry)
{
    //Walk through all the entries in the table and parse the data.
    for(uint32_t i=0; i<i_ffs_hdr->entry_count; i++)
    {
        ffs_entry* cur_entry = (&i_ffs_hdr->entries[i]);
        uint32_t secId = PNOR::INVALID_SECTION;

        // ffs entry check, 0 if checksums match
        if( PNOR::pnor_ffs_checksum(cur_entry, FFS_ENTRY_SIZE) != 0)
        {
            PNOR_UTIL_TRACE_W_BRK(BTLDR_TRC_UTILS_PARSE_CHECKSUM_ERROR,
                                   "E>PNOR::parseTOC:  "
                                   "Check sum error while parseing entry ",
                                   "%d in TOC", i);
            io_errCode |= ENTRY_ERR;
            io_errCode |= ENTRY_CHECKSUM_ERR;
            //note the entry we failed on
            o_err_entry = cur_entry;
            //break beacuse we are going to waste time checking other entries
            break;
        }
        //Figure out section enum
        getSectionEnum(cur_entry, &secId);

        if(secId == PNOR::INVALID_SECTION)
        {
            PNOR_UTIL_TRACE(BTLDR_TRC_UTILS_PARSE_INVALID_SECTION,
                            "PNOR::parseTOC: "
                            "Unsupported section found while parsing entry ",
                            "%d in TOC \n Entry name is \"%s\"", i,
                            cur_entry->name);
            //continue to skip invalid section
            continue;
        }
        ffs_hb_user_t* ffsUserData = NULL;
        ffsUserData = (ffs_hb_user_t*)&(cur_entry->user);

        //size
        io_TOC[secId].size =
                        ((uint64_t)cur_entry->size)*(i_ffs_hdr->block_size);

        //flashAddr
        io_TOC[secId].flashAddr=
                          ((uint64_t)cur_entry->base)*(i_ffs_hdr->block_size);

        //chipSelect
        io_TOC[secId].chip = ffsUserData->chip;

        //user data
        io_TOC[secId].integrity = ffsUserData->dataInteg;
        io_TOC[secId].version = ffsUserData->verCheck;
        io_TOC[secId].misc = ffsUserData->miscFlags;
        if((io_TOC[secId].flashAddr + io_TOC[secId].size) >
                (i_ffs_hdr->block_count*PAGESIZE))
        {
            PNOR_UTIL_TRACE_W_BRK(BTLDR_TRC_UTILS_PARSE_EXCEEDS_FLASH,
                                  "E>PNOR::parseTOC:  "
                                   "Exceeded flash while parsing entry ",
                                   "%d in TOC \n Entry name is \"%s\"", i,
                                    cur_entry->name);
            io_errCode |= ENTRY_ERR;
            io_errCode |= ENTRY_EXTENDS_BEYOND_FLASH;
            //note the entry we failed on
            o_err_entry = cur_entry;
            break;
        }

        if (io_TOC[secId].integrity == FFS_INTEG_ECC_PROTECT)
        {
            io_TOC[secId].size = ALIGN_PAGE_DOWN
                                ((io_TOC[secId].size * 8 ) / 9);
        }

        if (io_TOC[secId].version == FFS_VERS_SHA512
            && !PNOR::isSecureSection(secId))
        {
              //increment flash addr for sha header
            if (io_TOC[secId].integrity == FFS_INTEG_ECC_PROTECT)
            {
                io_TOC[secId].flashAddr += PAGESIZE_PLUS_ECC ;
            }
            else
            {
                io_TOC[secId].flashAddr += PAGESIZE ;
            }

            // now that we've skipped the header
            // adjust the size to reflect that
            io_TOC[secId].size -= PAGESIZE;
        }

    } // For TOC Entries
}

bool PNOR::isSecureSection(const uint32_t i_section)
{
#ifdef CONFIG_SECUREBOOT
    #ifdef BOOTLOADER
        return i_section == HB_BASE_CODE;
    // TODO securebootp9 uncomment these sections as they become ready for
    // inclusion in p9. Remove this comment after the last one.
    #else
    //    return i_section == HB_EXT_CODE ||
    //           i_section == HB_DATA ||
    //           i_section == SBE_IPL ||
    //           i_section == CENTAUR_SBE ||
    //           i_section == PAYLOAD ||
    //           i_section == SBKT ||
    //           i_section == OCC ||
    //           i_section == HB_RUNTIME;
        return false;
    #endif
#else
    return false;
#endif
}

OpenPOWER on IntegriCloud