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_dir_log                      PORTABLE C      */
35 /*                                                           6.1          */
36 /*  AUTHOR                                                                */
37 /*                                                                        */
38 /*    William E. Lamie, Microsoft Corporation                             */
39 /*                                                                        */
40 /*  DESCRIPTION                                                           */
41 /*                                                                        */
42 /*    This function converts directory entry write into log entry and add */
43 /*    it into log file.                                                   */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    media_ptr                             Media control block pointer   */
48 /*    logical_sector                        Original sector to write to   */
49 /*    offset                                Offset in original sector     */
50 /*    data                                  Data of log                   */
51 /*    data_size                             Size of data                  */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    return status                                                       */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _fx_utility_16_unsigned_write         Write a USHORT from memory    */
60 /*    _fx_utility_32_unsigned_write         Write a ULONG from memory     */
61 /*    _fx_utility_64_unsigned_write         Write a ULONG64 from memory   */
62 /*    memcpy                                Memory Copy                   */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    _fx_directory_entry_write                                           */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
73 /*  09-30-2020     William E. Lamie         Modified comment(s), verified */
74 /*                                            memcpy usage,               */
75 /*                                            resulting in version 6.1    */
76 /*                                                                        */
77 /**************************************************************************/
_fx_fault_tolerant_add_dir_log(FX_MEDIA * media_ptr,ULONG64 logical_sector,ULONG offset,UCHAR * data,ULONG data_size)78 UINT _fx_fault_tolerant_add_dir_log(FX_MEDIA *media_ptr, ULONG64 logical_sector, ULONG offset,
79                                     UCHAR *data, ULONG data_size)
80 {
81 ULONG                      file_size;
82 FX_FAULT_TOLERANT_DIR_LOG *dir_log;
83 
84     /* Increment the size of the log file. */
85     file_size = media_ptr -> fx_media_fault_tolerant_file_size + data_size + FX_FAULT_TOLERANT_DIR_LOG_ENTRY_SIZE;
86 
87     /* Check whether log file exceeds the buffer. */
88     if (file_size > media_ptr -> fx_media_fault_tolerant_memory_buffer_size)
89     {
90 
91         /*  Log file exceeds the size of the log buffer.  This is a failure. */
92         return(FX_NO_MORE_SPACE);
93     }
94 
95     /* Any data to write? */
96     if (data_size == 0)
97     {
98 
99         /* No. Just return. */
100         return FX_SUCCESS;
101     }
102 
103     /* Set log pointer. */
104     dir_log = (FX_FAULT_TOLERANT_DIR_LOG *)(media_ptr -> fx_media_fault_tolerant_memory_buffer +
105                                             media_ptr -> fx_media_fault_tolerant_file_size);
106 
107     /* Set log type. */
108     _fx_utility_16_unsigned_write((UCHAR *)&dir_log -> fx_fault_tolerant_dir_log_type,
109                                   FX_FAULT_TOLERANT_DIR_LOG_TYPE);
110 
111     /* Size of log. */
112     _fx_utility_16_unsigned_write((UCHAR *)&dir_log -> fx_fault_tolerant_dir_log_size,
113                                   data_size + FX_FAULT_TOLERANT_DIR_LOG_ENTRY_SIZE);
114 
115     /* Set sector and offset. */
116     _fx_utility_64_unsigned_write((UCHAR *)&dir_log -> fx_fault_tolerant_dir_log_sector, logical_sector);
117     _fx_utility_32_unsigned_write((UCHAR *)&dir_log -> fx_fault_tolerant_dir_log_offset, offset);
118 
119     memcpy(media_ptr -> fx_media_fault_tolerant_memory_buffer +  /* Use case of memcpy is verified. */
120            media_ptr -> fx_media_fault_tolerant_file_size + FX_FAULT_TOLERANT_DIR_LOG_ENTRY_SIZE,
121            data, data_size);
122 
123 
124     /* Update log information. */
125     media_ptr -> fx_media_fault_tolerant_file_size = (USHORT)file_size;
126     media_ptr -> fx_media_fault_tolerant_total_logs += 1;
127 
128     return(FX_SUCCESS);
129 }
130 #endif /* FX_ENABLE_FAULT_TOLERANT */
131 
132