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_add_FAT_log                      PORTABLE C      */
35 /*                                                           6.1          */
36 /*  AUTHOR                                                                */
37 /*                                                                        */
38 /*    William E. Lamie, Microsoft Corporation                             */
39 /*                                                                        */
40 /*  DESCRIPTION                                                           */
41 /*                                                                        */
42 /*    This function converts FAT write into log entry and add it into     */
43 /*    log file.                                                           */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    media_ptr                             Media control block pointer   */
48 /*    cluster                               Cluster entry number          */
49 /*    value                                 Next cluster value            */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    return status                                                       */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    _fx_utility_16_unsigned_write         Write a USHORT from memory    */
58 /*    _fx_utility_32_unsigned_write         Write a UINT from memory      */
59 /*                                                                        */
60 /*  CALLED BY                                                             */
61 /*                                                                        */
62 /*    _fx_utility_FAT_entry_write                                         */
63 /*                                                                        */
64 /*  RELEASE HISTORY                                                       */
65 /*                                                                        */
66 /*    DATE              NAME                      DESCRIPTION             */
67 /*                                                                        */
68 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
69 /*  09-30-2020     William E. Lamie         Modified comment(s),          */
70 /*                                            resulting in version 6.1    */
71 /*                                                                        */
72 /**************************************************************************/
_fx_fault_tolerant_add_FAT_log(FX_MEDIA * media_ptr,ULONG cluster,ULONG value)73 UINT _fx_fault_tolerant_add_FAT_log(FX_MEDIA *media_ptr, ULONG cluster, ULONG value)
74 {
75 ULONG                      file_size;
76 FX_FAULT_TOLERANT_FAT_LOG *fat_log;
77 
78     /* Increment the size of the log file. */
79     file_size = media_ptr -> fx_media_fault_tolerant_file_size + FX_FAULT_TOLERANT_FAT_LOG_ENTRY_SIZE;
80 
81     /* Check whether log file exceeds the buffer. */
82     if (file_size > media_ptr -> fx_media_fault_tolerant_memory_buffer_size)
83     {
84 
85         /*  Log file exceeds the size of the log buffer.  This is a failure. */
86         return(FX_NO_MORE_SPACE);
87     }
88 
89     /* Set log pointer. */
90     fat_log = (FX_FAULT_TOLERANT_FAT_LOG *)(media_ptr -> fx_media_fault_tolerant_memory_buffer +
91                                             media_ptr -> fx_media_fault_tolerant_file_size);
92 
93     /* Set log type. */
94     _fx_utility_16_unsigned_write((UCHAR *)&fat_log -> fx_fault_tolerant_FAT_log_type,
95                                   FX_FAULT_TOLERANT_FAT_LOG_TYPE);
96 
97     /* Size of log. */
98     _fx_utility_16_unsigned_write((UCHAR *)&fat_log -> fx_fault_tolerant_FAT_log_size,
99                                   FX_FAULT_TOLERANT_FAT_LOG_ENTRY_SIZE);
100 
101     /* Set cluster and value. */
102     _fx_utility_32_unsigned_write((UCHAR *)&fat_log -> fx_fault_tolerant_FAT_log_cluster, cluster);
103     _fx_utility_32_unsigned_write((UCHAR *)&fat_log -> fx_fault_tolerant_FAT_log_value, value);
104 
105     /* Update log information. */
106     media_ptr -> fx_media_fault_tolerant_file_size = (USHORT)file_size;
107     media_ptr -> fx_media_fault_tolerant_total_logs += 1;
108 
109     return(FX_SUCCESS);
110 }
111 #endif /* FX_ENABLE_FAULT_TOLERANT */
112 
113