Skip to content
This repository was archived by the owner on Nov 11, 2024. It is now read-only.

Commit e254127

Browse files
committed
consolidate MBR check
1 parent 66a9b27 commit e254127

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

source/partition.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ enum FSIB
102102
static const char FAT_SIG[3] = {'F', 'A', 'T'};
103103
static const char FS_INFO_SIG1[4] = {'R', 'R', 'a', 'A'};
104104
static const char FS_INFO_SIG2[4] = {'r', 'r', 'A', 'a'};
105+
static const char FS_TWL_SIG[8] = { 0xe9, 0x00, 0x00, 0x54, 0x57, 0x4c, 0x20, 0x20 };
106+
107+
static bool isValidMBR(uint8_t *sectorBuffer) {
108+
return (!memcmp(sectorBuffer + BPB_FAT16_fileSysType, FAT_SIG, sizeof(FAT_SIG)) ||
109+
!memcmp(sectorBuffer + BPB_FAT32_fileSysType, FAT_SIG, sizeof(FAT_SIG)) ||
110+
!memcmp(sectorBuffer, FS_TWL_SIG, sizeof(FS_TWL_SIG)));
111+
112+
}
105113

106114
sec_t FindFirstValidPartition_buf(const DISC_INTERFACE* disc, uint8_t *sectorBuffer)
107115
{
@@ -120,8 +128,8 @@ sec_t FindFirstValidPartition_buf(const DISC_INTERFACE* disc, uint8_t *sectorBuf
120128
for(i=0;i<4;i++,ptr+=16) {
121129
sec_t part_lba = u8array_to_u32(ptr, 0x8);
122130

123-
if (!memcmp(sectorBuffer + BPB_FAT16_fileSysType, FAT_SIG, sizeof(FAT_SIG)) ||
124-
!memcmp(sectorBuffer + BPB_FAT32_fileSysType, FAT_SIG, sizeof(FAT_SIG))) {
131+
if (isValidMBR(sectorBuffer))
132+
{
125133
return part_lba;
126134
}
127135

@@ -141,8 +149,7 @@ sec_t FindFirstValidPartition_buf(const DISC_INTERFACE* disc, uint8_t *sectorBuf
141149

142150
if(!_FAT_disc_readSectors (disc, part_lba2, 1, sectorBuffer)) return 0;
143151

144-
if (!memcmp(sectorBuffer + BPB_FAT16_fileSysType, FAT_SIG, sizeof(FAT_SIG)) ||
145-
!memcmp(sectorBuffer + BPB_FAT32_fileSysType, FAT_SIG, sizeof(FAT_SIG)))
152+
if (isValidMBR(sectorBuffer))
146153
{
147154
return part_lba2;
148155
}
@@ -151,8 +158,10 @@ sec_t FindFirstValidPartition_buf(const DISC_INTERFACE* disc, uint8_t *sectorBuf
151158
}
152159
} else {
153160
if(!_FAT_disc_readSectors (disc, part_lba, 1, sectorBuffer)) return 0;
154-
if (!memcmp(sectorBuffer + BPB_FAT16_fileSysType, FAT_SIG, sizeof(FAT_SIG)) ||
155-
!memcmp(sectorBuffer + BPB_FAT32_fileSysType, FAT_SIG, sizeof(FAT_SIG))) {
161+
162+
163+
if (isValidMBR(sectorBuffer))
164+
{
156165
return part_lba;
157166
}
158167
}
@@ -199,9 +208,7 @@ PARTITION* _FAT_partition_constructor_buf (const DISC_INTERFACE* disc, uint32_t
199208
}
200209
}
201210

202-
// Now verify that this is indeed a FAT partition
203-
if (memcmp(sectorBuffer + BPB_FAT16_fileSysType, FAT_SIG, sizeof(FAT_SIG)) &&
204-
memcmp(sectorBuffer + BPB_FAT32_fileSysType, FAT_SIG, sizeof(FAT_SIG)))
211+
if (!isValidMBR(sectorBuffer))
205212
{
206213
return NULL;
207214
}

0 commit comments

Comments
 (0)