1 |
|
|
/*************************************************************************** |
2 |
|
|
* Copyright (c) 2024 Microsoft Corporation |
3 |
|
|
* Copyright (c) 2026-present Eclipse ThreadX contributors |
4 |
|
|
* |
5 |
|
|
* This program and the accompanying materials are made available under the |
6 |
|
|
* terms of the MIT License which is available at |
7 |
|
|
* https://opensource.org/licenses/MIT. |
8 |
|
|
* |
9 |
|
|
* SPDX-License-Identifier: MIT |
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 |
|
|
/* FUNCTION RELEASE */ |
34 |
|
|
/* */ |
35 |
|
|
/* _ux_host_class_storage_partition_read PORTABLE C */ |
36 |
|
|
/* 6.1.2 */ |
37 |
|
|
/* AUTHOR */ |
38 |
|
|
/* */ |
39 |
|
|
/* Chaoqiong Xiao, Microsoft Corporation */ |
40 |
|
|
/* */ |
41 |
|
|
/* DESCRIPTION */ |
42 |
|
|
/* */ |
43 |
|
|
/* This function will analyze the partition table and parse all the */ |
44 |
|
|
/* partitions. It may happen that a partition entry points to a */ |
45 |
|
|
/* secondary partition table. */ |
46 |
|
|
/* */ |
47 |
|
|
/* INPUT */ |
48 |
|
|
/* */ |
49 |
|
|
/* storage Pointer to storage class */ |
50 |
|
|
/* sector_memory Pointer to memory for sector */ |
51 |
|
|
/* sector Sector number */ |
52 |
|
|
/* */ |
53 |
|
|
/* OUTPUT */ |
54 |
|
|
/* */ |
55 |
|
|
/* Completion Status */ |
56 |
|
|
/* */ |
57 |
|
|
/* CALLS */ |
58 |
|
|
/* */ |
59 |
|
|
/* _ux_host_class_storage_media_mount Mount media */ |
60 |
|
|
/* _ux_host_class_storage_media_open Open media */ |
61 |
|
|
/* _ux_utility_long_get Get 32-bit word */ |
62 |
|
|
/* */ |
63 |
|
|
/* CALLED BY */ |
64 |
|
|
/* */ |
65 |
|
|
/* _ux_host_class_storage_media_mount Mount media */ |
66 |
|
|
/* */ |
67 |
|
|
/**************************************************************************/ |
68 |
|
13 |
UINT _ux_host_class_storage_partition_read(UX_HOST_CLASS_STORAGE *storage, UCHAR *sector_memory, ULONG sector) |
69 |
|
|
{ |
70 |
|
|
#if defined(UX_HOST_CLASS_STORAGE_NO_FILEX) |
71 |
|
|
UX_PARAMETER_NOT_USED(storage); |
72 |
|
|
UX_PARAMETER_NOT_USED(sector_memory); |
73 |
|
|
UX_PARAMETER_NOT_USED(sector); |
74 |
|
|
return(UX_FUNCTION_NOT_SUPPORTED); |
75 |
|
|
#else |
76 |
|
13 |
UINT status = UX_ERROR; |
77 |
|
|
UINT partition_index; |
78 |
|
|
|
79 |
|
|
/* Check recursion/mount count before processing. */ |
80 |
✗✓ |
13 |
if (storage -> ux_host_class_storage_mounted_partitions_count > UX_HOST_CLASS_STORAGE_MAX_PARTITIONS_COUNT) |
81 |
|
|
{ |
82 |
|
|
return UX_HOST_CLASS_STORAGE_ERROR_MEDIA_NOT_READ; |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
/* Point the sector buffer to the first partition entry. */ |
86 |
|
13 |
sector_memory += UX_HOST_CLASS_STORAGE_PARTITION_TABLE_START; |
87 |
|
|
|
88 |
|
|
/* There are 4 partitions in a partition table. */ |
89 |
✓✓ |
65 |
for (partition_index = 0; partition_index < 4; partition_index++) |
90 |
|
|
{ |
91 |
|
|
/* Increment the mounted partition count for every entry processed. */ |
92 |
|
52 |
storage -> ux_host_class_storage_mounted_partitions_count++; |
93 |
|
|
|
94 |
|
|
/* Check again after incrementing. */ |
95 |
✗✓ |
52 |
if (storage -> ux_host_class_storage_mounted_partitions_count > UX_HOST_CLASS_STORAGE_MAX_PARTITIONS_COUNT) |
96 |
|
|
{ |
97 |
|
|
/* Too many partition entries processed, abort to prevent stack overflow. */ |
98 |
|
|
return UX_HOST_CLASS_STORAGE_ERROR_MEDIA_NOT_READ; |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
/* Check if we recognize this partition entry. */ |
102 |
✓✓✓ |
52 |
switch(*(sector_memory + UX_HOST_CLASS_STORAGE_PARTITION_TYPE)) |
103 |
|
|
{ |
104 |
|
8 |
case UX_HOST_CLASS_STORAGE_PARTITION_FAT_12: |
105 |
|
|
case UX_HOST_CLASS_STORAGE_PARTITION_FAT_16: |
106 |
|
|
case UX_HOST_CLASS_STORAGE_PARTITION_FAT_16L: |
107 |
|
|
case UX_HOST_CLASS_STORAGE_PARTITION_FAT_16_LBA_MAPPED: |
108 |
|
|
case UX_HOST_CLASS_STORAGE_PARTITION_FAT_32_1: |
109 |
|
|
case UX_HOST_CLASS_STORAGE_PARTITION_FAT_32_2: |
110 |
|
|
case UX_HOST_CLASS_STORAGE_PARTITION_EXFAT: |
111 |
|
|
/* We have found a legal partition entry pointing to a potential boot sector. */ |
112 |
|
8 |
status = _ux_host_class_storage_media_open(storage, sector + _ux_utility_long_get(sector_memory + UX_HOST_CLASS_STORAGE_PARTITION_SECTORS_BEFORE)); |
113 |
|
8 |
break; |
114 |
|
2 |
case UX_HOST_CLASS_STORAGE_PARTITION_EXTENDED: |
115 |
|
|
case UX_HOST_CLASS_STORAGE_PARTITION_EXTENDED_LBA_MAPPED: |
116 |
|
|
/* We have found an entry to an extended partition. We need to read that partition sector |
117 |
|
|
and recursively mount all partitions found. */ |
118 |
|
2 |
status = _ux_host_class_storage_media_mount(storage, sector + _ux_utility_long_get(sector_memory + UX_HOST_CLASS_STORAGE_PARTITION_SECTORS_BEFORE)); |
119 |
|
2 |
break; |
120 |
|
42 |
default: |
121 |
|
|
/* We have found something which is not a DOS recognized partition, or an empty entry. |
122 |
|
|
Ignore it and proceed with the rest. */ |
123 |
|
42 |
break; |
124 |
|
|
} |
125 |
|
|
/* Move to the next partition entry. */ |
126 |
|
52 |
sector_memory += UX_HOST_CLASS_STORAGE_PARTITION_TABLE_SIZE; |
127 |
|
|
} |
128 |
|
|
/* Return completion status. */ |
129 |
|
13 |
return(status); |
130 |
|
|
#endif |
131 |
|
|
} |
132 |
|
|
|