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_utility.h"
26 #include "fx_fault_tolerant.h"
27 
28 
29 #ifdef FX_ENABLE_FAULT_TOLERANT
30 /**************************************************************************/
31 /*                                                                        */
32 /*  FUNCTION                                               RELEASE        */
33 /*                                                                        */
34 /*    _fx_fault_tolerant_set_FAT_chain                     PORTABLE C     */
35 /*                                                           6.1          */
36 /*  AUTHOR                                                                */
37 /*                                                                        */
38 /*    William E. Lamie, Microsoft Corporation                             */
39 /*                                                                        */
40 /*  DESCRIPTION                                                           */
41 /*                                                                        */
42 /*    This function sets data of FAT chain.                               */
43 /*                                                                        */
44 /*  INPUT                                                                 */
45 /*                                                                        */
46 /*    media_ptr                             Media control block pointer   */
47 /*    use_bitmap                            Whether or not the orignal    */
48 /*                                            FAT chain uses bitmap       */
49 /*    insertion_front                       Previous cluster of head      */
50 /*    new_head_cluster                      Head cluster of new chain     */
51 /*    origianl_head_cluster                 Head cluster of the original  */
52 /*                                            chain                       */
53 /*    insertion_back                        next cluster of chain tail    */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    return status                                                       */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    _fx_utility_16_unsigned_write         Write a USHORT from memory    */
62 /*    _fx_utility_32_unsigned_write         Write a ULONG from memory     */
63 /*    _fx_fault_tolerant_calculate_checksum Compute Checksum of data      */
64 /*    _fx_fault_tolerant_write_log_file     Write log file                */
65 /*                                                                        */
66 /*  CALLED BY                                                             */
67 /*                                                                        */
68 /*    _fx_file_allocate                                                   */
69 /*    _fx_file_best_effort_allocate                                       */
70 /*    _fx_file_delete                                                     */
71 /*    _fx_file_truncate_release                                           */
72 /*    _fx_file_write                                                      */
73 /*                                                                        */
74 /*  RELEASE HISTORY                                                       */
75 /*                                                                        */
76 /*    DATE              NAME                      DESCRIPTION             */
77 /*                                                                        */
78 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
79 /*  09-30-2020     William E. Lamie         Modified comment(s),          */
80 /*                                            resulting in version 6.1    */
81 /*                                                                        */
82 /**************************************************************************/
_fx_fault_tolerant_set_FAT_chain(FX_MEDIA * media_ptr,UINT use_bitmap,ULONG insertion_front,ULONG new_head_cluster,ULONG original_head_cluster,ULONG insertion_back)83 UINT    _fx_fault_tolerant_set_FAT_chain(FX_MEDIA *media_ptr, UINT use_bitmap, ULONG insertion_front,
84                                          ULONG new_head_cluster, ULONG original_head_cluster,
85                                          ULONG insertion_back)
86 {
87 UINT                         status;
88 USHORT                       checksum;
89 UCHAR                        flag = FX_FAULT_TOLERANT_FLAG_FAT_CHAIN_VALID;
90 FX_FAULT_TOLERANT_FAT_CHAIN *FAT_chain;
91 
92     /* Set FAT chain pointer. */
93     FAT_chain = (FX_FAULT_TOLERANT_FAT_CHAIN *)(media_ptr -> fx_media_fault_tolerant_memory_buffer + FX_FAULT_TOLERANT_FAT_CHAIN_OFFSET);
94 
95     /* Parameters not used. Avoid compiler warnings. */
96     FX_PARAMETER_NOT_USED(use_bitmap);
97 
98 
99     /* Reset checksum first. */
100     _fx_utility_16_unsigned_write((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_checksumm, 0);
101 
102     /* Set clusters. */
103     _fx_utility_32_unsigned_write((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_insertion_front, insertion_front);
104     _fx_utility_32_unsigned_write((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_head_new, new_head_cluster);
105     _fx_utility_32_unsigned_write((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_head_original, original_head_cluster);
106     _fx_utility_32_unsigned_write((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_insertion_back, insertion_back);
107     _fx_utility_32_unsigned_write((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_next_deletion, FX_FREE_CLUSTER);
108 
109     /* Set flag and reserved. */
110     FAT_chain -> fx_fault_tolerant_FAT_chain_flag = flag;
111     FAT_chain -> fx_fault_tolerant_FAT_chain_reserved = (UCHAR)0;
112 
113     /* Calculate checksum. */
114     checksum = _fx_fault_tolerant_calculate_checksum((UCHAR *)FAT_chain, FX_FAULT_TOLERANT_FAT_CHAIN_SIZE);
115     _fx_utility_16_unsigned_write((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_checksumm, checksum);
116 
117     /* Write the log header and FAT chain.  */
118     status =  _fx_fault_tolerant_write_log_file(media_ptr, 0);
119 
120     /* Return the status.  */
121     return(status);
122 }
123 #endif /* FX_ENABLE_FAULT_TOLERANT */
124 
125