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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
|
/*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistribution of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistribution in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind.
* ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
* INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
* SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE
* FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
* SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA,
* OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
* PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
* LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
* EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*/
/*
* Functions to program the SDR repository, from built-in sensors or
* from sensors dumped in a binary file.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <fcntl.h>
#include <ipmitool/helper.h>
#include <ipmitool/log.h>
#include <ipmitool/bswap.h>
#include <ipmitool/ipmi.h>
#include <ipmitool/ipmi_intf.h>
#include <ipmitool/ipmi_mc.h>
#include <ipmitool/ipmi_strings.h>
#include <ipmitool/ipmi_sdr.h>
#define ADD_PARTIAL_SDR 0x25
#ifdef HAVE_PRAGMA_PACK
#pragma pack(1)
#endif
struct sdr_add_rq {
uint16_t reserve_id; /* reservation ID */
uint16_t id; /* record ID */
uint8_t offset; /* offset into SDR */
uint8_t in_progress; /* 0=partial, 1=last */
#define PARTIAL_ADD (0)
#define LAST_RECORD (1)
uint8_t data[1]; /* SDR record data */
} ATTRIBUTE_PACKING;
#ifdef HAVE_PRAGMA_PACK
#pragma pack(0)
#endif
/* This was formerly initialized to 24, reduced this to 19 so the overall
message fits into the recommended 32-byte limit */
static int sdr_max_write_len = 19;
int ipmi_parse_range_list(const char *rangeList, unsigned char *pHexList);
int ipmi_hex_to_dec( char * rangeList, unsigned char * pDecValue);
static int
partial_send(struct ipmi_intf *intf, struct ipmi_rq *req, uint16_t *id)
{
struct ipmi_rs *rsp;
rsp = intf->sendrecv(intf, req);
if (rsp == NULL) {
return -1;
}
if (rsp->ccode || rsp->data_len < 2) {
return -1;
}
*id = rsp->data[0] + (rsp->data[1] << 8);
return 0;
}
int
ipmi_sdr_add_record(struct ipmi_intf *intf, struct sdr_record_list *sdrr)
{
struct ipmi_rq req;
struct sdr_add_rq *sdr_rq;
uint16_t reserve_id;
uint16_t id;
int i;
int len = sdrr->length;
int rc = 0;
/* actually no SDR to program */
if (len < 1 || !sdrr->raw) {
lprintf(LOG_ERR, "ipmitool: bad record , skipped");
return 0;
}
if (ipmi_sdr_get_reservation(intf, 0, &reserve_id)) {
lprintf(LOG_ERR, "ipmitool: reservation failed");
return -1;
}
sdr_rq = (struct sdr_add_rq *)malloc(sizeof(*sdr_rq) + sdr_max_write_len);
if (sdr_rq == NULL) {
lprintf(LOG_ERR, "ipmitool: malloc failure");
return -1;
}
sdr_rq->reserve_id = reserve_id;
sdr_rq->in_progress = PARTIAL_ADD;
memset(&req, 0, sizeof(req));
req.msg.netfn = IPMI_NETFN_STORAGE;
req.msg.cmd = ADD_PARTIAL_SDR;
req.msg.data = (uint8_t *) sdr_rq;
/* header first */
sdr_rq->id = 0;
sdr_rq->offset = 0;
sdr_rq->data[0] = sdrr->id & 0xFF;
sdr_rq->data[1] = (sdrr->id >> 8) & 0xFF;
sdr_rq->data[2] = sdrr->version;
sdr_rq->data[3] = sdrr->type;
sdr_rq->data[4] = sdrr->length;
req.msg.data_len = 5 + sizeof(*sdr_rq) - 1;
if (partial_send(intf, &req, &id)) {
lprintf(LOG_ERR, "ipmitool: partial send error");
free(sdr_rq);
sdr_rq = NULL;
return -1;
}
i = 0;
/* sdr entry */
while (i < len) {
int data_len = 0;
if ( (len - i) <= sdr_max_write_len) {
/* last crunch */
data_len = len - i;
sdr_rq->in_progress = LAST_RECORD;
} else {
data_len = sdr_max_write_len;
}
sdr_rq->id = id;
sdr_rq->offset = i + 5;
memcpy(sdr_rq->data, sdrr->raw + i, data_len);
req.msg.data_len = data_len + sizeof(*sdr_rq) - 1;
if ((rc = partial_send(intf, &req, &id)) != 0) {
lprintf(LOG_ERR, "ipmitool: partial add failed");
break;
}
i += data_len;
}
free(sdr_rq);
sdr_rq = NULL;
return rc;
}
static int
ipmi_sdr_repo_clear(struct ipmi_intf *intf)
{
struct ipmi_rs * rsp;
struct ipmi_rq req;
uint8_t msg_data[8];
uint16_t reserve_id;
int try;
if (ipmi_sdr_get_reservation(intf, 0, &reserve_id))
return -1;
memset(&req, 0, sizeof(req));
req.msg.netfn = IPMI_NETFN_STORAGE;
req.msg.cmd = 0x27; // FIXME
req.msg.data = msg_data;
req.msg.data_len = 6;
msg_data[0] = reserve_id & 0xFF;
msg_data[1] = reserve_id >> 8;
msg_data[2] = 'C';
msg_data[3] = 'L';
msg_data[4] = 'R';
msg_data[5] = 0xAA;
for (try = 0; try < 5; try++) {
rsp = intf->sendrecv(intf, &req);
if (rsp == NULL) {
lprintf(LOG_ERR, "Unable to clear SDRR");
return -1;
}
if (rsp->ccode > 0) {
lprintf(LOG_ERR, "Unable to clear SDRR: %s",
val2str(rsp->ccode, completion_code_vals));
return -1;
}
if ((rsp->data[0] & 1) == 1) {
printf("SDRR successfully erased\n");
return 0;
}
printf("Wait for SDRR erasure completed...\n");
msg_data[5] = 0;
sleep(1);
}
/* if we are here we fed up trying erase */
return -1;
}
struct sdrr_queue {
struct sdr_record_list *head;
struct sdr_record_list *tail;
};
/*
* Fill the SDR repository from built-in sensors
*
*/
/*
* Get all the SDR records stored in <queue>
*/
static int
sdrr_get_records(struct ipmi_intf *intf, struct ipmi_sdr_iterator *itr,
struct sdrr_queue *queue)
{
struct sdr_get_rs *header;
queue->head = NULL;
queue->tail = NULL;
while ((header = ipmi_sdr_get_next_header(intf, itr)) != NULL) {
struct sdr_record_list *sdrr;
sdrr = malloc(sizeof (struct sdr_record_list));
if (sdrr == NULL) {
lprintf(LOG_ERR, "ipmitool: malloc failure");
return -1;
}
memset(sdrr, 0, sizeof (struct sdr_record_list));
sdrr->id = header->id;
sdrr->version = header->version;
sdrr->type = header->type;
sdrr->length = header->length;
sdrr->raw = ipmi_sdr_get_record(intf, header, itr);
(void)ipmi_sdr_print_name_from_rawentry(intf, sdrr->id, sdrr->type,sdrr->raw);
/* put in the record queue */
if (queue->head == NULL)
queue->head = sdrr;
else
queue->tail->next = sdrr;
queue->tail = sdrr;
}
return 0;
}
static int
sdr_copy_to_sdrr(struct ipmi_intf *intf, int use_builtin,
int from_addr, int to_addr)
{
int rc;
struct sdrr_queue sdrr_queue;
struct ipmi_sdr_iterator *itr;
struct sdr_record_list *sdrr;
struct sdr_record_list *sdrr_next;
/* generate list of records for this target */
intf->target_addr = from_addr;
/* initialize iterator */
itr = ipmi_sdr_start(intf, use_builtin);
if (itr == 0)
return 0;
printf("Load SDRs from 0x%x\n", from_addr);
rc = sdrr_get_records(intf, itr, &sdrr_queue);
ipmi_sdr_end(intf, itr);
/* ... */
/* write the SDRs to the destination SDR Repository */
intf->target_addr = to_addr;
for (sdrr = sdrr_queue.head; sdrr != NULL; sdrr = sdrr_next) {
sdrr_next = sdrr->next;
rc = ipmi_sdr_add_record(intf, sdrr);
if(rc < 0){
lprintf(LOG_ERR, "Cannot add SDR ID 0x%04x to repository...", sdrr->id);
}
free(sdrr);
sdrr = NULL;
}
return rc;
}
int
ipmi_sdr_add_from_sensors(struct ipmi_intf *intf, int maxslot)
{
int i;
int rc = 0;
int slave_addr;
int myaddr = intf->target_addr;
if (ipmi_sdr_repo_clear(intf)) {
lprintf(LOG_ERR, "Cannot erase SDRR. Give up.");
return -1;
}
/* First fill the SDRR from local built-in sensors */
rc = sdr_copy_to_sdrr(intf, 1, myaddr, myaddr);
/* Now fill the SDRR with remote sensors */
if( maxslot != 0 ) {
for (i = 0, slave_addr = 0xB0; i < maxslot; i++, slave_addr += 2) {
/* Hole in the PICMG 2.9 mapping */
if (slave_addr == 0xC2) slave_addr += 2;
if(sdr_copy_to_sdrr(intf, 0, slave_addr, myaddr) < 0)
{
rc = -1;
}
}
}
return rc;
}
int ipmi_hex_to_dec( char * strchar, unsigned char * pDecValue)
{
int rc = -1;
unsigned char retValue = 0;
if(
(strlen(strchar) == 4)
&&
(strchar[0] == '0')
&&
(strchar[1] == 'x')
)
{
rc = 0;
if((strchar[2] >= '0') && (strchar[2] <= '9'))
{
retValue += ((strchar[2]-'0') * 16);
}
else if((strchar[2] >= 'a') && (strchar[2] <= 'f'))
{
retValue += (((strchar[2]-'a') + 10) * 16);
}
else if((strchar[2] >= 'A') && (strchar[2] <= 'F'))
{
retValue += (((strchar[2]-'A') + 10) * 16);
}
else
{
rc = -1;
}
if((strchar[3] >= '0') && (strchar[3] <= '9'))
{
retValue += ((strchar[3]-'0'));
}
else if((strchar[3] >= 'a') && (strchar[3] <= 'f'))
{
retValue += (((strchar[3]-'a') + 10));
}
else if((strchar[3] >= 'A') && (strchar[3] <= 'F'))
{
retValue += (((strchar[3]-'A') + 10));
}
else
{
rc = -1;
}
}
if(rc == 0)
{
* pDecValue = retValue;
}
else
{
lprintf(LOG_ERR, "Must be Hex value of 4 characters (Ex.: 0x24)");
}
return rc;
}
#define MAX_NUM_SLOT 128
int ipmi_parse_range_list(const char *rangeList, unsigned char * pHexList)
{
int rc = -1;
unsigned char listOffset = 0;
char * nextString;
char * rangeString;
char * inProcessString = (char *) rangeList;
/* Discard empty string */
if(strlen(rangeList) == 0)
{
return rc;
}
/* First, cut to comma separated string */
nextString = strstr( rangeList, "," );
if(nextString != rangeList)
{
unsigned char isLast;
/* We get a valid string so far */
rc = 0;
do
{
if(nextString != NULL)
{
(*nextString)= 0;
nextString ++;
isLast = 0;
}
else
{
isLast = 1;
}
/* At this point, it is a single entry or a range */
rangeString = strstr( inProcessString, "-" );
if(rangeString == NULL)
{
unsigned char decValue = 0;
/* Single entry */
rc = ipmi_hex_to_dec( inProcessString, &decValue);
if(rc == 0)
{
if((decValue % 2) == 0)
{
pHexList[listOffset++] = decValue;
}
else
{
lprintf(LOG_ERR, "I2C address provided value must be even.");
}
}
}
else
{
unsigned char startValue = 0;
unsigned char endValue = 0;
(*rangeString)= 0; /* Cut string*/
rangeString ++;
/* Range */
rc = ipmi_hex_to_dec( inProcessString, &startValue);
if(rc == 0)
rc = ipmi_hex_to_dec( rangeString, &endValue);
if(rc == 0)
{
if(((startValue % 2) == 0) && ((endValue % 2) == 0))
{
do
{
pHexList[listOffset++] = startValue;
startValue += 2;
}
while(startValue != endValue);
pHexList[listOffset++] = endValue;
}
else
{
lprintf(LOG_ERR, "I2C address provided value must be even.");
}
}
}
if(isLast == 0)
{
/* Setup for next string */
inProcessString = nextString;
nextString = strstr( rangeList, "," );
}
}while ((isLast == 0) && (rc == 0));
}
return rc;
}
int
ipmi_sdr_add_from_list(struct ipmi_intf *intf, const char *rangeList)
{
int rc = 0;
int slave_addr;
int myaddr = intf->target_addr;
unsigned char listValue[MAX_NUM_SLOT];
memset( listValue, 0, MAX_NUM_SLOT );
/* Build list from string */
if(ipmi_parse_range_list(rangeList, listValue) != 0)
{
lprintf(LOG_ERR, "Range - List invalid, cannot be parsed.");
return -1;
}
{
unsigned char counter = 0;
printf("List to scan: (Built-in) ");
while(listValue[counter] != 0)
{
printf("%02x ", listValue[counter]);
counter++;
}
printf("\n");
}
printf("Clearing SDR Repository\n");
if (ipmi_sdr_repo_clear(intf)) {
lprintf(LOG_ERR, "Cannot erase SDRR. Give up.");
return -1;
}
/* First fill the SDRR from local built-in sensors */
printf("Sanning built-in sensors..\n");
rc = sdr_copy_to_sdrr(intf, 1, myaddr, myaddr);
/* Now fill the SDRR with provided sensors list */
{
unsigned char counter = 0;
while((rc == 0) && (listValue[counter] != 0))
{
slave_addr = listValue[counter];
printf("Scanning %02Xh..\n", slave_addr);
if(sdr_copy_to_sdrr(intf, 0, slave_addr, myaddr) < 0)
{
rc = -1;
}
counter++;
}
}
return rc;
}
/*
* Fill the SDR repository from records stored in a binary file
*
*/
static int
ipmi_sdr_read_records(const char *filename, struct sdrr_queue *queue)
{
int rc = 0;
int fd;
uint8_t binHdr[5];
queue->head = NULL;
queue->tail = NULL;
if ((fd = open(filename, O_RDONLY)) < 0) {
return -1;
}
while (read(fd, binHdr, 5) == 5) {
struct sdr_record_list *sdrr;
lprintf(LOG_DEBUG, "binHdr[0] (id[MSB]) = 0x%02x", binHdr[0]);
lprintf(LOG_DEBUG, "binHdr[1] (id[LSB]) = 0x%02x", binHdr[1]);
lprintf(LOG_DEBUG, "binHdr[2] (version) = 0x%02x", binHdr[2]);
lprintf(LOG_DEBUG, "binHdr[3] (type) = 0x%02x", binHdr[3]);
lprintf(LOG_DEBUG, "binHdr[4] (length) = 0x%02x", binHdr[4]);
sdrr = malloc(sizeof(*sdrr));
if (sdrr == NULL) {
lprintf(LOG_ERR, "ipmitool: malloc failure");
rc = -1;
break;
}
sdrr->id = (binHdr[1] << 8) | binHdr[0]; // LS Byte first
sdrr->version = binHdr[2];
sdrr->type = binHdr[3];
sdrr->length = binHdr[4];
if ((sdrr->raw = malloc(sdrr->length)) == NULL) {
lprintf(LOG_ERR, "ipmitool: malloc failure");
free(sdrr);
sdrr = NULL;
rc = -1;
break;
}
if (read(fd, sdrr->raw, sdrr->length) != sdrr->length) {
lprintf(LOG_ERR, "SDR from '%s' truncated", filename);
free(sdrr->raw);
sdrr->raw = NULL;
free(sdrr);
sdrr = NULL;
rc = -1;
break;
}
/* put in the record queue */
if (queue->head == NULL)
queue->head = sdrr;
else
queue->tail->next = sdrr;
queue->tail = sdrr;
}
close(fd);
return rc;
}
int
ipmi_sdr_add_from_file(struct ipmi_intf *intf, const char *ifile)
{
int rc;
struct sdrr_queue sdrr_queue;
struct sdr_record_list *sdrr;
struct sdr_record_list *sdrr_next;
/* read the SDR records from file */
rc = ipmi_sdr_read_records(ifile, &sdrr_queue);
if (ipmi_sdr_repo_clear(intf)) {
lprintf(LOG_ERR, "Cannot erase SDRR. Giving up.");
/* FIXME: free sdr list */
return -1;
}
/* write the SDRs to the SDR Repository */
for (sdrr = sdrr_queue.head; sdrr != NULL; sdrr = sdrr_next) {
sdrr_next = sdrr->next;
rc = ipmi_sdr_add_record(intf, sdrr);
if(rc < 0){
lprintf(LOG_ERR, "Cannot add SDR ID 0x%04x to repository...", sdrr->id);
}
free(sdrr);
sdrr = NULL;
}
return rc;
}
|