blob: d9a440e6462494c137fd295b8b47bc9db17045d6 [file] [log] [blame]
Dawid Fryckia642a942018-06-12 10:44:23 -07001/* Copyright 2018 Intel
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "ipmbbridged.hpp"
17#include "ipmbdefines.hpp"
18
19#include <inttypes.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22#include <unistd.h>
23
24/**
25 * @brief Ipmb utils for checksum
26 */
27bool ipmbChecksumValidate(uint8_t *data, uint8_t length)
28{
29 uint8_t checksum = 0;
30
31 // compute checksum.
32 for (uint8_t idx = 0; idx < length; idx++)
33 {
34 checksum += data[idx];
35 }
36
37 // check if checksum is valid.
38 if (0 == checksum)
39 {
40 // checksum valid.
41 return true;
42 }
43 else
44 {
45 // checksum invalid.
46 return false;
47 }
48}
49
50uint8_t ipmbChecksumCompute(uint8_t *data, uint8_t length)
51{
52 uint8_t checksum = 0;
53
54 // compute checksum.
55 for (uint8_t idx = 0; idx < length; idx++)
56 {
57 checksum += data[idx];
58 }
59
60 checksum = (~checksum) + 1;
61
62 // return computed checksum value.
63 return checksum;
64}
65
66inline bool ipmbConnectionHeaderChecksumValidate(IPMB_HEADER *ipmbHeader)
67{
68 return ipmbChecksumValidate(reinterpret_cast<uint8_t *>(ipmbHeader),
69 ipmbConnectionHeaderLength);
70}
71
72inline bool ipmbDataChecksumValidate(IPMB_HEADER *ipmbHeader, uint8_t length)
73{
74 return ipmbChecksumValidate(
75 (reinterpret_cast<uint8_t *>(ipmbHeader) + ipmbConnectionHeaderLength),
76 (length - ipmbConnectionHeaderLength));
77}
78
79bool isFrameValid(IPMB_HEADER *frame, uint8_t length)
80{
81 bool frameValid = ipmbConnectionHeaderChecksumValidate(frame);
82 if (false == frameValid)
83 {
84 // invalid connection header checksum.
85 return false;
86 }
87
88 // data checksum validation.
89 return ipmbDataChecksumValidate(frame, length);
90}