21 #include "Partition.h" 22 #include "pedigree/kernel/LockGuard.h" 23 #include "pedigree/kernel/Log.h" 24 #include "pedigree/kernel/Spinlock.h" 25 #include "pedigree/kernel/machine/Disk.h" 26 #include "pedigree/kernel/utilities/StaticString.h" 27 #include "pedigree/kernel/utilities/String.h" 28 #include "pedigree/kernel/utilities/utility.h" 35 const uint8_t g_ExtendedPartitionNumber = 5;
37 const uint8_t g_EmptyPartitionNumber = 0;
39 static const char *g_pPartitionTypes[256] = {
"Empty",
297 static int gNextPartition = 0;
300 msdosRegPartition(MsdosPartitionInfo *pPartitions,
int i,
Disk *pDisk)
307 const char *pStr = g_pPartitionTypes[pPartitions[i].type];
309 sstr += gNextPartition++;
317 static_cast<uint64_t>(LITTLE_TO_HOST32(pPartitions[i].start_lba)) *
319 static_cast<uint64_t>(LITTLE_TO_HOST32(pPartitions[i].size)) * 512ULL);
320 pObj->
setParent(static_cast<Device *>(pDisk));
321 pDisk->
addChild(static_cast<Device *>(pObj));
324 static bool msdosReadExtTable(
325 MsdosPartitionInfo *pPartitions,
Disk *pDisk,
int n, uint64_t partitionBase,
326 uint64_t currentBase)
328 for (
int i = 0; i < MSDOS_EXT_PARTTAB_NUM; i++)
331 if ((pPartitions[i].active != 0) && (pPartitions[i].active != 0x80))
333 WARNING(
"Invalid partition record found");
338 if (pPartitions[i].type == g_ExtendedPartitionNumber)
344 LITTLE_TO_HOST32(pPartitions[i].start_lba) + partitionBase;
348 pPartitions[i].start_lba =
349 static_cast<uint32_t
>(startLba & 0xFFFFFFFF);
353 if ((buff = pDisk->
read(pPartitions[i].start_lba * 512ULL)) == 0)
356 "Couldn't read next sector for the extended partition.");
360 uint8_t *buffer =
reinterpret_cast<uint8_t *
>(buff);
363 if (buffer[510] != MSDOS_IDENT_1 || buffer[511] != MSDOS_IDENT_2)
365 WARNING(
"Extended partition record read failed.");
372 MsdosPartitionInfo *pNextPartitions =
373 reinterpret_cast<MsdosPartitionInfo *
>(
374 &buffer[MSDOS_PARTTAB_START]);
375 if (!msdosReadExtTable(
376 pNextPartitions, pDisk, MSDOS_PARTTAB_NUM, partitionBase,
378 WARNING(
"Reading the extended partition table failed");
380 else if (pPartitions[i].type == g_EmptyPartitionNumber)
390 LITTLE_TO_HOST32(pPartitions[i].start_lba) + currentBase;
392 pPartitions[i].start_lba =
393 HOST_TO_LITTLE32(static_cast<uint32_t>(startLba & 0xFFFFFFFF));
394 msdosRegPartition(pPartitions, i, pDisk);
402 for (
int i = 0; i < MSDOS_PARTTAB_NUM; i++)
405 if ((pPartitions[i].active != 0) && (pPartitions[i].active != 0x80))
407 WARNING(
"Invalid partition record found");
412 if (pPartitions[i].type == g_ExtendedPartitionNumber)
414 uint64_t startLba = LITTLE_TO_HOST32(pPartitions[i].start_lba);
419 if ((buff = pDisk->
read(startLba * 512ULL)) == 0)
422 "Couldn't read next sector for the extended partition.");
426 uint8_t *buffer =
reinterpret_cast<uint8_t *
>(buff);
429 if (buffer[510] != MSDOS_IDENT_1 || buffer[511] != MSDOS_IDENT_2)
431 WARNING(
"Extended partition record read failed.");
437 MsdosPartitionInfo *pReadPartitions =
438 reinterpret_cast<MsdosPartitionInfo *
>(
439 &buffer[MSDOS_PARTTAB_START]);
440 if (!msdosReadExtTable(
441 pReadPartitions, pDisk, MSDOS_PARTTAB_NUM, startLba,
443 WARNING(
"Reading the extended partition table failed");
445 else if (pPartitions[i].type == g_EmptyPartitionNumber)
451 msdosRegPartition(pPartitions, i, pDisk);
461 if ((buff = pDisk->
read(0ULL)) == 0)
463 WARNING(
"Disk read failure during MS-DOS partition table search.");
467 uint8_t *buffer =
reinterpret_cast<uint8_t *
>(buff);
473 if (buffer[510] != MSDOS_IDENT_1 || buffer[511] != MSDOS_IDENT_2)
475 NOTICE(
"MS-DOS partition not found on disk " << diskName);
479 NOTICE(
"MS-DOS partition table found on disk " << diskName);
481 MsdosPartitionInfo *pPartitions =
482 reinterpret_cast<MsdosPartitionInfo *
>(&buffer[MSDOS_PARTTAB_START]);
bool msdosReadTable(MsdosPartitionInfo *pPartitions, Disk *pDisk)
virtual uintptr_t read(uint64_t location)
void addChild(Device *pDevice)
void setParent(Device *p)
virtual void getName(String &str)
bool msdosProbeDisk(Disk *pDisk)