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 /**   Utility                                                             */
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_utility.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _fx_utility_FAT_sector_get                          PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    William E. Lamie, Microsoft Corporation                             */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function gets the sector of supplied FAT entry.                */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    media_ptr                             Media control block pointer   */
48 /*    cluster                               Cluster entry number          */
49 /*                                                                        */
50 /*  OUTPUT                                                                */
51 /*                                                                        */
52 /*    return sector of FAT entry                                          */
53 /*                                                                        */
54 /*  CALLS                                                                 */
55 /*                                                                        */
56 /*                                                                        */
57 /*  CALLED BY                                                             */
58 /*                                                                        */
59 /*    FileX System Functions                                              */
60 /*                                                                        */
61 /*  RELEASE HISTORY                                                       */
62 /*                                                                        */
63 /*    DATE              NAME                      DESCRIPTION             */
64 /*                                                                        */
65 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
66 /*  09-30-2020     William E. Lamie         Modified comment(s),          */
67 /*                                            resulting in version 6.1    */
68 /*                                                                        */
69 /**************************************************************************/
_fx_utility_FAT_sector_get(FX_MEDIA * media_ptr,ULONG cluster)70 ULONG  _fx_utility_FAT_sector_get(FX_MEDIA *media_ptr, ULONG cluster)
71 {
72 
73 ULONG FAT_sector;
74 ULONG byte_offset;
75 
76     /* Determine which type of FAT is present.  */
77     if (media_ptr -> fx_media_12_bit_FAT)
78     {
79 
80         /* 12-bit FAT is present.  */
81 
82         /* Calculate the byte offset to the cluster entry.  */
83         byte_offset =  (((ULONG)cluster << 1) + cluster) >> 1;
84 
85     }
86     else if (!media_ptr -> fx_media_32_bit_FAT)
87     {
88 
89         /* 16-bit FAT is present.  */
90 
91         /* Calculate the byte offset to the cluster entry.  */
92         byte_offset =  (((ULONG)cluster) << 1);
93     }
94     else
95     {
96 
97         /* 32-bit FAT is present.  */
98 
99         /* Calculate the byte offset to the cluster entry.  */
100         byte_offset =  (((ULONG)cluster) * 4);
101     }
102 
103     /* Calculate the FAT sector the requested FAT entry resides in.  */
104     FAT_sector =  (byte_offset / media_ptr -> fx_media_bytes_per_sector) +
105         (ULONG)media_ptr -> fx_media_reserved_sectors;
106 
107     /* Return successful status.  */
108     return(FAT_sector);
109 }
110 
111