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