ipmi_hpmfwupg.c 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636
  1. /*
  2. * Copyright (c) 2006 Kontron Canada, Inc. All Rights Reserved.
  3. * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * Redistribution of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * Redistribution in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * Neither the name of Sun Microsystems, Inc. or the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * This software is provided "AS IS," without a warranty of any kind.
  21. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
  22. * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
  23. * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
  24. * SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE
  25. * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  26. * OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
  27. * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA,
  28. * OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  29. * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  30. * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
  31. * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  32. */
  33. #define _BSD_SOURCE || \
  34. (_XOPEN_SOURCE >= 500 || \
  35. _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED) && \
  36. !(_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700)
  37. #include <ipmitool/ipmi_intf.h>
  38. #include <ipmitool/ipmi_mc.h>
  39. #include <ipmitool/ipmi_hpmfwupg.h>
  40. #include <ipmitool/helper.h>
  41. #include <ipmitool/ipmi_strings.h>
  42. #include <ipmitool/log.h>
  43. #include "../src/plugins/lan/md5.h"
  44. #include <ctype.h>
  45. #include <stdio.h>
  46. #include <time.h>
  47. #include <sys/param.h>
  48. #include <unistd.h>
  49. #if HAVE_CONFIG_H
  50. # include <config.h>
  51. #endif
  52. /* From src/plugins/ipmi_intf.c: */
  53. uint16_t
  54. ipmi_intf_get_max_request_data_size(struct ipmi_intf * intf);
  55. extern int verbose;
  56. int HpmfwupgUpgrade(struct ipmi_intf *intf, char *imageFilename,
  57. int activate, int, int);
  58. int HpmfwupgValidateImageIntegrity(struct HpmfwupgUpgradeCtx *pFwupgCtx);
  59. int HpmfwupgPreparationStage(struct ipmi_intf *intf,
  60. struct HpmfwupgUpgradeCtx *pFwupgCtx, int option);
  61. int HpmfwupgUpgradeStage(struct ipmi_intf *intf,
  62. struct HpmfwupgUpgradeCtx *pFwupgCtx, int option);
  63. int HpmfwupgActivationStage(struct ipmi_intf *intf,
  64. struct HpmfwupgUpgradeCtx *pFwupgCtx);
  65. int HpmfwupgGetTargetUpgCapabilities(struct ipmi_intf *intf,
  66. struct HpmfwupgGetTargetUpgCapabilitiesCtx *pCtx);
  67. int HpmfwupgGetComponentProperties(struct ipmi_intf *intf,
  68. struct HpmfwupgGetComponentPropertiesCtx *pCtx);
  69. int HpmfwupgQuerySelftestResult(struct ipmi_intf *intf,
  70. struct HpmfwupgQuerySelftestResultCtx *pCtx,
  71. struct HpmfwupgUpgradeCtx *pFwupgCtx);
  72. int HpmfwupgQueryRollbackStatus(struct ipmi_intf *intf,
  73. struct HpmfwupgQueryRollbackStatusCtx *pCtx,
  74. struct HpmfwupgUpgradeCtx *pFwupgCtx);
  75. int HpmfwupgAbortUpgrade(struct ipmi_intf *intf,
  76. struct HpmfwupgAbortUpgradeCtx *pCtx);
  77. int HpmfwupgInitiateUpgradeAction(struct ipmi_intf *intf,
  78. struct HpmfwupgInitiateUpgradeActionCtx *pCtx,
  79. struct HpmfwupgUpgradeCtx *pFwupgCtx);
  80. int HpmfwupgUploadFirmwareBlock(struct ipmi_intf *intf,
  81. struct HpmfwupgUploadFirmwareBlockCtx *pCtx,
  82. struct HpmfwupgUpgradeCtx *pFwupgCtx, int count,
  83. unsigned int *pOffset, unsigned int *blockLen);
  84. int HpmfwupgFinishFirmwareUpload(struct ipmi_intf *intf,
  85. struct HpmfwupgFinishFirmwareUploadCtx *pCtx,
  86. struct HpmfwupgUpgradeCtx *pFwupgCtx, int option);
  87. int HpmfwupgActivateFirmware(struct ipmi_intf *intf,
  88. struct HpmfwupgActivateFirmwareCtx *pCtx,
  89. struct HpmfwupgUpgradeCtx *pFwupgCtx);
  90. int HpmfwupgGetUpgradeStatus(struct ipmi_intf *intf,
  91. struct HpmfwupgGetUpgradeStatusCtx *pCtxstruct,
  92. struct HpmfwupgUpgradeCtx *pFwupgCtx, int silent);
  93. int HpmfwupgManualFirmwareRollback(struct ipmi_intf *intf,
  94. struct HpmfwupgManualFirmwareRollbackCtx *pCtx);
  95. void HpmfwupgPrintUsage(void);
  96. unsigned char HpmfwupgCalculateChecksum(unsigned char *pData,
  97. unsigned int length);
  98. int HpmfwupgGetDeviceId(struct ipmi_intf *intf,
  99. struct ipm_devid_rsp *pGetDevId);
  100. int HpmfwupgGetBufferFromFile(char *imageFilename,
  101. struct HpmfwupgUpgradeCtx *pFwupgCtx);
  102. int HpmfwupgWaitLongDurationCmd(struct ipmi_intf *intf,
  103. struct HpmfwupgUpgradeCtx *pFwupgCtx);
  104. struct ipmi_rs *HpmfwupgSendCmd(struct ipmi_intf *intf,
  105. struct ipmi_rq req, struct HpmfwupgUpgradeCtx* pFwupgCtx);
  106. int HpmFwupgActionUploadFirmware(struct HpmfwupgComponentBitMask components,
  107. struct HpmfwupgUpgradeCtx* pFwupgCtx,
  108. unsigned char **pImagePtr,
  109. struct ipmi_intf *intf,
  110. int option,
  111. int *pFlagColdReset);
  112. int
  113. HpmfwupgPreUpgradeCheck(struct ipmi_intf *intf,
  114. struct HpmfwupgUpgradeCtx *pFwupgCtx,
  115. int componentMask, int option);
  116. /* HpmGetuserInput - get input from user
  117. *
  118. * returns TRUE if its Yes or FALSE if its No
  119. */
  120. int
  121. HpmGetUserInput(char *str)
  122. {
  123. char userInput[2];
  124. int ret;
  125. printf("%s", str);
  126. ret = scanf("%s", userInput);
  127. if (!ret) {
  128. return 1;
  129. }
  130. if (toupper(userInput[0]) == 'Y') {
  131. return 1;
  132. }
  133. return 0;
  134. }
  135. /* HpmDisplayLine - display the line with the given character
  136. */
  137. void
  138. HpmDisplayLine(char *s, int n)
  139. {
  140. while (n--) {
  141. printf ("%c", *s);
  142. }
  143. printf("\n");
  144. }
  145. /* HpmDisplayUpgradeHeader - display the Upgrade header information
  146. */
  147. void
  148. HpmDisplayUpgradeHeader(void)
  149. {
  150. printf("\n");
  151. HpmDisplayLine("-", 79);
  152. printf(
  153. "|ID | Name | Versions | %% |\n");
  154. printf(
  155. "| | | Active | Backup | File | |\n");
  156. printf(
  157. "|----|-------------|-----------------|-----------------|-----------------|----|\n");
  158. }
  159. /* HpmDisplayUpgrade - display the progress of the upgrade; prints the "."
  160. * for every 5% of its completion.
  161. */
  162. void
  163. HpmDisplayUpgrade(int skip, unsigned int totalSent,
  164. unsigned int displayFWLength, time_t timeElapsed)
  165. {
  166. int percent;
  167. static int old_percent = -1;
  168. if (skip) {
  169. printf("Skip|\n");
  170. return;
  171. }
  172. fflush(stdout);
  173. percent = ((float)totalSent / displayFWLength) * 100;
  174. if (percent != old_percent) {
  175. if (old_percent != -1) {
  176. printf("\b\b\b\b\b");
  177. }
  178. printf("%3d%%|", percent);
  179. old_percent = percent;
  180. }
  181. if (totalSent == displayFWLength) {
  182. /* Display the time taken to complete the upgrade */
  183. printf(
  184. "\n| |Upload Time: %02ld:%02ld | Image Size: %7d bytes |\n",
  185. timeElapsed / 60, timeElapsed % 60, totalSent);
  186. old_percent = -1;
  187. }
  188. }
  189. /* HpmDisplayVersionHeader - display the information about version header
  190. */
  191. void
  192. HpmDisplayVersionHeader(int mode)
  193. {
  194. if (mode & IMAGE_VER) {
  195. HpmDisplayLine("-", 74);
  196. printf(
  197. "|ID | Name | Versions |\n");
  198. printf(
  199. "| | | Active | Backup | File |\n");
  200. HpmDisplayLine("-", 74);
  201. } else {
  202. HpmDisplayLine("-",74 );
  203. printf(
  204. "|ID | Name | Versions |\n");
  205. printf(
  206. "| | | Active | Backup | Deferred |\n");
  207. HpmDisplayLine("-", 74);
  208. }
  209. }
  210. /* HpmDisplayVersion - display the version of the image and target
  211. */
  212. void
  213. HpmDisplayVersion(int mode, VERSIONINFO *pVersion, int upgradable)
  214. {
  215. /*
  216. * If the cold reset is required then we can display * on it
  217. * so that user is aware that he needs to do payload power
  218. * cycle after upgrade
  219. */
  220. printf("|%c%c%2d|%-13s|",
  221. pVersion->coldResetRequired ? '*' : ' ',
  222. upgradable ? '^' : ' ',
  223. pVersion->componentId, pVersion->descString);
  224. if (mode & TARGET_VER) {
  225. if ((pVersion->targetMajor == 0xFF
  226. || (pVersion->targetMajor == 0x7F))
  227. && pVersion->targetMinor == 0xFF) {
  228. printf(" ---.-- -------- |");
  229. } else {
  230. printf(" %3d.%02x %02X%02X%02X%02X |",
  231. pVersion->targetMajor,
  232. pVersion->targetMinor,
  233. pVersion->targetAux[0],
  234. pVersion->targetAux[1],
  235. pVersion->targetAux[2],
  236. pVersion->targetAux[3]);
  237. }
  238. if (mode & ROLLBACK_VER) {
  239. if ((pVersion->rollbackMajor == 0xFF
  240. || (pVersion->rollbackMajor == 0x7F))
  241. && pVersion->rollbackMinor == 0xFF) {
  242. printf(" ---.-- -------- |");
  243. } else {
  244. printf(" %3d.%02x %02X%02X%02X%02X |",
  245. pVersion->rollbackMajor,
  246. pVersion->rollbackMinor,
  247. pVersion->rollbackAux[0],
  248. pVersion->rollbackAux[1],
  249. pVersion->rollbackAux[2],
  250. pVersion->rollbackAux[3]);
  251. }
  252. } else {
  253. printf(" ---.-- -------- |");
  254. }
  255. }
  256. if (mode & IMAGE_VER) {
  257. if ((pVersion->imageMajor == 0xFF
  258. || (pVersion->imageMajor == 0x7F))
  259. && pVersion->imageMinor == 0xFF) {
  260. printf(" ---.-- |");
  261. } else {
  262. printf(" %3d.%02x %02X%02X%02X%02X |",
  263. pVersion->imageMajor,
  264. pVersion->imageMinor,
  265. pVersion->imageAux[0],
  266. pVersion->imageAux[1],
  267. pVersion->imageAux[2],
  268. pVersion->imageAux[3]);
  269. }
  270. } else {
  271. if ((pVersion->deferredMajor == 0xFF
  272. || (pVersion->deferredMajor == 0x7F))
  273. && pVersion->deferredMinor == 0xFF) {
  274. printf(" ---.-- -------- |");
  275. } else {
  276. printf(" %3d.%02x %02X%02X%02X%02X |",
  277. pVersion->deferredMajor,
  278. pVersion->deferredMinor,
  279. pVersion->deferredAux[0],
  280. pVersion->deferredAux[1],
  281. pVersion->deferredAux[2],
  282. pVersion->deferredAux[3]);
  283. }
  284. }
  285. }
  286. /* HpmfwupgTargerCheck - get target information and displays it on the screen
  287. */
  288. int
  289. HpmfwupgTargetCheck(struct ipmi_intf *intf, int option)
  290. {
  291. struct HpmfwupgGetTargetUpgCapabilitiesCtx targetCapCmd;
  292. int rc = HPMFWUPG_SUCCESS;
  293. int componentId = 0;
  294. struct ipm_devid_rsp devIdrsp;
  295. struct HpmfwupgGetComponentPropertiesCtx getCompProp;
  296. int mode = 0;
  297. rc = HpmfwupgGetDeviceId(intf, &devIdrsp);
  298. if (rc != HPMFWUPG_SUCCESS) {
  299. lprintf(LOG_NOTICE,
  300. "Verify whether the Target board is present \n");
  301. return HPMFWUPG_ERROR;
  302. }
  303. rc = HpmfwupgGetTargetUpgCapabilities(intf, &targetCapCmd);
  304. if (rc != HPMFWUPG_SUCCESS) {
  305. /* That indicates the target is not responding to the command
  306. * May be that there is no HPM support
  307. */
  308. lprintf(LOG_NOTICE,
  309. "Board might not be supporting the HPM.1 Standards\n");
  310. return rc;
  311. }
  312. if (option & VIEW_MODE) {
  313. lprintf(LOG_NOTICE, "-------Target Information-------");
  314. lprintf(LOG_NOTICE, "Device Id : 0x%x",
  315. devIdrsp.device_id);
  316. lprintf(LOG_NOTICE, "Device Revision : 0x%x",
  317. devIdrsp.device_revision);
  318. lprintf(LOG_NOTICE, "Product Id : 0x%04x",
  319. buf2short(devIdrsp.product_id));
  320. lprintf(LOG_NOTICE, "Manufacturer Id : 0x%04x (%s)\n\n",
  321. buf2short(devIdrsp.manufacturer_id),
  322. val2str(buf2short(devIdrsp.manufacturer_id),ipmi_oem_info));
  323. HpmDisplayVersionHeader(TARGET_VER|ROLLBACK_VER);
  324. }
  325. for (componentId = HPMFWUPG_COMPONENT_ID_0;
  326. componentId < HPMFWUPG_COMPONENT_ID_MAX;
  327. componentId++ ) {
  328. /* If the component is supported */
  329. if (((1 << componentId) & targetCapCmd.resp.componentsPresent.ComponentBits.byte)) {
  330. memset((PVERSIONINFO)&gVersionInfo[componentId], 0x00, sizeof(VERSIONINFO));
  331. getCompProp.req.componentId = componentId;
  332. getCompProp.req.selector = HPMFWUPG_COMP_GEN_PROPERTIES;
  333. rc = HpmfwupgGetComponentProperties(intf, &getCompProp);
  334. if (rc != HPMFWUPG_SUCCESS) {
  335. lprintf(LOG_NOTICE, "Get CompGenProp Failed for component Id %d\n",
  336. componentId);
  337. return rc;
  338. }
  339. gVersionInfo[componentId].rollbackSupported = getCompProp.resp.Response.
  340. generalPropResp.GeneralCompProperties.bitfield.rollbackBackup;
  341. gVersionInfo[componentId].coldResetRequired = getCompProp.resp.Response.
  342. generalPropResp.GeneralCompProperties.bitfield.payloadColdReset;
  343. gVersionInfo[componentId].deferredActivationSupported = getCompProp.resp.Response.
  344. generalPropResp.GeneralCompProperties.bitfield.deferredActivation;
  345. getCompProp.req.selector = HPMFWUPG_COMP_DESCRIPTION_STRING;
  346. rc = HpmfwupgGetComponentProperties(intf, &getCompProp);
  347. if (rc != HPMFWUPG_SUCCESS) {
  348. lprintf(LOG_NOTICE,
  349. "Get CompDescString Failed for component Id %d\n",
  350. componentId);
  351. return rc;
  352. }
  353. memcpy(gVersionInfo[componentId].descString,
  354. getCompProp.resp.Response.descStringResp.descString,
  355. HPMFWUPG_DESC_STRING_LENGTH);
  356. gVersionInfo[componentId].descString[HPMFWUPG_DESC_STRING_LENGTH] = '\0';
  357. getCompProp.req.selector = HPMFWUPG_COMP_CURRENT_VERSION;
  358. rc = HpmfwupgGetComponentProperties(intf, &getCompProp);
  359. if (rc != HPMFWUPG_SUCCESS) {
  360. lprintf(LOG_NOTICE,
  361. "Get CompCurrentVersion Failed for component Id %d\n",
  362. componentId);
  363. return rc;
  364. }
  365. gVersionInfo[componentId].componentId = componentId;
  366. gVersionInfo[componentId].targetMajor = getCompProp.resp.Response.
  367. currentVersionResp.currentVersion[0];
  368. gVersionInfo[componentId].targetMinor = getCompProp.resp.Response.
  369. currentVersionResp.currentVersion[1];
  370. gVersionInfo[componentId].targetAux[0] = getCompProp.resp.Response.
  371. currentVersionResp.currentVersion[2];
  372. gVersionInfo[componentId].targetAux[1] = getCompProp.resp.Response.
  373. currentVersionResp.currentVersion[3];
  374. gVersionInfo[componentId].targetAux[2] = getCompProp.resp.Response.
  375. currentVersionResp.currentVersion[4];
  376. gVersionInfo[componentId].targetAux[3] = getCompProp.resp.Response.
  377. currentVersionResp.currentVersion[5];
  378. mode = TARGET_VER;
  379. if (gVersionInfo[componentId].rollbackSupported) {
  380. getCompProp.req.selector = HPMFWUPG_COMP_ROLLBACK_FIRMWARE_VERSION;
  381. rc = HpmfwupgGetComponentProperties(intf, &getCompProp);
  382. if (rc != HPMFWUPG_SUCCESS) {
  383. lprintf(LOG_NOTICE,
  384. "Get CompRollbackVersion Failed for component Id %d\n",
  385. componentId);
  386. } else {
  387. gVersionInfo[componentId].rollbackMajor = getCompProp.resp
  388. .Response.rollbackFwVersionResp.rollbackFwVersion[0];
  389. gVersionInfo[componentId].rollbackMinor = getCompProp.resp
  390. .Response.rollbackFwVersionResp.rollbackFwVersion[1];
  391. gVersionInfo[componentId].rollbackAux[0] = getCompProp.resp.Response.rollbackFwVersionResp.rollbackFwVersion[2];
  392. gVersionInfo[componentId].rollbackAux[1] = getCompProp.resp.Response.rollbackFwVersionResp.rollbackFwVersion[3];
  393. gVersionInfo[componentId].rollbackAux[2] = getCompProp.resp.Response.rollbackFwVersionResp.rollbackFwVersion[4];
  394. gVersionInfo[componentId].rollbackAux[3] = getCompProp.resp.Response.rollbackFwVersionResp.rollbackFwVersion[5];
  395. }
  396. mode |= ROLLBACK_VER;
  397. } else {
  398. gVersionInfo[componentId].rollbackMajor = 0xff;
  399. gVersionInfo[componentId].rollbackMinor = 0xff;
  400. gVersionInfo[componentId].rollbackAux[0] = 0xff;
  401. gVersionInfo[componentId].rollbackAux[1] = 0xff;
  402. gVersionInfo[componentId].rollbackAux[2] = 0xff;
  403. gVersionInfo[componentId].rollbackAux[3] = 0xff;
  404. }
  405. if (gVersionInfo[componentId].deferredActivationSupported) {
  406. getCompProp.req.selector = HPMFWUPG_COMP_DEFERRED_FIRMWARE_VERSION;
  407. rc = HpmfwupgGetComponentProperties(intf, &getCompProp);
  408. if (rc != HPMFWUPG_SUCCESS) {
  409. lprintf(LOG_NOTICE,
  410. "Get CompRollbackVersion Failed for component Id %d\n",
  411. componentId);
  412. } else {
  413. gVersionInfo[componentId].deferredMajor = getCompProp.resp
  414. .Response.deferredFwVersionResp.deferredFwVersion[0];
  415. gVersionInfo[componentId].deferredMinor = getCompProp.resp
  416. .Response.deferredFwVersionResp.deferredFwVersion[1];
  417. gVersionInfo[componentId].deferredAux[0] = getCompProp.resp.Response.deferredFwVersionResp.deferredFwVersion[2];
  418. gVersionInfo[componentId].deferredAux[1] = getCompProp.resp.Response.deferredFwVersionResp.deferredFwVersion[3];
  419. gVersionInfo[componentId].deferredAux[2] = getCompProp.resp.Response.deferredFwVersionResp.deferredFwVersion[4];
  420. gVersionInfo[componentId].deferredAux[3] = getCompProp.resp.Response.deferredFwVersionResp.deferredFwVersion[5];
  421. }
  422. } else {
  423. gVersionInfo[componentId].deferredMajor = 0xff;
  424. gVersionInfo[componentId].deferredMinor = 0xff;
  425. gVersionInfo[componentId].deferredAux[0] = 0xff;
  426. gVersionInfo[componentId].deferredAux[1] = 0xff;
  427. gVersionInfo[componentId].deferredAux[2] = 0xff;
  428. gVersionInfo[componentId].deferredAux[3] = 0xff;
  429. }
  430. if (option & VIEW_MODE) {
  431. HpmDisplayVersion(mode,
  432. &gVersionInfo[componentId],
  433. 0);
  434. printf("\n");
  435. }
  436. }
  437. }
  438. if (option & VIEW_MODE) {
  439. HpmDisplayLine("-",74 );
  440. fflush(stdout);
  441. lprintf(LOG_NOTICE,
  442. "(*) Component requires Payload Cold Reset");
  443. printf("\n\n");
  444. }
  445. return HPMFWUPG_SUCCESS;
  446. }
  447. /* HpmfwupgUpgrade - perform the HPM.1 firmware upgrade procedure as defined
  448. * the IPM Controller Firmware Upgrade Specification version 1.0
  449. */
  450. int
  451. HpmfwupgUpgrade(struct ipmi_intf *intf, char *imageFilename, int activate,
  452. int componentMask, int option)
  453. {
  454. int rc = HPMFWUPG_SUCCESS;
  455. struct HpmfwupgUpgradeCtx fwupgCtx;
  456. /* INITIALIZE UPGRADE CONTEXT */
  457. memset(&fwupgCtx, 0, sizeof (fwupgCtx));
  458. /* GET IMAGE BUFFER FROM FILE */
  459. rc = HpmfwupgGetBufferFromFile(imageFilename, &fwupgCtx);
  460. /* VALIDATE IMAGE INTEGRITY */
  461. if (rc == HPMFWUPG_SUCCESS) {
  462. printf("Validating firmware image integrity...");
  463. fflush(stdout);
  464. rc = HpmfwupgValidateImageIntegrity(&fwupgCtx);
  465. if (rc == HPMFWUPG_SUCCESS) {
  466. printf("OK\n");
  467. fflush(stdout);
  468. }
  469. }
  470. /* PREPARATION STAGE */
  471. if (rc == HPMFWUPG_SUCCESS) {
  472. printf("Performing preparation stage...");
  473. fflush(stdout);
  474. rc = HpmfwupgPreparationStage(intf, &fwupgCtx, option);
  475. if (rc == HPMFWUPG_SUCCESS) {
  476. printf("OK\n");
  477. fflush(stdout);
  478. }
  479. }
  480. /* UPGRADE STAGE */
  481. if (rc == HPMFWUPG_SUCCESS) {
  482. if (option & VIEW_MODE) {
  483. lprintf(LOG_NOTICE,
  484. "\nComparing Target & Image File version");
  485. } else if (option & COMPARE_MODE) {
  486. lprintf(LOG_NOTICE,
  487. "\nPerforming upload for compare stage:");
  488. } else {
  489. lprintf(LOG_NOTICE, "\nPerforming upgrade stage:");
  490. }
  491. if (option & VIEW_MODE) {
  492. rc = HpmfwupgPreUpgradeCheck(intf,
  493. &fwupgCtx,componentMask, VIEW_MODE);
  494. } else {
  495. rc = HpmfwupgPreUpgradeCheck(intf, &fwupgCtx,
  496. componentMask, option);
  497. if (rc == HPMFWUPG_SUCCESS) {
  498. if (verbose) {
  499. printf("Component update mask : 0x%02x\n",
  500. fwupgCtx.compUpdateMask.ComponentBits.byte);
  501. }
  502. rc = HpmfwupgUpgradeStage(intf, &fwupgCtx, option);
  503. }
  504. }
  505. }
  506. /* ACTIVATION STAGE */
  507. if (rc == HPMFWUPG_SUCCESS && activate) {
  508. /* check if upgrade components mask is non-zero */
  509. if (fwupgCtx.compUpdateMask.ComponentBits.byte) {
  510. lprintf(LOG_NOTICE, "Performing activation stage: ");
  511. rc = HpmfwupgActivationStage(intf, &fwupgCtx);
  512. } else {
  513. lprintf(LOG_NOTICE,
  514. "No components updated. Skipping activation stage.\n");
  515. }
  516. }
  517. if (rc == HPMFWUPG_SUCCESS) {
  518. if (option & VIEW_MODE) {
  519. /* Dont display anything here in case we are just viewing it */
  520. lprintf(LOG_NOTICE," ");
  521. } else if (option & COMPARE_MODE) {
  522. lprintf(LOG_NOTICE,
  523. "\nFirmware comparison procedure complete\n");
  524. } else {
  525. lprintf(LOG_NOTICE,
  526. "\nFirmware upgrade procedure successful\n");
  527. }
  528. } else if (option & VIEW_MODE) {
  529. /* Dont display anything here in case we are just viewing it */
  530. lprintf(LOG_NOTICE," ");
  531. } else if (option & COMPARE_MODE) {
  532. lprintf(LOG_NOTICE,
  533. "Firmware comparison procedure failed\n");
  534. } else {
  535. lprintf(LOG_NOTICE, "Firmware upgrade procedure failed\n");
  536. }
  537. if (fwupgCtx.pImageData) {
  538. free(fwupgCtx.pImageData);
  539. fwupgCtx.pImageData = NULL;
  540. }
  541. return rc;
  542. }
  543. /* HpmfwupgValidateImageIntegrity - validate a HPM.1 firmware image file as
  544. * defined in section 4 of the IPM Controller Firmware Upgrade Specification
  545. * version 1.0
  546. */
  547. int
  548. HpmfwupgValidateImageIntegrity(struct HpmfwupgUpgradeCtx *pFwupgCtx)
  549. {
  550. struct HpmfwupgImageHeader *pImageHeader = (struct HpmfwupgImageHeader*)pFwupgCtx->pImageData;
  551. md5_state_t ctx;
  552. static unsigned char md[HPMFWUPG_MD5_SIGNATURE_LENGTH];
  553. unsigned char *pMd5Sig = pFwupgCtx->pImageData
  554. + (pFwupgCtx->imageSize - HPMFWUPG_MD5_SIGNATURE_LENGTH);
  555. /* Validate MD5 checksum */
  556. memset(md, 0, HPMFWUPG_MD5_SIGNATURE_LENGTH);
  557. memset(&ctx, 0, sizeof(md5_state_t));
  558. md5_init(&ctx);
  559. md5_append(&ctx, pFwupgCtx->pImageData,
  560. pFwupgCtx->imageSize - HPMFWUPG_MD5_SIGNATURE_LENGTH);
  561. md5_finish(&ctx, md);
  562. if (memcmp(md, pMd5Sig, HPMFWUPG_MD5_SIGNATURE_LENGTH) != 0) {
  563. lprintf(LOG_NOTICE, "\n Invalid MD5 signature");
  564. return HPMFWUPG_ERROR;
  565. }
  566. /* Validate Header signature */
  567. if(strncmp(pImageHeader->signature,
  568. HPMFWUPG_IMAGE_SIGNATURE,
  569. HPMFWUPG_HEADER_SIGNATURE_LENGTH) != 0) {
  570. lprintf(LOG_NOTICE,"\n Invalid image signature");
  571. return HPMFWUPG_ERROR;
  572. }
  573. /* Validate Header image format version */
  574. if (pImageHeader->formatVersion != HPMFWUPG_IMAGE_HEADER_VERSION) {
  575. lprintf(LOG_NOTICE,"\n Unrecognized image version");
  576. return HPMFWUPG_ERROR;
  577. }
  578. /* Validate header checksum */
  579. if (HpmfwupgCalculateChecksum((unsigned char*)pImageHeader,
  580. sizeof(struct HpmfwupgImageHeader)
  581. + pImageHeader->oemDataLength
  582. + sizeof(unsigned char)/*checksum*/) != 0) {
  583. lprintf(LOG_NOTICE,"\n Invalid header checksum");
  584. return HPMFWUPG_ERROR;
  585. }
  586. return HPMFWUPG_SUCCESS;
  587. }
  588. /* HpmfwupgPreparationStage - prepere stage of a firmware upgrade procedure as
  589. * defined in section 3.2 of the IPM Controller Firmware Upgrade Specification
  590. * version 1.0
  591. */
  592. int
  593. HpmfwupgPreparationStage(struct ipmi_intf *intf,
  594. struct HpmfwupgUpgradeCtx *pFwupgCtx, int option)
  595. {
  596. int componentId;
  597. int rc = HPMFWUPG_SUCCESS;
  598. struct HpmfwupgGetTargetUpgCapabilitiesCtx targetCapCmd;
  599. struct HpmfwupgImageHeader *pImageHeader = (struct HpmfwupgImageHeader*)
  600. pFwupgCtx->pImageData;
  601. /* Get device ID */
  602. rc = HpmfwupgGetDeviceId(intf, &pFwupgCtx->devId);
  603. /* Match current IPMC IDs with upgrade image */
  604. if (rc != HPMFWUPG_SUCCESS) {
  605. return HPMFWUPG_ERROR;
  606. }
  607. /* Validate device ID */
  608. if (pImageHeader->deviceId == pFwupgCtx->devId.device_id) {
  609. /* Validate product ID */
  610. if (memcmp(pImageHeader->prodId,
  611. pFwupgCtx->devId.product_id,
  612. HPMFWUPG_PRODUCT_ID_LENGTH ) == 0) {
  613. /* Validate man ID */
  614. if (memcmp(pImageHeader->manId,
  615. pFwupgCtx->devId.manufacturer_id,
  616. HPMFWUPG_MANUFATURER_ID_LENGTH) != 0) {
  617. lprintf(LOG_NOTICE,
  618. "\n Invalid image file for manufacturer %u",
  619. buf2short(pFwupgCtx->devId.manufacturer_id));
  620. rc = HPMFWUPG_ERROR;
  621. }
  622. } else {
  623. lprintf(LOG_NOTICE,
  624. "\n Invalid image file for product %u",
  625. buf2short(pFwupgCtx->devId.product_id));
  626. rc = HPMFWUPG_ERROR;
  627. }
  628. } else {
  629. lprintf(LOG_NOTICE, "\n Invalid device ID %x",
  630. pFwupgCtx->devId.device_id);
  631. rc = HPMFWUPG_ERROR;
  632. }
  633. if (rc != HPMFWUPG_SUCCESS) {
  634. /* Giving one more chance to user to check whether its OK to continue even if the
  635. * product ID does not match. This is helpful as sometimes we just want to update
  636. * and dont care whether we have a different product Id. If the user says NO then
  637. * we need to just bail out from here
  638. */
  639. if (!((option & FORCE_MODE) || (option & VIEW_MODE))) {
  640. printf("\n\n Use \"force\" option for copying all the components\n");
  641. return HPMFWUPG_ERROR;
  642. }
  643. printf("\n Image Information");
  644. printf("\n Device Id : 0x%x", pImageHeader->deviceId);
  645. printf("\n Prod Id : 0x%02x%02x",
  646. pImageHeader->prodId[1], pImageHeader->prodId[0]);
  647. printf("\n Manuf Id : 0x%02x%02x%02x",
  648. pImageHeader->manId[2],
  649. pImageHeader->manId[1],
  650. pImageHeader->manId[0]);
  651. printf("\n Board Information");
  652. printf("\n Device Id : 0x%x", pFwupgCtx->devId.device_id);
  653. printf("\n Prod Id : 0x%02x%02x",
  654. pFwupgCtx->devId.product_id[1], pFwupgCtx->devId.product_id[0]);
  655. printf("\n Manuf Id : 0x%02x%02x%02x",
  656. pFwupgCtx->devId.manufacturer_id[2],
  657. pFwupgCtx->devId.manufacturer_id[1],
  658. pFwupgCtx->devId.manufacturer_id[0]);
  659. if (HpmGetUserInput("\n Continue ignoring DeviceID/ProductID/ManufacturingID (Y/N): ")) {
  660. rc = HPMFWUPG_SUCCESS;
  661. } else {
  662. return HPMFWUPG_ERROR;
  663. }
  664. }
  665. /* Validate earliest compatible revision */
  666. /* Validate major & minor revision */
  667. if (pImageHeader->compRevision[0] > pFwupgCtx->devId.fw_rev1
  668. || (pImageHeader->compRevision[0] == pFwupgCtx->devId.fw_rev1
  669. && pImageHeader->compRevision[1] > pFwupgCtx->devId.fw_rev2)) {
  670. /* Version not compatible for upgrade */
  671. lprintf(LOG_NOTICE, "\n Version: Major: %d", pImageHeader->compRevision[0]);
  672. lprintf(LOG_NOTICE, " Minor: %x", pImageHeader->compRevision[1]);
  673. lprintf(LOG_NOTICE, " Not compatible with ");
  674. lprintf(LOG_NOTICE, " Version: Major: %d", pFwupgCtx->devId.fw_rev1);
  675. lprintf(LOG_NOTICE, " Minor: %x", pFwupgCtx->devId.fw_rev2);
  676. /* Confirming it once again */
  677. if (!((option & FORCE_MODE) || (option & VIEW_MODE))) {
  678. return HPMFWUPG_ERROR;
  679. }
  680. if (HpmGetUserInput("\n Continue IGNORING Earliest compatibility (Y/N): ")) {
  681. rc = HPMFWUPG_SUCCESS;
  682. } else {
  683. return HPMFWUPG_ERROR;
  684. }
  685. }
  686. /* Get target upgrade capabilities */
  687. rc = HpmfwupgGetTargetUpgCapabilities(intf, &targetCapCmd);
  688. if (rc != HPMFWUPG_SUCCESS) {
  689. return HPMFWUPG_ERROR;
  690. }
  691. /* Copy response to context */
  692. memcpy(&pFwupgCtx->targetCap,
  693. &targetCapCmd.resp,
  694. sizeof(struct HpmfwupgGetTargetUpgCapabilitiesResp));
  695. if (option & VIEW_MODE) {
  696. /* do nothing */
  697. } else {
  698. /* Make sure all component IDs defined in the
  699. * upgrade image are supported by the IPMC
  700. */
  701. if ((pImageHeader->components.ComponentBits.byte &
  702. pFwupgCtx->targetCap.componentsPresent.ComponentBits.byte) !=
  703. pImageHeader->components.ComponentBits.byte) {
  704. lprintf(LOG_NOTICE,
  705. "\n Some components present in the image file are not supported by the IPMC");
  706. return HPMFWUPG_ERROR;
  707. }
  708. /* Make sure the upgrade is desirable rigth now */
  709. if (pFwupgCtx->targetCap.GlobalCapabilities.bitField.fwUpgUndesirable == 1) {
  710. lprintf(LOG_NOTICE, "\n Upgrade undesirable at this moment");
  711. return HPMFWUPG_ERROR;
  712. }
  713. /* Get confimation from the user if he wants to continue when
  714. * service affected during upgrade
  715. */
  716. if (!(option & COMPARE_MODE)
  717. && (pFwupgCtx->targetCap.GlobalCapabilities.bitField.servAffectDuringUpg == 1
  718. || pImageHeader->imageCapabilities.bitField.servAffected == 1)) {
  719. if (HpmGetUserInput("\nServices may be affected during upgrade. Do you wish to continue? (y/n): ")) {
  720. rc = HPMFWUPG_SUCCESS;
  721. } else {
  722. return HPMFWUPG_ERROR;
  723. }
  724. }
  725. }
  726. /* Get the general properties of each component present in image */
  727. for (componentId = HPMFWUPG_COMPONENT_ID_0;
  728. componentId < HPMFWUPG_COMPONENT_ID_MAX;
  729. componentId++) {
  730. /* Reset component properties */
  731. memset(&pFwupgCtx->genCompProp[componentId], 0,
  732. sizeof (struct HpmfwupgGetGeneralPropResp));
  733. if ((1 << componentId & pImageHeader->components.ComponentBits.byte)) {
  734. struct HpmfwupgGetComponentPropertiesCtx getCompPropCmd;
  735. /* Get general component properties */
  736. getCompPropCmd.req.componentId = componentId;
  737. getCompPropCmd.req.selector = HPMFWUPG_COMP_GEN_PROPERTIES;
  738. rc = HpmfwupgGetComponentProperties(intf, &getCompPropCmd);
  739. if (rc == HPMFWUPG_SUCCESS) {
  740. /* Copy response to context */
  741. memcpy(&pFwupgCtx->genCompProp[componentId],
  742. &getCompPropCmd.resp,
  743. sizeof(struct HpmfwupgGetGeneralPropResp));
  744. }
  745. }
  746. }
  747. return rc;
  748. }
  749. int
  750. image_version_upgradable(VERSIONINFO *pVersionInfo)
  751. {
  752. /* If the image and active target versions are different, then
  753. * upgrade */
  754. if ((pVersionInfo->imageMajor != pVersionInfo->targetMajor)
  755. || (pVersionInfo->imageMinor != pVersionInfo->targetMinor)
  756. || (pVersionInfo->imageAux[0] != pVersionInfo->targetAux[0])
  757. || (pVersionInfo->imageAux[1] != pVersionInfo->targetAux[1])
  758. || (pVersionInfo->imageAux[2] != pVersionInfo->targetAux[2])
  759. || (pVersionInfo->imageAux[3] != pVersionInfo->targetAux[3])) {
  760. return (1);
  761. }
  762. /* If the image and active target versions are the same and rollback
  763. * is not supported, then there's nothing to do, skip the upgrade
  764. */
  765. if (!pVersionInfo->rollbackSupported) {
  766. return (0);
  767. }
  768. /* If the image and rollback target versions are different, then
  769. * go ahead and upgrade
  770. */
  771. if ((pVersionInfo->imageMajor != pVersionInfo->rollbackMajor)
  772. || (pVersionInfo->imageMinor != pVersionInfo->rollbackMinor)
  773. || (pVersionInfo->imageAux[0] != pVersionInfo->rollbackAux[0])
  774. || (pVersionInfo->imageAux[1] != pVersionInfo->rollbackAux[1])
  775. || (pVersionInfo->imageAux[2] != pVersionInfo->rollbackAux[2])
  776. || (pVersionInfo->imageAux[3] != pVersionInfo->rollbackAux[3])) {
  777. return (1);
  778. }
  779. /* Image and rollback target versions are the same too, skip it */
  780. return (0);
  781. }
  782. /* HpmfwupgValidateActionRecordChecksum - validate checksum of the specified
  783. * action record header.
  784. */
  785. int
  786. HpmfwupgValidateActionRecordChecksum(struct HpmfwupgActionRecord *pActionRecord)
  787. {
  788. int rc = HPMFWUPG_SUCCESS;
  789. /* Validate action record checksum */
  790. if (HpmfwupgCalculateChecksum((unsigned char*)pActionRecord,
  791. sizeof(struct HpmfwupgActionRecord)) != 0) {
  792. /* Due to ambiguity in the HPM.1 specification, for the case of
  793. * the Upload Firmware Image action type, the record header length
  794. * might be thought as either the first 3 bytes, or the first 34 bytes
  795. * which precede the firmware image data.
  796. * For the latter case we re-calculate the Upload Firmware Image
  797. * record checksum for the 34 byte header length.
  798. */
  799. if (pActionRecord->actionType != HPMFWUPG_ACTION_UPLOAD_FIRMWARE
  800. || HpmfwupgCalculateChecksum((unsigned char*)pActionRecord,
  801. sizeof(struct HpmfwupgActionRecord)
  802. + sizeof(struct HpmfwupgFirmwareImage))) {
  803. lprintf(LOG_NOTICE, " Invalid Action record.");
  804. rc = HPMFWUPG_ERROR;
  805. }
  806. }
  807. return rc;
  808. }
  809. /* HpmfwupgPreUpgradeCheck - make pre-Upgrade check, this mainly helps in
  810. * checking which all version upgrade is skippable because the image version
  811. * is same as target version.
  812. */
  813. int
  814. HpmfwupgPreUpgradeCheck(struct ipmi_intf *intf,
  815. struct HpmfwupgUpgradeCtx *pFwupgCtx,
  816. int componentMask, int option)
  817. {
  818. unsigned char *pImagePtr;
  819. struct HpmfwupgActionRecord *pActionRecord;
  820. struct HpmfwupgImageHeader *pImageHeader;
  821. int componentId;
  822. pImageHeader = (struct HpmfwupgImageHeader*)pFwupgCtx->pImageData;
  823. /* Put pointer after image header */
  824. pImagePtr = (unsigned char*)(pFwupgCtx->pImageData
  825. + sizeof(struct HpmfwupgImageHeader)
  826. + pImageHeader->oemDataLength
  827. + sizeof(unsigned char)/*chksum*/);
  828. if (option & VIEW_MODE) {
  829. HpmDisplayVersionHeader(TARGET_VER|ROLLBACK_VER|IMAGE_VER);
  830. }
  831. /* Perform actions defined in the image */
  832. while (pImagePtr < (pFwupgCtx->pImageData + pFwupgCtx->imageSize
  833. - HPMFWUPG_MD5_SIGNATURE_LENGTH)) {
  834. /* Get action record */
  835. pActionRecord = (struct HpmfwupgActionRecord*)pImagePtr;
  836. /* Validate action record checksum */
  837. if (HpmfwupgValidateActionRecordChecksum(pActionRecord) != HPMFWUPG_SUCCESS) {
  838. return HPMFWUPG_ERROR;
  839. }
  840. /* Validate affected components */
  841. if (pActionRecord->components.ComponentBits.byte
  842. && !pFwupgCtx->targetCap.componentsPresent.ComponentBits.byte) {
  843. lprintf(LOG_NOTICE,
  844. " Invalid action record. One or more affected components is not supported");
  845. return HPMFWUPG_ERROR;
  846. }
  847. switch (pActionRecord->actionType) {
  848. case HPMFWUPG_ACTION_BACKUP_COMPONENTS:
  849. {
  850. /* Make sure every component specified by
  851. * this action record
  852. * supports the backup operation
  853. */
  854. for (componentId = HPMFWUPG_COMPONENT_ID_0;
  855. componentId < HPMFWUPG_COMPONENT_ID_MAX;
  856. componentId++) {
  857. if (((1 << componentId) & pActionRecord->components.ComponentBits.byte)
  858. && pFwupgCtx->genCompProp[componentId].GeneralCompProperties.bitfield.rollbackBackup == 0) {
  859. lprintf(LOG_NOTICE,
  860. " Component ID %d does not support backup",
  861. componentId);
  862. return HPMFWUPG_ERROR;
  863. }
  864. }
  865. pImagePtr += sizeof(struct HpmfwupgActionRecord);
  866. }
  867. break;
  868. case HPMFWUPG_ACTION_PREPARE_COMPONENTS:
  869. {
  870. /* Make sure every components specified by
  871. * this action
  872. * supports the prepare operation
  873. */
  874. for (componentId = HPMFWUPG_COMPONENT_ID_0;
  875. componentId < HPMFWUPG_COMPONENT_ID_MAX;
  876. componentId++) {
  877. if (((1 << componentId) & pActionRecord->components.ComponentBits.byte)
  878. && pFwupgCtx->genCompProp[componentId].GeneralCompProperties.bitfield.preparationSupport == 0) {
  879. lprintf(LOG_NOTICE,
  880. " Component ID %d does not support preparation",
  881. componentId);
  882. return HPMFWUPG_ERROR;
  883. }
  884. }
  885. pImagePtr += sizeof(struct HpmfwupgActionRecord);
  886. }
  887. break;
  888. case HPMFWUPG_ACTION_UPLOAD_FIRMWARE:
  889. /* Upload all firmware blocks */
  890. {
  891. struct HpmfwupgFirmwareImage *pFwImage;
  892. unsigned char *pData;
  893. unsigned int firmwareLength;
  894. unsigned char mode;
  895. unsigned char componentId;
  896. unsigned char componentIdByte;
  897. unsigned int upgrade_comp;
  898. VERSIONINFO *pVersionInfo;
  899. /* Save component ID on which the upload is done */
  900. componentIdByte = pActionRecord->components.ComponentBits.byte;
  901. componentId = 0;
  902. while ((componentIdByte >>= 1) != 0) {
  903. componentId++;
  904. }
  905. pFwImage = (struct HpmfwupgFirmwareImage*)(pImagePtr
  906. + sizeof(struct HpmfwupgActionRecord));
  907. pData = ((unsigned char*)pFwImage
  908. + sizeof(struct HpmfwupgFirmwareImage));
  909. /* Get firmware length */
  910. firmwareLength = pFwImage->length[0];
  911. firmwareLength |= (pFwImage->length[1] << 8) & 0xff00;
  912. firmwareLength |= (pFwImage->length[2] << 16) & 0xff0000;
  913. firmwareLength |= (pFwImage->length[3] << 24) & 0xff000000;
  914. pVersionInfo = &gVersionInfo[componentId];
  915. pVersionInfo->imageMajor = pFwImage->version[0];
  916. pVersionInfo->imageMinor = pFwImage->version[1];
  917. pVersionInfo->imageAux[0] = pFwImage->version[2];
  918. pVersionInfo->imageAux[1] = pFwImage->version[3];
  919. pVersionInfo->imageAux[2] = pFwImage->version[4];
  920. pVersionInfo->imageAux[3] = pFwImage->version[5];
  921. mode = TARGET_VER | IMAGE_VER;
  922. /* check if component is selected for upgrade */
  923. upgrade_comp = !componentMask
  924. || (componentMask & pActionRecord->components.ComponentBits.byte);
  925. /* check if current component version requires upgrade */
  926. if (upgrade_comp && !(option & (FORCE_MODE|COMPARE_MODE))) {
  927. upgrade_comp = image_version_upgradable(pVersionInfo);
  928. }
  929. if (verbose) {
  930. lprintf(LOG_NOTICE,
  931. "%s component %d",
  932. (upgrade_comp ? "Updating" : "Skipping"),
  933. componentId);
  934. }
  935. if (upgrade_comp) {
  936. pFwupgCtx->compUpdateMask.ComponentBits.byte|= 1 << componentId;
  937. }
  938. if (option & VIEW_MODE) {
  939. if (pVersionInfo->rollbackSupported) {
  940. mode|= ROLLBACK_VER;
  941. }
  942. HpmDisplayVersion(mode,pVersionInfo, upgrade_comp);
  943. printf("\n");
  944. }
  945. pImagePtr = pData + firmwareLength;
  946. }
  947. break;
  948. default:
  949. lprintf(LOG_NOTICE,
  950. " Invalid Action type. Cannot continue");
  951. return HPMFWUPG_ERROR;
  952. break;
  953. }
  954. }
  955. if (option & VIEW_MODE) {
  956. HpmDisplayLine("-",74);
  957. fflush(stdout);
  958. lprintf(LOG_NOTICE,
  959. "(*) Component requires Payload Cold Reset");
  960. lprintf(LOG_NOTICE,
  961. "(^) Indicates component would be upgraded");
  962. }
  963. return HPMFWUPG_SUCCESS;
  964. }
  965. /* HpmfwupgUpgradeStage - upgrade stage of a firmware upgrade procedure as
  966. * defined in section 3.3 of the IPM Controller Firmware Upgrade Specification
  967. * version 1.0
  968. */
  969. int
  970. HpmfwupgUpgradeStage(struct ipmi_intf *intf,
  971. struct HpmfwupgUpgradeCtx *pFwupgCtx, int option)
  972. {
  973. struct HpmfwupgImageHeader *pImageHeader = (struct HpmfwupgImageHeader*)
  974. pFwupgCtx->pImageData;
  975. struct HpmfwupgActionRecord* pActionRecord;
  976. int rc = HPMFWUPG_SUCCESS;
  977. unsigned char *pImagePtr;
  978. int flagColdReset = FALSE;
  979. /* Put pointer after image header */
  980. pImagePtr = (unsigned char*)
  981. (pFwupgCtx->pImageData + sizeof(struct HpmfwupgImageHeader) +
  982. pImageHeader->oemDataLength + sizeof(unsigned char)/*checksum*/);
  983. if (!(option & VIEW_MODE)) {
  984. HpmDisplayUpgradeHeader();
  985. }
  986. /* Perform actions defined in the image */
  987. while (( pImagePtr < (pFwupgCtx->pImageData + pFwupgCtx->imageSize -
  988. HPMFWUPG_MD5_SIGNATURE_LENGTH))
  989. && (rc == HPMFWUPG_SUCCESS)) {
  990. /* Get action record */
  991. pActionRecord = (struct HpmfwupgActionRecord*)pImagePtr;
  992. /* Validate action record checksum */
  993. rc = HpmfwupgValidateActionRecordChecksum(pActionRecord);
  994. if (rc != HPMFWUPG_SUCCESS) {
  995. continue;
  996. }
  997. switch(pActionRecord->actionType) {
  998. case HPMFWUPG_ACTION_BACKUP_COMPONENTS:
  999. {
  1000. if (!(option & COMPARE_MODE)) {
  1001. /* Send Upgrade Action command */
  1002. struct HpmfwupgInitiateUpgradeActionCtx initUpgActionCmd;
  1003. /* Affect only selected components */
  1004. initUpgActionCmd.req.componentsMask.ComponentBits.byte =
  1005. pFwupgCtx->compUpdateMask.ComponentBits.byte &
  1006. pActionRecord->components.ComponentBits.byte;
  1007. /* Action is prepare components */
  1008. if (initUpgActionCmd.req.componentsMask.ComponentBits.byte) {
  1009. initUpgActionCmd.req.upgradeAction = HPMFWUPG_UPGRADE_ACTION_BACKUP;
  1010. rc = HpmfwupgInitiateUpgradeAction(intf, &initUpgActionCmd, pFwupgCtx);
  1011. }
  1012. }
  1013. pImagePtr+= sizeof(struct HpmfwupgActionRecord);
  1014. }
  1015. break;
  1016. case HPMFWUPG_ACTION_PREPARE_COMPONENTS:
  1017. {
  1018. if (!(option & COMPARE_MODE)) {
  1019. /* Send prepare components command */
  1020. struct HpmfwupgInitiateUpgradeActionCtx initUpgActionCmd;
  1021. /* Affect only selected components */
  1022. initUpgActionCmd.req.componentsMask.ComponentBits.byte =
  1023. pFwupgCtx->compUpdateMask.ComponentBits.byte &
  1024. pActionRecord->components.ComponentBits.byte;
  1025. if (initUpgActionCmd.req.componentsMask.ComponentBits.byte) {
  1026. /* Action is prepare components */
  1027. initUpgActionCmd.req.upgradeAction = HPMFWUPG_UPGRADE_ACTION_PREPARE;
  1028. rc = HpmfwupgInitiateUpgradeAction(intf, &initUpgActionCmd, pFwupgCtx);
  1029. }
  1030. }
  1031. pImagePtr+= sizeof(struct HpmfwupgActionRecord);
  1032. }
  1033. break;
  1034. case HPMFWUPG_ACTION_UPLOAD_FIRMWARE:
  1035. /* Upload all firmware blocks */
  1036. rc = HpmFwupgActionUploadFirmware(pActionRecord->components,
  1037. pFwupgCtx,
  1038. &pImagePtr,
  1039. intf,
  1040. option,
  1041. &flagColdReset);
  1042. break;
  1043. default:
  1044. lprintf(LOG_NOTICE, " Invalid Action type. Cannot continue");
  1045. rc = HPMFWUPG_ERROR;
  1046. break;
  1047. }
  1048. }
  1049. HpmDisplayLine("-", 79);
  1050. fflush(stdout);
  1051. lprintf(LOG_NOTICE, "(*) Component requires Payload Cold Reset");
  1052. return rc;
  1053. }
  1054. int
  1055. HpmFwupgActionUploadFirmware(struct HpmfwupgComponentBitMask components,
  1056. struct HpmfwupgUpgradeCtx *pFwupgCtx,
  1057. unsigned char **pImagePtr,
  1058. struct ipmi_intf *intf,
  1059. int option,
  1060. int *pFlagColdReset)
  1061. {
  1062. struct HpmfwupgFirmwareImage *pFwImage;
  1063. struct HpmfwupgInitiateUpgradeActionCtx initUpgActionCmd;
  1064. struct HpmfwupgUploadFirmwareBlockCtx uploadCmd;
  1065. struct HpmfwupgFinishFirmwareUploadCtx finishCmd;
  1066. VERSIONINFO *pVersionInfo;
  1067. time_t start,end;
  1068. int rc = HPMFWUPG_SUCCESS;
  1069. int skip = TRUE;
  1070. unsigned char *pData, *pDataInitial;
  1071. unsigned short count;
  1072. unsigned int totalSent = 0;
  1073. unsigned short bufLength = 0;
  1074. unsigned short bufLengthIsSet = 0;
  1075. unsigned int firmwareLength = 0;
  1076. unsigned int displayFWLength = 0;
  1077. unsigned char *pDataTemp;
  1078. unsigned int imageOffset = 0x00;
  1079. unsigned int blockLength = 0x00;
  1080. unsigned int lengthOfBlock = 0x00;
  1081. unsigned int numTxPkts = 0;
  1082. unsigned int numRxPkts = 0;
  1083. unsigned char mode = 0;
  1084. unsigned char componentId = 0x00;
  1085. unsigned char componentIdByte = 0x00;
  1086. uint16_t max_rq_size;
  1087. /* Save component ID on which the upload is done */
  1088. componentIdByte = components.ComponentBits.byte;
  1089. while ((componentIdByte>>= 1) != 0) {
  1090. componentId++;
  1091. }
  1092. pFwupgCtx->componentId = componentId;
  1093. pVersionInfo = (VERSIONINFO *)&gVersionInfo[componentId];
  1094. pFwImage = (struct HpmfwupgFirmwareImage*)((*pImagePtr)
  1095. + sizeof(struct HpmfwupgActionRecord));
  1096. pDataInitial = ((unsigned char *)pFwImage
  1097. + sizeof(struct HpmfwupgFirmwareImage));
  1098. pData = pDataInitial;
  1099. /* Find max buffer length according the connection parameters */
  1100. max_rq_size = ipmi_intf_get_max_request_data_size(intf);
  1101. /* validate lower bound of max request size */
  1102. if (max_rq_size <= sizeof(struct HpmfwupgUploadFirmwareBlockReq)) {
  1103. lprintf(LOG_ERROR, "Maximum request size is too small to "
  1104. "send a upload request.");
  1105. return HPMFWUPG_ERROR;
  1106. }
  1107. bufLength = max_rq_size - sizeof(struct HpmfwupgUploadFirmwareBlockReq);
  1108. /* Get firmware length */
  1109. firmwareLength = pFwImage->length[0];
  1110. firmwareLength|= (pFwImage->length[1] << 8) & 0xff00;
  1111. firmwareLength|= (pFwImage->length[2] << 16) & 0xff0000;
  1112. firmwareLength|= (pFwImage->length[3] << 24) & 0xff000000;
  1113. mode = TARGET_VER | IMAGE_VER;
  1114. if (pVersionInfo->rollbackSupported) {
  1115. mode |= ROLLBACK_VER;
  1116. }
  1117. if ((option & DEBUG_MODE)) {
  1118. printf("\n\n Comp ID : %d [%-20s]\n",
  1119. pVersionInfo->componentId,
  1120. pFwImage->desc);
  1121. } else {
  1122. HpmDisplayVersion(mode, pVersionInfo, 0);
  1123. }
  1124. if ((1 << componentId) & pFwupgCtx->compUpdateMask.ComponentBits.byte) {
  1125. if (verbose) {
  1126. lprintf(LOG_NOTICE, "Do not skip %d",
  1127. componentId);
  1128. }
  1129. skip = FALSE;
  1130. }
  1131. if (!skip) {
  1132. HpmDisplayUpgrade(0,0,1,0);
  1133. /* Initialize parameters */
  1134. uploadCmd.req = malloc(max_rq_size);
  1135. if (!uploadCmd.req) {
  1136. lprintf(LOG_ERR, "ipmitool: malloc failure");
  1137. return HPMFWUPG_ERROR;
  1138. }
  1139. uploadCmd.req->blockNumber = 0;
  1140. /* Send Initiate Upgrade Action */
  1141. initUpgActionCmd.req.componentsMask = components;
  1142. if (option & COMPARE_MODE) {
  1143. /* Action is compare */
  1144. initUpgActionCmd.req.upgradeAction = HPMFWUPG_UPGRADE_ACTION_COMPARE;
  1145. } else {
  1146. /* Action is upgrade */
  1147. initUpgActionCmd.req.upgradeAction = HPMFWUPG_UPGRADE_ACTION_UPGRADE;
  1148. }
  1149. rc = HpmfwupgInitiateUpgradeAction(intf, &initUpgActionCmd, pFwupgCtx);
  1150. if (rc != HPMFWUPG_SUCCESS) {
  1151. skip = TRUE;
  1152. }
  1153. if ((pVersionInfo->coldResetRequired) && (!skip)) {
  1154. *pFlagColdReset = TRUE;
  1155. }
  1156. /* pDataInitial is the starting pointer of the image data */
  1157. /* pDataTemp is one which we will move across */
  1158. pData = pDataInitial;
  1159. pDataTemp = pDataInitial;
  1160. lengthOfBlock = firmwareLength;
  1161. totalSent = 0x00;
  1162. displayFWLength= firmwareLength;
  1163. time(&start);
  1164. while ((pData < (pDataTemp+lengthOfBlock)) && (rc == HPMFWUPG_SUCCESS)) {
  1165. if ((pData+bufLength) <= (pDataTemp+lengthOfBlock)) {
  1166. count = bufLength;
  1167. } else {
  1168. count = (unsigned short)((pDataTemp+lengthOfBlock) - pData);
  1169. }
  1170. memcpy(&uploadCmd.req->data, pData, count);
  1171. imageOffset = 0x00;
  1172. blockLength = 0x00;
  1173. numTxPkts++;
  1174. rc = HpmfwupgUploadFirmwareBlock(intf, &uploadCmd,
  1175. pFwupgCtx, count, &imageOffset,&blockLength);
  1176. numRxPkts++;
  1177. if (rc != HPMFWUPG_SUCCESS) {
  1178. if (rc == HPMFWUPG_UPLOAD_BLOCK_LENGTH && !bufLengthIsSet) {
  1179. rc = HPMFWUPG_SUCCESS;
  1180. /* Retry with a smaller buffer length */
  1181. if (strstr(intf->name,"lan") != NULL && bufLength > 8) {
  1182. bufLength-= 8;
  1183. lprintf(LOG_INFO,
  1184. "Trying reduced buffer length: %d",
  1185. bufLength);
  1186. } else if (bufLength) {
  1187. bufLength-= 1;
  1188. lprintf(LOG_INFO,
  1189. "Trying reduced buffer length: %d",
  1190. bufLength);
  1191. } else {
  1192. rc = HPMFWUPG_ERROR;
  1193. }
  1194. } else if (rc == HPMFWUPG_UPLOAD_RETRY) {
  1195. rc = HPMFWUPG_SUCCESS;
  1196. } else {
  1197. fflush(stdout);
  1198. lprintf(LOG_NOTICE,
  1199. "\n Error in Upload FIRMWARE command [rc=%d]\n",
  1200. rc);
  1201. lprintf(LOG_NOTICE,
  1202. "\n TotalSent:0x%x ",
  1203. totalSent);
  1204. /* Exiting from the function */
  1205. rc = HPMFWUPG_ERROR;
  1206. }
  1207. } else {
  1208. /* success, buf length is valid */
  1209. bufLengthIsSet = 1;
  1210. if (imageOffset + blockLength > firmwareLength ||
  1211. imageOffset + blockLength < blockLength) {
  1212. /*
  1213. * blockLength is the remaining length of the firmware to upload so
  1214. * if imageOffset and blockLength sum is greater than the firmware
  1215. * length then its kind of error
  1216. */
  1217. lprintf(LOG_NOTICE,
  1218. "\n Error in Upload FIRMWARE command [rc=%d]\n",
  1219. rc);
  1220. lprintf(LOG_NOTICE,
  1221. "\n TotalSent:0x%x Img offset:0x%x Blk length:0x%x Fwlen:0x%x\n",
  1222. totalSent,imageOffset,blockLength,firmwareLength);
  1223. rc = HPMFWUPG_ERROR;
  1224. continue;
  1225. }
  1226. totalSent += count;
  1227. if (imageOffset != 0x00) {
  1228. /* block Length is valid */
  1229. lengthOfBlock = blockLength;
  1230. pDataTemp = pDataInitial + imageOffset;
  1231. pData = pDataTemp;
  1232. if (displayFWLength == firmwareLength) {
  1233. /* This is basically used only to make sure that we display uptil 100% */
  1234. displayFWLength = blockLength + totalSent;
  1235. }
  1236. } else {
  1237. pData += count;
  1238. }
  1239. time(&end);
  1240. /*
  1241. * Just added debug mode in case we need to see exactly how many bytes have
  1242. * gone through - Its a hidden option used mainly should be used for debugging
  1243. */
  1244. if (option & DEBUG_MODE) {
  1245. fflush(stdout);
  1246. printf(" Blk Num : %02x Bytes : %05x ",
  1247. uploadCmd.req->blockNumber,totalSent);
  1248. if (imageOffset || blockLength) {
  1249. printf("\n--> ImgOff : %x BlkLen : %x\n",
  1250. imageOffset,blockLength);
  1251. }
  1252. if (displayFWLength == totalSent) {
  1253. printf("\n Time Taken %02ld:%02ld",
  1254. (end-start)/60, (end-start)%60);
  1255. printf("\n\n");
  1256. }
  1257. } else {
  1258. HpmDisplayUpgrade(0, totalSent,
  1259. displayFWLength, (end-start));
  1260. }
  1261. uploadCmd.req->blockNumber++;
  1262. }
  1263. }
  1264. /* free buffer */
  1265. free(uploadCmd.req);
  1266. uploadCmd.req = NULL;
  1267. }
  1268. if (skip) {
  1269. HpmDisplayUpgrade(1,0,0,0);
  1270. if ((option & COMPARE_MODE)
  1271. && !pFwupgCtx->genCompProp[pFwupgCtx->componentId].GeneralCompProperties.bitfield.comparisonSupport) {
  1272. printf("| |Comparison isn't supported for given compenent. |\n");
  1273. }
  1274. *pImagePtr = pDataInitial + firmwareLength;
  1275. }
  1276. if (rc == HPMFWUPG_SUCCESS && !skip) {
  1277. /* Send finish component */
  1278. /* Set image length */
  1279. finishCmd.req.componentId = componentId;
  1280. /* We need to send the actual data that is sent
  1281. * not the comlete firmware image length
  1282. */
  1283. finishCmd.req.imageLength[0] = totalSent & 0xFF;
  1284. finishCmd.req.imageLength[1] = (totalSent >> 8) & 0xFF;
  1285. finishCmd.req.imageLength[2] = (totalSent >> 16) & 0xFF;
  1286. finishCmd.req.imageLength[3] = (totalSent >> 24) & 0xFF;
  1287. rc = HpmfwupgFinishFirmwareUpload(intf, &finishCmd,
  1288. pFwupgCtx, option);
  1289. *pImagePtr = pDataInitial + firmwareLength;
  1290. }
  1291. return rc;
  1292. }
  1293. /* HpmfwupgActivationStage - validate stage of a firmware upgrade procedure as
  1294. * defined in section 3.4 of the IPM Controller Firmware Upgrade Specification
  1295. * version 1.0
  1296. */
  1297. int
  1298. HpmfwupgActivationStage(struct ipmi_intf *intf,
  1299. struct HpmfwupgUpgradeCtx *pFwupgCtx)
  1300. {
  1301. int rc = HPMFWUPG_SUCCESS;
  1302. struct HpmfwupgActivateFirmwareCtx activateCmd;
  1303. struct HpmfwupgImageHeader *pImageHeader = (struct HpmfwupgImageHeader*)
  1304. pFwupgCtx->pImageData;
  1305. /* Print out stuf...*/
  1306. printf(" ");
  1307. fflush(stdout);
  1308. /* Activate new firmware */
  1309. activateCmd.req.rollback_override = 0;
  1310. rc = HpmfwupgActivateFirmware(intf, &activateCmd, pFwupgCtx);
  1311. if (rc == HPMFWUPG_SUCCESS) {
  1312. /* Query self test result if supported by target and new image */
  1313. if ((pFwupgCtx->targetCap.GlobalCapabilities.bitField.ipmcSelftestCap == 1)
  1314. || (pImageHeader->imageCapabilities.bitField.imageSelfTest == 1)) {
  1315. struct HpmfwupgQuerySelftestResultCtx selfTestCmd;
  1316. rc = HpmfwupgQuerySelftestResult(intf, &selfTestCmd,
  1317. pFwupgCtx);
  1318. if (rc == HPMFWUPG_SUCCESS) {
  1319. /* Get the self test result */
  1320. if (selfTestCmd.resp.result1 != 0x55) {
  1321. /* Perform manual rollback if necessary */
  1322. /* BACKUP/ MANUAL ROLLBACK not supported by this UA */
  1323. lprintf(LOG_NOTICE, " Self test failed:");
  1324. lprintf(LOG_NOTICE, " Result1 = %x",
  1325. selfTestCmd.resp.result1);
  1326. lprintf(LOG_NOTICE, " Result2 = %x",
  1327. selfTestCmd.resp.result2);
  1328. rc = HPMFWUPG_ERROR;
  1329. }
  1330. } else {
  1331. /* Perform manual rollback if necessary */
  1332. /* BACKUP / MANUAL ROLLBACK not supported by this UA */
  1333. lprintf(LOG_NOTICE," Self test failed.");
  1334. }
  1335. }
  1336. }
  1337. /* If activation / self test failed, query rollback
  1338. * status if automatic rollback supported
  1339. */
  1340. if (rc == HPMFWUPG_ERROR) {
  1341. if ((pFwupgCtx->targetCap.GlobalCapabilities.bitField.autRollback == 1)
  1342. && (pFwupgCtx->genCompProp[pFwupgCtx->componentId].GeneralCompProperties.bitfield.rollbackBackup != 0x00)) {
  1343. struct HpmfwupgQueryRollbackStatusCtx rollCmd;
  1344. lprintf(LOG_NOTICE," Getting rollback status...");
  1345. fflush(stdout);
  1346. rc = HpmfwupgQueryRollbackStatus(intf,
  1347. &rollCmd, pFwupgCtx);
  1348. }
  1349. }
  1350. return rc;
  1351. }
  1352. int
  1353. HpmfwupgGetBufferFromFile(char *imageFilename,
  1354. struct HpmfwupgUpgradeCtx *pFwupgCtx)
  1355. {
  1356. int rc = HPMFWUPG_SUCCESS;
  1357. int ret = 0;
  1358. FILE *pImageFile = fopen(imageFilename, "rb");
  1359. if (pImageFile == NULL) {
  1360. lprintf(LOG_ERR, "Cannot open image file '%s'",
  1361. imageFilename);
  1362. return HPMFWUPG_ERROR;
  1363. }
  1364. /* Get the raw data in file */
  1365. ret = fseek(pImageFile, 0, SEEK_END);
  1366. if (ret != 0) {
  1367. lprintf(LOG_ERR, "Failed to seek in the image file '%s'",
  1368. imageFilename);
  1369. return HPMFWUPG_ERROR;
  1370. }
  1371. pFwupgCtx->imageSize = ftell(pImageFile);
  1372. pFwupgCtx->pImageData = malloc(sizeof(unsigned char)*pFwupgCtx->imageSize);
  1373. if (pFwupgCtx->pImageData == NULL) {
  1374. lprintf(LOG_ERR, "ipmitool: malloc failure");
  1375. fclose(pImageFile);
  1376. return HPMFWUPG_ERROR;
  1377. }
  1378. rewind(pImageFile);
  1379. ret = fread(pFwupgCtx->pImageData,
  1380. sizeof(unsigned char),
  1381. pFwupgCtx->imageSize,
  1382. pImageFile);
  1383. if (ret != pFwupgCtx->imageSize) {
  1384. lprintf(LOG_ERR,
  1385. "Failed to read file %s size %d",
  1386. imageFilename,
  1387. pFwupgCtx->imageSize);
  1388. rc = HPMFWUPG_ERROR;
  1389. }
  1390. fclose(pImageFile);
  1391. return rc;
  1392. }
  1393. int
  1394. HpmfwupgGetDeviceId(struct ipmi_intf *intf, struct ipm_devid_rsp *pGetDevId)
  1395. {
  1396. struct ipmi_rs *rsp;
  1397. struct ipmi_rq req;
  1398. memset(&req, 0, sizeof(req));
  1399. req.msg.netfn = IPMI_NETFN_APP;
  1400. req.msg.cmd = BMC_GET_DEVICE_ID;
  1401. req.msg.data_len = 0;
  1402. rsp = HpmfwupgSendCmd(intf, req, NULL);
  1403. if (rsp == NULL) {
  1404. lprintf(LOG_ERR, "Error getting device ID.");
  1405. return HPMFWUPG_ERROR;
  1406. }
  1407. if (rsp->ccode != 0x00) {
  1408. lprintf(LOG_ERR, "Error getting device ID.");
  1409. lprintf(LOG_ERR, "compcode=0x%x: %s",
  1410. rsp->ccode,
  1411. val2str(rsp->ccode, completion_code_vals));
  1412. return HPMFWUPG_ERROR;
  1413. }
  1414. memcpy(pGetDevId, rsp->data, sizeof(struct ipm_devid_rsp));
  1415. return HPMFWUPG_SUCCESS;
  1416. }
  1417. int
  1418. HpmfwupgGetTargetUpgCapabilities(struct ipmi_intf *intf,
  1419. struct HpmfwupgGetTargetUpgCapabilitiesCtx *pCtx)
  1420. {
  1421. struct ipmi_rs *rsp;
  1422. struct ipmi_rq req;
  1423. pCtx->req.picmgId = HPMFWUPG_PICMG_IDENTIFIER;
  1424. memset(&req, 0, sizeof(req));
  1425. req.msg.netfn = IPMI_NETFN_PICMG;
  1426. req.msg.cmd = HPMFWUPG_GET_TARGET_UPG_CAPABILITIES;
  1427. req.msg.data = (unsigned char*)&pCtx->req;
  1428. req.msg.data_len = sizeof(struct HpmfwupgGetTargetUpgCapabilitiesReq);
  1429. rsp = HpmfwupgSendCmd(intf, req, NULL);
  1430. if (rsp == NULL) {
  1431. lprintf(LOG_ERR,
  1432. "Error getting target upgrade capabilities.");
  1433. return HPMFWUPG_ERROR;
  1434. }
  1435. if (rsp->ccode != 0x00) {
  1436. lprintf(LOG_ERR,
  1437. "Error getting target upgrade capabilities, ccode: 0x%x: %s",
  1438. rsp->ccode,
  1439. val2str(rsp->ccode, completion_code_vals));
  1440. return HPMFWUPG_ERROR;
  1441. }
  1442. memcpy(&pCtx->resp, rsp->data,
  1443. sizeof(struct HpmfwupgGetTargetUpgCapabilitiesResp));
  1444. if (verbose) {
  1445. lprintf(LOG_NOTICE, "TARGET UPGRADE CAPABILITIES");
  1446. lprintf(LOG_NOTICE, "-------------------------------");
  1447. lprintf(LOG_NOTICE, "HPM.1 version............%d ",
  1448. pCtx->resp.hpmVersion);
  1449. lprintf(LOG_NOTICE, "Component 0 presence....[%c] ",
  1450. pCtx->resp.componentsPresent.ComponentBits.bitField.component0 ? 'y' : 'n');
  1451. lprintf(LOG_NOTICE, "Component 1 presence....[%c] ",
  1452. pCtx->resp.componentsPresent.ComponentBits.bitField.component1 ? 'y' : 'n');
  1453. lprintf(LOG_NOTICE, "Component 2 presence....[%c] ",
  1454. pCtx->resp.componentsPresent.ComponentBits.bitField.component2 ? 'y' : 'n');
  1455. lprintf(LOG_NOTICE, "Component 3 presence....[%c] ",
  1456. pCtx->resp.componentsPresent.ComponentBits.bitField.component3 ? 'y' : 'n');
  1457. lprintf(LOG_NOTICE, "Component 4 presence....[%c] ",
  1458. pCtx->resp.componentsPresent.ComponentBits.bitField.component4 ? 'y' : 'n');
  1459. lprintf(LOG_NOTICE, "Component 5 presence....[%c] ",
  1460. pCtx->resp.componentsPresent.ComponentBits.bitField.component5 ? 'y' : 'n');
  1461. lprintf(LOG_NOTICE, "Component 6 presence....[%c] ",
  1462. pCtx->resp.componentsPresent.ComponentBits.bitField.component6 ? 'y' : 'n');
  1463. lprintf(LOG_NOTICE, "Component 7 presence....[%c] ",
  1464. pCtx->resp.componentsPresent.ComponentBits.bitField.component7 ? 'y' : 'n');
  1465. lprintf(LOG_NOTICE, "Upgrade undesirable.....[%c] ",
  1466. pCtx->resp.GlobalCapabilities.bitField.fwUpgUndesirable ? 'y' : 'n');
  1467. lprintf(LOG_NOTICE, "Aut rollback override...[%c] ",
  1468. pCtx->resp.GlobalCapabilities.bitField.autRollbackOverride ? 'y' : 'n');
  1469. lprintf(LOG_NOTICE, "IPMC degraded...........[%c] ",
  1470. pCtx->resp.GlobalCapabilities.bitField.ipmcDegradedDurinUpg ? 'y' : 'n');
  1471. lprintf(LOG_NOTICE, "Defered activation......[%c] ",
  1472. pCtx->resp.GlobalCapabilities.bitField.deferActivation ? 'y' : 'n');
  1473. lprintf(LOG_NOTICE, "Service affected........[%c] ",
  1474. pCtx->resp.GlobalCapabilities.bitField.servAffectDuringUpg ? 'y' : 'n');
  1475. lprintf(LOG_NOTICE, "Manual rollback.........[%c] ",
  1476. pCtx->resp.GlobalCapabilities.bitField.manualRollback ? 'y' : 'n');
  1477. lprintf(LOG_NOTICE, "Automatic rollback......[%c] ",
  1478. pCtx->resp.GlobalCapabilities.bitField.autRollback ? 'y' : 'n');
  1479. lprintf(LOG_NOTICE, "Self test...............[%c] ",
  1480. pCtx->resp.GlobalCapabilities.bitField.ipmcSelftestCap ? 'y' : 'n');
  1481. lprintf(LOG_NOTICE, "Upgrade timeout.........[%d sec] ",
  1482. pCtx->resp.upgradeTimeout*5);
  1483. lprintf(LOG_NOTICE, "Self test timeout.......[%d sec] ",
  1484. pCtx->resp.selftestTimeout*5);
  1485. lprintf(LOG_NOTICE, "Rollback timeout........[%d sec] ",
  1486. pCtx->resp.rollbackTimeout*5);
  1487. lprintf(LOG_NOTICE, "Inaccessibility timeout.[%d sec] \n",
  1488. pCtx->resp.inaccessTimeout*5);
  1489. }
  1490. return HPMFWUPG_SUCCESS;
  1491. }
  1492. int
  1493. HpmfwupgGetComponentProperties(struct ipmi_intf *intf,
  1494. struct HpmfwupgGetComponentPropertiesCtx *pCtx)
  1495. {
  1496. int rc = HPMFWUPG_SUCCESS;
  1497. struct ipmi_rs * rsp;
  1498. struct ipmi_rq req;
  1499. pCtx->req.picmgId = HPMFWUPG_PICMG_IDENTIFIER;
  1500. memset(&req, 0, sizeof(req));
  1501. req.msg.netfn = IPMI_NETFN_PICMG;
  1502. req.msg.cmd = HPMFWUPG_GET_COMPONENT_PROPERTIES;
  1503. req.msg.data = (unsigned char*)&pCtx->req;
  1504. req.msg.data_len = sizeof(struct HpmfwupgGetComponentPropertiesReq);
  1505. rsp = HpmfwupgSendCmd(intf, req, NULL);
  1506. if (rsp == NULL) {
  1507. lprintf(LOG_NOTICE,
  1508. "Error getting component properties\n");
  1509. return HPMFWUPG_ERROR;
  1510. }
  1511. if (rsp->ccode != 0x00) {
  1512. lprintf(LOG_NOTICE,
  1513. "Error getting component properties");
  1514. lprintf(LOG_NOTICE,
  1515. "compcode=0x%x: %s",
  1516. rsp->ccode,
  1517. val2str(rsp->ccode, completion_code_vals));
  1518. return HPMFWUPG_ERROR;
  1519. }
  1520. switch (pCtx->req.selector) {
  1521. case HPMFWUPG_COMP_GEN_PROPERTIES:
  1522. memcpy(&pCtx->resp, rsp->data, sizeof(struct HpmfwupgGetGeneralPropResp));
  1523. if (verbose) {
  1524. lprintf(LOG_NOTICE, "GENERAL PROPERTIES");
  1525. lprintf(LOG_NOTICE, "-------------------------------");
  1526. lprintf(LOG_NOTICE, "Payload cold reset req....[%c] ",
  1527. pCtx->resp.Response.generalPropResp.GeneralCompProperties.bitfield.payloadColdReset ? 'y' : 'n');
  1528. lprintf(LOG_NOTICE, "Def. activation supported.[%c] ",
  1529. pCtx->resp.Response.generalPropResp.GeneralCompProperties.bitfield.deferredActivation ? 'y' : 'n');
  1530. lprintf(LOG_NOTICE, "Comparison supported......[%c] ",
  1531. pCtx->resp.Response.generalPropResp.GeneralCompProperties.bitfield.comparisonSupport ? 'y' : 'n');
  1532. lprintf(LOG_NOTICE, "Preparation supported.....[%c] ",
  1533. pCtx->resp.Response.generalPropResp.GeneralCompProperties.bitfield.preparationSupport ? 'y' : 'n');
  1534. lprintf(LOG_NOTICE, "Rollback supported........[%c] \n",
  1535. pCtx->resp.Response.generalPropResp.GeneralCompProperties.bitfield.rollbackBackup ? 'y' : 'n');
  1536. }
  1537. break;
  1538. case HPMFWUPG_COMP_CURRENT_VERSION:
  1539. memcpy(&pCtx->resp, rsp->data,
  1540. sizeof(struct HpmfwupgGetCurrentVersionResp));
  1541. if (verbose) {
  1542. lprintf(LOG_NOTICE, "Current Version: ");
  1543. lprintf(LOG_NOTICE, " Major: %d",
  1544. pCtx->resp.Response.currentVersionResp.currentVersion[0]);
  1545. lprintf(LOG_NOTICE, " Minor: %x",
  1546. pCtx->resp.Response.currentVersionResp.currentVersion[1]);
  1547. lprintf(LOG_NOTICE, " Aux : %03d %03d %03d %03d\n",
  1548. pCtx->resp.Response.currentVersionResp.currentVersion[2],
  1549. pCtx->resp.Response.currentVersionResp.currentVersion[3],
  1550. pCtx->resp.Response.currentVersionResp.currentVersion[4],
  1551. pCtx->resp.Response.currentVersionResp.currentVersion[5]);
  1552. }
  1553. break;
  1554. case HPMFWUPG_COMP_DESCRIPTION_STRING:
  1555. memcpy(&pCtx->resp, rsp->data,
  1556. sizeof(struct HpmfwupgGetDescStringResp));
  1557. if (verbose) {
  1558. char descString[HPMFWUPG_DESC_STRING_LENGTH + 1];
  1559. memcpy(descString,
  1560. pCtx->resp.Response.descStringResp.descString,
  1561. HPMFWUPG_DESC_STRING_LENGTH);
  1562. descString[HPMFWUPG_DESC_STRING_LENGTH] = '\0';
  1563. lprintf(LOG_NOTICE,
  1564. "Description string: %s\n",
  1565. descString);
  1566. }
  1567. break;
  1568. case HPMFWUPG_COMP_ROLLBACK_FIRMWARE_VERSION:
  1569. memcpy(&pCtx->resp, rsp->data, sizeof(struct HpmfwupgGetRollbackFwVersionResp));
  1570. if (verbose) {
  1571. lprintf(LOG_NOTICE, "Rollback FW Version: ");
  1572. lprintf(LOG_NOTICE, " Major: %d",
  1573. pCtx->resp.Response.rollbackFwVersionResp.rollbackFwVersion[0]);
  1574. lprintf(LOG_NOTICE, " Minor: %x",
  1575. pCtx->resp.Response.rollbackFwVersionResp.rollbackFwVersion[1]);
  1576. lprintf(LOG_NOTICE, " Aux : %03d %03d %03d %03d\n",
  1577. pCtx->resp.Response.rollbackFwVersionResp.rollbackFwVersion[2],
  1578. pCtx->resp.Response.rollbackFwVersionResp.rollbackFwVersion[3],
  1579. pCtx->resp.Response.rollbackFwVersionResp.rollbackFwVersion[4],
  1580. pCtx->resp.Response.rollbackFwVersionResp.rollbackFwVersion[5]);
  1581. }
  1582. break;
  1583. case HPMFWUPG_COMP_DEFERRED_FIRMWARE_VERSION:
  1584. memcpy(&pCtx->resp, rsp->data, sizeof(struct HpmfwupgGetDeferredFwVersionResp));
  1585. if (verbose) {
  1586. lprintf(LOG_NOTICE, "Deferred FW Version: ");
  1587. lprintf(LOG_NOTICE, " Major: %d",
  1588. pCtx->resp.Response.deferredFwVersionResp.deferredFwVersion[0]);
  1589. lprintf(LOG_NOTICE, " Minor: %x",
  1590. pCtx->resp.Response.deferredFwVersionResp.deferredFwVersion[1]);
  1591. lprintf(LOG_NOTICE, " Aux : %03d %03d %03d %03d\n",
  1592. pCtx->resp.Response.deferredFwVersionResp.deferredFwVersion[2],
  1593. pCtx->resp.Response.deferredFwVersionResp.deferredFwVersion[3],
  1594. pCtx->resp.Response.deferredFwVersionResp.deferredFwVersion[4],
  1595. pCtx->resp.Response.deferredFwVersionResp.deferredFwVersion[5]);
  1596. }
  1597. break;
  1598. case HPMFWUPG_COMP_OEM_PROPERTIES:
  1599. /* OEM Properties command */
  1600. memcpy(&pCtx->resp, rsp->data, sizeof(struct HpmfwupgGetOemProperties));
  1601. if (verbose) {
  1602. unsigned char i = 0;
  1603. lprintf(LOG_NOTICE,"OEM Properties: ");
  1604. for (i=0; i < HPMFWUPG_OEM_LENGTH; i++) {
  1605. lprintf(LOG_NOTICE, " 0x%x ",
  1606. pCtx->resp.Response.oemProperties.oemRspData[i]);
  1607. }
  1608. }
  1609. break;
  1610. default:
  1611. lprintf(LOG_NOTICE,"Unsupported component selector");
  1612. rc = HPMFWUPG_ERROR;
  1613. break;
  1614. }
  1615. return rc;
  1616. }
  1617. int
  1618. HpmfwupgAbortUpgrade(struct ipmi_intf *intf,
  1619. struct HpmfwupgAbortUpgradeCtx *pCtx)
  1620. {
  1621. int rc = HPMFWUPG_SUCCESS;
  1622. struct ipmi_rs *rsp;
  1623. struct ipmi_rq req;
  1624. pCtx->req.picmgId = HPMFWUPG_PICMG_IDENTIFIER;
  1625. memset(&req, 0, sizeof(req));
  1626. req.msg.netfn = IPMI_NETFN_PICMG;
  1627. req.msg.cmd = HPMFWUPG_ABORT_UPGRADE;
  1628. req.msg.data = (unsigned char*)&pCtx->req;
  1629. req.msg.data_len = sizeof(struct HpmfwupgAbortUpgradeReq);
  1630. rsp = HpmfwupgSendCmd(intf, req, NULL);
  1631. if (rsp == NULL) {
  1632. lprintf(LOG_ERR, "Error - aborting upgrade.");
  1633. return HPMFWUPG_ERROR;
  1634. }
  1635. if (rsp->ccode != 0x00) {
  1636. lprintf(LOG_ERR, "Error aborting upgrade");
  1637. lprintf(LOG_ERR, "compcode=0x%x: %s",
  1638. rsp->ccode,
  1639. val2str(rsp->ccode, completion_code_vals));
  1640. rc = HPMFWUPG_ERROR;
  1641. }
  1642. return rc;
  1643. }
  1644. int
  1645. HpmfwupgInitiateUpgradeAction(struct ipmi_intf *intf,
  1646. struct HpmfwupgInitiateUpgradeActionCtx *pCtx,
  1647. struct HpmfwupgUpgradeCtx *pFwupgCtx)
  1648. {
  1649. int rc = HPMFWUPG_SUCCESS;
  1650. struct ipmi_rs *rsp;
  1651. struct ipmi_rq req;
  1652. pCtx->req.picmgId = HPMFWUPG_PICMG_IDENTIFIER;
  1653. memset(&req, 0, sizeof(req));
  1654. req.msg.netfn = IPMI_NETFN_PICMG;
  1655. req.msg.cmd = HPMFWUPG_INITIATE_UPGRADE_ACTION;
  1656. req.msg.data = (unsigned char*)&pCtx->req;
  1657. req.msg.data_len = sizeof(struct HpmfwupgInitiateUpgradeActionReq);
  1658. rsp = HpmfwupgSendCmd(intf, req, pFwupgCtx);
  1659. if (rsp == NULL) {
  1660. lprintf(LOG_ERR, "Error initiating upgrade action.");
  1661. return HPMFWUPG_ERROR;
  1662. }
  1663. /* Long duration command handling */
  1664. if (rsp->ccode == HPMFWUPG_COMMAND_IN_PROGRESS) {
  1665. rc = HpmfwupgWaitLongDurationCmd(intf, pFwupgCtx);
  1666. } else if (rsp->ccode != 0x00) {
  1667. lprintf(LOG_NOTICE,"Error initiating upgrade action");
  1668. lprintf(LOG_NOTICE, "compcode=0x%x: %s",
  1669. rsp->ccode,
  1670. val2str(rsp->ccode, completion_code_vals));
  1671. rc = HPMFWUPG_ERROR;
  1672. }
  1673. return rc;
  1674. }
  1675. int
  1676. HpmfwupgUploadFirmwareBlock(struct ipmi_intf *intf,
  1677. struct HpmfwupgUploadFirmwareBlockCtx *pCtx,
  1678. struct HpmfwupgUpgradeCtx *pFwupgCtx, int count,
  1679. unsigned int *imageOffset, unsigned int *blockLength)
  1680. {
  1681. int rc = HPMFWUPG_SUCCESS;
  1682. struct ipmi_rs *rsp;
  1683. struct ipmi_rq req;
  1684. pCtx->req->picmgId = HPMFWUPG_PICMG_IDENTIFIER;
  1685. memset(&req, 0, sizeof(req));
  1686. req.msg.netfn = IPMI_NETFN_PICMG;
  1687. req.msg.cmd = HPMFWUPG_UPLOAD_FIRMWARE_BLOCK;
  1688. req.msg.data = (unsigned char *)pCtx->req;
  1689. /* 2 is the size of the upload struct - data */
  1690. req.msg.data_len = 2 + count;
  1691. rsp = HpmfwupgSendCmd(intf, req, pFwupgCtx);
  1692. if (rsp == NULL) {
  1693. lprintf(LOG_NOTICE, "Error uploading firmware block.");
  1694. return HPMFWUPG_ERROR;
  1695. }
  1696. if (rsp->ccode == HPMFWUPG_COMMAND_IN_PROGRESS
  1697. || rsp->ccode == 0x00) {
  1698. /*
  1699. * We need to check if the response also contains the next upload firmware offset
  1700. * and the firmware length in its response - These are optional but very vital
  1701. */
  1702. if (rsp->data_len > 1) {
  1703. /*
  1704. * If the response data length is greater than 1 it should contain both the
  1705. * the Section offset and section length. Because we cannot just have
  1706. * Section offset without section length so the length should be 9
  1707. */
  1708. if (rsp->data_len == 9) {
  1709. /* rsp->data[1] - LSB rsp->data[2] - rsp->data[3] = MSB */
  1710. *imageOffset = (rsp->data[4] << 24) + (rsp->data[3] << 16) + (rsp->data[2] << 8) + rsp->data[1];
  1711. *blockLength = (rsp->data[8] << 24) + (rsp->data[7] << 16) + (rsp->data[6] << 8) + rsp->data[5];
  1712. } else {
  1713. /*
  1714. * The Spec does not say much for this kind of errors where the
  1715. * firmware returned only offset and length so currently returning it
  1716. * as 0x82 - Internal CheckSum Error
  1717. */
  1718. lprintf(LOG_NOTICE,
  1719. "Error wrong rsp->datalen %d for Upload Firmware block command\n",
  1720. rsp->data_len);
  1721. rsp->ccode = HPMFWUPG_INT_CHECKSUM_ERROR;
  1722. }
  1723. }
  1724. }
  1725. /* Long duration command handling */
  1726. if (rsp->ccode == HPMFWUPG_COMMAND_IN_PROGRESS) {
  1727. rc = HpmfwupgWaitLongDurationCmd(intf, pFwupgCtx);
  1728. } else if (rsp->ccode != 0x00) {
  1729. /* PATCH --> This validation is to handle retryables errors codes on IPMB bus.
  1730. * This will be fixed in the next release of open ipmi and this
  1731. * check will have to be removed. (Buggy version = 39)
  1732. */
  1733. if (HPMFWUPG_IS_RETRYABLE(rsp->ccode)) {
  1734. lprintf(LOG_DEBUG, "HPM: [PATCH]Retryable error detected");
  1735. rc = HPMFWUPG_UPLOAD_RETRY;
  1736. } else if (rsp->ccode == IPMI_CC_REQ_DATA_INV_LENGTH ||
  1737. rsp->ccode == IPMI_CC_REQ_DATA_FIELD_EXCEED) {
  1738. /* If completion code = 0xc7(0xc8), we will retry with a reduced buffer length.
  1739. * Do not print error.
  1740. */
  1741. rc = HPMFWUPG_UPLOAD_BLOCK_LENGTH;
  1742. } else {
  1743. lprintf(LOG_ERR, "Error uploading firmware block");
  1744. lprintf(LOG_ERR, "compcode=0x%x: %s",
  1745. rsp->ccode,
  1746. val2str(rsp->ccode,
  1747. completion_code_vals));
  1748. rc = HPMFWUPG_ERROR;
  1749. }
  1750. }
  1751. return rc;
  1752. }
  1753. int
  1754. HpmfwupgFinishFirmwareUpload(struct ipmi_intf *intf,
  1755. struct HpmfwupgFinishFirmwareUploadCtx *pCtx,
  1756. struct HpmfwupgUpgradeCtx *pFwupgCtx, int option)
  1757. {
  1758. int rc = HPMFWUPG_SUCCESS;
  1759. struct ipmi_rs *rsp;
  1760. struct ipmi_rq req;
  1761. pCtx->req.picmgId = HPMFWUPG_PICMG_IDENTIFIER;
  1762. memset(&req, 0, sizeof(req));
  1763. req.msg.netfn = IPMI_NETFN_PICMG;
  1764. req.msg.cmd = HPMFWUPG_FINISH_FIRMWARE_UPLOAD;
  1765. req.msg.data = (unsigned char*)&pCtx->req;
  1766. req.msg.data_len = sizeof(struct HpmfwupgFinishFirmwareUploadReq);
  1767. rsp = HpmfwupgSendCmd(intf, req, pFwupgCtx);
  1768. if (rsp == NULL) {
  1769. lprintf(LOG_ERR, "Error fininshing firmware upload.");
  1770. return HPMFWUPG_ERROR;
  1771. }
  1772. /* Long duration command handling */
  1773. if (rsp->ccode == HPMFWUPG_COMMAND_IN_PROGRESS) {
  1774. rc = HpmfwupgWaitLongDurationCmd(intf, pFwupgCtx);
  1775. } else if ((option & COMPARE_MODE) && rsp->ccode == 0x83) {
  1776. printf("| |Component's active copy doesn't match the upgrade image |\n");
  1777. } else if ((option & COMPARE_MODE) && rsp->ccode == IPMI_CC_OK) {
  1778. printf("| |Comparison passed |\n");
  1779. } else if ( rsp->ccode != IPMI_CC_OK ) {
  1780. lprintf(LOG_ERR, "Error finishing firmware upload");
  1781. lprintf(LOG_ERR, "compcode=0x%x: %s",
  1782. rsp->ccode,
  1783. val2str(rsp->ccode, completion_code_vals));
  1784. rc = HPMFWUPG_ERROR;
  1785. }
  1786. return rc;
  1787. }
  1788. int
  1789. HpmfwupgActivateFirmware(struct ipmi_intf *intf,
  1790. struct HpmfwupgActivateFirmwareCtx *pCtx,
  1791. struct HpmfwupgUpgradeCtx *pFwupgCtx)
  1792. {
  1793. int rc = HPMFWUPG_SUCCESS;
  1794. struct ipmi_rs *rsp;
  1795. struct ipmi_rq req;
  1796. pCtx->req.picmgId = HPMFWUPG_PICMG_IDENTIFIER;
  1797. memset(&req, 0, sizeof(req));
  1798. req.msg.netfn = IPMI_NETFN_PICMG;
  1799. req.msg.cmd = HPMFWUPG_ACTIVATE_FIRMWARE;
  1800. req.msg.data = (unsigned char*)&pCtx->req;
  1801. req.msg.data_len = sizeof(struct HpmfwupgActivateFirmwareReq)
  1802. - (!pCtx->req.rollback_override ? 1 : 0);
  1803. rsp = HpmfwupgSendCmd(intf, req, pFwupgCtx);
  1804. if (rsp == NULL) {
  1805. lprintf(LOG_ERR, "Error activating firmware.");
  1806. return HPMFWUPG_ERROR;
  1807. }
  1808. /* Long duration command handling */
  1809. if (rsp->ccode == HPMFWUPG_COMMAND_IN_PROGRESS) {
  1810. printf("Waiting firmware activation...");
  1811. fflush(stdout);
  1812. rc = HpmfwupgWaitLongDurationCmd(intf, pFwupgCtx);
  1813. if (rc == HPMFWUPG_SUCCESS) {
  1814. lprintf(LOG_NOTICE, "OK");
  1815. } else {
  1816. lprintf(LOG_NOTICE, "Failed");
  1817. }
  1818. } else if (rsp->ccode != IPMI_CC_OK) {
  1819. lprintf(LOG_ERR, "Error activating firmware");
  1820. lprintf(LOG_ERR, "compcode=0x%x: %s",
  1821. rsp->ccode,
  1822. val2str(rsp->ccode, completion_code_vals));
  1823. rc = HPMFWUPG_ERROR;
  1824. }
  1825. return rc;
  1826. }
  1827. int
  1828. HpmfwupgGetUpgradeStatus(struct ipmi_intf *intf,
  1829. struct HpmfwupgGetUpgradeStatusCtx *pCtx,
  1830. struct HpmfwupgUpgradeCtx *pFwupgCtx,
  1831. int silent)
  1832. {
  1833. struct ipmi_rs *rsp;
  1834. struct ipmi_rq req;
  1835. pCtx->req.picmgId = HPMFWUPG_PICMG_IDENTIFIER;
  1836. memset(&req, 0, sizeof(req));
  1837. req.msg.netfn = IPMI_NETFN_PICMG;
  1838. req.msg.cmd = HPMFWUPG_GET_UPGRADE_STATUS;
  1839. req.msg.data = (unsigned char*)&pCtx->req;
  1840. req.msg.data_len = sizeof(struct HpmfwupgGetUpgradeStatusReq);
  1841. rsp = HpmfwupgSendCmd(intf, req, pFwupgCtx);
  1842. if (!rsp) {
  1843. lprintf(LOG_NOTICE,
  1844. "Error getting upgrade status. Failed to get response.");
  1845. return HPMFWUPG_ERROR;
  1846. }
  1847. if (rsp->ccode == 0x00) {
  1848. memcpy(&pCtx->resp, rsp->data,
  1849. sizeof(struct HpmfwupgGetUpgradeStatusResp));
  1850. if (!silent) {
  1851. lprintf(LOG_NOTICE, "Upgrade status:");
  1852. lprintf(LOG_NOTICE,
  1853. " Command in progress: %x",
  1854. pCtx->resp.cmdInProcess);
  1855. lprintf(LOG_NOTICE,
  1856. " Last command completion code: %x",
  1857. pCtx->resp.lastCmdCompCode);
  1858. }
  1859. } else if (HPMFWUPG_IS_RETRYABLE(rsp->ccode)) {
  1860. /* PATCH --> This validation is to handle retryable errors
  1861. * codes on the IPMB bus.
  1862. * This will be fixed in the next release of
  1863. * open ipmi and this check can be removed.
  1864. * (Buggy version = 39)
  1865. */
  1866. if (!silent) {
  1867. lprintf(LOG_DEBUG, "HPM: Retryable error detected");
  1868. }
  1869. pCtx->resp.lastCmdCompCode = HPMFWUPG_COMMAND_IN_PROGRESS;
  1870. } else {
  1871. lprintf(LOG_NOTICE, "Error getting upgrade status");
  1872. lprintf(LOG_NOTICE, "compcode=0x%x: %s", rsp->ccode,
  1873. val2str(rsp->ccode, completion_code_vals));
  1874. return HPMFWUPG_ERROR;
  1875. }
  1876. return HPMFWUPG_SUCCESS;
  1877. }
  1878. int
  1879. HpmfwupgManualFirmwareRollback(struct ipmi_intf *intf,
  1880. struct HpmfwupgManualFirmwareRollbackCtx *pCtx)
  1881. {
  1882. struct HpmfwupgUpgradeCtx fwupgCtx;
  1883. struct HpmfwupgGetTargetUpgCapabilitiesCtx targetCapCmd;
  1884. int rc = HPMFWUPG_SUCCESS;
  1885. struct ipmi_rs *rsp;
  1886. struct ipmi_rq req;
  1887. /* prepare fake upgrade context */
  1888. memset(&fwupgCtx, 0, sizeof (fwupgCtx));
  1889. verbose--;
  1890. rc = HpmfwupgGetTargetUpgCapabilities(intf, &targetCapCmd);
  1891. verbose++;
  1892. if (rc != HPMFWUPG_SUCCESS) {
  1893. return rc;
  1894. }
  1895. memcpy(&fwupgCtx.targetCap, &targetCapCmd.resp, sizeof(targetCapCmd.resp));
  1896. pCtx->req.picmgId = HPMFWUPG_PICMG_IDENTIFIER;
  1897. memset(&req, 0, sizeof(req));
  1898. req.msg.netfn = IPMI_NETFN_PICMG;
  1899. req.msg.cmd = HPMFWUPG_MANUAL_FIRMWARE_ROLLBACK;
  1900. req.msg.data = (unsigned char*)&pCtx->req;
  1901. req.msg.data_len = sizeof(struct HpmfwupgManualFirmwareRollbackReq);
  1902. rsp = HpmfwupgSendCmd(intf, req, &fwupgCtx);
  1903. if (rsp == NULL) {
  1904. lprintf(LOG_ERR, "Error sending manual rollback.");
  1905. return HPMFWUPG_ERROR;
  1906. }
  1907. /* Long duration command handling */
  1908. if (rsp->ccode == IPMI_CC_OK
  1909. || rsp->ccode == HPMFWUPG_COMMAND_IN_PROGRESS) {
  1910. struct HpmfwupgQueryRollbackStatusCtx resCmd;
  1911. printf("Waiting firmware rollback...");
  1912. fflush(stdout);
  1913. rc = HpmfwupgQueryRollbackStatus(intf, &resCmd, &fwupgCtx);
  1914. } else if ( rsp->ccode != 0x00 ) {
  1915. lprintf(LOG_ERR, "Error sending manual rollback");
  1916. lprintf(LOG_ERR, "compcode=0x%x: %s",
  1917. rsp->ccode,
  1918. val2str(rsp->ccode, completion_code_vals));
  1919. rc = HPMFWUPG_ERROR;
  1920. }
  1921. return rc;
  1922. }
  1923. int
  1924. HpmfwupgQueryRollbackStatus(struct ipmi_intf *intf,
  1925. struct HpmfwupgQueryRollbackStatusCtx *pCtx,
  1926. struct HpmfwupgUpgradeCtx *pFwupgCtx)
  1927. {
  1928. int rc = HPMFWUPG_SUCCESS;
  1929. struct ipmi_rs *rsp;
  1930. struct ipmi_rq req;
  1931. unsigned int rollbackTimeout = 0;
  1932. unsigned int timeoutSec1, timeoutSec2;
  1933. pCtx->req.picmgId = HPMFWUPG_PICMG_IDENTIFIER;
  1934. memset(&req, 0, sizeof(req));
  1935. req.msg.netfn = IPMI_NETFN_PICMG;
  1936. req.msg.cmd = HPMFWUPG_QUERY_ROLLBACK_STATUS;
  1937. req.msg.data = (unsigned char*)&pCtx->req;
  1938. req.msg.data_len = sizeof(struct HpmfwupgQueryRollbackStatusReq);
  1939. /* If we are not in upgrade context, we use default timeout values */
  1940. if (pFwupgCtx != NULL) {
  1941. struct HpmfwupgImageHeader *pImageHeader;
  1942. if (pFwupgCtx->pImageData) {
  1943. pImageHeader = (struct HpmfwupgImageHeader*)pFwupgCtx->pImageData;
  1944. rollbackTimeout = pImageHeader->rollbackTimeout;
  1945. } else {
  1946. rollbackTimeout = 0;
  1947. }
  1948. /* Use the greater of the two timeouts (header and target caps) */
  1949. rollbackTimeout = MAX(rollbackTimeout,
  1950. pFwupgCtx->targetCap.rollbackTimeout) * 5;
  1951. } else {
  1952. rollbackTimeout = HPMFWUPG_DEFAULT_UPGRADE_TIMEOUT;
  1953. }
  1954. /* Poll rollback status until completion or timeout */
  1955. timeoutSec1 = time(NULL);
  1956. timeoutSec2 = time(NULL);
  1957. do {
  1958. /* Must wait at least 100 ms between status requests */
  1959. usleep(100000);
  1960. rsp = HpmfwupgSendCmd(intf, req, pFwupgCtx);
  1961. /* PATCH --> This validation is to handle retryables errors codes on IPMB bus.
  1962. * This will be fixed in the next release of open ipmi and this
  1963. * check will have to be removed. (Buggy version = 39)
  1964. */
  1965. if (rsp) {
  1966. if (HPMFWUPG_IS_RETRYABLE(rsp->ccode)) {
  1967. lprintf(LOG_DEBUG,"HPM: [PATCH]Retryable error detected");
  1968. rsp->ccode = HPMFWUPG_COMMAND_IN_PROGRESS;
  1969. }
  1970. }
  1971. timeoutSec2 = time(NULL);
  1972. } while (rsp
  1973. && ((rsp->ccode == HPMFWUPG_COMMAND_IN_PROGRESS)
  1974. || (rsp->ccode == IPMI_CC_TIMEOUT))
  1975. && (timeoutSec2 - timeoutSec1 < rollbackTimeout));
  1976. if (rsp == NULL) {
  1977. lprintf(LOG_ERR, "Error getting upgrade status.");
  1978. return HPMFWUPG_ERROR;
  1979. }
  1980. if (rsp->ccode == 0x00) {
  1981. memcpy(&pCtx->resp, rsp->data,
  1982. sizeof(struct HpmfwupgQueryRollbackStatusResp));
  1983. if (pCtx->resp.rollbackComp.ComponentBits.byte != 0) {
  1984. /* Rollback occurred */
  1985. lprintf(LOG_NOTICE,
  1986. "Rollback occurred on component mask: 0x%02x",
  1987. pCtx->resp.rollbackComp.ComponentBits.byte);
  1988. } else {
  1989. lprintf(LOG_NOTICE,
  1990. "No Firmware rollback occurred");
  1991. }
  1992. } else if (rsp->ccode == 0x81) {
  1993. lprintf(LOG_ERR,
  1994. "Rollback failed on component mask: 0x%02x",
  1995. pCtx->resp.rollbackComp.ComponentBits.byte);
  1996. rc = HPMFWUPG_ERROR;
  1997. } else {
  1998. lprintf(LOG_ERR,
  1999. "Error getting rollback status");
  2000. lprintf(LOG_ERR,
  2001. "compcode=0x%x: %s",
  2002. rsp->ccode,
  2003. val2str(rsp->ccode, completion_code_vals));
  2004. rc = HPMFWUPG_ERROR;
  2005. }
  2006. return rc;
  2007. }
  2008. int
  2009. HpmfwupgQuerySelftestResult(struct ipmi_intf *intf, struct HpmfwupgQuerySelftestResultCtx *pCtx,
  2010. struct HpmfwupgUpgradeCtx *pFwupgCtx)
  2011. {
  2012. int rc = HPMFWUPG_SUCCESS;
  2013. struct ipmi_rs *rsp;
  2014. struct ipmi_rq req;
  2015. unsigned char selfTestTimeout = 0;
  2016. unsigned int timeoutSec1, timeoutSec2;
  2017. pCtx->req.picmgId = HPMFWUPG_PICMG_IDENTIFIER;
  2018. /* If we are not in upgrade context, we use default timeout values */
  2019. if (pFwupgCtx != NULL) {
  2020. /* Getting selftest timeout from new image */
  2021. struct HpmfwupgImageHeader *pImageHeader = (struct HpmfwupgImageHeader*)
  2022. pFwupgCtx->pImageData;
  2023. selfTestTimeout = MAX(pImageHeader->selfTestTimeout,
  2024. pFwupgCtx->targetCap.selftestTimeout) * 5;
  2025. } else {
  2026. selfTestTimeout = HPMFWUPG_DEFAULT_UPGRADE_TIMEOUT;
  2027. }
  2028. memset(&req, 0, sizeof(req));
  2029. req.msg.netfn = IPMI_NETFN_PICMG;
  2030. req.msg.cmd = HPMFWUPG_QUERY_SELFTEST_RESULT;
  2031. req.msg.data = (unsigned char*)&pCtx->req;
  2032. req.msg.data_len = sizeof(struct HpmfwupgQuerySelftestResultReq);
  2033. /* Poll rollback status until completion or timeout */
  2034. timeoutSec1 = time(NULL);
  2035. timeoutSec2 = time(NULL);
  2036. do {
  2037. /* Must wait at least 100 ms between status requests */
  2038. usleep(100000);
  2039. rsp = HpmfwupgSendCmd(intf, req, pFwupgCtx);
  2040. /* PATCH --> This validation is to handle retryables errors codes on IPMB bus.
  2041. * This will be fixed in the next release of open ipmi and this
  2042. * check will have to be removed. (Buggy version = 39)
  2043. */
  2044. if (rsp) {
  2045. if (HPMFWUPG_IS_RETRYABLE(rsp->ccode)) {
  2046. lprintf(LOG_DEBUG,
  2047. "HPM: [PATCH]Retryable error detected");
  2048. rsp->ccode = HPMFWUPG_COMMAND_IN_PROGRESS;
  2049. }
  2050. }
  2051. timeoutSec2 = time(NULL);
  2052. } while (rsp
  2053. && (rsp->ccode == HPMFWUPG_COMMAND_IN_PROGRESS)
  2054. && (timeoutSec2 - timeoutSec1 < selfTestTimeout));
  2055. if (rsp == NULL) {
  2056. lprintf(LOG_NOTICE, "Error getting upgrade status\n");
  2057. return HPMFWUPG_ERROR;
  2058. }
  2059. if (rsp->ccode == 0x00) {
  2060. memcpy(&pCtx->resp, rsp->data,
  2061. sizeof(struct HpmfwupgQuerySelftestResultResp));
  2062. if (verbose) {
  2063. lprintf(LOG_NOTICE, "Self test results:");
  2064. lprintf(LOG_NOTICE, "Result1 = %x",
  2065. pCtx->resp.result1);
  2066. lprintf(LOG_NOTICE, "Result2 = %x",
  2067. pCtx->resp.result2);
  2068. }
  2069. } else {
  2070. lprintf(LOG_NOTICE, "Error getting self test results");
  2071. lprintf(LOG_NOTICE, "compcode=0x%x: %s",
  2072. rsp->ccode,
  2073. val2str(rsp->ccode, completion_code_vals));
  2074. rc = HPMFWUPG_ERROR;
  2075. }
  2076. return rc;
  2077. }
  2078. struct ipmi_rs *
  2079. HpmfwupgSendCmd(struct ipmi_intf *intf, struct ipmi_rq req,
  2080. struct HpmfwupgUpgradeCtx *pFwupgCtx)
  2081. {
  2082. struct ipmi_rs *rsp;
  2083. unsigned int inaccessTimeout = 0, inaccessTimeoutCounter = 0;
  2084. unsigned int upgradeTimeout = 0, upgradeTimeoutCounter = 0;
  2085. unsigned int timeoutSec1, timeoutSec2;
  2086. unsigned char retry = 0;
  2087. /* If we are not in upgrade context, we use default timeout values */
  2088. if (pFwupgCtx != NULL) {
  2089. inaccessTimeout = pFwupgCtx->targetCap.inaccessTimeout*5;
  2090. upgradeTimeout = pFwupgCtx->targetCap.upgradeTimeout*5;
  2091. } else {
  2092. /* keeping the inaccessTimeout to 60 seconds results in almost 2900 retries
  2093. * So if the target is not available it will be retrying the command for 2900
  2094. * times which is not effecient -So reducing the Timout to 5 seconds which is
  2095. * almost 200 retries if it continuously recieves 0xC3 as completion code.
  2096. */
  2097. inaccessTimeout = HPMFWUPG_DEFAULT_UPGRADE_TIMEOUT;
  2098. upgradeTimeout = HPMFWUPG_DEFAULT_UPGRADE_TIMEOUT;
  2099. }
  2100. timeoutSec1 = time(NULL);
  2101. do {
  2102. static unsigned char isValidSize = FALSE;
  2103. rsp = intf->sendrecv(intf, &req);
  2104. if (rsp == NULL) {
  2105. #define HPM_LAN_PACKET_RESIZE_LIMIT 6
  2106. /* also covers lanplus */
  2107. if (strstr(intf->name, "lan") != NULL) {
  2108. static int errorCount=0;
  2109. static struct ipmi_rs fakeRsp;
  2110. lprintf(LOG_DEBUG,
  2111. "HPM: no response available");
  2112. lprintf(LOG_DEBUG,
  2113. "HPM: the command may be rejected for security reasons");
  2114. if (req.msg.netfn == IPMI_NETFN_PICMG
  2115. && req.msg.cmd == HPMFWUPG_UPLOAD_FIRMWARE_BLOCK
  2116. && errorCount < HPM_LAN_PACKET_RESIZE_LIMIT
  2117. && (!isValidSize)) {
  2118. lprintf(LOG_DEBUG,
  2119. "HPM: upload firmware block API called");
  2120. lprintf(LOG_DEBUG,
  2121. "HPM: returning length error to force resize");
  2122. fakeRsp.ccode = IPMI_CC_REQ_DATA_INV_LENGTH;
  2123. rsp = &fakeRsp;
  2124. errorCount++;
  2125. } else if (req.msg.netfn == IPMI_NETFN_PICMG
  2126. && (req.msg.cmd == HPMFWUPG_ACTIVATE_FIRMWARE
  2127. || req.msg.cmd == HPMFWUPG_MANUAL_FIRMWARE_ROLLBACK)) {
  2128. /*
  2129. * rsp == NULL and command activate firmware or manual firmware
  2130. * rollback most likely occurs when we have sent a firmware activation
  2131. * request. Fake a command in progress response.
  2132. */
  2133. lprintf(LOG_DEBUG,
  2134. "HPM: activate/rollback firmware API called");
  2135. lprintf(LOG_DEBUG,
  2136. "HPM: returning in progress to handle IOL session lost");
  2137. fakeRsp.ccode = HPMFWUPG_COMMAND_IN_PROGRESS;
  2138. rsp = &fakeRsp;
  2139. } else if (req.msg.netfn == IPMI_NETFN_PICMG
  2140. && (req.msg.cmd == HPMFWUPG_QUERY_ROLLBACK_STATUS
  2141. || req.msg.cmd == HPMFWUPG_GET_UPGRADE_STATUS
  2142. || req.msg.cmd == HPMFWUPG_QUERY_SELFTEST_RESULT)
  2143. && ( !intf->target_addr || intf->target_addr == intf->my_addr)) {
  2144. /* reopen session only if target IPMC is directly accessed */
  2145. /*
  2146. * rsp == NULL and command get upgrade status or query rollback
  2147. * status most likely occurs when we are waiting for firmware
  2148. * activation. Try to re-open the IOL session (re-open will work
  2149. * once the IPMC recovers from firmware activation.
  2150. */
  2151. lprintf(LOG_DEBUG, "HPM: upg/rollback status firmware API called");
  2152. lprintf(LOG_DEBUG, "HPM: try to re-open IOL session");
  2153. {
  2154. /* force session re-open */
  2155. intf->abort = 1;
  2156. intf->close(intf);
  2157. while (intf->open(intf) == HPMFWUPG_ERROR
  2158. && inaccessTimeoutCounter < inaccessTimeout) {
  2159. inaccessTimeoutCounter += (time(NULL) - timeoutSec1);
  2160. timeoutSec1 = time(NULL);
  2161. }
  2162. /* Fake timeout to retry command */
  2163. fakeRsp.ccode = 0xc3;
  2164. rsp = &fakeRsp;
  2165. }
  2166. }
  2167. }
  2168. }
  2169. /* Handle inaccessibility timeout (rsp = NULL if IOL) */
  2170. if (rsp == NULL || rsp->ccode == 0xff || rsp->ccode == 0xc3 || rsp->ccode == 0xd3) {
  2171. if (inaccessTimeoutCounter < inaccessTimeout) {
  2172. timeoutSec2 = time(NULL);
  2173. if (timeoutSec2 > timeoutSec1) {
  2174. inaccessTimeoutCounter += timeoutSec2 - timeoutSec1;
  2175. timeoutSec1 = time(NULL);
  2176. }
  2177. usleep(100000);
  2178. retry = 1;
  2179. } else {
  2180. retry = 0;
  2181. }
  2182. } else if ( rsp->ccode == 0xc0 ) {
  2183. /* Handle node busy timeout */
  2184. if (upgradeTimeoutCounter < upgradeTimeout) {
  2185. timeoutSec2 = time(NULL);
  2186. if (timeoutSec2 > timeoutSec1) {
  2187. timeoutSec1 = time(NULL);
  2188. upgradeTimeoutCounter += timeoutSec2 - timeoutSec1;
  2189. }
  2190. usleep(100000);
  2191. retry = 1;
  2192. } else {
  2193. retry = 0;
  2194. }
  2195. } else {
  2196. # ifdef ENABLE_OPENIPMI_V39_PATCH
  2197. if (rsp->ccode == IPMI_CC_OK) {
  2198. errorCount = 0 ;
  2199. }
  2200. # endif
  2201. retry = 0;
  2202. if (req.msg.netfn == IPMI_NETFN_PICMG
  2203. && req.msg.cmd == HPMFWUPG_UPLOAD_FIRMWARE_BLOCK
  2204. && (!isValidSize)) {
  2205. lprintf(LOG_INFO,
  2206. "Buffer length is now considered valid");
  2207. isValidSize = TRUE;
  2208. }
  2209. }
  2210. } while (retry);
  2211. return rsp;
  2212. }
  2213. int
  2214. HpmfwupgWaitLongDurationCmd(struct ipmi_intf *intf,
  2215. struct HpmfwupgUpgradeCtx *pFwupgCtx)
  2216. {
  2217. int rc = HPMFWUPG_SUCCESS;
  2218. unsigned int upgradeTimeout = 0;
  2219. unsigned int timeoutSec1, timeoutSec2;
  2220. struct HpmfwupgGetUpgradeStatusCtx upgStatusCmd;
  2221. /* If we are not in upgrade context, we use default timeout values */
  2222. if (pFwupgCtx != NULL) {
  2223. upgradeTimeout = (unsigned int)(pFwupgCtx->targetCap.upgradeTimeout*5);
  2224. if (verbose) {
  2225. printf("Use File Upgrade Capabilities: %i seconds\n",
  2226. upgradeTimeout);
  2227. }
  2228. } else {
  2229. /* Try to retreive from Caps */
  2230. struct HpmfwupgGetTargetUpgCapabilitiesCtx targetCapCmd;
  2231. if(HpmfwupgGetTargetUpgCapabilities(intf, &targetCapCmd) != HPMFWUPG_SUCCESS) {
  2232. upgradeTimeout = HPMFWUPG_DEFAULT_UPGRADE_TIMEOUT;
  2233. if (verbose) {
  2234. printf("Use default timeout: %i seconds\n",
  2235. upgradeTimeout);
  2236. }
  2237. } else {
  2238. upgradeTimeout = (unsigned int)(targetCapCmd.resp.upgradeTimeout * 5);
  2239. if (verbose) {
  2240. printf("Use Command Upgrade Capabilities Timeout: %i seconds\n",
  2241. upgradeTimeout);
  2242. }
  2243. }
  2244. }
  2245. if (rc == HPMFWUPG_SUCCESS) {
  2246. /* Poll upgrade status until completion or timeout*/
  2247. timeoutSec1 = time(NULL);
  2248. timeoutSec2 = time(NULL);
  2249. rc = HpmfwupgGetUpgradeStatus(intf, &upgStatusCmd,
  2250. pFwupgCtx, 1);
  2251. }
  2252. while (
  2253. /* With KCS: Cover the case where we sometime
  2254. * receive d5 (on the first get status) from
  2255. * the ipmi driver.
  2256. */
  2257. (upgStatusCmd.resp.lastCmdCompCode == 0x80 ||
  2258. upgStatusCmd.resp.lastCmdCompCode == 0xD5)
  2259. && ((timeoutSec2 - timeoutSec1) < upgradeTimeout )
  2260. && (rc == HPMFWUPG_SUCCESS)) {
  2261. /* Must wait at least 1000 ms between status requests */
  2262. usleep(1000000);
  2263. timeoutSec2 = time(NULL);
  2264. rc = HpmfwupgGetUpgradeStatus(intf, &upgStatusCmd, pFwupgCtx, 1);
  2265. /*
  2266. * printf("Get Status: %x - %x = %x _ %x [%x]\n",
  2267. ( timeoutSec2, timeoutSec1,
  2268. * (timeoutSec2 - timeoutSec1),
  2269. * upgradeTimeout, rc);
  2270. */
  2271. }
  2272. if (upgStatusCmd.resp.lastCmdCompCode != 0x00) {
  2273. if (verbose) {
  2274. lprintf(LOG_NOTICE,
  2275. "Error waiting for command %x, compcode = %x",
  2276. upgStatusCmd.resp.cmdInProcess,
  2277. upgStatusCmd.resp.lastCmdCompCode);
  2278. }
  2279. rc = HPMFWUPG_ERROR;
  2280. }
  2281. return rc;
  2282. }
  2283. unsigned char
  2284. HpmfwupgCalculateChecksum(unsigned char *pData, unsigned int length)
  2285. {
  2286. unsigned char checksum = 0;
  2287. int dataIdx = 0;
  2288. for (dataIdx = 0; dataIdx < length; dataIdx++) {
  2289. checksum += pData[dataIdx];
  2290. }
  2291. return checksum;
  2292. }
  2293. void
  2294. HpmfwupgPrintUsage(void)
  2295. {
  2296. lprintf(LOG_NOTICE,
  2297. "help - This help menu.");
  2298. lprintf(LOG_NOTICE,
  2299. "");
  2300. lprintf(LOG_NOTICE,
  2301. "check - Check the target information.");
  2302. lprintf(LOG_NOTICE,
  2303. "check <file> - If the user is unsure of what update is going to be ");
  2304. lprintf(LOG_NOTICE,
  2305. " This will display the existing target version and");
  2306. lprintf(LOG_NOTICE,
  2307. " image version on the screen");
  2308. lprintf(LOG_NOTICE,
  2309. "");
  2310. lprintf(LOG_NOTICE,
  2311. "upgrade <file> [component x...] [force] [activate]");
  2312. lprintf(LOG_NOTICE,
  2313. " - Copies components from a valid HPM.1 image to the target.");
  2314. lprintf(LOG_NOTICE,
  2315. " If one or more components specified by \"component\",");
  2316. lprintf(LOG_NOTICE,
  2317. " only the specified components are copied.");
  2318. lprintf(LOG_NOTICE,
  2319. " Otherwise, all the image components are copied.");
  2320. lprintf(LOG_NOTICE,
  2321. " Before copy, each image component undergoes a version check");
  2322. lprintf(LOG_NOTICE,
  2323. " and can be skipped if the target component version");
  2324. lprintf(LOG_NOTICE,
  2325. " is the same or more recent.");
  2326. lprintf(LOG_NOTICE,
  2327. " Use \"force\" to bypass the version check results.");
  2328. lprintf(LOG_NOTICE,
  2329. " Make sure to check the versions first using the");
  2330. lprintf(LOG_NOTICE,
  2331. " \"check <file>\" command.");
  2332. lprintf(LOG_NOTICE,
  2333. " If \"activate\" is specified, the newly uploaded firmware");
  2334. lprintf(LOG_NOTICE,
  2335. " is activated.");
  2336. lprintf(LOG_NOTICE,
  2337. "upgstatus - Returns the status of the last long duration command.");
  2338. lprintf(LOG_NOTICE,
  2339. "");
  2340. lprintf(LOG_NOTICE,
  2341. "compare <file> - Perform \"Comparison of the Active Copy\" action for all the");
  2342. lprintf(LOG_NOTICE,
  2343. " components present in the file.");
  2344. lprintf(LOG_NOTICE,
  2345. "compare <file> component x - Compare only component <x> from the given <file>");
  2346. lprintf(LOG_NOTICE,
  2347. "activate - Activate the newly uploaded firmware.");
  2348. lprintf(LOG_NOTICE,
  2349. "activate norollback - Activate the newly uploaded firmware but inform");
  2350. lprintf(LOG_NOTICE,
  2351. " the target to not automatically rollback if ");
  2352. lprintf(LOG_NOTICE,
  2353. " the upgrade fails.");
  2354. lprintf(LOG_NOTICE,
  2355. "");
  2356. lprintf(LOG_NOTICE,
  2357. "targetcap - Get the target upgrade capabilities.");
  2358. lprintf(LOG_NOTICE,
  2359. "");
  2360. lprintf(LOG_NOTICE,
  2361. "compprop <id> <prop> - Get specified component properties from the target.");
  2362. lprintf(LOG_NOTICE,
  2363. " Valid component <id>: 0-7 ");
  2364. lprintf(LOG_NOTICE,
  2365. " Properties <prop> can be one of the following: ");
  2366. lprintf(LOG_NOTICE,
  2367. " 0- General properties");
  2368. lprintf(LOG_NOTICE,
  2369. " 1- Current firmware version");
  2370. lprintf(LOG_NOTICE,
  2371. " 2- Description string");
  2372. lprintf(LOG_NOTICE,
  2373. " 3- Rollback firmware version");
  2374. lprintf(LOG_NOTICE,
  2375. " 4- Deferred firmware version");
  2376. lprintf(LOG_NOTICE,
  2377. "");
  2378. lprintf(LOG_NOTICE,
  2379. "abort - Abort the on-going firmware upgrade.");
  2380. lprintf(LOG_NOTICE,
  2381. "");
  2382. lprintf(LOG_NOTICE,
  2383. "rollback - Performs a manual rollback on the IPM Controller.");
  2384. lprintf(LOG_NOTICE,
  2385. " firmware");
  2386. lprintf(LOG_NOTICE,
  2387. "rollbackstatus - Query the rollback status.");
  2388. lprintf(LOG_NOTICE,
  2389. "");
  2390. lprintf(LOG_NOTICE,
  2391. "selftestresult - Query the self test results.\n");
  2392. }
  2393. int
  2394. ipmi_hpmfwupg_main(struct ipmi_intf *intf, int argc, char **argv)
  2395. {
  2396. int rc = HPMFWUPG_SUCCESS;
  2397. int activateFlag = 0x00;
  2398. int componentMask = 0;
  2399. int componentId = 0;
  2400. int option = 0;
  2401. lprintf(LOG_DEBUG,"ipmi_hpmfwupg_main()");
  2402. lprintf(LOG_NOTICE, "\nPICMG HPM.1 Upgrade Agent %d.%d.%d: \n",
  2403. HPMFWUPG_VERSION_MAJOR, HPMFWUPG_VERSION_MINOR,
  2404. HPMFWUPG_VERSION_SUBMINOR);
  2405. if (argc < 1) {
  2406. lprintf(LOG_ERR, "Not enough parameters given.");
  2407. HpmfwupgPrintUsage();
  2408. return HPMFWUPG_ERROR;
  2409. }
  2410. if (strcmp(argv[0], "help") == 0) {
  2411. HpmfwupgPrintUsage();
  2412. return HPMFWUPG_SUCCESS;
  2413. } else if ((strcmp(argv[0], "check") == 0)) {
  2414. /* hpm check */
  2415. if (argv[1] == NULL) {
  2416. rc = HpmfwupgTargetCheck(intf,VIEW_MODE);
  2417. } else {
  2418. /* hpm check <filename> */
  2419. rc = HpmfwupgTargetCheck(intf,0);
  2420. if (rc == HPMFWUPG_SUCCESS) {
  2421. rc = HpmfwupgUpgrade(intf, argv[1], 0,
  2422. 0, VIEW_MODE);
  2423. }
  2424. }
  2425. } else if (strcmp(argv[0], "upgrade") == 0) {
  2426. int i =0;
  2427. for (i=1; i< argc ; i++) {
  2428. if (strcmp(argv[i],"activate") == 0) {
  2429. activateFlag = 1;
  2430. }
  2431. /* hpm upgrade <filename> force */
  2432. if (strcmp(argv[i],"force") == 0) {
  2433. option |= FORCE_MODE;
  2434. }
  2435. /* hpm upgrade <filename> component <comp Id> */
  2436. if (strcmp(argv[i],"component") == 0) {
  2437. if (i+1 < argc) {
  2438. /* Error Checking */
  2439. if (str2int(argv[i+1], &componentId) != 0
  2440. || componentId < 0
  2441. || componentId > HPMFWUPG_COMPONENT_ID_MAX) {
  2442. lprintf(LOG_ERR,
  2443. "Given Component ID '%s' is invalid.",
  2444. argv[i+1]);
  2445. lprintf(LOG_ERR,
  2446. "Valid Compoment ID is: <0..7>");
  2447. return HPMFWUPG_ERROR;
  2448. }
  2449. if( verbose ) {
  2450. lprintf(LOG_NOTICE,
  2451. "Component Id %d provided",
  2452. componentId );
  2453. }
  2454. componentMask |= 1 << componentId;
  2455. } else {
  2456. /* That indicates the user has
  2457. * given component on console but
  2458. * not given any ID
  2459. */
  2460. lprintf(LOG_NOTICE,
  2461. "No component Id provided\n");
  2462. return HPMFWUPG_ERROR;
  2463. }
  2464. }
  2465. if (strcmp(argv[i],"debug") == 0) {
  2466. option |= DEBUG_MODE;
  2467. }
  2468. }
  2469. rc = HpmfwupgTargetCheck(intf, 0);
  2470. if (rc == HPMFWUPG_SUCCESS) {
  2471. /* Call the Upgrade function to start the upgrade */
  2472. rc = HpmfwupgUpgrade(intf, argv[1], activateFlag,
  2473. componentMask, option);
  2474. }
  2475. } else if (strcmp(argv[0], "compare") == 0) {
  2476. int i = 0;
  2477. for (i=1; i< argc; i++) {
  2478. /* hpm compare <file> [component x...] */
  2479. if (strcmp(argv[i],"component") == 0) {
  2480. if (i+1 < argc) {
  2481. /* Error Checking */
  2482. if (str2int(argv[i+1], &componentId) != 0
  2483. || componentId < 0
  2484. || componentId > HPMFWUPG_COMPONENT_ID_MAX) {
  2485. lprintf(LOG_ERR,
  2486. "Given Component ID '%s' is invalid.",
  2487. argv[i+1]);
  2488. lprintf(LOG_ERR,
  2489. "Valid Compoment ID is: <0..7>");
  2490. return HPMFWUPG_ERROR;
  2491. }
  2492. if( verbose ) {
  2493. lprintf(LOG_NOTICE,
  2494. "Component Id %d provided",
  2495. componentId);
  2496. }
  2497. componentMask|= 1 << componentId;
  2498. } else {
  2499. /* That indicates the user
  2500. * has given component on
  2501. * console but not
  2502. * given any ID
  2503. */
  2504. lprintf(LOG_NOTICE,
  2505. "No component Id provided\n");
  2506. return HPMFWUPG_ERROR;
  2507. }
  2508. } else if (strcmp(argv[i],"debug") == 0) {
  2509. option|= DEBUG_MODE;
  2510. }
  2511. }
  2512. option|= (COMPARE_MODE);
  2513. rc = HpmfwupgTargetCheck(intf, 0);
  2514. if (rc == HPMFWUPG_SUCCESS) {
  2515. rc = HpmfwupgUpgrade(intf, argv[1], 0,
  2516. componentMask, option);
  2517. }
  2518. } else if ((argc >= 1) && (strcmp(argv[0], "activate") == 0)) {
  2519. struct HpmfwupgActivateFirmwareCtx cmdCtx;
  2520. if ((argc == 2) && (strcmp(argv[1], "norollback") == 0)) {
  2521. cmdCtx.req.rollback_override = 1;
  2522. } else {
  2523. cmdCtx.req.rollback_override = 0;
  2524. }
  2525. rc = HpmfwupgActivateFirmware(intf, &cmdCtx, NULL);
  2526. } else if ((argc == 1) && (strcmp(argv[0], "targetcap") == 0)) {
  2527. struct HpmfwupgGetTargetUpgCapabilitiesCtx cmdCtx;
  2528. verbose++;
  2529. rc = HpmfwupgGetTargetUpgCapabilities(intf, &cmdCtx);
  2530. } else if ((argc == 3) && (strcmp(argv[0], "compprop") == 0)) {
  2531. struct HpmfwupgGetComponentPropertiesCtx cmdCtx;
  2532. if (str2uchar(argv[1], &(cmdCtx.req.componentId)) != 0
  2533. || cmdCtx.req.componentId > 7) {
  2534. lprintf(LOG_ERR,
  2535. "Given Component ID '%s' is invalid.",
  2536. argv[1]);
  2537. lprintf(LOG_ERR,
  2538. "Valid Compoment ID is: <0..7>");
  2539. return (-1);
  2540. }
  2541. if (str2uchar(argv[2], &(cmdCtx.req.selector)) != 0
  2542. || cmdCtx.req.selector > 4) {
  2543. lprintf(LOG_ERR,
  2544. "Given Properties selector '%s' is invalid.",
  2545. argv[2]);
  2546. lprintf(LOG_ERR,
  2547. "Valid Properties selector is: <0..4>");
  2548. return (-1);
  2549. }
  2550. verbose++;
  2551. rc = HpmfwupgGetComponentProperties(intf, &cmdCtx);
  2552. } else if ((argc == 1) && (strcmp(argv[0], "abort") == 0)) {
  2553. struct HpmfwupgAbortUpgradeCtx cmdCtx;
  2554. verbose++;
  2555. rc = HpmfwupgAbortUpgrade(intf, &cmdCtx);
  2556. } else if ((argc == 1) && (strcmp(argv[0], "upgstatus") == 0)) {
  2557. struct HpmfwupgGetUpgradeStatusCtx cmdCtx;
  2558. verbose++;
  2559. rc = HpmfwupgGetUpgradeStatus(intf, &cmdCtx, NULL, 0);
  2560. } else if ((argc == 1) && (strcmp(argv[0], "rollback") == 0)) {
  2561. struct HpmfwupgManualFirmwareRollbackCtx cmdCtx;
  2562. verbose++;
  2563. rc = HpmfwupgManualFirmwareRollback(intf, &cmdCtx);
  2564. } else if ((argc == 1) && (strcmp(argv[0], "rollbackstatus") == 0)) {
  2565. struct HpmfwupgQueryRollbackStatusCtx cmdCtx;
  2566. verbose++;
  2567. rc = HpmfwupgQueryRollbackStatus(intf, &cmdCtx, NULL);
  2568. } else if ((argc == 1) && (strcmp(argv[0], "selftestresult") == 0)) {
  2569. struct HpmfwupgQuerySelftestResultCtx cmdCtx;
  2570. verbose++;
  2571. rc = HpmfwupgQuerySelftestResult(intf, &cmdCtx, NULL);
  2572. } else {
  2573. lprintf(LOG_ERR, "Invalid HPM command: %s", argv[0]);
  2574. HpmfwupgPrintUsage();
  2575. rc = HPMFWUPG_ERROR;
  2576. }
  2577. return rc;
  2578. }