-
Notifications
You must be signed in to change notification settings - Fork 43
/
RKBoot.cpp
314 lines (304 loc) · 6.59 KB
/
RKBoot.cpp
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
/*
* (C) Copyright 2017 Fuzhou Rockchip Electronics Co., Ltd
* Seth Liu 2017.03.01
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include "RKBoot.h"
extern UINT CRC_32(PBYTE pData, UINT ulSize);
bool CRKBoot::GetRc4DisableFlag()
{
return m_bRc4Disable;
}
bool CRKBoot::GetSignFlag()
{
return m_bSignFlag;
}
DWORD CRKBoot::GetVersion()
{
return m_version;
}
DWORD CRKBoot::GetMergeVersion()
{
return m_mergeVersion;
}
STRUCT_RKTIME CRKBoot::GetReleaseTime()
{
return m_releaseTime;
}
ENUM_RKDEVICE_TYPE CRKBoot::GetSupportDevice()
{
return m_supportDevice;
}
UCHAR CRKBoot::GetEntry471Count()
{
return m_471Count;
}
UCHAR CRKBoot::GetEntry472Count()
{
return m_472Count;
}
UCHAR CRKBoot::GetEntryLoaderCount()
{
return m_loaderCount;
}
bool CRKBoot::CrcCheck()
{
UINT*pOldCrc,ulNewCrc;
pOldCrc = (UINT*)(m_BootData+(m_BootSize-4));
ulNewCrc = CRC_32(m_BootData,m_BootSize-4);
return (*pOldCrc==ulNewCrc)?true:false;
}
bool CRKBoot::SaveEntryFile(ENUM_RKBOOTENTRY type,UCHAR ucIndex,string fileName)
{
DWORD dwOffset;
UCHAR ucCount,ucSize;
switch ( type )
{
case ENTRY471:
dwOffset = m_471Offset;
ucCount = m_471Count;
ucSize = m_471Size;
break;
case ENTRY472:
dwOffset = m_472Offset;
ucCount = m_472Count;
ucSize = m_472Size;
break;
case ENTRYLOADER:
dwOffset = m_loaderOffset;
ucCount = m_loaderCount;
ucSize = m_loaderSize;
break;
default:
return false;
}
if (ucIndex >= ucCount)
{
return false;
}
PSTRUCT_RKBOOT_ENTRY pEntry;
pEntry = (PSTRUCT_RKBOOT_ENTRY)(m_BootData+dwOffset+(ucSize*ucIndex));
FILE *file=NULL;
file = fopen(fileName.c_str(),"wb+");
if ( !file )
{
return false;
}
fwrite(m_BootData+pEntry->dwDataOffset,1,pEntry->dwDataSize,file);
fclose(file);
return true;
}
bool CRKBoot::GetEntryProperty(ENUM_RKBOOTENTRY type,UCHAR ucIndex,DWORD &dwSize,DWORD &dwDelay,char *pName)
{
DWORD dwOffset;
UCHAR ucCount,ucSize;
switch ( type )
{
case ENTRY471:
dwOffset = m_471Offset;
ucCount = m_471Count;
ucSize = m_471Size;
break;
case ENTRY472:
dwOffset = m_472Offset;
ucCount = m_472Count;
ucSize = m_472Size;
break;
case ENTRYLOADER:
dwOffset = m_loaderOffset;
ucCount = m_loaderCount;
ucSize = m_loaderSize;//Loader³¤¶ÈÉú³ÉʱÒѾ512¶ÔÆë
break;
default:
return false;
}
if (ucIndex >= ucCount)
{
return false;
}
PSTRUCT_RKBOOT_ENTRY pEntry;
pEntry = (PSTRUCT_RKBOOT_ENTRY)(m_BootData+dwOffset+(ucSize*ucIndex));
dwDelay = pEntry->dwDataDelay;
dwSize = pEntry->dwDataSize;
if (pName)
{
WCHAR_To_char(pEntry->szName,pName,20);
}
return true;
}
bool CRKBoot::GetEntryData(ENUM_RKBOOTENTRY type,UCHAR ucIndex,PBYTE lpData)
{
DWORD dwOffset;
UCHAR ucCount,ucSize;
switch ( type )
{
case ENTRY471:
dwOffset = m_471Offset;
ucCount = m_471Count;
ucSize = m_471Size;
break;
case ENTRY472:
dwOffset = m_472Offset;
ucCount = m_472Count;
ucSize = m_472Size;
break;
case ENTRYLOADER:
dwOffset = m_loaderOffset;
ucCount = m_loaderCount;
ucSize = m_loaderSize;
break;
default:
return false;
}
if (ucIndex >= ucCount)
{
return false;
}
PSTRUCT_RKBOOT_ENTRY pEntry;
pEntry = (PSTRUCT_RKBOOT_ENTRY)(m_BootData+dwOffset+(ucSize*ucIndex));
memcpy(lpData,m_BootData+pEntry->dwDataOffset,pEntry->dwDataSize);
return true;
}
char CRKBoot::GetIndexByName(ENUM_RKBOOTENTRY type,char *pName)
{
DWORD dwOffset;
UCHAR ucCount,ucSize;
switch ( type )
{
case ENTRY471:
dwOffset = m_471Offset;
ucCount = m_471Count;
ucSize = m_471Size;
break;
case ENTRY472:
dwOffset = m_472Offset;
ucCount = m_472Count;
ucSize = m_472Size;
break;
case ENTRYLOADER:
dwOffset = m_loaderOffset;
ucCount = m_loaderCount;
ucSize = m_loaderSize;
break;
default:
return -1;
}
for (UCHAR i=0;i<ucCount;i++)
{
PSTRUCT_RKBOOT_ENTRY pEntry;
pEntry = (PSTRUCT_RKBOOT_ENTRY)(m_BootData+dwOffset+(ucSize*i));
char szName[20];
WCHAR_To_char(pEntry->szName,szName,20);
if (strcasecmp(pName,szName)==0)
{
return i;
}
}
return -1;
}
CRKBoot::~CRKBoot()
{
if (m_BootData!=NULL)
{
delete []m_BootData;
}
}
CRKBoot::CRKBoot(PBYTE lpBootData,DWORD dwBootSize,bool &bCheck)
{
Rc4DisableFlag.setContainer(this);
Rc4DisableFlag.getter(&CRKBoot::GetRc4DisableFlag);
SignFlag.setContainer(this);
SignFlag.getter(&CRKBoot::GetSignFlag);
Version.setContainer(this);
Version.getter(&CRKBoot::GetVersion);
MergeVersion.setContainer(this);
MergeVersion.getter(&CRKBoot::GetMergeVersion);
ReleaseTime.setContainer(this);
ReleaseTime.getter(&CRKBoot::GetReleaseTime);
SupportDevice.setContainer(this);
SupportDevice.getter(&CRKBoot::GetSupportDevice);
Entry471Count.setContainer(this);
Entry471Count.getter(&CRKBoot::GetEntry471Count);
Entry472Count.setContainer(this);
Entry472Count.getter(&CRKBoot::GetEntry472Count);
EntryLoaderCount.setContainer(this);
EntryLoaderCount.getter(&CRKBoot::GetEntryLoaderCount);
bCheck = true;
if (lpBootData!=NULL)
{
m_BootData=lpBootData;
m_BootSize=dwBootSize;
bCheck=CrcCheck();
if (!bCheck)
{
return;
}
PSTRUCT_RKBOOT_HEAD pBootHead;
pBootHead = (PSTRUCT_RKBOOT_HEAD)(m_BootData);
if ( pBootHead->uiTag!=0x544F4F42)
{
bCheck=false;
return;
}
if (pBootHead->ucRc4Flag)
{
m_bRc4Disable = true;
}
else
m_bRc4Disable = false;
if (pBootHead->ucSignFlag=='S')
{
m_bSignFlag = true;
}
else
m_bSignFlag = false;
m_version = pBootHead->dwVersion;
m_mergeVersion = pBootHead->dwMergeVersion;
m_BootHeadSize = pBootHead->usSize;
m_releaseTime.usYear = pBootHead->stReleaseTime.usYear;
m_releaseTime.ucMonth = pBootHead->stReleaseTime.ucMonth;
m_releaseTime.ucDay = pBootHead->stReleaseTime.ucDay;
m_releaseTime.ucHour = pBootHead->stReleaseTime.ucHour;
m_releaseTime.ucMinute = pBootHead->stReleaseTime.ucMinute;
m_releaseTime.ucSecond = pBootHead->stReleaseTime.ucSecond;
m_supportDevice = pBootHead->emSupportChip;
m_471Offset = pBootHead->dw471EntryOffset;
m_471Count = pBootHead->uc471EntryCount;
m_471Size = pBootHead->uc471EntrySize;
m_472Offset = pBootHead->dw472EntryOffset;
m_472Count = pBootHead->uc472EntryCount;
m_472Size = pBootHead->uc472EntrySize;
m_loaderOffset = pBootHead->dwLoaderEntryOffset;
m_loaderCount = pBootHead->ucLoaderEntryCount;
m_loaderSize = pBootHead->ucLoaderEntrySize;
memcpy(m_crc,m_BootData+(m_BootSize-4),4);
}
else
{
bCheck = false;
m_BootData=NULL;
}
}
void CRKBoot::WCHAR_To_wchar(WCHAR *src,wchar_t *dst,int len)
{
int i;
memset(dst,0,len*sizeof(wchar_t));
for (i=0;i<len;i++)
{
memcpy(dst,src,2);
src++;
dst++;
}
}
void CRKBoot::WCHAR_To_char(WCHAR *src,char *dst,int len)
{
int i;
memset(dst,0,len*sizeof(char));
for (i=0;i<len;i++)
{
memcpy(dst,src,1);
src++;
dst++;
}
}