xref: /aosp_15_r20/external/intel-media-driver/Tools/MediaDriverTools/GenDmyHex/main.cpp (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
1 #include <dirent.h>
2 #include <sys/stat.h>
3 #include <cstring>
4 #include <list>
5 #include <stdio.h>
6 #include <cstdlib>
7 #include <string>
8 #include <algorithm>
9 
10 #define MAX_STRING_SIZE  4096
11 #define MAX_KERNEL_NAME_LENGTH 100
12 #define MAX_CACHE_STRING_LENGTH (MAX_KERNEL_NAME_LENGTH + 10)
13 
14 using namespace std;
15 
16 #define StrCmp strncasecmp
17 
main(int argc,char * argv[])18 int main(int argc, char *argv[])
19 {
20 
21     if (argc != 3)
22     {
23         fprintf(stderr, "Usage: GenDmyHex.exe <kernel hex dir> <kernel header file> \n");
24         exit(-1);
25     }
26 
27     struct dirent* ent = NULL;
28 
29     char dirkrnhex[MAX_STRING_SIZE];
30     FILE *pfkrnheader = NULL;
31     DIR *pHexDir = opendir(argv[1]);
32 
33     if (!pHexDir)
34     {
35         fprintf(stderr, "Open kernel hex dir failed!\n");
36         exit(-1);
37     }
38 
39     if(sprintf(dirkrnhex, "%s", argv[1]) < 0)
40     {
41         fprintf(stderr, "Copy kernel hex dir failed!\n");
42         exit(-1);
43     }
44     strcat(dirkrnhex, "/");
45 
46     if(!(pfkrnheader = fopen(argv[2], "r")))
47     {
48         fprintf(stderr, "Read kernel header file failed!\n");
49         exit(-1);
50     }
51 
52     ///////////////////////////////////////////////////////////
53     //Get Full kernel list from pre-built kernele header file//
54     ///////////////////////////////////////////////////////////
55 
56     list <string> KernelFullList;
57     char scache[MAX_CACHE_STRING_LENGTH];
58 
59     while (fgets(scache, MAX_CACHE_STRING_LENGTH, pfkrnheader))
60     {
61         string strcache(scache);
62         if (strcache.size() < 16)
63         {
64             continue;
65         }
66         string head = strcache.substr(8, 6);
67         if (!head.compare("IDR_VP"))
68         {
69             strcache.erase(0, 15);
70             strcache.erase(strcache.find(" "), strcache.size());
71             if (!strcache.compare("TOTAL_NUM_KERNELS") ||
72                 !strcache.compare("KERNEL_NAMES") ||
73                 !strcache.compare("LINKFILE_HEADER") ||
74                 !strcache.compare("LINKFILE_VERSION") ||
75                 !strcache.compare("LinkFile")
76                 )
77             {
78                 continue;
79             }
80             strcache.append(".hex");
81             KernelFullList.push_back(strcache);
82         }
83     }
84 
85     fclose(pfkrnheader);
86 
87     ////////////////////////////////////////////////////////////////////////////
88     // Remove a superset list of all the kernels according to generated kernel//
89     ////////////////////////////////////////////////////////////////////////////
90 
91     char dirNewfile[MAX_STRING_SIZE];
92     char dirOldfile[MAX_STRING_SIZE];
93 
94     while (NULL != (ent=readdir(pHexDir)))
95     {
96         int n = strlen(ent->d_name);
97         if (n < 4 || (StrCmp(ent->d_name + n - 4, ".hex", 4) != 0))
98         {
99             continue;
100         }
101 
102         //Rename kernel name which contains "Dscale" to "DScale" except for "P010_444Dscale16_Buf*"
103         string sfilename(ent->d_name);
104         size_t pos = sfilename.find("Dscale");
105         if (pos != string::npos)
106         {
107             string ssub = sfilename.substr(0, 9);
108             if (ssub.compare("P010_444D"))
109             {
110                 sfilename.replace(pos, 6, "DScale");
111                 strcpy(dirNewfile, dirkrnhex);
112                 strcpy(dirOldfile, dirkrnhex);
113                 strcat(dirOldfile, ent->d_name);
114                 strcat(dirNewfile, sfilename.c_str());
115                 if (rename(dirOldfile, dirNewfile))
116                 {
117                     fprintf(stderr, "Rename file %s to %s failed!\n", dirOldfile, dirNewfile);
118                     exit(-1);
119                 }
120             }
121 
122         }
123 
124         // Remove the generated kernel
125         list <string>::iterator iter = find(KernelFullList.begin(), KernelFullList.end(), sfilename);
126         if (iter != KernelFullList.end())
127         {
128             KernelFullList.erase(iter);
129         }
130     }
131 
132     closedir(pHexDir);
133 
134     ////////////////////////////////////////////////////////////////////////////////////
135     // Create a set of dummy kernel hex that are not included in the generated kernels//
136     ////////////////////////////////////////////////////////////////////////////////////
137 
138     while (!KernelFullList.empty())
139     {
140         string skernelhex = KernelFullList.front();
141         strcpy(dirNewfile, dirkrnhex);
142         strcat(dirNewfile, skernelhex.c_str());
143         FILE *pfkernelhex = NULL;
144         if (!(pfkernelhex = fopen(dirNewfile, "wb")))
145         {
146             fprintf(stderr, "Create dummy kernel hex file failed!\n");
147             exit(-1);
148         }
149         else
150         {
151             fclose(pfkernelhex);
152         }
153         KernelFullList.pop_front();
154     }
155 
156     fprintf(stdout, "Create dummy kernel hex files successfully!\n");
157     return 0;
158 }
159