2012-01-21 21:57:41 +01:00
|
|
|
/****************************************************************************
|
2012-09-22 15:47:52 +02:00
|
|
|
* Copyright (C) 2010 by Dimok
|
|
|
|
* (C) 2012 by FIX94
|
2012-01-21 21:57:41 +01:00
|
|
|
*
|
|
|
|
* This software is provided 'as-is', without any express or implied
|
|
|
|
* warranty. In no event will the authors be held liable for any
|
|
|
|
* damages arising from the use of this software.
|
|
|
|
*
|
|
|
|
* Permission is granted to anyone to use this software for any
|
|
|
|
* purpose, including commercial applications, and to alter it and
|
|
|
|
* redistribute it freely, subject to the following restrictions:
|
|
|
|
*
|
|
|
|
* 1. The origin of this software must not be misrepresented; you
|
|
|
|
* must not claim that you wrote the original software. If you use
|
|
|
|
* this software in a product, an acknowledgment in the product
|
|
|
|
* documentation would be appreciated but is not required.
|
|
|
|
*
|
|
|
|
* 2. Altered source versions must be plainly marked as such, and
|
|
|
|
* must not be misrepresented as being the original software.
|
|
|
|
*
|
|
|
|
* 3. This notice may not be removed or altered from any source
|
|
|
|
* distribution.
|
|
|
|
***************************************************************************/
|
|
|
|
#include <gccore.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2012-07-20 22:12:25 +02:00
|
|
|
#include <sdcard/gcsd.h>
|
2012-01-21 21:57:41 +01:00
|
|
|
#include "PartitionHandle.h"
|
|
|
|
#include "ntfs.h"
|
|
|
|
#include "fat.h"
|
|
|
|
#include "ext2.h"
|
2012-07-20 22:12:25 +02:00
|
|
|
#include "sdhc.h"
|
2012-08-05 15:48:15 +02:00
|
|
|
#include "usbstorage.h"
|
2012-11-28 13:05:21 +01:00
|
|
|
#include "gui/text.hpp"
|
2012-08-11 14:27:38 +02:00
|
|
|
#include "loader/nk.h"
|
2012-08-05 15:48:15 +02:00
|
|
|
#include "loader/utils.h"
|
|
|
|
#include "loader/wbfs.h"
|
2012-11-28 13:05:21 +01:00
|
|
|
#include "memory/mem2.hpp"
|
2012-01-21 21:57:41 +01:00
|
|
|
|
|
|
|
#define PARTITION_TYPE_DOS33_EXTENDED 0x05 /* DOS 3.3+ extended partition */
|
|
|
|
#define PARTITION_TYPE_WIN95_EXTENDED 0x0F /* Windows 95 extended partition */
|
2012-07-18 16:09:28 +02:00
|
|
|
|
|
|
|
//! libfat stuff
|
|
|
|
extern "C"
|
|
|
|
{
|
2012-10-01 14:53:49 +02:00
|
|
|
sec_t FindFirstValidPartition(const DISC_INTERFACE* disc);
|
2012-07-18 16:09:28 +02:00
|
|
|
}
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-05-16 16:48:01 +02:00
|
|
|
#define CACHE 32
|
2012-01-21 21:57:41 +01:00
|
|
|
#define SECTORS 64
|
|
|
|
|
2012-10-01 14:53:49 +02:00
|
|
|
static inline const char *PartFromType(int type)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2012-07-18 16:09:28 +02:00
|
|
|
case 0x00: return "Unused";
|
2012-01-21 21:57:41 +01:00
|
|
|
case 0x01: return "FAT12";
|
|
|
|
case 0x04: return "FAT16";
|
|
|
|
case 0x05: return "Extended";
|
|
|
|
case 0x06: return "FAT16";
|
|
|
|
case 0x07: return "NTFS";
|
|
|
|
case 0x0b: return "FAT32";
|
|
|
|
case 0x0c: return "FAT32";
|
|
|
|
case 0x0e: return "FAT16";
|
|
|
|
case 0x0f: return "Extended";
|
|
|
|
case 0x82: return "LxSWP";
|
|
|
|
case 0x83: return "LINUX";
|
|
|
|
case 0x8e: return "LxLVM";
|
|
|
|
case 0xa8: return "OSX";
|
|
|
|
case 0xab: return "OSXBT";
|
|
|
|
case 0xaf: return "OSXHF";
|
2012-07-18 16:09:28 +02:00
|
|
|
case 0xbf: return "WBFS";
|
2012-01-21 21:57:41 +01:00
|
|
|
case 0xe8: return "LUKS";
|
|
|
|
default: return "Unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-15 21:16:14 +02:00
|
|
|
void PartitionHandle::Init()
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-10-15 21:16:14 +02:00
|
|
|
interface = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PartitionHandle::SetDevice(const DISC_INTERFACE *discio)
|
|
|
|
{
|
|
|
|
Cleanup();
|
|
|
|
interface = discio;
|
|
|
|
|
2012-01-21 21:57:41 +01:00
|
|
|
// Sanity check
|
2012-10-15 21:16:14 +02:00
|
|
|
if(!interface)
|
2012-07-18 16:09:28 +02:00
|
|
|
return;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
|
|
|
// Start the device and check that it is inserted
|
2012-10-15 21:16:14 +02:00
|
|
|
if(!interface->startup())
|
2012-07-18 16:09:28 +02:00
|
|
|
return;
|
|
|
|
|
2012-10-15 21:16:14 +02:00
|
|
|
if(!interface->isInserted())
|
2012-07-18 16:09:28 +02:00
|
|
|
return;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
|
|
|
FindPartitions();
|
|
|
|
}
|
|
|
|
|
2012-10-15 21:16:14 +02:00
|
|
|
void PartitionHandle::Cleanup()
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-10-15 21:16:14 +02:00
|
|
|
UnMountAll();
|
|
|
|
if(interface != NULL)
|
2012-07-15 02:05:23 +02:00
|
|
|
interface->shutdown();
|
2012-10-15 21:16:14 +02:00
|
|
|
interface = NULL;
|
|
|
|
PartitionList.clear();
|
|
|
|
MountNameList.clear();
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PartitionHandle::IsMounted(int pos)
|
|
|
|
{
|
|
|
|
if(pos < 0 || pos >= (int) MountNameList.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(MountNameList[pos].size() == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-28 13:05:21 +01:00
|
|
|
bool PartitionHandle::Mount(int pos, const char *name, bool forceFAT)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-10-01 14:53:49 +02:00
|
|
|
if(valid(pos))
|
|
|
|
UnMount(pos);
|
2012-07-18 16:09:28 +02:00
|
|
|
|
2022-01-14 22:10:56 +01:00
|
|
|
if(!name || strlen(name) > 8)
|
2012-07-18 16:09:28 +02:00
|
|
|
return false;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-11-28 13:05:21 +01:00
|
|
|
if(pos >= (int)MountNameList.size())
|
|
|
|
MountNameList.resize(pos + 1);
|
2012-01-21 21:57:41 +01:00
|
|
|
|
|
|
|
MountNameList[pos] = name;
|
2013-09-24 17:21:31 +02:00
|
|
|
char DeviceSyn[10];
|
2022-01-14 22:10:56 +01:00
|
|
|
strcpy(DeviceSyn, name);
|
2013-09-24 17:21:31 +02:00
|
|
|
strcat(DeviceSyn, ":");
|
|
|
|
|
2012-07-18 16:09:28 +02:00
|
|
|
//! Some stupid partition manager think they don't need to edit the freaken MBR.
|
|
|
|
//! So we need to check the first 64 sectors and see if some partition is there.
|
|
|
|
//! libfat does that by default so let's use it.
|
2012-10-01 14:53:49 +02:00
|
|
|
if(forceFAT && (strlen(GetFSName(pos)) == 0 || strcmp(GetFSName(pos), "Unknown") == 0))
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-11-28 13:05:21 +01:00
|
|
|
if(fatMount(MountNameList[pos].c_str(), interface, 0, CACHE, SECTORS))
|
2012-07-18 16:09:28 +02:00
|
|
|
{
|
|
|
|
sec_t FAT_startSector = FindFirstValidPartition(interface);
|
|
|
|
AddPartition("FAT", FAT_startSector, 0xdeadbeaf, true, 0x0c, 0);
|
2012-11-28 13:05:21 +01:00
|
|
|
gprintf("FAT Partition at %s (forceFAT) mounted.\n", DeviceSyn);
|
2012-10-01 14:53:49 +02:00
|
|
|
SetWbfsHandle(pos, NULL);
|
2012-01-21 21:57:41 +01:00
|
|
|
return true;
|
2012-07-18 16:09:28 +02:00
|
|
|
}
|
|
|
|
}
|
2012-10-01 14:53:49 +02:00
|
|
|
if(!valid(pos))
|
|
|
|
return false;
|
2012-07-18 16:09:28 +02:00
|
|
|
|
2012-10-01 14:53:49 +02:00
|
|
|
SetWbfsHandle(pos, NULL);
|
2012-07-18 16:09:28 +02:00
|
|
|
if(strncmp(GetFSName(pos), "FAT", 3) == 0 || strcmp(GetFSName(pos), "GUID-Entry") == 0)
|
|
|
|
{
|
2012-11-28 13:05:21 +01:00
|
|
|
if(fatMount(MountNameList[pos].c_str(), interface, GetLBAStart(pos), CACHE, SECTORS))
|
2012-07-18 16:09:28 +02:00
|
|
|
{
|
2012-11-28 13:05:21 +01:00
|
|
|
gprintf("FAT Partition at %s mounted.\n", DeviceSyn);
|
|
|
|
PartitionList[pos].FSName = "FAT";
|
2012-07-18 16:09:28 +02:00
|
|
|
return true;
|
|
|
|
}
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
2012-07-20 23:18:29 +02:00
|
|
|
else if(strncmp(GetFSName(pos), "NTFS", 4) == 0 || strcmp(GetFSName(pos), "GUID-Entry") == 0)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-07-18 16:09:28 +02:00
|
|
|
if(ntfsMount(MountNameList[pos].c_str(), interface, GetLBAStart(pos), CACHE, SECTORS, NTFS_SHOW_HIDDEN_FILES | NTFS_RECOVER))
|
|
|
|
{
|
2012-11-28 13:05:21 +01:00
|
|
|
gprintf("NTFS Partition at %s mounted.\n", DeviceSyn);
|
2012-07-18 16:09:28 +02:00
|
|
|
PartitionList[pos].FSName = "NTFS";
|
2012-01-21 21:57:41 +01:00
|
|
|
return true;
|
2012-07-18 16:09:28 +02:00
|
|
|
}
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
2012-07-20 23:18:29 +02:00
|
|
|
else if(strncmp(GetFSName(pos), "LINUX", 5) == 0 || strcmp(GetFSName(pos), "GUID-Entry") == 0)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
if(ext2Mount(MountNameList[pos].c_str(), interface, GetLBAStart(pos), CACHE, SECTORS, EXT2_FLAG_DEFAULT))
|
2012-07-18 16:09:28 +02:00
|
|
|
{
|
2012-11-28 13:05:21 +01:00
|
|
|
gprintf("EXT Partition at %s mounted.\n", DeviceSyn);
|
2012-07-18 16:09:28 +02:00
|
|
|
PartitionList[pos].FSName = "LINUX";
|
2012-01-21 21:57:41 +01:00
|
|
|
return true;
|
2012-07-18 16:09:28 +02:00
|
|
|
}
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
else if(strncmp(GetFSName(pos), "WBFS", 4) == 0)
|
|
|
|
{
|
2012-07-18 16:09:28 +02:00
|
|
|
if(interface == &__io_usbstorage2_port0 || interface == &__io_usbstorage2_port1)
|
|
|
|
SetWbfsHandle(pos, wbfs_open_partition(__WBFS_ReadUSB, __WBFS_WriteUSB, NULL, USBStorage2_GetSectorSize(), GetSecCount(pos), GetLBAStart(pos), 0));
|
2012-07-20 22:12:25 +02:00
|
|
|
else if(interface == &__io_sdhc)
|
2012-07-18 16:09:28 +02:00
|
|
|
SetWbfsHandle(pos, wbfs_open_partition(__WBFS_ReadSDHC, __WBFS_WriteSDHC, NULL, 512, GetSecCount(pos), GetLBAStart(pos), 0));
|
|
|
|
if(GetWbfsHandle(pos))
|
2012-07-20 23:18:29 +02:00
|
|
|
{
|
2012-11-28 13:05:21 +01:00
|
|
|
gprintf("WBFS Partition at %s mounted.\n", DeviceSyn);
|
2012-07-20 23:18:29 +02:00
|
|
|
PartitionList[pos].FSName = "WBFS";
|
2012-07-18 16:09:28 +02:00
|
|
|
return true;
|
2012-07-20 23:18:29 +02:00
|
|
|
}
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
2012-11-28 13:05:21 +01:00
|
|
|
/* FAIL */
|
2012-01-21 21:57:41 +01:00
|
|
|
MountNameList[pos].clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PartitionHandle::UnMount(int pos)
|
|
|
|
{
|
2020-03-19 23:38:32 +01:00
|
|
|
if(!interface || (pos < 0 || pos >= (int)MountNameList.size()) || (MountNameList[pos].size() == 0))
|
2012-07-18 16:09:28 +02:00
|
|
|
return;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
|
|
|
WBFS_Close();
|
2013-09-24 17:21:31 +02:00
|
|
|
char DeviceSyn[10];
|
2020-03-19 23:38:32 +01:00
|
|
|
strcpy(DeviceSyn, MountName(pos));
|
2013-09-24 17:21:31 +02:00
|
|
|
strcat(DeviceSyn, ":");
|
|
|
|
DeviceSyn[9] = '\0';
|
|
|
|
|
2022-09-08 23:33:45 +02:00
|
|
|
const char *FSName = GetFSName(pos);
|
|
|
|
if(strncmp(FSName, "WBFS", 4) == 0)
|
2012-11-28 13:05:21 +01:00
|
|
|
{
|
|
|
|
wbfs_t *wbfshandle = GetWbfsHandle(pos);
|
|
|
|
if(wbfshandle) wbfs_close(wbfshandle);
|
|
|
|
gprintf("WBFS Partition at %s unmounted.\n", DeviceSyn);
|
|
|
|
}
|
2022-09-08 23:33:45 +02:00
|
|
|
else if(strncmp(FSName, "FAT", 3) == 0)
|
2012-11-28 13:05:21 +01:00
|
|
|
{
|
|
|
|
fatUnmount(DeviceSyn);
|
|
|
|
gprintf("FAT Partition at %s unmounted.\n", DeviceSyn);
|
|
|
|
}
|
2022-09-08 23:33:45 +02:00
|
|
|
else if(strncmp(FSName, "NTFS", 4) == 0)
|
2012-11-28 13:05:21 +01:00
|
|
|
{
|
|
|
|
ntfsUnmount(DeviceSyn, true);
|
|
|
|
gprintf("NTFS Partition at %s unmounted.\n", DeviceSyn);
|
|
|
|
}
|
2022-09-08 23:33:45 +02:00
|
|
|
else if(strncmp(FSName, "LINUX", 5) == 0)
|
2012-11-28 13:05:21 +01:00
|
|
|
{
|
|
|
|
ext2Unmount(DeviceSyn);
|
|
|
|
gprintf("EXT Partition at %s unmounted.\n", DeviceSyn);
|
|
|
|
}
|
|
|
|
/* Remove name from list */
|
2012-01-21 21:57:41 +01:00
|
|
|
MountNameList[pos].clear();
|
2012-11-28 13:05:21 +01:00
|
|
|
SetWbfsHandle(pos, NULL);
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-07-18 16:09:28 +02:00
|
|
|
bool PartitionHandle::IsExisting(u64 lba)
|
|
|
|
{
|
|
|
|
for(u32 i = 0; i < PartitionList.size(); ++i)
|
|
|
|
{
|
|
|
|
if(PartitionList[i].LBA_Start == lba)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-01 14:53:49 +02:00
|
|
|
s8 PartitionHandle::FindPartitions()
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-11-28 13:05:21 +01:00
|
|
|
MASTER_BOOT_RECORD *mbr = (MASTER_BOOT_RECORD*)MEM2_alloc(MAX_BYTES_PER_SECTOR);
|
|
|
|
if(mbr == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2012-01-21 21:57:41 +01:00
|
|
|
// Read the first sector on the device
|
2012-10-01 14:53:49 +02:00
|
|
|
if(!interface->readSectors(0, 1, mbr))
|
2012-11-28 13:05:21 +01:00
|
|
|
{
|
|
|
|
MEM2_free(mbr);
|
2012-07-18 16:09:28 +02:00
|
|
|
return -1;
|
2012-11-28 13:05:21 +01:00
|
|
|
}
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-07-18 16:09:28 +02:00
|
|
|
// If this is the devices master boot record
|
2013-08-09 15:58:06 +02:00
|
|
|
if(mbr->signature != MBR_SIGNATURE && mbr->signature != MBR_SIGNATURE_MOD)
|
2012-11-28 13:05:21 +01:00
|
|
|
{
|
|
|
|
MEM2_free(mbr);
|
2012-07-18 16:09:28 +02:00
|
|
|
return -1;
|
2012-11-28 13:05:21 +01:00
|
|
|
}
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-10-01 14:53:49 +02:00
|
|
|
for(u8 i = 0; i < 4; i++)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-10-01 14:53:49 +02:00
|
|
|
PARTITION_RECORD *partition = (PARTITION_RECORD *)&mbr->partitions[i];
|
2012-07-18 16:09:28 +02:00
|
|
|
|
|
|
|
if(partition->type == PARTITION_TYPE_GPT)
|
2012-04-23 18:41:00 +02:00
|
|
|
{
|
2019-11-18 00:47:24 +01:00
|
|
|
s8 ret = CheckGPT();
|
2012-07-18 16:09:28 +02:00
|
|
|
if(ret == 0) // if it's a GPT we don't need to go on looking through the mbr anymore
|
2020-10-29 00:36:11 +01:00
|
|
|
break;
|
2012-04-23 18:41:00 +02:00
|
|
|
}
|
2012-07-18 16:09:28 +02:00
|
|
|
if(partition->type == PARTITION_TYPE_DOS33_EXTENDED || partition->type == PARTITION_TYPE_WIN95_EXTENDED)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
CheckEBR(i, le32(partition->lba_start));
|
|
|
|
continue;
|
|
|
|
}
|
2012-07-18 16:09:28 +02:00
|
|
|
if(le32(partition->block_count) > 0 && !IsExisting(le32(partition->lba_start)))
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-07-18 16:09:28 +02:00
|
|
|
AddPartition(PartFromType(partition->type), le32(partition->lba_start),
|
2012-09-22 15:47:52 +02:00
|
|
|
le32(partition->block_count), (partition->status == PARTITION_BOOTABLE), partition->type, i);
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
}
|
2012-11-28 13:05:21 +01:00
|
|
|
MEM2_free(mbr);
|
2012-07-18 16:09:28 +02:00
|
|
|
return 0;
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PartitionHandle::CheckEBR(u8 PartNum, sec_t ebr_lba)
|
|
|
|
{
|
2012-11-28 13:05:21 +01:00
|
|
|
EXTENDED_BOOT_RECORD *ebr = (EXTENDED_BOOT_RECORD*)MEM2_alloc(MAX_BYTES_PER_SECTOR);
|
|
|
|
if(ebr == NULL)
|
|
|
|
return;
|
2012-01-21 21:57:41 +01:00
|
|
|
sec_t next_erb_lba = 0;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
// Read and validate the extended boot record
|
2012-10-01 14:53:49 +02:00
|
|
|
if(!interface->readSectors(ebr_lba + next_erb_lba, 1, ebr))
|
2012-11-28 13:05:21 +01:00
|
|
|
{
|
|
|
|
MEM2_free(ebr);
|
2012-04-23 18:41:00 +02:00
|
|
|
return;
|
2012-11-28 13:05:21 +01:00
|
|
|
}
|
2013-08-09 15:58:06 +02:00
|
|
|
if(ebr->signature != EBR_SIGNATURE && ebr->signature != EBR_SIGNATURE_MOD)
|
2012-11-28 13:05:21 +01:00
|
|
|
{
|
|
|
|
MEM2_free(ebr);
|
2012-04-23 18:41:00 +02:00
|
|
|
return;
|
2012-11-28 13:05:21 +01:00
|
|
|
}
|
2012-04-23 18:41:00 +02:00
|
|
|
|
2012-07-18 16:09:28 +02:00
|
|
|
if(le32(ebr->partition.block_count) > 0 && !IsExisting(ebr_lba + next_erb_lba + le32(ebr->partition.lba_start)))
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-07-18 16:09:28 +02:00
|
|
|
AddPartition(PartFromType(ebr->partition.type), ebr_lba + next_erb_lba + le32(ebr->partition.lba_start),
|
2012-09-22 15:47:52 +02:00
|
|
|
le32(ebr->partition.block_count), (ebr->partition.status == PARTITION_BOOTABLE), ebr->partition.type, PartNum);
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
// Get the start sector of the current partition
|
|
|
|
// and the next extended boot record in the chain
|
2012-04-23 18:41:00 +02:00
|
|
|
next_erb_lba = le32(ebr->next_ebr.lba_start);
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
while(next_erb_lba > 0);
|
2012-11-28 13:05:21 +01:00
|
|
|
|
|
|
|
MEM2_free(ebr);
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-07-18 16:09:28 +02:00
|
|
|
static const u8 TYPE_UNUSED[16] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
|
|
|
|
static const u8 TYPE_BIOS[16] = { 0x48,0x61,0x68,0x21,0x49,0x64,0x6F,0x6E,0x74,0x4E,0x65,0x65,0x64,0x45,0x46,0x49 };
|
|
|
|
static const u8 TYPE_LINUX_MS_BASIC_DATA[16] = { 0xA2,0xA0,0xD0,0xEB,0xE5,0xB9,0x33,0x44,0x87,0xC0,0x68,0xB6,0xB7,0x26,0x99,0xC7 };
|
|
|
|
|
2019-11-18 00:47:24 +01:00
|
|
|
s8 PartitionHandle::CheckGPT()
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-11-28 13:05:21 +01:00
|
|
|
GPT_HEADER *gpt_header = (GPT_HEADER*)MEM2_alloc(MAX_BYTES_PER_SECTOR);
|
|
|
|
if(gpt_header == NULL)
|
|
|
|
return -1;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-07-18 16:09:28 +02:00
|
|
|
// Read and validate the extended boot record
|
2012-10-01 14:53:49 +02:00
|
|
|
if(!interface->readSectors(1, 1, gpt_header))
|
2012-11-28 13:05:21 +01:00
|
|
|
{
|
|
|
|
MEM2_free(gpt_header);
|
2012-07-18 16:09:28 +02:00
|
|
|
return -1;
|
2012-11-28 13:05:21 +01:00
|
|
|
}
|
2012-07-18 16:09:28 +02:00
|
|
|
if(strncmp(gpt_header->magic, "EFI PART", 8) != 0)
|
2012-11-28 13:05:21 +01:00
|
|
|
{
|
|
|
|
MEM2_free(gpt_header);
|
2012-07-18 16:09:28 +02:00
|
|
|
return -1;
|
2012-11-28 13:05:21 +01:00
|
|
|
}
|
2012-04-23 18:41:00 +02:00
|
|
|
|
2012-07-18 16:09:28 +02:00
|
|
|
gpt_header->part_table_lba = le64(gpt_header->part_table_lba);
|
|
|
|
gpt_header->part_entries = le32(gpt_header->part_entries);
|
|
|
|
gpt_header->part_entry_size = le32(gpt_header->part_entry_size);
|
|
|
|
gpt_header->part_entry_checksum = le32(gpt_header->part_entry_checksum);
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-11-28 13:05:21 +01:00
|
|
|
u8 *sector_buf = (u8*)MEM2_alloc(MAX_BYTES_PER_SECTOR);
|
|
|
|
if(sector_buf == NULL)
|
|
|
|
{
|
|
|
|
MEM2_free(gpt_header);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-07-18 16:09:28 +02:00
|
|
|
u64 next_lba = gpt_header->part_table_lba;
|
|
|
|
for(u32 i = 0; i < gpt_header->part_entries; ++i)
|
|
|
|
{
|
2012-11-28 13:05:21 +01:00
|
|
|
if(!interface->readSectors(next_lba, 1, sector_buf))
|
2012-07-18 16:09:28 +02:00
|
|
|
break;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-07-18 16:09:28 +02:00
|
|
|
for(u32 n = 0; n < BYTES_PER_SECTOR/gpt_header->part_entry_size; ++n, ++i)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-11-28 13:05:21 +01:00
|
|
|
GUID_PART_ENTRY *part_entry = (GUID_PART_ENTRY*)(sector_buf+gpt_header->part_entry_size*n);
|
2012-07-18 16:09:28 +02:00
|
|
|
|
|
|
|
if(memcmp(part_entry->part_type_guid, TYPE_UNUSED, 16) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(IsExisting(le64(part_entry->part_first_lba)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
bool bootable = (memcmp(part_entry->part_type_guid, TYPE_BIOS, 16) == 0);
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2019-11-18 00:47:24 +01:00
|
|
|
AddPartition("GUID-Entry", le64(part_entry->part_first_lba), le64(part_entry->part_last_lba)-le64(part_entry->part_first_lba), bootable, PARTITION_TYPE_GPT, i);
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
2012-07-18 16:09:28 +02:00
|
|
|
next_lba++;
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
2012-11-28 13:05:21 +01:00
|
|
|
MEM2_free(sector_buf);
|
|
|
|
MEM2_free(gpt_header);
|
2012-07-18 16:09:28 +02:00
|
|
|
return 0;
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-10-01 14:53:49 +02:00
|
|
|
void PartitionHandle::AddPartition(const char *name, u64 lba_start, u64 sec_count, bool bootable, u8 part_type, u8 part_num)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-11-28 13:05:21 +01:00
|
|
|
u8 *buffer = (u8*)MEM2_alloc(MAX_BYTES_PER_SECTOR);
|
|
|
|
if(buffer == NULL)
|
2012-07-18 16:09:28 +02:00
|
|
|
return;
|
|
|
|
|
2012-11-28 13:05:21 +01:00
|
|
|
if(!interface->readSectors(lba_start, 1, buffer))
|
|
|
|
{
|
|
|
|
MEM2_free(buffer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
wbfs_head_t *head = (wbfs_head_t*)buffer;
|
2012-07-18 16:09:28 +02:00
|
|
|
|
2012-10-01 14:53:49 +02:00
|
|
|
if(head->magic == wbfs_htonl(WBFS_MAGIC))
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-07-18 16:09:28 +02:00
|
|
|
name = "WBFS";
|
|
|
|
part_type = 0xBF; //Override partition type on WBFS
|
|
|
|
//! correct sector size in physical sectors (512 bytes per sector)
|
|
|
|
sec_count = (u64) head->n_hd_sec * (u64) (1 << head->hd_sec_sz_s) / (u64) BYTES_PER_SECTOR;
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
2012-11-28 13:05:21 +01:00
|
|
|
else if(*((u16 *)(buffer + 0x1FE)) == 0x55AA)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-11-28 13:05:21 +01:00
|
|
|
//! Partition type can be missleading the correct partition format. Stupid lazy ass Partition Editors.
|
|
|
|
if((memcmp(buffer + 0x36, "FAT", 3) == 0 || memcmp(buffer + 0x52, "FAT", 3) == 0) &&
|
2012-07-18 16:09:28 +02:00
|
|
|
strncmp(PartFromType(part_type), "FAT", 3) != 0)
|
|
|
|
{
|
|
|
|
name = "FAT32";
|
|
|
|
part_type = 0x0c;
|
|
|
|
}
|
2012-11-28 13:05:21 +01:00
|
|
|
if(memcmp(buffer + 0x03, "NTFS", 4) == 0)
|
2012-07-18 16:09:28 +02:00
|
|
|
{
|
|
|
|
name = "NTFS";
|
|
|
|
part_type = 0x07;
|
|
|
|
}
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-10-01 14:53:49 +02:00
|
|
|
PartitionFS PartitionEntry;
|
|
|
|
PartitionEntry.FSName = name;
|
|
|
|
PartitionEntry.LBA_Start = lba_start;
|
|
|
|
PartitionEntry.SecCount = sec_count;
|
|
|
|
PartitionEntry.Bootable = bootable;
|
|
|
|
PartitionEntry.PartitionType = part_type;
|
|
|
|
PartitionEntry.PartitionNum = part_num;
|
|
|
|
PartitionList.push_back(PartitionEntry);
|
2012-11-28 13:05:21 +01:00
|
|
|
MEM2_free(buffer);
|
2012-10-01 14:53:49 +02:00
|
|
|
}
|
2012-07-18 16:09:28 +02:00
|
|
|
|
2012-10-01 14:53:49 +02:00
|
|
|
wbfs_t *PartitionHandle::GetWbfsHandle(int pos)
|
|
|
|
{
|
|
|
|
if(valid(pos))
|
|
|
|
return PartitionList[pos].wbfshandle;
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-07-18 16:09:28 +02:00
|
|
|
|
2012-10-01 14:53:49 +02:00
|
|
|
bool PartitionHandle::SetWbfsHandle(int pos, wbfs_t *wbfshandle)
|
|
|
|
{
|
|
|
|
if(valid(pos))
|
|
|
|
{
|
|
|
|
PartitionList[pos].wbfshandle = wbfshandle;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-04-23 18:41:00 +02:00
|
|
|
}
|