xref: /OK3568_Linux_fs/external/rkupdate/RKBoot.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 #include "RKBoot.h"
2 extern UINT CRC_32(PBYTE pData, UINT ulSize, UINT uiPreviousValue = 0);
3 
GetRc4DisableFlag()4 bool CRKBoot::GetRc4DisableFlag()
5 {
6     return m_bRc4Disable;
7 }
GetSignFlag()8 bool CRKBoot::GetSignFlag()
9 {
10     return m_bSignFlag;
11 }
GetVersion()12 DWORD CRKBoot::GetVersion()
13 {
14     return m_version;
15 }
GetMergeVersion()16 DWORD CRKBoot::GetMergeVersion()
17 {
18     return m_mergeVersion;
19 }
GetReleaseTime()20 STRUCT_RKTIME CRKBoot::GetReleaseTime()
21 {
22     return m_releaseTime;
23 }
GetSupportDevice()24 ENUM_RKDEVICE_TYPE CRKBoot::GetSupportDevice()
25 {
26     return m_supportDevice;
27 }
GetEntry471Count()28 UCHAR CRKBoot::GetEntry471Count()
29 {
30     return m_471Count;
31 }
GetEntry472Count()32 UCHAR CRKBoot::GetEntry472Count()
33 {
34     return m_472Count;
35 }
GetEntryLoaderCount()36 UCHAR CRKBoot::GetEntryLoaderCount()
37 {
38     return m_loaderCount;
39 }
CrcCheck()40 bool CRKBoot::CrcCheck()
41 {
42     UINT *pOldCrc, ulNewCrc;
43     pOldCrc = (UINT *)(m_BootData + (m_BootSize - 4));
44     ulNewCrc = CRC_32(m_BootData, m_BootSize - 4);
45     return (*pOldCrc == ulNewCrc) ? true : false;
46 }
SaveEntryFile(ENUM_RKBOOTENTRY type,UCHAR ucIndex,tstring fileName)47 bool CRKBoot::SaveEntryFile(ENUM_RKBOOTENTRY type, UCHAR ucIndex, tstring fileName)
48 {
49     DWORD dwOffset;
50     UCHAR ucCount, ucSize;
51     switch (type)
52     {
53         case ENTRY471:
54             dwOffset = m_471Offset;
55             ucCount = m_471Count;
56             ucSize = m_471Size;
57             break;
58         case ENTRY472:
59             dwOffset = m_472Offset;
60             ucCount = m_472Count;
61             ucSize = m_472Size;
62             break;
63         case ENTRYLOADER:
64             dwOffset = m_loaderOffset;
65             ucCount = m_loaderCount;
66             ucSize = m_loaderSize;
67             break;
68         default:
69             return false;
70     }
71     if (ucIndex >= ucCount)
72     {
73         return false;
74     }
75     PSTRUCT_RKBOOT_ENTRY pEntry;
76     pEntry = (PSTRUCT_RKBOOT_ENTRY)(m_BootData + dwOffset + (ucSize * ucIndex));
77     FILE *file = NULL;
78     file = fopen(fileName.c_str(), _T("wb+"));
79     if (!file)
80     {
81         return false;
82     }
83     fwrite(m_BootData + pEntry->dwDataOffset, 1, pEntry->dwDataSize, file);
84     fclose(file);
85     return true;
86 }
GetEntryProperty(ENUM_RKBOOTENTRY type,UCHAR ucIndex,DWORD & dwSize,DWORD & dwDelay,tchar * pName)87 bool CRKBoot::GetEntryProperty(ENUM_RKBOOTENTRY type, UCHAR ucIndex, DWORD &dwSize, DWORD &dwDelay, tchar *pName)
88 {
89     DWORD dwOffset;
90     UCHAR ucCount, ucSize;
91     switch (type)
92     {
93         case ENTRY471:
94             dwOffset = m_471Offset;
95             ucCount = m_471Count;
96             ucSize = m_471Size;
97             break;
98         case ENTRY472:
99             dwOffset = m_472Offset;
100             ucCount = m_472Count;
101             ucSize = m_472Size;
102             break;
103         case ENTRYLOADER:
104             dwOffset = m_loaderOffset;
105             ucCount = m_loaderCount;
106             ucSize = m_loaderSize;//Loader��������ʱ�Ѿ�512����
107             break;
108         default:
109             return false;
110     }
111     if (ucIndex >= ucCount)
112     {
113         return false;
114     }
115     PSTRUCT_RKBOOT_ENTRY pEntry;
116     pEntry = (PSTRUCT_RKBOOT_ENTRY)(m_BootData + dwOffset + (ucSize * ucIndex));
117     dwDelay = pEntry->dwDataDelay;
118     dwSize = pEntry->dwDataSize;
119     if (pName)
120     {
121         WCHAR_To_char(pEntry->szName, pName, 20);
122     }
123     return true;
124 }
GetEntryData(ENUM_RKBOOTENTRY type,UCHAR ucIndex,PBYTE lpData)125 bool CRKBoot::GetEntryData(ENUM_RKBOOTENTRY type, UCHAR ucIndex, PBYTE lpData)
126 {
127     DWORD dwOffset;
128     UCHAR ucCount, ucSize;
129     switch (type)
130     {
131         case ENTRY471:
132             dwOffset = m_471Offset;
133             ucCount = m_471Count;
134             ucSize = m_471Size;
135             break;
136         case ENTRY472:
137             dwOffset = m_472Offset;
138             ucCount = m_472Count;
139             ucSize = m_472Size;
140             break;
141         case ENTRYLOADER:
142             dwOffset = m_loaderOffset;
143             ucCount = m_loaderCount;
144             ucSize = m_loaderSize;
145             break;
146         default:
147             return false;
148     }
149     if (ucIndex >= ucCount)
150     {
151         return false;
152     }
153     PSTRUCT_RKBOOT_ENTRY pEntry;
154     pEntry = (PSTRUCT_RKBOOT_ENTRY)(m_BootData + dwOffset + (ucSize * ucIndex));
155     memcpy(lpData, m_BootData + pEntry->dwDataOffset, pEntry->dwDataSize);
156     return true;
157 }
GetIndexByName(ENUM_RKBOOTENTRY type,tchar * pName)158 CHAR CRKBoot::GetIndexByName(ENUM_RKBOOTENTRY type, tchar *pName)
159 {
160     DWORD dwOffset;
161     UCHAR ucCount, ucSize;
162     switch (type)
163     {
164         case ENTRY471:
165             dwOffset = m_471Offset;
166             ucCount = m_471Count;
167             ucSize = m_471Size;
168             break;
169         case ENTRY472:
170             dwOffset = m_472Offset;
171             ucCount = m_472Count;
172             ucSize = m_472Size;
173             break;
174         case ENTRYLOADER:
175             dwOffset = m_loaderOffset;
176             ucCount = m_loaderCount;
177             ucSize = m_loaderSize;
178             break;
179         default:
180             return -1;
181     }
182 
183     for (UCHAR i = 0; i < ucCount; i++)
184     {
185         PSTRUCT_RKBOOT_ENTRY pEntry;
186         pEntry = (PSTRUCT_RKBOOT_ENTRY)(m_BootData + dwOffset + (ucSize * i));
187 
188         char szName[20];
189         WCHAR_To_char(pEntry->szName, szName, 20);
190         if (_tcsicmp(pName, szName) == 0)
191         {
192             return i;
193         }
194     }
195     return -1;
196 }
197 
IsNewIDBFlag()198 bool CRKBoot::IsNewIDBFlag()
199 {
200     return m_NewIDBFlag;
201 }
202 
~CRKBoot()203 CRKBoot::~CRKBoot()
204 {
205     if (m_BootData != NULL)
206     {
207         delete []m_BootData;
208     }
209 }
210 
CRKBoot(PBYTE lpBootData,DWORD dwBootSize,bool & bCheck)211 CRKBoot::CRKBoot(PBYTE lpBootData, DWORD dwBootSize, bool &bCheck)
212 {
213     Rc4DisableFlag.setContainer(this);
214     Rc4DisableFlag.getter(&CRKBoot::GetRc4DisableFlag);
215     SignFlag.setContainer(this);
216     SignFlag.getter(&CRKBoot::GetSignFlag);
217     Version.setContainer(this);
218     Version.getter(&CRKBoot::GetVersion);
219     MergeVersion.setContainer(this);
220     MergeVersion.getter(&CRKBoot::GetMergeVersion);
221     ReleaseTime.setContainer(this);
222     ReleaseTime.getter(&CRKBoot::GetReleaseTime);
223     SupportDevice.setContainer(this);
224     SupportDevice.getter(&CRKBoot::GetSupportDevice);
225     Entry471Count.setContainer(this);
226     Entry471Count.getter(&CRKBoot::GetEntry471Count);
227     Entry472Count.setContainer(this);
228     Entry472Count.getter(&CRKBoot::GetEntry472Count);
229     EntryLoaderCount.setContainer(this);
230     EntryLoaderCount.getter(&CRKBoot::GetEntryLoaderCount);
231     bCheck = true;
232     if (lpBootData != NULL)
233     {
234         m_BootData = lpBootData;
235         m_BootSize = dwBootSize;
236         bCheck = CrcCheck();
237         if (!bCheck)
238         {
239             printf("CRKBoot : CrcCheck happen error\n");
240             return;
241         }
242         PSTRUCT_RKBOOT_HEAD pBootHead;
243         pBootHead = (PSTRUCT_RKBOOT_HEAD)(m_BootData);
244         if (pBootHead->uiTag != 0x544F4F42  && (pBootHead->uiTag != 0x2052444C)) //add suppoty rk356x new loader
245         {
246             bCheck = false;
247             printf("CRKBoot : pBootHead->uiTag!=0x544F4F42 or 0x2052444C\n");
248             return;
249         }
250 
251         if (0x2052444C == pBootHead->uiTag)
252         {
253             m_NewIDBFlag = true;
254             printf("RKBoot:this is new IDB flag\n");
255         }
256         else
257         {
258             m_NewIDBFlag = false;
259         }
260 
261         if (pBootHead->ucRc4Flag)
262         {
263             m_bRc4Disable = true;
264         }
265         else
266         {
267             m_bRc4Disable = false;
268         }
269         if (pBootHead->ucSignFlag == 'S')
270         {
271             m_bSignFlag = true;
272         }
273         else
274         {
275             m_bSignFlag = false;
276         }
277         m_version = pBootHead->dwVersion;
278         m_mergeVersion = pBootHead->dwMergeVersion;
279         m_BootHeadSize = pBootHead->usSize;
280         m_releaseTime.usYear = pBootHead->stReleaseTime.usYear;
281         m_releaseTime.ucMonth = pBootHead->stReleaseTime.ucMonth;
282         m_releaseTime.ucDay = pBootHead->stReleaseTime.ucDay;
283         m_releaseTime.ucHour = pBootHead->stReleaseTime.ucHour;
284         m_releaseTime.ucMinute = pBootHead->stReleaseTime.ucMinute;
285         m_releaseTime.ucSecond = pBootHead->stReleaseTime.ucSecond;
286         m_supportDevice = pBootHead->emSupportChip;
287 
288         m_471Offset = pBootHead->dw471EntryOffset;
289         m_471Count = pBootHead->uc471EntryCount;
290         m_471Size = pBootHead->uc471EntrySize;
291 
292         m_472Offset = pBootHead->dw472EntryOffset;
293         m_472Count = pBootHead->uc472EntryCount;
294         m_472Size = pBootHead->uc472EntrySize;
295 
296         m_loaderOffset = pBootHead->dwLoaderEntryOffset;
297         m_loaderCount = pBootHead->ucLoaderEntryCount;
298         m_loaderSize = pBootHead->ucLoaderEntrySize;
299 
300         memcpy(m_crc, m_BootData + (m_BootSize - 4), 4);
301     }
302     else
303     {
304         bCheck = false;
305         printf("CRKBoot : lpBootData = null\n");
306         m_BootData = NULL;
307     }
308 }
309 
WCHAR_To_char(WCHAR * src,char * dst,int len)310 void CRKBoot::WCHAR_To_char(WCHAR *src, char *dst, int len)
311 {
312     int i;
313     memset(dst, 0, len * sizeof(char));
314     for (i = 0; i < len; i++)
315     {
316         memcpy(dst, src, 1);
317         src++;
318         dst++;
319     }
320 }
321 
322