blob: 500f89ca821315a7e7389fe43bc2c5623bd3b950 [file] [log] [blame]
xf.libdd93d52023-05-12 07:10:14 -07001
2#include <stdio.h>
3#include <time.h>
4#include <unistd.h>
5#include <string.h>
6#include <stdlib.h>
7// #include <io.h>
8#include "LogInfo.h"
9#include "CSerial.h"
10#include "download.h"
11
12/*******************************************************************************
13 * 宏定义 *
14 *******************************************************************************/
15#define ERASENVRW (0)
16#define LOWORD(l) ((WORD)(l))
17#define MAXFILENUM (50)
18// 大小端转换
19#define ExchangeEndian(data) (data = ((data >> 24) & 0x000000FF) | ((data >> 8) & 0x0000FF00) | ((data << 8) & 0x00FF0000) | ((data << 24) & 0xFF000000))
20
21/*******************************************************************************
22 * 全局变量 *
23 *******************************************************************************/
24const stSTAT m_DownloadImageTable[] =
25 {
26 {STAT_TERMINATE, STAT_TERMINATE}, // 0
27 {STAT_TERMINATE, STAT_TERMINATE}, // 1
28 {STAT_TERMINATE, STAT_TERMINATE}, // 2
29 {STAT_TERMINATE, STAT_TERMINATE}, // 3
30 {STAT_TERMINATE, STAT_TERMINATE}, // 4
31 {STAT_EXECUTE_CODE, STAT_TERMINATE}, // 5
32 {STAT_ERASE_SEG_ST_ADDR, STAT_TERMINATE}, // 6
33 {STAT_TERMINATE, STAT_TERMINATE}, // 7
34 {STAT_TERMINATE, STAT_TERMINATE}, // 8
35 {STAT_PROG_DEV_END_ADDR, STAT_TERMINATE}, // 9
36 {STAT_SET_PACKET_SIZE, STAT_TERMINATE}, // 10
37 {STAT_DATAPACKET, STAT_TERMINATE}, // 11
38 {STAT_TERMINATE, STAT_TERMINATE}, // 12
39 {STAT_ERASE_SEG_END_ADDR, STAT_TERMINATE}, // 13
40 {STAT_PROG_DEV_ST_ADDR, STAT_TERMINATE}, // 14
41 {STAT_TERMINATE, STAT_TERMINATE}, // 15
42 {STAT_TERMINATE, STAT_TERMINATE}, // 16
43 {STAT_TERMINATE, STAT_TERMINATE} // 17
44};
45
46const stSTAT m_DownloadRAMImageTable[] =
47 {
48 {STAT_TERMINATE, STAT_TERMINATE}, // 0
49 {STAT_TERMINATE, STAT_TERMINATE}, // 1
50 {STAT_TERMINATE, STAT_TERMINATE}, // 2
51 {STAT_TERMINATE, STAT_TERMINATE}, // 3
52 {STAT_TERMINATE, STAT_TERMINATE}, // 4
53 {STAT_EXECUTE_CODE, STAT_TERMINATE}, // 5
54 {STAT_PROG_DEV_ST_ADDR, STAT_TERMINATE}, // 6
55 {STAT_TERMINATE, STAT_TERMINATE}, // 7
56 {STAT_TERMINATE, STAT_TERMINATE}, // 8
57 {STAT_PROG_DEV_END_ADDR, STAT_TERMINATE}, // 9
58 {STAT_SET_PACKET_SIZE, STAT_TERMINATE}, // 10
59 {STAT_DATAPACKET, STAT_TERMINATE}, // 11
60 {STAT_TERMINATE, STAT_TERMINATE}, // 12
61 {STAT_TERMINATE, STAT_TERMINATE}, // 13
62 {STAT_TERMINATE, STAT_TERMINATE}, // 14
63 {STAT_TERMINATE, STAT_TERMINATE}, // 15
64 {STAT_TERMINATE, STAT_TERMINATE}, // 16
65 {STAT_TERMINATE, STAT_TERMINATE} // 17
66};
67
68FILE *pf = NULL; // 版本文件FILE指针
69char g_path[FILE_PATH_LENGTH_MAX]; // 版本文件路径
70DWORD m_dwPartitionSize;
71DWORD m_dwBootStage1StartAddress; // Tloader起始地址
72DWORD m_dwBootStage1Size; // Tloader大小
73DWORD m_dwBootStage2StartAddress; // Tboot起始地址
74DWORD m_dwBootStage2Size; // Tboot大小
75DWORD m_iFileNum; // 文件总数
76BOOL m_StartAMT;
77BOOL m_StartAMT_MODEM;
78BOOL m_StartAMT_AP;
79BOOL m_bBackUpNV;
80BOOL m_bCRCCheck; // CRC校验标志位
81const stSTAT *m_pProtocolTable;
82stBinMasterInfo m_stMasterInfo; // 大BIN版本head结构
83stFileItem FileItem[MAXFILENUM]; // 用于存放image信息
84int g_DownLoadFileNum = 0; // 真正要下载的文件个数
85
86/*******************************************************************************
87 * 声明 *
88 *******************************************************************************/
89// 擦除NVRW
90static BOOL EraseNVRW();
91// 对下载文件进行解析
92static BOOL PreInitBoot();
93
94// 下载boot
95static BOOL DownloadOneBoot(DWORD dwBootStartAddress, DWORD dwBootSize, FILE *pBootFile, size_t dwPacketSize, DWORD dwOffset, BOOL SYNC);
96// 同步, 配置分区
97static BOOL DownloadPartition(size_t dwPartitionSize, FILE *pFile, DWORD dwPacketSize, DWORD dwOffset, BOOL bCheckPartition);
98// 释放boot资源
99static BOOL UnPreInitBoot();
100
101// 下载版本文件前初始化
102static BOOL Initialize();
103// 进行解析bin文件信息
104static BOOL FileAnalysis();
105// 获取版本文件信息接口
106static void GetFileList(BOOL bDownload, enDownloadType DownloadType, BYTE *pBuffer, DWORD dwSize, DWORD nIndex);
107// 解析大BIN文件信息总接口
108static void CollectDownloadImage(enDownloadType DownloadType, BYTE *pBuffer, DWORD dwSize);
109// 跳入下一个下载阶段
110static enSTAT GotoNextState(enSTAT enCurrentStat, BOOL bRet);
111// 下载文件
112static BOOL DownloadOneImage(stDownloadConfig CurrentConfig);
113// 下载文件接口
114static BOOL DownloadImage(stFileItem ImageInfo, BOOL bExecuteCode);
115// 下载文件总接口
116static BOOL ExchangeImage();
117// 下载版本文件后释放资源
118static BOOL UnInitialize();
119// 下载版本文件
120static BOOL DoDownload(enDownloadType DownloadType, BYTE *pBuffer, DWORD dwSize, BOOL bBackupNV, enDownloadNVBackupMethod Method, BOOL bEableCRCCheck);
121
122/*******************************************************************************
123 * 内部函数 *
124 *******************************************************************************/
125static BOOL EraseNVRW()
126{
127 BOOL bRet = FALSE;
128 BYTE *AckBuffer;
129 char buf[100];
130 LogInfo("start...");
131 BYTE *WriteBufferErase = (BYTE *)malloc(200);
132 memset(WriteBufferErase, 0, 200);
133 memcpy(WriteBufferErase, "erase nvrw", 10);
134
135 bRet = SendData(WriteBufferErase, 11, 10, 1);
136 usleep(1000 * 1000);
137 if (bRet)
138 {
139 AckBuffer = (BYTE *)ReadDataExtraFuncB(4, 20, 30);
140 }
141 memset(buf, 0, 100);
142 if (AckBuffer == NULL)
143 {
144 LogInfo("AckBuffer is null...");
145 return bRet;
146 }
147 strcpy(buf, (char *)AckBuffer);
148 if (bRet && (!memcmp(buf, "OKAY", 5)))
149 {
150 LogInfo("Erase OK...");
151
152 usleep(100 * 1000); // 延时必须要加,否则下载文件失败
153
154 bRet = TRUE;
155 }
156 else
157 {
158 LogInfo("Erase FAIL...");
159 bRet = FALSE;
160 }
161 return bRet;
162}
163
164// 如果打不开待下载的代理文件,则失败
165static BOOL PreInitBoot()
166{
167 m_dwBootStage1StartAddress = 0x00082000;
168 m_dwBootStage2StartAddress = 0x21ef0000;
169 printf("PreInitBootfunc:: g_path=%s.\n", g_path);
170
171 if (access(g_path, 0) != 0)
172 {
173 printf("PreInitBootfunc:: software file path is error.\n");
174 return FALSE;
175 }
176 pf = fopen(g_path, "rb");
177
178 if (pf == NULL)
179 {
180 LogInfo("Open file FAIL... %s", g_path);
181 return FALSE;
182 }
183 stBinMasterInfo MasterHead;
184 stFileItem FileItem;
185
186 int num = fread((BYTE *)&MasterHead, 1, sizeof(MasterHead), pf);
187 if (num == 0)
188 {
189 LogInfo("Read file FAIL... %s", g_path);
190 return FALSE;
191 }
192 m_iFileNum = MasterHead.nTotalFileNum;
193 m_stMasterInfo.nTotalFileNum = MasterHead.nTotalFileNum;
194
195 m_stMasterInfo.nFlashType = MasterHead.nFlashType;
196 m_stMasterInfo.nNVCoalition = MasterHead.nNVCoalition;
197 memcpy(m_stMasterInfo.chVersionIN, MasterHead.chVersionIN, sizeof(MasterHead.chVersionIN));
198 memcpy(m_stMasterInfo.chVersionOUT, MasterHead.chVersionOUT, sizeof(MasterHead.chVersionOUT));
199 m_stMasterInfo.iFileSize = MasterHead.iFileSize;
200 m_stMasterInfo.iImageStructOffset = MasterHead.iImageStructOffset;
201 m_stMasterInfo.PartitionLength = MasterHead.PartitionLength;
202 m_stMasterInfo.PartitionOffset = MasterHead.PartitionOffset;
203
204 m_stMasterInfo.tloaderLength = MasterHead.tloaderLength;
205 m_stMasterInfo.tloaderOffset = MasterHead.tloaderOffset;
206 m_stMasterInfo.tBootLength = MasterHead.tBootLength - 64;
207 m_stMasterInfo.tBootOffset = MasterHead.tBootOffset + 64;
208
209 m_dwBootStage2Size = m_stMasterInfo.tBootLength;
210 m_dwBootStage1Size = m_stMasterInfo.tloaderLength;
211 m_dwPartitionSize = m_stMasterInfo.PartitionLength;
212
213 LogInfo("PreInitBootfunc:: m_iFileNum = %d", m_iFileNum);
214 LogInfo("PreInitBootfunc:: m_stMasterInfo.nTotalFileNum = %d", m_stMasterInfo.nTotalFileNum);
215 LogInfo("PreInitBootfunc:: m_stMasterInfo.nFlashType = %d", m_stMasterInfo.nFlashType);
216 LogInfo("PreInitBootfunc:: m_stMasterInfo.nNVCoalition = %d", m_stMasterInfo.nNVCoalition);
217 LogInfo("PreInitBootfunc:: m_stMasterInfo.chVersionIN = %s", m_stMasterInfo.chVersionIN);
218 LogInfo("PreInitBootfunc:: m_stMasterInfo.chVersionOUT = %s", m_stMasterInfo.chVersionOUT);
219 LogInfo("PreInitBootfunc:: m_stMasterInfo.iFileSize = %d", m_stMasterInfo.iFileSize);
220 LogInfo("PreInitBootfunc:: m_stMasterInfo.iImageStructOffset = %d", m_stMasterInfo.iImageStructOffset);
221 LogInfo("PreInitBootfunc:: m_stMasterInfo.PartitionLength = %d", m_stMasterInfo.PartitionLength);
222 LogInfo("PreInitBootfunc:: m_stMasterInfo.PartitionOffset = %d", m_stMasterInfo.PartitionOffset);
223 LogInfo("PreInitBootfunc:: m_stMasterInfo.tloaderLength = %d", m_stMasterInfo.tloaderLength);
224 LogInfo("PreInitBootfunc:: m_stMasterInfo.tloaderOffset = %d", m_stMasterInfo.tloaderOffset);
225 LogInfo("PreInitBootfunc:: m_stMasterInfo.tBootLength = %d", m_stMasterInfo.tBootLength);
226 LogInfo("PreInitBootfunc:: m_stMasterInfo.tBootOffset = %d", m_stMasterInfo.tBootOffset);
227 LogInfo("PreInitBootfunc:: m_dwBootStage2Size = %d", m_dwBootStage2Size);
228 LogInfo("PreInitBootfunc:: m_dwBootStage1Size = %d", m_dwBootStage1Size);
229 LogInfo("PreInitBootfunc:: m_dwPartitionSize = %d", m_dwPartitionSize);
230
231 // 从tboot文件前面的64个字节中获取tboot下载和启动地址
232 unsigned char header[64] = {0};
233 fseek(pf, MasterHead.tBootOffset, 0);
234 if (fread(header, 1, sizeof(header), pf) != sizeof(header))
235 {
236 LogInfo("Read tboot FAIL...");
237 return FALSE;
238 }
239 // 获取TBoot下载地址
240 m_dwBootStage2StartAddress = (header[20] << 24) | (header[21] << 16) | (header[22] << 8) | (header[23]);
241 LogInfo("m_dwBootStage2StartAddress = 0x%x", m_dwBootStage2StartAddress);
242
243 if (pf != NULL)
244 {
245 fclose(pf);
246 pf = NULL;
247 }
248 return TRUE;
249}
250
251static BOOL DownloadOneBoot(DWORD dwBootStartAddress, DWORD dwBootSize, FILE *pBootFile, size_t dwPacketSize, DWORD dwOffset, BOOL SYNC)
252{
253 enSTAT State = STAT_SYNC;
254 BOOL bRet = FALSE;
255 BOOL bExit = FALSE;
256 DWORD dwCurrentSize = 0;
257 DWORD dwPacketCount = 0;
258
259 if (access(g_path, 0) != 0)
260 {
261 printf("DownloadOneBootfunc:: software file path is error.\n");
262 return FALSE;
263 }
264 pf = fopen(g_path, "rb");
265 if (pf == NULL)
266 {
267 LogInfo("DownloadOneBootfunc:: PreInitBoot Open file FAIL... %s", g_path);
268 return FALSE;
269 }
270 if (SYNC == FALSE)
271 {
272 State = STAT_START_BYTE;
273 LogInfo("DownloadOneBootfunc:: not need STAT_SYNC 0X5A...");
274 }
275
276 LogInfo("DownloadOneBootfunc:: dwOffset = %d...dwPacketSize = %d", dwOffset, dwPacketSize);
277 while (!bExit)
278 {
279 switch (State)
280 {
281 //------------------------------同步---------------------------------
282 case STAT_SYNC:
283 {
284 BYTE nCommand = 0;
285 BYTE nAck = 0;
286
287 // PrintInfo(_T("开始同步DownloadOneBoot..."));
288 nCommand = CMD_SYNC_TBOOT;
289 bRet = SendData(&nCommand, 1, 10, 20);
290 if (bRet)
291 {
292 bRet = ReadData(&nAck, 1, 20, 20);
293 }
294
295 if (bRet && (CMD_SYNC_BYTE_ACK_BOOTROM == nAck))
296 {
297 // PrintInfo(_T("同步完毕!"));
298 LogInfo("DownloadOneBootfunc:: Send STAT_SYNC OK...");
299 State = STAT_START_BYTE;
300 }
301 else
302 {
303 // 同步失败后退出
304 // PrintInfo(_T("同步失败!"));
305 LogInfo("DownloadOneBootfunc:: Send STAT_SYNC FAIL...nAck = 0x%x", nAck);
306 bExit = TRUE;
307 bRet = FALSE;
308 }
309 }
310 break;
311
312 //-----------------------------设置寄存器---------------------------------
313 case STAT_START_BYTE:
314 {
315 State = STAT_NO_MODIFY_REG; // 不执行配置,直接进入下一个流程
316 }
317 break;
318
319 case STAT_NO_MODIFY_REG:
320 {
321 BYTE nCommand = CMD_SEND_DATA_BOOTROM;
322
323 // PrintInfo(_T("设置寄存器..."));
324 bRet = SendData(&nCommand, 1, 10, 1);
325 if (bRet)
326 {
327 // PrintInfo(_T("设置寄存器完毕!"));
328 LogInfo("DownloadOneBootfunc:: Send CMD_SEND_DATA_BOOTROM OK...");
329 State = STAT_ADDRESS;
330 }
331 else
332 {
333 LogInfo("DownloadOneBootfunc:: Send CMD_SEND_DATA_BOOTROM FAIL...");
334 State = STAT_TERMINATE;
335 }
336 }
337 break;
338
339 //------------------------------地址---------------------------------
340 case STAT_ADDRESS:
341 {
342 DWORD dwStartAddress = dwBootStartAddress;
343 // PrintInfo(_T("设置地址..."));
344 ExchangeEndian(dwStartAddress);
345 bRet = SendData((BYTE *)&dwStartAddress, sizeof(DWORD), 10, 1);
346 if (bRet)
347 {
348 // PrintInfo(_T("设置地址完毕!"));
349 LogInfo("DownloadOneBootfunc:: Send dwStartAddress OK...");
350 State = STAT_DATA_LEN;
351 }
352 else
353 {
354 LogInfo("DownloadOneBootfunc:: Send dwStartAddress FAIL...");
355 State = STAT_TERMINATE;
356 }
357 }
358 break;
359
360 //----------------------------数据长度-------------------------------
361 case STAT_DATA_LEN:
362 {
363 DWORD dwSize = dwBootSize;
364 BYTE nAck = 0;
365
366 // PrintInfo(_T("设置数据长度..."));
367 ExchangeEndian(dwSize);
368 bRet = SendData((BYTE *)&dwSize, sizeof(DWORD), 20, 1);
369 if (bRet)
370 {
371 bRet = ReadData(&nAck, 1, 20, 20);
372 if (bRet && (CMD_SEND_ADDRESS_LEN_ACK_BOOTROM == nAck))
373 {
374 LogInfo("DownloadOneBootfunc:: Set address and length OK...");
375 // printf("DownloadOneBootfunc:: Set address and length OK...\n");
376 // 判断Boot文件的状态
377 if (NULL == pf)
378 {
379 LogInfo("DownloadOneBootfunc:: pf not exsist...");
380 State = STAT_TERMINATE;
381 }
382 else
383 {
384 fseek(pf, dwOffset, 0);
385 dwCurrentSize = dwBootSize;
386 LogInfo("DownloadOneBootfunc:: dwOffset = %d, dwBootSize = %d", dwOffset, dwBootSize);
387 // PrintInfo(_T("开始发送Boot数据..."));
388 State = STAT_DATAPACKET;
389 LogInfo("DownloadOneBootfunc:: start send BootFile...");
390 }
391 }
392 else
393 {
394 LogInfo("DownloadOneBootfunc:: Get ADDRESS_LEN_ACK FAIL!bRet=%d, nAck=%d", bRet, nAck);
395 State = STAT_TERMINATE;
396 }
397 }
398 else
399 {
400 LogInfo("DownloadOneBootfunc:: Send dwBootSize FAIL...");
401 State = STAT_TERMINATE;
402 }
403 }
404 break;
405
406 //----------------------------boot数据---------------------------------
407 case STAT_DATAPACKET:
408 {
409 BYTE WriteBuffer[PACKET_SIZE_BOOT] = {0};
410 BYTE nAck = 0;
411 size_t dwSendSize = 0;
412
413 if (dwPacketSize > PACKET_SIZE_BOOT)
414 {
415 // PrintInfo(_T("数据包长度越界!"));
416 LogInfo("DownloadOneBootfunc:: dwPacketSize>PACKET_SIZE_BOOT too big...");
417 bRet = FALSE;
418 State = STAT_TERMINATE;
419 }
420 else
421 {
422 LogInfo("DownloadOneBootfunc:: dwPacketSize = %d PACKET_SIZE_BOOT = %d", dwPacketSize, PACKET_SIZE_BOOT);
423 }
424
425 memset(WriteBuffer, 0, sizeof(WriteBuffer));
426 if (dwCurrentSize >= dwPacketSize)
427 {
428 LogInfo("DownloadOneBootfunc:: dwCurrentSize >= dwPacketSize...");
429 if (fread(&WriteBuffer[0], 1, dwPacketSize, pf) != dwPacketSize) // pBootFile->Read(&WriteBuffer[0], dwPacketSize) != dwPacketSize)
430 {
431 // PrintInfo(_T("Boot文件读取失败!"));
432 LogInfo("DownloadOneBootfunc:: Read bootfile FAIL...");
433 bRet = FALSE;
434 State = STAT_TERMINATE;
435 break;
436 }
437 LogInfo("DownloadOneBootfunc:: dwPacketSize = %d", dwPacketSize);
438 bRet = SendData(WriteBuffer, dwPacketSize, 10, 1);
439 LogInfo("DownloadOneBootfunc:: dwPacketCount = %d...bRet = %d", dwPacketCount, bRet);
440 if (bRet)
441 {
442 LogInfo("DownloadOneBootfunc:: send bootfile one packet OK...");
443 dwPacketCount++;
444 dwCurrentSize -= dwPacketSize;
445 if (0x00 == dwCurrentSize)
446 {
447 bRet = ReadData(&nAck, 1, 20, 20);
448 if (bRet && (CMD_SEND_DATA_ACK_BOOTROM == nAck))
449 {
450 State = STAT_EXECUTE_CODE; // 下载完成后,下一状态是执行代码 zxw 20121010
451 }
452 else
453 {
454 LogInfo("DownloadOneBootfunc:: Get send bootfile ACK FAIL!");
455 printf("DownloadOneBootfunc:: Get send bootfile ACK FAIL!\n");
456 bRet = FALSE;
457 State = STAT_TERMINATE;
458 }
459 }
460 }
461 else
462 {
463 LogInfo("DownloadOneBootfunc:: send bootfile one packet FAIL...");
464 bRet = FALSE;
465 State = STAT_TERMINATE;
466 }
467 }
468 else if (dwCurrentSize > 0)
469 {
470 if ((dwCurrentSize % 512) == 0)
471 {
472 dwSendSize = dwCurrentSize - 8; // 512整包时,拆包
473 LogInfo("DownloadOneImagefunc:: STAT_DATAPACKET split packet");
474 // printf("DownloadOneImagefunc:: STAT_DATAPACKET split packet\n");
475 }
476 else
477 {
478 dwSendSize = dwCurrentSize;
479 usleep(2 * 1000); // 最后一包,等待一段时间
480 }
481
482 LogInfo("DownloadOneBootfunc:: dwCurrentSize > 0 but <packetsize...");
483 if (fread(&WriteBuffer[0], 1, dwSendSize, pf) != dwSendSize) // pBootFile->Read(&WriteBuffer[0], dwCurrentSize) != dwCurrentSize)
484 {
485 // PrintInfo(_T("Boot文件读取失败!"));
486 LogInfo("DownloadOneBootfunc:: Read bootfile FAIL...");
487 bRet = FALSE;
488 State = STAT_TERMINATE;
489 break;
490 }
491 bRet = SendData(WriteBuffer, dwSendSize, 10, 1);
492 LogInfo("DownloadOneBootfunc:: dwPacketCount = %d...", dwPacketCount);
493 if (bRet)
494 {
495 dwPacketCount++;
496 dwCurrentSize -= dwSendSize;
497 if (dwCurrentSize == 0)
498 {
499 bRet = ReadData(&nAck, 1, 20, 20);
500 if (bRet && (CMD_SEND_DATA_ACK_BOOTROM == nAck))
501 {
502 State = STAT_EXECUTE_CODE; // 下载完成后,下一状态是执行代码 zxw 20121010
503 LogInfo("DownloadOneBootfunc:: send bootfile last packet OK...");
504 }
505 else
506 {
507 LogInfo("DownloadOneBootfunc:: Get send bootfile ACK FAIL!");
508 bRet = FALSE;
509 State = STAT_TERMINATE;
510 }
511 }
512 }
513 else
514 {
515 // PrintInfo(_T("BOOT数据发送失败!"));
516 LogInfo("DownloadOneBootfunc:: send bootfile one packet FAIL...");
517 bRet = FALSE;
518 State = STAT_TERMINATE;
519 }
520 }
521 else
522 {
523 bRet = TRUE;
524 State = STAT_EXECUTE_CODE; // 下载完成后,下一状态是执行代码 zxw 20121010
525 }
526 }
527 break;
528 case STAT_EXECUTE_CODE:
529 {
530 unsigned char buf[5];
531 BYTE nAck = 0;
532 DWORD dwStartAddress = dwBootStartAddress;
533 buf[0] = CMD_STARTUP_BOOTROM;
534 ExchangeEndian(dwStartAddress);
535 memcpy(&buf[1], &dwStartAddress, sizeof(DWORD));
536 // PrintInfo(_T("开始启动BOOT..."));
537 bRet = SendData(buf, sizeof(buf), 10, 1);
538 if (bRet)
539 {
540 bRet = ReadData(&nAck, 1, 20, 20);
541 if (bRet && (CMD_STARTUP_ACK_BOOTROM == nAck))
542 {
543 // PrintInfo(_T("启动Boot完毕!"));
544 LogInfo("DownloadOneBootfunc:: start boot OK...");
545 State = STAT_TERMINATE;
546 bRet = TRUE;
547 }
548 else
549 {
550 LogInfo("DownloadOneBootfunc:: Get start boot ACK FAIL...");
551 State = STAT_TERMINATE;
552 bRet = FALSE;
553 }
554 }
555 else
556 {
557 // PrintInfo(_T("启动Boot失败!"));
558 LogInfo("DownloadOneBootfunc:: send boot start command FAIL...");
559 State = STAT_TERMINATE;
560 bRet = FALSE;
561 }
562 }
563 break;
564 default:
565 bExit = TRUE;
566 break;
567 }
568 }
569
570 if (pf != NULL)
571 {
572 fclose(pf);
573 pf = NULL;
574 }
575 return bRet;
576}
577
578static BOOL DownloadPartition(size_t dwPartitionSize, FILE *pFile, DWORD dwPacketSize, DWORD dwOffset, BOOL bCheckPartition)
579{
580 enParpSTAT State = STAT_SYNC_PARTIITON;
581 BOOL bRet = FALSE;
582 BOOL bExit = FALSE;
583
584 DWORD dwCurrentSize = 0;
585 DWORD dwPacketCount = 0;
586
587 BYTE *AckBuffer;
588 if (access(g_path, 0) != 0)
589 {
590 printf("DownloadPartitionfunc:: software file path is error.\r\n");
591 return FALSE;
592 }
593 pf = fopen(g_path, "rb");
594 if (pf == NULL)
595 {
596 LogInfo("DownloadPartitionfunc:: Open file FAIL... %s", g_path);
597 return FALSE;
598 }
599
600 while (!bExit)
601 {
602 switch (State)
603 {
604 //------------------------------同步---------------------------------
605 case STAT_SYNC_PARTIITON:
606 {
607 BYTE nCommand = 0;
608 BYTE nAck = 0;
609
610 // PrintInfo(_T("开始同步DownloadPartition1..."));
611 LogInfo("DownloadPartitionfunc:: STAT_SYNC_PARTIITON DownloadPartition...");
612 nCommand = CMD_SYNC_TBOOT;
613 bRet = SendData(&nCommand, 1, 10, 1);
614 if (bRet)
615 {
616 bRet = ReadData(&nAck, 1, 10, 10);
617 }
618
619 if (bRet && (CMD_SYNC_ACK_TBOOT == nAck))
620 {
621 // PrintInfo(_T("同步完毕!"));
622 LogInfo("DownloadPartitionfunc:: STAT_SYNC_PARTIITON DownloadPartition OK...");
623 printf("DownloadPartitionfunc:: STAT_SYNC_PARTIITON DownloadPartition OK...\n");
624
625 State = STAT_SET_PARTITION_CMD;
626 }
627 else
628 {
629 // 空片下载的时候需要遍历flash生产坏块分区表需要一定的时间。
630 // PrintInfo(_T("同步失败!"));
631 LogInfo("DownloadPartitionfunc:: STAT_SYNC_PARTIITON DownloadPartition FAIL...");
632 State = STAT_SYNC_PARTIITON;
633 int count = 0;
634 count++;
635 if (count > 20)
636 {
637 State = STAT_EXIT;
638 bExit = TRUE;
639 bRet = FALSE;
640 }
641 }
642 usleep(10 * 1000);
643 }
644 break;
645
646 case STAT_SET_PARTITION_CMD:
647 {
648 BYTE CommandHead[] = "set partitions 1000\0";
649 BYTE CommandBuf[200] = {0};
650 BYTE bRead = 0;
651 BYTE ByteBuf[17] = {0};
652
653 // memset(CommandBuf,0,200);
654 // memset(AckBuffer,0,150);
655 memcpy(CommandBuf, CommandHead, sizeof(CommandHead));
656 // CommandBuf[sizeof(CommandHead)] = dwPartitionSize;
657
658 LogInfo("DownloadPartitionfunc:: STAT_SET_PARTITION_CMD strlen((char*)CommandBuf)) = %d...", strlen((char *)CommandBuf));
659 bRet = SendData(CommandBuf, strlen((char *)CommandBuf) + 1, 10, 1);
660 if (bRet)
661 {
662 AckBuffer = (BYTE *)ReadDataExtraFuncB(17, 10, 20);
663 }
664 if (AckBuffer != NULL)
665 {
666 LogInfo("DownloadPartitionfunc:: STAT_SET_PARTITION_CMD AckBuffer = %s...", AckBuffer);
667 }
668 else
669 {
670 LogInfo("DownloadPartitionfunc:: STAT_SET_PARTITION_CMD AckBuffer is NULL");
671 }
672
673 if (bRet && (!memcmp(AckBuffer, "OKAY RECV_TABLES", 16)))
674 {
675 // PrintInfo(_T("设置分区表命令成功!"));
676 LogInfo("DownloadPartitionfunc:: STAT_SET_PARTITION_CMD set partitions OK...");
677 printf("DownloadPartitionfunc:: STAT_SET_PARTITION_CMD set partitions OK...\n");
678
679 State = STAT_SEND_PARTIITON;
680 }
681 else
682 {
683 LogInfo("DownloadPartitionfunc:: STAT_SET_PARTITION_CMD set partitions FAIL...");
684 // PrintInfo(_T("设置分区表命令失败!"));
685 State = STAT_EXIT;
686
687 bExit = TRUE;
688 bRet = FALSE;
689 }
690 }
691 break;
692
693 case STAT_SEND_PARTIITON:
694 {
695 // memset(AckBuffer,0,150);
696 char *strfail1 = "FAIL INVALID_PARTITION_TABLE"; // 分区表自身内容不合法
697 char *strfail2 = "FAIL ACCEPTABLE_PARTITION_CHANGE"; // 分区表对比出错,但NV分区一致
698 char *strfail3 = "FAIL UNACCEPTABLE_PARTITION_CHANGE"; // 分区表对比出错,NV分区不一致
699 BYTE bRead = 0;
700
701 BYTE *WriteBuffer = (BYTE *)malloc(dwPartitionSize);
702 if (!WriteBuffer)
703 {
704 printf("DownloadPartitionfunc:: malloc WriteBuffer failed\n");
705 bRet = FALSE;
706 break;
707 }
708
709 fseek(pf, dwOffset, 0);
710 if (fread(WriteBuffer, 1, dwPartitionSize, pf) != dwPartitionSize)
711 {
712 // PrintInfo(_T("分区表文件读取失败!"));
713 LogInfo("DownloadPartitionfunc:: STAT_SEND_PARTIITON read partition data FAIL...");
714 if (WriteBuffer != NULL)
715 {
716 free(WriteBuffer);
717 WriteBuffer = NULL;
718 }
719 bRet = FALSE;
720 break;
721 }
722
723 int dlPTindex = 0;
724 int dlTime = dwPartitionSize / 1000;
725
726 printf("DownloadPartitionfunc:: STAT_SEND_PARTIITON start !\n");
727 for (dlPTindex = 0; dlPTindex < dlTime; dlPTindex++)
728 {
729 bRet = SendData(WriteBuffer + 1000 * dlPTindex, 1000, 10, 1);
730 printf("DownloadPartitionfunc:: STAT_SEND_PARTIITON dlPTindex = %d.\n", dlPTindex);
731 }
732 if (dwPartitionSize % 1000 != 0)
733 {
734 bRet = SendData(WriteBuffer + 1000 * dlTime, dwPartitionSize % 1000, 10, 1);
735 printf("DownloadPartitionfunc:: STAT_SEND_PARTIITON SendData\n");
736 }
737 if (bRet)
738 {
739 usleep(10 * 1000);
740 // bRet = ReadData(&bRead,1,50,20);
741 // bRet = ReadData(AckBuffer,strlen(strfail3)+1,50,20);
742 AckBuffer = (BYTE *)ReadDataExtraFuncB(strlen(strfail3) + 1, 10, 5);
743 }
744 printf("DownloadPartitionfunc:: STAT_SEND_PARTIITON ReadDataExtraFuncB\n");
745
746 char buf[100];
747 memset(buf, 0, 100);
748 strcpy(buf, (char *)AckBuffer);
749
750 if (buf[0] == '\0')
751 {
752 LogInfo("DownloadPartitionfunc:: STAT_SEND_PARTIITON read error !");
753 bExit = TRUE;
754 bRet = FALSE;
755 State = STAT_EXIT;
756 break;
757 }
758 if (!memcmp(buf, "OKAY", 4))
759 {
760 // PrintInfo("分区文件下载成功!");
761 LogInfo("DownloadPartitionfunc:: STAT_SEND_PARTIITON send partition OK...");
762 printf("DownloadPartitionfunc:: STAT_SEND_PARTIITON send partition OK...\n");
763
764 State = STAT_EXIT;
765 bRet = TRUE;
766 break;
767 }
768 if (!memcmp(buf, strfail1, strlen(strfail1)))
769 {
770 // PrintInfo("生成的分区表有问题!");
771 LogInfo("DownloadPartitionfunc:: STAT_SEND_PARTIITON partition has some problem..");
772 bExit = TRUE;
773 bRet = FALSE;
774 State = STAT_EXIT;
775 break;
776 }
777 if (!memcmp(buf, strfail2, strlen(strfail2)))
778 {
779 // memset(AckBuffer,0,150);
780 // PrintInfo("分区表发生变化,需要擦除所有分区除了NV分区进行下载!");
781
782 LogInfo("DownloadPartitionfunc:: FAIL ACCEPTABLE_PARTITION_CHANGE...");
783 printf("DownloadPartitionfunc:: PARTITION_CHANGE...\n");
784
785 BYTE *WriteBufferErase = (BYTE *)malloc(200);
786
787 memset(WriteBufferErase, 0, 200);
788 memcpy(WriteBufferErase, "erase auto", 10);
789
790 bRet = SendData(WriteBufferErase, 11, 10, 1);
791 if (bRet)
792 {
793 bRet = ReadData(AckBuffer, 5, 20, 4 * 60); // 适配nor flash,最大超时时间改为4分钟
794 }
795
796 memset(buf, 0, 100);
797 strcpy(buf, (char *)AckBuffer);
798
799 if (bRet && (!memcmp(buf, "OKAY", 5)))
800 {
801 if (WriteBuffer != NULL)
802 {
803 free(WriteBuffer);
804 WriteBuffer = NULL;
805 }
806
807 LogInfo("DownloadPartitionfunc:: Erase Partition OK...");
808 printf("DownloadPartitionfunc:: Erase Partition OK...\n");
809
810 // PrintInfo("擦除分区成功!");
811 usleep(100 * 1000); // 延时必须要加,否则下载文件失败
812 bExit = TRUE;
813
814 bRet = TRUE;
815 State = STAT_EXIT;
816 break;
817 }
818 else
819 {
820 if (WriteBuffer != NULL)
821 {
822 free(WriteBuffer);
823 WriteBuffer = NULL;
824 }
825 LogInfo("DownloadPartitionfunc:: Erase Partition FAIL...");
826 // PrintInfo("擦除分区失败!");
827 bExit = TRUE;
828 bRet = FALSE;
829 State = STAT_EXIT;
830 break;
831 }
832 }
833 if (!memcmp(buf, strfail3, strlen(strfail3)))
834 {
835 // PrintInfo("分区表信息发生变化,NV分区发生变化,下载异常中止!");
836 LogInfo("DownloadPartitionfunc:: Partition change, stop download...");
837 State = STAT_EXIT;
838
839 bExit = TRUE;
840 bRet = FALSE;
841
842 State = STAT_EXIT;
843 break;
844 }
845 else
846 {
847 LogInfo("DownloadPartitionfunc:: Partition other fail, stop download...");
848 State = STAT_EXIT;
849
850 bExit = TRUE;
851 bRet = FALSE;
852
853 State = STAT_EXIT;
854 break;
855 }
856 }
857 break;
858
859 default:
860 bExit = TRUE;
861 }
862 }
863 if (pf != NULL)
864 {
865 fclose(pf);
866 pf = NULL;
867 }
868 return bRet;
869}
870
871static BOOL UnPreInitBoot()
872{
873 return TRUE;
874}
875
876static BOOL Initialize()
877{
878 // 下载小系统使用,下载完小系统,该标志为真
879 // 当该标志为真时,PC侧会发送一个结束命令使UE启动小系统
880 m_StartAMT = FALSE;
881 m_StartAMT_MODEM = FALSE;
882 m_StartAMT_AP = FALSE;
883 // 清空协议指针
884 m_pProtocolTable = (stSTAT *)NULL;
885 return TRUE;
886}
887
888static BOOL FileAnalysis()
889{
890 int fileIndex = 0;
891 BOOL bRet = FALSE;
892 stBinImageHead BinFileHeader;
893 int i = 0;
894 if (access(g_path, 0) != 0)
895 {
896 printf("FileAnalysisfunc:: software file path is error.\n");
897 return FALSE;
898 }
899 pf = fopen(g_path, "rb");
900 if (pf == NULL)
901 {
902 // 文件打开失败
903 if (g_path != NULL)
904 {
905 LogInfo("FileAnalysisfunc:: Open file FAIL... %s", g_path);
906 }
907 else
908 {
909 LogInfo("FileAnalysisfunc:: g_path ptr is NULL");
910 }
911 }
912 else
913 {
914 if (g_path != NULL)
915 {
916 LogInfo("FileAnalysisfunc:: Open file OK... %s", g_path);
917 }
918 else
919 {
920 LogInfo("FileAnalysisfunc:: g_path ptr is NULL");
921 }
922 }
923 if (m_iFileNum <= 0)
924 {
925 LogInfo("FileAnalysisfunc:: file num error m_iFileNum = %d", m_iFileNum);
926 return bRet;
927 }
928 fseek(pf, sizeof(m_stMasterInfo), 0);
929 // 逐个文件添加到上/下载列表中
930 for (i = 0; i < m_iFileNum; i++)
931 {
932 if (fread((BYTE *)&BinFileHeader, 1, sizeof(stBinImageHead), pf) != sizeof(stBinImageHead)) // pBootFile->Read(&WriteBuffer[0], dwPacketSize) != dwPacketSize)
933 {
934 LogInfo("FileAnalysisfunc:: Read file FAIL...");
935 bRet = FALSE;
936 }
937 if (memcmp(BinFileHeader.PartitionName, "NVR", 3) != 0)
938 {
939 if (BinFileHeader.PartitionName != NULL)
940 {
941 LogInfo("FileAnalysisfunc:: not NVR file, BinFileHeader.PartitionName = %s", BinFileHeader.PartitionName);
942 }
943 else
944 {
945 LogInfo("FileAnalysisfunc:: not NVR file, BinFileHeader.PartitionName ptr is NULL");
946 }
947 fileIndex++;
948 g_DownLoadFileNum++;
949 }
950 else
951 {
952 if (BinFileHeader.PartitionName != NULL)
953 {
954 LogInfo("FileAnalysisfunc:: NVR file, BinFileHeader.PartitionName = %s", BinFileHeader.PartitionName);
955 }
956 else
957 {
958 LogInfo("FileAnalysisfunc:: NVR file, BinFileHeader.PartitionName ptr is NULL");
959 }
960 continue;
961 }
962 // 上载/下载
963 FileItem[fileIndex].bDownload = TRUE;
964
965 // 文件名称(包含路径)
966 FileItem[fileIndex].strFileName = g_path;
967
968 // 是否使用相对路径
969 FileItem[fileIndex].bUseRelativePath = 0;
970
971 // 是否直接从文件读取数据
972 FileItem[fileIndex].bReadFromFile = 1;
973
974 // 自动回收缓存空间
975 FileItem[fileIndex].bAutoDeleteImageCache = FALSE;
976
977 // 若从缓存中读取数据,赋值缓存指针
978 FileItem[fileIndex].pImageCache = NULL;
979
980 // 相对于文件/缓存的偏移地址
981 FileItem[fileIndex].dwOffset = BinFileHeader.iFileOffset;
982
983 // 分区名
984 strcpy(FileItem[fileIndex].PartitionName, (char *)BinFileHeader.PartitionName);
985
986 // 待写入的长度,单位:字节
987 FileItem[fileIndex].dwLength = BinFileHeader.iFileLength;
988
989 // 分区的偏移地址
990 FileItem[fileIndex].nPartitionOffset = BinFileHeader.iPartitionOffset;
991
992 // 分区文件类型 raw nv fs nand
993 strcpy(FileItem[fileIndex].PartitionType, (char *)BinFileHeader.PartitionType);
994
995 FileItem[fileIndex].nType = nand;
996
997 // 打印Log
998 LogInfo("FileAnalysisfunc:: FileItem[%d].bDownload = %d", fileIndex, FileItem[fileIndex].bDownload);
999 if (FileItem[fileIndex].strFileName != NULL)
1000 {
1001 LogInfo("FileAnalysisfunc:: FileItem[%d].strFileName = %s", fileIndex, FileItem[fileIndex].strFileName);
1002 }
1003 else
1004 {
1005 LogInfo("FileAnalysisfunc:: FileItem[%d].strFileName ptr is NULL", fileIndex);
1006 }
1007 LogInfo("FileAnalysisfunc:: FileItem[%d].bUseRelativePath = %d", fileIndex, FileItem[fileIndex].bUseRelativePath);
1008 LogInfo("FileAnalysisfunc:: FileItem[%d].bReadFromFile = %d", fileIndex, FileItem[fileIndex].bReadFromFile);
1009
1010 LogInfo("FileAnalysisfunc:: FileItem[%d].bAutoDeleteImageCache = %d", fileIndex, FileItem[fileIndex].bAutoDeleteImageCache);
1011
1012 if (FileItem[fileIndex].pImageCache != NULL)
1013 {
1014 LogInfo("FileAnalysisfunc:: FileItem[%d].pImageCache = %s", fileIndex, FileItem[fileIndex].pImageCache);
1015 }
1016 else
1017 {
1018 LogInfo("FileAnalysisfunc:: FileItem[%d].pImageCache ptr is NULL", fileIndex);
1019 }
1020 LogInfo("FileAnalysisfunc:: FileItem[%d].dwOffset = %d", fileIndex, FileItem[fileIndex].dwOffset);
1021 if (FileItem[fileIndex].PartitionName != NULL)
1022 {
1023 LogInfo("FileAnalysisfunc:: FileItem[%d].PartitionName = %s", fileIndex, FileItem[fileIndex].PartitionName);
1024 }
1025 else
1026 {
1027 LogInfo("FileAnalysisfunc:: FileItem[%d].PartitionName ptr is NULL", fileIndex);
1028 }
1029 LogInfo("FileAnalysisfunc:: FileItem[%d].dwLength = %d", fileIndex, FileItem[fileIndex].dwLength);
1030 LogInfo("FileAnalysisfunc:: FileItem[%d].nPartitionOffset = %d", fileIndex, FileItem[fileIndex].nPartitionOffset);
1031 LogInfo("FileAnalysisfunc:: FileItem[%d].PartitionType = %d", fileIndex, FileItem[fileIndex].PartitionType);
1032 LogInfo("FileAnalysisfunc:: FileItem[%d].nType = %d", fileIndex, FileItem[fileIndex].nType);
1033 // 计算单个文件的CRC校验值
1034 // if(m_bCRCCheck)
1035 //{
1036 // if(!GetCRCValue(&FileItem))
1037 // {
1038 // return bRet;
1039 // }
1040 // }
1041 }
1042 bRet = TRUE;
1043 if (pf != NULL)
1044 {
1045 fclose(pf);
1046 pf = NULL;
1047 LogInfo("FileAnalysisfunc:: fclose(pf)... ");
1048 }
1049 return bRet;
1050}
1051
1052static void GetFileList(BOOL bDownload, enDownloadType DownloadType, BYTE *pBuffer, DWORD dwSize, DWORD nIndex)
1053{
1054 switch (LOWORD(DownloadType))
1055 {
1056 case DOWNLOAD_TYPE_IMAGE:
1057 // 下载版本文件信息
1058 if (FileAnalysis())
1059 {
1060 LogInfo("GetFileListfunc:: FileAnalysis OK");
1061 }
1062 break;
1063 default:
1064 break;
1065 }
1066}
1067
1068static void CollectDownloadImage(enDownloadType DownloadType, BYTE *pBuffer, DWORD dwSize)
1069{
1070 GetFileList(TRUE, DownloadType, pBuffer, dwSize, 0);
1071 LogInfo("CollectDownloadImagefunc:: GetFileList OK... ");
1072}
1073
1074static enSTAT GotoNextState(enSTAT enCurrentStat, BOOL bRet)
1075{
1076 enSTAT NextState = STAT_TERMINATE;
1077 int nDim = 0;
1078
1079 if (NULL == m_pProtocolTable)
1080 {
1081 LogInfo("GotoNextStatefunc:: NULL == m_pProtocolTable...");
1082 return NextState;
1083 }
1084
1085 if (m_DownloadImageTable == m_pProtocolTable)
1086 {
1087 nDim = sizeof(m_DownloadImageTable) / sizeof(m_DownloadImageTable[0]);
1088 }
1089 else if (m_DownloadRAMImageTable == m_pProtocolTable)
1090 {
1091 nDim = sizeof(m_DownloadRAMImageTable) / sizeof(m_DownloadRAMImageTable[0]);
1092 }
1093 else
1094 {
1095 nDim = 0;
1096 }
1097
1098 if (enCurrentStat >= nDim)
1099 {
1100 if (m_DownloadImageTable == m_pProtocolTable)
1101 {
1102 LogInfo("GotoNextStatefunc:: m_pProtocolTable -> m_DownloadImageTable...");
1103 }
1104 else if (m_DownloadRAMImageTable == m_pProtocolTable)
1105 {
1106 LogInfo("GotoNextStatefunc:: m_pProtocolTable -> m_DownloadRAMImageTable...");
1107 }
1108 else
1109 {
1110 LogInfo("GotoNextStatefunc:: error state...");
1111 }
1112 return NextState;
1113 }
1114
1115 if (TRUE == bRet)
1116 {
1117 NextState = m_pProtocolTable[enCurrentStat].mNextTRUE;
1118 }
1119 else
1120 {
1121 NextState = m_pProtocolTable[enCurrentStat].mNextFALSE;
1122 }
1123
1124 return NextState;
1125}
1126
1127static BOOL DownloadOneImage(stDownloadConfig CurrentConfig)
1128{
1129 enSTAT State = CurrentConfig.mStartState;
1130
1131 BOOL bRet = FALSE;
1132 BOOL bExit = FALSE;
1133 ULONG ulAckCRCValue = 0;
1134
1135 DWORD dwCurrentSize = 0;
1136 DWORD dwPacketCount = 0;
1137
1138 BYTE CommandBuffer[255] = {0};
1139 BYTE *AckBuffer;
1140
1141 BYTE Buftmp[20] = {0}; // 用于临时存放数据, add zxw 20130422
1142 size_t dwPacketSize = 0; // add zxw 20130422 数据包长度
1143 DWORD dwDDRPacketSize = 0;
1144 BYTE *pBuffer = CurrentConfig.mCurrentFile.pImageCache;
1145 DWORD dwFileSize = 0;
1146
1147 if (access(g_path, 0) != 0)
1148 {
1149 printf("DownloadOneImagefunc:: software file path is error.\r\n");
1150 return FALSE;
1151 }
1152 pf = fopen(g_path, "rb");
1153 if (pf == NULL)
1154 {
1155 // 文件打开失败
1156 if (g_path != NULL)
1157 {
1158 LogInfo("DownloadOneImagefunc:: Open file FAIL... %s", g_path);
1159 printf("33333333333333333333333\n");
1160 }
1161 else
1162 {
1163 LogInfo("DownloadOneImagefunc:: g_path ptr is NULL", g_path);
1164 printf("44444444444444444444\n");
1165 }
1166 }
1167 else
1168 {
1169 LogInfo("DownloadOneImagefunc:: Open file OK ");
1170 printf("DownloadOneImagefunc:: Open file OK \n");
1171 }
1172 fseek(pf, CurrentConfig.mCurrentFile.dwOffset, 0);
1173 while (!bExit)
1174 {
1175 switch (State)
1176 {
1177 //-------------------------设置Packet长度----------------------------
1178 case STAT_SET_PACKET_SIZE:
1179 {
1180 LogInfo("DownloadOneImagefunc:: STAT_SET_PACKET_SIZE");
1181 printf("DownloadOneImagefunc:: STAT_SET_PACKET_SIZE\n");
1182
1183 memset(CommandBuffer, 0, 255);
1184 sprintf(CommandBuffer, "compat_write %s %08x %0x", CurrentConfig.mCurrentFile.PartitionName, CurrentConfig.mCurrentFile.nPartitionOffset, CurrentConfig.mCurrentFile.dwLength);
1185 LogInfo("DownloadOneImagefunc:: CommandBuffer = %s", CommandBuffer);
1186 printf("DownloadOneImagefunc:: CommandBuffer = %s\n", CommandBuffer);
1187 dwFileSize = CurrentConfig.mCurrentFile.dwLength;
1188 bRet = SendData(CommandBuffer, strlen((const char *)CommandBuffer) + 1, 20, 1);
1189 if (bRet)
1190 {
1191 AckBuffer = (BYTE *)ReadDataExtraFuncB(14, 10, 4 * 60); // 适配nor flash,最大超时时间改为4分钟
1192 LogInfo("DownloadOneImagefunc:: AckBuffer = %x", AckBuffer[0]);
1193 }
1194 if ((AckBuffer[0] == 0x00) && (AckBuffer[1] == 0x44))
1195 {
1196 int indexextra = 0;
1197 for (indexextra = 0; indexextra < 14; indexextra++)
1198 {
1199 AckBuffer[indexextra] = AckBuffer[indexextra + 1];
1200 }
1201 }
1202 if (bRet && (!memcmp(AckBuffer, "DATA", 4)))
1203 {
1204 // 成功
1205 LogInfo("DownloadOneImagefunc:: STAT_SET_PACKET_SIZE OK");
1206 printf("DownloadOneImagefunc:: STAT_SET_PACKET_SIZE OK\n");
1207
1208 dwDDRPacketSize = strtoul((char *)AckBuffer + 4, 0, 16);
1209 // 将此包不是发送的一包的长度值实际是是DDR中设置的一包内存的大小。
1210 if (dwDDRPacketSize > CurrentConfig.mCurrentFile.dwLength)
1211 // 如果返回待写数据长度,大于镜像文件长度,则失败
1212 {
1213 LogInfo("DownloadOneImagefunc:: return data length greater than file total length, fail...dwDDRPacketSize = %d, CurrentConfig.mCurrentFile.dwLength = %d", dwDDRPacketSize, CurrentConfig.mCurrentFile.dwLength);
1214 printf("DownloadOneImagefunc:: return data length greater than file total length, fail...dwDDRPacketSize = %d, CurrentConfig.mCurrentFile.dwLength = %d\n", dwDDRPacketSize, CurrentConfig.mCurrentFile.dwLength);
1215
1216 State = GotoNextState(State, FALSE);
1217 bRet = FALSE;
1218 }
1219 else
1220 {
1221 LogInfo("DownloadOneImagefunc:: return data length <= file total length, dwDDRPacketSize = %d, CurrentConfig.mCurrentFile.dwLength = %d", dwDDRPacketSize, CurrentConfig.mCurrentFile.dwLength);
1222 // 执行下一项流程
1223 dwCurrentSize = dwDDRPacketSize;
1224 State = GotoNextState(State, TRUE);
1225 }
1226 }
1227 else // 失败
1228 {
1229 LogInfo("DownloadOneImagefunc:: STAT_SET_PACKET_SIZE fail");
1230 State = GotoNextState(State, FALSE);
1231 bRet = FALSE;
1232 }
1233 }
1234 break;
1235
1236 //------------------------Image数据-----------------------------
1237 case STAT_DATAPACKET:
1238 {
1239 dwFileSize -= dwCurrentSize;
1240 if (dwCurrentSize >= CurrentConfig.mPacketSize)
1241 {
1242 dwPacketSize = CurrentConfig.mPacketSize;
1243 }
1244 else
1245 {
1246 dwPacketSize = dwCurrentSize;
1247 }
1248
1249 BYTE *WriteBuffer = (BYTE *)malloc(dwPacketSize);
1250 if (dwCurrentSize > 0)
1251 {
1252 while (1)
1253 {
1254 // 读取磁盘数据
1255 memset(WriteBuffer, 0, dwPacketSize);
1256
1257 if (CurrentConfig.mCurrentFile.bReadFromFile)
1258 {
1259 if (dwCurrentSize > dwPacketSize)
1260 {
1261 dwPacketSize = dwPacketSize;
1262 }
1263 else
1264 {
1265 dwPacketSize = dwCurrentSize;
1266
1267 if ((dwPacketSize % 512) == 0)
1268 {
1269 dwPacketSize -= 8;
1270 // usb若不支持长度是512整数包,用0包表示结束时,则在此处拆包。
1271 LogInfo("DownloadOneImagefunc:: STAT_DATAPACKET split packet");
1272 // printf("DownloadOneImagefunc:: STAT_DATAPACKET split packet\n");
1273 }
1274 else
1275 {
1276 usleep(2 * 1000);
1277 // 由于有拆包动作,最后一包需要等待先前数据发送完成。
1278 }
1279 }
1280 if (fread(&WriteBuffer[0], 1, dwPacketSize, pf) != dwPacketSize)
1281 {
1282 LogInfo("DownloadOneImagefunc:: read packte fail");
1283
1284 State = GotoNextState(State, FALSE);
1285 if (WriteBuffer != NULL)
1286 {
1287 free(WriteBuffer);
1288 WriteBuffer = NULL;
1289 }
1290 bRet = FALSE;
1291 break;
1292 }
1293 }
1294 else
1295 {
1296 if (CurrentConfig.mCurrentFile.pImageCache != NULL)
1297 {
1298 memcpy(&WriteBuffer[0], pBuffer, dwPacketSize);
1299 pBuffer += dwPacketSize;
1300 }
1301 }
1302
1303 // 发送数据到UE
1304 bRet = SendData(WriteBuffer, dwPacketSize, 20, 1);
1305 if (bRet == TRUE)
1306 {
1307 LogInfo("DownloadOneImagefunc:: STAT_DATAPACKET :send one packet success");
1308 // printf("DownloadOneImagefunc:: STAT_DATAPACKET :send one packet success\n");
1309 }
1310 else
1311 {
1312 LogInfo("DownloadOneImagefunc:: STAT_DATAPACKET :send one packet FAIL!");
1313 State = GotoNextState(State, FALSE);
1314 bRet = FALSE;
1315 break;
1316 }
1317 dwCurrentSize -= dwPacketSize;
1318 if (dwCurrentSize == 0)
1319 {
1320 LogInfo("DownloadOneImagefunc:: STAT_DATAPACKET :dwCurrentSize == 0");
1321 if (dwFileSize > 0) //
1322 {
1323 AckBuffer = (BYTE *)ReadDataExtraFuncB(14, 10, 4 * 60); // 适配nor flash,最大超时时间改为4分钟
1324 LogInfo("DownloadOneImagefunc:: AckBuffer = %x", AckBuffer[0]);
1325 if ((AckBuffer[0] == 0x00) && (AckBuffer[1] == 0x44))
1326 {
1327 int indexextra = 0;
1328 for (indexextra = 0; indexextra < 14; indexextra++)
1329 {
1330 AckBuffer[indexextra] = AckBuffer[indexextra + 1];
1331 }
1332 }
1333 if (bRet && (!memcmp(AckBuffer, "DATA", 4)))
1334 {
1335 // 成功
1336 LogInfo("DownloadOneImagefunc:: STAT_SET_PACKET_SIZE OK");
1337 printf("DownloadOneImagefunc:: STAT_SET_PACKET_SIZE OK\n");
1338
1339 dwDDRPacketSize = strtoul((char *)AckBuffer + 4, 0, 16);
1340 // 将此包不是发送的一包的长度值实际是是DDR中设置的一包内存的大小。
1341 if (dwDDRPacketSize > CurrentConfig.mCurrentFile.dwLength)
1342 // 如果返回待写数据长度,大于镜像文件长度,则失败
1343 {
1344 LogInfo("DownloadOneImagefunc:: return data length greater than file total length, fail...dwDDRPacketSize = %d, CurrentConfig.mCurrentFile.dwLength = %d", dwDDRPacketSize, CurrentConfig.mCurrentFile.dwLength);
1345 State = GotoNextState(State, FALSE);
1346 bRet = FALSE;
1347 }
1348 else
1349 {
1350 LogInfo("DownloadOneImagefunc:: return data length <= file total length, dwDDRPacketSize = %d, CurrentConfig.mCurrentFile.dwLength = %d", dwDDRPacketSize, CurrentConfig.mCurrentFile.dwLength);
1351 // 执行下一项流程
1352 dwCurrentSize = dwDDRPacketSize;
1353 }
1354 }
1355 else // 失败
1356 {
1357 LogInfo("DownloadOneImagefunc:: STAT_SET_PACKET_SIZE fail");
1358 State = GotoNextState(State, FALSE);
1359 bRet = FALSE;
1360 }
1361 }
1362 else // 文件的最后一包
1363 {
1364
1365 // lx,如果支持CRC校验,boot回复CRC校验值,否则回复OKEY
1366 if (m_bCRCCheck)
1367 {
1368 bRet = ReadData((BYTE *)&ulAckCRCValue, 4, 30, 4 * 60); // 适配nor flash,最大超时时间改为4分钟
1369 LogInfo("DownloadOneImagefunc:: CurrentConfig.mCurrentFile.ulCRCValue = %ld ulAckCRCValue = %ld", CurrentConfig.mCurrentFile.ulCRCValue, ulAckCRCValue);
1370 // printf("DownloadOneImagefunc:: CurrentConfig.mCurrentFile.ulCRCValue = %ld ulAckCRCValue = %ld\n", CurrentConfig.mCurrentFile.ulCRCValue, ulAckCRCValue);
1371 if (bRet && (CurrentConfig.mCurrentFile.ulCRCValue == ulAckCRCValue))
1372 {
1373 // PrintInfo(_T("Image data send success!"));
1374 LogInfo("DownloadOneImagefunc:: Image data send success!CurrentConfig.mCurrentFile.ulCRCValue = %ld ulAckCRCValue = %ld", CurrentConfig.mCurrentFile.ulCRCValue, ulAckCRCValue);
1375 // printf("DownloadOneImagefunc:: Image data send success!CurrentConfig.mCurrentFile.ulCRCValue = %ld ulAckCRCValue = %ld\n", CurrentConfig.mCurrentFile.ulCRCValue, ulAckCRCValue);
1376 bRet = TRUE;
1377 State = GotoNextState(State, TRUE);
1378 }
1379 else
1380 {
1381 // PrintInfo(_T("Image data send fail!"));
1382 LogInfo("DownloadOneImagefunc:: Image data send fail!");
1383 bRet = FALSE;
1384 State = GotoNextState(State, FALSE);
1385 }
1386 }
1387 else
1388 {
1389 LogInfo("DownloadOneImagefunc:: not CRCCheck");
1390 printf("DownloadOneImagefunc:: not CRCCheck\n");
1391 AckBuffer = (BYTE *)ReadDataExtraFuncB(5, 10, 4 * 60); // 适配nor flash,最大超时时间改为4分钟
1392 // bRet = ReadDataExtraFuncA(14, 10 , 100);
1393 if (AckBuffer != NULL)
1394 {
1395 LogInfo("DownloadOneImagefunc:: AckBuffer... %s", AckBuffer);
1396 // printf("DownloadOneImagefunc:: AckBuffer... %s\n", AckBuffer);
1397 }
1398 else
1399 {
1400 LogInfo("DownloadOneImagefunc:: AckBuffer is NULL");
1401 }
1402 if (bRet && (!memcmp(AckBuffer, "OKAY", 4)))
1403 {
1404 LogInfo("DownloadOneImagefunc:: Image data send success!");
1405 printf("DownloadOneImagefunc:: Image data send success!\n");
1406 bRet = TRUE;
1407 State = GotoNextState(State, TRUE);
1408 }
1409 else
1410 {
1411 LogInfo("DownloadOneImagefunc:: Image data send fail!");
1412 State = GotoNextState(State, FALSE);
1413 bRet = FALSE;
1414 }
1415 }
1416 }
1417 break;
1418 }
1419 }
1420 }
1421
1422 if (WriteBuffer != NULL)
1423 {
1424 free(WriteBuffer);
1425 WriteBuffer = NULL;
1426 }
1427 }
1428 break;
1429 //--------------------------执行代码-----------------------------
1430 case STAT_EXECUTE_CODE:
1431 {
1432 LogInfo("DownloadOneImagefunc:: EXECUTE_CODE IN mbExecuteCode = %d PartitionType = %s", CurrentConfig.mbExecuteCode, CurrentConfig.mCurrentFile.PartitionType);
1433 // printf("DownloadOneImagefunc:: EXECUTE_CODE IN mbExecuteCode = %d PartitionType = %s\n", CurrentConfig.mbExecuteCode, CurrentConfig.mCurrentFile.PartitionType);
1434 if ((TRUE == CurrentConfig.mbExecuteCode) && (memcmp(CurrentConfig.mCurrentFile.PartitionType, "zftl", 3) == 0))
1435 {
1436 LogInfo("DownloadOneImagefunc:: start EXECUTE_CODE");
1437 printf("DownloadOneImagefunc:: start EXECUTE_CODE\n");
1438 memset(CommandBuffer, 0, sizeof(CommandBuffer));
1439 memcpy(CommandBuffer, ("reboot"), strlen(("reboot")));
1440 bRet = SendData(CommandBuffer, strlen((const char *)CommandBuffer) + 1, 10, 1);
1441 if (bRet)
1442 {
1443 AckBuffer = (BYTE *)ReadDataExtraFuncB(14, 10, 10);
1444 if (AckBuffer != NULL)
1445 {
1446 LogInfo("DownloadOneImagefunc:: AckBuffer... %s", AckBuffer);
1447 // printf("DownloadOneImagefunc:: AckBuffer... %s\n", AckBuffer);
1448 }
1449 else
1450 {
1451 LogInfo("DownloadOneImagefunc:: AckBuffer is NULL");
1452 }
1453 }
1454 if (bRet && !memcmp(AckBuffer, "OKAY REBOOT", 11))
1455 {
1456 // PrintInfo(_T("正在执行代码..."));
1457 LogInfo("DownloadOneImagefunc:: EXECUTE_CODE on doing...");
1458 printf("DownloadOneImagefunc:: EXECUTE_CODE on doing...\n");
1459 }
1460 else
1461 {
1462 char *strInfo;
1463 char *strTemp;
1464
1465 // sprintf(strInfo,"bRet = %d",bRet);
1466 // sprintf(strTemp,"%s",&AckBuffer[0]);
1467 LogInfo("DownloadOneImagefunc:: EXECUTE_CODE fail");
1468
1469 bRet = FALSE;
1470 }
1471 }
1472 else
1473 {
1474 bRet = TRUE;
1475 }
1476 State = GotoNextState(State, TRUE);
1477 }
1478 break;
1479 default:
1480 bExit = TRUE;
1481 break;
1482 }
1483 }
1484
1485 if (pf != NULL)
1486 {
1487 fclose(pf);
1488 pf = NULL;
1489 LogInfo("DownloadOneImagefunc:: fclose(pf) ");
1490 }
1491 return bRet;
1492}
1493
1494// 修改这个函数的打印 bufantong
1495static BOOL DownloadImage(stFileItem ImageInfo, BOOL bExecuteCode)
1496{
1497 BOOL bRet = FALSE;
1498 stDownloadConfig CurrentConfig;
1499 CurrentConfig.mCurrentFile = ImageInfo;
1500
1501 // 判断文件的类型
1502 switch (ImageInfo.nType)
1503 {
1504 case nand: // 类型为nand 时,写操作之前全部擦除。
1505 LogInfo("DownloadImagefunc:: case nand...");
1506 printf("DownloadImagefunc:: case nand...\n");
1507
1508 m_pProtocolTable = m_DownloadImageTable;
1509
1510 CurrentConfig.mPacketSize = PACKET_SIZE_FLASH;
1511
1512 // 下载到Flash中的文件是不需要打开启动AMT的标志的
1513 m_StartAMT = FALSE;
1514 break;
1515
1516 case fs:
1517 LogInfo("DownloadImagefunc:: case fs...");
1518 printf("DownloadImagefunc:: case fs...\n");
1519 m_pProtocolTable = m_DownloadImageTable;
1520
1521 CurrentConfig.mPacketSize = PACKET_SIZE_FLASH;
1522
1523 // 下载到Flash中的文件是不需要打开启动AMT的标志的
1524 m_StartAMT = FALSE;
1525 break;
1526
1527 case zftl: // 该分支是下载NVR分区用的 分区名是NVR,NVRW ,RAMDISK三个分区是该类型,写操作时,是先备份再擦写再覆盖。
1528 LogInfo("DownloadImagefunc:: case zftl...");
1529 printf("DownloadImagefunc:: case zftl...\n");
1530 ImageInfo.nType = zftl;
1531 m_pProtocolTable = m_DownloadImageTable;
1532 CurrentConfig.mPacketSize = PACKET_SIZE_FLASH;
1533
1534 // 下载NV备份文件是不需要打开启动AMT的标志的
1535 m_StartAMT = FALSE;
1536 break;
1537
1538 case ddr:
1539 // pImageInfo->nType = EXTSRAM;
1540 LogInfo("DownloadImagefunc:: case ddr...");
1541 printf("DownloadImagefunc:: case ddr...\n");
1542 ImageInfo.nType = ddr;
1543 m_pProtocolTable = m_DownloadRAMImageTable;
1544 CurrentConfig.mPacketSize = PACKET_SIZE_IMAGE;
1545 CurrentConfig.mCurrentFile.PartitionName[0] = 'd';
1546 CurrentConfig.mCurrentFile.PartitionName[1] = 'd';
1547 CurrentConfig.mCurrentFile.PartitionName[2] = 'r';
1548 CurrentConfig.mCurrentFile.PartitionName[3] = '\0';
1549
1550 m_StartAMT = TRUE;
1551 break;
1552 case zloader:
1553 LogInfo("DownloadImagefunc:: case zloader...");
1554 printf("DownloadImagefunc:: case zloader...\n");
1555 // pImageInfo->nType = EXTSRAM;
1556 ImageInfo.nType = zloader;
1557 m_pProtocolTable = m_DownloadRAMImageTable;
1558 CurrentConfig.mPacketSize = PACKET_SIZE_IMAGE;
1559 CurrentConfig.mCurrentFile.PartitionName[0] = 'z';
1560 CurrentConfig.mCurrentFile.PartitionName[1] = 'l';
1561 CurrentConfig.mCurrentFile.PartitionName[2] = 'o';
1562 CurrentConfig.mCurrentFile.PartitionName[3] = 'a';
1563 CurrentConfig.mCurrentFile.PartitionName[4] = 'd';
1564 CurrentConfig.mCurrentFile.PartitionName[5] = 'e';
1565 CurrentConfig.mCurrentFile.PartitionName[6] = 'r';
1566 CurrentConfig.mCurrentFile.PartitionName[7] = '\0';
1567
1568 m_StartAMT = TRUE;
1569 break;
1570
1571 default:
1572 {
1573 LogInfo("DownloadImagefunc:: not right type...");
1574 printf("DownloadImagefunc:: not right type...\n");
1575 return TRUE;
1576 }
1577 break;
1578 }
1579
1580 if (ImageInfo.bReadFromFile)
1581 {
1582 if (access(g_path, 0) != 0)
1583 {
1584 printf("DownloadImagefunc:: software file path is error.\r\n");
1585 return FALSE;
1586 }
1587 pf = fopen(g_path, "rb");
1588 if (pf == NULL)
1589 {
1590 LogInfo("DownloadImagefunc:: Open file FAIL... %s", g_path);
1591 }
1592 }
1593 CurrentConfig.mStartState = STAT_SET_PACKET_SIZE;
1594 // CurrentConfig.mpFile = pf;
1595 CurrentConfig.mbExecuteCode = bExecuteCode;
1596 // 这个函数里也加了打印
1597 bRet = DownloadOneImage(CurrentConfig);
1598 // usleep(10*1000);
1599 if (ImageInfo.bReadFromFile)
1600 {
1601 if (pf != NULL)
1602 {
1603 fclose(pf);
1604 pf = NULL;
1605 LogInfo("DownloadImagefunc:: fclose(pf) ");
1606 }
1607 }
1608 return bRet;
1609}
1610
1611static BOOL ExchangeImage()
1612{
1613 BOOL bRet = FALSE;
1614 BOOL bExecuteCode = FALSE;
1615 LogInfo("ExchangeImagefunc:: g_DownLoadFileNum = %d ...", g_DownLoadFileNum);
1616 if (g_DownLoadFileNum == 0)
1617 {
1618 LogInfo("ExchangeImagefunc:: g_DownLoadFileNum == 0 ...", g_DownLoadFileNum);
1619 return bRet;
1620 }
1621 int fileIndex = 0;
1622 for (fileIndex = 1; fileIndex <= g_DownLoadFileNum; fileIndex++)
1623 {
1624 LogInfo("CYF start ExchangeImage = %d ...", fileIndex);
1625
1626 LogInfo("ExchangeImagefunc:: FileItem[%d].bDownload = %d", fileIndex, FileItem[fileIndex].bDownload);
1627 LogInfo("ExchangeImagefunc:: FileItem[%d].strFileName = %s", fileIndex, FileItem[fileIndex].strFileName);
1628 LogInfo("ExchangeImagefunc:: FileItem[%d].bUseRelativePath = %d", fileIndex, FileItem[fileIndex].bUseRelativePath);
1629 LogInfo("ExchangeImagefunc:: FileItem[%d].bReadFromFile = %d", fileIndex, FileItem[fileIndex].bReadFromFile);
1630 LogInfo("ExchangeImagefunc:: FileItem[%d].bAutoDeleteImageCache = %d", fileIndex, FileItem[fileIndex].bAutoDeleteImageCache);
1631 LogInfo("ExchangeImagefunc:: FileItem[%d].pImageCache = %s", fileIndex, FileItem[fileIndex].pImageCache);
1632 LogInfo("ExchangeImagefunc:: FileItem[%d].dwOffset = %d", fileIndex, FileItem[fileIndex].dwOffset);
1633 LogInfo("ExchangeImagefunc:: FileItem[%d].PartitionName = %s", fileIndex, FileItem[fileIndex].PartitionName);
1634 LogInfo("ExchangeImagefunc:: FileItem[%d].dwLength = %d", fileIndex, FileItem[fileIndex].dwLength);
1635 LogInfo("ExchangeImagefunc:: FileItem[%d].nPartitionOffset = %d", fileIndex, FileItem[fileIndex].nPartitionOffset);
1636 LogInfo("ExchangeImagefunc:: FileItem[%d].PartitionType = %d", fileIndex, FileItem[fileIndex].PartitionType);
1637 LogInfo("ExchangeImagefunc:: FileItem[%d].nType = %d", fileIndex, FileItem[fileIndex].nType);
1638
1639 LogInfo("ExchangeImagefunc:: bDownload FileItem[fileIndex].bDownload = %d...", FileItem[fileIndex].bDownload);
1640 if (FileItem[fileIndex].bDownload)
1641 {
1642 if (fileIndex == g_DownLoadFileNum)
1643 {
1644 LogInfo("ExchangeImagefunc:: bExecuteCode == TRUE ..., fileIndex = %d", fileIndex);
1645 bExecuteCode = FALSE;
1646 }
1647 else
1648 {
1649 LogInfo("ExchangeImagefunc:: bExecuteCode == FALSE ..., fileIndex = %d", fileIndex);
1650 }
1651 LogInfo("ExchangeImagefunc:: bDownload true ...");
1652 bRet = DownloadImage(FileItem[fileIndex], bExecuteCode);
1653 usleep(10 * 1000);
1654 }
1655 else
1656 {
1657 LogInfo("ExchangeImagefunc:: bDownload false...");
1658 }
1659
1660 if (bRet)
1661 {
1662 LogInfo("ExchangeImagefunc:: %d OK...", fileIndex);
1663 printf("ExchangeImagefunc:: %d OK...\r\n", fileIndex);
1664 }
1665 else
1666 {
1667 LogInfo("ExchangeImagefunc:: %d FAIL...", fileIndex);
1668 printf("ExchangeImagefunc:: %d FAIL...\r\n", fileIndex);
1669 break;
1670 }
1671 }
1672 return bRet;
1673}
1674
1675static BOOL UnInitialize()
1676{
1677 return TRUE;
1678}
1679
1680static BOOL DoDownload(enDownloadType DownloadType, BYTE *pBuffer, DWORD dwSize, BOOL bBackupNV, enDownloadNVBackupMethod Method, BOOL bEableCRCCheck)
1681{
1682 BOOL bRet = FALSE;
1683 BOOL bReturn = FALSE;
1684 BOOL bExit = FALSE;
1685 typedef enum
1686 {
1687 STAT_INIT,
1688 STAT_COLLECT_UPLOAD_NV_INFO,
1689 STAT_COLLECT_DOWNLOAD_IMAGE_INFO,
1690 STAT_EXCHANGE_IMAGE,
1691 STAT_START_AMT,
1692 STAT_UNINIT,
1693 STAT_EXIT
1694 } STAT_ITEM;
1695 int nState = STAT_INIT;
1696 LogInfo("DoDownloadfunc:: start...");
1697 m_bBackUpNV = bBackupNV;
1698 while (!bExit)
1699 {
1700 switch (nState)
1701 {
1702 case STAT_INIT: // 初始化资源
1703 bRet = Initialize();
1704 if (bRet)
1705 {
1706 LogInfo("DoDownloadfunc:: STAT_INIT OK...");
1707 // PrintInfo(_T("init sucess!"));
1708 nState = STAT_COLLECT_UPLOAD_NV_INFO;
1709 }
1710 else
1711 {
1712 // PrintInfo(_T("init fail!"));
1713 LogInfo("DoDownloadfunc:: STAT_INIT FAIL...");
1714 nState = STAT_EXIT;
1715 }
1716 break;
1717
1718 case STAT_COLLECT_UPLOAD_NV_INFO: // 收集备份NV上载信息
1719 // lx增加CRC校验握手状态
1720 if (bEableCRCCheck)
1721 {
1722 nState = STAT_CRC_ON;
1723 }
1724 else if (m_bCRCCheck)
1725 {
1726 nState = STAT_CRC_OFF;
1727 }
1728 else
1729 {
1730 nState = STAT_COLLECT_DOWNLOAD_IMAGE_INFO;
1731 }
1732 break;
1733
1734 case STAT_CRC_ON:
1735 {
1736 // 打开CRC校验
1737 BYTE CommandBuffer[255] = {0};
1738 BYTE AckBuffer[255] = {0};
1739 memset(CommandBuffer, 0, sizeof(CommandBuffer));
1740 memset(AckBuffer, 0, sizeof(AckBuffer));
1741 memcpy(&CommandBuffer[0], "CRC ON", 6);
1742 bRet = SendData(CommandBuffer, strlen((const char *)CommandBuffer) + 1, 10, 1);
1743 if (bRet)
1744 {
1745 bRet = ReadData(AckBuffer, 5, 10, 30);
1746 }
1747 if (bRet && (!memcmp(AckBuffer, "OKAY", 4)))
1748 {
1749 // boot支持CRC校验
1750 // PrintInfo(_T("CRC校验打开成功!"));
1751 LogInfo("DoDownloadfunc:: CRC open OK...");
1752 m_bCRCCheck = TRUE;
1753 nState = STAT_COLLECT_DOWNLOAD_IMAGE_INFO;
1754 }
1755 else // 失败
1756 {
1757 // PrintInfo(_T("CRC校验打开失败!"));
1758 LogInfo("DoDownloadfunc:: CRC open FAIL..");
1759 m_bCRCCheck = FALSE;
1760 nState = STAT_COLLECT_DOWNLOAD_IMAGE_INFO;
1761 }
1762 }
1763 break;
1764 case STAT_CRC_OFF:
1765 {
1766 // 关闭CRC校验
1767 BYTE CommandBuffer[255] = {0};
1768 BYTE AckBuffer[255] = {0};
1769 memset(CommandBuffer, 0, sizeof(CommandBuffer));
1770 memset(AckBuffer, 0, sizeof(AckBuffer));
1771 memcpy(&CommandBuffer[0], "CRC OFF", 7);
1772 bRet = SendData(CommandBuffer, strlen((const char *)CommandBuffer) + 1, 10, 1);
1773 if (bRet)
1774 {
1775 bRet = ReadData(AckBuffer, 5, 10, 30);
1776 }
1777 if (bRet && (!memcmp(AckBuffer, "OKAY", 4)))
1778 {
1779 // boot支持CRC校验
1780 // PrintInfo(_T("CRC校验关闭成功!"));
1781 LogInfo("DoDownloadfunc:: CRC close OK...");
1782 m_bCRCCheck = FALSE;
1783 nState = STAT_COLLECT_DOWNLOAD_IMAGE_INFO;
1784 }
1785 else // 失败
1786 {
1787 // PrintInfo(_T("CRC校验关闭失败!"));
1788 LogInfo("DoDownloadfunc:: CRC close FAIL...");
1789 m_bCRCCheck = TRUE;
1790 nState = STAT_COLLECT_DOWNLOAD_IMAGE_INFO;
1791 }
1792 }
1793 break;
1794
1795 case STAT_COLLECT_DOWNLOAD_IMAGE_INFO: // 收集下载Image信息
1796 // 收集下载Image信息
1797 LogInfo("DoDownloadfunc:: Collect image...");
1798 CollectDownloadImage(DownloadType, pBuffer, dwSize);
1799 nState = STAT_EXCHANGE_IMAGE;
1800 break;
1801
1802 case STAT_EXCHANGE_IMAGE: // Exchange数据
1803
1804 LogInfo("DoDownloadfunc:: ExchangeImage...");
1805 bRet = ExchangeImage();
1806 if (bRet)
1807 {
1808 // PrintInfo(_T("Exchange data success!"));
1809 nState = STAT_START_AMT;
1810 }
1811 else
1812 {
1813 // PrintInfo(_T("Exchange data fail!"));
1814 nState = STAT_UNINIT;
1815 }
1816 break;
1817
1818 case STAT_START_AMT: // 发送启动AMT标志
1819 bReturn = TRUE;
1820 nState = STAT_UNINIT;
1821 break;
1822
1823 case STAT_UNINIT: // 释放资源
1824 bRet = UnInitialize();
1825 if (bRet)
1826 {
1827 // PrintInfo(_T("release success!"));
1828 }
1829 else
1830 {
1831 // PrintInfo(_T("release fail!"));
1832 bReturn = FALSE;
1833 }
1834 nState = STAT_EXIT;
1835 break;
1836
1837 default:
1838 bExit = TRUE;
1839 }
1840 }
1841
1842 return bReturn;
1843}
1844
1845/*******************************************************************************
1846 * 外部函数 *
1847 *******************************************************************************/
1848BOOL ExecuteEraseNVRW(const char *softwarepath)
1849{
1850 BOOL bRet = FALSE;
1851
1852 LogInfo("ExecuteEraseNVRWfunc:: start...");
1853 printf("ExecuteEraseNVRWfunc:: start...");
1854
1855 strcpy(g_path, (char *)softwarepath);
1856
1857 if (!EraseNVRW())
1858 {
1859 LogInfo("ExecuteEraseNVRWfunc:: EraseNVRW FAIL...");
1860 }
1861 else
1862 {
1863 LogInfo("ExecuteEraseNVRWfunc:: EraseNVRW OK...");
1864 bRet = TRUE;
1865 }
1866 return bRet;
1867}
1868
1869int PrimaryDoDownload(const char *softwarepath)
1870{
1871 // 打开log文件
1872 // 提示开始下载
1873 printf("PrimaryDoDownloadfunc:: start\r\n");
1874 LogInfo("PrimaryDoDownloadfunc:: start\r\n");
1875
1876 enERRORCODE DownloadResult = Download_OK;
1877 strcpy(g_path, softwarepath);
1878
1879 // 下载版本文件
1880 if (!DoDownload(DOWNLOAD_TYPE_IMAGE, NULL, 0, FALSE, METHOD_NORMAL, FALSE))
1881 {
1882 LogInfo("PrimaryDoDownloadfunc:: DoDownload Image FAIL...");
1883 DownloadResult = Download_DOWNLOAD_IMAGE_FAIL;
1884 }
1885 else
1886 {
1887 LogInfo("PrimaryDoDownloadfunc:: DoDownload Image OK...");
1888 }
1889 LogInfo("PrimaryDoDownloadfunc:: DoDownload Image DownloadResult=%d", DownloadResult);
1890
1891 return DownloadResult;
1892}
1893
1894// 下载tloader, tboot, partion
1895BOOL DoDownloadBootForDL(BOOL bCheckPartition, const char *softwarepath)
1896{
1897 long n = 1000000;
1898 double duration = 0;
1899
1900 clock_t start;
1901 clock_t finish;
1902 DWORD nStartTime = 0;
1903 DWORD nStopTime = 0;
1904 BOOL bRet = FALSE;
1905 BOOL bTotalRet = TRUE;
1906 BOOL bExit = FALSE;
1907 typedef enum
1908 {
1909 STAT_INIT,
1910 STAT_DOWNLOAD_BOOT1,
1911 STAT_DOWNLOAD_BOOT2,
1912
1913 STAT_DOWNLOAD_PARTITION,
1914 STAT_UNINIT,
1915 STAT_EXIT
1916 } STAT;
1917 int nState = STAT_INIT;
1918 int nWhile = 0;
1919
1920 strcpy(g_path, softwarepath);
1921
1922 LogInfo("DoDownloadBootForDL:: start...");
1923 printf("DoDownloadBootForDL:: start...\n");
1924
1925 while (!bExit)
1926 {
1927 LogInfo("DoDownloadBootForDLfunc:: nWhile = %d\n", nWhile);
1928 printf("DoDownloadBootForDLfunc:: nWhile = %d\n", nWhile);
1929 nWhile++;
1930
1931 switch (nState)
1932 {
1933 case STAT_INIT: // 初始化资源
1934
1935 bRet = PreInitBoot();
1936
1937 if (bRet)
1938 {
1939 LogInfo("DoDownloadBootForDLfunc:: PreInitBoot OK...");
1940 nState = STAT_DOWNLOAD_BOOT1;
1941 }
1942 else
1943 {
1944 LogInfo("DoDownloadBootForDLfunc:: PreInitBoot FAIL...");
1945 printf("DoDownloadBootForDLfunc:: PreInitBoot FAIL...\n");
1946
1947 bTotalRet = FALSE;
1948 nState = STAT_EXIT;
1949 }
1950 break;
1951 /*********************add zxw 20121009 for 296301 begin************************************/
1952 case STAT_DOWNLOAD_BOOT1: // 下载stage1, TLoader
1953
1954 start = clock(); // 记录开始时间
1955 LogInfo("DoDownloadBootForDLfunc:: start download BOOT1...");
1956 printf("DoDownloadBootForDLfunc:: start download BOOT1...\n");
1957
1958 bRet = DownloadOneBoot(m_dwBootStage1StartAddress, m_dwBootStage1Size, pf, 4000, m_stMasterInfo.tloaderOffset, TRUE);
1959
1960 finish = clock(); // 结束时间
1961 duration = (double)(finish - start) / CLOCKS_PER_SEC;
1962 LogInfo("DoDownloadBootForDLfunc:: download BOOT1 communication time: %lf", duration);
1963
1964 if (bRet)
1965 {
1966 LogInfo("DoDownloadBootForDLfunc:: download BOOT1 OK");
1967 nState = STAT_DOWNLOAD_BOOT2;
1968 }
1969 else
1970 {
1971 LogInfo("DoDownloadBootForDLfunc:: download BOOT1 FAIL");
1972 printf("DoDownloadBootForDLfunc:: download BOOT1 FAIL\n");
1973
1974 bTotalRet = FALSE;
1975 nState = STAT_UNINIT;
1976 }
1977 break;
1978
1979 case STAT_DOWNLOAD_BOOT2: // 下载stage2, TBoot
1980
1981 start = clock(); // 记录开始时间
1982 LogInfo("DoDownloadBootForDLfunc:: start download BOOT2...");
1983 printf("DoDownloadBootForDLfunc:: start download BOOT2...\n");
1984
1985 bRet = DownloadOneBoot(m_dwBootStage2StartAddress, m_dwBootStage2Size, pf, 4000, m_stMasterInfo.tBootOffset, TRUE);
1986
1987 finish = clock(); // 结束时间
1988 duration = (double)(finish - start) / CLOCKS_PER_SEC;
1989 LogInfo("DoDownloadBootForDLfunc:: download BOOT2 communication time: %lf", duration);
1990
1991 if (bRet)
1992 {
1993 LogInfo("DoDownloadBootForDLfunc:: download BOOT2 OK");
1994 nState = STAT_DOWNLOAD_PARTITION;
1995 }
1996 else
1997 {
1998 LogInfo("DoDownloadBootForDLfunc:: download BOOT2 FAIL");
1999 printf("DoDownloadBootForDLfunc:: download BOOT2 FAIL\n");
2000
2001 nState = STAT_UNINIT;
2002 bTotalRet = FALSE;
2003 }
2004 break;
2005 /*********************add zxw 20121009 for 296301 end************************************/
2006 case STAT_DOWNLOAD_PARTITION:
2007
2008 start = clock(); // 记录开始时间
2009 LogInfo("DoDownloadBootForDLfunc:: start download PARTITION");
2010 printf("DoDownloadBootForDLfunc:: start download PARTITION\n");
2011
2012 bRet = DownloadPartition((size_t)m_dwPartitionSize, pf, 4096, m_stMasterInfo.PartitionOffset, FALSE);
2013
2014 finish = clock(); // 结束时间
2015 duration = (double)(finish - start) / CLOCKS_PER_SEC;
2016 LogInfo("DoDownloadBootForDLfunc:: download PARTITION communication time: %lf", duration);
2017
2018 if (bRet)
2019 {
2020 LogInfo("DoDownloadBootForDLfunc:: DownloadPartition : download OK");
2021 }
2022 else
2023 {
2024 LogInfo("DoDownloadBootForDLfunc:: DownloadPartition : download FAIL");
2025 printf("DoDownloadBootForDLfunc:: DownloadPartition : download FAIL\n");
2026 bTotalRet = FALSE;
2027 }
2028
2029 nState = STAT_UNINIT;
2030 break;
2031
2032 case STAT_UNINIT:
2033 bRet = UnPreInitBoot();
2034 if (bRet)
2035 {
2036 LogInfo("DoDownloadBootForDLfunc:: UnPreInitBoot OK");
2037 }
2038 else
2039 {
2040 LogInfo("DoDownloadBootForDLfunc:: UnPreInitBoot FAIL");
2041 bTotalRet = FALSE;
2042 }
2043 nState = STAT_EXIT;
2044 break;
2045
2046 default:
2047
2048 bExit = TRUE;
2049 break;
2050 }
2051 }
2052 return bTotalRet;
2053}