summaryrefslogtreecommitdiffstats
path: root/src/usr/pnor/runtime/rt_pnor.C
blob: 2766910032c0847909fc2cc49a38c54db6f51dc1 (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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/pnor/runtime/rt_pnor.C $                              */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2014,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                                                     */

#include <stdlib.h>
#include <targeting/common/targetservice.H>
#include <initservice/taskargs.H>

#include <runtime/rt_targeting.H>
#include <runtime/interface.h>

#include <pnor/pnorif.H>
#include <pnor/ecc.H>
#include <pnor/pnor_reasoncodes.H>
#include "rt_pnor.H"

#include "../ffs.h"
#include "../common/ffs_hb.H"
#include <util/align.H>
#include <runtime/customize_attrs_for_payload.H>
#include <securerom/ROM.H>
#include <config.h>
#include "../pnor_utils.H"
#include <runtime/common/runtime_utils.H>

// Trace definition
extern trace_desc_t* g_trac_pnor;

/**
 * @brief   set up _start() task entry procedure for PNOR daemon
 */
TASK_ENTRY_MACRO( RtPnor::init );

/**
 * @brief  Return the size and address of a given section of PNOR data
 */
errlHndl_t PNOR::getSectionInfo( PNOR::SectionId i_section,
                                 PNOR::SectionInfo_t& o_info )
{
    return Singleton<RtPnor>::instance().getSectionInfo(i_section,o_info);
}

/**
 * @brief  Write the data for a given section into PNOR
 */
errlHndl_t PNOR::flush( PNOR::SectionId i_section)
{
    return Singleton<RtPnor>::instance().flush(i_section);
}

/**
 * @brief  Returns information about a given side of PNOR
 */
errlHndl_t PNOR::getSideInfo( PNOR::SideId i_side,
                              PNOR::SideInfo_t& o_info)
{
    return Singleton<RtPnor>::instance().getSideInfo(i_side,o_info);
}

/**
 * @brief Clear pnor section
 */
errlHndl_t PNOR::clearSection(PNOR::SectionId i_section)
{
    return Singleton<RtPnor>::instance().clearSection(i_section);
}

void PNOR::getPnorInfo( PnorInfo_t& o_pnorInfo )
{
    o_pnorInfo.mmioOffset = LPC_SFC_MMIO_OFFSET | LPC_FW_SPACE;

    //Using sys target
    TARGETING::Target* sys = nullptr;
    TARGETING::targetService().getTopLevelTarget( sys );
    assert(sys != nullptr);

    o_pnorInfo.norWorkarounds = sys->getAttr<
            TARGETING::ATTR_PNOR_FLASH_WORKAROUNDS>();

#ifdef CONFIG_PNOR_IS_32MB
    o_pnorInfo.flashSize = 32*MEGABYTE;
#else
    o_pnorInfo.flashSize = 64*MEGABYTE;
#endif

}

/****************Public Methods***************************/

uint64_t RtPnor::iv_masterProcId = RUNTIME::HBRT_HYP_ID_UNKNOWN;

/**
 * STATIC
 * @brief Static Initializer
 */
void RtPnor::init(errlHndl_t &io_taskRetErrl)
{
    TRACFCOMP(g_trac_pnor, "RtPnor::init> " );
    do {
    io_taskRetErrl  = Singleton<RtPnor>::instance().getMasterProcId();
    if (io_taskRetErrl)
    {
        break;
    }
    io_taskRetErrl  = Singleton<RtPnor>::instance().readTOC();
    if (io_taskRetErrl)
    {
        break;
    }
    }while (0);
    TRACFCOMP(g_trac_pnor, "<RtPnor::init" );
}
/**************************************************************/
errlHndl_t RtPnor::getSectionInfo(PNOR::SectionId i_section,
                              PNOR::SectionInfo_t& o_info)
{
    TRACFCOMP(g_trac_pnor, ENTER_MRK"RtPnor::getSectionInfo");
    errlHndl_t l_err = nullptr;
    do
    {
        // Check if Section is invalid or inhibited from loading at runtime.
        bool l_inhibited = false;
        bool l_secure = PNOR::isEnforcedSecureSection(i_section);
        bool l_preVerified = RUNTIME::isPreVerifiedSection(i_section);
#ifdef CONFIG_SECUREBOOT
        l_inhibited = PNOR::isInhibitedSection(i_section);
#endif
        if (i_section == PNOR::INVALID_SECTION || l_inhibited || l_secure ||
            l_preVerified)
        {
            TRACFCOMP(g_trac_pnor, "RtPnor::getSectionInfo: Invalid Section %d",
                      static_cast<int>(i_section));

            if (l_preVerified)
            {
                TRACFCOMP(g_trac_pnor, ERR_MRK"RtPnor::getSectionInfo: pre-verified sections should be loaded via Hostboot Reserved Memory ");
            }
#ifdef CONFIG_SECUREBOOT
            else if (l_inhibited)
            {
                TRACFCOMP(g_trac_pnor, ERR_MRK"RtPnor::getSectionInfo: attribute overrides inhibited by secureboot");
            }
            else if (l_secure)
            {
                TRACFCOMP(g_trac_pnor, ERR_MRK"RtPnor::getSectionInfo: secure sections should be loaded via Hostboot Reserved Memory");
            }
#endif

            /*@
             * @errortype
             * @moduleid    PNOR::MOD_RTPNOR_GETSECTIONINFO
             * @reasoncode  PNOR::RC_RTPNOR_INVALID_SECTION
             * @userdata1   PNOR::SectionId
             * @userdata2[0:15]   Inhibited by secureboot
             * @userdata2[16:31]  Indication of a secure section
             * @userdata2[32:47]  Indication of a pre-verified section
             * @userdata2[48:63]  0
             * @devdesc     invalid section passed to getSectionInfo  or
             *              section prohibited by secureboot
             */
            l_err = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                            PNOR::MOD_RTPNOR_GETSECTIONINFO,
                                            PNOR::RC_RTPNOR_INVALID_SECTION,
                                            i_section,
                                            FOUR_UINT16_TO_UINT64(l_inhibited,
                                                                  l_secure,
                                                                  l_preVerified,
                                                                  0),
                                            true);
            break;
        }

        //size of the section
        uint64_t l_sizeBytes = iv_TOC[i_section].size;
        if (l_sizeBytes == 0)
        {
            TRACFCOMP(g_trac_pnor,"RtPnor::getSectionInfo: Section %d"
                    " size is 0", static_cast<int>(i_section));
            /*@
             * @errortype
             * @moduleid    PNOR::MOD_RTPNOR_GETSECTIONINFO
             * @reasoncode  PNOR::RC_SECTION_SIZE_IS_ZERO
             * @userdata1   PNOR::SectionId
             * @devdesc     section size is zero
             */
            l_err = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                            PNOR::MOD_RTPNOR_GETSECTIONINFO,
                                            PNOR::RC_SECTION_SIZE_IS_ZERO,
                                            i_section, 0,true);
            break;
        }

        //ecc
        bool l_ecc = (iv_TOC[i_section].integrity&FFS_INTEG_ECC_PROTECT) ?
                      true : false;

        void* l_pWorking = nullptr;
        void* l_pClean   = nullptr;

        //find the section in the map first
        if(iv_pnorMap.find(i_section) != iv_pnorMap.end())
        {
            //get the addresses from the map
            PnorAddrPair_t l_addrPair = iv_pnorMap[i_section];
            l_pWorking = l_addrPair.first;
            l_pClean   = l_addrPair.second;
        }
        else
        {
            //malloc twice -- one working copy and one clean copy
            //So, we can diff and write only the dirty bytes
            l_pWorking = malloc(l_sizeBytes);
            l_pClean   = malloc(l_sizeBytes);

            //offset = 0 : read the entire section
            l_err = readFromDevice(iv_masterProcId, i_section, 0, l_sizeBytes,
                                   l_ecc, l_pWorking);
            if(l_err)
            {
                TRACFCOMP(g_trac_pnor, "RtPnor::getSectionInfo:readFromDevice"
                      " failed");
                break;
            }

            //copy data to another pointer to save a clean copy of data
            memcpy(l_pClean, l_pWorking, l_sizeBytes);

            //save it in the map
            iv_pnorMap [i_section] = PnorAddrPair_t(l_pWorking, l_pClean);
        }
        //return the data in the struct
        o_info.id           = i_section;
        o_info.name         = SectionIdToString(i_section);
        o_info.vaddr        = reinterpret_cast<uint64_t>(l_pWorking);
        o_info.flashAddr    = iv_TOC[i_section].flashAddr;
        o_info.size         = l_sizeBytes;
        o_info.eccProtected = l_ecc;
        o_info.sha512Version=
            (iv_TOC[i_section].version & FFS_VERS_SHA512) ? true : false;
        o_info.sha512perEC  =
           (iv_TOC[i_section].version & FFS_VERS_SHA512_PER_EC) ? true : false;
        o_info.secure = iv_TOC[i_section].secure;
    } while (0);

    TRACFCOMP(g_trac_pnor, EXIT_MRK"RtPnor::getSectionInfo");
    return l_err;
}

/**************************************************************/
errlHndl_t RtPnor::flush( PNOR::SectionId i_section)
{
    TRACFCOMP(g_trac_pnor, ENTER_MRK"RtPnor::flush");
    errlHndl_t l_err = nullptr;
    do
    {
        if (i_section == PNOR::INVALID_SECTION)
        {
            TRACFCOMP(g_trac_pnor,"RtPnor::flush: Invalid Section: %d",
                    (int)i_section);
            /*@
             * @errortype
             * @moduleid    PNOR::MOD_RTPNOR_FLUSH
             * @reasoncode  PNOR::RC_INVALID_SECTION
             * @userdata1   PNOR::SectionId
             * @devdesc     invalid section passed to flush
             */
            l_err = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                            PNOR::MOD_RTPNOR_FLUSH,
                                            PNOR::RC_INVALID_SECTION,
                                            i_section, 0,true);
            break;
        }
        size_t l_sizeBytes = iv_TOC[i_section].size;
        if (l_sizeBytes == 0)
        {
            TRACFCOMP(g_trac_pnor,"RtPnor::flush: Section %d"
                " size is 0", (int)i_section);

            /*@
             * @errortype
             * @moduleid    PNOR::MOD_RTPNOR_FLUSH
             * @reasoncode  PNOR::RC_SECTION_SIZE_IS_ZERO
             * @userdata1   PNOR::SectionId
             * @devdesc     section size is zero
             */
            l_err = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                            PNOR::MOD_RTPNOR_FLUSH,
                                            PNOR::RC_SECTION_SIZE_IS_ZERO,
                                            i_section, 0,true);
            break;
        }

        //get the saved pointers for the partitionName
        PnorAddrMap_t::iterator l_it = iv_pnorMap.find(i_section);
        if(l_it == iv_pnorMap.end())
        {
            TRACFCOMP(g_trac_pnor,"RtPnor::flush: section %d has not been read before",
                      i_section);
            break;
        }
        PnorAddrPair_t l_addrPair = l_it->second;
        uint8_t* l_pWorking = reinterpret_cast<uint8_t*>(l_addrPair.first);
        uint8_t* l_pClean   = reinterpret_cast<uint8_t*>(l_addrPair.second);

        //ecc
        bool l_ecc = (iv_TOC[i_section].integrity&FFS_INTEG_ECC_PROTECT) ?
                            true : false;

        //find the diff between each pointer
        //write back to pnor what doesn't match
        TRACFCOMP(g_trac_pnor, "finding diff between working and clean copy...");
        for (uint64_t i = 0; i < (l_sizeBytes/PAGESIZE); i++)
        {
            if (0 != memcmp(l_pWorking, l_pClean, PAGESIZE))
            {
                TRACFCOMP(g_trac_pnor, "RtPnor::flush: page %d is different,"
                        " writing back to pnor", i);
                l_err = writeToDevice(iv_masterProcId, i_section,
                                      i*PAGESIZE,PAGESIZE, l_ecc, l_pWorking);
                if (l_err)
                {
                    TRACFCOMP(g_trac_pnor, "RtPnor::flush: writeToDevice failed");
                    break;
                }
                //update the clean copy
                memcpy(l_pClean, l_pWorking, PAGESIZE);
            }
            l_pWorking += PAGESIZE;
            l_pClean   += PAGESIZE;
        }

        if (l_err)
        {
            TRACFCOMP(g_trac_pnor,"RtPnor::flush: error writing section %d"
                    " back to pnor",(int)i_section);
            break;
        }
    } while (0);

    TRACFCOMP(g_trac_pnor, EXIT_MRK"RtPnor::flush");
    return l_err;
}
/*******Protected Methods**************/
RtPnor::RtPnor()
{
    do {
    errlHndl_t l_err = getMasterProcId();
    if (l_err)
    {
        errlCommit(l_err, PNOR_COMP_ID);
        break;
    }
    l_err = readTOC();
    if (l_err)
    {
        errlCommit(l_err, PNOR_COMP_ID);
        break;
    }
    } while (0);
}

/*************************/
RtPnor::~RtPnor()
{
}

/*******************Private Methods*********************/
errlHndl_t RtPnor::readFromDevice (uint64_t i_procId,
                                   PNOR::SectionId i_section,
                                   uint64_t i_offset,
                                   size_t i_size,
                                   bool i_ecc,
                                   void* o_data) const
{
    TRACFCOMP(g_trac_pnor, ENTER_MRK"RtPnor::readFromDevice: i_offset=0x%X, "
           "i_procId=%d sec=%d size=0x%X ecc=%d", i_offset, i_procId, i_section,
             i_size, i_ecc);
    errlHndl_t l_err        = nullptr;
    uint8_t*   l_eccBuffer  = nullptr;
    do
    {

        const char* l_partitionName  = SectionIdToString(i_section);
        void*  l_dataToRead          = o_data;
        size_t l_readSize            = i_size;
        size_t l_readSizePlusECC     = (i_size * 9)/8;
        uint64_t l_offset            = i_offset;

        // if we need to handle ECC, we need to read more
        if( i_ecc )
        {
            l_eccBuffer  = new uint8_t[l_readSizePlusECC]();
            l_dataToRead = l_eccBuffer;
            l_readSize   = l_readSizePlusECC;
            l_offset     = (i_offset * 9)/8;
        }

        int l_rc = 0;
        if (g_hostInterfaces && g_hostInterfaces->pnor_read)
        {
            // get the data from OPAL
            l_rc = g_hostInterfaces->pnor_read(i_procId, l_partitionName,
                    l_offset, l_dataToRead, l_readSize);
            if (l_rc < 0)
            {
                TRACFCOMP(g_trac_pnor, "RtPnor::readFromDevice: pnor_read"
                        " failed proc:%d, part:%s, offset:0x%X, size:0x%X,"
                        " dataPt:0x%X, rc:%d", i_procId, l_partitionName,
                        l_offset, l_readSize, l_dataToRead, l_rc);
                /*@
                 * @errortype
                 * @moduleid            PNOR::MOD_RTPNOR_READFROMDEVICE
                 * @reasoncode          PNOR::RC_PNOR_READ_FAILED
                 * @userdata1[00:31]    rc returned from pnor_read
                 * @userdata1[32:63]    section ID
                 * @userdata2[00:31]    offset within the section
                 * @userdata2[32:63]    size of data read in bytes
                 * @devdesc             g_hostInterfaces->pnor_read failed
                 * @custdesc            Error accessing system firmware flash
                 */
                //@todo Add PNOR callout RTC:116145
                l_err = new ERRORLOG::ErrlEntry(
                                 ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                 PNOR::MOD_RTPNOR_READFROMDEVICE,
                                 PNOR::RC_PNOR_READ_FAILED,
                                 TWO_UINT32_TO_UINT64(l_rc, i_section),
                                 TWO_UINT32_TO_UINT64(l_offset, l_readSize),
                                 true);
                break;
            }
            else if( l_rc != static_cast<int>(l_readSize) )
            {
                TRACFCOMP( g_trac_pnor, "RtPnor::readFromDevice: only read 0x%X bytes, expecting 0x%X", l_rc, l_readSize );

                if( PNOR::TOC == i_section )
                {
                    // we can't know how big the TOC partition is without
                    // reading it so we have to make a request for more
                    // data and then handle a smaller amount getting returned
                    TRACFCOMP( g_trac_pnor, "Ignoring mismatch for TOC" );
                }
                else // everything else should have a known size
                {
                    /*@
                     * @errortype
                     * @moduleid            PNOR::MOD_RTPNOR_READFROMDEVICE
                     * @reasoncode          PNOR::RC_WRONG_SIZE_FROM_READ
                     * @userdata1[00:31]    section ID
                     * @userdata1[32:63]    requested size of read
                     * @userdata2[00:31]    requested start offset into flash
                     * @userdata2[32:63]    actual amount read
                     * @devdesc             Amount of data read from pnor does
                     *                      not match expected size
                     * @custdesc          Error accessing system firmware flash
                     */
                    l_err = new ERRORLOG::ErrlEntry(
                                ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                PNOR::MOD_RTPNOR_READFROMDEVICE,
                                PNOR::RC_WRONG_SIZE_FROM_READ,
                                TWO_UINT32_TO_UINT64(i_section,l_readSize),
                                TWO_UINT32_TO_UINT64(l_offset,l_rc),
                                true);
                    break;
                }
            }
        }
        else
        {
            TRACFCOMP(g_trac_pnor,"RtPnor::readFromDevice: This version of"
                    " OPAL does not support pnor_read");
                /*@
                 * @errortype
                 * @moduleid           PNOR::MOD_RTPNOR_READFROMDEVICE
                 * @reasoncode         PNOR::RC_PNOR_READ_NOT_SUPPORTED
                 * @devdesc            g_hostInterfaces->pnor_read not supported
                 * @custdesc           Error accessing system firmware flash
                 */
                //@todo Add PNOR callout RTC:116145
                l_err = new ERRORLOG::ErrlEntry(
                                 ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                 PNOR::MOD_RTPNOR_READFROMDEVICE,
                                 PNOR::RC_PNOR_READ_NOT_SUPPORTED,
                                 0,0,true);
                break;
        }
        // remove the ECC data
        if( i_ecc )
        {
            TRACFCOMP(g_trac_pnor, "RtPnor::readFromDevice: removing ECC...");
            // remove the ECC and fix the original data if it is broken
            size_t l_eccSize = (l_rc/9)*8;
            l_eccSize = std::min( l_eccSize, i_size );
            PNOR::ECC::eccStatus ecc_stat =
                 PNOR::ECC::removeECC(reinterpret_cast<uint8_t*>(l_dataToRead),
                                      reinterpret_cast<uint8_t*>(o_data),
                                      l_eccSize); //logical size of read data

            // create an error if we couldn't correct things
            if( ecc_stat == PNOR::ECC::UNCORRECTABLE )
            {
                TRACFCOMP(g_trac_pnor,"RtPnor::readFromDevice>"
                    " Uncorrectable ECC error : chip=%d,offset=0x%.X",
                    i_procId, i_offset );
                /*@
                 * @errortype
                 * @moduleid    PNOR::MOD_RTPNOR_READFROMDEVICE
                 * @reasoncode  PNOR::RC_UNCORRECTABLE_ECC
                 * @devdesc     UNCORRECTABLE ECC
                 */
                //@todo Add PNOR callout RTC:116145
                l_err = new ERRORLOG::ErrlEntry(
                                    ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                    PNOR::MOD_RTPNOR_READFROMDEVICE,
                                    PNOR::RC_UNCORRECTABLE_ECC,
                                    0, 0, true);
                break;
            }

            // found an error so we need to fix something
            else if( ecc_stat != PNOR::ECC::CLEAN )
            {
                TRACFCOMP(g_trac_pnor,"RtPnor::readFromDevice>"
                      "Correctable ECC error : chip=%d, offset=0x%.X",
                      i_procId, i_offset );
                if (g_hostInterfaces && g_hostInterfaces->pnor_write)
                {

                    //need to write good data back to PNOR
                    int l_rc = g_hostInterfaces->pnor_write(i_procId,
                            l_partitionName,l_offset, l_dataToRead,l_readSize);
                    if (l_rc != static_cast<int>(l_readSize))
                    {
                        TRACFCOMP(g_trac_pnor, "RtPnor::readFromDevice> Error"
                        " writing corrected data back to device");

                        /*@
                         * @errortype
                         * @moduleid   PNOR::MOD_RTPNOR_READFROMDEVICE
                         * @reasoncode PNOR::RC_PNOR_WRITE_FAILED
                         * @userdata1  rc returned from pnor_write
                         * @userdata2  Expected size of write
                         * @devdesc    error writing corrected data back to PNOR
                         * @custdesc   Error accessing system firmware flash
                         */
                        l_err = new ERRORLOG::ErrlEntry(
                                          ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                          PNOR::MOD_RTPNOR_READFROMDEVICE,
                                          PNOR::RC_PNOR_WRITE_FAILED,
                                          l_rc, l_readSize, true);
                        errlCommit(l_err, PNOR_COMP_ID);
                    }
                }
            }
        }
    } while(0);

    if( l_eccBuffer )
    {
        delete[] l_eccBuffer;
    }

    TRACFCOMP(g_trac_pnor, EXIT_MRK"RtPnor::readFromDevice" );
    return l_err;
}

/*********************************************************************/
errlHndl_t RtPnor::writeToDevice( uint64_t i_procId,
                                  PNOR::SectionId i_section,
                                  uint64_t i_offset,
                                  size_t i_size,
                                  bool i_ecc,
                                  void* i_src )
{
    TRACFCOMP(g_trac_pnor, ENTER_MRK"RtPnor::writeToDevice: i_offset=0x%X, "
           "i_procId=%d sec=%d size=0x%X ecc=%d", i_offset, i_procId, i_section,
             i_size, i_ecc);
    errlHndl_t l_err        = nullptr;
    uint8_t*   l_eccBuffer  = nullptr;

    do
    {
        void*  l_dataToWrite      = i_src;
        size_t l_writeSize        = i_size;
        size_t l_writeSizePlusECC = (i_size * 9)/8;
        uint64_t l_offset         = i_offset;

        // apply ECC to data if needed
        if( i_ecc )
        {
            l_eccBuffer = new uint8_t[l_writeSizePlusECC];
            PNOR::ECC::injectECC( reinterpret_cast<uint8_t*>(i_src),
                              l_writeSize,
                              reinterpret_cast<uint8_t*>(l_eccBuffer) );
            l_dataToWrite = reinterpret_cast<void*>(l_eccBuffer);
            l_writeSize = l_writeSizePlusECC;
            l_offset    = (i_offset * 9)/8;
        }

        const char* l_partitionName = SectionIdToString(i_section);
        if (g_hostInterfaces && g_hostInterfaces->pnor_write)
        {
            //make call into opal to write the data
            int l_rc = g_hostInterfaces->pnor_write(i_procId,
                        l_partitionName,l_offset,l_dataToWrite,l_writeSize);
            if (l_rc != static_cast<int>(l_writeSize))
            {
                TRACFCOMP(g_trac_pnor, "RtPnor::writeToDevice: pnor_write failed "
                    "proc:%d, part:%s, offset:0x%X, size:0x%X, dataPt:0x%X,"
                    " rc:%d", i_procId, l_partitionName, l_offset, l_writeSize,
                    l_dataToWrite, l_rc);
                /*@
                 * @errortype
                 * @moduleid            PNOR::MOD_RTPNOR_WRITETODEVICE
                 * @reasoncode          PNOR::RC_PNOR_WRITE_FAILED
                 * @userdata1[00:31]    rc returned from pnor_write
                 * @userdata1[32:63]    section ID
                 * @userdata2[00:31]    offset within the section
                 * @userdata2[32:63]    size of data written in bytes
                 * @devdesc             g_hostInterfaces->pnor_write failed
                 * @custdesc            Error accessing system firmware flash
                 */
                l_err = new ERRORLOG::ErrlEntry(
                             ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                             PNOR::MOD_RTPNOR_WRITETODEVICE,
                             PNOR::RC_PNOR_WRITE_FAILED,
                             TWO_UINT32_TO_UINT64(l_rc, i_section),
                             TWO_UINT32_TO_UINT64(l_offset, l_writeSize),
                             true);
                 break;
            }
            else if( l_rc != static_cast<int>(l_writeSize) )
            {
                TRACFCOMP( g_trac_pnor, "RtPnor::writeToDevice: only read 0x%X bytes, expecting 0x%X", l_rc, l_writeSize );
            }
        }
        else
        {
            TRACFCOMP(g_trac_pnor,"RtPnor::writeToDevice: This version of"
                    " OPAL does not support pnor_write");
            /*@
             * @errortype
             * @moduleid           PNOR::MOD_RTPNOR_WRITETODEVICE
             * @reasoncode         PNOR::RC_PNOR_WRITE_NOT_SUPPORTED
             * @devdesc            g_hostInterfaces->pnor_write not supported
             * @custdesc           Error accessing system firmware flash
             */
            //@todo Add PNOR callout RTC:116145
            l_err = new ERRORLOG::ErrlEntry(
                             ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                             PNOR::MOD_RTPNOR_WRITETODEVICE,
                             PNOR::RC_PNOR_WRITE_NOT_SUPPORTED,
                             0,0,true);
            break;

        }
    } while(0);

    if( l_eccBuffer )
    {
        delete[] l_eccBuffer;
    }

    TRACFCOMP(g_trac_pnor, EXIT_MRK"RtPnor::writeToDevice" );
    return l_err;
}

/*****************************************************************/
errlHndl_t RtPnor::readTOC ()
{
    TRACFCOMP(g_trac_pnor, ENTER_MRK"RtPnor::readTOC" );
    errlHndl_t l_err = nullptr;
    uint8_t* l_toc0Buffer = new uint8_t[PNOR::TOC_SIZE];
    do {
        if (g_hostInterfaces && g_hostInterfaces->pnor_read)
        {
            // offset = 0 means read the entire PNOR::TOC partition
            // This offset is offset into the partition, not offset from the
            // beginning of the flash
            l_err = readFromDevice (iv_masterProcId, PNOR::TOC, 0,
                                    PNOR::TOC_SIZE, false, l_toc0Buffer);
            if (l_err)
            {
                TRACFCOMP(g_trac_pnor,"RtPnor::readTOC:readFromDevice failed"
                          " for TOC0");
                break;
            }

            //Pass along TOC buffer to be parsed, parseTOC will parse through
            // the buffer and store needed information in iv_TOC
            // Note: that Opal should always return a valid TOC
            l_err = PNOR::parseTOC(l_toc0Buffer, iv_TOC);
            if (l_err)
            {
                TRACFCOMP(g_trac_pnor, "RtPnor::readTOC: parseTOC failed");
                break;
            }
        }
    } while (0);

    if(l_toc0Buffer != nullptr)
    {
        delete[] l_toc0Buffer;
    }

    TRACFCOMP(g_trac_pnor, EXIT_MRK"RtPnor::readTOC" );
    return l_err;
}

/***********************************************************/
RtPnor& RtPnor::getInstance()
{
    return Singleton<RtPnor>::instance();
}
/***********************************************************/
errlHndl_t RtPnor::getSideInfo( PNOR::SideId i_side,
                                PNOR::SideInfo_t& o_info)
{
    errlHndl_t l_err = nullptr;

    do {
        // We only support the working side at runtime
        if( i_side != PNOR::WORKING )
        {
            /*@
             * @errortype
             * @moduleid           PNOR::MOD_RTPNOR_GETSIDEINFO
             * @reasoncode         PNOR::RC_INVALID_PNOR_SIDE
             * @userdata1          Requested SIDE
             * @userdata2          0
             * @devdesc            getSideInfo> Side not supported
             * @custdesc           A problem occurred while accessing the boot flash.
             */
            l_err = new ERRORLOG::ErrlEntry(
                             ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                             PNOR::MOD_RTPNOR_GETSIDEINFO,
                             PNOR::RC_INVALID_PNOR_SIDE,
                             TO_UINT64(i_side),
                             0,true);
            break;
        }

        o_info.id = PNOR::WORKING;
        o_info.side = (ALIGN_DOWN_X(iv_TOC[PNOR::HB_BASE_CODE].flashAddr,32*MEGABYTE) == 0)
          ? 'A':'B'; //@fixme TODO RTC:134436
        //iv_side[i].isGolden = (ffsUserData->miscFlags & FFS_MISC_GOLDEN);
        o_info.isGolden = false; //@fixme TODO RTC:134436
        o_info.isGuardPresent = (iv_TOC[PNOR::GUARD_DATA].flashAddr == 0)
          ? false : true;

        o_info.hasOtherSide = false; //@fixme TODO RTC:134436
        o_info.primaryTOC = iv_TOC[PNOR::TOC].flashAddr;
        o_info.backupTOC = 0; //@fixme TODO RTC:134436
        o_info.hbbAddress = iv_TOC[PNOR::HB_BASE_CODE].flashAddr;
        o_info.hbbMmioOffset = 0; //@fixme TODO RTC:134436
    } while(0);

    return l_err;
}


errlHndl_t RtPnor::clearSection(PNOR::SectionId i_section)
{
    TRACFCOMP(g_trac_pnor, "RtPnor::clearSection Section id = %d", i_section);
    errlHndl_t l_errl = nullptr;
    const uint64_t CLEAR_BYTE = 0xFF;
    uint8_t* l_buf = new uint8_t[PAGESIZE]();
    uint8_t* l_eccBuf = nullptr;

    do
    {
        // Flush pages of pnor section we are trying to clear
        l_errl = flush(i_section);
        if (l_errl)
        {
            TRACFCOMP( g_trac_pnor, ERR_MRK"RtPnor::clearSection: flush() failed on section",
                        i_section);
            break;
        }

        // Get PNOR section info
        uint64_t l_address = iv_TOC[i_section].flashAddr;
        uint64_t l_chipSelect = iv_TOC[i_section].chip;
        uint32_t l_size = iv_TOC[i_section].size;
        bool l_ecc = iv_TOC[i_section].integrity & FFS_INTEG_ECC_PROTECT;

        // Number of pages needed to cycle proper ECC
        // Meaning every 9th page will copy the l_eccBuf at offset 0
        const uint64_t l_eccCycleNum = 9;

        // Boundaries for properly splitting up an ECC page for 4K writes.
        // Subtract 1 from l_eccCycleNum because we start writing with offset 0
        // and add this value 8 times to complete a cycle.
        const uint64_t l_sizeOfOverlapSection =
                (PNOR::PAGESIZE_PLUS_ECC - PAGESIZE) /
                (l_eccCycleNum - 1);

        // Create clear section buffer
        memset(l_buf, CLEAR_BYTE, PAGESIZE);

        // apply ECC to data if needed
        if(l_ecc)
        {
            l_eccBuf = new uint8_t[PNOR::PAGESIZE_PLUS_ECC]();
            PNOR::ECC::injectECC( reinterpret_cast<uint8_t*>(l_buf),
                                  PAGESIZE,
                                  reinterpret_cast<uint8_t*>(l_eccBuf) );
            l_size = (l_size*9)/8;
        }

        // Write clear section page to PNOR
        for (uint64_t i = 0; i < l_size; i+=PAGESIZE)
        {
            if(l_ecc)
            {
                // Take (current page) mod (l_eccCycleNum) to get cycle position
                uint8_t l_bufPos = ( (i/PAGESIZE) % l_eccCycleNum );
                uint64_t l_bufOffset = l_sizeOfOverlapSection * l_bufPos;
                memcpy(l_buf, (l_eccBuf + l_bufOffset), PAGESIZE);
            }

            // Set ecc parameter to false to avoid double writes will only write
            // 4k at a time, even if the section is ecc protected.
            l_errl = writeToDevice( iv_masterProcId, i_section,
                                   (l_address + i), l_chipSelect,
                                   false, l_buf);
            if (l_errl)
            {
                TRACFCOMP( g_trac_pnor, ERR_MRK"RtPnor::clearSection: writeToDevice fail: eid=0x%X, rc=0x%X",
                           l_errl->eid(), l_errl->reasonCode());
                break;
            }
        }
        if (l_errl)
        {
            break;
        }
    } while(0);

    // Free allocated memory
    if(l_eccBuf)
    {
        delete[] l_eccBuf;
    }
    delete [] l_buf;

    return l_errl;
}

errlHndl_t RtPnor::getMasterProcId()
{
    errlHndl_t l_err = nullptr;

    do {
    TARGETING::Target* l_masterProc = nullptr;
    l_err = TARGETING::targetService().queryMasterProcChipTargetHandle(l_masterProc);
    if (l_err)
    {
        TRACFCOMP(g_trac_pnor, "RtPnor::getMasterProcId: queryMasterProcChipTargetHandle failed");
        break;
    }
    l_err = RT_TARG::getRtTarget(l_masterProc, iv_masterProcId);
    if (l_err)
    {
        TRACFCOMP(g_trac_pnor, "RtPnor::getMasterProcId: getRtTarget failed for master proc");
        break;
    }
    } while(0);

    return l_err;
}


OpenPOWER on IntegriCloud