1 /*
2  * Copyright (C) 2003-2015 FreeIPMI Core Team
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18 
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif /* HAVE_CONFIG_H */
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #ifdef STDC_HEADERS
26 #include <string.h>
27 #endif /* STDC_HEADERS */
28 #include <errno.h>
29 
30 #include "freeipmi/api/ipmi-sdr-repository-cmds-api.h"
31 #include "freeipmi/cmds/ipmi-sdr-repository-cmds.h"
32 #include "freeipmi/fiid/fiid.h"
33 #include "freeipmi/spec/ipmi-ipmb-lun-spec.h"
34 #include "freeipmi/spec/ipmi-netfn-spec.h"
35 
36 #include "ipmi-api-defs.h"
37 #include "ipmi-api-trace.h"
38 #include "ipmi-api-util.h"
39 
40 #include "libcommon/ipmi-fiid-util.h"
41 
42 #include "freeipmi-portability.h"
43 
44 int
ipmi_cmd_get_sdr_repository_info(ipmi_ctx_t ctx,fiid_obj_t obj_cmd_rs)45 ipmi_cmd_get_sdr_repository_info (ipmi_ctx_t ctx,
46                                   fiid_obj_t obj_cmd_rs)
47 {
48   fiid_obj_t obj_cmd_rq = NULL;
49   int rv = -1;
50 
51   if (!ctx || ctx->magic != IPMI_CTX_MAGIC)
52     {
53       ERR_TRACE (ipmi_ctx_errormsg (ctx), ipmi_ctx_errnum (ctx));
54       return (-1);
55     }
56 
57   if (!fiid_obj_valid (obj_cmd_rs))
58     {
59       API_SET_ERRNUM (ctx, IPMI_ERR_PARAMETERS);
60       return (-1);
61     }
62 
63   if (FIID_OBJ_TEMPLATE_COMPARE (obj_cmd_rs,
64                                  tmpl_cmd_get_sdr_repository_info_rs) < 0)
65     {
66       API_FIID_OBJECT_ERROR_TO_API_ERRNUM (ctx, obj_cmd_rs);
67       return (-1);
68     }
69 
70   if (!(obj_cmd_rq = fiid_obj_create (tmpl_cmd_get_sdr_repository_info_rq)))
71     {
72       API_ERRNO_TO_API_ERRNUM (ctx, errno);
73       goto cleanup;
74     }
75 
76   if (fill_cmd_get_repository_info (obj_cmd_rq) < 0)
77     {
78       API_ERRNO_TO_API_ERRNUM (ctx, errno);
79       goto cleanup;
80     }
81 
82   if (api_ipmi_cmd (ctx,
83                     IPMI_BMC_IPMB_LUN_BMC,
84                     IPMI_NET_FN_STORAGE_RQ,
85                     obj_cmd_rq,
86                     obj_cmd_rs) < 0)
87     {
88       ERR_TRACE (ipmi_ctx_errormsg (ctx), ipmi_ctx_errnum (ctx));
89       goto cleanup;
90     }
91 
92   rv = 0;
93  cleanup:
94   fiid_obj_destroy (obj_cmd_rq);
95   return (rv);
96 }
97 
98 int
ipmi_cmd_get_sdr_repository_allocation_info(ipmi_ctx_t ctx,fiid_obj_t obj_cmd_rs)99 ipmi_cmd_get_sdr_repository_allocation_info (ipmi_ctx_t ctx,
100                                              fiid_obj_t obj_cmd_rs)
101 {
102   fiid_obj_t obj_cmd_rq = NULL;
103   int rv = -1;
104 
105   if (!ctx || ctx->magic != IPMI_CTX_MAGIC)
106     {
107       ERR_TRACE (ipmi_ctx_errormsg (ctx), ipmi_ctx_errnum (ctx));
108       return (-1);
109     }
110 
111   if (!fiid_obj_valid (obj_cmd_rs))
112     {
113       API_SET_ERRNUM (ctx, IPMI_ERR_PARAMETERS);
114       return (-1);
115     }
116 
117   if (FIID_OBJ_TEMPLATE_COMPARE (obj_cmd_rs,
118                                  tmpl_cmd_get_sdr_repository_allocation_info_rs) < 0)
119     {
120       API_FIID_OBJECT_ERROR_TO_API_ERRNUM (ctx, obj_cmd_rs);
121       return (-1);
122     }
123 
124   if (!(obj_cmd_rq = fiid_obj_create (tmpl_cmd_get_sdr_repository_allocation_info_rq)))
125     {
126       API_ERRNO_TO_API_ERRNUM (ctx, errno);
127       goto cleanup;
128     }
129 
130   if (fill_cmd_get_repository_allocation_info (obj_cmd_rq) < 0)
131     {
132       API_ERRNO_TO_API_ERRNUM (ctx, errno);
133       goto cleanup;
134     }
135 
136   if (api_ipmi_cmd (ctx,
137                     IPMI_BMC_IPMB_LUN_BMC,
138                     IPMI_NET_FN_STORAGE_RQ,
139                     obj_cmd_rq,
140                     obj_cmd_rs) < 0)
141     {
142       ERR_TRACE (ipmi_ctx_errormsg (ctx), ipmi_ctx_errnum (ctx));
143       goto cleanup;
144     }
145 
146   rv = 0;
147  cleanup:
148   fiid_obj_destroy (obj_cmd_rq);
149   return (rv);
150 }
151 
152 int
ipmi_cmd_reserve_sdr_repository(ipmi_ctx_t ctx,fiid_obj_t obj_cmd_rs)153 ipmi_cmd_reserve_sdr_repository (ipmi_ctx_t ctx,
154                                  fiid_obj_t obj_cmd_rs)
155 {
156   fiid_obj_t obj_cmd_rq = NULL;
157   int rv = -1;
158 
159   if (!ctx || ctx->magic != IPMI_CTX_MAGIC)
160     {
161       ERR_TRACE (ipmi_ctx_errormsg (ctx), ipmi_ctx_errnum (ctx));
162       return (-1);
163     }
164 
165   if (!fiid_obj_valid (obj_cmd_rs))
166     {
167       API_SET_ERRNUM (ctx, IPMI_ERR_PARAMETERS);
168       return (-1);
169     }
170 
171   if (FIID_OBJ_TEMPLATE_COMPARE (obj_cmd_rs,
172                                  tmpl_cmd_reserve_sdr_repository_rs) < 0)
173     {
174       API_FIID_OBJECT_ERROR_TO_API_ERRNUM (ctx, obj_cmd_rs);
175       return (-1);
176     }
177 
178   if (!(obj_cmd_rq = fiid_obj_create (tmpl_cmd_reserve_sdr_repository_rq)))
179     {
180       API_ERRNO_TO_API_ERRNUM (ctx, errno);
181       goto cleanup;
182     }
183 
184   if (fill_cmd_reserve_sdr_repository (obj_cmd_rq) < 0)
185     {
186       API_ERRNO_TO_API_ERRNUM (ctx, errno);
187       goto cleanup;
188     }
189 
190   if (api_ipmi_cmd (ctx,
191                     IPMI_BMC_IPMB_LUN_BMC,
192                     IPMI_NET_FN_STORAGE_RQ,
193                     obj_cmd_rq,
194                     obj_cmd_rs) < 0)
195     {
196       ERR_TRACE (ipmi_ctx_errormsg (ctx), ipmi_ctx_errnum (ctx));
197       goto cleanup;
198     }
199 
200   rv = 0;
201  cleanup:
202   fiid_obj_destroy (obj_cmd_rq);
203   return (rv);
204 }
205 
206 int
ipmi_cmd_get_sdr(ipmi_ctx_t ctx,uint16_t reservation_id,uint16_t record_id,uint8_t offset_into_record,uint8_t bytes_to_read,fiid_obj_t obj_cmd_rs)207 ipmi_cmd_get_sdr (ipmi_ctx_t ctx,
208                   uint16_t reservation_id,
209                   uint16_t record_id,
210                   uint8_t offset_into_record,
211                   uint8_t bytes_to_read,
212                   fiid_obj_t obj_cmd_rs)
213 {
214   fiid_obj_t obj_cmd_rq = NULL;
215   int rv = -1;
216 
217   if (!ctx || ctx->magic != IPMI_CTX_MAGIC)
218     {
219       ERR_TRACE (ipmi_ctx_errormsg (ctx), ipmi_ctx_errnum (ctx));
220       return (-1);
221     }
222 
223   if (!fiid_obj_valid (obj_cmd_rs))
224     {
225       API_SET_ERRNUM (ctx, IPMI_ERR_PARAMETERS);
226       return (-1);
227     }
228 
229   if (FIID_OBJ_TEMPLATE_COMPARE (obj_cmd_rs,
230                                  tmpl_cmd_get_sdr_rs) < 0)
231     {
232       API_FIID_OBJECT_ERROR_TO_API_ERRNUM (ctx, obj_cmd_rs);
233       return (-1);
234     }
235 
236   if (!(obj_cmd_rq = fiid_obj_create (tmpl_cmd_get_sdr_rq)))
237     {
238       API_ERRNO_TO_API_ERRNUM (ctx, errno);
239       goto cleanup;
240     }
241 
242   if (fill_cmd_get_sdr (reservation_id,
243                         record_id,
244                         offset_into_record,
245                         bytes_to_read,
246                         obj_cmd_rq) < 0)
247     {
248       API_ERRNO_TO_API_ERRNUM (ctx, errno);
249       goto cleanup;
250     }
251 
252   if (api_ipmi_cmd (ctx,
253                     IPMI_BMC_IPMB_LUN_BMC,
254                     IPMI_NET_FN_STORAGE_RQ,
255                     obj_cmd_rq,
256                     obj_cmd_rs) < 0)
257     {
258       ERR_TRACE (ipmi_ctx_errormsg (ctx), ipmi_ctx_errnum (ctx));
259       goto cleanup;
260     }
261 
262   rv = 0;
263  cleanup:
264   fiid_obj_destroy (obj_cmd_rq);
265   return (rv);
266 }
267 
268 int
ipmi_cmd_get_sdr_repository_time(ipmi_ctx_t ctx,fiid_obj_t obj_cmd_rs)269 ipmi_cmd_get_sdr_repository_time (ipmi_ctx_t ctx,
270                                   fiid_obj_t obj_cmd_rs)
271 {
272   fiid_obj_t obj_cmd_rq = NULL;
273   int rv = -1;
274 
275   if (!ctx || ctx->magic != IPMI_CTX_MAGIC)
276     {
277       ERR_TRACE (ipmi_ctx_errormsg (ctx), ipmi_ctx_errnum (ctx));
278       return (-1);
279     }
280 
281   if (!fiid_obj_valid (obj_cmd_rs))
282     {
283       API_SET_ERRNUM (ctx, IPMI_ERR_PARAMETERS);
284       return (-1);
285     }
286 
287   if (FIID_OBJ_TEMPLATE_COMPARE (obj_cmd_rs,
288                                  tmpl_cmd_get_sdr_repository_time_rs) < 0)
289     {
290       API_FIID_OBJECT_ERROR_TO_API_ERRNUM (ctx, obj_cmd_rs);
291       return (-1);
292     }
293 
294   if (!(obj_cmd_rq = fiid_obj_create (tmpl_cmd_get_sdr_repository_time_rq)))
295     {
296       API_ERRNO_TO_API_ERRNUM (ctx, errno);
297       goto cleanup;
298     }
299 
300   if (fill_cmd_get_sdr_repository_time (obj_cmd_rq) < 0)
301     {
302       API_ERRNO_TO_API_ERRNUM (ctx, errno);
303       goto cleanup;
304     }
305 
306   if (api_ipmi_cmd (ctx,
307                     IPMI_BMC_IPMB_LUN_BMC,
308                     IPMI_NET_FN_STORAGE_RQ,
309                     obj_cmd_rq,
310                     obj_cmd_rs) < 0)
311     {
312       ERR_TRACE (ipmi_ctx_errormsg (ctx), ipmi_ctx_errnum (ctx));
313       goto cleanup;
314     }
315 
316   rv = 0;
317  cleanup:
318   fiid_obj_destroy (obj_cmd_rq);
319   return (rv);
320 }
321 
322 int
ipmi_cmd_set_sdr_repository_time(ipmi_ctx_t ctx,uint32_t time,fiid_obj_t obj_cmd_rs)323 ipmi_cmd_set_sdr_repository_time (ipmi_ctx_t ctx,
324                                   uint32_t time,
325                                   fiid_obj_t obj_cmd_rs)
326 {
327   fiid_obj_t obj_cmd_rq = NULL;
328   int rv = -1;
329 
330   if (!ctx || ctx->magic != IPMI_CTX_MAGIC)
331     {
332       ERR_TRACE (ipmi_ctx_errormsg (ctx), ipmi_ctx_errnum (ctx));
333       return (-1);
334     }
335 
336   if (!fiid_obj_valid (obj_cmd_rs))
337     {
338       API_SET_ERRNUM (ctx, IPMI_ERR_PARAMETERS);
339       return (-1);
340     }
341 
342   if (FIID_OBJ_TEMPLATE_COMPARE (obj_cmd_rs,
343                                  tmpl_cmd_set_sdr_repository_time_rs) < 0)
344     {
345       API_FIID_OBJECT_ERROR_TO_API_ERRNUM (ctx, obj_cmd_rs);
346       return (-1);
347     }
348 
349   if (!(obj_cmd_rq = fiid_obj_create (tmpl_cmd_set_sdr_repository_time_rq)))
350     {
351       API_ERRNO_TO_API_ERRNUM (ctx, errno);
352       goto cleanup;
353     }
354 
355   if (fill_cmd_set_sdr_repository_time (time, obj_cmd_rq) < 0)
356     {
357       API_ERRNO_TO_API_ERRNUM (ctx, errno);
358       goto cleanup;
359     }
360 
361   if (api_ipmi_cmd (ctx,
362                     IPMI_BMC_IPMB_LUN_BMC,
363                     IPMI_NET_FN_STORAGE_RQ,
364                     obj_cmd_rq,
365                     obj_cmd_rs) < 0)
366     {
367       ERR_TRACE (ipmi_ctx_errormsg (ctx), ipmi_ctx_errnum (ctx));
368       goto cleanup;
369     }
370 
371   rv = 0;
372  cleanup:
373   fiid_obj_destroy (obj_cmd_rq);
374   return (rv);
375 }
376