summaryrefslogtreecommitdiffstats
path: root/src/usr/hdat/hdathdif.C
blob: 329a1bd37576337e28863e86e7cf2839441e433f (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/hdat/hdathdif.C $                                     */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2016,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 hdathdif.C
 *
 * @brief This file contains the implementation of the HdatHdif class.
 *
 */


/*----------------------------------------------------------------------------*/
/* Includes                                                                   */
/*----------------------------------------------------------------------------*/
#include "hdathdif.H"
#include "hdatutil.H"

namespace HDAT
{

/*----------------------------------------------------------------------------*/
/* Global variables                                                           */
/*----------------------------------------------------------------------------*/
extern trace_desc_t * g_hdatTraceDesc;


/*----------------------------------------------------------------------------*/
/* Constants                                                                  */
/*----------------------------------------------------------------------------*/
const uint32_t HDAT_BOUNDARY = 16; // Pad structures to a 16 byte boundary
const char HDAT_PAD[15] = {'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
                           '\0', '\0', '\0', '\0', '\0', '\0', '\0'};

const int32_t HDAT_SUCCESS = 0;
const int32_t HDAT_FAILURE = -1;

/** @brief No children for a data structure*/
const uint32_t HDAT_NO_CHILD = 0;

/** @brief FFDC Version to classify data in error log */
const uint8_t HDAT_VERSION1 = 1;

/** @brief FFDC Sub Version to classify data in error log */
const uint8_t HDAT_PCIA_FFDC_SUBSEC = 1;
const uint8_t HDAT_NACA_FFDC_SUBSEC1 = 2;
const uint8_t HDAT_NACA_FFDC_SUBSEC2 = 3;

const uint32_t HDAT_REAL_ADDRESS_MASK = 0x80000000;
const uint64_t HDAT_REAL_ADDRESS_MASK64 = 0x8000000000000000ull;


/** @brief See the prologue in hdathdif.H
 */
HdatHdif::HdatHdif()
{
}


/** @brief See the prologue in hdathdif.H
 */
HdatHdif::HdatHdif(errlHndl_t &o_errlHndl,
                  const char i_eyeCatcher[],
                  uint32_t i_dataPtrCnt,
                  uint32_t i_instance,
                  uint32_t i_childCnt,
                  uint32_t i_ver)
:iv_totalSize(0), iv_padSize(0),iv_siblingPadSize(0),iv_dataPtrSize(0),
iv_childPtrSize(0),iv_dataPtrs(NULL), iv_childPtrs(NULL)
{
    HDAT_ENTER();

    const uint32_t HDAT_MULTIPLE = 16;
    uint32_t l_mod;

    o_errlHndl = NULL;

    iv_hdr.hdatStructId       = HDAT_HDIF_STRUCT_ID;
    iv_hdr.hdatInstance       = i_instance;
    iv_hdr.hdatVersion        = i_ver;
    iv_hdr.hdatSize           = sizeof(hdatHDIF_t);
    iv_hdr.hdatHdrSize        = sizeof(hdatHDIF_t);
    iv_hdr.hdatDataPtrOffset  = 0;
    iv_hdr.hdatDataPtrCnt     = 0;
    iv_hdr.hdatChildStrOffset = 0;
    iv_hdr.hdatChildStrCnt    = 0;

    memcpy(iv_hdr.hdatStructName, i_eyeCatcher, sizeof(iv_hdr.hdatStructName));

    if (i_dataPtrCnt > 0)
    {
        iv_hdr.hdatDataPtrOffset = iv_hdr.hdatSize;
        iv_dataPtrSize = i_dataPtrCnt * sizeof(hdatHDIFDataHdr_t);
        iv_dataPtrs = reinterpret_cast<hdatHDIFDataHdr_t *>(calloc(i_dataPtrCnt,
                                            sizeof(hdatHDIFDataHdr_t)));
        iv_hdr.hdatSize += iv_dataPtrSize;
    }

    if (NULL == o_errlHndl && i_childCnt > 0)
    {
        iv_hdr.hdatChildStrOffset = iv_hdr.hdatSize;

        iv_childPtrSize = i_childCnt * sizeof(hdatHDIFChildHdr_t);
        l_mod = iv_childPtrSize % HDAT_MULTIPLE;
        if (l_mod > 0)
        {
            iv_childPtrSize += HDAT_MULTIPLE - l_mod;
        }

        iv_childPtrs = reinterpret_cast<hdatHDIFChildHdr_t *>
                          (calloc(iv_childPtrSize, 1));
        iv_hdr.hdatSize += iv_childPtrSize;
    }

    iv_totalSize = iv_hdr.hdatSize;
    HDAT_EXIT();
}//end constructor

/** @brief See the prologue in hdathdif.H
 */
HdatHdif::~HdatHdif()
{
    HDAT_ENTER();
    free(iv_dataPtrs);
    free(iv_childPtrs);
    HDAT_EXIT();
}


/** @brief See the prologue in hdathdif.H
 */
uint32_t HdatHdif::size()
{
    return iv_hdr.hdatSize;
}
uint32_t HdatHdif::getSize()
{
    uint32_t l_size = 0;

    l_size += sizeof(iv_hdr);

    if (NULL != iv_dataPtrs)
    {
        l_size += iv_dataPtrSize;
    }

    if (NULL != iv_childPtrs)
    {
        l_size += iv_childPtrSize;
    }
    return l_size;
}
/** @brief See the prologue in hdathdif.H
 */
void HdatHdif::chgChildOffset(uint32_t i_whichChildEntry,
                                   uint32_t i_offset)
{
    hdatHDIFChildHdr_t *l_hdr;

    // If there are child structures, change the offset of the selected triple 
    // entry
    if (iv_hdr.hdatChildStrCnt > 0)
    {
        l_hdr = reinterpret_cast<hdatHDIFChildHdr_t *>(reinterpret_cast<char *>
                                   (iv_childPtrs) + sizeof(hdatHDIFChildHdr_t) *
                                   i_whichChildEntry);
        l_hdr->hdatOffset = i_offset;
    }

    return;
}



/** @brief See the prologue in hdathdif.H
 */
void HdatHdif::addChild(uint32_t i_whichChildEntry,
                           uint32_t i_size,
                           uint32_t i_numChildStr)
{
    HDAT_ENTER();

    hdatHDIFChildHdr_t *l_childHdr;

    iv_hdr.hdatChildStrCnt = i_whichChildEntry + 1; //(0 based index plus 1)

    // Address the child entry
    l_childHdr = reinterpret_cast<hdatHDIFChildHdr_t *>(reinterpret_cast<char *>
                  (iv_childPtrs) +
                  sizeof(hdatHDIFChildHdr_t) * i_whichChildEntry);

    // Update child entry information

    // If not yet done, set offset to first child structure.  Child structures
    // are contiguous after the first and are all the same size.
    if (0 == l_childHdr->hdatOffset)
    {
        l_childHdr->hdatOffset = iv_totalSize;
        l_childHdr->hdatSize   = i_size;
    }
    l_childHdr->hdatCnt++;

    // Increment total object size by child's size
    iv_totalSize += i_size;

    HDAT_EXIT();
    return;
}


/** @brief See the prologue in hdathdif.H
 */
void HdatHdif::addGrandChild(uint32_t i_size)
{

    // Increment total object size by the grandchild's size
    iv_totalSize += i_size;

    return;
}


/** @brief See the prologue in hdathdif.H
 */
void HdatHdif::addData(uint32_t i_whichDataEntry,
                       uint32_t i_size)
{
    HDAT_ENTER();

    hdatHDIFDataHdr_t *l_dataHdr;

    l_dataHdr = reinterpret_cast<hdatHDIFDataHdr_t *>(reinterpret_cast<char *>
               (iv_dataPtrs) +
               sizeof(hdatHDIFDataHdr_t) * i_whichDataEntry);

    // Update data entry information if there is any data
    if (i_size > 0)
    {
        l_dataHdr->hdatOffset = iv_hdr.hdatSize;
        l_dataHdr->hdatSize   = i_size;
    }
    // Increment total object size by data's size and increment
    // count of data entries
    iv_hdr.hdatSize += i_size;
    iv_totalSize += i_size;
    iv_hdr.hdatDataPtrCnt++;

    HDAT_EXIT();
    return;
}


/** @brief See the prologue in hdathdif.H
 */
void HdatHdif::maxSiblingSize(uint32_t i_numBytes)
{

    if (i_numBytes > iv_hdr.hdatSize)
    {
        iv_siblingPadSize = i_numBytes - iv_hdr.hdatSize;
        iv_hdr.hdatSize += iv_siblingPadSize;
        iv_totalSize += iv_siblingPadSize;
    }

}




/** @brief See the prologue in hdathdif.H
 */
void HdatHdif::align()
{
     uint32_t l_rem;

    // If the structure length is not a multiple of the boundary
    // requirement, compute the number of pad bytes needed
    l_rem = iv_hdr.hdatSize % HDAT_BOUNDARY;

    if (l_rem > 0)
    {
        iv_padSize = HDAT_BOUNDARY - l_rem;
        iv_hdr.hdatSize += iv_padSize;
        iv_totalSize += iv_padSize;
    }

    return;
}

uint8_t * HdatHdif::setHdif(uint8_t * io_virt_addr)
{
    HDAT_DBG("virtual address=0x%016llX",
            (uint64_t)io_virt_addr);

    uint8_t *l_temp = NULL;

    // If no children were ever added, ensure offset to child pointers is 0
    if (0 == this->iv_hdr.hdatChildStrCnt)
    {
        HDAT_DBG(" no child information were addded");
        this->iv_hdr.hdatChildStrOffset = 0;
    }

    //first write the hdatHDIF_t iv_hdr
    hdatHDIF_t * l_hdatHDIF= reinterpret_cast<hdatHDIF_t *>(io_virt_addr);


    l_hdatHDIF->hdatStructId = this->iv_hdr.hdatStructId;
    l_hdatHDIF->hdatInstance = this->iv_hdr.hdatInstance;
    l_hdatHDIF->hdatVersion = this->iv_hdr.hdatVersion;
    l_hdatHDIF->hdatSize = this->iv_hdr.hdatSize;
    l_hdatHDIF->hdatHdrSize = this->iv_hdr.hdatHdrSize;
    l_hdatHDIF->hdatDataPtrOffset = this->iv_hdr.hdatDataPtrOffset;
    l_hdatHDIF->hdatDataPtrCnt = this->iv_hdr.hdatDataPtrCnt;
    l_hdatHDIF->hdatChildStrCnt = this->iv_hdr.hdatChildStrCnt;
    l_hdatHDIF->hdatChildStrOffset = this->iv_hdr.hdatChildStrOffset;

    memcpy(l_hdatHDIF->hdatStructName,iv_hdr.hdatStructName,
           sizeof(iv_hdr.hdatStructName));

    //cast back to uint8_t and increment the pointer by size of
    //hdatHDIF_t to point the next

    l_temp = reinterpret_cast<uint8_t *>(l_hdatHDIF);
    l_temp += this->iv_hdr.hdatDataPtrOffset;

    HDAT_DBG("sizeof HDIF header=%x",sizeof(hdatHDIF_t));

    //write data pointer array hdatHDIFDataHdr_t   *iv_dataPtrs
    hdatHDIFDataHdr_t *l_hdatHDIFDataHdr =
                       reinterpret_cast<hdatHDIFDataHdr_t *>(l_temp);

    HDAT_DBG("writing Data pointers array from address=0x%016llX",
             (uint64_t)l_hdatHDIFDataHdr);

    //total number of data pointer header is HDAT_PARENT_LAST,
    //actual filled up number is
    //this->iv_hdr.hdatDataPtrCnt, but we should loop through the total number
    //to fillup the memory and increment the pointer

    uint8_t l_totdataHdrCnt = this->iv_dataPtrSize / sizeof(hdatHDIFDataHdr_t);

    for (uint8_t l_cnt=0; l_cnt < l_totdataHdrCnt; l_cnt++)
    {
        l_hdatHDIFDataHdr->hdatOffset = this->iv_dataPtrs[l_cnt].hdatOffset;
        l_hdatHDIFDataHdr->hdatSize = this->iv_dataPtrs[l_cnt].hdatSize;

        HDAT_DBG("wrote data array %d, at address 0x%016llX",
                 l_cnt,(uint64_t)l_hdatHDIFDataHdr);
        l_hdatHDIFDataHdr++;
    }


    //write Child pointers array
    hdatHDIFChildHdr_t *l_hdatHDIFChildHdr =
                      reinterpret_cast<hdatHDIFChildHdr_t *>(l_hdatHDIFDataHdr);

      //saving the start value
    uint8_t *l_tempChildHdr = reinterpret_cast<uint8_t *>(l_hdatHDIFChildHdr);

    HDAT_DBG("writing Child pointers array from address=0x%016llX",
             (uint64_t)l_hdatHDIFChildHdr);
    //total count is HDAT_CHILD_LAST but actual data is hdatChildStrCnt
    //but we need to copy the full to increase the memory pointer
    uint8_t l_totChldHdrCnt =
            this->iv_childPtrSize / sizeof(hdatHDIFChildHdr_t);


    for(uint8_t l_cnt = 0; l_cnt < l_totChldHdrCnt; l_cnt++)
    {
        l_hdatHDIFChildHdr->hdatOffset = this->iv_childPtrs[l_cnt].hdatOffset;
        l_hdatHDIFChildHdr->hdatSize = this->iv_childPtrs[l_cnt].hdatSize;
        l_hdatHDIFChildHdr->hdatCnt = this->iv_childPtrs[l_cnt].hdatCnt;

        HDAT_DBG("wrote child array %d, at address 0x%016llX",
                  l_cnt,(uint64_t)l_hdatHDIFChildHdr);
        l_hdatHDIFChildHdr++;
    }


    l_tempChildHdr += iv_childPtrSize;

    io_virt_addr = reinterpret_cast<uint8_t *>(l_tempChildHdr);

    return io_virt_addr;

}//end setHdif


uint8_t * HdatHdif::setpadding(uint8_t * io_virt_addr,
                               uint32_t &o_size)
{

    HDAT_DBG("address=0x%016llX",
             (uint64_t)io_virt_addr);
    uint32_t i = 0;

    if (this->iv_padSize > 0)
    {
        HDAT_DBG("adding iv_padSize=0x%x",iv_padSize);
        for ( i = 0; i < iv_padSize; i++)
        {
            io_virt_addr[i] = '\0';
        }
    }


    if (iv_siblingPadSize > 0)
    {
        HDAT_DBG("adding iv_siblingPadSize=0x%x",
                  iv_siblingPadSize);
        for ( i = 0; i < iv_siblingPadSize; i++)
        {
            io_virt_addr[i] = '\0';
        }
    }

    io_virt_addr += iv_padSize + iv_siblingPadSize;
    o_size = iv_totalSize;

    HDAT_DBG("exit address=0x%016llX",
             (uint64_t)io_virt_addr);

    return io_virt_addr;
}




uint32_t HdatHdif::getChildOffset()
{
    if ( this->iv_childPtrs )
    {
        return this->iv_childPtrs[0].hdatOffset;
    }
    else
    {
        return 0;
    }

}//end getChildOffset
/** @brief See the prologue in hdathdif.H
 */
void HdatHdif::startCommit(UtilMem &i_data)
{

    // If no children were ever added, ensure offset to child pointers
    // is 0.
    if (0 == iv_hdr.hdatChildStrCnt)
	{
        iv_hdr.hdatChildStrOffset = 0;
	}
    i_data.write(&iv_hdr,sizeof(iv_hdr));

    if ( NULL != iv_dataPtrs)
    {
        i_data.write(iv_dataPtrs ,iv_dataPtrSize);
    }

    // Write the child pointers section.  
    if (NULL != iv_childPtrs)
    {
        i_data.write(iv_childPtrs,iv_childPtrSize);
    }
}
void HdatHdif::print()
{
    HDAT_INF("****** HdatHdif base object ******");

    HDAT_INF("      iv_totalSize = %u", iv_totalSize);
    HDAT_INF("      iv_padSize = %u", iv_padSize);
    HDAT_INF("      iv_dataPtrSize = %u", iv_dataPtrSize);
    HDAT_INF("      iv_childPtrSize = %u", iv_childPtrSize);

    hdatPrintHdrs(&iv_hdr,
                  (const hdatHDIFDataHdr_t *)iv_dataPtrs,
                  NULL,
                  (const hdatHDIFChildHdr_t *)iv_childPtrs);
  

    return;
}

/** @brief See the prologue in hdathdif.H
 */
uint32_t HdatHdif::endCommitSize()
{
    uint32_t l_size = 0;

  // Write pad bytes if needed
    if (iv_padSize > 0)
    {
      l_size += iv_padSize;
    }

    if (iv_siblingPadSize > 0)
    {
      if (iv_siblingPadSize <= sizeof(HDAT_PAD))
      {
        l_size += iv_siblingPadSize;
      }
      else
      {
        l_size += iv_siblingPadSize;
      }
    }
    return l_size;

}
void HdatHdif::endCommit(UtilMem &i_data)
{

  char *l_buffer = NULL;

  // Write pad bytes if needed
    if (iv_padSize > 0)
    {
      i_data.write(HDAT_PAD,iv_padSize);
    }

    if (iv_siblingPadSize > 0)
    {
      if (iv_siblingPadSize <= sizeof(HDAT_PAD))
      {
        i_data.write(HDAT_PAD,iv_siblingPadSize);
      }
      else
      {
        l_buffer = new char[iv_siblingPadSize]();
        i_data.write(l_buffer,iv_siblingPadSize);
        delete[] l_buffer;
      }
    }

}


} //namespace HDAT
OpenPOWER on IntegriCloud