summaryrefslogtreecommitdiffstats
path: root/tools/image/sbe_default_tool.c
blob: 2b1679efef5cc8e23e5115f2d34b15688a6df6b5 (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
/// \file sbe_default_tool.c
/// \brief SBE-XIP image setter tool for attributes in fixed section
///
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <errno.h>
#include <fcntl.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <endian.h>


#define __PPE__
#include "fapi2.H"
#include "proc_sbe_fixed.H"

const char* g_usage =
"Usage: sbe_default_tool <image> <attribute> <value> <target type> <index>\n"
"The 'image' is the binary image with fixed section.\n"
"\n"
"The 'attribute' is the attribute to be set.\n"
"\n"
"The 'value' is the value of the attribute to be set.\n"
"\n"
"The 'target type' is the type of the target. The following targets are defined:\n"
"TARGET_TYPE_PROC_CHIP: chip target\n"
"TARGET_TYPE_PERV: pervasive target\n"
"TARGET_TYPE_CORE: core target\n"
"TARGET_TYPE_EQ: eq target\n"
"TARGET_TYPE_EX: ex target\n"
"\n"
"The 'index' is the index of the value. Checking is performed.\n"
"example:\n"
"./sbe_default_tool ./sbe_main.bin ATTR_PLL_RING 0x33CAFE34 TARGET_TYPE_PERV 0\n"
"./sbe_default_tool ./sbe_main.bin ATTR_SCRATCH_UINT8_1 12 TARGET_TYPE_PROC_CHIP 0\n"
    ;


void assertTarget(const char* str, unsigned int index)
{

    if(strcmp(str, "TARGET_TYPE_PROC_CHIP") == 0) {
        if (index > 0) {
            fprintf(stderr, "sbe_default_tool: index is larger than 0\n");
            exit(1);
        } 
        return;
    } else if(strcmp(str, "TARGET_TYPE_EX") == 0)  {
        if (index >= EX_TARGET_COUNT) {
            fprintf(stderr, "sbe_default_tool: index is larger than EX_TARGET_COUNT\n");
            exit(1);
        } 
        return;
    } else if(strcmp(str, "TARGET_TYPE_EQ") == 0)  {
        if (index >= EQ_TARGET_COUNT) {
            fprintf(stderr, "sbe_default_tool: index is larger than EQ_TARGET_COUNT\n");
            exit(1);
        } 
        return;
    } else if(strcmp(str, "TARGET_TYPE_CORE") == 0)  {
        if (index >= CORE_TARGET_COUNT) {
            fprintf(stderr, "sbe_default_tool: index is larger than EQ_TARGET_COUNT\n");
            exit(1);
        } 
        return;
    } else if(strcmp(str, "TARGET_TYPE_PERV") == 0)  {
        if (index >= PERV_TARGET_COUNT) {
            fprintf(stderr, "sbe_default_tool: index is larger than PERV_TARGET_COUNT\n");
            exit(1);
        } 
        return;
    } else {

        if (index >= PERV_TARGET_COUNT) {
            fprintf(stderr, "sbe_default_tool: target not supported:");
            fprintf(stderr, " %s\n", str);
            exit(1);
        }    
    }
}

void setAttribute(void* image, const char* attribute, unsigned int index, uint64_t val) {


    SbeXipItem item;
    void *thePointer;
    int rc;

    rc = sbe_xip_find(image, attribute, &item);
    if (rc) {
        fprintf(stderr, "sbe_default_tool: attribute not existing:");
        fprintf(stderr, " %s", attribute);
        exit(1);
    }

    // debug purpose
    //printf("offset in string section: 0x%x \n",     be32toh(item.iv_toc->iv_id));
    //printf("address: 0x%x \n", item.iv_address);

    sbe_xip_image2host(image, item.iv_address, &thePointer);

    // debug purpose
    //printf("pointer1: 0x%x \n", thePointer);
    //printf("val: 0x%llx \n", val);

    if(item.iv_toc->iv_type == SBE_XIP_UINT8) {

        *((uint8_t*)thePointer + index) = (uint8_t)val;

    } else if(item.iv_toc->iv_type == SBE_XIP_INT8) {

        *((int8_t*)thePointer + index) = (int8_t)val;

    } else if(item.iv_toc->iv_type == SBE_XIP_UINT16) {

        *((uint16_t*)thePointer + index) = (uint16_t)val;

    } else if(item.iv_toc->iv_type == SBE_XIP_INT16) {

        *((int16_t*)thePointer + index) = (int16_t)val;

    } else if(item.iv_toc->iv_type == SBE_XIP_UINT32) {

        *((uint32_t*)thePointer + index) = (uint32_t)val;

    } else if(item.iv_toc->iv_type == SBE_XIP_INT32) {

        *((int32_t*)thePointer + index) = (int32_t)val;

    } else if(item.iv_toc->iv_type == SBE_XIP_UINT64) {

        *((uint64_t*)thePointer + index) = (uint64_t)val;

    } else if(item.iv_toc->iv_type == SBE_XIP_INT64) {

        *((int64_t*)thePointer + index) = (int64_t)val;

    } else {
        fprintf(stderr, "sbe_default_tool: type not available");
        exit(1);
    }




    SBE_XIP_SECTION_NAMES(section_name);
    SBE_XIP_TYPE_STRINGS(type_name);

    // debug purpose
    //printf("pointer2: 0x%x \n", thePointer);
    //printf("section id: %s \n",  section_name[item.iv_toc->iv_section]);
    //printf("location in section: 0x%x \n",  be32toh(item.iv_toc->iv_data));
    //printf("type name: %s \n",  type_name[item.iv_toc->iv_type]);

    return;
}


uint64_t getAttribute(void* image, const char* attribute, unsigned int index) {

    uint64_t val = 0;

    SbeXipItem item;
    void *thePointer;
    int rc;

    rc = sbe_xip_find(image, attribute, &item);
    if (rc) {
        fprintf(stderr, "sbe_default_tool: attribute not existing:");
        fprintf(stderr, " %s", attribute);
        exit(1);
    }
    
    sbe_xip_image2host(image, item.iv_address, &thePointer);

    if(item.iv_toc->iv_type == SBE_XIP_UINT8) {

        val = *((uint8_t*)thePointer + index);
        
    } else if(item.iv_toc->iv_type == SBE_XIP_INT8) {

        val = *((int8_t*)thePointer + index);
        val &= 0xFF; 

    } else if(item.iv_toc->iv_type == SBE_XIP_UINT16) {

        val = *((uint16_t*)thePointer + index);

    } else if(item.iv_toc->iv_type == SBE_XIP_INT16) {

        val = *((int16_t*)thePointer + index);
        val &= 0xFFFF; 

    } else if(item.iv_toc->iv_type == SBE_XIP_UINT32) {

        val = *((uint32_t*)thePointer + index);

    } else if(item.iv_toc->iv_type == SBE_XIP_INT32) {

        val = *((int32_t*)thePointer + index);
        val &= 0xFFFFFFFF; 

    } else if(item.iv_toc->iv_type == SBE_XIP_UINT64) {

        val = *((uint64_t*)thePointer + index);

    } else if(item.iv_toc->iv_type == SBE_XIP_INT64) {

        val = *((int64_t*)thePointer + index);

    } else {
        fprintf(stderr, "sbe_default_tool: type not available");
        exit(1);
    }



    return val;
}

int  main(int argc, const char** argv)
{

    int fileFd, rc;
    void* image;
    struct stat buf;

    if(argc != 6) {
        fprintf(stderr, "sbe_default_tool: argument missing\n");
        fprintf(stderr, g_usage);
        exit(1);
    } 

    printf("sbe_default_tool %s %s %s %s %s\n", argv[1], argv[2], argv[3], argv[4], argv[5]);

    fileFd = open(argv[1], O_RDWR);
    if (fileFd < 0) {
        fprintf(stderr, "sbe_default_tool: open() of the file to be appended failed");
        exit(1);
    }

    rc = fstat(fileFd, &buf);
    if (rc) {
        fprintf(stderr, "sbe_default_tool: fstat() of the file to be appended failed");
        exit(1);
    }

    image = mmap(0, buf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fileFd, 0);
    if (image == MAP_FAILED) {
        fprintf(stderr, "sbe_default_tool: mmap() of the file to be appended failed");
        exit(1);
    }


    uint64_t val=strtoull(argv[3], 0, 0);

    unsigned int index = strtoul(argv[5], 0, 10);

    assertTarget(argv[4], index);

    setAttribute(image, argv[2], index, val);

    uint64_t check = getAttribute(image, argv[2], index);

    if((check & val) != check) {
            
        fprintf(stderr, "sbe_default_tool: set and get values not equal");
        fprintf(stderr, "%lx != %lx\n", check, val);
        exit(1);

    }

    rc = close(fileFd);
    if (rc) {
        fprintf(stderr, "sbe_default_tool: close() of modified image failed");
        exit(1);
    }

			
    return 0;
}
OpenPOWER on IntegriCloud