summaryrefslogtreecommitdiffstats
path: root/src/usr/diag/prdf/common/rule/prdrLoadChipCache.C
blob: 7ffd656032809085c1d15d641cf5bc77b9b70a2f (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/diag/prdf/common/rule/prdrLoadChipCache.C $           */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2016                             */
/* [+] 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                                                     */

#ifndef __HOSTBOOT_MODULE

#include <utilreg.H> // for UtilReg

#endif


#include <string.h>                // for strncat
#include <prdrLoadChipCache.H>
#include <utilfile.H>
#include <prdfTrace.H>

namespace Prdr
{
    // Declare private member instance.
    LoadChipCache::Cache_t LoadChipCache::cv_cache;

    //---------------------------------------------------------------------
    void LoadChipCache::flushCache()
    {
        // Delete all objects within the cache.
        for (Cache_t::iterator i = cv_cache.begin();
             i != cv_cache.end();
             ++i)
        {
            if (NULL != i->second)
                delete (Chip*)i->second;
        }

        // Clear map.
        cv_cache.clear();
    };
    //---------------------------------------------------------------------

    //---------------------------------------------------------------------
    errlHndl_t LoadChipCache::loadChip(const char * i_file,
                                       Chip ** o_chip)
    {
        errlHndl_t l_errl = NULL;
        *o_chip = NULL;

        Cache_t::iterator i = cv_cache.find(i_file);

        if (cv_cache.end() != i) // Found object in cache.
        {
            (*o_chip) = (Chip*)(*i).second;
            l_errl = NULL;
        }
        else
        {
            (*o_chip) = new Chip();

            do
            {
                // NOTE: to patch PRF files require rebuilding
                // entire Hostboot image and put in a special
                // location on FSP /nfs/test/pnor/ mount.

                const char * ext = ".prf";

                const size_t sz_file     = strlen( i_file );
                const size_t sz_ext      = strlen( ext );
                const size_t sz_filePath = sz_file + sz_ext;

                char filePath[sz_filePath + 1];
                memset( filePath, '\0', sizeof(filePath) );

                strncpy( filePath, i_file, sz_file );
                strncat( filePath, ext,    sz_ext  );

                #ifdef __HOSTBOOT_MODULE

                UtilFile l_ruleFile( filePath );
                if ( !l_ruleFile.exists() )
                {
                    PRDF_ERR( "LoadChipCache::loadChip() failed to find %s",
                              filePath );
                }
                else
                {
                    l_ruleFile.Open("r");
                }

                #else // not __HOSTBOOT_MODULE

                // Read the correct directory path for flash.
                const char * prdPath = "prdf/";
                const size_t sz_prdPath = strlen( prdPath );

                size_t sz_rootPath = 256;
                char rootPath[ sz_rootPath + sz_prdPath + sz_filePath + 1 ];
                memset( rootPath, '\0', sizeof(rootPath) );

                l_errl = UtilReg::read( "fstp/RO_Root", (void *)rootPath,
                                        sz_rootPath );
                if ( NULL != l_errl ) break;

                strncat( rootPath, prdPath,  sz_prdPath  );
                strncat( rootPath, filePath, sz_filePath );

                // Read /maint/data/... directory path for any prf file patch.
                const char * maintPath = "/maint/data/prdf/";
                const size_t sz_maintPath = strlen( maintPath );

                char patchPath[ sz_maintPath + sz_filePath + 1 ];
                memset( patchPath, '\0', sizeof(patchPath) );

                strncpy( patchPath, maintPath, sz_maintPath );
                strncat( patchPath, filePath,  sz_filePath );

                // Open File to read chip.
                UtilFile l_ruleFile( patchPath );
                if ( !l_ruleFile.exists() ) // check for patch file.
                {
                    l_ruleFile.Open(rootPath, "r");
                }
                else
                {
                    l_ruleFile.Open("r");
                }

                #endif // end __HOSTBOOT_MODULE

                // Load chip object.
                l_errl = LoadChip(l_ruleFile, *(*o_chip));

            } while (0);

            if (NULL == l_errl)
            {
                // Add chip object to the cache.
                cv_cache[i_file] = *o_chip;
            }
            else
            {
                PRDF_ERR("LoadChipCache::loadChip() l_errl is not null!");
                delete *o_chip;
                (*o_chip) = NULL;
            }

        }

        return l_errl;

    };
    //---------------------------------------------------------------------

} // end namespace Prdr
OpenPOWER on IntegriCloud