1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** FileX Component                                                       */
16 /**                                                                       */
17 /**   Fault Tolerant                                                      */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define FX_SOURCE_CODE
23 
24 #include "fx_api.h"
25 #include "fx_fault_tolerant.h"
26 #include "fx_utility.h"
27 
28 
29 #ifdef FX_ENABLE_FAULT_TOLERANT
30 /**************************************************************************/
31 /*                                                                        */
32 /*  FUNCTION                                               RELEASE        */
33 /*                                                                        */
34 /*    _fx_fault_tolerant_calculate_checksum               PORTABLE C      */
35 /*                                                           6.1          */
36 /*  AUTHOR                                                                */
37 /*                                                                        */
38 /*    William E. Lamie, Microsoft Corporation                             */
39 /*                                                                        */
40 /*  DESCRIPTION                                                           */
41 /*                                                                        */
42 /*    This function calculates checksum for consecutive data.  The size   */
43 /*    of the log file is required to be 4-byte aligned.  Therefore this   */
44 /*    checksum routine is able to perform 4-byte access.                  */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    data                                  Pointer to data               */
49 /*    len                                   Data length                   */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    Computed checksum value                                             */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*     _fx_utility_32_unsigned_read         Read a UINT from memory       */
58 /*                                                                        */
59 /*  CALLED BY                                                             */
60 /*                                                                        */
61 /*    _fx_fault_tolerant_enable                                           */
62 /*    _fx_fault_tolerant_cleanup_FAT_chain                                */
63 /*    _fx_fault_tolerant_reset_log_file                                   */
64 /*    _fx_fault_tolerant_set_FAT_chain                                    */
65 /*    _fx_fault_tolerant_transaction_end                                  */
66 /*                                                                        */
67 /*  RELEASE HISTORY                                                       */
68 /*                                                                        */
69 /*    DATE              NAME                      DESCRIPTION             */
70 /*                                                                        */
71 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
72 /*  09-30-2020     William E. Lamie         Modified comment(s),          */
73 /*                                            resulting in version 6.1    */
74 /*                                                                        */
75 /**************************************************************************/
_fx_fault_tolerant_calculate_checksum(UCHAR * data,UINT len)76 USHORT  _fx_fault_tolerant_calculate_checksum(UCHAR *data, UINT len)
77 {
78 ULONG checksum = 0;
79 ULONG long_value;
80 
81     while (len >= 4)
82     {
83 
84         /* Read first long value. */
85         long_value = _fx_utility_32_unsigned_read(data);
86 
87         /* Calculate checksum. */
88         checksum += (long_value >> 16) + (long_value & 0xFFFF);
89 
90         /* Decrease length. */
91         len -= sizeof(ULONG);
92         data += sizeof(ULONG);
93     }
94 
95     /* Trim high 16 bits of checksum. */
96     checksum = (checksum & 0xFFFF) + (checksum >> 16);
97     checksum = (checksum & 0xFFFF) + (checksum >> 16);
98 
99     return((USHORT)((~checksum) & 0xFFFF));
100 }
101 #endif /* FX_ENABLE_FAULT_TOLERANT */
102 
103