AMIRestoreDefaults.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. * AMIRestoreDefaults.c
  17. * AMI Restore Factory settings related implementation.
  18. *
  19. * Author: Gokula Kannan. S <gokulakannans@amiindia.co.in>
  20. ******************************************************************/
  21. #include "Debug.h"
  22. #include "Support.h"
  23. #include "IPMIDefs.h"
  24. #include "IPMI_AMIConf.h"
  25. #include "AMIRestoreDefaults.h"
  26. #include "flshfiles.h"
  27. #include "flashlib.h"
  28. #include "PendTask.h"
  29. #include <sys/reboot.h>
  30. #include <linux/reboot.h>
  31. #include <dlfcn.h>
  32. #include <pthread.h>
  33. //#include "OSPort.h"
  34. #include "PDKAccess.h"
  35. //#include "IPMIConf.h"
  36. #include "libpreserveconf.h"
  37. #include "featuredef.h"
  38. #define RESTORE_PRESERVE_CMD "/usr/local/bin/preservecfg 1 &"
  39. #define RESTORE_DEFAULTS_CMD "sh /etc/restoredefaults.sh restore &"
  40. int AMIRestoreDefaults(_NEAR_ INT8U* pReq, INT32U ReqLen, _NEAR_ INT8U* pRes,int BMCInst)
  41. {
  42. RestartService_T Service;
  43. INT8U *curchannel;
  44. if(ReqLen > 0)
  45. {
  46. *pRes = CC_REQ_INV_LEN;
  47. return sizeof(*pRes);
  48. }
  49. int retVal = PrepareFlashArea(FLSH_CMD_DUMMY_FLASH_AREA, g_corefeatures.dual_image_support);
  50. if (retVal != EXIT_SUCCESS)
  51. {
  52. IPMI_ERROR("Flash is going on currently");
  53. (*pRes) = CC_DEV_IN_FIRMWARE_UPDATE_MODE;
  54. goto end;
  55. }
  56. if(g_corefeatures.preserve_config == ENABLED)
  57. {
  58. system (RESTORE_PRESERVE_CMD);
  59. }
  60. else
  61. {
  62. system (RESTORE_DEFAULTS_CMD);
  63. }
  64. TINFO("Stopping adviserd...");
  65. system("/etc/init.d/adviserd.sh stop");
  66. TINFO("Restoring to default configuration... Done");
  67. /* PDK Module Post Set Reboot Cause*/
  68. if(g_PDKHandle[PDK_SETREBOOTCAUSE] != NULL)
  69. {
  70. ((INT8U(*)(INT8U,int)) g_PDKHandle[PDK_SETREBOOTCAUSE])(SETREBOOTCAUSE_IPMI_CMD_PROCESSING,BMCInst);
  71. }
  72. //Post BMC Reboot task to Pend task
  73. Service.ServiceName = REBOOT;
  74. Service.SleepSeconds = 3; // Sleep for 3 Seconds
  75. SetPendStatus(PEND_OP_RESTART_SERVICES, PEND_STATUS_PENDING);
  76. OS_THREAD_TLS_GET(g_tls.CurChannel,curchannel);
  77. PostPendTask(PEND_OP_RESTART_SERVICES, (INT8U *) &Service, sizeof(RestartService_T),*curchannel & 0xF,BMCInst);
  78. (*pRes) = CC_SUCCESS;
  79. end:
  80. return sizeof(*pRes);
  81. }
  82. int AMIGetPreserveConfStatus(_NEAR_ INT8U* pReq, INT32U ReqLen, _NEAR_ INT8U* pRes,int BMCInst)
  83. {
  84. GetPreserveConfigRes_T* pPreserveConfRes = ( GetPreserveConfigRes_T* ) pRes;
  85. GetPreserveConfigReq_T* pPreserveConfReq = ( GetPreserveConfigReq_T* ) pReq;
  86. int status = -1;
  87. void *dl_handle = NULL;
  88. int ( *dl_func )( int, char* )=NULL;
  89. if(g_corefeatures.preserve_config == ENABLED)
  90. {
  91. dl_handle = dlopen ( "/usr/local/lib/libpreserveconf.so", RTLD_NOW );
  92. if(NULL == dl_handle)
  93. {
  94. IPMI_ERROR("Error in loading libpreserveconf.so library %s\n", dlerror() );
  95. return -1;
  96. }
  97. pPreserveConfRes->CompletionCode = CC_NORMAL;
  98. if (ReqLen != 1)
  99. {
  100. pPreserveConfRes->CompletionCode = CC_REQ_INV_LEN;
  101. goto end;
  102. }
  103. if(pPreserveConfReq->Selector >= 200)
  104. {
  105. if(g_PDKHandle[PDK_GETPRESERVESTATUS] != NULL)
  106. {
  107. TDBG("gloabl handle is not NULL\n");
  108. status = ((int(*)(int, int))g_PDKHandle[PDK_GETPRESERVESTATUS]) (pPreserveConfReq->Selector, BMCInst);
  109. if (-1 == status)
  110. {
  111. pPreserveConfRes->CompletionCode = CC_UNSPECIFIED_ERR;
  112. }
  113. if (-2 == status)
  114. {
  115. pPreserveConfRes->CompletionCode = CC_PARAM_NOT_SUPPORTED;
  116. }
  117. else
  118. {
  119. pPreserveConfRes->Status = status;
  120. TDBG(" status :: %d\n", pPreserveConfRes->Status);
  121. }
  122. goto end;
  123. }
  124. }
  125. dl_func = dlsym (dl_handle, "GetPreserveStatus");
  126. if ( NULL == dl_func )
  127. {
  128. IPMI_ERROR("Error in getting symbol %s \n", dlerror());
  129. pPreserveConfRes->CompletionCode = CC_UNSPECIFIED_ERR;
  130. goto end;
  131. }
  132. //status = GetPreserveStatus(pPreserveConfReq->Selector, PRESERVE_AMI_FILE);
  133. status = dl_func(pPreserveConfReq->Selector, PRESERVE_AMI_FILE);
  134. if (-1 == status)
  135. {
  136. pPreserveConfRes->CompletionCode = CC_UNSPECIFIED_ERR;
  137. }
  138. else if (-2 == status)
  139. {
  140. pPreserveConfRes->CompletionCode = CC_PARAM_NOT_SUPPORTED;
  141. }
  142. else
  143. {
  144. pPreserveConfRes->Status = status;
  145. TDBG(" status :: %d\n", pPreserveConfRes->Status);
  146. }
  147. end:
  148. if (NULL != dl_handle)
  149. dlclose (dl_handle);
  150. return sizeof(GetPreserveConfigRes_T);
  151. }
  152. else
  153. {
  154. //If Preserve Conf feature is disabled in Project Configuration, return Not Support
  155. pPreserveConfRes->CompletionCode = CC_INV_CMD;
  156. return sizeof(GetPreserveConfigRes_T);
  157. }
  158. }
  159. int AMISetPreserveConfStatus(_NEAR_ INT8U* pReq, INT32U ReqLen, _NEAR_ INT8U* pRes,int BMCInst)
  160. {
  161. SetPreserveConfigRes_T* pPreserveConfRes = ( SetPreserveConfigRes_T* ) pRes;
  162. SetPreserveConfigReq_T* pPreserveConfReq = ( SetPreserveConfigReq_T* ) pReq;
  163. void *dl_handle = NULL;
  164. int ( *dl_func )( int, int, char* )=NULL;
  165. int status = -1;
  166. if(g_corefeatures.preserve_config == ENABLED)
  167. {
  168. dl_handle = dlopen ( "/usr/local/lib/libpreserveconf.so", RTLD_NOW );
  169. if(NULL == dl_handle)
  170. {
  171. IPMI_ERROR("Error in loading libpreserveconf.so library %s\n", dlerror() );
  172. return -1;
  173. }
  174. memset (pPreserveConfRes, 0x00, sizeof (SetPreserveConfigRes_T));
  175. pPreserveConfRes->CompletionCode = CC_NORMAL;
  176. if (ReqLen != 2)
  177. {
  178. pPreserveConfRes->CompletionCode = CC_REQ_INV_LEN;
  179. goto end;
  180. }
  181. if (pPreserveConfReq->Status == 0 || pPreserveConfReq->Status == 1)
  182. {
  183. if(pPreserveConfReq->Selector >= 200)
  184. {
  185. if(g_PDKHandle[PDK_SETPRESERVESTATUS] != NULL)
  186. {
  187. status = ((int(*)(int, int, int))g_PDKHandle[PDK_SETPRESERVESTATUS]) (pPreserveConfReq->Selector, pPreserveConfReq->Status, BMCInst);
  188. if( -1 == status)
  189. {
  190. pPreserveConfRes->CompletionCode = CC_UNSPECIFIED_ERR;
  191. }
  192. if (-2 == status)
  193. {
  194. pPreserveConfRes->CompletionCode = CC_PARAM_NOT_SUPPORTED;
  195. }
  196. else
  197. pPreserveConfRes->CompletionCode = status;
  198. goto end;
  199. }
  200. }
  201. dl_func = dlsym (dl_handle, "SetPreserveStatus");
  202. if ( NULL == dl_func )
  203. {
  204. IPMI_ERROR("Error in getting symbol %s \n", dlerror());
  205. pPreserveConfRes->CompletionCode = CC_UNSPECIFIED_ERR;
  206. goto end;
  207. }
  208. status = dl_func(pPreserveConfReq->Selector, pPreserveConfReq->Status, PRESERVE_AMI_FILE);
  209. if (-1 == status)
  210. {
  211. pPreserveConfRes->CompletionCode = CC_UNSPECIFIED_ERR;
  212. }
  213. else if (-2 == status)
  214. {
  215. pPreserveConfRes->CompletionCode = CC_PARAM_NOT_SUPPORTED;
  216. }
  217. else
  218. pPreserveConfRes->CompletionCode = CC_NORMAL;
  219. }
  220. else
  221. {
  222. pPreserveConfRes->CompletionCode = CC_UNSPECIFIED_ERR;
  223. }
  224. end:
  225. if (NULL != dl_handle)
  226. dlclose (dl_handle);
  227. return sizeof(SetPreserveConfigRes_T);
  228. }
  229. else
  230. {
  231. //If Preserve Conf feature is disabled in Project Configuration, return Not Support
  232. pPreserveConfRes->CompletionCode = CC_INV_CMD;
  233. return sizeof(SetPreserveConfigRes_T);
  234. }
  235. }
  236. int AMIGetAllPreserveConfStatus(_NEAR_ INT8U* pReq, INT32U ReqLen, _NEAR_ INT8U* pRes,int BMCInst)
  237. {
  238. GetAllPreserveConfigRes_T* pAllPreserveConfRes = ( GetAllPreserveConfigRes_T* ) pRes;
  239. int status = -1;
  240. void *dl_handle = NULL;
  241. int ( *dl_func )( unsigned int *, char* ) = NULL;
  242. unsigned int enabledstatus;
  243. if(g_corefeatures.preserve_config == ENABLED)
  244. {
  245. dl_handle = dlopen ( "/usr/local/lib/libpreserveconf.so", RTLD_NOW );
  246. if(NULL == dl_handle)
  247. {
  248. IPMI_ERROR("Error in loading libpreserveconf.so library %s\n", dlerror() );
  249. return -1;
  250. }
  251. pAllPreserveConfRes->CompletionCode = CC_NORMAL;
  252. dl_func = dlsym (dl_handle, "GetAllPreserveStatus");
  253. if ( NULL == dl_func )
  254. {
  255. IPMI_ERROR("Error in getting symbol %s \n", dlerror());
  256. pAllPreserveConfRes->CompletionCode = CC_UNSPECIFIED_ERR;
  257. if (NULL != dl_handle)
  258. dlclose (dl_handle);
  259. return sizeof(GetAllPreserveConfigRes_T);
  260. }
  261. status = dl_func( &enabledstatus, PRESERVE_AMI_FILE );
  262. if (-1 == status)
  263. {
  264. pAllPreserveConfRes->CompletionCode = CC_UNSPECIFIED_ERR;
  265. }
  266. else
  267. {
  268. pAllPreserveConfRes->EnabledStatus = enabledstatus;
  269. pAllPreserveConfRes->Status = status;
  270. TDBG(" status :: %d\n", pAllPreserveConfRes->Status);
  271. pAllPreserveConfRes->Reserved = 0x00;
  272. }
  273. if (NULL != dl_handle)
  274. dlclose (dl_handle);
  275. return sizeof(GetAllPreserveConfigRes_T);
  276. }
  277. else
  278. {
  279. // If Preserve Conf feature is disabled in Project Configuration, return Not Support
  280. pAllPreserveConfRes->CompletionCode = CC_INV_CMD;
  281. return sizeof(GetAllPreserveConfigRes_T);
  282. }
  283. }
  284. int AMISetAllPreserveConfStatus(_NEAR_ INT8U* pReq, INT32U ReqLen, _NEAR_ INT8U* pRes,int BMCInst)
  285. {
  286. SetAllPreserveConfigReq_T* pAllPreserveConfReq = ( SetAllPreserveConfigReq_T* ) pReq;
  287. SetAllPreserveConfigRes_T* pAllPreserveConfRes = ( SetAllPreserveConfigRes_T* ) pRes;
  288. void *dl_handle = NULL;
  289. int ( *dl_func )( int, char* ) = NULL;
  290. int status = -1;
  291. if ( pAllPreserveConfReq->Status >= BIT10)
  292. {
  293. pAllPreserveConfRes->CompletionCode = CC_INV_DATA_FIELD;
  294. return sizeof(SetAllPreserveConfigRes_T);
  295. }
  296. if( ENABLED == g_corefeatures.preserve_config )
  297. {
  298. dl_handle = dlopen ( "/usr/local/lib/libpreserveconf.so", RTLD_NOW );
  299. if( NULL == dl_handle )
  300. {
  301. IPMI_ERROR("Error in loading libpreserveconf.so library %s\n", dlerror() );
  302. return -1;
  303. }
  304. memset (pAllPreserveConfRes, 0x00, sizeof (SetAllPreserveConfigRes_T));
  305. pAllPreserveConfRes->CompletionCode = CC_NORMAL;
  306. dl_func = dlsym (dl_handle, "SetAllPreserveStatus");
  307. if ( NULL == dl_func )
  308. {
  309. IPMI_ERROR("Error in getting symbol %s \n", dlerror());
  310. pAllPreserveConfRes->CompletionCode = CC_UNSPECIFIED_ERR;
  311. if (NULL != dl_handle)
  312. dlclose (dl_handle);
  313. return sizeof(SetAllPreserveConfigRes_T);
  314. }
  315. status = dl_func( pAllPreserveConfReq->Status, PRESERVE_AMI_FILE);
  316. if ( -1 == status)
  317. {
  318. pAllPreserveConfRes->CompletionCode = CC_UNSPECIFIED_ERR;
  319. }
  320. else if ( -2 == status)
  321. {
  322. pAllPreserveConfRes->CompletionCode = CC_CMD_UNSUPPORTED_UNCONFIGURABLE;
  323. }
  324. else
  325. {
  326. pAllPreserveConfRes->CompletionCode = CC_NORMAL;
  327. }
  328. if (NULL != dl_handle)
  329. dlclose (dl_handle);
  330. return sizeof(SetAllPreserveConfigRes_T);
  331. }
  332. else
  333. {
  334. // If Preserve Conf feature is disabled in Project Configuration, return Not Support
  335. pAllPreserveConfRes->CompletionCode = CC_INV_CMD;
  336. return sizeof(SetAllPreserveConfigRes_T);
  337. }
  338. }