summaryrefslogtreecommitdiff
path: root/lib/ipmi_ime.c
blob: 6bdca3b89ca9470f70f1af5d476adfff5858e8d5 (plain)
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
/*
 * Copyright (c) 2007 Kontron Canada, Inc.  All Rights Reserved.
 *
 * Base on code from
 * Copyright (c) 2003 Sun Microsystems, Inc.  All Rights Reserved.
 *
 * 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.
 */

/****************************************************************************
*
*       Copyright (c) 2009 Kontron Canada, Inc.  All Rights Reserved.
*
*                              IME
*                    Intel Manageability Engine
*                      Firmware Update Agent
*
* The ME is an IPMI-enabled component included in Intel(R) Next Generation
*  Server Chipset Nehalem-EP platforms.
*
* These are a few synonyms for the ME :
*
* - Dynamic Power Node Manager
* - Intelligent Power Node Manager
* 
* Consult Intel litterature for more information on this technology.
*
* The ME firmware resides on the platform boot flash and contains read only
* boot code for the ME as well as boot image redundancy support. 
*
* This module implements an Upgrade Agent for the ME firwmare. Because the ME 
* implements IPMI command handling, the agent speaks directly to the ME. In other
* words, in order the reach the ME, the BMC must implement IPMB bridging.
*
* The update is done through IPMI (this is IPMITOOL right !), not HECI.
*
* Example: ME available at address 0x88 on IPMI channel 8:
*   ipmitool  -m 0x20 -t 0x88 -b 8 ime info
*
* !! WARNING - You MUST use an image provided by your board vendor. - WARNING !!
*
* author:
*  Jean-Michel.Audet@ca.kontron.com
*  Francois.Isabelle@ca.kontron.com
*
*****************************************************************************/
/*
 * HISTORY
 * ===========================================================================
 * 2009-04-20
 *
 * First public release of Kontron
 *
*/
#include <ipmitool/ipmi_ime.h>
#include <ipmitool/log.h>
#include <ipmitool/ipmi_intf.h>
#include <ipmitool/ipmi_mc.h>
#include <ipmitool/helper.h>
#include <ipmitool/ipmi_strings.h>


#undef OUTPUT_DEBUG

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>

static const int IME_SUCCESS              = 0;
static const int IME_ERROR                = -1;
static const int IME_RESTART              = -2;

#define IME_UPGRADE_BUFFER_SIZE           22
#define IME_RETRY_COUNT                   5

typedef struct ImeUpdateImageCtx
{
   uint32_t   size;
   uint8_t *  pData;
   uint8_t    crc8;
}tImeUpdateImageCtx;

typedef enum eImeState
{
   IME_STATE_IDLE                = 0,
   IME_STATE_UPDATE_REQUESTED    = 1,
   IME_STATE_UPDATE_IN_PROGRESS  = 2,
   IME_STATE_SUCCESS             = 3,
   IME_STATE_FAILED              = 4,
   IME_STATE_ROLLED_BACK         = 5,
   IME_STATE_ABORTED             = 6,
   IME_STATE_INIT_FAILED         = 7
} tImeStateEnum;


typedef enum tImeUpdateType
{
   IME_UPDTYPE_NORMAL            = 1,
   IME_UPDTYPE_MANUAL_ROLLBACK   = 3,
   IME_UPDTYPE_ABORT             = 4
} tImeUpdateType;


#ifdef HAVE_PRAGMA_PACK
#pragma pack(1)
#endif
typedef struct sImeStatus {
   uint8_t image_status;
   tImeStateEnum update_state;
   uint8_t update_attempt_status;
   uint8_t rollback_attempt_status;
   uint8_t update_type;
   uint8_t dependent_flag;
   uint8_t free_area_size[4];
} ATTRIBUTE_PACKING tImeStatus ;
#ifdef HAVE_PRAGMA_PACK
#pragma pack(0)
#endif

#ifdef HAVE_PRAGMA_PACK
#pragma pack(1)
#endif
typedef struct sImeCaps {
   uint8_t area_supported;
   uint8_t special_caps;
} ATTRIBUTE_PACKING tImeCaps ;
#ifdef HAVE_PRAGMA_PACK
#pragma pack(0)
#endif


static void ImePrintUsage(void);
static int  ImeGetInfo(struct ipmi_intf *intf);
static int  ImeUpgrade(struct ipmi_intf *intf, char* imageFilename);
static int  ImeManualRollback(struct ipmi_intf *intf);
static int  ImeUpdatePrepare(struct ipmi_intf *intf);
static int  ImeUpdateOpenArea(struct ipmi_intf *intf);
static int  ImeUpdateWriteArea(
                              struct ipmi_intf *intf,
                              uint8_t sequence, 
                              uint8_t length, 
                              uint8_t * pBuf
                          );
static int  ImeUpdateCloseArea(
                              struct ipmi_intf *intf,
                              uint32_t size, 
                              uint16_t checksum
                          );

static int ImeUpdateGetStatus(struct ipmi_intf *intf, tImeStatus *pStatus);
static int ImeUpdateGetCapabilities(struct ipmi_intf *intf, tImeCaps *pCaps );
static int  ImeUpdateRegisterUpdate(struct ipmi_intf *intf, tImeUpdateType type);

static int  ImeImageCtxFromFile(
                                 char * imageFilename, 
                                 tImeUpdateImageCtx * pImageCtx);
static int ImeUpdateShowStatus(struct ipmi_intf *intf);

static uint8_t ImeCrc8( uint32_t length, uint8_t * pBuf );


static int ImeGetInfo(struct ipmi_intf *intf)
{
   int rc = IME_ERROR;
   struct ipmi_rs * rsp;
   struct ipmi_rq req;
   struct ipm_devid_rsp *devid;
   const char *product=NULL;
   tImeStatus status;
   tImeCaps caps;

   memset(&req, 0, sizeof(req));
   req.msg.netfn = IPMI_NETFN_APP;
   req.msg.cmd = BMC_GET_DEVICE_ID;
   req.msg.data_len = 0;

   rsp = intf->sendrecv(intf, &req);
   if (rsp == NULL) {
      lprintf(LOG_ERR, "Get Device ID command failed");
      return IME_ERROR;
   }
   if (rsp->ccode > 0) {
      lprintf(LOG_ERR, "Get Device ID command failed: %s",
         val2str(rsp->ccode, completion_code_vals));
      return IME_ERROR;
   }

   devid = (struct ipm_devid_rsp *) rsp->data;

   lprintf(LOG_DEBUG,"Device ID                 : %i", devid->device_id);
   lprintf(LOG_DEBUG,"Device Revision           : %i",
                           devid->device_revision & IPM_DEV_DEVICE_ID_REV_MASK);

   if(
      (devid->device_id == 0)
      &&
      ((devid->device_revision & IPM_DEV_DEVICE_ID_REV_MASK) == 0)
      &&
      (
         (devid->manufacturer_id[0] == 0x57) // Intel
         &&
         (devid->manufacturer_id[1] == 0x01) // Intel
         &&
         (devid->manufacturer_id[2] == 0x00) // Intel
      )
      &&
      (
         (devid->product_id[1] == 0x0b)
         &&
         (devid->product_id[0] == 0x00)
      )
     )
   {
      rc = IME_SUCCESS;
      printf("Manufacturer Name          : %s\n",
               val2str( (long)IPM_DEV_MANUFACTURER_ID(devid->manufacturer_id), 
               ipmi_oem_info) );

      printf("Product ID                 : %u (0x%02x%02x)\n",
         buf2short((uint8_t *)(devid->product_id)),
         devid->product_id[1], devid->product_id[0]);
 
      product=oemval2str(IPM_DEV_MANUFACTURER_ID(devid->manufacturer_id),
                      (devid->product_id[1]<<8)+devid->product_id[0],
                      ipmi_oem_product_info);

      if (product!=NULL) 
      {
         printf("Product Name               : %s\n", product);
      }

      printf("Intel ME Firmware Revision : %x.%02x.%02x.%x%x%x.%x\n",
            ((devid->fw_rev1 & IPM_DEV_FWREV1_MAJOR_MASK )         ),
            ((devid->fw_rev2                             ) >>     4),
            ((devid->fw_rev2                             )  &  0x0f),
            ((devid->aux_fw_rev[1]                       ) >>     4),
            ((devid->aux_fw_rev[1]                       )  &  0x0f),
            ((devid->aux_fw_rev[2]                       ) >>     4),
            ((devid->aux_fw_rev[2]                       )  &  0x0f)
      );

      printf("SPS FW IPMI cmd version    : %x.%x\n",
         devid->aux_fw_rev[0] >>     4,
         devid->aux_fw_rev[0] &  0x0f);

      lprintf(LOG_DEBUG,"Flags: %xh", devid->aux_fw_rev[3]);

      printf("Current Image Type         : ");
      switch( (devid->aux_fw_rev[3] & 0x03) )
      {
         case 0:
            printf("Recovery\n");
         break;

         case 1:
            printf("Operational Image 1\n");
         break;

         case 2:
            printf("Operational Image 2\n");
         break;

         case 3:
         default:
            printf("Unknown\n");
         break;
      }
   }
   else
   {
         printf("Supported ME not found\n");
   }

   if(rc == IME_SUCCESS)
   {
      rc = ImeUpdateGetStatus(intf, &status);
   
      if(rc == IME_SUCCESS)
      {
         rc = ImeUpdateGetCapabilities(intf, &caps);
      }

   }
   
   if(rc == IME_SUCCESS)
   {
      uint8_t newImage  = ((status.image_status >> 1) & 0x01);
      uint8_t rollImage = ((status.image_status >> 2) & 0x01);
      uint8_t runArea   = ((status.image_status >> 3) & 0x03);
      uint8_t rollSup   = ((caps.special_caps   >> 0) & 0x01);
      uint8_t recovSup  = ((caps.special_caps   >> 1) & 0x01);

      uint8_t operSup   = ((caps.area_supported   >> 1) & 0x01);
      uint8_t piaSup    = ((caps.area_supported   >> 2) & 0x01);
      uint8_t sdrSup    = ((caps.area_supported   >> 3) & 0x01);

      printf("\nSupported Area\n");
      printf("   Operation Code          : %s\n", (operSup ? "Supported" : "Unsupported"));
      printf("   PIA                     : %s\n", (piaSup ? "Supported" : "Unsupported"));
      printf("   SDR                     : %s\n", (sdrSup ? "Supported" : "Unsupported"));

      printf("\nSpecial Capabilities\n");
      printf("   Rollback                : %s\n", (rollSup ? "Supported" : "Unsupported"));
      printf("   Recovery                : %s\n", (recovSup ? "Supported" : "Unsupported"));

      printf("\nImage Status\n");
      printf("   Staging (new)           : %s\n", (newImage ? "Valid" : "Invalid"));
      printf("   Rollback                : %s\n", (rollImage ? "Valid" : "Invalid"));
      if(runArea == 0)
         printf("   Running Image Area      : CODE\n");
      else
         printf("   Running Image Area      : CODE%d\n", runArea);

  }

   return rc;
}


static int ImeUpgrade(struct ipmi_intf *intf, char* imageFilename)
{
   int rc = IME_SUCCESS;
   tImeUpdateImageCtx imgCtx;
   tImeStatus imeStatus;
   time_t start,end,current;
   
   time(&start);

   memset(&imgCtx, 0, sizeof(tImeUpdateImageCtx));

   rc = ImeImageCtxFromFile(imageFilename, &imgCtx);

   if(
      (rc == IME_ERROR) ||
      (imgCtx.pData == NULL) ||
      (imgCtx.size == 0)
     )
   {
      return IME_ERROR;
   }

   ImeUpdateGetStatus(intf,&imeStatus);

   if(rc == IME_SUCCESS)
   {
      rc = ImeUpdatePrepare(intf);
      ImeUpdateGetStatus(intf,&imeStatus);
   }

   if(
      (rc == IME_SUCCESS) &&
      (imeStatus.update_state == IME_STATE_UPDATE_REQUESTED) 
     )
   {
      rc = ImeUpdateOpenArea(intf);
      ImeUpdateGetStatus(intf,&imeStatus);
   }
   else if(rc == IME_SUCCESS)
   {
      lprintf(LOG_ERROR,"ME state error (%i), aborting", imeStatus.update_state);
      rc = IME_ERROR;
   }


   if(
      (rc == IME_SUCCESS) &&
      (imeStatus.update_state == IME_STATE_UPDATE_IN_PROGRESS) 
     )
   {
      uint8_t sequence = 0;
      uint32_t counter = 0;
      uint8_t retry = 0;
      uint8_t shownPercent = 0xff;

      while(   
            (counter < imgCtx.size) && 
            (rc == IME_SUCCESS) &&
            (retry < IME_RETRY_COUNT)
           )
      {
         uint8_t length = IME_UPGRADE_BUFFER_SIZE;
         uint8_t currentPercent;

         if( (imgCtx.size - counter) < IME_UPGRADE_BUFFER_SIZE )
         {
            length = (imgCtx.size - counter);
         }

         rc = ImeUpdateWriteArea(intf,sequence,length,&imgCtx.pData[counter]);
         
         /*
         As per the flowchart Intel Dynamic Power Node Manager 1.5 IPMI Iface
         page 65
         We shall send the GetStatus command each time following a write area
         but this add too much time to the upgrade
         */   
         /*  ImeUpdateGetStatus(intf,&imeStatus); */
         counter += length;
         sequence ++;


         currentPercent = ((float)counter/imgCtx.size)*100;

         if(currentPercent != shownPercent)
         {
            shownPercent = currentPercent;
            printf("Percent: %02i,  ", shownPercent);
            time(&current);
            printf("Elapsed time %02ld:%02ld\r",((current-start)/60), ((current-start)%60));
            fflush(stdout);

         }
      }
      ImeUpdateGetStatus(intf,&imeStatus);
      printf("\n");
   }
   else if(rc == IME_SUCCESS)
   {
      lprintf(LOG_ERROR,"ME state error (%i), aborting", imeStatus.update_state);
      rc = IME_ERROR;
   }

   if(
      (rc == IME_SUCCESS) &&
      (imeStatus.update_state == IME_STATE_UPDATE_IN_PROGRESS) 
     )
   {
      rc = ImeUpdateCloseArea(intf, imgCtx.size, imgCtx.crc8);
      ImeUpdateGetStatus(intf,&imeStatus);
   }
   else if(rc == IME_SUCCESS)
   {
      lprintf(LOG_ERROR,"ME state error, aborting");
      rc = IME_ERROR;
   }

   if(
      (rc == IME_SUCCESS) &&
      (imeStatus.update_state == IME_STATE_UPDATE_REQUESTED) 
     )
   {
      printf("UpdateCompleted, Activate now\n");
      rc = ImeUpdateRegisterUpdate(intf, IME_UPDTYPE_NORMAL);
      ImeUpdateGetStatus(intf,&imeStatus);
   }
   else if(rc == IME_SUCCESS)
   {
      lprintf(LOG_ERROR,"ME state error, aborting");
      rc = IME_ERROR;
   }

   if(
      (rc == IME_SUCCESS) &&
      (imeStatus.update_state == IME_STATE_SUCCESS) 
     )
   {
      time(&end);
      printf("Update Completed in %02ld:%02ld\n",(end-start)/60, (end-start)%60);
   }
   else
   {
      time(&end);
      printf("Update Error\n");
      printf("\nTime Taken %02ld:%02ld\n",(end-start)/60, (end-start)%60);
   }

   return rc;
}


static int ImeUpdatePrepare(struct ipmi_intf *intf)
{
   struct ipmi_rs * rsp;
   struct ipmi_rq req;

   #ifdef OUTPUT_DEBUG
   printf("ImeUpdatePrepare\n");
   #endif

   memset(&req, 0, sizeof(req));
   req.msg.netfn = 0x30;  // OEM NetFn
   req.msg.cmd = 0xA0;
   req.msg.data_len = 0;

   rsp = intf->sendrecv(intf, &req);
   if (rsp == NULL) {
      lprintf(LOG_ERR, "UpdatePrepare command failed");
      return IME_ERROR;
   }
   if (rsp->ccode > 0) {
      lprintf(LOG_ERR, "UpdatePrepare command failed: %s",
         val2str(rsp->ccode, completion_code_vals));
      return IME_ERROR;
   }

   lprintf(LOG_DEBUG, "UpdatePrepare command succeed");
   return IME_SUCCESS;
}

static int ImeUpdateOpenArea(struct ipmi_intf *intf)
{
   struct ipmi_rs * rsp;
   struct ipmi_rq req;
   uint8_t buffer[ 2 ];

   #ifdef OUTPUT_DEBUG
   printf("ImeUpdateOpenArea\n");
   #endif

   memset(&req, 0, sizeof(req));
   req.msg.netfn = 0x30;  // OEM NetFn
   req.msg.cmd = 0xA1;

   buffer[0] = 0x01; // Area Type : Operational code
   buffer[1] = 0x00; // Reserved : 0
   req.msg.data = buffer;

   req.msg.data_len = 2;

   rsp = intf->sendrecv(intf, &req);
   if (rsp == NULL) {
      lprintf(LOG_ERR, "UpdateOpenArea command failed");
      return IME_ERROR;
   }
   if (rsp->ccode > 0) {
      lprintf(LOG_ERR, "UpdateOpenArea command failed: %s",
         val2str(rsp->ccode, completion_code_vals));
      return IME_ERROR;
   }

   lprintf(LOG_DEBUG, "UpdateOpenArea command succeed");
   return IME_SUCCESS;
}

static int ImeUpdateWriteArea(
                              struct ipmi_intf *intf,
                              uint8_t sequence, 
                              uint8_t length, 
                              uint8_t * pBuf
                          )
{
   struct ipmi_rs * rsp;
   struct ipmi_rq req;
   uint8_t buffer[ IME_UPGRADE_BUFFER_SIZE + 1 ];

//   printf("ImeUpdateWriteArea %i\n", sequence);

   if(length > IME_UPGRADE_BUFFER_SIZE)
      return IME_ERROR;

   buffer[0] = sequence;
   memcpy(&buffer[1], pBuf, length);

   memset(&req, 0, sizeof(req));
   req.msg.netfn = 0x30;  // OEM NetFn
   req.msg.cmd = 0xA2;
   req.msg.data = buffer;
   req.msg.data_len = length + 1;

   rsp = intf->sendrecv(intf, &req);
   if (rsp == NULL) {
      lprintf(LOG_ERR, "UpdateWriteArea command failed");
      return IME_ERROR;
   }
   if (rsp->ccode > 0) {
      lprintf(LOG_ERR, "UpdateWriteArea command failed: %s",
         val2str(rsp->ccode, completion_code_vals));
      if( rsp->ccode == 0x80) // restart operation
         return IME_RESTART;
      else
         return IME_ERROR;
   }

   lprintf(LOG_DEBUG, "UpdateWriteArea command succeed");
   return IME_SUCCESS;
}

static int ImeUpdateCloseArea(
                              struct ipmi_intf *intf,
                              uint32_t size, 
                              uint16_t checksum
                          )
{
   struct ipmi_rs * rsp;
   struct ipmi_rq req;
   uint8_t length = sizeof( uint32_t ) + sizeof( uint16_t ); 
   uint8_t buffer[ sizeof( uint32_t ) + sizeof( uint16_t ) ];

   #ifdef OUTPUT_DEBUG
   printf( "ImeUpdateCloseArea\n");
   #endif

   buffer[0] = (uint8_t)((size & 0x000000ff) >>  0);
   buffer[1] = (uint8_t)((size & 0x0000ff00) >>  8);
   buffer[2] = (uint8_t)((size & 0x00ff0000) >> 16);
   buffer[3] = (uint8_t)((size & 0xff000000) >> 24);

   buffer[4] = (uint8_t)((checksum & 0x00ff) >>  0);
   buffer[5] = (uint8_t)((checksum & 0xff00) >>  8);

   memset(&req, 0, sizeof(req));
   req.msg.netfn = 0x30;  // OEM NetFn
   req.msg.cmd = 0xA3;
   req.msg.data = buffer;
   req.msg.data_len = length;

   rsp = intf->sendrecv(intf, &req);
   if (rsp == NULL) {
      lprintf(LOG_ERR, "UpdateCloseArea command failed");
      return IME_ERROR;
   }
   if (rsp->ccode > 0) {
      lprintf(LOG_ERR, "UpdateCloseArea command failed: %s",
         val2str(rsp->ccode, completion_code_vals));
      return IME_ERROR;
   }

   lprintf(LOG_DEBUG, "UpdateCloseArea command succeed");
   return IME_SUCCESS;
}

static int ImeUpdateGetStatus(struct ipmi_intf *intf, tImeStatus *pStatus )
{
   struct      ipmi_rs * rsp;
   struct      ipmi_rq req;
   tImeStatus *pGetStatus;
   
   memset(pStatus, 0, sizeof(tImeStatus));
   pStatus->update_state = IME_STATE_ABORTED;


   #ifdef OUTPUT_DEBUG
   printf("ImeUpdateGetStatus: ");
   #endif

   memset(&req, 0, sizeof(req));
   req.msg.netfn = 0x30;  // OEM NetFn
   req.msg.cmd = 0xA6;
   req.msg.data_len = 0;

   rsp = intf->sendrecv(intf, &req);
   if (rsp == NULL) {
      lprintf(LOG_ERR, "UpdatePrepare command failed");
      return IME_ERROR;
   }
   if (rsp->ccode > 0) {
      lprintf(LOG_ERR, "UpdatePrepare command failed: %s",
         val2str(rsp->ccode, completion_code_vals));
      return IME_ERROR;
   }

   lprintf(LOG_DEBUG, "UpdatePrepare command succeed");

   pGetStatus = (tImeStatus *) rsp->data;
   
   memcpy( pStatus, pGetStatus, sizeof(tImeStatus));

   #ifdef OUTPUT_DEBUG
   printf("%x - ", pStatus->updateState);

   switch( pStatus->update_state )
   {
      case IME_STATE_IDLE:
         printf("IDLE\n");
      break;
      case IME_STATE_UPDATE_REQUESTED:
         printf("Update Requested\n");
      break;
      case IME_STATE_UPDATE_IN_PROGRESS:
         printf("Update in Progress\n");
      break;
      case IME_STATE_SUCCESS:
         printf("Update Success\n");
      break;
      case IME_STATE_FAILED:
         printf("Update Failed\n");
      break;
      case IME_STATE_ROLLED_BACK:
         printf("Update Rolled Back\n");
      break;
      case IME_STATE_ABORTED:
         printf("Update Aborted\n");
      break;
      case IME_STATE_INIT_FAILED:
         printf("Update Init Failed\n");
      break;
      default:
         printf("Unknown, reserved\n");
      break;
   }
   #endif

   return IME_SUCCESS;
}

static int ImeUpdateGetCapabilities(struct ipmi_intf *intf, tImeCaps *pCaps )
{
   struct      ipmi_rs * rsp;
   struct      ipmi_rq req;
   tImeCaps *  pGetCaps;
   
   memset(pCaps, 0, sizeof(tImeCaps));


   #ifdef OUTPUT_DEBUG
   printf("ImeUpdateGetStatus: ");
   #endif

   memset(&req, 0, sizeof(req));
   req.msg.netfn = 0x30;  // OEM NetFn
   req.msg.cmd = 0xA7;
   req.msg.data_len = 0;

   rsp = intf->sendrecv(intf, &req);
   if (rsp == NULL) {
      lprintf(LOG_ERR, "UpdatePrepare command failed");
      return IME_ERROR;
   }
   if (rsp->ccode > 0) {
      lprintf(LOG_ERR, "UpdatePrepare command failed: %s",
         val2str(rsp->ccode, completion_code_vals));
      return IME_ERROR;
   }

   lprintf(LOG_DEBUG, "UpdatePrepare command succeed");

   pGetCaps = (tImeCaps *) rsp->data;
   
   memcpy( pCaps, pGetCaps, sizeof(tImeCaps));

   return IME_SUCCESS;
}


static int ImeUpdateRegisterUpdate(struct ipmi_intf *intf, tImeUpdateType type)
{
   struct ipmi_rs * rsp;
   struct ipmi_rq req;
   uint8_t buffer[ 2 ];

   #ifdef OUTPUT_DEBUG
   printf( "ImeUpdateRegisterUpdate\n");
   #endif

   buffer[0] = type;  // Normal Update
   buffer[1] = 0;  // Flags, reserved

   memset(&req, 0, sizeof(req));
   req.msg.netfn = 0x30;  // OEM NetFn
   req.msg.cmd = 0xA4;
   req.msg.data = buffer;
   req.msg.data_len = 2;

   rsp = intf->sendrecv(intf, &req);
   if (rsp == NULL) {
      lprintf(LOG_ERR, "ImeUpdateRegisterUpdate command failed");
      return IME_ERROR;
   }
   if (rsp->ccode > 0) {
      lprintf(LOG_ERR, "ImeUpdateRegisterUpdate command failed: %s",
         val2str(rsp->ccode, completion_code_vals));
      return IME_ERROR;
   }

   lprintf(LOG_DEBUG, "ImeUpdateRegisterUpdate command succeed");
   return IME_SUCCESS;
}




static int ImeUpdateShowStatus(struct ipmi_intf *intf)
{
   struct ipmi_rs * rsp;
   struct ipmi_rq req;
   tImeStatus *pStatus;

   printf("ImeUpdateGetStatus: ");

   memset(&req, 0, sizeof(req));
   req.msg.netfn = 0x30;  // OEM NetFn
   req.msg.cmd = 0xA6;
   req.msg.data_len = 0;

   rsp = intf->sendrecv(intf, &req);
   if (rsp == NULL) {
      lprintf(LOG_ERR, "UpdatePrepare command failed");
      return IME_ERROR;
   }
   if (rsp->ccode > 0) {
      lprintf(LOG_ERR, "UpdatePrepare command failed: %s",
         val2str(rsp->ccode, completion_code_vals));
      return IME_ERROR;
   }

   lprintf(LOG_DEBUG, "UpdatePrepare command succeed");

   pStatus = (tImeStatus *) rsp->data ;

   
   printf("image_status: %x - ", pStatus->image_status);

   printf("update_state: %x - ", pStatus->update_state);

   switch( pStatus->update_state )
   {
      case IME_STATE_IDLE:
         printf("IDLE\n");
      break;
      case IME_STATE_UPDATE_REQUESTED:
         printf("Update Requested\n");
      break;
      case IME_STATE_UPDATE_IN_PROGRESS:
         printf("Update in Progress\n");
      break;
      case IME_STATE_SUCCESS:
         printf("Update Success\n");
      break;
      case IME_STATE_FAILED:
         printf("Update Failed\n");
      break;
      case IME_STATE_ROLLED_BACK:
         printf("Update Rolled Back\n");
      break;
      case IME_STATE_ABORTED:
         printf("Update Aborted\n");
      break;
      case IME_STATE_INIT_FAILED:
         printf("Update Init Failed\n");
      break;
      default:
         printf("Unknown, reserved\n");
      break;
   }
   printf("update_attempt_status  : %x\n", pStatus->update_attempt_status);
   printf("rollback_attempt_status: %x\n", pStatus->rollback_attempt_status);
   printf("update_type            : %x\n", pStatus->update_type);
   printf("dependent_flag         : %x\n", pStatus->dependent_flag);
   printf("free_area_size         : %x\n", pStatus->free_area_size[0]);
   printf("                       : %x\n", pStatus->free_area_size[1]);
   printf("                       : %x\n", pStatus->free_area_size[2]);
   printf("                       : %x\n", pStatus->free_area_size[3]);

   return IME_SUCCESS;
}


static int ImeImageCtxFromFile(
                                 char* imageFilename, 
                                 tImeUpdateImageCtx * pImageCtx
                               )
{
   int rc = IME_SUCCESS;
   FILE* pImageFile = fopen(imageFilename, "rb");
   
   if ( pImageFile == NULL )
   {
      lprintf(LOG_NOTICE,"Cannot open image file %s", imageFilename);
      rc = IME_ERROR;
   }
   
   if ( rc == IME_SUCCESS )
   {
      /* Get the raw data in file */
      fseek(pImageFile, 0, SEEK_END);
      pImageCtx->size  = ftell(pImageFile); 
      if (pImageCtx->size <= 0) {
         if (pImageCtx->size < 0)
            lprintf(LOG_ERR, "Error seeking %s. %s\n", imageFilename, strerror(errno));
         rc = IME_ERROR;
         fclose(pImageFile);
         return rc;
      }
      pImageCtx->pData = malloc(sizeof(unsigned char)*pImageCtx->size);
      rewind(pImageFile);

      if ( pImageCtx->pData != NULL )
      {
         if (pImageCtx->size < fread(pImageCtx->pData, sizeof(unsigned char), 
                                                   pImageCtx->size, pImageFile))
            rc = IME_ERROR;
      }
      else
      {
         rc = IME_ERROR;
      }
   }
   
   // Calculate checksum CRC8  
   if ( rc == IME_SUCCESS )
   {
      pImageCtx->crc8 = ImeCrc8(pImageCtx->size, pImageCtx->pData);
   }

    
   if( pImageFile != NULL)
   {
      fclose(pImageFile);
   }
    
   return rc;
}  

static uint8_t ImeCrc8( uint32_t length, uint8_t * pBuf )
{
   uint8_t crc = 0;
   uint32_t bufCount;

   for ( bufCount = 0; bufCount < length; bufCount++ )
   {
      uint8_t count;

      crc = crc ^ pBuf[bufCount];
  
      for ( count = 0; count < 8; count++ ) 
      {
         if (( crc & 0x80 ) != 0 )
         {
            crc <<= 1;
            crc ^= 0x07;
         }
         else
         {
            crc <<= 1;
         }
      }
   }

   lprintf(LOG_DEBUG,"CRC8: %02xh\n", crc);
   return crc;
} 


static int ImeManualRollback(struct ipmi_intf *intf)
{
   int rc = IME_SUCCESS;
   tImeStatus imeStatus;

   rc = ImeUpdateRegisterUpdate(intf, IME_UPDTYPE_MANUAL_ROLLBACK);
   ImeUpdateGetStatus(intf,&imeStatus);


   if(
      (rc == IME_SUCCESS) &&
      (imeStatus.update_state == IME_STATE_ROLLED_BACK) 
     )
   {
      printf("Manual Rollback Succeed\n");
      return IME_SUCCESS;
   }
   else
   {
      printf("Manual Rollback Completed With Error\n");
      return IME_ERROR;
   }
}



static void ImePrintUsage(void)
{
   lprintf(LOG_NOTICE,"help                    - This help menu");   
   lprintf(LOG_NOTICE,"info                    - Information about the present Intel ME");   
   lprintf(LOG_NOTICE,"update <file>           - Upgrade the ME firmware from received image <file>");
   lprintf(LOG_NOTICE,"rollback                - Manual Rollback ME");
//   lprintf(LOG_NOTICE,"rollback                - Rollback ME Firmware");
}



int ipmi_ime_main(struct ipmi_intf * intf, int argc, char ** argv)
{
   int rc = IME_SUCCESS;
   
   lprintf(LOG_DEBUG,"ipmi_ime_main()");
   

   if ( (argc == 0) || (strcmp(argv[0], "help") == 0) ) 
   {
      ImePrintUsage();
   }
   else if ( (argc == 0) || (strcmp(argv[0], "info") == 0) ) 
   {
      rc = ImeGetInfo(intf);
   }
   else if ( strcmp(argv[0], "update") == 0) 
   {
      if(argc == 2)
      {
         lprintf(LOG_NOTICE,"Update using file: %s", argv[1]);
         rc = ImeUpgrade(intf, argv[1]);
      }
      else
      {
         lprintf(LOG_ERROR,"File must be provided with this option, see help\n");
         rc = IME_ERROR;
      }
   }
   else if ( (argc == 0) || (strcmp(argv[0], "rollback") == 0) ) 
   {
      rc = ImeManualRollback(intf);
   }
   else
   {
      ImePrintUsage(); 
   }
   
   return rc;
}