ipmi_sunoem.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431
  1. /*
  2. * Copyright (c) 2009, 2014, Oracle and/or its affiliates. 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. #define _XOPEN_SOURCE
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <strings.h>
  37. #include <sys/socket.h>
  38. #include <netinet/in.h>
  39. #include <arpa/inet.h>
  40. #include <errno.h>
  41. #include <time.h>
  42. #include <unistd.h>
  43. #include <signal.h>
  44. #include <ctype.h>
  45. #include <sys/time.h>
  46. #include <limits.h>
  47. #include <fcntl.h>
  48. #include <sys/select.h>
  49. #include <termios.h>
  50. #include <ipmitool/ipmi.h>
  51. #include <ipmitool/ipmi_intf.h>
  52. #include <ipmitool/helper.h>
  53. #include <ipmitool/log.h>
  54. #include <ipmitool/ipmi_sel.h>
  55. #include <ipmitool/ipmi_sdr.h>
  56. #include <ipmitool/ipmi_strings.h>
  57. #include <ipmitool/ipmi_channel.h>
  58. #include <ipmitool/ipmi_sunoem.h>
  59. #include <ipmitool/ipmi_raw.h>
  60. static const struct valstr sunoem_led_type_vals[] = {
  61. { 0, "OK2RM" },
  62. { 1, "SERVICE" },
  63. { 2, "ACT" },
  64. { 3, "LOCATE" },
  65. { 0xFF, NULL },
  66. };
  67. static const struct valstr sunoem_led_mode_vals[] = {
  68. { 0, "OFF" },
  69. { 1, "ON" },
  70. { 2, "STANDBY" },
  71. { 3, "SLOW" },
  72. { 4, "FAST" },
  73. { 0xFF, NULL },
  74. };
  75. static const struct valstr sunoem_led_mode_optvals[] = {
  76. { 0, "STEADY_OFF" },
  77. { 1, "STEADY_ON" },
  78. { 2, "STANDBY_BLINK" },
  79. { 3, "SLOW_BLINK" },
  80. { 4, "FAST_BLINK" },
  81. { 0xFF, NULL },
  82. };
  83. #define SUNOEM_SUCCESS 1
  84. #define IPMI_SUNOEM_GETFILE_VERSION {3,2,0,0}
  85. #define IPMI_SUNOEM_GETBEHAVIOR_VERSION {3,2,0,0}
  86. /*
  87. * PRINT_NORMAL: print out the LED value as normal
  88. * PRINT_ERROR: print out "na" for the LED value
  89. */
  90. typedef enum
  91. {
  92. PRINT_NORMAL = 0, PRINT_ERROR
  93. } print_status_t;
  94. int ret_get = 0;
  95. int ret_set = 0;
  96. static void
  97. ipmi_sunoem_usage(void)
  98. {
  99. lprintf(LOG_NOTICE, "Usage: sunoem <command> [option...]");
  100. lprintf(LOG_NOTICE, "");
  101. lprintf(LOG_NOTICE, "Commands:");
  102. lprintf(LOG_NOTICE, " - cli [<command string> ...]");
  103. lprintf(LOG_NOTICE, " Execute SP CLI commands.");
  104. lprintf(LOG_NOTICE, "");
  105. lprintf(LOG_NOTICE, " - led get [<sensor_id>] [ledtype]");
  106. lprintf(LOG_NOTICE, " - Read status of LED found in Generic Device Locator.");
  107. lprintf(LOG_NOTICE, "");
  108. lprintf(LOG_NOTICE, " - led set <sensor_id> <led_mode> [led_type]");
  109. lprintf(LOG_NOTICE, " - Set mode of LED found in Generic Device Locator.");
  110. lprintf(LOG_NOTICE, " - You can pass 'all' as the <senso_rid> to change the LED mode of all sensors.");
  111. lprintf(LOG_NOTICE, " - Use 'sdr list generic' command to get list of Generic");
  112. lprintf(LOG_NOTICE, " - Devices that are controllable LEDs.");
  113. lprintf(LOG_NOTICE, "");
  114. lprintf(LOG_NOTICE, " - Required SIS LED Mode:");
  115. lprintf(LOG_NOTICE, " OFF Off");
  116. lprintf(LOG_NOTICE, " ON Steady On");
  117. lprintf(LOG_NOTICE, " STANDBY 100ms on 2900ms off blink rate");
  118. lprintf(LOG_NOTICE, " SLOW 1HZ blink rate");
  119. lprintf(LOG_NOTICE, " FAST 4HZ blink rate");
  120. lprintf(LOG_NOTICE, "");
  121. lprintf(LOG_NOTICE, " - Optional SIS LED Type:");
  122. lprintf(LOG_NOTICE, " OK2RM OK to Remove");
  123. lprintf(LOG_NOTICE, " SERVICE Service Required");
  124. lprintf(LOG_NOTICE, " ACT Activity");
  125. lprintf(LOG_NOTICE, " LOCATE Locate");
  126. lprintf(LOG_NOTICE, "");
  127. lprintf(LOG_NOTICE, " - nacname <ipmi_nac_name>");
  128. lprintf(LOG_NOTICE, " - Returns the full nac name");
  129. lprintf(LOG_NOTICE, "");
  130. lprintf(LOG_NOTICE, " - ping NUMBER <q>");
  131. lprintf(LOG_NOTICE, " - Send and Receive NUMBER (64 Byte) packets.");
  132. lprintf(LOG_NOTICE, "");
  133. lprintf(LOG_NOTICE, " - q - Quiet. Displays output at start and end");
  134. lprintf(LOG_NOTICE, "");
  135. lprintf(LOG_NOTICE, " - getval <target_name>");
  136. lprintf(LOG_NOTICE, " - Returns the ILOM property value");
  137. lprintf(LOG_NOTICE, "");
  138. lprintf(LOG_NOTICE, " - setval <property name> <property value> <timeout>");
  139. lprintf(LOG_NOTICE, " - Sets the ILOM property value");
  140. lprintf(LOG_NOTICE, " - If timeout is not specified, the default is 5 sec.");
  141. lprintf(LOG_NOTICE, " - NOTE: must be executed locally on host, not remotely over LAN!");
  142. lprintf(LOG_NOTICE, "");
  143. lprintf(LOG_NOTICE, " - sshkey del <user_id>");
  144. lprintf(LOG_NOTICE, " - Delete ssh key for user id from authorized_keys,");
  145. lprintf(LOG_NOTICE, " - view users with 'user list' command.");
  146. lprintf(LOG_NOTICE, "");
  147. lprintf(LOG_NOTICE, " - sshkey set <user_id> <id_rsa.pub>");
  148. lprintf(LOG_NOTICE, " - Set ssh key for a userid into authorized_keys,");
  149. lprintf(LOG_NOTICE, " - view users with 'user list' command.");
  150. lprintf(LOG_NOTICE, "");
  151. lprintf(LOG_NOTICE, " - version");
  152. lprintf(LOG_NOTICE, " - Display the software version");
  153. lprintf(LOG_NOTICE, "");
  154. lprintf(LOG_NOTICE, " - nacname <ipmi_nac_name>");
  155. lprintf(LOG_NOTICE, " - Returns the full nac name");
  156. lprintf(LOG_NOTICE, "");
  157. lprintf(LOG_NOTICE, " - getfile <file_string_id> <destination_file_name>");
  158. lprintf(LOG_NOTICE, " - Copy file <file_string_id> to <destination_file_name>");
  159. lprintf(LOG_NOTICE, "");
  160. lprintf(LOG_NOTICE, " - File string ids:");
  161. lprintf(LOG_NOTICE, " SSH_PUBKEYS");
  162. lprintf(LOG_NOTICE, " DIAG_PASSED");
  163. lprintf(LOG_NOTICE, " DIAG_FAILED");
  164. lprintf(LOG_NOTICE, " DIAG_END_TIME");
  165. lprintf(LOG_NOTICE, " DIAG_INVENTORY");
  166. lprintf(LOG_NOTICE, " DIAG_TEST_LOG");
  167. lprintf(LOG_NOTICE, " DIAG_START_TIME");
  168. lprintf(LOG_NOTICE, " DIAG_UEFI_LOG");
  169. lprintf(LOG_NOTICE, " DIAG_TEST_LOG");
  170. lprintf(LOG_NOTICE, " DIAG_LAST_LOG");
  171. lprintf(LOG_NOTICE, " DIAG_LAST_CMD");
  172. lprintf(LOG_NOTICE, "");
  173. lprintf(LOG_NOTICE, " - getbehavior <behavior_string_id>");
  174. lprintf(LOG_NOTICE, " - Test if ILOM behavior is enabled");
  175. lprintf(LOG_NOTICE, "");
  176. lprintf(LOG_NOTICE, " - Behavior string ids:");
  177. lprintf(LOG_NOTICE, " SUPPORTS_SIGNED_PACKAGES");
  178. lprintf(LOG_NOTICE, " REQUIRES_SIGNED_PACKAGES");
  179. lprintf(LOG_NOTICE, "");
  180. }
  181. #define SUNOEM_FAN_MODE_AUTO 0x00
  182. #define SUNOEM_FAN_MODE_MANUAL 0x01
  183. static void
  184. __sdr_list_empty(struct sdr_record_list * head)
  185. {
  186. struct sdr_record_list * e, *f;
  187. for (e = head; e != NULL; e = f) {
  188. f = e->next;
  189. free(e);
  190. }
  191. head = NULL;
  192. }
  193. /*
  194. * led_print
  195. * Print out the led name and the state, if stat is PRINT_NORMAL.
  196. * Otherwise, print out the led name and "na".
  197. * The state parameter is not referenced if stat is not PRINT_NORMAL.
  198. */
  199. static void
  200. led_print(const char * name, print_status_t stat, uint8_t state)
  201. {
  202. const char *theValue;
  203. if (stat == PRINT_NORMAL) {
  204. theValue = val2str(state, sunoem_led_mode_vals);
  205. } else {
  206. theValue = "na";
  207. }
  208. if (csv_output) {
  209. printf("%s,%s\n", name, theValue);
  210. } else {
  211. printf("%-16s | %s\n", name, theValue);
  212. }
  213. }
  214. #define CC_NORMAL 0x00
  215. #define CC_PARAM_OUT_OF_RANGE 0xc9
  216. #define CC_DEST_UNAVAILABLE 0xd3
  217. #define CC_UNSPECIFIED_ERR 0xff
  218. #define CC_INSUFFICIENT_PRIVILEGE 0xd4
  219. #define CC_INV_CMD 0xc1
  220. #define CC_INV_DATA_FIELD 0xcc
  221. /*
  222. * sunoem_led_get(....)
  223. *
  224. * OUTPUT:
  225. * SUNOEM_EC_INVALID_ARG if dev is NULL,
  226. * SUNOEM_EC_BMC_NOT_RESPONDING if no reply is obtained from BMC,
  227. * SUNOEM_EC_BMC_CCODE_NONZERO if completion code is nonzero,
  228. * SUNOEM_EC_SUCCESS otherwise.
  229. */
  230. static sunoem_ec_t
  231. sunoem_led_get(struct ipmi_intf * intf, struct sdr_record_generic_locator * dev,
  232. int ledtype, struct ipmi_rs **loc_rsp)
  233. {
  234. struct ipmi_rs * rsp;
  235. struct ipmi_rq req;
  236. uint8_t rqdata[7];
  237. int rqdata_len;
  238. if (dev == NULL) {
  239. *loc_rsp = NULL;
  240. return (SUNOEM_EC_INVALID_ARG);
  241. }
  242. rqdata[0] = dev->dev_slave_addr;
  243. if (ledtype == 0xFF)
  244. rqdata[1] = dev->oem;
  245. else
  246. rqdata[1] = ledtype;
  247. rqdata[2] = dev->dev_access_addr;
  248. rqdata[3] = dev->oem;
  249. rqdata[4] = dev->entity.id;
  250. rqdata[5] = dev->entity.instance;
  251. rqdata[6] = 0;
  252. rqdata_len = 7;
  253. req.msg.netfn = IPMI_NETFN_SUNOEM;
  254. req.msg.cmd = IPMI_SUNOEM_LED_GET;
  255. req.msg.lun = dev->lun;
  256. req.msg.data = rqdata;
  257. req.msg.data_len = rqdata_len;
  258. rsp = intf->sendrecv(intf, &req);
  259. /*
  260. * Just return NULL if there was
  261. * an error.
  262. */
  263. if (rsp == NULL) {
  264. *loc_rsp = NULL;
  265. return (SUNOEM_EC_BMC_NOT_RESPONDING);
  266. } else if (rsp->ccode > 0) {
  267. *loc_rsp = rsp;
  268. return (SUNOEM_EC_BMC_CCODE_NONZERO);
  269. } else {
  270. *loc_rsp = rsp;
  271. return (SUNOEM_EC_SUCCESS);
  272. }
  273. }
  274. static struct ipmi_rs *
  275. sunoem_led_set(struct ipmi_intf * intf, struct sdr_record_generic_locator * dev,
  276. int ledtype, int ledmode)
  277. {
  278. struct ipmi_rs * rsp;
  279. struct ipmi_rq req;
  280. uint8_t rqdata[9];
  281. int rqdata_len;
  282. if (dev == NULL)
  283. return NULL;
  284. rqdata[0] = dev->dev_slave_addr;
  285. if (ledtype == 0xFF)
  286. rqdata[1] = dev->oem;
  287. else
  288. rqdata[1] = ledtype;
  289. rqdata[2] = dev->dev_access_addr;
  290. rqdata[3] = dev->oem;
  291. rqdata[4] = ledmode;
  292. rqdata[5] = dev->entity.id;
  293. rqdata[6] = dev->entity.instance;
  294. rqdata[7] = 0;
  295. rqdata[8] = 0;
  296. rqdata_len = 9;
  297. req.msg.netfn = IPMI_NETFN_SUNOEM;
  298. req.msg.cmd = IPMI_SUNOEM_LED_SET;
  299. req.msg.lun = dev->lun;
  300. req.msg.data = rqdata;
  301. req.msg.data_len = rqdata_len;
  302. rsp = intf->sendrecv(intf, &req);
  303. if (rsp == NULL) {
  304. lprintf(LOG_ERR, "Sun OEM Set LED command failed.");
  305. return NULL;
  306. } else if (rsp->ccode > 0) {
  307. lprintf(LOG_ERR, "Sun OEM Set LED command failed: %s",
  308. val2str(rsp->ccode, completion_code_vals));
  309. return NULL;
  310. }
  311. return (rsp);
  312. }
  313. static void
  314. sunoem_led_get_byentity(struct ipmi_intf * intf, uint8_t entity_id,
  315. uint8_t entity_inst, int ledtype)
  316. {
  317. struct ipmi_rs * rsp;
  318. struct sdr_record_list *elist, *e;
  319. struct entity_id entity;
  320. sunoem_ec_t res;
  321. if (entity_id == 0)
  322. return;
  323. /* lookup sdrs with this entity */
  324. memset(&entity, 0, sizeof(struct entity_id));
  325. entity.id = entity_id;
  326. entity.instance = entity_inst;
  327. elist = ipmi_sdr_find_sdr_byentity(intf, &entity);
  328. if (elist == NULL)
  329. ret_get = -1;
  330. /* for each generic sensor get its led state */
  331. for (e = elist; e != NULL; e = e->next) {
  332. if (e->type != SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR)
  333. continue;
  334. res = sunoem_led_get(intf, e->record.genloc, ledtype, &rsp);
  335. if (res == SUNOEM_EC_SUCCESS && rsp && rsp->data_len == 1) {
  336. led_print((const char *) e->record.genloc->id_string, PRINT_NORMAL,
  337. rsp->data[0]);
  338. } else {
  339. led_print((const char *) e->record.genloc->id_string, PRINT_ERROR,
  340. 0);
  341. if (res != SUNOEM_EC_BMC_CCODE_NONZERO|| !rsp
  342. || rsp->ccode != CC_DEST_UNAVAILABLE) {
  343. ret_get = -1;
  344. }
  345. }
  346. }
  347. __sdr_list_empty(elist);
  348. }
  349. static void
  350. sunoem_led_set_byentity(struct ipmi_intf * intf, uint8_t entity_id,
  351. uint8_t entity_inst, int ledtype, int ledmode)
  352. {
  353. struct ipmi_rs * rsp;
  354. struct sdr_record_list *elist, *e;
  355. struct entity_id entity;
  356. if (entity_id == 0)
  357. return;
  358. /* lookup sdrs with this entity */
  359. memset(&entity, 0, sizeof(struct entity_id));
  360. entity.id = entity_id;
  361. entity.instance = entity_inst;
  362. elist = ipmi_sdr_find_sdr_byentity(intf, &entity);
  363. if (elist == NULL)
  364. ret_set = -1;
  365. /* for each generic sensor set its led state */
  366. for (e = elist; e != NULL; e = e->next) {
  367. if (e->type != SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR)
  368. continue;
  369. rsp = sunoem_led_set(intf, e->record.genloc, ledtype, ledmode);
  370. if (rsp && rsp->data_len == 0) {
  371. led_print((const char *) e->record.genloc->id_string, PRINT_NORMAL,
  372. ledmode);
  373. } else if (rsp == NULL) {
  374. ret_set = -1;
  375. }
  376. }
  377. __sdr_list_empty(elist);
  378. }
  379. /*
  380. * IPMI Request Data: 5 bytes
  381. *
  382. * [byte 0] devAddr Value from the "Device Slave Address" field in
  383. * LED's Generic Device Locator record in the SDR
  384. * [byte 1] led LED Type: OK2RM, ACT, LOCATE, SERVICE
  385. * [byte 2] ctrlrAddr Controller address; value from the "Device
  386. * Access Address" field, 0x20 if the LED is local
  387. * [byte 3] hwInfo The OEM field from the SDR record
  388. * [byte 4] force 1 = directly access the device
  389. * 0 = go thru its controller
  390. * Ignored if LED is local
  391. *
  392. * The format below is for Sun Blade Modular systems only
  393. * [byte 4] entityID The entityID field from the SDR record
  394. * [byte 5] entityIns The entityIns field from the SDR record
  395. * [byte 6] force 1 = directly access the device
  396. * 0 = go thru its controller
  397. * Ignored if LED is local
  398. */
  399. static int
  400. ipmi_sunoem_led_get(struct ipmi_intf * intf, int argc, char ** argv)
  401. {
  402. struct ipmi_rs * rsp;
  403. struct sdr_record_list *sdr;
  404. struct sdr_record_list *alist, *a;
  405. struct sdr_record_entity_assoc *assoc;
  406. int ledtype = 0xFF;
  407. int i;
  408. sunoem_ec_t res;
  409. /*
  410. * sunoem led/sbled get <id> [type]
  411. */
  412. if (argc < 1 || strncmp(argv[0], "help", 4) == 0) {
  413. ipmi_sunoem_usage();
  414. return (0);
  415. }
  416. if (argc > 1) {
  417. ledtype = str2val(argv[1], sunoem_led_type_vals);
  418. if (ledtype == 0xFF)
  419. lprintf(LOG_ERR,
  420. "Unknown ledtype, will use data from the SDR oem field");
  421. }
  422. if (strncasecmp(argv[0], "all", 3) == 0) {
  423. /* do all generic sensors */
  424. alist = ipmi_sdr_find_sdr_bytype(intf,
  425. SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR);
  426. if (alist == NULL)
  427. return (-1);
  428. for (a = alist; a != NULL; a = a->next) {
  429. if (a->type != SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR)
  430. continue;
  431. if (a->record.genloc->entity.logical)
  432. continue;
  433. res = sunoem_led_get(intf, a->record.genloc, ledtype, &rsp);
  434. if (res == SUNOEM_EC_SUCCESS && rsp && rsp->data_len == 1) {
  435. led_print((const char *) a->record.genloc->id_string,
  436. PRINT_NORMAL, rsp->data[0]);
  437. } else {
  438. led_print((const char *) a->record.genloc->id_string,
  439. PRINT_ERROR, 0);
  440. if (res != SUNOEM_EC_BMC_CCODE_NONZERO|| !rsp ||
  441. rsp->ccode != CC_DEST_UNAVAILABLE) {
  442. ret_get = -1;
  443. }
  444. }
  445. }
  446. __sdr_list_empty(alist);
  447. if (ret_get == -1)
  448. return (-1);
  449. return (0);
  450. }
  451. /* look up generic device locator record in SDR */
  452. sdr = ipmi_sdr_find_sdr_byid(intf, argv[0]);
  453. if (sdr == NULL) {
  454. lprintf(LOG_ERR, "No Sensor Data Record found for %s", argv[0]);
  455. return (-1);
  456. }
  457. if (sdr->type != SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR) {
  458. lprintf(LOG_ERR, "Invalid SDR type %d", sdr->type);
  459. return (-1);
  460. }
  461. if (!sdr->record.genloc->entity.logical) {
  462. /*
  463. * handle physical entity
  464. */
  465. res = sunoem_led_get(intf, sdr->record.genloc, ledtype, &rsp);
  466. if (res == SUNOEM_EC_SUCCESS && rsp && rsp->data_len == 1) {
  467. led_print((const char *) sdr->record.genloc->id_string,
  468. PRINT_NORMAL, rsp->data[0]);
  469. } else {
  470. led_print((const char *) sdr->record.genloc->id_string, PRINT_ERROR,
  471. 0);
  472. if (res != SUNOEM_EC_BMC_CCODE_NONZERO|| !rsp
  473. || rsp->ccode != CC_DEST_UNAVAILABLE) {
  474. ret_get = -1;
  475. }
  476. }
  477. if (ret_get == -1)
  478. return (-1);
  479. return (0);
  480. }
  481. /*
  482. * handle logical entity for LED grouping
  483. */
  484. lprintf(LOG_INFO, "LED %s is logical device", argv[0]);
  485. /* get entity assoc records */
  486. alist = ipmi_sdr_find_sdr_bytype(intf, SDR_RECORD_TYPE_ENTITY_ASSOC);
  487. if (alist == NULL)
  488. return (-1);
  489. for (a = alist; a != NULL; a = a->next) {
  490. if (a->type != SDR_RECORD_TYPE_ENTITY_ASSOC)
  491. continue;
  492. assoc = a->record.entassoc;
  493. if (assoc == NULL)
  494. continue;
  495. /* check that the entity id/instance matches our generic record */
  496. if (assoc->entity.id != sdr->record.genloc->entity.id
  497. || assoc->entity.instance
  498. != sdr->record.genloc->entity.instance)
  499. continue;
  500. if (assoc->flags.isrange) {
  501. /*
  502. * handle ranged entity associations
  503. *
  504. * the test for non-zero entity id is handled in
  505. * sunoem_led_get_byentity()
  506. */
  507. /* first range set - id 1 and 2 must be equal */
  508. if (assoc->entity_id_1 == assoc->entity_id_2)
  509. for (i = assoc->entity_inst_1; i <= assoc->entity_inst_2; i++)
  510. sunoem_led_get_byentity(intf, assoc->entity_id_1, i,
  511. ledtype);
  512. /* second range set - id 3 and 4 must be equal */
  513. if (assoc->entity_id_3 == assoc->entity_id_4)
  514. for (i = assoc->entity_inst_3; i <= assoc->entity_inst_4; i++)
  515. sunoem_led_get_byentity(intf, assoc->entity_id_3, i,
  516. ledtype);
  517. } else {
  518. /*
  519. * handle entity list
  520. */
  521. sunoem_led_get_byentity(intf, assoc->entity_id_1,
  522. assoc->entity_inst_1, ledtype);
  523. sunoem_led_get_byentity(intf, assoc->entity_id_2,
  524. assoc->entity_inst_2, ledtype);
  525. sunoem_led_get_byentity(intf, assoc->entity_id_3,
  526. assoc->entity_inst_3, ledtype);
  527. sunoem_led_get_byentity(intf, assoc->entity_id_4,
  528. assoc->entity_inst_4, ledtype);
  529. }
  530. }
  531. __sdr_list_empty(alist);
  532. if (ret_get == -1)
  533. return (-1);
  534. return (0);
  535. }
  536. /*
  537. * IPMI Request Data: 7 bytes
  538. *
  539. * [byte 0] devAddr Value from the "Device Slave Address" field in
  540. * LED's Generic Device Locator record in the SDR
  541. * [byte 1] led LED Type: OK2RM, ACT, LOCATE, SERVICE
  542. * [byte 2] ctrlrAddr Controller address; value from the "Device
  543. * Access Address" field, 0x20 if the LED is local
  544. * [byte 3] hwInfo The OEM field from the SDR record
  545. * [byte 4] mode LED Mode: OFF, ON, STANDBY, SLOW, FAST
  546. * [byte 5] force TRUE - directly access the device
  547. * FALSE - go thru its controller
  548. * Ignored if LED is local
  549. * [byte 6] role Used by BMC for authorization purposes
  550. *
  551. * The format below is for Sun Blade Modular systems only
  552. * [byte 5] entityID The entityID field from the SDR record
  553. * [byte 6] entityIns The entityIns field from the SDR record
  554. * [byte 7] force TRUE - directly access the device
  555. * FALSE - go thru its controller
  556. * Ignored if LED is local
  557. * [byte 8] role Used by BMC for authorization purposes
  558. *
  559. *
  560. * IPMI Response Data: 1 byte
  561. *
  562. * [byte 0] mode LED Mode: OFF, ON, STANDBY, SLOW, FAST
  563. */
  564. static int
  565. ipmi_sunoem_led_set(struct ipmi_intf * intf, int argc, char ** argv)
  566. {
  567. struct ipmi_rs * rsp;
  568. struct sdr_record_list *sdr;
  569. struct sdr_record_list *alist, *a;
  570. struct sdr_record_entity_assoc *assoc;
  571. int ledmode;
  572. int ledtype = 0xFF;
  573. int i;
  574. /*
  575. * sunoem led/sbled set <id> <mode> [type]
  576. */
  577. if (argc < 2 || strncmp(argv[0], "help", 4) == 0) {
  578. ipmi_sunoem_usage();
  579. return (0);
  580. }
  581. ledmode = str2val(argv[1], sunoem_led_mode_vals);
  582. if (ledmode == 0xFF) {
  583. ledmode = str2val(argv[1], sunoem_led_mode_optvals);
  584. if (ledmode == 0xFF) {
  585. lprintf(LOG_NOTICE, "Invalid LED Mode: %s", argv[1]);
  586. return (-1);
  587. }
  588. }
  589. if (argc > 3) {
  590. ledtype = str2val(argv[2], sunoem_led_type_vals);
  591. if (ledtype == 0xFF)
  592. lprintf(LOG_ERR,
  593. "Unknown ledtype, will use data from the SDR oem field");
  594. }
  595. if (strncasecmp(argv[0], "all", 3) == 0) {
  596. /* do all generic sensors */
  597. alist = ipmi_sdr_find_sdr_bytype(intf,
  598. SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR);
  599. if (alist == NULL)
  600. return (-1);
  601. for (a = alist; a != NULL; a = a->next) {
  602. if (a->type != SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR)
  603. continue;
  604. if (a->record.genloc->entity.logical)
  605. continue;
  606. rsp = sunoem_led_set(intf, a->record.genloc, ledtype, ledmode);
  607. if (rsp && rsp->ccode == 0)
  608. led_print((const char *) a->record.genloc->id_string,
  609. PRINT_NORMAL, ledmode);
  610. else
  611. ret_set = -1;
  612. }
  613. __sdr_list_empty(alist);
  614. if (ret_set == -1)
  615. return (-1);
  616. return (0);
  617. }
  618. /* look up generic device locator records in SDR */
  619. sdr = ipmi_sdr_find_sdr_byid(intf, argv[0]);
  620. if (sdr == NULL) {
  621. lprintf(LOG_ERR, "No Sensor Data Record found for %s", argv[0]);
  622. return (-1);
  623. }
  624. if (sdr->type != SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR) {
  625. lprintf(LOG_ERR, "Invalid SDR type %d", sdr->type);
  626. return (-1);
  627. }
  628. if (!sdr->record.genloc->entity.logical) {
  629. /*
  630. * handle physical entity
  631. */
  632. rsp = sunoem_led_set(intf, sdr->record.genloc, ledtype, ledmode);
  633. if (rsp && rsp->ccode == 0)
  634. led_print(argv[0], PRINT_NORMAL, ledmode);
  635. else
  636. return (-1);
  637. return (0);
  638. }
  639. /*
  640. * handle logical entity for LED grouping
  641. */
  642. lprintf(LOG_INFO, "LED %s is logical device", argv[0]);
  643. /* get entity assoc records */
  644. alist = ipmi_sdr_find_sdr_bytype(intf, SDR_RECORD_TYPE_ENTITY_ASSOC);
  645. if (alist == NULL)
  646. return (-1);
  647. for (a = alist; a != NULL; a = a->next) {
  648. if (a->type != SDR_RECORD_TYPE_ENTITY_ASSOC)
  649. continue;
  650. assoc = a->record.entassoc;
  651. if (assoc == NULL)
  652. continue;
  653. /* check that the entity id/instance matches our generic record */
  654. if (assoc->entity.id != sdr->record.genloc->entity.id
  655. || assoc->entity.instance
  656. != sdr->record.genloc->entity.instance)
  657. continue;
  658. if (assoc->flags.isrange) {
  659. /*
  660. * handle ranged entity associations
  661. *
  662. * the test for non-zero entity id is handled in
  663. * sunoem_led_get_byentity()
  664. */
  665. /* first range set - id 1 and 2 must be equal */
  666. if (assoc->entity_id_1 == assoc->entity_id_2)
  667. for (i = assoc->entity_inst_1; i <= assoc->entity_inst_2; i++)
  668. sunoem_led_set_byentity(intf, assoc->entity_id_1, i,
  669. ledtype, ledmode);
  670. /* second range set - id 3 and 4 must be equal */
  671. if (assoc->entity_id_3 == assoc->entity_id_4)
  672. for (i = assoc->entity_inst_3; i <= assoc->entity_inst_4; i++)
  673. sunoem_led_set_byentity(intf, assoc->entity_id_3, i,
  674. ledtype, ledmode);
  675. } else {
  676. /*
  677. * handle entity list
  678. */
  679. sunoem_led_set_byentity(intf, assoc->entity_id_1,
  680. assoc->entity_inst_1, ledtype, ledmode);
  681. sunoem_led_set_byentity(intf, assoc->entity_id_2,
  682. assoc->entity_inst_2, ledtype, ledmode);
  683. sunoem_led_set_byentity(intf, assoc->entity_id_3,
  684. assoc->entity_inst_3, ledtype, ledmode);
  685. sunoem_led_set_byentity(intf, assoc->entity_id_4,
  686. assoc->entity_inst_4, ledtype, ledmode);
  687. }
  688. }
  689. __sdr_list_empty(alist);
  690. if (ret_set == -1)
  691. return (-1);
  692. return (0);
  693. }
  694. static int
  695. ipmi_sunoem_sshkey_del(struct ipmi_intf * intf, uint8_t uid)
  696. {
  697. struct ipmi_rs * rsp;
  698. struct ipmi_rq req;
  699. memset(&req, 0, sizeof(struct ipmi_rq));
  700. req.msg.netfn = IPMI_NETFN_SUNOEM;
  701. req.msg.cmd = IPMI_SUNOEM_DEL_SSH_KEY;
  702. req.msg.data = &uid;
  703. req.msg.data_len = 1;
  704. rsp = intf->sendrecv(intf, &req);
  705. if (rsp == NULL) {
  706. lprintf(LOG_ERR, "Unable to delete ssh key for UID %d", uid);
  707. return (-1);
  708. } else if (rsp->ccode > 0) {
  709. lprintf(LOG_ERR, "Unable to delete ssh key for UID %d: %s", uid,
  710. val2str(rsp->ccode, completion_code_vals));
  711. return (-1);
  712. }
  713. printf("Deleted SSH key for user id %d\n", uid);
  714. return (0);
  715. }
  716. #define SSHKEY_BLOCK_SIZE 64
  717. static int
  718. ipmi_sunoem_sshkey_set(struct ipmi_intf * intf, uint8_t uid, char * ifile)
  719. {
  720. struct ipmi_rs * rsp;
  721. struct ipmi_rq req;
  722. FILE * fp;
  723. int count = 0;
  724. uint8_t wbuf[SSHKEY_BLOCK_SIZE + 3];
  725. int32_t i_size = 0;
  726. int32_t r = 0;
  727. int32_t size = 0;
  728. if (ifile == NULL) {
  729. lprintf(LOG_ERR, "Invalid or misisng input filename.");
  730. return (-1);
  731. }
  732. fp = ipmi_open_file_read(ifile);
  733. if (fp == NULL) {
  734. lprintf(LOG_ERR, "Unable to open file '%s' for reading.", ifile);
  735. return (-1);
  736. }
  737. memset(&req, 0, sizeof(struct ipmi_rq));
  738. req.msg.netfn = IPMI_NETFN_SUNOEM;
  739. req.msg.cmd = IPMI_SUNOEM_SET_SSH_KEY;
  740. req.msg.data = wbuf;
  741. if (fseek(fp, 0, SEEK_END) == (-1)) {
  742. lprintf(LOG_ERR, "Failed to seek in file '%s'.", ifile);
  743. if (fp != NULL)
  744. fclose(fp);
  745. return (-1);
  746. }
  747. size = (int32_t) ftell(fp);
  748. if (size < 0) {
  749. lprintf(LOG_ERR, "Failed to seek in file '%s'.", ifile);
  750. if (fp != NULL)
  751. fclose(fp);
  752. return (-1);
  753. } else if (size == 0) {
  754. lprintf(LOG_ERR, "File '%s' is empty.", ifile);
  755. if (fp != NULL)
  756. fclose(fp);
  757. return (-1);
  758. }
  759. if (fseek(fp, 0, SEEK_SET) == (-1)) {
  760. lprintf(LOG_ERR, "Failed to seek in file '%s'.", ifile);
  761. if (fp != NULL)
  762. fclose(fp);
  763. return (-1);
  764. }
  765. printf("Setting SSH key for user id %d...", uid);
  766. for (r = 0; r < size; r += i_size) {
  767. i_size = size - r;
  768. if (i_size > SSHKEY_BLOCK_SIZE)
  769. i_size = SSHKEY_BLOCK_SIZE;
  770. memset(wbuf, 0, SSHKEY_BLOCK_SIZE);
  771. fseek(fp, r, SEEK_SET);
  772. count = fread(wbuf + 3, 1, i_size, fp);
  773. if (count != i_size) {
  774. printf("failed\n");
  775. lprintf(LOG_ERR, "Unable to read %ld bytes from file '%s'.", i_size,
  776. ifile);
  777. if (fp != NULL)
  778. fclose(fp);
  779. return (-1);
  780. }
  781. printf(".");
  782. fflush(stdout);
  783. wbuf[0] = uid;
  784. if ((r + SSHKEY_BLOCK_SIZE) >= size)
  785. wbuf[1] = 0xff;
  786. else {
  787. if ((r / SSHKEY_BLOCK_SIZE) > UINT8_MAX) {
  788. printf("failed\n");
  789. lprintf(LOG_ERR, "Unable to pack byte %ld from file '%s'.", r,
  790. ifile);
  791. if (fp != NULL)
  792. fclose(fp);
  793. return (-1);
  794. }
  795. wbuf[1] = (uint8_t) (r / SSHKEY_BLOCK_SIZE);
  796. }
  797. wbuf[2] = (uint8_t) i_size;
  798. req.msg.data_len = i_size + 3;
  799. rsp = intf->sendrecv(intf, &req);
  800. if (rsp == NULL) {
  801. printf("failed\n");
  802. lprintf(LOG_ERR, "Unable to set ssh key for UID %d.", uid);
  803. if (fp != NULL)
  804. fclose(fp);
  805. return (-1);
  806. } /* if (rsp == NULL) */
  807. if (rsp->ccode != 0) {
  808. printf("failed\n");
  809. lprintf(LOG_ERR, "Unable to set ssh key for UID %d, %s.", uid,
  810. val2str(rsp->ccode, completion_code_vals));
  811. if (fp != NULL)
  812. fclose(fp);
  813. return (-1);
  814. } /* if (rsp->ccode != 0) */
  815. }
  816. printf("done\n");
  817. fclose(fp);
  818. return (0);
  819. }
  820. /*
  821. * This structure is used in both the request to and response from the BMC.
  822. */
  823. #define SUNOEM_CLI_LEGACY_VERSION 1
  824. #define SUNOEM_CLI_SEQNUM_VERSION 2
  825. #define SUNOEM_CLI_VERSION SUNOEM_CLI_SEQNUM_VERSION
  826. #define SUNOEM_CLI_HEADER 8 /* command + spare + handle */
  827. #define SUNOEM_CLI_BUF_SIZE (80 - SUNOEM_CLI_HEADER) /* Total 80 bytes */
  828. #define SUNOEM_CLI_MSG_SIZE(msg) (SUNOEM_CLI_HEADER + strlen((msg).buf) + 1)
  829. #ifdef HAVE_PRAGMA_PACK
  830. #pragma pack(push, 1)
  831. #endif
  832. typedef struct
  833. {
  834. /*
  835. * Set version to SUNOEM_CLI_VERSION.
  836. */
  837. uint8_t version;
  838. /*
  839. * The command in a request, or in a response indicates an error if
  840. * non-zero.
  841. */
  842. uint8_t command_response;
  843. uint8_t seqnum;
  844. uint8_t spare;
  845. /*
  846. * Opaque 4-byte handle, supplied in the response to an OPEN request,
  847. * and used in all subsequent POLL and CLOSE requests.
  848. */
  849. uint8_t handle[4];
  850. /*
  851. * The client data in a request, or the server data in a response. Must
  852. * by null terminated, i.e., it must be at least one byte, but can be
  853. * smaller if there's less data.
  854. */
  855. char buf[SUNOEM_CLI_BUF_SIZE];
  856. }__attribute__((packed)) sunoem_cli_msg_t;
  857. #ifdef HAVE_PRAGMA_PACK
  858. #pragma pack(pop)
  859. #endif
  860. /*
  861. * Command codes for the command_request field in each request.
  862. */
  863. #define SUNOEM_CLI_CMD_OPEN 0 /* Open a new connection */
  864. #define SUNOEM_CLI_CMD_FORCE 1 /* Close any existing connection, then open */
  865. #define SUNOEM_CLI_CMD_CLOSE 2 /* Close the current connection */
  866. #define SUNOEM_CLI_CMD_POLL 3 /* Poll for new data to/from the server */
  867. #define SUNOEM_CLI_CMD_EOF 4 /* Poll, client is out of data */
  868. #define SUNOEM_CLI_MAX_RETRY 3 /* Maximum number of retries */
  869. #define SUNOEM_CLI_INVALID_VER_ERR "Invalid version"
  870. #define SUNOEM_CLI_BUSY_ERR "Busy"
  871. typedef enum
  872. {
  873. C_CTL_B = 0x02, /* same as left arrow */
  874. C_CTL_C = 0x03,
  875. C_CTL_D = 0x04,
  876. C_CTL_F = 0x06, /* same as right arrow */
  877. C_CTL_N = 0x0E, /* same as down arrow */
  878. C_CTL_P = 0x10, /* same as up arrow */
  879. C_DEL = 0x7f
  880. } canon_char_t;
  881. static int
  882. sunoem_cli_unbufmode_start(FILE *f, struct termios *orig_ts)
  883. {
  884. struct termios ts;
  885. int rc;
  886. if ((rc = tcgetattr(fileno(f), &ts))) {
  887. return (rc);
  888. }
  889. *orig_ts = ts;
  890. ts.c_lflag &= ~(ICANON | ECHO | ISIG);
  891. ts.c_cc[VMIN] = 1;
  892. if ((rc = tcsetattr(fileno(f), TCSAFLUSH, &ts))) {
  893. return (rc);
  894. }
  895. return (0);
  896. }
  897. static int
  898. sunoem_cli_unbufmode_stop(FILE *f, struct termios *ts)
  899. {
  900. int rc;
  901. if ((rc = tcsetattr(fileno(f), TCSAFLUSH, ts))) {
  902. return (rc);
  903. }
  904. return (0);
  905. }
  906. static int
  907. ipmi_sunoem_cli(struct ipmi_intf * intf, int argc, char *argv[])
  908. {
  909. struct ipmi_rs *rsp;
  910. struct ipmi_rq req;
  911. sunoem_cli_msg_t cli_req;
  912. sunoem_cli_msg_t *cli_rsp;
  913. int arg_num = 0;
  914. int arg_pos = 0;
  915. time_t wait_time = 0;
  916. int retries;
  917. static uint8_t SunOemCliActingVersion = SUNOEM_CLI_VERSION;
  918. unsigned short first_char = 0; /*first char on the line*/
  919. struct termios orig_ts;
  920. int error = 0;
  921. time_t now = 0;
  922. int delay = 0;
  923. /* Prepare to open an SP shell session */
  924. memset(&cli_req, 0, sizeof(cli_req));
  925. cli_req.version = SunOemCliActingVersion;
  926. cli_req.command_response = SUNOEM_CLI_CMD_OPEN;
  927. if (argc > 0 && strcmp(argv[0], "force") == 0) {
  928. cli_req.command_response = SUNOEM_CLI_CMD_FORCE;
  929. argc--;
  930. argv++;
  931. }
  932. memset(&req, 0, sizeof(req));
  933. req.msg.netfn = IPMI_NETFN_SUNOEM;
  934. req.msg.cmd = IPMI_SUNOEM_CLI;
  935. req.msg.data = (uint8_t *) &cli_req;
  936. req.msg.data_len = SUNOEM_CLI_HEADER + 1;
  937. retries = 0;
  938. while (1) {
  939. cli_req.version = SunOemCliActingVersion;
  940. rsp = intf->sendrecv(intf, &req);
  941. if (rsp == NULL) {
  942. lprintf(LOG_ERR, "Sun OEM cli command failed");
  943. return (-1);
  944. }
  945. cli_rsp = (sunoem_cli_msg_t *) rsp->data;
  946. if ((cli_rsp->command_response != 0) || (rsp->ccode != 0)) {
  947. if (strncmp(cli_rsp->buf, SUNOEM_CLI_INVALID_VER_ERR,
  948. sizeof(SUNOEM_CLI_INVALID_VER_ERR) - 1) == 0
  949. || strncmp(&(cli_rsp->buf[1]), SUNOEM_CLI_INVALID_VER_ERR,
  950. sizeof(SUNOEM_CLI_INVALID_VER_ERR) - 1) == 0) {
  951. if (SunOemCliActingVersion == SUNOEM_CLI_VERSION) {
  952. /* Server doesn't support version SUNOEM_CLI_VERSION
  953. Fall back to legacy version, and try again*/
  954. SunOemCliActingVersion = SUNOEM_CLI_LEGACY_VERSION;
  955. continue;
  956. }
  957. /* Server doesn't support legacy version either */
  958. lprintf(LOG_ERR, "Failed to connect: %s", cli_rsp->buf);
  959. return (-1);
  960. } else if (strncmp(cli_rsp->buf, SUNOEM_CLI_BUSY_ERR,
  961. sizeof(SUNOEM_CLI_BUSY_ERR) - 1) == 0) {
  962. if (retries++ < SUNOEM_CLI_MAX_RETRY) {
  963. lprintf(LOG_INFO, "Failed to connect: %s, retrying",
  964. cli_rsp->buf);
  965. sleep(2);
  966. continue;
  967. }
  968. lprintf(LOG_ERR, "Failed to connect: %s", cli_rsp->buf);
  969. return (-1);
  970. } else {
  971. lprintf(LOG_ERR, "Failed to connect: %s", cli_rsp->buf);
  972. return (-1);
  973. }
  974. }
  975. break;
  976. }
  977. if (SunOemCliActingVersion == SUNOEM_CLI_SEQNUM_VERSION) {
  978. /*
  979. * Bit 1 of seqnum is used as an alternating sequence number
  980. * to allow a server that supports it to detect when a retry is being sent from the host IPMI driver.
  981. * Typically when this occurs, the server's last response message would have been dropped.
  982. * Once the server detects this condition, it will know that it should retry sending the response.
  983. */
  984. cli_req.seqnum ^= 0x1;
  985. }
  986. printf("Connected. Use ^D to exit.\n");
  987. fflush(NULL);
  988. /*
  989. * Remember the handle provided in the response, and issue a
  990. * series of "poll" commands to send and get data
  991. */
  992. memcpy(cli_req.handle, cli_rsp->handle, 4);
  993. cli_req.command_response = SUNOEM_CLI_CMD_POLL;
  994. /*
  995. * If no arguments make input unbuffered and so interactive
  996. */
  997. if (argc == 0) {
  998. if (sunoem_cli_unbufmode_start(stdin, &orig_ts)) {
  999. lprintf(LOG_ERR, "Failed to set interactive mode: %s",
  1000. strerror(errno));
  1001. return (-1);
  1002. }
  1003. }
  1004. while (rsp->ccode == 0 && cli_rsp->command_response == 0) {
  1005. int rc = 0;
  1006. int count = 0;
  1007. cli_req.buf[0] = '\0';
  1008. if (argc == 0) {
  1009. /*
  1010. * Accept input from stdin. Use select so we don't hang if
  1011. * there's no input to read. Select timeout is 500 msec.
  1012. */
  1013. struct timeval tv = { 0, 500000 }; /* 500 msec */
  1014. fd_set rfds;
  1015. FD_ZERO(&rfds);
  1016. FD_SET(0, &rfds);
  1017. rc = select(1, &rfds, NULL, NULL, &tv);
  1018. if (rc < 0) {
  1019. /* Select returned an error so close and exit */
  1020. printf("Broken pipe\n");
  1021. cli_req.command_response = SUNOEM_CLI_CMD_CLOSE;
  1022. } else if (rc > 0) {
  1023. /* Read data from stdin */
  1024. count = read(0, cli_req.buf, 1 /* sizeof (cli_req.buf) - 1 */);
  1025. /*
  1026. * If select said there was data but there was nothing to
  1027. * read. This implies user hit ^D.
  1028. * Also handle ^D input when pressed as first char at a new line.
  1029. */
  1030. if (count <= 0 || (first_char && cli_req.buf[0] == C_CTL_D)) {
  1031. cli_req.command_response = SUNOEM_CLI_CMD_EOF;
  1032. count = 0;
  1033. }
  1034. first_char = cli_req.buf[0] == '\n' || cli_req.buf[0] == '\r';
  1035. }
  1036. } else {
  1037. /*
  1038. * Get data from command line arguments
  1039. */
  1040. now = time(NULL);
  1041. if (now < wait_time) {
  1042. /* Do nothing; we're waiting */
  1043. } else if (arg_num >= argc) {
  1044. /* Last arg was sent. Set EOF */
  1045. cli_req.command_response = SUNOEM_CLI_CMD_EOF;
  1046. } else if (strncmp(argv[arg_num], "@wait=", 6) == 0) {
  1047. /* This is a wait command */
  1048. char *s = &argv[arg_num][6];
  1049. delay = 0;
  1050. if (*s != '\0') {
  1051. if (str2int(s, &delay)) {
  1052. delay = 0;
  1053. }
  1054. if (delay < 0) {
  1055. delay = 0;
  1056. }
  1057. }
  1058. wait_time = now + delay;
  1059. arg_num++;
  1060. } else {
  1061. /*
  1062. * Take data from args. It may be that the argument is larger
  1063. * than the request buffer can hold. So pull off BUF_SIZE
  1064. * number of characters at a time. When we've consumed the
  1065. * entire arg, append a newline and advance to the next arg.
  1066. */
  1067. int i;
  1068. char *s = argv[arg_num];
  1069. for (i = arg_pos;
  1070. s[i] != '\0' && count < (SUNOEM_CLI_BUF_SIZE - 2);
  1071. i++, count++) {
  1072. cli_req.buf[count] = s[i];
  1073. }
  1074. if (s[i] == '\0') {
  1075. /* Reached end of the arg string, so append a newline */
  1076. cli_req.buf[count++] = '\n';
  1077. /* Reset pos to 0 and advance to the next arg next time */
  1078. arg_pos = 0;
  1079. arg_num++;
  1080. } else {
  1081. /*
  1082. * Otherwise, there's still more characters in the arg
  1083. * to send, so remember where we left off
  1084. */
  1085. arg_pos = i;
  1086. }
  1087. }
  1088. }
  1089. /*
  1090. * Now send the clients's data (if any) and get data back from the
  1091. * server. Loop while the server is giving us data until we suck
  1092. * it dry.
  1093. */
  1094. do {
  1095. cli_req.buf[count++] = '\0'; /* Terminate the string */
  1096. memset(&req, 0, sizeof(req));
  1097. req.msg.netfn = IPMI_NETFN_SUNOEM;
  1098. req.msg.cmd = 0x19;
  1099. req.msg.data = (uint8_t *) &cli_req;
  1100. req.msg.data_len = SUNOEM_CLI_HEADER + count;
  1101. for (retries = 0; retries <= SUNOEM_CLI_MAX_RETRY; retries++) {
  1102. rsp = intf->sendrecv(intf, &req);
  1103. if (rsp == NULL) {
  1104. lprintf(LOG_ERR, "Communication error.");
  1105. error = 1;
  1106. goto cleanup;
  1107. }
  1108. if (rsp->ccode == IPMI_CC_TIMEOUT) { /* Retry if timed out. */
  1109. if (retries == SUNOEM_CLI_MAX_RETRY) { /* If it's the last retry. */
  1110. lprintf(LOG_ERR, "Excessive timeout.");
  1111. error = 1;
  1112. goto cleanup;
  1113. }
  1114. continue;
  1115. }
  1116. break;
  1117. } /* for (retries = 0; retries <= SUNOEM_CLI_MAX_RETRY; retries++) */
  1118. if (SunOemCliActingVersion == SUNOEM_CLI_SEQNUM_VERSION) {
  1119. cli_req.seqnum ^= 0x1; /* Toggle sequence number after request is sent */
  1120. }
  1121. cli_rsp = (sunoem_cli_msg_t *) rsp->data;
  1122. /* Make sure response string is null terminated */
  1123. cli_rsp->buf[sizeof(cli_rsp->buf) - 1] = '\0';
  1124. printf("%s", cli_rsp->buf);
  1125. fflush(NULL); /* Flush partial lines to stdout */
  1126. count = 0; /* Don't re-send the client's data */
  1127. if (cli_req.command_response == SUNOEM_CLI_CMD_EOF
  1128. && cli_rsp->command_response != 0 && rsp->ccode == 0) {
  1129. cli_rsp->command_response = 1;
  1130. }
  1131. } while (cli_rsp->command_response == 0 && cli_rsp->buf[0] != '\0');
  1132. }
  1133. cleanup:
  1134. /* Restore original input mode if cli was running interactively */
  1135. if (argc == 0) {
  1136. if (sunoem_cli_unbufmode_stop(stdin, &orig_ts)) {
  1137. lprintf(LOG_ERR, "Failed to restore interactive mode: %s",
  1138. strerror(errno));
  1139. return (-1);
  1140. }
  1141. }
  1142. return ((error == 0 && cli_rsp->command_response == SUNOEM_SUCCESS) ? 0 : -1);
  1143. }
  1144. #define ECHO_DATA_SIZE 64
  1145. #ifdef HAVE_PRAGMA_PACK
  1146. #pragma pack(push, 1)
  1147. #endif
  1148. typedef struct
  1149. {
  1150. uint16_t seq_num;
  1151. unsigned char data[ECHO_DATA_SIZE];
  1152. }__attribute__((packed)) sunoem_echo_msg_t;
  1153. #ifdef HAVE_PRAGMA_PACK
  1154. #pragma pack(pop)
  1155. #endif
  1156. /*
  1157. * Send and receive X packets to the BMC. Each packet has a
  1158. * payload size of (sunoem_echo_msg_t) bytes. Each packet is tagged with a
  1159. * sequence number
  1160. */
  1161. static int
  1162. ipmi_sunoem_echo(struct ipmi_intf * intf, int argc, char *argv[])
  1163. {
  1164. struct ipmi_rs *rsp;
  1165. struct ipmi_rq req;
  1166. sunoem_echo_msg_t echo_req;
  1167. sunoem_echo_msg_t *echo_rsp;
  1168. struct timeval start_time;
  1169. struct timeval end_time;
  1170. int rc = 0;
  1171. int received = 0;
  1172. int transmitted = 0;
  1173. int quiet_mode = 0;
  1174. uint16_t num, i, j;
  1175. uint32_t total_time, resp_time, min_time, max_time;
  1176. if (argc < 1) {
  1177. return (1);
  1178. }
  1179. if (argc == 2) {
  1180. if (*(argv[1]) == 'q') {
  1181. quiet_mode = 1;
  1182. } else {
  1183. lprintf(LOG_ERR, "Unknown option '%s' given.", argv[1]);
  1184. return (-1);
  1185. }
  1186. } else if (argc > 2) {
  1187. lprintf(LOG_ERR,
  1188. "Too many parameters given. See help for more information.");
  1189. return (-1);
  1190. }
  1191. /* The number of packets to send/receive */
  1192. if (str2ushort(argv[0], &num) != 0) {
  1193. lprintf(LOG_ERR,
  1194. "Given number of packets is either invalid or out of range.");
  1195. return (-1);
  1196. }
  1197. /* Fill in data packet */
  1198. for (i = 0; i < ECHO_DATA_SIZE; i++) {
  1199. echo_req.data[i] = (uint8_t) i;
  1200. }
  1201. memset(&req, 0, sizeof(req));
  1202. req.msg.netfn = IPMI_NETFN_SUNOEM;
  1203. req.msg.cmd = IPMI_SUNOEM_ECHO;
  1204. req.msg.data = (uint8_t *) &echo_req;
  1205. req.msg.data_len = sizeof(sunoem_echo_msg_t);
  1206. echo_req.seq_num = i;
  1207. min_time = INT_MAX;
  1208. max_time = 0;
  1209. total_time = 0;
  1210. for (i = 0; i < num; i++) {
  1211. echo_req.seq_num = i;
  1212. transmitted++;
  1213. gettimeofday(&start_time, NULL);
  1214. rsp = intf->sendrecv(intf, &req);
  1215. gettimeofday(&end_time, NULL);
  1216. resp_time = ((end_time.tv_sec - start_time.tv_sec) * 1000)
  1217. + ((end_time.tv_usec - start_time.tv_usec) / 1000);
  1218. if ((rsp == NULL) || (rsp->ccode != 0)) {
  1219. lprintf(LOG_ERR, "Sun OEM echo command failed. Seq # %d",
  1220. echo_req.seq_num);
  1221. rc = (-2);
  1222. break;
  1223. }
  1224. echo_rsp = (sunoem_echo_msg_t *) rsp->data;
  1225. /* Test if sequence # is valid */
  1226. if (echo_rsp->seq_num != echo_req.seq_num) {
  1227. printf("Invalid Seq # Expecting %d Received %d\n", echo_req.seq_num,
  1228. echo_rsp->seq_num);
  1229. rc = (-2);
  1230. break;
  1231. }
  1232. /* Test if response length is valid */
  1233. if (rsp->session.msglen == req.msg.data_len) {
  1234. printf("Invalid payload size for seq # %d. "
  1235. "Expecting %d Received %d\n", echo_rsp->seq_num,
  1236. req.msg.data_len, rsp->session.msglen);
  1237. rc = (-2);
  1238. break;
  1239. }
  1240. /* Test if the data is valid */
  1241. for (j = 0; j < ECHO_DATA_SIZE; j++) {
  1242. if (echo_rsp->data[j] != j) {
  1243. printf("Corrupt data packet. Seq # %d Offset %d\n",
  1244. echo_rsp->seq_num, j);
  1245. break;
  1246. }
  1247. } /* for (j = 0; j < ECHO_DATA_SIZE; j++) */
  1248. /* If the for loop terminated early - data is corrupt */
  1249. if (j != ECHO_DATA_SIZE) {
  1250. rc = (-2);
  1251. break;
  1252. }
  1253. /* cumalative time */
  1254. total_time += resp_time;
  1255. /* min time */
  1256. if (resp_time < min_time) {
  1257. min_time = resp_time;
  1258. }
  1259. /* max time */
  1260. if (resp_time > max_time) {
  1261. max_time = resp_time;
  1262. }
  1263. received++;
  1264. if (!quiet_mode) {
  1265. printf("Receive %lu Bytes - Seq. # %d time=%d ms\n",
  1266. sizeof(sunoem_echo_msg_t), echo_rsp->seq_num, resp_time);
  1267. }
  1268. } /* for (i = 0; i < num; i++) */
  1269. printf("%d packets transmitted, %d packets received\n", transmitted,
  1270. received);
  1271. if (received) {
  1272. printf("round-trip min/avg/max = %d/%d/%d ms\n", min_time,
  1273. total_time / received, max_time);
  1274. }
  1275. return (rc);
  1276. } /* ipmi_sunoem_echo(...) */
  1277. #ifdef HAVE_PRAGMA_PACK
  1278. #pragma pack(push, 1)
  1279. #endif
  1280. typedef struct
  1281. {
  1282. unsigned char oem_record_ver_num;
  1283. unsigned char major;
  1284. unsigned char minor;
  1285. unsigned char update;
  1286. unsigned char micro;
  1287. char nano[10];
  1288. char revision[10];
  1289. char version[40];
  1290. /*
  1291. * When adding new fields (using the spare bytes),
  1292. * add it immediately after the spare field to
  1293. * ensure backward compatability.
  1294. *
  1295. * e.g. char version[40];
  1296. * unsigned char spare[11];
  1297. * int new_item;
  1298. * } sunoem_version_response_t;
  1299. */
  1300. unsigned char spare[15];
  1301. }__attribute__((packed)) sunoem_version_response_t;
  1302. #ifdef HAVE_PRAGMA_PACK
  1303. #pragma pack(pop)
  1304. #endif
  1305. typedef struct
  1306. {
  1307. unsigned char major;
  1308. unsigned char minor;
  1309. unsigned char update;
  1310. unsigned char micro;
  1311. } supported_version_t;
  1312. static int
  1313. ipmi_sunoem_getversion(struct ipmi_intf * intf,
  1314. sunoem_version_response_t **version_rsp)
  1315. {
  1316. struct ipmi_rs *rsp;
  1317. struct ipmi_rq req;
  1318. memset(&req, 0, sizeof(req));
  1319. req.msg.netfn = IPMI_NETFN_SUNOEM;
  1320. req.msg.cmd = IPMI_SUNOEM_VERSION;
  1321. req.msg.data = NULL;
  1322. req.msg.data_len = 0;
  1323. rsp = intf->sendrecv(intf, &req);
  1324. if (rsp == NULL) {
  1325. lprintf(LOG_ERR, "Sun OEM Get SP Version Failed.");
  1326. return (-1);
  1327. }
  1328. if (rsp->ccode != 0) {
  1329. lprintf(LOG_ERR, "Sun OEM Get SP Version Failed: %d", rsp->ccode);
  1330. return (-1);
  1331. }
  1332. *version_rsp = (sunoem_version_response_t *) rsp->data;
  1333. return (0);
  1334. }
  1335. static void
  1336. ipmi_sunoem_print_required_version(const supported_version_t* supp_ver)
  1337. {
  1338. lprintf(LOG_ERR, "Command is not supported by this version of ILOM,"
  1339. " required at least: %d.%d.%d.%d", supp_ver->major, supp_ver->minor,
  1340. supp_ver->update, supp_ver->micro);
  1341. }
  1342. /*
  1343. * Function checks current version result against required version.
  1344. * Returns:
  1345. * - negative value if current ILOM version is smaller than required or
  1346. * in case of error
  1347. * - positive value if current ILOM version is greater than required
  1348. * - 0 if there is an exact ILOM version match
  1349. */
  1350. static int
  1351. ipmi_sunoem_checkversion(struct ipmi_intf * intf, supported_version_t* supp_ver)
  1352. {
  1353. sunoem_version_response_t *version_rsp;
  1354. int i = 1;
  1355. if (ipmi_sunoem_getversion(intf, &version_rsp)) {
  1356. lprintf(LOG_ERR, "Unable to get ILOM version");
  1357. return (-1);
  1358. }
  1359. if (version_rsp->major < supp_ver->major) return (-i);
  1360. if (version_rsp->major > supp_ver->major) return (i);
  1361. /*version_rsp->major == supp_ver->major*/
  1362. ++i;
  1363. if (version_rsp->minor < supp_ver->minor) return (-i);
  1364. if (version_rsp->minor > supp_ver->minor) return (i);
  1365. /*version_rsp->minor == supp_ver->minor*/
  1366. ++i;
  1367. if (version_rsp->update < supp_ver->update) return (-i);
  1368. if (version_rsp->update > supp_ver->update) return (i);
  1369. /*version_rsp->update == supp_ver->update*/
  1370. ++i;
  1371. if (version_rsp->micro < supp_ver->micro) return (-i);
  1372. if (version_rsp->micro > supp_ver->micro) return (i);
  1373. /*version_rsp->micro == supp_ver->micro*/
  1374. return (0);
  1375. }
  1376. /*
  1377. * Extract the SP version data including
  1378. * - major #
  1379. * - minor #
  1380. * - update #
  1381. * - micro #
  1382. * - nano #
  1383. * - Revision/Build #
  1384. */
  1385. static int
  1386. ipmi_sunoem_version(struct ipmi_intf * intf)
  1387. {
  1388. sunoem_version_response_t *version_rsp;
  1389. int rc = ipmi_sunoem_getversion(intf, &version_rsp);
  1390. if (!rc) {
  1391. printf("Version: %s\n", version_rsp->version);
  1392. }
  1393. return (rc);
  1394. }
  1395. /*
  1396. * IPMI Max string length is 16 bytes
  1397. * define in usr/src/common/include/ami/IPMI_SDRRecord.h
  1398. */
  1399. #define MAX_ID_STR_LEN 16
  1400. #define MAX_SUNOEM_NAC_SIZE 64
  1401. #define LUAPI_MAX_OBJ_PATH_LEN 256
  1402. #define LUAPI_MAX_OBJ_VAL_LEN 1024
  1403. #ifdef HAVE_PRAGMA_PACK
  1404. #pragma pack(push, 1)
  1405. #endif
  1406. typedef struct
  1407. {
  1408. unsigned char seq_num;
  1409. char nac_name[MAX_SUNOEM_NAC_SIZE];
  1410. }__attribute__((packed)) sunoem_nacname_t;
  1411. #ifdef HAVE_PRAGMA_PACK
  1412. #pragma pack(pop)
  1413. #endif
  1414. /*
  1415. * Retrieve the full NAC name of the IPMI target.
  1416. *
  1417. * The returned nac name may be larger than the payload size.
  1418. * In which case, it make take several request/payload to retrieve
  1419. * the entire full path name
  1420. *
  1421. * The initial seq_num is set to 0. If the return seq_num is incremented,
  1422. * only the 1st 72 bytes of the nac name is returned and the caller
  1423. * needs to get the next set of string data.
  1424. * If the returned seq_num is identical to the input seq_num, all data
  1425. * has been returned.
  1426. */
  1427. static int
  1428. ipmi_sunoem_nacname(struct ipmi_intf * intf, int argc, char *argv[])
  1429. {
  1430. struct ipmi_rs *rsp;
  1431. struct ipmi_rq req;
  1432. sunoem_nacname_t nacname_req;
  1433. sunoem_nacname_t *nacname_rsp;
  1434. char full_nac_name[LUAPI_MAX_OBJ_PATH_LEN];
  1435. if (argc < 1) {
  1436. return (1);
  1437. }
  1438. if (strlen(argv[0]) > MAX_ID_STR_LEN) {
  1439. lprintf(LOG_ERR,
  1440. "Sun OEM nacname command failed: Max size on IPMI name");
  1441. return (-1);
  1442. }
  1443. nacname_req.seq_num = 0;
  1444. strcpy(nacname_req.nac_name, argv[0]);
  1445. full_nac_name[0] = '\0';
  1446. while (1) {
  1447. memset(&req, 0, sizeof(req));
  1448. req.msg.netfn = IPMI_NETFN_SUNOEM;
  1449. req.msg.cmd = IPMI_SUNOEM_NACNAME;
  1450. req.msg.data = (uint8_t *) &nacname_req;
  1451. req.msg.data_len = sizeof(sunoem_nacname_t);
  1452. rsp = intf->sendrecv(intf, &req);
  1453. if (rsp == NULL) {
  1454. lprintf(LOG_ERR, "Sun OEM nacname command failed.");
  1455. return (-1);
  1456. }
  1457. if (rsp->ccode != 0) {
  1458. lprintf(LOG_ERR, "Sun OEM nacname command failed: %d", rsp->ccode);
  1459. return (-1);
  1460. }
  1461. nacname_rsp = (sunoem_nacname_t *) rsp->data;
  1462. strncat(full_nac_name, nacname_rsp->nac_name, MAX_SUNOEM_NAC_SIZE);
  1463. /*
  1464. * break out of the loop if there is no more data
  1465. * In most cases, if not all, the NAC name fits into a
  1466. * single payload
  1467. */
  1468. if (nacname_req.seq_num == nacname_rsp->seq_num) {
  1469. break;
  1470. }
  1471. /* Get the next seq of string bytes */
  1472. nacname_req.seq_num = nacname_rsp->seq_num;
  1473. /* Check if we exceeded the size of the full nac name */
  1474. if ((nacname_req.seq_num * MAX_SUNOEM_NAC_SIZE) > LUAPI_MAX_OBJ_PATH_LEN) {
  1475. lprintf(LOG_ERR,
  1476. "Sun OEM nacname command failed: invalid path length");
  1477. return (-1);
  1478. }
  1479. }
  1480. printf("NAC Name: %s\n", full_nac_name);
  1481. return (0);
  1482. }
  1483. /* Constants used by ipmi_sunoem_getval */
  1484. #define MAX_SUNOEM_VAL_PAYLOAD 79
  1485. #define MAX_SUNOEM_VAL_COMPACT_PAYLOAD 56
  1486. /*
  1487. * SUNOEM GET/SET LUAPI Commands
  1488. *
  1489. * SUNOEM_REQ_VAL - Request LUAPI Property Value
  1490. * SUNOEM_GET_VAL - Return the value from SUNOEM_REQ_VAL
  1491. * SUNOEM_SET_VAL - Set the LUAPI Property value
  1492. * SUNOEM_GET_STATUS - Return the Status from SUNOEM_SET_VAL
  1493. */
  1494. #define SUNOEM_REQ_VAL 1
  1495. #define SUNOEM_GET_VAL 2
  1496. #define SUNOEM_SET_VAL 3
  1497. #define SUNOEM_GET_STATUS 4
  1498. /* Status Code */
  1499. #define SUNOEM_REQ_RECV 1
  1500. #define SUNOEM_REQ_FAILED 2
  1501. #define SUNOEM_DATA_READY 3
  1502. #define SUNOEM_DATA_NOT_READY 4
  1503. #define SUNOEM_DATA_NOT_FOUND 5
  1504. #define GETVAL_MAX_RETRIES 5
  1505. /* Parameter type Codes */
  1506. #define SUNOEM_LUAPI_TARGET 0
  1507. #define SUNOEM_LUAPI_VALUE 1
  1508. #ifdef HAVE_PRAGMA_PACK
  1509. #pragma pack(push, 1)
  1510. #endif
  1511. typedef struct
  1512. {
  1513. unsigned char cmd_code;
  1514. unsigned char luapi_value[MAX_SUNOEM_VAL_PAYLOAD];
  1515. }__attribute__((packed)) sunoem_getval_t;
  1516. #ifdef HAVE_PRAGMA_PACK
  1517. #pragma pack(pop)
  1518. #endif
  1519. /*
  1520. * REQUEST PAYLOAD
  1521. *
  1522. * cmd_code - SUNOEM GET/SET LUAPI Cmds - see above
  1523. * param_type: 0: luapi_data contains the luapi property name
  1524. * 1: luapi_data contains the luapi value
  1525. * luapi_data: Either luapi property name or value
  1526. * tid: Transaction ID. If 0. This is the initial request for the
  1527. * param_type. If tid > 0, this luapi_data string is a concatenation
  1528. * of the previous request. Handle cases where the LUAPI target name
  1529. * or value is > MAX_SUNOEM_VAL_COMPACT_PAYLOAD
  1530. * eof: If non zero, this is the last payload for the request
  1531. */
  1532. #ifdef HAVE_PRAGMA_PACK
  1533. #pragma pack(push, 1)
  1534. #endif
  1535. typedef struct
  1536. {
  1537. unsigned char cmd_code;
  1538. unsigned char param_type;
  1539. unsigned char tid;
  1540. unsigned char eof;
  1541. char luapi_data[MAX_SUNOEM_VAL_COMPACT_PAYLOAD];
  1542. }__attribute__((packed)) sunoem_setval_t;
  1543. #ifdef HAVE_PRAGMA_PACK
  1544. #pragma pack(pop)
  1545. #endif
  1546. /*
  1547. * RESPONSE PAYLOAD
  1548. *
  1549. * status_code - see above for code definitions
  1550. * tid - transaction ID - assigned ny the ILOM stack
  1551. */
  1552. #ifdef HAVE_PRAGMA_PACK
  1553. #pragma pack(push, 1)
  1554. #endif
  1555. typedef struct
  1556. {
  1557. unsigned char status_code;
  1558. unsigned char tid;
  1559. }__attribute__((packed)) sunoem_setval_resp_t;
  1560. #ifdef HAVE_PRAGMA_PACK
  1561. #pragma pack(pop)
  1562. #endif
  1563. /*
  1564. * Return the ILOM target property value
  1565. */
  1566. static int
  1567. ipmi_sunoem_getval(struct ipmi_intf * intf, int argc, char *argv[])
  1568. {
  1569. struct ipmi_rs *rsp;
  1570. struct ipmi_rq req;
  1571. sunoem_getval_t getval_req;
  1572. sunoem_getval_t *getval_rsp;
  1573. int i;
  1574. const char* sp_path = "/SP";
  1575. supported_version_t supp_ver = { 3, 2, 0, 0 };
  1576. if (argc < 1) {
  1577. return (1);
  1578. }
  1579. if (strlen(argv[0]) > MAX_SUNOEM_VAL_PAYLOAD) {
  1580. lprintf(LOG_ERR,
  1581. "Sun OEM get value command failed: Max size on IPMI name");
  1582. return (-1);
  1583. }
  1584. if ((ipmi_sunoem_checkversion(intf, &supp_ver) < 0)
  1585. && (!strncmp(argv[0], sp_path, strlen(sp_path)))) {
  1586. argv[0][1] = 'X'; /*replace SP by X to gain access to hidden properties*/
  1587. memmove(&argv[0][2], &argv[0][3], strlen(argv[0]) - 2);
  1588. }
  1589. /*
  1590. * Setup the initial request to fetch the data.
  1591. * Upon function return, the next cmd (SUNOEM_GET_VAL)
  1592. * can be requested.
  1593. */
  1594. memset(&getval_req, 0, sizeof(getval_req));
  1595. strncpy((char*) getval_req.luapi_value, argv[0], MAX_SUNOEM_VAL_PAYLOAD);
  1596. getval_req.cmd_code = SUNOEM_REQ_VAL;
  1597. memset(&req, 0, sizeof(req));
  1598. req.msg.netfn = IPMI_NETFN_SUNOEM;
  1599. req.msg.cmd = IPMI_SUNOEM_GETVAL;
  1600. req.msg.data = (uint8_t *) &getval_req;
  1601. req.msg.data_len = sizeof(sunoem_getval_t);
  1602. rsp = intf->sendrecv(intf, &req);
  1603. if (rsp == NULL) {
  1604. lprintf(LOG_ERR, "Sun OEM getval1 command failed.");
  1605. return (-1);
  1606. }
  1607. if (rsp->ccode != 0) {
  1608. lprintf(LOG_ERR, "Sun OEM getval1 command failed: %d", rsp->ccode);
  1609. return (-1);
  1610. }
  1611. /*
  1612. * Fetch the data value - if it is not ready,
  1613. * retry the request up to GETVAL_MAX_RETRIES
  1614. */
  1615. for (i = 0; i < GETVAL_MAX_RETRIES; i++) {
  1616. memset(&req, 0, sizeof(req));
  1617. req.msg.netfn = IPMI_NETFN_SUNOEM;
  1618. req.msg.cmd = IPMI_SUNOEM_GETVAL;
  1619. getval_req.cmd_code = SUNOEM_GET_VAL;
  1620. req.msg.data = (uint8_t *) &getval_req;
  1621. req.msg.data_len = sizeof(sunoem_getval_t);
  1622. rsp = intf->sendrecv(intf, &req);
  1623. if (rsp == NULL) {
  1624. lprintf(LOG_ERR, "Sun OEM getval2 command failed.");
  1625. return (-1);
  1626. }
  1627. if (rsp->ccode != 0) {
  1628. lprintf(LOG_ERR, "Sun OEM getval2 command failed: %d", rsp->ccode);
  1629. return (-1);
  1630. }
  1631. getval_rsp = (sunoem_getval_t *) rsp->data;
  1632. if (getval_rsp->cmd_code == SUNOEM_DATA_READY) {
  1633. printf("Target Value: %s\n", getval_rsp->luapi_value);
  1634. return (0);
  1635. } else if (getval_rsp->cmd_code == SUNOEM_DATA_NOT_FOUND) {
  1636. lprintf(LOG_ERR, "Target: %s not found", getval_req.luapi_value);
  1637. return (-1);
  1638. }
  1639. sleep(1);
  1640. }
  1641. lprintf(LOG_ERR, "Unable to retrieve target value.");
  1642. return (-1);
  1643. }
  1644. static int
  1645. send_luapi_prop_name(struct ipmi_intf * intf, int len, char *prop_name,
  1646. unsigned char *tid_num)
  1647. {
  1648. int i = 0;
  1649. struct ipmi_rs *rsp;
  1650. struct ipmi_rq req;
  1651. sunoem_setval_t setval_req;
  1652. sunoem_setval_resp_t *setval_rsp;
  1653. *tid_num = 0;
  1654. while (i < len) {
  1655. /*
  1656. * Setup the request,
  1657. * Upon function return, the next cmd (SUNOEM_SET_VAL)
  1658. * can be requested.
  1659. */
  1660. memset(&req, 0, sizeof(req));
  1661. memset(&setval_req, 0, sizeof(sunoem_setval_t));
  1662. req.msg.netfn = IPMI_NETFN_SUNOEM;
  1663. req.msg.cmd = IPMI_SUNOEM_SETVAL;
  1664. setval_req.cmd_code = SUNOEM_SET_VAL;
  1665. setval_req.param_type = SUNOEM_LUAPI_TARGET;
  1666. setval_req.tid = *tid_num;
  1667. setval_req.eof = 0;
  1668. /*
  1669. * If the property name is > payload, only copy
  1670. * the payload size and increment the string offset (i)
  1671. * for the next payload
  1672. */
  1673. if (strlen(&(prop_name[i])) > MAX_SUNOEM_VAL_COMPACT_PAYLOAD) {
  1674. strncpy(setval_req.luapi_data, &(prop_name[i]),
  1675. MAX_SUNOEM_VAL_COMPACT_PAYLOAD);
  1676. } else {
  1677. strncpy(setval_req.luapi_data, &(prop_name[i]),
  1678. strlen(&(prop_name[i])));
  1679. }
  1680. req.msg.data = (uint8_t *) &setval_req;
  1681. req.msg.data_len = sizeof(sunoem_setval_t);
  1682. rsp = intf->sendrecv(intf, &req);
  1683. if (rsp == NULL) {
  1684. lprintf(LOG_ERR, "Sun OEM setval prop name: response is NULL");
  1685. return (-1);
  1686. }
  1687. if (rsp->ccode != 0) {
  1688. lprintf(LOG_ERR, "Sun OEM setval prop name: request failed: %d",
  1689. rsp->ccode);
  1690. return (-1);
  1691. }
  1692. setval_rsp = (sunoem_setval_resp_t *) rsp->data;
  1693. /*
  1694. * If the return code is other than data received, the
  1695. * request failed
  1696. */
  1697. if (setval_rsp->status_code != SUNOEM_REQ_RECV) {
  1698. lprintf(LOG_ERR,
  1699. "Sun OEM setval prop name: invalid status code: %d",
  1700. setval_rsp->status_code);
  1701. return (-1);
  1702. }
  1703. /* Use the tid returned by ILOM */
  1704. *tid_num = setval_rsp->tid;
  1705. /* Increment the string offset */
  1706. i += MAX_SUNOEM_VAL_COMPACT_PAYLOAD;
  1707. }
  1708. return (0);
  1709. }
  1710. static int
  1711. send_luapi_prop_value(struct ipmi_intf * intf, int len, char *prop_value,
  1712. unsigned char tid_num)
  1713. {
  1714. int i = 0;
  1715. struct ipmi_rs *rsp;
  1716. struct ipmi_rq req;
  1717. sunoem_setval_t setval_req;
  1718. sunoem_setval_resp_t *setval_rsp;
  1719. while (i < len) {
  1720. /*
  1721. * Setup the request,
  1722. * Upon function return, the next cmd (SUNOEM_GET_VAL)
  1723. * can be requested.
  1724. */
  1725. memset(&req, 0, sizeof(req));
  1726. memset(&setval_req, 0, sizeof(sunoem_setval_t));
  1727. req.msg.netfn = IPMI_NETFN_SUNOEM;
  1728. req.msg.cmd = IPMI_SUNOEM_SETVAL;
  1729. setval_req.cmd_code = SUNOEM_SET_VAL;
  1730. setval_req.param_type = SUNOEM_LUAPI_VALUE;
  1731. setval_req.tid = tid_num;
  1732. /*
  1733. * If the property name is > payload, only copy the
  1734. * the payload size and increment the string offset
  1735. * for the next payload
  1736. */
  1737. if (strlen(&(prop_value[i])) > MAX_SUNOEM_VAL_COMPACT_PAYLOAD) {
  1738. strncpy(setval_req.luapi_data, &(prop_value[i]),
  1739. MAX_SUNOEM_VAL_COMPACT_PAYLOAD);
  1740. } else {
  1741. /* Captured the entire string, mark this as the last payload */
  1742. strncpy(setval_req.luapi_data, &(prop_value[i]),
  1743. strlen(&(prop_value[i])));
  1744. setval_req.eof = 1;
  1745. }
  1746. req.msg.data = (uint8_t *) &setval_req;
  1747. req.msg.data_len = sizeof(sunoem_setval_t);
  1748. rsp = intf->sendrecv(intf, &req);
  1749. if (rsp == NULL) {
  1750. lprintf(LOG_ERR, "Sun OEM setval prop value: response is NULL");
  1751. return (-1);
  1752. }
  1753. if (rsp->ccode != 0) {
  1754. lprintf(LOG_ERR, "Sun OEM setval prop value: request failed: %d",
  1755. rsp->ccode);
  1756. return (-1);
  1757. }
  1758. setval_rsp = (sunoem_setval_resp_t *) rsp->data;
  1759. /*
  1760. * If the return code is other than data received, the
  1761. * request failed
  1762. */
  1763. if (setval_rsp->status_code != SUNOEM_REQ_RECV) {
  1764. lprintf(LOG_ERR,
  1765. "Sun OEM setval prop value: invalid status code: %d",
  1766. setval_rsp->status_code);
  1767. return (-1);
  1768. }
  1769. /* Increment the string offset */
  1770. i += MAX_SUNOEM_VAL_COMPACT_PAYLOAD;
  1771. }
  1772. return (0);
  1773. }
  1774. static int
  1775. ipmi_sunoem_setval(struct ipmi_intf * intf, int argc, char *argv[])
  1776. {
  1777. struct ipmi_rs *rsp;
  1778. struct ipmi_rq req;
  1779. sunoem_setval_t setval_req;
  1780. sunoem_setval_resp_t *setval_rsp;
  1781. int prop_len;
  1782. int value_len;
  1783. int i;
  1784. unsigned char tid_num;
  1785. int retries;
  1786. prop_len = strlen(argv[0]);
  1787. value_len = strlen(argv[1]);
  1788. if (prop_len > LUAPI_MAX_OBJ_PATH_LEN) {
  1789. lprintf(LOG_ERR,
  1790. "Sun OEM set value command failed: Max size on property name");
  1791. return (-1);
  1792. }
  1793. if (value_len > LUAPI_MAX_OBJ_VAL_LEN) {
  1794. lprintf(LOG_ERR,
  1795. "Sun OEM set value command failed: Max size on property value");
  1796. return (-1);
  1797. }
  1798. /* Test if there is a timeout specified */
  1799. if (argc == 3) {
  1800. if ((str2int(argv[2], &retries) != 0) || retries < 0) {
  1801. lprintf(LOG_ERR,
  1802. "Invalid input given or out of range for time-out parameter.");
  1803. return (-1);
  1804. }
  1805. } else {
  1806. retries = GETVAL_MAX_RETRIES;
  1807. }
  1808. /* Send the property name 1st */
  1809. if (send_luapi_prop_name(intf, prop_len, argv[0], &tid_num) != 0) {
  1810. /* return if there is an error */
  1811. return (-1);
  1812. }
  1813. if (send_luapi_prop_value(intf, value_len, argv[1], tid_num) != 0) {
  1814. /* return if there is an error */
  1815. return (-1);
  1816. }
  1817. /*
  1818. * Get The status of the command.
  1819. * if it is not ready, retry the request up to
  1820. * GETVAL_MAX_RETRIES
  1821. */
  1822. for (i = 0; i < retries; i++) {
  1823. memset(&req, 0, sizeof(req));
  1824. req.msg.netfn = IPMI_NETFN_SUNOEM;
  1825. req.msg.cmd = IPMI_SUNOEM_SETVAL;
  1826. setval_req.cmd_code = SUNOEM_GET_STATUS;
  1827. setval_req.tid = tid_num;
  1828. req.msg.data = (uint8_t *) &setval_req;
  1829. req.msg.data_len = sizeof(sunoem_setval_t);
  1830. rsp = intf->sendrecv(intf, &req);
  1831. if (rsp == NULL) {
  1832. lprintf(LOG_ERR, "Sun OEM setval command failed.");
  1833. return (-1);
  1834. }
  1835. if (rsp->ccode != 0) {
  1836. lprintf(LOG_ERR, "Sun OEM setval command failed: %d", rsp->ccode);
  1837. return (-1);
  1838. }
  1839. setval_rsp = (sunoem_setval_resp_t *) rsp->data;
  1840. if (setval_rsp->status_code == SUNOEM_DATA_READY) {
  1841. printf("Sun OEM setval command successful.\n");
  1842. return (0);
  1843. } else if (setval_rsp->status_code != SUNOEM_DATA_NOT_READY) {
  1844. lprintf(LOG_ERR, "Sun OEM setval command failed.");
  1845. return (-1);
  1846. }
  1847. sleep(1);
  1848. }
  1849. /* If we reached here, retries exceeded */
  1850. lprintf(LOG_ERR, "Sun OEM setval command failed: Command Timed Out");
  1851. return (-1);
  1852. }
  1853. #define MAX_FILE_DATA_SIZE 1024
  1854. #define MAX_FILEID_LEN 16
  1855. #define CORE_TUNNEL_SUBCMD_GET_FILE 11
  1856. #ifdef HAVE_PRAGMA_PACK
  1857. #pragma pack(push, 1)
  1858. #endif
  1859. typedef struct
  1860. {
  1861. unsigned char cmd_code;
  1862. unsigned char file_id[MAX_FILEID_LEN];
  1863. unsigned int block_num;
  1864. }__attribute__((packed)) getfile_req_t;
  1865. typedef struct
  1866. {
  1867. unsigned int block_num;
  1868. unsigned int data_size;
  1869. unsigned char eof;
  1870. unsigned char data[MAX_FILE_DATA_SIZE];
  1871. }__attribute__((packed)) getfile_rsp_t;
  1872. #ifdef HAVE_PRAGMA_PACK
  1873. #pragma pack(pop)
  1874. #endif
  1875. static int
  1876. ipmi_sunoem_getfile(struct ipmi_intf * intf, int argc, char *argv[])
  1877. {
  1878. struct ipmi_rs *rsp;
  1879. struct ipmi_rq req;
  1880. getfile_req_t getfile_req;
  1881. getfile_rsp_t *getfile_rsp;
  1882. int block_num = 0;
  1883. int nbo_blk_num; /* Network Byte Order Block Num */
  1884. FILE *fp;
  1885. unsigned data_size;
  1886. supported_version_t supp_ver = IPMI_SUNOEM_GETFILE_VERSION;
  1887. if (argc < 1) {
  1888. return (-1);
  1889. }
  1890. /*check if command is supported by this version of ilom*/
  1891. if (ipmi_sunoem_checkversion(intf, &supp_ver) < 0) {
  1892. ipmi_sunoem_print_required_version(&supp_ver);
  1893. return (-1);
  1894. }
  1895. /*
  1896. * File ID is < MAX_FILEID_LEN
  1897. * Save 1 byte for null Terminated string
  1898. */
  1899. if (strlen(argv[0]) >= MAX_FILE_DATA_SIZE) {
  1900. lprintf(LOG_ERR, "File ID >= %d characters", MAX_FILEID_LEN);
  1901. return (-1);
  1902. }
  1903. memset(&getfile_req, 0, sizeof(getfile_req));
  1904. strncpy((char*) getfile_req.file_id, argv[0], MAX_FILEID_LEN - 1);
  1905. /* Create the destination file */
  1906. fp = ipmi_open_file_write(argv[1]);
  1907. if (fp == NULL) {
  1908. lprintf(LOG_ERR, "Unable to open file: %s", argv[1]);
  1909. return (-1);
  1910. }
  1911. memset(&req, 0, sizeof(req));
  1912. req.msg.netfn = IPMI_NETFN_SUNOEM;
  1913. req.msg.cmd = IPMI_SUNOEM_CORE_TUNNEL;
  1914. req.msg.data = (uint8_t *) &getfile_req;
  1915. req.msg.data_len = sizeof(getfile_req_t);
  1916. getfile_req.cmd_code = CORE_TUNNEL_SUBCMD_GET_FILE;
  1917. do {
  1918. nbo_blk_num = htonl(block_num);
  1919. /* Block Num must be in network byte order */
  1920. memcpy(&(getfile_req.block_num), &nbo_blk_num,
  1921. sizeof(getfile_req.block_num));
  1922. rsp = intf->sendrecv(intf, &req);
  1923. if (rsp == NULL) {
  1924. lprintf(LOG_ERR, "Sun OEM getfile command failed.");
  1925. fclose(fp);
  1926. return (-1);
  1927. }
  1928. if (rsp->ccode != 0) {
  1929. lprintf(LOG_ERR, "Sun OEM getfile command failed: %d", rsp->ccode);
  1930. fclose(fp);
  1931. return (-1);
  1932. }
  1933. getfile_rsp = (getfile_rsp_t *) rsp->data;
  1934. memcpy(&data_size, &(getfile_rsp->data_size),
  1935. sizeof(getfile_rsp->data_size));
  1936. data_size = ntohl(data_size);
  1937. if (data_size > MAX_FILE_DATA_SIZE) {
  1938. lprintf(LOG_ERR, "Sun OEM getfile invalid data size: %d",
  1939. data_size);
  1940. fclose(fp);
  1941. return (-1);
  1942. }
  1943. /* Check if Block Num matches */
  1944. if (memcmp(&(getfile_req.block_num), &(getfile_rsp->block_num),
  1945. sizeof(getfile_req.block_num)) != 0) {
  1946. lprintf(LOG_ERR, "Sun OEM getfile Incorrect Block Num Returned");
  1947. lprintf(LOG_ERR, "Expecting: %x Received: %x",
  1948. getfile_req.block_num, getfile_rsp->block_num);
  1949. fclose(fp);
  1950. return (-1);
  1951. }
  1952. if (fwrite(getfile_rsp->data, 1, data_size, fp) != data_size) {
  1953. lprintf(LOG_ERR, "Sun OEM getfile write failed: %d", rsp->ccode);
  1954. fclose(fp);
  1955. return (-1);
  1956. }
  1957. block_num++;
  1958. } while (getfile_rsp->eof == 0);
  1959. fclose(fp);
  1960. return (0);
  1961. }
  1962. /*
  1963. * Query BMC for capability/behavior.
  1964. */
  1965. #define CORE_TUNNEL_SUBCMD_GET_BEHAVIOR 15
  1966. #define SUNOEM_BEHAVIORID_SIZE 32
  1967. #ifdef HAVE_PRAGMA_PACK
  1968. #pragma pack(push, 1)
  1969. #endif
  1970. typedef struct
  1971. {
  1972. unsigned char cmd_code;
  1973. unsigned char behavior_id[SUNOEM_BEHAVIORID_SIZE];
  1974. }__attribute__((packed)) getbehavior_req_t;
  1975. typedef struct
  1976. {
  1977. unsigned char enabled;
  1978. }__attribute__((packed)) getbehavior_rsp_t;
  1979. #ifdef HAVE_PRAGMA_PACK
  1980. #pragma pack(pop)
  1981. #endif
  1982. static int
  1983. ipmi_sunoem_getbehavior(struct ipmi_intf * intf, int argc, char *argv[])
  1984. {
  1985. struct ipmi_rq req;
  1986. struct ipmi_rs *rsp;
  1987. getbehavior_req_t getbehavior_req;
  1988. getbehavior_rsp_t *getbehavior_rsp;
  1989. supported_version_t supp_ver = IPMI_SUNOEM_GETBEHAVIOR_VERSION;
  1990. if (argc < 1) {
  1991. return (-1);
  1992. }
  1993. /*check if command is supported by this version of ilom*/
  1994. if (ipmi_sunoem_checkversion(intf, &supp_ver) < 0) {
  1995. ipmi_sunoem_print_required_version(&supp_ver);
  1996. return (-1);
  1997. }
  1998. /*
  1999. * Behavior ID is < SUNOEM_BEHAVIORID_SIZE.
  2000. * Save 1 byte for null terminated string
  2001. */
  2002. if (strlen(argv[0]) >= SUNOEM_BEHAVIORID_SIZE) {
  2003. lprintf(LOG_ERR, "Behavior ID >= %d characters",
  2004. SUNOEM_BEHAVIORID_SIZE);
  2005. return (-1);
  2006. }
  2007. memset(&getbehavior_req, 0, sizeof(getbehavior_req));
  2008. strncpy(getbehavior_req.behavior_id, argv[0], SUNOEM_BEHAVIORID_SIZE - 1);
  2009. memset(&req, 0, sizeof(req));
  2010. req.msg.netfn = IPMI_NETFN_SUNOEM;
  2011. req.msg.cmd = IPMI_SUNOEM_CORE_TUNNEL;
  2012. req.msg.data = (uint8_t *) &getbehavior_req;
  2013. req.msg.data_len = sizeof(getbehavior_req_t);
  2014. getbehavior_req.cmd_code = CORE_TUNNEL_SUBCMD_GET_BEHAVIOR;
  2015. rsp = intf->sendrecv(intf, &req);
  2016. if (rsp == NULL) {
  2017. lprintf(LOG_ERR, "Sun OEM getbehavior command failed.");
  2018. return (-1);
  2019. }
  2020. if (rsp->ccode != 0) {
  2021. lprintf(LOG_ERR, "Sun OEM getbehavior command failed: %d", rsp->ccode);
  2022. return (-1);
  2023. }
  2024. getbehavior_rsp = (getbehavior_rsp_t *) rsp->data;
  2025. printf("ILOM behavior %s %s enabled\n", getbehavior_req.behavior_id,
  2026. getbehavior_rsp->enabled ? "is" : "is not");
  2027. return (0);
  2028. }
  2029. int
  2030. ipmi_sunoem_main(struct ipmi_intf * intf, int argc, char ** argv)
  2031. {
  2032. int rc = 0;
  2033. if (argc == 0 || strcmp(argv[0], "help") == 0) {
  2034. ipmi_sunoem_usage();
  2035. return (0);
  2036. } /* if (argc == 0 || strcmp(argv[0], "help") == 0) */
  2037. if (strcmp(argv[0], "cli") == 0) {
  2038. rc = ipmi_sunoem_cli(intf, argc - 1, &argv[1]);
  2039. } else if ((strcmp(argv[0], "led") == 0) || (strcmp(argv[0], "sbled") == 0)) {
  2040. if (argc < 2) {
  2041. ipmi_sunoem_usage();
  2042. return (-1);
  2043. }
  2044. if (strcmp(argv[1], "get") == 0) {
  2045. if (argc < 3) {
  2046. char * arg[] = { "all" };
  2047. rc = ipmi_sunoem_led_get(intf, 1, arg);
  2048. } else {
  2049. rc = ipmi_sunoem_led_get(intf, argc - 2, &(argv[2]));
  2050. }
  2051. } else if (strcmp(argv[1], "set") == 0) {
  2052. if (argc < 4) {
  2053. ipmi_sunoem_usage();
  2054. return (-1);
  2055. }
  2056. rc = ipmi_sunoem_led_set(intf, argc - 2, &(argv[2]));
  2057. } else {
  2058. ipmi_sunoem_usage();
  2059. return (-1);
  2060. }
  2061. } else if (strcmp(argv[0], "sshkey") == 0) {
  2062. uint8_t uid = 0;
  2063. if (argc < 3) {
  2064. ipmi_sunoem_usage();
  2065. return (-1);
  2066. }
  2067. rc = str2uchar(argv[2], &uid);
  2068. if (rc == 0) {
  2069. /* conversion should be OK. */
  2070. } else if (rc == 2) {
  2071. lprintf(LOG_NOTICE, "Invalid interval given.");
  2072. return (-1);
  2073. } else {
  2074. /* defaults to rc = 3 */
  2075. lprintf(LOG_NOTICE, "Given interval is too big.");
  2076. return (-1);
  2077. }
  2078. if (strcmp(argv[1], "del") == 0) {
  2079. /* number of arguments, three, is already checked at this point */
  2080. rc = ipmi_sunoem_sshkey_del(intf, uid);
  2081. } else if (strcmp(argv[1], "set") == 0) {
  2082. if (argc < 4) {
  2083. ipmi_sunoem_usage();
  2084. return (-1);
  2085. }
  2086. rc = ipmi_sunoem_sshkey_set(intf, uid, argv[3]);
  2087. } else {
  2088. ipmi_sunoem_usage();
  2089. return (-1);
  2090. }
  2091. } else if (strcmp(argv[0], "ping") == 0) {
  2092. if (argc < 2) {
  2093. ipmi_sunoem_usage();
  2094. return (-1);
  2095. }
  2096. rc = ipmi_sunoem_echo(intf, argc - 1, &(argv[1]));
  2097. } else if (strcmp(argv[0], "version") == 0) {
  2098. rc = ipmi_sunoem_version(intf);
  2099. } else if (strcmp(argv[0], "nacname") == 0) {
  2100. if (argc < 2) {
  2101. ipmi_sunoem_usage();
  2102. return (-1);
  2103. }
  2104. rc = ipmi_sunoem_nacname(intf, argc - 1, &(argv[1]));
  2105. } else if (strcmp(argv[0], "getval") == 0) {
  2106. if (argc < 2) {
  2107. ipmi_sunoem_usage();
  2108. return (-1);
  2109. }
  2110. rc = ipmi_sunoem_getval(intf, argc - 1, &(argv[1]));
  2111. } else if (strcmp(argv[0], "setval") == 0) {
  2112. if (argc < 3) {
  2113. ipmi_sunoem_usage();
  2114. return (-1);
  2115. }
  2116. rc = ipmi_sunoem_setval(intf, argc - 1, &(argv[1]));
  2117. } else if (strcmp(argv[0], "getfile") == 0) {
  2118. if (argc < 3) {
  2119. ipmi_sunoem_usage();
  2120. return (-1);
  2121. }
  2122. rc = ipmi_sunoem_getfile(intf, argc - 1, &(argv[1]));
  2123. } else if (strcmp(argv[0], "getbehavior") == 0) {
  2124. if (argc < 2) {
  2125. ipmi_sunoem_usage();
  2126. return (-1);
  2127. }
  2128. rc = ipmi_sunoem_getbehavior(intf, argc - 1, &(argv[1]));
  2129. } else {
  2130. lprintf(LOG_ERR, "Invalid sunoem command: %s", argv[0]);
  2131. return (-1);
  2132. } /* if (strcmp(argv[0], "cli") == 0) */
  2133. return (rc);
  2134. }