ipmi_sdradd.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /*
  2. * Redistribution and use in source and binary forms, with or without
  3. * modification, are permitted provided that the following conditions
  4. * are met:
  5. *
  6. * Redistribution of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. *
  9. * Redistribution in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * Neither the name of Sun Microsystems, Inc. or the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * This software is provided "AS IS," without a warranty of any kind.
  18. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
  19. * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
  20. * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
  21. * SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE
  22. * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  23. * OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
  24. * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA,
  25. * OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  26. * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  27. * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
  28. * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  29. */
  30. /*
  31. * Functions to program the SDR repository, from built-in sensors or
  32. * from sensors dumped in a binary file.
  33. */
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <stdio.h>
  37. #include <time.h>
  38. #include <fcntl.h>
  39. #include <unistd.h>
  40. #include <ipmitool/helper.h>
  41. #include <ipmitool/log.h>
  42. #include <ipmitool/bswap.h>
  43. #include <ipmitool/ipmi.h>
  44. #include <ipmitool/ipmi_intf.h>
  45. #include <ipmitool/ipmi_mc.h>
  46. #include <ipmitool/ipmi_strings.h>
  47. #include <ipmitool/ipmi_sdr.h>
  48. #define ADD_PARTIAL_SDR 0x25
  49. #ifdef HAVE_PRAGMA_PACK
  50. #pragma pack(1)
  51. #endif
  52. struct sdr_add_rq {
  53. uint16_t reserve_id; /* reservation ID */
  54. uint16_t id; /* record ID */
  55. uint8_t offset; /* offset into SDR */
  56. uint8_t in_progress; /* 0=partial, 1=last */
  57. #define PARTIAL_ADD (0)
  58. #define LAST_RECORD (1)
  59. uint8_t data[1]; /* SDR record data */
  60. } ATTRIBUTE_PACKING;
  61. #ifdef HAVE_PRAGMA_PACK
  62. #pragma pack(0)
  63. #endif
  64. /* This was formerly initialized to 24, reduced this to 19 so the overall
  65. message fits into the recommended 32-byte limit */
  66. static int sdr_max_write_len = 19;
  67. int ipmi_parse_range_list(const char *rangeList, unsigned char *pHexList);
  68. int ipmi_hex_to_dec( char * rangeList, unsigned char * pDecValue);
  69. static int
  70. partial_send(struct ipmi_intf *intf, struct ipmi_rq *req, uint16_t *id)
  71. {
  72. struct ipmi_rs *rsp;
  73. rsp = intf->sendrecv(intf, req);
  74. if (rsp == NULL) {
  75. return -1;
  76. }
  77. if (rsp->ccode || rsp->data_len < 2) {
  78. return -1;
  79. }
  80. *id = rsp->data[0] + (rsp->data[1] << 8);
  81. return 0;
  82. }
  83. int
  84. ipmi_sdr_add_record(struct ipmi_intf *intf, struct sdr_record_list *sdrr)
  85. {
  86. struct ipmi_rq req;
  87. struct sdr_add_rq *sdr_rq;
  88. uint16_t reserve_id;
  89. uint16_t id;
  90. int i;
  91. int len = sdrr->length;
  92. int rc = 0;
  93. /* actually no SDR to program */
  94. if (len < 1 || !sdrr->raw) {
  95. lprintf(LOG_ERR, "ipmitool: bad record , skipped");
  96. return 0;
  97. }
  98. if (ipmi_sdr_get_reservation(intf, 0, &reserve_id)) {
  99. lprintf(LOG_ERR, "ipmitool: reservation failed");
  100. return -1;
  101. }
  102. sdr_rq = (struct sdr_add_rq *)malloc(sizeof(*sdr_rq) + sdr_max_write_len);
  103. if (sdr_rq == NULL) {
  104. lprintf(LOG_ERR, "ipmitool: malloc failure");
  105. return -1;
  106. }
  107. sdr_rq->reserve_id = reserve_id;
  108. sdr_rq->in_progress = PARTIAL_ADD;
  109. memset(&req, 0, sizeof(req));
  110. req.msg.netfn = IPMI_NETFN_STORAGE;
  111. req.msg.cmd = ADD_PARTIAL_SDR;
  112. req.msg.data = (uint8_t *) sdr_rq;
  113. /* header first */
  114. sdr_rq->id = 0;
  115. sdr_rq->offset = 0;
  116. sdr_rq->data[0] = sdrr->id & 0xFF;
  117. sdr_rq->data[1] = (sdrr->id >> 8) & 0xFF;
  118. sdr_rq->data[2] = sdrr->version;
  119. sdr_rq->data[3] = sdrr->type;
  120. sdr_rq->data[4] = sdrr->length;
  121. req.msg.data_len = 5 + sizeof(*sdr_rq) - 1;
  122. if (partial_send(intf, &req, &id)) {
  123. lprintf(LOG_ERR, "ipmitool: partial send error");
  124. free(sdr_rq);
  125. sdr_rq = NULL;
  126. return -1;
  127. }
  128. i = 0;
  129. /* sdr entry */
  130. while (i < len) {
  131. int data_len = 0;
  132. if ( (len - i) <= sdr_max_write_len) {
  133. /* last crunch */
  134. data_len = len - i;
  135. sdr_rq->in_progress = LAST_RECORD;
  136. } else {
  137. data_len = sdr_max_write_len;
  138. }
  139. sdr_rq->id = id;
  140. sdr_rq->offset = i + 5;
  141. memcpy(sdr_rq->data, sdrr->raw + i, data_len);
  142. req.msg.data_len = data_len + sizeof(*sdr_rq) - 1;
  143. if ((rc = partial_send(intf, &req, &id)) != 0) {
  144. lprintf(LOG_ERR, "ipmitool: partial add failed");
  145. break;
  146. }
  147. i += data_len;
  148. }
  149. free(sdr_rq);
  150. sdr_rq = NULL;
  151. return rc;
  152. }
  153. static int
  154. ipmi_sdr_repo_clear(struct ipmi_intf *intf)
  155. {
  156. struct ipmi_rs * rsp;
  157. struct ipmi_rq req;
  158. uint8_t msg_data[8];
  159. uint16_t reserve_id;
  160. int try;
  161. if (ipmi_sdr_get_reservation(intf, 0, &reserve_id))
  162. return -1;
  163. memset(&req, 0, sizeof(req));
  164. req.msg.netfn = IPMI_NETFN_STORAGE;
  165. req.msg.cmd = 0x27; // FIXME
  166. req.msg.data = msg_data;
  167. req.msg.data_len = 6;
  168. msg_data[0] = reserve_id & 0xFF;
  169. msg_data[1] = reserve_id >> 8;
  170. msg_data[2] = 'C';
  171. msg_data[3] = 'L';
  172. msg_data[4] = 'R';
  173. msg_data[5] = 0xAA;
  174. for (try = 0; try < 5; try++) {
  175. rsp = intf->sendrecv(intf, &req);
  176. if (rsp == NULL) {
  177. lprintf(LOG_ERR, "Unable to clear SDRR");
  178. return -1;
  179. }
  180. if (rsp->ccode > 0) {
  181. lprintf(LOG_ERR, "Unable to clear SDRR: %s",
  182. val2str(rsp->ccode, completion_code_vals));
  183. return -1;
  184. }
  185. if ((rsp->data[0] & 1) == 1) {
  186. printf("SDRR successfully erased\n");
  187. return 0;
  188. }
  189. printf("Wait for SDRR erasure completed...\n");
  190. msg_data[5] = 0;
  191. sleep(1);
  192. }
  193. /* if we are here we fed up trying erase */
  194. return -1;
  195. }
  196. struct sdrr_queue {
  197. struct sdr_record_list *head;
  198. struct sdr_record_list *tail;
  199. };
  200. /*
  201. * Fill the SDR repository from built-in sensors
  202. *
  203. */
  204. /*
  205. * Get all the SDR records stored in <queue>
  206. */
  207. static int
  208. sdrr_get_records(struct ipmi_intf *intf, struct ipmi_sdr_iterator *itr,
  209. struct sdrr_queue *queue)
  210. {
  211. struct sdr_get_rs *header;
  212. queue->head = NULL;
  213. queue->tail = NULL;
  214. while ((header = ipmi_sdr_get_next_header(intf, itr)) != NULL) {
  215. struct sdr_record_list *sdrr;
  216. sdrr = malloc(sizeof (struct sdr_record_list));
  217. if (sdrr == NULL) {
  218. lprintf(LOG_ERR, "ipmitool: malloc failure");
  219. return -1;
  220. }
  221. memset(sdrr, 0, sizeof (struct sdr_record_list));
  222. sdrr->id = header->id;
  223. sdrr->version = header->version;
  224. sdrr->type = header->type;
  225. sdrr->length = header->length;
  226. sdrr->raw = ipmi_sdr_get_record(intf, header, itr);
  227. (void)ipmi_sdr_print_name_from_rawentry(intf, sdrr->id, sdrr->type,sdrr->raw);
  228. /* put in the record queue */
  229. if (queue->head == NULL)
  230. queue->head = sdrr;
  231. else
  232. queue->tail->next = sdrr;
  233. queue->tail = sdrr;
  234. }
  235. return 0;
  236. }
  237. static int
  238. sdr_copy_to_sdrr(struct ipmi_intf *intf, int use_builtin,
  239. int from_addr, int to_addr)
  240. {
  241. int rc;
  242. struct sdrr_queue sdrr_queue;
  243. struct ipmi_sdr_iterator *itr;
  244. struct sdr_record_list *sdrr;
  245. struct sdr_record_list *sdrr_next;
  246. /* generate list of records for this target */
  247. intf->target_addr = from_addr;
  248. /* initialize iterator */
  249. itr = ipmi_sdr_start(intf, use_builtin);
  250. if (itr == 0)
  251. return 0;
  252. printf("Load SDRs from 0x%x\n", from_addr);
  253. rc = sdrr_get_records(intf, itr, &sdrr_queue);
  254. ipmi_sdr_end(intf, itr);
  255. /* ... */
  256. /* write the SDRs to the destination SDR Repository */
  257. intf->target_addr = to_addr;
  258. for (sdrr = sdrr_queue.head; sdrr != NULL; sdrr = sdrr_next) {
  259. sdrr_next = sdrr->next;
  260. rc = ipmi_sdr_add_record(intf, sdrr);
  261. if(rc < 0){
  262. lprintf(LOG_ERR, "Cannot add SDR ID 0x%04x to repository...", sdrr->id);
  263. }
  264. free(sdrr);
  265. sdrr = NULL;
  266. }
  267. return rc;
  268. }
  269. int
  270. ipmi_sdr_add_from_sensors(struct ipmi_intf *intf, int maxslot)
  271. {
  272. int i;
  273. int rc = 0;
  274. int slave_addr;
  275. int myaddr = intf->target_addr;
  276. if (ipmi_sdr_repo_clear(intf)) {
  277. lprintf(LOG_ERR, "Cannot erase SDRR. Give up.");
  278. return -1;
  279. }
  280. /* First fill the SDRR from local built-in sensors */
  281. rc = sdr_copy_to_sdrr(intf, 1, myaddr, myaddr);
  282. /* Now fill the SDRR with remote sensors */
  283. if( maxslot != 0 ) {
  284. for (i = 0, slave_addr = 0xB0; i < maxslot; i++, slave_addr += 2) {
  285. /* Hole in the PICMG 2.9 mapping */
  286. if (slave_addr == 0xC2) slave_addr += 2;
  287. if(sdr_copy_to_sdrr(intf, 0, slave_addr, myaddr) < 0)
  288. {
  289. rc = -1;
  290. }
  291. }
  292. }
  293. return rc;
  294. }
  295. int ipmi_hex_to_dec( char * strchar, unsigned char * pDecValue)
  296. {
  297. int rc = -1;
  298. unsigned char retValue = 0;
  299. if(
  300. (strlen(strchar) == 4)
  301. &&
  302. (strchar[0] == '0')
  303. &&
  304. (strchar[1] == 'x')
  305. )
  306. {
  307. rc = 0;
  308. if((strchar[2] >= '0') && (strchar[2] <= '9'))
  309. {
  310. retValue += ((strchar[2]-'0') * 16);
  311. }
  312. else if((strchar[2] >= 'a') && (strchar[2] <= 'f'))
  313. {
  314. retValue += (((strchar[2]-'a') + 10) * 16);
  315. }
  316. else if((strchar[2] >= 'A') && (strchar[2] <= 'F'))
  317. {
  318. retValue += (((strchar[2]-'A') + 10) * 16);
  319. }
  320. else
  321. {
  322. rc = -1;
  323. }
  324. if((strchar[3] >= '0') && (strchar[3] <= '9'))
  325. {
  326. retValue += ((strchar[3]-'0'));
  327. }
  328. else if((strchar[3] >= 'a') && (strchar[3] <= 'f'))
  329. {
  330. retValue += (((strchar[3]-'a') + 10));
  331. }
  332. else if((strchar[3] >= 'A') && (strchar[3] <= 'F'))
  333. {
  334. retValue += (((strchar[3]-'A') + 10));
  335. }
  336. else
  337. {
  338. rc = -1;
  339. }
  340. }
  341. if(rc == 0)
  342. {
  343. * pDecValue = retValue;
  344. }
  345. else
  346. {
  347. lprintf(LOG_ERR, "Must be Hex value of 4 characters (Ex.: 0x24)");
  348. }
  349. return rc;
  350. }
  351. #define MAX_NUM_SLOT 128
  352. int ipmi_parse_range_list(const char *rangeList, unsigned char * pHexList)
  353. {
  354. int rc = -1;
  355. unsigned char listOffset = 0;
  356. char * nextString;
  357. char * rangeString;
  358. char * inProcessString = (char *) rangeList;
  359. /* Discard empty string */
  360. if(strlen(rangeList) == 0)
  361. {
  362. return rc;
  363. }
  364. /* First, cut to comma separated string */
  365. nextString = strstr( rangeList, "," );
  366. if(nextString != rangeList)
  367. {
  368. unsigned char isLast;
  369. /* We get a valid string so far */
  370. rc = 0;
  371. do
  372. {
  373. if(nextString != NULL)
  374. {
  375. (*nextString)= 0;
  376. nextString ++;
  377. isLast = 0;
  378. }
  379. else
  380. {
  381. isLast = 1;
  382. }
  383. /* At this point, it is a single entry or a range */
  384. rangeString = strstr( inProcessString, "-" );
  385. if(rangeString == NULL)
  386. {
  387. unsigned char decValue = 0;
  388. /* Single entry */
  389. rc = ipmi_hex_to_dec( inProcessString, &decValue);
  390. if(rc == 0)
  391. {
  392. if((decValue % 2) == 0)
  393. {
  394. pHexList[listOffset++] = decValue;
  395. }
  396. else
  397. {
  398. lprintf(LOG_ERR, "I2C address provided value must be even.");
  399. }
  400. }
  401. }
  402. else
  403. {
  404. unsigned char startValue = 0;
  405. unsigned char endValue = 0;
  406. (*rangeString)= 0; /* Cut string*/
  407. rangeString ++;
  408. /* Range */
  409. rc = ipmi_hex_to_dec( inProcessString, &startValue);
  410. if(rc == 0)
  411. rc = ipmi_hex_to_dec( rangeString, &endValue);
  412. if(rc == 0)
  413. {
  414. if(((startValue % 2) == 0) && ((endValue % 2) == 0))
  415. {
  416. do
  417. {
  418. pHexList[listOffset++] = startValue;
  419. startValue += 2;
  420. }
  421. while(startValue != endValue);
  422. pHexList[listOffset++] = endValue;
  423. }
  424. else
  425. {
  426. lprintf(LOG_ERR, "I2C address provided value must be even.");
  427. }
  428. }
  429. }
  430. if(isLast == 0)
  431. {
  432. /* Setup for next string */
  433. inProcessString = nextString;
  434. nextString = strstr( rangeList, "," );
  435. }
  436. }while ((isLast == 0) && (rc == 0));
  437. }
  438. return rc;
  439. }
  440. int
  441. ipmi_sdr_add_from_list(struct ipmi_intf *intf, const char *rangeList)
  442. {
  443. int rc = 0;
  444. int slave_addr;
  445. int myaddr = intf->target_addr;
  446. unsigned char listValue[MAX_NUM_SLOT];
  447. memset( listValue, 0, MAX_NUM_SLOT );
  448. /* Build list from string */
  449. if(ipmi_parse_range_list(rangeList, listValue) != 0)
  450. {
  451. lprintf(LOG_ERR, "Range - List invalid, cannot be parsed.");
  452. return -1;
  453. }
  454. {
  455. unsigned char counter = 0;
  456. printf("List to scan: (Built-in) ");
  457. while(listValue[counter] != 0)
  458. {
  459. printf("%02x ", listValue[counter]);
  460. counter++;
  461. }
  462. printf("\n");
  463. }
  464. printf("Clearing SDR Repository\n");
  465. if (ipmi_sdr_repo_clear(intf)) {
  466. lprintf(LOG_ERR, "Cannot erase SDRR. Give up.");
  467. return -1;
  468. }
  469. /* First fill the SDRR from local built-in sensors */
  470. printf("Sanning built-in sensors..\n");
  471. rc = sdr_copy_to_sdrr(intf, 1, myaddr, myaddr);
  472. /* Now fill the SDRR with provided sensors list */
  473. {
  474. unsigned char counter = 0;
  475. while((rc == 0) && (listValue[counter] != 0))
  476. {
  477. slave_addr = listValue[counter];
  478. printf("Scanning %02Xh..\n", slave_addr);
  479. if(sdr_copy_to_sdrr(intf, 0, slave_addr, myaddr) < 0)
  480. {
  481. rc = -1;
  482. }
  483. counter++;
  484. }
  485. }
  486. return rc;
  487. }
  488. /*
  489. * Fill the SDR repository from records stored in a binary file
  490. *
  491. */
  492. static int
  493. ipmi_sdr_read_records(const char *filename, struct sdrr_queue *queue)
  494. {
  495. int rc = 0;
  496. int fd;
  497. uint8_t binHdr[5];
  498. queue->head = NULL;
  499. queue->tail = NULL;
  500. if ((fd = open(filename, O_RDONLY)) < 0) {
  501. return -1;
  502. }
  503. while (read(fd, binHdr, 5) == 5) {
  504. struct sdr_record_list *sdrr;
  505. lprintf(LOG_DEBUG, "binHdr[0] (id[MSB]) = 0x%02x", binHdr[0]);
  506. lprintf(LOG_DEBUG, "binHdr[1] (id[LSB]) = 0x%02x", binHdr[1]);
  507. lprintf(LOG_DEBUG, "binHdr[2] (version) = 0x%02x", binHdr[2]);
  508. lprintf(LOG_DEBUG, "binHdr[3] (type) = 0x%02x", binHdr[3]);
  509. lprintf(LOG_DEBUG, "binHdr[4] (length) = 0x%02x", binHdr[4]);
  510. sdrr = malloc(sizeof(*sdrr));
  511. if (sdrr == NULL) {
  512. lprintf(LOG_ERR, "ipmitool: malloc failure");
  513. rc = -1;
  514. break;
  515. }
  516. sdrr->id = (binHdr[1] << 8) | binHdr[0]; // LS Byte first
  517. sdrr->version = binHdr[2];
  518. sdrr->type = binHdr[3];
  519. sdrr->length = binHdr[4];
  520. if ((sdrr->raw = malloc(sdrr->length)) == NULL) {
  521. lprintf(LOG_ERR, "ipmitool: malloc failure");
  522. free(sdrr);
  523. sdrr = NULL;
  524. rc = -1;
  525. break;
  526. }
  527. if (read(fd, sdrr->raw, sdrr->length) != sdrr->length) {
  528. lprintf(LOG_ERR, "SDR from '%s' truncated", filename);
  529. free(sdrr->raw);
  530. sdrr->raw = NULL;
  531. free(sdrr);
  532. sdrr = NULL;
  533. rc = -1;
  534. break;
  535. }
  536. /* put in the record queue */
  537. if (queue->head == NULL)
  538. queue->head = sdrr;
  539. else
  540. queue->tail->next = sdrr;
  541. queue->tail = sdrr;
  542. }
  543. close(fd);
  544. return rc;
  545. }
  546. int
  547. ipmi_sdr_add_from_file(struct ipmi_intf *intf, const char *ifile)
  548. {
  549. int rc;
  550. struct sdrr_queue sdrr_queue;
  551. struct sdr_record_list *sdrr;
  552. struct sdr_record_list *sdrr_next;
  553. /* read the SDR records from file */
  554. rc = ipmi_sdr_read_records(ifile, &sdrr_queue);
  555. if (ipmi_sdr_repo_clear(intf)) {
  556. lprintf(LOG_ERR, "Cannot erase SDRR. Giving up.");
  557. /* FIXME: free sdr list */
  558. return -1;
  559. }
  560. /* write the SDRs to the SDR Repository */
  561. for (sdrr = sdrr_queue.head; sdrr != NULL; sdrr = sdrr_next) {
  562. sdrr_next = sdrr->next;
  563. rc = ipmi_sdr_add_record(intf, sdrr);
  564. if(rc < 0){
  565. lprintf(LOG_ERR, "Cannot add SDR ID 0x%04x to repository...", sdrr->id);
  566. }
  567. free(sdrr);
  568. sdrr = NULL;
  569. }
  570. return rc;
  571. }