|
27 | 27 | #include "ux_api.h" |
28 | 28 | #include "ux_host_class_storage.h" |
29 | 29 | #include "ux_host_stack.h" |
30 | | - |
31 | | - |
32 | 30 | /**************************************************************************/ |
33 | 31 | /* */ |
34 | 32 | /* FUNCTION RELEASE */ |
@@ -90,49 +88,55 @@ UINT _ux_host_class_storage_partition_read(UX_HOST_CLASS_STORAGE *storage, UCHA |
90 | 88 | UINT status = UX_ERROR; |
91 | 89 | UINT partition_index; |
92 | 90 |
|
| 91 | + /* Check recursion/mount count before processing. */ |
| 92 | + if (storage -> ux_host_class_storage_mounted_partitions_count > UX_HOST_CLASS_STORAGE_MAX_PARTITIONS_COUNT) |
| 93 | + { |
| 94 | + return UX_HOST_CLASS_STORAGE_ERROR_MEDIA_NOT_READ; |
| 95 | + } |
93 | 96 |
|
94 | 97 | /* Point the sector buffer to the first partition entry. */ |
95 | 98 | sector_memory += UX_HOST_CLASS_STORAGE_PARTITION_TABLE_START; |
96 | | - |
| 99 | + |
97 | 100 | /* There are 4 partitions in a partition table. */ |
98 | 101 | for (partition_index = 0; partition_index < 4; partition_index++) |
99 | 102 | { |
| 103 | + /* Increment the mounted partition count for every entry processed. */ |
| 104 | + storage -> ux_host_class_storage_mounted_partitions_count++; |
| 105 | + |
| 106 | + /* Check again after incrementing. */ |
| 107 | + if (storage -> ux_host_class_storage_mounted_partitions_count > UX_HOST_CLASS_STORAGE_MAX_PARTITIONS_COUNT) |
| 108 | + { |
| 109 | + /* Too many partition entries processed, abort to prevent stack overflow. */ |
| 110 | + return UX_HOST_CLASS_STORAGE_ERROR_MEDIA_NOT_READ; |
| 111 | + } |
100 | 112 |
|
101 | 113 | /* Check if we recognize this partition entry. */ |
102 | 114 | switch(*(sector_memory + UX_HOST_CLASS_STORAGE_PARTITION_TYPE)) |
103 | 115 | { |
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; |
| 116 | + case UX_HOST_CLASS_STORAGE_PARTITION_FAT_12: |
| 117 | + case UX_HOST_CLASS_STORAGE_PARTITION_FAT_16: |
| 118 | + case UX_HOST_CLASS_STORAGE_PARTITION_FAT_16L: |
| 119 | + case UX_HOST_CLASS_STORAGE_PARTITION_FAT_16_LBA_MAPPED: |
| 120 | + case UX_HOST_CLASS_STORAGE_PARTITION_FAT_32_1: |
| 121 | + case UX_HOST_CLASS_STORAGE_PARTITION_FAT_32_2: |
| 122 | + case UX_HOST_CLASS_STORAGE_PARTITION_EXFAT: |
| 123 | + /* We have found a legal partition entry pointing to a potential boot sector. */ |
| 124 | + status = _ux_host_class_storage_media_open(storage, sector + _ux_utility_long_get(sector_memory + UX_HOST_CLASS_STORAGE_PARTITION_SECTORS_BEFORE)); |
| 125 | + break; |
| 126 | + case UX_HOST_CLASS_STORAGE_PARTITION_EXTENDED: |
| 127 | + case UX_HOST_CLASS_STORAGE_PARTITION_EXTENDED_LBA_MAPPED: |
| 128 | + /* We have found an entry to an extended partition. We need to read that partition sector |
| 129 | + and recursively mount all partitions found. */ |
| 130 | + status = _ux_host_class_storage_media_mount(storage, sector + _ux_utility_long_get(sector_memory + UX_HOST_CLASS_STORAGE_PARTITION_SECTORS_BEFORE)); |
| 131 | + break; |
| 132 | + default: |
| 133 | + /* We have found something which is not a DOS recognized partition, or an empty entry. |
| 134 | + Ignore it and proceed with the rest. */ |
| 135 | + break; |
130 | 136 | } |
131 | | - |
132 | 137 | /* Move to the next partition entry. */ |
133 | 138 | sector_memory += UX_HOST_CLASS_STORAGE_PARTITION_TABLE_SIZE; |
134 | 139 | } |
135 | | - |
136 | 140 | /* Return completion status. */ |
137 | 141 | return(status); |
138 | 142 | #endif |
|
0 commit comments