1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdarg.h>
#include <strings.h>
#include <limits.h>
#include <syslog.h>
#include <sys/open.h>
#include <string.h>
#include <alloca.h>
#include <libintl.h>
#include <sys/stat.h>
#include <sys/systeminfo.h>
#include <picl.h>
#include <picltree.h>
#include <fru_access.h>
#include <sys/sgfrutree.h>
/*
* these functions will overlay the symbol table of libfruaccess
* at runtime
*/
container_hdl_t fru_open_container(picl_nodehdl_t fru);
int fru_close_container(container_hdl_t fru);
int fru_get_num_sections(container_hdl_t container,
door_cred_t *cred);
int fru_get_sections(container_hdl_t container, section_t *section,
int max_sections, door_cred_t *cred);
int fru_get_num_segments(section_hdl_t section, door_cred_t *cred);
int fru_get_segments(section_hdl_t section, segment_t *segment,
int max_segments, door_cred_t *cred);
int fru_add_segment(section_hdl_t section, segment_t *segment,
section_hdl_t *newsection, door_cred_t *cred);
int fru_delete_segment(segment_hdl_t segment,
section_hdl_t *newsection, door_cred_t *cred);
ssize_t fru_read_segment(segment_hdl_t segment, void *buffer,
size_t nbytes, door_cred_t *cred);
ssize_t fru_write_segment(segment_hdl_t segment, const void *data,
size_t nbytes, segment_hdl_t *newsegment,
door_cred_t *cred);
int fru_get_num_packets(segment_hdl_t segment, door_cred_t *cred);
int fru_get_packets(segment_hdl_t segment, packet_t *packet,
int max_packets, door_cred_t *cred);
int fru_update_payload(packet_hdl_t packet, const void *data,
size_t nbytes, packet_hdl_t *newpacket, door_cred_t *cred);
int fru_append_packet(segment_hdl_t segment, packet_t *packet,
const void *payload, size_t nbytes,
segment_hdl_t *newsegment, door_cred_t *cred);
int fru_delete_packet(packet_hdl_t packet,
segment_hdl_t *newsegment, door_cred_t *cred);
int fru_is_data_available(picl_nodehdl_t fru);
#define PICL_PROP_SC_HANDLE "SC_handle"
#define PICL_PROP_DATA_AVAIL "FRUDataAvailable"
#define MAX_LINE_SIZE 1024
#define OPENDEVFRU gettext("fru_open_dev: open of %s failed %s")
#define GETPV gettext("fru_open_container: ptree_get_propval_by_name failed %s")
static int
fru_open_dev(void)
{
static int opendevfru = 0;
static int frufd = 0;
if ((opendevfru == 0) && (frufd == 0)) {
if ((frufd = open(FRU_PSEUDO_DEV, O_RDWR, access)) == -1) {
syslog(LOG_ERR, OPENDEVFRU, FRU_PSEUDO_DEV,
strerror(errno));
return (-1);
}
opendevfru = 1;
}
return (frufd);
}
/*
* Look up the container_hdl in the PICL tree.
*/
container_hdl_t
fru_open_container(picl_nodehdl_t fruh)
{
int err;
container_hdl_t container_hdl;
if (fru_open_dev() == -1) {
return (0);
}
err = ptree_get_propval_by_name(fruh, PICL_PROP_DATA_AVAIL, NULL, 0);
if (err != PICL_SUCCESS) {
syslog(LOG_ERR, GETPV, PICL_PROP_DATA_AVAIL, err);
return (0);
}
err = ptree_get_propval_by_name(fruh, PICL_PROP_SC_HANDLE,
&container_hdl, sizeof (container_hdl_t));
if (err != PICL_SUCCESS) {
syslog(LOG_ERR, GETPV, PICL_PROP_SC_HANDLE, err);
return (0);
}
return (container_hdl);
}
/*
* Note : fru_open_container and fru_close_container do not map onto the opens
* and closes of the sgfru device on lw8. There is one sgfru device which
* handles all containers.
*/
/*ARGSUSED*/
int
fru_close_container(container_hdl_t fru)
{
if (fru_open_dev() == -1) {
return (-1);
}
return (0);
}
/*ARGSUSED*/
int
fru_get_num_sections(container_hdl_t container, door_cred_t *cred)
{
section_info_t numsections;
int fd;
if ((fd = fru_open_dev()) == -1) {
return (-1);
}
numsections.hdl = container;
numsections.cnt = 0;
if (ioctl(fd, SGFRU_GETNUMSECTIONS, &numsections) != 0) {
return (-1);
}
return (numsections.cnt);
}
/*ARGSUSED*/
int
fru_get_sections(container_hdl_t container, section_t *section,
int max_sections, door_cred_t *cred)
{
sections_t sections;
int fd;
if ((fd = fru_open_dev()) == -1) {
return (-1);
}
sections.fru_hdl = container;
sections.fru_cnt = max_sections;
sections.frus = section;
if (ioctl(fd, SGFRU_GETSECTIONS, §ions) != 0) {
return (-1);
}
return (sections.fru_cnt);
}
/*ARGSUSED*/
int
fru_get_num_segments(section_hdl_t section, door_cred_t *cred)
{
segment_info_t numsegments;
int fd;
if ((fd = fru_open_dev()) == -1) {
return (-1);
}
numsegments.hdl = section;
numsegments.cnt = 0;
if (ioctl(fd, SGFRU_GETNUMSEGMENTS, &numsegments) != 0) {
return (-1);
}
return (numsegments.cnt);
}
/*ARGSUSED*/
int
fru_get_segments(section_hdl_t section, segment_t *segment, int max_segments,
door_cred_t *cred)
{
segments_t segments;
int fd;
if ((fd = fru_open_dev()) == -1) {
return (-1);
}
segments.fru_hdl = section;
segments.fru_cnt = max_segments;
segments.frus = segment;
if (ioctl(fd, SGFRU_GETSEGMENTS, &segments) != 0) {
return (-1);
}
return (segments.fru_cnt);
}
/*ARGSUSED*/
int
fru_add_segment(section_hdl_t section, segment_t *segment,
section_hdl_t *newsection, door_cred_t *cred)
{
segments_t newsegment;
int fd;
/* check the effective uid of the client */
if (cred->dc_euid != 0) {
errno = EPERM;
return (-1); /* not a root */
}
if ((fd = fru_open_dev()) == -1) {
return (-1);
}
newsegment.fru_hdl = section;
newsegment.fru_cnt = 1;
newsegment.frus = segment;
if (ioctl(fd, SGFRU_ADDSEGMENT, &newsegment) != 0) {
return (-1);
}
/*
* The new segment handle is returned in segment,
* return the updated section handle in newsection.
*/
*newsection = newsegment.fru_hdl;
return (0);
}
int
fru_delete_segment(segment_hdl_t segment, section_hdl_t *newsection,
door_cred_t *cred)
{
segment_info_t delsegment;
int fd;
/* check the effective uid of the client */
if (cred->dc_euid != 0) {
errno = EPERM;
return (-1); /* not a root */
}
if ((fd = fru_open_dev()) == -1) {
return (-1);
}
delsegment.hdl = segment;
if (ioctl(fd, SGFRU_DELETESEGMENT, &delsegment) != 0) {
return (-1);
}
/* Return the updated section handle in newsection. */
*newsection = delsegment.hdl;
return (0);
}
/*ARGSUSED*/
ssize_t
fru_read_segment(segment_hdl_t segment, void *buffer, size_t nbytes,
door_cred_t *cred)
{
segments_t readsegment;
int fd;
if ((fd = fru_open_dev()) == -1) {
return (-1);
}
readsegment.fru_hdl = segment;
readsegment.fru_cnt = nbytes;
readsegment.frus = buffer;
if (ioctl(fd, SGFRU_READRAWSEGMENT, &readsegment) != 0) {
return (-1);
}
return ((ssize_t)readsegment.fru_cnt);
}
/*ARGSUSED*/
ssize_t
fru_write_segment(segment_hdl_t segment, const void *buffer, size_t nbytes,
segment_hdl_t *newsegment, door_cred_t *cred)
{
segments_t writesegment;
int fd;
if ((fd = fru_open_dev()) == -1) {
return (-1);
}
writesegment.fru_hdl = segment;
writesegment.fru_cnt = nbytes;
writesegment.frus = (void *)buffer;
if (ioctl(fd, SGFRU_WRITERAWSEGMENT, &writesegment) != 0) {
return (-1);
}
/* Return the updated segment handle in newsegment. */
*newsegment = writesegment.fru_hdl;
return ((ssize_t)writesegment.fru_cnt);
}
/*ARGSUSED*/
int
fru_get_num_packets(segment_hdl_t segment, door_cred_t *cred)
{
packet_info_t numpackets;
int fd;
if ((fd = fru_open_dev()) == -1) {
return (-1);
}
numpackets.hdl = segment;
numpackets.cnt = 0;
if (ioctl(fd, SGFRU_GETNUMPACKETS, &numpackets) != 0) {
return (-1);
}
return (numpackets.cnt);
}
/*ARGSUSED*/
int
fru_get_packets(segment_hdl_t segment, packet_t *packet, int max_packets,
door_cred_t *cred)
{
packets_t packets;
int fd;
if ((fd = fru_open_dev()) == -1) {
return (-1);
}
packets.fru_hdl = segment;
packets.fru_cnt = max_packets;
packets.frus = packet;
if (ioctl(fd, SGFRU_GETPACKETS, &packets) != 0) {
return (-1);
}
return (packets.fru_cnt);
}
/*ARGSUSED*/
ssize_t
fru_get_payload(packet_hdl_t packet, void *buffer, size_t nbytes,
door_cred_t *cred)
{
payload_t payload;
int fd;
if ((fd = fru_open_dev()) == -1) {
return (-1);
}
payload.fru_hdl = packet;
payload.fru_cnt = nbytes;
payload.frus = buffer;
if (ioctl(fd, SGFRU_GETPAYLOAD, &payload) != 0) {
return (-1);
}
return ((ssize_t)payload.fru_cnt);
}
int
fru_update_payload(packet_hdl_t packet, const void *data, size_t nbytes,
packet_hdl_t *newpacket, door_cred_t *cred)
{
payload_t payload;
int fd;
/* check the effective uid of the client */
if (cred->dc_euid != 0) {
errno = EPERM;
return (-1); /* not a root */
}
if ((fd = fru_open_dev()) == -1) {
return (-1);
}
payload.fru_hdl = packet;
payload.fru_cnt = nbytes;
payload.frus = (void *)data;
if (ioctl(fd, SGFRU_UPDATEPAYLOAD, &payload) != 0) {
return (-1);
}
/* Return the updated packet handle in newpacket. */
*newpacket = payload.fru_hdl;
return (0);
}
int
fru_append_packet(segment_hdl_t segment, packet_t *packet, const void *payload,
size_t nbytes, segment_hdl_t *newsegment, door_cred_t *cred)
{
append_info_t appendpkt;
int fd;
/* check the effective uid of the client */
if (cred->dc_euid != 0) {
errno = EPERM;
return (-1); /* not a root */
}
if ((fd = fru_open_dev()) == -1) {
return (-1);
}
appendpkt.packet = *packet;
appendpkt.payload_hdl = segment;
appendpkt.payload_cnt = nbytes;
appendpkt.payload_data = (void *)payload;
if (ioctl(fd, SGFRU_APPENDPACKET, &appendpkt) != 0) {
return (-1);
}
/*
* The new packet handle is returned in packet,
* return the updated segment handle in newsegment.
*/
packet->handle = appendpkt.packet.handle;
*newsegment = appendpkt.payload_hdl;
return (0);
}
int
fru_delete_packet(packet_hdl_t packet, segment_hdl_t *newsegment,
door_cred_t *cred)
{
packet_info_t delpacket;
int fd;
/* check the effective uid of the client */
if (cred->dc_euid != 0) {
errno = EPERM;
return (-1); /* not a root */
}
if ((fd = fru_open_dev()) == -1) {
return (-1);
}
delpacket.hdl = packet;
if (ioctl(fd, SGFRU_DELETEPACKET, &delpacket) != 0) {
return (-1);
}
/* Return the updated segment handle in newsegment. */
*newsegment = delpacket.hdl;
return (0);
}
/*
* Description :
* fru_is_data_available() checks to see if the frudata
* is available on a fru.
*
* Arguments :
* picl_nodehdl_t holds the picl node handle of the fru.
*
* Return :
* int
* return 1: if FRUID information is available
* return 0: if FRUID information is not present
*
*/
/* ARGSUSED */
int
fru_is_data_available(picl_nodehdl_t fru)
{
return (0);
}
|