AMIDevice.c 211 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151
  1. /*****************************************************************
  2. *****************************************************************
  3. ** **
  4. ** (C)Copyright 2005-2006, American Megatrends Inc. **
  5. ** **
  6. ** All Rights Reserved. **
  7. ** **
  8. ** 6145-F, Northbelt Parkway, Norcross, **
  9. ** **
  10. ** Georgia - 30071, USA. Phone-(770)-246-8600. **
  11. ** **
  12. *****************************************************************
  13. *****************************************************************
  14. ******************************************************************
  15. ******************************************************************
  16. *
  17. * AMIDevice.c
  18. *
  19. * Author: Basavaraj Astekar <basavaraja@ami.com>
  20. *
  21. ******************************************************************/
  22. #define ENABLE_DEBUG_MACROS 0
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <linux/kernel.h>
  26. #include <sys/sysinfo.h>
  27. #include <dlfcn.h>
  28. #include <openssl/pem.h>
  29. #include "Types.h"
  30. //#include "OSPort.h"
  31. #include "Support.h"
  32. #include "IPMIDefs.h"
  33. #include "IPMI_AppDevice.h"
  34. #include "IPMI_AMIDevice.h"
  35. #include "IPMI_AMI.h"
  36. #include "AMIDevice.h"
  37. #include "SharedMem.h"
  38. #include "dbgout.h"
  39. #include "ipmi_userifc.h"
  40. //#include "sshutil.h"
  41. #include "Platform.h"
  42. #include "Indicators.h"
  43. #include "Debug.h"
  44. #include "flashlib.h"
  45. #include "NVRAccess.h"
  46. #include "MsgHndlr.h"
  47. #include "PMConfig.h"
  48. #include "YafuStatus.h"
  49. #include "Sensor.h"
  50. #include <unistd.h>
  51. #include <sys/reboot.h>
  52. #include <linux/reboot.h>
  53. #include "PDKCmds.h"
  54. //#include "IPMIConf.h"
  55. #include "PDKAccess.h"
  56. #include "Ethaddr.h"
  57. #include "PDKCmdsAccess.h"
  58. #include "flshfiles.h"
  59. #include "PendTask.h"
  60. //#include "IPMI_AMIConf.h"
  61. #include "featuredef.h"
  62. #include<sys/prctl.h>
  63. #include "ubenv.h"
  64. #include "fwinfo.h"
  65. #include "blowfish.h"
  66. #include "ldapconf.h"
  67. #include "activedir_cfg.h"
  68. #include "ubenv.h"
  69. #include "safesystem.h"
  70. #include "usb_ioctl.h"
  71. #include "vmedia_instance.h"
  72. #include "ncml.h"
  73. #include <sys/stat.h>
  74. #include <string.h>
  75. #include "IPMI_AMILicense.h"
  76. #include "WS_base32.h"
  77. #include "bmc_type.h"
  78. //static _FAR_ SensorSharedMem_T* pSenSharedMem;
  79. /* For BIOS flashing
  80. * see firmware/apps/gpio_utilities
  81. * included by way of libami/flashlib
  82. */
  83. extern int assert_host_reset();
  84. extern int write_one_bios_block( int blocknum, unsigned char *data );
  85. extern int read_one_bios_block( int blocknum, unsigned char *data );
  86. extern int deassert_host_reset();
  87. extern unsigned long CalculateChksum (char *data, unsigned long size);
  88. void* EraseCopyFlash (void*);
  89. void* VerifyFlashStatus (void*);
  90. void* YAFUTimerTask(void *pArg);
  91. int YAFUTimerAction();
  92. static int CheckForBootOption(char bootoption);
  93. static bool IsfwuploadSelectorValid(char uploadselector);
  94. /* For Firmware update
  95. */
  96. #define INVALID_IP_ADDR "0.0.0.0"
  97. #define NO_ADDRESS 0xFFFFFFFF
  98. #define FLASH_TIMEOUT_IN_SECONDS 1200
  99. #define SET_FIRM_REQ_BYTES 200
  100. /* U-Boot Memory Test */
  101. #define UBOOT_MEMTEST_ENABLE_VAR "do_memtest"
  102. #define UBOOT_MEMTEST_STATUS_VAR "memtest_pass"
  103. #define MEMTEST_PASS "yes"
  104. #define MEMTEST_FAIL "no"
  105. #define MEMTEST_ENABLE "1"
  106. FlashTimerInfo_T g_FlashTimerInfo;
  107. #define MEMGETINFO _IOR('M', 1, struct mtd_info_user)
  108. #define MEMERASE _IOW('M', 2, struct erase_info_user)
  109. #define MEMLOCK _IOW('M', 5, struct erase_info_user)
  110. #define MEMUNLOCK _IOW('M', 6, struct erase_info_user)
  111. #define MAX_CMDLINE 50
  112. #define MAX_MODULE 16
  113. /* YafuTimer Related definitions*/
  114. #define UBOOT_MAX_SIZE 65536
  115. /* Timer timeouts for diff interfaces and flasing types(Interactive and ForceFlash)*/
  116. #define YAFU_LAN_IA_TIMER_COUNT 600
  117. #define YAFU_LAN_FF_TIMER_COUNT 600
  118. #define YAFU_USB_IA_TIMER_COUNT 180
  119. #define YAFU_USB_FF_TIMER_COUNT 720
  120. #define YAFU_KCS_IA_TIMER_COUNT 3600
  121. #define YAFU_KCS_FF_TIMER_COUNT 3600
  122. #define YAFU_MSECS_PER_TICK 1000
  123. #define YAFU_FLASH_SUCCEEDED 1
  124. #define YAFU_FLASH_CHUNK 1024
  125. /* Yafu Flashing Types Interactive/Generic*/
  126. #define YAFU_INTERACTIVE_FLASH 1
  127. #ifndef CONFIG_SPX_FEATURE_IPMI_NO_YAFU_SUPPORT
  128. #define DEFAULT_MTD_DEVICE "/dev/mtd0"
  129. #define SIGNED_HASH_SIZE 128
  130. BOOL FMHComp;
  131. BOOL ActivateFlashStatus =0x00;
  132. int LastStatCode = 0x00;
  133. int VerifyFlashStatusCode = 0x00;
  134. INT8U *allocmem;
  135. int DualImgPreserveConf=TRUE;
  136. INT8U *WriteMemOff=NULL;
  137. unsigned long Sizetocpy=0;
  138. unsigned long Flashoffset;
  139. unsigned long Fixoffset;
  140. INT8U *VWriteMemOff=NULL;
  141. INT32U ImgSize = 0;
  142. unsigned long Sizetoverify;
  143. unsigned long VFlashoffset;
  144. unsigned long VOffset;
  145. unsigned long VFixoffset;
  146. int gDeviceNode;
  147. /* Yafu Timer relating variables*/
  148. static env_t environment;
  149. unsigned long YafuTimerCnt = 0;
  150. char UBootCfg[UBOOT_MAX_SIZE];
  151. int YafuflashStatus = 0;
  152. int SetBootParam = 0;
  153. extern unsigned long FlashStart, EnvStart, EnvSize;
  154. int YafuFlashMode=0;
  155. int Vstart=0;
  156. int Vthreadstate=0;
  157. int ECFstart=0;
  158. int ECFthreadstate=0;
  159. pthread_t threadid, threadid_yafu;
  160. pthread_t threadidv; /*Verify Flash*/
  161. pthread_mutex_t YafuThreadMutex;
  162. pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  163. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  164. pthread_cond_t condv = PTHREAD_COND_INITIALIZER; /*Verify Flash*/
  165. pthread_mutex_t mutexv = PTHREAD_MUTEX_INITIALIZER; /*Verify Flash*/
  166. struct erase_info_user {
  167. uint32_t start;
  168. uint32_t length;
  169. };
  170. struct mtd_info_user{
  171. unsigned char type;
  172. unsigned long flags;
  173. unsigned long size;
  174. unsigned long erasesize;
  175. unsigned long oobblock;
  176. unsigned long oobsize;
  177. unsigned long ecctype;
  178. unsigned long eccsize;
  179. };
  180. /* Initialize the Global structure with default MTD information */
  181. AMIYAFUSwitchFlashDeviceRes_T gAMIYAFUSwitchFlashDevice;
  182. int HookSkips = 0;
  183. #endif
  184. static const INT8U m_DualResBits [] = {2,1,2,1,2,1,1,2};
  185. //extern IfcName_T Ifcnametable[MAX_LAN_CHANNELS];
  186. static const INT8U TriggerEventParamLength[] ={
  187. 1, /*Length for Critical Flag*/
  188. 1, /*Length for Non Critical Flag*/
  189. 1, /*Length for Non Recoverable Flag*/
  190. 1, /*Length for Fan Troubled Flag*/
  191. 1, /*Length for WDT Expire Flag*/
  192. 1, /*Length for System DC On Flag*/
  193. 1, /*Length for System DC Off Flag*/
  194. 1, /*Length for System DC Reset Flag*/
  195. 1+4, /*Length for Specific date time Flag*/
  196. 1, /*Length for LPC Reset Flag*/
  197. };
  198. /*PAM and NSS defalut order Information*/
  199. PamOrder_T g_DfaultOrder [MODULES_TO_MODIFIED]=
  200. {
  201. {1, PAM_IPMI},
  202. {2, PAM_LDAP},
  203. {3, PAM_AD},
  204. {4, PAM_RADIUS}
  205. };
  206. PamOrder_T g_NssOrder [MODULES_TO_MODIFIED] =
  207. {
  208. {1, "ipmi"},
  209. {2, "ldap"},
  210. {3, "ad "},
  211. {4, "radius"}
  212. };
  213. char g_FlashingImage;
  214. char g_RunningImage;
  215. char g_InactiveImage;
  216. unsigned char g_FWUploadSelector = 0xFF;
  217. static unsigned char g_FWBootSelector = 0xFF;
  218. char g_rebootstatus=TRUE;
  219. #ifndef CONFIG_SPX_FEATURE_IPMI_NO_YAFU_SUPPORT
  220. int InitYafuVar()
  221. {
  222. gAMIYAFUSwitchFlashDevice.CompletionCode = 0x0;
  223. strcpy(gAMIYAFUSwitchFlashDevice.MTDName,DEFAULT_MTD_DEVICE);
  224. gAMIYAFUSwitchFlashDevice.FlashInfo.FlashSize = g_coremacros.global_used_flash_size;
  225. gAMIYAFUSwitchFlashDevice.FlashInfo.FlashAddress = g_coremacros.global_used_flash_start;
  226. gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize = g_coremacros.global_erase_blk_size;
  227. gAMIYAFUSwitchFlashDevice.FlashInfo.FlashProductID = 0;
  228. gAMIYAFUSwitchFlashDevice.FlashInfo.FlashWidth = 8;
  229. gAMIYAFUSwitchFlashDevice.FlashInfo.FMHCompliance = 1;
  230. gAMIYAFUSwitchFlashDevice.FlashInfo.Reserved = 0;
  231. gAMIYAFUSwitchFlashDevice.FlashInfo.NoEraseBlks = 0;
  232. return 0;
  233. }
  234. #endif
  235. static int Decrypt_LicenseKey(unsigned char *LicenseKey,int size, char *DecrypteLicenseKey);
  236. static int IsDuplicateKey(unsigned char *LicenseKey);
  237. /**
  238. * FlashTimerTask
  239. **/
  240. void
  241. FlashTimerTask (int BMCInst)
  242. {
  243. static int m_InitFlashVars = 0;
  244. if (0 == m_InitFlashVars)
  245. {
  246. m_InitFlashVars = 1;
  247. g_FlashTimerInfo.TimeOut = 0;
  248. g_FlashTimerInfo.FlashProcessStarted = 0;
  249. }
  250. if (0 == g_FlashTimerInfo.FlashProcessStarted)
  251. {
  252. g_FlashTimerInfo.TimeOut = 0;
  253. return;
  254. }
  255. if (0 == g_FlashTimerInfo.TimeOut)
  256. {
  257. IPMI_DBG_PRINT ("Flashing Process Timed out \n");
  258. g_FlashTimerInfo.FlashProcessStarted = 0;
  259. reboot (LINUX_REBOOT_CMD_RESTART);
  260. }
  261. g_FlashTimerInfo.TimeOut--;
  262. }
  263. /**
  264. ** @fn DefaultSettingsForDualImageSupport
  265. ** @brief To set the flashing image as inactive image defaultly.
  266. ** @param[in] - void.
  267. ** @retval Will return 0 on success.
  268. **
  269. **/
  270. int DefaultSettingsForDualImageSupport()
  271. {
  272. DUALIMGINFO_STRUCT info;
  273. char envval[5] = {0};
  274. int bootselector;
  275. if( GetDualImageOptions(&info) == -1)
  276. {
  277. TCRIT("Unable to get GetDualImageOptions");
  278. return -1;
  279. }
  280. /* running and inactive images are remain same till next reboot*/
  281. g_RunningImage = info.runningImg;
  282. g_InactiveImage = info.InactiveImg;
  283. //default upload image is inactive image
  284. g_FWUploadSelector = AUTO_INACTIVE_IMAGE;
  285. g_FlashingImage = info.InactiveImg;
  286. if( GetUBootParam("bootselector",envval) == 0 )
  287. {
  288. sscanf(envval,"%d",&bootselector);
  289. //Checking for valid Boot selector option
  290. if( CheckForBootOption(bootselector) == 0)
  291. {
  292. g_FWBootSelector = bootselector;
  293. }
  294. }
  295. return 0;
  296. }
  297. /**
  298. * @fn SetMostRecentlyProgFW
  299. * @brief Sets the most recently programmed firmware image to env variable recentlyprogfw.
  300. * @param[in] - Nill
  301. * @retval 0 - on success.
  302. * -1 - on failure case
  303. */
  304. int SetMostRecentlyProgFW(void)
  305. {
  306. char envval[5];
  307. memset(envval,0,sizeof(envval));
  308. if(( g_FlashingImage == IMAGE_1) || (g_FlashingImage == IMAGE_BOTH)) {
  309. sprintf(envval,"%d",IMAGE_1);
  310. }
  311. else if( g_FlashingImage == IMAGE_2) {
  312. sprintf(envval,"%d",IMAGE_2);
  313. }
  314. else {
  315. return -1;
  316. }
  317. if( SetUBootParam("recentlyprogfw",envval) != 0 )
  318. {
  319. TCRIT("Unable to set env variable updation\n");
  320. return -1;
  321. }
  322. return 0;
  323. }
  324. #ifndef CONFIG_SPX_FEATURE_IPMI_NO_YAFU_SUPPORT
  325. int Get_Cuurent_Bios_Img_Info(FlashDetails *biosinfo, char *MTD_Device)
  326. {
  327. struct mtd_info_user user_info;
  328. int retval = -1;
  329. int MTDDev = 0;
  330. MTDDev = open (MTD_Device, O_RDONLY);
  331. if (MTDDev < 0)
  332. {
  333. TCRIT ("Cannot open mtd raw device %s. Exiting...\n", MTD_Device);
  334. return retval;
  335. }
  336. memset(&user_info, 0x00, sizeof(struct mtd_info_user));
  337. if (ioctl(MTDDev, MEMGETINFO, &user_info) < 0)
  338. {
  339. printf("Unable to get MTD info via IOCTL(MEMGETINFO) ... %s\n", strerror(errno));
  340. return retval;
  341. }
  342. biosinfo->FlashSize= user_info.size;
  343. biosinfo->FlashAddress = CONFIG_SPX_BIOS_FLASH_START ;
  344. biosinfo->FlashEraseBlkSize = user_info.erasesize ;
  345. retval = 0;
  346. return retval;
  347. }
  348. int
  349. Switch_Device (int SPIDevice, INT8U *pRes)
  350. {
  351. AMIYAFUSwitchFlashDeviceRes_T *pAMIYAFUSwitchFlashDevice_Res = (AMIYAFUSwitchFlashDeviceRes_T *)pRes;
  352. int retval = -1;
  353. memset((char *)pAMIYAFUSwitchFlashDevice_Res, 0, sizeof(AMIYAFUSwitchFlashDeviceRes_T));
  354. if(access("/var/yafu_bios_update_selection", F_OK) != 0)
  355. {
  356. if ((SPIDevice == 0) || (g_PDKHandle[PDK_SWITCH_SPI] == NULL))
  357. {
  358. if (SPIDevice)
  359. {
  360. TCRIT("Invalid SPI Device Node. Defaulting to 0\n");
  361. }
  362. strcpy((char *)&pAMIYAFUSwitchFlashDevice_Res->MTDName[0], (char *)DEFAULT_MTD_DEVICE);
  363. pAMIYAFUSwitchFlashDevice_Res->FlashInfo.FlashSize = g_coremacros.global_used_flash_size;
  364. pAMIYAFUSwitchFlashDevice_Res->FlashInfo.FlashAddress = g_coremacros.global_used_flash_start;
  365. pAMIYAFUSwitchFlashDevice_Res->FlashInfo.FlashEraseBlkSize = g_coremacros.global_erase_blk_size;
  366. pAMIYAFUSwitchFlashDevice_Res->FlashInfo.FMHCompliance = 0x01;
  367. retval = 0;
  368. goto exit_gracefully;
  369. }
  370. else
  371. {
  372. retval = ((int(*)(int, INT8U*))g_PDKHandle[PDK_SWITCH_SPI]) (SPIDevice, pRes);
  373. if (retval != 0)
  374. {
  375. TCRIT("Unable to identify the SPI Partition from the MTD Mapping\n");
  376. goto exit_gracefully;
  377. }
  378. ActivateFlashStatus = 1;
  379. HookSkips = 1;
  380. }
  381. }
  382. else
  383. {
  384. char MTD_Device[128];
  385. FlashDetails biosinfo;
  386. ActivateFlashStatus = 0x00;
  387. StartInitFlashSetting();
  388. /* find the MTD number for HOST SPI */
  389. if (FindHostBiosDeviceName(MTD_Device) != 0)
  390. goto exit_gracefully;
  391. strcpy((char *)&pAMIYAFUSwitchFlashDevice_Res->MTDName[0], MTD_Device);
  392. if(Get_Cuurent_Bios_Img_Info(&biosinfo, MTD_Device) < 0)
  393. goto exit_gracefully;
  394. pAMIYAFUSwitchFlashDevice_Res->FlashInfo.FlashSize = biosinfo.FlashSize;
  395. pAMIYAFUSwitchFlashDevice_Res->FlashInfo.FlashAddress = biosinfo.FlashAddress;
  396. pAMIYAFUSwitchFlashDevice_Res->FlashInfo.FlashEraseBlkSize = biosinfo.FlashEraseBlkSize;
  397. retval = 0;
  398. }
  399. exit_gracefully:
  400. return retval;
  401. }
  402. /*---------------------------------------
  403. * AMIYAFUSwitchFlashDevice
  404. *---------------------------------------*/
  405. int AMIYAFUSwitchFlashDevice(_NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes ,_NEAR_ int BMCInst)
  406. {
  407. AMIYAFUSwitchFlashDeviceReq_T *pAMIYAFUSwitchFlashDevice_Req = (AMIYAFUSwitchFlashDeviceReq_T *)pReq;
  408. AMIYAFUSwitchFlashDeviceRes_T *pAMIYAFUSwitchFlashDevice_Res = (AMIYAFUSwitchFlashDeviceRes_T *)pRes;
  409. int SPIDevice = (int)(pAMIYAFUSwitchFlashDevice_Req->SPIDevice);
  410. int ErrCode = 0;
  411. ErrCode = Switch_Device(SPIDevice, (INT8U *)pAMIYAFUSwitchFlashDevice_Res);
  412. if (ErrCode != 0)
  413. {
  414. TCRIT("Unable to Switch to the appropriate SPI Device\n");
  415. pAMIYAFUSwitchFlashDevice_Res->CompletionCode = YAFU_CC_READ_ERR;
  416. return sizeof(INT8U);
  417. }
  418. pAMIYAFUSwitchFlashDevice_Res->CompletionCode = CC_NORMAL;
  419. memcpy((char *)&gAMIYAFUSwitchFlashDevice, (char *)pAMIYAFUSwitchFlashDevice_Res, sizeof(AMIYAFUSwitchFlashDeviceRes_T));
  420. gDeviceNode = SPIDevice;
  421. return (sizeof(AMIYAFUSwitchFlashDeviceRes_T));
  422. }
  423. /*---------------------------------------
  424. * AMIYAFURestoreFlashDevice
  425. *---------------------------------------*/
  426. int AMIYAFURestoreFlashDevice(_NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes ,_NEAR_ int BMCInst)
  427. {
  428. int retval = 0;
  429. AMIYAFUSwitchFlashDeviceRes_T *pAMIYAFUSwitchFlashDevice_Res = (AMIYAFUSwitchFlashDeviceRes_T *)pRes;
  430. if (g_PDKHandle[PDK_REVERT_SPI] != NULL)
  431. {
  432. retval = ((int(*)(void))g_PDKHandle[PDK_REVERT_SPI]) ();
  433. if (retval != 0)
  434. {
  435. TCRIT("Unable to clear the flash mapping information\n");
  436. pAMIYAFUSwitchFlashDevice_Res->CompletionCode = YAFU_CC_READ_ERR;
  437. return sizeof(INT8U);
  438. }
  439. }
  440. pAMIYAFUSwitchFlashDevice_Res->CompletionCode = CC_NORMAL;
  441. return (sizeof(AMIYAFUSwitchFlashDeviceRes_T));
  442. }
  443. /*---------------------------------------
  444. * AMIYAFUNotAcks
  445. *---------------------------------------*/
  446. int AMIYAFUNotAcks(INT8U *pRes,INT16U ErrorCode,INT32U Seqnum)
  447. {
  448. AMIYAFUNotAck* pAMIYAFUNotAcknowledge =(AMIYAFUNotAck*)pRes;
  449. pAMIYAFUNotAcknowledge->CompletionCode = YAFU_ERR_STATE;
  450. pAMIYAFUNotAcknowledge->NotAck.Seqnum = Seqnum;
  451. pAMIYAFUNotAcknowledge->NotAck.YafuCmd = CMD_AMI_YAFU_COMMON_NAK;
  452. pAMIYAFUNotAcknowledge->NotAck.Datalen = 0x02;
  453. pAMIYAFUNotAcknowledge->NotAck.CRC32chksum = 0x00;
  454. pAMIYAFUNotAcknowledge->ErrorCode = ErrorCode;
  455. return (sizeof(AMIYAFUNotAck));
  456. }
  457. /*---------------------------------------
  458. * AMIYAFUGetFlashInfo
  459. *---------------------------------------*/
  460. int AMIYAFUGetFlashInfo ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes ,_NEAR_ int BMCInst)
  461. {
  462. AMIYAFUGetFlashInfoReq_T *pAMIYAFUFlashInfoReq = (AMIYAFUGetFlashInfoReq_T *)pReq;
  463. AMIYAFUGetFlashInfoRes_T* pAMIYAFUGetFlashInfo = (AMIYAFUGetFlashInfoRes_T*)pRes;
  464. pAMIYAFUGetFlashInfo->CompletionCode = YAFU_CC_NORMAL;
  465. pAMIYAFUGetFlashInfo->FlashInfoRes.Seqnum = pAMIYAFUFlashInfoReq->FlashInfoReq.Seqnum;
  466. pAMIYAFUGetFlashInfo->FlashInfoRes.YafuCmd= pAMIYAFUFlashInfoReq->FlashInfoReq.YafuCmd;
  467. pAMIYAFUGetFlashInfo->FlashInfo.FlashSize = gAMIYAFUSwitchFlashDevice.FlashInfo.FlashSize;
  468. pAMIYAFUGetFlashInfo->FlashInfo.FlashAddress = gAMIYAFUSwitchFlashDevice.FlashInfo.FlashAddress;
  469. pAMIYAFUGetFlashInfo->FlashInfo.FlashEraseBlkSize = gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize;
  470. pAMIYAFUGetFlashInfo->FlashInfo.FlashProductID = 0;
  471. pAMIYAFUGetFlashInfo->FlashInfo.FlashWidth = 8;
  472. pAMIYAFUGetFlashInfo->FlashInfo.FMHCompliance = gAMIYAFUSwitchFlashDevice.FlashInfo.FMHCompliance;
  473. if(pAMIYAFUGetFlashInfo->FlashInfo.FMHCompliance == 0x01)
  474. FMHComp = 1;
  475. pAMIYAFUGetFlashInfo->FlashInfo.Reserved = 0;
  476. pAMIYAFUGetFlashInfo->FlashInfo.NoEraseBlks = ((gAMIYAFUSwitchFlashDevice.FlashInfo.FlashSize)/(gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize));
  477. if((pAMIYAFUGetFlashInfo->FlashInfo.NoEraseBlks * pAMIYAFUGetFlashInfo->FlashInfo.FlashEraseBlkSize) == pAMIYAFUGetFlashInfo->FlashInfo.FlashSize)
  478. pAMIYAFUGetFlashInfo->FlashInfoRes.Datalen= 0x20;
  479. else
  480. pAMIYAFUGetFlashInfo->FlashInfoRes.Datalen = 0x20 + pAMIYAFUGetFlashInfo->FlashInfo.NoEraseBlks;
  481. pAMIYAFUGetFlashInfo->FlashInfoRes.CRC32chksum = CalculateChksum((char *)&pAMIYAFUGetFlashInfo->FlashInfo,sizeof(pAMIYAFUGetFlashInfo->FlashInfo));
  482. return( sizeof( AMIYAFUGetFlashInfoRes_T ) );
  483. }
  484. /*---------------------------------------
  485. * AMIYAFUGetFirmwareInfo
  486. *---------------------------------------*/
  487. #define FW_INFO_FILE "/proc/ractrends/Helper/FwInfo"
  488. static void GetFirmwareVersion(unsigned int* Major,unsigned int* Minor,unsigned int* Rev,unsigned int* ProductID)
  489. {
  490. char aline[100];
  491. int AuxVer = 0;
  492. int i = 0, count = 0, ret = 0;
  493. FILE* fp = fopen(FW_INFO_FILE,"rb");
  494. if(fp == NULL)
  495. {
  496. TCRIT("Unable to find firmware version info!!!\n");
  497. *Major = 0;
  498. *Minor = 0;
  499. *Rev = 0;
  500. return ;
  501. }
  502. fgets(aline,93,fp);
  503. for(i = 0; i < 93; i++)
  504. {
  505. if (aline[i] == '\0')
  506. break;
  507. if (aline[i] == '.')
  508. ++count;
  509. }
  510. if(count == 3)
  511. sscanf(aline,"FW_VERSION=%d.%d.%d.%d",Major,Minor,&AuxVer,Rev);
  512. else
  513. sscanf(aline,"FW_VERSION=%d.%d.%d",Major,Minor,Rev);
  514. while(fgets(aline,93,fp) != NULL)
  515. {
  516. ret = sscanf(aline,"FW_PRODUCTID=%d",ProductID);
  517. if (ret == 1)
  518. break;
  519. }
  520. fclose(fp);
  521. return;
  522. }
  523. int AMIYAFUGetFirmwareInfo ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes,_NEAR_ int BMCInst ) //Added by winston for YAFU */
  524. {
  525. static unsigned int Major,Minor,Rev,ProductID;
  526. // static unsigned char MinorBCD;
  527. AMIYAFUGetFirmwareInfoReq_T *pAMIYAFUGetFirmwareInfoReq = (AMIYAFUGetFirmwareInfoReq_T *)pReq;
  528. AMIYAFUGetFirmwareInfoRes_T* pAMIYAFUGetFirmwareInfo = (AMIYAFUGetFirmwareInfoRes_T*)pRes;
  529. static int firsttime = 1;
  530. if(firsttime == 1)
  531. {
  532. GetFirmwareVersion(&Major,&Minor,&Rev,&ProductID);
  533. // MinorBCD = ((Minor/10)<<4)+(Minor%10);
  534. firsttime = 0;
  535. }
  536. pAMIYAFUGetFirmwareInfo->CompletionCode = CC_NORMAL;
  537. pAMIYAFUGetFirmwareInfo->FirmwareinfoRes.Seqnum = pAMIYAFUGetFirmwareInfoReq->FirmwareinfoReq.Seqnum;
  538. pAMIYAFUGetFirmwareInfo->FirmwareinfoRes.YafuCmd= pAMIYAFUGetFirmwareInfoReq->FirmwareinfoReq.YafuCmd;
  539. pAMIYAFUGetFirmwareInfo->FirmwareinfoRes.Datalen= 0x26;
  540. pAMIYAFUGetFirmwareInfo->FirmwareinfoRes.CRC32chksum = 0x00;
  541. pAMIYAFUGetFirmwareInfo->FirmwareInfo.FirmMajVersion = (INT8U)Major;
  542. pAMIYAFUGetFirmwareInfo->FirmwareInfo.FirmMinVersion = (INT8U)Minor;
  543. pAMIYAFUGetFirmwareInfo->FirmwareInfo.FirmAuxVersion = Rev;
  544. pAMIYAFUGetFirmwareInfo->FirmwareInfo.FirmBuildNum = 0;
  545. pAMIYAFUGetFirmwareInfo->FirmwareInfo.Reserved = 0x00;
  546. strcpy((char *)pAMIYAFUGetFirmwareInfo->FirmwareInfo.FirmwareName,"Rom.ima");
  547. pAMIYAFUGetFirmwareInfo->FirmwareInfo.FirmwareSize = gAMIYAFUSwitchFlashDevice.FlashInfo.FlashSize;
  548. pAMIYAFUGetFirmwareInfo->FirmwareInfo.ProductID = (INT32U)ProductID;
  549. pAMIYAFUGetFirmwareInfo->FirmwareinfoRes.CRC32chksum = CalculateChksum((char *)&pAMIYAFUGetFirmwareInfo->FirmwareInfo,sizeof(pAMIYAFUGetFirmwareInfo->FirmwareInfo));
  550. return( sizeof( AMIYAFUGetFirmwareInfoRes_T ) );
  551. }
  552. /*---------------------------------------
  553. * AMIYAFUGetFMHInfo
  554. *---------------------------------------*/
  555. int AMIYAFUGetFMHInfo ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes ,_NEAR_ int BMCInst)
  556. {
  557. int fd = -1;
  558. DWORD i=0,Offset =0,m=0;
  559. INT8U *Buf=NULL;
  560. FlashMH FlashModHeader;
  561. ALT_FMHead AltFmh;
  562. char *FMHDetails = NULL;
  563. AMIYAFUGetFMHInfoReq_T *pAMIYAFUGetFMHInfoReq = (AMIYAFUGetFMHInfoReq_T *)pReq;
  564. AMIYAFUGetFMHInfoRes_T* pAMIYAFUGetFMHInfo = (AMIYAFUGetFMHInfoRes_T*)pRes;
  565. INT8U TotalFMH = 0,ImgCount = 0,NoOfImg = IMAGE_1;
  566. INT16U NoOfFMH = 0;
  567. int ErrCode = 0, retval = 0;
  568. pAMIYAFUGetFMHInfo->NumFMH = 0x00;
  569. FMHDetails = malloc(MAX_FMHLENGTH);
  570. if(FMHDetails == NULL)
  571. {
  572. printf("Error in malloc of FMHDetails");
  573. return -1;
  574. }
  575. if(g_corefeatures.dual_image_support == ENABLED)
  576. {
  577. NoOfImg = IMAGE_2;
  578. }
  579. // This hook can be used to perform any OEM routines such as fetching FMH from different location instead of mtd
  580. // Returning 0 from this hook will revert back to performing default probe and find FMH sequence
  581. // Returning <non-zero> from this hook will validate the ErrCode from the hook, and return appropriately. Default action will be skipped
  582. if (g_PDKHandle[PDK_OEMRESTOREFMH] != NULL)
  583. {
  584. retval = ((int(*)(int, char*, INT8U*, int*, int))g_PDKHandle[PDK_OEMRESTOREFMH])
  585. (gDeviceNode, FMHDetails, &TotalFMH, &ErrCode, BMCInst);
  586. if (retval != 0)
  587. {
  588. if (ErrCode != 0)
  589. {
  590. return (AMIYAFUNotAcks(pRes,YAFU_CC_READ_ERR,pAMIYAFUGetFMHInfoReq->FMHReq.Seqnum));
  591. }
  592. goto skip_probe;
  593. }
  594. }
  595. fd=open(gAMIYAFUSwitchFlashDevice.MTDName, O_RDONLY);
  596. if(fd == -1)
  597. {
  598. if (FMHDetails != NULL)
  599. {
  600. free(FMHDetails);
  601. }
  602. return (AMIYAFUNotAcks(pRes,YAFU_CC_DEV_OPEN_ERR,pAMIYAFUGetFMHInfoReq->FMHReq.Seqnum));
  603. }
  604. for(ImgCount = IMAGE_1;ImgCount<=NoOfImg;ImgCount++)
  605. {
  606. NoOfFMH = 0;
  607. if(ImgCount ==IMAGE_2)
  608. {
  609. Offset = GetMacrodefine_getint("CONFIG_SPX_FEATURE_GLOBAL_USED_FLASH_SIZE", 0);
  610. }
  611. if ( lseek (fd, Offset, SEEK_SET) == -1)
  612. {
  613. fprintf (stderr,
  614. "seek error on %s: %s\n",
  615. gAMIYAFUSwitchFlashDevice.MTDName, strerror (errno));
  616. if (fd != -1)
  617. {
  618. close(fd);
  619. fd=-1;
  620. }
  621. if (FMHDetails != NULL)
  622. {
  623. free(FMHDetails);
  624. }
  625. return (AMIYAFUNotAcks(pRes,YAFU_CC_SEEK_ERR,pAMIYAFUGetFMHInfoReq->FMHReq.Seqnum));
  626. }
  627. for(i=0;i<((gAMIYAFUSwitchFlashDevice.FlashInfo.FlashSize)/(gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize));i++)
  628. {
  629. Buf= (INT8U *)malloc(64);
  630. if(Buf == NULL)
  631. return (AMIYAFUNotAcks(pRes,YAFU_CC_ALLOC_ERR,pAMIYAFUGetFMHInfoReq->FMHReq.Seqnum));
  632. if ( lseek (fd, ( Offset + ( i*(gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize)) ), SEEK_SET) == -1)
  633. {
  634. fprintf (stderr,
  635. "seek error on %s: %s\n",
  636. gAMIYAFUSwitchFlashDevice.MTDName, strerror (errno));
  637. if (fd != -1)
  638. {
  639. close(fd);
  640. fd=-1;
  641. }
  642. if (Buf != NULL)
  643. {
  644. free(Buf);
  645. }
  646. if (FMHDetails != NULL)
  647. {
  648. free(FMHDetails);
  649. }
  650. return (AMIYAFUNotAcks(pRes,YAFU_CC_SEEK_ERR,pAMIYAFUGetFMHInfoReq->FMHReq.Seqnum));
  651. }
  652. if( (read(fd,Buf,64)) != 64 )
  653. {
  654. if (fd != -1)
  655. {
  656. close(fd);
  657. fd=-1;
  658. }
  659. if (Buf != NULL)
  660. {
  661. free(Buf);
  662. }
  663. if (FMHDetails != NULL)
  664. {
  665. free(FMHDetails);
  666. }
  667. return (AMIYAFUNotAcks(pRes,YAFU_CC_READ_ERR,pAMIYAFUGetFMHInfoReq->FMHReq.Seqnum));
  668. }
  669. memcpy((char*)&FlashModHeader,Buf,64);
  670. if(strncmp((char *)FlashModHeader.FmhSignature,"$MODULE$",(sizeof(FlashModHeader.FmhSignature) -1)) == 0 ) //Check FMH Signature
  671. {
  672. if(FlashModHeader.EndSignature != 21930)
  673. continue;
  674. memcpy((FMHDetails + m),(char *)&FlashModHeader,64);
  675. printf("Module Name = %s\n",FlashModHeader.ModuleName);
  676. m +=64;
  677. NoOfFMH += 0x1;
  678. free(Buf);
  679. }
  680. else
  681. {
  682. if ( lseek (fd, ( Offset + ( i*(gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize)) ) +((gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize) -sizeof(AltFmh)), SEEK_SET) == -1)
  683. {
  684. fprintf (stderr,
  685. "seek error on %s: %s\n",
  686. gAMIYAFUSwitchFlashDevice.MTDName, strerror (errno));
  687. if (fd != -1)
  688. {
  689. close(fd);
  690. fd=-1;
  691. }
  692. if (Buf != NULL)
  693. {
  694. free(Buf);
  695. }
  696. if (FMHDetails != NULL)
  697. {
  698. free(FMHDetails);
  699. }
  700. return (AMIYAFUNotAcks(pRes,YAFU_CC_SEEK_ERR,pAMIYAFUGetFMHInfoReq->FMHReq.Seqnum));
  701. }
  702. if( (read(fd,Buf,sizeof(AltFmh))) != sizeof(AltFmh) )
  703. {
  704. fprintf (stderr,
  705. "Bytes read error %s: %s\n",
  706. gAMIYAFUSwitchFlashDevice.MTDName, strerror (errno));
  707. if (fd != -1)
  708. {
  709. close(fd);
  710. fd=-1;
  711. }
  712. if (Buf != NULL)
  713. {
  714. free(Buf);
  715. }
  716. if (FMHDetails != NULL)
  717. {
  718. free(FMHDetails);
  719. }
  720. return (AMIYAFUNotAcks(pRes,YAFU_CC_READ_ERR,pAMIYAFUGetFMHInfoReq->FMHReq.Seqnum));
  721. }
  722. memcpy((char *)&AltFmh,Buf,sizeof(AltFmh));
  723. if(strncmp((char *)AltFmh.Signature,"$MODULE$",(sizeof(AltFmh.Signature) -1)) == 0 ) //Check FMH Signature
  724. {
  725. if ( lseek (fd, ( Offset + ( i*(gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize)) ) +AltFmh.LinkAddress, SEEK_SET) == -1)
  726. {
  727. fprintf (stderr,
  728. "seek error on %s: %s\n",
  729. gAMIYAFUSwitchFlashDevice.MTDName, strerror (errno));
  730. if (fd != -1)
  731. {
  732. close(fd);
  733. fd=-1;
  734. }
  735. if (Buf != NULL)
  736. {
  737. free(Buf);
  738. }
  739. if (FMHDetails != NULL)
  740. {
  741. free(FMHDetails);
  742. }
  743. return (AMIYAFUNotAcks(pRes,YAFU_CC_SEEK_ERR,pAMIYAFUGetFMHInfoReq->FMHReq.Seqnum));
  744. }
  745. if( (read(fd,Buf,64)) != 64 )
  746. {
  747. fprintf (stderr,
  748. "Bytes read error %s: %s\n",
  749. gAMIYAFUSwitchFlashDevice.MTDName, strerror (errno));
  750. if (fd != -1)
  751. {
  752. close(fd);
  753. fd=-1;
  754. }
  755. if (Buf != NULL)
  756. {
  757. free(Buf);
  758. }
  759. if (FMHDetails != NULL)
  760. {
  761. free(FMHDetails);
  762. }
  763. return (AMIYAFUNotAcks(pRes,YAFU_CC_READ_ERR,pAMIYAFUGetFMHInfoReq->FMHReq.Seqnum));
  764. }
  765. memcpy((char*)&FlashModHeader,Buf,64);
  766. if(strncmp((char *)FlashModHeader.FmhSignature,"$MODULE$",(sizeof(FlashModHeader.FmhSignature) -1)) == 0 ) //Check FMH Signature
  767. {
  768. if(FlashModHeader.EndSignature != 21930)
  769. continue;
  770. memcpy((FMHDetails +m),(char *)&FlashModHeader,64);
  771. printf("Module Name = %s\n",FlashModHeader.ModuleName);
  772. m +=64;
  773. NoOfFMH += 0x1;
  774. }
  775. }
  776. free(Buf);
  777. Buf=NULL;
  778. }
  779. }
  780. if(ImgCount == IMAGE_1)
  781. {
  782. pAMIYAFUGetFMHInfo->NumFMH = NoOfFMH;
  783. }
  784. else if(ImgCount == IMAGE_2)
  785. {
  786. pAMIYAFUGetFMHInfo->NumFMH |= NoOfFMH<<8;
  787. }
  788. TotalFMH+= NoOfFMH;
  789. }
  790. skip_probe:
  791. if(pAMIYAFUGetFMHInfoReq->FMHReq.Datalen == 0x00)
  792. {
  793. pAMIYAFUGetFMHInfo->FMHRes.Datalen= 0x04 + (TotalFMH* 64);
  794. }
  795. else
  796. {
  797. pAMIYAFUGetFMHInfo->CompletionCode = CC_INV_DATA_FIELD;
  798. if (fd != -1)
  799. {
  800. close(fd);
  801. fd=-1;
  802. }
  803. if (Buf != NULL)
  804. {
  805. free(Buf);
  806. }
  807. if (FMHDetails != NULL)
  808. {
  809. free(FMHDetails);
  810. }
  811. return sizeof (*pRes);
  812. }
  813. memcpy (( INT8U*) (pAMIYAFUGetFMHInfo + 1),( INT8U*)FMHDetails,(TotalFMH * 64) );
  814. pAMIYAFUGetFMHInfo->CompletionCode = YAFU_CC_NORMAL;
  815. pAMIYAFUGetFMHInfo->FMHRes.Seqnum = pAMIYAFUGetFMHInfoReq->FMHReq.Seqnum;
  816. pAMIYAFUGetFMHInfo->FMHRes.YafuCmd= pAMIYAFUGetFMHInfoReq->FMHReq.YafuCmd;
  817. pAMIYAFUGetFMHInfo->Reserved = 0x00;
  818. pAMIYAFUGetFMHInfo->FMHRes.CRC32chksum = CalculateChksum((char *)&pAMIYAFUGetFMHInfo->Reserved,pAMIYAFUGetFMHInfo->FMHRes.Datalen);
  819. if (FMHDetails != NULL)
  820. free (FMHDetails);
  821. return( sizeof( AMIYAFUGetFMHInfoRes_T ) + (TotalFMH * 64));
  822. }
  823. /*---------------------------------------
  824. * AMIYAFUGetStatus
  825. *---------------------------------------*/
  826. int AMIYAFUGetStatus ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes ,_NEAR_ int BMCInst)
  827. {
  828. AMIYAFUGetStatusReq_T *pAMIYAFUGetStatusReq =(AMIYAFUGetStatusReq_T *)pReq;
  829. AMIYAFUGetStatusRes_T* pAMIYAFUGetStatus = (AMIYAFUGetStatusRes_T*)pRes;
  830. pAMIYAFUGetStatus->GetStatusRes.Seqnum = pAMIYAFUGetStatusReq->GetStatusReq.Seqnum;
  831. pAMIYAFUGetStatus->GetStatusRes.YafuCmd= pAMIYAFUGetStatusReq->GetStatusReq.YafuCmd;
  832. pAMIYAFUGetStatus->LastStatusCode =(INT16U) LastStatCode;
  833. pAMIYAFUGetStatus->YAFUState = 0x00;
  834. pAMIYAFUGetStatus->Mode = 0x00;
  835. pAMIYAFUGetStatus->Reserved = 0x00;
  836. pAMIYAFUGetStatus->GetStatusRes.Datalen=8;
  837. pAMIYAFUGetStatus->Message[0] = 0;
  838. pAMIYAFUGetStatus->CompletionCode = YAFU_CC_NORMAL;
  839. pAMIYAFUGetStatus->GetStatusRes.CRC32chksum = CalculateChksum((char *)&pAMIYAFUGetStatus->LastStatusCode,(INT32U)pAMIYAFUGetStatus->GetStatusRes.Datalen);
  840. return ( sizeof( AMIYAFUGetStatusRes_T ) );
  841. }
  842. /*---------------------------------------
  843. * YAFUTimerAction
  844. *---------------------------------------*/
  845. int YAFUTimerAction()
  846. {
  847. /* Preserving the UBoot configurations*/
  848. char *mtdDevice = MTDDEVICE;
  849. int mtd;
  850. mtd = open (mtdDevice, O_RDWR);
  851. if (mtd < 0)
  852. {
  853. TCRIT ("Cannot open mtd raw device %s. Exiting...\n", mtdDevice);
  854. return errno;
  855. }
  856. /* Write back environment into flash */
  857. if(WriteEnv (&environment,mtd, (EnvStart - FlashStart), EnvSize, GetMacrodefine_getint("CONFIG_SPX_FEATURE_GLOBAL_ERASE_BLOCK_SIZE", 0)) != 0)
  858. {
  859. TCRIT ("Cannot write back u-boot environment preserved into flash\n");
  860. CloseEnv(&environment,mtd);
  861. return -1;
  862. }
  863. CloseEnv(&environment,mtd);
  864. // Deactivate Flash Mode
  865. ActivateFlashStatus = 0x00;
  866. LastStatCode = 0x00;
  867. if(g_corefeatures.dual_image_support == ENABLED)
  868. {
  869. SetMostRecentlyProgFW();
  870. }
  871. // Resetting the BMC
  872. /* Reboot system here */
  873. sleep(1);
  874. reboot (LINUX_REBOOT_CMD_RESTART);
  875. return 0;
  876. }
  877. /*---------------------------------------
  878. * YAFUTimerTask
  879. *---------------------------------------*/
  880. void* YAFUTimerTask(void *pArg)
  881. {
  882. INT8U YafuInterfaceType=0;
  883. INT8U *cursessiontype = (INT8U*)pArg;
  884. prctl(PR_SET_NAME,__FUNCTION__,0,0,0);
  885. if(cursessiontype != NULL)
  886. YafuInterfaceType = *cursessiontype;
  887. if( YafuInterfaceType == LAN_SESSION_TYPE)
  888. {
  889. if( YafuFlashMode == YAFU_INTERACTIVE_FLASH)
  890. YafuTimerCnt = YAFU_LAN_IA_TIMER_COUNT;
  891. else
  892. YafuTimerCnt = YAFU_LAN_FF_TIMER_COUNT ;
  893. }
  894. else if( YafuInterfaceType == KCS_SESSION_TYPE)
  895. {
  896. if( YafuFlashMode == YAFU_INTERACTIVE_FLASH)
  897. YafuTimerCnt = YAFU_KCS_IA_TIMER_COUNT;
  898. else
  899. YafuTimerCnt = YAFU_KCS_FF_TIMER_COUNT ;
  900. }
  901. else if(YafuInterfaceType == USB_SESSION_TYPE)
  902. {
  903. if( YafuFlashMode == YAFU_INTERACTIVE_FLASH)
  904. YafuTimerCnt = YAFU_USB_IA_TIMER_COUNT;
  905. else
  906. YafuTimerCnt = YAFU_USB_FF_TIMER_COUNT;
  907. }
  908. else
  909. {
  910. TDBG(" Flash request received from unknown Interface, so terminating!! \n");
  911. return NULL;
  912. }
  913. TINFO("YafuTimer has started with Timeout = %ld\n",YafuTimerCnt);
  914. while (1)
  915. {
  916. if(YafuflashStatus == YAFU_FLASH_SUCCEEDED)
  917. break;
  918. YafuTimerCnt--;
  919. usleep (1000 * YAFU_MSECS_PER_TICK);
  920. if( !YafuTimerCnt)
  921. {
  922. TDBG("Flashing process has Timed OUT, so preserving the UBoot and resetting the BMC\n");
  923. YAFUTimerAction();
  924. break;
  925. }
  926. }
  927. return NULL;
  928. }
  929. /*---------------------------------------
  930. * AMIYAFUActivateFlashMode
  931. *---------------------------------------*/
  932. int AMIYAFUActivateFlashMode ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes ,_NEAR_ int BMCInst)
  933. {
  934. int RetVal = 0, mtdDev=0;
  935. _FAR_ BMCInfo_t* pBMCInfo = &g_BMCInfo[BMCInst];
  936. AMIYAFUActivateFlashModeReq_T *pAMIYAFUActivateFlashReq = (AMIYAFUActivateFlashModeReq_T *)pReq;
  937. AMIYAFUActivateFlashModeRes_T* pAMIYAFUActivateFlash = (AMIYAFUActivateFlashModeRes_T*)pRes;
  938. _FAR_ SensorSharedMem_T* pSenSharedMem;
  939. INT8U* sessiontype = NULL;
  940. int ErrVal = 0;
  941. if(CalculateChksum((char *)&pAMIYAFUActivateFlashReq->Mode,sizeof(INT16U)) != pAMIYAFUActivateFlashReq->ActivateflashReq.CRC32chksum)
  942. {
  943. LastStatCode=YAFU_INVALID_CHKSUM;
  944. return (AMIYAFUNotAcks(pRes,YAFU_INVALID_CHKSUM,pAMIYAFUActivateFlashReq->ActivateflashReq.Seqnum));
  945. }
  946. if (g_PDKHandle[PDK_ACTIVATEFLASHMODE] != NULL)
  947. {
  948. ErrVal = ((int(*)(INT16U, int *, int))g_PDKHandle[PDK_ACTIVATEFLASHMODE]) (pAMIYAFUActivateFlashReq->Mode, &RetVal, BMCInst);
  949. }
  950. if ((g_PDKHandle[PDK_ACTIVATEFLASHMODE] == NULL) || (ErrVal == 0))
  951. RetVal = PrepareFlashArea (FLSH_CMD_PREP_YAFU_FLASH_AREA, g_corefeatures.dual_image_support);
  952. if ((RetVal == 0) || (RetVal == 4))
  953. {
  954. ActivateFlashStatus = 0x01;
  955. if(access("/var/yafu_bios_update_selection", F_OK) != 0)
  956. {
  957. YafuFlashMode = pAMIYAFUActivateFlashReq->Mode;
  958. if(threadid_yafu == 0)
  959. {
  960. OS_THREAD_TLS_GET(g_tls.CurSessionType,sessiontype);
  961. TINFO("Creating Thread for YafuTimerTask\n");
  962. pthread_create(&threadid_yafu, NULL, YAFUTimerTask,(void *)sessiontype);
  963. }
  964. SetBootParam = 1;
  965. if((RetVal = InitEnv(&environment,&mtdDev,&SetBootParam)) != 0)
  966. {
  967. printf("Cannot init U-Boot Env in YAFUActivateFlashMode for YafuTimerThread\n");
  968. return RetVal;
  969. }
  970. close(mtdDev);
  971. RetVal =0;
  972. }
  973. }
  974. else
  975. {
  976. ActivateFlashStatus = 0x00;
  977. }
  978. if( g_corefeatures.online_flashing_support != ENABLED)
  979. {
  980. pSenSharedMem = (_FAR_ SensorSharedMem_T*)&pBMCInfo->SensorSharedMem; //m_hSensorSharedMem; /* Create mutex for Sensor shared memory */
  981. OS_THREAD_MUTEX_ACQUIRE(&pBMCInfo->SensorSharedMemMutex,WAIT_INFINITE);
  982. pSenSharedMem->GlobalSensorScanningEnable = FALSE;
  983. /* Release mutex for Sensor shared memory */
  984. OS_THREAD_MUTEX_RELEASE(&pBMCInfo->SensorSharedMemMutex);
  985. }
  986. if(pAMIYAFUActivateFlashReq->ActivateflashReq.Datalen == 0x02)
  987. pAMIYAFUActivateFlash->ActivateflashRes.Datalen= 0x02;
  988. else
  989. {
  990. LastStatCode=YAFU_CC_INVALID_DATLEN;
  991. return (AMIYAFUNotAcks(pRes,YAFU_CC_INVALID_DATLEN,pAMIYAFUActivateFlashReq->ActivateflashReq.Seqnum));
  992. }
  993. LOCK_BMC_SHARED_MEM(BMCInst);
  994. g_MBMCInfo.FlashType= YAFU_FLASH;
  995. UNLOCK_BMC_SHARED_MEM(BMCInst);
  996. pAMIYAFUActivateFlash->CompletionCode = YAFU_CC_NORMAL;
  997. pAMIYAFUActivateFlash->ActivateflashRes.Seqnum = pAMIYAFUActivateFlashReq->ActivateflashReq.Seqnum;
  998. pAMIYAFUActivateFlash->ActivateflashRes.YafuCmd= pAMIYAFUActivateFlashReq->ActivateflashReq.YafuCmd;
  999. pAMIYAFUActivateFlash->ActivateflashRes.CRC32chksum = 0x00;
  1000. pAMIYAFUActivateFlash->Delay = 0x00;
  1001. return ( sizeof( AMIYAFUActivateFlashModeRes_T ) );
  1002. }
  1003. /*-------------------------------------------
  1004. * AMIYAFUDualImgSup
  1005. *------------------------------------------*/
  1006. int AMIYAFUDualImgSup(_NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes ,_NEAR_ int BMCInst)
  1007. {
  1008. AMIYAFUDualImgSupReq_T *pAMIYAFUDualImgSupReq = (AMIYAFUDualImgSupReq_T *)pReq;
  1009. AMIYAFUDualImgSupRes_T *pAMIYAFUDualImgSupRes = (AMIYAFUDualImgSupRes_T *)pRes;
  1010. if(g_corefeatures.dual_image_support == ENABLED)
  1011. {
  1012. if(CalculateChksum((char *)&pAMIYAFUDualImgSupReq->PreserveConf,pAMIYAFUDualImgSupReq->DualImgSupReq.Datalen) != pAMIYAFUDualImgSupReq->DualImgSupReq.CRC32chksum)
  1013. {
  1014. LastStatCode=YAFU_INVALID_CHKSUM;
  1015. return (AMIYAFUNotAcks(pRes,YAFU_INVALID_CHKSUM,pAMIYAFUDualImgSupReq->DualImgSupReq.Seqnum));
  1016. }
  1017. pAMIYAFUDualImgSupRes->CompletionCode = YAFU_CC_NORMAL;
  1018. if((pAMIYAFUDualImgSupReq->PreserveConf == TRUE) || (pAMIYAFUDualImgSupReq->PreserveConf == FALSE))
  1019. {
  1020. DualImgPreserveConf = pAMIYAFUDualImgSupReq->PreserveConf;
  1021. }
  1022. else
  1023. {
  1024. LastStatCode = YAFU_CC_INVALID_DATA;
  1025. return (AMIYAFUNotAcks(pRes,YAFU_CC_INVALID_DATA,pAMIYAFUDualImgSupReq->DualImgSupReq.CRC32chksum));
  1026. }
  1027. }
  1028. else
  1029. {
  1030. *pRes = CC_INV_CMD;
  1031. return sizeof(INT8U);
  1032. }
  1033. return sizeof(AMIYAFUDualImgSupRes_T);
  1034. }
  1035. /*---------------------------------------
  1036. * AMIYAFUAllocateMemory
  1037. *---------------------------------------*/
  1038. int AMIYAFUAllocateMemory ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes,_NEAR_ int BMCInst )
  1039. {
  1040. //char command[64];
  1041. //FILE *fp = NULL;
  1042. struct sysinfo s_info;
  1043. int error;
  1044. INT32U free_mem=0;
  1045. AMIYAFUAllocateMemoryReq_T *pAMIYAFUAllocateMemoryReq = (AMIYAFUAllocateMemoryReq_T *)pReq;
  1046. if (ActivateFlashStatus == 0x01)
  1047. {
  1048. AMIYAFUAllocateMemoryRes_T* pAMIYAFUAllocateMemory = (AMIYAFUAllocateMemoryRes_T*)pRes;
  1049. if(CalculateChksum((char *)&pAMIYAFUAllocateMemoryReq->Sizeofmemtoalloc,sizeof(INT32U)) != pAMIYAFUAllocateMemoryReq->AllocmemReq.CRC32chksum)
  1050. {
  1051. LastStatCode=YAFU_INVALID_CHKSUM;
  1052. return (AMIYAFUNotAcks(pRes,YAFU_INVALID_CHKSUM,pAMIYAFUAllocateMemoryReq->AllocmemReq.Seqnum));
  1053. }
  1054. if(pAMIYAFUAllocateMemoryReq->AllocmemReq.Datalen== 0x04)
  1055. pAMIYAFUAllocateMemory->AllocmemRes.Datalen = 0x04;
  1056. else
  1057. {
  1058. LastStatCode=YAFU_CC_INVALID_DATLEN;
  1059. return (AMIYAFUNotAcks(pRes,YAFU_CC_INVALID_DATLEN,pAMIYAFUAllocateMemoryReq->AllocmemReq.Seqnum));
  1060. }
  1061. if(pAMIYAFUAllocateMemoryReq->Sizeofmemtoalloc > (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize) )
  1062. {
  1063. /*strcpy( command, "free | grep Mem | awk '{print $4}'" );
  1064. fp = popen( command, "r" );
  1065. if( fp == NULL )
  1066. return -1;
  1067. fscanf(fp, "%ld", &free_mem );
  1068. pclose(fp);*/
  1069. error=sysinfo(&s_info);
  1070. if(error !=0)
  1071. {
  1072. fprintf(stderr,"\nError in getting free RAM Memory using Sysinfo system call\n Error Code:%d\n",error);
  1073. LastStatCode=YAFU_CC_GET_MEM_ERR;
  1074. return(AMIYAFUNotAcks(pRes,YAFU_CC_GET_MEM_ERR,pAMIYAFUAllocateMemoryReq->AllocmemReq.Seqnum));
  1075. }
  1076. free_mem=s_info.freeram;
  1077. /*Reserve the one block of memory for future allocation to fix the page fault in EraseCopyFlash function*/
  1078. free_mem = free_mem - gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize;
  1079. if(pAMIYAFUAllocateMemoryReq->Sizeofmemtoalloc > free_mem)
  1080. {
  1081. fprintf(stderr,"\nFlash Image Size(%d) is greater than Free Memory(%d) in RAM \n\n",(INT32U)pAMIYAFUAllocateMemoryReq->Sizeofmemtoalloc,free_mem);
  1082. pAMIYAFUAllocateMemory->Addofallocmem = (INT32U)0xfffffffe;
  1083. }
  1084. else
  1085. {
  1086. allocmem =(INT8U *) malloc ( pAMIYAFUAllocateMemoryReq->Sizeofmemtoalloc);
  1087. if(allocmem == NULL)
  1088. pAMIYAFUAllocateMemory->Addofallocmem = (INT32U)0xffffffff;
  1089. else
  1090. pAMIYAFUAllocateMemory->Addofallocmem = (INT32U)allocmem ;
  1091. }
  1092. ImgSize = pAMIYAFUAllocateMemoryReq->Sizeofmemtoalloc;
  1093. }
  1094. else
  1095. {
  1096. allocmem =(INT8U *) malloc ( pAMIYAFUAllocateMemoryReq->Sizeofmemtoalloc);
  1097. if(allocmem == NULL)
  1098. pAMIYAFUAllocateMemory->Addofallocmem = (INT32U)0xffffffff;
  1099. else
  1100. pAMIYAFUAllocateMemory->Addofallocmem = (INT32U)allocmem ;
  1101. ImgSize = pAMIYAFUAllocateMemoryReq->Sizeofmemtoalloc;
  1102. }
  1103. pAMIYAFUAllocateMemory->CompletionCode = YAFU_CC_NORMAL;
  1104. LastStatCode= (int)pAMIYAFUAllocateMemory->CompletionCode;
  1105. pAMIYAFUAllocateMemory->AllocmemRes.Seqnum = pAMIYAFUAllocateMemoryReq->AllocmemReq.Seqnum;
  1106. pAMIYAFUAllocateMemory->AllocmemRes.YafuCmd= pAMIYAFUAllocateMemoryReq->AllocmemReq.YafuCmd;
  1107. pAMIYAFUAllocateMemory->AllocmemRes.CRC32chksum = CalculateChksum((char *)&pAMIYAFUAllocateMemory->Addofallocmem,sizeof(INT32U));
  1108. return ( sizeof(AMIYAFUAllocateMemoryRes_T));
  1109. }
  1110. else
  1111. {
  1112. LastStatCode = YAFU_CC_IN_DEACTIVATE;
  1113. return (AMIYAFUNotAcks(pRes,YAFU_CC_IN_DEACTIVATE,pAMIYAFUAllocateMemoryReq->AllocmemReq.Seqnum));
  1114. }
  1115. }
  1116. /*---------------------------------------
  1117. * AMIYAFUFreeMemory
  1118. *---------------------------------------*/
  1119. int AMIYAFUFreeMemory ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes ,_NEAR_ int BMCInst)
  1120. {
  1121. AMIYAFUFreeMemoryReq_T *pAMIYAFUFreeMemoryReq = (AMIYAFUFreeMemoryReq_T *)pReq;
  1122. if(ActivateFlashStatus == 0x01)
  1123. {
  1124. INT32U *memtofree;
  1125. AMIYAFUFreeMemoryRes_T* pAMIYAFUFreeMemory =(AMIYAFUFreeMemoryRes_T*)pRes;
  1126. if(CalculateChksum((char *)&pAMIYAFUFreeMemoryReq->AddrtobeFreed,sizeof(INT32U)) != pAMIYAFUFreeMemoryReq->FreememReq.CRC32chksum)
  1127. {
  1128. LastStatCode=YAFU_INVALID_CHKSUM;
  1129. return (AMIYAFUNotAcks(pRes,YAFU_INVALID_CHKSUM,pAMIYAFUFreeMemoryReq->FreememReq.Seqnum));
  1130. }
  1131. if(pAMIYAFUFreeMemoryReq->FreememReq.Datalen== 0x04)
  1132. pAMIYAFUFreeMemory->FreememRes.Datalen= 0x01;
  1133. else
  1134. {
  1135. LastStatCode = YAFU_CC_INVALID_DATLEN;
  1136. return (AMIYAFUNotAcks(pRes,YAFU_CC_INVALID_DATLEN,pAMIYAFUFreeMemoryReq->FreememReq.Seqnum));
  1137. }
  1138. memtofree = (INT32U *)pAMIYAFUFreeMemoryReq->AddrtobeFreed;
  1139. free(memtofree);
  1140. pAMIYAFUFreeMemory->Status =0x00;
  1141. pAMIYAFUFreeMemory->CompletionCode = YAFU_CC_NORMAL;
  1142. LastStatCode = pAMIYAFUFreeMemory->CompletionCode;
  1143. pAMIYAFUFreeMemory->FreememRes.Seqnum = pAMIYAFUFreeMemoryReq->FreememReq.Seqnum;
  1144. pAMIYAFUFreeMemory->FreememRes.YafuCmd= pAMIYAFUFreeMemoryReq->FreememReq.YafuCmd;
  1145. pAMIYAFUFreeMemory->FreememRes.CRC32chksum = CalculateChksum((char *)&pAMIYAFUFreeMemory->Status,sizeof(INT8U));
  1146. return (sizeof(AMIYAFUFreeMemoryRes_T));
  1147. }
  1148. else
  1149. {
  1150. LastStatCode = YAFU_CC_IN_DEACTIVATE;
  1151. return (AMIYAFUNotAcks(pRes,YAFU_CC_IN_DEACTIVATE,pAMIYAFUFreeMemoryReq->FreememReq.Seqnum));
  1152. }
  1153. }
  1154. /*---------------------------------------
  1155. * AMIYAFUReadFlash
  1156. *---------------------------------------*/
  1157. int AMIYAFUReadFlash ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes,_NEAR_ int BMCInst )
  1158. {
  1159. AMIYAFUReadFlashReq_T *pAMIYAFUReadFlashReq = (AMIYAFUReadFlashReq_T *)pReq;
  1160. if(ActivateFlashStatus == 0x01)
  1161. {
  1162. int fd =0;
  1163. //INT8U *offset;
  1164. INT8U *Buf = 0;
  1165. DWORD StartOffset = 0;
  1166. Buf = NULL;
  1167. AMIYAFUReadFlashRes_T* pAMIYAFUReadFlash =(AMIYAFUReadFlashRes_T*)pRes;
  1168. if(CalculateChksum((char *)&pAMIYAFUReadFlashReq->offsettoread,pAMIYAFUReadFlashReq->ReadFlashReq.Datalen) != pAMIYAFUReadFlashReq->ReadFlashReq.CRC32chksum)
  1169. {
  1170. LastStatCode=YAFU_INVALID_CHKSUM;
  1171. return (AMIYAFUNotAcks(pRes,YAFU_INVALID_CHKSUM,pAMIYAFUReadFlashReq->ReadFlashReq.Seqnum));
  1172. }
  1173. if(pAMIYAFUReadFlashReq->ReadFlashReq.Datalen!= 0x07)
  1174. {
  1175. LastStatCode = YAFU_CC_INVALID_DATLEN ;
  1176. return (AMIYAFUNotAcks(pRes,YAFU_CC_INVALID_DATLEN,pAMIYAFUReadFlashReq->ReadFlashReq.Seqnum));
  1177. }
  1178. // offset = (INT8U *) pAMIYAFUReadFlashReq->offsettoread;
  1179. Buf = malloc(pAMIYAFUReadFlashReq->Sizetoread);
  1180. if(Buf == NULL)
  1181. {
  1182. LastStatCode = YAFU_CC_ALLOC_ERR;
  1183. return (AMIYAFUNotAcks(pRes,YAFU_CC_ALLOC_ERR,pAMIYAFUReadFlashReq->ReadFlashReq.Seqnum));
  1184. }
  1185. if(g_corefeatures.dual_image_support == ENABLED)
  1186. {
  1187. if( g_FlashingImage == IMAGE_2 )
  1188. StartOffset = g_coremacros.global_used_flash_size;
  1189. }
  1190. fd=open(gAMIYAFUSwitchFlashDevice.MTDName, O_RDONLY);
  1191. if(fd == -1)
  1192. {
  1193. LastStatCode = YAFU_CC_DEV_OPEN_ERR;
  1194. IPMI_ERROR ("Amiyafuupdate:Unable to open %s device/n", gAMIYAFUSwitchFlashDevice.MTDName);
  1195. printf("Unable to open %s device\n", gAMIYAFUSwitchFlashDevice.MTDName);
  1196. if(Buf != NULL)
  1197. {
  1198. free(Buf);
  1199. }
  1200. return (AMIYAFUNotAcks(pRes,YAFU_CC_DEV_OPEN_ERR,pAMIYAFUReadFlashReq->ReadFlashReq.Seqnum));
  1201. }
  1202. if ( lseek (fd,StartOffset + pAMIYAFUReadFlashReq->offsettoread, SEEK_SET) == -1)
  1203. {
  1204. LastStatCode = YAFU_CC_SEEK_ERR;
  1205. fprintf (stderr,
  1206. "seek error on %s: %s\n",
  1207. gAMIYAFUSwitchFlashDevice.MTDName, strerror (errno));
  1208. if(Buf != NULL)
  1209. {
  1210. free(Buf);
  1211. }
  1212. if(fd != -1)
  1213. {
  1214. close(fd);
  1215. }
  1216. return (AMIYAFUNotAcks(pRes,YAFU_CC_SEEK_ERR,pAMIYAFUReadFlashReq->ReadFlashReq.Seqnum));
  1217. }
  1218. if( (read(fd,Buf,pAMIYAFUReadFlashReq->Sizetoread)) != pAMIYAFUReadFlashReq->Sizetoread)
  1219. {
  1220. LastStatCode = YAFU_CC_READ_ERR;
  1221. fprintf (stderr,
  1222. "Bytes read error %s: %s\n",
  1223. gAMIYAFUSwitchFlashDevice.MTDName, strerror (errno));
  1224. if(Buf != NULL)
  1225. {
  1226. free(Buf);
  1227. }
  1228. if(fd != -1)
  1229. {
  1230. close(fd);
  1231. }
  1232. return (AMIYAFUNotAcks(pRes,YAFU_CC_READ_ERR,pAMIYAFUReadFlashReq->ReadFlashReq.Seqnum));
  1233. }
  1234. pAMIYAFUReadFlash->ReadFlashRes.Datalen= pAMIYAFUReadFlashReq->Sizetoread;
  1235. pAMIYAFUReadFlash->CompletionCode = YAFU_CC_NORMAL;
  1236. LastStatCode = pAMIYAFUReadFlash->CompletionCode;
  1237. pAMIYAFUReadFlash->ReadFlashRes.Seqnum = pAMIYAFUReadFlashReq->ReadFlashReq.Seqnum;
  1238. pAMIYAFUReadFlash->ReadFlashRes.YafuCmd= pAMIYAFUReadFlashReq->ReadFlashReq.YafuCmd;
  1239. pAMIYAFUReadFlash->ReadFlashRes.CRC32chksum = CalculateChksum((char *)&Buf,sizeof(pAMIYAFUReadFlashReq->Sizetoread));
  1240. memcpy (( INT8U*) (pAMIYAFUReadFlash + 1),
  1241. ( INT8U*)Buf,
  1242. pAMIYAFUReadFlashReq->Sizetoread );
  1243. free(Buf);
  1244. if(fd != -1)
  1245. {
  1246. close(fd);
  1247. }
  1248. return (sizeof(AMIYAFUReadFlashRes_T) + pAMIYAFUReadFlashReq->Sizetoread);
  1249. }
  1250. else
  1251. {
  1252. LastStatCode = YAFU_CC_IN_DEACTIVATE;
  1253. return (AMIYAFUNotAcks(pRes,YAFU_CC_IN_DEACTIVATE,pAMIYAFUReadFlashReq->ReadFlashReq.Seqnum));
  1254. }
  1255. }
  1256. /*---------------------------------------
  1257. * AMIYAFUWriteFlash
  1258. *---------------------------------------*/
  1259. int AMIYAFUWriteFlash ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes ,_NEAR_ int BMCInst)
  1260. {
  1261. AMIYAFUWriteFlashReq_T *pAMIYAFUWriteFlashReq = (AMIYAFUWriteFlashReq_T *)pReq;
  1262. if(ActivateFlashStatus == 0x01)
  1263. {
  1264. //INT8U *OffsetToWrite;
  1265. int fd;
  1266. DWORD StartOffset = 0;
  1267. AMIYAFUWriteFlashRes_T* pAMIYAFUWriteFlash = (AMIYAFUWriteFlashRes_T*)pRes;
  1268. if(CalculateChksum((char *)&pAMIYAFUWriteFlashReq->offsettowrite,pAMIYAFUWriteFlashReq->WriteFlashReq.Datalen) != pAMIYAFUWriteFlashReq->WriteFlashReq.CRC32chksum)
  1269. {
  1270. LastStatCode=YAFU_INVALID_CHKSUM;
  1271. return (AMIYAFUNotAcks(pRes,YAFU_INVALID_CHKSUM,pAMIYAFUWriteFlashReq->WriteFlashReq.Seqnum));
  1272. }
  1273. //OffsetToWrite = (INT8U *)pAMIYAFUWriteFlashReq->offsettowrite;
  1274. if(g_corefeatures.dual_image_support == ENABLED)
  1275. {
  1276. if( g_FlashingImage == IMAGE_2 )
  1277. StartOffset = g_coremacros.global_used_flash_size;
  1278. }
  1279. fd=open(gAMIYAFUSwitchFlashDevice.MTDName, O_RDWR);
  1280. if (fd == -1)
  1281. {
  1282. LastStatCode = YAFU_CC_DEV_OPEN_ERR;
  1283. printf("ERROR: open failed (%s)\n", strerror(errno));
  1284. return (AMIYAFUNotAcks(pRes,YAFU_CC_DEV_OPEN_ERR,pAMIYAFUWriteFlashReq->WriteFlashReq.Seqnum));
  1285. }
  1286. if ( lseek (fd, StartOffset + pAMIYAFUWriteFlashReq->offsettowrite, SEEK_SET) == -1)
  1287. {
  1288. LastStatCode = YAFU_CC_SEEK_ERR;
  1289. fprintf (stderr,
  1290. "seek error on %s: %s\n",
  1291. gAMIYAFUSwitchFlashDevice.MTDName, strerror (errno));
  1292. return (AMIYAFUNotAcks(pRes,YAFU_CC_SEEK_ERR,pAMIYAFUWriteFlashReq->WriteFlashReq.Seqnum));
  1293. }
  1294. if(write(fd,(pReq + sizeof(AMIYAFUWriteFlashReq_T)),(pAMIYAFUWriteFlashReq->WriteFlashReq.Datalen - 5)) != (pAMIYAFUWriteFlashReq->WriteFlashReq.Datalen - 5))
  1295. {
  1296. LastStatCode = YAFU_CC_WRITE_ERR;
  1297. return (AMIYAFUNotAcks(pRes,YAFU_CC_SEEK_ERR,pAMIYAFUWriteFlashReq->WriteFlashReq.Seqnum));
  1298. }
  1299. pAMIYAFUWriteFlash->SizeWritten = (pAMIYAFUWriteFlashReq->WriteFlashReq.Datalen - 5);
  1300. pAMIYAFUWriteFlash->CompletionCode = YAFU_CC_NORMAL;
  1301. LastStatCode = pAMIYAFUWriteFlash->CompletionCode;
  1302. pAMIYAFUWriteFlash->WriteFlashRes.Seqnum = pAMIYAFUWriteFlashReq->WriteFlashReq.Seqnum;
  1303. pAMIYAFUWriteFlash->WriteFlashRes.YafuCmd= pAMIYAFUWriteFlashReq->WriteFlashReq.YafuCmd;
  1304. pAMIYAFUWriteFlash->WriteFlashRes.Datalen= 0x02;
  1305. pAMIYAFUWriteFlash->WriteFlashRes.CRC32chksum = CalculateChksum((char *)&(pAMIYAFUWriteFlash->SizeWritten),sizeof(INT16U));
  1306. close(fd);
  1307. return (sizeof(AMIYAFUWriteFlashRes_T));
  1308. }
  1309. else
  1310. {
  1311. LastStatCode = YAFU_CC_IN_DEACTIVATE;
  1312. return (AMIYAFUNotAcks(pRes,YAFU_CC_IN_DEACTIVATE,pAMIYAFUWriteFlashReq->WriteFlashReq.Seqnum));
  1313. }
  1314. }
  1315. /*---------------------------------------
  1316. * AMIYAFUEraseFlash
  1317. *---------------------------------------*/
  1318. int AMIYAFUEraseFlash ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes,_NEAR_ int BMCInst )
  1319. {
  1320. AMIYAFUErashFlashReq_T *pAMIYAFUEraseFlashReq = (AMIYAFUErashFlashReq_T *)pReq;
  1321. if(ActivateFlashStatus == 0x01)
  1322. {
  1323. INT32U offset;
  1324. int fd;
  1325. DWORD StartOffset = 0;
  1326. struct erase_info_user erase_info;
  1327. AMIYAFUErashFlashRes_T* pAMIYAFUEraseFlash =(AMIYAFUErashFlashRes_T*)pRes;
  1328. if(CalculateChksum((char *)&pAMIYAFUEraseFlashReq->Blknumtoerase,sizeof(INT32U)) != pAMIYAFUEraseFlashReq->EraseFlashReq.CRC32chksum)
  1329. {
  1330. LastStatCode=YAFU_INVALID_CHKSUM;
  1331. return (AMIYAFUNotAcks(pRes,YAFU_INVALID_CHKSUM,pAMIYAFUEraseFlashReq->EraseFlashReq.Seqnum));
  1332. }
  1333. if(pAMIYAFUEraseFlashReq->EraseFlashReq.Datalen== 0x04)
  1334. pAMIYAFUEraseFlash->EraseFlashRes.Datalen= 0x01;
  1335. else
  1336. {
  1337. LastStatCode= YAFU_CC_INVALID_DATLEN;
  1338. return (AMIYAFUNotAcks(pRes,YAFU_CC_INVALID_DATLEN,pAMIYAFUEraseFlashReq->EraseFlashReq.Seqnum));
  1339. }
  1340. offset = ((pAMIYAFUEraseFlashReq->Blknumtoerase) -1) * (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize);
  1341. if(g_corefeatures.dual_image_support == ENABLED)
  1342. {
  1343. if( g_FlashingImage == IMAGE_2 )
  1344. StartOffset = g_coremacros.global_used_flash_size;
  1345. }
  1346. fd=open(gAMIYAFUSwitchFlashDevice.MTDName, O_RDWR);
  1347. if (fd == -1)
  1348. {
  1349. LastStatCode = YAFU_CC_DEV_OPEN_ERR;
  1350. printf("ERROR: open failed (%s)\n", strerror(errno));
  1351. return (AMIYAFUNotAcks(pRes,YAFU_CC_DEV_OPEN_ERR,pAMIYAFUEraseFlashReq->EraseFlashReq.Seqnum));
  1352. }
  1353. erase_info.start = StartOffset + offset;
  1354. erase_info.length = (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize);
  1355. if((ioctl (fd, MEMERASE, &erase_info)) != -1)
  1356. {
  1357. pAMIYAFUEraseFlash->Status = 0x00;
  1358. }
  1359. else
  1360. {
  1361. pAMIYAFUEraseFlash->Status = 0x01;
  1362. }
  1363. pAMIYAFUEraseFlash->CompletionCode =YAFU_CC_NORMAL;
  1364. LastStatCode = (INT16U) pAMIYAFUEraseFlash->CompletionCode;
  1365. pAMIYAFUEraseFlash->EraseFlashRes.Seqnum = pAMIYAFUEraseFlashReq->EraseFlashReq.Seqnum;
  1366. pAMIYAFUEraseFlash->EraseFlashRes.YafuCmd= pAMIYAFUEraseFlashReq->EraseFlashReq.YafuCmd;
  1367. pAMIYAFUEraseFlash->EraseFlashRes.CRC32chksum = CalculateChksum((char *)&(pAMIYAFUEraseFlash->Status),sizeof(INT8U));
  1368. return (sizeof(AMIYAFUErashFlashRes_T));
  1369. }
  1370. else
  1371. {
  1372. LastStatCode = YAFU_CC_IN_DEACTIVATE;
  1373. return (AMIYAFUNotAcks(pRes,YAFU_CC_IN_DEACTIVATE,pAMIYAFUEraseFlashReq->EraseFlashReq.Seqnum));
  1374. }
  1375. }
  1376. /*---------------------------------------
  1377. * AMIYAFUProtectFlash
  1378. *---------------------------------------*/
  1379. int AMIYAFUProtectFlash ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes,_NEAR_ int BMCInst )
  1380. {
  1381. AMIYAFUProtectFlashReq_T *pAMIYAFUProtectFlashReq = (AMIYAFUProtectFlashReq_T *)pReq;
  1382. if(ActivateFlashStatus == 0x01)
  1383. {
  1384. INT32U offset;
  1385. int fd;
  1386. DWORD StartOffset = 0;
  1387. struct erase_info_user erase_info;
  1388. AMIYAFUProtectFlashRes_T* pAMIYAFUProtectFlash = (AMIYAFUProtectFlashRes_T*)pRes;
  1389. if(CalculateChksum((char *)&pAMIYAFUProtectFlashReq->Blknum,pAMIYAFUProtectFlashReq->ProtectFlashReq.Datalen) != pAMIYAFUProtectFlashReq->ProtectFlashReq.CRC32chksum)
  1390. {
  1391. LastStatCode=YAFU_INVALID_CHKSUM;
  1392. return (AMIYAFUNotAcks(pRes,YAFU_INVALID_CHKSUM,pAMIYAFUProtectFlashReq->ProtectFlashReq.Seqnum));
  1393. }
  1394. if(pAMIYAFUProtectFlashReq->ProtectFlashReq.Datalen== 0x05)
  1395. pAMIYAFUProtectFlash->ProtectFlashRes.Datalen= 0x01;
  1396. else
  1397. {
  1398. LastStatCode = YAFU_CC_INVALID_DATLEN;
  1399. return (AMIYAFUNotAcks(pRes,YAFU_CC_INVALID_DATLEN,pAMIYAFUProtectFlashReq->ProtectFlashReq.Seqnum));
  1400. }
  1401. if(access("/var/yafu_bios_update_selection", F_OK) != 0)
  1402. {
  1403. if(g_corefeatures.dual_image_support == ENABLED)
  1404. {
  1405. if( g_FlashingImage == IMAGE_2 )
  1406. StartOffset = g_coremacros.global_used_flash_size;
  1407. }
  1408. }
  1409. fd=open(gAMIYAFUSwitchFlashDevice.MTDName, O_RDWR);
  1410. if (fd == -1) {
  1411. LastStatCode = YAFU_CC_DEV_OPEN_ERR;
  1412. printf("ERROR: open failed (%s)\n", strerror(errno));
  1413. return (AMIYAFUNotAcks(pRes,YAFU_CC_DEV_OPEN_ERR,pAMIYAFUProtectFlashReq->ProtectFlashReq.Seqnum));
  1414. }
  1415. if(pAMIYAFUProtectFlashReq->Blknum == 0xffffffff)
  1416. {
  1417. erase_info.start = (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashAddress);
  1418. erase_info.length = (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashSize);
  1419. }
  1420. else
  1421. {
  1422. offset = StartOffset + ( ((pAMIYAFUProtectFlashReq->Blknum)-1) * (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize) );
  1423. erase_info.start = offset;
  1424. erase_info.length = (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashSize);
  1425. }
  1426. if(pAMIYAFUProtectFlashReq->Protect == 0x01)
  1427. {
  1428. if((ioctl (fd, MEMLOCK, &erase_info)) != -1)
  1429. pAMIYAFUProtectFlash->Status = 0x00;
  1430. else
  1431. pAMIYAFUProtectFlash->Status = 0x01;
  1432. }
  1433. else if(pAMIYAFUProtectFlashReq->Protect == 0x00)
  1434. {
  1435. if((ioctl (fd, MEMUNLOCK, &erase_info)) != -1)
  1436. pAMIYAFUProtectFlash->Status = 0x00;
  1437. else
  1438. pAMIYAFUProtectFlash->Status = 0x01;
  1439. }
  1440. pAMIYAFUProtectFlash->CompletionCode = YAFU_CC_NORMAL;
  1441. LastStatCode = pAMIYAFUProtectFlash->CompletionCode;
  1442. pAMIYAFUProtectFlash->ProtectFlashRes.Seqnum = pAMIYAFUProtectFlashReq->ProtectFlashReq.Seqnum;
  1443. pAMIYAFUProtectFlash->ProtectFlashRes.YafuCmd= pAMIYAFUProtectFlashReq->ProtectFlashReq.YafuCmd;
  1444. pAMIYAFUProtectFlash->ProtectFlashRes.CRC32chksum = CalculateChksum((char *)&(pAMIYAFUProtectFlash->Status),sizeof(INT8U));
  1445. close(fd);
  1446. return (sizeof(AMIYAFUProtectFlashRes_T));
  1447. }
  1448. else
  1449. {
  1450. LastStatCode = YAFU_CC_IN_DEACTIVATE;
  1451. return (AMIYAFUNotAcks(pRes,YAFU_CC_IN_DEACTIVATE,pAMIYAFUProtectFlashReq->ProtectFlashReq.Seqnum));
  1452. }
  1453. }
  1454. void* EraseCopyFlash (void *pArg)
  1455. {
  1456. int fd;
  1457. INT32U i=0,j;
  1458. INT8U *Buf = NULL;
  1459. INT8U *TmpBuf = NULL;
  1460. struct erase_info_user erase_info;
  1461. DWORD StartOffset = 0;
  1462. void *dl_handle = NULL;
  1463. int (*dl_func)(int, unsigned long, INT8U*, unsigned long, INT32U);
  1464. int retval = 0;
  1465. int ErrCode = 0;
  1466. INT8U FlashError=0;
  1467. prctl(PR_SET_NAME,__FUNCTION__,0,0,0);
  1468. while(1)
  1469. {
  1470. ECFthreadstate=1;
  1471. if(ECFstart==1)
  1472. {
  1473. if(access("/var/yafu_bios_update_selection", F_OK) != 0)
  1474. {
  1475. if(g_corefeatures.dual_image_support == ENABLED)
  1476. {
  1477. if( g_FlashingImage == IMAGE_2 )
  1478. StartOffset = g_coremacros.global_used_flash_size;
  1479. }
  1480. }
  1481. // This hook can be used to perform any OEM flashing routines
  1482. // Returning 0 from this hook will revert back to performing default flashing sequence
  1483. // Returning <non-zero> from this hook will validate the ErrCode from the hook, and return appropriately. Default action will be skipped
  1484. // BMCInst is not required for this hook, because flashing can be done by only 1 instance at a time.
  1485. if (g_PDKHandle[PDK_OEMFLASH] != NULL)
  1486. {
  1487. retval = ((int(*)(int, INT8U*, unsigned long, int*))g_PDKHandle[PDK_OEMFLASH])
  1488. (gDeviceNode, WriteMemOff, Sizetocpy, &ErrCode);
  1489. if (retval != 0)
  1490. {
  1491. if (ErrCode != 0)
  1492. {
  1493. LastStatCode = YAFU_FLASH_ERASE_FAILURE;
  1494. }
  1495. else
  1496. {
  1497. LastStatCode = YAFU_ECF_SUCCESS;
  1498. }
  1499. ECFstart=0;
  1500. continue;
  1501. }
  1502. }
  1503. fd=open(gAMIYAFUSwitchFlashDevice.MTDName, O_RDWR);
  1504. if (fd == -1)
  1505. {
  1506. LastStatCode = YAFU_CC_DEV_OPEN_ERR;
  1507. printf("ERROR: open failed (%s)\n", strerror(errno));
  1508. return (void*)0;
  1509. }
  1510. if(access("/var/bios_update_selection", F_OK) != 0)
  1511. {
  1512. FlashError = 0;
  1513. // Loop through the entire file size. Each iteration will handle a block of data
  1514. // The block is set to Erase Block Size configured in the PRJ
  1515. for(j=0;j < (Sizetocpy/(gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize));j++)
  1516. {
  1517. // Allocate a buffer equivalent to the Erase Block Size configured in the PRJ
  1518. Buf = malloc((gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize));
  1519. if(Buf == NULL)
  1520. {
  1521. OS_THREAD_MUTEX_ACQUIRE(&YafuThreadMutex,WAIT_INFINITE);
  1522. LastStatCode = YAFU_CC_ALLOC_ERR;
  1523. OS_THREAD_MUTEX_RELEASE(&YafuThreadMutex);
  1524. ECFstart=0;
  1525. FlashError = 1;
  1526. break;
  1527. }
  1528. // Assign the allocated buffer to a temporary pointer
  1529. TmpBuf = Buf;
  1530. // Seek to the required offset in the MTD Device
  1531. if ( lseek (fd,StartOffset + Flashoffset, SEEK_SET) == -1)
  1532. {
  1533. OS_THREAD_MUTEX_ACQUIRE(&YafuThreadMutex,WAIT_INFINITE);
  1534. LastStatCode = YAFU_CC_SEEK_ERR;
  1535. OS_THREAD_MUTEX_RELEASE(&YafuThreadMutex);
  1536. fprintf (stderr,
  1537. "seek error on %s: %s\n",
  1538. gAMIYAFUSwitchFlashDevice.MTDName, strerror (errno));
  1539. ECFstart=0;
  1540. FlashError = 1;
  1541. free(Buf);
  1542. break;
  1543. }
  1544. // Read a block of data from the MTD Device.
  1545. if( (read(fd,Buf,(gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize))) != (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize))
  1546. {
  1547. OS_THREAD_MUTEX_ACQUIRE(&YafuThreadMutex,WAIT_INFINITE);
  1548. LastStatCode= YAFU_CC_READ_ERR;
  1549. OS_THREAD_MUTEX_RELEASE(&YafuThreadMutex);
  1550. fprintf (stderr,
  1551. "Bytes read error %s: %s\n",
  1552. gAMIYAFUSwitchFlashDevice.MTDName, strerror (errno));
  1553. ECFstart=0;
  1554. FlashError = 1;
  1555. free(Buf);
  1556. break;
  1557. }
  1558. // Compares the read buffer block with a block of data read from the memory.
  1559. // Previously it would compare the entire block in a byte-by-byte manner
  1560. // Now, we have modified it to use memcmp() on a smaller chunk in the block.
  1561. // The chunk size has been defined as 1024 Bytes.
  1562. // Smaller chunks is to allow smaller processing time for each iteration
  1563. for(i=0;i<(gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize / YAFU_FLASH_CHUNK);i++)
  1564. {
  1565. if (memcmp(Buf, WriteMemOff, YAFU_FLASH_CHUNK) != 0)
  1566. {
  1567. WriteMemOff = WriteMemOff - (i * YAFU_FLASH_CHUNK);
  1568. break;
  1569. }
  1570. Buf += YAFU_FLASH_CHUNK;
  1571. WriteMemOff += YAFU_FLASH_CHUNK;
  1572. }
  1573. // Comparing the buffer has been completed. Now, free the allocated buffer
  1574. Buf = TmpBuf;
  1575. free (Buf);
  1576. // If the buffers were same for the entire block, then we would skip that block. Continue with the next block again
  1577. // If the buffers were found different in the memcmp(), then we would proceed to flash that buffer into the flash device
  1578. if (i == (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize / YAFU_FLASH_CHUNK))
  1579. {
  1580. printf("Skipping the erase block %ld\n",(Flashoffset / (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize)));
  1581. Flashoffset += (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize);
  1582. continue;
  1583. }
  1584. // This stage starts the erase/write cycle on the flash device for each block
  1585. // Fill the erase structure with the flash device offset and the erase block size
  1586. erase_info.start = StartOffset + Flashoffset;
  1587. printf("Upgrading the block = %ld\n",(Flashoffset / (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize)));
  1588. erase_info.length = (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize);
  1589. // Erase the offset using the file descriptor ioctl.
  1590. if((ioctl (fd, MEMERASE, &erase_info)) == -1)
  1591. {
  1592. OS_THREAD_MUTEX_ACQUIRE(&YafuThreadMutex,WAIT_INFINITE);
  1593. LastStatCode = YAFU_FLASH_ERASE_FAILURE;
  1594. OS_THREAD_MUTEX_RELEASE(&YafuThreadMutex);
  1595. ECFstart=0;
  1596. FlashError = 1;
  1597. break;
  1598. }
  1599. // Seek to the appropriate offset
  1600. if ( lseek (fd, StartOffset + Flashoffset, SEEK_SET) == -1)
  1601. {
  1602. OS_THREAD_MUTEX_ACQUIRE(&YafuThreadMutex,WAIT_INFINITE);
  1603. LastStatCode = YAFU_CC_SEEK_ERR;
  1604. OS_THREAD_MUTEX_RELEASE(&YafuThreadMutex);
  1605. fprintf (stderr,
  1606. "seek error on %s: %s\n",
  1607. gAMIYAFUSwitchFlashDevice.MTDName, strerror (errno));
  1608. ECFstart=0;
  1609. FlashError = 1;
  1610. break;
  1611. }
  1612. // This code set loops through the block of data, and writes the block of data into the flash deviec
  1613. // The write into the flash device is done in smaller chunks instead of performing for the whole block in a single shot
  1614. // Previously, we would perform write() call on the whole block of data.
  1615. // On platforms with low SPI clock frequency, writing that big block of data was taking more time in the driver layer
  1616. // This was devoiding other interfaces from handling the commands quickly.
  1617. // Some OEMs required IPMB reponse within 110ms which was timing out during flashing.
  1618. // Writing in smaller chunks would bring back control to user space again, thereby giving some cpu time to otehr interface threads also
  1619. // With this change, the IPMB response was being procesed and sent within 110ms in 99% of the cases.
  1620. for (i = 0; i < (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize / YAFU_FLASH_CHUNK); i++)
  1621. {
  1622. if (write (fd, WriteMemOff, YAFU_FLASH_CHUNK) != YAFU_FLASH_CHUNK)
  1623. {
  1624. OS_THREAD_MUTEX_ACQUIRE(&YafuThreadMutex,WAIT_INFINITE);
  1625. LastStatCode = YAFU_CC_WRITE_ERR;
  1626. OS_THREAD_MUTEX_RELEASE(&YafuThreadMutex);
  1627. ECFstart=0;
  1628. FlashError = 1;
  1629. break;
  1630. }
  1631. WriteMemOff += YAFU_FLASH_CHUNK;
  1632. }
  1633. Flashoffset += (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize);
  1634. }
  1635. }
  1636. else
  1637. {
  1638. dl_handle = NULL;
  1639. dl_handle = dlopen((char *)PDKAPP_LIB, RTLD_LAZY);
  1640. if (dl_handle)
  1641. {
  1642. dl_func = dlsym(dl_handle,"PDK_YafuDoFullBiosFlash");
  1643. if(NULL != dl_func)
  1644. {
  1645. retval = dl_func(fd,Flashoffset,WriteMemOff,Sizetocpy, gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize);
  1646. if(retval != 0)
  1647. {
  1648. OS_THREAD_MUTEX_ACQUIRE(&YafuThreadMutex,WAIT_INFINITE);
  1649. LastStatCode = YAFU_FLASH_ERASE_FAILURE;
  1650. OS_THREAD_MUTEX_RELEASE(&YafuThreadMutex);
  1651. ECFstart=0;
  1652. }
  1653. }
  1654. else
  1655. {
  1656. FlashError = 0;
  1657. for(j=0;j < (Sizetocpy/(gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize));j++)
  1658. {
  1659. Buf = malloc((gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize));
  1660. if(Buf == NULL)
  1661. {
  1662. OS_THREAD_MUTEX_ACQUIRE(&YafuThreadMutex,WAIT_INFINITE);
  1663. LastStatCode = YAFU_CC_ALLOC_ERR;
  1664. OS_THREAD_MUTEX_RELEASE(&YafuThreadMutex);
  1665. ECFstart=0;
  1666. FlashError = 1;
  1667. break;
  1668. }
  1669. TmpBuf = Buf;
  1670. if ( lseek (fd, Flashoffset, SEEK_SET) == -1)
  1671. {
  1672. OS_THREAD_MUTEX_ACQUIRE(&YafuThreadMutex,WAIT_INFINITE);
  1673. LastStatCode = YAFU_CC_SEEK_ERR;
  1674. OS_THREAD_MUTEX_RELEASE(&YafuThreadMutex);
  1675. fprintf (stderr,
  1676. "seek error on %s: %s\n",
  1677. gAMIYAFUSwitchFlashDevice.MTDName, strerror (errno));
  1678. ECFstart=0;
  1679. FlashError = 1;
  1680. break;
  1681. }
  1682. if( (read(fd,Buf,(gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize))) != (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize))
  1683. {
  1684. OS_THREAD_MUTEX_ACQUIRE(&YafuThreadMutex,WAIT_INFINITE);
  1685. LastStatCode= YAFU_CC_READ_ERR;
  1686. OS_THREAD_MUTEX_RELEASE(&YafuThreadMutex);
  1687. fprintf (stderr,
  1688. "Bytes read error %s: %s\n",
  1689. gAMIYAFUSwitchFlashDevice.MTDName, strerror (errno));
  1690. ECFstart=0;
  1691. FlashError = 1;
  1692. break;
  1693. }
  1694. for (i = 0; i < (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize / YAFU_FLASH_CHUNK); i++)
  1695. {
  1696. if (memcmp(Buf, WriteMemOff, YAFU_FLASH_CHUNK) != 0)
  1697. {
  1698. WriteMemOff = WriteMemOff - (i * YAFU_FLASH_CHUNK);
  1699. break;
  1700. }
  1701. Buf += YAFU_FLASH_CHUNK;
  1702. WriteMemOff += YAFU_FLASH_CHUNK;
  1703. }
  1704. Buf = TmpBuf;
  1705. free (Buf);
  1706. if (i == (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize / YAFU_FLASH_CHUNK))
  1707. {
  1708. printf("Skipping the erase block %ld\n",
  1709. (Flashoffset / (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize)));
  1710. Flashoffset += (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize);
  1711. continue;
  1712. }
  1713. erase_info.start =Flashoffset;
  1714. printf("Upgrading the block = %ld\n",(Flashoffset / (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize)));
  1715. erase_info.length = (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize);
  1716. if((ioctl (fd, MEMERASE, &erase_info)) == -1)
  1717. {
  1718. OS_THREAD_MUTEX_ACQUIRE(&YafuThreadMutex,WAIT_INFINITE);
  1719. LastStatCode = YAFU_FLASH_ERASE_FAILURE;
  1720. OS_THREAD_MUTEX_RELEASE(&YafuThreadMutex);
  1721. ECFstart=0;
  1722. FlashError = 1;
  1723. break;
  1724. }
  1725. if ( lseek (fd,Flashoffset, SEEK_SET) == -1)
  1726. {
  1727. OS_THREAD_MUTEX_ACQUIRE(&YafuThreadMutex,WAIT_INFINITE);
  1728. LastStatCode = YAFU_CC_SEEK_ERR;
  1729. OS_THREAD_MUTEX_RELEASE(&YafuThreadMutex);
  1730. fprintf (stderr,
  1731. "seek error on %s: %s\n",
  1732. gAMIYAFUSwitchFlashDevice.MTDName, strerror (errno));
  1733. ECFstart=0;
  1734. FlashError = 1;
  1735. break;
  1736. }
  1737. for (i = 0; i < (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize / YAFU_FLASH_CHUNK); i++)
  1738. {
  1739. if (write (fd, WriteMemOff, YAFU_FLASH_CHUNK) != YAFU_FLASH_CHUNK)
  1740. {
  1741. OS_THREAD_MUTEX_ACQUIRE(&YafuThreadMutex,WAIT_INFINITE);
  1742. LastStatCode = YAFU_CC_WRITE_ERR;
  1743. OS_THREAD_MUTEX_RELEASE(&YafuThreadMutex);
  1744. ECFstart=0;
  1745. FlashError = 1;
  1746. break;
  1747. }
  1748. WriteMemOff += YAFU_FLASH_CHUNK;
  1749. }
  1750. Flashoffset += (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize);
  1751. }
  1752. }
  1753. dlclose(dl_handle);
  1754. }
  1755. }
  1756. close(fd);
  1757. ECFstart=0;
  1758. if (FlashError == 0)
  1759. {
  1760. OS_THREAD_MUTEX_ACQUIRE(&YafuThreadMutex, WAIT_INFINITE);
  1761. LastStatCode = YAFU_ECF_SUCCESS;
  1762. OS_THREAD_MUTEX_RELEASE(&YafuThreadMutex);
  1763. }
  1764. }
  1765. else
  1766. {
  1767. pthread_mutex_lock(&mutex);
  1768. pthread_cond_wait(&cond, &mutex);
  1769. pthread_mutex_unlock(&mutex);
  1770. }
  1771. }
  1772. }
  1773. int CheckImageSign()
  1774. {
  1775. INT8U *HashString = NULL,*pHash = NULL,*AllocatedMem = NULL;
  1776. static RSA *RSAPubKey=NULL;
  1777. INT32U RemBytes = 0;
  1778. FILE *pubfp = NULL;
  1779. int nRet = 0;
  1780. unsigned char strHashOfImg[SHA_DIGEST_LENGTH]={0};
  1781. unsigned char strBuf[BUFSIZE]={0};
  1782. SHA_CTX c;
  1783. int Count = 0;
  1784. char str[SHA_DIGEST_LENGTH*2] = {0};
  1785. RemBytes = ImgSize % gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize;
  1786. if(RemBytes == SIGNED_HASH_SIZE) // Consider that the image is signed image
  1787. {
  1788. if (ENABLED == g_corefeatures.signed_hashed_image_support)
  1789. {
  1790. HashString = malloc(RemBytes);
  1791. /* Copy the Hash String from Image */
  1792. memcpy(HashString,allocmem+(ImgSize-RemBytes),RemBytes);
  1793. /* Open the Public Key File and get the Key first */
  1794. pubfp = fopen(TEMP_PUB_KEY,"rb");
  1795. if(pubfp==NULL)
  1796. {
  1797. IPMI_ERROR("Cannot open file public.pem\n");
  1798. free(HashString);
  1799. return -1;
  1800. }
  1801. if (!PEM_read_RSA_PUBKEY(pubfp, &RSAPubKey, NULL, NULL))
  1802. {
  1803. IPMI_ERROR("Error loading RSA Public Key File.\n");
  1804. free(HashString);
  1805. fclose(pubfp);
  1806. return -1;
  1807. }
  1808. fclose(pubfp);
  1809. pHash = malloc(RSA_size(RSAPubKey));
  1810. if(!(pHash))
  1811. {
  1812. IPMI_ERROR("Memory allocation failed!!\n");
  1813. free(HashString);
  1814. return -1;
  1815. }
  1816. memset(pHash,0,RemBytes);
  1817. nRet = RSA_public_decrypt(RemBytes, HashString, pHash,RSAPubKey,RSA_PKCS1_PADDING);
  1818. if(nRet==-1)
  1819. {
  1820. IPMI_ERROR("Decrypting the enc-hash failed with return code %d..\n",nRet);
  1821. free(pHash);
  1822. free(HashString);
  1823. return -1;
  1824. }
  1825. if(allocmem == NULL)
  1826. {
  1827. IPMI_ERROR("Decrypting the enc-hash failed with return code %d..\n",nRet);
  1828. free(pHash);
  1829. free(HashString);
  1830. return -1;
  1831. }
  1832. AllocatedMem = allocmem;
  1833. //returns 1 on success and 0 otherwise
  1834. if(SHA1_Init(&c)!=1)
  1835. {
  1836. free(pHash);
  1837. free(HashString);
  1838. return -1;
  1839. }
  1840. for(Count=0;Count<((ImgSize)/(BUFSIZE));Count++)
  1841. {
  1842. nRet = BUFSIZE;
  1843. memcpy(strBuf,AllocatedMem,nRet);
  1844. if(SHA1_Update(&c,strBuf,(unsigned long)nRet)!=1)
  1845. {
  1846. IPMI_ERROR("Decrypting the enc-hash failed with return code %d..\n",nRet);
  1847. free(pHash);
  1848. free(HashString);
  1849. return -1;
  1850. }
  1851. AllocatedMem += BUFSIZE;
  1852. }
  1853. memset(strHashOfImg,0,SHA_DIGEST_LENGTH);
  1854. if(SHA1_Final(strHashOfImg,&c)!=1)
  1855. {
  1856. IPMI_ERROR("Error in SHA1 Final\n");
  1857. free(pHash);
  1858. free(HashString);
  1859. return -1;
  1860. }
  1861. for(Count=0;Count<SHA_DIGEST_LENGTH;Count++)
  1862. {
  1863. if ( Count == 0)
  1864. {
  1865. sprintf(str,"%02x",strHashOfImg[Count]);
  1866. }
  1867. else
  1868. {
  1869. sprintf(str,"%s%02x",str,strHashOfImg[Count]);
  1870. }
  1871. TDBG("%x",strHashOfImg[Count]);
  1872. }
  1873. if(memcmp(str,(char *)pHash,(SHA_DIGEST_LENGTH*2))==0)
  1874. {
  1875. IPMI_ERROR("IMAGE IS VALID\n");
  1876. free(pHash);
  1877. free(HashString);
  1878. }
  1879. else
  1880. {
  1881. IPMI_ERROR("INVALID IMAGE\n");
  1882. free(pHash);
  1883. free(HashString);
  1884. return -1;
  1885. }
  1886. }
  1887. else
  1888. {
  1889. return -2;
  1890. }
  1891. }
  1892. return 0;
  1893. }
  1894. /*---------------------------------------
  1895. * AMIYAFUEraseCopyFlash
  1896. *---------------------------------------*/
  1897. int AMIYAFUEraseCopyFlash ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes,_NEAR_ int BMCInst )
  1898. {
  1899. AMIYAFUEraseCopyFlashReq_T *pAMIYAFUEraseCopyFlashReq = (AMIYAFUEraseCopyFlashReq_T *)pReq;
  1900. int Ret = 0;
  1901. if(ActivateFlashStatus == 0x01)
  1902. {
  1903. INT8U *MemOffset = NULL;
  1904. AMIYAFUEraseCopyFlashRes_T* pAMIYAFUEraseCopyFlash = (AMIYAFUEraseCopyFlashRes_T*)pRes;
  1905. if(CalculateChksum((char *)&pAMIYAFUEraseCopyFlashReq->Memoffset,pAMIYAFUEraseCopyFlashReq->EraseCpyFlashReq.Datalen) != pAMIYAFUEraseCopyFlashReq->EraseCpyFlashReq.CRC32chksum)
  1906. {
  1907. LastStatCode=YAFU_INVALID_CHKSUM;
  1908. return (AMIYAFUNotAcks(pRes,YAFU_INVALID_CHKSUM,pAMIYAFUEraseCopyFlashReq->EraseCpyFlashReq.Seqnum));
  1909. }
  1910. if(pAMIYAFUEraseCopyFlashReq->EraseCpyFlashReq.Datalen == 0x0c)
  1911. {
  1912. pAMIYAFUEraseCopyFlash->EraseCpyFlashRes.Datalen = 0x04;
  1913. }
  1914. else
  1915. {
  1916. LastStatCode = YAFU_CC_INVALID_DATLEN;
  1917. return (AMIYAFUNotAcks(pRes,YAFU_CC_INVALID_DATLEN,pAMIYAFUEraseCopyFlashReq->EraseCpyFlashReq.Seqnum));
  1918. }
  1919. MemOffset = (INT8U *) pAMIYAFUEraseCopyFlashReq->Memoffset;
  1920. if(ECFstart==0)
  1921. {
  1922. if(threadid == 0) // Thread is not Created Initialize the Mutex
  1923. {
  1924. OS_THREAD_MUTEX_INIT(YafuThreadMutex, PTHREAD_MUTEX_RECURSIVE);
  1925. Ret = CheckImageSign();
  1926. if(Ret == -1)
  1927. {
  1928. LastStatCode = YAFU_CC_INVALID_SIGN_IMAGE;
  1929. return (AMIYAFUNotAcks(pRes,YAFU_CC_INVALID_SIGN_IMAGE,pAMIYAFUEraseCopyFlashReq->EraseCpyFlashReq.Seqnum));
  1930. }
  1931. else if(Ret == -2)
  1932. {
  1933. LastStatCode = YAFU_CC_SIGNED_SUPP_NOT_ENABLED;
  1934. return (AMIYAFUNotAcks(pRes,YAFU_CC_SIGNED_SUPP_NOT_ENABLED,pAMIYAFUEraseCopyFlashReq->EraseCpyFlashReq.Seqnum));
  1935. }
  1936. }
  1937. WriteMemOff=MemOffset;
  1938. Sizetocpy=pAMIYAFUEraseCopyFlashReq->Sizetocopy;
  1939. Flashoffset=pAMIYAFUEraseCopyFlashReq->Flashoffset;
  1940. Fixoffset=pAMIYAFUEraseCopyFlashReq->Flashoffset;
  1941. if(access("/var/bios_update_selection", F_OK) == 0)
  1942. {
  1943. int MTDDev;
  1944. MTDDev=open(gAMIYAFUSwitchFlashDevice.MTDName, O_RDWR);
  1945. if (MTDDev == -1)
  1946. {
  1947. LastStatCode = YAFU_CC_DEV_OPEN_ERR;
  1948. printf("ERROR: open failed (%s)\n", strerror(errno));
  1949. return (AMIYAFUNotAcks(pRes,YAFU_CC_DEV_OPEN_ERR,pAMIYAFUEraseCopyFlashReq->EraseCpyFlashReq.Seqnum));
  1950. }
  1951. if(YafuVerifyBiosImage(MTDDev, Flashoffset, WriteMemOff, Sizetocpy, gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize) != 0 )
  1952. {
  1953. LastStatCode = YAFU_CC_WRITE_ERR;
  1954. close(MTDDev);
  1955. return (AMIYAFUNotAcks(pRes,YAFU_CC_WRITE_ERR,pAMIYAFUEraseCopyFlashReq->EraseCpyFlashReq.Seqnum));
  1956. }
  1957. close(MTDDev);
  1958. }
  1959. ECFstart=1;
  1960. OS_THREAD_MUTEX_ACQUIRE(&YafuThreadMutex, WAIT_INFINITE);
  1961. if(ECFthreadstate==0)
  1962. {
  1963. //OS_CREATE_THREAD (EraseCopyFlash, NULL, NULL);
  1964. pthread_create(&threadid, NULL, EraseCopyFlash, NULL);
  1965. }
  1966. else
  1967. {
  1968. pthread_mutex_lock(&mutex);
  1969. pthread_cond_broadcast(&cond);
  1970. pthread_mutex_unlock(&mutex);
  1971. }
  1972. pAMIYAFUEraseCopyFlash->CompletionCode = YAFU_CC_NORMAL;
  1973. }
  1974. else
  1975. {
  1976. pAMIYAFUEraseCopyFlash->CompletionCode = YAFU_ECF_PROGRESS;
  1977. }
  1978. LastStatCode = (INT16U)pAMIYAFUEraseCopyFlash->CompletionCode;
  1979. OS_THREAD_MUTEX_RELEASE(&YafuThreadMutex);
  1980. pAMIYAFUEraseCopyFlash->Sizecopied = pAMIYAFUEraseCopyFlashReq->Sizetocopy;
  1981. pAMIYAFUEraseCopyFlash->EraseCpyFlashRes.Seqnum = pAMIYAFUEraseCopyFlashReq->EraseCpyFlashReq.Seqnum;
  1982. pAMIYAFUEraseCopyFlash->EraseCpyFlashRes.YafuCmd= pAMIYAFUEraseCopyFlashReq->EraseCpyFlashReq.YafuCmd;
  1983. pAMIYAFUEraseCopyFlash->EraseCpyFlashRes.CRC32chksum = CalculateChksum((char *)&(pAMIYAFUEraseCopyFlash->Sizecopied),sizeof(INT32U));
  1984. return (sizeof(AMIYAFUEraseCopyFlashRes_T));
  1985. }
  1986. else
  1987. {
  1988. LastStatCode = YAFU_CC_IN_DEACTIVATE;
  1989. return (AMIYAFUNotAcks(pRes,YAFU_CC_IN_DEACTIVATE,pAMIYAFUEraseCopyFlashReq->EraseCpyFlashReq.Seqnum));
  1990. }
  1991. }
  1992. int AMIYAFUGetECFStatus( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes,_NEAR_ int BMCInst )
  1993. {
  1994. AMIYAFUGetECFStatusReq_T *pAMIYAFUGetECFStatusReq = (AMIYAFUGetECFStatusReq_T *)pReq;
  1995. AMIYAFUGetECFStatusRes_T* pAMIYAFUGetECFStatus = (AMIYAFUGetECFStatusRes_T*)pRes;
  1996. if ((LastStatCode != YAFU_CC_NORMAL) && (LastStatCode != YAFU_ECF_SUCCESS))
  1997. {
  1998. return (AMIYAFUNotAcks(pRes,LastStatCode,pAMIYAFUGetECFStatusReq->GetECFStatusReq.Seqnum));
  1999. }
  2000. pAMIYAFUGetECFStatus->CompletionCode=YAFU_CC_NORMAL;
  2001. pAMIYAFUGetECFStatus->Status =(INT16U) LastStatCode;
  2002. pAMIYAFUGetECFStatus->Progress= ((Flashoffset-Fixoffset)*100)/(Sizetocpy);
  2003. pAMIYAFUGetECFStatus->GetECFStatusRes.Datalen=0x00;
  2004. pAMIYAFUGetECFStatus->GetECFStatusRes.Seqnum = pAMIYAFUGetECFStatusReq->GetECFStatusReq.Seqnum;
  2005. pAMIYAFUGetECFStatus->GetECFStatusRes.YafuCmd= pAMIYAFUGetECFStatusReq->GetECFStatusReq.YafuCmd;
  2006. pAMIYAFUGetECFStatus->GetECFStatusRes.CRC32chksum = CalculateChksum((char *)&pAMIYAFUGetECFStatus->Status,(INT32U)pAMIYAFUGetECFStatus->GetECFStatusRes.Datalen);
  2007. return ( sizeof( AMIYAFUGetECFStatusRes_T ) );
  2008. }
  2009. void* VerifyFlashStatus (void *pArg)
  2010. {
  2011. int fd;
  2012. INT32U i,j;
  2013. INT8U *FlashBuf = NULL;
  2014. INT8U *TmpFlashBuf = NULL;
  2015. DWORD StartOffset = 0;
  2016. int retval = 0;
  2017. int ErrCode = 0;
  2018. INT8U VerifyError=0;
  2019. prctl(PR_SET_NAME,__FUNCTION__,0,0,0);
  2020. while(1)
  2021. {
  2022. Vthreadstate=1;
  2023. if(Vstart==1)
  2024. {
  2025. if(access("/var/yafu_bios_update_selection", F_OK) != 0)
  2026. {
  2027. if(g_corefeatures.dual_image_support == ENABLED)
  2028. {
  2029. if( g_FlashingImage == IMAGE_2 )
  2030. StartOffset = g_coremacros.global_used_flash_size;
  2031. }
  2032. }
  2033. // This hook can be used to perform any OEM verification routines
  2034. // Returning 0 from this hook will revert back to performing default verification sequence
  2035. // Returning <non-zero> from this hook will validate the ErrCode from the hook, and return appropriately. Default action will be skipped
  2036. // BMCInst is not required for this hook, because flashing can be done by only 1 instance at a time.
  2037. if (g_PDKHandle[PDK_OEMVERIFY] != NULL)
  2038. {
  2039. retval = ((int(*)(int, INT8U*, unsigned long, int*))g_PDKHandle[PDK_OEMVERIFY])
  2040. (gDeviceNode, VWriteMemOff, Sizetoverify, &ErrCode);
  2041. if (retval != 0)
  2042. {
  2043. if (ErrCode != 0)
  2044. {
  2045. LastStatCode = YAFU_VERIFY_FAILURE;
  2046. }
  2047. else
  2048. {
  2049. LastStatCode = YAFU_VERIFY_SUCCESS;
  2050. }
  2051. Vstart=0;
  2052. continue;
  2053. }
  2054. }
  2055. fd = open(gAMIYAFUSwitchFlashDevice.MTDName, O_RDONLY);
  2056. if (fd == -1) {
  2057. VerifyFlashStatusCode = YAFU_CC_DEV_OPEN_ERR;
  2058. printf("ERROR: open failed (%s)\n", strerror(errno));
  2059. return (void*)0;
  2060. }
  2061. VerifyError=0;
  2062. // Loop through the entire image size in memory. The verify would be handled in blocks of data, each erase block in size
  2063. for(j=0;j<(Sizetoverify/(gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize));j++)
  2064. {
  2065. // Allocate a buffer for storing the block of data to be read from flash. It will be erase block size
  2066. FlashBuf = malloc ((gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize));
  2067. if(FlashBuf == NULL)
  2068. {
  2069. VerifyFlashStatusCode = YAFU_CC_ALLOC_ERR;
  2070. Vstart=0;
  2071. VerifyError=1;
  2072. break;
  2073. }
  2074. // Assign it to a tempory pointer for future use
  2075. TmpFlashBuf = FlashBuf;
  2076. // Seek the MTD Device to the appropriate offset
  2077. if ( lseek (fd, StartOffset + VFlashoffset, SEEK_SET) == -1)
  2078. {
  2079. VerifyFlashStatusCode = YAFU_CC_SEEK_ERR;
  2080. fprintf (stderr,
  2081. "seek error on %s: %s\n",
  2082. gAMIYAFUSwitchFlashDevice.MTDName, strerror (errno));
  2083. Vstart=0;
  2084. free(FlashBuf);
  2085. VerifyError=1;
  2086. break;
  2087. }
  2088. // Read a full erase block of data from the flash device.
  2089. // Since read is faster than write, we can read a whole block of data in a single shot
  2090. if( (read(fd,FlashBuf,(gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize)))!= (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize))
  2091. {
  2092. VerifyFlashStatusCode = YAFU_CC_READ_ERR;
  2093. fprintf (stderr,
  2094. "Bytes read error %s: %s\n",
  2095. gAMIYAFUSwitchFlashDevice.MTDName, strerror (errno));
  2096. Vstart=0;
  2097. free(FlashBuf);
  2098. VerifyError=1;
  2099. break;
  2100. }
  2101. // Compares the read buffer block with a block of data read from the memory.
  2102. // Previously it would compare the entire block in a byte-by-byte manner
  2103. // Now, we have modified it to use memcmp() on a smaller chunk in the block.
  2104. // The chunk size has been defined as 1024 Bytes.
  2105. // Smaller chunks is to allow smaller processing time for each iteration
  2106. for (i = 0; i < (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize / YAFU_FLASH_CHUNK); i++)
  2107. {
  2108. if (memcmp(FlashBuf, VWriteMemOff, YAFU_FLASH_CHUNK) != 0)
  2109. {
  2110. VWriteMemOff = VWriteMemOff - (i * YAFU_FLASH_CHUNK);
  2111. break;
  2112. }
  2113. FlashBuf += YAFU_FLASH_CHUNK;
  2114. VWriteMemOff += YAFU_FLASH_CHUNK;
  2115. }
  2116. // If the data was found to be same between the data read from flash and the data in memory,
  2117. // then, it means the block is successfully written. So proceed with then block
  2118. // If the block was found different, then stop this process with an error, since verify failed
  2119. if (i == (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize / YAFU_FLASH_CHUNK))
  2120. {
  2121. VOffset = (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize);
  2122. }
  2123. else
  2124. {
  2125. TDBG("FlashBuf after verify: Flash = 0x%08lX Mem = 0x%08lX\n",(INT32U)FlashBuf, (INT32U)VWriteMemOff);
  2126. TDBG("Verify Failure \n");
  2127. VOffset = (INT32U )VWriteMemOff;
  2128. VerifyError = 1;
  2129. VerifyFlashStatusCode = YAFU_INVALID_CHKSUM;
  2130. break;
  2131. }
  2132. FlashBuf = TmpFlashBuf;
  2133. free (FlashBuf);
  2134. VFlashoffset += (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize);
  2135. }
  2136. close(fd);
  2137. Vstart=0;
  2138. if (VerifyError==0)
  2139. {
  2140. VerifyFlashStatusCode = YAFU_VERIFY_SUCCESS;
  2141. }
  2142. // This hook can be used to perform an OEM actions if required upon completion of verification phase
  2143. // This will be the last access point before the flashing sequence exits
  2144. // PDK_OEMSaveFMH has not been added separately. Instead the FMH can be saved from within this POST_VERIFY hook itself
  2145. // BMCInst is not required for this hook, because flashing can be done by only 1 instance at a time.
  2146. retval = 0;
  2147. if (g_PDKHandle[PDK_POST_VERIFY] != NULL)
  2148. {
  2149. retval = ((int(*)(int, INT8U, int))g_PDKHandle[PDK_POST_VERIFY]) (gDeviceNode, VerifyError, VerifyFlashStatusCode);
  2150. if (retval != 0)
  2151. {
  2152. TCRIT("Error in handling the Post Verify OEM Sequence\n");
  2153. }
  2154. }
  2155. }
  2156. else
  2157. {
  2158. pthread_mutex_lock(&mutexv);
  2159. pthread_cond_wait(&condv, &mutexv);
  2160. pthread_mutex_unlock(&mutexv);
  2161. }
  2162. }
  2163. }
  2164. /*---------------------------------------
  2165. * AMIYAFUVerifyFlash
  2166. *---------------------------------------*/
  2167. int AMIYAFUVerifyFlash ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes,_NEAR_ int BMCInst )
  2168. {
  2169. AMIYAFUVerifyFlashReq_T *pAMIYAFUVerifyFlashReq = (AMIYAFUVerifyFlashReq_T *)pReq;
  2170. if(ActivateFlashStatus == 0x01)
  2171. {
  2172. INT8U *MemOffset;
  2173. AMIYAFUVerifyFlashRes_T* pAMIYAFUVerfyFlash = (AMIYAFUVerifyFlashRes_T*)pRes;
  2174. if(CalculateChksum((char *)&pAMIYAFUVerifyFlashReq->Memoffset,(3 * sizeof(INT32U))) != pAMIYAFUVerifyFlashReq->VerifyFlashReq.CRC32chksum)
  2175. {
  2176. LastStatCode=YAFU_INVALID_CHKSUM;
  2177. printf("Error in verify flash \n");
  2178. return (AMIYAFUNotAcks(pRes,YAFU_INVALID_CHKSUM,pAMIYAFUVerifyFlashReq->VerifyFlashReq.Seqnum));
  2179. }
  2180. if(pAMIYAFUVerifyFlashReq->VerifyFlashReq.Datalen== 0x0c)
  2181. pAMIYAFUVerfyFlash->VerifyFlashRes.Datalen= 0x04;
  2182. else
  2183. {
  2184. LastStatCode = YAFU_CC_INVALID_DATLEN;
  2185. return (AMIYAFUNotAcks(pRes,YAFU_CC_INVALID_DATLEN,pAMIYAFUVerifyFlashReq->VerifyFlashReq.Seqnum));
  2186. }
  2187. /* wait until Erase Copy Flash to finish */
  2188. while(ECFstart!=0)
  2189. {
  2190. // fprintf(stderr,"\nErase Copy Flash in Progress %d ",ECFstart);
  2191. }
  2192. MemOffset = (INT8U *)pAMIYAFUVerifyFlashReq->Memoffset;
  2193. if(Vstart==0)
  2194. {
  2195. VWriteMemOff=MemOffset;
  2196. Sizetoverify=pAMIYAFUVerifyFlashReq->Sizetoverify;
  2197. VFlashoffset=pAMIYAFUVerifyFlashReq->Flashoffset;
  2198. VFixoffset=pAMIYAFUVerifyFlashReq->Flashoffset;
  2199. Vstart=1;
  2200. VerifyFlashStatusCode = 0x00;
  2201. if(Vthreadstate==0)
  2202. {
  2203. //OS_CREATE_THREAD (EraseCopyFlash, NULL, NULL);
  2204. pthread_create(&threadidv, NULL, VerifyFlashStatus, NULL);
  2205. }
  2206. else
  2207. {
  2208. pthread_mutex_lock(&mutexv);
  2209. pthread_cond_broadcast(&condv);
  2210. pthread_mutex_unlock(&mutexv);
  2211. }
  2212. pAMIYAFUVerfyFlash->CompletionCode = YAFU_CC_NORMAL;
  2213. }
  2214. else
  2215. {
  2216. pAMIYAFUVerfyFlash->CompletionCode = YAFU_VERIFY_PROGRESS;
  2217. }
  2218. pAMIYAFUVerfyFlash->Offset = (gAMIYAFUSwitchFlashDevice.FlashInfo.FlashEraseBlkSize);
  2219. LastStatCode = (INT16U)pAMIYAFUVerfyFlash->CompletionCode;
  2220. pAMIYAFUVerfyFlash->VerifyFlashRes.Seqnum = pAMIYAFUVerifyFlashReq->VerifyFlashReq.Seqnum;
  2221. pAMIYAFUVerfyFlash->VerifyFlashRes.YafuCmd= pAMIYAFUVerifyFlashReq->VerifyFlashReq.YafuCmd;
  2222. pAMIYAFUVerfyFlash->VerifyFlashRes.CRC32chksum = CalculateChksum((char *)&(pAMIYAFUVerfyFlash->Offset),sizeof(INT32U));
  2223. return (sizeof(AMIYAFUVerifyFlashRes_T));
  2224. }
  2225. else
  2226. {
  2227. LastStatCode = YAFU_CC_IN_DEACTIVATE;
  2228. return (AMIYAFUNotAcks(pRes,YAFU_CC_IN_DEACTIVATE,pAMIYAFUVerifyFlashReq->VerifyFlashReq.Seqnum));
  2229. }
  2230. }
  2231. int AMIYAFUGetVerifyStatus ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes,_NEAR_ int BMCInst )
  2232. {
  2233. AMIYAFUGetVerifyStatusReq_T *pAMIYAFUGetVerifyStatusReq = (AMIYAFUGetVerifyStatusReq_T *)pReq;
  2234. AMIYAFUGetVerifyStatusRes_T* pAMIYAFUGetVerifyStatus = (AMIYAFUGetVerifyStatusRes_T*)pRes;
  2235. if(LastStatCode!=YAFU_CC_NORMAL&&LastStatCode!=YAFU_VERIFY_SUCCESS)
  2236. {
  2237. return (AMIYAFUNotAcks(pRes,LastStatCode,pAMIYAFUGetVerifyStatusReq->GetVerifyStatusReq.Seqnum));
  2238. }
  2239. pAMIYAFUGetVerifyStatus->CompletionCode=YAFU_CC_NORMAL;
  2240. pAMIYAFUGetVerifyStatus->Status =(INT16U) VerifyFlashStatusCode;
  2241. pAMIYAFUGetVerifyStatus->Offset=VOffset;
  2242. pAMIYAFUGetVerifyStatus->Progress= ((VFlashoffset-VFixoffset)*100)/(Sizetoverify);
  2243. pAMIYAFUGetVerifyStatus->GetVerifyStatusRes.Datalen=0x00;
  2244. pAMIYAFUGetVerifyStatus->GetVerifyStatusRes.Seqnum = pAMIYAFUGetVerifyStatusReq->GetVerifyStatusReq.Seqnum;
  2245. pAMIYAFUGetVerifyStatus->GetVerifyStatusRes.YafuCmd= pAMIYAFUGetVerifyStatusReq->GetVerifyStatusReq.YafuCmd;
  2246. pAMIYAFUGetVerifyStatus->GetVerifyStatusRes.CRC32chksum = CalculateChksum((char *)&(pAMIYAFUGetVerifyStatus->Offset),sizeof(INT32U));
  2247. return ( sizeof( AMIYAFUGetVerifyStatusRes_T ) );
  2248. }
  2249. /*---------------------------------------
  2250. * AMIYAFUReadMemory
  2251. *---------------------------------------*/
  2252. int AMIYAFUReadMemory ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes,_NEAR_ int BMCInst )
  2253. {
  2254. AMIYAFUReadMemoryReq_T *pAMIYAFUReadMemoryReq =(AMIYAFUReadMemoryReq_T *)pReq;
  2255. if(ActivateFlashStatus == 0x01)
  2256. {
  2257. INT32U *offsetoread;
  2258. INT8U *Buf = 0;
  2259. AMIYAFUReadMemoryRes_T* pAMIYAFUReadMemory = (AMIYAFUReadMemoryRes_T*)pRes;
  2260. if(CalculateChksum((char *)&pAMIYAFUReadMemoryReq->Memoffset,pAMIYAFUReadMemoryReq->ReadMemReq.Datalen) != pAMIYAFUReadMemoryReq->ReadMemReq.CRC32chksum)
  2261. {
  2262. LastStatCode=YAFU_INVALID_CHKSUM;
  2263. return (AMIYAFUNotAcks(pRes,YAFU_INVALID_CHKSUM,pAMIYAFUReadMemoryReq->ReadMemReq.Seqnum));
  2264. }
  2265. if(pAMIYAFUReadMemoryReq->ReadMemReq.Datalen != 0x07)
  2266. {
  2267. LastStatCode = YAFU_CC_INVALID_DATLEN;
  2268. return (AMIYAFUNotAcks(pRes,YAFU_CC_INVALID_DATLEN,pAMIYAFUReadMemoryReq->ReadMemReq.Seqnum));
  2269. }
  2270. offsetoread = (INT32U *)pAMIYAFUReadMemoryReq->Memoffset;
  2271. Buf = malloc(pAMIYAFUReadMemoryReq->Sizetoread);
  2272. if(Buf == NULL)
  2273. {
  2274. LastStatCode = YAFU_CC_ALLOC_ERR;
  2275. printf("Unable to allocate memory/n");
  2276. return (AMIYAFUNotAcks(pRes,YAFU_CC_ALLOC_ERR,pAMIYAFUReadMemoryReq->ReadMemReq.Seqnum));
  2277. }
  2278. memcpy (( INT8U*) Buf,
  2279. ( INT8U*)offsetoread,
  2280. pAMIYAFUReadMemoryReq->Sizetoread );
  2281. memcpy (( INT8U*) (pAMIYAFUReadMemory + 1),
  2282. ( INT8U*)Buf,
  2283. pAMIYAFUReadMemoryReq->Sizetoread );
  2284. pAMIYAFUReadMemory->CompletionCode = YAFU_CC_NORMAL;
  2285. LastStatCode = (INT16U)pAMIYAFUReadMemory->CompletionCode;
  2286. pAMIYAFUReadMemory->ReadMemRes.Seqnum = pAMIYAFUReadMemoryReq->ReadMemReq.Seqnum;
  2287. pAMIYAFUReadMemory->ReadMemRes.Datalen= pAMIYAFUReadMemoryReq->Sizetoread;
  2288. pAMIYAFUReadMemory->ReadMemRes.CRC32chksum = CalculateChksum((char *)Buf,pAMIYAFUReadMemoryReq->Sizetoread);
  2289. free(Buf);
  2290. return (sizeof(AMIYAFUReadMemoryRes_T) + pAMIYAFUReadMemoryReq->Sizetoread );
  2291. }
  2292. else
  2293. {
  2294. LastStatCode = YAFU_CC_IN_DEACTIVATE;
  2295. return (AMIYAFUNotAcks(pRes,YAFU_CC_IN_DEACTIVATE,pAMIYAFUReadMemoryReq->ReadMemReq.Seqnum));
  2296. }
  2297. }
  2298. /*---------------------------------------
  2299. * AMIYAFUWriteMemory
  2300. *---------------------------------------*/
  2301. int AMIYAFUWriteMemory ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes,_NEAR_ int BMCInst )
  2302. {
  2303. AMIYAFUWriteMemoryReq_T *pAMIYAFUWriteMemoryReq = (AMIYAFUWriteMemoryReq_T *)pReq;
  2304. if(ActivateFlashStatus == 0x01)
  2305. {
  2306. INT8U *OffsetToWrite;
  2307. AMIYAFUWriteMemoryRes_T* pAMIYAFUWriteMemory = (AMIYAFUWriteMemoryRes_T*)pRes;
  2308. if(CalculateChksum((char *)&pAMIYAFUWriteMemoryReq->Memoffset,pAMIYAFUWriteMemoryReq->WriteMemReq.Datalen) != pAMIYAFUWriteMemoryReq->WriteMemReq.CRC32chksum)
  2309. {
  2310. LastStatCode=YAFU_INVALID_CHKSUM;
  2311. printf("Error in checksum of write memory \n");
  2312. return (AMIYAFUNotAcks(pRes,YAFU_INVALID_CHKSUM,pAMIYAFUWriteMemoryReq->WriteMemReq.Seqnum));
  2313. }
  2314. OffsetToWrite = (INT8U *)pAMIYAFUWriteMemoryReq->Memoffset;
  2315. memcpy (( INT8U*) OffsetToWrite,
  2316. (pReq + sizeof(AMIYAFUWriteMemoryReq_T)),
  2317. (pAMIYAFUWriteMemoryReq->WriteMemReq.Datalen - 5));
  2318. pAMIYAFUWriteMemory->SizeWritten = (pAMIYAFUWriteMemoryReq->WriteMemReq.Datalen - 5);
  2319. pAMIYAFUWriteMemory->CompletionCode = YAFU_CC_NORMAL;
  2320. LastStatCode = (INT16U) pAMIYAFUWriteMemory->CompletionCode;
  2321. pAMIYAFUWriteMemory->WriteMemRes.Seqnum= pAMIYAFUWriteMemoryReq->WriteMemReq.Seqnum;
  2322. pAMIYAFUWriteMemory->WriteMemRes.YafuCmd= pAMIYAFUWriteMemoryReq->WriteMemReq.YafuCmd;
  2323. pAMIYAFUWriteMemory->WriteMemRes.Datalen=0x02;
  2324. pAMIYAFUWriteMemory->WriteMemRes.CRC32chksum = CalculateChksum((char *)&pAMIYAFUWriteMemory->SizeWritten,sizeof(INT16U));
  2325. return (sizeof(AMIYAFUWriteMemoryRes_T));
  2326. }
  2327. else
  2328. {
  2329. LastStatCode = YAFU_CC_IN_DEACTIVATE;
  2330. return (AMIYAFUNotAcks(pRes,YAFU_CC_IN_DEACTIVATE,pAMIYAFUWriteMemoryReq->WriteMemReq.Seqnum));
  2331. }
  2332. }
  2333. /*---------------------------------------
  2334. * AMIYAFUCopyMemory
  2335. *---------------------------------------*/
  2336. int AMIYAFUCopyMemory ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes,_NEAR_ int BMCInst )
  2337. {
  2338. AMIYAFUCopyMemoryReq_T *pAMIYAFUCopyMemoryReq = (AMIYAFUCopyMemoryReq_T *)pReq;
  2339. if(ActivateFlashStatus == 0x01)
  2340. {
  2341. INT8U *SrcOffset;
  2342. INT8U *DestOffset;
  2343. AMIYAFUCopyMemoryRes_T* pAMIYAFUCopyMemory = (AMIYAFUCopyMemoryRes_T*)pRes;
  2344. if(CalculateChksum((char *)&pAMIYAFUCopyMemoryReq->MemoffsetSrc,pAMIYAFUCopyMemoryReq->CopyMemReq.Datalen) != pAMIYAFUCopyMemoryReq->CopyMemReq.CRC32chksum)
  2345. {
  2346. LastStatCode=YAFU_INVALID_CHKSUM;
  2347. return (AMIYAFUNotAcks(pRes,YAFU_INVALID_CHKSUM,pAMIYAFUCopyMemoryReq->CopyMemReq.Seqnum));
  2348. }
  2349. if(pAMIYAFUCopyMemoryReq->CopyMemReq.Datalen== 0x0c)
  2350. pAMIYAFUCopyMemory->CopyMemRes.Datalen= 0x04;
  2351. else
  2352. {
  2353. LastStatCode = YAFU_CC_INVALID_DATLEN;
  2354. return (AMIYAFUNotAcks(pRes,YAFU_CC_INVALID_DATLEN,pAMIYAFUCopyMemoryReq->CopyMemReq.Seqnum));
  2355. }
  2356. SrcOffset = (INT8U *)pAMIYAFUCopyMemoryReq->MemoffsetSrc;
  2357. DestOffset = (INT8U *)pAMIYAFUCopyMemoryReq->MemoffsetDest;
  2358. memcpy(DestOffset,SrcOffset,pAMIYAFUCopyMemoryReq->Sizetocopy);
  2359. pAMIYAFUCopyMemory->CompletionCode = YAFU_CC_NORMAL;
  2360. LastStatCode = (INT16U)pAMIYAFUCopyMemory->CompletionCode;
  2361. pAMIYAFUCopyMemory->CopyMemRes.Seqnum = pAMIYAFUCopyMemoryReq->CopyMemReq.Seqnum;
  2362. pAMIYAFUCopyMemory->CopyMemRes.YafuCmd= pAMIYAFUCopyMemoryReq->CopyMemReq.YafuCmd;
  2363. pAMIYAFUCopyMemory->Sizecopied = pAMIYAFUCopyMemoryReq->Sizetocopy;
  2364. pAMIYAFUCopyMemory->CopyMemRes.CRC32chksum= CalculateChksum((char *)&pAMIYAFUCopyMemory->Sizecopied,sizeof(INT32U));
  2365. return (sizeof(AMIYAFUCopyMemoryRes_T));
  2366. }
  2367. else
  2368. {
  2369. LastStatCode = YAFU_CC_IN_DEACTIVATE;
  2370. return (AMIYAFUNotAcks(pRes,YAFU_CC_IN_DEACTIVATE,pAMIYAFUCopyMemoryReq->CopyMemReq.Seqnum));
  2371. }
  2372. }
  2373. /*---------------------------------------
  2374. * AMIYAFUCompareMemory
  2375. *---------------------------------------*/
  2376. int AMIYAFUCompareMemory ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes ,int BMCInst)
  2377. {
  2378. AMIYAFUCompareMemoryReq_T *pAMIYAFUCompareMemoryReq = (AMIYAFUCompareMemoryReq_T *)pReq;
  2379. if( ActivateFlashStatus == 0x01)
  2380. {
  2381. INT8U *FirstOffset;
  2382. INT8U *SecondOffset;
  2383. INT32U i=0;
  2384. AMIYAFUCompareMemoryRes_T* pAMIYAFUCompareMemory = ( AMIYAFUCompareMemoryRes_T*)pRes;
  2385. if(CalculateChksum((char *)&pAMIYAFUCompareMemoryReq->Memoffset1,pAMIYAFUCompareMemoryReq->CmpMemReq.Datalen) != pAMIYAFUCompareMemoryReq->CmpMemReq.CRC32chksum)
  2386. {
  2387. LastStatCode=YAFU_INVALID_CHKSUM;
  2388. return (AMIYAFUNotAcks(pRes,YAFU_INVALID_CHKSUM,pAMIYAFUCompareMemoryReq->CmpMemReq.Seqnum));
  2389. }
  2390. if(pAMIYAFUCompareMemoryReq->CmpMemReq.Datalen== 0x0c)
  2391. pAMIYAFUCompareMemory->CmpMemRes.Datalen= 0x04;
  2392. else
  2393. {
  2394. LastStatCode = YAFU_CC_INVALID_DATLEN;
  2395. return (AMIYAFUNotAcks(pRes,YAFU_CC_INVALID_DATLEN,pAMIYAFUCompareMemoryReq->CmpMemReq.Seqnum));
  2396. }
  2397. FirstOffset = (INT8U *) pAMIYAFUCompareMemoryReq->Memoffset1;
  2398. SecondOffset = (INT8U *) pAMIYAFUCompareMemoryReq->Memoffset2;
  2399. for(i=0;i<pAMIYAFUCompareMemoryReq->SizetoCmp;i++)
  2400. {
  2401. if(*FirstOffset == *SecondOffset)
  2402. {
  2403. FirstOffset++;
  2404. SecondOffset++;
  2405. }
  2406. else
  2407. {
  2408. printf("The value of i = 0x%08X\n", i);
  2409. printf("Inside else\n");
  2410. break;
  2411. }
  2412. }
  2413. if( i == pAMIYAFUCompareMemoryReq->SizetoCmp)
  2414. pAMIYAFUCompareMemory->Offset = 0x00;
  2415. else
  2416. pAMIYAFUCompareMemory->Offset = (INT32U )FirstOffset;
  2417. pAMIYAFUCompareMemory->CompletionCode = YAFU_CC_NORMAL;
  2418. LastStatCode = (INT16U)pAMIYAFUCompareMemory->CompletionCode;
  2419. pAMIYAFUCompareMemory->CmpMemRes.Seqnum = pAMIYAFUCompareMemoryReq->CmpMemReq.Seqnum;
  2420. pAMIYAFUCompareMemory->CmpMemRes.YafuCmd= pAMIYAFUCompareMemoryReq->CmpMemReq.YafuCmd;
  2421. pAMIYAFUCompareMemory->CmpMemRes.CRC32chksum = CalculateChksum((char *)&pAMIYAFUCompareMemory->Offset,sizeof(INT32U));
  2422. return (sizeof(AMIYAFUCompareMemoryRes_T));
  2423. }
  2424. else
  2425. {
  2426. LastStatCode = YAFU_CC_IN_DEACTIVATE;
  2427. return (AMIYAFUNotAcks(pRes,YAFU_CC_IN_DEACTIVATE,pAMIYAFUCompareMemoryReq->CmpMemReq.Seqnum));
  2428. }
  2429. }
  2430. /*---------------------------------------
  2431. * AMIYAFUClearMemory
  2432. *---------------------------------------*/
  2433. int AMIYAFUClearMemory ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes,_NEAR_ int BMCInst )
  2434. {
  2435. AMIYAFUClearMemoryReq_T *pAMIYAFUClearMemoryReq = (AMIYAFUClearMemoryReq_T *)pReq;
  2436. if(ActivateFlashStatus == 0x01)
  2437. {
  2438. INT8U *Offset;
  2439. AMIYAFUClearMemoryRes_T* pAMIYAFUClearMemory = (AMIYAFUClearMemoryRes_T*)pRes;
  2440. if(CalculateChksum((char *)&pAMIYAFUClearMemoryReq->MemofftoClear,pAMIYAFUClearMemoryReq->ClearMemReq.Datalen) != pAMIYAFUClearMemoryReq->ClearMemReq.CRC32chksum)
  2441. {
  2442. LastStatCode=YAFU_INVALID_CHKSUM;
  2443. return (AMIYAFUNotAcks(pRes,YAFU_INVALID_CHKSUM, pAMIYAFUClearMemoryReq->ClearMemReq.Seqnum));
  2444. }
  2445. if(pAMIYAFUClearMemoryReq->ClearMemReq.Datalen== 0x08)
  2446. pAMIYAFUClearMemory->ClearMemRes.Datalen= 0x04;
  2447. else
  2448. {
  2449. LastStatCode = YAFU_CC_INVALID_DATLEN;
  2450. return (AMIYAFUNotAcks(pRes,YAFU_CC_INVALID_DATLEN, pAMIYAFUClearMemoryReq->ClearMemReq.Seqnum));
  2451. }
  2452. Offset = (INT8U *)pAMIYAFUClearMemoryReq->MemofftoClear;
  2453. memset(Offset,0,pAMIYAFUClearMemoryReq->SizetoClear);
  2454. pAMIYAFUClearMemory->CompletionCode = YAFU_CC_NORMAL;
  2455. LastStatCode = pAMIYAFUClearMemory->CompletionCode;
  2456. pAMIYAFUClearMemory->ClearMemRes.Seqnum = pAMIYAFUClearMemoryReq->ClearMemReq.Seqnum;
  2457. pAMIYAFUClearMemory->ClearMemRes.YafuCmd=pAMIYAFUClearMemoryReq->ClearMemReq.YafuCmd;
  2458. pAMIYAFUClearMemory->SizeCleared=pAMIYAFUClearMemoryReq->SizetoClear;
  2459. pAMIYAFUClearMemory->ClearMemRes.CRC32chksum = CalculateChksum((char *)&pAMIYAFUClearMemory->SizeCleared,sizeof(INT32U));
  2460. return (sizeof(AMIYAFUClearMemoryRes_T));
  2461. }
  2462. else
  2463. {
  2464. LastStatCode = YAFU_CC_IN_DEACTIVATE;
  2465. return (AMIYAFUNotAcks(pRes,YAFU_CC_IN_DEACTIVATE, pAMIYAFUClearMemoryReq->ClearMemReq.Seqnum));
  2466. }
  2467. }
  2468. /*---------------------------------------
  2469. * AMIYAFUGetBootConfig
  2470. *---------------------------------------*/
  2471. int AMIYAFUGetBootConfig ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes,_NEAR_ int BMCInst )
  2472. {
  2473. AMIYAFUGetBootConfigReq_T *pAMIYAFUGetBootConfigReq = (AMIYAFUGetBootConfigReq_T *)pReq;
  2474. char *Buffer = NULL;
  2475. int len = 0;
  2476. if( ActivateFlashStatus == 0x01)
  2477. {
  2478. int Retval;
  2479. AMIYAFUGetBootConfigRes_T* pAMIYAFUGetBootConfig = (AMIYAFUGetBootConfigRes_T*)pRes;
  2480. if(CalculateChksum((char *)&pAMIYAFUGetBootConfigReq->VarName[0],pAMIYAFUGetBootConfigReq->GetBootReq.Datalen) != pAMIYAFUGetBootConfigReq->GetBootReq.CRC32chksum)
  2481. {
  2482. LastStatCode=YAFU_INVALID_CHKSUM;
  2483. return (AMIYAFUNotAcks(pRes,YAFU_INVALID_CHKSUM, pAMIYAFUGetBootConfigReq->GetBootReq.Seqnum));
  2484. }
  2485. if(pAMIYAFUGetBootConfigReq->GetBootReq.Datalen == 0x41)
  2486. pAMIYAFUGetBootConfig->GetBootRes.Datalen= 0x42;
  2487. else
  2488. {
  2489. LastStatCode = YAFU_CC_INVALID_DATLEN;
  2490. return (AMIYAFUNotAcks(pRes,YAFU_CC_INVALID_DATLEN, pAMIYAFUGetBootConfigReq->GetBootReq.Seqnum));
  2491. }
  2492. Buffer = malloc (MAX_BOOTVAL_LENGTH);
  2493. if(Buffer == NULL)
  2494. {
  2495. LastStatCode=YAFU_CC_ALLOC_ERR;
  2496. return (AMIYAFUNotAcks(pRes,YAFU_CC_ALLOC_ERR,pAMIYAFUGetBootConfigReq->GetBootReq.Seqnum));
  2497. }
  2498. memset(Buffer,0,MAX_BOOTVAL_LENGTH);
  2499. Retval= GetUBootParam((char *)&pAMIYAFUGetBootConfigReq->VarName,Buffer);
  2500. len = strlen(Buffer);
  2501. len++;
  2502. if(Retval != 0)
  2503. pAMIYAFUGetBootConfig->Status = 0x00;
  2504. else
  2505. pAMIYAFUGetBootConfig->Status = 0x01;
  2506. pAMIYAFUGetBootConfig->CompletionCode = YAFU_CC_NORMAL;
  2507. LastStatCode = (INT16U)pAMIYAFUGetBootConfig->CompletionCode;
  2508. pAMIYAFUGetBootConfig->GetBootRes.Seqnum = pAMIYAFUGetBootConfigReq->GetBootReq.Seqnum;
  2509. pAMIYAFUGetBootConfig->GetBootRes.YafuCmd=pAMIYAFUGetBootConfigReq->GetBootReq.YafuCmd;
  2510. memcpy (( INT8U*) (pAMIYAFUGetBootConfig + 1),
  2511. ( INT8U*)Buffer,len );
  2512. pAMIYAFUGetBootConfig->GetBootRes.Datalen = len + 1; //one for status
  2513. pAMIYAFUGetBootConfig->GetBootRes.CRC32chksum= CalculateChksum((char *)&pAMIYAFUGetBootConfig->Status,pAMIYAFUGetBootConfig->GetBootRes.Datalen);
  2514. free(Buffer);
  2515. return (sizeof(AMIYAFUGetBootConfigRes_T) + len);
  2516. }
  2517. else
  2518. {
  2519. LastStatCode = YAFU_CC_IN_DEACTIVATE;
  2520. return (AMIYAFUNotAcks(pRes,YAFU_CC_IN_DEACTIVATE, pAMIYAFUGetBootConfigReq->GetBootReq.Seqnum));
  2521. }
  2522. }
  2523. /*---------------------------------------
  2524. * AMIYAFUSetBootConfig
  2525. *---------------------------------------*/
  2526. int AMIYAFUSetBootConfig ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes,_NEAR_ int BMCInst )
  2527. {
  2528. AMIYAFUSetBootConfigReq_T *pAMIYAFUSetBootConfigReq = (AMIYAFUSetBootConfigReq_T *)pReq;
  2529. if( ActivateFlashStatus == 0x01)
  2530. {
  2531. int RetVal=0;
  2532. char *BootVal = NULL;
  2533. AMIYAFUSetBootConfigRes_T* pAMIYAFUSetBootConfig = (AMIYAFUSetBootConfigRes_T*)pRes;
  2534. if(CalculateChksum((char *)&pAMIYAFUSetBootConfigReq->VarName[0],pAMIYAFUSetBootConfigReq->SetBootReq.Datalen) != pAMIYAFUSetBootConfigReq->SetBootReq.CRC32chksum)
  2535. {
  2536. LastStatCode=YAFU_INVALID_CHKSUM;
  2537. return (AMIYAFUNotAcks(pRes,YAFU_INVALID_CHKSUM, pAMIYAFUSetBootConfigReq->SetBootReq.Seqnum));
  2538. }
  2539. BootVal = malloc (pAMIYAFUSetBootConfigReq->SetBootReq.Datalen - 65);
  2540. if(BootVal == NULL)
  2541. {
  2542. LastStatCode=YAFU_CC_ALLOC_ERR;
  2543. return (AMIYAFUNotAcks(pRes,YAFU_CC_ALLOC_ERR,pAMIYAFUSetBootConfigReq->SetBootReq.Seqnum));
  2544. }
  2545. pReq += sizeof(AMIYAFUSetBootConfigReq_T);
  2546. memcpy(BootVal,pReq,(pAMIYAFUSetBootConfigReq->SetBootReq.Datalen - 65));
  2547. memset(pReq,0,(pAMIYAFUSetBootConfigReq->SetBootReq.Datalen - 65));
  2548. pReq -= sizeof(AMIYAFUSetBootConfigReq_T);
  2549. if(strcmp((char *)pAMIYAFUSetBootConfigReq->VarName,"baudrate") != 0)
  2550. {
  2551. RetVal = SetUBootParam((char *)pAMIYAFUSetBootConfigReq->VarName,BootVal);
  2552. if(RetVal != 0)
  2553. {
  2554. pAMIYAFUSetBootConfig->Status = 0x00;
  2555. }
  2556. else
  2557. {
  2558. pAMIYAFUSetBootConfig->Status = 0x01;
  2559. }
  2560. }
  2561. else
  2562. pAMIYAFUSetBootConfig->Status = 0x01;
  2563. pAMIYAFUSetBootConfig->CompletionCode = YAFU_CC_NORMAL;
  2564. LastStatCode = (INT16U)pAMIYAFUSetBootConfig->CompletionCode;
  2565. pAMIYAFUSetBootConfig->SetBootRes.Seqnum = pAMIYAFUSetBootConfigReq->SetBootReq.Seqnum;
  2566. pAMIYAFUSetBootConfig->SetBootRes.YafuCmd = pAMIYAFUSetBootConfigReq->SetBootReq.YafuCmd;
  2567. pAMIYAFUSetBootConfig->SetBootRes.Datalen = 0x01;
  2568. pAMIYAFUSetBootConfig->SetBootRes.CRC32chksum= CalculateChksum((char *)&pAMIYAFUSetBootConfig->Status,pAMIYAFUSetBootConfig->SetBootRes.Datalen );
  2569. free(BootVal);
  2570. return (sizeof(AMIYAFUSetBootConfigRes_T));
  2571. }
  2572. else
  2573. {
  2574. LastStatCode = YAFU_CC_IN_DEACTIVATE;
  2575. return (AMIYAFUNotAcks(pRes,YAFU_CC_IN_DEACTIVATE, pAMIYAFUSetBootConfigReq->SetBootReq.Seqnum));
  2576. }
  2577. }
  2578. /*---------------------------------------
  2579. * AMIYAFUGetBootVars
  2580. *---------------------------------------*/
  2581. int AMIYAFUGetBootVars ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes,_NEAR_ int BMCInst )
  2582. {
  2583. AMIYAFUGetBootVarsReq_T *pAMIYAFUGetBootVarsReq = (AMIYAFUGetBootVarsReq_T *)pReq;
  2584. if(ActivateFlashStatus == 0x01)
  2585. {
  2586. //int RetVal;
  2587. char *Buffer;
  2588. int BootVarlen =0;
  2589. AMIYAFUGetBootVarsRes_T* pAMIYAFUGetBootVars = (AMIYAFUGetBootVarsRes_T*)pRes;
  2590. pAMIYAFUGetBootVars->VarCount = 0X00;
  2591. Buffer = malloc (MAX_BOOTVAR_LENGTH);
  2592. if(Buffer == NULL)
  2593. {
  2594. LastStatCode=YAFU_CC_ALLOC_ERR;
  2595. return (AMIYAFUNotAcks(pRes,YAFU_CC_ALLOC_ERR,pAMIYAFUGetBootVarsReq->GetBootReq.Seqnum));
  2596. }
  2597. GetAllUBootParam ((char *)&pAMIYAFUGetBootVars->VarCount,Buffer,&BootVarlen);
  2598. memcpy (( INT8U*) (pAMIYAFUGetBootVars + 1),
  2599. ( INT8U*)Buffer,BootVarlen );
  2600. pAMIYAFUGetBootVars->GetBootRes.Datalen = BootVarlen + 1; //One added for Variable count
  2601. pAMIYAFUGetBootVars->CompletionCode = YAFU_CC_NORMAL;
  2602. LastStatCode = (INT16U)pAMIYAFUGetBootVars->CompletionCode;
  2603. pAMIYAFUGetBootVars->GetBootRes.Seqnum = pAMIYAFUGetBootVarsReq->GetBootReq.Seqnum;
  2604. pAMIYAFUGetBootVars->GetBootRes.YafuCmd = pAMIYAFUGetBootVarsReq->GetBootReq.YafuCmd;
  2605. pAMIYAFUGetBootVars->GetBootRes.CRC32chksum= CalculateChksum((char *)&pAMIYAFUGetBootVars->VarCount,pAMIYAFUGetBootVars->GetBootRes.Datalen);
  2606. free(Buffer);
  2607. return (sizeof(AMIYAFUGetBootVarsRes_T)+ BootVarlen);
  2608. }
  2609. else
  2610. {
  2611. LastStatCode = YAFU_CC_IN_DEACTIVATE;
  2612. return (AMIYAFUNotAcks(pRes,YAFU_CC_IN_DEACTIVATE, pAMIYAFUGetBootVarsReq->GetBootReq.Seqnum));
  2613. }
  2614. }
  2615. /*---------------------------------------
  2616. * AMIYAFUDeactivateFlash
  2617. *---------------------------------------*/
  2618. int AMIYAFUDeactivateFlash ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes,_NEAR_ int BMCInst )
  2619. {
  2620. char Cmd[128];
  2621. AMIYAFUDeactivateFlashReq_T *pAMIYAFUDeactivateFlashReq = (AMIYAFUDeactivateFlashReq_T *)pReq;
  2622. if(ActivateFlashStatus == 0x01)
  2623. {
  2624. AMIYAFUDeactivateFlashRes_T* pAMIYAFUDeactivateFlash = (AMIYAFUDeactivateFlashRes_T*)pRes;
  2625. if(pAMIYAFUDeactivateFlashReq->DeactivateFlashReq.Datalen== 0x00)
  2626. pAMIYAFUDeactivateFlash->DeactivateFlashRes.Datalen= 0x01;
  2627. else
  2628. {
  2629. LastStatCode = YAFU_CC_INVALID_DATLEN;
  2630. return (AMIYAFUNotAcks(pRes,YAFU_CC_INVALID_DATLEN, pAMIYAFUDeactivateFlashReq->DeactivateFlashReq.Seqnum));
  2631. }
  2632. ActivateFlashStatus = 0x00;
  2633. if(access("/var/yafu_bios_update_selection", F_OK) == 0)
  2634. {
  2635. StartInitFlashSetting();
  2636. sprintf(Cmd, "rm %s", FLASHER_COMPLETION_FILE_INIT7);
  2637. safe_system(Cmd);
  2638. sprintf(Cmd, "/sbin/rmmod host_spi_flash_hw");
  2639. safe_system(Cmd);
  2640. sprintf(Cmd, "/sbin/rmmod host_spi_flash");
  2641. safe_system(Cmd);
  2642. sprintf(Cmd, "rm /var/yafu_bios_update_selection");
  2643. safe_system(Cmd);
  2644. }
  2645. if(g_corefeatures.online_flashing_support == ENABLED)
  2646. {
  2647. unlink(FLASHER_INIT_START);
  2648. unlink(FLASHER_COMPLETION_FILE_INIT7);
  2649. }
  2650. pAMIYAFUDeactivateFlash->Status = 0x00;
  2651. pAMIYAFUDeactivateFlash->CompletionCode = YAFU_CC_NORMAL;
  2652. LastStatCode = (INT16U) pAMIYAFUDeactivateFlash->CompletionCode;
  2653. pAMIYAFUDeactivateFlash->DeactivateFlashRes.Seqnum = pAMIYAFUDeactivateFlashReq->DeactivateFlashReq.Seqnum;
  2654. pAMIYAFUDeactivateFlash->DeactivateFlashRes.YafuCmd = pAMIYAFUDeactivateFlashReq->DeactivateFlashReq.YafuCmd;
  2655. pAMIYAFUDeactivateFlash->DeactivateFlashRes.CRC32chksum= CalculateChksum((char *)&pAMIYAFUDeactivateFlash->Status,sizeof(INT8U));
  2656. if(g_corefeatures.dual_image_support == ENABLED && access("/var/bios_update_selection", F_OK) != 0)
  2657. {
  2658. SetMostRecentlyProgFW();
  2659. }
  2660. return (sizeof(AMIYAFUDeactivateFlashRes_T));
  2661. }
  2662. else
  2663. {
  2664. LastStatCode = YAFU_CC_IN_DEACTIVATE;
  2665. return (AMIYAFUNotAcks(pRes,YAFU_CC_IN_DEACTIVATE, pAMIYAFUDeactivateFlashReq->DeactivateFlashReq.Seqnum));
  2666. }
  2667. }
  2668. /*---------------------------------------
  2669. * AMIYAFUResetDevice
  2670. *---------------------------------------*/
  2671. int AMIYAFUResetDevice ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes,_NEAR_ int BMCInst )
  2672. {
  2673. int Restart_Flag = 0;
  2674. AMIYAFUResetDeviceReq_T *pAMIYAFUResetDeviceReq = (AMIYAFUResetDeviceReq_T *)pReq;
  2675. AMIYAFUResetDeviceRes_T* pAMIYAFUResetDevice = (AMIYAFUResetDeviceRes_T*)pRes;
  2676. // YafuflashStatus is updated to terminate the YafuTimerThread
  2677. YafuflashStatus = YAFU_FLASH_SUCCEEDED;
  2678. if(CalculateChksum((char *)&pAMIYAFUResetDeviceReq->WaitSec,sizeof(INT16U)) != pAMIYAFUResetDeviceReq->ResetReq.CRC32chksum)
  2679. {
  2680. LastStatCode=YAFU_INVALID_CHKSUM;
  2681. return (AMIYAFUNotAcks(pRes,YAFU_INVALID_CHKSUM, pAMIYAFUResetDeviceReq->ResetReq.Seqnum));
  2682. }
  2683. if(pAMIYAFUResetDeviceReq->ResetReq.Datalen== 0x02)
  2684. pAMIYAFUResetDevice->ResetRes.Datalen= 0x01;
  2685. else
  2686. {
  2687. LastStatCode = YAFU_CC_INVALID_DATLEN;
  2688. return (AMIYAFUNotAcks(pRes,YAFU_CC_INVALID_DATLEN, pAMIYAFUResetDeviceReq->ResetReq.Seqnum));
  2689. }
  2690. if(access("/var/yafu_bios_update_selection", F_OK) != 0)
  2691. {
  2692. if(g_corefeatures.preserve_config == ENABLED)
  2693. {
  2694. if(pAMIYAFUResetDeviceReq->WaitSec == 0x02)
  2695. {
  2696. Restart_Flag = 1;
  2697. TINFO("Trying to preserve config on full flash using preservecfg application");
  2698. safe_system("/usr/local/bin/preservecfg 2");
  2699. }
  2700. }
  2701. if(g_corefeatures.dual_image_support == ENABLED)
  2702. {
  2703. if(DualImgPreserveConf == FALSE)
  2704. {
  2705. safe_system("rm -rf /conf/*");
  2706. }
  2707. }
  2708. }
  2709. /* Considering rebootstatus flag*/
  2710. RestartDeviceWithNewFirmware();
  2711. pAMIYAFUResetDevice->Status = 0x01;
  2712. pAMIYAFUResetDevice->CompletionCode = YAFU_CC_NORMAL;
  2713. LastStatCode = (INT16U) pAMIYAFUResetDevice->CompletionCode;
  2714. pAMIYAFUResetDevice->ResetRes.Seqnum = pAMIYAFUResetDeviceReq->ResetReq.Seqnum;
  2715. pAMIYAFUResetDevice->ResetRes.YafuCmd = pAMIYAFUResetDeviceReq->ResetReq.YafuCmd;
  2716. pAMIYAFUResetDevice->ResetRes.CRC32chksum=CalculateChksum((char *)&pAMIYAFUResetDevice->Status,sizeof(INT8U));
  2717. return (sizeof(AMIYAFUResetDeviceRes_T));
  2718. }
  2719. int AMIYAFUFWSelectFlash ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes,_NEAR_ int BMCInst )
  2720. {
  2721. AMIYAFUFWSelectFlashModeReq_T *pAMIYAFUFWSelectFlashModeReq = (AMIYAFUFWSelectFlashModeReq_T *)pReq;
  2722. AMIYAFUFWSelectFlashModeRes_T* pAMIYAFUFWSElectFlashMode = (AMIYAFUFWSelectFlashModeRes_T*)pRes;
  2723. if(CalculateChksum((char *)&pAMIYAFUFWSelectFlashModeReq->fwselect,sizeof(INT8U)) != pAMIYAFUFWSelectFlashModeReq->fwselectflashReq.CRC32chksum)
  2724. {
  2725. LastStatCode=YAFU_INVALID_CHKSUM;
  2726. return (AMIYAFUNotAcks(pRes,YAFU_INVALID_CHKSUM, pAMIYAFUFWSelectFlashModeReq->fwselectflashReq.Seqnum));
  2727. }
  2728. if(pAMIYAFUFWSelectFlashModeReq->fwselectflashReq.Datalen == 0x01)
  2729. pAMIYAFUFWSElectFlashMode->fwselectflashRes.Datalen = 0x01;
  2730. else
  2731. {
  2732. LastStatCode = YAFU_CC_INVALID_DATLEN;
  2733. return (AMIYAFUNotAcks(pRes,YAFU_CC_INVALID_DATLEN, pAMIYAFUFWSelectFlashModeReq->fwselectflashReq.Seqnum));
  2734. }
  2735. if (pAMIYAFUFWSelectFlashModeReq->fwselect == FW_BMC )
  2736. {
  2737. safe_system("rm -f /var/yafu_bios_update_selection");
  2738. }
  2739. else if (pAMIYAFUFWSelectFlashModeReq->fwselect == FW_BIOS)
  2740. {
  2741. safe_system("touch /var/yafu_bios_update_selection");
  2742. }
  2743. pAMIYAFUFWSElectFlashMode->CompletionCode = YAFU_CC_NORMAL;
  2744. LastStatCode = (INT16U) pAMIYAFUFWSElectFlashMode->CompletionCode;
  2745. pAMIYAFUFWSElectFlashMode->fwselectflashRes.Seqnum = pAMIYAFUFWSelectFlashModeReq->fwselectflashReq.Seqnum;
  2746. pAMIYAFUFWSElectFlashMode->fwselectflashRes.YafuCmd = pAMIYAFUFWSelectFlashModeReq->fwselectflashReq.YafuCmd;
  2747. return (sizeof(AMIYAFUResetDeviceRes_T));
  2748. }
  2749. #endif
  2750. /**
  2751. * @fn CheckForBootOption
  2752. * @brief Check for valid boot selector option.
  2753. * @param[in] bootoption - boot selector option.
  2754. * @retval 0 - for valid boot selector option.
  2755. * 1 - invalid boot option.
  2756. */
  2757. static int CheckForBootOption(char bootoption)
  2758. {
  2759. //Checking for valid Boot selector option
  2760. if( (bootoption != AUTO_HIGH_VER_FW) &&
  2761. (bootoption != LOWER_IMAGE_FW ) &&
  2762. (bootoption != HIGHER_IMAGE_FW ) &&
  2763. (bootoption != AUTO_LOW_VER_FW) &&
  2764. (bootoption != MOST_RECENTLY_PROG_FW) &&
  2765. (bootoption != LEAST_RECENTLY_PROG_FW) )
  2766. {
  2767. return -1;
  2768. }
  2769. return 0;
  2770. }
  2771. /**
  2772. * @fn IsfwuploadSelectorValid
  2773. * @brief Check for valid firmware upload selector.
  2774. * @param[in] uploadselector - Firmwareupload selector option.
  2775. * @retval TRUE - for valid firmware upload selector.
  2776. * FALSE - invalid firmware upload selector option.
  2777. */
  2778. static bool IsfwuploadSelectorValid(char uploadselector)
  2779. {
  2780. return( (uploadselector == AUTO_INACTIVE_IMAGE)
  2781. || (uploadselector == IMAGE_1 )
  2782. || (uploadselector == IMAGE_2 )
  2783. || (uploadselector == IMAGE_BOTH )
  2784. );
  2785. }
  2786. int AMIDualImageSupport( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes,_NEAR_ int BMCInst )
  2787. {
  2788. AMIDualImageSupReq_T *pAMIDualImageSupReq = (AMIDualImageSupReq_T *)pReq;
  2789. AMIDualImageSupRes_T *pAMIDualImageSupRes = (AMIDualImageSupRes_T *)pRes;
  2790. char envval[5] = {0};
  2791. int bootselector = 0;
  2792. unsigned char fwuploadselector = 0;
  2793. FWINFO_STRUCT FwInfo;
  2794. char tempstr[30];
  2795. char * pTemp;
  2796. if(g_corefeatures.dual_image_support == ENABLED)
  2797. {
  2798. if(ReqLen != m_DualResBits[pAMIDualImageSupReq->Parameter - 1])
  2799. {
  2800. *pRes = CC_REQ_INV_LEN;
  2801. return sizeof(INT8U);
  2802. }
  2803. switch (pAMIDualImageSupReq->Parameter) //validating for command sub specifier
  2804. {
  2805. case SETFWBOOTSELECTOR:
  2806. if (IsCardInFlashMode() != 0)
  2807. {
  2808. *pRes = CC_DEV_IN_FIRMWARE_UPDATE_MODE;
  2809. return sizeof(INT8U);
  2810. }
  2811. //Checking for valid Boot selector option
  2812. if( CheckForBootOption(pAMIDualImageSupReq->BootSelector) != 0 )
  2813. {
  2814. IPMI_ERROR("Invalid Bootoption");
  2815. *pRes = CC_INV_DATA_FIELD;
  2816. return sizeof(INT8U);
  2817. }
  2818. /*caching bootselector value to avoid unnecessary writes to same values in next set */
  2819. if( g_FWBootSelector != pAMIDualImageSupReq->BootSelector )
  2820. {
  2821. /* sets env if bootselector differs with previous one */
  2822. memset(envval,0,sizeof(envval));
  2823. sprintf(envval,"%d",pAMIDualImageSupReq->BootSelector);
  2824. if( SetUBootParam("bootselector",envval) != 0 )
  2825. {
  2826. IPMI_ERROR("Setting env variable bootselector failed");
  2827. *pRes = CC_UNSPECIFIED_ERR;
  2828. return sizeof(INT8U);
  2829. }
  2830. g_FWBootSelector = pAMIDualImageSupReq->BootSelector;
  2831. }
  2832. pAMIDualImageSupRes->BootSelOpt.SetBootSelector = pAMIDualImageSupReq->BootSelector;
  2833. pAMIDualImageSupRes->CompletionCode = CC_NORMAL;
  2834. return sizeof(INT8U) + sizeof(pAMIDualImageSupRes->BootSelOpt.SetBootSelector);
  2835. break;
  2836. case GETFWBOOTSELECTOR:
  2837. if(g_FWBootSelector == 0xFF) /* reads from env at very first time */
  2838. {
  2839. if( GetUBootParam("bootselector",envval) != 0 )
  2840. {
  2841. IPMI_ERROR("Reading env variable bootselector failed");
  2842. *pRes = CC_UNSPECIFIED_ERR;
  2843. return sizeof(INT8U);
  2844. }
  2845. sscanf(envval,"%d",&bootselector);
  2846. //Checking for valid Boot selector option
  2847. if( CheckForBootOption(bootselector) !=0 )
  2848. {
  2849. IPMI_ERROR("Invalid Bootoption");
  2850. *pRes = CC_UNSPECIFIED_ERR;
  2851. return sizeof(INT8U);
  2852. }
  2853. g_FWBootSelector = bootselector;
  2854. }
  2855. pAMIDualImageSupRes->BootSelOpt.GetBootSelector = g_FWBootSelector;
  2856. pAMIDualImageSupRes->CompletionCode = CC_NORMAL;
  2857. return sizeof(INT8U) + sizeof(pAMIDualImageSupRes->BootSelOpt.GetBootSelector);
  2858. break;
  2859. case SETFWUPLOADSELECTOR:
  2860. if( IsCardInFlashMode() != 0)
  2861. {
  2862. *pRes = CC_DEV_IN_FIRMWARE_UPDATE_MODE;
  2863. return sizeof(INT8U);
  2864. }
  2865. fwuploadselector = pAMIDualImageSupReq->BootSelector;
  2866. if( IsfwuploadSelectorValid(fwuploadselector) == FALSE)
  2867. {
  2868. *pRes = CC_INV_DATA_FIELD;
  2869. return sizeof(INT8U);
  2870. }
  2871. if( fwuploadselector != g_FWUploadSelector )
  2872. {
  2873. if(fwuploadselector == AUTO_INACTIVE_IMAGE)
  2874. {
  2875. fwuploadselector = g_InactiveImage;
  2876. }
  2877. if ( InformFlasher(fwuploadselector,FLSH_DUAL_IMAGE_OPTIONS) != 0 )
  2878. {
  2879. IPMI_ERROR(" SETFWUPLOADSELECTOR unable to communicate flasher process \n");
  2880. *pRes = CC_UNSPECIFIED_ERR;
  2881. return sizeof(INT8U);
  2882. }
  2883. g_FWUploadSelector = pAMIDualImageSupReq->BootSelector;
  2884. g_FlashingImage = fwuploadselector;
  2885. }
  2886. pAMIDualImageSupRes->BootSelOpt.SetUploadSelector = pAMIDualImageSupReq->BootSelector;
  2887. pAMIDualImageSupRes->CompletionCode = CC_NORMAL;
  2888. return sizeof(INT8U) + sizeof(pAMIDualImageSupRes->BootSelOpt.SetUploadSelector);
  2889. break;
  2890. case GETFWUPLOADSELECTOR:
  2891. if( IsfwuploadSelectorValid(g_FWUploadSelector) == FALSE )
  2892. {
  2893. *pRes = CC_UNSPECIFIED_ERR;
  2894. return sizeof(INT8U);
  2895. }
  2896. pAMIDualImageSupRes->BootSelOpt.GetUploadSelector = (g_FWUploadSelector == AUTO_INACTIVE_IMAGE) ? g_InactiveImage:g_FWUploadSelector;
  2897. pAMIDualImageSupRes->CompletionCode = CC_NORMAL;
  2898. return sizeof(INT8U) + sizeof(pAMIDualImageSupRes->BootSelOpt.GetUploadSelector);
  2899. break;
  2900. case SETREBOOTSTATUS:
  2901. if ((pAMIDualImageSupReq->BootSelector != REBOOT_DEVICE) && (pAMIDualImageSupReq->BootSelector != NO_REBOOT_DEVICE))
  2902. {
  2903. *pRes = CC_INV_DATA_FIELD;
  2904. return sizeof(INT8U);
  2905. }
  2906. if ( InformFlasher(pAMIDualImageSupReq->BootSelector,FLSH_REBOOT_DEVICE_OPTIONS) != 0 )
  2907. {
  2908. IPMI_ERROR(" SetRebootStatus unable to communicate flasher process \n");
  2909. *pRes = CC_UNSPECIFIED_ERR;
  2910. return sizeof(INT8U);
  2911. }
  2912. g_rebootstatus = pAMIDualImageSupRes->BootSelOpt.SetRebootStatus = pAMIDualImageSupReq->BootSelector;
  2913. pAMIDualImageSupRes->CompletionCode = CC_NORMAL;
  2914. return sizeof(INT8U) + sizeof(pAMIDualImageSupRes->BootSelOpt.SetRebootStatus);
  2915. break;
  2916. case GETREBOOTSTATUS:
  2917. pAMIDualImageSupRes->BootSelOpt.GetRebootStatus = g_rebootstatus;
  2918. pAMIDualImageSupRes->CompletionCode = CC_NORMAL;
  2919. return sizeof(INT8U) + sizeof(pAMIDualImageSupRes->BootSelOpt.GetRebootStatus);
  2920. break;
  2921. case GETCURACTIVEIMG:
  2922. pAMIDualImageSupRes->BootSelOpt.GetCurActiveImg = g_RunningImage;
  2923. pAMIDualImageSupRes->CompletionCode = CC_NORMAL;
  2924. return sizeof(INT8U) + sizeof(pAMIDualImageSupRes->BootSelOpt.GetCurActiveImg);
  2925. break;
  2926. case GETDUALIMGFWINFO:
  2927. if((pAMIDualImageSupReq->BootSelector != IMAGE_1) && (pAMIDualImageSupReq->BootSelector != IMAGE_2))
  2928. {
  2929. IPMI_ERROR("Invalid Image Selector %x\n",pAMIDualImageSupReq->BootSelector);
  2930. *pRes = CC_INV_DATA_FIELD;
  2931. return sizeof(INT8U);
  2932. }
  2933. if(pAMIDualImageSupReq->BootSelector == IMAGE_1)
  2934. {
  2935. if (0 != GetIMG1FWInfo(&FwInfo))
  2936. {
  2937. IPMI_ERROR("Error while getting the FWInfo for IMAGE1\n");
  2938. *pRes = CC_UNSPECIFIED_ERR;
  2939. return sizeof(INT8U);
  2940. }
  2941. }
  2942. if(pAMIDualImageSupReq->BootSelector == IMAGE_2)
  2943. {
  2944. if (0 != GetIMG2FWInfo(&FwInfo))
  2945. {
  2946. IPMI_ERROR("Error while getting the FWInfo for IMAGE2\n");
  2947. *pRes = CC_UNSPECIFIED_ERR;
  2948. return sizeof(INT8U);
  2949. }
  2950. }
  2951. /*Parse the Major and Minor Version*/
  2952. memset(tempstr,'\0', sizeof(tempstr));
  2953. pTemp = (char *)FwInfo.FWVersion;
  2954. sscanf(pTemp," %[^.] ",tempstr);
  2955. pAMIDualImageSupRes->BootSelOpt.GetFwVersion.MajVer = atoi(tempstr);
  2956. pTemp = strchr(pTemp,'.');
  2957. pTemp = pTemp + 1;
  2958. memset(tempstr,'\0', sizeof(tempstr));
  2959. sscanf(pTemp," %[^.] ",tempstr);
  2960. pAMIDualImageSupRes->BootSelOpt.GetFwVersion.MinVer = atoi(tempstr);
  2961. return sizeof(INT8U) + sizeof(pAMIDualImageSupRes->BootSelOpt.GetFwVersion);
  2962. break;
  2963. default:
  2964. *pRes = CC_INV_DATA_FIELD;
  2965. return sizeof(INT8U);
  2966. }
  2967. }
  2968. else
  2969. {
  2970. *pRes = CC_INV_CMD;
  2971. return sizeof(INT8U);
  2972. }
  2973. }
  2974. /*
  2975. *@fn AMIGetNMChNum
  2976. *@brief This command helps in framing send message command if the Sensor Owner ID is
  2977. other than BMC like Node Manager.This retrives the correct IPMB channel associated
  2978. with the device.
  2979. *@param pReq - Request for the command
  2980. *@param ReqLen - Request length for the command
  2981. *@param pRes - Respose for the command
  2982. *@return Returns size of AMIGetChNumRes_T
  2983. */
  2984. int AMIGetNMChNum ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes,_NEAR_ int BMCInst )
  2985. {
  2986. AMIGetChNumRes_T* pAMIGetChNumRes = (AMIGetChNumRes_T*) pRes;
  2987. _FAR_ BMCInfo_t* pBMCInfo = &g_BMCInfo[BMCInst];
  2988. if(g_corefeatures.node_manager != ENABLED)
  2989. {
  2990. *pRes = CC_DEVICE_NOT_SUPPORTED;
  2991. return sizeof(*pRes);
  2992. }
  2993. pAMIGetChNumRes->ChannelNum = (pBMCInfo->NMConfig.NM_IPMBBus ? pBMCInfo->SecondaryIPMBCh : pBMCInfo->PrimaryIPMBCh);
  2994. pAMIGetChNumRes->CompletionCode = CC_NORMAL;
  2995. return sizeof(AMIGetChNumRes_T);
  2996. }
  2997. /**
  2998. *@fn AMIGetEthIndex
  2999. *@brief This command helps in getting the Corresponding EthIndex Value
  3000. for the requested Channel Number
  3001. *@param pReq - Request for the command
  3002. *@param ReqLen - Request length for the command
  3003. *@param pRes - Respose for the command
  3004. *@return Returns size of AMIGetEthIndexReq_T
  3005. */
  3006. int AMIGetEthIndex ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes ,_NEAR_ int BMCInst)
  3007. {
  3008. INT8U EthIndex = 0;
  3009. AMIGetEthIndexReq_T *pAMIGetEthIndexReq = (AMIGetEthIndexReq_T *) pReq;
  3010. AMIGetEthIndexRes_T *pAMIGetEthIndexRes = (AMIGetEthIndexRes_T *) pRes;
  3011. pAMIGetEthIndexRes->CompletionCode = CC_NORMAL;
  3012. EthIndex = GetEthIndex(pAMIGetEthIndexReq->ChannelNum, BMCInst);
  3013. pAMIGetEthIndexRes->EthIndex=EthIndex;
  3014. return sizeof(AMIGetEthIndexRes_T);
  3015. }
  3016. /*-----------------------------------
  3017. * Functions used by internal API
  3018. *-----------------------------------*/
  3019. /**
  3020. * *@fn AMIGetFruDetails
  3021. * *@brief This command can get the total fru and the repective fru ids
  3022. * *@param pReq - Request for the command
  3023. * *@param ReqLen - Request length for the command
  3024. * *@param pRes - Respose for the command
  3025. * */
  3026. int AMIGetFruDetails( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes,int BMCInst )
  3027. {
  3028. AMIGetFruDetailReq_T *pAMIFruDetailReq = (AMIGetFruDetailReq_T *) pReq;
  3029. AMIGetFruDetailRes_T *pAMIFruDetailRes = (AMIGetFruDetailRes_T *) pRes;
  3030. _FAR_ BMCInfo_t* pBMCInfo = &g_BMCInfo[BMCInst];
  3031. memset((char *)pRes,0,sizeof(AMIGetFruDetailRes_T));
  3032. if(pAMIFruDetailReq->FruReq==0xFF)
  3033. {
  3034. pAMIFruDetailRes->TotalFru = pBMCInfo->FRUConfig.total_frus;
  3035. pAMIFruDetailRes->CompletionCode = CC_NORMAL;
  3036. return sizeof(pAMIFruDetailRes->TotalFru) + sizeof(pAMIFruDetailRes->CompletionCode);
  3037. }
  3038. else if(pAMIFruDetailReq->FruReq < pBMCInfo->FRUConfig.total_frus)
  3039. {
  3040. pAMIFruDetailRes->TotalFru = pBMCInfo->FRUConfig.total_frus;
  3041. pAMIFruDetailRes->DeviceNo = pBMCInfo->FRUConfig.m_FRUInfo[pAMIFruDetailReq->FruReq]->DeviceID;
  3042. strcpy((char *)pAMIFruDetailRes->FRUName,pBMCInfo->FRUConfig.m_FRUInfo[pAMIFruDetailReq->FruReq]->FRUName);
  3043. pAMIFruDetailRes->CompletionCode = CC_NORMAL;
  3044. return sizeof(AMIGetFruDetailRes_T);
  3045. }
  3046. else
  3047. {
  3048. pAMIFruDetailRes->CompletionCode = CC_PARAM_OUT_OF_RANGE;
  3049. return sizeof(AMIGetFruDetailRes_T);
  3050. }
  3051. return 0;
  3052. }
  3053. /**
  3054. * *@fn AMISetTriggerEvent
  3055. * *@brief This command is used to enable/disable trigger event based on the parameter input
  3056. * *@param pReq - Request for the command
  3057. * *@param ReqLen - Request length for the command
  3058. * *@param pRes - Response for the command
  3059. * *@param BMCInst - BMC Instance Value
  3060. **/
  3061. int AMISetTriggerEvent( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes ,int BMCInst )
  3062. {
  3063. _NEAR_ AMISetTriggerEventReq_T *pAMISetTriggerEventReq = (AMISetTriggerEventReq_T *)pReq;
  3064. _NEAR_ AMISetTriggerEventRes_T *pAMISetTriggerEventRes = (AMISetTriggerEventRes_T *)pRes;
  3065. INT32U LocalTime,TriggerTime;
  3066. INT32U TriggerLength = 0;
  3067. BMCInfo_t *pBMCInfo = &g_BMCInfo[BMCInst];
  3068. TriggerLength = TriggerEventParamLength[pAMISetTriggerEventReq->TriggerParam-1];
  3069. if(TriggerLength != 0)
  3070. {
  3071. if((ReqLen - 1) != TriggerLength)
  3072. {
  3073. *pRes = CC_REQ_INV_LEN;
  3074. TDBG("Your entered Parameter %d is not a valid data \n",pAMISetTriggerEventReq->TriggerParam);
  3075. return sizeof (INT8U);
  3076. }
  3077. }
  3078. else
  3079. {
  3080. *pRes = CC_INV_DATA_FIELD;
  3081. return sizeof(INT8U);
  3082. }
  3083. if(1< pAMISetTriggerEventReq->EnableDisableFlag)
  3084. {
  3085. *pRes = CC_INV_DATA_FIELD;
  3086. printf("Your entered Parameter %d is not a valid data \n",pAMISetTriggerEventReq->TriggerParam);
  3087. return sizeof (INT8U);
  3088. }
  3089. switch(pAMISetTriggerEventReq->TriggerParam)
  3090. {
  3091. case CRITICAL:
  3092. pBMCInfo->TriggerEvent.CriticalFlag = pAMISetTriggerEventReq->EnableDisableFlag;
  3093. break;
  3094. case NON_CRITICAL:
  3095. pBMCInfo->TriggerEvent.NONCriticalFlag = pAMISetTriggerEventReq->EnableDisableFlag;
  3096. break;
  3097. case NON_RECOVERABLE:
  3098. pBMCInfo->TriggerEvent.NONRecoverableFlag= pAMISetTriggerEventReq->EnableDisableFlag;
  3099. break;
  3100. case FAN_TROUBLED:
  3101. pBMCInfo->TriggerEvent.FanTroubled= pAMISetTriggerEventReq->EnableDisableFlag;
  3102. break;
  3103. case BMC_WDT_EXPIRE:
  3104. pBMCInfo->TriggerEvent.WDTTimeExpire = pAMISetTriggerEventReq->EnableDisableFlag;
  3105. break;
  3106. case SYS_DC_ON:
  3107. pBMCInfo->TriggerEvent.SysDConFlag= pAMISetTriggerEventReq->EnableDisableFlag;
  3108. break;
  3109. case SYS_DC_OFF:
  3110. pBMCInfo->TriggerEvent.SysDCoffFlag= pAMISetTriggerEventReq->EnableDisableFlag;
  3111. break;
  3112. case SYS_RESET:
  3113. pBMCInfo->TriggerEvent.SysResetFlag = pAMISetTriggerEventReq->EnableDisableFlag;
  3114. break;
  3115. case SPEC_DATE_TIME:
  3116. if(pAMISetTriggerEventReq->EnableDisableFlag)
  3117. {
  3118. if(UNSPECIFIED_UTC_OFFSET == ((INT16)pBMCInfo->GenConfig.SELTimeUTCOffset))
  3119. {
  3120. LocalTime = GET_SYSTEM_TIME_STAMP ();
  3121. }
  3122. else
  3123. {
  3124. LocalTime = GET_SYSTEM_TIME_STAMP() + (((INT16)pBMCInfo->GenConfig.SELTimeUTCOffset) * 60);
  3125. }
  3126. TriggerTime = ipmitoh_u32(pAMISetTriggerEventReq->TriggerData.Time);
  3127. if(TriggerTime<LocalTime)
  3128. {
  3129. TDBG("Invalid Time Specified\n");
  3130. *pRes = OEMCC_INV_DATE_TIME;
  3131. return sizeof(INT8U);
  3132. }
  3133. pBMCInfo->TriggerEvent.Time = pAMISetTriggerEventReq->TriggerData.Time;
  3134. }
  3135. pBMCInfo->TriggerEvent.SpecDateTime = pAMISetTriggerEventReq->EnableDisableFlag;
  3136. break;
  3137. case LPC_RESET_FLAG:
  3138. pBMCInfo->TriggerEvent.LPCResetFlag = pAMISetTriggerEventReq->EnableDisableFlag;
  3139. break;
  3140. default:
  3141. pAMISetTriggerEventRes->CompletionCode = CC_PARAM_NOT_SUPPORTED;
  3142. return sizeof(INT8U);
  3143. }
  3144. pAMISetTriggerEventRes->CompletionCode = CC_NORMAL;
  3145. FlushIPMI((INT8U*)&pBMCInfo->TriggerEvent,(INT8U*)&pBMCInfo->TriggerEvent,pBMCInfo->IPMIConfLoc.TriggerEventAddr,
  3146. sizeof(TriggerEventCfg_T),BMCInst);
  3147. return sizeof(*pAMISetTriggerEventRes);
  3148. }
  3149. /**
  3150. * *@fn AMIGetTriggerEvent
  3151. * *@brief This command is used to get the enable/disable trigger event based on the parameter input
  3152. * *@param pReq - Request for the command
  3153. * *@param ReqLen - Request length for the command
  3154. * *@param pRes - Response for the command
  3155. * *@param BMCInst - BMC Instance Value
  3156. **/
  3157. int AMIGetTriggerEvent( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes ,int BMCInst )
  3158. {
  3159. _NEAR_ AMIGetTriggerEventReq_T *pAMIGetTriggerEventReq = (AMIGetTriggerEventReq_T *)pReq;
  3160. _NEAR_ AMIGetTriggerEventRes_T *pAMIGetTriggerEventRes = (AMIGetTriggerEventRes_T *)pRes;
  3161. BMCInfo_t *pBMCInfo = &g_BMCInfo[BMCInst];
  3162. pAMIGetTriggerEventRes->AMIGetTriggerEvent.CompletionCode = CC_NORMAL;
  3163. switch(pAMIGetTriggerEventReq->TriggerParam)
  3164. {
  3165. case CRITICAL:
  3166. pAMIGetTriggerEventRes->AMIGetTriggerEvent.EnableDisableFlag = pBMCInfo->TriggerEvent.CriticalFlag;
  3167. return sizeof(AMIGetTriggerEvent_T);
  3168. case NON_CRITICAL:
  3169. pAMIGetTriggerEventRes->AMIGetTriggerEvent.EnableDisableFlag = pBMCInfo->TriggerEvent.NONCriticalFlag;
  3170. return sizeof(AMIGetTriggerEvent_T);
  3171. case NON_RECOVERABLE:
  3172. pAMIGetTriggerEventRes->AMIGetTriggerEvent.EnableDisableFlag = pBMCInfo->TriggerEvent.NONRecoverableFlag;
  3173. return sizeof(AMIGetTriggerEvent_T);
  3174. case FAN_TROUBLED:
  3175. pAMIGetTriggerEventRes->AMIGetTriggerEvent.EnableDisableFlag = pBMCInfo->TriggerEvent.FanTroubled;
  3176. return sizeof(AMIGetTriggerEvent_T);
  3177. case BMC_WDT_EXPIRE:
  3178. pAMIGetTriggerEventRes->AMIGetTriggerEvent.EnableDisableFlag = pBMCInfo->TriggerEvent.WDTTimeExpire;
  3179. return sizeof(AMIGetTriggerEvent_T);
  3180. case SYS_DC_ON:
  3181. pAMIGetTriggerEventRes->AMIGetTriggerEvent.EnableDisableFlag = pBMCInfo->TriggerEvent.SysDConFlag;
  3182. return sizeof(AMIGetTriggerEvent_T);
  3183. case SYS_DC_OFF:
  3184. pAMIGetTriggerEventRes->AMIGetTriggerEvent.EnableDisableFlag = pBMCInfo->TriggerEvent.SysDCoffFlag;
  3185. return sizeof(AMIGetTriggerEvent_T);
  3186. case SYS_RESET:
  3187. pAMIGetTriggerEventRes->AMIGetTriggerEvent.EnableDisableFlag = pBMCInfo->TriggerEvent.SysResetFlag;
  3188. return sizeof(AMIGetTriggerEvent_T);
  3189. case SPEC_DATE_TIME:
  3190. pAMIGetTriggerEventRes->AMIGetTriggerEvent.EnableDisableFlag =pBMCInfo->TriggerEvent.SpecDateTime;
  3191. pAMIGetTriggerEventRes->TriggerData.Time =pBMCInfo->TriggerEvent.Time;
  3192. if(pBMCInfo->TriggerEvent.Time == 0)
  3193. pAMIGetTriggerEventRes->TriggerData.Time = GET_SYSTEM_TIME_STAMP();
  3194. return sizeof(AMIGetTriggerEvent_T)+sizeof(pAMIGetTriggerEventRes->TriggerData.Time);
  3195. case LPC_RESET_FLAG:
  3196. pAMIGetTriggerEventRes->AMIGetTriggerEvent.EnableDisableFlag =pBMCInfo->TriggerEvent.LPCResetFlag;
  3197. return sizeof(AMIGetTriggerEvent_T);
  3198. default:
  3199. pAMIGetTriggerEventRes->AMIGetTriggerEvent.CompletionCode = CC_PARAM_NOT_SUPPORTED;
  3200. return sizeof(INT8U);
  3201. }
  3202. pAMIGetTriggerEventRes->AMIGetTriggerEvent.CompletionCode = CC_INV_DATA_FIELD;
  3203. return sizeof(INT8U);
  3204. }
  3205. /**
  3206. * *@fn AMIGetSolConf
  3207. * *@brief This command is used to get the SOL configuration
  3208. * *@param pReq - Request for the command
  3209. * *@param ReqLen - Request length for the command
  3210. * *@param pRes - Response for the command
  3211. * *@param BMCInst - BMC Instance Value
  3212. **/
  3213. int AMIGetSolConf( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes ,int BMCInst )
  3214. {
  3215. _NEAR_ AMIGetSOLConfRes_T *pAMIGetSOLConfRes = (AMIGetSOLConfRes_T *)pRes;
  3216. BMCInfo_t *pBMCInfo = &g_BMCInfo[BMCInst];
  3217. INTU strsize = 0;
  3218. memset(pRes,0,sizeof(AMIGetSOLConfRes_T));
  3219. pAMIGetSOLConfRes->SolSessionTimeout = pBMCInfo->IpmiConfig.SOLSessionTimeOut;
  3220. TDBG("SOL Session Timeout is %d \n",pAMIGetSOLConfRes->SolSessionTimeout);
  3221. strsize = strlen(pBMCInfo->IpmiConfig.pSOLPort);
  3222. strncpy(pAMIGetSOLConfRes->SolIfcPort,pBMCInfo->IpmiConfig.pSOLPort,strsize);
  3223. pAMIGetSOLConfRes->SolIfcPort[strsize] = '\0';
  3224. TDBG("SOL Port Configuration is %s \n",pAMIGetSOLConfRes->SolIfcPort);
  3225. pAMIGetSOLConfRes->CompletionCode = CC_NORMAL;
  3226. return sizeof(AMIGetSOLConfRes_T);
  3227. }
  3228. /**
  3229. * *@fn AMISetLoginAuditConfig
  3230. * *@brief This command is used to enable/disable login audit based on the parameter input
  3231. * *@param pReq - Request for the command
  3232. * *@param ReqLen - Request length for the command
  3233. * *@param pRes - Response for the command
  3234. * *@param BMCInst - BMC Instance Value
  3235. **/
  3236. int AMISetLoginAuditConfig( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes ,int BMCInst )
  3237. {
  3238. _NEAR_ AMISetLoginAuditCfgReq_T *pAMISetLoginAuditCfgReq = (AMISetLoginAuditCfgReq_T *)pReq;
  3239. _NEAR_ AMISetLoginAuditCfgRes_T *pAMISetLoginAuditCfgRes = (AMISetLoginAuditCfgRes_T *)pRes;
  3240. BMCInfo_t *pBMCInfo = &g_BMCInfo[BMCInst];
  3241. /* Chk request Length */
  3242. if(ReqLen != sizeof(AMISetLoginAuditCfgReq_T) )
  3243. {
  3244. *pRes = CC_REQ_INV_LEN;
  3245. return sizeof (INT8U);
  3246. }
  3247. /* Check Reserved bits */
  3248. if( (pAMISetLoginAuditCfgReq->WebLogAuditCfg & 0xF0) || (pAMISetLoginAuditCfgReq->IPMILogAuditCfg & 0xF0)
  3249. || (pAMISetLoginAuditCfgReq->TelnetLogAuditCfg & 0xF0) || (pAMISetLoginAuditCfgReq->SSHLogAuditCfg & 0xF0)
  3250. || (pAMISetLoginAuditCfgReq->KVMLogAuditCfg & 0xF0))
  3251. {
  3252. *pRes = CC_INV_DATA_FIELD;
  3253. return sizeof(INT8U);
  3254. }
  3255. pBMCInfo->LoginAuditCfg.WebEventMask = pAMISetLoginAuditCfgReq->WebLogAuditCfg;
  3256. pBMCInfo->LoginAuditCfg.IPMIEventMask = pAMISetLoginAuditCfgReq->IPMILogAuditCfg;
  3257. pBMCInfo->LoginAuditCfg.TelnetEventMask = pAMISetLoginAuditCfgReq->TelnetLogAuditCfg;
  3258. pBMCInfo->LoginAuditCfg.SSHEventMask = pAMISetLoginAuditCfgReq->SSHLogAuditCfg;
  3259. pBMCInfo->LoginAuditCfg.KVMEventMask = pAMISetLoginAuditCfgReq->KVMLogAuditCfg;
  3260. FlushIPMI((INT8U*)&pBMCInfo->LoginAuditCfg,(INT8U*)&pBMCInfo->LoginAuditCfg,pBMCInfo->IPMIConfLoc.LoginAuditCfgAddr,
  3261. sizeof(LoginAuditConfig_T),BMCInst);
  3262. pAMISetLoginAuditCfgRes->CompletionCode = CC_NORMAL;
  3263. return sizeof(AMISetLoginAuditCfgRes_T);
  3264. }
  3265. /**
  3266. * *@fn AMIGetLoginAuditConfig
  3267. * *@brief This command is used to get the enable/disable login audit based on the parameter input
  3268. * *@param pReq - Request for the command
  3269. * *@param ReqLen - Request length for the command
  3270. * *@param pRes - Response for the command
  3271. * *@param BMCInst - BMC Instance Value
  3272. **/
  3273. int AMIGetLoginAuditConfig( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes ,int BMCInst )
  3274. {
  3275. _NEAR_ AMIGetLoginAuditCfgRes_T *pAMIGetLoginAuditCfgRes = (AMIGetLoginAuditCfgRes_T *)pRes;
  3276. BMCInfo_t *pBMCInfo = &g_BMCInfo[BMCInst];
  3277. /* Chk request Length */
  3278. if( ReqLen )
  3279. {
  3280. *pRes = CC_REQ_INV_LEN;
  3281. return sizeof (INT8U);
  3282. }
  3283. pAMIGetLoginAuditCfgRes->CompletionCode = CC_NORMAL;
  3284. pAMIGetLoginAuditCfgRes->WebLogAuditCfg = pBMCInfo->LoginAuditCfg.WebEventMask;
  3285. pAMIGetLoginAuditCfgRes->IPMILogAuditCfg = pBMCInfo->LoginAuditCfg.IPMIEventMask;
  3286. pAMIGetLoginAuditCfgRes->TelnetLogAuditCfg = pBMCInfo->LoginAuditCfg.TelnetEventMask;
  3287. pAMIGetLoginAuditCfgRes->SSHLogAuditCfg = pBMCInfo->LoginAuditCfg.SSHEventMask;
  3288. pAMIGetLoginAuditCfgRes->KVMLogAuditCfg = pBMCInfo->LoginAuditCfg.KVMEventMask;
  3289. return sizeof(AMIGetLoginAuditCfgRes_T);
  3290. }
  3291. /**
  3292. **@fn AMIGetAllIPv6Address
  3293. **@brief This command is used to get the ipv6 address
  3294. **@param pReq - Request for the command
  3295. **@param ReqLen - Request length for the command
  3296. **@param pRes - Response for the command
  3297. **@param BMCInst - BMC Instance Value
  3298. **/
  3299. int
  3300. AMIGetAllIPv6Address ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes ,int BMCInst)
  3301. {
  3302. INT8U EthIndex;
  3303. INT8U netindex= 0xFF;
  3304. INT8U i =0;
  3305. char IfcName[16];
  3306. NWCFG_STRUCT NWConfig;
  3307. NWCFG6_STRUCT NWConfig6;
  3308. AMIGetIPv6AddrReq_T *pAMIGetIPv6AddrReq = (AMIGetIPv6AddrReq_T *)pReq;
  3309. AMIGetIPv6AddrRes_T *pAMIGetIPv6AddrRes = (AMIGetIPv6AddrRes_T *)pRes;
  3310. if ((sizeof (pAMIGetIPv6AddrRes->GlobalPrefix) < pAMIGetIPv6AddrReq->IPCnt) || \
  3311. ((pAMIGetIPv6AddrReq->IPCnt + pAMIGetIPv6AddrReq->Index) > sizeof (pAMIGetIPv6AddrRes->GlobalIPAddr[0])))
  3312. {
  3313. *pRes = CC_INV_DATA_FIELD;
  3314. return sizeof (INT8U);
  3315. }
  3316. EthIndex= GetEthIndex(pAMIGetIPv6AddrReq->ChannelNum & 0x0F, BMCInst);
  3317. if(0xff == EthIndex)
  3318. {
  3319. *pRes = CC_INV_DATA_FIELD;
  3320. return sizeof (INT8U);
  3321. }
  3322. memset(IfcName,0,sizeof(IfcName));
  3323. /*Get the EthIndex*/
  3324. if(GetIfcName(EthIndex,IfcName, BMCInst) == -1)
  3325. {
  3326. TCRIT("Error in Getting Ifc name\n");
  3327. *pRes = CC_INV_DATA_FIELD;
  3328. return sizeof (INT8U);
  3329. }
  3330. // for(i=0;i<sizeof(Ifcnametable)/sizeof(IfcName_T);i++)
  3331. // {
  3332. // if(strcmp(Ifcnametable[i].Ifcname,IfcName) == 0)
  3333. // {
  3334. // netindex= Ifcnametable[i].Index;
  3335. // break;
  3336. // }
  3337. // }
  3338. if(netindex == 0xFF)
  3339. {
  3340. *pRes = CC_INV_DATA_FIELD;
  3341. return sizeof (INT8U);
  3342. }
  3343. memset(pAMIGetIPv6AddrRes,0,sizeof(AMIGetIPv6AddrRes_T));
  3344. pAMIGetIPv6AddrRes->CompletionCode = 0;
  3345. nwReadNWCfg_v4_v6( &NWConfig, &NWConfig6, netindex,g_corefeatures.global_ipv6);
  3346. memcpy (pAMIGetIPv6AddrRes->GlobalIPAddr, &NWConfig6.GlobalIPAddr[pAMIGetIPv6AddrReq->Index], (pAMIGetIPv6AddrReq->IPCnt *sizeof (pAMIGetIPv6AddrRes->GlobalIPAddr[0])));
  3347. memcpy (&pAMIGetIPv6AddrRes->GlobalPrefix, &NWConfig6.GlobalPrefix[pAMIGetIPv6AddrReq->Index], (pAMIGetIPv6AddrReq->IPCnt *sizeof (pAMIGetIPv6AddrRes->GlobalPrefix[0])));
  3348. return sizeof(AMIGetIPv6AddrRes_T);
  3349. }
  3350. /**
  3351. * *@fn ValidateUsrInput
  3352. * *@brief This function validates the user input..
  3353. * *@param pAMISetPamReq - Req from the user.
  3354. * *@returns 0 on success
  3355. * * -1 on failure
  3356. **/
  3357. int
  3358. ValidateUsrInput (AMISetPamReq_T *pAMISetPamReq)
  3359. {
  3360. INT8U Index = 0;
  3361. INT8U Cntr = 0;
  3362. for (Index = 0;Index < MODULES_TO_MODIFIED; Index++)
  3363. {
  3364. /* check for value which is greater than order length*/
  3365. if ((pAMISetPamReq->Sqnce [Index] > MODULES_TO_MODIFIED) || (pAMISetPamReq->Sqnce [Index] < 1))
  3366. {
  3367. TDBG ("Value exceeds the length %d \n", pAMISetPamReq->Sqnce[Index]);
  3368. return ERR_RETRN;
  3369. }
  3370. for (Cntr =Index + 1; Cntr < MODULES_TO_MODIFIED; Cntr++)
  3371. {
  3372. /*Check for Duplicate value */
  3373. if ((pAMISetPamReq->Sqnce[Index]==pAMISetPamReq->Sqnce[Cntr]))
  3374. {
  3375. TDBG ("Duplicate value %d \n", pAMISetPamReq->Sqnce[Index]);
  3376. return ERR_RETRN;
  3377. }
  3378. }
  3379. }
  3380. return 0;
  3381. }
  3382. /**
  3383. * *@fn GetPamOrder
  3384. * *@brief This function is used to get PAM authentication order sequence (ipmi ldap ad).
  3385. * *@param FileName - name of the file to get the seqence.
  3386. * *@param FileOrder - Used to hold the sequence order.
  3387. * *@returns 0 on success
  3388. * * -1 on failure
  3389. **/
  3390. int
  3391. GetPamOrder (char *FileName, INT8U *FileOrder)
  3392. {
  3393. FILE *pfptr;
  3394. char LineBuf [PAM_BUF_LEN];
  3395. INT8U Cnt=0, Index =0, Ndx = 0, Mcnt = 0;
  3396. INT8U TmpBuf [MODULES_TO_MODIFIED + MODULES_TO_MODIFIED];
  3397. pfptr = fopen (FileName, "r");
  3398. if (NULL == pfptr)
  3399. {
  3400. TDBG ("Enable to open %s file\n", FileName);
  3401. return -1;
  3402. }
  3403. memset (TmpBuf, 0, (2*MODULES_TO_MODIFIED));
  3404. while (!feof(pfptr))
  3405. {
  3406. memset (LineBuf, 0, PAM_BUF_LEN);
  3407. if(NULL == fgets (LineBuf, PAM_BUF_LEN, pfptr))
  3408. {
  3409. continue;
  3410. }
  3411. for (Index = 0;Index < (sizeof (g_DfaultOrder) /sizeof (PamOrder_T)); Index++)
  3412. {
  3413. /*Read the order from file according to Section and store it an array*/
  3414. if (NULL != strstr (LineBuf, g_DfaultOrder[Index].PamModule))
  3415. {
  3416. /*Check the PAM order for Auth section*/
  3417. if (NULL != strstr (LineBuf, "auth"))
  3418. {
  3419. FileOrder[Cnt] = g_DfaultOrder[Index].SqnceNo;
  3420. Cnt++;
  3421. break;
  3422. }
  3423. /*Check the PAM order for Account section*/
  3424. else if (NULL != strstr (LineBuf, "account"))
  3425. {
  3426. TmpBuf [Ndx] = g_DfaultOrder[Index].SqnceNo;
  3427. Ndx++;
  3428. break;
  3429. }
  3430. /*Check the PAM order for Password section*/
  3431. else if (NULL != strstr (LineBuf, "password"))
  3432. {
  3433. TmpBuf [MODULES_TO_MODIFIED + Mcnt] = g_DfaultOrder[Index].SqnceNo;
  3434. Mcnt++;
  3435. break;
  3436. }
  3437. }
  3438. }
  3439. }
  3440. /*Compare All the orders */
  3441. for (Index = 0;Index < (sizeof (g_DfaultOrder) /sizeof (PamOrder_T)); Index++)
  3442. {
  3443. if ((FileOrder [Index] != TmpBuf [Index]) || (FileOrder [Index] != TmpBuf [MODULES_TO_MODIFIED + Index]))
  3444. {
  3445. fclose(pfptr);
  3446. return ERR_RETRN;
  3447. }
  3448. }
  3449. fclose (pfptr);
  3450. return 0;
  3451. }
  3452. /**
  3453. * *@fn CompPamOrder
  3454. * *@brief This function Compares the new pam order with existing order.
  3455. * *@param FileOrder - Existing pam order.
  3456. * *@param FileOrder - New PAM order.
  3457. * *@returns 0 if two orders are equal.
  3458. * * 1 if orders are not equal.
  3459. **/
  3460. int
  3461. CompPamOrder (INT8U *FileOrder, INT8U *NewOrder)
  3462. {
  3463. INT8U Index = 0;
  3464. for (Index = 0;Index < (sizeof (g_DfaultOrder) /sizeof (PamOrder_T)); Index++)
  3465. {
  3466. if (FileOrder [Index] != NewOrder[Index])
  3467. return 1;
  3468. }
  3469. return 0;
  3470. }
  3471. /**
  3472. * *@fn SetPamOrder
  3473. * *@brief This function Set the new pam order..
  3474. * *@param FileName - Name of the file.
  3475. * *@param pAMISetPamReq - New pam sequence from user.
  3476. * *@returns 0 on success
  3477. * * -1 on failure
  3478. **/
  3479. int
  3480. SetPamOrder (char *FileName, AMISetPamReq_T *pAMISetPamReq)
  3481. {
  3482. FILE *pPamfd, *pNewfd;
  3483. char TmpBuf [PAM_BUF_LEN];
  3484. char LineBuf [PAM_BUF_LEN];
  3485. char *ptr;
  3486. INT8U Order=0, Cnt=0;
  3487. INT8U Flag=0, Index =0;
  3488. pPamfd = fopen (FileName, "r");
  3489. if (NULL == pPamfd)
  3490. {
  3491. TDBG ("Enable to open %s file\n", FileName);
  3492. return ERR_RETRN;
  3493. }
  3494. pNewfd = fopen ("/conf/pam_tmpfile", "w");
  3495. if (NULL == pNewfd)
  3496. {
  3497. fclose(pPamfd);
  3498. TDBG ("Enable to create %s file\n", "pam_tmpfile");
  3499. return -1;
  3500. }
  3501. while (!feof(pPamfd))
  3502. {
  3503. memset (LineBuf, 0, PAM_BUF_LEN);
  3504. if(NULL == fgets (LineBuf, PAM_BUF_LEN, pPamfd))
  3505. {
  3506. continue;
  3507. }
  3508. Flag = 0;
  3509. for (Index = 0;Index < MODULES_TO_MODIFIED; Index++)
  3510. {
  3511. memset (TmpBuf, 0, PAM_BUF_LEN);
  3512. /*Replace the old sequence with new order*/
  3513. if (NULL != (ptr = strstr (LineBuf, g_DfaultOrder[Index].PamModule)))
  3514. {
  3515. strncpy (TmpBuf, LineBuf, (ptr-LineBuf));
  3516. Order = pAMISetPamReq->Sqnce[Cnt];
  3517. strcat (TmpBuf, g_DfaultOrder[Order -1].PamModule);
  3518. Cnt ++;
  3519. if (Cnt == MODULES_TO_MODIFIED)
  3520. Cnt = 0;
  3521. Flag = 1;
  3522. break;
  3523. }
  3524. }
  3525. if (Flag)
  3526. fprintf (pNewfd,"%s\n", TmpBuf);
  3527. else
  3528. fprintf (pNewfd,"%s", LineBuf);
  3529. }
  3530. fclose (pNewfd);
  3531. fclose (pPamfd);
  3532. rename("/conf/pam_tmpfile", FileName);
  3533. return 0;
  3534. }
  3535. /**
  3536. * *@fn SetPamOrder
  3537. * *@brief This function Set the new pam order..
  3538. * *@param FileName - Name of the file.
  3539. * *@param pAMISetPamReq - New pam sequence from user.
  3540. * *@param FileOrder - Existing pam auth sequence order.
  3541. **/
  3542. int SetNssFileOrder (char *FileName, AMISetPamReq_T *pAMISetPamReq, INT8U *FileOrder)
  3543. {
  3544. char TmpBuf [PAM_BUF_LEN];
  3545. char LineBuf [PAM_BUF_LEN];
  3546. FILE *pPamfd, *pNewfd;
  3547. char *ptr;
  3548. INT8U Order=0;
  3549. INT8U Index =0;
  3550. pPamfd = fopen (FileName, "r");
  3551. if (NULL == pPamfd)
  3552. {
  3553. TDBG ("Enable to open %s file\n", FileName);
  3554. return ERR_RETRN;
  3555. }
  3556. pNewfd = fopen ("/conf/pam_tmpfile", "w");
  3557. if (NULL == pNewfd)
  3558. {
  3559. fclose(pPamfd);
  3560. TDBG ("Enable to create %s file\n", "pam_tmpfile");
  3561. return -1;
  3562. }
  3563. memset (LineBuf, 0, PAM_BUF_LEN);
  3564. memset (TmpBuf, 0, PAM_BUF_LEN);
  3565. if (NULL == fgets (LineBuf, PAM_BUF_LEN, pPamfd))
  3566. {
  3567. fclose(pPamfd);
  3568. fclose(pNewfd);
  3569. return ERR_RETRN;
  3570. }
  3571. Order = FileOrder[0];
  3572. if (NULL != (ptr = strstr (LineBuf, g_NssOrder[Order -1].PamModule)))
  3573. {
  3574. strncpy (TmpBuf, LineBuf, (ptr-LineBuf));
  3575. }
  3576. for (Index = 0;Index < (sizeof (g_DfaultOrder) /sizeof (PamOrder_T));Index++)
  3577. {
  3578. Order = pAMISetPamReq->Sqnce[Index];
  3579. strcat (TmpBuf, g_NssOrder[Order -1].PamModule);
  3580. strcat (TmpBuf, " ");
  3581. }
  3582. Order = FileOrder[Index -1];
  3583. if (NULL != (ptr = (strstr (LineBuf, g_NssOrder[Order - 1].PamModule))+strlen(g_NssOrder[Order - 1].PamModule)))
  3584. {
  3585. Order = strlen (g_NssOrder[Order - 1].PamModule);
  3586. strcat (TmpBuf, LineBuf + (ptr-LineBuf) + Order);
  3587. }
  3588. fprintf (pNewfd,"%s\n", TmpBuf);
  3589. fclose (pNewfd);
  3590. fclose (pPamfd);
  3591. rename( "/conf/pam_tmpfile", FileName);
  3592. return 0;
  3593. }
  3594. /**
  3595. * *@fn ArrangOrder
  3596. * *@brief This command is used to to arrange the values to assending order
  3597. * *@param FileOrder - array to store the cuurent order.
  3598. **/
  3599. void
  3600. ArrangOrder (INT8U *FileOrder)
  3601. {
  3602. INT8U Index =0,Tmp=0, Cntr =0;
  3603. for(Index = 0; Index < sizeof (g_NssOrder)/ sizeof (PamOrder_T); Index++)
  3604. {
  3605. for(Cntr = 0; Cntr < (sizeof (g_NssOrder)/ sizeof (PamOrder_T))-1; Cntr++)
  3606. {
  3607. if(FileOrder[Cntr]> FileOrder[Cntr + 1])
  3608. {
  3609. Tmp = FileOrder[Cntr + 1];
  3610. FileOrder[Cntr + 1] = FileOrder[Cntr];
  3611. FileOrder[Cntr] = Tmp;
  3612. }
  3613. }
  3614. }
  3615. }
  3616. /**
  3617. * *@fn GetNssOrder
  3618. * *@brief This command is used to get NSS sequence (ipmi ldap ad).
  3619. * *@param FileName - Name of the file
  3620. * *@param FileOrder - array to store the cuurent order.
  3621. * *@returns 0 on success
  3622. * * -1 on failure
  3623. **/
  3624. int GetNssOrder (char *FileName, INT8U *FileOrder)
  3625. {
  3626. char LineBuf [PAM_BUF_LEN];
  3627. FILE *pPamfd;
  3628. char *ptr = NULL;
  3629. INT8U Index =0,Cntr = 0;
  3630. INT8U TmpBuf [MODULES_TO_MODIFIED];
  3631. pPamfd = fopen (FileName, "r");
  3632. if (NULL == pPamfd)
  3633. {
  3634. TDBG ("Enable to open %s file\n", FileName);
  3635. return ERR_RETRN;
  3636. }
  3637. memset (LineBuf, 0, PAM_BUF_LEN);
  3638. if (NULL == fgets (LineBuf, PAM_BUF_LEN, pPamfd))
  3639. {
  3640. fclose(pPamfd);
  3641. return ERR_RETRN;
  3642. }
  3643. /*Find the position of the strings (ipmi ldap ad)*/
  3644. for (Index = 0;Index < sizeof (g_NssOrder)/ sizeof (PamOrder_T); Index++)
  3645. {
  3646. if (NULL != (ptr = strstr (LineBuf, g_NssOrder[Index].PamModule)))
  3647. {
  3648. TmpBuf [Cntr] = (ptr -LineBuf);
  3649. Cntr ++;
  3650. }
  3651. }
  3652. if (Cntr != sizeof (g_NssOrder)/ sizeof (PamOrder_T))
  3653. {
  3654. TDBG ("One of the Module not found");
  3655. fclose(pPamfd);
  3656. return ERR_RETRN;
  3657. }
  3658. /* arrenge into assending order*/
  3659. ArrangOrder (TmpBuf);
  3660. Cntr = 0;
  3661. while (Cntr <sizeof (g_NssOrder)/ sizeof (PamOrder_T) )
  3662. {
  3663. for (Index = 0;Index < sizeof (g_NssOrder)/ sizeof (PamOrder_T); Index++)
  3664. {
  3665. if (0 == strncmp (LineBuf + TmpBuf [Cntr], g_NssOrder[Index].PamModule, strlen (g_NssOrder[Index].PamModule)))
  3666. {
  3667. FileOrder[Cntr] = Index + 1;
  3668. Cntr ++;
  3669. break;
  3670. }
  3671. }
  3672. }
  3673. fclose(pPamfd);
  3674. return 0;
  3675. }
  3676. int
  3677. GetAllFilesOrder (AMIGetPamRes_T *pAMIGetPamRes)
  3678. {
  3679. INT8U TmpBuf [MODULES_TO_MODIFIED];
  3680. /*Get the PAM sequence order for file pam_wounix*/
  3681. if (ERR_RETRN == GetPamOrder (PAM_WOUNIX_FILE, pAMIGetPamRes->Seqnce))
  3682. {
  3683. TDBG ("Pam order bw different sec are %s not same\n", PAM_WOUNIX_FILE);
  3684. pAMIGetPamRes->CompletionCode = CC_PAM_ORDER_SEC_DIFERS;
  3685. return ERR_RETRN;
  3686. }
  3687. /*Get the PAM sequence order for file pam_witunix*/
  3688. if (ERR_RETRN == GetPamOrder (PAM_WITHUNIX_FILE, TmpBuf))
  3689. {
  3690. TDBG ("Pam order bw different sec are %s not same\n", PAM_WITHUNIX_FILE);
  3691. pAMIGetPamRes->CompletionCode = CC_PAM_ORDER_SEC_DIFERS;
  3692. return ERR_RETRN;
  3693. }
  3694. /*Verify All the orders are Same */
  3695. if (1 == CompPamOrder (TmpBuf, pAMIGetPamRes->Seqnce))
  3696. {
  3697. TDBG ("Orders are not same for files %s & %s \n", PAM_WOUNIX_FILE, PAM_WITHUNIX_FILE);
  3698. pAMIGetPamRes->CompletionCode = CC_PAM_ORDER_FILE_DIFERS;
  3699. return ERR_RETRN;
  3700. }
  3701. /*Get the NSS sequence order for nsswitch file */
  3702. if (ERR_RETRN == GetNssOrder (PAM_NSSWITCH_FILE, pAMIGetPamRes->Seqnce))
  3703. {
  3704. TDBG ("Pam order bw different sec are %s not same\n", PAM_NSSWITCH_FILE);
  3705. pAMIGetPamRes->CompletionCode = CC_PAM_ORDER_SEC_DIFERS;
  3706. return ERR_RETRN;
  3707. }
  3708. /*Verify All the orders are Same */
  3709. if (1 == CompPamOrder (TmpBuf, pAMIGetPamRes->Seqnce))
  3710. {
  3711. TDBG ("Orders are not same for files %s & %s \n", PAM_WOUNIX_FILE, PAM_NSSWITCH_FILE);
  3712. pAMIGetPamRes->CompletionCode = CC_PAM_ORDER_FILE_DIFERS;
  3713. return ERR_RETRN;
  3714. }
  3715. return 0;
  3716. }
  3717. /**
  3718. * *@fn AMIGetPamOrder
  3719. * *@brief This command is used to get PAM authentication order sequence (ipmi ldap ad).
  3720. * *@param pReq - Request for the command
  3721. * *@param ReqLen - Request length for the command
  3722. * *@param pRes - Response for the command
  3723. * *@param BMCInst - BMC Instance Value
  3724. **/
  3725. int
  3726. AMIGetPamOrder ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes, int BMCInst)
  3727. {
  3728. AMIGetPamRes_T *pAMIGetPamRes = (AMIGetPamRes_T *)pRes;
  3729. if(g_corefeatures.pam_reorder == ENABLED)
  3730. {
  3731. if (ERR_RETRN == GetAllFilesOrder (pAMIGetPamRes))
  3732. {
  3733. return sizeof (AMISetPamRes_T);
  3734. }
  3735. pAMIGetPamRes->CompletionCode = CC_NORMAL;
  3736. }
  3737. else
  3738. {
  3739. pAMIGetPamRes->CompletionCode = CC_INV_CMD;
  3740. }
  3741. return sizeof (AMIGetPamRes_T);
  3742. }
  3743. /**
  3744. * *@fn SetPamSqnceOrder
  3745. * *@brief This function Set the new pam order..
  3746. * *@param FileName - Name of the file.
  3747. * *@param pAMISetPamReq - New pam sequence from user.
  3748. * *@returns 0 on success
  3749. **/
  3750. int
  3751. SetPamSqnceOrder (char *FileName, AMISetPamReq_T *pAMISetPamReq)
  3752. {
  3753. INT8U FileOrder [MODULES_TO_MODIFIED];
  3754. /*Get the authentication sequence order from the file */
  3755. if (ERR_RETRN == GetPamOrder (FileName, FileOrder))
  3756. {
  3757. SetPamOrder (FileName, pAMISetPamReq);
  3758. return 0;
  3759. }
  3760. /*Compare the PAM orders if same , do not reorder*/
  3761. if (0 == CompPamOrder (FileOrder, pAMISetPamReq->Sqnce))
  3762. {
  3763. TDBG ("PAM auth orders are same\n");
  3764. return 1;
  3765. }
  3766. /*reorder the files according to the user input */
  3767. SetPamOrder (FileName, pAMISetPamReq);
  3768. return 0;
  3769. }
  3770. /**
  3771. * *@fn SetNssSqnceOrder
  3772. * *@brief This function Set the new NSS order..
  3773. * *@param FileName - Name of the file.
  3774. * *@param pAMISetPamReq - New pam sequence from user.
  3775. * *@returns 0 on success
  3776. **/
  3777. int
  3778. SetNssSqnceOrder (char *FileName, AMISetPamReq_T *pAMISetPamReq)
  3779. {
  3780. INT8U TmpBuf [MODULES_TO_MODIFIED];
  3781. memset (TmpBuf, 0 , MODULES_TO_MODIFIED);
  3782. if (ERR_RETRN == GetNssOrder (FileName, TmpBuf))
  3783. {
  3784. SetNssFileOrder (FileName, pAMISetPamReq, TmpBuf);
  3785. return 0;
  3786. }
  3787. /*Compare the PAM orders if same , do not reorder*/
  3788. if (0 == CompPamOrder (pAMISetPamReq->Sqnce, TmpBuf))
  3789. {
  3790. TDBG ("PAM auth orders are same\n");
  3791. return 1;
  3792. }
  3793. SetNssFileOrder (FileName, pAMISetPamReq, TmpBuf);
  3794. return 0;
  3795. }
  3796. /**
  3797. * *@fn AMISetPamOrder
  3798. * *@brief This command is used to set PAM authentication sequence order(ipmi ldap ad).
  3799. * *@param pReq - Request for the command
  3800. * *@param ReqLen - Request length for the command
  3801. * *@param pRes - Response for the command
  3802. * *@param BMCInst - BMC Instance Value
  3803. **/
  3804. int
  3805. AMISetPamOrder ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes,int BMCInst)
  3806. {
  3807. AMISetPamRes_T *pAMISetPamRes = (AMISetPamRes_T *)pRes;
  3808. INT8U *curchannel;
  3809. if(g_corefeatures.pam_reorder == ENABLED)
  3810. {
  3811. int ret;
  3812. RestartService_T Service;
  3813. AMISetPamReq_T *pAMISetPamReq = (AMISetPamReq_T *)pReq;
  3814. AMIGetPamRes_T pAMIGetPamRes;
  3815. memset (&pAMIGetPamRes, 0, sizeof (AMIGetPamRes_T));
  3816. if (ERR_RETRN == ValidateUsrInput (pAMISetPamReq))
  3817. {
  3818. TDBG ("In-Valid user input\n");
  3819. *pRes = CC_INV_DATA_FIELD;
  3820. return sizeof (AMISetPamRes_T);
  3821. }
  3822. /*Check the previous and New order if same, return without changing the order*/
  3823. if (0 == GetAllFilesOrder (&pAMIGetPamRes))
  3824. {
  3825. if (0 == CompPamOrder (pAMIGetPamRes.Seqnce, pAMISetPamReq->Sqnce))
  3826. {
  3827. TDBG ("PAM auth orders are same\n");
  3828. *pRes = CC_SAME_PAM_ORDER;
  3829. return sizeof (AMISetPamRes_T);
  3830. }
  3831. }
  3832. SetPamSqnceOrder (PAM_WOUNIX_FILE, pAMISetPamReq);
  3833. SetPamSqnceOrder (PAM_WITHUNIX_FILE, pAMISetPamReq);
  3834. ret=SetNssSqnceOrder (PAM_NSSWITCH_FILE, pAMISetPamReq);
  3835. if(ret == 0)
  3836. {
  3837. Service.ServiceName = WEBSERVER;
  3838. SetPendStatus(PEND_OP_RESTART_SERVICES, PEND_STATUS_PENDING);
  3839. OS_THREAD_TLS_GET(g_tls.CurChannel,curchannel);
  3840. PostPendTask(PEND_OP_RESTART_SERVICES, (INT8U *) &Service, 1, *curchannel & 0xF,BMCInst);
  3841. }
  3842. pAMISetPamRes->CompletionCode = CC_NORMAL;
  3843. }
  3844. else
  3845. {
  3846. pAMISetPamRes->CompletionCode = CC_INV_CMD;
  3847. }
  3848. return sizeof (AMISetPamRes_T);
  3849. }
  3850. typedef struct {
  3851. INT8U Length;
  3852. } PACKED FWConfParams_T;
  3853. FWConfParams_T FWConfParams[] = {
  3854. { 0 },
  3855. { 200 },
  3856. { 200 },
  3857. { 1 },
  3858. { 1 }
  3859. };
  3860. /**
  3861. * *@fn AMIStartTFTPFwUpdate
  3862. * *@brief This command is used to start application to do firmware update
  3863. * *@param pReq - Request for the command
  3864. * *@param ReqLen - Request length for the command
  3865. * *@param pRes - Response for the command
  3866. * *@param BMCInst - BMC Instance Value
  3867. **/
  3868. int
  3869. AMIStartTFTPFwUpdate ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes, int BMCInst)
  3870. {
  3871. AMIStartFwUpdateReq_T *pAMIStartFwUpdateReq = (AMIStartFwUpdateReq_T *)pReq;
  3872. AMIStartFwUpdateRes_T *pAMIStartFwUpdateRes = (AMIStartFwUpdateRes_T *)pRes;
  3873. FirmwareConfig_T FwConfig;
  3874. INT8U *curchannel;
  3875. if(IsCardInFlashMode()) {
  3876. pAMIStartFwUpdateRes->CompletionCode = CC_DEV_IN_FIRMWARE_UPDATE_MODE;
  3877. return sizeof(INT8U);
  3878. }
  3879. if(g_corefeatures.fwupdate_protocol_select == CORE_FEATURE_ENABLED)
  3880. {
  3881. getFirmwareConfig(&FwConfig);
  3882. if((!FwConfig.Protocol.tftp.Host[0]) ||
  3883. (!FwConfig.Protocol.tftp.RemoteFilePath[0])) {
  3884. pAMIStartFwUpdateRes->CompletionCode = CC_OP_NOT_ALLOWED;
  3885. return sizeof(INT8U);
  3886. }
  3887. FwConfig.Protocol.tftp.PreserveCfg = pAMIStartFwUpdateReq->PreserveCfg;
  3888. IPMI_DBG_PRINT("Starting TFTP firmware update...\n");
  3889. SetPendStatus(PEND_OP_START_FW_UPDATE_TFTP, PEND_STATUS_PENDING);
  3890. setFirmwareConfig(&FwConfig);
  3891. OS_THREAD_TLS_GET(g_tls.CurChannel,curchannel);
  3892. PostPendTask(PEND_OP_START_FW_UPDATE_TFTP, (INT8U*)&FwConfig, sizeof(FirmwareConfig_T),
  3893. *curchannel & 0xF, BMCInst);
  3894. LOCK_BMC_SHARED_MEM(BMCInst);
  3895. g_MBMCInfo.FlashType= TFTP_FLASH;
  3896. UNLOCK_BMC_SHARED_MEM(BMCInst);
  3897. pAMIStartFwUpdateRes->CompletionCode = CC_NORMAL;
  3898. return sizeof(AMIStartFwUpdateRes_T);
  3899. }
  3900. else
  3901. {
  3902. pAMIStartFwUpdateRes->CompletionCode = CC_INV_CMD;
  3903. return sizeof(INT8U);
  3904. }
  3905. }
  3906. int
  3907. AMIGetTftpProgressStatus( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes, int BMCInst)
  3908. {
  3909. STRUCTURED_PROGRESS_STATUS prog;
  3910. AMIGetTftpProgressStatusRes_T *pAMIGetTftpProgressStatusRes = (AMIGetTftpProgressStatusRes_T *)pRes;
  3911. /*
  3912. if(!IsCardInFlashMode()) {
  3913. pAMIGetTftpProgressStatusRes->CompletionCode = CC_COULD_NOT_PROVIDE_RESP;
  3914. return sizeof(INT8U);
  3915. }
  3916. */
  3917. if(g_corefeatures.fwupdate_protocol_select == CORE_FEATURE_ENABLED)
  3918. {
  3919. memset(&prog, 0, sizeof(STRUCTURED_PROGRESS_STATUS));
  3920. pAMIGetTftpProgressStatusRes->CompletionCode = CC_NORMAL;
  3921. GetProgressStatus(&prog);
  3922. pAMIGetTftpProgressStatusRes->Progress = prog.Progress;
  3923. pAMIGetTftpProgressStatusRes->Overall = prog.Overall;
  3924. return sizeof(AMIGetTftpProgressStatusRes_T);
  3925. }
  3926. else
  3927. {
  3928. pAMIGetTftpProgressStatusRes->CompletionCode = CC_INV_CMD;
  3929. return sizeof(INT8U);
  3930. }
  3931. }
  3932. /**
  3933. * *@fn AMISetFWCfg
  3934. * *@brief This command is used to set configuration of firmware update
  3935. * *@param pReq - Request for the command
  3936. * *@param ReqLen - Request length for the command
  3937. * *@param pRes - Response for the command
  3938. * *@param BMCInst - BMC Instance Value
  3939. **/
  3940. int
  3941. AMISetFWCfg ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes, int BMCInst)
  3942. {
  3943. AMISetFWCfgReq_T *pAMISetFWCfgReq = (AMISetFWCfgReq_T *)pReq;
  3944. AMISetFWCfgRes_T *pAMISetFWCfgRes = (AMISetFWCfgRes_T *)pRes;
  3945. int paramTblSize = sizeof(FWConfParams) / sizeof(FWConfParams_T);
  3946. int dataLen = ReqLen - 1;
  3947. int Parameter = pAMISetFWCfgReq->Parameter;
  3948. int Ret = 0;
  3949. char ipaddrstr[INET6_ADDRSTRLEN];
  3950. FirmwareConfig_T FwConfig;
  3951. char nulldata[SET_FIRM_REQ_BYTES]={0};
  3952. if(IsCardInFlashMode()) {
  3953. pAMISetFWCfgRes->CompletionCode = CC_DEV_IN_FIRMWARE_UPDATE_MODE;
  3954. return sizeof(INT8U);
  3955. }
  3956. if(g_corefeatures.fwupdate_protocol_select == CORE_FEATURE_ENABLED)
  3957. {
  3958. if(Parameter >= paramTblSize) {
  3959. pAMISetFWCfgRes->CompletionCode = CC_PARAM_OUT_OF_RANGE;
  3960. return sizeof(INT8U);
  3961. }
  3962. if(dataLen != FWConfParams[Parameter].Length) {
  3963. pAMISetFWCfgRes->CompletionCode = CC_REQ_INV_LEN;
  3964. return sizeof(INT8U);
  3965. }
  3966. getFirmwareConfig(&FwConfig);
  3967. switch(Parameter)
  3968. {
  3969. case CONNECTION_INFO_HOST:
  3970. if(memcmp(pAMISetFWCfgReq->Data,nulldata,SET_FIRM_REQ_BYTES))
  3971. {
  3972. Ret = GetIPAddrstr(pAMISetFWCfgReq->Data,ipaddrstr);
  3973. if((Ret == -1) || (strcmp(ipaddrstr,INVALID_IP_ADDR) == 0))
  3974. {
  3975. TDBG("Error in getting IP address string\n");
  3976. pAMISetFWCfgRes->CompletionCode = CC_INV_DATA_FIELD;
  3977. return sizeof(INT8U);
  3978. }
  3979. }
  3980. memcpy(&FwConfig.Protocol.tftp.Host, &pAMISetFWCfgReq->Data, FWConfParams[Parameter].Length);
  3981. break;
  3982. case CONNECTION_INFO_FILE_PATH:
  3983. memcpy(&FwConfig.Protocol.tftp.RemoteFilePath, &pAMISetFWCfgReq->Data, FWConfParams[Parameter].Length);
  3984. break;
  3985. case CONNECTION_INFO_RETRY:
  3986. memcpy(&FwConfig.Protocol.tftp.Retry, &pAMISetFWCfgReq->Data, FWConfParams[Parameter].Length);
  3987. break;
  3988. case FW_UPDATE_SELECT :
  3989. memcpy(&FwConfig.FlashFWSelect, &pAMISetFWCfgReq->Data, FWConfParams[Parameter].Length);
  3990. if(FwConfig.FlashFWSelect == FW_BMC)
  3991. safe_system("rm -f /var/bios_update_selection");
  3992. else if (FwConfig.FlashFWSelect == FW_BIOS)
  3993. safe_system("touch /var/bios_update_selection");
  3994. break;
  3995. default:
  3996. pAMISetFWCfgRes->CompletionCode = CC_PARAM_OUT_OF_RANGE;
  3997. return sizeof(INT8U);
  3998. break;
  3999. }
  4000. setFirmwareConfig(&FwConfig);
  4001. pAMISetFWCfgRes->CompletionCode = CC_NORMAL;
  4002. return sizeof(AMISetFWCfgRes_T);
  4003. }
  4004. else
  4005. {
  4006. pAMISetFWCfgRes->CompletionCode = CC_INV_CMD;
  4007. return sizeof(INT8U);
  4008. }
  4009. }
  4010. /**
  4011. * *@fn AMIGetFWCfg
  4012. * *@brief This command is used to get configuration of firmware update
  4013. * *@param pReq - Request for the command
  4014. * *@param ReqLen - Request length for the command
  4015. * *@param pRes - Response for the command
  4016. * *@param BMCInst - BMC Instance Value
  4017. **/
  4018. int
  4019. AMIGetFWCfg ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes, int BMCInst)
  4020. {
  4021. AMIGetFWCfgReq_T *pAMIGetFWCfgReq = (AMIGetFWCfgReq_T *)pReq;
  4022. AMIGetFWCfgRes_T *pAMIGetFWCfgRes = (AMIGetFWCfgRes_T *)pRes;
  4023. FirmwareConfig_T FwConfig;
  4024. int paramTblSize = sizeof(FWConfParams) / sizeof(FWConfParams_T);
  4025. int parameter = pAMIGetFWCfgReq->Parameter;
  4026. int res_size = 0;
  4027. if(IsCardInFlashMode()) {
  4028. pAMIGetFWCfgRes->CompletionCode = CC_DEV_IN_FIRMWARE_UPDATE_MODE;
  4029. return sizeof(INT8U);
  4030. }
  4031. if(g_corefeatures.fwupdate_protocol_select == CORE_FEATURE_ENABLED)
  4032. {
  4033. if(parameter > paramTblSize) {
  4034. pAMIGetFWCfgRes->CompletionCode = CC_PARAM_OUT_OF_RANGE;
  4035. return sizeof(INT8U);
  4036. }
  4037. getFirmwareConfig(&FwConfig);
  4038. res_size = sizeof(pAMIGetFWCfgReq->Parameter) + FWConfParams[parameter].Length;
  4039. switch(parameter)
  4040. {
  4041. case CONNECTION_INFO_HOST:
  4042. memcpy(&pAMIGetFWCfgRes->Data, &FwConfig.Protocol.tftp.Host, FWConfParams[parameter].Length);
  4043. break;
  4044. case CONNECTION_INFO_FILE_PATH:
  4045. memcpy(&pAMIGetFWCfgRes->Data, &FwConfig.Protocol.tftp.RemoteFilePath, FWConfParams[parameter].Length);
  4046. break;
  4047. case CONNECTION_INFO_RETRY:
  4048. memcpy(&pAMIGetFWCfgRes->Data, &FwConfig.Protocol.tftp.Retry, FWConfParams[parameter].Length);
  4049. break;
  4050. case FW_UPDATE_SELECT :
  4051. memcpy(&pAMIGetFWCfgRes->Data, &FwConfig.FlashFWSelect, FWConfParams[parameter].Length);
  4052. break;
  4053. default:
  4054. pAMIGetFWCfgRes->CompletionCode = CC_PARAM_OUT_OF_RANGE;
  4055. return sizeof(INT8U);
  4056. break;
  4057. }
  4058. pAMIGetFWCfgRes->CompletionCode = CC_NORMAL;
  4059. return res_size;
  4060. }
  4061. else
  4062. {
  4063. pAMIGetFWCfgRes->CompletionCode = CC_INV_CMD;
  4064. return sizeof(INT8U);
  4065. }
  4066. }
  4067. /**
  4068. * *@fn SetFirmwareProtocol
  4069. * *@brief This command is used to set protocol type of Firmware update
  4070. * *@param pReq - Request for the command
  4071. * *@param ReqLen - Request length for the command
  4072. * *@param pRes - Response for the command
  4073. * *@param BMCInst - BMC Instance Value
  4074. **/
  4075. int
  4076. AMISetFWProtocol ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes, int BMCInst)
  4077. {
  4078. AMISetFWProtocolReq_T *pAMISetFWProtocolReq = (AMISetFWProtocolReq_T *)pReq;
  4079. AMISetFWProtocolRes_T *pAMISetFWProtocolRes = (AMISetFWProtocolRes_T *)pRes;
  4080. FirmwareConfig_T FwConfig;
  4081. if(IsCardInFlashMode()) {
  4082. pAMISetFWProtocolRes->CompletionCode = CC_DEV_IN_FIRMWARE_UPDATE_MODE;
  4083. return sizeof(INT8U);
  4084. }
  4085. getFirmwareConfig(&FwConfig);
  4086. if(g_corefeatures.fwupdate_protocol_select == CORE_FEATURE_ENABLED)
  4087. {
  4088. switch(pAMISetFWProtocolReq->ProtocolType)
  4089. {
  4090. case PROTOCOL_HTTP_HTTPS:
  4091. FwConfig.ProtocolType = PROTOCOL_HTTP_HTTPS;
  4092. break;
  4093. case PROTOCOL_TFTP:
  4094. FwConfig.ProtocolType = PROTOCOL_TFTP;
  4095. break;
  4096. default:
  4097. pAMISetFWProtocolRes->CompletionCode = CC_PARAM_OUT_OF_RANGE;
  4098. return sizeof(INT8U);
  4099. break;
  4100. }
  4101. setFirmwareConfig(&FwConfig);
  4102. pAMISetFWProtocolRes->CompletionCode = CC_NORMAL;
  4103. }
  4104. else
  4105. {
  4106. pAMISetFWProtocolRes->CompletionCode = CC_INV_CMD;
  4107. }
  4108. return sizeof(AMISetFWProtocolRes_T);
  4109. }
  4110. /**
  4111. * *@fn GetFWProtocol
  4112. * *@brief This command is used to get protocol type of Firmware update
  4113. * *@param pReq - Request for the command
  4114. * *@param ReqLen - Request length for the command
  4115. * *@param pRes - Response for the command
  4116. * *@param BMCInst - BMC Instance Value
  4117. **/
  4118. int
  4119. AMIGetFWProtocol ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes, int BMCInst)
  4120. {
  4121. // AMIGetFWProtocolReq_T *pAMIGetFWProtocolReq = (AMIGetFWProtocolReq_T *)pReq;
  4122. AMIGetFWProtocolRes_T *pAMIGetFWProtocolRes = (AMIGetFWProtocolRes_T *)pRes;
  4123. FirmwareConfig_T FwConfig;
  4124. memset(pAMIGetFWProtocolRes,0,sizeof(AMIGetFWProtocolRes_T));
  4125. getFirmwareConfig(&FwConfig);
  4126. pAMIGetFWProtocolRes->ProtocolType = FwConfig.ProtocolType;
  4127. return sizeof(AMIGetFWProtocolRes_T);
  4128. }
  4129. /**
  4130. * *@fn AMISetPwdEncryptionKey
  4131. * *@brief This command is used to set the password encryption key
  4132. * *@param pReq - Request for the command
  4133. * *@param ReqLen - Request length for the command
  4134. * *@param pRes - Response for the command
  4135. * *@param BMCInst - BMC Instance Value
  4136. **/
  4137. int
  4138. AMISetPwdEncryptionKey(_NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes, int BMCInst)
  4139. {
  4140. AMISetPwdEncKeyReq_T *pAMISetPwdEncKeyReq = (AMISetPwdEncKeyReq_T *)pReq;
  4141. AMISetPwdEncKeyRes_T *pAMISetPwdEncKeyRes = (AMISetPwdEncKeyRes_T *)pRes;
  4142. _FAR_ BMCInfo_t* pBMCInfo = &g_BMCInfo[BMCInst];
  4143. EncryptedUserInfo_T OldEncUserPaswd[MAX_USER_CFG_MDS];
  4144. INT8S EncryptedUserPswd[MAX_ENCRYPTED_PSWD_LEN] = {0};
  4145. INT8S DecryptedUserPswd[MAX_ENCRYPTED_PSWD_LEN] = {0};
  4146. unsigned char oldEncryptKey[MAX_SIZE_KEY + 1] = {0};
  4147. unsigned char newEncryptKey[MAX_SIZE_KEY + 1] = {0};
  4148. Smtp_Config_T* pm_Smtp_Config;
  4149. INT8S decryptedSMTPPswd[MAX_SMTP_PASSWD_LEN] = {0};
  4150. INT8S encryptedSMTPPswd[MAX_SMTP_PASSWD_LEN] = {0};
  4151. int count = 0;
  4152. AD_Config_T adcfg;
  4153. LDAPCONFIG ldapconf;
  4154. /* First take the backup of old encrypted user passwords to revert in case there is a failure in decrypting/encrypting*/
  4155. LOCK_BMC_SHARED_MEM(BMCInst);
  4156. memcpy(&(OldEncUserPaswd[0]), pBMCInfo->EncryptedUserInfo[0].EncryptedPswd, MAX_USER_CFG_MDS * MAX_ENCRYPTED_PSWD_LEN);
  4157. UNLOCK_BMC_SHARED_MEM(BMCInst);
  4158. if(ReqLen < 1 || ReqLen > MAX_SIZE_KEY)
  4159. {
  4160. TCRIT("AMISetPwdEncryptionKey(): Request length=%d is invalid\n", ReqLen);
  4161. pAMISetPwdEncKeyRes->CompletionCode = CC_REQ_INV_LEN;
  4162. return sizeof(INT8U);
  4163. }
  4164. if(g_corefeatures.userpswd_encryption != ENABLED)
  4165. {
  4166. TCRIT("AMISetPwdEncryptionKey(): Password Encryption Feature is NOT enabled.\n");
  4167. pAMISetPwdEncKeyRes->CompletionCode = CC_INV_CMD;
  4168. return sizeof(INT8U);
  4169. }
  4170. TDBG("AMISetPwdEncryptionKey(): REQUEST Len=%d Bytes: %s\n", ReqLen, pAMISetPwdEncKeyReq->PwdEncryptKey);
  4171. /* Copy the old/current encryption key to a buffer */
  4172. LOCK_BMC_SHARED_MEM(BMCInst);
  4173. memcpy(oldEncryptKey, &(g_MBMCInfo.PwdEncKey), MAX_SIZE_KEY);
  4174. UNLOCK_BMC_SHARED_MEM(BMCInst);
  4175. /* Copy the new encryption key to a buffer */
  4176. memcpy(newEncryptKey, pAMISetPwdEncKeyReq->PwdEncryptKey, ReqLen);
  4177. /* Do nothing if there is no change in the encryption keys */
  4178. if(memcmp(oldEncryptKey, newEncryptKey, MAX_SIZE_KEY) == 0)
  4179. {
  4180. TCRIT("AMISetPwdEncryptionKey(): Both the new and the existing encryption keys are same.\n");
  4181. pAMISetPwdEncKeyRes->CompletionCode = CC_NORMAL;
  4182. return sizeof(INT8U);
  4183. }
  4184. /* Decrypt all the IPMI User passwords with the old encrypt key and encrypt those with the new encrypt key */
  4185. for(count = 0; count < MAX_USER_CFG_MDS; count++)
  4186. {
  4187. /* Check if User ID is 0; if it is 0, skip the user as there is no need to set the password for empty slots*/
  4188. if((pBMCInfo->UserInfo[count].UserId) == 0)
  4189. continue; //No need to set the password for empty slots
  4190. /* Check if password is encrypted */
  4191. if(_fmemcmp(pBMCInfo->UserInfo[count].UserPassword, "$ENCRYPTED$", 11) == 0)
  4192. {
  4193. /* Decrypt the password with old key */
  4194. if(DecryptPassword((INT8S *)(pBMCInfo->EncryptedUserInfo[count].EncryptedPswd), MAX_PASSWORD_LEN, DecryptedUserPswd, MAX_PASSWORD_LEN, oldEncryptKey))
  4195. {
  4196. TCRIT("Error in Decrypting the IPMI User Password for user id = %d \n", count+1);
  4197. goto error;
  4198. }
  4199. /* Encrypt the password with new key */
  4200. if(EncryptPassword(DecryptedUserPswd, MAX_PASSWORD_LEN, EncryptedUserPswd, MAX_PASSWORD_LEN, newEncryptKey))
  4201. {
  4202. TCRIT("Error in Encrypting the IPMI User Password for user with ID = %d..\n", count+1);
  4203. goto error;
  4204. }
  4205. LOCK_BMC_SHARED_MEM(BMCInst);
  4206. memset(&(pBMCInfo->EncryptedUserInfo[count].EncryptedPswd), 0, MAX_ENCRYPTED_PSWD_LEN);
  4207. memcpy(&(pBMCInfo->EncryptedUserInfo[count].EncryptedPswd), EncryptedUserPswd, MAX_ENCRYPTED_PSWD_LEN);
  4208. UNLOCK_BMC_SHARED_MEM(BMCInst);
  4209. }
  4210. }
  4211. // Get the LDAP Configurations
  4212. memset((unsigned char*)&ldapconf, 0, sizeof(LDAPCONFIG));
  4213. if(getldapconfig(&ldapconf) != 0) //This will decrypt the password as well
  4214. {
  4215. TCRIT("Couldn't able to get the ldap configuration\n");
  4216. goto error;
  4217. }
  4218. // Get the AD Configurations
  4219. memset((unsigned char*)&adcfg, 0, sizeof(AD_Config_T));
  4220. if(GetADConfig (&adcfg) != 0) //This will decrypt the password as well
  4221. {
  4222. TCRIT("Could not get Active Directory Configuration\n");
  4223. goto error;
  4224. }
  4225. //For SMTP
  4226. for(count = 0; count < MAX_LAN_CHANNELS; count++)
  4227. {
  4228. pm_Smtp_Config = &pBMCInfo->SmtpConfig[count];
  4229. // For Primary SMTP Server
  4230. if(pm_Smtp_Config->EnableDisableSMTP == 1 && pm_Smtp_Config->EnableDisableSmtpAuth == 1)
  4231. {
  4232. // First decrypt with the old encryption key
  4233. if(DecryptPassword((INT8S *)pm_Smtp_Config->Passwd, MAX_SMTP_PASSWD_LEN-1, decryptedSMTPPswd, MAX_SMTP_PASSWD_LEN-1, oldEncryptKey))
  4234. {
  4235. TCRIT("\n Error in Decrypting the Primary SMTP Password. \n");
  4236. goto error;
  4237. }
  4238. // Now encrypt with the new encryption key
  4239. if(EncryptPassword(decryptedSMTPPswd, MAX_SMTP_PASSWD_LEN-1, encryptedSMTPPswd, MAX_SMTP_PASSWD_LEN-1, newEncryptKey))
  4240. {
  4241. TCRIT("Error in encrypting the Primary SMTP password.\n");
  4242. goto error;
  4243. }
  4244. memset(&(pm_Smtp_Config->Passwd),0, MAX_SMTP_PASSWD_LEN-1);
  4245. memcpy(&(pm_Smtp_Config->Passwd), encryptedSMTPPswd, MAX_SMTP_PASSWD_LEN-1);
  4246. }
  4247. // For Secondary SMTP Server
  4248. if(pm_Smtp_Config->EnableDisableSMTP2 == 1 && pm_Smtp_Config->EnableDisableSmtp2Auth == 1)
  4249. {
  4250. memset(decryptedSMTPPswd, 0, MAX_SMTP_PASSWD_LEN);
  4251. memset(encryptedSMTPPswd, 0, MAX_SMTP_PASSWD_LEN);
  4252. // First decrypt with the old encryption key
  4253. if(DecryptPassword((INT8S *)pm_Smtp_Config->Passwd2, MAX_SMTP_PASSWD_LEN-1, decryptedSMTPPswd, MAX_SMTP_PASSWD_LEN-1, oldEncryptKey))
  4254. {
  4255. TCRIT("\n Error in Decrypting the Secondary SMTP Password. \n");
  4256. goto error;
  4257. }
  4258. // Now encrypt with the new encryption key
  4259. if(EncryptPassword(decryptedSMTPPswd, MAX_SMTP_PASSWD_LEN-1, encryptedSMTPPswd, MAX_SMTP_PASSWD_LEN-1, newEncryptKey))
  4260. {
  4261. TCRIT("Error in encrypting the Secondary SMTP password.\n");
  4262. goto error;
  4263. }
  4264. memset(&(pm_Smtp_Config->Passwd2),0, MAX_SMTP_PASSWD_LEN-1);
  4265. memcpy(&(pm_Smtp_Config->Passwd2), encryptedSMTPPswd, MAX_SMTP_PASSWD_LEN-1);
  4266. FlushIPMI((INT8U*)&pBMCInfo->SmtpConfig[0], (INT8U*)&pBMCInfo->SmtpConfig[count],
  4267. pBMCInfo->IPMIConfLoc.SmtpConfigAddr, sizeof(Smtp_Config_T), BMCInst);
  4268. }
  4269. }
  4270. // Now set the new encryption Key to the /conf/pwdEncKey file
  4271. if(setEncryptKey(newEncryptKey))
  4272. {
  4273. TCRIT("Error in writing the new encryption key to /conf/pwdEncKey file. quitting...\n");
  4274. goto error;
  4275. }
  4276. LOCK_BMC_SHARED_MEM(BMCInst);
  4277. memset(&(g_MBMCInfo.PwdEncKey), 0 , MAX_SIZE_KEY);
  4278. memcpy(&(g_MBMCInfo.PwdEncKey), &(pAMISetPwdEncKeyReq->PwdEncryptKey), ReqLen);
  4279. UNLOCK_BMC_SHARED_MEM(BMCInst);
  4280. // Encryption Key is set. Now, save the LDAP configurations.
  4281. if(ldapconf.Enable == 1)
  4282. {
  4283. if(setldapconfig(&ldapconf) != 0) //This will encrypt the password as well
  4284. {
  4285. TCRIT("Error in setting LDAP configuration\n");
  4286. goto error;
  4287. }
  4288. }
  4289. else
  4290. {
  4291. TDBG("LDAP Disabled !! ldap login works when its enabled \n");
  4292. }
  4293. // Encryption Key is set. Now, save the AD configurations.
  4294. if(adcfg.ADEnable == 1)
  4295. {
  4296. if(SetADConfig(&adcfg) != 0) //This will encrypt the password as well
  4297. {
  4298. TCRIT("Error in setting AD configuration:\n");
  4299. goto error;
  4300. }
  4301. }
  4302. else
  4303. {
  4304. TDBG("AD is Not Enabled !! AD login works when its enabled.\n");
  4305. pAMISetPwdEncKeyRes->CompletionCode = CC_NORMAL;
  4306. }
  4307. TDBG("The new encryption key has set.\n");
  4308. FlushIPMI((INT8U*)&pBMCInfo->EncryptedUserInfo[0], (INT8U*)&pBMCInfo->EncryptedUserInfo[0], pBMCInfo->IPMIConfLoc.EncUserPasswdAddr,
  4309. sizeof(EncryptedUserInfo_T) * MAX_USER_CFG_MDS, BMCInst);
  4310. pAMISetPwdEncKeyRes->CompletionCode = CC_NORMAL;
  4311. return sizeof(AMISetPwdEncKeyRes_T);
  4312. error:
  4313. /* There is a failure in Decryption/Encryption; so reverting to the old values */
  4314. LOCK_BMC_SHARED_MEM(BMCInst);
  4315. memcpy(&(pBMCInfo->EncryptedUserInfo[0].EncryptedPswd), &(OldEncUserPaswd[0]), MAX_USER_CFG_MDS * MAX_ENCRYPTED_PSWD_LEN);
  4316. UNLOCK_BMC_SHARED_MEM(BMCInst);
  4317. pAMISetPwdEncKeyRes->CompletionCode = CC_UNSPECIFIED_ERR;
  4318. return sizeof(INT8U);
  4319. }
  4320. /**
  4321. * *@fn AMISetUBootMemtest
  4322. * *@brief This command is used to Enable/Disable the U-Boot Memory Test
  4323. * *@param pReq - Request for the command
  4324. * *@param ReqLen - Request length for the command
  4325. * *@param pRes - Response for the command
  4326. * *@param BMCInst - BMC Instance Value
  4327. **/
  4328. int
  4329. AMISetUBootMemtest(_NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes, int BMCInst)
  4330. {
  4331. AMISetUBootMemtestReq_T *pAMISetUBootMemtestReq = (AMISetUBootMemtestReq_T *)pReq;
  4332. AMISetUBootMemtestRes_T *pAMISetUBootMemtestRes = (AMISetUBootMemtestRes_T *)pRes;
  4333. int RetVal;
  4334. char BootVal[5];
  4335. memset(BootVal,0,sizeof(BootVal));
  4336. /*Check the Reserved value ( 0 - Disable; 1 - Enable memory Test)*/
  4337. if(1 < pAMISetUBootMemtestReq->EnableMemoryTest)
  4338. {
  4339. *pRes = CC_INV_DATA_FIELD;
  4340. return sizeof(INT8U);
  4341. }
  4342. /*Enable/Disable the U-Boot Memory Test*/
  4343. sprintf(BootVal,"%d",pAMISetUBootMemtestReq->EnableMemoryTest);
  4344. RetVal = SetUBootParam(UBOOT_MEMTEST_ENABLE_VAR,BootVal);
  4345. if(0 != RetVal)
  4346. {
  4347. *pRes = CC_UNSPECIFIED_ERR;
  4348. return sizeof(INT8U);
  4349. }
  4350. pAMISetUBootMemtestRes->CompletionCode = CC_NORMAL;
  4351. return sizeof(INT8U);
  4352. }
  4353. /**
  4354. * *@fn AMIGetUBootMemtestStatus
  4355. * *@brief This command is used to get the status of U-Boot Memory Test
  4356. * *@param pReq - Request for the command
  4357. * *@param ReqLen - Request length for the command
  4358. * *@param pRes - Response for the command
  4359. * *@param BMCInst - BMC Instance Value
  4360. **/
  4361. int
  4362. AMIGetUBootMemtestStatus(_NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes, int BMCInst)
  4363. {
  4364. AMIGetUBootMemtestStatusRes_T *pAMIGetUBootMemtestStatusRes = (AMIGetUBootMemtestStatusRes_T *)pRes;
  4365. char BootVal[10] = {0};
  4366. char Memtest[10] = {0};
  4367. int RetVal;
  4368. /*Get the U-Boot Memory Test Result*/
  4369. RetVal = GetUBootParam(UBOOT_MEMTEST_STATUS_VAR,BootVal);
  4370. if(0 != RetVal)
  4371. {
  4372. *pRes = CC_UNSPECIFIED_ERR;
  4373. return sizeof(INT8U);
  4374. }
  4375. /*Get the do_memtest U-Boot Env Value*/
  4376. RetVal = GetUBootParam(UBOOT_MEMTEST_ENABLE_VAR,Memtest);
  4377. if(0 != RetVal)
  4378. {
  4379. *pRes = CC_UNSPECIFIED_ERR;
  4380. return sizeof(INT8U);
  4381. }
  4382. /*Memory Test idle (0)*/
  4383. pAMIGetUBootMemtestStatusRes->MemtestStatus = 0;
  4384. if(strcmp(BootVal,MEMTEST_PASS) == 0)
  4385. {
  4386. /*Memory Test Success (1)*/
  4387. pAMIGetUBootMemtestStatusRes->MemtestStatus = 1;
  4388. }
  4389. else if(strcmp(BootVal,MEMTEST_FAIL) == 0)
  4390. {
  4391. /*Memory Test Fail (2)*/
  4392. pAMIGetUBootMemtestStatusRes->MemtestStatus = 2;
  4393. }
  4394. /*Memory Test diable (0)*/
  4395. pAMIGetUBootMemtestStatusRes->IsMemtestEnable = 0;
  4396. if(strcmp(Memtest,MEMTEST_ENABLE) == 0)
  4397. {
  4398. /*Memory Test is Enabled (1)*/
  4399. pAMIGetUBootMemtestStatusRes->IsMemtestEnable = 1;
  4400. }
  4401. pAMIGetUBootMemtestStatusRes->CompletionCode = CC_NORMAL;
  4402. return sizeof(AMIGetUBootMemtestStatusRes_T);
  4403. }
  4404. /**
  4405. **@fn SetDefaultTimezone
  4406. **@brief This command is used set the defalut timezone value.
  4407. **@param TimeZone - Timezone string -initial zone name.
  4408. **@param BMCInst - BMC Instance Value
  4409. **@return 0 on success -1 on failure
  4410. **/
  4411. int
  4412. SetDefaultTimezone (INT8U *TimeZone, int BMCInst)
  4413. {
  4414. _FAR_ BMCInfo_t* pBMCInfo = &g_BMCInfo[BMCInst];
  4415. INT16 UTCOffset = 0;
  4416. time_t Curtime = 0;
  4417. char Cmd [ZONE_PATH_SIZE];
  4418. char Zonepath [ZONE_PATH_SIZE];
  4419. struct stat sb, buf;
  4420. struct tm *loctm = NULL;
  4421. INT8U Size = 0;
  4422. INT8U Zonename [TIME_ZONE_LEN];
  4423. char Filename[MAXFILESIZE];
  4424. if (NULL == TimeZone)
  4425. {
  4426. return -1;
  4427. }
  4428. IPMI_CONFIGS_FILE(BMCInst,Filename);
  4429. memset (Zonepath, 0, ZONE_PATH_SIZE);
  4430. memset (Cmd, 0, ZONE_PATH_SIZE);
  4431. memset (Zonename, 0, TIME_ZONE_LEN);
  4432. memcpy (Zonename,TimeZone, TIME_ZONE_LEN);
  4433. if (0 == memcmp (TimeZone, TIMEZONE_OFFSET_PVE, strlen(TIMEZONE_OFFSET_PVE)))
  4434. {
  4435. Size = strlen (TIMEZONE_OFFSET_PVE) - sizeof (INT8U);
  4436. TimeZone [Size] = '-';
  4437. }
  4438. else if (0 == memcmp (TimeZone, TIMEZONE_OFFSET_NVE, strlen(TIMEZONE_OFFSET_NVE)))
  4439. {
  4440. Size = strlen (TIMEZONE_OFFSET_NVE) - sizeof (INT8U);
  4441. TimeZone [Size] = '+';
  4442. }
  4443. snprintf (Zonepath, ZONE_PATH_SIZE, "%s%s", TIME_ZONE_PATH,(char*)TimeZone);
  4444. /*Verify the file presence*/
  4445. if (stat (Zonepath, &sb))
  4446. {
  4447. return -1;
  4448. }
  4449. if (!S_ISREG (sb.st_mode))
  4450. {
  4451. return -1;
  4452. }
  4453. snprintf (Cmd, ZONE_PATH_SIZE, "%s %s %s", LINK_CMD, Zonepath, LOCALTIME);
  4454. safe_system (Cmd);
  4455. /*Getting UTC offset and updating*/
  4456. time(&Curtime);
  4457. loctm = localtime(&Curtime);
  4458. UTCOffset = (loctm->tm_gmtoff/MAX_MINS);
  4459. if (((UTCOffset <= SEL_UTC_MAX_RANGE) && (UTCOffset >= SEL_UTC_MIN_RANGE)) || (UTCOffset == UNSPECIFIED_UTC_OFFSET))
  4460. {
  4461. pBMCInfo->GenConfig.SELTimeUTCOffset = UTCOffset;
  4462. }
  4463. memcpy (pBMCInfo->GenConfig.TimeZone, Zonename, sizeof (pBMCInfo->GenConfig.TimeZone));
  4464. /*Write to NVRAM*/
  4465. if (stat(Filename, &buf) == 0)
  4466. {
  4467. FlushIPMI((INT8U*)&pBMCInfo->GenConfig,(INT8U*)&pBMCInfo->GenConfig,pBMCInfo->IPMIConfLoc.GenConfigAddr,
  4468. sizeof(GENConfig_T),BMCInst);
  4469. }
  4470. return 0;
  4471. }
  4472. /**
  4473. **@fn AMISetTimeZone
  4474. **@brief This command is used to set the timezone.
  4475. **@param pReq - Request for the command
  4476. **@param ReqLen - Request length for the command
  4477. **@param pRes - Response for the command
  4478. **@param BMCInst - BMC Instance Value
  4479. **/
  4480. int
  4481. AMISetTimeZone (_NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes, int BMCInst)
  4482. {
  4483. AMISetTimeZone_T *pAMISetTimeZoneReq = (AMISetTimeZone_T *)pReq;
  4484. if(g_corefeatures.time_zone_support == ENABLED)
  4485. {
  4486. if ((ReqLen <= 0) || (ReqLen >= sizeof (AMISetTimeZone_T)))
  4487. {
  4488. *pRes = CC_REQ_INV_LEN;
  4489. return sizeof(INT8U);
  4490. }
  4491. if (pAMISetTimeZoneReq->ZoneName [0] == '\0')
  4492. {
  4493. *pRes = CC_INV_DATA_FIELD;
  4494. return sizeof(INT8U);
  4495. }
  4496. pAMISetTimeZoneReq->ZoneName [ReqLen] = '\0';
  4497. if (-1 == SetDefaultTimezone (pAMISetTimeZoneReq->ZoneName, BMCInst))
  4498. {
  4499. *pRes = CC_INV_DATA_FIELD;
  4500. return sizeof(INT8U);
  4501. }
  4502. *pRes = CC_NORMAL;
  4503. }
  4504. else
  4505. {
  4506. *pRes = CC_INV_CMD;
  4507. }
  4508. return sizeof(INT8U);
  4509. }
  4510. /**
  4511. **@fn AMIGetTimeZone
  4512. **@brief This command is used to get the timezone.
  4513. **@param pReq - Request for the command
  4514. **@param ReqLen - Request length for the command
  4515. **@param pRes - Response for the command
  4516. **@param BMCInst - BMC Instance Value
  4517. **/
  4518. int
  4519. AMIGetTimeZone (_NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes, int BMCInst)
  4520. {
  4521. AMIGetTimeZone_T *pAMISetTimeZoneRes = (AMIGetTimeZone_T *)pRes;
  4522. if(g_corefeatures.time_zone_support == ENABLED)
  4523. {
  4524. _FAR_ BMCInfo_t* pBMCInfo = &g_BMCInfo[BMCInst];
  4525. memset (pAMISetTimeZoneRes, 0, sizeof(AMIGetTimeZone_T));
  4526. pAMISetTimeZoneRes->CompletionCode = CC_NORMAL;
  4527. memcpy (pAMISetTimeZoneRes->ZoneName, pBMCInfo->GenConfig.TimeZone, sizeof (pBMCInfo->GenConfig.TimeZone));
  4528. ReqLen = strlen ((char *)pAMISetTimeZoneRes->ZoneName);
  4529. }
  4530. else
  4531. {
  4532. pAMISetTimeZoneRes->CompletionCode = CC_INV_CMD;
  4533. ReqLen = 0;
  4534. }
  4535. return ReqLen + sizeof (INT8U);
  4536. }
  4537. /**
  4538. **@fn AMIGetNTPCfg
  4539. **@brief This command is used to get the NTP configs.
  4540. **@param pReq - Request for the command
  4541. **@param ReqLen - Request length for the command
  4542. **@param pRes - Response for the command
  4543. **@param BMCInst - BMC Instance Value
  4544. **/
  4545. int AMIGetNTPCfg(_NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes, int BMCInst)
  4546. {
  4547. AMIGetNTPCfgRes_T *pAMIGetNTPCfgRes = (AMIGetNTPCfgRes_T *)pRes;
  4548. if(g_corefeatures.ntp_server_support == ENABLED)
  4549. {
  4550. INT8U PrimServer[MAX_SERVER_LEN]={0};
  4551. INT8U SecServer[MAX_SERVER_LEN]={0};
  4552. INT8U Status[MAX_STATUS_LEN]={0};
  4553. void *dl_handle = NULL;
  4554. int ( *dl_func_ser )( char *, char *,unsigned int );
  4555. int ( *dl_func_stat )( char * );
  4556. dl_handle = NULL;
  4557. dl_handle = dlopen ( "/usr/local/lib/libntpconf.so", RTLD_NOW );
  4558. if(NULL == dl_handle)
  4559. {
  4560. IPMI_ERROR("Error in loading libntpconf.so library %s\n", dlerror() );
  4561. pAMIGetNTPCfgRes->CompletionCode = CC_UNSPECIFIED_ERR;
  4562. return sizeof(INT8U);
  4563. }
  4564. dl_func_ser = dlsym( dl_handle, "libami_getntpServer" );
  4565. if(NULL == dl_func_ser)
  4566. {
  4567. IPMI_ERROR("Error in loading symbol libami_getntpServer %s\n", dlerror() );
  4568. dlclose(dl_handle);
  4569. pAMIGetNTPCfgRes->CompletionCode = CC_UNSPECIFIED_ERR;
  4570. return sizeof(INT8U);
  4571. }
  4572. dl_func_stat = dlsym( dl_handle, "libami_getntpStatus" );
  4573. if(NULL == dl_func_stat)
  4574. {
  4575. IPMI_ERROR("Error in loading symbol libami_getntpStatus %s\n", dlerror() );
  4576. dlclose(dl_handle);
  4577. pAMIGetNTPCfgRes->CompletionCode = CC_UNSPECIFIED_ERR;
  4578. return sizeof(INT8U);
  4579. }
  4580. dl_func_ser((char*)PrimServer,(char*)SecServer, MAX_SERVER_LEN);
  4581. dl_func_stat((char*)Status);
  4582. memset(pAMIGetNTPCfgRes,0,sizeof(AMIGetNTPCfgRes_T));
  4583. strncpy((char*) pAMIGetNTPCfgRes->PrimServer,(char*)PrimServer,MAX_SERVER_LEN);
  4584. strncpy((char*)pAMIGetNTPCfgRes->SecServer,(char*)SecServer,MAX_SERVER_LEN);
  4585. if(!strncmp((char*)Status ,"Auto" ,MAX_STATUS_LEN))
  4586. {
  4587. pAMIGetNTPCfgRes->Status = AUTO;
  4588. }
  4589. else if(!strncmp((char*)Status ,"Manual" ,MAX_STATUS_LEN))
  4590. {
  4591. pAMIGetNTPCfgRes->Status = MANUAL;
  4592. }
  4593. else
  4594. {
  4595. pAMIGetNTPCfgRes->Status = FAIL;
  4596. }
  4597. dlclose(dl_handle);
  4598. return sizeof(AMIGetNTPCfgRes_T);
  4599. }
  4600. else
  4601. {
  4602. pAMIGetNTPCfgRes->CompletionCode = CC_INV_CMD;
  4603. return sizeof(INT8U);
  4604. }
  4605. }
  4606. /**
  4607. **@fn AMISetNTPCfg
  4608. **@brief This command is used to set the NTP configs.
  4609. **@param pReq - Request for the command
  4610. **@param ReqLen - Request length for the command
  4611. **@param pRes - Response for the command
  4612. **@param BMCInst - BMC Instance Value
  4613. **/
  4614. int AMISetNTPCfg(_NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes, int BMCInst)
  4615. {
  4616. AMISetNTPCfgReq_T *pAMISetNTPCfgReq = (AMISetNTPCfgReq_T *)pReq;
  4617. AMISetNTPCfgRes_T *pAMISetNTPCfgRes = (AMISetNTPCfgRes_T *)pRes;
  4618. if(g_corefeatures.ntp_server_support == ENABLED)
  4619. {
  4620. void *dl_handle = NULL;
  4621. int ( *dl_func_ser )( unsigned char, char * );
  4622. int ( *dl_func_stat )( char * );
  4623. int ( *dl_func_get_ser )( char * , char *, unsigned int );
  4624. INT8U PrimServer[MAX_SERVER_LEN]={0};
  4625. INT8U SecServer[MAX_SERVER_LEN]={0};
  4626. INT8U Status[MAX_STATUS_LEN]="Manual";
  4627. INT8U update_ntp[MAX_SERVER_LEN]={0};
  4628. int ret;
  4629. dl_handle = NULL;
  4630. dl_handle = dlopen ( "/usr/local/lib/libntpconf.so", RTLD_NOW );
  4631. if(NULL == dl_handle)
  4632. {
  4633. IPMI_ERROR("Error in loading libntpconf.so library %s\n", dlerror() );
  4634. pAMISetNTPCfgRes->CompletionCode = CC_UNSPECIFIED_ERR;
  4635. return sizeof(AMISetNTPCfgRes_T);
  4636. }
  4637. switch (pAMISetNTPCfgReq->param)
  4638. {
  4639. case PARAM_SERVER_PRIM: // for setting primary server
  4640. case PARAM_SERVER_SEC: // for setting secondary server
  4641. if(ReqLen != sizeof(AMISetNTPCfgReq_T))
  4642. {
  4643. pAMISetNTPCfgRes->CompletionCode=CC_REQ_INV_LEN;
  4644. break;
  4645. }
  4646. dl_func_ser = dlsym( dl_handle, "libami_setntpServer" );
  4647. if(NULL == dl_func_ser)
  4648. {
  4649. IPMI_ERROR("Error in loading symbol libami_setntpServer %s\n", dlerror() );
  4650. pAMISetNTPCfgRes->CompletionCode = CC_UNSPECIFIED_ERR;
  4651. break;
  4652. }
  4653. dl_func_ser(pAMISetNTPCfgReq->param,(char*)pAMISetNTPCfgReq->ntpconf.Server);
  4654. pAMISetNTPCfgRes->CompletionCode=CC_NORMAL;
  4655. break;
  4656. case PARAM_STATUS: //for setting the status
  4657. if(ReqLen != (1+ sizeof(pAMISetNTPCfgReq->ntpconf.Status)))
  4658. {
  4659. pAMISetNTPCfgRes->CompletionCode=CC_REQ_INV_LEN;
  4660. break;
  4661. }
  4662. if(pAMISetNTPCfgReq->ntpconf.Status != AUTO && pAMISetNTPCfgReq->ntpconf.Status != MANUAL)
  4663. {
  4664. pAMISetNTPCfgRes->CompletionCode=CC_INV_DATA_FIELD;
  4665. break;
  4666. }
  4667. dl_func_stat = dlsym( dl_handle, "libami_setntpStatus" );
  4668. if(NULL == dl_func_stat)
  4669. {
  4670. IPMI_ERROR("Error in loading symbol libami_setntpStatus %s\n", dlerror() );
  4671. pAMISetNTPCfgRes->CompletionCode = CC_UNSPECIFIED_ERR;
  4672. break;
  4673. }
  4674. if(pAMISetNTPCfgReq->ntpconf.Status == AUTO)
  4675. {
  4676. strncpy((char*)Status, "Auto", MAX_STATUS_LEN);
  4677. dl_func_get_ser = dlsym(dl_handle, "libami_getntpServer");
  4678. if(NULL == dl_func_get_ser)
  4679. {
  4680. IPMI_ERROR("Error in loading symbol libami_getntpServer %s\n", dlerror() );
  4681. dlclose(dl_handle);
  4682. pAMISetNTPCfgRes->CompletionCode = CC_UNSPECIFIED_ERR;
  4683. return sizeof(INT8U);
  4684. }
  4685. dl_func_get_ser((char*)PrimServer,(char*)SecServer, MAX_SERVER_LEN);
  4686. sprintf((char*)update_ntp, "ntpdate -b -s -u %s", PrimServer);
  4687. ret = safe_system((char*)update_ntp);
  4688. if(0 != ret)
  4689. {
  4690. TCRIT("\n NTP update failure in primary server::%d\n", ret);
  4691. sprintf((char*)update_ntp, "ntpdate -b -s -u %s", SecServer);
  4692. ret = safe_system((char*)update_ntp);
  4693. if(0 != ret)
  4694. {
  4695. TCRIT("\n NTP update failure in secondary server::%d\n", ret);
  4696. strncpy((char*)Status, "Failure", MAX_STATUS_LEN);
  4697. TCRIT("Error in restarting NTP Date\n");
  4698. pAMISetNTPCfgRes->CompletionCode = CC_NTP_RESTART_ERROR;
  4699. dl_func_stat((char*)Status);
  4700. break;
  4701. }
  4702. }
  4703. }
  4704. dl_func_stat((char*)Status);
  4705. pAMISetNTPCfgRes->CompletionCode=CC_NORMAL;
  4706. break;
  4707. default:
  4708. pAMISetNTPCfgRes->CompletionCode=CC_INV_DATA_FIELD;
  4709. }
  4710. dlclose(dl_handle);
  4711. return sizeof(AMISetNTPCfgRes_T);
  4712. }
  4713. else
  4714. {
  4715. pAMISetNTPCfgRes->CompletionCode = CC_INV_CMD;
  4716. return sizeof(AMISetNTPCfgRes_T);
  4717. }
  4718. }
  4719. /*---------------------------------------
  4720. * AMIYAFUReplaceSignedImageKey
  4721. *---------------------------------------*/
  4722. int AMIYAFUReplaceSignedImageKey ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes,_NEAR_ int BMCInst )
  4723. {
  4724. AMIYafuSignedImageKeyReq_T *pAMIYafuSignedImageReq = (AMIYafuSignedImageKeyReq_T *)pReq;
  4725. AMIYafuSignedImageKeyRes_T *pAMIYafuSignedImageRes = (AMIYafuSignedImageKeyRes_T *)pRes;
  4726. int SizeWritten = 0;
  4727. FILE *fp =NULL;
  4728. if(g_corefeatures.signed_hashed_image_support == ENABLED)
  4729. {
  4730. /* Check Requested Length */
  4731. if(((ReqLen) > MAX_PERMITTED_KEY_SIZE) || ((ReqLen) <= 0))
  4732. {
  4733. IPMI_ERROR("Invalid Request Length\n");
  4734. pAMIYafuSignedImageRes->CompletionCode = CC_REQ_INV_LEN;
  4735. return sizeof(AMIYafuSignedImageKeyRes_T);
  4736. }
  4737. fp = fopen(TEMP_PUB_KEY, "wb");
  4738. if(fp == NULL)
  4739. {
  4740. IPMI_ERROR("Error in Opening File\n");
  4741. pAMIYafuSignedImageRes->CompletionCode = CC_UNSPECIFIED_ERR;
  4742. return sizeof(AMIYafuSignedImageKeyRes_T);
  4743. }
  4744. SizeWritten = fwrite(&pAMIYafuSignedImageReq->PubKey[0],1,(ReqLen),fp);
  4745. if(SizeWritten != (ReqLen))
  4746. {
  4747. IPMI_ERROR("Error in Writing into the file\n");
  4748. pAMIYafuSignedImageRes->CompletionCode = CC_UNSPECIFIED_ERR;
  4749. fclose(fp);
  4750. return sizeof(AMIYafuSignedImageKeyRes_T);
  4751. }
  4752. fclose(fp);
  4753. /* Now Open the File in Read Mode */
  4754. fp = fopen(TEMP_PUB_KEY, "rb");
  4755. if(fp == NULL)
  4756. {
  4757. IPMI_ERROR("Error in Opening File\n");
  4758. pAMIYafuSignedImageRes->CompletionCode = CC_UNSPECIFIED_ERR;
  4759. unlink(TEMP_PUB_KEY);
  4760. return sizeof(AMIYafuSignedImageKeyRes_T);
  4761. }
  4762. if(PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL) == NULL)
  4763. {
  4764. IPMI_ERROR("Error in loading RSA public key file %s\n", TEMP_PUB_KEY);
  4765. fclose(fp);
  4766. unlink(TEMP_PUB_KEY);
  4767. pAMIYafuSignedImageRes->CompletionCode = CC_PUBLICKEY_VALIDATION_FAILED;
  4768. return sizeof(AMIYafuSignedImageKeyRes_T);
  4769. }
  4770. fclose(fp);
  4771. if(moveFile(TEMP_PUB_KEY,CONF_PUB_KEY) != 0)
  4772. {
  4773. IPMI_ERROR("Cannot Move File Named as %s\n",CONF_PUB_KEY);
  4774. pAMIYafuSignedImageRes->CompletionCode = CC_UNSPECIFIED_ERR;
  4775. return sizeof(AMIYafuSignedImageKeyRes_T);
  4776. }
  4777. pAMIYafuSignedImageRes->CompletionCode = CC_NORMAL;
  4778. return sizeof(AMIYafuSignedImageKeyRes_T);
  4779. }
  4780. else
  4781. {
  4782. pAMIYafuSignedImageRes->CompletionCode = CC_INV_CMD;
  4783. return sizeof(AMIYafuSignedImageKeyRes_T);
  4784. }
  4785. }
  4786. #define USB_DEVICE "/dev/usb"
  4787. int IsKVMSessionRunning()
  4788. {
  4789. int ServiceRet;
  4790. SERVICE_CONF_STRUCT KVMServiceConf;
  4791. int activecount = 0;
  4792. if(g_corefeatures.service_config == ENABLED)
  4793. {
  4794. ServiceRet = get_service_configurations(KVM_SERVICE_NAME,&KVMServiceConf);
  4795. if(ServiceRet == -1)
  4796. {
  4797. IPMI_ERROR("\nCannot get Service Configurations\n");
  4798. return 1;
  4799. }
  4800. }
  4801. if(isNotEditable((unsigned char *)&(KVMServiceConf.CurrentActiveSession),sizeof(KVMServiceConf.CurrentActiveSession)) )
  4802. {
  4803. getNotEditableData((unsigned char *)&(KVMServiceConf.CurrentActiveSession),sizeof(KVMServiceConf.CurrentActiveSession), (unsigned char *)&(activecount));
  4804. IPMI_ERROR("KVM Session, setting CurrentActiveSession value After Mask::%d\n", KVMServiceConf.CurrentActiveSession);
  4805. }
  4806. if(activecount)
  4807. {
  4808. IPMI_ERROR("\nKVM Running::%d:activecount:%d\n",KVMServiceConf.CurrentActiveSession,activecount);
  4809. return 1;
  4810. }
  4811. return 0;
  4812. }
  4813. int OngetSetVirtualDevice(int cmd,u8 *deviceStatus)
  4814. {
  4815. int usbfd;
  4816. u8 status=0;
  4817. /* Open the USB Device */
  4818. usbfd=open(USB_DEVICE,O_RDWR);
  4819. if (usbfd < 0)
  4820. {
  4821. TCRIT("Error:AMIVirtualDeviceEnable Unable to open USB Device\n");
  4822. return CC_NODE_BUSY;
  4823. }
  4824. if(cmd == USB_GET_ALL_DEVICE_STATUS)
  4825. {
  4826. if (ioctl (usbfd, cmd, &status))
  4827. {
  4828. TWARN ("Error: AMIVirtualDeviceEnable() USB_ENABLE_ALL_DEVICE IOCTL Failed\n");
  4829. close(usbfd);
  4830. return CC_NODE_BUSY;
  4831. }
  4832. *deviceStatus = status;
  4833. }
  4834. else
  4835. {
  4836. if(IsVirtualMediaRunning() || IsKVMSessionRunning() || IsLmediaRmediaRunning())
  4837. {
  4838. if(cmd == USB_ENABLE_ALL_DEVICE)
  4839. {
  4840. if (ioctl (usbfd, USB_GET_ALL_DEVICE_STATUS, &status))
  4841. {
  4842. TWARN ("Error: AMIVirtualDeviceEnable() USB_GET_ALL_DEVICE_STATUS IOCTL Failed\n");
  4843. close(usbfd);
  4844. return CC_NODE_BUSY;
  4845. }
  4846. if(status == 0)
  4847. {
  4848. if (ioctl (usbfd, cmd, NULL))
  4849. {
  4850. TWARN ("Error: AMIVirtualDeviceEnable() USB_ENABLE_ALL_DEVICE IOCTL Failed\n");
  4851. close(usbfd);
  4852. return CC_NODE_BUSY;
  4853. }
  4854. }
  4855. else
  4856. {
  4857. close(usbfd);
  4858. return CC_NODE_BUSY;
  4859. }
  4860. }
  4861. else
  4862. {
  4863. close(usbfd);
  4864. return CC_NODE_BUSY;
  4865. }
  4866. }
  4867. else if (ioctl (usbfd, cmd, NULL))
  4868. {
  4869. TWARN ("Error: AMIVirtualDeviceEnable() USB_ENABLE_ALL_DEVICE IOCTL Failed\n");
  4870. close(usbfd);
  4871. return CC_NODE_BUSY;
  4872. }
  4873. }
  4874. close(usbfd);
  4875. return 0;
  4876. }
  4877. int AMIVirtualDeviceSetStatus ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes,_NEAR_ int BMCInst )
  4878. {
  4879. AMIVirtualDeviceSetStatusReq_T *pAMIVirtualDeviceSetStatusReq = (AMIVirtualDeviceSetStatusReq_T *)pReq;
  4880. AMIVirtualDeviceSetStatusRes_T *pAMIVirtualDeviceSetStatusRes = (AMIVirtualDeviceSetStatusRes_T *)pRes;
  4881. void *pHandle = NULL;
  4882. INT16 (*GetLicenseStatus)(const char*, BOOL*);
  4883. if(g_corefeatures.power_consumption_virtual_device_usb == ENABLED)
  4884. {
  4885. if ((ReqLen <= 0) || (ReqLen > sizeof (AMIVirtualDeviceSetStatusReq_T)))
  4886. {
  4887. *pRes = CC_REQ_INV_LEN;
  4888. return sizeof(INT8U);
  4889. }
  4890. //check if license is proper to run this command
  4891. if (g_PDKHandle[PDK_LICENSE_STATUS] != NULL)
  4892. {
  4893. int retval = 0;
  4894. retval = ((int(*)(char *))g_PDKHandle[PDK_LICENSE_STATUS]) ( "MEDIA" );
  4895. //if proper license is not there then return from the command
  4896. if ( retval <= 0 )
  4897. {
  4898. TCRIT("Licenseing error : No proper license to run this command");
  4899. pAMIVirtualDeviceSetStatusRes->CompletionCode = CC_OP_NOT_SUPPORTED;
  4900. return sizeof(INT8U);
  4901. }
  4902. }
  4903. else
  4904. {
  4905. pHandle = dlopen("/usr/local/lib/liblicense.so",RTLD_NOW);
  4906. BOOL IsValidApp = TRUE;
  4907. if(NULL != pHandle)
  4908. {
  4909. GetLicenseStatus = dlsym(pHandle, "GetLicenseStatus");
  4910. if(NULL != GetLicenseStatus)
  4911. {
  4912. GetLicenseStatus("MEDIA", &IsValidApp);
  4913. }
  4914. dlclose(pHandle);
  4915. }
  4916. if ( TRUE != IsValidApp )
  4917. {
  4918. TDBG("Licenseing error : No proper license to run this command\n");
  4919. pAMIVirtualDeviceSetStatusRes->CompletionCode = CC_OP_NOT_SUPPORTED;
  4920. return sizeof(INT8U);
  4921. }
  4922. }
  4923. switch (pAMIVirtualDeviceSetStatusReq->Status)
  4924. {
  4925. case VIRTUAL_DEVICE_DISABLE:
  4926. pAMIVirtualDeviceSetStatusRes->CompletionCode = OngetSetVirtualDevice(USB_DISABLE_ALL_DEVICE,0);
  4927. break;
  4928. case VIRTUAL_DEVICE_ENABLE:
  4929. pAMIVirtualDeviceSetStatusRes->CompletionCode = OngetSetVirtualDevice(USB_ENABLE_ALL_DEVICE,0);
  4930. break;
  4931. default:
  4932. {
  4933. TDBG("\n Case Failure in device status\n");
  4934. *pRes =CC_INV_DATA_FIELD;
  4935. return sizeof(INT8U);
  4936. }
  4937. }
  4938. }
  4939. else
  4940. {
  4941. *pRes =CC_INV_CMD;
  4942. return sizeof(INT8U);
  4943. }
  4944. return sizeof(AMIVirtualDeviceSetStatusRes_T);
  4945. }
  4946. int AMIVirtualDeviceGetStatus ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes,_NEAR_ int BMCInst )
  4947. {
  4948. u8 Status;
  4949. if(g_corefeatures.power_consumption_virtual_device_usb == ENABLED)
  4950. {
  4951. AMIVirtualDeviceGetStatusRes_T *pAMIVirtualDeviceSetStatusRes = (AMIVirtualDeviceGetStatusRes_T *)pRes;
  4952. pAMIVirtualDeviceSetStatusRes->CompletionCode = OngetSetVirtualDevice(USB_GET_ALL_DEVICE_STATUS,(u8 *)&Status);
  4953. pAMIVirtualDeviceSetStatusRes->Status = Status;
  4954. return sizeof(AMIVirtualDeviceGetStatusRes_T);
  4955. }
  4956. else
  4957. {
  4958. *pRes =CC_INV_CMD;
  4959. return sizeof(INT8U);
  4960. }
  4961. }
  4962. int AMIGetLicenseValidity ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes,_NEAR_ int BMCInst )
  4963. {
  4964. AMIGetLicenseValidityRes_T * AMIGetLicenseValidityRes = (AMIGetLicenseValidityRes_T * )pRes;
  4965. FILE *fd_St;
  4966. char OldAppCode[APPCODE_LEN] = {0}, NewAppCode[APPCODE_LEN] = {0}, StatusInfo[BUFFER_LENGTH];
  4967. BOOL Status = FALSE;
  4968. INT8U Validity = -1;
  4969. void *pHandle = NULL;
  4970. INT16 (*GetLicenseStatus)(const char*, BOOL*);
  4971. if(g_corefeatures.runtime_license_support != ENABLED)
  4972. {
  4973. AMIGetLicenseValidityRes->CompletionCode = CC_INV_CMD;
  4974. return sizeof(AMIGetLicenseValidityRes->CompletionCode);
  4975. }
  4976. memset(AMIGetLicenseValidityRes, 0, sizeof(AMIGetLicenseValidityRes_T));
  4977. fd_St = fopen(LICENSE_CONF, "r");
  4978. if(fd_St == NULL)
  4979. {
  4980. TDBG("\n problem in opening file");
  4981. AMIGetLicenseValidityRes->CompletionCode = CC_UNSPECIFIED_ERR;
  4982. return sizeof(INT8U);
  4983. }
  4984. AMIGetLicenseValidityRes->CompletionCode = CC_NORMAL;
  4985. pHandle = dlopen("/usr/local/lib/liblicense.so",RTLD_NOW);
  4986. while(fgets((char *)StatusInfo, BUFFER_LENGTH, fd_St) != NULL)
  4987. {
  4988. sscanf(StatusInfo, "%[^:]", NewAppCode);
  4989. if(strcmp(OldAppCode, NewAppCode) == 0)
  4990. continue;
  4991. memcpy(AMIGetLicenseValidityRes-> Lic_Info[AMIGetLicenseValidityRes->NumOfFeatures].FeatureName, NewAppCode, strlen(NewAppCode));
  4992. Status = FALSE;
  4993. if(NULL != pHandle)
  4994. {
  4995. GetLicenseStatus = dlsym(pHandle, "GetLicenseStatus");
  4996. if(NULL != GetLicenseStatus)
  4997. {
  4998. Validity = GetLicenseStatus(NewAppCode, &Status);
  4999. }
  5000. }
  5001. if(0 > Validity)
  5002. {
  5003. fclose(fd_St);
  5004. if(NULL != pHandle)
  5005. {
  5006. dlclose(pHandle);
  5007. }
  5008. AMIGetLicenseValidityRes->CompletionCode = CC_UNSPECIFIED_ERR;
  5009. return sizeof(INT8U);
  5010. }
  5011. strcpy(OldAppCode,NewAppCode);
  5012. memcpy(&AMIGetLicenseValidityRes->Lic_Info[AMIGetLicenseValidityRes->NumOfFeatures].Validity, &Validity, sizeof(Validity));
  5013. AMIGetLicenseValidityRes->NumOfFeatures += 1;
  5014. }
  5015. fclose(fd_St);
  5016. dlclose(pHandle);
  5017. return (sizeof(INT8U) + sizeof(AMIGetLicenseValidityRes->NumOfFeatures) + (AMIGetLicenseValidityRes->NumOfFeatures * sizeof(LicenseInfo_T)));
  5018. }
  5019. int AMIAddLicenseKey ( _NEAR_ INT8U *pReq, INT32U ReqLen, _NEAR_ INT8U *pRes, _NEAR_ int BMCInst )
  5020. {
  5021. AMIAddLicenseKeyReq_T AMIAddLicenseKeyReq;
  5022. AMIAddLicenseKeyRes_T *AMIAddLicenseKeyRes = (AMIAddLicenseKeyRes_T *)pRes;
  5023. FILE *fd_conf, *fd_st, *fd_tmp, *fd_key, *fd_cnt;
  5024. char ConfBf[BUFFER_LENGTH], StatusBf[BUFFER_LENGTH], KeyBf[LIC_KEY_LEN + 1]= {0};
  5025. char App_Conf[APPCODE_LEN] = {0}, App_St[APPCODE_LEN] = {0};
  5026. INT16U St_Vdt = 0, St_Cnt = 0;
  5027. char IfcName[16];
  5028. INT16S BitVal, pos = 0;
  5029. INT16U InitDayCnt;
  5030. int i;
  5031. INT32U FtrList = 0;
  5032. NWCFG_STRUCT NWConfig;
  5033. INT8U EthIndex = 0x0;
  5034. LicenseKeyInfo_T LicStr;
  5035. if(g_corefeatures.runtime_license_support != ENABLED)
  5036. {
  5037. AMIAddLicenseKeyRes->CompletionCode = CC_INV_CMD;
  5038. return sizeof(AMIAddLicenseKeyRes->CompletionCode);
  5039. }
  5040. memset(AMIAddLicenseKeyReq.LicenseKey, 0, sizeof(AMIAddLicenseKeyReq_T));
  5041. for(i = 0; i < ReqLen; i++)
  5042. {
  5043. if(pReq[i] != '-')
  5044. {
  5045. AMIAddLicenseKeyReq.LicenseKey[pos++] = pReq[i];
  5046. }
  5047. }
  5048. if(strlen((char *)AMIAddLicenseKeyReq.LicenseKey) != LIC_KEY_LEN)
  5049. {
  5050. TDBG("\nAMIAddLicenseKey():Request length invalid\n");
  5051. AMIAddLicenseKeyRes->CompletionCode = CC_REQ_INV_LEN;
  5052. return sizeof(AMIAddLicenseKeyRes->CompletionCode);
  5053. }
  5054. if(0 != IsDuplicateKey(AMIAddLicenseKeyReq.LicenseKey))
  5055. {
  5056. AMIAddLicenseKeyRes->CompletionCode = CC_INVALID_KEY;
  5057. return sizeof(AMIAddLicenseKeyRes->CompletionCode);
  5058. }
  5059. if(0 != Decrypt_LicenseKey(AMIAddLicenseKeyReq.LicenseKey, strlen((char *)AMIAddLicenseKeyReq.LicenseKey), (char *)&LicStr))
  5060. {
  5061. TDBG("Problem in decrypting\n");
  5062. AMIAddLicenseKeyRes->CompletionCode = CC_UNSPECIFIED_ERR;
  5063. return sizeof(AMIAddLicenseKeyRes->CompletionCode);
  5064. }
  5065. if((0 != memcmp(LicStr.Signature, LIC_SIGNATURE, LIC_SIGNATURE_LEN)) ||
  5066. (0 == LicStr.Lic_Validity ) || (0 == LicStr.FtrList))
  5067. {
  5068. AMIAddLicenseKeyRes->CompletionCode = CC_INVALID_KEY;
  5069. return sizeof(AMIAddLicenseKeyRes->CompletionCode);
  5070. }
  5071. if(0 != GetIfcName(EthIndex, IfcName, BMCInst))
  5072. {
  5073. TCRIT("\nAMIAddLicenseKey(): Error in Getting IfcName\n");
  5074. AMIAddLicenseKeyRes->CompletionCode = CC_UNSPECIFIED_ERR;
  5075. return sizeof (AMIAddLicenseKeyRes->CompletionCode);
  5076. }
  5077. if(0 != nwGetNWInformations(&NWConfig, IfcName))
  5078. {
  5079. TDBG("\nAMIAddLicenseKey(): Problem in loading MAC address\n");
  5080. AMIAddLicenseKeyRes->CompletionCode = CC_UNSPECIFIED_ERR;
  5081. return sizeof(AMIAddLicenseKeyRes->CompletionCode);
  5082. }
  5083. if(LicStr.MACAddr[3] == 0xFF && LicStr.MACAddr[4] == 0xFF && LicStr.MACAddr[5] == 0xFF )
  5084. {
  5085. if(LicStr.MACAddr[0] != 0xFF || LicStr.MACAddr[1] != 0xFF || LicStr.MACAddr[2] != 0xFF)
  5086. {
  5087. if((memcmp(LicStr.MACAddr, NWConfig.MAC, MAC_OUI_LEN) != 0))
  5088. {
  5089. TDBG("\nAMIAddLicenseKey(): Invalid MAC address\n");
  5090. AMIAddLicenseKeyRes->CompletionCode = CC_INVALID_KEY;
  5091. return sizeof(AMIAddLicenseKeyRes->CompletionCode);
  5092. }
  5093. }
  5094. }
  5095. else
  5096. {
  5097. if(memcmp(LicStr.MACAddr, NWConfig.MAC, sizeof(LicStr.MACAddr)) != 0)
  5098. {
  5099. TDBG("\nAMIAddLicenseKey(): Invalid MAC address\n");
  5100. AMIAddLicenseKeyRes->CompletionCode = CC_INVALID_KEY;
  5101. return sizeof(AMIAddLicenseKeyRes->CompletionCode);
  5102. }
  5103. }
  5104. fd_conf = fopen(LICENSE_CONF, "r");
  5105. if(fd_conf == NULL)
  5106. {
  5107. TDBG("\nAMIAddLicenseKey(): Problem in opening %s file\n", LICENSE_CONF);
  5108. AMIAddLicenseKeyRes->CompletionCode = CC_UNSPECIFIED_ERR;
  5109. return sizeof(AMIAddLicenseKeyRes->CompletionCode);
  5110. }
  5111. fd_st = fopen(LICENSE_STATUS_FILE, "a+");
  5112. if(fd_st == NULL)
  5113. {
  5114. TDBG("\nAMIAddLicenseKey(): Problem in opening %s file\n", LICENSE_STATUS_FILE);
  5115. fclose(fd_conf);
  5116. AMIAddLicenseKeyRes->CompletionCode = CC_UNSPECIFIED_ERR;
  5117. return sizeof(AMIAddLicenseKeyRes->CompletionCode);
  5118. }
  5119. fd_cnt = fopen(LICENSE_CNT_DAYS_FILE,"r");
  5120. if(fd_cnt == NULL)
  5121. {
  5122. TDBG("\nAMIAddLicenseKey(): Problem in opening %s file\n", LICENSE_CNT_DAYS_FILE);
  5123. fclose(fd_conf);
  5124. fclose(fd_st);
  5125. AMIAddLicenseKeyRes->CompletionCode = CC_UNSPECIFIED_ERR;
  5126. return sizeof(AMIAddLicenseKeyRes->CompletionCode);
  5127. }
  5128. fscanf(fd_cnt, "%hd", &InitDayCnt);
  5129. if(0 == InitDayCnt && 0xFF != LicStr.Lic_Validity)
  5130. {
  5131. InitDayCnt++;
  5132. }
  5133. fclose(fd_cnt);
  5134. FtrList = LicStr.FtrList;
  5135. while(fgets(ConfBf, BUFFER_LENGTH, fd_conf) != NULL)
  5136. {
  5137. INT16U Validity;
  5138. BOOL IsDelApp = FALSE;
  5139. BitVal = FtrList & 0x1;
  5140. FtrList = FtrList >> 1;
  5141. if(BitVal != 1)
  5142. {
  5143. continue;
  5144. }
  5145. sscanf(ConfBf, "%[^:]", App_Conf);
  5146. fseek(fd_st, 0, SEEK_SET);
  5147. while( fgets(StatusBf, BUFFER_LENGTH, fd_st) != NULL)
  5148. {
  5149. sscanf(StatusBf, "%[^:]:%hd", App_St, &Validity);
  5150. // check if app is already licensed and its validity /
  5151. if(0 ==strcmp(App_Conf, App_St))
  5152. {
  5153. if(0xFF == LicStr.Lic_Validity || Validity < LicStr.Lic_Validity)
  5154. {
  5155. IsDelApp = TRUE;
  5156. break;
  5157. }
  5158. else
  5159. {
  5160. TDBG("\nAMIAddLicenseKey(): Invalid Validity\n");
  5161. AMIAddLicenseKeyRes->CompletionCode = CC_INVALID_KEY;
  5162. return sizeof(AMIAddLicenseKeyRes->CompletionCode);
  5163. }
  5164. }
  5165. }
  5166. if(IsDelApp)
  5167. {
  5168. fd_tmp = fopen(LICENSE_TEMP_FILE, "w");
  5169. if(fd_tmp == NULL)
  5170. {
  5171. TDBG("\nAMIAddLicenseKey(): Problem in creating %s file\n", LICENSE_TEMP_FILE);
  5172. fclose(fd_st);
  5173. fclose(fd_conf);
  5174. }
  5175. fseek(fd_st, 0, SEEK_SET);
  5176. while(fgets(StatusBf, BUFFER_LENGTH, fd_st) != NULL)
  5177. {
  5178. sscanf(StatusBf, "%[^:]:%hd:%hd", App_St, &St_Vdt, &St_Cnt);
  5179. if(strcmp(App_Conf, App_St) != 0 )
  5180. {
  5181. fprintf(fd_tmp, "%s:%hd:%hd",App_St,St_Vdt,St_Cnt);
  5182. }
  5183. }
  5184. fclose(fd_st);
  5185. fd_st=fopen(LICENSE_STATUS_FILE,"w");
  5186. if(NULL == fd_st)
  5187. {
  5188. printf("Problem in opening %s file\n", LICENSE_STATUS_FILE);
  5189. return -1;
  5190. }
  5191. fseek(fd_tmp, 0, SEEK_SET);
  5192. while(!feof(fd_tmp))
  5193. {
  5194. if(fgets(StatusBf, BUFFER_LENGTH, fd_tmp) == NULL)
  5195. break;
  5196. fputs(StatusBf, fd_st);
  5197. }
  5198. fclose(fd_st);
  5199. fclose(fd_tmp);
  5200. fd_st =NULL;
  5201. IsDelApp = FALSE;
  5202. }
  5203. if(fd_st == NULL)
  5204. {
  5205. fd_st = fopen(LICENSE_STATUS_FILE, "a+");
  5206. if(fd_st == NULL)
  5207. {
  5208. TDBG("\nAMIAddLicenseKey(): Problem in opening %s file\n", LICENSE_STATUS_FILE);
  5209. AMIAddLicenseKeyRes->CompletionCode = CC_UNSPECIFIED_ERR;
  5210. return sizeof(AMIAddLicenseKeyRes->CompletionCode);
  5211. }
  5212. }
  5213. else
  5214. {
  5215. fseek(fd_st, 0, SEEK_END);
  5216. }
  5217. fprintf(fd_st, "%s:%hd:%hd", App_Conf, (short int)LicStr.Lic_Validity, InitDayCnt);
  5218. fprintf(fd_st,"\n");
  5219. }
  5220. fclose(fd_st);
  5221. fclose(fd_conf);
  5222. if( FtrList > 0)
  5223. {
  5224. TDBG("\nAMIAddLicenseKey(): NON-Existing App \n");
  5225. AMIAddLicenseKeyRes->CompletionCode = CC_INVALID_KEY;
  5226. return sizeof(AMIAddLicenseKeyRes->CompletionCode);
  5227. }
  5228. fd_key = fopen(LICENSE_KEY_FILE, "a+");
  5229. if(NULL == fd_key)
  5230. {
  5231. TDBG("\nAMIAddLicenseKey(): problem in opening license_keys file");
  5232. AMIAddLicenseKeyRes->CompletionCode = CC_UNSPECIFIED_ERR;
  5233. return sizeof(AMIAddLicenseKeyRes->CompletionCode);
  5234. }
  5235. memcpy(KeyBf, AMIAddLicenseKeyReq.LicenseKey, LIC_KEY_LEN);
  5236. fputs(KeyBf, fd_key);
  5237. fprintf(fd_key,"\n");
  5238. fclose(fd_key);
  5239. if(0xFF != LicStr.Lic_Validity)
  5240. {
  5241. fd_cnt = fopen(LICENSE_CNT_DAYS_FILE, "w");
  5242. if(NULL == fd_cnt)
  5243. {
  5244. TDBG("\nProblem in Creating %s file\n", fd_cnt);
  5245. AMIAddLicenseKeyRes->CompletionCode = CC_UNSPECIFIED_ERR;
  5246. return sizeof(AMIAddLicenseKeyRes->CompletionCode);
  5247. }
  5248. fprintf(fd_cnt, "%hd", InitDayCnt);
  5249. fclose(fd_cnt);
  5250. }
  5251. safe_system("/etc/init.d/licenseapp.sh manual");
  5252. AMIAddLicenseKeyRes->CompletionCode = CC_NORMAL;
  5253. return sizeof (AMIAddLicenseKeyRes_T);
  5254. }
  5255. int Decrypt_LicenseKey(unsigned char * LicenseKey, int Size, char *DecryptedLicenseKey)
  5256. {
  5257. unsigned char UnMap[Size], DecodedPasswd[Size];
  5258. INT8U key[MAX_SIZE_KEY] = {0};
  5259. memset(UnMap, 0, Size);
  5260. if(0 == UnMap32(LicenseKey, Size, UnMap))
  5261. {
  5262. TDBG("\n Decrypt_LicenseKey():Problem in reverse mapping");
  5263. return -1;
  5264. }
  5265. memset(DecodedPasswd, 0, Size);
  5266. if(0 == Decode32(UnMap, Size, DecodedPasswd))
  5267. {
  5268. TDBG("\n Decrypt_LicenseKey():Problem in Decoding\n");
  5269. return -1;
  5270. }
  5271. /* Get the encryption key from /conf/pwdEncKey file */
  5272. if(getEncryptKey(key))
  5273. {
  5274. TDBG("Unable to get the Decryption key. So, quitting...\n");
  5275. return -1;
  5276. }
  5277. return DecryptPassword((char *)DecodedPasswd, Size, DecryptedLicenseKey, Size, (unsigned char *)key);
  5278. }
  5279. int IsDuplicateKey(unsigned char *LicenseKey)
  5280. {
  5281. FILE *fd_Key;
  5282. char LicKeyBf[LIC_KEY_LEN + 1];
  5283. fd_Key = fopen (LICENSE_KEY_FILE, "r");
  5284. if(fd_Key == NULL)
  5285. {
  5286. fd_Key = fopen (LICENSE_KEY_FILE, "a+");
  5287. if(fd_Key == NULL)
  5288. {
  5289. TDBG("\nproblem in opening license/keysfile\n");
  5290. return -1;
  5291. }
  5292. }
  5293. fseek(fd_Key, 0, SEEK_SET);
  5294. while( NULL != fgets(LicKeyBf, LIC_KEY_LEN + 1, fd_Key))
  5295. {
  5296. if(memcmp(LicKeyBf, LicenseKey, strlen((char *)LicenseKey))== 0)
  5297. {
  5298. fclose(fd_Key);
  5299. return -2;
  5300. }
  5301. }
  5302. fclose(fd_Key);
  5303. return 0;
  5304. }