~ubuntu-branches/ubuntu/vivid/ipmitool/vivid-updates

« back to all changes in this revision

Viewing changes to lib/ipmi_sdradd.c

  • Committer: Package Import Robot
  • Author(s): Luk Claes
  • Date: 2013-05-07 18:12:12 UTC
  • mfrom: (5.1.11 raring-proposed)
  • Revision ID: package-import@ubuntu.com-20130507181212-fitwelheknk3tqnq
Tags: 1.8.12-1
Merge new upstream from Ubuntu (Closes: #692292).

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
 
53
53
#define ADD_PARTIAL_SDR 0x25
54
54
 
 
55
#ifdef HAVE_PRAGMA_PACK
 
56
#pragma pack(1)
 
57
#endif
55
58
struct sdr_add_rq {
56
59
  uint16_t reserve_id;  /* reservation ID */
57
60
  uint16_t id;          /* record ID */
60
63
#define PARTIAL_ADD (0)
61
64
#define LAST_RECORD (1)
62
65
  uint8_t data[1];      /* SDR record data */
63
 
} __attribute__ ((packed));
 
66
} ATTRIBUTE_PACKING;
 
67
#ifdef HAVE_PRAGMA_PACK
 
68
#pragma pack(0)
 
69
#endif
64
70
 
65
 
static int sdr_max_write_len = 24;
 
71
/* This was formerly initialized to 24, reduced this to 19 so the overall
 
72
   message fits into the recommended 32-byte limit */
 
73
static int sdr_max_write_len = 19;
 
74
int ipmi_parse_range_list(const char *rangeList, unsigned char *pHexList);
 
75
int ipmi_hex_to_dec( char * rangeList, unsigned char * pDecValue);
66
76
 
67
77
static int
68
78
partial_send(struct ipmi_intf *intf, struct ipmi_rq *req, uint16_t *id)
93
103
  int rc = 0;
94
104
 
95
105
  /* actually no SDR to program */
96
 
  if (len < 1 || !sdrr->raw)
 
106
  if (len < 1 || !sdrr->raw) {
 
107
    lprintf(LOG_ERR, "ipmitool: bad record , skipped");
97
108
    return 0;
 
109
  }
98
110
 
99
 
  if (ipmi_sdr_get_reservation(intf, 0, &reserve_id))
 
111
  if (ipmi_sdr_get_reservation(intf, 0, &reserve_id)) {
 
112
    lprintf(LOG_ERR, "ipmitool: reservation failed");
100
113
    return -1;
 
114
  }
101
115
 
102
116
  sdr_rq = (struct sdr_add_rq *)malloc(sizeof(*sdr_rq) + sdr_max_write_len);
103
117
  if (sdr_rq == NULL) {
123
137
  req.msg.data_len = 5 + sizeof(*sdr_rq) - 1;
124
138
 
125
139
  if (partial_send(intf, &req, &id)) {
 
140
     lprintf(LOG_ERR, "ipmitool: partial send error");
126
141
    free(sdr_rq);
127
142
    return -1;
128
143
  }
131
146
 
132
147
  /* sdr entry */
133
148
  while (i < len) {
134
 
    int data_len = 0;
135
 
 
136
 
    if (len - i < sdr_max_write_len) {
 
149
     int data_len = 0;
 
150
     if ( (len - i) <= sdr_max_write_len) {
137
151
      /* last crunch */
138
152
      data_len = len - i;
139
153
      sdr_rq->in_progress = LAST_RECORD;
147
161
    req.msg.data_len = data_len + sizeof(*sdr_rq) - 1;
148
162
 
149
163
    if ((rc = partial_send(intf, &req, &id)) != 0) {
 
164
       lprintf(LOG_ERR, "ipmitool: partial add failed");
150
165
      break;
151
166
    }
152
167
 
153
168
    i += data_len;
154
169
  }
 
170
 
155
171
  free(sdr_rq);
156
172
  return rc;
157
173
}
238
254
      return -1;
239
255
    }
240
256
    memset(sdrr, 0, sizeof (struct sdr_record_list));
 
257
   
241
258
    sdrr->id = header->id;
242
259
    sdrr->version = header->version;
243
260
    sdrr->type = header->type;
244
261
    sdrr->length = header->length;
245
262
    sdrr->raw = ipmi_sdr_get_record(intf, header, itr);
 
263
    (void)ipmi_sdr_print_name_from_rawentry(intf,  sdrr->id, sdrr->type,sdrr->raw);
246
264
 
247
265
    /* put in the record queue */
248
266
    if (queue->head == NULL)
266
284
 
267
285
  /* generate list of records for this target */
268
286
  intf->target_addr = from_addr;
 
287
 
 
288
  /* initialize iterator */
269
289
  itr = ipmi_sdr_start(intf, use_builtin);
270
290
  if (itr == 0)
271
291
    return 0;
273
293
  printf("Load SDRs from 0x%x\n", from_addr);
274
294
  rc = sdrr_get_records(intf, itr, &sdrr_queue);
275
295
  ipmi_sdr_end(intf, itr);
 
296
  /* ... */
276
297
 
277
298
  /* write the SDRs to the destination SDR Repository */
278
299
  intf->target_addr = to_addr;
304
325
  rc = sdr_copy_to_sdrr(intf, 1, myaddr, myaddr);
305
326
 
306
327
  /* Now fill the SDRR with remote sensors */
307
 
  for (i = 0, slave_addr = 0xB0; i < maxslot; i++, slave_addr += 2) {
308
 
    /* Hole in the PICMG 2.9 mapping */
309
 
    if (slave_addr == 0xC2) slave_addr += 2;
310
 
    if(sdr_copy_to_sdrr(intf, 0, slave_addr, myaddr) < 0)
311
 
    {
312
 
      rc = -1;
313
 
    }
314
 
  }
 
328
  if( maxslot != 0 ) {
 
329
     for (i = 0, slave_addr = 0xB0; i < maxslot; i++, slave_addr += 2) {
 
330
        /* Hole in the PICMG 2.9 mapping */
 
331
        if (slave_addr == 0xC2) slave_addr += 2;
 
332
        if(sdr_copy_to_sdrr(intf, 0, slave_addr, myaddr) < 0)
 
333
        {
 
334
           rc = -1;
 
335
        }
 
336
     }
 
337
  }
 
338
  return rc;
 
339
}
 
340
 
 
341
int ipmi_hex_to_dec( char * strchar, unsigned char * pDecValue)
 
342
{
 
343
  int rc = -1;
 
344
  unsigned char retValue = 0; 
 
345
 
 
346
  if(
 
347
     (strlen(strchar) == 4) 
 
348
     &&
 
349
     (strchar[0] == '0') 
 
350
     && 
 
351
     (strchar[1] == 'x') 
 
352
    ) 
 
353
  {
 
354
      rc = 0;
 
355
 
 
356
      if((strchar[2] >= '0') && (strchar[2] <= '9')) 
 
357
      {
 
358
        retValue += ((strchar[2]-'0') * 16);
 
359
      }
 
360
      else if((strchar[2] >= 'a') && (strchar[2] <= 'f')) 
 
361
      {
 
362
        retValue += (((strchar[2]-'a') + 10) * 16);
 
363
      }
 
364
      else if((strchar[2] >= 'A') && (strchar[2] <= 'F')) 
 
365
      {
 
366
        retValue += (((strchar[2]-'A') + 10) * 16);
 
367
      }
 
368
      else
 
369
      {
 
370
        rc = -1;
 
371
      }
 
372
 
 
373
      if((strchar[3] >= '0') && (strchar[3] <= '9')) 
 
374
      {
 
375
        retValue += ((strchar[3]-'0'));
 
376
      }
 
377
      else if((strchar[3] >= 'a') && (strchar[3] <= 'f')) 
 
378
      {
 
379
        retValue += (((strchar[3]-'a') + 10));
 
380
      }
 
381
      else if((strchar[3] >= 'A') && (strchar[3] <= 'F')) 
 
382
      {
 
383
        retValue += (((strchar[3]-'A') + 10));
 
384
      }
 
385
      else
 
386
      {
 
387
        rc = -1;
 
388
      }
 
389
  }
 
390
 
 
391
  if(rc == 0) 
 
392
  {
 
393
    * pDecValue = retValue;
 
394
  }
 
395
  else
 
396
  {
 
397
    lprintf(LOG_ERR, "Must be Hex value of 4 characters (Ex.: 0x24)\n");
 
398
  }
 
399
 
 
400
  return rc;
 
401
}
 
402
 
 
403
 
 
404
 
 
405
#define MAX_NUM_SLOT  128
 
406
int ipmi_parse_range_list(const char *rangeList, unsigned char * pHexList)
 
407
{
 
408
  int rc = -1;
 
409
 
 
410
  unsigned char listOffset = 0;
 
411
  char * nextString;
 
412
  char * rangeString;
 
413
  char * inProcessString = (char *) rangeList;
 
414
 
 
415
  /* Discard empty string */
 
416
  if(strlen(rangeList) == 0) 
 
417
  {
 
418
    return rc;
 
419
  }
 
420
 
 
421
  /* First, cut to comma separated string */
 
422
  nextString = strstr( rangeList, "," );
 
423
 
 
424
  if(nextString != rangeList) 
 
425
  {
 
426
    unsigned char isLast;
 
427
    /* We get a valid string so far */
 
428
    rc = 0;
 
429
 
 
430
    do
 
431
    {
 
432
      if(nextString != NULL) 
 
433
      {
 
434
        (*nextString)= 0;
 
435
        nextString   ++;
 
436
        isLast = 0;
 
437
      }
 
438
      else
 
439
      {
 
440
        isLast = 1;
 
441
      }
 
442
 
 
443
      /* At this point, it is a single entry or a range */
 
444
      rangeString = strstr( inProcessString, "-" );
 
445
      if(rangeString == NULL) 
 
446
      {
 
447
        unsigned char decValue = 0;
 
448
 
 
449
        /* Single entry */
 
450
        rc = ipmi_hex_to_dec( inProcessString, &decValue); 
 
451
        
 
452
        if(rc == 0)
 
453
        {  
 
454
          if((decValue % 2) == 0)
 
455
          {
 
456
            pHexList[listOffset++] = decValue;
 
457
          }
 
458
          else
 
459
          {
 
460
            lprintf(LOG_ERR, "I2c address provided value must be even.\n");
 
461
          }
 
462
        }
 
463
      }
 
464
      else
 
465
      {
 
466
        unsigned char startValue = 0;
 
467
        unsigned char endValue = 0;
 
468
 
 
469
 
 
470
        (*rangeString)= 0; /* Cut string*/
 
471
        rangeString ++;
 
472
 
 
473
        /* Range */
 
474
        rc = ipmi_hex_to_dec( inProcessString, &startValue); 
 
475
        if(rc == 0) 
 
476
          rc = ipmi_hex_to_dec( rangeString, &endValue); 
 
477
 
 
478
        if(rc == 0)
 
479
        {  
 
480
          if(((startValue % 2) == 0) && ((endValue % 2) == 0))
 
481
          {
 
482
            do
 
483
            {
 
484
              pHexList[listOffset++] = startValue;
 
485
              startValue += 2;
 
486
            }
 
487
            while(startValue != endValue);
 
488
            pHexList[listOffset++] = endValue;
 
489
          }
 
490
          else
 
491
          {
 
492
            lprintf(LOG_ERR, "I2c address provided value must be even.\n");
 
493
          }
 
494
        }
 
495
      }
 
496
 
 
497
      if(isLast == 0) 
 
498
      {
 
499
        /* Setup for next string */
 
500
        inProcessString = nextString;
 
501
        nextString = strstr( rangeList, "," );
 
502
      }
 
503
    }while ((isLast == 0) && (rc == 0)); 
 
504
  }
 
505
 
 
506
  return rc;
 
507
}
 
508
 
 
509
int
 
510
ipmi_sdr_add_from_list(struct ipmi_intf *intf, const char *rangeList)
 
511
{
 
512
  int i;
 
513
  int rc = 0;
 
514
  int slave_addr;
 
515
  int myaddr = intf->target_addr;
 
516
  unsigned char listValue[MAX_NUM_SLOT];
 
517
 
 
518
  memset( listValue, 0, MAX_NUM_SLOT );
 
519
 
 
520
  /* Build list from string */
 
521
  if(ipmi_parse_range_list(rangeList, listValue) != 0)
 
522
  {
 
523
    lprintf(LOG_ERR, "Range - List invalid, cannot be parsed.");
 
524
    return -1;
 
525
  }
 
526
 
 
527
  {
 
528
    unsigned char counter = 0;
 
529
    printf("List to scan: (Built-in) ");
 
530
    while(listValue[counter] != 0) 
 
531
    {
 
532
      printf("%02x ", listValue[counter]);
 
533
      counter++;
 
534
    }
 
535
    printf("\n");
 
536
  }
 
537
 
 
538
  printf("Clearing SDR Repository\n");
 
539
  if (ipmi_sdr_repo_clear(intf)) {
 
540
    lprintf(LOG_ERR, "Cannot erase SDRR. Give up.");
 
541
    return -1;
 
542
  }
 
543
 
 
544
  /* First fill the SDRR from local built-in sensors */
 
545
  printf("Sanning built-in sensors..\n");
 
546
  rc = sdr_copy_to_sdrr(intf, 1, myaddr, myaddr);
 
547
 
 
548
  /* Now fill the SDRR with provided sensors list */
 
549
  {
 
550
    unsigned char counter = 0;
 
551
    while((rc == 0) && (listValue[counter] != 0)) 
 
552
    {
 
553
      slave_addr = listValue[counter];
 
554
      printf("Scanning %02Xh..\n", slave_addr);
 
555
      if(sdr_copy_to_sdrr(intf, 0, slave_addr, myaddr) < 0)
 
556
      {
 
557
         rc = -1;
 
558
      }
 
559
      counter++;
 
560
    }
 
561
  }
 
562
 
315
563
  return rc;
316
564
}
317
565