XIMMaker.cpp
16.2 KB
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/*****************************************************************************
* Copyright Statement:
* --------------------
* This software is protected by Copyright and the information contained
* herein is confidential. The software may not be copied and the information
* contained herein may not be used or disclosed except with the written
* permission of MediaTek Inc. (C) 2006
*
* BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
* RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
* AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
* NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
* SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
* SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
* THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
* NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
* SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
* BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
* LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
* AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
* OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
* MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
*
* THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
* WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
* LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
* RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
* THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/
// XIMMaker.cpp : Defines the entry point for the console application.
//
#pragma warning(disable:4786)
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <Windows.h>
#include <fstream>
#include "boost/foreach.hpp"
#include "boost/shared_ptr.hpp"
#include "XGetopt.h"
#include "br_GFH_dl_package_info.h"
#include "DigitalSignature.h"
#include "Utility.h"
#define MAX_EXTRA_INFO_NUM 16
using std::ios_base;
using std::vector;
using std::string;
using std::ifstream;
using std::ofstream;
using std::cout;
using std::endl;
using std::equal;
using std::copy;
using boost::shared_ptr;
static bool AddCurrentDirPathStr(string& file)
{
vector<char> currentDir(1024, 0);
GetCurrentDirectory(currentDir.size(), ¤tDir[0]);
file = string(¤tDir[0]) + '\\' + file;
return true;
}
static bool CheckFilePathAbsolute(const string *filepath)
{
// two kind of path will be regarded as absolute path
// has :\ , ex: C:\...
// has \\ , ex: \\xxxxx
if(filepath->rfind("\\\\") != string::npos || filepath->rfind(":\\") != string::npos)
{
return true;
}
else
return false;
}
static bool ParseArguments(int argc, char* argv[],
string& INI_File,
string& IM_file,
string& XIM_file)
{
int arg;
while ((arg = getopt(argc, argv, "s:i:o:")) != EOF)
{
switch (arg)
{
case 's':
INI_File = optarg;
cout << "Get INI File settings" << endl;
break;
case 'i':
IM_file = optarg;
cout << "Get IM File settings" << endl;
break;
case 'o':
XIM_file = optarg;
cout << "Get XIM File settings" << endl;
break;
default:
cout << "WARNING: no handler for option -" << arg << endl;
return false;
}
}
if (INI_File.empty())
{
cout << "Please specify a config file with \"-s\" option" << endl;
return false;
}
if (IM_file.empty())
{
cout << "Please specify an input file with \"-i\" option" << endl;
return false;
}
if (XIM_file.empty())
{
cout << "Please specify an output file with \"-o\" option" << endl;
return false;
}
// Add current directory path before the file name
if(!CheckFilePathAbsolute(&INI_File))
AddCurrentDirPathStr(INI_File);
printf("INI_file:%s\n", INI_File.c_str());
if(!CheckFilePathAbsolute(&IM_file))
AddCurrentDirPathStr(IM_file);
printf("IM_file:%s\n", IM_file.c_str());
if(!CheckFilePathAbsolute(&XIM_file))
AddCurrentDirPathStr(XIM_file);
printf("XIM_file:%s\n", XIM_file.c_str());
return true;
}
static bool FileExists(const char *fileName)
{
return (GetFileAttributes(fileName) != 0xFFFFFFFF);
}
static bool CheckFileExists(const string& file)
{
if (!FileExists(file.c_str()))
{
/*
string filename = file;
AddCurrentDirPathStr(filename);
if (!FileExists(filename.c_str()))
{*/
cout << file << " does not exist!" << endl;
return false;
/*
}
exist_file = filename;
*/
}
return true;
}
void STR2M1(char *str, unsigned short *m1, unsigned int &m1_len) {
char *p;
// parse STR to m1 array
for(m1_len=0, p=strtok(str, ", \t\r\n"); NULL!=p; m1_len++, p=strtok(NULL, ", \t\r\n")) {
m1[m1_len] = (unsigned short)(strtoul(p, NULL, 16)&0x0000FFFF);
}
}
static bool GetConfig(const string& configFile,
int& IM_format,
string& platform_id,
string& project_id,
bool& BL_debug_flag)
{
if(false == CheckFileExists(configFile))
{
cout << "ERROR: CheckFileExists() fail!" << endl;
return false;
}
vector<string> sections;
bool result = GetProfileSectionFromINI(configFile, sections);
if(!result)
{
printf("Fail!\n");
}
int ret = 0;
//
// Reade INI file for [XIMGenerator Config]
//
string sectionName = "XIMGenerator Config";
// get IM_format [Must]
{
//IM_format = 1; // default is NAND
IM_format = IM_NOR; // default is NOR or serial Flash
vector<char> buf(256, 0);
ret = ::GetPrivateProfileString(sectionName.c_str(),
"ImageDevice", "", &buf[0], buf.size(), configFile.c_str());
string imageDevice(&buf[0]);
if(FindStringWithoutCaseSensitive(imageDevice, "NAND"))
{
IM_format = IM_NAND;
}
/*
if(FindStringWithoutCaseSensitive(imageDevice, "NOR"))
{
IM_format = 0;
}
*/
}
// get platform_id [Must]
{
vector<char> buf(256, 0);
ret = ::GetPrivateProfileString(sectionName.c_str(),
"Handset_platform_id_to_apply", "", &buf[0], buf.size(), configFile.c_str());
platform_id = reinterpret_cast<char *>(&buf[0]);
// Check the length of platform ID
if(platform_id.size() >= 128)
{
cout << "ERROR: \"Handset_platform_id_to_apply\" can't accept more than 127 characters." << endl;
return false;
}
}
// get project_id [Must]
{
vector<char> buf(256, 0);
ret = ::GetPrivateProfileString(sectionName.c_str(),
"Handset_project_id_to_apply", "", &buf[0], buf.size(), configFile.c_str());
project_id = reinterpret_cast<char *>(&buf[0]);
// Check the length of project ID
if(project_id.size() >= 64)
{
cout << "ERROR: \"Handset_project_id_to_apply\" can't accept more than 63 characters." << endl;
return false;
}
}
// get BL Debug flag [Optional]
{
BL_debug_flag = false;
vector<char> buf(256, 0);
ret = ::GetPrivateProfileString(sectionName.c_str(),
"BL_Debug", "", &buf[0], buf.size(), configFile.c_str());
string bootloaderDebugFlag(&buf[0]);
if(FindStringWithoutCaseSensitive(bootloaderDebugFlag, "ON"))
{
cout << "BL Debug ON" << endl;
BL_debug_flag = true;
}
}
return true;
}
static bool AppendSignature(const int sigSize,
const DL_PACKAGE_GFH& header,
const vector<unsigned char>& IMContent,
const unsigned int IMHeaderSize,
vector<unsigned char>& XIM_content)
{
vector<unsigned char> sig1(sigSize, 0);
vector<unsigned char> sig2(sigSize, 0);
// copy DL_PACKAGE_GFH
CopyMemory(&XIM_content[0], &header, sizeof(DL_PACKAGE_GFH));
// copy .IM content
CopyMemory(&XIM_content[sizeof(DL_PACKAGE_GFH)], &IMContent[0], IMContent.size() );
// Sign (DL_PACKAGE_GFH header + NAND Image Header)
if(!Sign(&XIM_content[0], sizeof(DL_PACKAGE_GFH)+IMHeaderSize, &sig1[0]))
return false;
// Sign (DL_PACKAGE_GFH header + .IM file)
if(!Sign(&XIM_content[0], sizeof(DL_PACKAGE_GFH)+IMContent.size(), &sig2[0]))
return false;
// copy signature
CopyMemory(&XIM_content[sizeof(DL_PACKAGE_GFH)+IMContent.size()], &sig1[0], sigSize);
CopyMemory(&XIM_content[sizeof(DL_PACKAGE_GFH)+IMContent.size()+sigSize], &sig2[0], sigSize);
return true;
}
static bool SaveXIMContent(vector<unsigned char>& XIMContent, string& filename)
{
ofstream out(filename.c_str(), ofstream::binary | ofstream::out);
if (!out.is_open())
{
cout << "Cannot open \"" << filename << "\"" << endl;
return false;
}
out.write(reinterpret_cast<const char *>(&XIMContent[0]), XIMContent.size());
out.close();
return true;
}
static bool GetAllBinFileInfoByDevice(string& configFile, IM_FORMAT imFormat,
GFH_DL_PACKAGE_EXTRA_INFO* extraInfo, int extraInfoSize)
{
if(false == CheckFileExists(configFile))
{
cout << "ERROR: CheckFileExists() fail!" << endl;
return false;
}
if(imFormat != IM_NAND && imFormat != IM_NOR)
{
cout << "ERROR: Unknown IM_FORMAT !" << endl;
return false;
}
const unsigned int REMAP_BASE = 0x08000000;
memset(extraInfo, 0, sizeof(GFH_DL_PACKAGE_EXTRA_INFO)*extraInfoSize);
int index = 0; // index for GFH_DL_PACKAGE_EXTRA_INFO count
vector<string> sectionVectorINI;
if(!GetProfileSectionFromINI(configFile, sectionVectorINI))
{
return false;
}
for(int i = 0; i < sectionVectorINI.size(); i++)
{
MTK_BIN_TYPE_E binType = CheckBinType(sectionVectorINI[i]);
if(MTK_BIN_UNKNOWN == binType)
{
continue;
}
else
{
switch(binType)
{
case MTK_BIN_MAUI:
if( !FillExtraInfoByBinType(configFile, imFormat,
sectionVectorINI[i].c_str(), GetMauiFileTypeByString(sectionVectorINI[i]),
extraInfo, index))
{
cout << "ERROR: Fill extraInfo fail!" << endl;
return false;
}
break;
case MTK_BIN_RESOUCE:
if( !FillExtraInfoByBinType(configFile, imFormat,
sectionVectorINI[i].c_str(), V_RESOURCE_BINARY,
extraInfo, index))
{
cout << "ERROR: Fill extraInfo fail!" << endl;
return false;
}
break;
case MTK_BIN_CUSTOM:
if( !FillExtraInfoByBinType(configFile, imFormat,
sectionVectorINI[i].c_str(), GetCutomeFileTypeByString(sectionVectorINI[i]),
extraInfo, index))
{
cout << "ERROR: Fill extraInfo fail!" << endl;
return false;
}
break;
case MTK_BIN_FILE_SYSTEM:
if( !FillExtraInfoByBinType(configFile, imFormat,
sectionVectorINI[i].c_str(), V_FILE_SYSTEM_BINARY,
extraInfo, index))
{
cout << "ERROR: Fill extraInfo fail!" << endl;
return false;
}
break;
default:
cout << "ERROR: UNKNOWN TYPE BIN" << endl;
return false;
}
if(index >= extraInfoSize)
{
cout << "WARNING: Too many different bin file types !" << endl;
return false;
}
}
}
return true;
}
static void DisplayExeVer(void)
{
TCHAR szFullPath[MAX_PATH];
DWORD dwVerInfoSize = 0;
DWORD dwVerHnd;
VS_FIXEDFILEINFO * pFileInfo;
GetModuleFileName(NULL, szFullPath, sizeof(szFullPath));
dwVerInfoSize = GetFileVersionInfoSize(szFullPath, &dwVerHnd);
if (dwVerInfoSize)
{
// If we were able to get the information, process it:
HANDLE hMem;
LPVOID lpvMem;
unsigned int uInfoSize = 0;
hMem = GlobalAlloc(GMEM_MOVEABLE, dwVerInfoSize);
lpvMem = GlobalLock(hMem);
GetFileVersionInfo(szFullPath, dwVerHnd, dwVerInfoSize, lpvMem);
::VerQueryValue(lpvMem, (LPTSTR)("\\"), (void**)&pFileInfo, &uInfoSize);
WORD m_nFileVersion[4];
// Product version from the FILEVERSION of the version info resource
m_nFileVersion[0] = HIWORD(pFileInfo->dwFileVersionMS);
m_nFileVersion[1] = LOWORD(pFileInfo->dwFileVersionMS);
m_nFileVersion[2] = HIWORD(pFileInfo->dwFileVersionLS);
m_nFileVersion[3] = LOWORD(pFileInfo->dwFileVersionLS);
cout << "------------------------------" << endl;
cout << "XIM_Maker.exe Ver:" << m_nFileVersion[0] << "." << m_nFileVersion[1] << "." << m_nFileVersion[2] << endl;
cout << "------------------------------" << endl;
GlobalUnlock(hMem);
GlobalFree(hMem);
}
else
{
cout << "[Warning] GetFileVersionInfoSize failed" << endl;
return;
}
}
int main(int argc, char* argv[])
{
//
// 1. Parse the command line arguments
//
string strINI; // config file
string strIM; // input .IM file
string strXIM; // output .XIM file
// dump version information
DisplayExeVer();
if(false == ParseArguments(argc, argv, strINI, strIM, strXIM))
{
cout << "ERROR: ParseArguments() fail! " << endl;
return 1;
}
//
// 2. Get general info from config file
//
int IMFormat;
string platformID;
string projectID;
bool debugFlagBL;
if(false == GetConfig(strINI, IMFormat, platformID, projectID, debugFlagBL))
{
cout << "ERROR: GetConfig() fail!" << endl;
return 2;
}
//
// 3. Get device dependent info from config file
//
GFH_DL_PACKAGE_EXTRA_INFO extraInfo[MAX_EXTRA_INFO_NUM];
memset(extraInfo, 0, sizeof(GFH_DL_PACKAGE_EXTRA_INFO)*MAX_EXTRA_INFO_NUM);
if(false == GetAllBinFileInfoByDevice(strINI, (IM_FORMAT)IMFormat,
extraInfo, MAX_EXTRA_INFO_NUM ))
{
cout << "ERROR: GetAllBinFileInfoByDevice() fail!" << endl;
return 3;
}
// 3.1: check if the bin update rules are matched
if(false == CheckAllBinFileInfoByDevice((IM_FORMAT)IMFormat,
extraInfo, MAX_EXTRA_INFO_NUM))
{
cout << "ERROR: CheckAllBinFileInfoByDevice() fail!" << endl;
return 3;
}
//
// 4. Read .IM file and parse the NAND Image Header
//
vector<unsigned char> IMContent;
int IMHeaderSize = 0;
if(false == ReadIMFileAndHeaderSize((IM_FORMAT)IMFormat, strIM,
IMContent,IMHeaderSize))
{
cout << "ERROR: ReadIMFileAndHeaderSize() fail" << endl;
return 4;
}
//
// 5. Create DL_PACKAGE_GFH header
//
int sigSize = GetSigLen();
DL_PACKAGE_GFH XIMHeader;
if(false == CreateHeaderXIM(IMFormat, platformID, projectID, debugFlagBL,
IMContent.size(), sigSize, XIMHeader, extraInfo,
MAX_EXTRA_INFO_NUM))
{
cout << "ERROR: CreateHeaderXIM() fail!" << endl;
return 5;
}
//
// 6. Sign (DL_PACKAGE_GFH+ NAND Image Header) AND (.IM file without NAND Image Header)
//
vector<unsigned char> XIMOutputContent(
sizeof(DL_PACKAGE_GFH)+IMContent.size()+2*sigSize , 0);
if(false == AppendSignature(sigSize, XIMHeader, IMContent, IMHeaderSize ,XIMOutputContent))
{
cout << "ERROR: AppendSignature() fail!" << endl;
return 6;
}
//
// 7. Save XIM content
//
if(false == SaveXIMContent(XIMOutputContent, strXIM))
{
cout << "ERROR: SaveXIMContent() fail!" << endl;
return 7;
}
cout << "Succesfully generate " << strXIM << " file" << endl;
return 0;
}