summaryrefslogtreecommitdiffstats
path: root/src/usr/vfs/vfsrp.C
blob: 64426f40e2e43fc6ddd73a11bfc4ef40eb7d662b (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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/vfs/vfsrp.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                                                     */
/**
 * @file vfsrp.C
 * @brief Virtual File system Extended image support
 */

#include <limits.h>
#include <string.h>
#include <errno.h>
#include <sys/vfs.h>
#include <vfs/vfs.H>
#include <vfs/vfs_reasoncodes.H>
#include <sys/task.h>
#include <sys/mm.h>
#include <initservice/taskargs.H>
#include <trace/interface.H>
#include "vfsrp.H"
#include <pnor/pnorif.H>
#include <util/align.H>
#include <errl/errludstring.H>
#include <errl/errlmanager.H>
#include <secureboot/service.H>
#include <secureboot/containerheader.H>
#include <kernel/console.H>
#include <config.h>

using namespace VFS;

// Trace definitions
trace_desc_t * g_trac_vfs = NULL;
TRAC_INIT(&g_trac_vfs, VFS_COMP_NAME, KILOBYTE, TRACE::BUFFER_SLOW);

/**
 * setup _start and handle barrier
 */
TASK_ENTRY_MACRO( VfsRp::init );

// ----------------------------------------------------------------------------

VfsRp::~VfsRp()
{
    // Unloading of this module currently not supported.
    // unregister msg queue?  Currently can't do that
    // msg_q_destroy(iv_msgQ);
}

// ----------------------------------------------------------------------------

/**
 * STATIC initializer of vfs resource provider entry point
 */
void VfsRp::init( errlHndl_t &io_taskRetErrl )
{
    errlHndl_t err = NULL;

    err = Singleton<VfsRp>::instance()._init();


    io_taskRetErrl= err ;
}
// ----------------------------------------------------------------------------

/**
 * Helper function to start vfs messge handler
 */
void* VfsRp::msg_handler(void * unused)
{
    Singleton<VfsRp>::instance().msgHandler();
    return NULL;
}

// ----------------------------------------------------------------------------

void * VfsRp::vfsWatcher(void * unused)
{
    Singleton<VfsRp>::instance()._vfsWatcher();
    return NULL;
}

// ----------------------------------------------------------------------------

void* VfsRp::loadUnloadMonitored(void * i_msg)
{
    task_detach();
    Singleton<VfsRp>::instance()._loadUnloadMonitored((msg_t*)i_msg);
    return NULL;
}

// ----------------------------------------------------------------------------

void* VfsRp::loadUnload(void * i_msg)
{
    Singleton<VfsRp>::instance()._loadUnload((msg_t*)i_msg);
    return NULL;
}

// ----------------------------------------------------------------------------

void* VfsRp::execMonitored(void * i_msg)
{
    task_detach();
    Singleton<VfsRp>::instance()._execMonitored((msg_t*)i_msg);
    return NULL;
}

// ----------------------------------------------------------------------------

void* VfsRp::exec(void * i_msg)
{
    Singleton<VfsRp>::instance()._exec((msg_t*)i_msg);
    return NULL;
}
/**
 * Initialze the vfs resource provider
 */
errlHndl_t VfsRp::_init()
{
    errlHndl_t err = NULL;
    size_t rc = 0;

    do {

    iv_msgQ = msg_q_create();
    rc = msg_q_register(iv_msgQ, VFS_ROOT_MSG_VFS);
    assert(rc == 0,"Msg Queue Registration failed in VFS");

#ifdef CONFIG_SECUREBOOT
    err = loadSecureSection(PNOR::HB_EXT_CODE);
    if(err)
    {
        break;
    }
#endif

    // Discover PNOR virtual address of extended image
    PNOR::SectionInfo_t l_pnor_info;
    err = PNOR::getSectionInfo(PNOR::HB_EXT_CODE, l_pnor_info);
    if(err)
    {
        break;
    }

    iv_pnor_vaddr = l_pnor_info.vaddr;
    iv_hbExtSecure = l_pnor_info.secure;
#ifdef CONFIG_SECUREBOOT
    // Assume if HBI is signed it also has a hash page table.
    if (iv_hbExtSecure)
    {
        // store the hash page table offset for reference
        iv_hashPageTableOffset = iv_pnor_vaddr;

        // Extract total protected payload size from header
        iv_protectedPayloadSize = l_pnor_info.secureProtectedPayloadSize;

        // calculate the hash page table size
        iv_hashPageTableSize = iv_protectedPayloadSize - VFS_MODULE_TABLE_SIZE;
        TRACFCOMP(g_trac_vfs, "VfsRp::_init HB_EXT total payload_text_size = 0x%X, hash page table size = 0x%X",
                  l_pnor_info.secureProtectedPayloadSize,
                  iv_hashPageTableSize);
        // skip the hash page table
        iv_pnor_vaddr += iv_hashPageTableSize;

        // Compute offset to the unprotected payload virtual address range.
        // This offset should be subtracted from the secure address
        iv_unprotectedOffset = VMM_VADDR_SPNOR_DELTA+VMM_VADDR_SPNOR_DELTA;
    }
#endif

    rc = mm_alloc_block
        (iv_msgQ,
         (void *)VFS_EXTENDED_MODULE_VADDR,
         ALIGN_PAGE(l_pnor_info.size)
        );
    if(rc == 0)
    {
        // Note: permissions are set elsewhere

        // Start msg_handler task watcher
        //  NOTE: This would be a weak consistancy issues if
        //  task_create were not a system call.
        task_create(VfsRp::vfsWatcher, NULL);
    }
    else
    {
        /*@ errorlog tag
         * @errortype       ERRL_SEV_CRITICAL_SYS_TERM
         * @moduleid        VFS_MODULE_ID
         * @reasoncode      VFS_ALLOC_VMEM_FAILED
         * @userdata1       returncode from mm_alloc_block()
         * @userdata2       Size of memory to allocate
         *
         * @devdesc         Could not allocate virtual memory.
         *
         */
        err = new ERRORLOG::ErrlEntry
            (
             ERRORLOG::ERRL_SEV_CRITICAL_SYS_TERM,   //  severity
             VFS::VFS_MODULE_ID,                     //  moduleid
             VFS::VFS_ALLOC_VMEM_FAILED,             //  reason Code
             rc,                                     //  user1 = rc
             l_pnor_info.size,                       //  user2 = size
             true /*Add HB Software Callout*/
            );
        break;
    }
    } while (0);

    return err;
}


// ----------------------------------------------------------------------------

void VfsRp::_vfsWatcher()
{

    while(1)
    {
        tid_t tid = task_create(VfsRp::msg_handler, NULL);
        assert( tid > 0 );

        int childsts = 0;
        void * childRc = NULL;

        // The msg_handler will only return if there is a problem
        tid_t tidRc = task_wait_tid( tid,
                                     &childsts,
                                     &childRc);

        TRACFCOMP(g_trac_vfs, ERR_MRK
                  "VFS msg_handler crashed. tid:%d status:%d childRc:%p. "
                  "Restarting VFS...",
                  tid, childsts, childRc);

        /*@ errorlog tag
         * @errortype       ERRL_SEV_CRITICAL_SYS_TERM
         * @moduleid        VFS_WATCHER
         * @reasoncode      VFS_TASK_CRASHED
         * @userdata1       tidRc
         * @userdata2       task Rc
         *
         * @devdesc         VFS RP Task crashed.
         * @custdesc  A problem was detected during the IPL of they system:
         *            Task crashed.
         *
         */
        errlHndl_t err = new ERRORLOG::ErrlEntry
            (
             ERRORLOG::ERRL_SEV_CRITICAL_SYS_TERM, // severity
             VFS::VFS_WATCHER,                     // moduleid
             VFS::VFS_TASK_CRASHED,                // reason
             (uint64_t)tidRc,                      // tid rc
             (uint64_t)childRc,                    // child rc
             true
            );

        // Message is only saved in iv_msg for messages from the kernel
        if(iv_msg != NULL)
        {
            if(childRc == NULL) // Critical error not yet reported
            {
                ERRORLOG::errlCommit(err, VFS_COMP_ID);
            }
            else // Crit error already generated
            {
                delete err;
                err = nullptr;
            }

            iv_msg->data[1] = -EIO;  /* I/O error */
            iv_msg->extra_data = childRc;  /* Critical code from PNOR */
            msg_respond(iv_msgQ, iv_msg);
            iv_msg = NULL;
        }
        else //nothing to respond to - just commit error and restar task
        {
            ERRORLOG::errlCommit(err, VFS_COMP_ID);
        }

        // Loop and restart the vfs msg_handler task
    }
}

// ----------------------------------------------------------------------------

void VfsRp::msgHandler()
{
    while(1)
    {
        iv_msg = NULL;
        msg_t* msg = msg_wait(iv_msgQ);

        switch(msg->type)
        {
            case VFS_MSG_LOAD:
                {
                    TRACDCOMP(g_trac_vfs, "Load request: %s",
                              (const char *) msg->data[0]);

                    // run in own task so page faults can be handled
                    task_create(loadUnloadMonitored, msg);
                }
                break;

            case VFS_MSG_UNLOAD:
                {
                    TRACDCOMP(g_trac_vfs, "Unload request: %s",
                              (const char *) msg->data[0]);

                    // run in own task so page faults can be handled
                    task_create(loadUnloadMonitored, msg);

                }
                break;

            case VFS_MSG_EXEC:
                {
                    TRACDCOMP(g_trac_vfs, "EXEC request: %s",
                           (const char*)((msg_t*)msg->data[0])->data[0]);

                    // run in own task so page faults can be handled
                    task_create(execMonitored, msg);

                }
                break;

            case MSG_MM_RP_READ:
                {
                    iv_msg = msg; // save message in case task crashes
                    uint64_t vaddr = msg->data[0]; //page aligned
                    uint64_t paddr = msg->data[1]; //page aligned
                    // Get relative virtual address within VFS space
                    vaddr-=VFS_EXTENDED_MODULE_VADDR;
                    do
                    {
#ifdef CONFIG_SECUREBOOT
                        if (SECUREBOOT::enabled() && iv_hbExtSecure)
                        {
                            errlHndl_t l_errl = verify_page(vaddr);
                            // Failed to pass secureboot verification
                            if(l_errl)
                            {
                                msg->data[1] = -EACCES;
                                SECUREBOOT::handleSecurebootFailure(
                                    l_errl, false, true);
                                break;
                            }
                        }
#endif
                        memcpy((void *)paddr, (void *)(iv_pnor_vaddr
                               -iv_unprotectedOffset+vaddr),
                               PAGE_SIZE);
                        mm_icache_invalidate((void*)paddr,PAGE_SIZE/8);
                        msg->data[1] = 0;
                    } while(0);
                }
                msg_respond(iv_msgQ, msg);
                break;

            case MSG_MM_RP_WRITE:
                assert(0);              // not supported now
                //msg->data[1] = 0;
                //msg_respond(iv_msgQ, msg);
                break;

            case MSG_MM_RP_PERM:
                msg->data[1] = -EINVAL;
                msg_respond(iv_msgQ, msg);
                break;

            default:
                msg_free(msg);
                break;
        }
    } // while(1)
}

// ----------------------------------------------------------------------------

void VfsRp::_loadUnloadMonitored(msg_t * i_msg)
{
    tid_t tid = task_create(VfsRp::loadUnload, i_msg);
    assert( tid > 0 );

    int childsts = 0;
    void * childRc = NULL;

    tid_t tidRc = task_wait_tid( tid,
                                 &childsts,
                                 &childRc);


    if(childsts == TASK_STATUS_CRASHED)
    {
        TRACFCOMP(g_trac_vfs, ERR_MRK
                  "VFS load/unload crashed. tid:%d status:%d childRc:%p",
                  tid, childsts, childRc);

        /*@ errorlog tag
         * @errortype       ERRL_SEV_CRITICAL_SYS_TERM
         * @moduleid        VFS_MODULE_LOAD_MONITOR
         * @reasoncode      VFS_TASK_CRASHED
         * @userdata1       tidRc
         * @userdata2       Task Status
         *
         * @devdesc         VFS Task crashed.
         * @custdesc  A problem was detected during the IPL of they system:
         *            Task crashed.
         */
        errlHndl_t err = new ERRORLOG::ErrlEntry
            (
             ERRORLOG::ERRL_SEV_CRITICAL_SYS_TERM,     // severity
             VFS::VFS_MODULE_LOAD_MONITOR,             // moduleid
             VFS::VFS_TASK_CRASHED,                    // reason Code
             (uint64_t)tidRc,                          // tid rc
             (uint64_t)childsts,                       // task status
             true
            );

        if(childRc != NULL) // crit elog aleady generated
        {
            err->setSev(ERRORLOG::ERRL_SEV_INFORMATIONAL);
        }

        // tell caller that load/unload failed
        i_msg->data[0] = reinterpret_cast<uint64_t>(err);
        msg_respond(iv_msgQ, i_msg);
    }
}

// ----------------------------------------------------------------------------

void VfsRp::_loadUnload(msg_t * i_msg)
{
    errlHndl_t err = NULL;

    // Find VfsSystemModule
    //  The TOC for the extended image sits at the start of the image and is
    //  not location dependent, so just use the one pointed to by iv_pnor_vaddr
    //  to avoid having to copy it to this block
    VfsSystemModule * module =
        vfs_find_module
        (
        (VfsSystemModule *)(iv_pnor_vaddr + VFS_EXTENDED_MODULE_TABLE_OFFSET),
         (const char *) i_msg->data[0]
        );

    if(module)
    {
        int rc = 0;

        // don't want the possibility of a function called in this module
        // until it's completely loaded and inited, so hold
        // off any query or other operation.
        mutex_lock(&iv_mutex);

        ModuleList_t::iterator mod_itr =
            std::find(iv_loaded.begin(),iv_loaded.end(),module);

        if(i_msg->type == VFS_MSG_LOAD)
        {
            if(mod_itr == iv_loaded.end()) // make sure it's not already loaded
            {
                // Set mem access parms
                rc = vfs_module_perms(module);
                if(!rc)
                {
                    iv_loaded.push_back(module);

                    if(module->init)
                    {
                        (module->init)(NULL);
                    }

                }
            }
        }
        else // unload
        {
            if(mod_itr != iv_loaded.end()) // Loaded
            {
                iv_loaded.erase(mod_itr);

                if(module->fini)
                {
                    (module->fini)(NULL);
                }

                rc = mm_set_permission(module->text,
                                       ALIGN_PAGE(module->byte_count),
                                       NO_ACCESS);

                rc = mm_remove_pages(RELEASE,
                                     module->text,
                                     ALIGN_PAGE(module->byte_count));
            }
            // else module was not loaded
        }

        mutex_unlock(&iv_mutex);

        if(rc)
        {
            /*@ errorlog tag
             * @errortype       ERRL_SEV_CRITICAL_SYS_TERM
             * @moduleid        VFS_MODULE_ID
             * @reasoncode      VFS_PERMS_VMEM_FAILED
             * @userdata1       returncode from mm_set_permission()
             * @userdata2       message type (LOAD or UNLOAD)
             *
             * @devdesc         Could not set permissions on virtual memory.
             *
             */
            err = new ERRORLOG::ErrlEntry
                (
                 ERRORLOG::ERRL_SEV_CRITICAL_SYS_TERM,   //  severity
                 VFS::VFS_MODULE_ID,                     //  moduleid
                 VFS_PERMS_VMEM_FAILED,                  //  reason Code
                 rc,                                     //  user1 = rc
                 i_msg->type,                            //  user2
                 true /*Add HB Software Callout*/ );
        }
    }
    else
    {
        // Module does not exist in extended image.
        // If it exists then it is in the base image and it's already
        // initialized, however, we still should not be here, so put out a
        // trace msg;
        // If it does not exist anywhere then also create an error log
        TRACFCOMP(g_trac_vfs, ERR_MRK"load Module not found: %s",
                 (const char *) i_msg->data[0]);

        uint64_t * name = (uint64_t*)i_msg->data[0];

        if(!module_exists((const char *)i_msg->data[0]))
        {
            /*@ errorlog tag
             * @errortype       ERRL_SEV_CRITICAL_SYS_TERM
             * @moduleid        VFS_MODULE_ID
             * @reasoncode      VFS_MODULE_DOES_NOT_EXIST
             * @userdata1       first 8 bytes of module name
             * @userdata2       next 8 bytes of module name
             *
             * @devdesc         Requested module does not exist.
             *
             */
            err = new ERRORLOG::ErrlEntry
                (
                 ERRORLOG::ERRL_SEV_CRITICAL_SYS_TERM,   //  severity
                 VFS::VFS_MODULE_ID,                     //  moduleid
                 VFS_MODULE_DOES_NOT_EXIST,              //  reason Code
                 name[0],
                 name[1]
                );
            err->addProcedureCallout( HWAS::EPUB_PRC_HB_CODE,
                                      HWAS::SRCI_PRIORITY_HIGH );
            err->addProcedureCallout( HWAS::EPUB_PRC_SP_CODE,
                                      HWAS::SRCI_PRIORITY_LOW );
        }
    }
    i_msg->data[0] = (uint64_t) err;
    msg_respond(iv_msgQ, i_msg);
}

// ----------------------------------------------------------------------------

void VfsRp::_execMonitored(msg_t * i_msg)
{
    tid_t tid = task_create(VfsRp::exec, i_msg);
    assert( tid > 0 );

    int childsts = 0;
    void * childRc = NULL;

    tid_t tidRc = task_wait_tid( tid,
                                 &childsts,
                                 &childRc);

    if(childsts == TASK_STATUS_CRASHED)
    {
        TRACFCOMP(g_trac_vfs, ERR_MRK
                  "VFS exec crashed. tid:%d status:%d childRc:%p",
                  tid, childsts, childRc);

        /*@ errorlog tag
         * @errortype       ERRL_SEV_CRITICAL_SYS_TERM
         * @moduleid        VFS_MODULE_EXEC_MONITOR
         * @reasoncode      VFS_TASK_CRASHED
         * @userdata1       tidRc
         * @userdata2       task Rc
         *
         * @devdesc         VFS Task crashed.
         * @custdesc  A problem was detected during the IPL of they system:
         *            Task crashed.
         *
         */
        errlHndl_t err = new ERRORLOG::ErrlEntry
            (
             ERRORLOG::ERRL_SEV_CRITICAL_SYS_TERM,     // severity
             VFS::VFS_MODULE_EXEC_MONITOR,             // moduleid
             VFS::VFS_TASK_CRASHED,                    // reason Code
             (uint64_t)tidRc,                          // tid rc
             (uint64_t)childRc,                        // child rc
             true
            );

        if(childRc != NULL) // crit elog aleady generated
        {
            err->setSev(ERRORLOG::ERRL_SEV_INFORMATIONAL);
        }

        // tell caller that exec/startup failed
        i_msg->data[0] = reinterpret_cast<uint64_t>(err);
        msg_respond(iv_msgQ, i_msg);
    }
}

// ----------------------------------------------------------------------------

void VfsRp::_exec(msg_t * i_msg)
{
    msg_t * msg1 = (msg_t *) i_msg->data[0];

    //  The TOC for the extended image sits at the start of the image and is
    //  not location dependent, so just use the one pointed to by iv_pnor_vaddr
    //  to avoid having to copy it to this block
    VfsSystemModule * module =
        vfs_find_module((VfsSystemModule *)(iv_pnor_vaddr +
                                            VFS_EXTENDED_MODULE_TABLE_OFFSET),
                        (const char*) msg1->data[0]);

    msg_q_t vfsRmsgQ = (msg_q_t) i_msg->data[1];

    msg1->data[0] = (uint64_t) vfs_start_entrypoint(module);

    msg_respond(vfsRmsgQ,msg1);
    msg_free(i_msg);
}

// ----------------------------------------------------------------------------

bool VfsRp::module_exists(const char * i_name) const
{
    bool result = false;
    VfsSystemModule * module = vfs_find_module(VFS_MODULES,i_name);
    if(!module)
    {
        module = vfs_find_module((VfsSystemModule *)(iv_pnor_vaddr +
                                            VFS_EXTENDED_MODULE_TABLE_OFFSET),
                                 i_name);
    }
    if(module) result = true;
    return result;
}

// ----------------------------------------------------------------------------

const VfsSystemModule * VfsRp::get_vfs_info(const char * i_name) const
{
    return vfs_find_module((VfsSystemModule *)(iv_pnor_vaddr +
                                             VFS_EXTENDED_MODULE_TABLE_OFFSET),
                           i_name);
}

// ----------------------------------------------------------------------------

const char * VfsRp::get_name_from_address(const void * i_vaddr) const
{
    const char * result = NULL;

    VfsSystemModule * module = vfs_find_address
        ((VfsSystemModule *)(iv_pnor_vaddr + VFS_EXTENDED_MODULE_TABLE_OFFSET),
         i_vaddr);

    if(!module) // look in the base modules
    {
        module = vfs_find_address(VFS_MODULES,i_vaddr);
    }
    if(module)
    {
        result = module->module;
    }
    return result;
}

// ----------------------------------------------------------------------------

bool VfsRp::is_module_loaded(const char * i_name)
{
    bool result = false;
    const VfsSystemModule * module = get_vfs_info(i_name);
    if(module)
    {
        mutex_lock(&iv_mutex);
        ModuleList_t::const_iterator i =
            std::find(iv_loaded.begin(),iv_loaded.end(),module);
        if(i != iv_loaded.end())
        {
            result = true;
        }
        mutex_unlock(&iv_mutex);
    }
    if(!result)  // look in the base
    {
        module = vfs_find_module(VFS_MODULES,i_name);
        if(module)
        {
            // all base modules are always loaded
            result = true;
        }
    }

    return result;
}

// ----------------------------------------------------------------------------

void VfsRp::get_test_modules(std::vector<const char *> & o_list) const
{
    o_list.clear();
    o_list.reserve(32);

    VfsSystemModule * vfsItr =
        (VfsSystemModule *) (iv_pnor_vaddr + VFS_EXTENDED_MODULE_TABLE_OFFSET);

    //TRACDCOMP(g_trac_vfs,"finding test modules...");

    while(vfsItr->module[0] != '\0')
    {
        if (0 == memcmp(vfsItr->module, "libtest", 7))
        {
            if (NULL != vfsItr->start)
            {
                //TRACDCOMP( g_trac_vfs, "%s",vfsItr->module);
                o_list.push_back(vfsItr->module);
            }
        }
        vfsItr++;
    }
}


errlHndl_t VfsRp::verify_page(uint64_t i_vaddr, uint64_t i_baseOffset,
                              uint64_t i_hashPageTableOffset) const
{
    errlHndl_t l_errl = nullptr;
    uint64_t l_pnorVaddr = iv_pnor_vaddr-iv_unprotectedOffset+i_vaddr;

    // Get current hash page table entry
    TRACDCOMP(g_trac_vfs, "VfsRp::verify_page Current Page vaddr = 0x%llX, index = %d, bin file offset = 0x%llX",
             i_vaddr,
             getHashPageTableIndex(i_vaddr,i_baseOffset),
             i_vaddr+PAGE_SIZE+iv_protectedPayloadSize);
    PAGE_TABLE_ENTRY_t* l_pageTableEntry = getHashPageTableEntry(i_vaddr,
                                                        i_baseOffset,
                                                        i_hashPageTableOffset);

    // Get previous hash page table entry
    uint64_t l_prevPage = i_vaddr - PAGE_SIZE;
    TRACDCOMP(g_trac_vfs, "VfsRp::verify_page Prev Page vaddr = 0x%llX, index = %d, bin file offset = 0x%llX",
             l_prevPage,
             getHashPageTableIndex(l_prevPage,i_baseOffset),
             l_prevPage+PAGE_SIZE+iv_protectedPayloadSize);
    PAGE_TABLE_ENTRY_t* l_prevPageTableEntry = getHashPageTableEntry(
                                                        l_prevPage,
                                                        i_baseOffset,
                                                        i_hashPageTableOffset);

    // Concatenate previous page table entry with current page data
    std::vector< std::pair<void*,size_t> > l_blobs;
    l_blobs.push_back(std::make_pair<void*,size_t>(l_prevPageTableEntry,
                                                   HASH_PAGE_TABLE_ENTRY_SIZE));
    l_blobs.push_back(std::make_pair<void*,size_t>(
                                        reinterpret_cast<void*>(l_pnorVaddr),
                                        PAGE_SIZE));
    SHA512_t l_curPageHash = {0};
    SECUREBOOT::hashConcatBlobs(l_blobs, l_curPageHash);

    // Compare existing hash page table entry with the derived one.
    if (memcmp(l_pageTableEntry,l_curPageHash,HASH_PAGE_TABLE_ENTRY_SIZE) != 0)
    {
        TRACFCOMP(g_trac_vfs, "ERROR:>VfsRp::verify_page secureboot verify fail on vaddr 0x%llX, offset into HBI 0x%llX",
                              i_vaddr,
                              i_vaddr+PAGE_SIZE+iv_protectedPayloadSize);
        /*@
         * @severity        ERRL_SEV_CRITICAL_SYS_TERM
         * @moduleid        VFS_VERIFY_PAGE
         * @reasoncode      VFS_PAGE_VERIFY_FAILED
         * @userdata1       Kernel RC
         * @userdata2       virtual address accessed
         *
         * @devdesc         Secureboot page verify failure.
         * @custdesc        Corrupted flash image or firmware error during system boot
         */
        l_errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_CRITICAL_SYS_TERM,
                                         VFS_VERIFY_PAGE,
                                         VFS_PAGE_VERIFY_FAILED,
                                         TO_UINT64(EACCES),
                                         i_vaddr,
                                         true);
        l_errl->collectTrace(VFS_COMP_NAME);
        l_errl->collectTrace(PNOR_COMP_NAME);
    }

    return l_errl;
}

VfsRp& VfsRp::getInstance()
{
    return Singleton<VfsRp>::instance();
}

// -- External interface ------------------------------------------------------

errlHndl_t VFS::module_load_unload(const char * i_module, VfsMessages i_msgtype)
{
    errlHndl_t err = NULL;
    msg_q_t vfsQ = msg_q_resolve(VFS_ROOT_MSG_VFS);
    msg_t* msg = msg_allocate();
    msg->type = i_msgtype;
    msg->data[0] = (uint64_t) i_module;
    int rc = msg_sendrecv(vfsQ, msg);

    if (0 == rc)
    {
        err = (errlHndl_t) msg->data[0];
    }
    else
    {
        /*@ errorlog tag
         * @errortype       ERRL_SEV_CRITICAL_SYS_TERM
         * @moduleid        VFS_MODULE_ID
         * @reasoncode      VFS_LOAD_FAILED
         * @userdata1       returncode from msg_sendrecv()
         * @userdata2       VfsMessages type [LOAD | UNLOAD]
         *
         * @devdesc         Could not load/unload module.
         *
         */
        err = new ERRORLOG::ErrlEntry
            (
             ERRORLOG::ERRL_SEV_CRITICAL_SYS_TERM,   //  severity
             VFS::VFS_MODULE_ID,                     //  moduleid
             VFS::VFS_LOAD_FAILED,                   //  reason Code
             rc,                                     //  user1 = msg_sendrecv rc
             i_msgtype,                              //  user2 = message type
             true /*Add HB Software Callout*/
            );
    }

    msg_free(msg);
    return err;
}

// ----------------------------------------------------------------------------

void VFS::find_test_modules(std::vector<const char *> & o_list)
{
    Singleton<VfsRp>::instance().get_test_modules(o_list);
}

// ----------------------------------------------------------------------------

bool VFS::module_exists(const char * i_name)
{
    return Singleton<VfsRp>::instance().module_exists(i_name);
}

// -----------------------------------------------------------------------------

errlHndl_t VFS::module_address(const char * i_name, const char *& o_address, size_t & o_size)
{
    errlHndl_t err = NULL;
    o_address = NULL;
    o_size = 0;

    const VfsSystemModule * vfs = Singleton<VfsRp>::instance().get_vfs_info(i_name);
    if(!vfs || (vfs->text != vfs->data))
    {
        // module not found or is not a data module
        uint64_t name[2] = { 0, 0 };
        strncpy( reinterpret_cast<char*>(name), i_name, sizeof(uint64_t)*2 );
        /*@ errorlog tag
         * @errortype       ERRL_SEV_INFORMATIONAL
         * @moduleid        VFS_MODULE_ID
         * @reasoncode      VFS_INVALID_DATA_MODULE
         * @userdata1       First 8 bytes of module name
         * @userdata2       Next 8 bytes of module name
         *
         * @devdesc         Module is not a data module
         *
         */
        err = new ERRORLOG::ErrlEntry
            (
             ERRORLOG::ERRL_SEV_INFORMATIONAL,       //  severity
             VFS::VFS_MODULE_ID,                     //  moduleid
             VFS::VFS_INVALID_DATA_MODULE,           //  reason Code
             name[0],
             name[1],
             true /*Add HB Software Callout*/
            );
        ERRORLOG::ErrlUserDetailsString(i_name).addToLog(err);
    }
    else
    {
        o_address = (const char *)vfs->data;
        o_size = vfs->byte_count;
    }
    return err;
}

const char * VFS::module_find_name(const void * i_vaddr)
{
    return Singleton<VfsRp>::instance().get_name_from_address(i_vaddr);
}

bool VFS::module_is_loaded(const char * i_name)
{
    return Singleton<VfsRp>::instance().is_module_loaded(i_name);
}
OpenPOWER on IntegriCloud