summaryrefslogtreecommitdiffstats
path: root/src/usr/errl/errlud.C
blob: 09be2bacfdd4f7ee18213d5352eb5d1d66e4a7a8 (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/errl/errlud.C $                                       */
/*                                                                        */
/* IBM CONFIDENTIAL                                                       */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 2011,2013              */
/*                                                                        */
/* p1                                                                     */
/*                                                                        */
/* Object Code Only (OCO) source materials                                */
/* Licensed Internal Code Source Materials                                */
/* IBM HostBoot Licensed Internal Code                                    */
/*                                                                        */
/* The source code for this program is not published or otherwise         */
/* divested of its trade secrets, irrespective of what has been           */
/* deposited with the U.S. Copyright Office.                              */
/*                                                                        */
/* Origin: 30                                                             */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
/**
 *  @file errlud.C
 *
 *  @brief <Brief Description of this file>
 *
 *  <Detailed description of what this file does, functions it includes,
 *  etc,>
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <hbotcompid.H>
#include <errl/errlentry.H>



namespace ERRORLOG
{


extern trace_desc_t* g_trac_errl;


//***************************************************************************
// Constructor

ErrlUD::ErrlUD(
    const void * i_data,
    uint64_t     i_size,
    compId_t     i_compid,
    uint8_t      i_ver,
    uint8_t      i_sst )  :

    ErrlSctn( ERRL_SID_USER_DEFINED, 0, i_ver, i_sst, i_compid ),
    iv_pData( NULL ),
    iv_Size( 0 )
{
    uint64_t l_cb;

    l_cb = addData( i_data, i_size );
    if( 0 == l_cb )
    {
        // Rare.
        TRACFCOMP( g_trac_errl, "ErrlUD::ErrlUD(): addData rets error");
    }
}





/*****************************************************************************/
// Destructor

ErrlUD::~ErrlUD()
{
    free( iv_pData );
}





/*****************************************************************************/
// Add data. This works the first time when there is no data and works for
// subsequent times when you want to append data. Return [new] size of buffer.

uint64_t ErrlUD::addData(const void *i_data, const uint64_t i_size)
{
    // Expected new size of user data.
    uint64_t l_newsize = iv_Size + i_size;

    // Resize memory block
    iv_pData = static_cast<uint8_t*>(realloc(iv_pData, l_newsize));

    // Copy new data to new area, past existing data (if any)
    memcpy( iv_pData + iv_Size, i_data, i_size );

    // Save new size of the user-provided data. This will also
    // be what this method returns.
    iv_Size = l_newsize;

    return iv_Size;
}



/*****************************************************************************/
// Data Export size

uint64_t ErrlUD::flatSize()
{
    uint64_t	l_rc = 0;

    l_rc = iv_header.flatSize() + iv_Size;

    // Round up to next nearest 4-byte boundary.
    l_rc = ( l_rc + 3 ) & ~3; 

    return l_rc;
}


/*****************************************************************************/
// Data Export. Return how many bytes were written or zero on error.

uint64_t ErrlUD::flatten( void * o_pBuffer, const uint64_t i_cbBuffer )
{
    uint64_t  l_rc = 0;
    uint64_t  l_cb = 0;
    uint8_t * pBuffer = static_cast<uint8_t *>(o_pBuffer);
    uint64_t  l_cbFlat = this->flatSize();

    if ( i_cbBuffer >= l_cbFlat )
    {
        // Tell the PEL section header what is the new length.
        iv_header.iv_slen = l_cbFlat;

        // Flatten the PEL section header
        l_cb = iv_header.flatten( pBuffer, i_cbBuffer );
        pBuffer += l_cb;

        // Followed by the user data
        memcpy( pBuffer, iv_pData, iv_Size );
        pBuffer += iv_Size;

        // Buffer is rounded up to the nearst 4-byte boundary, pad with zeroes
        for (uint64_t i = l_cb + iv_Size; i < l_cbFlat; i++)
        {
            *pBuffer++ = 0;
        }

        // return how many bytes were flattened
        l_rc = l_cbFlat;
    }
    else
    {
        TRACFCOMP( g_trac_errl, "ErrlUD::flatten: buffer too small");
    }


    return l_rc;
}

} //namespace
OpenPOWER on IntegriCloud