1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-12-21 09:52:09 +00:00

pf2: implement open/close disk

This commit is contained in:
Michael Scire 2020-11-25 23:51:49 -08:00
parent 4466a74e40
commit 75f006b002
7 changed files with 220 additions and 14 deletions

View file

@ -22,4 +22,7 @@ namespace ams::prfile2::pdm {
pdm::Error Initialize(u32 config, void *param); pdm::Error Initialize(u32 config, void *param);
pdm::Error OpenDisk(InitDisk *init_disk_table, HandleType *out);
pdm::Error CloseDisk(HandleType handle);
} }

View file

@ -15,6 +15,7 @@
*/ */
#pragma once #pragma once
#include <vapours/prfile2/prfile2_build_config.hpp> #include <vapours/prfile2/prfile2_build_config.hpp>
#include <vapours/prfile2/prfile2_handle.hpp>
#include <vapours/prfile2/pdm/prfile2_pdm_types.hpp> #include <vapours/prfile2/pdm/prfile2_pdm_types.hpp>
namespace ams::prfile2::pdm { namespace ams::prfile2::pdm {
@ -41,14 +42,14 @@ namespace ams::prfile2::pdm {
}; };
struct FunctionTable { struct FunctionTable {
pdm::Error (*initialize)(Disk *); pdm::Error (*initialize)(HandleType disk_handle);
pdm::Error (*finalize)(Disk *); pdm::Error (*finalize)(HandleType disk_handle);
pdm::Error (*mount)(Disk *); pdm::Error (*mount)(HandleType disk_handle);
pdm::Error (*unmount)(Disk *); pdm::Error (*unmount)(HandleType disk_handle);
pdm::Error (*format)(Disk *, const u8 *); pdm::Error (*format)(HandleType disk_handle, const u8 *);
pdm::Error (*physical_read)(Disk *disk, u8 *dst, u32 block, u32 count, u32 *num_read); pdm::Error (*physical_read)(HandleType disk_handle, u8 *dst, u32 block, u32 count, u32 *num_read);
pdm::Error (*physical_write)(Disk *disk, const u8 *src, u32 block, u32 count, u32 *num_read); pdm::Error (*physical_write)(HandleType disk_handle, const u8 *src, u32 block, u32 count, u32 *num_read);
pdm::Error (*get_disk_info)(Disk *disk, DiskInfo *out); pdm::Error (*get_disk_info)(HandleType disk_handle, DiskInfo *out);
}; };
struct DiskTable { struct DiskTable {

View file

@ -34,12 +34,34 @@ namespace ams::prfile2::pdm {
volatile NonBlockingProtocolType nbc; volatile NonBlockingProtocolType nbc;
volatile NonBlockingProtocolType nbc_detect; volatile NonBlockingProtocolType nbc_detect;
volatile NonBlockingProtocolType nbc_req; volatile NonBlockingProtocolType nbc_req;
template<size_t Ix>
constexpr ALWAYS_INLINE bool GetStatusBit() const {
constexpr u32 Mask = (1u << Ix);
return (this->status & Mask) != 0;
}
template<size_t Ix>
constexpr ALWAYS_INLINE void SetStatusBit(bool en) {
constexpr u32 Mask = (1u << Ix);
if (en) {
this->status |= Mask;
} else {
this->status &= ~Mask;
}
}
constexpr bool IsOpen() const { return this->GetStatusBit<0>(); }
constexpr void SetOpen(bool en) { this->SetStatusBit<0>(en); }
constexpr bool IsLocked() const { return this->GetStatusBit<1>(); }
constexpr void SetLocked(bool en) { this->SetStatusBit<1>(en); }
}; };
namespace disk { namespace disk {
pdm::Error OpenDisk(InitDisk *init_disk_table, Disk **out); pdm::Error OpenDisk(InitDisk *init_disk_table, HandleType *out);
pdm::Error CloseDisk(Disk *disk); pdm::Error CloseDisk(HandleType handle);
/* ... */ /* ... */

View file

@ -40,6 +40,21 @@ namespace ams::prfile2 {
return val; return val;
}; };
constexpr ALWAYS_INLINE u8 GetHandleId(HandleType handle) {
return handle.Get<HandleField::Id>();
}
constexpr ALWAYS_INLINE u16 GetHandleSignature(HandleType handle) {
return handle.Get<HandleField::Signature>();
}
constexpr inline const HandleType InvalidHandle = {};
constexpr ALWAYS_INLINE bool IsInvalidHandle(HandleType handle) {
return handle.value == 0;
}
static_assert(IsInvalidHandle(InvalidHandle));
constexpr ALWAYS_INLINE HandleType ConstructDiskHandle(u8 id, u16 signature) { return ConstructHandle(id, HandleKind_Disk, signature); } constexpr ALWAYS_INLINE HandleType ConstructDiskHandle(u8 id, u16 signature) { return ConstructHandle(id, HandleKind_Disk, signature); }
constexpr ALWAYS_INLINE HandleType ConstructPartitionHandle(u8 id, u16 signature) { return ConstructHandle(id, HandleKind_Partition, signature); } constexpr ALWAYS_INLINE HandleType ConstructPartitionHandle(u8 id, u16 signature) { return ConstructHandle(id, HandleKind_Partition, signature); }

View file

@ -41,4 +41,27 @@ namespace ams::prfile2::pdm {
return pdm::Error_Ok; return pdm::Error_Ok;
} }
pdm::Error OpenDisk(InitDisk *init_disk_table, HandleType *out) {
/* Check the arguments. */
if (out == nullptr) {
return pdm::Error_InvalidParameter;
}
/* Set the output as invalid. */
*out = InvalidHandle;
/* Open the disk. */
return disk::OpenDisk(init_disk_table, out);
}
pdm::Error CloseDisk(HandleType handle) {
/* Check the input. */
if (IsInvalidHandle(handle)) {
return pdm::Error_InvalidParameter;
}
/* Close the disk. */
return disk::CloseDisk(handle);
}
} }

View file

@ -0,0 +1,142 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#if defined(ATMOSPHERE_IS_STRATOSPHERE)
#include <stratosphere.hpp>
#elif defined(ATMOSPHERE_IS_MESOSPHERE)
#include <mesosphere.hpp>
#elif defined(ATMOSPHERE_IS_EXOSPHERE)
#include <exosphere.hpp>
#else
#include <vapours.hpp>
#endif
#include "prfile2_pdm_disk_set.hpp"
namespace ams::prfile2::pdm::disk {
pdm::Error OpenDisk(InitDisk *init_disk_table, HandleType *out) {
/* Check the arguments. */
if (out == nullptr || init_disk_table == nullptr || init_disk_table->function == nullptr) {
return pdm::Error_InvalidParameter;
}
/* Find a free disk holder. */
DiskHolder *holder = nullptr;
for (auto &h : impl::g_disk_set.disk_holders) {
if (h.disk == nullptr) {
holder = std::addressof(h);
break;
}
}
if (holder == nullptr) {
return pdm::Error_NotExistFreeDiskStruct;
}
/* Find a free disk. */
Disk *disk = nullptr;
for (auto &d : impl::g_disk_set.disks) {
if (!d.IsOpen()) {
disk = std::addressof(d);
break;
}
}
if (disk == nullptr) {
return pdm::Error_NotExistFreeDiskStruct;
}
const auto disk_id = disk - impl::g_disk_set.disks;
/* Call the disk initialze function. */
init_disk_table->function(std::addressof(disk->disk_table), init_disk_table->ui_ext);
/* Set the disk as open .*/
disk->SetOpen(true);
/* Set the init disk table. */
disk->init_disk_table = init_disk_table;
/* Note the opened disk. */
++impl::g_disk_set.num_allocated_disks;
/* Increment the disk's signature. */
disk->signature = static_cast<u16>(disk->signature + 1);
/* Increment the disk's open count. */
++disk->open_count;
/* Set the disk in the holder. */
holder->signature = disk->signature;
holder->disk = disk;
/* Set the output handle. */
*out = ConstructDiskHandle(disk_id, disk->signature);
/* If this was the first opening for the disk, initialize it. */
if (disk->open_count == 1) {
if (auto err = disk->disk_table.function_table->initialize(*out); err != pdm::Error_Ok) {
/* Close the disk. */
--disk->open_count;
disk->SetOpen(false);
--impl::g_disk_set.num_allocated_disks;
/* Reset the holder. */
holder->disk = nullptr;
return pdm::Error_DriverError;
}
}
return pdm::Error_Ok;
}
pdm::Error CloseDisk(HandleType handle) {
/* Get the disk. */
Disk *disk = GetDisk(handle);
if (disk == nullptr) {
return pdm::Error_InvalidParameter;
}
/* Check that the disk is open and unlocked. */
if (!disk->IsOpen()) {
return pdm::Error_StateClosed;
}
if (disk->IsLocked()) {
return pdm::Error_StateLocked;
}
/* Close the disk. */
if (disk->open_count == 1) {
/* Finalize the disk. */
if (auto err = disk->disk_table.function_table->finalize(handle); err != pdm::Error_Ok) {
if (auto *part = disk->current_partition; part != nullptr) {
/* TODO: part::SetDriverErrorCode(part, err); */
}
return pdm::Error_DriverError;
}
/* Set the disk as closed. */
disk->SetOpen(false);
--impl::g_disk_set.num_allocated_disks;
/* Clear the disk holder. */
impl::g_disk_set.disk_holders[GetHandleId(handle)].disk = nullptr;
}
/* Decrement the disk's open count. */
--disk->open_count;
return pdm::Error_Ok;
}
}

View file

@ -26,8 +26,8 @@ namespace ams::prfile2::pdm {
ALWAYS_INLINE Disk *GetDisk(HandleType handle) { ALWAYS_INLINE Disk *GetDisk(HandleType handle) {
if (AMS_LIKELY(IsDiskHandle(handle))) { if (AMS_LIKELY(IsDiskHandle(handle))) {
if (const auto id = handle.Get<HandleField::Id>(); AMS_LIKELY(id < MaxDisks)) { if (const auto id = GetHandleId(handle); AMS_LIKELY(id < MaxDisks)) {
const auto signature = handle.Get<HandleField::Signature>(); const auto signature = GetHandleSignature(handle);
Disk *disk = std::addressof(impl::g_disk_set.disks[id]); Disk *disk = std::addressof(impl::g_disk_set.disks[id]);
for (const auto &holder : impl::g_disk_set.disk_holders) { for (const auto &holder : impl::g_disk_set.disk_holders) {
@ -43,8 +43,8 @@ namespace ams::prfile2::pdm {
ALWAYS_INLINE Partition *GetPartition(HandleType handle) { ALWAYS_INLINE Partition *GetPartition(HandleType handle) {
if (AMS_LIKELY(IsPartitionHandle(handle))) { if (AMS_LIKELY(IsPartitionHandle(handle))) {
if (const auto id = handle.Get<HandleField::Id>(); AMS_LIKELY(id < MaxPartitions)) { if (const auto id = GetHandleId(handle); AMS_LIKELY(id < MaxPartitions)) {
const auto signature = handle.Get<HandleField::Signature>(); const auto signature = GetHandleSignature(handle);
Partition *part = std::addressof(impl::g_disk_set.partitions[id]); Partition *part = std::addressof(impl::g_disk_set.partitions[id]);
for (const auto &holder : impl::g_disk_set.partition_holders) { for (const auto &holder : impl::g_disk_set.partition_holders) {