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 /**   Media                                                               */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define FX_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 
27 #include "fx_api.h"
28 #include "fx_system.h"
29 #include "fx_media.h"
30 #include "fx_utility.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _fx_media_write                                     PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    William E. Lamie, Microsoft Corporation                             */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function writes the specified raw logical sector to the        */
46 /*    specified media.                                                    */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    media_ptr                             Media control block pointer   */
51 /*    logical_sector                        Logical sector to read        */
52 /*    buffer_ptr                            Pointer to caller's buffer    */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    return status                                                       */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    _fx_utility_logical_sector_write      Write logical sector utility  */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    Application Code                                                    */
65 /*                                                                        */
66 /*  RELEASE HISTORY                                                       */
67 /*                                                                        */
68 /*    DATE              NAME                      DESCRIPTION             */
69 /*                                                                        */
70 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
71 /*  09-30-2020     William E. Lamie         Modified comment(s),          */
72 /*                                            resulting in version 6.1    */
73 /*                                                                        */
74 /**************************************************************************/
_fx_media_write(FX_MEDIA * media_ptr,ULONG logical_sector,VOID * buffer_ptr)75 UINT  _fx_media_write(FX_MEDIA *media_ptr, ULONG logical_sector, VOID *buffer_ptr)
76 {
77 
78 UINT                   status;
79 
80 #ifdef TX_ENABLE_EVENT_TRACE
81 TX_TRACE_BUFFER_ENTRY *trace_event;
82 ULONG                  trace_timestamp;
83 #endif
84 
85 
86 #ifndef FX_MEDIA_STATISTICS_DISABLE
87 
88     /* Increment the number of times this service has been called.  */
89     media_ptr -> fx_media_writes++;
90 #endif
91 
92     /* Check the media to make sure it is open.  */
93     if (media_ptr -> fx_media_id != FX_MEDIA_ID)
94     {
95 
96         /* Return the media not opened error.  */
97         return(FX_MEDIA_NOT_OPEN);
98     }
99 
100     /* If trace is enabled, insert this event into the trace buffer.  */
101     FX_TRACE_IN_LINE_INSERT(FX_TRACE_MEDIA_WRITE, media_ptr, logical_sector, buffer_ptr, 0, FX_TRACE_MEDIA_EVENTS, &trace_event, &trace_timestamp)
102 
103     /* Protect against other threads accessing the media.  */
104     FX_PROTECT
105 
106     /* Check for write protect at the media level (set by driver).  */
107     if (media_ptr -> fx_media_driver_write_protect)
108     {
109 
110         /* Release media protection.  */
111         FX_UNPROTECT
112 
113         /* Return write protect error.  */
114         return(FX_WRITE_PROTECT);
115     }
116 
117     /* Write the logical sector.  */
118     status =  _fx_utility_logical_sector_write(media_ptr, (ULONG64) logical_sector, buffer_ptr, ((ULONG) 1), FX_DATA_SECTOR);
119 
120 #ifdef TX_ENABLE_EVENT_TRACE
121 
122     /* Check for successful status.  */
123     if (status == FX_SUCCESS)
124     {
125 
126         /* Update the trace event with the bytes written.  */
127         FX_TRACE_EVENT_UPDATE(trace_event, trace_timestamp, FX_TRACE_MEDIA_WRITE, 0, 0, 0, media_ptr -> fx_media_bytes_per_sector)
128     }
129 #endif
130 
131     /* Release media protection.  */
132     FX_UNPROTECT
133 
134     /* Return status to the caller.  */
135     return(status);
136 }
137 
138