Api.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <sys/socket.h>
  7. #include <fcntl.h>
  8. #include <net/if.h>
  9. #include <netinet/in.h>
  10. #include <sys/ioctl.h>
  11. #include "main.h"
  12. #include "Api.h"
  13. #include "SDRRecord.h"
  14. #include "SELRecord.h"
  15. #include "SEL.h"
  16. #include "MsgHndlr.h"
  17. int InitSdrConfig(void)
  18. {
  19. printf("Init SDR Config...\r\n");
  20. g_BMCInfo.SDRConfig.SDRError = 0;
  21. g_BMCInfo.SDRConfig.UpdatingSDR = FALSE;
  22. g_BMCInfo.SDRConfig.UpdatingChannel = 0;
  23. g_BMCInfo.SDRConfig.TrackPOffset = 0;
  24. g_BMCInfo.SDRConfig.TrackRecID = 0;
  25. g_BMCInfo.SDRConfig.ReservationID = 0;
  26. g_BMCInfo.SDRConfig.IPMB_Seqnum = 0;
  27. g_BMCInfo.SDRConfig.PartAddbytes = 0;
  28. g_BMCInfo.SDRConfig.LatestRecordID = 0;
  29. g_BMCInfo.SDRConfig.NumMarkedRecords = 0;
  30. g_BMCInfo.SDRConfig.SDRRAM = (SDRRepository_T*)g_BMCInfo.pSDR;
  31. g_BMCInfo.SDRConfig.RepositoryInfo.Version = 0x51;
  32. g_BMCInfo.SDRConfig.RepositoryInfo.RecCt = ((SDRRepository_T*)g_BMCInfo.SDRConfig.SDRRAM)->NumRecords;
  33. g_BMCInfo.SDRConfig.RepositoryInfo.FreeSpace = 0xffff;
  34. g_BMCInfo.SDRConfig.RepositoryInfo.AddTimeStamp = 0;
  35. g_BMCInfo.SDRConfig.RepositoryInfo.EraseTimeStamp = 0;
  36. g_BMCInfo.SDRConfig.RepositoryInfo.OpSupport = 0x23;
  37. //TODO:
  38. g_BMCInfo.SDRConfig.RepositoryAllocInfo.NumAllocUnits = 0;
  39. g_BMCInfo.SDRConfig.RepositoryAllocInfo.AllocUnitSize = 0;
  40. g_BMCInfo.SDRConfig.RepositoryAllocInfo.NumFreeAllocUnits = 0;
  41. g_BMCInfo.SDRConfig.RepositoryAllocInfo.LargestFreeBlock = 0;
  42. g_BMCInfo.SDRConfig.RepositoryAllocInfo.MaxRecSize = 0;
  43. return 0;
  44. }
  45. int InitSelConfig(void)
  46. {
  47. printf("InitSelConfig...\n");
  48. g_BMCInfo.SELConfig.SelReservationID = 0;;
  49. g_BMCInfo.SELConfig.LastEvtTS = 0;
  50. g_BMCInfo.SELConfig.PartialAddRecordID = 0;
  51. g_BMCInfo.SELConfig.PartialAddRecOffset = 0;
  52. g_BMCInfo.SELConfig.PartialAdd = 0;
  53. g_BMCInfo.SELConfig.SenMonSELFlag = 0;
  54. g_BMCInfo.SELConfig.MaxSELRecord = MAX_SEL_RECORD;
  55. // g_BMCInfo.SELConfig.RsrvIDCancelled = FALSE;
  56. g_BMCInfo.SELConfig.SELOverFlow = FALSE;
  57. g_BMCInfo.SELConfig.selalmostfull = 0;
  58. // SELEventRecord_T SelPartialAddRecord;
  59. // g_BMCInfo.SELConfig.SELEventMsg [16];
  60. return 0;
  61. }
  62. int PlatformInit(void)
  63. {
  64. uint8_t PrimaryIPMBBusNum, SecondaryIPMBBusNum;
  65. printf("Init Platform...\r\n");
  66. // //hardware init
  67. // GPIO_InitTypeDef GPIO_InitStruct;
  68. // GPIO_InitStruct.Pin = GA0_PIN | GA1_PIN | GA2_PIN | GA3_PIN | GA4_PIN | RACKID2_PIN;
  69. // GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  70. // GPIO_InitStruct.Pull = GPIO_NOPULL;
  71. // GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  72. // stm32_gpio_init(GPIOH, &GPIO_InitStruct);
  73. // GPIO_InitStruct.Pin = GAP_PIN | RACKID1_PIN | RACKID3_PIN | RACKID4_PIN | RACKID5_PIN;
  74. // stm32_gpio_init(GPIOI, &GPIO_InitStruct);
  75. g_BMCInfo.SelfTestByte = 0;
  76. g_BMCInfo.SlotID = PDK_GetSlotID();
  77. g_BMCInfo.ChassisID = PDK_GetChassisID();
  78. g_BMCInfo.ChassisIdentify = FALSE;
  79. //init DevGUID
  80. g_BMCInfo.DeviceGUID[0] = 0x01;
  81. g_BMCInfo.DeviceGUID[1] = 0x01;
  82. g_BMCInfo.DeviceGUID[2] = 0x01;
  83. g_BMCInfo.DeviceGUID[3] = 0x01;
  84. g_BMCInfo.DeviceGUID[4] = 0x01;
  85. g_BMCInfo.DeviceGUID[5] = 0x01;
  86. g_BMCInfo.DeviceGUID[6] = 0x01;
  87. g_BMCInfo.DeviceGUID[7] = 0x01;
  88. g_BMCInfo.DeviceGUID[8] = 0x01;
  89. g_BMCInfo.DeviceGUID[9] = 0x01;
  90. g_BMCInfo.DeviceGUID[10] = 0x01;
  91. g_BMCInfo.DeviceGUID[11] = 0x01;
  92. g_BMCInfo.DeviceGUID[12] = 0x01;
  93. g_BMCInfo.DeviceGUID[13] = 0x01;
  94. g_BMCInfo.DeviceGUID[14] = 0x01;
  95. g_BMCInfo.DeviceGUID[15] = 0x01;
  96. g_BMCInfo.FwMajorVer = FW_VERSION_MAJOR;
  97. g_BMCInfo.FwMinorVer = FW_VERSION_MINOR;
  98. g_BMCInfo.SendMsgSeqNum = 0;
  99. // g_BMCInfo.OemFlags.BladeWorkMode = BLADE_IPMC;
  100. // g_BMCInfo.OemFlags.chassisManageFnEnable = 0;
  101. // g_BMCInfo.OemFlags.thisBladeIndex = 0;
  102. // g_BMCInfo.OemFlags.bladeStatus = 1; //0: not present, 1: normal, 2: error, others: reserved.
  103. g_BMCInfo.HealthLevel = SENSOR_STATUS_NORMAL;
  104. g_BMCInfo.SensorSharedMem.SensorTick= 0;
  105. g_BMCInfo.SenConfig.PowerOnTick = 0;
  106. g_BMCInfo.SenConfig.SysResetTick = 0;
  107. g_BMCInfo.CurTimerTick = 0;
  108. g_BMCInfo.CurTimerSecond = 0;
  109. g_BMCInfo.BootValidMinutes = 0;
  110. return 0;
  111. }
  112. int InitTimerTaskTbl(void)
  113. {
  114. printf("InitTimerTaskTbl...\n");
  115. g_BMCInfo.TimerTaskTblSize = 2;
  116. memcpy(g_BMCInfo.TimerTaskTbl, m_TimerTaskTbl, sizeof(TimerTaskTbl_T)*g_BMCInfo.TimerTaskTblSize);
  117. }
  118. int Init_SessionTbl(void)
  119. {
  120. printf("Init_SessionTbl...\n");
  121. g_BMCInfo.SessionHandle = 0;
  122. g_BMCInfo.UDSSessionHandle = 0;
  123. g_BMCInfo.IpmiConfig.MaxSession = 10;
  124. g_BMCInfo.IpmiConfig.SessionTimeOut = 10; //10s
  125. g_BMCInfo.IpmiConfig.SendMsgTimeout = 10; //10s
  126. /*Allocating Memory to hold session Table informations */
  127. g_BMCInfo.SessionTblInfo.SessionTbl = (SessionInfo_T *) malloc(sizeof(SessionInfo_T)*( g_BMCInfo.IpmiConfig.MaxSession + 1));
  128. if(g_BMCInfo.SessionTblInfo.SessionTbl == NULL)
  129. {
  130. printf("Error in allocating memory for SessionTbl \n");
  131. return 1;
  132. }
  133. g_BMCInfo.SessionTblInfo.Count = 0;
  134. /*Initialize the Session Table memory */
  135. memset(g_BMCInfo.SessionTblInfo.SessionTbl,0,sizeof(SessionInfo_T)*(g_BMCInfo.IpmiConfig.MaxSession + 1));
  136. /*Allocating Memory to hold UDS session Table informations */
  137. g_BMCInfo.UDSSessionTblInfo.UDSSessionTbl = (UDSSessionTbl_T *) malloc(sizeof(UDSSessionTbl_T)*(g_BMCInfo.IpmiConfig.MaxSession + 1));
  138. if(g_BMCInfo.UDSSessionTblInfo.UDSSessionTbl == NULL)
  139. {
  140. printf("Error in allocating memory for SessionTbl \n");
  141. return 1;
  142. }
  143. g_BMCInfo.UDSSessionTblInfo.SessionCount = 0;
  144. /*Initialize the UDS Session Table memory */
  145. memset(g_BMCInfo.UDSSessionTblInfo.UDSSessionTbl,0,sizeof(UDSSessionTbl_T)*(g_BMCInfo.IpmiConfig.MaxSession + 1));
  146. }
  147. const char FirstPowerOnStr[] = "First power on the bmc";
  148. int Init_IPMI_FRU_SDR_SEL(void)
  149. {
  150. int i;
  151. uint32_t sdrSize =
  152. sizeof(SDRRepository_T) + sizeof(HdrMgmtCtrlrDevLocator_T) + sizeof(HdrFullSensorRec_T)*SENSOR_NUMBERS;
  153. uint32_t selSize = sizeof(SELRepository_T) + sizeof(SELRec_T)*MAX_SEL_RECORD ;
  154. uint8_t* pSDR = NULL;
  155. uint8_t* pSEL = NULL;
  156. g_BMCInfo.pSDR = malloc(sdrSize);
  157. if((g_BMCInfo.pSDR == NULL) && (sdrSize != 0))
  158. {
  159. printf("g_BMCInfo.pSDR Malloc failed!\r\n");
  160. }
  161. g_BMCInfo.pSEL = malloc(selSize);
  162. if((g_BMCInfo.pSEL == NULL) && (selSize != 0))
  163. {
  164. printf("g_BMCInfo.pSEL Malloc failed!\r\n");
  165. }
  166. // FLASH_GetIPMI(&g_BMCInfo.IpmiConfig);
  167. // if(strncmp(g_BMCInfo.IpmiConfig.FirstPowerOnStr, FirstPowerOnStr, sizeof(FirstPowerOnStr)) != 0)
  168. if(1)
  169. {
  170. //first power on
  171. printf("BMC first power on!\r\n");
  172. /************************** Init IPMI ******************************/
  173. memcpy(g_BMCInfo.IpmiConfig.FirstPowerOnStr, FirstPowerOnStr, sizeof(FirstPowerOnStr));
  174. g_BMCInfo.IpmiConfig.SerialIfcSupport = SERIAL_IFC_SUPPORT;
  175. g_BMCInfo.IpmiConfig.SerialTerminalSupport = SERIAL_TERMINAL_SUPPORT;
  176. g_BMCInfo.IpmiConfig.LANIfcSupport = LAN_IFC_SUPPORT;
  177. g_BMCInfo.IpmiConfig.SYSIfcSupport = SYS_IFC_SUPPORT;
  178. g_BMCInfo.IpmiConfig.GrpExtnSupport = GROUP_EXTERN_SUPPORT;
  179. g_BMCInfo.IpmiConfig.UDSIfcSupport = UDS_IFC_SUPPORT;
  180. g_BMCInfo.IpmiConfig.ChassisTimerInterval = CHASSIS_TIMER_INTERVAL;
  181. g_BMCInfo.IpmiConfig.PowerCycleInterval = PWR_CYCLE_INTERVAL;
  182. g_BMCInfo.IpmiConfig.FanControlInterval = FAN_CONTROL_INTERVAL;
  183. g_BMCInfo.IpmiConfig.RearmSetSensorThreshold = REARM_SET_SENSOR_THRESHOLD;
  184. g_BMCInfo.IpmiConfig.SELTimeUTCOffset = 8*60;
  185. //IPMB
  186. g_BMCInfo.IpmiConfig.PrimaryIPMBSupport = PRIMARY_IPMB_SUPPORT;
  187. g_BMCInfo.IpmiConfig.SecondaryIPMBSupport = SECONDARY_IPMB_SUPPORT;
  188. g_BMCInfo.IpmiConfig.PrimaryIPMBBus = PRIMARY_IPMB_BUS;
  189. g_BMCInfo.IpmiConfig.SecondaryIPMBBus = SECONDARY_IPMB_BUS;
  190. g_BMCInfo.IpmiConfig.PrimaryIPMBAddr = PRIMARY_IPMB_ADDR;
  191. g_BMCInfo.IpmiConfig.SecondaryIPMBAddr = SECONDARY_IPMB_ADDR;
  192. //Init FRU
  193. memcpy(&g_BMCInfo.FRU, &Default_FRUData, sizeof(OemFRUData_T));
  194. /************************ Init SDR *************************************/
  195. pSDR = g_BMCInfo.pSDR;
  196. //init SDR repository header
  197. ((SDRRepository_T*)pSDR)->Signature[0] = 0x00;
  198. ((SDRRepository_T*)pSDR)->Signature[1] = 0x11;
  199. ((SDRRepository_T*)pSDR)->Signature[2] = 0x22;
  200. ((SDRRepository_T*)pSDR)->Signature[3] = 0x33;
  201. ((SDRRepository_T*)pSDR)->NumRecords = SENSOR_NUMBERS + 1;
  202. ((SDRRepository_T*)pSDR)->Size = sdrSize;
  203. ((SDRRepository_T*)pSDR)->AddTimeStamp = 0;
  204. ((SDRRepository_T*)pSDR)->EraseTimeStamp = 0;
  205. //init MgmtCtrlrDevLocator SDR
  206. pSDR += sizeof(SDRRepository_T);
  207. ((HdrMgmtCtrlrDevLocator_T*)pSDR)->Valid = 1;
  208. ((HdrMgmtCtrlrDevLocator_T*)pSDR)->Len = sizeof(HdrMgmtCtrlrDevLocator_T);
  209. memcpy(&(((HdrMgmtCtrlrDevLocator_T*)pSDR)->MgmtCtrlrDevLocator), &bmc_sdr, sizeof(MgmtCtrlrDevLocator_T));
  210. //init FullSensorRec SDR
  211. pSDR += sizeof(HdrMgmtCtrlrDevLocator_T);
  212. for(i=0;i<SENSOR_NUMBERS;i++)
  213. {
  214. ((HdrFullSensorRec_T*)pSDR)->Valid = 1;
  215. ((HdrFullSensorRec_T*)pSDR)->Len = sizeof(HdrFullSensorRec_T);
  216. memcpy(&(((HdrFullSensorRec_T*)pSDR)->FullSensorRec), &full_sdr_tbl[i], sizeof(FullSensorRec_T));
  217. pSDR += sizeof(HdrFullSensorRec_T);
  218. }
  219. /******************************* Init SEL *************************************/
  220. pSEL = g_BMCInfo.pSEL;
  221. ((SELRepository_T*)pSEL)->Signature[0] = 0x00;
  222. ((SELRepository_T*)pSEL)->Signature[1] = 0x11;
  223. ((SELRepository_T*)pSEL)->Signature[2] = 0x22;
  224. ((SELRepository_T*)pSEL)->Signature[3] = 0x33;
  225. ((SELRepository_T*)pSEL)->NumRecords = 0;
  226. ((SELRepository_T*)pSEL)->Padding = 0;
  227. ((SELRepository_T*)pSEL)->AddTimeStamp = 0;
  228. ((SELRepository_T*)pSEL)->EraseTimeStamp = 0;
  229. ((SELRepository_T*)pSEL)->FirstRecID = 0;
  230. ((SELRepository_T*)pSEL)->LastRecID = 0;
  231. ((SELRepository_T*)pSEL)->SELIndex = 0;
  232. ((SELRepository_T*)pSEL)->SELRecord = (SELRec_T*)(pSEL + sizeof(SELRepository_T));
  233. UpdateFlash();
  234. }
  235. else
  236. {
  237. // //Init FRU
  238. // FLASH_GetFRU(&g_BMCInfo.FRU);
  239. // //Init SDR
  240. // FLASH_GetSDR(g_BMCInfo.pSDR, sdrSize);
  241. // //Init SEL
  242. // FLASH_GetSEL(g_BMCInfo.pSEL, selSize);
  243. }
  244. return 0;
  245. }
  246. int Init_UserInfoTbl(void)
  247. {
  248. int i;
  249. if(1) //first power on
  250. {
  251. memset(g_BMCInfo.UserInfoTbl, 0x0, sizeof(UserInfo_T)*MAX_USER_NUM);
  252. g_BMCInfo.UserInfoTbl[0].UserId = 1;
  253. strcpy(g_BMCInfo.UserInfoTbl[0].UserName, "admin");
  254. strcpy(g_BMCInfo.UserInfoTbl[0].UserPassword, "admin");
  255. g_BMCInfo.UserInfoTbl[0].UserStatus = TRUE;
  256. g_BMCInfo.CurrentNoUser = 1;
  257. g_BMCInfo.pUserInfo = NULL;
  258. FlushUserInfoTbl();
  259. }
  260. else
  261. {
  262. UpdateUserInfoTble();
  263. g_BMCInfo.CurrentNoUser = 0;
  264. for(i=0;i<MAX_USER_NUM;i++)
  265. {
  266. if((g_BMCInfo.UserInfoTbl[i].UserId != 0) && (g_BMCInfo.UserInfoTbl[i].UserStatus == TRUE))
  267. g_BMCInfo.CurrentNoUser++;
  268. }
  269. g_BMCInfo.pUserInfo = NULL;
  270. }
  271. }
  272. int UpdateFlash(void)
  273. {
  274. //TODO:
  275. return 0;
  276. }
  277. int SetSysTime(uint32_t *timeSecond)
  278. {
  279. g_BMCInfo.SELTimeSecond = *timeSecond;
  280. return 0;
  281. }
  282. int GetSysTime(void)
  283. {
  284. return g_BMCInfo.SELTimeSecond;
  285. }
  286. int PostEventMessage (uint8_t *EventMsg,uint8_t size)
  287. {
  288. uint8_t SelReq [sizeof(SELEventRecord_T)];
  289. uint8_t SelRes [sizeof(AddSELRes_T)];
  290. SELEventRecord_T* SelRecord = ( SELEventRecord_T*) SelReq;
  291. SelRecord->hdr.Type = 0x02;
  292. SelRecord->hdr.TimeStamp = GetSysTime ();
  293. memcpy (SelRecord->GenID, EventMsg, size);
  294. LockedAddSELEntry(SelReq, sizeof (SELEventRecord_T), SelRes);
  295. return 0;
  296. }
  297. uint8_t PDK_GetSlotID(void)
  298. {
  299. uint8_t SlotID = 0;
  300. uint8_t check = 0;
  301. if(stm32_gpio_read(GA0_PORT, GA0_PIN) == GPIO_PIN_RESET)
  302. SlotID |= 0x01;
  303. if(stm32_gpio_read(GA1_PORT, GA1_PIN) == GPIO_PIN_RESET)
  304. SlotID |= 0x02;
  305. if(stm32_gpio_read(GA2_PORT, GA2_PIN) == GPIO_PIN_RESET)
  306. SlotID |= 0x04;
  307. if(stm32_gpio_read(GA3_PORT, GA3_PIN) == GPIO_PIN_RESET)
  308. SlotID |= 0x08;
  309. if(stm32_gpio_read(GA4_PORT, GA4_PIN) == GPIO_PIN_RESET)
  310. SlotID |= 0x10;
  311. if(stm32_gpio_read(GAP_PORT, GAP_PIN) == GPIO_PIN_RESET)
  312. SlotID |= 0x20;
  313. int i;
  314. for(i=0;i<6;i++)
  315. check ^= (SlotID>>i)&0x01;
  316. if(check == 0)
  317. printf("Slot ID check error! GAP = %#x, GA[4:0] = %#x.\n", (SlotID>>5), (SlotID&0x1f));
  318. return SlotID&0x1f;
  319. }
  320. uint8_t PDK_GetChassisID(void)
  321. {
  322. uint8_t ChassisID = 0;
  323. if(stm32_gpio_read(RACKID0_PORT, RACKID0_PIN) == GPIO_PIN_SET)
  324. ChassisID |= 0x01;
  325. if(stm32_gpio_read(RACKID1_PORT, RACKID1_PIN) == GPIO_PIN_SET)
  326. ChassisID |= 0x02;
  327. if(stm32_gpio_read(RACKID2_PORT, RACKID2_PIN) == GPIO_PIN_SET)
  328. ChassisID |= 0x04;
  329. if(stm32_gpio_read(RACKID3_PORT, RACKID3_PIN) == GPIO_PIN_SET)
  330. ChassisID |= 0x08;
  331. if(stm32_gpio_read(RACKID4_PORT, RACKID4_PIN) == GPIO_PIN_SET)
  332. ChassisID |= 0x10;
  333. if(stm32_gpio_read(RACKID5_PORT, RACKID5_PIN) == GPIO_PIN_SET)
  334. ChassisID |= 0x20;
  335. return ChassisID;
  336. }
  337. int PDK_PowerOffChassis(void)
  338. {
  339. printf("Api power off chassis\n");
  340. g_BMCInfo.PowerGoodFlag = 0;
  341. }
  342. int PDK_PowerOnChassis(void)
  343. {
  344. printf("power on chassis\n");
  345. g_BMCInfo.PowerGoodFlag = 1;
  346. }
  347. int PDK_SoftOffChassis(void)
  348. {
  349. printf("soft off chassis\n");;
  350. }
  351. int PDK_PowerCycleChassis(void)
  352. {
  353. printf("power cycle chassis\n");;
  354. }
  355. int PDK_ResetChassis(void)
  356. {
  357. printf("power reset chassis\n");;
  358. }
  359. int PDK_DiagInterruptChassis(void)
  360. {
  361. printf("power diag chassis\n");;
  362. }
  363. int PDK_FanControl(void)
  364. {
  365. ;
  366. }
  367. //设置IP地址
  368. /*
  369. * 函数名称 : int setip(char *ip)
  370. * 函数功能 : 设置系统IP地址
  371. * 参 数 :
  372. *char *ip :设置的IP地址,以点分十进制的字符串方式表示,如“192.168.0.5”
  373. * 返 回 值 : 0 : 成功 ; -1 : 失败
  374. */
  375. int setip(char *ip)
  376. {
  377. struct ifreq temp;
  378. struct sockaddr_in *addr;
  379. int fd = 0;
  380. int ret = -1;
  381. strcpy(temp.ifr_name, "eth0");
  382. if((fd=socket(AF_INET, SOCK_STREAM, 0))<0)
  383. {
  384. return -1;
  385. }
  386. addr = (struct sockaddr_in *)&(temp.ifr_addr);
  387. addr->sin_family = AF_INET;
  388. addr->sin_addr.s_addr = inet_addr(ip);
  389. ret = ioctl(fd, SIOCSIFADDR, &temp);
  390. close(fd);
  391. if(ret < 0)
  392. return -1;
  393. return 0;
  394. }
  395. //获取IP地址
  396. /*
  397. * 函数名称 : char * getip(char *ip_buf)
  398. * 函数功能 : 获取系統IP地址
  399. * 参 数 :
  400. *char *ip_buf :用来存放IP地址的内存空间
  401. * 返 回 值 : ip_buf : 存放IP地址的内存地址
  402. */
  403. char* getip(char *ip_buf)
  404. {
  405. struct ifreq temp;
  406. struct sockaddr_in *myaddr;
  407. int fd = 0;
  408. int ret = -1;
  409. strcpy(temp.ifr_name, "eth0");
  410. if((fd=socket(AF_INET, SOCK_STREAM, 0))<0)
  411. {
  412. return NULL;
  413. }
  414. ret = ioctl(fd, SIOCGIFADDR, &temp);
  415. close(fd);
  416. if(ret < 0)
  417. return NULL;
  418. myaddr = (struct sockaddr_in *)&(temp.ifr_addr);
  419. strcpy(ip_buf, (char*)inet_ntoa(myaddr->sin_addr));
  420. return ip_buf;
  421. }
  422. char* getmac(char *mac_buf)
  423. {
  424. struct ifreq temp;
  425. struct sockaddr_in *myaddr;
  426. int fd = 0;
  427. int ret = -1;
  428. strcpy(temp.ifr_name, "eth0");
  429. if((fd=socket(AF_INET, SOCK_STREAM, 0))<0)
  430. {
  431. return NULL;
  432. }
  433. if(ioctl(fd,SIOCGIFHWADDR,&temp)<0)
  434. {
  435. printf("Get mac address ioctl fail!\n");
  436. }
  437. else
  438. {
  439. sprintf(mac_buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
  440. (unsigned char)temp.ifr_hwaddr.sa_data[0],
  441. (unsigned char)temp.ifr_hwaddr.sa_data[1],
  442. (unsigned char)temp.ifr_hwaddr.sa_data[2],
  443. (unsigned char)temp.ifr_hwaddr.sa_data[3],
  444. (unsigned char)temp.ifr_hwaddr.sa_data[4],
  445. (unsigned char)temp.ifr_hwaddr.sa_data[5]);
  446. }
  447. close(fd);
  448. return mac_buf;
  449. }
  450. int FlushUserInfoTbl(void)
  451. {
  452. sf_sector_erase(5, USERTBL_FLASH_ADDR);
  453. sf_write(5, USERTBL_FLASH_ADDR, (uint8_t*)g_BMCInfo.UserInfoTbl, sizeof(UserInfo_T)*MAX_USER_NUM);
  454. return 0;
  455. }
  456. int UpdateUserInfoTble(void)
  457. {
  458. sf_read(5, USERTBL_FLASH_ADDR, (uint8_t*)g_BMCInfo.UserInfoTbl, sizeof(UserInfo_T)*MAX_USER_NUM);
  459. return 0;
  460. }