1 /*****************************************************************************\
2  *  $Id: ipmi-fru-inventory-device-cmds.c,v 1.22 2010-07-28 21:19:56 chu11 Exp $
3  *****************************************************************************
4  *  Copyright (C) 2007-2015 Lawrence Livermore National Security, LLC.
5  *  Copyright (C) 2007 The Regents of the University of California.
6  *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
7  *  Written by Albert Chu <chu11@llnl.gov>
8  *  UCRL-CODE-232183
9  *
10  *  This file is part of Ipmi-fru, a tool used for retrieving
11  *  motherboard field replaceable unit (FRU) information. For details,
12  *  see http://www.llnl.gov/linux/.
13  *
14  *  Ipmi-fru is free software; you can redistribute it and/or modify
15  *  it under the terms of the GNU General Public License as published by the
16  *  Free Software Foundation; either version 3 of the License, or (at your
17  *  option) any later version.
18  *
19  *  Ipmi-fru is distributed in the hope that it will be useful, but
20  *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21  *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
22  *  for more details.
23  *
24  *  You should have received a copy of the GNU General Public License along
25  *  with Ipmi-fru.  If not, see <http://www.gnu.org/licenses/>.
26 \*****************************************************************************/
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif /* HAVE_CONFIG_H */
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <errno.h>
35 
36 #include "freeipmi/cmds/ipmi-fru-inventory-device-cmds.h"
37 #include "freeipmi/fiid/fiid.h"
38 #include "freeipmi/spec/ipmi-cmd-spec.h"
39 
40 #include "libcommon/ipmi-fiid-util.h"
41 #include "libcommon/ipmi-fill-util.h"
42 #include "libcommon/ipmi-trace.h"
43 
44 #include "freeipmi-portability.h"
45 
46 fiid_template_t tmpl_cmd_get_fru_inventory_area_info_rq =
47   {
48     { 8, "cmd", FIID_FIELD_REQUIRED | FIID_FIELD_LENGTH_FIXED},
49     { 8, "fru_device_id", FIID_FIELD_REQUIRED | FIID_FIELD_LENGTH_FIXED},
50     { 0, "", 0}
51   };
52 
53 fiid_template_t tmpl_cmd_get_fru_inventory_area_info_rs =
54   {
55     { 8, "cmd", FIID_FIELD_REQUIRED | FIID_FIELD_LENGTH_FIXED | FIID_FIELD_MAKES_PACKET_SUFFICIENT},
56     { 8, "comp_code", FIID_FIELD_REQUIRED | FIID_FIELD_LENGTH_FIXED | FIID_FIELD_MAKES_PACKET_SUFFICIENT},
57     { 16, "fru_inventory_area_size", FIID_FIELD_REQUIRED | FIID_FIELD_LENGTH_FIXED},
58     { 1, "device_is_accessed", FIID_FIELD_REQUIRED | FIID_FIELD_LENGTH_FIXED},
59     { 7, "reserved", FIID_FIELD_REQUIRED | FIID_FIELD_LENGTH_FIXED},
60     { 0, "", 0}
61   };
62 
63 fiid_template_t tmpl_cmd_read_fru_data_rq =
64   {
65     { 8, "cmd", FIID_FIELD_REQUIRED | FIID_FIELD_LENGTH_FIXED},
66     { 8, "fru_device_id", FIID_FIELD_REQUIRED | FIID_FIELD_LENGTH_FIXED},
67     { 16, "fru_inventory_offset_to_read", FIID_FIELD_REQUIRED | FIID_FIELD_LENGTH_FIXED},
68     { 8, "count_to_read", FIID_FIELD_REQUIRED | FIID_FIELD_LENGTH_FIXED},
69     { 0, "", 0}
70   };
71 
72 /* 2040 = 255 * 8, 255 b/c count_returned field in request is 1 byte long */
73 fiid_template_t tmpl_cmd_read_fru_data_rs =
74   {
75     { 8, "cmd", FIID_FIELD_REQUIRED | FIID_FIELD_LENGTH_FIXED | FIID_FIELD_MAKES_PACKET_SUFFICIENT},
76     { 8, "comp_code", FIID_FIELD_REQUIRED | FIID_FIELD_LENGTH_FIXED | FIID_FIELD_MAKES_PACKET_SUFFICIENT},
77     { 8, "count_returned", FIID_FIELD_REQUIRED | FIID_FIELD_LENGTH_FIXED},
78     { 2040, "requested_data", FIID_FIELD_OPTIONAL | FIID_FIELD_LENGTH_VARIABLE},
79     { 0, "", 0}
80   };
81 
82 /* 2040 = 255 * 8, 255 b/c bytes_to_write field in request is 1 byte long */
83 fiid_template_t tmpl_cmd_write_fru_data_rq =
84   {
85     { 8, "cmd", FIID_FIELD_REQUIRED | FIID_FIELD_LENGTH_FIXED},
86     { 8, "fru_device_id", FIID_FIELD_REQUIRED | FIID_FIELD_LENGTH_FIXED},
87     { 16, "fru_inventory_offset_to_write", FIID_FIELD_REQUIRED | FIID_FIELD_LENGTH_FIXED},
88     { 2040, "data_to_write", FIID_FIELD_REQUIRED | FIID_FIELD_LENGTH_VARIABLE},
89     { 0, "", 0}
90   };
91 
92 fiid_template_t tmpl_cmd_write_fru_data_rs =
93   {
94     { 8, "cmd", FIID_FIELD_REQUIRED | FIID_FIELD_LENGTH_FIXED | FIID_FIELD_MAKES_PACKET_SUFFICIENT},
95     { 8, "comp_code", FIID_FIELD_REQUIRED | FIID_FIELD_LENGTH_FIXED  | FIID_FIELD_MAKES_PACKET_SUFFICIENT},
96     { 8, "count_written", FIID_FIELD_REQUIRED | FIID_FIELD_LENGTH_FIXED},
97     { 0, "", 0}
98   };
99 
100 int
fill_cmd_get_fru_inventory_area_info(uint8_t fru_device_id,fiid_obj_t obj_cmd_rq)101 fill_cmd_get_fru_inventory_area_info (uint8_t fru_device_id,
102                                       fiid_obj_t obj_cmd_rq)
103 {
104   if (fru_device_id == IPMI_FRU_DEVICE_ID_RESERVED
105       || !fiid_obj_valid (obj_cmd_rq))
106     {
107       SET_ERRNO (EINVAL);
108       return (-1);
109     }
110 
111   if (FIID_OBJ_TEMPLATE_COMPARE (obj_cmd_rq, tmpl_cmd_get_fru_inventory_area_info_rq) < 0)
112     {
113       ERRNO_TRACE (errno);
114       return (-1);
115     }
116 
117   FILL_FIID_OBJ_CLEAR (obj_cmd_rq);
118   FILL_FIID_OBJ_SET (obj_cmd_rq, "cmd", IPMI_CMD_GET_FRU_INVENTORY_AREA_INFO);
119   FILL_FIID_OBJ_SET (obj_cmd_rq, "fru_device_id", fru_device_id);
120   return (0);
121 }
122 
123 int
fill_cmd_read_fru_data(uint8_t fru_device_id,uint16_t fru_inventory_offset_to_read,uint8_t count_to_read,fiid_obj_t obj_cmd_rq)124 fill_cmd_read_fru_data (uint8_t fru_device_id,
125                         uint16_t fru_inventory_offset_to_read,
126                         uint8_t count_to_read,
127                         fiid_obj_t obj_cmd_rq)
128 {
129   if (!fiid_obj_valid (obj_cmd_rq))
130     {
131       SET_ERRNO (EINVAL);
132       return (-1);
133     }
134 
135   if (FIID_OBJ_TEMPLATE_COMPARE (obj_cmd_rq, tmpl_cmd_read_fru_data_rq) < 0)
136     {
137       ERRNO_TRACE (errno);
138       return (-1);
139     }
140 
141   FILL_FIID_OBJ_CLEAR (obj_cmd_rq);
142   FILL_FIID_OBJ_SET (obj_cmd_rq, "cmd", IPMI_CMD_READ_FRU_DATA);
143   FILL_FIID_OBJ_SET (obj_cmd_rq, "fru_device_id", fru_device_id);
144   FILL_FIID_OBJ_SET (obj_cmd_rq, "fru_inventory_offset_to_read", fru_inventory_offset_to_read);
145   FILL_FIID_OBJ_SET (obj_cmd_rq, "count_to_read", count_to_read);
146   return (0);
147 }
148 
149 int
fill_cmd_write_fru_data(uint8_t fru_device_id,uint16_t fru_inventory_offset_to_write,const void * data_to_write,unsigned int data_to_write_len,fiid_obj_t obj_cmd_rq)150 fill_cmd_write_fru_data (uint8_t fru_device_id,
151                          uint16_t fru_inventory_offset_to_write,
152                          const void *data_to_write,
153                          unsigned int data_to_write_len,
154                          fiid_obj_t obj_cmd_rq)
155 {
156   if ((data_to_write
157        && data_to_write_len > IPMI_FRU_DATA_MAX)
158       || !fiid_obj_valid (obj_cmd_rq))
159     {
160       SET_ERRNO (EINVAL);
161       return (-1);
162     }
163 
164   if (FIID_OBJ_TEMPLATE_COMPARE (obj_cmd_rq, tmpl_cmd_write_fru_data_rq) < 0)
165     {
166       ERRNO_TRACE (errno);
167       return (-1);
168     }
169 
170   FILL_FIID_OBJ_CLEAR (obj_cmd_rq);
171   FILL_FIID_OBJ_SET (obj_cmd_rq, "cmd", IPMI_CMD_WRITE_FRU_DATA);
172   FILL_FIID_OBJ_SET (obj_cmd_rq, "fru_device_id", fru_device_id);
173   FILL_FIID_OBJ_SET (obj_cmd_rq, "fru_inventory_offset_to_write", fru_inventory_offset_to_write);
174   if (data_to_write && data_to_write_len)
175     FILL_FIID_OBJ_SET_DATA (obj_cmd_rq, "data_to_write", data_to_write, data_to_write_len);
176   return (0);
177 }
178