summaryrefslogtreecommitdiffstats
path: root/src/usr/pnor/pnor_common.C
blob: e8972e2c43cc96b10cccd2d2f96a9aaa92137cce (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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/pnor/pnor_common.C $                                  */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2014,2015                        */
/* [+] 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                                                     */

#include "pnor_common.H"
#include <pnor/pnorif.H>
#include <pnor/pnor_reasoncodes.H>

#include "ffs.h"           //Common header file with BuildingBlock.
#include "common/ffs_hb.H" //Hostboot def of user data in ffs_entry struct

#include <initservice/initserviceif.H>
#include <util/align.H>

// Trace definition
trace_desc_t* g_trac_pnor = NULL;
TRAC_INIT(&g_trac_pnor, PNOR_COMP_NAME, 4*KILOBYTE, TRACE::BUFFER_SLOW); //4K

// Easy macro replace for unit testing
//#define TRACUCOMP(args...)  TRACFCOMP(args)
#define TRACUCOMP(args...)

/**
 * 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 */
    "WINK",     /**< PNOR::WINK           : Sleep Winkle 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        : FIRDATA */
    "ATTR_TMP",  /**< PNOR::ATTR_TMP       : Temporary Attribute Overrides */
    "ATTR_PERM", /**< PNOR::ATTR_PERM      : Permanent Attribute Overrides */
    "CAPP",     /**< PNOR::CAPP           : CAPP LID */
    "TEST",     /**< PNOR::TEST           : Test space for PNOR*/
    //Not currently used
//    "XXX",    /**< NUM_SECTIONS       : Used as invalid entry */
};

/**
 * @brief calculates the checksum on data(ffs header/entry) and will return
 *    0 if the checksums match
 */
uint32_t PNOR::pnor_ffs_checksum(void* data, size_t size)
{
    uint32_t checksum = 0;

    for (size_t i = 0; i < (size/4); i++)
    {
        checksum ^= ((uint32_t*)data)[i];
    }

    checksum = htobe32(checksum);
    return checksum;
}

errlHndl_t PNOR::parseTOC(uint8_t* i_toc0Buffer, uint8_t* i_toc1Buffer,
           uint32_t & o_TOC_used, SectionData_t * o_TOC, uint64_t i_baseVAddr)
{
    TRACUCOMP(g_trac_pnor,"PNOR::parseTOC>");
    errlHndl_t l_errhdl = NULL;

    bool TOC_0_failed = false;

    do{
        o_TOC_used = 0;

        for (uint32_t cur_TOC = 0; cur_TOC < NUM_TOCS; ++cur_TOC)
        {
            TRACFCOMP(g_trac_pnor, "PNOR::parseTOC verifying TOC: %d",cur_TOC);
            uint64_t nextVAddr = i_baseVAddr;

            // Zero out my table
            for( size_t id = PNOR::FIRST_SECTION;
                 id <= PNOR::NUM_SECTIONS; //include extra entry for error paths
                 ++id )
            {
                o_TOC[id].id = (PNOR::SectionId)id;
                //everything else should default to zero
            }
            // Read TOC information from TOC 0 and then TOC 1
            ffs_hdr* l_ffs_hdr;
            if (cur_TOC == 0)
            {
                l_ffs_hdr = (ffs_hdr*) i_toc0Buffer;
            }
            else if (cur_TOC == 1)
            {
                l_ffs_hdr = (ffs_hdr*) i_toc1Buffer;
            }

            // ffs entry check, 0 if checksums match
            if( PNOR::pnor_ffs_checksum(l_ffs_hdr, FFS_HDR_SIZE) != 0)
            {
                //@TODO - RTC:90780 - May need to handle this differently
                // in SP-less config
                TRACFCOMP(g_trac_pnor,"PNOR::parseTOC pnor_ffs_checksum header"
                        " checksums do not match.");
                if (cur_TOC == 0)
                {
                    TRACFCOMP(g_trac_pnor, "PNOR::parseTOC TOC 0 failed header checksum");
                    TOC_0_failed = true;
                    o_TOC_used = 1;
                    continue;
                }
                else if (cur_TOC == 1 && TOC_0_failed)
                {
                    // Both TOC's failed
                    TRACFCOMP(g_trac_pnor, "PNOR::parseTOC both TOCs are corrupted");
                    /*@
                     * @errortype
                     * @moduleid   PNOR::MOD_PNORCOMMON_PARSETOC
                     * @reasoncode PNOR::RC_CORRUPTED_TOCS
                     * @devdesc    Both TOCs are corruputed
                     */
                    //@todo Add PNOR callout RTC:116145
                    l_errhdl = new ERRORLOG::ErrlEntry
                               (ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                PNOR::MOD_PNORCOMMON_PARSETOC,
                                PNOR::RC_CORRUPTED_TOCS,
                                0, 0, true);
                    break;
                }
                else
                {
                    // TOC 1 failed
                    TRACFCOMP(g_trac_pnor, "PNOR::parseTOC TOC 1 failed header checksum");
                    break;
                }
            }

            // Only check header if on first TOC or the first TOC failed
            if (cur_TOC == 0 || TOC_0_failed)
            {
                TRACFCOMP(g_trac_pnor, "PNOR::parseTOC: FFS Block size=0x%.8X,"
                 " Partition Table Size = 0x%.8x, entry_count=%d",
                 l_ffs_hdr->block_size,l_ffs_hdr->size,l_ffs_hdr->entry_count);

                uint64_t spaceUsed = (sizeof(ffs_entry))*l_ffs_hdr->entry_count;

                /* Checking FFS Header to make sure it looks valid */
                bool header_good = true;
                if(l_ffs_hdr->magic != FFS_MAGIC)
                {
                    TRACFCOMP(g_trac_pnor, "E>PNOR::parseTOC: Invalid magic"
                            " number in FFS header: 0x%.4X",l_ffs_hdr->magic);
                    header_good = false;
                }
                else if(l_ffs_hdr->version != SUPPORTED_FFS_VERSION)
                {
                    TRACFCOMP(g_trac_pnor, "E>PNOR::parseTOC:Unsupported FFS"
                            " Header version: 0x%.4X", l_ffs_hdr->version);
                    header_good = false;
                }
                else if(l_ffs_hdr->entry_size != sizeof(ffs_entry))
                {
                    TRACFCOMP(g_trac_pnor, "E>PNOR::parseTOC: Unexpected"
                            " entry_size(0x%.8x) in FFS header: 0x%.4X",
                            l_ffs_hdr->entry_size);
                    header_good = false;
                }
                else if(l_ffs_hdr->entries == NULL)
                {
                    TRACFCOMP(g_trac_pnor, "E>PNOR::parseTOC:"
                            "  FFS Header pointer to entries is NULL.");
                    header_good = false;
                }
                else if(l_ffs_hdr->block_size != PAGESIZE)
                {
                    TRACFCOMP(g_trac_pnor, "E>PNOR::parseTOC:  Unsupported"
                            " Block Size(0x%.4X). PNOR Blocks must be 4k",
                              l_ffs_hdr->block_size);
                    header_good = false;
                }
                else if(l_ffs_hdr->block_count == 0)
                {
                    TRACFCOMP(g_trac_pnor, "E>PNOR::parseTOC:  Unsupported"
                            " Block COunt(0x%.4X). Device cannot be zero"
                            " blocks in length.",l_ffs_hdr->block_count);
                    header_good = false;
                }
                //Make sure all the entries fit in specified partition
                //table size
                else if(spaceUsed >
                    ((l_ffs_hdr->block_size*l_ffs_hdr->size)-sizeof(ffs_hdr)))
                {
                    TRACFCOMP(g_trac_pnor, "E>PNOR::parseTOC:  FFS Entries"
                            " (0x%.16X) go past end of FFS Table.",spaceUsed);
                    header_good = false;
                }

                if(!header_good)
                {
                    //Shutdown if we detected a partition table issue
                    //for any reason
                    if (TOC_0_failed)
                    {
                       /*@
                        * @errortype
                        * @moduleid   PNOR::MOD_PNORCOMMON_PARSETOC
                        * @reasoncode PNOR::RC_BAD_TOC_HEADER
                        * @devdesc    TOC 0 doesn't have a good header
                        */
                        l_errhdl = new ERRORLOG::ErrlEntry
                                             (ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                              PNOR::MOD_PNORCOMMON_PARSETOC,
                                              PNOR::RC_BAD_TOC_HEADER,
                                              0, 0, true);
                        break;
                    }
                    else
                    {
                        TOC_0_failed = true;
                    }
                    //Try TOC1
                    continue;
                }
            }

            ffs_hb_user_t* ffsUserData = NULL;

            //Walk through all the entries in the table and parse the data.
            for(uint32_t i=0; i<l_ffs_hdr->entry_count; i++)
            {
                ffs_entry* cur_entry = (&l_ffs_hdr->entries[i]);

                TRACUCOMP(g_trac_pnor, "PNOR::parseTOC:  Entry %d, name=%s, "
                        "pointer=0x%X",i,cur_entry->name, (uint64_t)cur_entry);

                uint32_t secId = PNOR::INVALID_SECTION;

                // ffs entry check, 0 if checksums match
                if( PNOR::pnor_ffs_checksum(cur_entry, FFS_ENTRY_SIZE) != 0)
                {
                    //@TODO - RTC:90780 - May need to handle this differently
                    // in SP-less config
                    TRACFCOMP(g_trac_pnor, "PNOR::parseTOC pnor_ffs_checksum"
                            " entry checksums do not match");
                    if (cur_TOC == 0)
                    {
                        TRACFCOMP(g_trac_pnor,"PNOR::parseTOC TOC 0 entry"
                                " checksum failed");
                        TOC_0_failed = true;
                        o_TOC_used = 1;
                        break;
                    }
                    else if (cur_TOC == 1 && TOC_0_failed)
                    {
                        // Both TOC's failed
                        TRACFCOMP(g_trac_pnor, "PNOR::parseTOC both TOC's are"
                                  " corrupted");
                       /*@
                        * @errortype
                        * @moduleid   PNOR::MOD_PNORCOMMON_PARSETOC
                        * @reasoncode PNOR::RC_PARTITION_TABLE_INVALID
                        * @devdesc    Both TOCs are corrupted
                        */
                        l_errhdl = new ERRORLOG::ErrlEntry
                                             (ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                              PNOR::MOD_PNORCOMMON_PARSETOC,
                                              PNOR::RC_PARTITION_TABLE_INVALID,
                                              0, 0, true);
                        break;
                    }
                    else
                    {
                        // TOC 1 failed
                        TRACFCOMP(g_trac_pnor, "PNOR::parseTOC TOC 1 entry"
                                " checksum failed");
                        break;
                    }
                }

                // Only set data if on first TOC or the first TOC failed
                if (cur_TOC == 0 || TOC_0_failed)
                {
                    //Figure out section enum
                    for(uint32_t eyeIndex=PNOR::TOC;eyeIndex<PNOR::NUM_SECTIONS;
                                     eyeIndex++)
                    {
                        if(strcmp(cv_EYECATCHER[eyeIndex],cur_entry->name) == 0)
                        {
                            secId = eyeIndex;
                            TRACUCOMP(g_trac_pnor, "PNOR::parseTOC: sectionId=%d", secId);
                            break;
                        }
                    }

                    if(secId == PNOR::INVALID_SECTION)
                    {
                        TRACFCOMP(g_trac_pnor, "PNOR::parseTOC:  Unrecognized"
                                " Section name(%s), skipping",cur_entry->name);
                        continue;
                    }

                    ffsUserData = (ffs_hb_user_t*)&(cur_entry->user);

                    //size
                    o_TOC[secId].size = ((uint64_t)cur_entry->size)*PAGESIZE;

                    //virtAddr
                    o_TOC[secId].virtAddr = nextVAddr;
                    nextVAddr += o_TOC[secId].size;

                    //flashAddr
                    o_TOC[secId].flashAddr=((uint64_t)cur_entry->base)*PAGESIZE;

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

                    //user data
                    o_TOC[secId].integrity = ffsUserData->dataInteg;
                    o_TOC[secId].version = ffsUserData->verCheck;
                    o_TOC[secId].misc = ffsUserData->miscFlags;

                    TRACFCOMP(g_trac_pnor,"PNOR::parseTOC: User Data %s",
                            cur_entry->name);

                    if (o_TOC[secId].integrity == FFS_INTEG_ECC_PROTECT)
                    {
                        TRACFCOMP(g_trac_pnor, "PNOR::TOC: ECC enabled for %s",
                                cur_entry->name);
                        o_TOC[secId].size = ALIGN_PAGE_DOWN
                                            ((o_TOC[secId].size * 8 ) / 9);
                    }

                    // TODO RTC:96009 handle version header w/secureboot
                    if (o_TOC[secId].version == FFS_VERS_SHA512)
                    {
                        TRACFCOMP(g_trac_pnor, "PNOR::parseTOC: Incrementing"
                                " Flash Address for SHA Header");
                        if (o_TOC[secId].integrity == FFS_INTEG_ECC_PROTECT)
                        {
                            o_TOC[secId].flashAddr += PAGESIZE_PLUS_ECC;
                        }
                        else
                        {
                            o_TOC[secId].flashAddr += PAGESIZE;
                        }
                    }

                    if((o_TOC[secId].flashAddr + o_TOC[secId].size) >
                            (l_ffs_hdr->block_count*PAGESIZE))
                    {
                        TRACFCOMP(g_trac_pnor, "E>PNOR::parseTOC:Partition(%s)"
                                " at base address (0x%.8x) extends past end of"
                                " flash device",cur_entry->name,
                                o_TOC[secId].flashAddr);
                       /*@
                        * @errortype
                        * @moduleid   PNOR::MOD_PNORCOMMON_PARSETOC
                        * @reasoncode PNOR::RC_SECTION_SIZE_IS_BIG
                        * @devdesc    Invalid partition table
                        */
                        l_errhdl = new ERRORLOG::ErrlEntry
                                             (ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                              PNOR::MOD_PNORCOMMON_PARSETOC,
                                              PNOR::RC_SECTION_SIZE_IS_BIG,
                                              0, 0, true);
                        break;
                    }
                }
            }
            if (l_errhdl)
            {
                TRACFCOMP(g_trac_pnor, ERR_MRK"PNOR::parseTOC: error parsing");
                break;
            }

            for(PNOR::SectionId tmpId = PNOR::FIRST_SECTION;
                tmpId < PNOR::NUM_SECTIONS;
                tmpId = (PNOR::SectionId) (tmpId + 1) )
            {
                TRACFCOMP(g_trac_pnor, "%s:    size=0x%.8X  flash=0x%.8X  "
                       "virt=0x%.16X", cv_EYECATCHER[tmpId], o_TOC[tmpId].size,
                       o_TOC[tmpId].flashAddr, o_TOC[tmpId].virtAddr );
            }
        }
    } while(0);

    TRACUCOMP(g_trac_pnor, "< PNOR::parseTOC" );
    return l_errhdl;
}

OpenPOWER on IntegriCloud