summaryrefslogtreecommitdiff
path: root/riff/InitializeDut.cpp
blob: 9088c135e12edd44e0f5728b051f9adf4ba1fbda (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
/*
 * InitializeDut.cpp
 *
 * Copyright (C) ST-Ericsson SA 2011
 * Authors: Srimanta Panda <srimanta.panda@stericsson.com>,
 *          Ola Borgelin <ola.borgelin@stericsson.com>,
 *          Karin Hedlund <karin.hedlund@stericsson.com>,
 *          Markus Andersson <markus.m.andersson@stericsson.com> for ST-Ericsson.
 * License terms: 3-clause BSD license
 *
 */

/*
 * @addtogroup Commands
 * @{
 */

#include "InitializeDut.h"
#include "Logger.h"
#include "DUT.h"
#include "Utilities.h"
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sys/stat.h>

using namespace std;


int InitializeDut::readIsswFile(char* buf)
{
    int res = 0;
    FILE* isswImage;

    isswImage = fopen(isswPath_, "rb");

    if (isswImage == NULL) {
        logger_.log(Logger::ERR, "InitializeDut.cpp (%d) %s() error while opening file %s", __LINE__, __FUNCTION__, isswPath_);
        return -1;
    }

    res = fread(buf, 1, issw_.size, isswImage);

    fclose(isswImage);
    return 0;
}

int InitializeDut::readSecondIsswFile(char* buf)
{
    int res = 0;
    FILE* xloaderImage;

    xloaderImage = fopen(xloaderPath_, "rb");

    if (xloaderImage == NULL) {
        logger_.log(Logger::ERR, "InitializeDut.cpp (%d) %s() error while opening file %s", __LINE__, __FUNCTION__, xloaderPath_);
        return -1;
    }

    res = fread(buf, 1, xload_.size, xloaderImage);
    fclose(xloaderImage);
    return 0;
}

int InitializeDut::readPwrMgtFile(char* buf)
{
    FILE* pwrmgmtImage;

    pwrmgmtImage = fopen(pwrmgmtPath_, "rb");

    if (pwrmgmtImage == NULL) {
        logger_.log(Logger::ERR, "InitializeDut.cpp (%d) %s() error while opening file %s", __LINE__, __FUNCTION__, pwrmgmtPath_);
        return -1;
    }

    fread(buf, 1, pwrMgt_.size, pwrmgmtImage);
    fclose(pwrmgmtImage);
    return 0;
}

int InitializeDut::readMeminitFile(char* buf)
{
    FILE* meminitImage;

    meminitImage = fopen(meminitPath_, "rb");

    if (meminitImage == NULL) {
        logger_.log(Logger::ERR, "InitializeDut.cpp (%d) %s() error while opening file %s", __LINE__, __FUNCTION__, meminitPath_);
        return -1;
    }

    fread(buf, 1, meminit_.size, meminitImage);
    fclose(meminitImage);
    return 0;
}

int InitializeDut::readNormal(char* buf)
{
    FILE* normalImage;

    normalImage = fopen(normalPath_, "rb");

    if (normalImage == NULL) {
        logger_.log(Logger::ERR, "InitializeDut.cpp (%d) %s() error while opening file %s", __LINE__, __FUNCTION__, normalPath_);
        return -1;
    }

    fread(buf, 1, normal_.size, normalImage);
    fclose(normalImage);
    return 0;
}

int InitializeDut::commWrite2kChunks(Device_t device, void* buffer, size_t size)
{
    char* buf = static_cast<char*>(buffer);

    while (size) {
        size_t toSend = size > 2048 ? 2048 : size;

        if (comm_write(device, buf, toSend) < 0)
            return -1;

        size -= toSend;
        buf += toSend;
    }

    return 0;
}

int InitializeDut::sendTocIssw(Device_t device)
{
    //Send TOC+ISSW to the board
    int index = 0;
    tocSection* tocSections = (tocSection*)tocArea;
    unsigned int bootIndication;
    char* sendBuffer = NULL;
    int sendBufferSize = 0;
    char* tempNORMAL = (char*) "NORMAL";

    memset(tocSections, 0xff, 512);

    //Build TOC Sections List
    tocSections[index++] = issw_;

    // X-Loader section
    tocSections[index++] = xload_;
    tocSections[index++] = meminit_;

    if (strcmp(pwrMgt_.filename, tempNORMAL) == 0) {
        tocSections[index++] = normal_;
    } else {
        tocSections[index++] = pwrMgt_;
        tocSections[index++] = normal_;
    }

    sendBufferSize = 512 + issw_.size;
    sendBuffer = (char*)malloc(sendBufferSize);

    bootIndication = 0xf0030002;

    if (comm_write(device, (char*)&bootIndication, 4) < 0)
        return -1;

    if (comm_write(device, (char*)&sendBufferSize, 4) < 0)
        return -1;

    memcpy(sendBuffer, tocSections, 512);
    readIsswFile(sendBuffer + 512);

    commWrite2kChunks(device, sendBuffer, sendBufferSize);

    return 0;
}

int InitializeDut::createTocEntry(tocSection* issw, tocSection* xload, tocSection* meminit, tocSection* normal, tocSection* pwrMgt)
{
    int offset = 512;

    createTocFile(issw, isswPath_, "ISSW", &offset); //ISSW TOC
    createTocFile(xload, xloaderPath_, "X-LOADER", &offset); //X-LOADER TOC
    createTocFile(meminit, meminitPath_, "MEM_INIT", &offset); //MEMINIT TOC

    if (strlen(pwrmgmtPath_) != 0) {
        createTocFile(pwrMgt, pwrmgmtPath_, "PWR_MGT", &offset); //PWR_MGT TOC
        createTocFile(normal, normalPath_, "NORMAL", &offset); //NORMAL TOC
    } else {
        createTocFile(pwrMgt, normalPath_, "NORMAL", &offset); //NORMAL TOC
        *normal = *pwrMgt;
    }

    return 0;
}

int InitializeDut::createTocFile(tocSection* toc, const char* filePath, const char* tocName, int* offset)
{
    size_t size = 0;

    if ((filePath == NULL) || (strlen(filePath) == 0))
        return -1;

    size = getFileSize(filePath);

    // Create the TOC entry
    toc->start = *offset;
    toc->size = size;
    toc->flags = 0;
    toc->align = 0;
    toc->loadAddress = 0;
    strcpy(toc->filename, tocName);

    *offset += size;

    return 0;
}

int InitializeDut::getFileSize(const char* filePath)
{

    struct stat filestatus;
    stat(filePath, &filestatus);

    return filestatus.st_size;

}

int InitializeDut::initializeHardware(Device_t device)
{

    char asicIdUsb[65];
    unsigned char secureMode;

    unsigned int byteSynchro = 0x53548815;

    Config config(configPath_);

    isswPath_ = config.getValue("ISSWPATH");
    xloaderPath_ = config.getValue("XLOADERPATH");
    meminitPath_ = config.getValue("MEMINITPATH");
    pwrmgmtPath_ = config.getValue("PWRMGMTPATH");
    normalPath_ = config.getValue("NORMALPATH");

    if (comm_write(device, (char*)&byteSynchro, 4) < 0)
        return -1;

    if (comm_read(device, asicIdUsb, 65) < 0)
        return -1;

    secureMode = asicIdUsb[11];

    if (createTocEntry(&issw_, &xload_, &meminit_, &normal_, &pwrMgt_))
        return -1;

    if (sendTocIssw(device))
        return -1;

    while (1) {
        unsigned int reqId;

        if (comm_read(device, (char*)&reqId, 4) < 0)
            return -1;

        char *sendBuffer = NULL;

        switch (reqId) {
        case 0xA0400000:
            /* 2nd Loader ISSW Requested XLoader */
            logger_.log(Logger::INFO, "X-LOADER token");

            if (comm_write(device, (char*)&xload_.size, 4) < 0)
                return -1;

            sendBuffer = (char*)malloc(xload_.size);

            if (readSecondIsswFile(sendBuffer) < 0)
            {
            	free(sendBuffer);
            	return -1;
            }



            commWrite2kChunks(device, sendBuffer, xload_.size);
            logger_.log(Logger::INFO, "X-LOADER sent");
            free(sendBuffer);

            break;

        case 0xA0300002:
            /*PWR_MGT token*/
            logger_.log(Logger::INFO, "PWR_MGT token");

            if (comm_write(device, (char*)&pwrMgt_.size, 4) < 0)
                return -1;

            sendBuffer = (char*)malloc(pwrMgt_.size);

            if (readPwrMgtFile(sendBuffer) < 0)
            {
            	free(sendBuffer);
            	return -1;
            }

            commWrite2kChunks(device, sendBuffer, pwrMgt_.size);
            logger_.log(Logger::INFO, "PWR_MGT sent");
            free(sendBuffer);

            break;

        case 0xA0300000:
            /* Mem Init token */
            logger_.log(Logger::INFO, "MEM_INIT token");

            if (comm_write(device, (char*)&meminit_.size, 4) < 0)
                return -1;

            sendBuffer = (char*)malloc(meminit_.size);

            if (readMeminitFile(sendBuffer) < 0)
            {
            	free(sendBuffer);
            	return -1;
            }

            commWrite2kChunks(device, sendBuffer, meminit_.size);
            logger_.log(Logger::INFO, "MEM_INIT sent");
            free(sendBuffer);

            break;

        case 0xA0300001:
            /* Application token */
            logger_.log(Logger::INFO, "NORMAL token");

            if (comm_write(device, (char*)&normal_.size, 4) < 0)
                return -1;

            sendBuffer = (char*)malloc(normal_.size);

            if (readNormal(sendBuffer) < 0)
            {
            	free(sendBuffer);
            	return -1;
            }

            commWrite2kChunks(device, sendBuffer, normal_.size);
            logger_.log(Logger::INFO, "NORMAL sent");
            free(sendBuffer);

            return 0;

            break;

        default:
            logger_.log(Logger::INFO, "UNKNOWN token");
        }
    }
}
InitializeDut::InitializeDut(const char* configPath):
		logger_("InitializeDut")
{
    configPath_ = configPath;
}

int InitializeDut::run(DUT* dut)
{
    int error = 0;
    Device_t commDevice = dut->getCommDevice();
    LCDContext lcdContext = 0;

    char version[100] = {0};
    int versionSize = 100;
    char protocol[100] = {0};
    int protocolSize = 100;

    logger_.log(Logger::INFO, "Initializing device...");

    error = initializeHardware(commDevice);
    if (0 != error)
    {
        logger_.log(Logger::ERR,"LCD ERROR: Error while initializing device %d", error);
        return error;
    }

    logger_.log(Logger::INFO, "Initializing finished");

    logger_.log(Logger::INFO, "Starting initialization of LCD...");
    void** deviceObjectStorage = comm_get_object_storage(commDevice);
    error = CreateContext(&lcdContext, dut->getId());
    if (0 != error)
    {
        logger_.log(Logger::ERR,"LCD ERROR: Error while creating LCD context %d", error);
        return error;
    }

    dut->setLCDContext(lcdContext);
    error = ConfigureCommunicationDevice(lcdContext, (void*)comm_read_nowait, (void*)comm_write_nowait, (void*)comm_cancel);
    if (0 != error)
    {
        logger_.log(Logger::ERR,"LCD ERROR: Error while configuring communication device %d", error);
        return error;
    }


    error = SwitchProtocolFamily(lcdContext, R15_PROTOCOL_FAMILY);
    if (0 != error)
    {
        logger_.log(Logger::ERR,"LCD ERROR: Error while setting protocol family %d", error);
        return error;
    }


    error = StartContext(lcdContext, deviceObjectStorage);
    if (0 != error)
    {
        logger_.log(Logger::ERR,"LCD ERROR: Error while starting LCD context %d", error);
        return error;
    }

    logger_.log(Logger::INFO, "LCD initialization finished successfully");

    error = SetProgressCallback(lcdContext, (void*)comm_progress);
    if (0 != error)
    {
        logger_.log(Logger::ERR,"LCD ERROR: Error while setting Progress listener %d", error);
        return error;
    }

    logger_.log(Logger::INFO, "Progress listener added successfully");

    error = System_LoaderStartupStatus(dut->getLCDContext(), version, &versionSize, protocol, &protocolSize);
    if (0 != error)
    {
        logger_.log(Logger::ERR,"LCD ERROR: Failed to receive loader startup status %d", error);
        return error;
    }

    logger_.log(Logger::INFO, "Loader successfully started");
    logger_.log(Logger::INFO, "  Version: %s", version);
    logger_.log(Logger::INFO, "  Protocol: %s", protocol);

    Utilities::sleep(100);

    return error;
}

const char * InitializeDut::get_command_name()
{
    return (char *)"INITIALIZE_DUT";
}

/* @} */