ipmi_fru.c 131 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212
  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 <ipmitool/ipmi.h>
  33. #include <ipmitool/log.h>
  34. #include <ipmitool/helper.h>
  35. #include <ipmitool/ipmi_intf.h>
  36. #include <ipmitool/ipmi_fru.h>
  37. #include <ipmitool/ipmi_mc.h>
  38. #include <ipmitool/ipmi_sdr.h>
  39. #include <ipmitool/ipmi_strings.h> /* IANA id strings */
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <time.h>
  43. #include <errno.h>
  44. #if HAVE_CONFIG_H
  45. # include <config.h>
  46. #endif
  47. #define FRU_MULTIREC_CHUNK_SIZE (255 + sizeof(struct fru_multirec_header))
  48. static const char *section_id[4] = {
  49. "Internal Use Section",
  50. "Chassis Section",
  51. "Board Section",
  52. "Product Section"
  53. };
  54. /* From lib/dimm_spd.c: */
  55. int
  56. ipmi_spd_print_fru(struct ipmi_intf * intf, uint8_t id);
  57. /* From src/plugins/ipmi_intf.c: */
  58. void
  59. ipmi_intf_set_max_request_data_size(struct ipmi_intf * intf, uint16_t size);
  60. void
  61. ipmi_intf_set_max_response_data_size(struct ipmi_intf * intf, uint16_t size);
  62. extern int verbose;
  63. static void ipmi_fru_read_to_bin(struct ipmi_intf * intf, char * pFileName, uint8_t fruId);
  64. static void ipmi_fru_write_from_bin(struct ipmi_intf * intf, char * pFileName, uint8_t fruId);
  65. static int ipmi_fru_upg_ekeying(struct ipmi_intf * intf, char * pFileName, uint8_t fruId);
  66. static int ipmi_fru_get_multirec_location_from_fru(struct ipmi_intf * intf, uint8_t fruId,
  67. struct fru_info *pFruInfo, uint32_t * pRetLocation,
  68. uint32_t * pRetSize);
  69. static int ipmi_fru_get_multirec_from_file(char * pFileName, uint8_t * pBufArea,
  70. uint32_t size, uint32_t offset);
  71. static int ipmi_fru_get_multirec_size_from_file(char * pFileName, uint32_t * pSize, uint32_t * pOffset);
  72. int ipmi_fru_get_adjust_size_from_buffer(uint8_t *pBufArea, uint32_t *pSize);
  73. static void ipmi_fru_picmg_ext_print(uint8_t * fru_data, int off, int length);
  74. static int ipmi_fru_set_field_string(struct ipmi_intf * intf, unsigned
  75. char fruId, uint8_t f_type, uint8_t f_index, char *f_string);
  76. static int
  77. ipmi_fru_set_field_string_rebuild(struct ipmi_intf * intf, uint8_t fruId,
  78. struct fru_info fru, struct fru_header header,
  79. uint8_t f_type, uint8_t f_index, char *f_string);
  80. static void
  81. fru_area_print_multirec_bloc(struct ipmi_intf * intf, struct fru_info * fru,
  82. uint8_t id, uint32_t offset);
  83. int
  84. read_fru_area(struct ipmi_intf * intf, struct fru_info *fru, uint8_t id,
  85. uint32_t offset, uint32_t length, uint8_t *frubuf);
  86. void free_fru_bloc(t_ipmi_fru_bloc *bloc);
  87. /* get_fru_area_str - Parse FRU area string from raw data
  88. *
  89. * @data: raw FRU data
  90. * @offset: offset into data for area
  91. *
  92. * returns pointer to FRU area string
  93. */
  94. char * get_fru_area_str(uint8_t * data, uint32_t * offset)
  95. {
  96. static const char bcd_plus[] = "0123456789 -.:,_";
  97. char * str;
  98. int len, off, size, i, j, k, typecode;
  99. union {
  100. uint32_t bits;
  101. char chars[4];
  102. } u;
  103. size = 0;
  104. off = *offset;
  105. /* bits 6:7 contain format */
  106. typecode = ((data[off] & 0xC0) >> 6);
  107. // printf("Typecode:%i\n", typecode);
  108. /* bits 0:5 contain length */
  109. len = data[off++];
  110. len &= 0x3f;
  111. switch (typecode) {
  112. case 0: /* 00b: binary/unspecified */
  113. /* hex dump -> 2x length */
  114. size = (len*2);
  115. break;
  116. case 2: /* 10b: 6-bit ASCII */
  117. /* 4 chars per group of 1-3 bytes */
  118. size = ((((len+2)*4)/3) & ~3);
  119. break;
  120. case 3: /* 11b: 8-bit ASCII */
  121. case 1: /* 01b: BCD plus */
  122. /* no length adjustment */
  123. size = len;
  124. break;
  125. }
  126. if (size < 1) {
  127. *offset = off;
  128. return NULL;
  129. }
  130. str = malloc(size+1);
  131. if (str == NULL)
  132. return NULL;
  133. memset(str, 0, size+1);
  134. if (len == 0) {
  135. str[0] = '\0';
  136. *offset = off;
  137. return str;
  138. }
  139. switch (typecode) {
  140. case 0: /* Binary */
  141. strncpy(str, buf2str(&data[off], len), len*2);
  142. break;
  143. case 1: /* BCD plus */
  144. for (k=0; k<len; k++)
  145. str[k] = bcd_plus[(data[off+k] & 0x0f)];
  146. str[k] = '\0';
  147. break;
  148. case 2: /* 6-bit ASCII */
  149. for (i=j=0; i<len; i+=3) {
  150. u.bits = 0;
  151. k = ((len-i) < 3 ? (len-i) : 3);
  152. #if WORDS_BIGENDIAN
  153. u.chars[3] = data[off+i];
  154. u.chars[2] = (k > 1 ? data[off+i+1] : 0);
  155. u.chars[1] = (k > 2 ? data[off+i+2] : 0);
  156. #define CHAR_IDX 3
  157. #else
  158. memcpy((void *)&u.bits, &data[off+i], k);
  159. #define CHAR_IDX 0
  160. #endif
  161. for (k=0; k<4; k++) {
  162. str[j++] = ((u.chars[CHAR_IDX] & 0x3f) + 0x20);
  163. u.bits >>= 6;
  164. }
  165. }
  166. str[j] = '\0';
  167. break;
  168. case 3:
  169. memcpy(str, &data[off], len);
  170. str[len] = '\0';
  171. break;
  172. }
  173. off += len;
  174. *offset = off;
  175. return str;
  176. }
  177. /* is_valid_filename - checks file/path supplied by user
  178. *
  179. * input_filename - user input string
  180. *
  181. * returns 0 if path is ok
  182. * returns (-1) if path is NULL
  183. * returns (-2) if path is too short
  184. * returns (-3) if path is too long
  185. */
  186. int
  187. is_valid_filename(const char *input_filename)
  188. {
  189. if (input_filename == NULL) {
  190. lprintf(LOG_ERR, "ERROR: NULL pointer passed.");
  191. return (-1);
  192. }
  193. if (strlen(input_filename) < 1) {
  194. lprintf(LOG_ERR, "File/path is invalid.");
  195. return (-2);
  196. }
  197. if (strlen(input_filename) >= 512) {
  198. lprintf(LOG_ERR, "File/path must be shorter than 512 bytes.");
  199. return (-3);
  200. }
  201. return 0;
  202. } /* is_valid_filename() */
  203. /* build_fru_bloc - build fru bloc for write protection
  204. *
  205. * @intf: ipmi interface
  206. * @fru_info: information about FRU device
  207. * @id : Fru id
  208. * @soffset : Source offset (from buffer)
  209. * @doffset : Destination offset (in device)
  210. * @length : Size of data to write (in bytes)
  211. * @pFrubuf : Pointer on data to write
  212. *
  213. * returns 0 on success
  214. * returns -1 on error
  215. */
  216. #define FRU_NUM_BLOC_COMMON_HEADER 6
  217. t_ipmi_fru_bloc *
  218. build_fru_bloc(struct ipmi_intf * intf, struct fru_info *fru, uint8_t id)
  219. {
  220. t_ipmi_fru_bloc * p_first, * p_bloc, * p_new;
  221. struct ipmi_rs * rsp;
  222. struct ipmi_rq req;
  223. struct fru_header header;
  224. struct fru_multirec_header rec_hdr;
  225. uint8_t msg_data[4];
  226. uint32_t off;
  227. uint16_t i;
  228. /*
  229. * get COMMON Header format
  230. */
  231. msg_data[0] = id;
  232. msg_data[1] = 0;
  233. msg_data[2] = 0;
  234. msg_data[3] = 8;
  235. memset(&req, 0, sizeof(req));
  236. req.msg.netfn = IPMI_NETFN_STORAGE;
  237. req.msg.cmd = GET_FRU_DATA;
  238. req.msg.data = msg_data;
  239. req.msg.data_len = 4;
  240. rsp = intf->sendrecv(intf, &req);
  241. if (rsp == NULL) {
  242. lprintf(LOG_ERR, " Device not present (No Response)");
  243. return NULL;
  244. }
  245. if (rsp->ccode > 0) {
  246. lprintf(LOG_ERR," Device not present (%s)",
  247. val2str(rsp->ccode, completion_code_vals));
  248. return NULL;
  249. }
  250. if (verbose > 1) {
  251. printbuf(rsp->data, rsp->data_len, "FRU DATA");
  252. }
  253. memcpy(&header, rsp->data + 1, 8);
  254. /* verify header checksum */
  255. if (ipmi_csum((uint8_t *)&header, 8)) {
  256. lprintf(LOG_ERR, " Bad header checksum");
  257. return NULL;
  258. }
  259. if (header.version != 1) {
  260. lprintf(LOG_ERR, " Unknown FRU header version 0x%02x", header.version);
  261. return NULL;
  262. }
  263. /******************************************
  264. Malloc and fill up the bloc contents
  265. *******************************************/
  266. // Common header
  267. p_first = malloc(sizeof(struct ipmi_fru_bloc));
  268. if (!p_first) {
  269. lprintf(LOG_ERR, "ipmitool: malloc failure");
  270. return NULL;
  271. }
  272. p_bloc = p_first;
  273. p_bloc->next = NULL;
  274. p_bloc->start= 0;
  275. p_bloc->size = fru->size;
  276. strcpy((char *)p_bloc->blocId, "Common Header Section");
  277. for (i = 0; i < 4; i++) {
  278. if (header.offsets[i]) {
  279. p_new = malloc(sizeof(struct ipmi_fru_bloc));
  280. if (!p_new) {
  281. lprintf(LOG_ERR, "ipmitool: malloc failure");
  282. free_fru_bloc(p_first);
  283. return NULL;
  284. }
  285. p_new->next = NULL;
  286. p_new->start = header.offsets[i] * 8;
  287. p_new->size = fru->size - p_new->start;
  288. strncpy((char *)p_new->blocId, section_id[i], sizeof(p_new->blocId));
  289. /* Make sure string is null terminated */
  290. p_new->blocId[sizeof(p_new->blocId)-1] = 0;
  291. p_bloc->next = p_new;
  292. p_bloc->size = p_new->start - p_bloc->start;
  293. p_bloc = p_new;
  294. }
  295. }
  296. // Multi
  297. if (header.offset.multi) {
  298. off = header.offset.multi * 8;
  299. do {
  300. /*
  301. * check for odd offset for the case of fru devices
  302. * accessed by words
  303. */
  304. if (fru->access && (off & 1)) {
  305. lprintf(LOG_ERR, " Unaligned offset for a block: %d", off);
  306. /* increment offset */
  307. off++;
  308. break;
  309. }
  310. if (read_fru_area(intf, fru, id, off, 5,
  311. (uint8_t *) &rec_hdr) < 0) {
  312. break;
  313. }
  314. p_new = malloc(sizeof(struct ipmi_fru_bloc));
  315. if (!p_new) {
  316. lprintf(LOG_ERR, "ipmitool: malloc failure");
  317. free_fru_bloc(p_first);
  318. return NULL;
  319. }
  320. p_new->next = NULL;
  321. p_new->start = off;
  322. p_new->size = fru->size - p_new->start;
  323. sprintf((char *)p_new->blocId, "Multi-Rec Area: Type %i",
  324. rec_hdr.type);
  325. p_bloc->next = p_new;
  326. p_bloc->size = p_new->start - p_bloc->start;
  327. p_bloc = p_new;
  328. off += rec_hdr.len + sizeof(struct fru_multirec_header);
  329. /* verify record header */
  330. if (ipmi_csum((uint8_t *)&rec_hdr,
  331. sizeof(struct fru_multirec_header))) {
  332. /* can't reliably judge for the rest space */
  333. break;
  334. }
  335. } while (!(rec_hdr.format & 0x80) && (off < fru->size));
  336. lprintf(LOG_DEBUG,"Multi-Record area ends at: %i (%xh)", off, off);
  337. if (fru->size > off) {
  338. // Bloc for remaining space
  339. p_new = malloc(sizeof(struct ipmi_fru_bloc));
  340. if (!p_new) {
  341. lprintf(LOG_ERR, "ipmitool: malloc failure");
  342. free_fru_bloc(p_first);
  343. return NULL;
  344. }
  345. p_new->next = NULL;
  346. p_new->start = off;
  347. p_new->size = fru->size - p_new->start;
  348. strcpy((char *)p_new->blocId, "Unused space");
  349. p_bloc->next = p_new;
  350. p_bloc->size = p_new->start - p_bloc->start;
  351. }
  352. }
  353. /* Dump blocs */
  354. for(p_bloc = p_first, i = 0; p_bloc; p_bloc = p_bloc->next) {
  355. lprintf(LOG_DEBUG ,"Bloc Numb : %i", i++);
  356. lprintf(LOG_DEBUG ,"Bloc Id : %s", p_bloc->blocId);
  357. lprintf(LOG_DEBUG ,"Bloc Start: %i", p_bloc->start);
  358. lprintf(LOG_DEBUG ,"Bloc Size : %i", p_bloc->size);
  359. lprintf(LOG_DEBUG ,"");
  360. }
  361. return p_first;
  362. }
  363. void
  364. free_fru_bloc(t_ipmi_fru_bloc *bloc)
  365. {
  366. t_ipmi_fru_bloc * del;
  367. while (bloc) {
  368. del = bloc;
  369. bloc = bloc->next;
  370. free(del);
  371. del = NULL;
  372. }
  373. }
  374. /*
  375. * write FRU[doffset:length] from the pFrubuf[soffset:length]
  376. * rc=1 on success
  377. **/
  378. int
  379. write_fru_area(struct ipmi_intf * intf, struct fru_info *fru, uint8_t id,
  380. uint16_t soffset, uint16_t doffset,
  381. uint16_t length, uint8_t *pFrubuf)
  382. {
  383. uint16_t tmp, finish;
  384. struct ipmi_rs * rsp;
  385. struct ipmi_rq req;
  386. uint8_t msg_data[255+3];
  387. uint16_t writeLength;
  388. uint16_t found_bloc = 0;
  389. finish = doffset + length; /* destination offset */
  390. if (finish > fru->size)
  391. {
  392. lprintf(LOG_ERROR, "Return error");
  393. return -1;
  394. }
  395. if (fru->access && ((doffset & 1) || (length & 1))) {
  396. lprintf(LOG_ERROR, "Odd offset or length specified");
  397. return (-1);
  398. }
  399. t_ipmi_fru_bloc * fru_bloc = build_fru_bloc(intf, fru, id);
  400. t_ipmi_fru_bloc * saved_fru_bloc = fru_bloc;
  401. memset(&req, 0, sizeof(req));
  402. req.msg.netfn = IPMI_NETFN_STORAGE;
  403. req.msg.cmd = SET_FRU_DATA;
  404. req.msg.data = msg_data;
  405. /* initialize request size only once */
  406. if (fru->max_write_size == 0) {
  407. uint16_t max_rq_size = ipmi_intf_get_max_request_data_size(intf);
  408. /* validate lower bound of the maximum request data size */
  409. if (max_rq_size <= 3) {
  410. lprintf(LOG_ERROR, "Maximum request size is too small to send "
  411. "a write request");
  412. return -1;
  413. }
  414. /*
  415. * Write FRU Info command returns the number of written bytes in
  416. * a single byte field.
  417. */
  418. if (max_rq_size - 3 > 255) {
  419. /* Limit the max write size with 255 bytes. */
  420. fru->max_write_size = 255;
  421. } else {
  422. /* subtract 1 byte for FRU ID an 2 bytes for offset */
  423. fru->max_write_size = max_rq_size - 3;
  424. }
  425. /* check word access */
  426. if (fru->access) {
  427. fru->max_write_size &= ~1;
  428. }
  429. }
  430. do {
  431. uint16_t end_bloc;
  432. uint8_t protected_bloc = 0;
  433. /* Write per bloc, try to find the end of a bloc*/
  434. while (fru_bloc && fru_bloc->start + fru_bloc->size <= doffset) {
  435. fru_bloc = fru_bloc->next;
  436. found_bloc++;
  437. }
  438. if (fru_bloc && fru_bloc->start + fru_bloc->size < finish) {
  439. end_bloc = fru_bloc->start + fru_bloc->size;
  440. } else {
  441. end_bloc = finish;
  442. }
  443. /* calculate write length */
  444. tmp = end_bloc - doffset;
  445. /* check that write length is more than maximum request size */
  446. if (tmp > fru->max_write_size) {
  447. writeLength = fru->max_write_size;
  448. } else {
  449. writeLength = tmp;
  450. }
  451. /* copy fru data */
  452. memcpy(&msg_data[3], pFrubuf + soffset, writeLength);
  453. /* check word access */
  454. if (fru->access) {
  455. writeLength &= ~1;
  456. }
  457. tmp = doffset;
  458. if (fru->access) {
  459. tmp >>= 1;
  460. }
  461. msg_data[0] = id;
  462. msg_data[1] = (uint8_t)tmp;
  463. msg_data[2] = (uint8_t)(tmp >> 8);
  464. req.msg.data_len = writeLength + 3;
  465. if(fru_bloc) {
  466. lprintf(LOG_INFO,"Writing %d bytes (Bloc #%i: %s)",
  467. writeLength, found_bloc, fru_bloc->blocId);
  468. } else {
  469. lprintf(LOG_INFO,"Writing %d bytes", writeLength);
  470. }
  471. rsp = intf->sendrecv(intf, &req);
  472. if (!rsp) {
  473. break;
  474. }
  475. if (rsp->ccode == 0xc7 || rsp->ccode == 0xc8 || rsp->ccode == 0xca) {
  476. if (fru->max_write_size > 8) {
  477. fru->max_write_size -= 8;
  478. lprintf(LOG_INFO, "Retrying FRU write with request size %d",
  479. fru->max_write_size);
  480. continue;
  481. }
  482. } else if(rsp->ccode == 0x80) {
  483. rsp->ccode = 0;
  484. // Write protected section
  485. protected_bloc = 1;
  486. }
  487. if (rsp->ccode > 0)
  488. break;
  489. if (protected_bloc == 0) {
  490. // Write OK, bloc not protected, continue
  491. lprintf(LOG_INFO,"Wrote %d bytes", writeLength);
  492. doffset += writeLength;
  493. soffset += writeLength;
  494. } else {
  495. if(fru_bloc) {
  496. // Bloc protected, advise user and jump over protected bloc
  497. lprintf(LOG_INFO,
  498. "Bloc [%s] protected at offset: %i (size %i bytes)",
  499. fru_bloc->blocId, fru_bloc->start, fru_bloc->size);
  500. lprintf(LOG_INFO,"Jumping over this bloc");
  501. } else {
  502. lprintf(LOG_INFO,
  503. "Remaining FRU is protected following offset: %i",
  504. doffset);
  505. }
  506. soffset += end_bloc - doffset;
  507. doffset = end_bloc;
  508. }
  509. } while (doffset < finish);
  510. if (saved_fru_bloc) {
  511. free_fru_bloc(saved_fru_bloc);
  512. }
  513. return doffset >= finish;
  514. }
  515. /* read_fru_area - fill in frubuf[offset:length] from the FRU[offset:length]
  516. *
  517. * @intf: ipmi interface
  518. * @fru: fru info
  519. * @id: fru id
  520. * @offset: offset into buffer
  521. * @length: how much to read
  522. * @frubuf: buffer read into
  523. *
  524. * returns -1 on error
  525. * returns 0 if successful
  526. */
  527. int
  528. read_fru_area(struct ipmi_intf * intf, struct fru_info *fru, uint8_t id,
  529. uint32_t offset, uint32_t length, uint8_t *frubuf)
  530. {
  531. uint32_t off = offset, tmp, finish;
  532. struct ipmi_rs * rsp;
  533. struct ipmi_rq req;
  534. uint8_t msg_data[4];
  535. if (offset > fru->size) {
  536. lprintf(LOG_ERR, "Read FRU Area offset incorrect: %d > %d",
  537. offset, fru->size);
  538. return -1;
  539. }
  540. finish = offset + length;
  541. if (finish > fru->size) {
  542. finish = fru->size;
  543. lprintf(LOG_NOTICE, "Read FRU Area length %d too large, "
  544. "Adjusting to %d",
  545. offset + length, finish - offset);
  546. }
  547. memset(&req, 0, sizeof(req));
  548. req.msg.netfn = IPMI_NETFN_STORAGE;
  549. req.msg.cmd = GET_FRU_DATA;
  550. req.msg.data = msg_data;
  551. req.msg.data_len = 4;
  552. if (fru->max_read_size == 0) {
  553. uint16_t max_rs_size = ipmi_intf_get_max_response_data_size(intf) - 1;
  554. /* validate lower bound of the maximum response data size */
  555. if (max_rs_size <= 1) {
  556. lprintf(LOG_ERROR, "Maximum response size is too small to send "
  557. "a read request");
  558. return -1;
  559. }
  560. /*
  561. * Read FRU Info command may read up to 255 bytes of data.
  562. */
  563. if (max_rs_size - 1 > 255) {
  564. /* Limit the max read size with 255 bytes. */
  565. fru->max_read_size = 255;
  566. } else {
  567. /* subtract 1 byte for bytes count */
  568. fru->max_read_size = max_rs_size - 1;
  569. }
  570. /* check word access */
  571. if (fru->access) {
  572. fru->max_read_size &= ~1;
  573. }
  574. }
  575. do {
  576. tmp = fru->access ? off >> 1 : off;
  577. msg_data[0] = id;
  578. msg_data[1] = (uint8_t)(tmp & 0xff);
  579. msg_data[2] = (uint8_t)(tmp >> 8);
  580. tmp = finish - off;
  581. if (tmp > fru->max_read_size)
  582. msg_data[3] = (uint8_t)fru->max_read_size;
  583. else
  584. msg_data[3] = (uint8_t)tmp;
  585. rsp = intf->sendrecv(intf, &req);
  586. if (rsp == NULL) {
  587. lprintf(LOG_NOTICE, "FRU Read failed");
  588. break;
  589. }
  590. if (rsp->ccode > 0) {
  591. /* if we get C7h or C8h or CAh return code then we requested too
  592. * many bytes at once so try again with smaller size */
  593. if ((rsp->ccode == 0xc7 || rsp->ccode == 0xc8 || rsp->ccode == 0xca)
  594. && fru->max_read_size > 8) {
  595. if (fru->max_read_size > 32) {
  596. /* subtract read length more aggressively */
  597. fru->max_read_size -= 8;
  598. } else {
  599. /* subtract length less aggressively */
  600. fru->max_read_size--;
  601. }
  602. lprintf(LOG_INFO, "Retrying FRU read with request size %d",
  603. fru->max_read_size);
  604. continue;
  605. }
  606. lprintf(LOG_NOTICE, "FRU Read failed: %s",
  607. val2str(rsp->ccode, completion_code_vals));
  608. break;
  609. }
  610. tmp = fru->access ? rsp->data[0] << 1 : rsp->data[0];
  611. memcpy(frubuf, rsp->data + 1, tmp);
  612. off += tmp;
  613. frubuf += tmp;
  614. /* sometimes the size returned in the Info command
  615. * is too large. return 0 so higher level function
  616. * still attempts to parse what was returned */
  617. if (tmp == 0 && off < finish) {
  618. return 0;
  619. }
  620. } while (off < finish);
  621. if (off < finish) {
  622. return -1;
  623. }
  624. return 0;
  625. }
  626. /* read_fru_area - fill in frubuf[offset:length] from the FRU[offset:length]
  627. *
  628. * @intf: ipmi interface
  629. * @fru: fru info
  630. * @id: fru id
  631. * @offset: offset into buffer
  632. * @length: how much to read
  633. * @frubuf: buffer read into
  634. *
  635. * returns -1 on error
  636. * returns 0 if successful
  637. */
  638. int
  639. read_fru_area_section(struct ipmi_intf * intf, struct fru_info *fru, uint8_t id,
  640. uint32_t offset, uint32_t length, uint8_t *frubuf)
  641. {
  642. static uint32_t fru_data_rqst_size = 20;
  643. uint32_t off = offset, tmp, finish;
  644. struct ipmi_rs * rsp;
  645. struct ipmi_rq req;
  646. uint8_t msg_data[4];
  647. if (offset > fru->size) {
  648. lprintf(LOG_ERR, "Read FRU Area offset incorrect: %d > %d",
  649. offset, fru->size);
  650. return -1;
  651. }
  652. finish = offset + length;
  653. if (finish > fru->size) {
  654. finish = fru->size;
  655. lprintf(LOG_NOTICE, "Read FRU Area length %d too large, "
  656. "Adjusting to %d",
  657. offset + length, finish - offset);
  658. }
  659. memset(&req, 0, sizeof(req));
  660. req.msg.netfn = IPMI_NETFN_STORAGE;
  661. req.msg.cmd = GET_FRU_DATA;
  662. req.msg.data = msg_data;
  663. req.msg.data_len = 4;
  664. #ifdef LIMIT_ALL_REQUEST_SIZE
  665. if (fru_data_rqst_size > 16)
  666. #else
  667. if (fru->access && fru_data_rqst_size > 16)
  668. #endif
  669. fru_data_rqst_size = 16;
  670. do {
  671. tmp = fru->access ? off >> 1 : off;
  672. msg_data[0] = id;
  673. msg_data[1] = (uint8_t)(tmp & 0xff);
  674. msg_data[2] = (uint8_t)(tmp >> 8);
  675. tmp = finish - off;
  676. if (tmp > fru_data_rqst_size)
  677. msg_data[3] = (uint8_t)fru_data_rqst_size;
  678. else
  679. msg_data[3] = (uint8_t)tmp;
  680. rsp = intf->sendrecv(intf, &req);
  681. if (rsp == NULL) {
  682. lprintf(LOG_NOTICE, "FRU Read failed");
  683. break;
  684. }
  685. if (rsp->ccode > 0) {
  686. /* if we get C7 or C8 or CA return code then we requested too
  687. * many bytes at once so try again with smaller size */
  688. if ((rsp->ccode == 0xc7 || rsp->ccode == 0xc8 || rsp->ccode == 0xca) &&
  689. (--fru_data_rqst_size > 8)) {
  690. lprintf(LOG_INFO, "Retrying FRU read with request size %d",
  691. fru_data_rqst_size);
  692. continue;
  693. }
  694. lprintf(LOG_NOTICE, "FRU Read failed: %s",
  695. val2str(rsp->ccode, completion_code_vals));
  696. break;
  697. }
  698. tmp = fru->access ? rsp->data[0] << 1 : rsp->data[0];
  699. memcpy((frubuf + off)-offset, rsp->data + 1, tmp);
  700. off += tmp;
  701. /* sometimes the size returned in the Info command
  702. * is too large. return 0 so higher level function
  703. * still attempts to parse what was returned */
  704. if (tmp == 0 && off < finish)
  705. return 0;
  706. } while (off < finish);
  707. if (off < finish)
  708. return -1;
  709. return 0;
  710. }
  711. static void
  712. fru_area_print_multirec_bloc(struct ipmi_intf * intf, struct fru_info * fru,
  713. uint8_t id, uint32_t offset)
  714. {
  715. uint8_t * fru_data = NULL;
  716. uint32_t i;
  717. struct fru_multirec_header * h;
  718. uint32_t last_off, len;
  719. i = last_off = offset;
  720. fru_data = malloc(fru->size + 1);
  721. if (fru_data == NULL) {
  722. lprintf(LOG_ERR, " Out of memory!");
  723. return;
  724. }
  725. memset(fru_data, 0, fru->size + 1);
  726. do {
  727. h = (struct fru_multirec_header *) (fru_data + i);
  728. // read area in (at most) FRU_MULTIREC_CHUNK_SIZE bytes at a time
  729. if ((last_off < (i + sizeof(*h))) || (last_off < (i + h->len)))
  730. {
  731. len = fru->size - last_off;
  732. if (len > FRU_MULTIREC_CHUNK_SIZE)
  733. len = FRU_MULTIREC_CHUNK_SIZE;
  734. if (read_fru_area(intf, fru, id, last_off, len, fru_data) < 0)
  735. break;
  736. last_off += len;
  737. }
  738. //printf("Bloc Numb : %i\n", counter);
  739. printf("Bloc Start: %i\n", i);
  740. printf("Bloc Size : %i\n", h->len);
  741. printf("\n");
  742. i += h->len + sizeof (struct fru_multirec_header);
  743. } while (!(h->format & 0x80));
  744. i = offset;
  745. do {
  746. h = (struct fru_multirec_header *) (fru_data + i);
  747. printf("Bloc Start: %i\n", i);
  748. printf("Bloc Size : %i\n", h->len);
  749. printf("\n");
  750. i += h->len + sizeof (struct fru_multirec_header);
  751. } while (!(h->format & 0x80));
  752. lprintf(LOG_DEBUG ,"Multi-Record area ends at: %i (%xh)",i,i);
  753. free(fru_data);
  754. fru_data = NULL;
  755. }
  756. /* fru_area_print_chassis - Print FRU Chassis Area
  757. *
  758. * @intf: ipmi interface
  759. * @fru: fru info
  760. * @id: fru id
  761. * @offset: offset pointer
  762. */
  763. static void
  764. fru_area_print_chassis(struct ipmi_intf * intf, struct fru_info * fru,
  765. uint8_t id, uint32_t offset)
  766. {
  767. char * fru_area;
  768. uint8_t * fru_data;
  769. uint32_t fru_len, i;
  770. uint8_t tmp[2];
  771. fru_len = 0;
  772. /* read enough to check length field */
  773. if (read_fru_area(intf, fru, id, offset, 2, tmp) == 0) {
  774. fru_len = 8 * tmp[1];
  775. }
  776. if (fru_len == 0) {
  777. return;
  778. }
  779. fru_data = malloc(fru_len);
  780. if (fru_data == NULL) {
  781. lprintf(LOG_ERR, "ipmitool: malloc failure");
  782. return;
  783. }
  784. memset(fru_data, 0, fru_len);
  785. /* read in the full fru */
  786. if (read_fru_area(intf, fru, id, offset, fru_len, fru_data) < 0) {
  787. free(fru_data);
  788. fru_data = NULL;
  789. return;
  790. }
  791. /*
  792. * skip first two bytes which specify
  793. * fru area version and fru area length
  794. */
  795. i = 2;
  796. printf(" Chassis Type : %s\n",
  797. chassis_type_desc[fru_data[i] >
  798. (sizeof(chassis_type_desc)/sizeof(chassis_type_desc[0])) - 1 ?
  799. 2 : fru_data[i]]);
  800. i++;
  801. fru_area = get_fru_area_str(fru_data, &i);
  802. if (fru_area != NULL) {
  803. if (strlen(fru_area) > 0) {
  804. printf(" Chassis Part Number : %s\n", fru_area);
  805. }
  806. free(fru_area);
  807. fru_area = NULL;
  808. }
  809. fru_area = get_fru_area_str(fru_data, &i);
  810. if (fru_area != NULL) {
  811. if (strlen(fru_area) > 0) {
  812. printf(" Chassis Serial : %s\n", fru_area);
  813. }
  814. free(fru_area);
  815. fru_area = NULL;
  816. }
  817. /* read any extra fields */
  818. while ((fru_data[i] != 0xc1) && (i < fru_len))
  819. {
  820. int j = i;
  821. fru_area = get_fru_area_str(fru_data, &i);
  822. if (fru_area != NULL) {
  823. if (strlen(fru_area) > 0) {
  824. printf(" Chassis Extra : %s\n", fru_area);
  825. }
  826. free(fru_area);
  827. fru_area = NULL;
  828. }
  829. if (i == j) {
  830. break;
  831. }
  832. }
  833. if (fru_data != NULL) {
  834. free(fru_data);
  835. fru_data = NULL;
  836. }
  837. }
  838. /* fru_area_print_board - Print FRU Board Area
  839. *
  840. * @intf: ipmi interface
  841. * @fru: fru info
  842. * @id: fru id
  843. * @offset: offset pointer
  844. */
  845. static void
  846. fru_area_print_board(struct ipmi_intf * intf, struct fru_info * fru,
  847. uint8_t id, uint32_t offset)
  848. {
  849. char * fru_area;
  850. uint8_t * fru_data;
  851. uint32_t fru_len;
  852. uint32_t i;
  853. time_t tval;
  854. uint8_t tmp[2];
  855. fru_len = 0;
  856. /* read enough to check length field */
  857. if (read_fru_area(intf, fru, id, offset, 2, tmp) == 0) {
  858. fru_len = 8 * tmp[1];
  859. }
  860. if (fru_len <= 0) {
  861. return;
  862. }
  863. fru_data = malloc(fru_len);
  864. if (fru_data == NULL) {
  865. lprintf(LOG_ERR, "ipmitool: malloc failure");
  866. return;
  867. }
  868. memset(fru_data, 0, fru_len);
  869. /* read in the full fru */
  870. if (read_fru_area(intf, fru, id, offset, fru_len, fru_data) < 0) {
  871. free(fru_data);
  872. fru_data = NULL;
  873. return;
  874. }
  875. /*
  876. * skip first three bytes which specify
  877. * fru area version, fru area length
  878. * and fru board language
  879. */
  880. i = 3;
  881. tval=((fru_data[i+2] << 16) + (fru_data[i+1] << 8) + (fru_data[i]));
  882. tval=tval * 60;
  883. tval=tval + secs_from_1970_1996;
  884. printf(" Board Mfg Date : %s", asctime(localtime(&tval)));
  885. i += 3; /* skip mfg. date time */
  886. fru_area = get_fru_area_str(fru_data, &i);
  887. if (fru_area != NULL) {
  888. if (strlen(fru_area) > 0) {
  889. printf(" Board Mfg : %s\n", fru_area);
  890. }
  891. free(fru_area);
  892. fru_area = NULL;
  893. }
  894. fru_area = get_fru_area_str(fru_data, &i);
  895. if (fru_area != NULL) {
  896. if (strlen(fru_area) > 0) {
  897. printf(" Board Product : %s\n", fru_area);
  898. }
  899. free(fru_area);
  900. fru_area = NULL;
  901. }
  902. fru_area = get_fru_area_str(fru_data, &i);
  903. if (fru_area != NULL) {
  904. if (strlen(fru_area) > 0) {
  905. printf(" Board Serial : %s\n", fru_area);
  906. }
  907. free(fru_area);
  908. fru_area = NULL;
  909. }
  910. fru_area = get_fru_area_str(fru_data, &i);
  911. if (fru_area != NULL) {
  912. if (strlen(fru_area) > 0) {
  913. printf(" Board Part Number : %s\n", fru_area);
  914. }
  915. free(fru_area);
  916. fru_area = NULL;
  917. }
  918. fru_area = get_fru_area_str(fru_data, &i);
  919. if (fru_area != NULL) {
  920. if (strlen(fru_area) > 0 && verbose > 0) {
  921. printf(" Board FRU ID : %s\n", fru_area);
  922. }
  923. free(fru_area);
  924. fru_area = NULL;
  925. }
  926. /* read any extra fields */
  927. while ((fru_data[i] != 0xc1) && (i < fru_len))
  928. {
  929. int j = i;
  930. fru_area = get_fru_area_str(fru_data, &i);
  931. if (fru_area != NULL) {
  932. if (strlen(fru_area) > 0) {
  933. printf(" Board Extra : %s\n", fru_area);
  934. }
  935. free(fru_area);
  936. fru_area = NULL;
  937. }
  938. if (i == j)
  939. break;
  940. }
  941. if (fru_data != NULL) {
  942. free(fru_data);
  943. fru_data = NULL;
  944. }
  945. }
  946. /* fru_area_print_product - Print FRU Product Area
  947. *
  948. * @intf: ipmi interface
  949. * @fru: fru info
  950. * @id: fru id
  951. * @offset: offset pointer
  952. */
  953. static void
  954. fru_area_print_product(struct ipmi_intf * intf, struct fru_info * fru,
  955. uint8_t id, uint32_t offset)
  956. {
  957. char * fru_area;
  958. uint8_t * fru_data;
  959. uint32_t fru_len, i;
  960. uint8_t tmp[2];
  961. fru_len = 0;
  962. /* read enough to check length field */
  963. if (read_fru_area(intf, fru, id, offset, 2, tmp) == 0) {
  964. fru_len = 8 * tmp[1];
  965. }
  966. if (fru_len == 0) {
  967. return;
  968. }
  969. fru_data = malloc(fru_len);
  970. if (fru_data == NULL) {
  971. lprintf(LOG_ERR, "ipmitool: malloc failure");
  972. return;
  973. }
  974. memset(fru_data, 0, fru_len);
  975. /* read in the full fru */
  976. if (read_fru_area(intf, fru, id, offset, fru_len, fru_data) < 0) {
  977. free(fru_data);
  978. fru_data = NULL;
  979. return;
  980. }
  981. /*
  982. * skip first three bytes which specify
  983. * fru area version, fru area length
  984. * and fru board language
  985. */
  986. i = 3;
  987. fru_area = get_fru_area_str(fru_data, &i);
  988. if (fru_area != NULL) {
  989. if (strlen(fru_area) > 0) {
  990. printf(" Product Manufacturer : %s\n", fru_area);
  991. }
  992. free(fru_area);
  993. fru_area = NULL;
  994. }
  995. fru_area = get_fru_area_str(fru_data, &i);
  996. if (fru_area != NULL) {
  997. if (strlen(fru_area) > 0) {
  998. printf(" Product Name : %s\n", fru_area);
  999. }
  1000. free(fru_area);
  1001. fru_area = NULL;
  1002. }
  1003. fru_area = get_fru_area_str(fru_data, &i);
  1004. if (fru_area != NULL) {
  1005. if (strlen(fru_area) > 0) {
  1006. printf(" Product Part Number : %s\n", fru_area);
  1007. }
  1008. free(fru_area);
  1009. fru_area = NULL;
  1010. }
  1011. fru_area = get_fru_area_str(fru_data, &i);
  1012. if (fru_area != NULL) {
  1013. if (strlen(fru_area) > 0) {
  1014. printf(" Product Version : %s\n", fru_area);
  1015. }
  1016. free(fru_area);
  1017. fru_area = NULL;
  1018. }
  1019. fru_area = get_fru_area_str(fru_data, &i);
  1020. if (fru_area != NULL) {
  1021. if (strlen(fru_area) > 0) {
  1022. printf(" Product Serial : %s\n", fru_area);
  1023. }
  1024. free(fru_area);
  1025. fru_area = NULL;
  1026. }
  1027. fru_area = get_fru_area_str(fru_data, &i);
  1028. if (fru_area != NULL) {
  1029. if (strlen(fru_area) > 0) {
  1030. printf(" Product Asset Tag : %s\n", fru_area);
  1031. }
  1032. free(fru_area);
  1033. fru_area = NULL;
  1034. }
  1035. fru_area = get_fru_area_str(fru_data, &i);
  1036. if (fru_area != NULL) {
  1037. if (strlen(fru_area) > 0 && verbose > 0) {
  1038. printf(" Product FRU ID : %s\n", fru_area);
  1039. }
  1040. free(fru_area);
  1041. fru_area = NULL;
  1042. }
  1043. /* read any extra fields */
  1044. while ((fru_data[i] != 0xc1) && (i < fru_len))
  1045. {
  1046. int j = i;
  1047. fru_area = get_fru_area_str(fru_data, &i);
  1048. if (fru_area != NULL) {
  1049. if (strlen(fru_area) > 0) {
  1050. printf(" Product Extra : %s\n", fru_area);
  1051. }
  1052. free(fru_area);
  1053. fru_area = NULL;
  1054. }
  1055. if (i == j)
  1056. break;
  1057. }
  1058. if (fru_data != NULL) {
  1059. free(fru_data);
  1060. fru_data = NULL;
  1061. }
  1062. }
  1063. /* fru_area_print_multirec - Print FRU Multi Record Area
  1064. *
  1065. * @intf: ipmi interface
  1066. * @fru: fru info
  1067. * @id: fru id
  1068. * @offset: offset pointer
  1069. */
  1070. static void
  1071. fru_area_print_multirec(struct ipmi_intf * intf, struct fru_info * fru,
  1072. uint8_t id, uint32_t offset)
  1073. {
  1074. uint8_t * fru_data;
  1075. struct fru_multirec_header * h;
  1076. struct fru_multirec_powersupply * ps;
  1077. struct fru_multirec_dcoutput * dc;
  1078. struct fru_multirec_dcload * dl;
  1079. uint16_t peak_capacity;
  1080. uint8_t peak_hold_up_time;
  1081. uint32_t last_off;
  1082. last_off = offset;
  1083. fru_data = malloc(FRU_MULTIREC_CHUNK_SIZE);
  1084. if (fru_data == NULL) {
  1085. lprintf(LOG_ERR, "ipmitool: malloc failure");
  1086. return;
  1087. }
  1088. memset(fru_data, 0, FRU_MULTIREC_CHUNK_SIZE);
  1089. h = (struct fru_multirec_header *) (fru_data);
  1090. do {
  1091. if (read_fru_area(intf, fru, id, last_off, sizeof(*h), fru_data) < 0) {
  1092. break;
  1093. }
  1094. if (h->len && read_fru_area(intf, fru, id,
  1095. last_off + sizeof(*h), h->len, fru_data + sizeof(*h)) < 0) {
  1096. break;
  1097. }
  1098. last_off += h->len + sizeof(*h);
  1099. switch (h->type) {
  1100. case FRU_RECORD_TYPE_POWER_SUPPLY_INFORMATION:
  1101. ps = (struct fru_multirec_powersupply *)
  1102. (fru_data + sizeof(struct fru_multirec_header));
  1103. #if WORDS_BIGENDIAN
  1104. ps->capacity = BSWAP_16(ps->capacity);
  1105. ps->peak_va = BSWAP_16(ps->peak_va);
  1106. ps->lowend_input1 = BSWAP_16(ps->lowend_input1);
  1107. ps->highend_input1 = BSWAP_16(ps->highend_input1);
  1108. ps->lowend_input2 = BSWAP_16(ps->lowend_input2);
  1109. ps->highend_input2 = BSWAP_16(ps->highend_input2);
  1110. ps->combined_capacity = BSWAP_16(ps->combined_capacity);
  1111. ps->peak_cap_ht = BSWAP_16(ps->peak_cap_ht);
  1112. #endif
  1113. peak_hold_up_time = (ps->peak_cap_ht & 0xf000) >> 12;
  1114. peak_capacity = ps->peak_cap_ht & 0x0fff;
  1115. printf (" Power Supply Record\n");
  1116. printf (" Capacity : %d W\n",
  1117. ps->capacity);
  1118. printf (" Peak VA : %d VA\n",
  1119. ps->peak_va);
  1120. printf (" Inrush Current : %d A\n",
  1121. ps->inrush_current);
  1122. printf (" Inrush Interval : %d ms\n",
  1123. ps->inrush_interval);
  1124. printf (" Input Voltage Range 1 : %d-%d V\n",
  1125. ps->lowend_input1 / 100, ps->highend_input1 / 100);
  1126. printf (" Input Voltage Range 2 : %d-%d V\n",
  1127. ps->lowend_input2 / 100, ps->highend_input2 / 100);
  1128. printf (" Input Frequency Range : %d-%d Hz\n",
  1129. ps->lowend_freq, ps->highend_freq);
  1130. printf (" A/C Dropout Tolerance : %d ms\n",
  1131. ps->dropout_tolerance);
  1132. printf (" Flags : %s%s%s%s%s\n",
  1133. ps->predictive_fail ? "'Predictive fail' " : "",
  1134. ps->pfc ? "'Power factor correction' " : "",
  1135. ps->autoswitch ? "'Autoswitch voltage' " : "",
  1136. ps->hotswap ? "'Hot swap' " : "",
  1137. ps->predictive_fail ? ps->rps_threshold ?
  1138. ps->tach ? "'Two pulses per rotation'" : "'One pulse per rotation'" :
  1139. ps->tach ? "'Failure on pin de-assertion'" : "'Failure on pin assertion'" : "");
  1140. printf (" Peak capacity : %d W\n",
  1141. peak_capacity);
  1142. printf (" Peak capacity holdup : %d s\n",
  1143. peak_hold_up_time);
  1144. if (ps->combined_capacity == 0)
  1145. printf (" Combined capacity : not specified\n");
  1146. else
  1147. printf (" Combined capacity : %d W (%s and %s)\n",
  1148. ps->combined_capacity,
  1149. combined_voltage_desc [ps->combined_voltage1],
  1150. combined_voltage_desc [ps->combined_voltage2]);
  1151. if (ps->predictive_fail)
  1152. printf (" Fan lower threshold : %d RPS\n",
  1153. ps->rps_threshold);
  1154. break;
  1155. case FRU_RECORD_TYPE_DC_OUTPUT:
  1156. dc = (struct fru_multirec_dcoutput *)
  1157. (fru_data + sizeof(struct fru_multirec_header));
  1158. #if WORDS_BIGENDIAN
  1159. dc->nominal_voltage = BSWAP_16(dc->nominal_voltage);
  1160. dc->max_neg_dev = BSWAP_16(dc->max_neg_dev);
  1161. dc->max_pos_dev = BSWAP_16(dc->max_pos_dev);
  1162. dc->ripple_and_noise = BSWAP_16(dc->ripple_and_noise);
  1163. dc->min_current = BSWAP_16(dc->min_current);
  1164. dc->max_current = BSWAP_16(dc->max_current);
  1165. #endif
  1166. printf (" DC Output Record\n");
  1167. printf (" Output Number : %d\n",
  1168. dc->output_number);
  1169. printf (" Standby power : %s\n",
  1170. dc->standby ? "Yes" : "No");
  1171. printf (" Nominal voltage : %.2f V\n",
  1172. (double) dc->nominal_voltage / 100);
  1173. printf (" Max negative deviation : %.2f V\n",
  1174. (double) dc->max_neg_dev / 100);
  1175. printf (" Max positive deviation : %.2f V\n",
  1176. (double) dc->max_pos_dev / 100);
  1177. printf (" Ripple and noise pk-pk : %d mV\n",
  1178. dc->ripple_and_noise);
  1179. printf (" Minimum current draw : %.3f A\n",
  1180. (double) dc->min_current / 1000);
  1181. printf (" Maximum current draw : %.3f A\n",
  1182. (double) dc->max_current / 1000);
  1183. break;
  1184. case FRU_RECORD_TYPE_DC_LOAD:
  1185. dl = (struct fru_multirec_dcload *)
  1186. (fru_data + sizeof(struct fru_multirec_header));
  1187. #if WORDS_BIGENDIAN
  1188. dl->nominal_voltage = BSWAP_16(dl->nominal_voltage);
  1189. dl->min_voltage = BSWAP_16(dl->min_voltage);
  1190. dl->max_voltage = BSWAP_16(dl->max_voltage);
  1191. dl->ripple_and_noise = BSWAP_16(dl->ripple_and_noise);
  1192. dl->min_current = BSWAP_16(dl->min_current);
  1193. dl->max_current = BSWAP_16(dl->max_current);
  1194. #endif
  1195. printf (" DC Load Record\n");
  1196. printf (" Output Number : %d\n",
  1197. dl->output_number);
  1198. printf (" Nominal voltage : %.2f V\n",
  1199. (double) dl->nominal_voltage / 100);
  1200. printf (" Min voltage allowed : %.2f V\n",
  1201. (double) dl->min_voltage / 100);
  1202. printf (" Max voltage allowed : %.2f V\n",
  1203. (double) dl->max_voltage / 100);
  1204. printf (" Ripple and noise pk-pk : %d mV\n",
  1205. dl->ripple_and_noise);
  1206. printf (" Minimum current load : %.3f A\n",
  1207. (double) dl->min_current / 1000);
  1208. printf (" Maximum current load : %.3f A\n",
  1209. (double) dl->max_current / 1000);
  1210. break;
  1211. case FRU_RECORD_TYPE_OEM_EXTENSION:
  1212. {
  1213. struct fru_multirec_oem_header *oh=(struct fru_multirec_oem_header *)
  1214. &fru_data[sizeof(struct fru_multirec_header)];
  1215. uint32_t iana = oh->mfg_id[0] | oh->mfg_id[1]<<8 | oh->mfg_id[2]<<16;
  1216. /* Now makes sure this is really PICMG record */
  1217. if( iana == IPMI_OEM_PICMG ){
  1218. printf(" PICMG Extension Record\n");
  1219. ipmi_fru_picmg_ext_print(fru_data,
  1220. sizeof(struct fru_multirec_header),
  1221. h->len);
  1222. }
  1223. /* FIXME: Add OEM record support here */
  1224. else{
  1225. printf(" OEM (%s) Record\n", val2str( iana, ipmi_oem_info));
  1226. }
  1227. }
  1228. break;
  1229. }
  1230. } while (!(h->format & 0x80));
  1231. lprintf(LOG_DEBUG ,"Multi-Record area ends at: %i (%xh)", last_off, last_off);
  1232. free(fru_data);
  1233. }
  1234. /* ipmi_fru_query_new_value - Query new values to replace original FRU content
  1235. *
  1236. * @data: FRU data
  1237. * @offset: offset of the bytes to be modified in data
  1238. * @len: size of the modified data
  1239. *
  1240. * returns : TRUE if data changed
  1241. * returns : FALSE if data not changed
  1242. */
  1243. int ipmi_fru_query_new_value(uint8_t *data,int offset, size_t len)
  1244. {
  1245. int status=FALSE;
  1246. int ret;
  1247. char answer;
  1248. printf("Would you like to change this value <y/n> ? ");
  1249. ret = scanf("%c", &answer);
  1250. if (ret != 1) {
  1251. return FALSE;
  1252. }
  1253. if( answer == 'y' || answer == 'Y' ){
  1254. int i;
  1255. unsigned int *holder;
  1256. holder = malloc(len);
  1257. printf(
  1258. "Enter hex values for each of the %d entries (lsb first), "
  1259. "hit <enter> between entries\n", (int)len);
  1260. /* I can't assign scanf' %x into a single char */
  1261. for( i=0;i<len;i++ ){
  1262. ret = scanf("%x", holder+i);
  1263. if (ret != 1) {
  1264. free(holder);
  1265. return FALSE;
  1266. }
  1267. }
  1268. for( i=0;i<len;i++ ){
  1269. data[offset++] = (unsigned char) *(holder+i);
  1270. }
  1271. /* &data[offset++] */
  1272. free(holder);
  1273. holder = NULL;
  1274. status = TRUE;
  1275. }
  1276. else{
  1277. printf("Entered %c\n",answer);
  1278. }
  1279. return status;
  1280. }
  1281. /* ipmi_fru_oemkontron_edit -
  1282. * Query new values to replace original FRU content
  1283. * This is a generic enough to support any type of 'OEM' record
  1284. * because the user supplies 'IANA number' , 'record Id' and 'record' version'
  1285. *
  1286. * However, the parser must have 'apriori' knowledge of the record format
  1287. * The currently supported record is :
  1288. *
  1289. * IANA : 15000 (Kontron)
  1290. * RECORD ID : 3
  1291. * RECORD VERSION: 0 (or 1)
  1292. *
  1293. * I would have like to put that stuff in an OEM specific file, but apart for
  1294. * the record format information, all commands are really standard 'FRU' command
  1295. *
  1296. *
  1297. * @data: FRU data
  1298. * @offset: start of the current multi record (start of header)
  1299. * @len: len of the current record (excluding header)
  1300. * @h: pointer to record header
  1301. * @oh: pointer to OEM /PICMG header
  1302. *
  1303. * returns: TRUE if data changed
  1304. * returns: FALSE if data not changed
  1305. */
  1306. #define OEM_KONTRON_INFORMATION_RECORD 3
  1307. #define EDIT_OEM_KONTRON_COMPLETE_ARG_COUNT 12
  1308. #define GET_OEM_KONTRON_COMPLETE_ARG_COUNT 5
  1309. /*
  1310. ./src/ipmitool fru edit 0
  1311. oem 15000 3 0 name instance FIELD1 FIELD2 FIELD3 crc32
  1312. */
  1313. #define OEM_KONTRON_SUBCOMMAND_ARG_POS 2
  1314. #define OEM_KONTRON_IANA_ARG_POS 3
  1315. #define OEM_KONTRON_RECORDID_ARG_POS 4
  1316. #define OEM_KONTRON_FORMAT_ARG_POS 5
  1317. #define OEM_KONTRON_NAME_ARG_POS 6
  1318. #define OEM_KONTRON_INSTANCE_ARG_POS 7
  1319. #define OEM_KONTRON_VERSION_ARG_POS 8
  1320. #define OEM_KONTRON_BUILDDATE_ARG_POS 9
  1321. #define OEM_KONTRON_UPDATEDATE_ARG_POS 10
  1322. #define OEM_KONTRON_CRC32_ARG_POS 11
  1323. #define OEM_KONTRON_FIELD_SIZE 8
  1324. #define OEM_KONTRON_VERSION_FIELD_SIZE 10
  1325. #ifdef HAVE_PRAGMA_PACK
  1326. #pragma pack(1)
  1327. #endif
  1328. typedef struct OemKontronInformationRecordV0{
  1329. uint8_t field1TypeLength;
  1330. uint8_t field1[OEM_KONTRON_FIELD_SIZE];
  1331. uint8_t field2TypeLength;
  1332. uint8_t field2[OEM_KONTRON_FIELD_SIZE];
  1333. uint8_t field3TypeLength;
  1334. uint8_t field3[OEM_KONTRON_FIELD_SIZE];
  1335. uint8_t crcTypeLength;
  1336. uint8_t crc32[OEM_KONTRON_FIELD_SIZE];
  1337. }tOemKontronInformationRecordV0;
  1338. #ifdef HAVE_PRAGMA_PACK
  1339. #pragma pack(0)
  1340. #endif
  1341. #ifdef HAVE_PRAGMA_PACK
  1342. #pragma pack(1)
  1343. #endif
  1344. typedef struct OemKontronInformationRecordV1{
  1345. uint8_t field1TypeLength;
  1346. uint8_t field1[OEM_KONTRON_VERSION_FIELD_SIZE];
  1347. uint8_t field2TypeLength;
  1348. uint8_t field2[OEM_KONTRON_FIELD_SIZE];
  1349. uint8_t field3TypeLength;
  1350. uint8_t field3[OEM_KONTRON_FIELD_SIZE];
  1351. uint8_t crcTypeLength;
  1352. uint8_t crc32[OEM_KONTRON_FIELD_SIZE];
  1353. }tOemKontronInformationRecordV1;
  1354. #ifdef HAVE_PRAGMA_PACK
  1355. #pragma pack(0)
  1356. #endif
  1357. /*
  1358. ./src/ipmitool fru get 0 oem iana 3
  1359. */
  1360. static void ipmi_fru_oemkontron_get( int argc, char ** argv,uint8_t * fru_data,
  1361. int off,int len,
  1362. struct fru_multirec_header *h,
  1363. struct fru_multirec_oem_header *oh)
  1364. {
  1365. static int badParams=FALSE;
  1366. int start = off;
  1367. int offset = start;
  1368. offset += sizeof(struct fru_multirec_oem_header);
  1369. if(!badParams){
  1370. /* the 'OEM' field is already checked in caller */
  1371. if( argc > OEM_KONTRON_SUBCOMMAND_ARG_POS ){
  1372. if(strncmp("oem", argv[OEM_KONTRON_SUBCOMMAND_ARG_POS],3)){
  1373. printf("usage: fru get <id> <oem>\n");
  1374. badParams = TRUE;
  1375. return;
  1376. }
  1377. }
  1378. if( argc<GET_OEM_KONTRON_COMPLETE_ARG_COUNT ){
  1379. printf("usage: oem <iana> <recordid>\n");
  1380. printf("usage: oem 15000 3\n");
  1381. badParams = TRUE;
  1382. return;
  1383. }
  1384. }
  1385. if(!badParams){
  1386. if(oh->record_id == OEM_KONTRON_INFORMATION_RECORD ) {
  1387. uint8_t version;
  1388. printf("Kontron OEM Information Record\n");
  1389. version = oh->record_version;
  1390. uint8_t blockCount;
  1391. uint8_t blockIndex=0;
  1392. unsigned int matchInstance = 0;
  1393. uint8_t instance = 0;
  1394. if (str2uchar(argv[OEM_KONTRON_INSTANCE_ARG_POS], &instance) != 0) {
  1395. lprintf(LOG_ERR,
  1396. "Instance argument '%s' is either invalid or out of range.",
  1397. argv[OEM_KONTRON_INSTANCE_ARG_POS]);
  1398. badParams = TRUE;
  1399. return;
  1400. }
  1401. blockCount = fru_data[offset++];
  1402. for(blockIndex=0;blockIndex<blockCount;blockIndex++){
  1403. void * pRecordData;
  1404. uint8_t nameLen;
  1405. nameLen = ( fru_data[offset++] &= 0x3F );
  1406. printf(" Name: %*.*s\n",nameLen, nameLen, (const char *)(fru_data+offset));
  1407. offset+=nameLen;
  1408. pRecordData = &fru_data[offset];
  1409. printf(" Record Version: %d\n", version);
  1410. if( version == 0 )
  1411. {
  1412. printf(" Version: %*.*s\n",
  1413. OEM_KONTRON_FIELD_SIZE,
  1414. OEM_KONTRON_FIELD_SIZE,
  1415. ((tOemKontronInformationRecordV0 *) pRecordData)->field1);
  1416. printf(" Build Date: %*.*s\n",
  1417. OEM_KONTRON_FIELD_SIZE,
  1418. OEM_KONTRON_FIELD_SIZE,
  1419. ((tOemKontronInformationRecordV0 *) pRecordData)->field2);
  1420. printf(" Update Date: %*.*s\n",
  1421. OEM_KONTRON_FIELD_SIZE,
  1422. OEM_KONTRON_FIELD_SIZE,
  1423. ((tOemKontronInformationRecordV0 *) pRecordData)->field3);
  1424. printf(" Checksum: %*.*s\n\n",
  1425. OEM_KONTRON_FIELD_SIZE,
  1426. OEM_KONTRON_FIELD_SIZE,
  1427. ((tOemKontronInformationRecordV0 *) pRecordData)->crc32);
  1428. matchInstance++;
  1429. offset+= sizeof(tOemKontronInformationRecordV0);
  1430. offset++;
  1431. }
  1432. else if ( version == 1 )
  1433. {
  1434. printf(" Version: %*.*s\n",
  1435. OEM_KONTRON_VERSION_FIELD_SIZE,
  1436. OEM_KONTRON_VERSION_FIELD_SIZE,
  1437. ((tOemKontronInformationRecordV1 *) pRecordData)->field1);
  1438. printf(" Build Date: %*.*s\n",
  1439. OEM_KONTRON_FIELD_SIZE,
  1440. OEM_KONTRON_FIELD_SIZE,
  1441. ((tOemKontronInformationRecordV1 *) pRecordData)->field2);
  1442. printf(" Update Date: %*.*s\n",
  1443. OEM_KONTRON_FIELD_SIZE,
  1444. OEM_KONTRON_FIELD_SIZE,
  1445. ((tOemKontronInformationRecordV1 *) pRecordData)->field3);
  1446. printf(" Checksum: %*.*s\n\n",
  1447. OEM_KONTRON_FIELD_SIZE,
  1448. OEM_KONTRON_FIELD_SIZE,
  1449. ((tOemKontronInformationRecordV1 *) pRecordData)->crc32);
  1450. matchInstance++;
  1451. offset+= sizeof(tOemKontronInformationRecordV1);
  1452. offset++;
  1453. }
  1454. else
  1455. {
  1456. printf (" Unsupported version %d\n",version);
  1457. }
  1458. }
  1459. }
  1460. }
  1461. }
  1462. static int ipmi_fru_oemkontron_edit( int argc, char ** argv,uint8_t * fru_data,
  1463. int off,int len,
  1464. struct fru_multirec_header *h,
  1465. struct fru_multirec_oem_header *oh)
  1466. {
  1467. static int badParams=FALSE;
  1468. int hasChanged = FALSE;
  1469. int start = off;
  1470. int offset = start;
  1471. int length = len;
  1472. int i;
  1473. uint8_t record_id = 0;
  1474. offset += sizeof(struct fru_multirec_oem_header);
  1475. if(!badParams){
  1476. /* the 'OEM' field is already checked in caller */
  1477. if( argc > OEM_KONTRON_SUBCOMMAND_ARG_POS ){
  1478. if(strncmp("oem", argv[OEM_KONTRON_SUBCOMMAND_ARG_POS],3)){
  1479. printf("usage: fru edit <id> <oem> <args...>\n");
  1480. badParams = TRUE;
  1481. return hasChanged;
  1482. }
  1483. }
  1484. if( argc<EDIT_OEM_KONTRON_COMPLETE_ARG_COUNT ){
  1485. printf("usage: oem <iana> <recordid> <format> <args...>\n");
  1486. printf("usage: oem 15000 3 0 <name> <instance> <field1>"\
  1487. " <field2> <field3> <crc32>\n");
  1488. badParams = TRUE;
  1489. return hasChanged;
  1490. }
  1491. if (str2uchar(argv[OEM_KONTRON_RECORDID_ARG_POS], &record_id) != 0) {
  1492. lprintf(LOG_ERR,
  1493. "Record ID argument '%s' is either invalid or out of range.",
  1494. argv[OEM_KONTRON_RECORDID_ARG_POS]);
  1495. badParams = TRUE;
  1496. return hasChanged;
  1497. }
  1498. if (record_id == OEM_KONTRON_INFORMATION_RECORD) {
  1499. for(i=OEM_KONTRON_VERSION_ARG_POS;i<=OEM_KONTRON_CRC32_ARG_POS;i++){
  1500. if( (strlen(argv[i]) != OEM_KONTRON_FIELD_SIZE) &&
  1501. (strlen(argv[i]) != OEM_KONTRON_VERSION_FIELD_SIZE)) {
  1502. printf("error: version fields must have %d characters\n",
  1503. OEM_KONTRON_FIELD_SIZE);
  1504. badParams = TRUE;
  1505. return hasChanged;
  1506. }
  1507. }
  1508. }
  1509. }
  1510. if(!badParams){
  1511. if(oh->record_id == OEM_KONTRON_INFORMATION_RECORD ) {
  1512. uint8_t formatVersion = 0;
  1513. uint8_t version;
  1514. if (str2uchar(argv[OEM_KONTRON_FORMAT_ARG_POS], &formatVersion) != 0) {
  1515. lprintf(LOG_ERR,
  1516. "Format argument '%s' is either invalid or out of range.",
  1517. argv[OEM_KONTRON_FORMAT_ARG_POS]);
  1518. badParams = TRUE;
  1519. return hasChanged;
  1520. }
  1521. printf(" Kontron OEM Information Record\n");
  1522. version = oh->record_version;
  1523. if( version == formatVersion ){
  1524. uint8_t blockCount;
  1525. uint8_t blockIndex=0;
  1526. uint8_t matchInstance = 0;
  1527. uint8_t instance = 0;
  1528. if (str2uchar(argv[OEM_KONTRON_INSTANCE_ARG_POS], &instance) != 0) {
  1529. lprintf(LOG_ERR,
  1530. "Instance argument '%s' is either invalid or out of range.",
  1531. argv[OEM_KONTRON_INSTANCE_ARG_POS]);
  1532. badParams = TRUE;
  1533. return hasChanged;
  1534. }
  1535. blockCount = fru_data[offset++];
  1536. printf(" blockCount: %d\n",blockCount);
  1537. for(blockIndex=0;blockIndex<blockCount;blockIndex++){
  1538. void * pRecordData;
  1539. uint8_t nameLen;
  1540. nameLen = ( fru_data[offset++] & 0x3F );
  1541. if( version == 0 || version == 1 )
  1542. {
  1543. if(!strncmp((char *)argv[OEM_KONTRON_NAME_ARG_POS],
  1544. (const char *)(fru_data+offset),nameLen)&& (matchInstance == instance)){
  1545. printf ("Found : %s\n",argv[OEM_KONTRON_NAME_ARG_POS]);
  1546. offset+=nameLen;
  1547. pRecordData = &fru_data[offset];
  1548. if( version == 0 )
  1549. {
  1550. memcpy( ((tOemKontronInformationRecordV0 *)
  1551. pRecordData)->field1 ,
  1552. argv[OEM_KONTRON_VERSION_ARG_POS] ,
  1553. OEM_KONTRON_FIELD_SIZE);
  1554. memcpy( ((tOemKontronInformationRecordV0 *)
  1555. pRecordData)->field2 ,
  1556. argv[OEM_KONTRON_BUILDDATE_ARG_POS],
  1557. OEM_KONTRON_FIELD_SIZE);
  1558. memcpy( ((tOemKontronInformationRecordV0 *)
  1559. pRecordData)->field3 ,
  1560. argv[OEM_KONTRON_UPDATEDATE_ARG_POS],
  1561. OEM_KONTRON_FIELD_SIZE);
  1562. memcpy( ((tOemKontronInformationRecordV0 *)
  1563. pRecordData)->crc32 ,
  1564. argv[OEM_KONTRON_CRC32_ARG_POS] ,
  1565. OEM_KONTRON_FIELD_SIZE);
  1566. }
  1567. else
  1568. {
  1569. memcpy( ((tOemKontronInformationRecordV1 *)
  1570. pRecordData)->field1 ,
  1571. argv[OEM_KONTRON_VERSION_ARG_POS] ,
  1572. OEM_KONTRON_VERSION_FIELD_SIZE);
  1573. memcpy( ((tOemKontronInformationRecordV1 *)
  1574. pRecordData)->field2 ,
  1575. argv[OEM_KONTRON_BUILDDATE_ARG_POS],
  1576. OEM_KONTRON_FIELD_SIZE);
  1577. memcpy( ((tOemKontronInformationRecordV1 *)
  1578. pRecordData)->field3 ,
  1579. argv[OEM_KONTRON_UPDATEDATE_ARG_POS],
  1580. OEM_KONTRON_FIELD_SIZE);
  1581. memcpy( ((tOemKontronInformationRecordV1 *)
  1582. pRecordData)->crc32 ,
  1583. argv[OEM_KONTRON_CRC32_ARG_POS] ,
  1584. OEM_KONTRON_FIELD_SIZE);
  1585. }
  1586. matchInstance++;
  1587. hasChanged = TRUE;
  1588. }
  1589. else if(!strncmp((char *)argv[OEM_KONTRON_NAME_ARG_POS],
  1590. (const char *)(fru_data+offset), nameLen)){
  1591. printf ("Skipped : %s [instance %d]\n",argv[OEM_KONTRON_NAME_ARG_POS],
  1592. (unsigned int)matchInstance);
  1593. matchInstance++;
  1594. offset+=nameLen;
  1595. }
  1596. else {
  1597. offset+=nameLen;
  1598. }
  1599. if( version == 0 )
  1600. {
  1601. offset+= sizeof(tOemKontronInformationRecordV0);
  1602. }
  1603. else
  1604. {
  1605. offset+= sizeof(tOemKontronInformationRecordV1);
  1606. }
  1607. offset++;
  1608. }
  1609. else
  1610. {
  1611. printf (" Unsupported version %d\n",version);
  1612. }
  1613. }
  1614. }
  1615. else{
  1616. printf(" Version: %d\n",version);
  1617. }
  1618. }
  1619. if( hasChanged ){
  1620. uint8_t record_checksum =0;
  1621. uint8_t header_checksum =0;
  1622. int index;
  1623. lprintf(LOG_DEBUG,"Initial record checksum : %x",h->record_checksum);
  1624. lprintf(LOG_DEBUG,"Initial header checksum : %x",h->header_checksum);
  1625. for(index=0;index<length;index++){
  1626. record_checksum+= fru_data[start+index];
  1627. }
  1628. /* Update Record checksum */
  1629. h->record_checksum = ~record_checksum + 1;
  1630. for(index=0;index<(sizeof(struct fru_multirec_header) -1);index++){
  1631. uint8_t data= *( (uint8_t *)h+ index);
  1632. header_checksum+=data;
  1633. }
  1634. /* Update header checksum */
  1635. h->header_checksum = ~header_checksum + 1;
  1636. lprintf(LOG_DEBUG,"Final record checksum : %x",h->record_checksum);
  1637. lprintf(LOG_DEBUG,"Final header checksum : %x",h->header_checksum);
  1638. /* write back data */
  1639. }
  1640. }
  1641. return hasChanged;
  1642. }
  1643. /* ipmi_fru_picmg_ext_edit - Query new values to replace original FRU content
  1644. *
  1645. * @data: FRU data
  1646. * @offset: start of the current multi record (start of header)
  1647. * @len: len of the current record (excluding header)
  1648. * @h: pointer to record header
  1649. * @oh: pointer to OEM /PICMG header
  1650. *
  1651. * returns: TRUE if data changed
  1652. * returns: FALSE if data not changed
  1653. */
  1654. static int ipmi_fru_picmg_ext_edit(uint8_t * fru_data,
  1655. int off,int len,
  1656. struct fru_multirec_header *h,
  1657. struct fru_multirec_oem_header *oh)
  1658. {
  1659. int hasChanged = FALSE;
  1660. int start = off;
  1661. int offset = start;
  1662. int length = len;
  1663. offset += sizeof(struct fru_multirec_oem_header);
  1664. switch (oh->record_id)
  1665. {
  1666. case FRU_AMC_ACTIVATION:
  1667. printf(" FRU_AMC_ACTIVATION\n");
  1668. {
  1669. int index=offset;
  1670. uint16_t max_current;
  1671. max_current = fru_data[offset];
  1672. max_current |= fru_data[++offset]<<8;
  1673. printf(" Maximum Internal Current(@12V): %.2f A (0x%02x)\n",
  1674. (float)max_current / 10.0f, max_current);
  1675. if( ipmi_fru_query_new_value(fru_data,index,2) ){
  1676. max_current = fru_data[index];
  1677. max_current |= fru_data[++index]<<8;
  1678. printf(" New Maximum Internal Current(@12V): %.2f A (0x%02x)\n",
  1679. (float)max_current / 10.0f, max_current);
  1680. hasChanged = TRUE;
  1681. }
  1682. printf(" Module Activation Readiness: %i sec.\n", fru_data[++offset]);
  1683. printf(" Descriptor Count: %i\n", fru_data[++offset]);
  1684. printf("\n");
  1685. for (++offset;
  1686. offset < (off + length);
  1687. offset += sizeof(struct fru_picmgext_activation_record)) {
  1688. struct fru_picmgext_activation_record * a =
  1689. (struct fru_picmgext_activation_record *) &fru_data[offset];
  1690. printf(" IPMB-Address: 0x%x\n", a->ibmb_addr);
  1691. printf(" Max. Module Current: %.2f A\n", (float)a->max_module_curr / 10.0f);
  1692. printf("\n");
  1693. }
  1694. }
  1695. break;
  1696. case FRU_AMC_CURRENT:
  1697. printf(" FRU_AMC_CURRENT\n");
  1698. {
  1699. int index=offset;
  1700. unsigned char current;
  1701. current = fru_data[index];
  1702. printf(" Current draw(@12V): %.2f A (0x%02x)\n",
  1703. (float)current / 10.0f, current);
  1704. if( ipmi_fru_query_new_value(fru_data, index, 1) ){
  1705. current = fru_data[index];
  1706. printf(" New Current draw(@12V): %.2f A (0x%02x)\n",
  1707. (float)current / 10.0f, current);
  1708. hasChanged = TRUE;
  1709. }
  1710. }
  1711. break;
  1712. }
  1713. if( hasChanged ){
  1714. uint8_t record_checksum =0;
  1715. uint8_t header_checksum =0;
  1716. int index;
  1717. lprintf(LOG_DEBUG,"Initial record checksum : %x",h->record_checksum);
  1718. lprintf(LOG_DEBUG,"Initial header checksum : %x",h->header_checksum);
  1719. for(index=0;index<length;index++){
  1720. record_checksum+= fru_data[start+index];
  1721. }
  1722. /* Update Record checksum */
  1723. h->record_checksum = ~record_checksum + 1;
  1724. for(index=0;index<(sizeof(struct fru_multirec_header) -1);index++){
  1725. uint8_t data= *( (uint8_t *)h+ index);
  1726. header_checksum+=data;
  1727. }
  1728. /* Update header checksum */
  1729. h->header_checksum = ~header_checksum + 1;
  1730. lprintf(LOG_DEBUG,"Final record checksum : %x",h->record_checksum);
  1731. lprintf(LOG_DEBUG,"Final header checksum : %x",h->header_checksum);
  1732. /* write back data */
  1733. }
  1734. return hasChanged;
  1735. }
  1736. /* ipmi_fru_picmg_ext_print - prints OEM fru record (PICMG)
  1737. *
  1738. * @fru_data: FRU data
  1739. * @offset: offset of the bytes to be modified in data
  1740. * @length: size of the record
  1741. *
  1742. * returns : n/a
  1743. */
  1744. static void ipmi_fru_picmg_ext_print(uint8_t * fru_data, int off, int length)
  1745. {
  1746. struct fru_multirec_oem_header *h;
  1747. int guid_count;
  1748. int offset = off;
  1749. int start_offset = off;
  1750. int i;
  1751. h = (struct fru_multirec_oem_header *) &fru_data[offset];
  1752. offset += sizeof(struct fru_multirec_oem_header);
  1753. switch (h->record_id)
  1754. {
  1755. case FRU_PICMG_BACKPLANE_P2P:
  1756. {
  1757. uint8_t index;
  1758. unsigned int data;
  1759. struct fru_picmgext_slot_desc *slot_d;
  1760. slot_d =
  1761. (struct fru_picmgext_slot_desc*)&fru_data[offset];
  1762. offset += sizeof(struct fru_picmgext_slot_desc);
  1763. printf(" FRU_PICMG_BACKPLANE_P2P\n");
  1764. while (offset <= (start_offset+length)) {
  1765. printf("\n");
  1766. printf(" Channel Type: ");
  1767. switch (slot_d->chan_type)
  1768. {
  1769. case 0x00:
  1770. case 0x07:
  1771. printf("PICMG 2.9\n");
  1772. break;
  1773. case 0x08:
  1774. printf("Single Port Fabric IF\n");
  1775. break;
  1776. case 0x09:
  1777. printf("Double Port Fabric IF\n");
  1778. break;
  1779. case 0x0a:
  1780. printf("Full Channel Fabric IF\n");
  1781. break;
  1782. case 0x0b:
  1783. printf("Base IF\n");
  1784. break;
  1785. case 0x0c:
  1786. printf("Update Channel IF\n");
  1787. break;
  1788. case 0x0d:
  1789. printf("ShMC Cross Connect\n");
  1790. break;
  1791. default:
  1792. printf("Unknown IF (0x%x)\n",
  1793. slot_d->chan_type);
  1794. break;
  1795. }
  1796. printf(" Slot Addr. : %02x\n",
  1797. slot_d->slot_addr );
  1798. printf(" Channel Count: %i\n",
  1799. slot_d->chn_count);
  1800. for (index = 0;
  1801. index < (slot_d->chn_count);
  1802. index++) {
  1803. struct fru_picmgext_chn_desc *d;
  1804. data = (fru_data[offset+0]) |
  1805. (fru_data[offset+1] << 8) |
  1806. (fru_data[offset+2] << 16);
  1807. d = (struct fru_picmgext_chn_desc *)&data;
  1808. if (verbose) {
  1809. printf( " "
  1810. "Chn: %02x -> "
  1811. "Chn: %02x in "
  1812. "Slot: %02x\n",
  1813. d->local_chn,
  1814. d->remote_chn,
  1815. d->remote_slot);
  1816. }
  1817. offset += FRU_PICMGEXT_CHN_DESC_RECORD_SIZE;
  1818. }
  1819. slot_d = (struct fru_picmgext_slot_desc*)&fru_data[offset];
  1820. offset += sizeof(struct fru_picmgext_slot_desc);
  1821. }
  1822. }
  1823. break;
  1824. case FRU_PICMG_ADDRESS_TABLE:
  1825. {
  1826. unsigned int hwaddr;
  1827. unsigned int sitetype;
  1828. unsigned int sitenum;
  1829. unsigned int entries;
  1830. unsigned int i;
  1831. char *picmg_site_type_strings[] = {
  1832. "AdvancedTCA Board",
  1833. "Power Entry",
  1834. "Shelf FRU Information",
  1835. "Dedicated ShMC",
  1836. "Fan Tray",
  1837. "Fan Filter Tray",
  1838. "Alarm",
  1839. "AdvancedMC Module",
  1840. "PMC",
  1841. "Rear Transition Module"};
  1842. printf(" FRU_PICMG_ADDRESS_TABLE\n");
  1843. printf(" Type/Len: 0x%02x\n", fru_data[offset++]);
  1844. printf(" Shelf Addr: ");
  1845. for (i=0;i<20;i++) {
  1846. printf("0x%02x ", fru_data[offset++]);
  1847. }
  1848. printf("\n");
  1849. entries = fru_data[offset++];
  1850. printf(" Addr Table Entries: 0x%02x\n", entries);
  1851. for (i=0; i<entries; i++) {
  1852. hwaddr = fru_data[offset];
  1853. sitenum = fru_data[offset + 1];
  1854. sitetype = fru_data[offset + 2];
  1855. printf(
  1856. " HWAddr: 0x%02x (0x%02x) SiteNum: %d SiteType: 0x%02x %s\n",
  1857. hwaddr, hwaddr * 2,
  1858. sitenum, sitetype,
  1859. (sitetype < 0xa) ?
  1860. picmg_site_type_strings[sitetype] :
  1861. "Reserved");
  1862. offset += 3;
  1863. }
  1864. }
  1865. break;
  1866. case FRU_PICMG_SHELF_POWER_DIST:
  1867. {
  1868. unsigned int entries;
  1869. unsigned int feeds;
  1870. unsigned int hwaddr;
  1871. unsigned int i;
  1872. unsigned int id;
  1873. unsigned int j;
  1874. unsigned int maxext;
  1875. unsigned int maxint;
  1876. unsigned int minexp;
  1877. printf(" FRU_PICMG_SHELF_POWER_DIST\n");
  1878. feeds = fru_data[offset++];
  1879. printf(" Number of Power Feeds: 0x%02x\n",
  1880. feeds);
  1881. for (i=0; i<feeds; i++) {
  1882. printf(" Feed %d:\n", i);
  1883. maxext = fru_data[offset] |
  1884. (fru_data[offset+1] << 8);
  1885. offset += 2;
  1886. maxint = fru_data[offset] |
  1887. (fru_data[offset+1] << 8);
  1888. offset += 2;
  1889. minexp = fru_data[offset];
  1890. offset += 1;
  1891. entries = fru_data[offset];
  1892. offset += 1;
  1893. printf(
  1894. " Max External Current: %d.%d Amps (0x%04x)\n",
  1895. maxext / 10, maxext % 10, maxext);
  1896. if (maxint < 0xffff) {
  1897. printf(
  1898. " Max Internal Current: %d.%d Amps (0x%04x)\n",
  1899. maxint / 10, maxint % 10,
  1900. maxint);
  1901. } else {
  1902. printf(
  1903. " Max Internal Current: Not Specified\n");
  1904. }
  1905. if (minexp >= 0x48 && minexp <= 0x90) {
  1906. printf(
  1907. " Min Expected Voltage: -%02d.%dV\n",
  1908. minexp / 2, (minexp % 2) * 5);
  1909. } else {
  1910. printf(
  1911. " Min Expected Voltage: -%dV (actual invalid value 0x%x)\n",
  1912. 36, minexp);
  1913. }
  1914. for (j=0; j < entries; j++) {
  1915. hwaddr = fru_data[offset++];
  1916. id = fru_data[offset++];
  1917. printf(
  1918. " FRU HW Addr: 0x%02x (0x%02x)",
  1919. hwaddr, hwaddr * 2);
  1920. printf(
  1921. " FRU ID: 0x%02x\n",
  1922. id);
  1923. }
  1924. }
  1925. }
  1926. break;
  1927. case FRU_PICMG_SHELF_ACTIVATION:
  1928. {
  1929. unsigned int i;
  1930. unsigned int count = 0;
  1931. printf(" FRU_PICMG_SHELF_ACTIVATION\n");
  1932. printf(
  1933. " Allowance for FRU Act Readiness: 0x%02x\n",
  1934. fru_data[offset++]);
  1935. count = fru_data[offset++];
  1936. printf(
  1937. " FRU activation and Power Desc Cnt: 0x%02x\n",
  1938. count);
  1939. for (i=0; i<count; i++) {
  1940. printf(" HW Addr: 0x%02x ",
  1941. fru_data[offset++]);
  1942. printf(" FRU ID: 0x%02x ",
  1943. fru_data[offset++]);
  1944. printf(" Max FRU Power: 0x%04x ",
  1945. fru_data[offset+0] |
  1946. (fru_data[offset+1]<<8));
  1947. offset += 2;
  1948. printf(" Config: 0x%02x \n",
  1949. fru_data[offset++]);
  1950. }
  1951. }
  1952. break;
  1953. case FRU_PICMG_SHMC_IP_CONN:
  1954. printf(" FRU_PICMG_SHMC_IP_CONN\n");
  1955. break;
  1956. case FRU_PICMG_BOARD_P2P:
  1957. printf(" FRU_PICMG_BOARD_P2P\n");
  1958. guid_count = fru_data[offset++];
  1959. printf(" GUID count: %2d\n", guid_count);
  1960. for (i = 0 ; i < guid_count; i++ ) {
  1961. int j;
  1962. printf(" GUID [%2d]: 0x", i);
  1963. for (j=0; j < sizeof(struct fru_picmgext_guid);
  1964. j++) {
  1965. printf("%02x", fru_data[offset+j]);
  1966. }
  1967. printf("\n");
  1968. offset += sizeof(struct fru_picmgext_guid);
  1969. }
  1970. printf("\n");
  1971. for (; offset < off + length;
  1972. offset += sizeof(struct fru_picmgext_link_desc)) {
  1973. /* to solve little endian /big endian problem */
  1974. struct fru_picmgext_link_desc *d;
  1975. unsigned int data = (fru_data[offset+0]) |
  1976. (fru_data[offset+1] << 8) |
  1977. (fru_data[offset+2] << 16) |
  1978. (fru_data[offset+3] << 24);
  1979. d = (struct fru_picmgext_link_desc *) &data;
  1980. printf(" Link Grouping ID: 0x%02x\n",
  1981. d->grouping);
  1982. printf(" Link Type Extension: 0x%02x - ",
  1983. d->ext);
  1984. if (d->type == FRU_PICMGEXT_LINK_TYPE_BASE) {
  1985. switch (d->ext) {
  1986. case 0:
  1987. printf("10/100/1000BASE-T Link (four-pair)\n");
  1988. break;
  1989. case 1:
  1990. printf("ShMC Cross-connect (two-pair)\n");
  1991. break;
  1992. default:
  1993. printf("Unknown\n");
  1994. break;
  1995. }
  1996. } else if (d->type == FRU_PICMGEXT_LINK_TYPE_FABRIC_ETHERNET) {
  1997. switch (d->ext) {
  1998. case 0:
  1999. printf("1000Base-BX\n");
  2000. break;
  2001. case 1:
  2002. printf("10GBase-BX4 [XAUI]\n");
  2003. break;
  2004. case 2:
  2005. printf("FC-PI\n");
  2006. break;
  2007. case 3:
  2008. printf("1000Base-KX\n");
  2009. break;
  2010. case 4:
  2011. printf("10GBase-KX4\n");
  2012. break;
  2013. default:
  2014. printf("Unknown\n");
  2015. break;
  2016. }
  2017. } else if (d->type == FRU_PICMGEXT_LINK_TYPE_FABRIC_ETHERNET_10GBD) {
  2018. switch (d->ext) {
  2019. case 0:
  2020. printf("10GBase-KR\n");
  2021. break;
  2022. case 1:
  2023. printf("40GBase-KR4\n");
  2024. break;
  2025. default:
  2026. printf("Unknown\n");
  2027. break;
  2028. }
  2029. } else if (d->type == FRU_PICMGEXT_LINK_TYPE_FABRIC_INFINIBAND) {
  2030. printf("Unknown\n");
  2031. } else if (d->type == FRU_PICMGEXT_LINK_TYPE_FABRIC_STAR) {
  2032. printf("Unknown\n");
  2033. } else if (d->type == FRU_PICMGEXT_LINK_TYPE_PCIE) {
  2034. printf("Unknown\n");
  2035. } else {
  2036. printf("Unknown\n");
  2037. }
  2038. printf(" Link Type: 0x%02x - ",
  2039. d->type);
  2040. switch (d->type) {
  2041. case FRU_PICMGEXT_LINK_TYPE_BASE:
  2042. printf("PICMG 3.0 Base Interface 10/100/1000\n");
  2043. break;
  2044. case FRU_PICMGEXT_LINK_TYPE_FABRIC_ETHERNET:
  2045. printf("PICMG 3.1 Ethernet Fabric Interface\n");
  2046. printf(" Base signaling Link Class\n");
  2047. break;
  2048. case FRU_PICMGEXT_LINK_TYPE_FABRIC_INFINIBAND:
  2049. printf("PICMG 3.2 Infiniband Fabric Interface\n");
  2050. break;
  2051. case FRU_PICMGEXT_LINK_TYPE_FABRIC_STAR:
  2052. printf("PICMG 3.3 Star Fabric Interface\n");
  2053. break;
  2054. case FRU_PICMGEXT_LINK_TYPE_PCIE:
  2055. printf("PICMG 3.4 PCI Express Fabric Interface\n");
  2056. break;
  2057. case FRU_PICMGEXT_LINK_TYPE_FABRIC_ETHERNET_10GBD:
  2058. printf("PICMG 3.1 Ethernet Fabric Interface\n");
  2059. printf(" 10.3125Gbd signaling Link Class\n");
  2060. break;
  2061. default:
  2062. if (d->type == 0 || d->type == 0xff) {
  2063. printf("Reserved\n");
  2064. } else if (d->type >= 0x06 && d->type <= 0xef) {
  2065. printf("Reserved\n");
  2066. } else if (d->type >= 0xf0 && d->type <= 0xfe) {
  2067. printf("OEM GUID Definition\n");
  2068. } else {
  2069. printf("Invalid\n");
  2070. }
  2071. break;
  2072. }
  2073. printf(" Link Designator: \n");
  2074. printf(" Port Flag: 0x%02x\n",
  2075. d->desig_port);
  2076. printf(" Interface: 0x%02x - ",
  2077. d->desig_if);
  2078. switch (d->desig_if) {
  2079. case FRU_PICMGEXT_DESIGN_IF_BASE:
  2080. printf("Base Interface\n");
  2081. break;
  2082. case FRU_PICMGEXT_DESIGN_IF_FABRIC:
  2083. printf("Fabric Interface\n");
  2084. break;
  2085. case FRU_PICMGEXT_DESIGN_IF_UPDATE_CHANNEL:
  2086. printf("Update Channel\n");
  2087. break;
  2088. case FRU_PICMGEXT_DESIGN_IF_RESERVED:
  2089. printf("Reserved\n");
  2090. break;
  2091. default:
  2092. printf("Invalid");
  2093. break;
  2094. }
  2095. printf(" Channel Number: 0x%02x\n",
  2096. d->desig_channel);
  2097. printf("\n");
  2098. }
  2099. break;
  2100. case FRU_AMC_CURRENT:
  2101. {
  2102. unsigned char current;
  2103. printf(" FRU_AMC_CURRENT\n");
  2104. current = fru_data[offset];
  2105. printf(" Current draw(@12V): %.2f A [ %.2f Watt ]\n",
  2106. (float)current / 10.0f,
  2107. (float)current / 10.0f * 12.0f);
  2108. printf("\n");
  2109. }
  2110. break;
  2111. case FRU_AMC_ACTIVATION:
  2112. printf(" FRU_AMC_ACTIVATION\n");
  2113. {
  2114. uint16_t max_current;
  2115. max_current = fru_data[offset];
  2116. max_current |= fru_data[++offset]<<8;
  2117. printf(" Maximum Internal Current(@12V): %.2f A [ %.2f Watt ]\n",
  2118. (float)max_current / 10.0f,
  2119. (float)max_current / 10.0f * 12.0f);
  2120. printf(" Module Activation Readiness: %i sec.\n", fru_data[++offset]);
  2121. printf(" Descriptor Count: %i\n", fru_data[++offset]);
  2122. printf("\n");
  2123. for(++offset; offset < off + length;
  2124. offset += sizeof(struct fru_picmgext_activation_record))
  2125. {
  2126. struct fru_picmgext_activation_record *a;
  2127. a = (struct fru_picmgext_activation_record *)&fru_data[offset];
  2128. printf(" IPMB-Address: 0x%x\n",
  2129. a->ibmb_addr);
  2130. printf(" Max. Module Current: %.2f A\n",
  2131. (float)a->max_module_curr / 10.0f);
  2132. printf("\n");
  2133. }
  2134. }
  2135. break;
  2136. case FRU_AMC_CARRIER_P2P:
  2137. {
  2138. uint16_t index;
  2139. printf(" FRU_CARRIER_P2P\n");
  2140. for(; offset < off + length; ) {
  2141. struct fru_picmgext_carrier_p2p_record * h =
  2142. (struct fru_picmgext_carrier_p2p_record *)&fru_data[offset];
  2143. printf("\n");
  2144. printf(" Resource ID: %i",
  2145. (h->resource_id & 0x07));
  2146. printf(" Type: ");
  2147. if ((h->resource_id>>7) == 1) {
  2148. printf("AMC\n");
  2149. } else {
  2150. printf("Local\n");
  2151. }
  2152. printf(" Descriptor Count: %i\n",
  2153. h->p2p_count);
  2154. offset += sizeof(struct fru_picmgext_carrier_p2p_record);
  2155. for (index = 0; index < h->p2p_count; index++) {
  2156. /* to solve little endian /big endian problem */
  2157. unsigned char data[3];
  2158. struct fru_picmgext_carrier_p2p_descriptor * desc;
  2159. # ifndef WORDS_BIGENDIAN
  2160. data[0] = fru_data[offset+0];
  2161. data[1] = fru_data[offset+1];
  2162. data[2] = fru_data[offset+2];
  2163. # else
  2164. data[0] = fru_data[offset+2];
  2165. data[1] = fru_data[offset+1];
  2166. data[2] = fru_data[offset+0];
  2167. # endif
  2168. desc = (struct fru_picmgext_carrier_p2p_descriptor*)&data;
  2169. printf(" Port: %02d\t-> Remote Port: %02d\t",
  2170. desc->local_port, desc->remote_port);
  2171. if ((desc->remote_resource_id >> 7) == 1) {
  2172. printf("[ AMC ID: %02d ]\n",
  2173. desc->remote_resource_id & 0x0F);
  2174. } else {
  2175. printf("[ local ID: %02d ]\n",
  2176. desc->remote_resource_id & 0x0F);
  2177. }
  2178. offset += sizeof(struct fru_picmgext_carrier_p2p_descriptor);
  2179. }
  2180. }
  2181. }
  2182. break;
  2183. case FRU_AMC_P2P:
  2184. {
  2185. unsigned int index;
  2186. unsigned char channel_count;
  2187. struct fru_picmgext_amc_p2p_record * h;
  2188. printf(" FRU_AMC_P2P\n");
  2189. guid_count = fru_data[offset];
  2190. printf(" GUID count: %2d\n", guid_count);
  2191. for (i = 0 ; i < guid_count; i++) {
  2192. int j;
  2193. printf(" GUID %2d: ", i);
  2194. for (j=0; j < sizeof(struct fru_picmgext_guid);
  2195. j++) {
  2196. printf("%02x", fru_data[offset+j]);
  2197. offset += sizeof(struct fru_picmgext_guid);
  2198. printf("\n");
  2199. }
  2200. h = (struct fru_picmgext_amc_p2p_record *)&fru_data[++offset];
  2201. printf(" %s",
  2202. (h->record_type ?
  2203. "AMC Module:" : "On-Carrier Device"));
  2204. printf(" Resource ID: %i\n", h->resource_id);
  2205. offset += sizeof(struct fru_picmgext_amc_p2p_record);
  2206. channel_count = fru_data[offset++];
  2207. printf(" Descriptor Count: %i\n",
  2208. channel_count);
  2209. for (index = 0; index < channel_count; index++) {
  2210. unsigned int data;
  2211. struct fru_picmgext_amc_channel_desc_record *d;
  2212. /* pack the data in little endian format.
  2213. * Stupid intel...
  2214. */
  2215. data = fru_data[offset] |
  2216. (fru_data[offset + 1] << 8) |
  2217. (fru_data[offset + 2] << 16);
  2218. d = (struct fru_picmgext_amc_channel_desc_record *)&data;
  2219. printf(" Lane 0 Port: %i\n",
  2220. d->lane0port);
  2221. printf(" Lane 1 Port: %i\n",
  2222. d->lane1port);
  2223. printf(" Lane 2 Port: %i\n",
  2224. d->lane2port);
  2225. printf(" Lane 3 Port: %i\n\n",
  2226. d->lane3port);
  2227. offset += FRU_PICMGEXT_AMC_CHANNEL_DESC_RECORD_SIZE;
  2228. }
  2229. for (; offset < off + length;) {
  2230. unsigned int data[2];
  2231. struct fru_picmgext_amc_link_desc_record *l;
  2232. l = (struct fru_picmgext_amc_link_desc_record *)&data[0];
  2233. data[0] = fru_data[offset] |
  2234. (fru_data[offset + 1] << 8) |
  2235. (fru_data[offset + 2] << 16) |
  2236. (fru_data[offset + 3] << 24);
  2237. data[1] = fru_data[offset + 4];
  2238. printf( " Link Designator: Channel ID: %i\n"
  2239. " Port Flag 0: %s%s%s%s\n",
  2240. l->channel_id,
  2241. (l->port_flag_0)?"o":"-",
  2242. (l->port_flag_1)?"o":"-",
  2243. (l->port_flag_2)?"o":"-",
  2244. (l->port_flag_3)?"o":"-" );
  2245. switch (l->type) {
  2246. case FRU_PICMGEXT_AMC_LINK_TYPE_PCIE:
  2247. /* AMC.1 */
  2248. printf( " Link Type: %02x - "
  2249. "AMC.1 PCI Express\n", l->type);
  2250. switch (l->type_ext) {
  2251. case AMC_LINK_TYPE_EXT_PCIE_G1_NSSC:
  2252. printf( " Link Type Ext: %i - "
  2253. " Gen 1 capable - non SSC\n",
  2254. l->type_ext);
  2255. break;
  2256. case AMC_LINK_TYPE_EXT_PCIE_G1_SSC:
  2257. printf( " Link Type Ext: %i - "
  2258. " Gen 1 capable - SSC\n",
  2259. l->type_ext);
  2260. break;
  2261. case AMC_LINK_TYPE_EXT_PCIE_G2_NSSC:
  2262. printf( " Link Type Ext: %i - "
  2263. " Gen 2 capable - non SSC\n",
  2264. l->type_ext);
  2265. break;
  2266. case AMC_LINK_TYPE_EXT_PCIE_G2_SSC:
  2267. printf( " Link Type Ext: %i - "
  2268. " Gen 2 capable - SSC\n",
  2269. l->type_ext);
  2270. break;
  2271. default:
  2272. printf( " Link Type Ext: %i - "
  2273. " Invalid\n",
  2274. l->type_ext);
  2275. break;
  2276. }
  2277. break;
  2278. case FRU_PICMGEXT_AMC_LINK_TYPE_PCIE_AS1:
  2279. case FRU_PICMGEXT_AMC_LINK_TYPE_PCIE_AS2:
  2280. /* AMC.1 */
  2281. printf( " Link Type: %02x - "
  2282. "AMC.1 PCI Express Advanced Switching\n",
  2283. l->type);
  2284. printf(" Link Type Ext: %i\n",
  2285. l->type_ext);
  2286. break;
  2287. case FRU_PICMGEXT_AMC_LINK_TYPE_ETHERNET:
  2288. /* AMC.2 */
  2289. printf( " Link Type: %02x - "
  2290. "AMC.2 Ethernet\n",
  2291. l->type);
  2292. switch (l->type_ext) {
  2293. case AMC_LINK_TYPE_EXT_ETH_1000_BX:
  2294. printf( " Link Type Ext: %i - "
  2295. " 1000Base-Bx (SerDES Gigabit) Ethernet Link\n",
  2296. l->type_ext);
  2297. break;
  2298. case AMC_LINK_TYPE_EXT_ETH_10G_XAUI:
  2299. printf( " Link Type Ext: %i - "
  2300. " 10Gbit XAUI Ethernet Link\n",
  2301. l->type_ext);
  2302. break;
  2303. default:
  2304. printf( " Link Type Ext: %i - "
  2305. " Invalid\n",
  2306. l->type_ext);
  2307. break;
  2308. }
  2309. break;
  2310. case FRU_PICMGEXT_AMC_LINK_TYPE_STORAGE:
  2311. /* AMC.3 */
  2312. printf( " Link Type: %02x - "
  2313. "AMC.3 Storage\n",
  2314. l->type);
  2315. switch (l->type_ext) {
  2316. case AMC_LINK_TYPE_EXT_STORAGE_FC:
  2317. printf( " Link Type Ext: %i - "
  2318. " Fibre Channel\n",
  2319. l->type_ext);
  2320. break;
  2321. case AMC_LINK_TYPE_EXT_STORAGE_SATA:
  2322. printf( " Link Type Ext: %i - "
  2323. " Serial ATA\n",
  2324. l->type_ext);
  2325. break;
  2326. case AMC_LINK_TYPE_EXT_STORAGE_SAS:
  2327. printf( " Link Type Ext: %i - "
  2328. " Serial Attached SCSI\n",
  2329. l->type_ext);
  2330. break;
  2331. default:
  2332. printf( " Link Type Ext: %i - "
  2333. " Invalid\n",
  2334. l->type_ext);
  2335. break;
  2336. }
  2337. break;
  2338. case FRU_PICMGEXT_AMC_LINK_TYPE_RAPIDIO:
  2339. /* AMC.4 */
  2340. printf( " Link Type: %02x - "
  2341. "AMC.4 Serial Rapid IO\n",
  2342. l->type);
  2343. printf(" Link Type Ext: %i\n",
  2344. l->type_ext);
  2345. break;
  2346. default:
  2347. printf( " Link Type: %02x - "
  2348. "reserved or OEM GUID",
  2349. l->type);
  2350. printf(" Link Type Ext: %i\n",
  2351. l->type_ext);
  2352. break;
  2353. }
  2354. printf(" Link group Id: %i\n",
  2355. l->group_id);
  2356. printf(" Link Asym Match: %i\n\n",
  2357. l->asym_match);
  2358. offset += FRU_PICMGEXT_AMC_LINK_DESC_RECORD_SIZE;
  2359. }
  2360. }
  2361. }
  2362. break;
  2363. case FRU_AMC_CARRIER_INFO:
  2364. {
  2365. unsigned char extVersion;
  2366. unsigned char siteCount;
  2367. printf(" FRU_CARRIER_INFO\n");
  2368. extVersion = fru_data[offset++];
  2369. siteCount = fru_data[offset++];
  2370. printf(" AMC.0 extension version: R%d.%d\n",
  2371. (extVersion >> 0)& 0x0F,
  2372. (extVersion >> 4)& 0x0F );
  2373. printf(" Carrier Sie Number Cnt: %d\n", siteCount);
  2374. for (i = 0 ; i < siteCount; i++ ){
  2375. printf(" Site ID: %i \n", fru_data[offset++]);
  2376. }
  2377. printf("\n");
  2378. }
  2379. break;
  2380. case FRU_PICMG_CLK_CARRIER_P2P:
  2381. {
  2382. unsigned char desc_count;
  2383. int i,j;
  2384. printf(" FRU_PICMG_CLK_CARRIER_P2P\n");
  2385. desc_count = fru_data[offset++];
  2386. for(i=0; i<desc_count; i++){
  2387. unsigned char resource_id;
  2388. unsigned char channel_count;
  2389. resource_id = fru_data[offset++];
  2390. channel_count = fru_data[offset++];
  2391. printf("\n");
  2392. printf(" Clock Resource ID: 0x%02x Type: ", resource_id);
  2393. if((resource_id & 0xC0)>>6 == 0) {printf("On-Carrier-Device\n");}
  2394. else if((resource_id & 0xC0)>>6 == 1) {printf("AMC slot\n");}
  2395. else if((resource_id & 0xC0)>>6 == 2) {printf("Backplane\n");}
  2396. else{ printf("reserved\n");}
  2397. printf(" Channel Count: 0x%02x\n", channel_count);
  2398. for(j=0; j<channel_count; j++){
  2399. unsigned char loc_channel, rem_channel, rem_resource;
  2400. loc_channel = fru_data[offset++];
  2401. rem_channel = fru_data[offset++];
  2402. rem_resource = fru_data[offset++];
  2403. printf(" CLK-ID: 0x%02x ->", loc_channel);
  2404. printf(" remote CLKID: 0x%02x ", rem_channel);
  2405. if((rem_resource & 0xC0)>>6 == 0) {printf("[ Carrier-Dev");}
  2406. else if((rem_resource & 0xC0)>>6 == 1) {printf("[ AMC slot ");}
  2407. else if((rem_resource & 0xC0)>>6 == 2) {printf("[ Backplane ");}
  2408. else{ printf("reserved ");}
  2409. printf(" 0x%02x ]\n", rem_resource&0xF);
  2410. }
  2411. }
  2412. printf("\n");
  2413. }
  2414. break;
  2415. case FRU_PICMG_CLK_CONFIG:
  2416. {
  2417. unsigned char resource_id, descr_count;
  2418. int i,j;
  2419. printf(" FRU_PICMG_CLK_CONFIG\n");
  2420. resource_id = fru_data[offset++];
  2421. descr_count = fru_data[offset++];
  2422. printf("\n");
  2423. printf(" Clock Resource ID: 0x%02x\n", resource_id);
  2424. printf(" Descr. Count: 0x%02x\n", descr_count);
  2425. for(i=0; i<descr_count; i++){
  2426. unsigned char channel_id, control;
  2427. unsigned char indirect_cnt, direct_cnt;
  2428. channel_id = fru_data[offset++];
  2429. control = fru_data[offset++];
  2430. printf(" CLK-ID: 0x%02x - ", channel_id);
  2431. printf("CTRL 0x%02x [ %12s ]\n",
  2432. control,
  2433. ((control&0x1)==0)?"Carrier IPMC":"Application");
  2434. indirect_cnt = fru_data[offset++];
  2435. direct_cnt = fru_data[offset++];
  2436. printf(" Cnt: Indirect 0x%02x / Direct 0x%02x\n",
  2437. indirect_cnt,
  2438. direct_cnt);
  2439. /* indirect desc */
  2440. for(j=0; j<indirect_cnt; j++){
  2441. unsigned char feature;
  2442. unsigned char dep_chn_id;
  2443. feature = fru_data[offset++];
  2444. dep_chn_id = fru_data[offset++];
  2445. printf(" Feature: 0x%02x [%8s] - ", feature, (feature&0x1)==1?"Source":"Receiver");
  2446. printf(" Dep. CLK-ID: 0x%02x\n", dep_chn_id);
  2447. }
  2448. /* direct desc */
  2449. for(j=0; j<direct_cnt; j++){
  2450. unsigned char feature, family, accuracy;
  2451. unsigned int freq, min_freq, max_freq;
  2452. feature = fru_data[offset++];
  2453. family = fru_data[offset++];
  2454. accuracy = fru_data[offset++];
  2455. freq = (fru_data[offset+0] << 0 ) | (fru_data[offset+1] << 8 )
  2456. | (fru_data[offset+2] << 16) | (fru_data[offset+3] << 24);
  2457. offset += 4;
  2458. min_freq = (fru_data[offset+0] << 0 ) | (fru_data[offset+1] << 8 )
  2459. | (fru_data[offset+2] << 16) | (fru_data[offset+3] << 24);
  2460. offset += 4;
  2461. max_freq = (fru_data[offset+0] << 0 ) | (fru_data[offset+1] << 8 )
  2462. | (fru_data[offset+2] << 16) | (fru_data[offset+3] << 24);
  2463. offset += 4;
  2464. printf(" - Feature: 0x%02x - PLL: %x / Asym: %s\n",
  2465. feature,
  2466. (feature > 1) & 1,
  2467. (feature&1)?"Source":"Receiver");
  2468. printf(" Family: 0x%02x - AccLVL: 0x%02x\n", family, accuracy);
  2469. printf(" FRQ: %-9ld - min: %-9ld - max: %-9ld\n",
  2470. freq, min_freq, max_freq);
  2471. }
  2472. printf("\n");
  2473. }
  2474. printf("\n");
  2475. }
  2476. break;
  2477. case FRU_UTCA_FRU_INFO_TABLE:
  2478. case FRU_UTCA_CARRIER_MNG_IP:
  2479. case FRU_UTCA_CARRIER_INFO:
  2480. case FRU_UTCA_CARRIER_LOCATION:
  2481. case FRU_UTCA_SHMC_IP_LINK:
  2482. case FRU_UTCA_POWER_POLICY:
  2483. case FRU_UTCA_ACTIVATION:
  2484. case FRU_UTCA_PM_CAPABILTY:
  2485. case FRU_UTCA_FAN_GEOGRAPHY:
  2486. case FRU_UTCA_CLOCK_MAPPING:
  2487. case FRU_UTCA_MSG_BRIDGE_POLICY:
  2488. case FRU_UTCA_OEM_MODULE_DESC:
  2489. printf(" Not implemented yet. uTCA specific record found!!\n");
  2490. printf(" - Record ID: 0x%02x\n", h->record_id);
  2491. break;
  2492. default:
  2493. printf(" Unknown OEM Extension Record ID: %x\n", h->record_id);
  2494. break;
  2495. }
  2496. }
  2497. /* __ipmi_fru_print - Do actual work to print a FRU by its ID
  2498. *
  2499. * @intf: ipmi interface
  2500. * @id: fru id
  2501. *
  2502. * returns -1 on error
  2503. * returns 0 if successful
  2504. * returns 1 if device not present
  2505. */
  2506. static int
  2507. __ipmi_fru_print(struct ipmi_intf * intf, uint8_t id)
  2508. {
  2509. struct ipmi_rs * rsp;
  2510. struct ipmi_rq req;
  2511. struct fru_info fru;
  2512. struct fru_header header;
  2513. uint8_t msg_data[4];
  2514. memset(&fru, 0, sizeof(struct fru_info));
  2515. memset(&header, 0, sizeof(struct fru_header));
  2516. /*
  2517. * get info about this FRU
  2518. */
  2519. memset(msg_data, 0, 4);
  2520. msg_data[0] = id;
  2521. memset(&req, 0, sizeof(req));
  2522. req.msg.netfn = IPMI_NETFN_STORAGE;
  2523. req.msg.cmd = GET_FRU_INFO;
  2524. req.msg.data = msg_data;
  2525. req.msg.data_len = 1;
  2526. rsp = intf->sendrecv(intf, &req);
  2527. if (rsp == NULL) {
  2528. printf(" Device not present (No Response)\n");
  2529. return -1;
  2530. }
  2531. if (rsp->ccode > 0) {
  2532. printf(" Device not present (%s)\n",
  2533. val2str(rsp->ccode, completion_code_vals));
  2534. return -1;
  2535. }
  2536. memset(&fru, 0, sizeof(fru));
  2537. fru.size = (rsp->data[1] << 8) | rsp->data[0];
  2538. fru.access = rsp->data[2] & 0x1;
  2539. lprintf(LOG_DEBUG, "fru.size = %d bytes (accessed by %s)",
  2540. fru.size, fru.access ? "words" : "bytes");
  2541. if (fru.size < 1) {
  2542. lprintf(LOG_ERR, " Invalid FRU size %d", fru.size);
  2543. return -1;
  2544. }
  2545. /*
  2546. * retrieve the FRU header
  2547. */
  2548. msg_data[0] = id;
  2549. msg_data[1] = 0;
  2550. msg_data[2] = 0;
  2551. msg_data[3] = 8;
  2552. memset(&req, 0, sizeof(req));
  2553. req.msg.netfn = IPMI_NETFN_STORAGE;
  2554. req.msg.cmd = GET_FRU_DATA;
  2555. req.msg.data = msg_data;
  2556. req.msg.data_len = 4;
  2557. rsp = intf->sendrecv(intf, &req);
  2558. if (rsp == NULL) {
  2559. printf(" Device not present (No Response)\n");
  2560. return 1;
  2561. }
  2562. if (rsp->ccode > 0) {
  2563. printf(" Device not present (%s)\n",
  2564. val2str(rsp->ccode, completion_code_vals));
  2565. return 1;
  2566. }
  2567. if (verbose > 1)
  2568. printbuf(rsp->data, rsp->data_len, "FRU DATA");
  2569. memcpy(&header, rsp->data + 1, 8);
  2570. if (header.version != 1) {
  2571. lprintf(LOG_ERR, " Unknown FRU header version 0x%02x",
  2572. header.version);
  2573. return -1;
  2574. }
  2575. /* offsets need converted to bytes
  2576. * but that conversion is not done to the structure
  2577. * because we may end up with offset > 255
  2578. * which would overflow our 1-byte offset field */
  2579. lprintf(LOG_DEBUG, "fru.header.version: 0x%x",
  2580. header.version);
  2581. lprintf(LOG_DEBUG, "fru.header.offset.internal: 0x%x",
  2582. header.offset.internal * 8);
  2583. lprintf(LOG_DEBUG, "fru.header.offset.chassis: 0x%x",
  2584. header.offset.chassis * 8);
  2585. lprintf(LOG_DEBUG, "fru.header.offset.board: 0x%x",
  2586. header.offset.board * 8);
  2587. lprintf(LOG_DEBUG, "fru.header.offset.product: 0x%x",
  2588. header.offset.product * 8);
  2589. lprintf(LOG_DEBUG, "fru.header.offset.multi: 0x%x",
  2590. header.offset.multi * 8);
  2591. /*
  2592. * rather than reading the entire part
  2593. * only read the areas we'll format
  2594. */
  2595. /* chassis area */
  2596. if ((header.offset.chassis*8) >= sizeof(struct fru_header))
  2597. fru_area_print_chassis(intf, &fru, id, header.offset.chassis*8);
  2598. /* board area */
  2599. if ((header.offset.board*8) >= sizeof(struct fru_header))
  2600. fru_area_print_board(intf, &fru, id, header.offset.board*8);
  2601. /* product area */
  2602. if ((header.offset.product*8) >= sizeof(struct fru_header))
  2603. fru_area_print_product(intf, &fru, id, header.offset.product*8);
  2604. /* multirecord area */
  2605. if( verbose==0 ) /* scipp parsing multirecord */
  2606. return 0;
  2607. if ((header.offset.multi*8) >= sizeof(struct fru_header))
  2608. fru_area_print_multirec(intf, &fru, id, header.offset.multi*8);
  2609. return 0;
  2610. }
  2611. /* ipmi_fru_print - Print a FRU from its SDR locator record
  2612. *
  2613. * @intf: ipmi interface
  2614. * @fru: SDR FRU Locator Record
  2615. *
  2616. * returns -1 on error
  2617. */
  2618. int
  2619. ipmi_fru_print(struct ipmi_intf * intf, struct sdr_record_fru_locator * fru)
  2620. {
  2621. char desc[17];
  2622. uint8_t bridged_request = 0;
  2623. uint32_t save_addr;
  2624. uint32_t save_channel;
  2625. int rc = 0;
  2626. if (fru == NULL)
  2627. return __ipmi_fru_print(intf, 0);
  2628. /* Logical FRU Device
  2629. * dev_type == 0x10
  2630. * modifier
  2631. * 0x00 = IPMI FRU Inventory
  2632. * 0x01 = DIMM Memory ID
  2633. * 0x02 = IPMI FRU Inventory
  2634. * 0x03 = System Processor FRU
  2635. * 0xff = unspecified
  2636. *
  2637. * EEPROM 24C01 or equivalent
  2638. * dev_type >= 0x08 && dev_type <= 0x0f
  2639. * modifier
  2640. * 0x00 = unspecified
  2641. * 0x01 = DIMM Memory ID
  2642. * 0x02 = IPMI FRU Inventory
  2643. * 0x03 = System Processor Cartridge
  2644. */
  2645. if (fru->dev_type != 0x10 &&
  2646. (fru->dev_type_modifier != 0x02 ||
  2647. fru->dev_type < 0x08 || fru->dev_type > 0x0f))
  2648. return -1;
  2649. if (fru->dev_slave_addr == IPMI_BMC_SLAVE_ADDR &&
  2650. fru->device_id == 0)
  2651. return 0;
  2652. memset(desc, 0, sizeof(desc));
  2653. memcpy(desc, fru->id_string, fru->id_code & 0x01f);
  2654. desc[fru->id_code & 0x01f] = 0;
  2655. printf("FRU Device Description : %s (ID %d)\n", desc, fru->device_id);
  2656. switch (fru->dev_type_modifier) {
  2657. case 0x00:
  2658. case 0x02:
  2659. if (BRIDGE_TO_SENSOR(intf, fru->dev_slave_addr,
  2660. fru->channel_num)) {
  2661. bridged_request = 1;
  2662. save_addr = intf->target_addr;
  2663. intf->target_addr = fru->dev_slave_addr;
  2664. save_channel = intf->target_channel;
  2665. intf->target_channel = fru->channel_num;
  2666. }
  2667. /* print FRU */
  2668. rc = __ipmi_fru_print(intf, fru->device_id);
  2669. if (bridged_request) {
  2670. intf->target_addr = save_addr;
  2671. intf->target_channel = save_channel;
  2672. }
  2673. break;
  2674. case 0x01:
  2675. rc = ipmi_spd_print_fru(intf, fru->device_id);
  2676. break;
  2677. default:
  2678. if (verbose)
  2679. printf(" Unsupported device 0x%02x "
  2680. "type 0x%02x with modifier 0x%02x\n",
  2681. fru->device_id, fru->dev_type,
  2682. fru->dev_type_modifier);
  2683. else
  2684. printf(" Unsupported device\n");
  2685. }
  2686. printf("\n");
  2687. return rc;
  2688. }
  2689. /* ipmi_fru_print_all - Print builtin FRU + SDR FRU Locator records
  2690. *
  2691. * @intf: ipmi interface
  2692. *
  2693. * returns -1 on error
  2694. */
  2695. static int
  2696. ipmi_fru_print_all(struct ipmi_intf * intf)
  2697. {
  2698. struct ipmi_sdr_iterator * itr;
  2699. struct sdr_get_rs * header;
  2700. struct sdr_record_fru_locator * fru;
  2701. int rc;
  2702. struct ipmi_rs * rsp;
  2703. struct ipmi_rq req;
  2704. struct ipm_devid_rsp *devid;
  2705. struct sdr_record_mc_locator * mc;
  2706. uint32_t save_addr;
  2707. printf("FRU Device Description : Builtin FRU Device (ID 0)\n");
  2708. /* TODO: Figure out if FRU device 0 may show up in SDR records. */
  2709. /* Do a Get Device ID command to determine device support */
  2710. memset (&req, 0, sizeof(req));
  2711. req.msg.netfn = IPMI_NETFN_APP;
  2712. req.msg.cmd = BMC_GET_DEVICE_ID;
  2713. req.msg.data_len = 0;
  2714. rsp = intf->sendrecv(intf, &req);
  2715. if (rsp == NULL) {
  2716. lprintf(LOG_ERR, "Get Device ID command failed");
  2717. return -1;
  2718. }
  2719. if (rsp->ccode > 0) {
  2720. lprintf(LOG_ERR, "Get Device ID command failed: %s",
  2721. val2str(rsp->ccode, completion_code_vals));
  2722. return -1;
  2723. }
  2724. devid = (struct ipm_devid_rsp *) rsp->data;
  2725. /* Check the FRU inventory device bit to decide whether various */
  2726. /* FRU commands can be issued to FRU device #0 LUN 0 */
  2727. if (devid->adtl_device_support & 0x08) { /* FRU Inventory Device bit? */
  2728. rc = ipmi_fru_print(intf, NULL);
  2729. printf("\n");
  2730. }
  2731. if ((itr = ipmi_sdr_start(intf, 0)) == NULL)
  2732. return -1;
  2733. /* Walk the SDRs looking for FRU Devices and Management Controller Devices. */
  2734. /* For FRU devices, print the FRU from the SDR locator record. */
  2735. /* For MC devices, issue FRU commands to the satellite controller to print */
  2736. /* FRU data. */
  2737. while ((header = ipmi_sdr_get_next_header(intf, itr)) != NULL)
  2738. {
  2739. if (header->type == SDR_RECORD_TYPE_MC_DEVICE_LOCATOR ) {
  2740. /* Check the capabilities of the Management Controller Device */
  2741. mc = (struct sdr_record_mc_locator *)
  2742. ipmi_sdr_get_record(intf, header, itr);
  2743. /* Does this MC device support FRU inventory device? */
  2744. if (mc && (mc->dev_support & 0x08) && /* FRU inventory device? */
  2745. intf->target_addr != mc->dev_slave_addr) {
  2746. /* Yes. Prepare to issue FRU commands to FRU device #0 LUN 0 */
  2747. /* using the slave address specified in the MC record. */
  2748. /* save current target address */
  2749. save_addr = intf->target_addr;
  2750. /* set new target address to satellite controller */
  2751. intf->target_addr = mc->dev_slave_addr;
  2752. printf("FRU Device Description : %-16s\n", mc->id_string);
  2753. /* print the FRU by issuing FRU commands to the satellite */
  2754. /* controller. */
  2755. rc = __ipmi_fru_print(intf, 0);
  2756. printf("\n");
  2757. /* restore previous target */
  2758. intf->target_addr = save_addr;
  2759. }
  2760. if (mc) {
  2761. free(mc);
  2762. mc = NULL;
  2763. }
  2764. continue;
  2765. }
  2766. if (header->type != SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR)
  2767. continue;
  2768. /* Print the FRU from the SDR locator record. */
  2769. fru = (struct sdr_record_fru_locator *)
  2770. ipmi_sdr_get_record(intf, header, itr);
  2771. if (fru == NULL || !fru->logical) {
  2772. if (fru) {
  2773. free(fru);
  2774. fru = NULL;
  2775. }
  2776. continue;
  2777. }
  2778. rc = ipmi_fru_print(intf, fru);
  2779. free(fru);
  2780. fru = NULL;
  2781. }
  2782. ipmi_sdr_end(intf, itr);
  2783. return rc;
  2784. }
  2785. /* ipmi_fru_read_help() - print help text for 'read'
  2786. *
  2787. * returns void
  2788. */
  2789. void
  2790. ipmi_fru_read_help()
  2791. {
  2792. lprintf(LOG_NOTICE, "fru read <fru id> <fru file>");
  2793. lprintf(LOG_NOTICE, "Note: FRU ID and file(incl. full path) must be specified.");
  2794. lprintf(LOG_NOTICE, "Example: ipmitool fru read 0 /root/fru.bin");
  2795. } /* ipmi_fru_read_help() */
  2796. static void
  2797. ipmi_fru_read_to_bin(struct ipmi_intf * intf,
  2798. char * pFileName,
  2799. uint8_t fruId)
  2800. {
  2801. struct ipmi_rs * rsp;
  2802. struct ipmi_rq req;
  2803. struct fru_info fru;
  2804. uint8_t msg_data[4];
  2805. uint8_t * pFruBuf;
  2806. msg_data[0] = fruId;
  2807. memset(&req, 0, sizeof(req));
  2808. req.msg.netfn = IPMI_NETFN_STORAGE;
  2809. req.msg.cmd = GET_FRU_INFO;
  2810. req.msg.data = msg_data;
  2811. req.msg.data_len = 1;
  2812. rsp = intf->sendrecv(intf, &req);
  2813. if (!rsp)
  2814. return;
  2815. if (rsp->ccode > 0) {
  2816. if (rsp->ccode == 0xc3)
  2817. printf (" Timeout accessing FRU info. (Device not present?)\n");
  2818. return;
  2819. }
  2820. memset(&fru, 0, sizeof(fru));
  2821. fru.size = (rsp->data[1] << 8) | rsp->data[0];
  2822. fru.access = rsp->data[2] & 0x1;
  2823. if (verbose) {
  2824. printf("Fru Size = %d bytes\n",fru.size);
  2825. printf("Fru Access = %xh\n", fru.access);
  2826. }
  2827. pFruBuf = malloc(fru.size);
  2828. if (pFruBuf != NULL) {
  2829. printf("Fru Size : %d bytes\n",fru.size);
  2830. read_fru_area(intf, &fru, fruId, 0, fru.size, pFruBuf);
  2831. } else {
  2832. lprintf(LOG_ERR, "Cannot allocate %d bytes\n", fru.size);
  2833. return;
  2834. }
  2835. if(pFruBuf != NULL)
  2836. {
  2837. FILE * pFile;
  2838. pFile = fopen(pFileName,"wb");
  2839. if (pFile) {
  2840. fwrite(pFruBuf, fru.size, 1, pFile);
  2841. printf("Done\n");
  2842. } else {
  2843. lprintf(LOG_ERR, "Error opening file %s\n", pFileName);
  2844. free(pFruBuf);
  2845. pFruBuf = NULL;
  2846. return;
  2847. }
  2848. fclose(pFile);
  2849. }
  2850. free(pFruBuf);
  2851. pFruBuf = NULL;
  2852. }
  2853. static void
  2854. ipmi_fru_write_from_bin(struct ipmi_intf * intf,
  2855. char * pFileName,
  2856. uint8_t fruId)
  2857. {
  2858. struct ipmi_rs *rsp;
  2859. struct ipmi_rq req;
  2860. struct fru_info fru;
  2861. uint8_t msg_data[4];
  2862. uint8_t *pFruBuf;
  2863. uint16_t len = 0;
  2864. FILE *pFile;
  2865. msg_data[0] = fruId;
  2866. memset(&req, 0, sizeof (req));
  2867. req.msg.netfn = IPMI_NETFN_STORAGE;
  2868. req.msg.cmd = GET_FRU_INFO;
  2869. req.msg.data = msg_data;
  2870. req.msg.data_len = 1;
  2871. rsp = intf->sendrecv(intf, &req);
  2872. if (!rsp)
  2873. return;
  2874. if (rsp->ccode) {
  2875. if (rsp->ccode == 0xc3)
  2876. printf(" Timeout accessing FRU info. (Device not present?)\n");
  2877. return;
  2878. }
  2879. memset(&fru, 0, sizeof(fru));
  2880. fru.size = (rsp->data[1] << 8) | rsp->data[0];
  2881. fru.access = rsp->data[2] & 0x1;
  2882. if (verbose) {
  2883. printf("Fru Size = %d bytes\n", fru.size);
  2884. printf("Fru Access = %xh\n", fru.access);
  2885. }
  2886. pFruBuf = malloc(fru.size);
  2887. if (pFruBuf == NULL) {
  2888. lprintf(LOG_ERR, "Cannot allocate %d bytes\n", fru.size);
  2889. return;
  2890. }
  2891. pFile = fopen(pFileName, "rb");
  2892. if (pFile != NULL) {
  2893. len = fread(pFruBuf, 1, fru.size, pFile);
  2894. printf("Fru Size : %d bytes\n", fru.size);
  2895. printf("Size to Write : %d bytes\n", len);
  2896. fclose(pFile);
  2897. } else {
  2898. lprintf(LOG_ERR, "Error opening file %s\n", pFileName);
  2899. }
  2900. if (len != 0) {
  2901. write_fru_area(intf, &fru, fruId,0, 0, len, pFruBuf);
  2902. lprintf(LOG_INFO,"Done");
  2903. }
  2904. free(pFruBuf);
  2905. pFruBuf = NULL;
  2906. }
  2907. /* ipmi_fru_write_help() - print help text for 'write'
  2908. *
  2909. * retruns void
  2910. */
  2911. void
  2912. ipmi_fru_write_help()
  2913. {
  2914. lprintf(LOG_NOTICE, "fru write <fru id> <fru file>");
  2915. lprintf(LOG_NOTICE, "Note: FRU ID and file(incl. full path) must be specified.");
  2916. lprintf(LOG_NOTICE, "Example: ipmitool fru write 0 /root/fru.bin");
  2917. } /* ipmi_fru_write_help() */
  2918. /* ipmi_fru_edit_help - print help text for 'fru edit' command
  2919. *
  2920. * returns void
  2921. */
  2922. void
  2923. ipmi_fru_edit_help()
  2924. {
  2925. lprintf(LOG_NOTICE,
  2926. "fru edit <fruid> field <section> <index> <string> - edit FRU string");
  2927. lprintf(LOG_NOTICE,
  2928. "fru edit <fruid> oem iana <record> <format> <args> - limited OEM support");
  2929. } /* ipmi_fru_edit_help() */
  2930. /* ipmi_fru_edit_multirec - Query new values to replace original FRU content
  2931. *
  2932. * @intf: interface to use
  2933. * @id: FRU id to work on
  2934. *
  2935. * returns: nothing
  2936. */
  2937. /* Work in progress, copy paste most of the stuff for other functions in this
  2938. file ... not elegant yet */
  2939. static int
  2940. ipmi_fru_edit_multirec(struct ipmi_intf * intf, uint8_t id ,
  2941. int argc, char ** argv)
  2942. {
  2943. struct ipmi_rs * rsp;
  2944. struct ipmi_rq req;
  2945. struct fru_info fru;
  2946. struct fru_header header;
  2947. uint8_t msg_data[4];
  2948. uint16_t retStatus = 0;
  2949. uint32_t offFruMultiRec;
  2950. uint32_t fruMultiRecSize = 0;
  2951. struct fru_info fruInfo;
  2952. retStatus = ipmi_fru_get_multirec_location_from_fru(intf, id, &fruInfo,
  2953. &offFruMultiRec,
  2954. &fruMultiRecSize);
  2955. if (retStatus != 0) {
  2956. return retStatus;
  2957. }
  2958. lprintf(LOG_DEBUG, "FRU Size : %lu\n", fruMultiRecSize);
  2959. lprintf(LOG_DEBUG, "Multi Rec offset: %lu\n", offFruMultiRec);
  2960. {
  2961. memset(&fru, 0, sizeof(struct fru_info));
  2962. memset(&header, 0, sizeof(struct fru_header));
  2963. /*
  2964. * get info about this FRU
  2965. */
  2966. memset(msg_data, 0, 4);
  2967. msg_data[0] = id;
  2968. memset(&req, 0, sizeof(req));
  2969. req.msg.netfn = IPMI_NETFN_STORAGE;
  2970. req.msg.cmd = GET_FRU_INFO;
  2971. req.msg.data = msg_data;
  2972. req.msg.data_len = 1;
  2973. rsp = intf->sendrecv(intf, &req);
  2974. if (rsp == NULL) {
  2975. printf(" Device not present (No Response)\n");
  2976. return -1;
  2977. }
  2978. if (rsp->ccode > 0) {
  2979. printf(" Device not present (%s)\n",
  2980. val2str(rsp->ccode, completion_code_vals));
  2981. return -1;
  2982. }
  2983. memset(&fru, 0, sizeof(fru));
  2984. fru.size = (rsp->data[1] << 8) | rsp->data[0];
  2985. fru.access = rsp->data[2] & 0x1;
  2986. lprintf(LOG_DEBUG, "fru.size = %d bytes (accessed by %s)",
  2987. fru.size, fru.access ? "words" : "bytes");
  2988. if (fru.size < 1) {
  2989. lprintf(LOG_ERR, " Invalid FRU size %d", fru.size);
  2990. return -1;
  2991. }
  2992. }
  2993. {
  2994. uint8_t * fru_data;
  2995. uint32_t i;
  2996. uint32_t offset= offFruMultiRec;
  2997. struct fru_multirec_header * h;
  2998. uint32_t last_off, len;
  2999. uint8_t error=0;
  3000. i = last_off = offset;
  3001. memset(&fru, 0, sizeof(fru));
  3002. fru_data = malloc(fru.size + 1);
  3003. if (fru_data == NULL) {
  3004. lprintf(LOG_ERR, " Out of memory!");
  3005. return -1;
  3006. }
  3007. memset(fru_data, 0, fru.size + 1);
  3008. do {
  3009. h = (struct fru_multirec_header *) (fru_data + i);
  3010. /* read area in (at most) FRU_MULTIREC_CHUNK_SIZE bytes at a time */
  3011. if ((last_off < (i + sizeof(*h))) || (last_off < (i + h->len)))
  3012. {
  3013. len = fru.size - last_off;
  3014. if (len > FRU_MULTIREC_CHUNK_SIZE)
  3015. len = FRU_MULTIREC_CHUNK_SIZE;
  3016. if (read_fru_area(intf, &fru, id, last_off, len, fru_data) < 0)
  3017. break;
  3018. last_off += len;
  3019. }
  3020. if( h->type == FRU_RECORD_TYPE_OEM_EXTENSION ){
  3021. struct fru_multirec_oem_header *oh=(struct fru_multirec_oem_header *)
  3022. &fru_data[i + sizeof(struct fru_multirec_header)];
  3023. uint32_t iana = oh->mfg_id[0] | oh->mfg_id[1]<<8 | oh->mfg_id[2]<<16;
  3024. uint32_t suppliedIana = 0 ;
  3025. /* Now makes sure this is really PICMG record */
  3026. /* Default to PICMG for backward compatibility */
  3027. if( argc <=2 ) {
  3028. suppliedIana = IPMI_OEM_PICMG;
  3029. } else {
  3030. if( !strncmp( argv[2] , "oem" , 3 )) {
  3031. /* Expect IANA number next */
  3032. if( argc <= 3 ) {
  3033. lprintf(LOG_ERR, "oem iana <record> <format> [<args>]");
  3034. error = 1;
  3035. } else {
  3036. if (str2uint(argv[3], &suppliedIana) == 0) {
  3037. lprintf(LOG_DEBUG,
  3038. "using iana: %d",
  3039. suppliedIana);
  3040. } else {
  3041. lprintf(LOG_ERR,
  3042. "Given IANA '%s' is invalid.",
  3043. argv[3]);
  3044. error = 1;
  3045. }
  3046. }
  3047. }
  3048. }
  3049. if( suppliedIana == iana ) {
  3050. lprintf(LOG_DEBUG, "Matching record found" );
  3051. if( iana == IPMI_OEM_PICMG ){
  3052. if( ipmi_fru_picmg_ext_edit(fru_data,
  3053. i + sizeof(struct fru_multirec_header),
  3054. h->len, h, oh )){
  3055. /* The fru changed */
  3056. write_fru_area(intf,&fru,id, i,i,
  3057. h->len+ sizeof(struct fru_multirec_header), fru_data);
  3058. }
  3059. }
  3060. else if( iana == IPMI_OEM_KONTRON ) {
  3061. if( ipmi_fru_oemkontron_edit( argc,argv,fru_data,
  3062. i + sizeof(struct fru_multirec_header),
  3063. h->len, h, oh )){
  3064. /* The fru changed */
  3065. write_fru_area(intf,&fru,id, i,i,
  3066. h->len+ sizeof(struct fru_multirec_header), fru_data);
  3067. }
  3068. }
  3069. /* FIXME: Add OEM record support here */
  3070. else{
  3071. printf(" OEM IANA (%s) Record not support in this mode\n",
  3072. val2str( iana, ipmi_oem_info));
  3073. error = 1;
  3074. }
  3075. }
  3076. }
  3077. i += h->len + sizeof (struct fru_multirec_header);
  3078. } while (!(h->format & 0x80) && (error != 1));
  3079. free(fru_data);
  3080. fru_data = NULL;
  3081. }
  3082. return 0;
  3083. }
  3084. /* ipmi_fru_get_help - print help text for 'fru get'
  3085. *
  3086. * returns void
  3087. */
  3088. void
  3089. ipmi_fru_get_help()
  3090. {
  3091. lprintf(LOG_NOTICE,
  3092. "fru get <fruid> oem iana <record> <format> <args> - limited OEM support");
  3093. } /* ipmi_fru_get_help() */
  3094. void
  3095. ipmi_fru_internaluse_help()
  3096. {
  3097. lprintf(LOG_NOTICE,
  3098. "fru internaluse <fru id> info - get internal use area size");
  3099. lprintf(LOG_NOTICE,
  3100. "fru internaluse <fru id> print - print internal use area in hex");
  3101. lprintf(LOG_NOTICE,
  3102. "fru internaluse <fru id> read <fru file> - read internal use area to file");
  3103. lprintf(LOG_NOTICE,
  3104. "fru internaluse <fru id> write <fru file> - write internal use area from file");
  3105. } /* void ipmi_fru_internaluse_help() */
  3106. /* ipmi_fru_get_multirec - Query new values to replace original FRU content
  3107. *
  3108. * @intf: interface to use
  3109. * @id: FRU id to work on
  3110. *
  3111. * returns: nothing
  3112. */
  3113. /* Work in progress, copy paste most of the stuff for other functions in this
  3114. file ... not elegant yet */
  3115. static int
  3116. ipmi_fru_get_multirec(struct ipmi_intf * intf, uint8_t id ,
  3117. int argc, char ** argv)
  3118. {
  3119. struct ipmi_rs * rsp;
  3120. struct ipmi_rq req;
  3121. struct fru_info fru;
  3122. struct fru_header header;
  3123. uint8_t msg_data[4];
  3124. uint16_t retStatus = 0;
  3125. uint32_t offFruMultiRec;
  3126. uint32_t fruMultiRecSize = 0;
  3127. struct fru_info fruInfo;
  3128. retStatus = ipmi_fru_get_multirec_location_from_fru(intf, id, &fruInfo,
  3129. &offFruMultiRec,
  3130. &fruMultiRecSize);
  3131. if (retStatus != 0) {
  3132. return retStatus;
  3133. }
  3134. lprintf(LOG_DEBUG, "FRU Size : %lu\n", fruMultiRecSize);
  3135. lprintf(LOG_DEBUG, "Multi Rec offset: %lu\n", offFruMultiRec);
  3136. {
  3137. memset(&fru, 0, sizeof(struct fru_info));
  3138. memset(&header, 0, sizeof(struct fru_header));
  3139. /*
  3140. * get info about this FRU
  3141. */
  3142. memset(msg_data, 0, 4);
  3143. msg_data[0] = id;
  3144. memset(&req, 0, sizeof(req));
  3145. req.msg.netfn = IPMI_NETFN_STORAGE;
  3146. req.msg.cmd = GET_FRU_INFO;
  3147. req.msg.data = msg_data;
  3148. req.msg.data_len = 1;
  3149. rsp = intf->sendrecv(intf, &req);
  3150. if (rsp == NULL) {
  3151. printf(" Device not present (No Response)\n");
  3152. return -1;
  3153. }
  3154. if (rsp->ccode > 0) {
  3155. printf(" Device not present (%s)\n",
  3156. val2str(rsp->ccode, completion_code_vals));
  3157. return -1;
  3158. }
  3159. memset(&fru, 0, sizeof(fru));
  3160. fru.size = (rsp->data[1] << 8) | rsp->data[0];
  3161. fru.access = rsp->data[2] & 0x1;
  3162. lprintf(LOG_DEBUG, "fru.size = %d bytes (accessed by %s)",
  3163. fru.size, fru.access ? "words" : "bytes");
  3164. if (fru.size < 1) {
  3165. lprintf(LOG_ERR, " Invalid FRU size %d", fru.size);
  3166. return -1;
  3167. }
  3168. }
  3169. {
  3170. uint8_t * fru_data;
  3171. uint32_t i;
  3172. uint32_t offset= offFruMultiRec;
  3173. struct fru_multirec_header * h;
  3174. uint32_t last_off, len;
  3175. uint8_t error=0;
  3176. i = last_off = offset;
  3177. fru_data = malloc(fru.size + 1);
  3178. if (fru_data == NULL) {
  3179. lprintf(LOG_ERR, " Out of memory!");
  3180. return -1;
  3181. }
  3182. memset(fru_data, 0, fru.size + 1);
  3183. do {
  3184. h = (struct fru_multirec_header *) (fru_data + i);
  3185. /* read area in (at most) FRU_MULTIREC_CHUNK_SIZE bytes at a time */
  3186. if ((last_off < (i + sizeof(*h))) || (last_off < (i + h->len)))
  3187. {
  3188. len = fru.size - last_off;
  3189. if (len > FRU_MULTIREC_CHUNK_SIZE)
  3190. len = FRU_MULTIREC_CHUNK_SIZE;
  3191. if (read_fru_area(intf, &fru, id, last_off, len, fru_data) < 0)
  3192. break;
  3193. last_off += len;
  3194. }
  3195. if( h->type == FRU_RECORD_TYPE_OEM_EXTENSION ){
  3196. struct fru_multirec_oem_header *oh=(struct fru_multirec_oem_header *)
  3197. &fru_data[i + sizeof(struct fru_multirec_header)];
  3198. uint32_t iana = oh->mfg_id[0] | oh->mfg_id[1]<<8 | oh->mfg_id[2]<<16;
  3199. uint32_t suppliedIana = 0 ;
  3200. /* Now makes sure this is really PICMG record */
  3201. if( !strncmp( argv[2] , "oem" , 3 )) {
  3202. /* Expect IANA number next */
  3203. if( argc <= 3 ) {
  3204. lprintf(LOG_ERR, "oem iana <record> <format>");
  3205. error = 1;
  3206. } else {
  3207. if (str2uint(argv[3], &suppliedIana) == 0) {
  3208. lprintf(LOG_DEBUG,
  3209. "using iana: %d",
  3210. suppliedIana);
  3211. } else {
  3212. lprintf(LOG_ERR,
  3213. "Given IANA '%s' is invalid.",
  3214. argv[3]);
  3215. error = 1;
  3216. }
  3217. }
  3218. }
  3219. if( suppliedIana == iana ) {
  3220. lprintf(LOG_DEBUG, "Matching record found" );
  3221. if( iana == IPMI_OEM_KONTRON ) {
  3222. ipmi_fru_oemkontron_get( argc,argv,fru_data,
  3223. i + sizeof(struct fru_multirec_header),
  3224. h->len, h, oh );
  3225. }
  3226. /* FIXME: Add OEM record support here */
  3227. else{
  3228. printf(" OEM IANA (%s) Record not supported in this mode\n",
  3229. val2str( iana, ipmi_oem_info));
  3230. error = 1;
  3231. }
  3232. }
  3233. }
  3234. i += h->len + sizeof (struct fru_multirec_header);
  3235. } while (!(h->format & 0x80) && (error != 1));
  3236. free(fru_data);
  3237. fru_data = NULL;
  3238. }
  3239. return 0;
  3240. }
  3241. static int
  3242. ipmi_fru_upg_ekeying(struct ipmi_intf * intf,
  3243. char * pFileName,
  3244. uint8_t fruId)
  3245. {
  3246. struct fru_info fruInfo = {0};
  3247. uint8_t *buf = NULL;
  3248. uint32_t offFruMultiRec = 0;
  3249. uint32_t fruMultiRecSize = 0;
  3250. uint32_t offFileMultiRec = 0;
  3251. uint32_t fileMultiRecSize = 0;
  3252. if (pFileName == NULL) {
  3253. lprintf(LOG_ERR, "File expected, but none given.");
  3254. return (-1);
  3255. }
  3256. if (ipmi_fru_get_multirec_location_from_fru(intf, fruId, &fruInfo,
  3257. &offFruMultiRec, &fruMultiRecSize) != 0) {
  3258. lprintf(LOG_ERR, "Failed to get multirec location from FRU.");
  3259. return (-1);
  3260. }
  3261. lprintf(LOG_DEBUG, "FRU Size : %lu\n", fruMultiRecSize);
  3262. lprintf(LOG_DEBUG, "Multi Rec offset: %lu\n", offFruMultiRec);
  3263. if (ipmi_fru_get_multirec_size_from_file(pFileName, &fileMultiRecSize,
  3264. &offFileMultiRec) != 0) {
  3265. lprintf(LOG_ERR, "Failed to get multirec size from file '%s'.", pFileName);
  3266. return (-1);
  3267. }
  3268. buf = malloc(fileMultiRecSize);
  3269. if (buf == NULL) {
  3270. lprintf(LOG_ERR, "ipmitool: malloc failure");
  3271. return (-1);
  3272. }
  3273. if (ipmi_fru_get_multirec_from_file(pFileName, buf, fileMultiRecSize,
  3274. offFileMultiRec) != 0) {
  3275. lprintf(LOG_ERR, "Failed to get multirec from file '%s'.", pFileName);
  3276. if (buf != NULL) {
  3277. free(buf);
  3278. buf = NULL;
  3279. }
  3280. return (-1);
  3281. }
  3282. if (ipmi_fru_get_adjust_size_from_buffer(buf, &fileMultiRecSize) != 0) {
  3283. lprintf(LOG_ERR, "Failed to adjust size from buffer.");
  3284. if (buf != NULL) {
  3285. free(buf);
  3286. buf = NULL;
  3287. }
  3288. return (-1);
  3289. }
  3290. if (write_fru_area(intf, &fruInfo, fruId, 0, offFruMultiRec,
  3291. fileMultiRecSize, buf) != 0) {
  3292. lprintf(LOG_ERR, "Failed to write FRU area.");
  3293. if (buf != NULL) {
  3294. free(buf);
  3295. buf = NULL;
  3296. }
  3297. return (-1);
  3298. }
  3299. if (buf != NULL) {
  3300. free(buf);
  3301. buf = NULL;
  3302. }
  3303. lprintf(LOG_INFO, "Done upgrading Ekey.");
  3304. return 0;
  3305. }
  3306. /* ipmi_fru_upgekey_help - print help text for 'upgEkey'
  3307. *
  3308. * returns void
  3309. */
  3310. void
  3311. ipmi_fru_upgekey_help()
  3312. {
  3313. lprintf(LOG_NOTICE, "fru upgEkey <fru id> <fru file>");
  3314. lprintf(LOG_NOTICE, "Note: FRU ID and file(incl. full path) must be specified.");
  3315. lprintf(LOG_NOTICE, "Example: ipmitool fru upgEkey 0 /root/fru.bin");
  3316. } /* ipmi_fru_upgekey_help() */
  3317. static int
  3318. ipmi_fru_get_multirec_size_from_file(char * pFileName,
  3319. uint32_t * pSize,
  3320. uint32_t * pOffset)
  3321. {
  3322. struct fru_header header;
  3323. FILE * pFile;
  3324. uint8_t len = 0;
  3325. uint32_t end = 0;
  3326. *pSize = 0;
  3327. pFile = fopen(pFileName,"rb");
  3328. if (pFile) {
  3329. rewind(pFile);
  3330. len = fread(&header, 1, 8, pFile);
  3331. fseek(pFile, 0, SEEK_END);
  3332. end = ftell(pFile);
  3333. fclose(pFile);
  3334. }
  3335. lprintf(LOG_DEBUG, "File Size = %lu\n", end);
  3336. lprintf(LOG_DEBUG, "Len = %u\n", len);
  3337. if (len != 8) {
  3338. printf("Error with file %s in getting size\n", pFileName);
  3339. return -1;
  3340. }
  3341. if (header.version != 0x01) {
  3342. printf ("Unknown FRU header version %02x.\n", header.version);
  3343. return -1;
  3344. }
  3345. /* Retreive length */
  3346. if (((header.offset.internal * 8) > (header.offset.internal * 8)) &&
  3347. ((header.offset.internal * 8) < end))
  3348. end = (header.offset.internal * 8);
  3349. if (((header.offset.chassis * 8) > (header.offset.chassis * 8)) &&
  3350. ((header.offset.chassis * 8) < end))
  3351. end = (header.offset.chassis * 8);
  3352. if (((header.offset.board * 8) > (header.offset.board * 8)) &&
  3353. ((header.offset.board * 8) < end))
  3354. end = (header.offset.board * 8);
  3355. if (((header.offset.product * 8) > (header.offset.product * 8)) &&
  3356. ((header.offset.product * 8) < end))
  3357. end = (header.offset.product * 8);
  3358. *pSize = end - (header.offset.multi * 8);
  3359. *pOffset = (header.offset.multi * 8);
  3360. return 0;
  3361. }
  3362. int
  3363. ipmi_fru_get_adjust_size_from_buffer(uint8_t * fru_data, uint32_t *pSize)
  3364. {
  3365. struct fru_multirec_header * head;
  3366. int status = 0;
  3367. uint8_t checksum = 0;
  3368. uint8_t counter = 0;
  3369. uint16_t count = 0;
  3370. do {
  3371. checksum = 0;
  3372. head = (struct fru_multirec_header *) (fru_data + count);
  3373. if (verbose) {
  3374. printf("Adding (");
  3375. }
  3376. for (counter = 0; counter < sizeof(struct fru_multirec_header); counter++) {
  3377. if (verbose) {
  3378. printf(" %02X", *(fru_data + count + counter));
  3379. }
  3380. checksum += *(fru_data + count + counter);
  3381. }
  3382. if (verbose) {
  3383. printf(")");
  3384. }
  3385. if (checksum != 0) {
  3386. lprintf(LOG_ERR, "Bad checksum in Multi Records");
  3387. status = (-1);
  3388. if (verbose) {
  3389. printf("--> FAIL");
  3390. }
  3391. } else if (verbose) {
  3392. printf("--> OK");
  3393. }
  3394. if (verbose > 1 && checksum == 0) {
  3395. for (counter = 0; counter < head->len; counter++) {
  3396. printf(" %02X", *(fru_data + count + counter
  3397. + sizeof(struct fru_multirec_header)));
  3398. }
  3399. }
  3400. if (verbose) {
  3401. printf("\n");
  3402. }
  3403. count += head->len + sizeof (struct fru_multirec_header);
  3404. } while ((!(head->format & 0x80)) && (status == 0));
  3405. *pSize = count;
  3406. lprintf(LOG_DEBUG, "Size of multirec: %lu\n", *pSize);
  3407. return status;
  3408. }
  3409. static int
  3410. ipmi_fru_get_multirec_from_file(char * pFileName, uint8_t * pBufArea,
  3411. uint32_t size, uint32_t offset)
  3412. {
  3413. FILE * pFile;
  3414. uint32_t len = 0;
  3415. if (pFileName == NULL) {
  3416. lprintf(LOG_ERR, "Invalid file name given.");
  3417. return (-1);
  3418. }
  3419. errno = 0;
  3420. pFile = fopen(pFileName, "rb");
  3421. if (!pFile) {
  3422. lprintf(LOG_ERR, "Error opening file '%s': %i -> %s.", pFileName, errno,
  3423. strerror(errno));
  3424. return (-1);
  3425. }
  3426. errno = 0;
  3427. if (fseek(pFile, offset, SEEK_SET) != 0) {
  3428. lprintf(LOG_ERR, "Failed to seek in file '%s': %i -> %s.", pFileName, errno,
  3429. strerror(errno));
  3430. fclose(pFile);
  3431. return (-1);
  3432. }
  3433. len = fread(pBufArea, size, 1, pFile);
  3434. fclose(pFile);
  3435. if (len != 1) {
  3436. lprintf(LOG_ERR, "Error in file '%s'.", pFileName);
  3437. return (-1);
  3438. }
  3439. return 0;
  3440. }
  3441. static int
  3442. ipmi_fru_get_multirec_location_from_fru(struct ipmi_intf * intf,
  3443. uint8_t fruId,
  3444. struct fru_info *pFruInfo,
  3445. uint32_t * pRetLocation,
  3446. uint32_t * pRetSize)
  3447. {
  3448. struct ipmi_rs * rsp;
  3449. struct ipmi_rq req;
  3450. uint8_t msg_data[4];
  3451. uint32_t end;
  3452. struct fru_header header;
  3453. *pRetLocation = 0;
  3454. msg_data[0] = fruId;
  3455. memset(&req, 0, sizeof(req));
  3456. req.msg.netfn = IPMI_NETFN_STORAGE;
  3457. req.msg.cmd = GET_FRU_INFO;
  3458. req.msg.data = msg_data;
  3459. req.msg.data_len = 1;
  3460. rsp = intf->sendrecv(intf, &req);
  3461. if (!rsp) {
  3462. if (verbose > 1)
  3463. printf("no response\n");
  3464. return -1;
  3465. }
  3466. if (rsp->ccode > 0) {
  3467. if (rsp->ccode == 0xc3)
  3468. printf (" Timeout accessing FRU info. (Device not present?)\n");
  3469. else
  3470. printf (" CCODE = 0x%02x\n", rsp->ccode);
  3471. return -1;
  3472. }
  3473. pFruInfo->size = (rsp->data[1] << 8) | rsp->data[0];
  3474. pFruInfo->access = rsp->data[2] & 0x1;
  3475. if (verbose > 1)
  3476. printf("pFruInfo->size = %d bytes (accessed by %s)\n",
  3477. pFruInfo->size, pFruInfo->access ? "words" : "bytes");
  3478. if (!pFruInfo->size)
  3479. return -1;
  3480. msg_data[0] = fruId;
  3481. msg_data[1] = 0;
  3482. msg_data[2] = 0;
  3483. msg_data[3] = 8;
  3484. memset(&req, 0, sizeof(req));
  3485. req.msg.netfn = IPMI_NETFN_STORAGE;
  3486. req.msg.cmd = GET_FRU_DATA;
  3487. req.msg.data = msg_data;
  3488. req.msg.data_len = 4;
  3489. rsp = intf->sendrecv(intf, &req);
  3490. if (!rsp)
  3491. return -1;
  3492. if (rsp->ccode > 0) {
  3493. if (rsp->ccode == 0xc3)
  3494. printf (" Timeout while reading FRU data. (Device not present?)\n");
  3495. return -1;
  3496. }
  3497. if (verbose > 1)
  3498. printbuf(rsp->data, rsp->data_len, "FRU DATA");
  3499. memcpy(&header, rsp->data + 1, 8);
  3500. if (header.version != 0x01) {
  3501. printf (" Unknown FRU header version %02x.\n", header.version);
  3502. return -1;
  3503. }
  3504. end = pFruInfo->size;
  3505. /* Retreive length */
  3506. if (((header.offset.internal * 8) > (header.offset.internal * 8)) &&
  3507. ((header.offset.internal * 8) < end))
  3508. end = (header.offset.internal * 8);
  3509. if (((header.offset.chassis * 8) > (header.offset.chassis * 8)) &&
  3510. ((header.offset.chassis * 8) < end))
  3511. end = (header.offset.chassis * 8);
  3512. if (((header.offset.board * 8) > (header.offset.board * 8)) &&
  3513. ((header.offset.board * 8) < end))
  3514. end = (header.offset.board * 8);
  3515. if (((header.offset.product * 8) > (header.offset.product * 8)) &&
  3516. ((header.offset.product * 8) < end))
  3517. end = (header.offset.product * 8);
  3518. *pRetSize = end;
  3519. *pRetLocation = 8 * header.offset.multi;
  3520. return 0;
  3521. }
  3522. /* ipmi_fru_get_internal_use_offset - Retreive internal use offset
  3523. *
  3524. * @intf: ipmi interface
  3525. * @id: fru id
  3526. *
  3527. * returns -1 on error
  3528. * returns 0 if successful
  3529. * returns 1 if device not present
  3530. */
  3531. static int
  3532. ipmi_fru_get_internal_use_info( struct ipmi_intf * intf,
  3533. uint8_t id,
  3534. struct fru_info * fru,
  3535. uint16_t * size,
  3536. uint16_t * offset)
  3537. {
  3538. struct ipmi_rs * rsp;
  3539. struct ipmi_rq req;
  3540. struct fru_header header;
  3541. uint8_t msg_data[4];
  3542. // Init output value
  3543. * offset = 0;
  3544. * size = 0;
  3545. memset(fru, 0, sizeof(struct fru_info));
  3546. memset(&header, 0, sizeof(struct fru_header));
  3547. /*
  3548. * get info about this FRU
  3549. */
  3550. memset(msg_data, 0, 4);
  3551. msg_data[0] = id;
  3552. memset(&req, 0, sizeof(req));
  3553. req.msg.netfn = IPMI_NETFN_STORAGE;
  3554. req.msg.cmd = GET_FRU_INFO;
  3555. req.msg.data = msg_data;
  3556. req.msg.data_len = 1;
  3557. rsp = intf->sendrecv(intf, &req);
  3558. if (rsp == NULL) {
  3559. printf(" Device not present (No Response)\n");
  3560. return -1;
  3561. }
  3562. if (rsp->ccode > 0) {
  3563. printf(" Device not present (%s)\n",
  3564. val2str(rsp->ccode, completion_code_vals));
  3565. return -1;
  3566. }
  3567. memset(&fru, 0, sizeof(fru));
  3568. fru->size = (rsp->data[1] << 8) | rsp->data[0];
  3569. fru->access = rsp->data[2] & 0x1;
  3570. lprintf(LOG_DEBUG, "fru.size = %d bytes (accessed by %s)",
  3571. fru->size, fru->access ? "words" : "bytes");
  3572. if (fru->size < 1) {
  3573. lprintf(LOG_ERR, " Invalid FRU size %d", fru->size);
  3574. return -1;
  3575. }
  3576. /*
  3577. * retrieve the FRU header
  3578. */
  3579. msg_data[0] = id;
  3580. msg_data[1] = 0;
  3581. msg_data[2] = 0;
  3582. msg_data[3] = 8;
  3583. memset(&req, 0, sizeof(req));
  3584. req.msg.netfn = IPMI_NETFN_STORAGE;
  3585. req.msg.cmd = GET_FRU_DATA;
  3586. req.msg.data = msg_data;
  3587. req.msg.data_len = 4;
  3588. rsp = intf->sendrecv(intf, &req);
  3589. if (rsp == NULL) {
  3590. printf(" Device not present (No Response)\n");
  3591. return 1;
  3592. }
  3593. if (rsp->ccode > 0) {
  3594. printf(" Device not present (%s)\n",
  3595. val2str(rsp->ccode, completion_code_vals));
  3596. return 1;
  3597. }
  3598. if (verbose > 1)
  3599. printbuf(rsp->data, rsp->data_len, "FRU DATA");
  3600. memcpy(&header, rsp->data + 1, 8);
  3601. if (header.version != 1) {
  3602. lprintf(LOG_ERR, " Unknown FRU header version 0x%02x",
  3603. header.version);
  3604. return -1;
  3605. }
  3606. lprintf(LOG_DEBUG, "fru.header.version: 0x%x",
  3607. header.version);
  3608. lprintf(LOG_DEBUG, "fru.header.offset.internal: 0x%x",
  3609. header.offset.internal * 8);
  3610. lprintf(LOG_DEBUG, "fru.header.offset.chassis: 0x%x",
  3611. header.offset.chassis * 8);
  3612. lprintf(LOG_DEBUG, "fru.header.offset.board: 0x%x",
  3613. header.offset.board * 8);
  3614. lprintf(LOG_DEBUG, "fru.header.offset.product: 0x%x",
  3615. header.offset.product * 8);
  3616. lprintf(LOG_DEBUG, "fru.header.offset.multi: 0x%x",
  3617. header.offset.multi * 8);
  3618. if((header.offset.internal*8) == 0)
  3619. {
  3620. * size = 0;
  3621. * offset = 0;
  3622. }
  3623. else
  3624. {
  3625. (* offset) = (header.offset.internal*8);
  3626. if(header.offset.chassis != 0)
  3627. {
  3628. (* size) = ((header.offset.chassis*8)-(* offset));
  3629. }
  3630. else if(header.offset.board != 0)
  3631. {
  3632. (* size) = ((header.offset.board*8)-(* offset));
  3633. }
  3634. else if(header.offset.product != 0)
  3635. {
  3636. (* size) = ((header.offset.product*8)-(* offset));
  3637. }
  3638. else if(header.offset.multi != 0)
  3639. {
  3640. (* size) = ((header.offset.multi*8)-(* offset));
  3641. }
  3642. else
  3643. {
  3644. (* size) = (fru->size - (* offset));
  3645. }
  3646. }
  3647. return 0;
  3648. }
  3649. /* ipmi_fru_info_internal_use - print internal use info
  3650. *
  3651. * @intf: ipmi interface
  3652. * @id: fru id
  3653. *
  3654. * returns -1 on error
  3655. * returns 0 if successful
  3656. * returns 1 if device not present
  3657. */
  3658. static int
  3659. ipmi_fru_info_internal_use(struct ipmi_intf * intf, uint8_t id)
  3660. {
  3661. struct fru_info fru;
  3662. uint16_t size;
  3663. uint16_t offset;
  3664. int rc = 0;
  3665. rc = ipmi_fru_get_internal_use_info(intf, id, &fru, &size, &offset);
  3666. if(rc == 0)
  3667. {
  3668. lprintf(LOG_DEBUG, "Internal Use Area Offset: %i", offset);
  3669. printf( "Internal Use Area Size : %i\n", size);
  3670. }
  3671. else
  3672. {
  3673. lprintf(LOG_ERR, "Cannot access internal use area");
  3674. return -1;
  3675. }
  3676. return 0;
  3677. }
  3678. /* ipmi_fru_help - print help text for FRU subcommand
  3679. *
  3680. * returns void
  3681. */
  3682. void
  3683. ipmi_fru_help()
  3684. {
  3685. lprintf(LOG_NOTICE,
  3686. "FRU Commands: print read write upgEkey edit internaluse get");
  3687. } /* ipmi_fru_help() */
  3688. /* ipmi_fru_read_internal_use - print internal use are in hex or file
  3689. *
  3690. * @intf: ipmi interface
  3691. * @id: fru id
  3692. *
  3693. * returns -1 on error
  3694. * returns 0 if successful
  3695. * returns 1 if device not present
  3696. */
  3697. static int
  3698. ipmi_fru_read_internal_use(struct ipmi_intf * intf, uint8_t id, char * pFileName)
  3699. {
  3700. struct fru_info fru;
  3701. uint16_t size;
  3702. uint16_t offset;
  3703. int rc = 0;
  3704. rc = ipmi_fru_get_internal_use_info(intf, id, &fru, &size, &offset);
  3705. if(rc == 0)
  3706. {
  3707. uint8_t * frubuf;
  3708. lprintf(LOG_DEBUG, "Internal Use Area Offset: %i", offset);
  3709. printf( "Internal Use Area Size : %i\n", size);
  3710. frubuf = malloc( size );
  3711. if(frubuf)
  3712. {
  3713. rc = read_fru_area_section(intf, &fru, id, offset, size, frubuf);
  3714. if(rc == 0)
  3715. {
  3716. if(pFileName == NULL)
  3717. {
  3718. uint16_t counter;
  3719. for(counter = 0; counter < size; counter ++)
  3720. {
  3721. if((counter % 16) == 0)
  3722. printf("\n%02i- ", (counter / 16));
  3723. printf("%02X ", frubuf[counter]);
  3724. }
  3725. }
  3726. else
  3727. {
  3728. FILE * pFile;
  3729. pFile = fopen(pFileName,"wb");
  3730. if (pFile)
  3731. {
  3732. fwrite(frubuf, size, 1, pFile);
  3733. printf("Done\n");
  3734. }
  3735. else
  3736. {
  3737. lprintf(LOG_ERR, "Error opening file %s\n", pFileName);
  3738. free(frubuf);
  3739. frubuf = NULL;
  3740. return -1;
  3741. }
  3742. fclose(pFile);
  3743. }
  3744. }
  3745. printf("\n");
  3746. free(frubuf);
  3747. frubuf = NULL;
  3748. }
  3749. }
  3750. else
  3751. {
  3752. lprintf(LOG_ERR, "Cannot access internal use area");
  3753. }
  3754. return 0;
  3755. }
  3756. /* ipmi_fru_write_internal_use - print internal use are in hex or file
  3757. *
  3758. * @intf: ipmi interface
  3759. * @id: fru id
  3760. *
  3761. * returns -1 on error
  3762. * returns 0 if successful
  3763. * returns 1 if device not present
  3764. */
  3765. static int
  3766. ipmi_fru_write_internal_use(struct ipmi_intf * intf, uint8_t id, char * pFileName)
  3767. {
  3768. struct fru_info fru;
  3769. uint16_t size;
  3770. uint16_t offset;
  3771. int rc = 0;
  3772. rc = ipmi_fru_get_internal_use_info(intf, id, &fru, &size, &offset);
  3773. if(rc == 0)
  3774. {
  3775. uint8_t * frubuf;
  3776. FILE * fp;
  3777. uint32_t fileLength = 0;
  3778. lprintf(LOG_DEBUG, "Internal Use Area Offset: %i", offset);
  3779. printf( "Internal Use Area Size : %i\n", size);
  3780. fp = fopen(pFileName, "r");
  3781. if(fp)
  3782. {
  3783. /* Retreive file length, check if it's fits the Eeprom Size */
  3784. fseek(fp, 0 ,SEEK_END);
  3785. fileLength = ftell(fp);
  3786. lprintf(LOG_ERR, "File Size: %i", fileLength);
  3787. lprintf(LOG_ERR, "Area Size: %i", size);
  3788. if(fileLength != size)
  3789. {
  3790. lprintf(LOG_ERR, "File size does not fit Eeprom Size");
  3791. fclose(fp);
  3792. fp = NULL;
  3793. }
  3794. else
  3795. {
  3796. fseek(fp, 0 ,SEEK_SET);
  3797. }
  3798. }
  3799. if(fp)
  3800. {
  3801. frubuf = malloc( size );
  3802. if(frubuf)
  3803. {
  3804. uint16_t fru_read_size;
  3805. fru_read_size = fread(frubuf, 1, size, fp);
  3806. if(fru_read_size == size)
  3807. {
  3808. rc = write_fru_area(intf, &fru, id, 0, offset, size, frubuf);
  3809. if(rc == 0)
  3810. {
  3811. lprintf(LOG_INFO, "Done\n");
  3812. }
  3813. }
  3814. else
  3815. {
  3816. lprintf(LOG_ERR, "Unable to read file: %i\n", fru_read_size);
  3817. }
  3818. free(frubuf);
  3819. frubuf = NULL;
  3820. }
  3821. fclose(fp);
  3822. fp = NULL;
  3823. }
  3824. }
  3825. else
  3826. {
  3827. lprintf(LOG_ERR, "Cannot access internal use area");
  3828. }
  3829. return 0;
  3830. }
  3831. int
  3832. ipmi_fru_main(struct ipmi_intf * intf, int argc, char ** argv)
  3833. {
  3834. int rc = 0;
  3835. uint8_t fru_id = 0;
  3836. if (argc < 1) {
  3837. rc = ipmi_fru_print_all(intf);
  3838. }
  3839. else if (strncmp(argv[0], "help", 4) == 0) {
  3840. ipmi_fru_help();
  3841. return 0;
  3842. }
  3843. else if (strncmp(argv[0], "print", 5) == 0 ||
  3844. strncmp(argv[0], "list", 4) == 0) {
  3845. if (argc > 1) {
  3846. if (strcmp(argv[1], "help") == 0) {
  3847. lprintf(LOG_NOTICE, "fru print [fru id] - print information about FRU(s)");
  3848. return 0;
  3849. }
  3850. if (is_fru_id(argv[1], &fru_id) != 0)
  3851. return (-1);
  3852. rc = __ipmi_fru_print(intf, fru_id);
  3853. } else {
  3854. rc = ipmi_fru_print_all(intf);
  3855. }
  3856. }
  3857. else if (!strncmp(argv[0], "read", 5)) {
  3858. if (argc > 1 && strcmp(argv[1], "help") == 0) {
  3859. ipmi_fru_read_help();
  3860. return 0;
  3861. } else if (argc < 3) {
  3862. lprintf(LOG_ERR, "Not enough parameters given.");
  3863. ipmi_fru_read_help();
  3864. return (-1);
  3865. }
  3866. if (is_fru_id(argv[1], &fru_id) != 0)
  3867. return (-1);
  3868. /* There is a file name in the parameters */
  3869. if (is_valid_filename(argv[2]) != 0)
  3870. return (-1);
  3871. if (verbose) {
  3872. printf("FRU ID : %d\n", fru_id);
  3873. printf("FRU File : %s\n", argv[2]);
  3874. }
  3875. /* TODO - rc is missing */
  3876. ipmi_fru_read_to_bin(intf, argv[2], fru_id);
  3877. }
  3878. else if (!strncmp(argv[0], "write", 5)) {
  3879. if (argc > 1 && strcmp(argv[1], "help") == 0) {
  3880. ipmi_fru_write_help();
  3881. return 0;
  3882. } else if (argc < 3) {
  3883. lprintf(LOG_ERR, "Not enough parameters given.");
  3884. ipmi_fru_write_help();
  3885. return (-1);
  3886. }
  3887. if (is_fru_id(argv[1], &fru_id) != 0)
  3888. return (-1);
  3889. /* There is a file name in the parameters */
  3890. if (is_valid_filename(argv[2]) != 0)
  3891. return (-1);
  3892. if (verbose) {
  3893. printf("FRU ID : %d\n", fru_id);
  3894. printf("FRU File : %s\n", argv[2]);
  3895. }
  3896. /* TODO - rc is missing */
  3897. ipmi_fru_write_from_bin(intf, argv[2], fru_id);
  3898. }
  3899. else if (!strncmp(argv[0], "upgEkey", 7)) {
  3900. if (argc > 1 && strcmp(argv[1], "help") == 0) {
  3901. ipmi_fru_upgekey_help();
  3902. return 0;
  3903. } else if (argc < 3) {
  3904. lprintf(LOG_ERR, "Not enough parameters given.");
  3905. ipmi_fru_upgekey_help();
  3906. return (-1);
  3907. }
  3908. if (is_fru_id(argv[1], &fru_id) != 0)
  3909. return (-1);
  3910. /* There is a file name in the parameters */
  3911. if (is_valid_filename(argv[2]) != 0)
  3912. return (-1);
  3913. rc = ipmi_fru_upg_ekeying(intf, argv[2], fru_id);
  3914. }
  3915. else if (!strncmp(argv[0], "internaluse", 11)) {
  3916. if (argc > 1 && strcmp(argv[1], "help") == 0) {
  3917. ipmi_fru_internaluse_help();
  3918. return 0;
  3919. }
  3920. if ( (argc >= 3) && (!strncmp(argv[2], "info", 4)) ) {
  3921. if (is_fru_id(argv[1], &fru_id) != 0)
  3922. return (-1);
  3923. rc = ipmi_fru_info_internal_use(intf, fru_id);
  3924. }
  3925. else if ( (argc >= 3) && (!strncmp(argv[2], "print", 5)) ) {
  3926. if (is_fru_id(argv[1], &fru_id) != 0)
  3927. return (-1);
  3928. rc = ipmi_fru_read_internal_use(intf, fru_id, NULL);
  3929. }
  3930. else if ( (argc >= 4) && (!strncmp(argv[2], "read", 4)) ) {
  3931. if (is_fru_id(argv[1], &fru_id) != 0)
  3932. return (-1);
  3933. /* There is a file name in the parameters */
  3934. if (is_valid_filename(argv[3]) != 0)
  3935. return (-1);
  3936. lprintf(LOG_DEBUG, "FRU ID : %d", fru_id);
  3937. lprintf(LOG_DEBUG, "FRU File : %s", argv[3]);
  3938. rc = ipmi_fru_read_internal_use(intf, fru_id, argv[3]);
  3939. }
  3940. else if ( (argc >= 4) && (!strncmp(argv[2], "write", 5)) ) {
  3941. if (is_fru_id(argv[1], &fru_id) != 0)
  3942. return (-1);
  3943. /* There is a file name in the parameters */
  3944. if (is_valid_filename(argv[3]) != 0)
  3945. return (-1);
  3946. lprintf(LOG_DEBUG, "FRU ID : %d", fru_id);
  3947. lprintf(LOG_DEBUG, "FRU File : %s", argv[3]);
  3948. rc = ipmi_fru_write_internal_use(intf, fru_id, argv[3]);
  3949. } else {
  3950. lprintf(LOG_ERR,
  3951. "Either unknown command or not enough parameters given.");
  3952. ipmi_fru_internaluse_help();
  3953. return (-1);
  3954. }
  3955. }
  3956. else if (!strncmp(argv[0], "edit", 4)) {
  3957. if (argc > 1 && strcmp(argv[1], "help") == 0) {
  3958. ipmi_fru_edit_help();
  3959. return 0;
  3960. } else if (argc < 2) {
  3961. lprintf(LOG_ERR, "Not enough parameters given.");
  3962. ipmi_fru_edit_help();
  3963. return (-1);
  3964. }
  3965. if (argc >= 2) {
  3966. if (is_fru_id(argv[1], &fru_id) != 0)
  3967. return (-1);
  3968. if (verbose) {
  3969. printf("FRU ID : %d\n", fru_id);
  3970. }
  3971. } else {
  3972. printf("Using default FRU ID: %d\n", fru_id);
  3973. }
  3974. if (argc >= 3) {
  3975. if (!strncmp(argv[2], "field", 5)) {
  3976. if (argc != 6) {
  3977. lprintf(LOG_ERR, "Not enough parameters given.");
  3978. ipmi_fru_edit_help();
  3979. return (-1);
  3980. }
  3981. rc = ipmi_fru_set_field_string(intf, fru_id, *argv[3], *argv[4],
  3982. (char *) argv[5]);
  3983. } else if (!strncmp(argv[2], "oem", 3)) {
  3984. rc = ipmi_fru_edit_multirec(intf, fru_id, argc, argv);
  3985. } else {
  3986. lprintf(LOG_ERR, "Invalid command: %s", argv[2]);
  3987. ipmi_fru_edit_help();
  3988. return (-1);
  3989. }
  3990. } else {
  3991. rc = ipmi_fru_edit_multirec(intf, fru_id, argc, argv);
  3992. }
  3993. }
  3994. else if (!strncmp(argv[0], "get", 4)) {
  3995. if (argc > 1 && (strncmp(argv[1], "help", 4) == 0)) {
  3996. ipmi_fru_get_help();
  3997. return 0;
  3998. } else if (argc < 2) {
  3999. lprintf(LOG_ERR, "Not enough parameters given.");
  4000. ipmi_fru_get_help();
  4001. return (-1);
  4002. }
  4003. if (argc >= 2) {
  4004. if (is_fru_id(argv[1], &fru_id) != 0)
  4005. return (-1);
  4006. if (verbose) {
  4007. printf("FRU ID : %d\n", fru_id);
  4008. }
  4009. } else {
  4010. printf("Using default FRU ID: %d\n", fru_id);
  4011. }
  4012. if (argc >= 3) {
  4013. if (!strncmp(argv[2], "oem", 3)) {
  4014. rc = ipmi_fru_get_multirec(intf, fru_id, argc, argv);
  4015. } else {
  4016. lprintf(LOG_ERR, "Invalid command: %s", argv[2]);
  4017. ipmi_fru_get_help();
  4018. return (-1);
  4019. }
  4020. } else {
  4021. rc = ipmi_fru_get_multirec(intf, fru_id, argc, argv);
  4022. }
  4023. }
  4024. else {
  4025. lprintf(LOG_ERR, "Invalid FRU command: %s", argv[0]);
  4026. ipmi_fru_help();
  4027. return (-1);
  4028. }
  4029. return rc;
  4030. }
  4031. /* ipmi_fru_set_field_string - Set a field string to a new value, Need to be the same size. If
  4032. * size if not equal, the function ipmi_fru_set_field_string_rebuild
  4033. * will be called.
  4034. *
  4035. * @intf: ipmi interface
  4036. * @id: fru id
  4037. * @f_type: Type of the Field : c=Chassis b=Board p=Product
  4038. * @f_index: findex of the field, zero indexed.
  4039. * @f_string: NULL terminated string
  4040. *
  4041. * returns -1 on error
  4042. * returns 1 if successful
  4043. */
  4044. static int
  4045. ipmi_fru_set_field_string(struct ipmi_intf * intf, uint8_t fruId, uint8_t
  4046. f_type, uint8_t f_index, char *f_string)
  4047. {
  4048. struct ipmi_rs *rsp;
  4049. struct ipmi_rq req;
  4050. struct fru_info fru;
  4051. struct fru_header header;
  4052. uint8_t msg_data[4];
  4053. uint8_t checksum;
  4054. int i = 0;
  4055. int rc = 1;
  4056. uint8_t *fru_data = NULL;
  4057. uint8_t *fru_area = NULL;
  4058. uint32_t fru_field_offset, fru_field_offset_tmp;
  4059. uint32_t fru_section_len, header_offset;
  4060. memset(msg_data, 0, 4);
  4061. msg_data[0] = fruId;
  4062. memset(&req, 0, sizeof(req));
  4063. req.msg.netfn = IPMI_NETFN_STORAGE;
  4064. req.msg.cmd = GET_FRU_INFO;
  4065. req.msg.data = msg_data;
  4066. req.msg.data_len = 1;
  4067. rsp = intf->sendrecv(intf, &req);
  4068. if (rsp == NULL) {
  4069. printf(" Device not present (No Response)\n");
  4070. rc = (-1);
  4071. goto ipmi_fru_set_field_string_out;
  4072. }
  4073. if (rsp->ccode > 0) {
  4074. printf(" Device not present (%s)\n",
  4075. val2str(rsp->ccode, completion_code_vals));
  4076. rc = (-1);
  4077. goto ipmi_fru_set_field_string_out;
  4078. }
  4079. memset(&fru, 0, sizeof(fru));
  4080. fru.size = (rsp->data[1] << 8) | rsp->data[0];
  4081. fru.access = rsp->data[2] & 0x1;
  4082. if (fru.size < 1) {
  4083. printf(" Invalid FRU size %d", fru.size);
  4084. rc = (-1);
  4085. goto ipmi_fru_set_field_string_out;
  4086. }
  4087. /*
  4088. * retrieve the FRU header
  4089. */
  4090. msg_data[0] = fruId;
  4091. msg_data[1] = 0;
  4092. msg_data[2] = 0;
  4093. msg_data[3] = 8;
  4094. memset(&req, 0, sizeof(req));
  4095. req.msg.netfn = IPMI_NETFN_STORAGE;
  4096. req.msg.cmd = GET_FRU_DATA;
  4097. req.msg.data = msg_data;
  4098. req.msg.data_len = 4;
  4099. rsp = intf->sendrecv(intf, &req);
  4100. if (rsp == NULL)
  4101. {
  4102. printf(" Device not present (No Response)\n");
  4103. rc = (-1);
  4104. goto ipmi_fru_set_field_string_out;
  4105. }
  4106. if (rsp->ccode > 0)
  4107. {
  4108. printf(" Device not present (%s)\n",
  4109. val2str(rsp->ccode, completion_code_vals));
  4110. rc = (-1);
  4111. goto ipmi_fru_set_field_string_out;
  4112. }
  4113. if (verbose > 1)
  4114. printbuf(rsp->data, rsp->data_len, "FRU DATA");
  4115. memcpy(&header, rsp->data + 1, 8);
  4116. if (header.version != 1)
  4117. {
  4118. printf(" Unknown FRU header version 0x%02x",
  4119. header.version);
  4120. rc = (-1);
  4121. goto ipmi_fru_set_field_string_out;
  4122. }
  4123. fru_data = malloc( fru.size );
  4124. if( fru_data == NULL )
  4125. {
  4126. printf("Out of memory!\n");
  4127. rc = (-1);
  4128. goto ipmi_fru_set_field_string_out;
  4129. }
  4130. /* Setup offset from the field type */
  4131. /* Chassis type field */
  4132. if (f_type == 'c' ) {
  4133. header_offset = (header.offset.chassis * 8);
  4134. read_fru_area(intf ,&fru, fruId, header_offset , 3 , fru_data);
  4135. fru_field_offset = 3;
  4136. fru_section_len = *(fru_data + 1) * 8;
  4137. }
  4138. /* Board type field */
  4139. else if (f_type == 'b' ) {
  4140. header_offset = (header.offset.board * 8);
  4141. read_fru_area(intf ,&fru, fruId, header_offset , 3 , fru_data);
  4142. fru_field_offset = 6;
  4143. fru_section_len = *(fru_data + 1) * 8;
  4144. }
  4145. /* Product type field */
  4146. else if (f_type == 'p' ) {
  4147. header_offset = (header.offset.product * 8);
  4148. read_fru_area(intf ,&fru, fruId, header_offset , 3 , fru_data);
  4149. fru_field_offset = 3;
  4150. fru_section_len = *(fru_data + 1) * 8;
  4151. }
  4152. else
  4153. {
  4154. printf("Wrong field type.");
  4155. rc = (-1);
  4156. goto ipmi_fru_set_field_string_out;
  4157. }
  4158. memset(fru_data, 0, fru.size);
  4159. if( read_fru_area(intf ,&fru, fruId, header_offset ,
  4160. fru_section_len , fru_data) < 0 )
  4161. {
  4162. rc = (-1);
  4163. goto ipmi_fru_set_field_string_out;
  4164. }
  4165. /* Convert index from character to decimal */
  4166. f_index= f_index - 0x30;
  4167. /*Seek to field index */
  4168. for (i=0; i <= f_index; i++) {
  4169. fru_field_offset_tmp = fru_field_offset;
  4170. if (fru_area != NULL) {
  4171. free(fru_area);
  4172. fru_area = NULL;
  4173. }
  4174. fru_area = (uint8_t *) get_fru_area_str(fru_data, &fru_field_offset);
  4175. }
  4176. if( (fru_area == NULL ) || strlen((const char *)fru_area) == 0 ) {
  4177. printf("Field not found !\n");
  4178. rc = (-1);
  4179. goto ipmi_fru_set_field_string_out;
  4180. }
  4181. if ( strlen((const char *)fru_area) == strlen((const char *)f_string) )
  4182. {
  4183. printf("Updating Field '%s' with '%s' ...\n", fru_area, f_string );
  4184. memcpy(fru_data + fru_field_offset_tmp + 1,
  4185. f_string, strlen(f_string));
  4186. checksum = 0;
  4187. /* Calculate Header Checksum */
  4188. for (i = 0; i < fru_section_len - 1; i++)
  4189. {
  4190. checksum += fru_data[i];
  4191. }
  4192. checksum = (~checksum) + 1;
  4193. fru_data[fru_section_len - 1] = checksum;
  4194. /* Write the updated section to the FRU data; source offset => 0 */
  4195. if( write_fru_area(intf, &fru, fruId, 0,
  4196. header_offset, fru_section_len, fru_data) < 0 )
  4197. {
  4198. printf("Write to FRU data failed.\n");
  4199. rc = (-1);
  4200. goto ipmi_fru_set_field_string_out;
  4201. }
  4202. }
  4203. else {
  4204. printf("String size are not equal, resizing fru to fit new string\n");
  4205. if(
  4206. ipmi_fru_set_field_string_rebuild(intf,fruId,fru,header,f_type,f_index,f_string)
  4207. )
  4208. {
  4209. rc = (-1);
  4210. goto ipmi_fru_set_field_string_out;
  4211. }
  4212. }
  4213. ipmi_fru_set_field_string_out:
  4214. if (fru_data != NULL) {
  4215. free(fru_data);
  4216. fru_data = NULL;
  4217. }
  4218. if (fru_area != NULL) {
  4219. free(fru_area);
  4220. fru_area = NULL;
  4221. }
  4222. return rc;
  4223. }
  4224. /*
  4225. This function can update a string within of the following section when the size is not equal:
  4226. Chassis
  4227. Product
  4228. Board
  4229. */
  4230. /* ipmi_fru_set_field_string_rebuild - Set a field string to a new value, When size are not
  4231. * the same size.
  4232. *
  4233. * This function can update a string within of the following section when the size is not equal:
  4234. *
  4235. * - Chassis
  4236. * - Product
  4237. * - Board
  4238. *
  4239. * @intf: ipmi interface
  4240. * @fruId: fru id
  4241. * @fru: info about fru
  4242. * @header: contain the header of the FRU
  4243. * @f_type: Type of the Field : c=Chassis b=Board p=Product
  4244. * @f_index: findex of the field, zero indexed.
  4245. * @f_string: NULL terminated string
  4246. *
  4247. * returns -1 on error
  4248. * returns 1 if successful
  4249. */
  4250. #define DBG_RESIZE_FRU
  4251. static int
  4252. ipmi_fru_set_field_string_rebuild(struct ipmi_intf * intf, uint8_t fruId,
  4253. struct fru_info fru, struct fru_header header,
  4254. uint8_t f_type, uint8_t f_index, char *f_string)
  4255. {
  4256. int i = 0;
  4257. uint8_t *fru_data_old = NULL;
  4258. uint8_t *fru_data_new = NULL;
  4259. uint8_t *fru_area = NULL;
  4260. uint32_t fru_field_offset, fru_field_offset_tmp;
  4261. uint32_t fru_section_len, header_offset;
  4262. uint32_t chassis_offset, board_offset, product_offset;
  4263. uint32_t chassis_len, board_len, product_len, product_len_new;
  4264. int num_byte_change = 0, padding_len = 0;
  4265. uint32_t counter;
  4266. unsigned char cksum;
  4267. int rc = 1;
  4268. fru_data_old = calloc( fru.size, sizeof(uint8_t) );
  4269. fru_data_new = malloc( fru.size );
  4270. if( fru_data_old == NULL || fru_data_new == NULL )
  4271. {
  4272. printf("Out of memory!\n");
  4273. rc = (-1);
  4274. goto ipmi_fru_set_field_string_rebuild_out;
  4275. }
  4276. /*************************
  4277. 1) Read ALL FRU */
  4278. printf("Read All FRU area\n");
  4279. printf("Fru Size : %u bytes\n", fru.size);
  4280. /* Read current fru data */
  4281. read_fru_area(intf ,&fru, fruId, 0, fru.size , fru_data_old);
  4282. #ifdef DBG_RESIZE_FRU
  4283. printf("Copy to new FRU\n");
  4284. #endif
  4285. /*************************
  4286. 2) Copy all FRU to new FRU */
  4287. memcpy(fru_data_new, fru_data_old, fru.size);
  4288. /* Build location of all modifiable components */
  4289. chassis_offset = (header.offset.chassis * 8);
  4290. board_offset = (header.offset.board * 8);
  4291. product_offset = (header.offset.product * 8);
  4292. /* Retrieve length of all modifiable components */
  4293. chassis_len = *(fru_data_old + chassis_offset + 1) * 8;
  4294. board_len = *(fru_data_old + board_offset + 1) * 8;
  4295. product_len = *(fru_data_old + product_offset + 1) * 8;
  4296. product_len_new = product_len;
  4297. /* Chassis type field */
  4298. if (f_type == 'c' )
  4299. {
  4300. header_offset = chassis_offset;
  4301. fru_field_offset = chassis_offset + 3;
  4302. fru_section_len = chassis_len;
  4303. }
  4304. /* Board type field */
  4305. else if (f_type == 'b' )
  4306. {
  4307. header_offset = board_offset;
  4308. fru_field_offset = board_offset + 6;
  4309. fru_section_len = board_len;
  4310. }
  4311. /* Product type field */
  4312. else if (f_type == 'p' )
  4313. {
  4314. header_offset = product_offset;
  4315. fru_field_offset = product_offset + 3;
  4316. fru_section_len = product_len;
  4317. }
  4318. else
  4319. {
  4320. printf("Wrong field type.");
  4321. rc = (-1);
  4322. goto ipmi_fru_set_field_string_rebuild_out;
  4323. }
  4324. /*************************
  4325. 3) Seek to field index */
  4326. for (i = 0;i <= f_index; i++) {
  4327. fru_field_offset_tmp = fru_field_offset;
  4328. if (fru_area != NULL) {
  4329. free(fru_area);
  4330. fru_area = NULL;
  4331. }
  4332. fru_area = (uint8_t *) get_fru_area_str(fru_data_old, &fru_field_offset);
  4333. }
  4334. if( (fru_area == NULL ) || strlen((const char *)fru_area) == 0 ) {
  4335. printf("Field not found (1)!\n");
  4336. rc = (-1);
  4337. goto ipmi_fru_set_field_string_rebuild_out;
  4338. }
  4339. #ifdef DBG_RESIZE_FRU
  4340. printf("Section Length: %u\n", fru_section_len);
  4341. #endif
  4342. /*************************
  4343. 4) Check number of padding bytes and bytes changed */
  4344. for(counter = 2; counter < fru_section_len; counter ++)
  4345. {
  4346. if(*(fru_data_old + (header_offset + fru_section_len - counter)) == 0)
  4347. padding_len ++;
  4348. else
  4349. break;
  4350. }
  4351. num_byte_change = strlen(f_string) - strlen(fru_area);
  4352. #ifdef DBG_RESIZE_FRU
  4353. printf("Padding Length: %u\n", padding_len);
  4354. printf("NumByte Change: %i\n", num_byte_change);
  4355. printf("Start SecChnge: %x\n", *(fru_data_old + fru_field_offset_tmp));
  4356. printf("End SecChnge : %x\n", *(fru_data_old + fru_field_offset_tmp + strlen(f_string) + 1));
  4357. printf("Start Section : %x\n", *(fru_data_old + header_offset));
  4358. printf("End Sec wo Pad: %x\n", *(fru_data_old + header_offset + fru_section_len - 2 - padding_len));
  4359. printf("End Section : %x\n", *(fru_data_old + header_offset + fru_section_len - 1));
  4360. #endif
  4361. /* Calculate New Padding Length */
  4362. padding_len -= num_byte_change;
  4363. #ifdef DBG_RESIZE_FRU
  4364. printf("New Padding Length: %i\n", padding_len);
  4365. #endif
  4366. /*************************
  4367. 5) Check if section must be resize. This occur when padding length is not between 0 and 7 */
  4368. if( (padding_len < 0) || (padding_len >= 8))
  4369. {
  4370. uint32_t remaining_offset = ((header.offset.product * 8) + product_len);
  4371. int change_size_by_8;
  4372. if(padding_len >= 8)
  4373. {
  4374. /* Section must be set smaller */
  4375. change_size_by_8 = ((padding_len) / 8) * (-1);
  4376. }
  4377. else
  4378. {
  4379. /* Section must be set bigger */
  4380. change_size_by_8 = 1 + (((padding_len+1) / 8) * (-1));
  4381. }
  4382. /* Recalculate padding and section length base on the section changes */
  4383. fru_section_len += (change_size_by_8 * 8);
  4384. padding_len += (change_size_by_8 * 8);
  4385. #ifdef DBG_RESIZE_FRU
  4386. printf("change_size_by_8: %i\n", change_size_by_8);
  4387. printf("New Padding Length: %i\n", padding_len);
  4388. printf("change_size_by_8: %i\n", change_size_by_8);
  4389. printf("header.offset.board: %i\n", header.offset.board);
  4390. #endif
  4391. /* Must move sections */
  4392. /* Section that can be modified are as follow
  4393. Chassis
  4394. Board
  4395. product */
  4396. /* Chassis type field */
  4397. if (f_type == 'c' )
  4398. {
  4399. printf("Moving Section Chassis, from %i to %i\n",
  4400. ((header.offset.board) * 8),
  4401. ((header.offset.board + change_size_by_8) * 8)
  4402. );
  4403. memcpy(
  4404. (fru_data_new + ((header.offset.board + change_size_by_8) * 8)),
  4405. (fru_data_old + (header.offset.board) * 8),
  4406. board_len
  4407. );
  4408. header.offset.board += change_size_by_8;
  4409. }
  4410. /* Board type field */
  4411. if ((f_type == 'c' ) || (f_type == 'b' ))
  4412. {
  4413. printf("Moving Section Product, from %i to %i\n",
  4414. ((header.offset.product) * 8),
  4415. ((header.offset.product + change_size_by_8) * 8)
  4416. );
  4417. memcpy(
  4418. (fru_data_new + ((header.offset.product + change_size_by_8) * 8)),
  4419. (fru_data_old + (header.offset.product) * 8),
  4420. product_len
  4421. );
  4422. header.offset.product += change_size_by_8;
  4423. }
  4424. if ((f_type == 'c' ) || (f_type == 'b' ) || (f_type == 'p' )) {
  4425. printf("Change multi offset from %d to %d\n", header.offset.multi, header.offset.multi + change_size_by_8);
  4426. header.offset.multi += change_size_by_8;
  4427. }
  4428. /* Adjust length of the section */
  4429. if (f_type == 'c')
  4430. {
  4431. *(fru_data_new + chassis_offset + 1) += change_size_by_8;
  4432. }
  4433. else if( f_type == 'b')
  4434. {
  4435. *(fru_data_new + board_offset + 1) += change_size_by_8;
  4436. }
  4437. else if( f_type == 'p')
  4438. {
  4439. *(fru_data_new + product_offset + 1) += change_size_by_8;
  4440. product_len_new = *(fru_data_new + product_offset + 1) * 8;
  4441. }
  4442. /* Rebuild Header checksum */
  4443. {
  4444. unsigned char * pfru_header = (unsigned char *) &header;
  4445. header.checksum = 0;
  4446. for(counter = 0; counter < (sizeof(struct fru_header) -1); counter ++)
  4447. {
  4448. header.checksum += pfru_header[counter];
  4449. }
  4450. header.checksum = (0 - header.checksum);
  4451. memcpy(fru_data_new, pfru_header, sizeof(struct fru_header));
  4452. }
  4453. /* Move remaining sections in 1 copy */
  4454. printf("Moving Remaining Bytes (Multi-Rec , etc..), from %i to %i\n",
  4455. remaining_offset,
  4456. ((header.offset.product) * 8) + product_len_new
  4457. );
  4458. if(((header.offset.product * 8) + product_len_new - remaining_offset) < 0)
  4459. {
  4460. memcpy(
  4461. fru_data_new + (header.offset.product * 8) + product_len_new,
  4462. fru_data_old + remaining_offset,
  4463. fru.size - remaining_offset
  4464. );
  4465. }
  4466. else
  4467. {
  4468. memcpy(
  4469. fru_data_new + (header.offset.product * 8) + product_len_new,
  4470. fru_data_old + remaining_offset,
  4471. fru.size - ((header.offset.product * 8) + product_len_new)
  4472. );
  4473. }
  4474. }
  4475. /* Update only if it's fits padding length as defined in the spec, otherwise, it's an internal
  4476. error */
  4477. /*************************
  4478. 6) Update Field and sections */
  4479. if( (padding_len >=0) && (padding_len < 8))
  4480. {
  4481. /* Do not requires any change in other section */
  4482. /* Change field length */
  4483. printf(
  4484. "Updating Field : '%s' with '%s' ... (Length from '%d' to '%d')\n",
  4485. fru_area, f_string,
  4486. (int)*(fru_data_old + fru_field_offset_tmp),
  4487. (int)(0xc0 + strlen(f_string)));
  4488. *(fru_data_new + fru_field_offset_tmp) = (0xc0 + strlen(f_string));
  4489. memcpy(fru_data_new + fru_field_offset_tmp + 1, f_string, strlen(f_string));
  4490. /* Copy remaing bytes in section */
  4491. #ifdef DBG_RESIZE_FRU
  4492. printf("Copying remaining of sections: %d \n",
  4493. (int)((fru_data_old + header_offset + fru_section_len - 1) -
  4494. (fru_data_old + fru_field_offset_tmp + strlen(f_string) + 1)));
  4495. #endif
  4496. memcpy((fru_data_new + fru_field_offset_tmp + 1 +
  4497. strlen(f_string)),
  4498. (fru_data_old + fru_field_offset_tmp + 1 +
  4499. strlen(fru_area)),
  4500. ((fru_data_old + header_offset + fru_section_len - 1) -
  4501. (fru_data_old + fru_field_offset_tmp + strlen(f_string) + 1)));
  4502. /* Add Padding if required */
  4503. for(counter = 0; counter < padding_len; counter ++)
  4504. {
  4505. *(fru_data_new + header_offset + fru_section_len - 1 -
  4506. padding_len + counter) = 0;
  4507. }
  4508. /* Calculate New Checksum */
  4509. cksum = 0;
  4510. for( counter = 0; counter <fru_section_len-1; counter ++ )
  4511. {
  4512. cksum += *(fru_data_new + header_offset + counter);
  4513. }
  4514. *(fru_data_new + header_offset + fru_section_len - 1) = (0 - cksum);
  4515. #ifdef DBG_RESIZE_FRU
  4516. printf("Calculate New Checksum: %x\n", (0 - cksum));
  4517. #endif
  4518. }
  4519. else
  4520. {
  4521. printf( "Internal error, padding length %i (must be from 0 to 7) ", padding_len );
  4522. rc = (-1);
  4523. goto ipmi_fru_set_field_string_rebuild_out;
  4524. }
  4525. /*************************
  4526. 7) Finally, write new FRU */
  4527. printf("Writing new FRU.\n");
  4528. if( write_fru_area( intf, &fru, fruId, 0, 0, fru.size, fru_data_new ) < 0 )
  4529. {
  4530. printf("Write to FRU data failed.\n");
  4531. rc = (-1);
  4532. goto ipmi_fru_set_field_string_rebuild_out;
  4533. }
  4534. printf("Done.\n");
  4535. ipmi_fru_set_field_string_rebuild_out:
  4536. if (fru_area != NULL) {
  4537. free(fru_area);
  4538. fru_area = NULL;
  4539. }
  4540. if (fru_data_new != NULL) {
  4541. free(fru_data_new);
  4542. fru_data_new = NULL;
  4543. }
  4544. if (fru_data_old != NULL) {
  4545. free(fru_data_old);
  4546. fru_data_old = NULL;
  4547. }
  4548. return rc;
  4549. }