xref: /aosp_15_r20/external/intel-media-driver/Tools/MediaDriverTools/KrnToHex_IGA/KrnToHex_IGA.cpp (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
1 /*
2 * Copyright (c) 2017, Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22 
23 #include <stdio.h>
24 #include <string.h>
25 
26 #define BUFFER_SIZE   4096
27 #define MAX_FILE_PATH 4096
28 #define MAX_FILE_NAME 260
29 
30 #ifdef LINUX_
31 #define StrCmp strcasecmp
32 #else
33 #define StrCmp stricmp
34 #endif
35 
usage()36 int usage()
37 {
38     fprintf(stderr, "KernelToHex <source file>\r\n"   \
39                     "Convert krn into hex file (single file)\r\n\r\n");
40     return -1;
41 }
42 
KrnToHex(char * file_krn,char * file_hex)43 bool KrnToHex(char *file_krn, char *file_hex)
44 {
45     char     buffer[BUFFER_SIZE];
46     char     aux_file_hex[MAX_FILE_PATH];
47     char     line[40];
48     FILE     *f_in, *f_out;
49 
50     unsigned int   *pbuffer;
51     unsigned int    n_read;
52 
53     int      i;
54     char     *pfile_handle;
55 
56     // Generate destination file name (if not provided)
57     if (!file_hex)
58     {
59         file_hex = aux_file_hex;
60 
61         strncpy(file_hex, file_krn, MAX_FILE_PATH - 5);
62         file_hex[MAX_FILE_PATH - 5] = 0;
63         i = (int)strlen(file_hex);
64 
65         pfile_handle = strrchr(file_hex, '.');
66         if (!pfile_handle)
67         {
68             pfile_handle = file_hex + i;
69         }
70         sprintf(pfile_handle, ".hex");
71     }
72 
73     // Open files
74     f_in  = fopen(file_krn, "rb");
75     f_out = fopen(file_hex, "wb");
76 
77     // Check if files successfully open/created
78     if ((f_in != NULL) && (f_out != NULL))
79     {
80         while (true)
81         {
82             n_read = fread(buffer, sizeof(char), BUFFER_SIZE, f_in);
83             if (n_read == 0)
84             {
85                 break;
86             }
87 
88             pbuffer = (unsigned int *)buffer;
89 
90             for (; n_read >= 16; n_read -= 16, pbuffer+=4)
91             {
92                 sprintf(line, "%08x %08x %08x %08x\r\n", pbuffer[0],  pbuffer[1], pbuffer[2], pbuffer[3]);
93                 fwrite(line, 1, 37, f_out);
94             }
95 
96             if(n_read > 0)
97             {
98                 for (; n_read > 4; n_read -= 4, pbuffer++)
99                 {
100                     sprintf(line, "%08x ", pbuffer[0]);
101                     fwrite(line, 1, 9, f_out);
102                 }
103                 sprintf(line, "%08x\r\n", pbuffer[0]);
104                 fwrite(line, 1, 10, f_out);
105             }
106         }
107     }
108     else
109     {
110         if (f_in == NULL)
111         {
112             fprintf(stderr, "Failed to open input file \"%s\"\r\n", file_krn);
113         }
114 
115         if (f_out == NULL)
116         {
117             fprintf(stderr, "Failed to create output file \"%s\"\r\n", file_hex);
118         }
119     }
120 
121     // Add 128 bytes padding at the end of each biniary
122     for (int i = 0; i < 8; i++)
123     {
124         sprintf(line, "%08x %08x %08x %08x\r\n", 0, 0, 0, 0);
125         fwrite(line, 1, 37, f_out);
126     }
127 
128     if (f_in != NULL)
129     {
130         fclose(f_in);
131     }
132 
133     if (f_out != NULL)
134     {
135         fclose(f_out);
136     }
137 
138     return true;
139 }
140 
main(int argc,char * argv[])141 int main(int argc, char* argv[])
142 {
143     char *p = NULL;
144 
145     if (argc == 2)
146     {
147         p = strrchr(argv[1], '.');
148 
149         if (p != NULL && StrCmp(p, ".krn") == 0)
150         {
151             KrnToHex(argv[1], NULL);
152         }
153     }
154     else
155     {
156         return (usage());
157     }
158 
159     return 0;
160 }
161