File: main.cpp

package info (click to toggle)
intel-media-driver 23.1.1%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 146,180 kB
  • sloc: cpp: 1,508,381; ansic: 205,392; asm: 45,336; python: 653; sh: 177; makefile: 16
file content (506 lines) | stat: -rwxr-xr-x 17,798 bytes parent folder | download | duplicates (4)
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
// Disable deprecated
#pragma warning(disable : 4995)

#include <assert.h>
#include <list>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
#include "linkfile.h"

#ifdef LINUX_
#include <dirent.h>
#include <sys/stat.h>
#include <strings.h>
#include <cstring>
#endif

using namespace std;

// Defaults
#define KERNEL                      "kernel"
#define KERNEL_HEADER_FILE_NAME     "krnheader.h"

char KERNEL_COMPONENT_DIR[MAX_STRING_SIZE]        = "";
char KERNEL_HEADER_FILE[MAX_STRING_SIZE]        = "";
char KERNEL_TEMP_HEADER_FILE[MAX_STRING_SIZE]    = "";
char KERNEL_BINARY_FILE[MAX_STRING_SIZE]        = "";
char KERNEL_SEARCH_DIR[MAX_STRING_SIZE]            = "";

char  KERNEL_HEADER_PREFIX[8]       = "";
char  KERNEL_HEADER_GEN[8]          = "";
char  KERNEL_HEADER_KIND[16]        = "";
char  KERNEL_HEADER_PREFIX_UPPER[8] = "";
char  KERNEL_HEADER_SENTRY[64]      = "";
char  KERNEL_IDR[16]                = "";

static const char *COPYRIGHT        =
    "/*\n"
    " * Copyright (c) 2019, Intel Corporation\n"
    " *\n"
    " * Permission is hereby granted, free of charge, to any person obtaining a\n"
    " * copy of this software and associated documentation files (the\n"
    " * 'Software'), to deal in the Software without restriction, including\n"
    " * without limitation the rights to use, copy, modify, merge, publish,\n"
    " * distribute, sublicense, and/or sell copies of the Software, and to\n"
    " * permit persons to whom the Software is furnished to do so, subject to\n"
    " * the following conditions:\n"
    " *\n"
    " * The above copyright notice and this permission notice shall be included\n"
    " * in all copies or substantial portions of the Software.\n"
    " *\n"
    " * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS\n"
    " * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n"
    " * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\n"
    " * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\n"
    " * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\n"
    " * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\n"
    " * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n"
    "*/\n"
    "\n"
    "////////////////////////////////////////////////////////////////////////////////\n"
    "// !!! WARNING - AUTO GENERATED FILE. DO NOT EDIT DIRECTLY. !!!\n"
    "// Generated by GenKrnBin.exe tool\n"
    "////////////////////////////////////////////////////////////////////////////////\n"
;

#define INDEX_OPEN_FORMAT_STRING         "#ifndef %s\n#define %s\n\n\0",              \
    KERNEL_HEADER_SENTRY,                                                                   \
    KERNEL_HEADER_SENTRY
#define INDEX_ENTRY_FORMAT_STRING(index) "#define %s_%s %d\n\0",                          \
    KERNEL_IDR, index, dwKernelCount
#define INDEX_CLOSE_FORMAT_STRING        "\n#endif // %s\0", KERNEL_HEADER_SENTRY
#define INDEX_ENTRY_FORMAT_INT(index,i)  "#define %s_%s %d\n\0",                          \
    KERNEL_IDR, index, i
#define KERNEL_NAME_DEFINE(index)        "#define %s_%s   \\\n",                          \
    KERNEL_IDR, index
#define KERNEL_NAME_ENTRY(index)         "    _" "T(\"%s\"),\\\n",                        \
    index
#define KERNEL_ID_ENTRY(index)           "    _" "T(\"%03d\"),\\\n",                      \
    index

#ifdef LINUX_
#define StrCmp strncasecmp
#else
#define StrCmp strnicmp
#endif

void ConcatenateKernelBinary(char *pKernelName, bool bVerbose);

// scratch global variables for writing files
char    g_Buffer[MAX_STRING_SIZE];
bool    g_bReturn;

// size_t is integral data type returned by the language operator sizeof and is defined in the <cstring> header file (among others) as an unsigned integral type
unsigned int   g_SizetOfTextToWrite;
unsigned int   g_dwNumBytesWritten;

// globals used in multiple functions
FILE          *g_hKernelBinary        = NULL;
unsigned int   g_dwTotalKernelCount   = 0;
unsigned int   g_dwCurrentKernelCount = 0;

// offset table
unsigned int  *g_pOffsetTable;
unsigned int   g_dwOffsetTableSize;
unsigned int   g_dwCurrentKernel;

#ifdef LINUX_
char strupr(char *str) 
{ 
    char *ptr = str;

    while (*ptr != '\0') 
    { 
        if (islower(*ptr)) 
        *ptr = toupper(*ptr); 
        ptr++; 
    }
}

#define _strupr_s strupr
#endif

int main(int argc, char *argv[])
{
    // list template class is based on a doubly-linked list
    list <string> KernelList; // creates an empty list for elements of type "string"
    list <string>::const_iterator iterator; // allows iterating over list <string>
    FILE *hKernelDirs, *hKernels, *hHeaderFile, *hTempHeaderFile;

    // TCHAR data type is char string used to represent a string of either the WCHAR or char type
    char SearchString[MAX_STRING_SIZE];
    char KernelNameNoExt[MAX_STRING_SIZE];
    char KernelNameFull[MAX_STRING_SIZE];
    char KernelBinName[MAX_STRING_SIZE];
    bool  bKernelNames, bVerbose;
    char* pcHeaderFile, pcTempHeaderFile;
    unsigned int dwKernelCount, dwHeaderSize, dwTempHeaderSize, dwBytesRead;

    bVerbose = false;
    if (argc < 5)
    {
        fprintf(stderr, "Usage: GenKrnBin.exe <kernel root dir> <component> [-verbose]\n");
        exit (-1);
    }

    int idx = 5;        // scan the command-line options from index 3
    while (idx < argc)
    {
        if (StrCmp(argv[idx], "-verbose", 8) == 0)
        {
            bVerbose = true;
        }
        ++idx;
    }

#ifdef LINUX_
    sprintf(KERNEL_COMPONENT_DIR,"%s", argv[1]);
    strncpy(KERNEL_HEADER_PREFIX, argv[2], sizeof(KERNEL_HEADER_PREFIX));
    strncpy(KERNEL_HEADER_GEN, argv[3], sizeof(KERNEL_HEADER_GEN));
    strncpy(KERNEL_HEADER_KIND, argv[4], sizeof(KERNEL_HEADER_KIND));
    strncpy(KERNEL_HEADER_PREFIX_UPPER, argv[2], sizeof(KERNEL_HEADER_PREFIX));
   _strupr_s(KERNEL_HEADER_PREFIX_UPPER);

    sprintf(KERNEL_HEADER_FILE, "%s/ig%skrn_%s_%s.h", KERNEL_COMPONENT_DIR, KERNEL_HEADER_PREFIX, KERNEL_HEADER_GEN, KERNEL_HEADER_KIND);
    sprintf(KERNEL_TEMP_HEADER_FILE, "%s/%stemp%s", KERNEL_COMPONENT_DIR, KERNEL_HEADER_PREFIX, KERNEL_HEADER_FILE_NAME);
    sprintf(KERNEL_BINARY_FILE, "%s/ig%skrn_%s_%s.bin", KERNEL_COMPONENT_DIR, KERNEL_HEADER_PREFIX, KERNEL_HEADER_GEN, KERNEL_HEADER_KIND);
    sprintf(KERNEL_SEARCH_DIR, "%s/g*", KERNEL_COMPONENT_DIR);
    sprintf(KERNEL_HEADER_SENTRY, "__%sKRNHEADER_H__", KERNEL_HEADER_PREFIX_UPPER);
    sprintf(KERNEL_IDR, "IDR_%s", KERNEL_HEADER_PREFIX_UPPER);
#else
    sprintf(KERNEL_COMPONENT_DIR,"%s", argv[1]);
    strncpy(KERNEL_HEADER_PREFIX, argv[2], sizeof(KERNEL_HEADER_PREFIX));
    strncpy(KERNEL_HEADER_PREFIX_UPPER, argv[2], sizeof(KERNEL_HEADER_PREFIX));
    _strupr_s(KERNEL_HEADER_PREFIX_UPPER);

    sprintf(KERNEL_HEADER_FILE, "%s\\ig%skrn_g11_icllp.h", KERNEL_COMPONENT_DIR, KERNEL_HEADER_PREFIX);
    sprintf(KERNEL_TEMP_HEADER_FILE, "%s\\%stemp%s", KERNEL_COMPONENT_DIR, KERNEL_HEADER_PREFIX, KERNEL_HEADER_FILE_NAME);
    sprintf(KERNEL_BINARY_FILE, "%s\\ig%skrn_g11_icllp.bin", KERNEL_COMPONENT_DIR, KERNEL_HEADER_PREFIX);
    sprintf(KERNEL_SEARCH_DIR, "%s\\g*", KERNEL_COMPONENT_DIR);
    sprintf(KERNEL_HEADER_SENTRY, "__%sKRNHEADER_H__", KERNEL_HEADER_PREFIX_UPPER);
    sprintf(KERNEL_IDR, "IDR_%s", KERNEL_HEADER_PREFIX_UPPER);
#endif
    bKernelNames = true;

    /////////////////////////////////////////////////
    // Generate a superset list of all the kernels //
    /////////////////////////////////////////////////


#ifdef LINUX_
    sprintf(SearchString, "%s", KERNEL_COMPONENT_DIR);
 
    struct dirent* ent = NULL;

    DIR *pDir=NULL;
    pDir=opendir(SearchString);
    int n;

    while (NULL != (ent=readdir(pDir)))
    {      
        n = strlen(ent->d_name);
        //printf("kernel name  = %s; ent->d_reclen = %d\n", ent->d_name, n);
        //printf("kernel offset= %s\n", ent->d_name + n - 4);

        if ((StrCmp(ent->d_name + n - 4, ".krn", 4) == 0) ||
            (StrCmp(ent->d_name + n - 4, ".hex", 4) == 0))
        {
            KernelList.push_back(string(ent->d_name));
            continue;
        }
        else
        {
            continue;
        }
    };

    closedir(pDir);
#endif

    // include LinkFile.txt
    KernelList.push_back(string("LinkFile.krn"));

    // remove duplicate entries from the list (create superset)
    KernelList.sort();

    // assumes list is sorted. removes subsequent consecutive equal values.
    KernelList.unique();

    ///////////////////////////////////////////////////////////////////
    // Create a temp header that defines the indicies into the table //
    ///////////////////////////////////////////////////////////////////

    // create the header file that will define the kernel IDs
    hHeaderFile = fopen(KERNEL_HEADER_FILE, "w");
    if (hHeaderFile == NULL)
    {
        fprintf(stderr, "Failed to open Temp Header File\n");
        exit (-1);
    }

    // print the copyright comment text to the file
    fwrite(COPYRIGHT, 1, strlen(COPYRIGHT), hHeaderFile);

    fprintf(hHeaderFile, INDEX_OPEN_FORMAT_STRING);
    fprintf(hHeaderFile, INDEX_ENTRY_FORMAT_INT("LINKFILE_VERSION", LINKFILE_VERSION));
    fprintf(hHeaderFile, INDEX_ENTRY_FORMAT_INT("LINKFILE_HEADER" , sizeof(LinkFileHeader)));
    fprintf(hHeaderFile, "\n");

    for (iterator = KernelList.begin(), dwKernelCount = 0;
        iterator != KernelList.end(); iterator++, dwKernelCount++)
    {
        strncpy(KernelNameNoExt, iterator->c_str(), MAX_STRING_SIZE);

        *(strrchr(KernelNameNoExt, '.')) = '\0';
        char *c;
 
        while ((c = strchr(KernelNameNoExt, '.')) != NULL) *c = '_';
        fprintf(hHeaderFile, INDEX_ENTRY_FORMAT_STRING(KernelNameNoExt));
    }

    fprintf(hHeaderFile, INDEX_ENTRY_FORMAT_STRING("TOTAL_NUM_KERNELS"));

    // kernel names are defined in the same sequence as they are loaded
    if (bKernelNames)
    {
        fprintf(hHeaderFile, "\n#if _DEBUG || _RELEASE_INTERNAL\n");
        fprintf(hHeaderFile, KERNEL_NAME_DEFINE("KERNEL_NAMES"));
        for (iterator = KernelList.begin(), dwKernelCount = 0;
        iterator != KernelList.end(); iterator++, dwKernelCount++)
        {
            strncpy(KernelNameNoExt, iterator->c_str(), MAX_STRING_SIZE);
            *(strrchr(KernelNameNoExt, '.')) = '\0';
            char *c;
            while ((c = strchr(KernelNameNoExt, '.')) != NULL) *c = '_';
            fprintf(hHeaderFile, KERNEL_NAME_ENTRY(KernelNameNoExt));
        }
        fprintf(hHeaderFile, "    _" "T(\"\")\n");
        fprintf(hHeaderFile, "#else // !_DEBUG\n");
        fprintf(hHeaderFile, KERNEL_NAME_DEFINE("KERNEL_NAMES"));
        for (int i = 0; i < dwKernelCount; i++)
        {
            fprintf(hHeaderFile, KERNEL_ID_ENTRY(i));
        }
        fprintf(hHeaderFile, "    _" "T(\"\")\n");
        fprintf(hHeaderFile, "#endif // _DEBUG\n");
    }

    fprintf(hHeaderFile, INDEX_CLOSE_FORMAT_STRING);
    fprintf(hHeaderFile, "\n");
    g_dwTotalKernelCount = dwKernelCount;
    fclose(hHeaderFile);

    ///////////////////////////////////////////////////////////////////
    // Create bin containing kernel table and actual kernel binaries //
    ///////////////////////////////////////////////////////////////////
    // allocate kernel offset table
    g_dwOffsetTableSize = (g_dwTotalKernelCount + 1) * sizeof(unsigned int);
    g_pOffsetTable      = (unsigned int *)malloc(g_dwOffsetTableSize);
    if (!g_pOffsetTable)
    {
        fprintf(stderr, "Failed to allocate offset table\n");
        exit (-1);
    }

    // do for every family specific folder
    // create the file that will be the concatenation of all kernel binaries
    sprintf(KernelBinName, "%s", KERNEL_BINARY_FILE);
    g_hKernelBinary = fopen(KernelBinName, "wb");
    if (g_hKernelBinary == NULL)
    {
        fprintf(stderr, "Failed to open Kernel Bin File\n");
        exit (-1);
    }

    // write empty offset table
    memset(g_pOffsetTable,  0, g_dwOffsetTableSize);
    fwrite( g_pOffsetTable, 1, g_dwOffsetTableSize, (FILE *)g_hKernelBinary);

    // create link information for the current family folder
#ifdef LINUX_
    sprintf(KernelNameFull, "%s", KERNEL_COMPONENT_DIR);
#else
    sprintf(KernelNameFull, "%s", KERNEL_COMPONENT_DIR);
#endif
    CreateLinkFile(KernelNameFull, KernelList);

    // for every kernel in the superset list
    g_dwCurrentKernel = 0;
    for (iterator = KernelList.begin(); iterator != KernelList.end(); iterator++, g_dwCurrentKernel++)
    {
        // check if this kernel exists for this platform
        // cFileName contains file name
#ifdef LINUX_
        sprintf(KernelNameFull, "%s/%s", KERNEL_COMPONENT_DIR, iterator->c_str());
#else
        sprintf(KernelNameFull, "%s\\%s", KERNEL_COMPONENT_DIR, iterator->c_str());
#endif
        // add kernel binary, update offset table
        ConcatenateKernelBinary(KernelNameFull, bVerbose);
    }

    // write offset table
    fseek(g_hKernelBinary, 0, SEEK_SET);
    fwrite(g_pOffsetTable, 1, g_dwOffsetTableSize, g_hKernelBinary);

    fclose(g_hKernelBinary);

#ifdef LINUX_
    sprintf(KernelNameFull, "%s", KERNEL_COMPONENT_DIR);
#else
    sprintf(KernelNameFull, "%s", KERNEL_COMPONENT_DIR);
#endif

    DeleteLinkFile(KernelNameFull);

    // free offset table
    if (g_pOffsetTable)
    {
        free(g_pOffsetTable);
    }

    return 0;
}

void ConcatenateKernelBinary(char *pKernelName, bool bVerbose)
{
    FILE *hKernel;
    char pBuffer[MAX_STRING_SIZE];
    unsigned int dwBytesRead, dwBytesWritten, dwBinaryPos, dwFileSize;
    int   iLength;

    // update offset table
    dwBinaryPos = ftell(g_hKernelBinary) - g_dwOffsetTableSize;
    g_pOffsetTable[g_dwCurrentKernel] = dwBinaryPos;

    // the function opens an existing file and returns a handle that can be used to access the object
    // open .krn/.hex file
    hKernel = fopen(pKernelName, "r");

    if (hKernel == NULL)
    {
        fclose(hKernel);
        g_pOffsetTable[g_dwCurrentKernel + 1] = dwBinaryPos;
        return;
    }
    else
    {
        fseek(hKernel, 0, SEEK_END);
        dwFileSize = ftell(hKernel);
        fseek(hKernel, 0, SEEK_SET);
    }

    // print kernel name processed
    if (bVerbose)
    {
        fprintf(stderr, "%s\n", pKernelName);
    }

    // .hex file
    iLength = strlen(pKernelName);
    if (StrCmp(&pKernelName[iLength-4], ".hex", 4) == 0)
    {
        int    iLine = 1;
        char   pBinBuffer[MAX_STRING_SIZE];
        int    iBinLeft = MAX_STRING_SIZE;
        unsigned int * pBinCurrent = (unsigned int *)pBinBuffer;
        char  *pBuffCurrent = pBuffer;
        int    iBuffLeft  = 0;

        dwBytesRead = 0;
        do
        {
            // shift and read hex data
            if (iBuffLeft < 35)
            {
                // move incomplete line to the beginning of the buffer
                if (iBuffLeft)
                {
                    memcpy(pBuffer, pBuffCurrent, iBuffLeft);
                }

                // fill buffer (leave room for 0 at the end of the string)
                pBuffCurrent = pBuffer;
                if (dwBytesRead = fread(pBuffCurrent + iBuffLeft, 1, sizeof(pBuffer) - iBuffLeft - 1, hKernel))
                {
                    // add data
                    iBuffLeft += dwBytesRead;

                    // terminate string with 0 to avoid parsing issues
                    pBuffCurrent[iBuffLeft] = 0;

                    // no data to read - EOF
                    if (dwBytesRead == 0)
                    {
                        iBuffLeft = -1;
                    }
                }
                else
                {
                    iBuffLeft = -1;
                }
            }

            // process hex data one line at a time (35 chars => "xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx")
            while (iBuffLeft >= 35 && iBinLeft > 0)
            {
                // skip CR/LF/TAB (control characters)
                while (iBuffLeft > 35 && *pBuffCurrent < 32)
                {
                    pBuffCurrent++;
                    iBuffLeft--;
                }

                // convert hex to bin (4 DW = 16 B = 128 bits/line)
                if ( 4 != sscanf(pBuffCurrent, "%08x %08x %08x %08x", pBinCurrent, pBinCurrent+1, pBinCurrent+2, pBinCurrent+3) )
                {
                    fprintf(stderr, "Fail to process file %s at line %d\n", pKernelName, iLine);
                    exit(-1);
                }

                // line counter
                iLine++;

                // increment hex buffer
                pBuffCurrent += 35;
                iBuffLeft    -= 35;

                // increment bin buffer
                pBinCurrent += 4;
                iBinLeft    -= 4 * sizeof(unsigned int);

            }

            if (iBinLeft == 0 || iBuffLeft < 0)
            {
                // get actual size of binary buffer
                dwBytesRead = sizeof(pBinBuffer) - iBinLeft;

                dwBinaryPos = ftell(g_hKernelBinary);
                fwrite(pBinBuffer, 1, dwBytesRead, g_hKernelBinary);

                if (iBuffLeft >= 0)
                {
                    pBinCurrent = (unsigned int *)pBinBuffer;
                    iBinLeft    = sizeof(pBinBuffer);
                }
            }
        } while (dwBytesRead > 0 && iBuffLeft >= 0);
    }
    else
    {
        if (fread(pBuffer, 1, sizeof(pBuffer), hKernel))
        {
            dwBinaryPos = ftell(g_hKernelBinary);
            fwrite(pBuffer, 1, dwFileSize, g_hKernelBinary);
        }
    }

    dwBinaryPos = ftell(g_hKernelBinary) - g_dwOffsetTableSize;
    g_pOffsetTable[g_dwCurrentKernel+1] = dwBinaryPos;

    // close kernel file
    if (hKernel != NULL) fclose(hKernel);
}