boolCheckJumpToApplication(void) { bool ret = false; char szBuff[128]; DWORD dwBytesRead = 0; if (ReadFile(hSerial, szBuff, sizeof(szBuff), &dwBytesRead, NULL)) { if (dwBytesRead > 0) { szBuff[dwBytesRead] = '\0'; // 添加终止符 // printf("Get data command: %s\n", szBuff); if (szBuff[0] == 'A' && szBuff[2] == 'P') { ret = true; printf("[BOOT] request to jump tp application\n"); } elseif (szBuff[0] == 'V' && szBuff[4] == 'D') { app_is_valid = true; printf("[BOOT] updated and app is valid\n"); } elseif (szBuff[0] == 'I' && szBuff[1] == 'N') { app_is_valid = false; printf("[BOOT] updated and app is invalid\n"); } elseif (szBuff[0] == 'F' && szBuff[3] == 'E') { trig_file = true; printf("[BOOT] request data of .exe file\n"); } } } return ret; }
boolAppUpdate(void) { bool ret = false; char szBuff[512000]; // 分配512K缓冲区 DWORD dwBytesRead = 0; bool trig_file = false; if (ReadFile(hSerial, szBuff, sizeof(szBuff), &dwBytesRead, NULL)) { if (dwBytesRead > 0) { szBuff[dwBytesRead] = '\0'; printf("[BOOT] Get file size: %d\n", dwBytesRead); ret = true; } }
if (ret == true) { app_is_valid = false; /* open json file */ FILE *file = NULL; file = fopen("app_demo.exe", "wb"); if (file == NULL) { printf("Open file failed!\n"); ret = false; }
/* write json file */ if (ret == true) { int ret = fwrite(szBuff, sizeof(char), dwBytesRead, file); if (ret == 0) { printf("write to file failed!\n"); ret = false; }
fclose(file); } } return ret; }
我这里写的比较简单,纯粹验证可行性;不能忘记在main的执行中调用AppUpdate:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/**************下面是Bootloader的程序内容****************/ printf("[BOOT] This is Bootloader Program\n");
ComInit();
while(1) { if (CheckJumpToApplication()) { CloseHandle(hSerial); // printf("Need Jump to Application\n"); request_update = false; /*重新写入配置标志到json文件*/ WriteGlobalConfiguration(); break; } if (trig_file == true) { AppUpdate(); trig_file = false; } }