1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2006-2007 MStar Semiconductor, Inc.
4 // All rights reserved.
5 //
6 // Unless otherwise stipulated in writing, any and all information contained
7 // herein regardless in any format shall remain the sole proprietary of
8 // MStar Semiconductor Inc. and be kept in strict confidence
9 // (¡§MStar Confidential Information¡¨) by the recipient.
10 // Any unauthorized act including without limitation unauthorized disclosure,
11 // copying, use, reproduction, sale, distribution, modification, disassembling,
12 // reverse engineering and compiling of the contents of MStar Confidential
13 // Information is unlawful and strictly prohibited. MStar hereby reserves the
14 // rights to any and all damages, losses, costs and expenses resulting therefrom.
15 //
16 ////////////////////////////////////////////////////////////////////////////////
17
18 ///////////////////////////////////////////////////////////////////////////////////////////////////
19 ///
20 /// file main.c
21 /// @brief main entry point of the application program
22 /// @author MStar Semiconductor Inc.
23 ///////////////////////////////////////////////////////////////////////////////////////////////////
24
25 //-------------------------------------------------------------------------------------------------
26 // Include Files
27 //-------------------------------------------------------------------------------------------------
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 //-------------------------------------------------------------------------------------------------
33 // Local Defines
34 //-------------------------------------------------------------------------------------------------
35 #define VERSIONEMBADDER_ERROR(fmt, args...) printf("[VERSION_EMBADDER ERR][%06d] " fmt, __LINE__, ## args)
36 #define VERSIONEMBADDER_DEBUG(fmt, args...) //printf("[VERSION_EMBADDER DEBUG][%06d] " fmt, __LINE__, ## args)
37 #define STRLEN 32
38 #define CHNAGEINDX 9
39
40 //-------------------------------------------------------------------------------------------------
41 // Macros
42 //-------------------------------------------------------------------------------------------------
43
44 //-------------------------------------------------------------------------------------------------
45 // Global Variables
46 //-------------------------------------------------------------------------------------------------
47 static char UtopiaVersionStr[]="UTOPIA_VERSION_2.0";
48 static char UtopiaChangeListStr[]="$Change:";
49
50 typedef struct _MS_SW_VERSION_NUM
51 {
52 char UtopiaBspVersion[STRLEN];
53 int UtopiaVerNum;
54 }MS_SW_VERSION_NUM;
55
56 struct version_node
57 {
58 char* pVer;
59 struct version_node *next;
60 };
61
62 //-------------------------------------------------------------------------------------------------
63 // Local functions
64 //-------------------------------------------------------------------------------------------------
push(struct version_node ** pHead,char * pVer)65 int push(struct version_node **pHead, char* pVer)
66 {
67 struct version_node *pNode = (struct version_node *)malloc(sizeof(struct version_node));
68
69 if(!pNode)
70 {
71 VERSIONEMBADDER_ERROR("pNode creation failed\n");
72 return 1;
73 }
74
75 pNode->pVer = pVer;
76 pNode->next = *pHead;
77 *pHead = pNode;
78
79 return 0;
80 }
81
pop(struct version_node ** pHead)82 char* pop(struct version_node **pHead)
83 {
84 char* pVer;
85 struct version_node *pPreHead;
86
87 if((*pHead)==NULL)
88 {
89 VERSIONEMBADDER_DEBUG("pHead is empty\n");
90 return NULL;
91 }
92
93 pPreHead = *pHead;
94 pVer = (*pHead)->pVer;
95 *pHead = (*pHead)->next;
96 free(pPreHead);
97
98 return pVer;
99 }
100
101 //-------------------------------------------------------------------------------------------------
102 // Memory dump
103 //-------------------------------------------------------------------------------------------------
memdump(char * ptr,size_t size)104 void memdump(char*ptr, size_t size)
105 {
106 long count;
107
108 for(count=0; count < size; count++)
109 {
110 printf("%x ", *(ptr+count));
111 if((count%16) == 15)
112 {
113 printf("\n");
114 }
115 }
116
117 printf("\n");
118 }
119
120 //-------------------------------------------------------------------------------------------------
121 // Main function
122 //-------------------------------------------------------------------------------------------------
main(int argc,char * argv[])123 int main(int argc, char* argv[])
124 {
125 struct version_node *pVesList = NULL;
126 FILE* pFileLibR = NULL;
127 FILE* pFileLibW = NULL;
128 FILE* pFileVerR = NULL;
129 size_t LibRSize;
130 size_t VerRSize;
131 char* pbufLibR;
132 char* pbufVerR;
133 char* pbuf;
134 size_t result;
135 long count;
136 unsigned int* pLibVer;
137 unsigned int changelist;
138
139 pFileLibR = fopen(argv[1], "rb");
140 pFileLibW = fopen(argv[2], "wb");
141 pFileVerR = fopen(argv[3], "rb");
142
143 /* obtain files */
144 if(NULL == pFileLibR)
145 {
146 VERSIONEMBADDER_ERROR("fopen(%s) error!!!\n", argv[1]);
147 return 0;
148 }
149
150 if(NULL == pFileLibW)
151 {
152 VERSIONEMBADDER_ERROR("fopen(%s) error!!!\n", argv[2]);
153 return 0;
154 }
155
156 if(NULL == pFileVerR)
157 {
158 VERSIONEMBADDER_ERROR("fopen(%s) error!!!\n", argv[3]);
159 return 0;
160 }
161
162 /* obtain original library file size */
163 fseek (pFileLibR , 0 , SEEK_END);
164 LibRSize = ftell(pFileLibR);
165 rewind (pFileLibR);
166
167 fseek (pFileVerR , 0 , SEEK_END);
168 VerRSize = ftell(pFileVerR);
169 rewind (pFileVerR);
170
171 /* allocate memory to contain the whole file */
172 pbufLibR = (char*) malloc ((size_t)LibRSize);
173 if(pbufLibR == NULL)
174 {
175 VERSIONEMBADDER_ERROR("malloc error!!!\n");
176 return 0;
177 }
178
179 pbufVerR = (char*) malloc ((size_t)VerRSize);
180 if(pbufVerR == NULL)
181 {
182 VERSIONEMBADDER_ERROR("malloc error!!!\n");
183 return 0;
184 }
185
186 /* copy the file into the buffer */
187 result = fread (pbufLibR, 1, LibRSize, pFileLibR);
188 if (result != LibRSize)
189 {
190 fclose(pFileLibR);
191 fclose(pFileLibW);
192 fclose(pFileVerR);
193 free(pbufLibR);
194 free(pbufVerR);
195 VERSIONEMBADDER_ERROR ("fread(%s) error!!! with result:%ld\n", argv[1], result);
196 return 0;
197 }
198
199 result = fread (pbufVerR, 1, VerRSize, pFileVerR);
200 if (result != VerRSize)
201 {
202 fclose(pFileLibR);
203 fclose(pFileLibW);
204 fclose(pFileVerR);
205 free(pbufLibR);
206 free(pbufVerR);
207 VERSIONEMBADDER_ERROR ("fread(%s) error!!! with result:%ld\n", argv[3], result);
208 return 0;
209 }
210
211 VERSIONEMBADDER_DEBUG("###### version info in original utopia lib ######\n");
212
213 /* in utopia library, find out where "UtopiaVersionStr" is */
214 pbuf = NULL;
215
216 for(count=0; count < LibRSize; count++)
217 {
218 if (strncmp(pbufLibR+count, UtopiaVersionStr, sizeof(UtopiaVersionStr)) == 0)
219 {
220 pbuf = pbufLibR+count;
221 VERSIONEMBADDER_DEBUG("pbuf: %s\n", pbuf);
222 VERSIONEMBADDER_DEBUG("offset_in_file: 0x%lx\n", count);
223 push(&pVesList, pbuf + STRLEN);
224
225 }
226 }
227
228 if(pbuf == NULL)
229 {
230 VERSIONEMBADDER_ERROR("%s no found!!!\n", UtopiaVersionStr);
231 fwrite (pbufLibR , sizeof(char), LibRSize, pFileLibW);
232 fclose(pFileLibR);
233 fclose(pFileLibW);
234 fclose(pFileVerR);
235 free(pbufLibR);
236 free(pbufVerR);
237 return 0;
238 }
239
240 VERSIONEMBADDER_DEBUG("###### version info in version_info ######\n");
241
242 /* in version_info, find change list number*/
243 pbuf = NULL;
244 for(count = 0; count < VerRSize; count++)
245 {
246 if (strncmp(pbufVerR + count, UtopiaChangeListStr, sizeof(UtopiaChangeListStr) - 1) == 0)
247 {
248 pbuf = pbufVerR+count;
249 VERSIONEMBADDER_DEBUG("pbuf: %s\n", pbuf);
250 VERSIONEMBADDER_DEBUG("offset_in_file: 0x%lx\n", count);
251 }
252 }
253
254 if(!pbuf)
255 {
256 VERSIONEMBADDER_ERROR("can not find %s in %s\n", UtopiaChangeListStr, argv[3]);
257 fclose(pFileLibR);
258 fclose(pFileLibW);
259 fclose(pFileVerR);
260 free(pbufLibR);
261 free(pbufVerR);
262 return 0;
263 }
264
265 pbuf = pbuf + CHNAGEINDX;
266 changelist = strtol(pbuf, NULL, 10);
267 VERSIONEMBADDER_DEBUG("changelist: %d\n", changelist);
268
269 /* update UtopiaVerNum in utopia library */
270 while((pLibVer = (unsigned int* )pop(&pVesList)) != NULL)
271 {
272 VERSIONEMBADDER_DEBUG("pLibVer=0x%x\n", pLibVer);
273 VERSIONEMBADDER_DEBUG("UtopiaVerNum = 0x%x\n", *pLibVer);
274 *pLibVer = changelist;
275 }
276
277 /* copy the buffer into the file */
278 result = fwrite (pbufLibR , sizeof(char), LibRSize, pFileLibW);
279 if (result != LibRSize)
280 {
281 fclose(pFileLibR);
282 fclose(pFileLibW);
283 fclose(pFileVerR);
284 free(pbufLibR);
285 free(pbufVerR);
286 VERSIONEMBADDER_ERROR ("fwrite error!!! with result:%ld\n",result);
287 return 0;
288 }
289
290 /* terminate */
291 fclose(pFileLibR);
292 fclose(pFileLibW);
293 fclose(pFileVerR);
294 free(pbufLibR);
295 free(pbufVerR);
296
297 return 0;
298 }
299