ipmi_raw.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * Redistribution of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * Redistribution in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * Neither the name of Sun Microsystems, Inc. or the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * This software is provided "AS IS," without a warranty of any kind.
  20. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
  21. * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
  22. * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
  23. * SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE
  24. * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  25. * OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
  26. * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA,
  27. * OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  28. * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  29. * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
  30. * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  31. */
  32. #include <string.h>
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. #include <ipmitool/ipmi.h>
  36. #include <ipmitool/log.h>
  37. #include <ipmitool/helper.h>
  38. #include <ipmitool/ipmi_intf.h>
  39. #include <ipmitool/ipmi_raw.h>
  40. #include <ipmitool/ipmi_strings.h>
  41. #define IPMI_I2C_MASTER_MAX_SIZE 0x40 /* 64 bytes */
  42. static int is_valid_param(const char *input_param, uint8_t *uchr_ptr,
  43. const char *label);
  44. int ipmi_spd_print(uint8_t *, int);
  45. /* ipmi_master_write_read - Perform I2C write/read transactions
  46. *
  47. * This function performs an I2C master write-read function through
  48. * IPMI interface. It has a maximum transfer size of 32 bytes.
  49. *
  50. * @intf: ipmi interface
  51. * @bus: channel number, i2c bus id and type
  52. * @addr: i2c slave address
  53. * @wdata: data to write
  54. * @wsize: length of data to write (max 64 bytes)
  55. * @rsize: length of data to read (max 64 bytes)
  56. *
  57. * Returns pointer to IPMI Response
  58. */
  59. struct ipmi_rs *
  60. ipmi_master_write_read(struct ipmi_intf * intf, uint8_t bus, uint8_t addr,
  61. uint8_t * wdata, uint8_t wsize, uint8_t rsize)
  62. {
  63. struct ipmi_rq req;
  64. struct ipmi_rs * rsp;
  65. uint8_t rqdata[IPMI_I2C_MASTER_MAX_SIZE + 3];
  66. if (rsize > IPMI_I2C_MASTER_MAX_SIZE) {
  67. lprintf(LOG_ERR, "Master Write-Read: Too many bytes (%d) to read", rsize);
  68. return NULL;
  69. }
  70. if (wsize > IPMI_I2C_MASTER_MAX_SIZE) {
  71. lprintf(LOG_ERR, "Master Write-Read: Too many bytes (%d) to write", wsize);
  72. return NULL;
  73. }
  74. memset(&req, 0, sizeof(struct ipmi_rq));
  75. req.msg.netfn = IPMI_NETFN_APP;
  76. req.msg.cmd = 0x52; /* master write-read */
  77. req.msg.data = rqdata;
  78. req.msg.data_len = 3;
  79. memset(rqdata, 0, IPMI_I2C_MASTER_MAX_SIZE + 3);
  80. rqdata[0] = bus; /* channel number, bus id, bus type */
  81. rqdata[1] = addr; /* slave address */
  82. rqdata[2] = rsize; /* number of bytes to read */
  83. if (wsize > 0) {
  84. /* copy in data to write */
  85. memcpy(rqdata+3, wdata, wsize);
  86. req.msg.data_len += wsize;
  87. lprintf(LOG_DEBUG, "Writing %d bytes to i2cdev %02Xh", wsize, addr);
  88. }
  89. if (rsize > 0) {
  90. lprintf(LOG_DEBUG, "Reading %d bytes from i2cdev %02Xh", rsize, addr);
  91. }
  92. rsp = intf->sendrecv(intf, &req);
  93. if (rsp == NULL) {
  94. lprintf(LOG_ERR, "I2C Master Write-Read command failed");
  95. return NULL;
  96. }
  97. else if (rsp->ccode > 0) {
  98. switch (rsp->ccode) {
  99. case 0x81:
  100. lprintf(LOG_ERR, "I2C Master Write-Read command failed: Lost Arbitration");
  101. break;
  102. case 0x82:
  103. lprintf(LOG_ERR, "I2C Master Write-Read command failed: Bus Error");
  104. break;
  105. case 0x83:
  106. lprintf(LOG_ERR, "I2C Master Write-Read command failed: NAK on Write");
  107. break;
  108. case 0x84:
  109. lprintf(LOG_ERR, "I2C Master Write-Read command failed: Truncated Read");
  110. break;
  111. default:
  112. lprintf(LOG_ERR, "I2C Master Write-Read command failed: %s",
  113. val2str(rsp->ccode, completion_code_vals));
  114. break;
  115. }
  116. return NULL;
  117. }
  118. return rsp;
  119. }
  120. #define RAW_SPD_SIZE 512
  121. int
  122. ipmi_rawspd_main(struct ipmi_intf * intf, int argc, char ** argv)
  123. {
  124. struct ipmi_rs *rsp;
  125. uint8_t msize = IPMI_I2C_MASTER_MAX_SIZE; /* allow to override default */
  126. uint8_t channel = 0;
  127. uint8_t i2cbus = 0;
  128. uint8_t i2caddr = 0;
  129. uint8_t spd_data[RAW_SPD_SIZE];
  130. int i;
  131. memset(spd_data, 0, RAW_SPD_SIZE);
  132. if (argc < 2 || strncmp(argv[0], "help", 4) == 0) {
  133. lprintf(LOG_NOTICE, "usage: spd <i2cbus> <i2caddr> [channel] [maxread]");
  134. return 0;
  135. }
  136. if (is_valid_param(argv[0], &i2cbus, "i2cbus") != 0)
  137. return (-1);
  138. if (is_valid_param(argv[1], &i2caddr, "i2caddr") != 0)
  139. return (-1);
  140. if (argc >= 3) {
  141. if (is_valid_param(argv[2], &channel, "channel") != 0)
  142. return (-1);
  143. }
  144. if (argc >= 4) {
  145. if (is_valid_param(argv[3], &msize, "maxread") != 0)
  146. return (-1);
  147. }
  148. i2cbus = ((channel & 0xF) << 4) | ((i2cbus & 7) << 1) | 1;
  149. for (i = 0; i < RAW_SPD_SIZE; i+= msize) {
  150. rsp = ipmi_master_write_read(intf, i2cbus, i2caddr,
  151. (uint8_t *)&i, 1, msize );
  152. if (rsp == NULL) {
  153. lprintf(LOG_ERR, "Unable to perform I2C Master Write-Read");
  154. return -1;
  155. }
  156. memcpy(spd_data+i, rsp->data, msize);
  157. }
  158. ipmi_spd_print(spd_data, i);
  159. return 0;
  160. }
  161. static void rawi2c_usage(void)
  162. {
  163. lprintf(LOG_NOTICE, "usage: i2c [bus=public|# [chan=#] <i2caddr> <read bytes> [write data]");
  164. lprintf(LOG_NOTICE, " bus=public is default");
  165. lprintf(LOG_NOTICE, " chan=0 is default, bus= must be specified to use chan=");
  166. }
  167. int
  168. ipmi_rawi2c_main(struct ipmi_intf * intf, int argc, char ** argv)
  169. {
  170. struct ipmi_rs * rsp;
  171. uint8_t wdata[IPMI_I2C_MASTER_MAX_SIZE];
  172. uint8_t i2caddr = 0;
  173. uint8_t rsize = 0;
  174. uint8_t wsize = 0;
  175. unsigned int rbus = 0;
  176. uint8_t bus = 0;
  177. int i = 0;
  178. /* handle bus= argument */
  179. if (argc > 2 && strncmp(argv[0], "bus=", 4) == 0) {
  180. i = 1;
  181. if (strncmp(argv[0], "bus=public", 10) == 0)
  182. bus = 0;
  183. else if (sscanf(argv[0], "bus=%u", &rbus) == 1)
  184. bus = ((rbus & 7) << 1) | 1;
  185. else
  186. bus = 0;
  187. /* handle channel= argument
  188. * the bus= argument must be supplied first on command line */
  189. if (argc > 3 && strncmp(argv[1], "chan=", 5) == 0) {
  190. i = 2;
  191. if (sscanf(argv[1], "chan=%u", &rbus) == 1)
  192. bus |= rbus << 4;
  193. }
  194. }
  195. if ((argc-i) < 2 || strncmp(argv[0], "help", 4) == 0) {
  196. rawi2c_usage();
  197. return 0;
  198. }
  199. else if (argc-i-2 > IPMI_I2C_MASTER_MAX_SIZE) {
  200. lprintf(LOG_ERR, "Raw command input limit (%d bytes) exceeded",
  201. IPMI_I2C_MASTER_MAX_SIZE);
  202. return -1;
  203. }
  204. if (is_valid_param(argv[i++], &i2caddr, "i2caddr") != 0)
  205. return (-1);
  206. if (is_valid_param(argv[i++], &rsize, "read size") != 0)
  207. return (-1);
  208. if (i2caddr == 0) {
  209. lprintf(LOG_ERR, "Invalid I2C address 0");
  210. rawi2c_usage();
  211. return -1;
  212. }
  213. memset(wdata, 0, IPMI_I2C_MASTER_MAX_SIZE);
  214. for (; i < argc; i++) {
  215. uint8_t val = 0;
  216. if (is_valid_param(argv[i], &val, "parameter") != 0)
  217. return (-1);
  218. wdata[wsize] = val;
  219. wsize++;
  220. }
  221. lprintf(LOG_INFO, "RAW I2C REQ (i2caddr=%x readbytes=%d writebytes=%d)",
  222. i2caddr, rsize, wsize);
  223. printbuf(wdata, wsize, "WRITE DATA");
  224. rsp = ipmi_master_write_read(intf, bus, i2caddr, wdata, wsize, rsize);
  225. if (rsp == NULL) {
  226. lprintf(LOG_ERR, "Unable to perform I2C Master Write-Read");
  227. return -1;
  228. }
  229. if (wsize > 0) {
  230. if (verbose || rsize == 0)
  231. printf("Wrote %d bytes to I2C device %02Xh\n", wsize, i2caddr);
  232. }
  233. if (rsize > 0) {
  234. if (verbose || wsize == 0)
  235. printf("Read %d bytes from I2C device %02Xh\n", rsp->data_len, i2caddr);
  236. if (rsp->data_len < rsize)
  237. return -1;
  238. /* print the raw response buffer */
  239. for (i=0; i<rsp->data_len; i++) {
  240. if (((i%16) == 0) && (i != 0))
  241. printf("\n");
  242. printf(" %2.2x", rsp->data[i]);
  243. }
  244. printf("\n");
  245. if (rsp->data_len <= 4) {
  246. uint32_t bit;
  247. int j;
  248. for (i = 0; i < rsp->data_len; i++) {
  249. for (j = 1, bit = 0x80; bit > 0; bit /= 2, j++) {
  250. printf("%s", (rsp->data[i] & bit) ? "1" : "0");
  251. }
  252. printf(" ");
  253. }
  254. printf("\n");
  255. }
  256. }
  257. return 0;
  258. }
  259. /* ipmi_raw_help() - print 'raw' help text
  260. *
  261. * returns void
  262. */
  263. void
  264. ipmi_raw_help()
  265. {
  266. lprintf(LOG_NOTICE, "RAW Commands: raw <netfn> <cmd> [data]");
  267. print_valstr(ipmi_netfn_vals, "Network Function Codes", LOG_NOTICE);
  268. lprintf(LOG_NOTICE, "(can also use raw hex values)");
  269. } /* ipmi_raw_help() */
  270. int
  271. ipmi_raw_main(struct ipmi_intf * intf, int argc, char ** argv)
  272. {
  273. struct ipmi_rs * rsp;
  274. struct ipmi_rq req;
  275. uint8_t netfn, cmd, lun;
  276. uint16_t netfn_tmp = 0;
  277. int i;
  278. uint8_t data[256];
  279. if (argc == 1 && strncmp(argv[0], "help", 4) == 0) {
  280. ipmi_raw_help();
  281. return 0;
  282. }
  283. else if (argc < 2) {
  284. lprintf(LOG_ERR, "Not enough parameters given.");
  285. ipmi_raw_help();
  286. return (-1);
  287. }
  288. else if (argc > sizeof(data))
  289. {
  290. lprintf(LOG_NOTICE, "Raw command input limit (256 bytes) exceeded");
  291. return -1;
  292. }
  293. lun = intf->target_lun;
  294. netfn_tmp = str2val(argv[0], ipmi_netfn_vals);
  295. if (netfn_tmp == 0xff) {
  296. if (is_valid_param(argv[0], &netfn, "netfn") != 0)
  297. return (-1);
  298. } else {
  299. if (netfn_tmp >= UINT8_MAX) {
  300. lprintf(LOG_ERR, "Given netfn \"%s\" is out of range.", argv[0]);
  301. return (-1);
  302. }
  303. netfn = netfn_tmp;
  304. }
  305. if (is_valid_param(argv[1], &cmd, "command") != 0)
  306. return (-1);
  307. memset(data, 0, sizeof(data));
  308. memset(&req, 0, sizeof(req));
  309. req.msg.netfn = netfn;
  310. req.msg.lun = lun;
  311. req.msg.cmd = cmd;
  312. req.msg.data = data;
  313. for (i=2; i<argc; i++) {
  314. uint8_t val = 0;
  315. if (is_valid_param(argv[i], &val, "data") != 0)
  316. return (-1);
  317. req.msg.data[i-2] = val;
  318. req.msg.data_len++;
  319. }
  320. lprintf(LOG_INFO,
  321. "RAW REQ (channel=0x%x netfn=0x%x lun=0x%x cmd=0x%x data_len=%d)",
  322. intf->target_channel & 0x0f, req.msg.netfn,req.msg.lun ,
  323. req.msg.cmd, req.msg.data_len);
  324. printbuf(req.msg.data, req.msg.data_len, "RAW REQUEST");
  325. rsp = intf->sendrecv(intf, &req);
  326. if (rsp == NULL) {
  327. lprintf(LOG_ERR, "Unable to send RAW command "
  328. "(channel=0x%x netfn=0x%x lun=0x%x cmd=0x%x)",
  329. intf->target_channel & 0x0f, req.msg.netfn, req.msg.lun, req.msg.cmd);
  330. return -1;
  331. }
  332. if (rsp->ccode > 0) {
  333. lprintf(LOG_ERR, "Unable to send RAW command "
  334. "(channel=0x%x netfn=0x%x lun=0x%x cmd=0x%x rsp=0x%x): %s",
  335. intf->target_channel & 0x0f, req.msg.netfn, req.msg.lun, req.msg.cmd, rsp->ccode,
  336. val2str(rsp->ccode, completion_code_vals));
  337. return -1;
  338. }
  339. lprintf(LOG_INFO, "RAW RSP (%d bytes)", rsp->data_len);
  340. /* print the raw response buffer */
  341. for (i=0; i<rsp->data_len; i++) {
  342. if (((i%16) == 0) && (i != 0))
  343. printf("\n");
  344. printf(" %2.2x", rsp->data[i]);
  345. }
  346. printf("\n");
  347. return 0;
  348. }
  349. /* is_valid_param -
  350. *
  351. * @input_param: string to convert from
  352. * @uchr_ptr: pointer where to store converted value
  353. * @label: string used in error message
  354. *
  355. * returns 0 if parameter is valid
  356. * returns (-1) if parameter is invalid/on error
  357. */
  358. int
  359. is_valid_param(const char *input_param, uint8_t *uchr_ptr, const char *label) {
  360. if (input_param == NULL || label == NULL) {
  361. lprintf(LOG_ERROR, "ERROR: NULL pointer passed.");
  362. return (-1);
  363. }
  364. if (str2uchar(input_param, uchr_ptr) == 0)
  365. return 0;
  366. lprintf(LOG_ERR, "Given %s \"%s\" is invalid.", label, input_param);
  367. return (-1);
  368. }