gfabootlmast.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <malloc.h>
  4. #include <ctype.h>
  5. #include <errno.h>
  6. #include <unistd.h>
  7. #include <gfaserial.h>
  8. #include <gfamininetmst.h>
  9. #include <byteswap.h>
  10. #include "bl_commands.h"
  11. #include "gfabootlmast.h"
  12. #include "dbghlp.h"
  13. ////////////////////////////////////////////////////////////////////////////////////
  14. #define _dword_offset(m) (offsetof(GFA_APP_IMG_HEADER, m) / sizeof(uint32_t) - 2)
  15. #define _EXEC_PROGRESS_CALLBACK(pfn, ...) if(pfn) (*pfn)(__FILE__, __LINE__, __VA_ARGS__)
  16. #define _GFA_MAX_DUMP_DWORDS 16
  17. #define _BOOTLOADER_DEF_BAUDRATE 19200
  18. ////////////////////////////////////////////////////////////////////////////////////
  19. typedef struct _GFA_BLM
  20. {
  21. HGFAMINEMST hMst;
  22. GFA_BLM_DL_PROGRESS_PARAMS dlpp;
  23. PFN_GFA_BLM_DL_PROGRESS pfnDlProgress;
  24. void *pUserParam;
  25. int nVerbosity;
  26. }GFA_BLM, *LPGFA_BLM;
  27. typedef const GFA_BLM *LPCGFA_BLM;
  28. ////////////////////////////////////////////////////////////////////////////////////
  29. HGFABLM GfaBlmOpen(LPCGFA_BLM_CFG_PARAMS pblmcfg)
  30. {
  31. if(pblmcfg)
  32. {
  33. HGFAMINEMST hMst = GfaMininetMasterOpen(&pblmcfg->mmcp);
  34. if(hMst)
  35. {
  36. LPGFA_BLM pBlm = malloc(sizeof(GFA_BLM));
  37. memset(pBlm, 0, sizeof(GFA_BLM));
  38. pBlm->hMst = hMst;
  39. pBlm->pfnDlProgress = pblmcfg->pfnDlProgress;
  40. pBlm->pUserParam = pblmcfg->pUserParam;
  41. return (HGFABLM)pBlm;
  42. }
  43. return NULL;
  44. }
  45. errno = EINVAL;
  46. return NULL;
  47. }
  48. ////////////////////////////////////////////////////////////////////////////////////
  49. void GfaBlmClose(HGFABLM hBlm)
  50. {
  51. if(hBlm)
  52. {
  53. LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
  54. GfaMininetMasterClose(pBlm->hMst);
  55. free(pBlm);
  56. }
  57. }
  58. /////////////////////////////////////////////////////////////////////////////
  59. int GfaBlmResetSlaveIndex(HGFABLM hBlm, uint8_t nNodeAddr)
  60. {
  61. if(hBlm)
  62. {
  63. LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
  64. return GfaMininetMasterResetSlaveIndex(pBlm->hMst, nNodeAddr);
  65. }
  66. errno = EINVAL;
  67. return -1;
  68. }
  69. ////////////////////////////////////////////////////////////////////////////////////
  70. ssize_t GfaBlmBUCmdPollData(HGFABLM hBlm, uint8_t nNodeAddr, void *pData, size_t nCbData, uint32_t nTimeoutMS)
  71. {
  72. if(hBlm && pData && nCbData && !NODE_IS_MULTICAST(nNodeAddr))
  73. {
  74. size_t s, nReceived = 0;
  75. ssize_t nLen, nRet = -1;
  76. uint8_t cmd[32];
  77. uint8_t txb[32];
  78. uint8_t rxb[512];
  79. LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
  80. uint8_t *pszData = (uint8_t*)pData;
  81. if(nTimeoutMS > 0)
  82. {
  83. struct timeval tvRX;
  84. tvRX.tv_sec = nTimeoutMS / 1000;
  85. tvRX.tv_usec = (nTimeoutMS % 1000) * 1000;
  86. GfaMininetMasterSaveTimeouts(pBlm->hMst);
  87. GfaMininetMasterSetTimeouts(pBlm->hMst, &tvRX, NULL);
  88. }
  89. while(nReceived < nCbData)
  90. {
  91. s = GfaBlmBuildCmdDataPacket("BU", 0, NULL, 0, cmd, sizeof(cmd), true);
  92. nLen = GfaMininetMasterBuildFrame(pBlm->hMst, nNodeAddr, 0, cmd, s, txb, sizeof(txb));
  93. if((nRet = GfaMininetMasterTransmitFrame(pBlm->hMst, txb, nLen)) <= 0)
  94. {
  95. nRet = -1;
  96. break;
  97. }
  98. if((nLen = GfaMininetMasterReceiveFrame(pBlm->hMst, rxb, sizeof(rxb), true)) > 0)
  99. {
  100. uint8_t nIndex;
  101. nRet = GfaMininetMasterEvaluateSlaveResponse(pBlm->hMst, nNodeAddr, rxb, nLen, true, &nIndex);
  102. if(nRet == MINET_SLAVE_RESPONSE_SUCCESS)
  103. {
  104. if((nRet = GfaMininetMasterGetDataFromSlaveFrame(rxb, nLen, pszData, nCbData - nReceived)) > 0)
  105. {
  106. pszData += nRet;
  107. nReceived += nRet;
  108. }
  109. }
  110. else if(nRet == MINET_SLAVE_RESPONSE_ACK)
  111. {
  112. if(nTimeoutMS > 0)
  113. {
  114. if(nTimeoutMS >= 100)
  115. {
  116. usleep(100000);
  117. nTimeoutMS -= 100;
  118. }
  119. else
  120. {
  121. usleep(nTimeoutMS * 1000);
  122. nTimeoutMS = 0;
  123. }
  124. }
  125. else
  126. {
  127. errno = ETIMEDOUT;
  128. nReceived = 0;
  129. break;
  130. }
  131. }
  132. else if(nRet == MINET_SLAVE_RESPONSE_INDEX_IS_STATUS_CODE)
  133. {
  134. errno = -(int)nIndex;
  135. nReceived = 0;
  136. break;
  137. }
  138. else
  139. {
  140. errno = EPROTO;
  141. nReceived = 0;
  142. break;
  143. }
  144. }
  145. else
  146. {
  147. nReceived = 0;
  148. break;
  149. }
  150. }
  151. if(nTimeoutMS > 0)
  152. GfaMininetMasterRestoreTimeouts(pBlm->hMst);
  153. return nReceived;
  154. }
  155. errno = EINVAL;
  156. return -1;
  157. }
  158. /////////////////////////////////////////////////////////////////////////////
  159. int GfaBlmBUCmdReset(HGFABLM hBlm, uint8_t nNodeAddr, uint32_t nInitialBaudrate)
  160. {
  161. if(hBlm)
  162. {
  163. LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
  164. uint8_t nIndex;
  165. size_t s;
  166. ssize_t nRet, nLen;
  167. uint8_t cmd = COMMAND_RESET;
  168. uint8_t cmddata[16];
  169. char txb[32], rxb[256], ack[2];
  170. if((nRet = GfaBlmBootloaderSetBaudrate(hBlm, nNodeAddr, nInitialBaudrate)) != 0)
  171. return -1;
  172. s = GfaBlmBuildCmdDataPacket("BU", 0, &cmd, 1, cmddata, sizeof(cmddata), true);
  173. nLen = GfaMininetMasterBuildFrame(pBlm->hMst, nNodeAddr, 0, cmddata, s, txb, sizeof(txb));
  174. if((nRet = GfaMininetMasterTransmitFrame(pBlm->hMst, txb, nLen)) != nLen)
  175. return nRet;
  176. if(NODE_IS_MULTICAST(nNodeAddr))
  177. return 0;
  178. if((nLen = GfaMininetMasterReceiveFrame(pBlm->hMst, rxb, sizeof(rxb), true)) <= 0)
  179. return -1;
  180. nRet = GfaMininetMasterEvaluateSlaveResponse(pBlm->hMst, nNodeAddr, rxb, nLen, true, &nIndex);
  181. if( (nRet == MINET_SLAVE_RESPONSE_SUCCESS) ||
  182. (nRet == MINET_SLAVE_RESPONSE_ACK))
  183. {
  184. if(nRet == MINET_SLAVE_RESPONSE_SUCCESS)
  185. {
  186. if((nRet = GfaMininetMasterGetDataFromSlaveFrame(rxb, nLen, ack, 2)) != 2)
  187. return -1;
  188. }
  189. else
  190. {
  191. if((nRet = GfaBlmBUCmdPollData(hBlm, nNodeAddr, ack, 2, 200)) != 2)
  192. return -1;
  193. }
  194. if( (ack[0] == 0) &&
  195. (ack[1] == COMMAND_ACK))
  196. {
  197. #if 0
  198. do
  199. {
  200. if(nTimeoutMS > GFA_BOOTLOADER_EXEC_WAIT_TIME)
  201. {
  202. usleep(GFA_BOOTLOADER_EXEC_WAIT_TIME * 1000);
  203. nTimeoutMS -= GFA_BOOTLOADER_EXEC_WAIT_TIME;
  204. }
  205. else
  206. {
  207. usleep(nTimeoutMS * 1000);
  208. nTimeoutMS = 0;
  209. }
  210. if((nRet = GfaMininetMasterResetSlaveIndex(pBlm->hMst, nNodeAddr)) == 0)
  211. break;
  212. GfaMininetMasterPurgeDeviceRXBuffer(pBlm->hMst);
  213. }
  214. while(nTimeoutMS > 0);
  215. #endif
  216. GfaMininetMasterPurgeDeviceRXBuffer(pBlm->hMst);
  217. return GfaMininetMasterResetLocalIndex(pBlm->hMst, nNodeAddr);
  218. }
  219. }
  220. else if(nRet == MINET_SLAVE_RESPONSE_INDEX_IS_STATUS_CODE)
  221. {
  222. errno = -(int)nIndex;
  223. return -1;
  224. }
  225. return -1;
  226. }
  227. errno = EINVAL;
  228. return -1;
  229. }
  230. /////////////////////////////////////////////////////////////////////////////
  231. int GfaBlmMininetPing(HGFABLM hBlm, uint8_t nNodeAddr)
  232. {
  233. if(hBlm && !NODE_IS_MULTICAST(nNodeAddr))
  234. {
  235. LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
  236. return GfaMininetMasterPingSlave(pBlm->hMst, nNodeAddr);
  237. }
  238. errno = EINVAL;
  239. return -1;
  240. }
  241. /////////////////////////////////////////////////////////////////////////////
  242. int GfaBlmBUCmdPing(HGFABLM hBlm, uint8_t nNodeAddr)
  243. {
  244. if(hBlm && !NODE_IS_MULTICAST(nNodeAddr))
  245. {
  246. uint8_t nIndex;
  247. ssize_t nRet, nLen;
  248. uint8_t nCmd = COMMAND_PING;
  249. LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
  250. char txb[32], rxb[32], cmd[8], ack[2];
  251. size_t s = GfaBlmBuildCmdDataPacket("BU", 0, &nCmd, 1, cmd, sizeof(cmd), true);
  252. nLen = GfaMininetMasterBuildFrame(pBlm->hMst, nNodeAddr, 0, cmd, s, txb, sizeof(txb));
  253. if((nRet = GfaMininetMasterTransmitFrame(pBlm->hMst, txb, nLen)) < nLen)
  254. return nRet;
  255. if((nLen = GfaMininetMasterReceiveFrame(pBlm->hMst, rxb, sizeof(rxb), true)) <= 0)
  256. return -1;
  257. nRet = GfaMininetMasterEvaluateSlaveResponse(pBlm->hMst, nNodeAddr, rxb, nLen, true, &nIndex);
  258. if( (nRet == MINET_SLAVE_RESPONSE_SUCCESS) ||
  259. (nRet == MINET_SLAVE_RESPONSE_ACK))
  260. {
  261. if(nRet == MINET_SLAVE_RESPONSE_SUCCESS)
  262. {
  263. if((nRet = GfaMininetMasterGetDataFromSlaveFrame(rxb, nLen, ack, 2)) != 2)
  264. return -1;
  265. }
  266. else
  267. {
  268. if((nRet = GfaBlmBUCmdPollData(hBlm, nNodeAddr, ack, 2, 200)) != 2)
  269. return -1;
  270. }
  271. if(ack[0] == 0)
  272. {
  273. return (ack[1] == COMMAND_ACK) ? 0 : -1;
  274. }
  275. else
  276. {
  277. errno = EPROTO;
  278. return -1;
  279. }
  280. }
  281. else if(nRet == MINET_SLAVE_RESPONSE_INDEX_IS_STATUS_CODE)
  282. {
  283. errno = -(int)nIndex;
  284. return -1;
  285. }
  286. return -1;
  287. }
  288. errno = EINVAL;
  289. return -1;
  290. }
  291. /////////////////////////////////////////////////////////////////////////////
  292. int GfaBlmBUCmdGetStatus(HGFABLM hBlm, uint8_t nNodeAddr, uint8_t *pbStatus)
  293. {
  294. if(hBlm && pbStatus && !NODE_IS_MULTICAST(nNodeAddr))
  295. {
  296. uint8_t nIndex;
  297. ssize_t nRet, nLen;
  298. uint8_t nCmd = COMMAND_GET_STATUS;
  299. LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
  300. char txb[32], rxb[32], cmd[8], ack[3], stat[3];
  301. size_t s = GfaBlmBuildCmdDataPacket("BU", 0, &nCmd, 1, cmd, sizeof(cmd), true);
  302. nLen = GfaMininetMasterBuildFrame(pBlm->hMst, nNodeAddr, 0, cmd, s, txb, sizeof(txb));
  303. if((nRet = GfaMininetMasterTransmitFrame(pBlm->hMst, txb, nLen)) < nLen)
  304. return nRet;
  305. if((nLen = GfaMininetMasterReceiveFrame(pBlm->hMst, rxb, sizeof(rxb), true)) <= 0)
  306. return -1;
  307. nRet = GfaMininetMasterEvaluateSlaveResponse(pBlm->hMst, nNodeAddr, rxb, nLen, true, &nIndex);
  308. if( (nRet == MINET_SLAVE_RESPONSE_SUCCESS) ||
  309. (nRet == MINET_SLAVE_RESPONSE_ACK))
  310. {
  311. if(nRet == MINET_SLAVE_RESPONSE_SUCCESS)
  312. {
  313. if((nRet = GfaMininetMasterGetDataFromSlaveFrame(rxb, nLen, ack, 2)) != 2)
  314. return -1;
  315. }
  316. else
  317. {
  318. if((nRet = GfaBlmBUCmdPollData(hBlm, nNodeAddr, ack, 2, 500)) != 2)
  319. return -1;
  320. }
  321. if(ack[0] == 0)
  322. {
  323. if(ack[1] == COMMAND_ACK)
  324. {
  325. if((nRet = GfaBlmBUCmdPollData(hBlm, nNodeAddr, stat, 3, 500)) != 3)
  326. return -1;
  327. if((stat[0] == 3) && (stat[1] == stat[2]))
  328. {
  329. if(pbStatus)
  330. *pbStatus = stat[2];
  331. nLen = GfaMininetMasterBuildFrame(pBlm->hMst, nNodeAddr, 0, "BU\xCC", 3, txb, sizeof(txb));
  332. if((nRet = GfaMininetMasterTransmitFrame(pBlm->hMst, txb, nLen)) != nLen)
  333. return nRet;
  334. if((nRet = GfaMininetMasterReceiveFrame(pBlm->hMst, rxb, sizeof(rxb), true)) <= 0)
  335. return -1;
  336. nRet = GfaMininetMasterEvaluateSlaveResponse(pBlm->hMst, nNodeAddr, rxb, nRet, true, &nIndex);
  337. if(nRet == MINET_SLAVE_RESPONSE_ACK)
  338. return 0;
  339. else
  340. {
  341. // TRACE("");
  342. errno = EPROTO;
  343. return -1;
  344. }
  345. }
  346. else
  347. {
  348. errno = EPROTO;
  349. return -1;
  350. }
  351. }
  352. else
  353. {
  354. errno = EPROTO;
  355. return -1;
  356. }
  357. }
  358. else
  359. {
  360. errno = EPROTO;
  361. return -1;
  362. }
  363. }
  364. else if(nRet == MINET_SLAVE_RESPONSE_INDEX_IS_STATUS_CODE)
  365. {
  366. errno = -(int)nIndex;
  367. return -1;
  368. }
  369. return -1;
  370. }
  371. errno = EINVAL;
  372. return -1;
  373. }
  374. /////////////////////////////////////////////////////////////////////////////
  375. int GfaBlmBUCmdDownload(HGFABLM hBlm, uint8_t nNodeAddr, uint32_t nFlashStartAddr, uint32_t nCbData, uint32_t nTimeoutMS)
  376. {
  377. if(hBlm && nCbData && !NODE_IS_MULTICAST(nNodeAddr))
  378. {
  379. size_t s;
  380. ssize_t nRet, nLen;
  381. uint8_t nIndex, nStatus;
  382. LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
  383. uint32_t nAddr = bswap_32(nFlashStartAddr), nCount = bswap_32(nCbData);
  384. uint8_t txb[32], rxb[32], cmd[16], data[9], ack[2];
  385. data[0] = COMMAND_DOWNLOAD;
  386. struct timeval tv;
  387. memcpy(&data[1], &nAddr, sizeof(nAddr));
  388. memcpy(&data[5], &nCount, sizeof(nCount));
  389. tv.tv_sec = nTimeoutMS / 1000;
  390. tv.tv_usec = (nTimeoutMS % 1000) * 1000;
  391. s = GfaBlmBuildCmdDataPacket("BU", 0, data, sizeof(data), cmd, sizeof(cmd), true);
  392. nLen = GfaMininetMasterBuildFrame(pBlm->hMst, nNodeAddr, 0, cmd, s, txb, sizeof(txb));
  393. GfaMininetMasterSaveTimeouts(pBlm->hMst);
  394. GfaMininetMasterSetTimeouts(pBlm->hMst, &tv, NULL);
  395. memset(&pBlm->dlpp, 0, sizeof(GFA_BLM_DL_PROGRESS_PARAMS));
  396. pBlm->dlpp.nCbTotal = nCbData;
  397. pBlm->dlpp.nFlashStartAddr = nFlashStartAddr;
  398. pBlm->dlpp.nCntFlashPages = (nCbData + GFA_BOOTLOADER_FLASH_PAGE_SIZE - 1) / GFA_BOOTLOADER_FLASH_PAGE_SIZE;
  399. pBlm->dlpp.nNodeAddr = nNodeAddr;
  400. pBlm->dlpp.pParam = pBlm->pUserParam;
  401. pBlm->dlpp.nCtx = GBDPS_StartEraseFlash;
  402. _EXEC_PROGRESS_CALLBACK(pBlm->pfnDlProgress, &pBlm->dlpp);
  403. if((nRet = GfaMininetMasterTransmitFrame(pBlm->hMst, txb, nLen)) != nLen)
  404. {
  405. pBlm->dlpp.nCtx = GBDPS_Error;
  406. pBlm->dlpp.nErrorCode = errno;
  407. _EXEC_PROGRESS_CALLBACK(pBlm->pfnDlProgress, &pBlm->dlpp);
  408. GfaMininetMasterRestoreTimeouts(pBlm->hMst);
  409. return -1;
  410. }
  411. if((nRet = GfaMininetMasterReceiveFrame(pBlm->hMst, rxb, sizeof(rxb), true)) <= 0)
  412. {
  413. pBlm->dlpp.nCtx = GBDPS_Error;
  414. pBlm->dlpp.nErrorCode = errno;
  415. _EXEC_PROGRESS_CALLBACK(pBlm->pfnDlProgress, &pBlm->dlpp);
  416. GfaMininetMasterRestoreTimeouts(pBlm->hMst);
  417. return -1;
  418. }
  419. GfaMininetMasterRestoreTimeouts(pBlm->hMst);
  420. nRet = GfaMininetMasterEvaluateSlaveResponse(pBlm->hMst, nNodeAddr, rxb, nRet, true, &nIndex);
  421. if(nRet == MINET_SLAVE_RESPONSE_SUCCESS)
  422. {
  423. if((nRet = GfaMininetMasterGetDataFromSlaveFrame(rxb, nLen, ack, 2)) != 2)
  424. {
  425. pBlm->dlpp.nCtx = GBDPS_Error;
  426. pBlm->dlpp.nErrorCode = errno;
  427. _EXEC_PROGRESS_CALLBACK(pBlm->pfnDlProgress, &pBlm->dlpp);
  428. return -1;
  429. }
  430. if((ack[0] == 0) && (ack[1] == COMMAND_ACK))
  431. {
  432. if((nRet = GfaBlmBUCmdGetStatus(hBlm, nNodeAddr, &nStatus)))
  433. {
  434. pBlm->dlpp.nCtx = GBDPS_Error;
  435. pBlm->dlpp.nErrorCode = errno;
  436. _EXEC_PROGRESS_CALLBACK(pBlm->pfnDlProgress, &pBlm->dlpp);
  437. return -1;
  438. }
  439. if(nStatus == COMMAND_RET_SUCCESS)
  440. {
  441. pBlm->dlpp.nCtx = GBDPS_EndEraseFlash;
  442. _EXEC_PROGRESS_CALLBACK(pBlm->pfnDlProgress, &pBlm->dlpp);
  443. return 0;
  444. }
  445. }
  446. errno = EPROTO;
  447. return -1;
  448. }
  449. else if(nRet == MINET_SLAVE_RESPONSE_ACK)
  450. {
  451. if((nRet = GfaBlmBUCmdPollData(hBlm, nNodeAddr, ack, 2, nTimeoutMS)) != 2)
  452. {
  453. pBlm->dlpp.nCtx = GBDPS_Error;
  454. pBlm->dlpp.nErrorCode = errno;
  455. _EXEC_PROGRESS_CALLBACK(pBlm->pfnDlProgress, &pBlm->dlpp);
  456. return -1;
  457. }
  458. if((ack[0] == 0) && (ack[1] == COMMAND_ACK))
  459. {
  460. if((nRet = GfaBlmBUCmdGetStatus(hBlm, nNodeAddr, &nStatus)))
  461. {
  462. pBlm->dlpp.nCtx = GBDPS_Error;
  463. pBlm->dlpp.nErrorCode = errno;
  464. _EXEC_PROGRESS_CALLBACK(pBlm->pfnDlProgress, &pBlm->dlpp);
  465. return -1;
  466. }
  467. if(nStatus == COMMAND_RET_SUCCESS)
  468. {
  469. pBlm->dlpp.nCtx = GBDPS_EndEraseFlash;
  470. _EXEC_PROGRESS_CALLBACK(pBlm->pfnDlProgress, &pBlm->dlpp);
  471. return 0;
  472. }
  473. }
  474. errno = EPROTO;
  475. pBlm->dlpp.nCtx = GBDPS_Error;
  476. pBlm->dlpp.nErrorCode = errno;
  477. _EXEC_PROGRESS_CALLBACK(pBlm->pfnDlProgress, &pBlm->dlpp);
  478. return -1;
  479. }
  480. else if(nRet == MINET_SLAVE_RESPONSE_INDEX_IS_STATUS_CODE)
  481. {
  482. errno = -(int)nIndex;
  483. pBlm->dlpp.nCtx = GBDPS_Error;
  484. pBlm->dlpp.nErrorCode = errno;
  485. _EXEC_PROGRESS_CALLBACK(pBlm->pfnDlProgress, &pBlm->dlpp);
  486. return -1;
  487. }
  488. else
  489. {
  490. errno = EPROTO;
  491. pBlm->dlpp.nCtx = GBDPS_Error;
  492. pBlm->dlpp.nErrorCode = errno;
  493. _EXEC_PROGRESS_CALLBACK(pBlm->pfnDlProgress, &pBlm->dlpp);
  494. return -1;
  495. }
  496. }
  497. errno = EINVAL;
  498. return -1;
  499. }
  500. /////////////////////////////////////////////////////////////////////////////
  501. int GfaBlmBUCmdSendDataBlock(HGFABLM hBlm, uint8_t nNodeAddr, const void *pDataBlock, size_t nCbDataBlock)
  502. {
  503. if(hBlm && pDataBlock && nCbDataBlock && (nCbDataBlock <= 250) && !NODE_IS_MULTICAST(nNodeAddr))
  504. {
  505. ssize_t nRet;
  506. struct timeval tv;
  507. LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
  508. tv.tv_sec = 0;
  509. tv.tv_usec = 500000;
  510. GfaMininetMasterSaveTimeouts(pBlm->hMst);
  511. GfaMininetMasterSetTimeouts(pBlm->hMst, &tv, NULL);
  512. do
  513. {
  514. size_t s;
  515. ssize_t nLen;
  516. uint8_t nIndex, nStatus;
  517. uint8_t txb[512], rxb[32], cmd[256], data[256], ack[2];
  518. data[0] = COMMAND_SEND_DATA;
  519. memcpy(&data[1], pDataBlock, nCbDataBlock);
  520. s = GfaBlmBuildCmdDataPacket("BU", 0, data, nCbDataBlock + 1, cmd, sizeof(cmd), true);
  521. nLen = GfaMininetMasterBuildFrame(pBlm->hMst, nNodeAddr, 0, cmd, s, txb, sizeof(txb));
  522. if((nRet = GfaMininetMasterTransmitFrame(pBlm->hMst, txb, nLen)) != nLen)
  523. {
  524. nRet = -1;
  525. break;
  526. }
  527. if((nRet = GfaMininetMasterReceiveFrame(pBlm->hMst, rxb, sizeof(rxb), true)) <= 0)
  528. {
  529. nRet = -1;
  530. break;
  531. }
  532. nRet = GfaMininetMasterEvaluateSlaveResponse(pBlm->hMst, nNodeAddr, rxb, nRet, true, &nIndex);
  533. if(nRet == MINET_SLAVE_RESPONSE_SUCCESS)
  534. {
  535. if((nRet = GfaMininetMasterGetDataFromSlaveFrame(rxb, nLen, ack, 2)) != 2)
  536. {
  537. nRet = -1;
  538. break;
  539. }
  540. if((ack[0] == 0) && (ack[1] == COMMAND_ACK))
  541. {
  542. if((nRet = GfaBlmBUCmdGetStatus(hBlm, nNodeAddr, &nStatus)))
  543. {
  544. nRet = -1;
  545. break;
  546. }
  547. if(nStatus != COMMAND_RET_SUCCESS)
  548. {
  549. errno = -nStatus;
  550. nRet = -1;
  551. break;
  552. }
  553. nRet = 0;
  554. break;
  555. }
  556. errno = EPROTO;
  557. nRet = -1;
  558. break;
  559. }
  560. else if(nRet == MINET_SLAVE_RESPONSE_ACK)
  561. {
  562. if((nRet = GfaBlmBUCmdPollData(hBlm, nNodeAddr, ack, 2, 5000)) != 2)
  563. {
  564. nRet = -1;
  565. break;
  566. }
  567. if((ack[0] == 0) && (ack[1] == COMMAND_ACK))
  568. {
  569. if((nRet = GfaBlmBUCmdGetStatus(hBlm, nNodeAddr, &nStatus)))
  570. {
  571. nRet = -1;
  572. break;
  573. }
  574. if(nStatus != COMMAND_RET_SUCCESS)
  575. {
  576. errno = -nStatus;
  577. nRet = -1;
  578. break;
  579. }
  580. nRet = 0;
  581. break;
  582. }
  583. errno = EPROTO;
  584. nRet = -1;
  585. break;
  586. }
  587. else if(nRet == MINET_SLAVE_RESPONSE_INDEX_IS_STATUS_CODE)
  588. {
  589. errno = -(int)nIndex;
  590. nRet = -1;
  591. break;
  592. }
  593. }
  594. while(false);
  595. GfaMininetMasterRestoreTimeouts(pBlm->hMst);
  596. return nRet;
  597. }
  598. errno = EINVAL;
  599. return -1;
  600. }
  601. /////////////////////////////////////////////////////////////////////////////
  602. int GfaBlmBUCmdSendData(HGFABLM hBlm, uint8_t nNodeAddr, const void *pData, size_t nCbData, size_t nCbBlock)
  603. {
  604. if(hBlm && pData && nCbData && !NODE_IS_MULTICAST(nNodeAddr))
  605. {
  606. int nRet;
  607. LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
  608. const uint8_t *pbData = (const uint8_t*)pData;
  609. uint32_t nSent = 0;
  610. if(pBlm->dlpp.nNodeAddr != nNodeAddr)
  611. {
  612. errno = EINVAL;
  613. return -1;
  614. }
  615. if(!nCbBlock || nCbBlock < 4 || nCbBlock > GFA_BOOTLOADER_MAX_SEND_DATA_BLOCK_SIZE)
  616. nCbBlock = GFA_BOOTLOADER_MAX_SEND_DATA_BLOCK_SIZE;
  617. else
  618. nCbBlock &= ~0x03;
  619. pBlm->dlpp.nCtx = GBDPS_StartUploadBlocks;
  620. pBlm->dlpp.nCbBlock = nCbBlock;
  621. pBlm->dlpp.nCbSent = nSent;
  622. _EXEC_PROGRESS_CALLBACK(pBlm->pfnDlProgress, &pBlm->dlpp);
  623. while(nCbData >= nCbBlock)
  624. {
  625. if((nRet = GfaBlmBUCmdSendDataBlock(hBlm, nNodeAddr, pbData, nCbBlock)) != 0)
  626. {
  627. pBlm->dlpp.nCtx = GBDPS_Error;
  628. pBlm->dlpp.nErrorCode = errno;
  629. _EXEC_PROGRESS_CALLBACK(pBlm->pfnDlProgress, &pBlm->dlpp);
  630. return -1;
  631. }
  632. nCbData -= nCbBlock;
  633. pbData += nCbBlock;
  634. nSent += nCbBlock;
  635. pBlm->dlpp.nCtx = GBDPS_UploadBlock;
  636. pBlm->dlpp.nCbSent = nSent;
  637. _EXEC_PROGRESS_CALLBACK(pBlm->pfnDlProgress, &pBlm->dlpp);
  638. }
  639. if(nCbData)
  640. {
  641. if((nRet = GfaBlmBUCmdSendDataBlock(hBlm, nNodeAddr, pbData, nCbData)) != 0)
  642. {
  643. pBlm->dlpp.nCtx = GBDPS_Error;
  644. pBlm->dlpp.nErrorCode = errno;
  645. _EXEC_PROGRESS_CALLBACK(pBlm->pfnDlProgress, &pBlm->dlpp);
  646. return -1;
  647. }
  648. nSent += nCbData;
  649. nCbData = 0;
  650. pBlm->dlpp.nCtx = GBDPS_UploadBlock;
  651. pBlm->dlpp.nCbSent = nSent;
  652. _EXEC_PROGRESS_CALLBACK(pBlm->pfnDlProgress, &pBlm->dlpp);
  653. }
  654. pBlm->dlpp.nCtx = GBDPS_EndUploadBlocks;
  655. pBlm->dlpp.nCbSent = nSent;
  656. _EXEC_PROGRESS_CALLBACK(pBlm->pfnDlProgress, &pBlm->dlpp);
  657. return 0;
  658. }
  659. errno = EINVAL;
  660. return -1;
  661. }
  662. /////////////////////////////////////////////////////////////////////////////
  663. int GfaBlmBUCmdSendDataFile(HGFABLM hBlm, uint8_t nNodeAddr, const char *pszFilename, uint32_t nFlashStartAddr, size_t nCbBlock, uint32_t nTimeoutMS)
  664. {
  665. if(hBlm && pszFilename && !NODE_IS_MULTICAST(nNodeAddr))
  666. {
  667. int nRet = -1;
  668. ssize_t nFileLen;
  669. FILE *pf = NULL;
  670. uint8_t *pBuf = NULL;
  671. do
  672. {
  673. if(!(pf = fopen(pszFilename, "rb")))
  674. break;
  675. if(fseek(pf, 0, SEEK_END))
  676. break;
  677. if((nFileLen = ftell(pf)) < 0)
  678. break;
  679. if(fseek(pf, 0, SEEK_SET))
  680. break;
  681. if( (nFileLen > 0) &&
  682. (pBuf = (uint8_t*)malloc(nFileLen)) &&
  683. (fread(pBuf, 1, nFileLen, pf) == (size_t)nFileLen))
  684. {
  685. if((nRet = GfaBlmBUCmdDownload(hBlm, nNodeAddr, nFlashStartAddr, nFileLen, nTimeoutMS)) == 0)
  686. nRet = GfaBlmBUCmdSendData(hBlm, nNodeAddr, pBuf, nFileLen, nCbBlock);
  687. }
  688. }
  689. while(false);
  690. if(pBuf)
  691. free(pBuf);
  692. if(pf)
  693. fclose(pf);
  694. return nRet;
  695. }
  696. errno = EINVAL;
  697. return -1;
  698. }
  699. /////////////////////////////////////////////////////////////////////////////
  700. GFA_BLM_EXEC_CONTEXT GfaBlmGetExecutionContext(HGFABLM hBlm, uint8_t nNodeAddr)
  701. {
  702. if(hBlm && !NODE_IS_MULTICAST(nNodeAddr))
  703. {
  704. uint32_t nLoops = 0;
  705. uint8_t nIndex;
  706. ssize_t nRet, nLen;
  707. LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
  708. char txb[32], rxb[256];
  709. do
  710. {
  711. nLen = GfaMininetMasterBuildFrame(pBlm->hMst, nNodeAddr, 0, "BU", 2, txb, sizeof(txb));
  712. if((nRet = GfaMininetMasterTransmitFrame(pBlm->hMst, txb, nLen)) != nLen)
  713. return GfaBlmCtx_Err;
  714. if((nLen = GfaMininetMasterReceiveFrame(pBlm->hMst, rxb, sizeof(rxb), true)) <= 0)
  715. return GfaBlmCtx_Err;
  716. nRet = GfaMininetMasterEvaluateSlaveResponse(pBlm->hMst, nNodeAddr, rxb, nLen, true, &nIndex);
  717. }
  718. while((nRet == MINET_SLAVE_RESPONSE_SUCCESS) && (nLoops++ < 10));
  719. if(nRet == MINET_SLAVE_RESPONSE_SUCCESS)
  720. return GfaBlmCtx_Err;
  721. if(nRet == MINET_SLAVE_RESPONSE_ACK)
  722. return GfaBlmCtx_Boot;
  723. else if(nRet == MINET_SLAVE_RESPONSE_INDEX_IS_STATUS_CODE)
  724. return GfaBlmCtx_App;
  725. return GfaBlmCtx_Err;
  726. }
  727. errno = EINVAL;
  728. return GfaBlmCtx_Err;
  729. }
  730. /////////////////////////////////////////////////////////////////////////////
  731. int GfaBlmBootloaderExecute(HGFABLM hBlm, uint8_t nNodeAddr, uint32_t *pnImgCRC32, uint32_t nTimeoutMS)
  732. {
  733. if(hBlm && !NODE_IS_MULTICAST(nNodeAddr))
  734. {
  735. uint8_t nIndex;
  736. uint32_t nImgCRC32;
  737. ssize_t nRet, nLen;
  738. LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
  739. char txb[32], rxb[32];
  740. nLen = GfaMininetMasterBuildFrame(pBlm->hMst, nNodeAddr, 0, "BE", 2, txb, sizeof(txb));
  741. if((nRet = GfaMininetMasterTransmitFrame(pBlm->hMst, txb, nLen)) != nLen)
  742. return -1;
  743. if((nLen = GfaMininetMasterReceiveFrame(pBlm->hMst, rxb, sizeof(rxb), true)) <= 0)
  744. return -1;
  745. nRet = GfaMininetMasterEvaluateSlaveResponse(pBlm->hMst, nNodeAddr, rxb, nLen, true, &nIndex);
  746. if(nRet == MINET_SLAVE_RESPONSE_SUCCESS)
  747. {
  748. if((nRet = GfaMininetMasterGetDataFromSlaveFrame(rxb, nLen, &nImgCRC32, sizeof(nImgCRC32))) == 4)
  749. {
  750. if(pnImgCRC32)
  751. *pnImgCRC32 = bswap_32(nImgCRC32);
  752. GfaBlmSetDeviceBaudrate(hBlm, _BOOTLOADER_DEF_BAUDRATE);
  753. do
  754. {
  755. if(nTimeoutMS > GFA_BOOTLOADER_EXEC_WAIT_TIME)
  756. {
  757. usleep(GFA_BOOTLOADER_EXEC_WAIT_TIME * 1000);
  758. nTimeoutMS -= GFA_BOOTLOADER_EXEC_WAIT_TIME;
  759. }
  760. else
  761. {
  762. usleep(nTimeoutMS * 1000);
  763. nTimeoutMS = 0;
  764. }
  765. if((nRet = GfaMininetMasterResetSlaveIndex(pBlm->hMst, nNodeAddr)) == 0)
  766. break;
  767. GfaMininetMasterPurgeDeviceRXBuffer(pBlm->hMst);
  768. }
  769. while(nTimeoutMS > 0);
  770. return nRet;
  771. }
  772. }
  773. else if(nRet == MINET_SLAVE_RESPONSE_ACK)
  774. {
  775. return 0;
  776. }
  777. else if(nRet == MINET_SLAVE_RESPONSE_INDEX_IS_STATUS_CODE)
  778. {
  779. errno = -(int)nIndex;
  780. return -1;
  781. }
  782. return -1;
  783. }
  784. errno = EINVAL;
  785. return -1;
  786. }
  787. /////////////////////////////////////////////////////////////////////////////
  788. int GfaBlmGetDeviceBaudrate(HGFABLM hBlm, uint32_t *pnBaudrate)
  789. {
  790. if(hBlm)
  791. {
  792. LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
  793. return GfaMininetMasterGetBaudrate(pBlm->hMst, pnBaudrate);
  794. }
  795. errno = EINVAL;
  796. return -1;
  797. }
  798. /////////////////////////////////////////////////////////////////////////////
  799. int GfaBlmSetDeviceBaudrate(HGFABLM hBlm, uint32_t nBaudrate)
  800. {
  801. if(hBlm)
  802. {
  803. LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
  804. return GfaMininetMasterSetBaudrate(pBlm->hMst, nBaudrate);
  805. }
  806. errno = EINVAL;
  807. return -1;
  808. }
  809. /////////////////////////////////////////////////////////////////////////////
  810. int GfaBlmBootloaderSetBaudrate(HGFABLM hBlm, uint8_t nNodeAddr, uint32_t nBaudrate)
  811. {
  812. if(hBlm)
  813. {
  814. uint8_t nIndex;
  815. size_t s;
  816. ssize_t nRet, nLen;
  817. LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
  818. if(GfaMininetMasterIsValidBaudrate(pBlm->hMst, nBaudrate))
  819. {
  820. char txb[32], rxb[32], cmd[8];
  821. uint32_t nBr = bswap_32(nBaudrate);
  822. s = GfaBlmBuildCmdDataPacket("BB", 0, &nBr, sizeof(nBr), cmd, sizeof(cmd), false);
  823. nLen = GfaMininetMasterBuildFrame(pBlm->hMst, nNodeAddr, 0, cmd, s, txb, sizeof(txb));
  824. if((nRet = GfaMininetMasterTransmitFrame(pBlm->hMst, txb, nLen)) != nLen)
  825. return nRet;
  826. if(NODE_IS_MULTICAST(nNodeAddr))
  827. return 0;
  828. if((nLen = GfaMininetMasterReceiveFrame(pBlm->hMst, rxb, sizeof(rxb), true)) <= 0)
  829. return -1;
  830. nRet = GfaMininetMasterEvaluateSlaveResponse(pBlm->hMst, nNodeAddr, rxb, nLen, true, &nIndex);
  831. if(nRet == MINET_SLAVE_RESPONSE_ACK)
  832. {
  833. return GfaMininetMasterSetBaudrate(pBlm->hMst, nBaudrate);
  834. }
  835. else if(nRet == MINET_SLAVE_RESPONSE_INDEX_IS_STATUS_CODE)
  836. {
  837. errno = -(int)nIndex;
  838. return -1;
  839. }
  840. return -1;
  841. }
  842. }
  843. errno = EINVAL;
  844. return -1;
  845. }
  846. /////////////////////////////////////////////////////////////////////////////
  847. int GfaBlmBootloaderDump(HGFABLM hBlm, uint8_t nNodeAddr, uint32_t nAddress, uint32_t nCntDwords, void *pBuffer, size_t nCbBuffer)
  848. {
  849. if(hBlm && pBuffer && nCntDwords && !NODE_IS_MULTICAST(nNodeAddr))
  850. {
  851. size_t s;
  852. uint8_t nIndex;
  853. ssize_t nRet, nLen;
  854. LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
  855. uint32_t i, dmp[_GFA_MAX_DUMP_DWORDS];
  856. char txb[32], rxb[128], cmd[10];
  857. if(nCntDwords > _GFA_MAX_DUMP_DWORDS)
  858. nCntDwords = _GFA_MAX_DUMP_DWORDS;
  859. if((nCntDwords * sizeof(uint32_t)) > nCbBuffer)
  860. {
  861. errno = ENOMEM;
  862. return -1;
  863. }
  864. struct _MEM
  865. {
  866. uint32_t nAddr;
  867. uint32_t nCount;
  868. }mem = {bswap_32(nAddress), bswap_32(nCntDwords)};
  869. s = GfaBlmBuildCmdDataPacket("BD", 0, &mem, sizeof(mem), cmd, sizeof(cmd), false);
  870. nLen = GfaMininetMasterBuildFrame(pBlm->hMst, nNodeAddr, 0, cmd, s, txb, sizeof(txb));
  871. if((nRet = GfaMininetMasterTransmitFrame(pBlm->hMst, txb, nLen)) != nLen)
  872. return nRet;
  873. if((nLen = GfaMininetMasterReceiveFrame(pBlm->hMst, rxb, sizeof(rxb), true)) <= 0)
  874. return -1;
  875. nRet = GfaMininetMasterEvaluateSlaveResponse(pBlm->hMst, nNodeAddr, rxb, nLen, true, &nIndex);
  876. if(nRet == MINET_SLAVE_RESPONSE_SUCCESS)
  877. {
  878. if((nRet = GfaMininetMasterGetDataFromSlaveFrame(rxb, nLen, dmp, sizeof(uint32_t) * nCntDwords)) != (ssize_t)(sizeof(uint32_t) * nCntDwords))
  879. return -1;
  880. for(i = 0; i < nCntDwords; ++i)
  881. {
  882. dmp[i] = bswap_32(dmp[i]);
  883. }
  884. memcpy(pBuffer, dmp, sizeof(uint32_t) * nCntDwords);
  885. return 0;
  886. }
  887. else if(nRet == MINET_SLAVE_RESPONSE_ACK)
  888. {
  889. if((nRet = GfaBlmBUCmdPollData(hBlm, nNodeAddr, dmp, sizeof(uint32_t) * nCntDwords, 200)) != (ssize_t)(sizeof(uint32_t) * nCntDwords))
  890. return -1;
  891. for(i = 0; i < nCntDwords; ++i)
  892. {
  893. dmp[i] = bswap_32(dmp[i]);
  894. }
  895. memcpy(pBuffer, dmp, sizeof(uint32_t) * nCntDwords);
  896. return 0;
  897. }
  898. else if(nRet == MINET_SLAVE_RESPONSE_INDEX_IS_STATUS_CODE)
  899. {
  900. errno = -(int)nIndex;
  901. return -1;
  902. }
  903. return -1;
  904. }
  905. errno = EINVAL;
  906. return -1;
  907. }
  908. int GfaBlmGetImgInfo(HGFABLM hBlm, uint8_t nNodeAddr, uint32_t nDumpAddr, bool bCtxIsApp, LPGFA_IMG_INFO pii)
  909. {
  910. if(hBlm && pii && !NODE_IS_MULTICAST(nNodeAddr))
  911. {
  912. ssize_t nRet = 0;
  913. uint32_t i, j, nPfx0Addr = 0;
  914. int32_t nPfx0Index = -1, nPfx1Index = -1;
  915. uint32_t aDump[_GFA_MAX_DUMP_DWORDS];
  916. GFA_APP_IMG_HEADER aih;
  917. memset(&aih, 0, sizeof(aih));
  918. memset(pii, 0, sizeof(GFA_IMG_INFO));
  919. for(i = 0; i < 257; i += _GFA_MAX_DUMP_DWORDS)
  920. {
  921. if((nRet = GfaBlmBootloaderDump(hBlm, nNodeAddr, nDumpAddr, _GFA_MAX_DUMP_DWORDS, aDump, sizeof(aDump))) == 0)
  922. {
  923. for(j = 0; j < _GFA_MAX_DUMP_DWORDS; ++j, nDumpAddr += sizeof(uint32_t))
  924. {
  925. switch(aDump[j])
  926. {
  927. case GFA_APP_IMG_HEADER_PREFIX_0:
  928. nPfx0Addr = nDumpAddr;
  929. nPfx0Index = i + j;
  930. continue;
  931. case GFA_APP_IMG_HEADER_PREFIX_1:
  932. nPfx1Index = i + j;
  933. break;
  934. default:
  935. break;
  936. }
  937. if((nPfx0Index >= 0) && (nPfx1Index == (nPfx0Index + 1)))
  938. {
  939. uint32_t nDmpStart = nPfx0Addr + 2 * sizeof(uint32_t), *pDwDst = ((uint32_t*)&aih) + 2;
  940. uint32_t nCntDwAvail = 15 - j;
  941. if(bCtxIsApp)
  942. {
  943. uint32_t nDwOffsImgMaterialNum = _dword_offset(app.pszImgMaterialNum);
  944. uint32_t nDwOffsImgNameBuild = _dword_offset(app.pszImgNameBuild);
  945. uint32_t nDwOffsImgCRC32 = _dword_offset(nImgCRC32);
  946. size_t nSizeDst = (nDwOffsImgNameBuild + 1) * sizeof(uint32_t);
  947. uint32_t nCntDwToDmp, nCntDwToCpy;
  948. if(nCntDwAvail > nDwOffsImgNameBuild)
  949. {
  950. // have nImgLength, nImgCRC32, pszImgMaterialNum, pszImgNameBuild
  951. // need nothing
  952. nCntDwToCpy = nDwOffsImgNameBuild + 1;
  953. nCntDwToDmp = 0;
  954. memcpy(pDwDst, &aDump[j + 1], nCntDwToCpy * sizeof(uint32_t));
  955. }
  956. else if(nCntDwAvail == nDwOffsImgNameBuild)
  957. {
  958. // have nImgLength, nImgCRC32, pszImgMaterialNum
  959. // need pszImgNameBuild
  960. nCntDwToCpy = nDwOffsImgNameBuild;
  961. nCntDwToDmp = 1;
  962. nDmpStart += nCntDwToCpy * sizeof(uint32_t);
  963. memcpy(pDwDst, &aDump[j + 1], nCntDwToCpy * sizeof(uint32_t));
  964. pDwDst += nCntDwToCpy;
  965. nSizeDst -= nCntDwToCpy * sizeof(uint32_t);
  966. }
  967. else if(nCntDwAvail > nDwOffsImgCRC32)
  968. {
  969. // have nImgLength, nImgCRC32
  970. // need pszImgMaterialNum, pszImgNameBuild
  971. nCntDwToCpy = 2;
  972. nCntDwToDmp = 2;
  973. nDmpStart += nDwOffsImgMaterialNum * sizeof(uint32_t);
  974. memcpy(pDwDst, &aDump[j + 1], nCntDwToCpy * sizeof(uint32_t));
  975. pDwDst += nDwOffsImgMaterialNum;
  976. nSizeDst -= nDwOffsImgMaterialNum * sizeof(uint32_t);
  977. }
  978. else if(nCntDwAvail == nDwOffsImgCRC32)
  979. {
  980. // have nImgLength
  981. // need nImgCRC32, pszImgMaterialNum, pszImgNameBuild
  982. nCntDwToCpy = nDwOffsImgCRC32;
  983. nCntDwToDmp = 7; // 1 + 4 + 2
  984. nDmpStart += sizeof(uint32_t);
  985. memcpy(pDwDst, &aDump[j + 1], nCntDwToCpy * sizeof(uint32_t));
  986. pDwDst += nCntDwToCpy;
  987. nSizeDst -= nCntDwToCpy * sizeof(uint32_t);
  988. }
  989. else
  990. {
  991. // have nothing
  992. // need nImgLength, nImgCRC32, pszImgMaterialNum, pszImgNameBuild
  993. nCntDwToCpy = 0;
  994. nCntDwToDmp = nDwOffsImgNameBuild + 1; // 2 + 4 + 2
  995. }
  996. if(nCntDwToDmp)
  997. {
  998. if((nRet = GfaBlmBootloaderDump(hBlm, nNodeAddr, nDmpStart, nCntDwToDmp, pDwDst, nSizeDst)) != 0)
  999. return -1;
  1000. }
  1001. pii->nImgLength = aih.nImgLength;
  1002. pii->nImgCRC32 = aih.nImgCRC32;
  1003. if((nRet = GfaBlmBootloaderDump(hBlm, nNodeAddr, (uint32_t)aih.app.pszImgMaterialNum, GFA_APP_MAX_IMG_MATERIAL_NUM_LENGTH / sizeof(uint32_t), pii->szImgMaterialNum, GFA_APP_MAX_IMG_MATERIAL_NUM_LENGTH)) != 0)
  1004. return -1;
  1005. if((nRet = GfaBlmBootloaderDump(hBlm, nNodeAddr, (uint32_t)aih.app.pszImgNameBuild, GFA_APP_MAX_IMG_NAME_BUILD_LENGTH / sizeof(uint32_t), pii->szImgNameBuild, GFA_APP_MAX_IMG_NAME_BUILD_LENGTH)) != 0)
  1006. return -1;
  1007. pii->szImgMaterialNum[GFA_APP_MAX_IMG_MATERIAL_NUM_LENGTH - 1] = '\0';
  1008. pii->szImgNameBuild[GFA_APP_MAX_IMG_NAME_BUILD_LENGTH - 1] = '\0';
  1009. return 0;
  1010. }
  1011. else
  1012. {
  1013. uint32_t nDwOffsImgMaterialNum = _dword_offset(bl.szImgMaterialNum);
  1014. uint32_t nDwOffsImgNameBuild = _dword_offset(bl.szImgNameBuild);
  1015. uint32_t nDwOffsImgCRC32 = _dword_offset(nImgCRC32);
  1016. size_t nSizeDst = nDwOffsImgNameBuild * sizeof(uint32_t) + GFA_APP_MAX_IMG_NAME_BUILD_LENGTH;
  1017. uint32_t nCntDwToDmp, nCntDwToCpy;
  1018. if(nCntDwAvail >= (nDwOffsImgNameBuild + GFA_APP_MAX_IMG_NAME_BUILD_LENGTH / sizeof(uint32_t)))
  1019. {
  1020. // have nImgLength, nImgCRC32, szImgMaterialNum, szImgNameBuild
  1021. // need nothing
  1022. nCntDwToCpy = nDwOffsImgNameBuild + GFA_APP_MAX_IMG_NAME_BUILD_LENGTH / sizeof(uint32_t);
  1023. nCntDwToDmp = 0;
  1024. memcpy(pDwDst, &aDump[j + 1], nCntDwToCpy * sizeof(uint32_t));
  1025. }
  1026. else if(nCntDwAvail > nDwOffsImgMaterialNum)
  1027. {
  1028. // have nImgLength, nImgCRC32, szImgMaterialNum
  1029. // need szImgNameBuild
  1030. nCntDwToCpy = nCntDwAvail;
  1031. nCntDwToDmp = nSizeDst / sizeof(uint32_t) - nCntDwToCpy; // (1...3) + 6
  1032. nDmpStart += nCntDwToCpy * sizeof(uint32_t);
  1033. memcpy(pDwDst, &aDump[j + 1], nCntDwToCpy * sizeof(uint32_t));
  1034. pDwDst += nCntDwToCpy;
  1035. nSizeDst -= nCntDwToCpy * sizeof(uint32_t);
  1036. }
  1037. else if(nCntDwAvail > nDwOffsImgCRC32)
  1038. {
  1039. // have nImgLength, nImgCRC32
  1040. // need szImgMaterialNum, szImgNameBuild
  1041. nCntDwToCpy = 2;
  1042. nCntDwToDmp = (GFA_APP_MAX_IMG_MATERIAL_NUM_LENGTH + GFA_APP_MAX_IMG_NAME_BUILD_LENGTH) / sizeof(uint32_t); // 4 + 6
  1043. nDmpStart += nDwOffsImgMaterialNum * sizeof(uint32_t);
  1044. memcpy(pDwDst, &aDump[j + 1], nCntDwToCpy * sizeof(uint32_t));
  1045. pDwDst += nDwOffsImgMaterialNum;
  1046. nSizeDst -= nDwOffsImgMaterialNum * sizeof(uint32_t);
  1047. }
  1048. else if(nCntDwAvail == nDwOffsImgCRC32)
  1049. {
  1050. // have nImgLength
  1051. // need nImgCRC32, szImgMaterialNum, szImgNameBuild
  1052. nCntDwToCpy = nDwOffsImgCRC32;
  1053. nCntDwToDmp = nSizeDst / sizeof(uint32_t) - nCntDwToCpy; // 1 + 4 + 4 + 6
  1054. nDmpStart += sizeof(uint32_t);
  1055. memcpy(pDwDst, &aDump[j + 1], nCntDwToCpy * sizeof(uint32_t));
  1056. pDwDst += nCntDwToCpy;
  1057. nSizeDst -= nCntDwToCpy * sizeof(uint32_t);
  1058. }
  1059. else
  1060. {
  1061. // have nothing
  1062. // need nImgLength, nImgCRC32, pszImgMaterialNum, pszImgNameBuild
  1063. nCntDwToCpy = 0;
  1064. nCntDwToDmp = nSizeDst / sizeof(uint32_t); // 2 + 4 + 4 + 6
  1065. }
  1066. if(nCntDwToDmp)
  1067. {
  1068. if((nRet = GfaBlmBootloaderDump(hBlm, nNodeAddr, nDmpStart, nCntDwToDmp, pDwDst, nSizeDst)) != 0)
  1069. return -1;
  1070. }
  1071. pii->nImgLength = aih.nImgLength;
  1072. pii->nImgCRC32 = aih.nImgCRC32;
  1073. memcpy(pii->szImgMaterialNum, aih.bl.szImgMaterialNum, GFA_APP_MAX_IMG_MATERIAL_NUM_LENGTH);
  1074. memcpy(pii->szImgNameBuild, aih.bl.szImgNameBuild, GFA_APP_MAX_IMG_NAME_BUILD_LENGTH);
  1075. pii->szImgMaterialNum[GFA_APP_MAX_IMG_MATERIAL_NUM_LENGTH - 1] = '\0';
  1076. pii->szImgNameBuild[GFA_APP_MAX_IMG_NAME_BUILD_LENGTH - 1] = '\0';
  1077. return 0;
  1078. }
  1079. }
  1080. }
  1081. }
  1082. else
  1083. {
  1084. break;
  1085. }
  1086. }
  1087. return -1;
  1088. }
  1089. errno = EINVAL;
  1090. return -1;
  1091. }
  1092. /////////////////////////////////////////////////////////////////////////////
  1093. int GfaBlmGetInfoBD(HGFABLM hBlm, uint8_t nNodeAddr, uint32_t nAppBaseAddr, LPGFA_BL_APP_IMG_INFO paii)
  1094. {
  1095. if(hBlm && paii && !NODE_IS_MULTICAST(nNodeAddr))
  1096. {
  1097. int nRet1, nRet2;
  1098. if((nRet1 = GfaBlmGetImgInfo(hBlm, nNodeAddr, GFA_APP_BOOTLOADER_START_ADDRESS, false, &paii->bl)) < 0)
  1099. {
  1100. memset(&paii->bl, 0, sizeof(paii->bl));
  1101. paii->bl.nImgLength = paii->bl.nImgCRC32 = 0xFFFFFFFF;
  1102. }
  1103. if((nRet2 = GfaBlmGetImgInfo(hBlm, nNodeAddr, nAppBaseAddr, true, &paii->app)) < 0)
  1104. {
  1105. memset(&paii->app, 0, sizeof(paii->app));
  1106. paii->app.nImgLength = paii->app.nImgCRC32 = 0xFFFFFFFF;
  1107. }
  1108. return nRet1 + nRet2;
  1109. }
  1110. errno = EINVAL;
  1111. return -1;
  1112. }
  1113. /////////////////////////////////////////////////////////////////////////////
  1114. int GfaBlmGetInfoBI(HGFABLM hBlm, uint8_t nNodeAddr, LPGFA_BL_APP_IMG_INFO paii)
  1115. {
  1116. if(hBlm && paii && !NODE_IS_MULTICAST(nNodeAddr))
  1117. {
  1118. uint8_t nIndex;
  1119. ssize_t nRet, nLen;
  1120. char txb[32], rxb[256];
  1121. LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
  1122. nLen = GfaMininetMasterBuildFrame(pBlm->hMst, nNodeAddr, 0, "BI", 2, txb, sizeof(txb));
  1123. if((nRet = GfaMininetMasterTransmitFrame(pBlm->hMst, txb, nLen)) != nLen)
  1124. return nRet;
  1125. if((nLen = GfaMininetMasterReceiveFrame(pBlm->hMst, rxb, sizeof(rxb), true)) <= 0)
  1126. return -1;
  1127. nRet = GfaMininetMasterEvaluateSlaveResponse(pBlm->hMst, nNodeAddr, rxb, nLen, true, &nIndex);
  1128. if(nRet == MINET_SLAVE_RESPONSE_SUCCESS)
  1129. {
  1130. if((nRet = GfaMininetMasterGetDataFromSlaveFrame(rxb, nLen, paii, sizeof(GFA_BL_APP_IMG_INFO))) == sizeof(GFA_BL_APP_IMG_INFO))
  1131. {
  1132. paii->bl.nImgLength = bswap_32(paii->bl.nImgLength);
  1133. paii->bl.nImgCRC32 = bswap_32(paii->bl.nImgCRC32);
  1134. paii->app.nImgLength = bswap_32(paii->app.nImgLength);
  1135. paii->app.nImgCRC32 = bswap_32(paii->app.nImgCRC32);
  1136. return 0;
  1137. }
  1138. errno = EPROTO;
  1139. return -1;
  1140. }
  1141. else if(nRet == MINET_SLAVE_RESPONSE_ACK)
  1142. {
  1143. errno = EPROTO;
  1144. return -1;
  1145. }
  1146. else if(nRet == MINET_SLAVE_RESPONSE_INDEX_IS_STATUS_CODE)
  1147. {
  1148. errno = -(int)nIndex;
  1149. return -1;
  1150. }
  1151. return -1;
  1152. }
  1153. errno = EINVAL;
  1154. return -1;
  1155. }
  1156. /////////////////////////////////////////////////////////////////////////////
  1157. int GfaBlmReadMaterialAndSerialID(HGFABLM hBlm, uint8_t nNodeAddr, char *pszMaterial, size_t nCbMaterial, char *pszSerial, size_t nCbSerial)
  1158. {
  1159. if(hBlm && pszMaterial && (nCbMaterial >= GFA_APP_MAX_IMG_MATERIAL_NUM_LENGTH) && pszSerial && (nCbSerial >= GFA_APP_MAX_IMG_SERIAL_NUM_LENGTH) && !NODE_IS_MULTICAST(nNodeAddr))
  1160. {
  1161. uint8_t nIndex;
  1162. ssize_t nRet, nLen;
  1163. char txb[32], rxb[64], data[32];
  1164. LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
  1165. nLen = GfaMininetMasterBuildFrame(pBlm->hMst, nNodeAddr, 0, "BR", 2, txb, sizeof(txb));
  1166. if((nRet = GfaMininetMasterTransmitFrame(pBlm->hMst, txb, nLen)) != nLen)
  1167. return nRet;
  1168. if((nLen = GfaMininetMasterReceiveFrame(pBlm->hMst, rxb, sizeof(rxb), true)) <= 0)
  1169. return -1;
  1170. nRet = GfaMininetMasterEvaluateSlaveResponse(pBlm->hMst, nNodeAddr, rxb, nLen, true, &nIndex);
  1171. if(nRet == MINET_SLAVE_RESPONSE_SUCCESS)
  1172. {
  1173. if((nRet = GfaMininetMasterGetDataFromSlaveFrame(rxb, nLen, data, sizeof(data))) != (GFA_APP_MAX_IMG_MATERIAL_NUM_LENGTH + GFA_APP_MAX_IMG_SERIAL_NUM_LENGTH))
  1174. return -1;
  1175. memcpy(pszMaterial, data, GFA_APP_MAX_IMG_MATERIAL_NUM_LENGTH);
  1176. pszMaterial[GFA_APP_MAX_IMG_MATERIAL_NUM_LENGTH - 1] = '\0';
  1177. memcpy(pszSerial, &data[GFA_APP_MAX_IMG_MATERIAL_NUM_LENGTH], GFA_APP_MAX_IMG_SERIAL_NUM_LENGTH);
  1178. pszSerial[GFA_APP_MAX_IMG_SERIAL_NUM_LENGTH - 1] = '\0';
  1179. return 0;
  1180. }
  1181. else if(nRet == MINET_SLAVE_RESPONSE_ACK)
  1182. {
  1183. if((nRet = GfaBlmBUCmdPollData(hBlm, nNodeAddr, data, sizeof(data), 200)) != (ssize_t)sizeof(data))
  1184. return -1;
  1185. memcpy(pszMaterial, data, GFA_APP_MAX_IMG_MATERIAL_NUM_LENGTH);
  1186. pszMaterial[GFA_APP_MAX_IMG_MATERIAL_NUM_LENGTH - 1] = '\0';
  1187. memcpy(pszSerial, &data[GFA_APP_MAX_IMG_MATERIAL_NUM_LENGTH], GFA_APP_MAX_IMG_SERIAL_NUM_LENGTH);
  1188. pszSerial[GFA_APP_MAX_IMG_SERIAL_NUM_LENGTH - 1] = '\0';
  1189. return 0;
  1190. }
  1191. else if(nRet == MINET_SLAVE_RESPONSE_INDEX_IS_STATUS_CODE)
  1192. {
  1193. errno = -(int)nIndex;
  1194. return -1;
  1195. }
  1196. return -1;
  1197. }
  1198. errno = EINVAL;
  1199. return -1;
  1200. }
  1201. /////////////////////////////////////////////////////////////////////////////
  1202. int GfaBlmWriteMaterialAndSerialID(HGFABLM hBlm, uint8_t nNodeAddr, const char *pszMaterial, const char *pszSerial)
  1203. {
  1204. if(hBlm && pszMaterial && pszSerial && !NODE_IS_MULTICAST(nNodeAddr))
  1205. {
  1206. uint8_t nIndex;
  1207. size_t s;
  1208. ssize_t nRet, nLen;
  1209. size_t nLenMaterial, nLenSerial;
  1210. LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
  1211. struct _TS
  1212. {
  1213. char szMaterial[16];
  1214. char szSerial[16];
  1215. }ts;
  1216. char txb[64], rxb[32], cmd[64];
  1217. nLenMaterial = strlen(pszMaterial);
  1218. nLenSerial = strlen(pszSerial);
  1219. if((nLenMaterial > 15) || (nLenSerial > 15))
  1220. {
  1221. errno = EINVAL;
  1222. return -1;
  1223. }
  1224. memcpy(ts.szMaterial, pszMaterial, nLenMaterial);
  1225. if(nLenMaterial < 15)
  1226. memset(&ts.szMaterial[nLenMaterial], ' ', 15 - nLenMaterial);
  1227. ts.szMaterial[15] = '\0';
  1228. memcpy(ts.szSerial, pszSerial, nLenSerial);
  1229. if(nLenSerial < 15)
  1230. memset(&ts.szSerial[nLenSerial], ' ', 15 - nLenSerial);
  1231. ts.szSerial[15] = '\0';
  1232. s = GfaBlmBuildCmdDataPacket("BW", 0, &ts, sizeof(ts), cmd, sizeof(cmd), false);
  1233. nLen = GfaMininetMasterBuildFrame(pBlm->hMst, nNodeAddr, 0, cmd, s, txb, sizeof(txb));
  1234. if((nRet = GfaMininetMasterTransmitFrame(pBlm->hMst, txb, nLen)) != nLen)
  1235. return nRet;
  1236. if((nRet = GfaMininetMasterReceiveFrame(pBlm->hMst, rxb, sizeof(rxb), true)) <= 0)
  1237. return -1;
  1238. nRet = GfaMininetMasterEvaluateSlaveResponse(pBlm->hMst, nNodeAddr, rxb, nRet, true, &nIndex);
  1239. if(nRet == MINET_SLAVE_RESPONSE_SUCCESS)
  1240. {
  1241. return 0;
  1242. }
  1243. else if(nRet == MINET_SLAVE_RESPONSE_ACK)
  1244. {
  1245. return 0;
  1246. }
  1247. else if(nRet == MINET_SLAVE_RESPONSE_INDEX_IS_STATUS_CODE)
  1248. {
  1249. errno = -(int)nIndex;
  1250. return -1;
  1251. }
  1252. return -1;
  1253. }
  1254. errno = EINVAL;
  1255. return -1;
  1256. }
  1257. /////////////////////////////////////////////////////////////////////////////
  1258. HGFAMINEMST GfaBlmGetMininetMasterHandle(HGFABLM hBlm)
  1259. {
  1260. if(hBlm)
  1261. {
  1262. LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
  1263. return pBlm->hMst;
  1264. }
  1265. errno = EINVAL;
  1266. return NULL;
  1267. }
  1268. /////////////////////////////////////////////////////////////////////////////
  1269. int GfaBlmSetVerbosity(HGFABLM hBlm, int nVerbosity)
  1270. {
  1271. if(hBlm)
  1272. {
  1273. LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
  1274. if(nVerbosity < 0)
  1275. nVerbosity = 0;
  1276. else if(nVerbosity > 4)
  1277. nVerbosity = 4;
  1278. pBlm->nVerbosity = nVerbosity;
  1279. return GfaMininetMasterSetVerbosity(pBlm->hMst, pBlm->nVerbosity);
  1280. }
  1281. errno = EINVAL;
  1282. return -1;
  1283. }
  1284. /////////////////////////////////////////////////////////////////////////////
  1285. uint8_t GfaBlmDataCheckSum(const void *pData, size_t nCbData)
  1286. {
  1287. uint8_t chk = 0;
  1288. const uint8_t *pbData = (const uint8_t*)pData;
  1289. while(nCbData--) {
  1290. chk += *pbData++;
  1291. }
  1292. return chk;
  1293. }
  1294. /////////////////////////////////////////////////////////////////////////////
  1295. size_t GfaBlmBuildCmdDataPacket(const char *pszCmd, uint8_t tiCmd, const void *pCmdData, size_t nCbCmdData, void *pPacket, size_t nCbPacket, bool bAddLenAndCheck)
  1296. {
  1297. size_t nLen = strlen(pszCmd);
  1298. uint8_t *pbPacket = (uint8_t*)pPacket;
  1299. tiCmd = tiCmd; // not yet used
  1300. if(nCbPacket < (nLen + nCbCmdData + ((pCmdData && nCbCmdData && bAddLenAndCheck) ? 2 : 0)))
  1301. return 0;
  1302. memcpy(pbPacket, pszCmd, nLen);
  1303. pbPacket += nLen;
  1304. if(pCmdData && nCbCmdData)
  1305. {
  1306. if(bAddLenAndCheck)
  1307. {
  1308. *pbPacket++ = nCbCmdData + 2;
  1309. *pbPacket++ = GfaBlmDataCheckSum(pCmdData, nCbCmdData);
  1310. nLen += 2;
  1311. }
  1312. memcpy(pbPacket, pCmdData, nCbCmdData);
  1313. nLen += nCbCmdData;
  1314. }
  1315. return nLen;
  1316. }
  1317. const char* GfaBlmStrError(int nErrorCode)
  1318. {
  1319. switch(nErrorCode)
  1320. {
  1321. case -COMMAND_RET_UNKNOWN_CMD:
  1322. return "Unknown Bootloader command";
  1323. case -COMMAND_RET_INVALID_CMD:
  1324. return "Invalid Bootloader command";
  1325. case -COMMAND_RET_INVALID_ADR:
  1326. return "Invalid Flash address";
  1327. case -COMMAND_RET_FLASH_FAIL:
  1328. return "Bootloader failed to erase flash";
  1329. case -COMMAND_RET_CRC_FAIL:
  1330. return "Invalid Image CRC32";
  1331. default:
  1332. return GfaMininetMasterStrError(nErrorCode);
  1333. }
  1334. }