-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsmbiosmdrv2handler.cpp
More file actions
1334 lines (1153 loc) · 41.7 KB
/
smbiosmdrv2handler.cpp
File metadata and controls
1334 lines (1153 loc) · 41.7 KB
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
/*
// Copyright (c) 2018 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <commandutils.hpp>
#include <ipmid/api.hpp>
#include <ipmid/utils.hpp>
#include <oemcommands.hpp>
#include <phosphor-logging/log.hpp>
#include <sdbusplus/message/types.hpp>
#include <smbiosmdrv2handler.hpp>
#include <xyz/openbmc_project/Common/error.hpp>
#include <cstdint>
#include <fstream>
#include <string>
#include <vector>
std::unique_ptr<MDRV2> mdrv2 = nullptr;
static constexpr const uint8_t ccOemInvalidChecksum = 0x85;
static constexpr size_t dataInfoSize = 16;
static constexpr const uint8_t ccStorageLeak = 0xC4;
static void register_netfn_smbiosmdrv2_functions() __attribute__((constructor));
int MDRV2::agentLookup(const uint16_t& agentId)
{
int agentIndex = -1;
if (lastAgentId == agentId)
{
return lastAgentIndex;
}
if (agentId == smbiosAgentId)
{
return firstAgentIndex;
}
return agentIndex;
}
int MDRV2::sdplusMdrv2GetProperty(const std::string& name,
ipmi::DbusVariant& value,
const std::string& service)
{
std::shared_ptr<sdbusplus::asio::connection> bus = getSdBus();
sdbusplus::message_t method =
bus->new_method_call(service.c_str(), mdrv2Path, dbusProperties, "Get");
method.append(mdrv2Interface, name);
sdbusplus::message_t reply = bus->call(method);
try
{
sdbusplus::message_t reply = bus->call(method);
reply.read(value);
}
catch (const sdbusplus::exception_t& e)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Error get property, sdbusplus call failed",
phosphor::logging::entry("ERROR=%s", e.what()));
return -1;
}
return 0;
}
int MDRV2::syncDirCommonData(uint8_t idIndex, uint32_t size,
const std::string& service)
{
std::vector<uint32_t> commonData;
std::shared_ptr<sdbusplus::asio::connection> bus = getSdBus();
sdbusplus::message_t method =
bus->new_method_call(service.c_str(), mdrv2Path, mdrv2Interface,
"SynchronizeDirectoryCommonData");
method.append(idIndex, size);
try
{
sdbusplus::message_t reply = bus->call(method);
reply.read(commonData);
}
catch (const sdbusplus::exception_t& e)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Error sync dir common data with service",
phosphor::logging::entry("ERROR=%s", e.what()));
return -1;
}
if (commonData.size() < syncDirCommonSize)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Error sync dir common data - data length invalid");
return -1;
}
smbiosDir.dir[idIndex].common.dataSetSize = commonData.at(0);
smbiosDir.dir[idIndex].common.dataVersion = commonData.at(1);
smbiosDir.dir[idIndex].common.timestamp = commonData.at(2);
return 0;
}
int MDRV2::findDataId(const uint8_t* dataInfo, const size_t& len,
const std::string& service)
{
int idIndex = -1;
if (dataInfo == nullptr)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Error dataInfo, input is null point");
return -1;
}
std::shared_ptr<sdbusplus::asio::connection> bus = getSdBus();
sdbusplus::message_t method = bus->new_method_call(
service.c_str(), mdrv2Path, mdrv2Interface, "FindIdIndex");
std::vector<uint8_t> info;
info.resize(len);
std::copy(dataInfo, dataInfo + len, info.data());
method.append(info);
try
{
sdbusplus::message_t reply = bus->call(method);
reply.read(idIndex);
}
catch (const sdbusplus::exception_t& e)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Error find id index",
phosphor::logging::entry("ERROR=%s", e.what()),
phosphor::logging::entry("SERVICE=%s", service.c_str()),
phosphor::logging::entry("PATH=%s", mdrv2Path));
return -1;
}
return idIndex;
}
uint16_t MDRV2::getSessionHandle(Mdr2DirStruct* dir)
{
if (dir == NULL)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Empty dir point");
return 0;
}
dir->sessionHandle++;
if (dir->sessionHandle == 0)
{
dir->sessionHandle = 1;
}
return dir->sessionHandle;
}
int MDRV2::findLockHandle(const uint16_t& lockHandle)
{
int idIndex = -1;
for (int index = 0; index < smbiosDir.dirEntries; index++)
{
if (lockHandle == smbiosDir.dir[index].lockHandle)
{
return index;
}
}
return idIndex;
}
bool MDRV2::smbiosIsUpdating(uint8_t index)
{
if (index >= maxDirEntries)
{
return false;
}
if (smbiosDir.dir[index].stage == MDR2SMBIOSStatusEnum::mdr2Updating)
{
return true;
}
return false;
}
uint32_t MDRV2::calcChecksum32(uint8_t* buf, uint32_t len)
{
uint32_t sum = 0;
if (buf == nullptr)
{
return invalidChecksum;
}
for (uint32_t index = 0; index < len; index++)
{
sum += buf[index];
}
return sum;
}
/** @brief implements mdr2 agent status command
* @param agentId
* @param dirVersion
*
* @returns IPMI completion code plus response data
* - mdrVersion
* - agentVersion
* - dirVersion
* - dirEntries
* - dataRequest
*/
ipmi::RspType<uint8_t, uint8_t, uint8_t, uint8_t, uint8_t>
mdr2AgentStatus(uint16_t agentId, uint8_t dirVersion)
{
if (mdrv2 == nullptr)
{
mdrv2 = std::make_unique<MDRV2>();
}
int agentIndex = mdrv2->agentLookup(agentId);
if (agentIndex == -1)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Unknown agent id", phosphor::logging::entry("ID=%x", agentId));
return ipmi::responseParmOutOfRange();
}
constexpr uint8_t mdrVersion = mdr2Version;
constexpr uint8_t agentVersion = smbiosAgentVersion;
uint8_t dirVersionResp = mdrv2->smbiosDir.dirVersion;
uint8_t dirEntries = mdrv2->smbiosDir.dirEntries;
uint8_t dataRequest;
if (mdrv2->smbiosDir.remoteDirVersion != dirVersion)
{
mdrv2->smbiosDir.remoteDirVersion = dirVersion;
dataRequest =
static_cast<uint8_t>(DirDataRequestEnum::dirDataRequested);
}
else
{
dataRequest =
static_cast<uint8_t>(DirDataRequestEnum::dirDataNotRequested);
}
return ipmi::responseSuccess(mdrVersion, agentVersion, dirVersionResp,
dirEntries, dataRequest);
}
/** @brief implements mdr2 get directory command
* @param agentId
* @param dirIndex
* @returns IPMI completion code plus response data
* - dataOut
*/
ipmi::RspType<std::vector<uint8_t>> mdr2GetDir(uint16_t agentId,
uint8_t dirIndex)
{
std::shared_ptr<sdbusplus::asio::connection> bus = getSdBus();
std::string service = ipmi::getService(*bus, mdrv2Interface, mdrv2Path);
if (mdrv2 == nullptr)
{
mdrv2 = std::make_unique<MDRV2>();
}
int agentIndex = mdrv2->agentLookup(agentId);
if (agentIndex == -1)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Unknown agent id", phosphor::logging::entry("ID=%x", agentId));
return ipmi::responseParmOutOfRange();
}
ipmi::DbusVariant value = static_cast<uint8_t>(0);
if (0 != mdrv2->sdplusMdrv2GetProperty("DirectoryEntries", value, service))
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Error getting DirEnries");
return ipmi::responseUnspecifiedError();
}
if (std::get<uint8_t>(value) == 0)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Error getting directory entries",
phosphor::logging::entry("VALUE=%x", std::get<uint8_t>(value)));
return ipmi::responseUnspecifiedError();
}
if (dirIndex > std::get<uint8_t>(value))
{
return ipmi::responseParmOutOfRange();
}
sdbusplus::message_t method = bus->new_method_call(
service.c_str(), mdrv2Path, mdrv2Interface, "GetDirectoryInformation");
method.append(dirIndex);
std::vector<uint8_t> dataOut;
try
{
sdbusplus::message_t reply = bus->call(method);
reply.read(dataOut);
}
catch (const sdbusplus::exception_t& e)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Error get dir", phosphor::logging::entry("ERROR=%s", e.what()),
phosphor::logging::entry("SERVICE=%s", service.c_str()),
phosphor::logging::entry("PATH=%s", mdrv2Path));
return ipmi::responseResponseError();
}
constexpr size_t getDirRespSize = 6;
if (dataOut.size() < getDirRespSize)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Error get dir, response length invalid");
return ipmi::responseUnspecifiedError();
}
if (dataOut.size() > MAX_IPMI_BUFFER) // length + completion code should no
// more than MAX_IPMI_BUFFER
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Data length send from service is invalid");
return ipmi::responseResponseError();
}
return ipmi::responseSuccess(dataOut);
}
/** @brief implements mdr2 send directory info command
* @param agentId
* @param dirVersion
* @param dirIndex
* @param returnedEntries
* @param remainingEntries
* @param dataInfo
* dataInfo is 32 Bytes in size and contains below parameters
* - dataInfo, size, dataSetSize, dataVersion, timestamp
*
* @returns IPMI completion code plus response data
* - bool
*/
ipmi::RspType<bool> mdr2SendDir(uint16_t agentId, uint8_t dirVersion,
uint8_t dirIndex, uint8_t returnedEntries,
uint8_t remainingEntries,
std::vector<uint8_t> dataInfo)
{
if ((static_cast<size_t>(returnedEntries) * dataInfoSize) !=
dataInfo.size())
{
return ipmi::responseReqDataLenInvalid();
}
std::shared_ptr<sdbusplus::asio::connection> bus = getSdBus();
std::string service = ipmi::getService(*bus, mdrv2Interface, mdrv2Path);
if (mdrv2 == nullptr)
{
mdrv2 = std::make_unique<MDRV2>();
}
int agentIndex = mdrv2->agentLookup(agentId);
if (agentIndex == -1)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Unknown agent id", phosphor::logging::entry("ID=%x", agentId));
return ipmi::responseParmOutOfRange();
}
if ((dirIndex + returnedEntries) > maxDirEntries)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Too many directory entries");
return ipmi::response(ccStorageLeak);
}
sdbusplus::message_t method = bus->new_method_call(
service.c_str(), mdrv2Path, mdrv2Interface, "SendDirectoryInformation");
method.append(dirVersion, dirIndex, returnedEntries, remainingEntries,
dataInfo);
bool terminate = false;
try
{
sdbusplus::message_t reply = bus->call(method);
reply.read(terminate);
}
catch (const sdbusplus::exception_t& e)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Error send dir", phosphor::logging::entry("ERROR=%s", e.what()),
phosphor::logging::entry("SERVICE=%s", service.c_str()),
phosphor::logging::entry("PATH=%s", mdrv2Path));
return ipmi::responseResponseError();
}
return ipmi::responseSuccess(terminate);
}
/** @brief implements mdr2 get data info command
* @param agentId
* @param dataInfo
*
* @returns IPMI completion code plus response data
* - response - mdrVersion, data info, validFlag,
* dataLength, dataVersion, timeStamp
*/
ipmi::RspType<std::vector<uint8_t>>
mdr2GetDataInfo(uint16_t agentId, std::vector<uint8_t> dataInfo)
{
constexpr size_t getDataInfoReqSize = 16;
if (dataInfo.size() < getDataInfoReqSize)
{
return ipmi::responseReqDataLenInvalid();
}
std::shared_ptr<sdbusplus::asio::connection> bus = getSdBus();
std::string service = ipmi::getService(*bus, mdrv2Interface, mdrv2Path);
if (mdrv2 == nullptr)
{
mdrv2 = std::make_unique<MDRV2>();
}
int agentIndex = mdrv2->agentLookup(agentId);
if (agentIndex == -1)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Unknown agent id", phosphor::logging::entry("ID=%x", agentId));
return ipmi::responseParmOutOfRange();
}
int idIndex = mdrv2->findDataId(dataInfo.data(), dataInfo.size(), service);
if ((idIndex < 0) || (idIndex >= maxDirEntries))
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Invalid Data ID", phosphor::logging::entry("IDINDEX=%x", idIndex));
return ipmi::responseParmOutOfRange();
}
sdbusplus::message_t method = bus->new_method_call(
service.c_str(), mdrv2Path, mdrv2Interface, "GetDataInformation");
method.append(static_cast<uint8_t>(idIndex));
std::vector<uint8_t> res;
try
{
sdbusplus::message_t reply = bus->call(method);
reply.read(res);
}
catch (const sdbusplus::exception_t& e)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Error get data info",
phosphor::logging::entry("ERROR=%s", e.what()),
phosphor::logging::entry("SERVICE=%s", service.c_str()),
phosphor::logging::entry("PATH=%s", mdrv2Path));
return ipmi::responseResponseError();
}
if (res.size() != sizeof(MDRiiGetDataInfoResponse))
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Get data info response length not invalid");
return ipmi::responseResponseError();
}
return ipmi::responseSuccess(res);
}
/** @brief implements mdr2 data info offer command
* @param agentId - Offer a agent ID to get the "Data Set ID"
*
* @returns IPMI completion code plus response data
* - dataOut - data Set Id
*/
ipmi::RspType<std::vector<uint8_t>> mdr2DataInfoOffer(uint16_t agentId)
{
std::shared_ptr<sdbusplus::asio::connection> bus = getSdBus();
std::string service = ipmi::getService(*bus, mdrv2Interface, mdrv2Path);
if (mdrv2 == nullptr)
{
mdrv2 = std::make_unique<MDRV2>();
}
int agentIndex = mdrv2->agentLookup(agentId);
if (agentIndex == -1)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Unknown agent id", phosphor::logging::entry("ID=%x", agentId));
return ipmi::responseParmOutOfRange();
}
sdbusplus::message_t method = bus->new_method_call(
service.c_str(), mdrv2Path, mdrv2Interface, "GetDataOffer");
std::vector<uint8_t> dataOut;
try
{
sdbusplus::message_t reply = bus->call(method);
reply.read(dataOut);
}
catch (const sdbusplus::exception_t& e)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Error send data info offer",
phosphor::logging::entry("ERROR=%s", e.what()),
phosphor::logging::entry("SERVICE=%s", service.c_str()),
phosphor::logging::entry("PATH=%s", mdrv2Path));
return ipmi::responseResponseError();
}
constexpr size_t respInfoSize = 16;
if (dataOut.size() != respInfoSize)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Error send data info offer, return length invalid");
return ipmi::responseUnspecifiedError();
}
return ipmi::responseSuccess(dataOut);
}
/** @brief implements mdr2 send data info command
* @param agentId
* @param dataInfo
* @param validFlag
* @param dataLength
* @param dataVersion
* @param timeStamp
*
* @returns IPMI completion code plus response data
* - bool
*/
ipmi::RspType<bool> mdr2SendDataInfo(uint16_t agentId,
std::array<uint8_t, dataInfoSize> dataInfo,
uint8_t validFlag, uint32_t dataLength,
uint32_t dataVersion, uint32_t timeStamp)
{
if (dataLength > smbiosTableStorageSize)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Requested data length is out of SMBIOS Table storage size.");
return ipmi::responseParmOutOfRange();
}
std::shared_ptr<sdbusplus::asio::connection> bus = getSdBus();
std::string service = ipmi::getService(*bus, mdrv2Interface, mdrv2Path);
if (mdrv2 == nullptr)
{
mdrv2 = std::make_unique<MDRV2>();
}
int agentIndex = mdrv2->agentLookup(agentId);
if (agentIndex == -1)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Unknown agent id", phosphor::logging::entry("ID=%x", agentId));
return ipmi::responseParmOutOfRange();
}
int idIndex = mdrv2->findDataId(dataInfo.data(), dataInfo.size(), service);
if ((idIndex < 0) || (idIndex >= maxDirEntries))
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Invalid Data ID", phosphor::logging::entry("IDINDEX=%x", idIndex));
return ipmi::responseParmOutOfRange();
}
sdbusplus::message_t method = bus->new_method_call(
service.c_str(), mdrv2Path, mdrv2Interface, "SendDataInformation");
method.append((uint8_t)idIndex, validFlag, dataLength, dataVersion,
timeStamp);
bool entryChanged = true;
try
{
sdbusplus::message_t reply = bus->call(method);
reply.read(entryChanged);
}
catch (const sdbusplus::exception_t& e)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Error send data info",
phosphor::logging::entry("ERROR=%s", e.what()),
phosphor::logging::entry("SERVICE=%s", service.c_str()),
phosphor::logging::entry("PATH=%s", mdrv2Path));
return ipmi::responseResponseError();
}
return ipmi::responseSuccess(entryChanged);
}
/**
@brief This command is MDR related get data block command.
@param - agentId
@param - lockHandle
@param - xferOffset
@param - xferLength
@return on success
- xferLength
- checksum
- data
**/
ipmi::RspType<uint32_t, // xferLength
uint32_t, // Checksum
std::vector<uint8_t> // data
>
mdr2GetDataBlock(uint16_t agentId, uint16_t lockHandle, uint32_t xferOffset,
uint32_t xferLength)
{
if (mdrv2 == nullptr)
{
mdrv2 = std::make_unique<MDRV2>();
}
int agentIndex = mdrv2->agentLookup(agentId);
if (agentIndex == -1)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Unknown agent id", phosphor::logging::entry("ID=%x", agentId));
return ipmi::responseParmOutOfRange();
}
int idIndex = mdrv2->findLockHandle(lockHandle);
if ((idIndex < 0) || (idIndex >= maxDirEntries))
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Invalid Data ID", phosphor::logging::entry("IDINDEX=%x", idIndex));
return ipmi::responseParmOutOfRange();
}
if (xferOffset >= mdrv2->smbiosDir.dir[idIndex].common.size)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Offset is outside of range.");
return ipmi::responseParmOutOfRange();
}
size_t outSize = (xferLength > mdrv2->smbiosDir.dir[idIndex].xferSize)
? mdrv2->smbiosDir.dir[idIndex].xferSize
: xferLength;
if (outSize > UINT_MAX - xferOffset)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Out size and offset are out of range");
return ipmi::responseParmOutOfRange();
}
if ((xferOffset + outSize) > mdrv2->smbiosDir.dir[idIndex].common.size)
{
outSize = mdrv2->smbiosDir.dir[idIndex].common.size - xferOffset;
}
uint32_t respXferLength = outSize;
if (respXferLength > xferLength)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Get data block unexpected error.");
return ipmi::responseUnspecifiedError();
}
if ((xferOffset + outSize) >
UINT_MAX -
reinterpret_cast<size_t>(mdrv2->smbiosDir.dir[idIndex].dataStorage))
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Input data to calculate checksum is out of range");
return ipmi::responseParmOutOfRange();
}
uint32_t u32Checksum = mdrv2->calcChecksum32(
mdrv2->smbiosDir.dir[idIndex].dataStorage + xferOffset, outSize);
if (u32Checksum == invalidChecksum)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Get data block failed - invalid checksum");
return ipmi::response(ccOemInvalidChecksum);
}
std::vector<uint8_t> data(outSize);
std::copy(&mdrv2->smbiosDir.dir[idIndex].dataStorage[xferOffset],
&mdrv2->smbiosDir.dir[idIndex].dataStorage[xferOffset + outSize],
data.begin());
return ipmi::responseSuccess(respXferLength, u32Checksum, data);
}
/** @brief implements mdr2 send data block command
* @param agentId
* @param lockHandle
* @param xferOffset
* @param xferLength
* @param checksum
*
* @returns IPMI completion code
*/
ipmi::RspType<> mdr2SendDataBlock(uint16_t agentId, uint16_t lockHandle,
uint32_t xferOffset, uint32_t xferLength,
uint32_t checksum)
{
if (mdrv2 == nullptr)
{
mdrv2 = std::make_unique<MDRV2>();
}
int agentIndex = mdrv2->agentLookup(agentId);
if (agentIndex == -1)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Unknown agent id", phosphor::logging::entry("ID=%x", agentId));
return ipmi::responseParmOutOfRange();
}
int idIndex = mdrv2->findLockHandle(lockHandle);
if ((idIndex < 0) || (idIndex >= maxDirEntries))
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Invalid Data ID", phosphor::logging::entry("IDINDEX=%x", idIndex));
return ipmi::responseParmOutOfRange();
}
if (mdrv2->smbiosIsUpdating(idIndex))
{
if (xferOffset > UINT_MAX - xferLength)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Offset and length are out of range");
return ipmi::responseParmOutOfRange();
}
if (((xferOffset + xferLength) >
mdrv2->smbiosDir.dir[idIndex].maxDataSize) ||
((xferOffset + xferLength) >
mdrv2->smbiosDir.dir[idIndex].common.dataSetSize))
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Send data block Invalid offset/length");
return ipmi::responseReqDataLenExceeded();
}
if (reinterpret_cast<size_t>(
mdrv2->smbiosDir.dir[idIndex].dataStorage) >
UINT_MAX - xferOffset)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Offset is out of range");
return ipmi::responseParmOutOfRange();
}
uint8_t* destAddr = mdrv2->smbiosDir.dir[idIndex].dataStorage +
xferOffset;
uint8_t* sourceAddr = reinterpret_cast<uint8_t*>(mdrv2->area->vPtr);
uint32_t calcChecksum = mdrv2->calcChecksum32(sourceAddr, xferLength);
if (calcChecksum != checksum)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Send data block Invalid checksum");
return ipmi::response(ccOemInvalidChecksum);
}
else
{
if (reinterpret_cast<size_t>(sourceAddr) > UINT_MAX - xferLength)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Length is out of range");
return ipmi::responseParmOutOfRange();
}
std::copy(sourceAddr, sourceAddr + xferLength, destAddr);
}
}
else
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Send data block failed, other data is updating");
return ipmi::responseDestinationUnavailable();
}
return ipmi::responseSuccess();
}
bool MDRV2::storeDatatoFlash(MDRSMBIOSHeader* mdrHdr, uint8_t* data)
{
std::ofstream smbiosFile(mdrType2File,
std::ios_base::binary | std::ios_base::trunc);
if (!smbiosFile.good())
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Write data from flash error - Open MDRV2 table file failure");
return false;
}
try
{
smbiosFile.write(reinterpret_cast<char*>(mdrHdr),
sizeof(MDRSMBIOSHeader));
smbiosFile.write(reinterpret_cast<char*>(data), mdrHdr->dataSize);
}
catch (const std::ofstream::failure& e)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Write data from flash error - write data error",
phosphor::logging::entry("ERROR=%s", e.what()));
return false;
}
return true;
}
void SharedMemoryArea::Initialize(uint32_t addr, uint32_t areaSize)
{
int memDriver = 0;
// open mem driver for the system memory access
memDriver = open("/dev/vgasharedmem", O_RDONLY);
if (memDriver < 0)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Cannot access mem driver");
throw std::system_error(EIO, std::generic_category());
}
// map the system memory
vPtr = mmap(NULL, // where to map to: don't mind
areaSize, // how many bytes ?
PROT_READ, // want to read and write
MAP_SHARED, // no copy on write
memDriver, // handle to /dev/mem
(physicalAddr & pageMask)); // hopefully the Text-buffer :-)
close(memDriver);
if (vPtr == MAP_FAILED)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Failed to map share memory");
throw std::system_error(EIO, std::generic_category());
}
size = areaSize;
physicalAddr = addr;
}
bool MDRV2::smbiosUnlock(uint8_t index)
{
bool ret = false;
switch (smbiosDir.dir[index].stage)
{
case MDR2SMBIOSStatusEnum::mdr2Updating:
smbiosDir.dir[index].stage = MDR2SMBIOSStatusEnum::mdr2Updated;
smbiosDir.dir[index].lock = MDR2DirLockEnum::mdr2DirUnlock;
timer->stop();
smbiosDir.dir[index].lockHandle = 0;
ret = true;
break;
case MDR2SMBIOSStatusEnum::mdr2Updated:
case MDR2SMBIOSStatusEnum::mdr2Loaded:
smbiosDir.dir[index].lock = MDR2DirLockEnum::mdr2DirUnlock;
timer->stop();
smbiosDir.dir[index].lockHandle = 0;
ret = true;
break;
default:
break;
}
return ret;
}
bool MDRV2::smbiosTryLock(uint8_t flag, uint8_t index, uint16_t* session,
uint16_t timeout)
{
bool ret = false;
uint32_t u32Status = 0;
if (timeout == 0)
{
timeout = defaultTimeout;
}
std::chrono::microseconds usec(timeout * sysClock);
switch (smbiosDir.dir[index].stage)
{
case MDR2SMBIOSStatusEnum::mdr2Updating:
if (smbiosDir.dir[index].lock != MDR2DirLockEnum::mdr2DirLock)
{
smbiosDir.dir[index].lock = MDR2DirLockEnum::mdr2DirLock;
timer->start(usec);
lockIndex = index;
*session = getSessionHandle(&smbiosDir);
smbiosDir.dir[index].lockHandle = *session;
ret = true;
}
break;
case MDR2SMBIOSStatusEnum::mdr2Init:
if (flag)
{
smbiosDir.dir[index].stage = MDR2SMBIOSStatusEnum::mdr2Updating;
smbiosDir.dir[index].lock = MDR2DirLockEnum::mdr2DirUnlock;
timer->start(usec);
lockIndex = index;
*session = getSessionHandle(&smbiosDir);
smbiosDir.dir[index].lockHandle = *session;
ret = true;
}
break;
case MDR2SMBIOSStatusEnum::mdr2Updated:
case MDR2SMBIOSStatusEnum::mdr2Loaded:
if (smbiosDir.dir[index].lock != MDR2DirLockEnum::mdr2DirLock)
{
if (flag)
{
smbiosDir.dir[index].stage =
MDR2SMBIOSStatusEnum::mdr2Updating;
smbiosDir.dir[index].lock = MDR2DirLockEnum::mdr2DirUnlock;
}
else
{
smbiosDir.dir[index].lock = MDR2DirLockEnum::mdr2DirLock;
}
timer->start(usec);
lockIndex = index;
*session = getSessionHandle(&smbiosDir);
smbiosDir.dir[index].lockHandle = *session;
ret = true;
}
break;
default:
break;
}
return ret;
}
void MDRV2::timeoutHandler()
{
smbiosUnlock(lockIndex);
mdrv2->area.reset(nullptr);
}
/** @brief implements mdr2 lock data command
* @param agentId
* @param dataInfo
* @param timeout
*
* @returns IPMI completion code plus response data
* - mdr2Version
* - session
* - dataLength
* - xferAddress
* - xferLength
*/
ipmi::RspType<uint8_t, // mdr2Version
uint16_t, // session
uint32_t, // dataLength