xref: /aosp_15_r20/external/intel-media-driver/Tools/MediaDriverTools/KrnToHex/KrnToHex.cpp (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
1 /*
2 * Copyright (c) 2019, 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 #include <stdlib.h>
26 
27 
28 #define BUFFER_SIZE   4096
29 #define MAX_FILE_PATH 4096
30 #define MAX_FILE_NAME 260
31 
32 #define StrCmp strcasecmp
33 
34 
usage()35 int usage()
36 {
37     fprintf(stderr, "KernelToHex <source file>\r\n"   \
38         "Convert krn into hex file (single file)\r\n\r\n");
39     return -1;
40 }
41 
KrnToHex(char * file_krn)42 bool KrnToHex(char *file_krn)
43 {
44     char     buffer[BUFFER_SIZE];
45     char     *aux_file_hex;
46     char     line[40];
47     FILE     *f_in, *f_out;
48     char     * file_hex = nullptr;
49 
50     unsigned int   *pbuffer;
51     unsigned int    n_read;
52 
53     int      i;
54     char     *pfile_handle;
55     aux_file_hex = (char *)malloc(MAX_FILE_PATH);
56     // Generate destination file name (if not provided)
57     if (aux_file_hex)
58     {
59         memset(aux_file_hex, 0, MAX_FILE_PATH);
60 
61         file_hex = aux_file_hex;
62         i = strlen(file_krn) - 4;
63         memcpy(file_hex, file_krn, strlen(file_krn) - 4);
64 
65         sprintf(aux_file_hex, "%s.hex", file_hex);
66     }
67 
68     // Open files
69      f_in = fopen(file_krn, "rb");
70      f_out = fopen(file_hex, "wb");
71 
72     // Check if files successfully open/created
73     if ((f_in != NULL) && (f_out != NULL))
74     {
75         while (true)
76         {
77             n_read = fread(buffer, sizeof(char), BUFFER_SIZE, f_in);
78             if (n_read == 0)
79             {
80                 break;
81             }
82 
83             pbuffer = (unsigned int *)buffer;
84 
85             for (; n_read >= 16; n_read -= 16, pbuffer += 4)
86             {
87                 sprintf(line, "%08x %08x %08x %08x\r\n", pbuffer[0], pbuffer[1], pbuffer[2], pbuffer[3]);
88                 fwrite(line, 1, 37, f_out);
89             }
90 
91             if (n_read > 0)
92             {
93                 for (; n_read > 4; n_read -= 4, pbuffer++)
94                 {
95                     sprintf(line, "%08x ", pbuffer[0]);
96                     fwrite(line, 1, 9, f_out);
97                 }
98                 sprintf(line, "%08x\r\n", pbuffer[0]);
99                 fwrite(line, 1, 10, f_out);
100             }
101         }
102     }
103     else
104     {
105         if (f_in == NULL)
106         {
107             fprintf(stderr, "Failed to open input file \"%s\"\r\n", file_krn);
108         }
109 
110         if (f_out == NULL)
111         {
112             fprintf(stderr, "Failed to create output file \"%s\"\r\n", file_hex);
113         }
114     }
115 
116 
117     if (f_in != NULL)
118     {
119         fclose(f_in);
120     }
121 
122     if (f_out != NULL)
123     {
124         fclose(f_out);
125     }
126 
127     if (aux_file_hex != nullptr)
128     {
129         free(aux_file_hex);
130         aux_file_hex = nullptr;
131     }
132 
133     return true;
134 }
135 
main(int argc,char * argv[])136 int main(int argc, char* argv[])
137 {
138     char *p = NULL;
139 
140     if (argc == 2)
141     {
142         p = strrchr(argv[1], '.');
143 
144         if (p != NULL && StrCmp(p, ".krn") == 0)
145         {
146             KrnToHex(argv[1]);
147         }
148     }
149     else
150     {
151         return (usage());
152     }
153 
154     return 0;
155 }
156