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 /** USBX Component                                                        */
16 /**                                                                       */
17 /**   Storage Class                                                       */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 
23 /* Include necessary system files.  */
24 
25 #define UX_SOURCE_CODE
26 
27 #include "ux_api.h"
28 #include "ux_host_class_storage.h"
29 #include "ux_host_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_host_class_storage_partition_read               PORTABLE C      */
37 /*                                                           6.1.2        */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function will analyze the partition table and parse all the    */
45 /*    partitions. It may happen that a partition entry points to a        */
46 /*    secondary partition table.                                          */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    storage                               Pointer to storage class      */
51 /*    sector_memory                         Pointer to memory for sector  */
52 /*    sector                                Sector number                 */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    Completion Status                                                   */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    _ux_host_class_storage_media_mount    Mount media                   */
61 /*    _ux_host_class_storage_media_open     Open media                    */
62 /*    _ux_utility_long_get                  Get 32-bit word               */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    _ux_host_class_storage_media_mount    Mount media                   */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
73 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
74 /*                                            added option to disable FX  */
75 /*                                            media integration,          */
76 /*                                            resulting in version 6.1    */
77 /*  11-09-2020     Chaoqiong Xiao           Modified comment(s),          */
78 /*                                            added exFAT support,        */
79 /*                                            resulting in version 6.1.2  */
80 /*                                                                        */
81 /**************************************************************************/
_ux_host_class_storage_partition_read(UX_HOST_CLASS_STORAGE * storage,UCHAR * sector_memory,ULONG sector)82 UINT  _ux_host_class_storage_partition_read(UX_HOST_CLASS_STORAGE *storage, UCHAR *sector_memory, ULONG sector)
83 {
84 #if defined(UX_HOST_CLASS_STORAGE_NO_FILEX)
85     UX_PARAMETER_NOT_USED(storage);
86     UX_PARAMETER_NOT_USED(sector_memory);
87     UX_PARAMETER_NOT_USED(sector);
88     return(UX_FUNCTION_NOT_SUPPORTED);
89 #else
90 UINT        status =  UX_ERROR;
91 UINT        partition_index;
92 
93 
94     /* Point the sector buffer to the first partition entry.  */
95     sector_memory +=  UX_HOST_CLASS_STORAGE_PARTITION_TABLE_START;
96 
97     /* There are 4 partitions in a partition table.  */
98     for (partition_index = 0; partition_index < 4; partition_index++)
99     {
100 
101         /* Check if we recognize this partition entry.  */
102         switch(*(sector_memory + UX_HOST_CLASS_STORAGE_PARTITION_TYPE))
103         {
104 
105         case UX_HOST_CLASS_STORAGE_PARTITION_FAT_12:
106         case UX_HOST_CLASS_STORAGE_PARTITION_FAT_16:
107         case UX_HOST_CLASS_STORAGE_PARTITION_FAT_16L:
108         case UX_HOST_CLASS_STORAGE_PARTITION_FAT_16_LBA_MAPPED:
109         case UX_HOST_CLASS_STORAGE_PARTITION_FAT_32_1:
110         case UX_HOST_CLASS_STORAGE_PARTITION_FAT_32_2:
111         case UX_HOST_CLASS_STORAGE_PARTITION_EXFAT:
112 
113             /* We have found a legal partition entry pointing to a potential boot sector.  */
114             status =  _ux_host_class_storage_media_open(storage, sector + _ux_utility_long_get(sector_memory + UX_HOST_CLASS_STORAGE_PARTITION_SECTORS_BEFORE));
115             break;
116 
117         case UX_HOST_CLASS_STORAGE_PARTITION_EXTENDED:
118         case UX_HOST_CLASS_STORAGE_PARTITION_EXTENDED_LBA_MAPPED:
119 
120             /* We have found an entry to an extended partition. We need to read that partition sector
121                and recursively mount all partitions found.  */
122             status =  _ux_host_class_storage_media_mount(storage, sector + _ux_utility_long_get(sector_memory + UX_HOST_CLASS_STORAGE_PARTITION_SECTORS_BEFORE));
123             break;
124 
125         default:
126 
127             /* We have found something which is not a DOS recognized partition, or an empty entry.
128                Ignore it and proceed with the rest.  */
129             break;
130         }
131 
132         /* Move to the next partition entry.  */
133         sector_memory +=  UX_HOST_CLASS_STORAGE_PARTITION_TABLE_SIZE;
134     }
135 
136     /* Return completion status.  */
137     return(status);
138 #endif
139 }
140 
141