mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2025-04-22 04:24:48 +00:00
Initial rewritten code
This commit is contained in:
commit
d8dd06d1df
633 changed files with 56447 additions and 6444 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -38,6 +38,9 @@
|
|||
*.x86_64
|
||||
*.hex
|
||||
|
||||
# Deko3d shaders
|
||||
*.dksh
|
||||
|
||||
# Switch Executables
|
||||
*.nso
|
||||
*.nro
|
||||
|
|
5
Makefile
5
Makefile
|
@ -65,6 +65,7 @@ dist-no-debug: all
|
|||
mkdir -p atmosphere-$(AMSVER)/atmosphere/contents/0100000000000037
|
||||
mkdir -p atmosphere-$(AMSVER)/atmosphere/contents/010000000000003C
|
||||
mkdir -p atmosphere-$(AMSVER)/atmosphere/contents/0100000000000015
|
||||
mkdir -p atmosphere-$(AMSVER)/atmosphere/contents/0100000000000042
|
||||
mkdir -p atmosphere-$(AMSVER)/atmosphere/fatal_errors
|
||||
mkdir -p atmosphere-$(AMSVER)/atmosphere/config_templates
|
||||
mkdir -p atmosphere-$(AMSVER)/atmosphere/config
|
||||
|
@ -93,11 +94,13 @@ dist-no-debug: all
|
|||
cp stratosphere/ro/ro.nsp atmosphere-$(AMSVER)/atmosphere/contents/0100000000000037/exefs.nsp
|
||||
cp stratosphere/jpegdec/jpegdec.nsp atmosphere-$(AMSVER)/atmosphere/contents/010000000000003C/exefs.nsp
|
||||
cp stratosphere/lm/lm.nsp atmosphere-$(AMSVER)/atmosphere/contents/0100000000000015/exefs.nsp
|
||||
cp stratosphere/pgl/pgl.nsp atmosphere-$(AMSVER)/atmosphere/contents/0100000000000042/exefs.nsp
|
||||
mkdir -p atmosphere-$(AMSVER)/atmosphere/contents/0100000000000032/flags
|
||||
touch atmosphere-$(AMSVER)/atmosphere/contents/0100000000000032/flags/boot2.flag
|
||||
mkdir -p atmosphere-$(AMSVER)/atmosphere/contents/0100000000000037/flags
|
||||
touch atmosphere-$(AMSVER)/atmosphere/contents/0100000000000037/flags/boot2.flag
|
||||
cp troposphere/reboot_to_payload/reboot_to_payload.nro atmosphere-$(AMSVER)/switch/reboot_to_payload.nro
|
||||
cp troposphere/daybreak/daybreak.nro atmosphere-$(AMSVER)/switch/daybreak.nro
|
||||
cd atmosphere-$(AMSVER); zip -r ../atmosphere-$(AMSVER).zip ./*; cd ../;
|
||||
rm -r atmosphere-$(AMSVER)
|
||||
mkdir out
|
||||
|
@ -144,6 +147,8 @@ dist: dist-no-debug
|
|||
cp stratosphere/erpt/erpt.elf atmosphere-$(AMSVER)-debug/erpt.elf
|
||||
cp stratosphere/jpegdec/jpegdec.elf atmosphere-$(AMSVER)-debug/jpegdec.elf
|
||||
cp stratosphere/lm/lm.elf atmosphere-$(AMSVER)-debug/lm.elf
|
||||
cp stratosphere/pgl/pgl.elf atmosphere-$(AMSVER)-debug/pgl.elf
|
||||
cp troposphere/daybreak/daybreak.elf atmosphere-$(AMSVER)-debug/daybreak.elf
|
||||
cd atmosphere-$(AMSVER)-debug; zip -r ../atmosphere-$(AMSVER)-debug.zip ./*; cd ../;
|
||||
rm -r atmosphere-$(AMSVER)-debug
|
||||
mv atmosphere-$(AMSVER)-debug.zip out/atmosphere-$(AMSVER)-debug.zip
|
||||
|
|
|
@ -1,4 +1,31 @@
|
|||
# Changelog
|
||||
## 0.14.1
|
||||
+ An issue was fixed in 0.14.0 that would cause a black screen on boot when the INI1's size was not aligned to 8 bytes.
|
||||
+ General system stability improvements to enhance the user's experience.
|
||||
## 0.14.0
|
||||
+ An API (`ams:su`) was added to allow homebrew to safely install system upgrades or downgrades.
|
||||
+ This is a re-implementation of the logic that `ns` uses to install gamecard system updates.
|
||||
+ Nintendo (and now atmosphère) uses an installation process that can recover no matter where a failure occurs, which should significantly improve the safety of custom system update installation.
|
||||
+ Support was added to `exosphère` for running on Mariko hardware.
|
||||
+ **Please note**: Atmosphère still does not support Mariko, and should not be run on Mariko yet.
|
||||
+ Certain stratosphere components do not handle mariko-specific logic fully correctly yet, and may initialize or interact with hardware incorrectly.
|
||||
+ This will be fixed and support will be added over the remainder of the Summer.
|
||||
+ A homebrew application (`daybreak`) was added that uses the system updater API (with thanks to @Adubbz for both design and implementation).
|
||||
+ `daybreak` is included with atmosphère, and functions as a safer/more accurate equivalent to e.g. ChoiDujourNX.
|
||||
+ Upgrades/downgrades can be installed from a folder containing the update NCAs on the SD card.
|
||||
+ Because the update logic functions identically to Nintendo's, `daybreak` will be safe to use on Mariko when the rest of atmosphère has support.
|
||||
+ **Please note**: Daybreak requires that meta (.cnmt) NCAs have the correct extension `.cnmt.nca`.
|
||||
+ This is because gamecard system update logic uses extension to determine whether to mount the content.
|
||||
+ [Several](https://gist.github.com/HookedBehemoth/df36b5970e1c5b1b512ec7bdd9043c6e) [scripts](https://gist.github.com/antiKk/279966c27fdfd9c7fe63b4ae410f89c4) have been made by community members to automatically rename folders with incorrect extensions.
|
||||
+ A bug was fixed that would cause file-based emummc to throw an error (showing a hexdump) on boot.
|
||||
+ Major thanks to @hexkyz for tracking down and resolving this.
|
||||
+ A number of minor issues were resolved, including:
|
||||
+ fusee now prints information to the screen when an error occurs, instead of getting stuck trying to initialize the display.
|
||||
+ A race condition in Horizon was worked around that could prevent boot under certain circumstances.
|
||||
+ A bug was fixed that would cause atmosphère modules to open ten copies of certain filesystems instead of one.
|
||||
+ This could cause object exhaustion under certain circumstances.
|
||||
+ For those interested in atmosphère's future development plans, the project's [roadmap](https://github.com/Atmosphere-NX/Atmosphere/blob/ac9832c5ce7be5832f6d29f6564a9c03e7efd22f/docs/roadmap.md) was updated.
|
||||
+ General system stability improvements to enhance the user's experience.
|
||||
## 0.13.0
|
||||
+ `exosphère`, atmosphère's secure monitor re-implementation, was completely re-written.
|
||||
+ `exosphère` was the first component authored for the project in early 2018. It is written in C, and in a style very different from the rest of atmosphère's code.
|
||||
|
|
|
@ -1,15 +1,10 @@
|
|||
# Planned Features
|
||||
atmosphère has a number of features that are either works-in-progress or planned. Please note that while time-estimates are given, they are loose, and things may be completed sooner or later than advertised.
|
||||
|
||||
The following descriptions were last updated on June 15th, 2020.
|
||||
|
||||
## system updater api
|
||||
* **Description**: A planned extension api for stratosphere (tenatively `ams:su`), this will provide an interface for homebrew to safely install system upgrades or downgrades. This will allow for much more easily transitioning safely between different versions of the operating system.
|
||||
* **Development Status**: Backend/implementation completed; final stages (user-facing ipc api) to be written by SciresM.
|
||||
* **Estimated Time**: June 2020
|
||||
The following descriptions were last updated on July 7th, 2020.
|
||||
|
||||
## ams-on-mariko
|
||||
* **Description**: Atmosphere cannot run as-is on Mariko hardware. A large number of changes are needed in many components. Although exosphere's rewrite laid most groundwork on the secure monitor side, there is still work to do there -- and additional work is needed on the bootloader and stratosphere sides as well. Mariko support will also require further design thought; atmosphere's debugging design heavily relies on reboot-to-payload and (more generally) the ability to perform warmboot bootrom hax at will. This is not possible on Mariko, and will require a new design/software support for whatever solution is chosen.
|
||||
* **Description**: Atmosphere cannot run as-is on Mariko hardware. A large number of changes are needed in many components. Although secure monitor support is complete in exosphere, additional work is needed on the bootloader and stratosphere sides as well. Mariko support will also require further design thought; atmosphere's debugging design heavily relies on reboot-to-payload and (more generally) the ability to perform warmboot bootrom hax at will. This is not possible on Mariko, and will require a new design/software support for whatever solution is chosen.
|
||||
* **Development Status**: Planned.
|
||||
* **Estimated Time**: Summer 2020
|
||||
|
||||
|
@ -20,7 +15,7 @@ The following descriptions were last updated on June 15th, 2020.
|
|||
|
||||
## mesosphere
|
||||
* **Description**: mesosphère is a reimplementation of the Horizon operating system's Kernel. It aims to provide an open-source reference for Nintendo's code.
|
||||
* **Development Status**: Under semi-active development by SciresM; temporarily on pause while the System Updater API is completed.
|
||||
* **Development Status**: Under active development by SciresM.
|
||||
* **Estimated Time**: Mid-to-Late 2020
|
||||
|
||||
## tma reimplementation
|
||||
|
@ -47,3 +42,21 @@ The following descriptions were last updated on June 15th, 2020.
|
|||
* **Description**: General system stability improvements to enhance the user's experience.
|
||||
* **Development Status**: Undergoing active development by all members of the atmosphère team.
|
||||
* **Estimated Time**: June 15th.
|
||||
|
||||
# Completed features
|
||||
|
||||
The following features were previously included under the planned features section and are now complete.
|
||||
|
||||
Please note that this is not an exhaustive list of features present in atmosphère, and only serves to indicate what from the above has been completed.
|
||||
|
||||
## system updater homebrew
|
||||
* **Description**: A user homebrew making use of the new system updater api, so that users can actually use the new api in practice.
|
||||
* **Completion Time**: July 2020
|
||||
|
||||
## system updater api
|
||||
* **Description**: A planned extension api for stratosphere (tenatively `ams:su`), this will provide an interface for homebrew to safely install system upgrades or downgrades. This will allow for much more easily transitioning safely between different versions of the operating system.
|
||||
* **Completion Time**: June 2020
|
||||
|
||||
## exosphere re-write
|
||||
* **Description**: exosphère, atmosphère's reimplementation of Horizon's Secure Monitor, was the first component authored for the project in early 2018. It is written in C, and in a style very different from the rest of atmosphère's code. In addition, exosphère was written to conform to constraints that no longer apply in an environment where it is not launched from the web browser, and where using a custom firmware image to orchestrate wake-from-sleep is possible. exosphère currently uses all but 1 KB of the space available to it, putting it at risk of breaking as future firmware updates are supported. A re-write will solve these issues.
|
||||
* **Completion Time**: June 2020
|
|
@ -48,7 +48,12 @@ namespace ams::secmon::boot {
|
|||
const auto pmc = MemoryRegionVirtualDevicePmc.GetAddress();
|
||||
|
||||
/* Set the physical address of the warmboot binary to scratch 1. */
|
||||
reg::Write(pmc + APBDEV_PMC_SCRATCH1, static_cast<u32>(MemoryRegionPhysicalDramSecureDataStoreWarmbootFirmware.GetAddress()));
|
||||
if (GetSocType() == fuse::SocType_Mariko) {
|
||||
reg::Write(pmc + APBDEV_PMC_SECURE_SCRATCH119, static_cast<u32>(MemoryRegionPhysicalDramSecureDataStoreWarmbootFirmware.GetAddress()));
|
||||
} else /* if (GetSocType() == fuse::SocType_Erista) */ {
|
||||
reg::Write(pmc + APBDEV_PMC_SCRATCH1, static_cast<u32>(MemoryRegionPhysicalDramSecureDataStoreWarmbootFirmware.GetAddress()));
|
||||
}
|
||||
|
||||
|
||||
/* Configure logging by setting bits 18-19 of scratch 20. */
|
||||
reg::ReadWrite(pmc + APBDEV_PMC_SCRATCH20, REG_BITS_VALUE(18, 2, 0));
|
||||
|
@ -66,14 +71,74 @@ namespace ams::secmon::boot {
|
|||
/* The warmboot key as a parameter. The latter is a better solution, but it would be nice to take */
|
||||
/* care of it here. Perhaps we should read the number of anti-downgrade fuses burnt, and translate that */
|
||||
/* to the warmboot key? To be decided during the process of implementing ams-on-mariko support. */
|
||||
reg::Write(pmc + APBDEV_PMC_SECURE_SCRATCH32, 0x129);
|
||||
}
|
||||
|
||||
constinit const u8 DeviceMasterKeySourceKekSource[se::AesBlockSize] = {
|
||||
0x0C, 0x91, 0x09, 0xDB, 0x93, 0x93, 0x07, 0x81, 0x07, 0x3C, 0xC4, 0x16, 0x22, 0x7C, 0x6C, 0x28
|
||||
};
|
||||
|
||||
/* This function derives the master kek and device keys using the tsec root key. */
|
||||
/* NOTE: Exosphere does not use this in practice, and expects the bootloader to set up keys already. */
|
||||
/* NOTE: This function is currently not implemented. If implemented, it will only be a reference implementation. */
|
||||
[[maybe_unused]]
|
||||
void DeriveMasterKekAndDeviceKey() {
|
||||
/* TODO: Decide whether to implement this. */
|
||||
void DeriveMasterKekAndDeviceKeyErista(bool is_prod) {
|
||||
/* NOTE: Exosphere does not use this in practice, and expects the bootloader to set up keys already. */
|
||||
/* NOTE: This function is currently not implemented. If implemented, it will only be a reference implementation. */
|
||||
if constexpr (false) {
|
||||
/* TODO: Consider implementing this as a reference. */
|
||||
}
|
||||
}
|
||||
|
||||
/* NOTE: These are just latest-master-kek encrypted with BEK. */
|
||||
/* We can get away with only including latest because exosphere supports newer-than-expected master key in engine. */
|
||||
/* TODO: Update on next change of keys. */
|
||||
constinit const u8 MarikoMasterKekSourceProd[se::AesBlockSize] = {
|
||||
0x0E, 0x44, 0x0C, 0xED, 0xB4, 0x36, 0xC0, 0x3F, 0xAA, 0x1D, 0xAE, 0xBF, 0x62, 0xB1, 0x09, 0x82
|
||||
};
|
||||
|
||||
constinit const u8 MarikoMasterKekSourceDev[se::AesBlockSize] = {
|
||||
0xF9, 0x37, 0xCF, 0x9A, 0xBD, 0x86, 0xBB, 0xA9, 0x9C, 0x9E, 0x03, 0xC4, 0xFC, 0xBC, 0x3B, 0xCE
|
||||
};
|
||||
|
||||
constinit const u8 MasterKeySource[se::AesBlockSize] = {
|
||||
0xD8, 0xA2, 0x41, 0x0A, 0xC6, 0xC5, 0x90, 0x01, 0xC6, 0x1D, 0x6A, 0x26, 0x7C, 0x51, 0x3F, 0x3C
|
||||
};
|
||||
|
||||
void DeriveMasterKekAndDeviceKeyMariko(bool is_prod) {
|
||||
/* Clear all keyslots other than KEK and SBK in SE1. */
|
||||
for (int i = 0; i < pkg1::AesKeySlot_Count; ++i) {
|
||||
if (i != pkg1::AesKeySlot_MarikoKek && i != pkg1::AesKeySlot_SecureBoot) {
|
||||
se::ClearAesKeySlot(i);
|
||||
}
|
||||
}
|
||||
|
||||
/* Clear all keyslots in SE2. */
|
||||
for (int i = 0; i < pkg1::AesKeySlot_Count; ++i) {
|
||||
se::ClearAesKeySlot2(i);
|
||||
}
|
||||
|
||||
/* Derive the master kek. */
|
||||
se::SetEncryptedAesKey128(pkg1::AesKeySlot_MasterKek, pkg1::AesKeySlot_MarikoKek, is_prod ? MarikoMasterKekSourceProd : MarikoMasterKekSourceDev, se::AesBlockSize);
|
||||
|
||||
/* Derive the device master key source kek. */
|
||||
se::SetEncryptedAesKey128(pkg1::AesKeySlot_DeviceMasterKeySourceKekMariko, pkg1::AesKeySlot_SecureBoot, DeviceMasterKeySourceKekSource, se::AesBlockSize);
|
||||
|
||||
/* Clear the KEK, now that we're done using it. */
|
||||
se::ClearAesKeySlot(pkg1::AesKeySlot_MarikoKek);
|
||||
}
|
||||
|
||||
void DeriveMasterKekAndDeviceKey(bool is_prod) {
|
||||
if (GetSocType() == fuse::SocType_Mariko) {
|
||||
DeriveMasterKekAndDeviceKeyMariko(is_prod);
|
||||
} else /* if (GetSocType() == fuse::SocType_Erista) */ {
|
||||
DeriveMasterKekAndDeviceKeyErista(is_prod);
|
||||
}
|
||||
}
|
||||
|
||||
void DeriveMasterKey() {
|
||||
if (GetSocType() == fuse::SocType_Mariko) {
|
||||
se::SetEncryptedAesKey128(pkg1::AesKeySlot_Master, pkg1::AesKeySlot_MasterKek, MasterKeySource, se::AesBlockSize);
|
||||
} else /* if (GetSocType() == fuse::SocType_Erista) */ {
|
||||
/* Nothing to do here; erista bootloader will have derived master key already. */
|
||||
}
|
||||
}
|
||||
|
||||
void SetupRandomKey(int slot, se::KeySlotLockFlags flags) {
|
||||
|
@ -218,6 +283,9 @@ namespace ams::secmon::boot {
|
|||
/* Get the current key generation. */
|
||||
const int current_generation = secmon::GetKeyGeneration();
|
||||
|
||||
/* Get the kek slot. */
|
||||
const int kek_slot = GetSocType() == fuse::SocType_Mariko ? pkg1::AesKeySlot_DeviceMasterKeySourceKekMariko : pkg1::AesKeySlot_DeviceMasterKeySourceKekErista;
|
||||
|
||||
/* Iterate for all generations. */
|
||||
for (int i = 0; i < pkg1::OldDeviceMasterKeyCount; ++i) {
|
||||
const int generation = pkg1::KeyGeneration_4_0_0 + i;
|
||||
|
@ -229,7 +297,7 @@ namespace ams::secmon::boot {
|
|||
se::SetEncryptedAesKey128(pkg1::AesKeySlot_Temporary, pkg1::AesKeySlot_Temporary, is_prod ? DeviceMasterKekSourcesProd[i] : DeviceMasterKekSourcesDev[i], se::AesBlockSize);
|
||||
|
||||
/* Decrypt the device master key source into the work block. */
|
||||
se::DecryptAes128(work_block, se::AesBlockSize, pkg1::AesKeySlot_DeviceMasterKeySourceKek, DeviceMasterKeySourceSources[i], se::AesBlockSize);
|
||||
se::DecryptAes128(work_block, se::AesBlockSize, kek_slot, DeviceMasterKeySourceSources[i], se::AesBlockSize);
|
||||
|
||||
/* If we're decrypting the current device master key, decrypt into the keyslot. */
|
||||
if (generation == current_generation) {
|
||||
|
@ -244,14 +312,11 @@ namespace ams::secmon::boot {
|
|||
}
|
||||
|
||||
/* Clear and lock the Device Master Key Source Kek. */
|
||||
se::ClearAesKeySlot(pkg1::AesKeySlot_DeviceMasterKeySourceKek);
|
||||
se::LockAesKeySlot(pkg1::AesKeySlot_DeviceMasterKeySourceKek, se::KeySlotLockFlags_AllLockKek);
|
||||
se::ClearAesKeySlot(pkg1::AesKeySlot_DeviceMasterKeySourceKekMariko);
|
||||
se::LockAesKeySlot(pkg1::AesKeySlot_DeviceMasterKeySourceKekMariko, se::KeySlotLockFlags_AllLockKek);
|
||||
}
|
||||
|
||||
void DeriveAllKeys() {
|
||||
/* Determine whether we're prod. */
|
||||
const bool is_prod = IsProduction();
|
||||
|
||||
void DeriveAllKeys(bool is_prod) {
|
||||
/* Get the ephemeral work block. */
|
||||
u8 * const work_block = se::GetEphemeralWorkBlock();
|
||||
ON_SCOPE_EXIT { util::ClearMemory(work_block, se::AesBlockSize); };
|
||||
|
@ -265,6 +330,9 @@ namespace ams::secmon::boot {
|
|||
/* Derive the master keys. */
|
||||
DeriveAllMasterKeys(is_prod, work_block);
|
||||
|
||||
/* Lock the master key as a kek. */
|
||||
se::LockAesKeySlot(pkg1::AesKeySlot_Master, se::KeySlotLockFlags_AllLockKek);
|
||||
|
||||
/* Derive the device master keys. */
|
||||
DeriveAllDeviceMasterKeys(is_prod, work_block);
|
||||
|
||||
|
@ -300,16 +368,21 @@ namespace ams::secmon::boot {
|
|||
/* Initialize the rng. */
|
||||
se::InitializeRandom();
|
||||
|
||||
/* Determine whether we're production. */
|
||||
const bool is_prod = IsProduction();
|
||||
|
||||
/* Derive the master kek and device key. */
|
||||
if constexpr (false) {
|
||||
DeriveMasterKekAndDeviceKey();
|
||||
}
|
||||
/* NOTE: This is a no-op on erista, because fusee will have set up keys. */
|
||||
DeriveMasterKekAndDeviceKey(is_prod);
|
||||
|
||||
/* Lock the device key as only usable as a kek. */
|
||||
se::LockAesKeySlot(pkg1::AesKeySlot_Device, se::KeySlotLockFlags_AllLockKek);
|
||||
|
||||
/* Derive all keys. */
|
||||
DeriveAllKeys();
|
||||
/* Derive the master key. */
|
||||
DeriveMasterKey();
|
||||
|
||||
/* Derive all other keys. */
|
||||
DeriveAllKeys(is_prod);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -348,6 +421,9 @@ namespace ams::secmon::boot {
|
|||
/* Set the security engine to Per Key Secure. */
|
||||
se::SetPerKeySecure();
|
||||
|
||||
/* Set the security engine to Context Save Secure. */
|
||||
se::SetContextSaveSecure();
|
||||
|
||||
/* Setup the PMC registers. */
|
||||
SetupPmcRegisters();
|
||||
|
||||
|
|
|
@ -42,7 +42,7 @@ namespace ams::secmon {
|
|||
i2c::SetRegisterAddress(i2c::Port_5, MemoryRegionVirtualDeviceI2c5.GetAddress());
|
||||
pinmux::SetRegisterAddress(MemoryRegionVirtualDeviceApbMisc.GetAddress(), MemoryRegionVirtualDeviceGpio.GetAddress());
|
||||
pmc::SetRegisterAddress(MemoryRegionVirtualDevicePmc.GetAddress());
|
||||
se::SetRegisterAddress(MemoryRegionVirtualDeviceSecurityEngine.GetAddress());
|
||||
se::SetRegisterAddress(MemoryRegionVirtualDeviceSecurityEngine.GetAddress(), MemoryRegionVirtualDeviceSecurityEngine2.GetAddress());
|
||||
uart::SetRegisterAddress(MemoryRegionVirtualDeviceUart.GetAddress());
|
||||
wdt::SetRegisterAddress(MemoryRegionVirtualDeviceTimer.GetAddress());
|
||||
util::SetRegisterAddress(MemoryRegionVirtualDeviceTimer.GetAddress());
|
||||
|
|
|
@ -33,8 +33,6 @@ namespace ams::secmon {
|
|||
u32 mdcr_el2;
|
||||
u32 mdcr_el3;
|
||||
u32 spsr_el3;
|
||||
u64 dbgbvcr_el1[12];
|
||||
u64 dbgwvcr_el1[ 8];
|
||||
};
|
||||
|
||||
struct CoreContext {
|
||||
|
@ -61,30 +59,6 @@ namespace ams::secmon {
|
|||
HW_CPU_GET_MDCR_EL2 (dr.mdcr_el2);
|
||||
HW_CPU_GET_MDCR_EL3 (dr.mdcr_el3);
|
||||
HW_CPU_GET_SPSR_EL3 (dr.spsr_el3);
|
||||
|
||||
/* Save debug breakpoints. */
|
||||
HW_CPU_GET_DBGBVR0_EL1(dr.dbgbvcr_el1[ 0]);
|
||||
HW_CPU_GET_DBGBCR0_EL1(dr.dbgbvcr_el1[ 1]);
|
||||
HW_CPU_GET_DBGBVR1_EL1(dr.dbgbvcr_el1[ 2]);
|
||||
HW_CPU_GET_DBGBCR1_EL1(dr.dbgbvcr_el1[ 3]);
|
||||
HW_CPU_GET_DBGBVR2_EL1(dr.dbgbvcr_el1[ 4]);
|
||||
HW_CPU_GET_DBGBCR2_EL1(dr.dbgbvcr_el1[ 5]);
|
||||
HW_CPU_GET_DBGBVR3_EL1(dr.dbgbvcr_el1[ 6]);
|
||||
HW_CPU_GET_DBGBCR3_EL1(dr.dbgbvcr_el1[ 7]);
|
||||
HW_CPU_GET_DBGBVR4_EL1(dr.dbgbvcr_el1[ 8]);
|
||||
HW_CPU_GET_DBGBCR4_EL1(dr.dbgbvcr_el1[ 9]);
|
||||
HW_CPU_GET_DBGBVR5_EL1(dr.dbgbvcr_el1[10]);
|
||||
HW_CPU_GET_DBGBCR5_EL1(dr.dbgbvcr_el1[11]);
|
||||
|
||||
/* Save debug watchpoints. */
|
||||
HW_CPU_GET_DBGWVR0_EL1(dr.dbgwvcr_el1[0]);
|
||||
HW_CPU_GET_DBGWCR0_EL1(dr.dbgwvcr_el1[1]);
|
||||
HW_CPU_GET_DBGWVR1_EL1(dr.dbgwvcr_el1[2]);
|
||||
HW_CPU_GET_DBGWCR1_EL1(dr.dbgwvcr_el1[3]);
|
||||
HW_CPU_GET_DBGWVR2_EL1(dr.dbgwvcr_el1[4]);
|
||||
HW_CPU_GET_DBGWCR2_EL1(dr.dbgwvcr_el1[5]);
|
||||
HW_CPU_GET_DBGWVR3_EL1(dr.dbgwvcr_el1[6]);
|
||||
HW_CPU_GET_DBGWCR3_EL1(dr.dbgwvcr_el1[7]);
|
||||
}
|
||||
|
||||
void RestoreDebugRegisters(const DebugRegisters &dr) {
|
||||
|
@ -100,30 +74,6 @@ namespace ams::secmon {
|
|||
HW_CPU_SET_MDCR_EL2 (dr.mdcr_el2);
|
||||
HW_CPU_SET_MDCR_EL3 (dr.mdcr_el3);
|
||||
HW_CPU_SET_SPSR_EL3 (dr.spsr_el3);
|
||||
|
||||
/* Restore debug breakpoints. */
|
||||
HW_CPU_SET_DBGBVR0_EL1(dr.dbgbvcr_el1[ 0]);
|
||||
HW_CPU_SET_DBGBCR0_EL1(dr.dbgbvcr_el1[ 1]);
|
||||
HW_CPU_SET_DBGBVR1_EL1(dr.dbgbvcr_el1[ 2]);
|
||||
HW_CPU_SET_DBGBCR1_EL1(dr.dbgbvcr_el1[ 3]);
|
||||
HW_CPU_SET_DBGBVR2_EL1(dr.dbgbvcr_el1[ 4]);
|
||||
HW_CPU_SET_DBGBCR2_EL1(dr.dbgbvcr_el1[ 5]);
|
||||
HW_CPU_SET_DBGBVR3_EL1(dr.dbgbvcr_el1[ 6]);
|
||||
HW_CPU_SET_DBGBCR3_EL1(dr.dbgbvcr_el1[ 7]);
|
||||
HW_CPU_SET_DBGBVR4_EL1(dr.dbgbvcr_el1[ 8]);
|
||||
HW_CPU_SET_DBGBCR4_EL1(dr.dbgbvcr_el1[ 9]);
|
||||
HW_CPU_SET_DBGBVR5_EL1(dr.dbgbvcr_el1[10]);
|
||||
HW_CPU_SET_DBGBCR5_EL1(dr.dbgbvcr_el1[11]);
|
||||
|
||||
/* Restore debug watchpoints. */
|
||||
HW_CPU_SET_DBGWVR0_EL1(dr.dbgwvcr_el1[0]);
|
||||
HW_CPU_SET_DBGWCR0_EL1(dr.dbgwvcr_el1[1]);
|
||||
HW_CPU_SET_DBGWVR1_EL1(dr.dbgwvcr_el1[2]);
|
||||
HW_CPU_SET_DBGWCR1_EL1(dr.dbgwvcr_el1[3]);
|
||||
HW_CPU_SET_DBGWVR2_EL1(dr.dbgwvcr_el1[4]);
|
||||
HW_CPU_SET_DBGWCR2_EL1(dr.dbgwvcr_el1[5]);
|
||||
HW_CPU_SET_DBGWVR3_EL1(dr.dbgwvcr_el1[6]);
|
||||
HW_CPU_SET_DBGWCR3_EL1(dr.dbgwvcr_el1[7]);
|
||||
}
|
||||
|
||||
constinit CoreContext g_core_contexts[NumCores] = {};
|
||||
|
|
|
@ -16,10 +16,24 @@
|
|||
#include <exosphere.hpp>
|
||||
#include "secmon_error.hpp"
|
||||
|
||||
namespace {
|
||||
namespace ams {
|
||||
|
||||
constexpr bool SaveSystemStateForDebug = false;
|
||||
namespace {
|
||||
|
||||
constexpr bool SaveSystemStateForDebug = false;
|
||||
constexpr bool LogSystemStateForDebug = false;
|
||||
|
||||
void LogU64(u64 value) {
|
||||
char buffer[2 * sizeof(value)];
|
||||
for (size_t i = 0; i < sizeof(value); ++i) {
|
||||
buffer[sizeof(buffer) - 1 - (2 * i) - 0] = "0123456789ABCDEF"[(value >> 0) & 0xF];
|
||||
buffer[sizeof(buffer) - 1 - (2 * i) - 1] = "0123456789ABCDEF"[(value >> 4) & 0xF];
|
||||
value >>= 8;
|
||||
}
|
||||
log::SendText(buffer, sizeof(buffer));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
namespace ams::diag {
|
||||
|
@ -98,6 +112,57 @@ namespace ams::secmon {
|
|||
util::WaitMicroSeconds(1000);
|
||||
}
|
||||
|
||||
ALWAYS_INLINE void LogSystemStateForDebugErrorReboot(u64 lr, u64 sp) {
|
||||
log::SendText("*** Error Reboot ***\n", 21);
|
||||
log::Flush();
|
||||
|
||||
u64 temp_reg;
|
||||
|
||||
__asm__ __volatile__("mrs %0, esr_el3" : "=r"(temp_reg) :: "memory");
|
||||
log::SendText("ESR_EL3: ", 9);
|
||||
LogU64(temp_reg);
|
||||
log::SendText("\n", 1);
|
||||
log::Flush();
|
||||
|
||||
__asm__ __volatile__("mrs %0, elr_el3" : "=r"(temp_reg) :: "memory");
|
||||
log::SendText("ELR_EL3: ", 9);
|
||||
LogU64(temp_reg);
|
||||
log::SendText("\n", 1);
|
||||
log::Flush();
|
||||
|
||||
__asm__ __volatile__("mrs %0, far_el3" : "=r"(temp_reg) :: "memory");
|
||||
log::SendText("FAR_EL3: ", 9);
|
||||
LogU64(temp_reg);
|
||||
log::SendText("\n", 1);
|
||||
log::Flush();
|
||||
|
||||
log::SendText("LR: ", 9);
|
||||
LogU64(lr);
|
||||
log::SendText("\n", 1);
|
||||
log::Flush();
|
||||
|
||||
log::SendText("SP: ", 9);
|
||||
LogU64(sp);
|
||||
log::SendText("\n", 1);
|
||||
log::Flush();
|
||||
|
||||
log::SendText("Stack:\n", 7);
|
||||
log::Flush();
|
||||
|
||||
char buf[2];
|
||||
for (int i = 0; i < 0x100; ++i) {
|
||||
const u8 byte = *(volatile u8 *)(sp + i);
|
||||
buf[0] = "0123456789ABCDEF"[(byte >> 4) & 0xF];
|
||||
buf[1] = "0123456789ABCDEF"[(byte >> 0) & 0xF];
|
||||
log::SendText(buf, 2);
|
||||
log::Flush();
|
||||
if (util::IsAligned(i + 1, 0x10)) {
|
||||
log::SendText("\n", 1);
|
||||
log::Flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void SetError(pkg1::ErrorInfo info) {
|
||||
|
@ -114,6 +179,14 @@ namespace ams::secmon {
|
|||
SaveSystemStateForDebugErrorReboot();
|
||||
}
|
||||
|
||||
if constexpr (LogSystemStateForDebug) {
|
||||
u64 lr, sp;
|
||||
__asm__ __volatile__("mov %0, lr" : "=r"(lr) :: "memory");
|
||||
__asm__ __volatile__("mov %0, sp" : "=r"(sp) :: "memory");
|
||||
|
||||
LogSystemStateForDebugErrorReboot(lr, sp);
|
||||
}
|
||||
|
||||
/* Lockout the security engine. */
|
||||
se::Lockout();
|
||||
|
||||
|
|
|
@ -51,27 +51,27 @@ namespace ams::secmon {
|
|||
|
||||
constinit bool g_is_cold_boot = true;
|
||||
|
||||
constinit const se::StickyBits ExpectedSeStickyBits = {
|
||||
constinit se::StickyBits ExpectedSeStickyBits = {
|
||||
.se_security = (1 << 0), /* SE_HARD_SETTING */
|
||||
.tzram_security = 0,
|
||||
.crypto_security_perkey = (1 << pkg1::AesKeySlot_UserEnd) - 1,
|
||||
.crypto_keytable_access = {
|
||||
(1 << 6) | (1 << 5) | (0 << 4) | (1 << 3) | (0 << 2) | (1 << 1) | (0 << 0), /* 0: User keyslot. KEYUSE, UIVUPDATE, OIVUPDATE, KEYUPDATE enabled. UIVREAD, OIVREAD, KEYREAD disabled. */
|
||||
(1 << 6) | (1 << 5) | (0 << 4) | (1 << 3) | (0 << 2) | (1 << 1) | (0 << 0), /* 1: User keyslot. KEYUSE, UIVUPDATE, OIVUPDATE, KEYUPDATE enabled. UIVREAD, OIVREAD, KEYREAD disabled. */
|
||||
(1 << 6) | (1 << 5) | (0 << 4) | (1 << 3) | (0 << 2) | (1 << 1) | (0 << 0), /* 2: User keyslot. KEYUSE, UIVUPDATE, OIVUPDATE, KEYUPDATE enabled. UIVREAD, OIVREAD, KEYREAD disabled. */
|
||||
(1 << 6) | (1 << 5) | (0 << 4) | (1 << 3) | (0 << 2) | (1 << 1) | (0 << 0), /* 3: User keyslot. KEYUSE, UIVUPDATE, OIVUPDATE, KEYUPDATE enabled. UIVREAD, OIVREAD, KEYREAD disabled. */
|
||||
(1 << 6) | (1 << 5) | (0 << 4) | (1 << 3) | (0 << 2) | (1 << 1) | (0 << 0), /* 4: User keyslot. KEYUSE, UIVUPDATE, OIVUPDATE, KEYUPDATE enabled. UIVREAD, OIVREAD, KEYREAD disabled. */
|
||||
(1 << 6) | (1 << 5) | (0 << 4) | (1 << 3) | (0 << 2) | (1 << 1) | (0 << 0), /* 5: User keyslot. KEYUSE, UIVUPDATE, OIVUPDATE, KEYUPDATE enabled. UIVREAD, OIVREAD, KEYREAD disabled. */
|
||||
(0 << 6) | (0 << 5) | (0 << 4) | (0 << 3) | (0 << 2) | (0 << 1) | (0 << 0), /* 6: Unused keyslot. KEYUSE, UIVUPDATE, UIVREAD, OIVUPDATE, OIVREAD, KEYUPDATE, KEYREAD disabled. */
|
||||
(0 << 6) | (0 << 5) | (0 << 4) | (0 << 3) | (0 << 2) | (0 << 1) | (0 << 0), /* 7: Unused keyslot. KEYUSE, UIVUPDATE, UIVREAD, OIVUPDATE, OIVREAD, KEYUPDATE, KEYREAD disabled. */
|
||||
(0 << 6) | (1 << 5) | (0 << 4) | (1 << 3) | (0 << 2) | (1 << 1) | (0 << 0), /* 8: Temp keyslot. UIVUPDATE, OIVUPDATE, KEYUPDATE enabled. KEYUSE, UIVREAD, OIVREAD, KEYREAD disabled. */
|
||||
(0 << 6) | (1 << 5) | (0 << 4) | (1 << 3) | (0 << 2) | (1 << 1) | (0 << 0), /* 9: SmcTemp keyslot. UIVUPDATE, OIVUPDATE, KEYUPDATE enabled. KEYUSE, UIVREAD, OIVREAD, KEYREAD disabled. */
|
||||
(0 << 6) | (0 << 5) | (0 << 4) | (0 << 3) | (0 << 2) | (0 << 1) | (0 << 0), /* 10: Wrap1 keyslot. KEYUSE, UIVUPDATE, UIVREAD, OIVUPDATE, OIVREAD, KEYUPDATE, KEYREAD disabled. */
|
||||
(0 << 6) | (0 << 5) | (0 << 4) | (0 << 3) | (0 << 2) | (0 << 1) | (0 << 0), /* 11: Wrap2 keyslot. KEYUSE, UIVUPDATE, UIVREAD, OIVUPDATE, OIVREAD, KEYUPDATE, KEYREAD disabled. */
|
||||
(0 << 6) | (0 << 5) | (0 << 4) | (0 << 3) | (0 << 2) | (0 << 1) | (0 << 0), /* 12: DMaster keyslot. KEYUSE, UIVUPDATE, UIVREAD, OIVUPDATE, OIVREAD, KEYUPDATE, KEYREAD disabled. */
|
||||
(0 << 6) | (0 << 5) | (0 << 4) | (0 << 3) | (0 << 2) | (0 << 1) | (0 << 0), /* 13: Master keyslot. KEYUSE, UIVUPDATE, UIVREAD, OIVUPDATE, OIVREAD, KEYUPDATE, KEYREAD disabled. */
|
||||
(0 << 6) | (0 << 5) | (0 << 4) | (0 << 3) | (0 << 2) | (0 << 1) | (0 << 0), /* 14: Unused keyslot. KEYUSE, UIVUPDATE, UIVREAD, OIVUPDATE, OIVREAD, KEYUPDATE, KEYREAD disabled. */
|
||||
(0 << 6) | (0 << 5) | (0 << 4) | (0 << 3) | (0 << 2) | (0 << 1) | (0 << 0), /* 13: Device keyslot. KEYUSE, UIVUPDATE, UIVREAD, OIVUPDATE, OIVREAD, KEYUPDATE, KEYREAD disabled. */
|
||||
(0 << 7) | (1 << 6) | (1 << 5) | (0 << 4) | (1 << 3) | (0 << 2) | (1 << 1) | (0 << 0), /* 0: User keyslot. KEY. KEYUSE, UIVUPDATE, OIVUPDATE, KEYUPDATE enabled. UIVREAD, OIVREAD, KEYREAD disabled. */
|
||||
(0 << 7) | (1 << 6) | (1 << 5) | (0 << 4) | (1 << 3) | (0 << 2) | (1 << 1) | (0 << 0), /* 1: User keyslot. KEY. KEYUSE, UIVUPDATE, OIVUPDATE, KEYUPDATE enabled. UIVREAD, OIVREAD, KEYREAD disabled. */
|
||||
(0 << 7) | (1 << 6) | (1 << 5) | (0 << 4) | (1 << 3) | (0 << 2) | (1 << 1) | (0 << 0), /* 2: User keyslot. KEY. KEYUSE, UIVUPDATE, OIVUPDATE, KEYUPDATE enabled. UIVREAD, OIVREAD, KEYREAD disabled. */
|
||||
(0 << 7) | (1 << 6) | (1 << 5) | (0 << 4) | (1 << 3) | (0 << 2) | (1 << 1) | (0 << 0), /* 3: User keyslot. KEY. KEYUSE, UIVUPDATE, OIVUPDATE, KEYUPDATE enabled. UIVREAD, OIVREAD, KEYREAD disabled. */
|
||||
(0 << 7) | (1 << 6) | (1 << 5) | (0 << 4) | (1 << 3) | (0 << 2) | (1 << 1) | (0 << 0), /* 4: User keyslot. KEY. KEYUSE, UIVUPDATE, OIVUPDATE, KEYUPDATE enabled. UIVREAD, OIVREAD, KEYREAD disabled. */
|
||||
(0 << 7) | (1 << 6) | (1 << 5) | (0 << 4) | (1 << 3) | (0 << 2) | (1 << 1) | (0 << 0), /* 5: User keyslot. KEY. KEYUSE, UIVUPDATE, OIVUPDATE, KEYUPDATE enabled. UIVREAD, OIVREAD, KEYREAD disabled. */
|
||||
(1 << 7) | (0 << 6) | (0 << 5) | (0 << 4) | (0 << 3) | (0 << 2) | (0 << 1) | (0 << 0), /* 6: Unused keyslot. KEK. KEYUSE, UIVUPDATE, UIVREAD, OIVUPDATE, OIVREAD, KEYUPDATE, KEYREAD disabled. */
|
||||
(1 << 7) | (0 << 6) | (0 << 5) | (0 << 4) | (0 << 3) | (0 << 2) | (0 << 1) | (0 << 0), /* 7: Unused keyslot. KEK. KEYUSE, UIVUPDATE, UIVREAD, OIVUPDATE, OIVREAD, KEYUPDATE, KEYREAD disabled. */
|
||||
(0 << 7) | (0 << 6) | (1 << 5) | (0 << 4) | (1 << 3) | (0 << 2) | (1 << 1) | (0 << 0), /* 8: Temp keyslot. KEY. UIVUPDATE, OIVUPDATE, KEYUPDATE enabled. KEYUSE, UIVREAD, OIVREAD, KEYREAD disabled. */
|
||||
(0 << 7) | (0 << 6) | (1 << 5) | (0 << 4) | (1 << 3) | (0 << 2) | (1 << 1) | (0 << 0), /* 9: SmcTemp keyslot. KEY. UIVUPDATE, OIVUPDATE, KEYUPDATE enabled. KEYUSE, UIVREAD, OIVREAD, KEYREAD disabled. */
|
||||
(1 << 7) | (0 << 6) | (0 << 5) | (0 << 4) | (0 << 3) | (0 << 2) | (0 << 1) | (0 << 0), /* 10: Wrap1 keyslot. KEK. KEYUSE, UIVUPDATE, UIVREAD, OIVUPDATE, OIVREAD, KEYUPDATE, KEYREAD disabled. */
|
||||
(0 << 7) | (0 << 6) | (0 << 5) | (0 << 4) | (0 << 3) | (0 << 2) | (0 << 1) | (0 << 0), /* 11: Wrap2 keyslot. KEY. KEYUSE, UIVUPDATE, UIVREAD, OIVUPDATE, OIVREAD, KEYUPDATE, KEYREAD disabled. */
|
||||
(1 << 7) | (0 << 6) | (0 << 5) | (0 << 4) | (0 << 3) | (0 << 2) | (0 << 1) | (0 << 0), /* 12: DMaster keyslot. KEK. KEYUSE, UIVUPDATE, UIVREAD, OIVUPDATE, OIVREAD, KEYUPDATE, KEYREAD disabled. */
|
||||
(1 << 7) | (0 << 6) | (0 << 5) | (0 << 4) | (0 << 3) | (0 << 2) | (0 << 1) | (0 << 0), /* 13: Master keyslot. KEK. KEYUSE, UIVUPDATE, UIVREAD, OIVUPDATE, OIVREAD, KEYUPDATE, KEYREAD disabled. */
|
||||
(1 << 7) | (0 << 6) | (0 << 5) | (0 << 4) | (0 << 3) | (0 << 2) | (0 << 1) | (0 << 0), /* 14: Unused keyslot. KEK. KEYUSE, UIVUPDATE, UIVREAD, OIVUPDATE, OIVREAD, KEYUPDATE, KEYREAD disabled. */
|
||||
(1 << 7) | (0 << 6) | (0 << 5) | (0 << 4) | (0 << 3) | (0 << 2) | (0 << 1) | (0 << 0), /* 13: Device keyslot. KEK. KEYUSE, UIVUPDATE, UIVREAD, OIVUPDATE, OIVREAD, KEYUPDATE, KEYREAD disabled. */
|
||||
},
|
||||
.rsa_security_perkey = 0,
|
||||
.rsa_keytable_access = {
|
||||
|
@ -139,6 +139,16 @@ namespace ams::secmon {
|
|||
}
|
||||
|
||||
void VerifySecurityEngineStickyBits() {
|
||||
/* On mariko, an extra sticky bit is set. */
|
||||
if (GetSocType() == fuse::SocType_Mariko) {
|
||||
ExpectedSeStickyBits.se_security |= (1 << 5);
|
||||
} else /* if (GetSocType() == fuse::SocType_Erista) */ {
|
||||
/* Erista does not support DST_KEYTABLE_ONLY, and so all keys will have the bit clear. */
|
||||
for (size_t i = 0; i < util::size(ExpectedSeStickyBits.crypto_keytable_access); ++i) {
|
||||
ExpectedSeStickyBits.crypto_keytable_access[i] &= ~(1 << 7);
|
||||
}
|
||||
}
|
||||
|
||||
if (!se::ValidateStickyBits(ExpectedSeStickyBits)) {
|
||||
SetError(pkg1::ErrorInfo_InvalidSecurityEngineStickyBits);
|
||||
AMS_ABORT("Invalid sticky bits");
|
||||
|
@ -938,12 +948,16 @@ namespace ams::secmon {
|
|||
return reg::Read(MC + MC_SECURITY_CFG3) == 0;
|
||||
}
|
||||
|
||||
void SetupLogForBoot() {
|
||||
log::Initialize();
|
||||
log::SendText("OHAYO\n", 6);
|
||||
log::Flush();
|
||||
}
|
||||
|
||||
void LogExitLp0() {
|
||||
/* NOTE: Nintendo only does this on dev, but we will always do it. */
|
||||
if (true /* !pkg1::IsProduction() */) {
|
||||
log::Initialize();
|
||||
log::SendText("OHAYO\n", 6);
|
||||
log::Flush();
|
||||
SetupLogForBoot();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -969,7 +983,7 @@ namespace ams::secmon {
|
|||
InitializeConfigurationContext();
|
||||
|
||||
/* Initialize uart for logging. */
|
||||
log::Initialize();
|
||||
SetupLogForBoot();
|
||||
|
||||
/* Initialize the security engine. */
|
||||
se::Initialize();
|
||||
|
@ -1017,12 +1031,16 @@ namespace ams::secmon {
|
|||
|
||||
/* Overwrite keys that we want to be random with random contents. */
|
||||
se::InitializeRandom();
|
||||
se::ConfigureAutomaticContextSave();
|
||||
se::SetRandomKey(pkg1::AesKeySlot_Temporary);
|
||||
se::GenerateSrk();
|
||||
se::SetRandomKey(pkg1::AesKeySlot_TzramSaveKek);
|
||||
|
||||
/* Initialize pmc secure scratch. */
|
||||
pmc::InitializeRandomScratch();
|
||||
if (GetSocType() == fuse::SocType_Erista) {
|
||||
pmc::InitializeRandomScratch();
|
||||
}
|
||||
pmc::LockSecureRegister(pmc::SecureRegister_Srk);
|
||||
|
||||
/* Setup secure registers. */
|
||||
SetupSecureRegisters();
|
||||
|
|
|
@ -264,6 +264,13 @@ namespace ams::secmon {
|
|||
{
|
||||
reg::Write(AHB_ARBC(AHB_GIZMO_TZRAM), (1u << 7));
|
||||
}
|
||||
|
||||
/* NOTE: This is Mariko only in Nintendo's firmware. */
|
||||
/* Still, it seems to have no adverse effects on Erista... */
|
||||
/* TODO: Find a way to get access to SocType this early (fuse driver isn't alive yet), only write on mariko? */
|
||||
{
|
||||
reg::ReadWrite(AHB_ARBC(AHB_AHB_SPARE_REG), AHB_REG_BITS_VALUE(AHB_SPARE_REG_AHB_SPARE_REG, 0xE0000));
|
||||
}
|
||||
}
|
||||
|
||||
void SetupSocDmaControllersCpuMemoryControllersEnableMmuWarmboot() {
|
||||
|
|
|
@ -257,7 +257,7 @@ namespace ams::secmon::smc {
|
|||
}
|
||||
|
||||
int PrepareDeviceMasterKey(int generation) {
|
||||
if (generation == pkg1::KeyGeneration_1_0_0) {
|
||||
if (generation == pkg1::KeyGeneration_1_0_0 && GetSocType() == fuse::SocType_Erista) {
|
||||
return pkg1::AesKeySlot_Device;
|
||||
}
|
||||
if (generation == GetKeyGeneration()) {
|
||||
|
|
|
@ -262,6 +262,11 @@ namespace ams::secmon::smc {
|
|||
/* Get whether this unit should allow writing to the calibration partition. */
|
||||
args.r[1] = (GetEmummcConfiguration().IsEmummcActive() || GetSecmonConfiguration().AllowWritingToCalibrationBinarySysmmc());
|
||||
break;
|
||||
case ConfigItem::ExosphereEmummcType:
|
||||
/* Get what kind of emummc this unit has active. */
|
||||
/* NOTE: This may return values other than 1 in the future. */
|
||||
args.r[1] = (GetEmummcConfiguration().IsEmummcActive() ? 1 : 0);
|
||||
break;
|
||||
default:
|
||||
return SmcResult::InvalidArgument;
|
||||
}
|
||||
|
|
|
@ -47,6 +47,7 @@ namespace ams::secmon::smc {
|
|||
ExosphereHasRcmBugPatch = 65004,
|
||||
ExosphereBlankProdInfo = 65005,
|
||||
ExosphereAllowCalWrites = 65006,
|
||||
ExosphereEmummcType = 65007,
|
||||
};
|
||||
|
||||
SmcResult SmcGetConfigUser(SmcArguments &args);
|
||||
|
|
|
@ -325,7 +325,11 @@ namespace ams::secmon::smc {
|
|||
}
|
||||
|
||||
void SaveSecureContextForMariko() {
|
||||
/* TODO: Implement this when adding ams-on-mariko support. */
|
||||
/* Save security engine context to TZRAM SE carveout (inaccessible to cpu). */
|
||||
se::SaveContextAutomatic();
|
||||
|
||||
/* Save TZRAM to shadow-TZRAM in always-on power domain. */
|
||||
se::SaveTzramAutomatic();
|
||||
}
|
||||
|
||||
void SaveSecureContext() {
|
||||
|
|
|
@ -3,7 +3,11 @@ import sys, lz4
|
|||
from struct import unpack as up
|
||||
|
||||
def lz4_compress(data):
|
||||
return lz4.block.compress(data, 'high_compression', store_size=False)
|
||||
try:
|
||||
import lz4.block as block
|
||||
except ImportError:
|
||||
block = lz4.LZ4_compress
|
||||
return block.compress(data, 'high_compression', store_size=False)
|
||||
|
||||
def split_binary(data):
|
||||
A, B, START, BOOT_CODE_START, BOOT_CODE_END, PROGRAM_START, C, D = up('<QQQQQQQQ', data[:0x40])
|
||||
|
@ -19,7 +23,7 @@ def split_binary(data):
|
|||
|
||||
def main(argc, argv):
|
||||
if argc != 3:
|
||||
print 'Usage: %s in outdir' % argv[0]
|
||||
print('Usage: %s in outdir' % argv[0])
|
||||
return 1
|
||||
with open(argv[1], 'rb') as f:
|
||||
data = f.read()
|
||||
|
@ -30,4 +34,4 @@ def main(argc, argv):
|
|||
return 0
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main(len(sys.argv), sys.argv))
|
||||
sys.exit(main(len(sys.argv), sys.argv))
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
#ifndef FUSEE_LOG_H
|
||||
#define FUSEE_LOG_H
|
||||
|
||||
#define PRINT_MESSAGE_MAX_LENGTH 512
|
||||
#define PRINT_MESSAGE_MAX_LENGTH 1024
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
|
|
|
@ -17,17 +17,26 @@
|
|||
#include <stdbool.h>
|
||||
#include <stdarg.h>
|
||||
#include "utils.h"
|
||||
#include "display/video_fb.h"
|
||||
#include "lib/log.h"
|
||||
|
||||
__attribute__ ((noreturn)) void generic_panic(void) {
|
||||
print(SCREEN_LOG_LEVEL_ERROR, "Panic raised!");
|
||||
|
||||
while (true) {
|
||||
/* Lock. */
|
||||
}
|
||||
}
|
||||
|
||||
__attribute__((noreturn)) void fatal_error(const char *fmt, ...) {
|
||||
/* Forcefully initialize the screen if logging is disabled. */
|
||||
if (log_get_log_level() == SCREEN_LOG_LEVEL_NONE) {
|
||||
/* Zero-fill the framebuffer and register it as printk provider. */
|
||||
video_init((void *)0xC0000000);
|
||||
|
||||
/* Override the global logging level. */
|
||||
log_set_log_level(SCREEN_LOG_LEVEL_ERROR);
|
||||
}
|
||||
|
||||
/* Display fatal error. */
|
||||
va_list args;
|
||||
print(SCREEN_LOG_LEVEL_ERROR, "Fatal error: ");
|
||||
va_start(args, fmt);
|
||||
|
|
|
@ -19,9 +19,10 @@
|
|||
#include "exception_handlers.h"
|
||||
#include "utils.h"
|
||||
#include "lib/log.h"
|
||||
#include "lib/vsprintf.h"
|
||||
|
||||
#define CODE_DUMP_SIZE 0x30
|
||||
#define STACK_DUMP_SIZE 0x60
|
||||
#define STACK_DUMP_SIZE 0x30
|
||||
|
||||
extern const uint32_t exception_handler_table[];
|
||||
|
||||
|
@ -34,6 +35,40 @@ static const char *register_names[] = {
|
|||
"SP", "LR", "PC", "CPSR",
|
||||
};
|
||||
|
||||
/* Adapted from https://gist.github.com/ccbrown/9722406 */
|
||||
static void hexdump(const void* data, size_t size, uintptr_t addrbase, char* strbuf) {
|
||||
const uint8_t *d = (const uint8_t *)data;
|
||||
char ascii[17] = {0};
|
||||
ascii[16] = '\0';
|
||||
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
if (i % 16 == 0) {
|
||||
strbuf += sprintf(strbuf, "%0*" PRIXPTR ": | ", 2 * sizeof(addrbase), addrbase + i);
|
||||
}
|
||||
strbuf += sprintf(strbuf, "%02X ", d[i]);
|
||||
if (d[i] >= ' ' && d[i] <= '~') {
|
||||
ascii[i % 16] = d[i];
|
||||
} else {
|
||||
ascii[i % 16] = '.';
|
||||
}
|
||||
if ((i+1) % 8 == 0 || i+1 == size) {
|
||||
strbuf += sprintf(strbuf, " ");
|
||||
if ((i+1) % 16 == 0) {
|
||||
strbuf += sprintf(strbuf, "| %s \n", ascii);
|
||||
} else if (i+1 == size) {
|
||||
ascii[(i+1) % 16] = '\0';
|
||||
if ((i+1) % 16 <= 8) {
|
||||
strbuf += sprintf(strbuf, " ");
|
||||
}
|
||||
for (size_t j = (i+1) % 16; j < 16; j++) {
|
||||
strbuf += sprintf(strbuf, " ");
|
||||
}
|
||||
strbuf += sprintf(strbuf, "| %s \n", ascii);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setup_exception_handlers(void) {
|
||||
volatile uint32_t *bpmp_exception_handler_table = (volatile uint32_t *)0x6000F200;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
|
@ -44,38 +79,40 @@ void setup_exception_handlers(void) {
|
|||
}
|
||||
|
||||
void exception_handler_main(uint32_t *registers, unsigned int exception_type) {
|
||||
uint8_t code_dump[CODE_DUMP_SIZE];
|
||||
uint8_t stack_dump[STACK_DUMP_SIZE];
|
||||
size_t code_dump_size;
|
||||
size_t stack_dump_size;
|
||||
char exception_log[0x400] = {0};
|
||||
uint8_t code_dump[CODE_DUMP_SIZE] = {0};
|
||||
uint8_t stack_dump[STACK_DUMP_SIZE] = {0};
|
||||
size_t code_dump_size = 0;
|
||||
size_t stack_dump_size = 0;
|
||||
|
||||
uint32_t pc = registers[15];
|
||||
uint32_t cpsr = registers[16];
|
||||
|
||||
uint32_t instr_addr = pc + ((cpsr & 0x20) ? 2 : 4) - CODE_DUMP_SIZE;
|
||||
|
||||
print(SCREEN_LOG_LEVEL_ERROR, "\nSomething went wrong...\n");
|
||||
|
||||
sprintf(exception_log, "An exception occured!\n");
|
||||
|
||||
code_dump_size = safecpy(code_dump, (const void *)instr_addr, CODE_DUMP_SIZE);
|
||||
stack_dump_size = safecpy(stack_dump, (const void *)registers[13], STACK_DUMP_SIZE);
|
||||
|
||||
print(SCREEN_LOG_LEVEL_ERROR | SCREEN_LOG_LEVEL_NO_PREFIX, "\nException type: %s\n",
|
||||
exception_names[exception_type]);
|
||||
print(SCREEN_LOG_LEVEL_ERROR | SCREEN_LOG_LEVEL_NO_PREFIX, "\nRegisters:\n\n");
|
||||
sprintf(exception_log + strlen(exception_log), "\nException type: %s\n", exception_names[exception_type]);
|
||||
sprintf(exception_log + strlen(exception_log), "\nRegisters:\n");
|
||||
|
||||
/* Print r0 to pc. */
|
||||
for (int i = 0; i < 16; i += 2) {
|
||||
print(SCREEN_LOG_LEVEL_ERROR | SCREEN_LOG_LEVEL_NO_PREFIX, "%-7s%08"PRIX32" %-7s%08"PRIX32"\n",
|
||||
sprintf(exception_log + strlen(exception_log), "%-7s%08"PRIX32" %-7s%08"PRIX32"\n",
|
||||
register_names[i], registers[i], register_names[i+1], registers[i+1]);
|
||||
}
|
||||
|
||||
/* Print cpsr. */
|
||||
print(SCREEN_LOG_LEVEL_ERROR | SCREEN_LOG_LEVEL_NO_PREFIX, "%-7s%08"PRIX32"\n", register_names[16], registers[16]);
|
||||
|
||||
print(SCREEN_LOG_LEVEL_ERROR | SCREEN_LOG_LEVEL_NO_PREFIX, "\nCode dump:\n");
|
||||
hexdump(code_dump, code_dump_size, instr_addr);
|
||||
print(SCREEN_LOG_LEVEL_ERROR | SCREEN_LOG_LEVEL_NO_PREFIX, "\nStack dump:\n");
|
||||
hexdump(stack_dump, stack_dump_size, registers[13]);
|
||||
print(SCREEN_LOG_LEVEL_ERROR | SCREEN_LOG_LEVEL_NO_PREFIX, "\n");
|
||||
fatal_error("An exception occured!\n");
|
||||
sprintf(exception_log + strlen(exception_log), "%-7s%08"PRIX32"\n", register_names[16], registers[16]);
|
||||
|
||||
/* Print code and stack regions. */
|
||||
sprintf(exception_log + strlen(exception_log), "\nCode dump:\n");
|
||||
hexdump(code_dump, code_dump_size, instr_addr, exception_log + strlen(exception_log));
|
||||
sprintf(exception_log + strlen(exception_log), "\nStack dump:\n");
|
||||
hexdump(stack_dump, stack_dump_size, registers[13], exception_log + strlen(exception_log));
|
||||
sprintf(exception_log + strlen(exception_log), "\n");
|
||||
|
||||
/* Throw fatal error with the full exception log. */
|
||||
fatal_error(exception_log);
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
#ifndef FUSEE_LOG_H
|
||||
#define FUSEE_LOG_H
|
||||
|
||||
#define PRINT_MESSAGE_MAX_LENGTH 512
|
||||
#define PRINT_MESSAGE_MAX_LENGTH 1024
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
|
|
|
@ -42,6 +42,8 @@ static const char *get_error_desc_str(uint32_t error_desc) {
|
|||
return "SError";
|
||||
case 0x301:
|
||||
return "Bad SVC";
|
||||
case 0xFFD:
|
||||
return "Stack overflow";
|
||||
case 0xFFE:
|
||||
return "std::abort() called";
|
||||
default:
|
||||
|
@ -90,12 +92,15 @@ static void _check_and_display_atmosphere_fatal_error(void) {
|
|||
char filepath[0x40];
|
||||
snprintf(filepath, sizeof(filepath) - 1, "/atmosphere/fatal_errors/report_%016llx.bin", ctx.report_identifier);
|
||||
filepath[sizeof(filepath)-1] = 0;
|
||||
write_to_file(&ctx, sizeof(ctx), filepath);
|
||||
print(SCREEN_LOG_LEVEL_ERROR | SCREEN_LOG_LEVEL_NO_PREFIX,"Report saved to %s\n", filepath);
|
||||
if (write_to_file(&ctx, sizeof(ctx), filepath) != sizeof(ctx)) {
|
||||
print(SCREEN_LOG_LEVEL_ERROR | SCREEN_LOG_LEVEL_NO_PREFIX, "Failed to save report to the SD card!\n");
|
||||
} else {
|
||||
print(SCREEN_LOG_LEVEL_ERROR | SCREEN_LOG_LEVEL_NO_PREFIX, "Report saved to %s\n", filepath);
|
||||
}
|
||||
}
|
||||
|
||||
/* Display error. */
|
||||
print(SCREEN_LOG_LEVEL_ERROR | SCREEN_LOG_LEVEL_NO_PREFIX,"\nPress POWER to reboot\n");
|
||||
print(SCREEN_LOG_LEVEL_ERROR | SCREEN_LOG_LEVEL_NO_PREFIX, "\nPress POWER to reboot\n");
|
||||
}
|
||||
|
||||
/* Wait for button and reboot. */
|
||||
|
|
|
@ -106,7 +106,7 @@ void load_stage2(const char *bct0) {
|
|||
}
|
||||
|
||||
if (strlen(config.path) + 1 + sizeof(stage2_args_t) > CHAINLOADER_ARG_DATA_MAX_SIZE) {
|
||||
print(SCREEN_LOG_LEVEL_ERROR, "Stage2's path name is too big!\n");
|
||||
fatal_error("Stage2's path name is too big!\n");
|
||||
}
|
||||
|
||||
if (!check_32bit_address_loadable(config.entrypoint)) {
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
#include "car.h"
|
||||
#include "btn.h"
|
||||
#include "lib/log.h"
|
||||
#include "lib/vsprintf.h"
|
||||
#include "display/video_fb.h"
|
||||
|
||||
#include <inttypes.h>
|
||||
|
@ -112,11 +113,11 @@ __attribute__((noreturn)) void fatal_error(const char *fmt, ...) {
|
|||
/* Turn on the backlight after initializing the lfb */
|
||||
/* to avoid flickering. */
|
||||
display_backlight(true);
|
||||
|
||||
/* Override the global logging level. */
|
||||
log_set_log_level(SCREEN_LOG_LEVEL_ERROR);
|
||||
}
|
||||
|
||||
/* Override the global logging level. */
|
||||
log_set_log_level(SCREEN_LOG_LEVEL_ERROR);
|
||||
|
||||
/* Display fatal error. */
|
||||
va_list args;
|
||||
print(SCREEN_LOG_LEVEL_ERROR, "Fatal error: ");
|
||||
|
@ -137,37 +138,3 @@ __attribute__((noinline)) bool overlaps(uint64_t as, uint64_t ae, uint64_t bs, u
|
|||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Adapted from https://gist.github.com/ccbrown/9722406 */
|
||||
void hexdump(const void* data, size_t size, uintptr_t addrbase) {
|
||||
const uint8_t *d = (const uint8_t *)data;
|
||||
char ascii[17];
|
||||
ascii[16] = '\0';
|
||||
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
if (i % 16 == 0) {
|
||||
print(SCREEN_LOG_LEVEL_ERROR | SCREEN_LOG_LEVEL_NO_PREFIX, "%0*" PRIXPTR ": | ", 2 * sizeof(addrbase), addrbase + i);
|
||||
}
|
||||
print(SCREEN_LOG_LEVEL_ERROR | SCREEN_LOG_LEVEL_NO_PREFIX, "%02X ", d[i]);
|
||||
if (d[i] >= ' ' && d[i] <= '~') {
|
||||
ascii[i % 16] = d[i];
|
||||
} else {
|
||||
ascii[i % 16] = '.';
|
||||
}
|
||||
if ((i+1) % 8 == 0 || i+1 == size) {
|
||||
print(SCREEN_LOG_LEVEL_ERROR | SCREEN_LOG_LEVEL_NO_PREFIX, " ");
|
||||
if ((i+1) % 16 == 0) {
|
||||
print(SCREEN_LOG_LEVEL_ERROR | SCREEN_LOG_LEVEL_NO_PREFIX, "| %s \n", ascii);
|
||||
} else if (i+1 == size) {
|
||||
ascii[(i+1) % 16] = '\0';
|
||||
if ((i+1) % 16 <= 8) {
|
||||
print(SCREEN_LOG_LEVEL_ERROR | SCREEN_LOG_LEVEL_NO_PREFIX, " ");
|
||||
}
|
||||
for (size_t j = (i+1) % 16; j < 16; j++) {
|
||||
print(SCREEN_LOG_LEVEL_ERROR | SCREEN_LOG_LEVEL_NO_PREFIX, " ");
|
||||
}
|
||||
print(SCREEN_LOG_LEVEL_ERROR | SCREEN_LOG_LEVEL_NO_PREFIX, "| %s \n", ascii);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -117,15 +117,12 @@ static inline bool check_32bit_address_range_in_program(uintptr_t addr, size_t s
|
|||
overlaps_a(start, end, __start__, __end__);
|
||||
}
|
||||
|
||||
void hexdump(const void* data, size_t size, uintptr_t addrbase);
|
||||
|
||||
__attribute__((noreturn)) void watchdog_reboot(void);
|
||||
__attribute__((noreturn)) void pmc_reboot(uint32_t scratch0);
|
||||
__attribute__((noreturn)) void reboot_to_self(void);
|
||||
__attribute__((noreturn)) void wait_for_button_and_reboot(void);
|
||||
|
||||
__attribute__((noreturn)) void generic_panic(void);
|
||||
|
||||
__attribute__((noreturn)) void fatal_error(const char *fmt, ...);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -18,67 +18,9 @@
|
|||
#include <string.h>
|
||||
#include "device_partition.h"
|
||||
|
||||
int device_partition_read_data(device_partition_t *devpart, void *dst, uint64_t sector, uint64_t num_sectors)
|
||||
{
|
||||
int rc;
|
||||
if (!devpart->initialized) {
|
||||
rc = devpart->initializer(devpart);
|
||||
if (rc != 0) {
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
if ((devpart->read_cipher != NULL) && (devpart->crypto_mode != DevicePartitionCryptoMode_None)) {
|
||||
for (uint64_t i = 0; i < num_sectors; i += devpart->crypto_work_buffer_num_sectors) {
|
||||
uint64_t n = (i + devpart->crypto_work_buffer_num_sectors > num_sectors) ? (num_sectors - i) : devpart->crypto_work_buffer_num_sectors;
|
||||
rc = devpart->reader(devpart, devpart->crypto_work_buffer, sector + i, n);
|
||||
if (rc != 0) {
|
||||
return rc;
|
||||
}
|
||||
rc = devpart->read_cipher(devpart, sector + i, n);
|
||||
if (rc != 0) {
|
||||
return rc;
|
||||
}
|
||||
memcpy(dst + (size_t)(devpart->sector_size * i), devpart->crypto_work_buffer, (size_t)(devpart->sector_size * n));
|
||||
}
|
||||
return 0;
|
||||
} else {
|
||||
return devpart->reader(devpart, dst, sector, num_sectors);
|
||||
}
|
||||
}
|
||||
|
||||
int device_partition_write_data(device_partition_t *devpart, const void *src, uint64_t sector, uint64_t num_sectors)
|
||||
{
|
||||
int rc;
|
||||
if (!devpart->initialized) {
|
||||
rc = devpart->initializer(devpart);
|
||||
if (rc != 0) {
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
if ((devpart->write_cipher != NULL) && (devpart->crypto_mode != DevicePartitionCryptoMode_None)) {
|
||||
for (uint64_t i = 0; i < num_sectors; i += devpart->crypto_work_buffer_num_sectors) {
|
||||
uint64_t n = (i + devpart->crypto_work_buffer_num_sectors > num_sectors) ? (num_sectors - i) : devpart->crypto_work_buffer_num_sectors;
|
||||
memcpy(devpart->crypto_work_buffer, src + (size_t)(devpart->sector_size * i), (size_t)(devpart->sector_size * n));
|
||||
rc = devpart->write_cipher(devpart, sector + i, n);
|
||||
if (rc != 0) {
|
||||
return rc;
|
||||
}
|
||||
rc = devpart->writer(devpart, devpart->crypto_work_buffer, sector + i, n);
|
||||
if (rc != 0) {
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
} else {
|
||||
return devpart->writer(devpart, src, sector, num_sectors);
|
||||
}
|
||||
}
|
||||
|
||||
int emu_device_partition_read_data(device_partition_t *devpart, void *dst, uint64_t sector, uint64_t num_sectors, const char *origin_path, int num_parts, uint64_t part_limit)
|
||||
{
|
||||
int device_partition_read_data(device_partition_t *devpart, void *dst, uint64_t sector, uint64_t num_sectors) {
|
||||
int rc = 0;
|
||||
uint64_t target_sector = sector;
|
||||
char target_path[0x300 + 1] = {0};
|
||||
|
||||
/* Perform initialization steps, if necessary. */
|
||||
if (!devpart->initialized) {
|
||||
|
@ -88,33 +30,37 @@ int emu_device_partition_read_data(device_partition_t *devpart, void *dst, uint6
|
|||
}
|
||||
}
|
||||
|
||||
/* Prepare the right file path if using file mode. */
|
||||
if (devpart->emu_use_file && (origin_path != NULL)) {
|
||||
/* Handle data in multiple parts, if necessary. */
|
||||
if (num_parts > 0) {
|
||||
int target_part = 0;
|
||||
uint64_t data_offset = sector * devpart->sector_size;
|
||||
/* Handle emulation. */
|
||||
if (devpart->is_emulated) {
|
||||
/* Prepare the right file path if using file mode. */
|
||||
if (devpart->emu_use_file) {
|
||||
int num_parts = devpart->emu_num_parts;
|
||||
uint64_t part_limit = devpart->emu_part_limit;
|
||||
|
||||
if (data_offset >= part_limit) {
|
||||
uint64_t data_offset_aligned = (data_offset + (part_limit - 1)) & ~(part_limit - 1);
|
||||
target_part = (data_offset_aligned == data_offset) ? (data_offset / part_limit) : (data_offset_aligned / part_limit) - 1;
|
||||
target_sector = (data_offset - (target_part * part_limit)) / devpart->sector_size;
|
||||
|
||||
/* Target part is invalid. */
|
||||
if (target_part > num_parts) {
|
||||
return -1;
|
||||
/* Handle data in multiple parts, if necessary. */
|
||||
if (num_parts > 0) {
|
||||
int target_part = 0;
|
||||
char target_path[0x300 + 1] = {0};
|
||||
uint64_t data_offset = sector * devpart->sector_size;
|
||||
|
||||
if (data_offset >= part_limit) {
|
||||
uint64_t data_offset_aligned = (data_offset + (part_limit - 1)) & ~(part_limit - 1);
|
||||
target_part = (data_offset_aligned == data_offset) ? (data_offset / part_limit) : (data_offset_aligned / part_limit) - 1;
|
||||
target_sector = (data_offset - (target_part * part_limit)) / devpart->sector_size;
|
||||
|
||||
/* Target part is invalid. */
|
||||
if (target_part > num_parts) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Treat the path as a folder with each part inside. */
|
||||
snprintf(target_path, sizeof(target_path) - 1, "%s/%02d", devpart->emu_root_path, target_part);
|
||||
|
||||
/* Update the target file path. */
|
||||
strcpy(devpart->emu_file_path, target_path);
|
||||
}
|
||||
|
||||
/* Treat the path as a folder with each part inside. */
|
||||
snprintf(target_path, sizeof(target_path) - 1, "%s/%02d", origin_path, target_part);
|
||||
} else {
|
||||
/* If there are no parts, copy the origin path directly. */
|
||||
strcpy(target_path, origin_path);
|
||||
}
|
||||
|
||||
/* Update the target file path. */
|
||||
devpart->emu_file_path = target_path;
|
||||
}
|
||||
|
||||
/* Read the partition data. */
|
||||
|
@ -147,11 +93,9 @@ int emu_device_partition_read_data(device_partition_t *devpart, void *dst, uint6
|
|||
return rc;
|
||||
}
|
||||
|
||||
int emu_device_partition_write_data(device_partition_t *devpart, const void *src, uint64_t sector, uint64_t num_sectors, const char *origin_path, int num_parts, uint64_t part_limit)
|
||||
{
|
||||
int device_partition_write_data(device_partition_t *devpart, const void *src, uint64_t sector, uint64_t num_sectors) {
|
||||
int rc = 0;
|
||||
uint64_t target_sector = sector;
|
||||
char target_path[0x300 + 1] = {0};
|
||||
|
||||
/* Perform initialization steps, if necessary. */
|
||||
if (!devpart->initialized) {
|
||||
|
@ -161,33 +105,37 @@ int emu_device_partition_write_data(device_partition_t *devpart, const void *src
|
|||
}
|
||||
}
|
||||
|
||||
/* Prepare the right file path if using file mode. */
|
||||
if (devpart->emu_use_file && (origin_path != NULL)) {
|
||||
/* Handle data in multiple parts, if necessary. */
|
||||
if (num_parts > 0) {
|
||||
int target_part = 0;
|
||||
uint64_t data_offset = sector * devpart->sector_size;
|
||||
/* Handle emulation. */
|
||||
if (devpart->is_emulated) {
|
||||
/* Prepare the right file path if using file mode. */
|
||||
if (devpart->emu_use_file) {
|
||||
int num_parts = devpart->emu_num_parts;
|
||||
uint64_t part_limit = devpart->emu_part_limit;
|
||||
|
||||
if (data_offset >= part_limit) {
|
||||
uint64_t data_offset_aligned = (data_offset + (part_limit - 1)) & ~(part_limit - 1);
|
||||
target_part = (data_offset_aligned == data_offset) ? (data_offset / part_limit) : (data_offset_aligned / part_limit) - 1;
|
||||
target_sector = (data_offset - (target_part * part_limit)) / devpart->sector_size;
|
||||
|
||||
/* Target part is invalid. */
|
||||
if (target_part > num_parts) {
|
||||
return -1;
|
||||
/* Handle data in multiple parts, if necessary. */
|
||||
if (num_parts > 0) {
|
||||
int target_part = 0;
|
||||
char target_path[0x300 + 1] = {0};
|
||||
uint64_t data_offset = sector * devpart->sector_size;
|
||||
|
||||
if (data_offset >= part_limit) {
|
||||
uint64_t data_offset_aligned = (data_offset + (part_limit - 1)) & ~(part_limit - 1);
|
||||
target_part = (data_offset_aligned == data_offset) ? (data_offset / part_limit) : (data_offset_aligned / part_limit) - 1;
|
||||
target_sector = (data_offset - (target_part * part_limit)) / devpart->sector_size;
|
||||
|
||||
/* Target part is invalid. */
|
||||
if (target_part > num_parts) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Treat the path as a folder with each part inside. */
|
||||
snprintf(target_path, sizeof(target_path) - 1, "%s/%02d", devpart->emu_root_path, target_part);
|
||||
|
||||
/* Update the target file path. */
|
||||
strcpy(devpart->emu_file_path, target_path);
|
||||
}
|
||||
|
||||
/* Treat the path as a folder with each part inside. */
|
||||
snprintf(target_path, sizeof(target_path) - 1, "%s/%02d", origin_path, target_part);
|
||||
} else {
|
||||
/* If there are no parts, copy the origin path directly. */
|
||||
strcpy(target_path, origin_path);
|
||||
}
|
||||
|
||||
/* Update the target file path. */
|
||||
devpart->emu_file_path = target_path;
|
||||
}
|
||||
|
||||
/* Write the partition data. */
|
||||
|
|
|
@ -67,13 +67,16 @@ typedef struct device_partition_t {
|
|||
uint8_t __attribute__((aligned(16))) iv[DEVPART_IV_MAX_SIZE]; /* IV. */
|
||||
bool initialized;
|
||||
|
||||
char *emu_file_path; /* Emulated device file path. */
|
||||
/* Emulation only. */
|
||||
bool is_emulated;
|
||||
bool emu_use_file;
|
||||
char emu_root_path[0x100 + 1];
|
||||
char emu_file_path[0x300 + 1];
|
||||
int emu_num_parts;
|
||||
uint64_t emu_part_limit;
|
||||
} device_partition_t;
|
||||
|
||||
int device_partition_read_data(device_partition_t *devpart, void *dst, uint64_t sector, uint64_t num_sectors);
|
||||
int device_partition_write_data(device_partition_t *devpart, const void *src, uint64_t sector, uint64_t num_sectors);
|
||||
int emu_device_partition_read_data(device_partition_t *devpart, void *dst, uint64_t sector, uint64_t num_sectors, const char *origin_path, int num_parts, uint64_t part_limit);
|
||||
int emu_device_partition_write_data(device_partition_t *devpart, const void *src, uint64_t sector, uint64_t num_sectors, const char *origin_path, int num_parts, uint64_t part_limit);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,489 +0,0 @@
|
|||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/iosupport.h>
|
||||
#include <sys/param.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "emu_dev.h"
|
||||
#include "utils.h"
|
||||
|
||||
static int emudev_open(struct _reent *r, void *fileStruct, const char *path, int flags, int mode);
|
||||
static int emudev_close(struct _reent *r, void *fd);
|
||||
static ssize_t emudev_write(struct _reent *r, void *fd, const char *ptr, size_t len);
|
||||
static ssize_t emudev_read(struct _reent *r, void *fd, char *ptr, size_t len);
|
||||
static off_t emudev_seek(struct _reent *r, void *fd, off_t pos, int whence);
|
||||
static int emudev_fstat(struct _reent *r, void *fd, struct stat *st);
|
||||
static int emudev_stat(struct _reent *r, const char *file, struct stat *st);
|
||||
static int emudev_fsync(struct _reent *r, void *fd);
|
||||
|
||||
typedef struct emudev_device_t {
|
||||
devoptab_t devoptab;
|
||||
|
||||
char origin_path[0x300+1];
|
||||
int num_parts;
|
||||
uint64_t part_limit;
|
||||
uint8_t *tmp_sector;
|
||||
device_partition_t devpart;
|
||||
char name[32+1];
|
||||
char root_path[34+1];
|
||||
bool setup, registered;
|
||||
} emudev_device_t;
|
||||
|
||||
typedef struct emudev_file_t {
|
||||
emudev_device_t *device;
|
||||
int open_flags;
|
||||
uint64_t offset;
|
||||
} emudev_file_t;
|
||||
|
||||
static emudev_device_t g_emudev_devices[EMUDEV_MAX_DEVICES] = {0};
|
||||
|
||||
static devoptab_t g_emudev_devoptab = {
|
||||
.structSize = sizeof(emudev_file_t),
|
||||
.open_r = emudev_open,
|
||||
.close_r = emudev_close,
|
||||
.write_r = emudev_write,
|
||||
.read_r = emudev_read,
|
||||
.seek_r = emudev_seek,
|
||||
.fstat_r = emudev_fstat,
|
||||
.stat_r = emudev_stat,
|
||||
.fsync_r = emudev_fsync,
|
||||
.deviceData = NULL,
|
||||
};
|
||||
|
||||
static emudev_device_t *emudev_find_device(const char *name) {
|
||||
for (size_t i = 0; i < EMUDEV_MAX_DEVICES; i++) {
|
||||
if (g_emudev_devices[i].setup && strcmp(g_emudev_devices[i].name, name) == 0) {
|
||||
return &g_emudev_devices[i];
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int emudev_mount_device(const char *name, const device_partition_t *devpart, const char *origin_path, int num_parts, uint64_t part_limit) {
|
||||
emudev_device_t *device = NULL;
|
||||
|
||||
if (name[0] == '\0' || devpart == NULL) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (strlen(name) > 32) {
|
||||
errno = ENAMETOOLONG;
|
||||
return -1;
|
||||
}
|
||||
if (emudev_find_device(name) != NULL) {
|
||||
errno = EEXIST; /* Device already exists */
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Find an unused slot. */
|
||||
for (size_t i = 0; i < EMUDEV_MAX_DEVICES; i++) {
|
||||
if (!g_emudev_devices[i].setup) {
|
||||
device = &g_emudev_devices[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (device == NULL) {
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(device, 0, sizeof(emudev_device_t));
|
||||
device->devoptab = g_emudev_devoptab;
|
||||
device->devpart = *devpart;
|
||||
strcpy(device->name, name);
|
||||
strcpy(device->root_path, name);
|
||||
strcat(device->root_path, ":/");
|
||||
|
||||
/* Copy the file path for file mode. */
|
||||
if (devpart->emu_use_file)
|
||||
strcpy(device->origin_path, origin_path);
|
||||
|
||||
device->num_parts = num_parts;
|
||||
device->part_limit = part_limit;
|
||||
|
||||
device->devoptab.name = device->name;
|
||||
device->devoptab.deviceData = device;
|
||||
|
||||
/* Initialize immediately. */
|
||||
int rc = device->devpart.initializer(&device->devpart);
|
||||
if (rc != 0) {
|
||||
errno = rc;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Allocate memory for our intermediate sector. */
|
||||
device->tmp_sector = (uint8_t *)malloc(devpart->sector_size);
|
||||
if (device->tmp_sector == NULL) {
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
|
||||
device->setup = true;
|
||||
device->registered = false;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int emudev_register_device(const char *name) {
|
||||
emudev_device_t *device = emudev_find_device(name);
|
||||
if (device == NULL) {
|
||||
errno = ENOENT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (device->registered) {
|
||||
/* Do nothing if the device is already registered. */
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (AddDevice(&device->devoptab) == -1) {
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
} else {
|
||||
device->registered = true;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int emudev_unregister_device(const char *name) {
|
||||
emudev_device_t *device = emudev_find_device(name);
|
||||
char drname[40];
|
||||
|
||||
if (device == NULL) {
|
||||
errno = ENOENT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!device->registered) {
|
||||
/* Do nothing if the device is not registered. */
|
||||
return 0;
|
||||
}
|
||||
|
||||
strcpy(drname, name);
|
||||
strcat(drname, ":");
|
||||
|
||||
if (RemoveDevice(drname) == -1) {
|
||||
errno = ENOENT;
|
||||
return -1;
|
||||
} else {
|
||||
device->registered = false;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int emudev_unmount_device(const char *name) {
|
||||
int rc;
|
||||
emudev_device_t *device = emudev_find_device(name);
|
||||
|
||||
if (device == NULL) {
|
||||
errno = ENOENT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
rc = emudev_unregister_device(name);
|
||||
if (rc == -1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
free(device->tmp_sector);
|
||||
device->devpart.finalizer(&device->devpart);
|
||||
memset(device, 0, sizeof(emudev_device_t));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int emudev_unmount_all(void) {
|
||||
for (size_t i = 0; i < EMUDEV_MAX_DEVICES; i++) {
|
||||
int rc = emudev_unmount_device(g_emudev_devices[i].name);
|
||||
if (rc != 0) {
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int emudev_open(struct _reent *r, void *fileStruct, const char *path, int flags, int mode) {
|
||||
(void)mode;
|
||||
emudev_file_t *f = (emudev_file_t *)fileStruct;
|
||||
emudev_device_t *device = (emudev_device_t *)(r->deviceData);
|
||||
|
||||
/* Only allow "device:/". */
|
||||
if (strcmp(path, device->root_path) != 0) {
|
||||
r->_errno = ENOENT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Forbid some flags that we explicitly don't support.*/
|
||||
if (flags & (O_APPEND | O_TRUNC | O_EXCL)) {
|
||||
r->_errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(f, 0, sizeof(emudev_file_t));
|
||||
f->device = device;
|
||||
f->open_flags = flags;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int emudev_close(struct _reent *r, void *fd) {
|
||||
(void)r;
|
||||
emudev_file_t *f = (emudev_file_t *)fd;
|
||||
memset(f, 0, sizeof(emudev_file_t));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static ssize_t emudev_write(struct _reent *r, void *fd, const char *ptr, size_t len) {
|
||||
emudev_file_t *f = (emudev_file_t *)fd;
|
||||
emudev_device_t *device = f->device;
|
||||
size_t sector_size = device->devpart.sector_size;
|
||||
uint64_t sector_begin = f->offset / sector_size;
|
||||
uint64_t sector_end = (f->offset + len + sector_size - 1) / sector_size;
|
||||
uint64_t sector_end_aligned;
|
||||
uint64_t current_sector = sector_begin;
|
||||
const uint8_t *data = (const uint8_t *)ptr;
|
||||
|
||||
int no = 0;
|
||||
|
||||
if (sector_end >= device->devpart.num_sectors) {
|
||||
len = (size_t)(sector_size * device->devpart.num_sectors - f->offset);
|
||||
sector_end = device->devpart.num_sectors;
|
||||
}
|
||||
|
||||
sector_end_aligned = sector_end - ((f->offset + len) % sector_size != 0 ? 1 : 0);
|
||||
|
||||
if (len == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Unaligned at the start, we need to read the sector and incorporate the data. */
|
||||
if (f->offset % sector_size != 0) {
|
||||
size_t nb = (size_t)(len <= (sector_size - (f->offset % sector_size)) ? len : sector_size - (f->offset % sector_size));
|
||||
no = emu_device_partition_read_data(&device->devpart, device->tmp_sector, sector_begin, 1, device->origin_path, device->num_parts, device->part_limit);
|
||||
if (no != 0) {
|
||||
r->_errno = no;
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(device->tmp_sector + (f->offset % sector_size), data, nb);
|
||||
|
||||
no = emu_device_partition_write_data(&device->devpart, device->tmp_sector, sector_begin, 1, device->origin_path, device->num_parts, device->part_limit);
|
||||
if (no != 0) {
|
||||
r->_errno = no;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Advance */
|
||||
data += sector_size - (f->offset % sector_size);
|
||||
current_sector++;
|
||||
}
|
||||
|
||||
/* Check if we're already done (otherwise this causes a bug in handling the last sector of the range). */
|
||||
if (current_sector == sector_end) {
|
||||
f->offset += len;
|
||||
return len;
|
||||
}
|
||||
|
||||
/* Write all of the sector-aligned data. */
|
||||
if (current_sector != sector_end_aligned) {
|
||||
no = emu_device_partition_write_data(&device->devpart, data, current_sector, sector_end_aligned - current_sector, device->origin_path, device->num_parts, device->part_limit);
|
||||
if (no != 0) {
|
||||
r->_errno = no;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
data += sector_size * (sector_end_aligned - current_sector);
|
||||
current_sector = sector_end_aligned;
|
||||
|
||||
/* Unaligned at the end, we need to read the sector and incorporate the data. */
|
||||
if (sector_end != sector_end_aligned) {
|
||||
no = emu_device_partition_read_data(&device->devpart, device->tmp_sector, sector_end_aligned, 1, device->origin_path, device->num_parts, device->part_limit);
|
||||
if (no != 0) {
|
||||
r->_errno = no;
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(device->tmp_sector, data, (size_t)((f->offset + len) % sector_size));
|
||||
|
||||
no = emu_device_partition_write_data(&device->devpart, device->tmp_sector, sector_end_aligned, 1, device->origin_path, device->num_parts, device->part_limit);
|
||||
if (no != 0) {
|
||||
r->_errno = no;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Advance */
|
||||
data += sector_size - ((f->offset + len) % sector_size);
|
||||
current_sector++;
|
||||
}
|
||||
|
||||
f->offset += len;
|
||||
return len;
|
||||
}
|
||||
|
||||
static ssize_t emudev_read(struct _reent *r, void *fd, char *ptr, size_t len) {
|
||||
emudev_file_t *f = (emudev_file_t *)fd;
|
||||
emudev_device_t *device = f->device;
|
||||
size_t sector_size = device->devpart.sector_size;
|
||||
uint64_t sector_begin = f->offset / sector_size;
|
||||
uint64_t sector_end = (f->offset + len + sector_size - 1) / sector_size;
|
||||
uint64_t sector_end_aligned;
|
||||
uint64_t current_sector = sector_begin;
|
||||
uint8_t *data = (uint8_t *)ptr;
|
||||
|
||||
int no = 0;
|
||||
|
||||
if (sector_end >= device->devpart.num_sectors) {
|
||||
len = (size_t)(sector_size * device->devpart.num_sectors - f->offset);
|
||||
sector_end = device->devpart.num_sectors;
|
||||
}
|
||||
|
||||
sector_end_aligned = sector_end - ((f->offset + len) % sector_size != 0 ? 1 : 0);
|
||||
|
||||
if (len == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Unaligned at the start, we need to read the sector and incorporate the data. */
|
||||
if (f->offset % sector_size != 0) {
|
||||
size_t nb = (size_t)(len <= (sector_size - (f->offset % sector_size)) ? len : sector_size - (f->offset % sector_size));
|
||||
no = emu_device_partition_read_data(&device->devpart, device->tmp_sector, sector_begin, 1, device->origin_path, device->num_parts, device->part_limit);
|
||||
if (no != 0) {
|
||||
r->_errno = no;
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(data, device->tmp_sector + (f->offset % sector_size), nb);
|
||||
|
||||
/* Advance */
|
||||
data += sector_size - (f->offset % sector_size);
|
||||
current_sector++;
|
||||
}
|
||||
|
||||
/* Check if we're already done (otherwise this causes a bug in handling the last sector of the range). */
|
||||
if (current_sector == sector_end) {
|
||||
f->offset += len;
|
||||
return len;
|
||||
}
|
||||
|
||||
/* Read all of the sector-aligned data. */
|
||||
if (current_sector != sector_end_aligned) {
|
||||
no = emu_device_partition_read_data(&device->devpart, data, current_sector, sector_end_aligned - current_sector, device->origin_path, device->num_parts, device->part_limit);
|
||||
if (no != 0) {
|
||||
r->_errno = no;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
data += sector_size * (sector_end_aligned - current_sector);
|
||||
current_sector = sector_end_aligned;
|
||||
|
||||
/* Unaligned at the end, we need to read the sector and incorporate the data. */
|
||||
if (sector_end != sector_end_aligned) {
|
||||
no = emu_device_partition_read_data(&device->devpart, device->tmp_sector, sector_end_aligned, 1, device->origin_path, device->num_parts, device->part_limit);
|
||||
if (no != 0) {
|
||||
r->_errno = no;
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(data, device->tmp_sector, (size_t)((f->offset + len) % sector_size));
|
||||
|
||||
/* Advance */
|
||||
data += sector_size - ((f->offset + len) % sector_size);
|
||||
current_sector++;
|
||||
}
|
||||
|
||||
f->offset += len;
|
||||
return len;
|
||||
}
|
||||
|
||||
static off_t emudev_seek(struct _reent *r, void *fd, off_t pos, int whence) {
|
||||
emudev_file_t *f = (emudev_file_t *)fd;
|
||||
emudev_device_t *device = f->device;
|
||||
uint64_t off;
|
||||
|
||||
switch (whence) {
|
||||
case SEEK_SET:
|
||||
off = 0;
|
||||
break;
|
||||
case SEEK_CUR:
|
||||
off = f->offset;
|
||||
break;
|
||||
case SEEK_END:
|
||||
off = device->devpart.num_sectors * device->devpart.sector_size;
|
||||
break;
|
||||
default:
|
||||
r->_errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (pos < 0 && pos + off < 0) {
|
||||
/* don't allow seek to before the beginning of the file */
|
||||
r->_errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
f->offset = (uint64_t)(pos + off);
|
||||
return (off_t)(pos + off);
|
||||
}
|
||||
|
||||
static void emudev_stat_impl(emudev_device_t *device, struct stat *st) {
|
||||
memset(st, 0, sizeof(struct stat));
|
||||
st->st_size = (off_t)(device->devpart.num_sectors * device->devpart.sector_size);
|
||||
st->st_nlink = 1;
|
||||
|
||||
st->st_blksize = device->devpart.sector_size;
|
||||
st->st_blocks = st->st_size / st->st_blksize;
|
||||
|
||||
st->st_mode = S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
|
||||
}
|
||||
|
||||
static int emudev_fstat(struct _reent *r, void *fd, struct stat *st) {
|
||||
(void)r;
|
||||
emudev_file_t *f = (emudev_file_t *)fd;
|
||||
emudev_device_t *device = f->device;
|
||||
emudev_stat_impl(device, st);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int emudev_stat(struct _reent *r, const char *file, struct stat *st) {
|
||||
emudev_device_t *device = (emudev_device_t *)(r->deviceData);
|
||||
if (strcmp(file, device->root_path) != 0) {
|
||||
r->_errno = ENOENT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
emudev_stat_impl(device, st);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int emudev_fsync(struct _reent *r, void *fd) {
|
||||
/* Nothing to do. */
|
||||
(void)r;
|
||||
(void)fd;
|
||||
return 0;
|
||||
}
|
|
@ -14,6 +14,7 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
#include "exception_handlers.h"
|
||||
|
@ -22,7 +23,7 @@
|
|||
#include "lib/log.h"
|
||||
|
||||
#define CODE_DUMP_SIZE 0x30
|
||||
#define STACK_DUMP_SIZE 0x60
|
||||
#define STACK_DUMP_SIZE 0x30
|
||||
|
||||
extern const uint32_t exception_handler_table[];
|
||||
|
||||
|
@ -35,6 +36,40 @@ static const char *register_names[] = {
|
|||
"SP", "LR", "PC", "CPSR",
|
||||
};
|
||||
|
||||
/* Adapted from https://gist.github.com/ccbrown/9722406 */
|
||||
static void hexdump(const void* data, size_t size, uintptr_t addrbase, char* strbuf) {
|
||||
const uint8_t *d = (const uint8_t *)data;
|
||||
char ascii[17] = {0};
|
||||
ascii[16] = '\0';
|
||||
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
if (i % 16 == 0) {
|
||||
strbuf += sprintf(strbuf, "%0*" PRIXPTR ": | ", 2 * sizeof(addrbase), addrbase + i);
|
||||
}
|
||||
strbuf += sprintf(strbuf, "%02X ", d[i]);
|
||||
if (d[i] >= ' ' && d[i] <= '~') {
|
||||
ascii[i % 16] = d[i];
|
||||
} else {
|
||||
ascii[i % 16] = '.';
|
||||
}
|
||||
if ((i+1) % 8 == 0 || i+1 == size) {
|
||||
strbuf += sprintf(strbuf, " ");
|
||||
if ((i+1) % 16 == 0) {
|
||||
strbuf += sprintf(strbuf, "| %s \n", ascii);
|
||||
} else if (i+1 == size) {
|
||||
ascii[(i+1) % 16] = '\0';
|
||||
if ((i+1) % 16 <= 8) {
|
||||
strbuf += sprintf(strbuf, " ");
|
||||
}
|
||||
for (size_t j = (i+1) % 16; j < 16; j++) {
|
||||
strbuf += sprintf(strbuf, " ");
|
||||
}
|
||||
strbuf += sprintf(strbuf, "| %s \n", ascii);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setup_exception_handlers(void) {
|
||||
volatile uint32_t *bpmp_exception_handler_table = (volatile uint32_t *)0x6000F200;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
|
@ -45,38 +80,40 @@ void setup_exception_handlers(void) {
|
|||
}
|
||||
|
||||
void exception_handler_main(uint32_t *registers, unsigned int exception_type) {
|
||||
uint8_t code_dump[CODE_DUMP_SIZE];
|
||||
uint8_t stack_dump[STACK_DUMP_SIZE];
|
||||
size_t code_dump_size;
|
||||
size_t stack_dump_size;
|
||||
char exception_log[0x400] = {0};
|
||||
uint8_t code_dump[CODE_DUMP_SIZE] = {0};
|
||||
uint8_t stack_dump[STACK_DUMP_SIZE] = {0};
|
||||
size_t code_dump_size = 0;
|
||||
size_t stack_dump_size = 0;
|
||||
|
||||
uint32_t pc = registers[15];
|
||||
uint32_t cpsr = registers[16];
|
||||
|
||||
uint32_t instr_addr = pc + ((cpsr & 0x20) ? 2 : 4) - CODE_DUMP_SIZE;
|
||||
|
||||
print(SCREEN_LOG_LEVEL_ERROR, "\nSomething went wrong...\n");
|
||||
|
||||
sprintf(exception_log, "An exception occured!\n");
|
||||
|
||||
code_dump_size = safecpy(code_dump, (const void *)instr_addr, CODE_DUMP_SIZE);
|
||||
stack_dump_size = safecpy(stack_dump, (const void *)registers[13], STACK_DUMP_SIZE);
|
||||
|
||||
print(SCREEN_LOG_LEVEL_ERROR | SCREEN_LOG_LEVEL_NO_PREFIX, "\nException type: %s\n",
|
||||
exception_names[exception_type]);
|
||||
print(SCREEN_LOG_LEVEL_ERROR | SCREEN_LOG_LEVEL_NO_PREFIX, "\nRegisters:\n\n");
|
||||
sprintf(exception_log + strlen(exception_log), "\nException type: %s\n", exception_names[exception_type]);
|
||||
sprintf(exception_log + strlen(exception_log), "\nRegisters:\n");
|
||||
|
||||
/* Print r0 to pc. */
|
||||
for (int i = 0; i < 16; i += 2) {
|
||||
print(SCREEN_LOG_LEVEL_ERROR | SCREEN_LOG_LEVEL_NO_PREFIX, "%-7s%08"PRIX32" %-7s%08"PRIX32"\n",
|
||||
sprintf(exception_log + strlen(exception_log), "%-7s%08"PRIX32" %-7s%08"PRIX32"\n",
|
||||
register_names[i], registers[i], register_names[i+1], registers[i+1]);
|
||||
}
|
||||
|
||||
/* Print cpsr. */
|
||||
print(SCREEN_LOG_LEVEL_ERROR | SCREEN_LOG_LEVEL_NO_PREFIX, "%-7s%08"PRIX32"\n", register_names[16], registers[16]);
|
||||
|
||||
print(SCREEN_LOG_LEVEL_ERROR | SCREEN_LOG_LEVEL_NO_PREFIX, "\nCode dump:\n");
|
||||
hexdump(code_dump, code_dump_size, instr_addr);
|
||||
print(SCREEN_LOG_LEVEL_ERROR | SCREEN_LOG_LEVEL_NO_PREFIX, "\nStack dump:\n");
|
||||
hexdump(stack_dump, stack_dump_size, registers[13]);
|
||||
print(SCREEN_LOG_LEVEL_ERROR | SCREEN_LOG_LEVEL_NO_PREFIX, "\n");
|
||||
fatal_error("An exception occurred!\n");
|
||||
sprintf(exception_log + strlen(exception_log), "%-7s%08"PRIX32"\n", register_names[16], registers[16]);
|
||||
|
||||
/* Print code and stack regions. */
|
||||
sprintf(exception_log + strlen(exception_log), "\nCode dump:\n");
|
||||
hexdump(code_dump, code_dump_size, instr_addr, exception_log + strlen(exception_log));
|
||||
sprintf(exception_log + strlen(exception_log), "\nStack dump:\n");
|
||||
hexdump(stack_dump, stack_dump_size, registers[13], exception_log + strlen(exception_log));
|
||||
sprintf(exception_log + strlen(exception_log), "\n");
|
||||
|
||||
/* Throw fatal error with the full exception log. */
|
||||
fatal_error(exception_log);
|
||||
}
|
||||
|
|
|
@ -302,7 +302,6 @@ int fsdev_register_keys(const char *name, unsigned int target_firmware, BisParti
|
|||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int fsdev_unmount_all(void) {
|
||||
for (size_t i = 0; i < FF_VOLUMES; i++) {
|
||||
int ret = fsdev_unmount_device(g_fsdev_devices[i].name);
|
||||
|
|
|
@ -112,42 +112,3 @@ int gpt_iterate_through_entries(FILE *disk, size_t sector_size, gpt_entry_iterat
|
|||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int gpt_iterate_through_emu_entries(FILE *disk, size_t sector_size, gpt_emu_entry_iterator_t callback, void *param, const char *origin_path, int num_parts, uint64_t part_limit) {
|
||||
efi_header_t hdr;
|
||||
efi_entry_t entry;
|
||||
size_t offset = 2 * 512; /* Sector #2. */
|
||||
size_t delta;
|
||||
|
||||
/* Get the header. */
|
||||
if (gpt_get_header(&hdr, disk, sector_size) == -1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Seek to the entry table. */
|
||||
if (fseek(disk, sector_size * hdr.entries_first_lba - offset, SEEK_CUR) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
offset = sector_size * hdr.entries_first_lba;
|
||||
delta = hdr.entry_size - sizeof(efi_entry_t);
|
||||
|
||||
/* Iterate through the entries. */
|
||||
for (uint32_t i = 0; i < hdr.entry_count; i++) {
|
||||
if (!fread(&entry, sizeof(efi_entry_t), 1, disk)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (callback(&entry, param, offset, disk, origin_path, num_parts, part_limit) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (delta != 0 && fseek(disk, delta, SEEK_CUR) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
offset += hdr.entry_size;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -56,6 +56,5 @@ typedef int (*gpt_emu_entry_iterator_t)(const efi_entry_t *entry, void *param, s
|
|||
|
||||
int gpt_get_header(efi_header_t *out, FILE *disk, size_t sector_size);
|
||||
int gpt_iterate_through_entries(FILE *disk, size_t sector_size, gpt_entry_iterator_t callback, void *param);
|
||||
int gpt_iterate_through_emu_entries(FILE *disk, size_t sector_size, gpt_emu_entry_iterator_t callback, void *param, const char *origin_path, int num_parts, uint64_t part_limit);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -963,6 +963,11 @@ void package2_patch_kernel(void *_kernel, size_t *kernel_size, bool is_sd_kernel
|
|||
}
|
||||
|
||||
if (kernel_info == NULL && is_sd_kernel) {
|
||||
/* If the kernel is mesosphere, patch it. */
|
||||
if (*(volatile uint32_t *)((uintptr_t)_kernel + 4) == 0x3053534D) {
|
||||
*out_ini1 = (void *)((uintptr_t)_kernel + *(volatile uint32_t *)((uintptr_t)_kernel + 8));
|
||||
*(volatile uint64_t *)((uintptr_t)_kernel + 8) = (uint64_t)*kernel_size;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -971,12 +976,6 @@ void package2_patch_kernel(void *_kernel, size_t *kernel_size, bool is_sd_kernel
|
|||
const uint32_t kernel_ldr_offset = *((volatile uint64_t *)((uintptr_t)_kernel + kernel_info->embedded_ini_ptr + 8));
|
||||
memcpy((void *)((uintptr_t)_kernel + kernel_ldr_offset), kernel_ldr_bin, kernel_ldr_bin_size);
|
||||
|
||||
/* Set target firmware for our kernel loader. */
|
||||
uint32_t *kldr_u32 = (uint32_t *)((uintptr_t)_kernel + kernel_ldr_offset);
|
||||
if (kldr_u32[1] == 0x30444C4D) {
|
||||
kldr_u32[2] = target_firmware;
|
||||
}
|
||||
|
||||
/* Update size. */
|
||||
*kernel_size = kernel_ldr_offset + kernel_ldr_bin_size;
|
||||
|
||||
|
|
|
@ -273,7 +273,10 @@ void derive_bis_key(void *dst, BisPartition partition_id, uint32_t target_firmwa
|
|||
}
|
||||
};
|
||||
|
||||
const uint32_t bis_key_generation = fuse_get_5x_key_generation();
|
||||
uint32_t bis_key_generation = fuse_get_5x_key_generation();
|
||||
if (bis_key_generation > 0) {
|
||||
bis_key_generation -= 1;
|
||||
}
|
||||
|
||||
static const uint8_t AL16 bis_kek_source[0x10] = {0x34, 0xC1, 0xA0, 0xC4, 0x82, 0x58, 0xF8, 0xB4, 0xFA, 0x9E, 0x5E, 0x6A, 0xDA, 0xFC, 0x7E, 0x4F};
|
||||
switch (partition_id) {
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
#ifndef FUSEE_LOG_H
|
||||
#define FUSEE_LOG_H
|
||||
|
||||
#define PRINT_MESSAGE_MAX_LENGTH 512
|
||||
#define PRINT_MESSAGE_MAX_LENGTH 1024
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
|
|
|
@ -64,7 +64,6 @@
|
|||
#undef u32
|
||||
|
||||
extern const uint8_t warmboot_bin[];
|
||||
extern const uint32_t warmboot_bin_size;
|
||||
|
||||
static const uint8_t retail_pkc_modulus[0x100] = {
|
||||
0xF7, 0x86, 0x47, 0xAB, 0x71, 0x89, 0x81, 0xB5, 0xCF, 0x0C, 0xB0, 0xE8, 0x48, 0xA7, 0xFD, 0xAD,
|
||||
|
@ -236,6 +235,8 @@ static uint32_t nxboot_get_specific_target_firmware(uint32_t target_firmware){
|
|||
#define CHECK_NCA(NCA_ID, VERSION) do { if (is_nca_present(NCA_ID)) { return ATMOSPHERE_TARGET_FIRMWARE_##VERSION; } } while(0)
|
||||
|
||||
if (target_firmware >= ATMOSPHERE_TARGET_FIRMWARE_10_0_0) {
|
||||
CHECK_NCA("5077973537f6735b564dd7475b779f87", 10_1_1); /* Exclusive to China. */
|
||||
CHECK_NCA("fd1faed0ca750700d254c0915b93d506", 10_1_0);
|
||||
CHECK_NCA("34728c771299443420820d8ae490ea41", 10_0_4);
|
||||
CHECK_NCA("5b1df84f88c3334335bbb45d8522cbb4", 10_0_3);
|
||||
CHECK_NCA("e951bc9dedcd54f65ffd83d4d050f9e0", 10_0_2);
|
||||
|
@ -649,7 +650,7 @@ uint32_t nxboot_main(void) {
|
|||
} else {
|
||||
emummc_size = get_file_size("atmosphere/emummc.kip");
|
||||
if (emummc_size != 0) {
|
||||
/* Allocate memory for the TSEC firmware. */
|
||||
/* Allocate memory for the emummc KIP. */
|
||||
emummc = memalign(0x100, emummc_size);
|
||||
|
||||
if (emummc == NULL) {
|
||||
|
@ -838,13 +839,7 @@ uint32_t nxboot_main(void) {
|
|||
|
||||
/* Derive new device keys. */
|
||||
{
|
||||
if (target_firmware >= ATMOSPHERE_TARGET_FIRMWARE_5_0_0) {
|
||||
derive_new_device_keys(fuse_get_retail_type() != 0, KEYSLOT_SWITCH_5XNEWDEVICEKEYGENKEY, target_firmware);
|
||||
} else if (target_firmware >= ATMOSPHERE_TARGET_FIRMWARE_4_0_0) {
|
||||
derive_new_device_keys(fuse_get_retail_type() != 0, KEYSLOT_SWITCH_4XNEWDEVICEKEYGENKEY, target_firmware);
|
||||
} else {
|
||||
/* No new keys to derive */
|
||||
}
|
||||
derive_new_device_keys(fuse_get_retail_type() != 0, KEYSLOT_SWITCH_5XNEWDEVICEKEYGENKEY, target_firmware);
|
||||
}
|
||||
|
||||
/* Set the system partition's keys. */
|
||||
|
|
|
@ -36,7 +36,6 @@ static bool g_emmc_device_initialized = false;
|
|||
|
||||
static bool g_fsdev_ready = false;
|
||||
static bool g_rawdev_ready = false;
|
||||
static bool g_emudev_ready = false;
|
||||
|
||||
static bool g_is_emummc = false;
|
||||
|
||||
|
@ -356,87 +355,6 @@ static int nxfs_mount_partition_gpt_callback(const efi_entry_t *entry, void *par
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int nxfs_mount_emu_partition_gpt_callback(const efi_entry_t *entry, void *param, size_t entry_offset, FILE *disk, const char *origin_path, int num_parts, uint64_t part_limit) {
|
||||
(void)entry_offset;
|
||||
(void)disk;
|
||||
device_partition_t *parent = (device_partition_t *)param;
|
||||
device_partition_t devpart = *parent;
|
||||
char name_buffer[128];
|
||||
const uint16_t *utf16name = entry->name;
|
||||
uint32_t name_len;
|
||||
int rc;
|
||||
|
||||
static const struct {
|
||||
const char *partition_name;
|
||||
const char *mount_point;
|
||||
bool is_fat;
|
||||
bool is_encrypted;
|
||||
bool register_immediately;
|
||||
} known_partitions[] = {
|
||||
{"PRODINFO", "prodinfo", false, true, false},
|
||||
{"PRODINFOF", "prodinfof", true, true, false},
|
||||
{"BCPKG2-1-Normal-Main", "bcpkg21", false, false, true},
|
||||
{"BCPKG2-2-Normal-Sub", "bcpkg22", false, false, false},
|
||||
{"BCPKG2-3-SafeMode-Main", "bcpkg23", false, false, false},
|
||||
{"BCPKG2-4-SafeMode-Sub", "bcpkg24", false, false, false},
|
||||
{"BCPKG2-5-Repair-Main", "bcpkg25", false, false, false},
|
||||
{"BCPKG2-6-Repair-Sub", "bcpkg26", false, false, false},
|
||||
{"SAFE", "safe", true, true, false},
|
||||
{"SYSTEM", "system", true, true, true},
|
||||
{"USER", "user", true, true, false},
|
||||
};
|
||||
|
||||
/* Convert the partition name to ASCII, for comparison. */
|
||||
for (name_len = 0; name_len < sizeof(entry->name) && *utf16name != 0; name_len++) {
|
||||
name_buffer[name_len] = (char)*utf16name++;
|
||||
}
|
||||
name_buffer[name_len] = '\0';
|
||||
|
||||
/* Mount the partition, if we know about it. */
|
||||
for (size_t i = 0; i < sizeof(known_partitions)/sizeof(known_partitions[0]); i++) {
|
||||
if (strcmp(name_buffer, known_partitions[i].partition_name) == 0) {
|
||||
devpart.start_sector += entry->first_lba;
|
||||
devpart.num_sectors = (entry->last_lba + 1) - entry->first_lba;
|
||||
if (parent->num_sectors < devpart.num_sectors) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (known_partitions[i].is_encrypted) {
|
||||
devpart.read_cipher = nxfs_bis_crypto_decrypt;
|
||||
devpart.write_cipher = nxfs_bis_crypto_encrypt;
|
||||
devpart.crypto_mode = DevicePartitionCryptoMode_Xts;
|
||||
}
|
||||
|
||||
if (known_partitions[i].is_fat) {
|
||||
rc = fsdev_mount_device(known_partitions[i].mount_point, &devpart, false);
|
||||
if (rc == -1) {
|
||||
return -1;
|
||||
}
|
||||
if (known_partitions[i].register_immediately) {
|
||||
rc = fsdev_register_device(known_partitions[i].mount_point);
|
||||
if (rc == -1) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
rc = emudev_mount_device(known_partitions[i].mount_point, &devpart, origin_path, num_parts, part_limit);
|
||||
if (rc == -1) {
|
||||
return -1;
|
||||
}
|
||||
if (known_partitions[i].register_immediately) {
|
||||
rc = emudev_register_device(known_partitions[i].mount_point);
|
||||
if (rc == -1) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int nxfs_mount_sd() {
|
||||
device_partition_t model;
|
||||
int rc;
|
||||
|
@ -446,6 +364,7 @@ int nxfs_mount_sd() {
|
|||
model.device_struct = &g_sd_mmcpart;
|
||||
model.start_sector = 0;
|
||||
model.num_sectors = 1u << 30; /* arbitrary numbers of sectors. TODO: find the size of the SD in sectors. */
|
||||
model.is_emulated = false;
|
||||
|
||||
/* Mount the SD card device. */
|
||||
rc = fsdev_mount_device("sdmc", &model, true);
|
||||
|
@ -479,6 +398,7 @@ int nxfs_mount_emmc() {
|
|||
model.device_struct = &g_emmc_boot0_mmcpart;
|
||||
model.start_sector = 0;
|
||||
model.num_sectors = 0x184000 / model.sector_size;
|
||||
model.is_emulated = false;
|
||||
|
||||
/* Mount boot0 device. */
|
||||
rc = rawdev_mount_device("boot0", &model, true);
|
||||
|
@ -499,6 +419,7 @@ int nxfs_mount_emmc() {
|
|||
model.device_struct = &g_emmc_boot1_mmcpart;
|
||||
model.start_sector = 0;
|
||||
model.num_sectors = 0x80000 / model.sector_size;
|
||||
model.is_emulated = false;
|
||||
|
||||
/* Mount boot1 device. */
|
||||
rc = rawdev_mount_device("boot1", &model, false);
|
||||
|
@ -514,6 +435,7 @@ int nxfs_mount_emmc() {
|
|||
model.device_struct = &g_emmc_user_mmcpart;
|
||||
model.start_sector = 0;
|
||||
model.num_sectors = (256ull << 30) / model.sector_size;
|
||||
model.is_emulated = false;
|
||||
|
||||
/* Mount raw NAND device. */
|
||||
rc = rawdev_mount_device("rawnand", &model, false);
|
||||
|
@ -558,10 +480,11 @@ int nxfs_mount_emummc_partition(uint64_t emummc_start_sector) {
|
|||
model = g_emummc_devpart_template;
|
||||
model.start_sector = emummc_start_sector + (0x400000 * 0 / model.sector_size);
|
||||
model.num_sectors = 0x400000 / model.sector_size;
|
||||
model.is_emulated = true;
|
||||
model.emu_use_file = false;
|
||||
|
||||
/* Mount emulated boot0 device. */
|
||||
rc = emudev_mount_device("boot0", &model, NULL, 0, 0);
|
||||
rc = rawdev_mount_device("boot0", &model, true);
|
||||
|
||||
/* Failed to mount boot0 device. */
|
||||
if (rc == -1) {
|
||||
|
@ -569,7 +492,7 @@ int nxfs_mount_emummc_partition(uint64_t emummc_start_sector) {
|
|||
}
|
||||
|
||||
/* Register emulated boot0 device. */
|
||||
rc = emudev_register_device("boot0");
|
||||
rc = rawdev_register_device("boot0");
|
||||
|
||||
/* Failed to register boot0 device. */
|
||||
if (rc == -1) {
|
||||
|
@ -580,10 +503,11 @@ int nxfs_mount_emummc_partition(uint64_t emummc_start_sector) {
|
|||
model = g_emummc_devpart_template;
|
||||
model.start_sector = emummc_start_sector + (0x400000 * 1 / model.sector_size);
|
||||
model.num_sectors = 0x400000 / model.sector_size;
|
||||
model.is_emulated = true;
|
||||
model.emu_use_file = false;
|
||||
|
||||
/* Mount emulated boot1 device. */
|
||||
rc = emudev_mount_device("boot1", &model, NULL, 0, 0);
|
||||
rc = rawdev_mount_device("boot1", &model, false);
|
||||
|
||||
/* Failed to mount boot1. */
|
||||
if (rc == -1) {
|
||||
|
@ -596,10 +520,11 @@ int nxfs_mount_emummc_partition(uint64_t emummc_start_sector) {
|
|||
model = g_emummc_devpart_template;
|
||||
model.start_sector = emummc_start_sector + (0x400000 * 2 / model.sector_size);
|
||||
model.num_sectors = (256ull << 30) / model.sector_size;
|
||||
model.is_emulated = true;
|
||||
model.emu_use_file = false;
|
||||
|
||||
/* Mount emulated raw NAND device. */
|
||||
rc = emudev_mount_device("rawnand", &model, NULL, 0, 0);
|
||||
rc = rawdev_mount_device("rawnand", &model, false);
|
||||
|
||||
/* Failed to mount raw NAND. */
|
||||
if (rc == -1) {
|
||||
|
@ -607,7 +532,7 @@ int nxfs_mount_emummc_partition(uint64_t emummc_start_sector) {
|
|||
}
|
||||
|
||||
/* Register emulated raw NAND device. */
|
||||
rc = emudev_register_device("rawnand");
|
||||
rc = rawdev_register_device("rawnand");
|
||||
|
||||
/* Failed to register raw NAND device. */
|
||||
if (rc == -1) {
|
||||
|
@ -623,14 +548,14 @@ int nxfs_mount_emummc_partition(uint64_t emummc_start_sector) {
|
|||
}
|
||||
|
||||
/* Iterate the GPT and mount each emulated raw NAND partition. */
|
||||
rc = gpt_iterate_through_emu_entries(rawnand, model.sector_size, nxfs_mount_emu_partition_gpt_callback, &model, NULL, 0, 0);
|
||||
rc = gpt_iterate_through_entries(rawnand, model.sector_size, nxfs_mount_partition_gpt_callback, &model);
|
||||
|
||||
/* Close emulated raw NAND device. */
|
||||
fclose(rawnand);
|
||||
|
||||
/* All emulated devices are ready. */
|
||||
if (rc == 0) {
|
||||
g_emudev_ready = true;
|
||||
g_rawdev_ready = true;
|
||||
g_is_emummc = true;
|
||||
}
|
||||
|
||||
|
@ -641,10 +566,9 @@ int nxfs_mount_emummc_file(const char *emummc_path, int num_parts, uint64_t part
|
|||
device_partition_t model;
|
||||
int rc;
|
||||
FILE *rawnand;
|
||||
bool is_exfat;
|
||||
char emummc_boot0_path[0x300 + 1] = {0};
|
||||
char emummc_boot1_path[0x300 + 1] = {0};
|
||||
|
||||
|
||||
/* Check if the SD card is EXFAT formatted. */
|
||||
rc = fsdev_is_exfat("sdmc");
|
||||
|
||||
|
@ -652,29 +576,20 @@ int nxfs_mount_emummc_file(const char *emummc_path, int num_parts, uint64_t part
|
|||
if (rc == -1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Set EXFAT status. */
|
||||
is_exfat = (rc == 1);
|
||||
|
||||
/* Reject single part in FAT32. */
|
||||
/* NOTE: This check has no effect in the current design. */
|
||||
if (!is_exfat && (num_parts < 1)) {
|
||||
return -2;
|
||||
}
|
||||
|
||||
|
||||
/* We want a folder with the archive bit set. */
|
||||
rc = fsdev_get_attr(emummc_path);
|
||||
|
||||
/* Failed to get file DOS attributes. */
|
||||
if (rc == -1) {
|
||||
return -3;
|
||||
return -2;
|
||||
}
|
||||
|
||||
|
||||
/* Our path is not a directory. */
|
||||
if (!(rc & AM_DIR)) {
|
||||
return -4;
|
||||
return -3;
|
||||
}
|
||||
|
||||
|
||||
/* Check if the archive bit is not set. */
|
||||
if (!(rc & AM_ARC)) {
|
||||
/* Try to set the archive bit. */
|
||||
|
@ -682,102 +597,117 @@ int nxfs_mount_emummc_file(const char *emummc_path, int num_parts, uint64_t part
|
|||
|
||||
/* Failed to set file DOS attributes. */
|
||||
if (rc == -1) {
|
||||
return -5;
|
||||
return -4;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Prepare boot0 file path. */
|
||||
snprintf(emummc_boot0_path, sizeof(emummc_boot0_path) - 1, "%s/%s", emummc_path, "boot0");
|
||||
|
||||
/* Setup an emulation template for boot0. */
|
||||
model = g_emummc_devpart_template;
|
||||
model.start_sector = 0;
|
||||
model.num_sectors = 0x400000 / model.sector_size;
|
||||
model.is_emulated = true;
|
||||
model.emu_use_file = true;
|
||||
|
||||
/* Prepare boot0 file path. */
|
||||
snprintf(emummc_boot0_path, sizeof(emummc_boot0_path) - 1, "%s/%s", emummc_path, "boot0");
|
||||
model.emu_num_parts = 0;
|
||||
model.emu_part_limit = 0;
|
||||
strcpy(model.emu_root_path, emummc_path);
|
||||
strcpy(model.emu_file_path, emummc_boot0_path);
|
||||
|
||||
/* Mount emulated boot0 device. */
|
||||
rc = emudev_mount_device("boot0", &model, emummc_boot0_path, 0, 0);
|
||||
rc = rawdev_mount_device("boot0", &model, true);
|
||||
|
||||
/* Failed to mount boot0 device. */
|
||||
if (rc == -1) {
|
||||
return -6;
|
||||
return -5;
|
||||
}
|
||||
|
||||
|
||||
/* Register emulated boot0 device. */
|
||||
rc = emudev_register_device("boot0");
|
||||
rc = rawdev_register_device("boot0");
|
||||
|
||||
/* Failed to register boot0 device. */
|
||||
if (rc == -1) {
|
||||
return -7;
|
||||
return -6;
|
||||
}
|
||||
|
||||
|
||||
/* Prepare boot1 file path. */
|
||||
snprintf(emummc_boot1_path, sizeof(emummc_boot1_path) - 1, "%s/%s", emummc_path, "boot1");
|
||||
|
||||
/* Setup an emulation template for boot1. */
|
||||
model = g_emummc_devpart_template;
|
||||
model.start_sector = 0;
|
||||
model.num_sectors = 0x400000 / model.sector_size;
|
||||
model.is_emulated = true;
|
||||
model.emu_use_file = true;
|
||||
|
||||
/* Prepare boot1 file path. */
|
||||
snprintf(emummc_boot1_path, sizeof(emummc_boot1_path) - 1, "%s/%s", emummc_path, "boot1");
|
||||
|
||||
model.emu_num_parts = 0;
|
||||
model.emu_part_limit = 0;
|
||||
strcpy(model.emu_root_path, emummc_path);
|
||||
strcpy(model.emu_file_path, emummc_boot1_path);
|
||||
|
||||
/* Mount emulated boot1 device. */
|
||||
rc = emudev_mount_device("boot1", &model, emummc_boot1_path, 0, 0);
|
||||
rc = rawdev_mount_device("boot1", &model, false);
|
||||
|
||||
/* Failed to mount boot1. */
|
||||
if (rc == -1) {
|
||||
return -8;
|
||||
return -7;
|
||||
}
|
||||
|
||||
|
||||
/* Register emulated boot1 device. */
|
||||
rc = emudev_register_device("boot1");
|
||||
rc = rawdev_register_device("boot1");
|
||||
|
||||
/* Failed to register boot1 device. */
|
||||
if (rc == -1) {
|
||||
return -9;
|
||||
return -8;
|
||||
}
|
||||
|
||||
|
||||
/* Setup a template for raw NAND. */
|
||||
model = g_emummc_devpart_template;
|
||||
model.start_sector = 0;
|
||||
model.num_sectors = (256ull << 30) / model.sector_size;
|
||||
model.is_emulated = true;
|
||||
model.emu_use_file = true;
|
||||
model.emu_num_parts = num_parts;
|
||||
model.emu_part_limit = part_limit;
|
||||
strcpy(model.emu_root_path, emummc_path);
|
||||
strcpy(model.emu_file_path, emummc_path);
|
||||
|
||||
/* Mount emulated raw NAND device from single or multiple parts. */
|
||||
rc = emudev_mount_device("rawnand", &model, emummc_path, num_parts, part_limit);
|
||||
rc = rawdev_mount_device("rawnand", &model, false);
|
||||
|
||||
/* Failed to mount raw NAND. */
|
||||
if (rc == -1) {
|
||||
return -10;
|
||||
return -9;
|
||||
}
|
||||
|
||||
|
||||
/* Register emulated raw NAND device. */
|
||||
rc = emudev_register_device("rawnand");
|
||||
rc = rawdev_register_device("rawnand");
|
||||
|
||||
/* Failed to register raw NAND device. */
|
||||
if (rc == -1) {
|
||||
return -11;
|
||||
return -10;
|
||||
}
|
||||
|
||||
|
||||
/* Open emulated raw NAND device. */
|
||||
rawnand = fopen("rawnand:/", "rb");
|
||||
|
||||
/* Failed to open emulated raw NAND device. */
|
||||
if (rawnand == NULL) {
|
||||
return -12;
|
||||
return -11;
|
||||
}
|
||||
|
||||
|
||||
/* Iterate the GPT and mount each emulated raw NAND partition. */
|
||||
rc = gpt_iterate_through_emu_entries(rawnand, model.sector_size, nxfs_mount_emu_partition_gpt_callback, &model, emummc_path, num_parts, part_limit);
|
||||
rc = gpt_iterate_through_entries(rawnand, model.sector_size, nxfs_mount_partition_gpt_callback, &model);
|
||||
|
||||
/* Close emulated raw NAND device. */
|
||||
fclose(rawnand);
|
||||
|
||||
/* All emulated devices are ready. */
|
||||
if (rc == 0) {
|
||||
g_emudev_ready = true;
|
||||
g_rawdev_ready = true;
|
||||
g_is_emummc = true;
|
||||
}
|
||||
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
@ -805,18 +735,6 @@ int nxfs_unmount_emmc() {
|
|||
return rc;
|
||||
}
|
||||
|
||||
int nxfs_unmount_emummc() {
|
||||
int rc = 0;
|
||||
|
||||
/* Unmount all emulated devices. */
|
||||
if (g_emudev_ready) {
|
||||
rc = emudev_unmount_all();
|
||||
g_emudev_ready = false;
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
int nxfs_init() {
|
||||
int rc;
|
||||
|
||||
|
@ -832,5 +750,5 @@ int nxfs_init() {
|
|||
}
|
||||
|
||||
int nxfs_end() {
|
||||
return ((nxfs_unmount_sd() || nxfs_unmount_emmc() || nxfs_unmount_emummc()) ? -1 : 0);
|
||||
return ((nxfs_unmount_sd() || nxfs_unmount_emmc()) ? -1 : 0);
|
||||
}
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
|
||||
#include "fs_dev.h"
|
||||
#include "raw_dev.h"
|
||||
#include "emu_dev.h"
|
||||
|
||||
int nxfs_init();
|
||||
int nxfs_end();
|
||||
|
|
|
@ -77,9 +77,6 @@ static rawdev_device_t *rawdev_find_device(const char *name) {
|
|||
|
||||
int rawdev_mount_device(const char *name, const device_partition_t *devpart, bool initialize_immediately) {
|
||||
rawdev_device_t *device = NULL;
|
||||
char drname[40];
|
||||
strcpy(drname, name);
|
||||
strcat(drname, ":");
|
||||
|
||||
if (name[0] == '\0' || devpart == NULL) {
|
||||
errno = EINVAL;
|
||||
|
@ -205,6 +202,19 @@ int rawdev_unmount_device(const char *name) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
int rawdev_register_keys(const char *name, unsigned int target_firmware, BisPartition part) {
|
||||
rawdev_device_t *device = rawdev_find_device(name);
|
||||
|
||||
if (device == NULL) {
|
||||
errno = ENOENT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
derive_bis_key(device->devpart.keys, part, target_firmware);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int rawdev_unmount_all(void) {
|
||||
for (size_t i = 0; i < RAWDEV_MAX_DEVICES; i++) {
|
||||
int rc = rawdev_unmount_device(g_rawdev_devices[i].name);
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "device_partition.h"
|
||||
#include "key_derivation.h"
|
||||
|
||||
#define RAWDEV_MAX_DEVICES 16
|
||||
|
||||
|
@ -29,6 +30,8 @@ int rawdev_register_device(const char *name);
|
|||
int rawdev_unregister_device(const char *name);
|
||||
int rawdev_unmount_device(const char *name); /* also unregisters. */
|
||||
|
||||
int rawdev_register_keys(const char *name, unsigned int target_firmware, BisPartition part);
|
||||
|
||||
int rawdev_unmount_all(void);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -51,7 +51,6 @@ static bool g_stratosphere_boot_enabled = true;
|
|||
static bool g_stratosphere_ncm_enabled = false;
|
||||
|
||||
extern const uint8_t loader_kip[], pm_kip[], sm_kip[], spl_kip[], boot_kip[], ncm_kip[], ams_mitm_kip[];
|
||||
extern const uint32_t loader_kip_size, pm_kip_size, sm_kip_size, spl_kip_size, boot_kip_size, ncm_kip_size, ams_mitm_kip_size;
|
||||
|
||||
static emummc_fs_ver_t g_fs_ver = FS_VER_1_0_0;
|
||||
|
||||
|
|
|
@ -157,12 +157,18 @@ __attribute__ ((noreturn)) void generic_panic(void) {
|
|||
}
|
||||
|
||||
__attribute__((noreturn)) void fatal_error(const char *fmt, ...) {
|
||||
/* Override the global logging level. */
|
||||
log_set_log_level(SCREEN_LOG_LEVEL_ERROR);
|
||||
|
||||
/* Display fatal error. */
|
||||
va_list args;
|
||||
print(SCREEN_LOG_LEVEL_ERROR, "Fatal error: ");
|
||||
va_start(args, fmt);
|
||||
vprint(SCREEN_LOG_LEVEL_ERROR, fmt, args);
|
||||
va_end(args);
|
||||
print(SCREEN_LOG_LEVEL_ERROR | SCREEN_LOG_LEVEL_NO_PREFIX, "\n Press POWER to reboot.\n");
|
||||
|
||||
/* Wait for button and reboot. */
|
||||
wait_for_button_and_reboot();
|
||||
}
|
||||
|
||||
|
@ -174,37 +180,3 @@ __attribute__((noinline)) bool overlaps(uint64_t as, uint64_t ae, uint64_t bs, u
|
|||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Adapted from https://gist.github.com/ccbrown/9722406 */
|
||||
void hexdump(const void* data, size_t size, uintptr_t addrbase) {
|
||||
const uint8_t *d = (const uint8_t *)data;
|
||||
char ascii[17];
|
||||
ascii[16] = '\0';
|
||||
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
if (i % 16 == 0) {
|
||||
printf("%0*" PRIXPTR ": | ", 2 * sizeof(addrbase), addrbase + i);
|
||||
}
|
||||
printf("%02X ", d[i]);
|
||||
if (d[i] >= ' ' && d[i] <= '~') {
|
||||
ascii[i % 16] = d[i];
|
||||
} else {
|
||||
ascii[i % 16] = '.';
|
||||
}
|
||||
if ((i+1) % 8 == 0 || i+1 == size) {
|
||||
printf(" ");
|
||||
if ((i+1) % 16 == 0) {
|
||||
printf("| %s \n", ascii);
|
||||
} else if (i+1 == size) {
|
||||
ascii[(i+1) % 16] = '\0';
|
||||
if ((i+1) % 16 <= 8) {
|
||||
printf(" ");
|
||||
}
|
||||
for (size_t j = (i+1) % 16; j < 16; j++) {
|
||||
printf(" ");
|
||||
}
|
||||
printf("| %s \n", ascii);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -118,8 +118,6 @@ static inline bool check_32bit_address_range_in_program(uintptr_t addr, size_t s
|
|||
overlaps_a(start, end, __start__, __end__);
|
||||
}
|
||||
|
||||
void hexdump(const void* data, size_t size, uintptr_t addrbase);
|
||||
|
||||
__attribute__((noreturn)) void watchdog_reboot(void);
|
||||
__attribute__((noreturn)) void pmc_reboot(uint32_t scratch0);
|
||||
__attribute__((noreturn)) void reboot_to_fusee_primary(void);
|
||||
|
@ -129,8 +127,6 @@ __attribute__((noreturn)) void wait_for_button_and_reboot(void);
|
|||
void wait_for_button(void);
|
||||
|
||||
__attribute__((noreturn)) void generic_panic(void);
|
||||
|
||||
__attribute__((noreturn)) void fatal_error(const char *fmt, ...);
|
||||
|
||||
|
||||
#endif
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
[subrepo]
|
||||
remote = https://github.com/Atmosphere-NX/Atmosphere-libs
|
||||
branch = master
|
||||
commit = cf8f0c3c1f006e07c0b3976908220d3e7e83f7fa
|
||||
parent = 033ae1dbe09ba354849caf90ca2a2f114d9b3b4b
|
||||
commit = cac5957d3f4b1417cf76a83cf704a14a254dd4dc
|
||||
parent = 3726def6ecc547e64912ddb050737ebd296366e7
|
||||
method = merge
|
||||
cmdver = 0.4.1
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
ATMOSPHERE_LIBRARIES := libmesosphere libstratosphere
|
||||
ATMOSPHERE_LIBRARIES := libmesosphere libstratosphere libexosphere
|
||||
|
||||
TOPTARGETS := all clean
|
||||
|
||||
|
|
|
@ -17,8 +17,10 @@ endif
|
|||
|
||||
export ATMOSPHERE_DEFINES := -DATMOSPHERE
|
||||
export ATMOSPHERE_SETTINGS := -fPIE -g
|
||||
export ATMOSPHERE_CFLAGS := -Wall -ffunction-sections -fdata-sections -fno-strict-aliasing -fwrapv \
|
||||
-fno-asynchronous-unwind-tables -fno-unwind-tables -fno-stack-protector
|
||||
export ATMOSPHERE_CFLAGS := -Wall -ffunction-sections -fdata-sections -fno-strict-aliasing -fwrapv \
|
||||
-fno-asynchronous-unwind-tables -fno-unwind-tables -fno-stack-protector \
|
||||
-Wno-format-truncation -Wno-format-zero-length -Wno-stringop-truncation
|
||||
|
||||
export ATMOSPHERE_CXXFLAGS := -fno-rtti -fno-exceptions -std=gnu++20
|
||||
export ATMOSPHERE_ASFLAGS :=
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ export CFLAGS := $(ATMOSPHERE_CFLAGS) $(SETTINGS) $(DEFINES) $(INCLUDE)
|
|||
export CXXFLAGS := $(CFLAGS) $(ATMOSPHERE_CXXFLAGS) -fno-use-cxa-atexit
|
||||
export ASFLAGS := $(ATMOSPHERE_ASFLAGS) $(SETTINGS) $(DEFINES)
|
||||
|
||||
export LDFLAGS = -specs=$(TOPDIR)/$(notdir $(TOPDIR)).specs -nostdlib -nostartfiles -g $(SETTINGS) -Wl,-Map,$(notdir $*.map) -Wl,-z,relro,-z,now
|
||||
export LDFLAGS = -specs=$(TOPDIR)/$(notdir $(TOPDIR)).specs -fno-asynchronous-unwind-tables -fno-unwind-tables -nostdlib -nostartfiles -g $(SETTINGS) -Wl,-Map,$(notdir $*.map) -Wl,-z,relro,-z,now
|
||||
|
||||
export CXXWRAPS := -Wl,--wrap,__cxa_pure_virtual \
|
||||
-Wl,--wrap,__cxa_throw \
|
||||
|
|
|
@ -19,34 +19,40 @@
|
|||
namespace ams::pkg1 {
|
||||
|
||||
enum AesKeySlot {
|
||||
AesKeySlot_UserStart = 0,
|
||||
AesKeySlot_UserStart = 0,
|
||||
|
||||
AesKeySlot_TzramSaveKek = 2,
|
||||
AesKeySlot_TzramSaveKey = 3,
|
||||
AesKeySlot_TzramSaveKek = 2,
|
||||
AesKeySlot_TzramSaveKey = 3,
|
||||
|
||||
AesKeySlot_UserLast = 5,
|
||||
AesKeySlot_UserEnd = AesKeySlot_UserLast + 1,
|
||||
AesKeySlot_UserLast = 5,
|
||||
AesKeySlot_UserEnd = AesKeySlot_UserLast + 1,
|
||||
|
||||
AesKeySlot_SecmonStart = 8,
|
||||
AesKeySlot_SecmonStart = 8,
|
||||
|
||||
AesKeySlot_Temporary = 8,
|
||||
AesKeySlot_Smc = 9,
|
||||
AesKeySlot_RandomForUserWrap = 10,
|
||||
AesKeySlot_RandomForKeyStorageWrap = 11,
|
||||
AesKeySlot_DeviceMaster = 12,
|
||||
AesKeySlot_Master = 13,
|
||||
AesKeySlot_Device = 15,
|
||||
AesKeySlot_Temporary = 8,
|
||||
AesKeySlot_Smc = 9,
|
||||
AesKeySlot_RandomForUserWrap = 10,
|
||||
AesKeySlot_RandomForKeyStorageWrap = 11,
|
||||
AesKeySlot_DeviceMaster = 12,
|
||||
AesKeySlot_Master = 13,
|
||||
AesKeySlot_Device = 15,
|
||||
|
||||
AesKeySlot_SecmonEnd = 16,
|
||||
AesKeySlot_Count = 16,
|
||||
AesKeySlot_SecmonEnd = AesKeySlot_Count,
|
||||
|
||||
/* Used only during boot. */
|
||||
AesKeySlot_Tsec = 12,
|
||||
AesKeySlot_TsecRoot = 13,
|
||||
AesKeySlot_SecureBoot = 14,
|
||||
AesKeySlot_SecureStorage = 15,
|
||||
AesKeySlot_Tsec = 12,
|
||||
AesKeySlot_TsecRoot = 13,
|
||||
AesKeySlot_SecureBoot = 14,
|
||||
AesKeySlot_SecureStorage = 15,
|
||||
|
||||
AesKeySlot_MasterKek = 13,
|
||||
AesKeySlot_DeviceMasterKeySourceKek = 14,
|
||||
AesKeySlot_DeviceMasterKeySourceKekErista = 10,
|
||||
AesKeySlot_MasterKek = 13,
|
||||
AesKeySlot_DeviceMasterKeySourceKekMariko = 14,
|
||||
|
||||
/* Mariko only keyslots, used during boot. */
|
||||
AesKeySlot_MarikoKek = 12,
|
||||
AesKeySlot_MarikoBek = 13,
|
||||
};
|
||||
|
||||
enum RsaKeySlot {
|
||||
|
|
|
@ -26,6 +26,9 @@ namespace ams::se {
|
|||
void ClearAesKeyIv(int slot);
|
||||
void LockAesKeySlot(int slot, u32 flags);
|
||||
|
||||
/* NOTE: This is Nintendo's API, but if we actually want to use SE2 we should use a different one. */
|
||||
void ClearAesKeySlot2(int slot);
|
||||
|
||||
void SetAesKey(int slot, const void *key, size_t key_size);
|
||||
|
||||
void SetEncryptedAesKey128(int dst_slot, int kek_slot, const void *key, size_t key_size);
|
||||
|
|
|
@ -18,13 +18,14 @@
|
|||
|
||||
namespace ams::se {
|
||||
|
||||
void SetRegisterAddress(uintptr_t address);
|
||||
void SetRegisterAddress(uintptr_t address, uintptr_t address2);
|
||||
|
||||
void Initialize();
|
||||
|
||||
void SetSecure(bool secure);
|
||||
void SetTzramSecure();
|
||||
void SetPerKeySecure();
|
||||
void SetContextSaveSecure();
|
||||
|
||||
void Lockout();
|
||||
|
||||
|
|
|
@ -53,4 +53,8 @@ namespace ams::se {
|
|||
bool ValidateStickyBits(const StickyBits &bits);
|
||||
void SaveContext(Context *dst);
|
||||
|
||||
void ConfigureAutomaticContextSave();
|
||||
void SaveContextAutomatic();
|
||||
void SaveTzramAutomatic();
|
||||
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
#define AHB_MASTER_SWID (0x018)
|
||||
#define AHB_MASTER_SWID_1 (0x038)
|
||||
#define AHB_GIZMO_TZRAM (0x054)
|
||||
#define AHB_AHB_SPARE_REG (0x110)
|
||||
|
||||
#define AHB_REG_BITS_MASK(NAME) REG_NAMED_BITS_MASK (AHB_, NAME)
|
||||
#define AHB_REG_BITS_VALUE(NAME, VALUE) REG_NAMED_BITS_VALUE (AHB_, NAME, VALUE)
|
||||
|
@ -39,3 +40,8 @@ DEFINE_AHB_REG_BIT_ENUM(ARBITRATION_DISABLE_COP, 1, ENABLE, DISABLE);
|
|||
DEFINE_AHB_REG_BIT_ENUM(ARBITRATION_DISABLE_AHBDMA, 5, ENABLE, DISABLE);
|
||||
DEFINE_AHB_REG_BIT_ENUM(ARBITRATION_DISABLE_USB, 6, ENABLE, DISABLE);
|
||||
DEFINE_AHB_REG_BIT_ENUM(ARBITRATION_DISABLE_USB2, 18, ENABLE, DISABLE);
|
||||
|
||||
DEFINE_AHB_REG(AHB_SPARE_REG_CSITE_PADMACRO3_TRIM_SEL, 0, 5);
|
||||
DEFINE_AHB_REG_BIT_ENUM(AHB_SPARE_REG_OBS_OVERRIDE_EN, 5, DISABLE, ENABLE);
|
||||
DEFINE_AHB_REG_BIT_ENUM(AHB_SPARE_REG_APB2JTAG_OVERRIDE_EN, 6, DISABLE, ENABLE);
|
||||
DEFINE_AHB_REG(AHB_SPARE_REG_AHB_SPARE_REG, 12, 32-12);
|
||||
|
|
|
@ -114,6 +114,7 @@
|
|||
#define APBDEV_PMC_SECURE_SCRATCH113 (0xB1C)
|
||||
#define APBDEV_PMC_SECURE_SCRATCH114 (0xB20)
|
||||
#define APBDEV_PMC_SECURE_SCRATCH115 (0xB24)
|
||||
#define APBDEV_PMC_SECURE_SCRATCH119 (0xB34)
|
||||
|
||||
|
||||
#define PMC_REG_BITS_MASK(NAME) REG_NAMED_BITS_MASK (APBDEV_PMC, NAME)
|
||||
|
|
|
@ -38,7 +38,7 @@ namespace ams::clkrst {
|
|||
reg::ReadWrite(g_register_address + param.clk_enb_offset, REG_BITS_VALUE(param.index, 1, 0));
|
||||
|
||||
/* Set the clock source. */
|
||||
if (param.clk_src != 0) {
|
||||
if (param.clk_src_offset != 0) {
|
||||
reg::Write(g_register_address + param.clk_src_offset, (param.clk_src << 29) | (param.clk_div << 0));
|
||||
}
|
||||
|
||||
|
@ -89,11 +89,11 @@ namespace ams::clkrst {
|
|||
}
|
||||
|
||||
void EnableUartBClock() {
|
||||
EnableClock(UartAClock);
|
||||
EnableClock(UartBClock);
|
||||
}
|
||||
|
||||
void EnableUartCClock() {
|
||||
EnableClock(UartAClock);
|
||||
EnableClock(UartCClock);
|
||||
}
|
||||
|
||||
void EnableActmonClock() {
|
||||
|
|
|
@ -20,6 +20,10 @@ namespace ams::fuse {
|
|||
|
||||
namespace {
|
||||
|
||||
static constexpr SocType SocType_CommonInternal = static_cast<SocType>(-1);
|
||||
static_assert(SocType_CommonInternal != SocType_Erista);
|
||||
static_assert(SocType_CommonInternal != SocType_Mariko);
|
||||
|
||||
struct BypassEntry {
|
||||
u32 offset;
|
||||
u32 value;
|
||||
|
@ -42,6 +46,11 @@ namespace ams::fuse {
|
|||
using HardwareType3 = util::BitPack32::Field<Reserved::Next, 4, int>;
|
||||
};
|
||||
|
||||
struct OdmWord28 {
|
||||
using Regulator = util::BitPack32::Field<0, 1, int>;
|
||||
using Reserved = util::BitPack32::Field<1, 31, int>;
|
||||
};
|
||||
|
||||
constexpr ALWAYS_INLINE int GetHardwareStateValue(const util::BitPack32 odm_word4) {
|
||||
constexpr auto HardwareState1Shift = 0;
|
||||
constexpr auto HardwareState2Shift = OdmWord4::HardwareState1::Count + HardwareState1Shift;
|
||||
|
@ -73,8 +82,16 @@ namespace ams::fuse {
|
|||
return GetRegisterRegion()->fuse;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE volatile FuseChipRegisters &GetChipRegisters() {
|
||||
return GetRegisterRegion()->chip;
|
||||
ALWAYS_INLINE volatile FuseChipRegistersCommon &GetChipRegistersCommon() {
|
||||
return GetRegisterRegion()->chip_common;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE volatile FuseChipRegistersErista &GetChipRegistersErista() {
|
||||
return GetRegisterRegion()->chip_erista;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE volatile FuseChipRegistersMariko &GetChipRegistersMariko() {
|
||||
return GetRegisterRegion()->chip_mariko;
|
||||
}
|
||||
|
||||
bool IsIdle() {
|
||||
|
@ -85,6 +102,31 @@ namespace ams::fuse {
|
|||
while (!IsIdle()) { /* ... */ }
|
||||
}
|
||||
|
||||
u32 GetOdmWordImpl(int index, fuse::SocType soc_type) {
|
||||
if (index < 8) {
|
||||
volatile auto &chip = GetChipRegistersCommon();
|
||||
return chip.FUSE_RESERVED_ODM_0[index - 0];
|
||||
} else if (soc_type == SocType_Mariko) {
|
||||
volatile auto &chip = GetChipRegistersMariko();
|
||||
if (index < 22) {
|
||||
return chip.FUSE_RESERVED_ODM_8[index - 8];
|
||||
} else if (index < 25) {
|
||||
return chip.FUSE_RESERVED_ODM_22[index - 22];
|
||||
} else if (index < 26) {
|
||||
return chip.FUSE_RESERVED_ODM_25[index - 25];
|
||||
} else if (index < 29) {
|
||||
return chip.FUSE_RESERVED_ODM_26[index - 26];
|
||||
} else if (index < 30) {
|
||||
return chip.FUSE_RESERVED_ODM_29[index - 29];
|
||||
}
|
||||
}
|
||||
AMS_ABORT("Invalid ODM fuse read");
|
||||
}
|
||||
|
||||
u32 GetCommonOdmWord(int index) {
|
||||
return GetOdmWordImpl(index, SocType_CommonInternal);
|
||||
}
|
||||
|
||||
bool IsNewFuseFormat() {
|
||||
/* On mariko, this should always be true. */
|
||||
if (GetSocType() != SocType_Erista) {
|
||||
|
@ -92,7 +134,7 @@ namespace ams::fuse {
|
|||
}
|
||||
|
||||
/* Require that the format version be non-zero in odm4. */
|
||||
if (util::BitPack32{GetOdmWord(4)}.Get<OdmWord4::FormatVersion>() == 0) {
|
||||
if (util::BitPack32{GetCommonOdmWord(4)}.Get<OdmWord4::FormatVersion>() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -100,8 +142,8 @@ namespace ams::fuse {
|
|||
constexpr u32 NewFuseFormatMagic0 = 0x8E61ECAE;
|
||||
constexpr u32 NewFuseFormatMagic1 = 0xF2BA3BB2;
|
||||
|
||||
const u32 w0 = GetOdmWord(0);
|
||||
const u32 w1 = GetOdmWord(1);
|
||||
const u32 w0 = GetCommonOdmWord(0);
|
||||
const u32 w1 = GetCommonOdmWord(1);
|
||||
|
||||
return w0 == NewFuseFormatMagic0 && w1 == NewFuseFormatMagic1;
|
||||
}
|
||||
|
@ -206,12 +248,12 @@ namespace ams::fuse {
|
|||
}
|
||||
|
||||
u32 GetOdmWord(int index) {
|
||||
return GetChipRegisters().FUSE_RESERVED_ODM[index];
|
||||
return GetOdmWordImpl(index, GetSocType());
|
||||
}
|
||||
|
||||
void GetEcid(br::BootEcid *out) {
|
||||
/* Get the registers. */
|
||||
volatile auto &chip = GetChipRegisters();
|
||||
volatile auto &chip = GetChipRegistersCommon();
|
||||
|
||||
/* Read the ecid components. */
|
||||
const u32 vendor = reg::Read(chip.FUSE_OPT_VENDOR_CODE) & ((1u << 4) - 1);
|
||||
|
@ -235,7 +277,7 @@ namespace ams::fuse {
|
|||
|
||||
u64 GetDeviceId() {
|
||||
/* Get the registers. */
|
||||
volatile auto &chip = GetChipRegisters();
|
||||
volatile auto &chip = GetChipRegistersCommon();
|
||||
|
||||
/* Read the device id components. */
|
||||
/* NOTE: Device ID is "basically" just an alternate encoding of Ecid. */
|
||||
|
@ -258,12 +300,12 @@ namespace ams::fuse {
|
|||
}
|
||||
|
||||
DramId GetDramId() {
|
||||
return static_cast<DramId>(util::BitPack32{GetOdmWord(4)}.Get<OdmWord4::DramId>());
|
||||
return static_cast<DramId>(util::BitPack32{GetCommonOdmWord(4)}.Get<OdmWord4::DramId>());
|
||||
}
|
||||
|
||||
HardwareType GetHardwareType() {
|
||||
/* Read the odm word. */
|
||||
const util::BitPack32 odm_word4 = { GetOdmWord(4) };
|
||||
const util::BitPack32 odm_word4 = { GetCommonOdmWord(4) };
|
||||
|
||||
/* Get the value. */
|
||||
const auto value = GetHardwareTypeValue(odm_word4);
|
||||
|
@ -280,7 +322,7 @@ namespace ams::fuse {
|
|||
|
||||
HardwareState GetHardwareState() {
|
||||
/* Read the odm word. */
|
||||
const util::BitPack32 odm_word4 = { GetOdmWord(4) };
|
||||
const util::BitPack32 odm_word4 = { GetCommonOdmWord(4) };
|
||||
|
||||
/* Get the value. */
|
||||
const auto value = GetHardwareStateValue(odm_word4);
|
||||
|
@ -293,22 +335,28 @@ namespace ams::fuse {
|
|||
}
|
||||
|
||||
PatchVersion GetPatchVersion() {
|
||||
const auto patch_version = reg::Read(GetChipRegisters().FUSE_SOC_SPEEDO_1_CALIB);
|
||||
const auto patch_version = reg::Read(GetChipRegistersCommon().FUSE_SOC_SPEEDO_1_CALIB);
|
||||
return static_cast<PatchVersion>(static_cast<int>(GetSocType() << 12) | patch_version);
|
||||
}
|
||||
|
||||
QuestState GetQuestState() {
|
||||
return static_cast<QuestState>(util::BitPack32{GetOdmWord(4)}.Get<OdmWord4::QuestState>());
|
||||
return static_cast<QuestState>(util::BitPack32{GetCommonOdmWord(4)}.Get<OdmWord4::QuestState>());
|
||||
}
|
||||
|
||||
pmic::Regulator GetRegulator() {
|
||||
/* TODO: How should mariko be handled? This reads from ODM word 28 in fuses (not present in erista...). */
|
||||
return pmic::Regulator_Erista_Max77621;
|
||||
if (GetSocType() == SocType_Mariko) {
|
||||
/* Read the odm word. */
|
||||
const util::BitPack32 odm_word28 = { GetOdmWordImpl(28, SocType_Mariko) };
|
||||
|
||||
return static_cast<pmic::Regulator>(odm_word28.Get<OdmWord28::Regulator>() + 1);
|
||||
} else /* if (GetSocType() == SocType_Erista) */ {
|
||||
return pmic::Regulator_Erista_Max77621;
|
||||
}
|
||||
}
|
||||
|
||||
int GetDeviceUniqueKeyGeneration() {
|
||||
if (IsNewFuseFormat()) {
|
||||
return util::BitPack32{GetOdmWord(2)}.Get<OdmWord2::DeviceUniqueKeyGeneration>();
|
||||
return util::BitPack32{GetCommonOdmWord(2)}.Get<OdmWord2::DeviceUniqueKeyGeneration>();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
|
@ -344,13 +392,13 @@ namespace ams::fuse {
|
|||
}
|
||||
|
||||
/* Some patched units use XUSB in RCM. */
|
||||
if (reg::Read(GetChipRegisters().FUSE_RESERVED_SW) & 0x80) {
|
||||
if (reg::Read(GetChipRegistersCommon().FUSE_RESERVED_SW) & 0x80) {
|
||||
g_has_rcm_bug_patch = true;
|
||||
break;
|
||||
}
|
||||
|
||||
/* Other units have a proper ipatch instead. */
|
||||
u32 word_count = reg::Read(GetChipRegisters().FUSE_FIRST_BOOTROM_PATCH_SIZE) & 0x7F;
|
||||
u32 word_count = reg::Read(GetChipRegistersCommon().FUSE_FIRST_BOOTROM_PATCH_SIZE) & 0x7F;
|
||||
u32 word_addr = 191;
|
||||
|
||||
while (word_count && !g_has_rcm_bug_patch) {
|
||||
|
@ -379,7 +427,7 @@ namespace ams::fuse {
|
|||
}
|
||||
|
||||
bool IsOdmProductionMode() {
|
||||
return reg::HasValue(GetChipRegisters().FUSE_SECURITY_MODE, FUSE_REG_BITS_ENUM(SECURITY_MODE_SECURITY_MODE, ENABLED));
|
||||
return reg::HasValue(GetChipRegistersCommon().FUSE_SECURITY_MODE, FUSE_REG_BITS_ENUM(SECURITY_MODE_SECURITY_MODE, ENABLED));
|
||||
}
|
||||
|
||||
void ConfigureFuseBypass() {
|
||||
|
|
|
@ -42,12 +42,162 @@ namespace ams::fuse {
|
|||
u32 FUSE_PRIVATE_KEY2_NONZERO;
|
||||
u32 FUSE_PRIVATE_KEY3_NONZERO;
|
||||
u32 FUSE_PRIVATE_KEY4_NONZERO;
|
||||
u32 _0x94[0x1B];
|
||||
u32 _0x94;
|
||||
};
|
||||
static_assert(util::is_pod<FuseRegisters>::value);
|
||||
static_assert(sizeof(FuseRegisters) == 0x100);
|
||||
static_assert(sizeof(FuseRegisters) == 0x98);
|
||||
|
||||
struct FuseChipRegisters {
|
||||
struct FuseChipRegistersCommon {
|
||||
u32 _0x98[0x1A];
|
||||
u32 FUSE_PRODUCTION_MODE;
|
||||
u32 FUSE_JTAG_SECUREID_VALID;
|
||||
u32 FUSE_ODM_LOCK;
|
||||
u32 FUSE_OPT_OPENGL_EN;
|
||||
u32 FUSE_SKU_INFO;
|
||||
u32 FUSE_CPU_SPEEDO_0_CALIB;
|
||||
u32 FUSE_CPU_IDDQ_CALIB;
|
||||
u32 _0x11C;
|
||||
u32 _0x120;
|
||||
u32 _0x124;
|
||||
u32 FUSE_OPT_FT_REV;
|
||||
u32 FUSE_CPU_SPEEDO_1_CALIB;
|
||||
u32 FUSE_CPU_SPEEDO_2_CALIB;
|
||||
u32 FUSE_SOC_SPEEDO_0_CALIB;
|
||||
u32 FUSE_SOC_SPEEDO_1_CALIB;
|
||||
u32 FUSE_SOC_SPEEDO_2_CALIB;
|
||||
u32 FUSE_SOC_IDDQ_CALIB;
|
||||
u32 _0x144;
|
||||
u32 FUSE_FA;
|
||||
u32 FUSE_RESERVED_PRODUCTION;
|
||||
u32 FUSE_HDMI_LANE0_CALIB;
|
||||
u32 FUSE_HDMI_LANE1_CALIB;
|
||||
u32 FUSE_HDMI_LANE2_CALIB;
|
||||
u32 FUSE_HDMI_LANE3_CALIB;
|
||||
u32 FUSE_ENCRYPTION_RATE;
|
||||
u32 FUSE_PUBLIC_KEY[0x8];
|
||||
u32 FUSE_TSENSOR1_CALIB;
|
||||
u32 FUSE_TSENSOR2_CALIB;
|
||||
u32 _0x18C;
|
||||
u32 FUSE_OPT_CP_REV;
|
||||
u32 FUSE_OPT_PFG;
|
||||
u32 FUSE_TSENSOR0_CALIB;
|
||||
u32 FUSE_FIRST_BOOTROM_PATCH_SIZE;
|
||||
u32 FUSE_SECURITY_MODE;
|
||||
u32 FUSE_PRIVATE_KEY[0x5];
|
||||
u32 FUSE_ARM_JTAG_DIS;
|
||||
u32 FUSE_BOOT_DEVICE_INFO;
|
||||
u32 FUSE_RESERVED_SW;
|
||||
u32 FUSE_OPT_VP9_DISABLE;
|
||||
u32 FUSE_RESERVED_ODM_0[8 - 0];
|
||||
u32 FUSE_OBS_DIS;
|
||||
u32 _0x1EC;
|
||||
u32 FUSE_USB_CALIB;
|
||||
u32 FUSE_SKU_DIRECT_CONFIG;
|
||||
u32 FUSE_KFUSE_PRIVKEY_CTRL;
|
||||
u32 FUSE_PACKAGE_INFO;
|
||||
u32 FUSE_OPT_VENDOR_CODE;
|
||||
u32 FUSE_OPT_FAB_CODE;
|
||||
u32 FUSE_OPT_LOT_CODE_0;
|
||||
u32 FUSE_OPT_LOT_CODE_1;
|
||||
u32 FUSE_OPT_WAFER_ID;
|
||||
u32 FUSE_OPT_X_COORDINATE;
|
||||
u32 FUSE_OPT_Y_COORDINATE;
|
||||
u32 FUSE_OPT_SEC_DEBUG_EN;
|
||||
u32 FUSE_OPT_OPS_RESERVED;
|
||||
u32 _0x224;
|
||||
u32 FUSE_GPU_IDDQ_CALIB;
|
||||
u32 FUSE_TSENSOR3_CALIB;
|
||||
u32 _0x234;
|
||||
u32 _0x238;
|
||||
u32 _0x23C;
|
||||
u32 _0x240;
|
||||
u32 _0x244;
|
||||
u32 FUSE_OPT_SAMPLE_TYPE;
|
||||
u32 FUSE_OPT_SUBREVISION;
|
||||
u32 FUSE_OPT_SW_RESERVED_0;
|
||||
u32 FUSE_OPT_SW_RESERVED_1;
|
||||
u32 FUSE_TSENSOR4_CALIB;
|
||||
u32 FUSE_TSENSOR5_CALIB;
|
||||
u32 FUSE_TSENSOR6_CALIB;
|
||||
u32 FUSE_TSENSOR7_CALIB;
|
||||
u32 FUSE_OPT_PRIV_SEC_EN;
|
||||
u32 _0x268;
|
||||
u32 _0x26C;
|
||||
u32 _0x270;
|
||||
u32 _0x274;
|
||||
u32 _0x278;
|
||||
u32 FUSE_FUSE2TSEC_DEBUG_DISABLE;
|
||||
u32 FUSE_TSENSOR_COMMON;
|
||||
u32 FUSE_OPT_CP_BIN;
|
||||
u32 FUSE_OPT_GPU_DISABLE;
|
||||
u32 FUSE_OPT_FT_BIN;
|
||||
u32 FUSE_OPT_DONE_MAP;
|
||||
u32 _0x294;
|
||||
u32 FUSE_APB2JTAG_DISABLE;
|
||||
u32 FUSE_ODM_INFO;
|
||||
u32 _0x2A0;
|
||||
u32 _0x2A4;
|
||||
u32 FUSE_ARM_CRYPT_DE_FEATURE;
|
||||
u32 _0x2AC;
|
||||
u32 _0x2B0;
|
||||
u32 _0x2B4;
|
||||
u32 _0x2B8;
|
||||
u32 _0x2BC;
|
||||
u32 FUSE_WOA_SKU_FLAG;
|
||||
u32 FUSE_ECO_RESERVE_1;
|
||||
u32 FUSE_GCPLEX_CONFIG_FUSE;
|
||||
u32 FUSE_PRODUCTION_MONTH;
|
||||
u32 FUSE_RAM_REPAIR_INDICATOR;
|
||||
u32 FUSE_TSENSOR9_CALIB;
|
||||
u32 _0x2D8;
|
||||
u32 FUSE_VMIN_CALIBRATION;
|
||||
u32 FUSE_AGING_SENSOR_CALIBRATION;
|
||||
u32 FUSE_DEBUG_AUTHENTICATION;
|
||||
u32 FUSE_SECURE_PROVISION_INDEX;
|
||||
u32 FUSE_SECURE_PROVISION_INFO;
|
||||
u32 FUSE_OPT_GPU_DISABLE_CP1;
|
||||
u32 FUSE_SPARE_ENDIS;
|
||||
u32 FUSE_ECO_RESERVE_0;
|
||||
u32 _0x2FC;
|
||||
u32 _0x300;
|
||||
u32 FUSE_RESERVED_CALIB0;
|
||||
u32 FUSE_RESERVED_CALIB1;
|
||||
u32 FUSE_OPT_GPU_TPC0_DISABLE;
|
||||
u32 FUSE_OPT_GPU_TPC0_DISABLE_CP1;
|
||||
u32 FUSE_OPT_CPU_DISABLE;
|
||||
u32 FUSE_OPT_CPU_DISABLE_CP1;
|
||||
u32 FUSE_TSENSOR10_CALIB;
|
||||
u32 FUSE_TSENSOR10_CALIB_AUX;
|
||||
u32 _0x324;
|
||||
u32 _0x328;
|
||||
u32 _0x32C;
|
||||
u32 _0x330;
|
||||
u32 _0x334;
|
||||
u32 FUSE_OPT_GPU_TPC0_DISABLE_CP2;
|
||||
u32 FUSE_OPT_GPU_TPC1_DISABLE;
|
||||
u32 FUSE_OPT_GPU_TPC1_DISABLE_CP1;
|
||||
u32 FUSE_OPT_GPU_TPC1_DISABLE_CP2;
|
||||
u32 FUSE_OPT_CPU_DISABLE_CP2;
|
||||
u32 FUSE_OPT_GPU_DISABLE_CP2;
|
||||
u32 FUSE_USB_CALIB_EXT;
|
||||
u32 FUSE_RESERVED_FIELD;
|
||||
u32 _0x358;
|
||||
u32 _0x35C;
|
||||
u32 _0x360;
|
||||
u32 _0x364;
|
||||
u32 _0x368;
|
||||
u32 _0x36C;
|
||||
u32 _0x370;
|
||||
u32 _0x374;
|
||||
u32 _0x378;
|
||||
u32 FUSE_SPARE_REALIGNMENT_REG;
|
||||
u32 FUSE_SPARE_BIT[0x20];
|
||||
};
|
||||
static_assert(util::is_pod<FuseChipRegistersCommon>::value);
|
||||
static_assert(sizeof(FuseChipRegistersCommon) == 0x400 - 0x98);
|
||||
|
||||
struct FuseChipRegistersErista {
|
||||
u32 _0x98[0x1A];
|
||||
u32 FUSE_PRODUCTION_MODE;
|
||||
u32 FUSE_JTAG_SECUREID_VALID;
|
||||
u32 FUSE_ODM_LOCK;
|
||||
|
@ -87,7 +237,7 @@ namespace ams::fuse {
|
|||
u32 FUSE_BOOT_DEVICE_INFO;
|
||||
u32 FUSE_RESERVED_SW;
|
||||
u32 FUSE_OPT_VP9_DISABLE;
|
||||
u32 FUSE_RESERVED_ODM[0x8];
|
||||
u32 FUSE_RESERVED_ODM_0[8 - 0];
|
||||
u32 FUSE_OBS_DIS;
|
||||
u32 FUSE_NOR_INFO;
|
||||
u32 FUSE_USB_CALIB;
|
||||
|
@ -121,34 +271,34 @@ namespace ams::fuse {
|
|||
u32 FUSE_TSENSOR7_CALIB;
|
||||
u32 FUSE_OPT_PRIV_SEC_EN;
|
||||
u32 FUSE_PKC_DISABLE;
|
||||
u32 _0x16C;
|
||||
u32 _0x170;
|
||||
u32 _0x174;
|
||||
u32 _0x178;
|
||||
u32 _0x26C;
|
||||
u32 _0x270;
|
||||
u32 _0x274;
|
||||
u32 _0x278;
|
||||
u32 FUSE_FUSE2TSEC_DEBUG_DISABLE;
|
||||
u32 FUSE_TSENSOR_COMMON;
|
||||
u32 FUSE_OPT_CP_BIN;
|
||||
u32 FUSE_OPT_GPU_DISABLE;
|
||||
u32 FUSE_OPT_FT_BIN;
|
||||
u32 FUSE_OPT_DONE_MAP;
|
||||
u32 _0x194;
|
||||
u32 _0x294;
|
||||
u32 FUSE_APB2JTAG_DISABLE;
|
||||
u32 FUSE_ODM_INFO;
|
||||
u32 _0x1A0;
|
||||
u32 _0x1A4;
|
||||
u32 _0x2A0;
|
||||
u32 _0x2A4;
|
||||
u32 FUSE_ARM_CRYPT_DE_FEATURE;
|
||||
u32 _0x1AC;
|
||||
u32 _0x1B0;
|
||||
u32 _0x1B4;
|
||||
u32 _0x1B8;
|
||||
u32 _0x1BC;
|
||||
u32 _0x2AC;
|
||||
u32 _0x2B0;
|
||||
u32 _0x2B4;
|
||||
u32 _0x2B8;
|
||||
u32 _0x2BC;
|
||||
u32 FUSE_WOA_SKU_FLAG;
|
||||
u32 FUSE_ECO_RESERVE_1;
|
||||
u32 FUSE_GCPLEX_CONFIG_FUSE;
|
||||
u32 FUSE_PRODUCTION_MONTH;
|
||||
u32 FUSE_RAM_REPAIR_INDICATOR;
|
||||
u32 FUSE_TSENSOR9_CALIB;
|
||||
u32 _0x1D8;
|
||||
u32 _0x2D8;
|
||||
u32 FUSE_VMIN_CALIBRATION;
|
||||
u32 FUSE_AGING_SENSOR_CALIBRATION;
|
||||
u32 FUSE_DEBUG_AUTHENTICATION;
|
||||
|
@ -157,8 +307,8 @@ namespace ams::fuse {
|
|||
u32 FUSE_OPT_GPU_DISABLE_CP1;
|
||||
u32 FUSE_SPARE_ENDIS;
|
||||
u32 FUSE_ECO_RESERVE_0;
|
||||
u32 _0x1FC;
|
||||
u32 _0x200;
|
||||
u32 _0x2FC;
|
||||
u32 _0x300;
|
||||
u32 FUSE_RESERVED_CALIB0;
|
||||
u32 FUSE_RESERVED_CALIB1;
|
||||
u32 FUSE_OPT_GPU_TPC0_DISABLE;
|
||||
|
@ -181,23 +331,175 @@ namespace ams::fuse {
|
|||
u32 FUSE_USB_CALIB_EXT;
|
||||
u32 FUSE_RESERVED_FIELD;
|
||||
u32 FUSE_OPT_ECC_EN;
|
||||
u32 _0x25C;
|
||||
u32 _0x260;
|
||||
u32 _0x264;
|
||||
u32 _0x268;
|
||||
u32 _0x35C;
|
||||
u32 _0x360;
|
||||
u32 _0x364;
|
||||
u32 _0x368;
|
||||
u32 _0x36C;
|
||||
u32 _0x370;
|
||||
u32 _0x374;
|
||||
u32 _0x378;
|
||||
u32 FUSE_SPARE_REALIGNMENT_REG;
|
||||
u32 FUSE_SPARE_BIT[0x20];
|
||||
};
|
||||
static_assert(util::is_pod<FuseChipRegistersErista>::value);
|
||||
static_assert(sizeof(FuseChipRegistersErista) == 0x400 - 0x98);
|
||||
|
||||
struct FuseChipRegistersMariko {
|
||||
u32 FUSE_RESERVED_ODM_8[22 - 8];
|
||||
u32 FUSE_KEK[4];
|
||||
u32 FUSE_BEK[4];
|
||||
u32 _0xF0[4];
|
||||
u32 FUSE_PRODUCTION_MODE;
|
||||
u32 FUSE_JTAG_SECUREID_VALID;
|
||||
u32 FUSE_ODM_LOCK;
|
||||
u32 FUSE_OPT_OPENGL_EN;
|
||||
u32 FUSE_SKU_INFO;
|
||||
u32 FUSE_CPU_SPEEDO_0_CALIB;
|
||||
u32 FUSE_CPU_IDDQ_CALIB;
|
||||
u32 FUSE_RESERVED_ODM_22[25 - 22];
|
||||
u32 FUSE_OPT_FT_REV;
|
||||
u32 FUSE_CPU_SPEEDO_1_CALIB;
|
||||
u32 FUSE_CPU_SPEEDO_2_CALIB;
|
||||
u32 FUSE_SOC_SPEEDO_0_CALIB;
|
||||
u32 FUSE_SOC_SPEEDO_1_CALIB;
|
||||
u32 FUSE_SOC_SPEEDO_2_CALIB;
|
||||
u32 FUSE_SOC_IDDQ_CALIB;
|
||||
u32 FUSE_RESERVED_ODM_25[26 - 25];
|
||||
u32 FUSE_FA;
|
||||
u32 FUSE_RESERVED_PRODUCTION;
|
||||
u32 FUSE_HDMI_LANE0_CALIB;
|
||||
u32 FUSE_HDMI_LANE1_CALIB;
|
||||
u32 FUSE_HDMI_LANE2_CALIB;
|
||||
u32 FUSE_HDMI_LANE3_CALIB;
|
||||
u32 FUSE_ENCRYPTION_RATE;
|
||||
u32 FUSE_PUBLIC_KEY[0x8];
|
||||
u32 FUSE_TSENSOR1_CALIB;
|
||||
u32 FUSE_TSENSOR2_CALIB;
|
||||
u32 FUSE_OPT_SECURE_SCC_DIS;
|
||||
u32 FUSE_OPT_CP_REV;
|
||||
u32 FUSE_OPT_PFG;
|
||||
u32 FUSE_TSENSOR0_CALIB;
|
||||
u32 FUSE_FIRST_BOOTROM_PATCH_SIZE;
|
||||
u32 FUSE_SECURITY_MODE;
|
||||
u32 FUSE_PRIVATE_KEY[0x5];
|
||||
u32 FUSE_ARM_JTAG_DIS;
|
||||
u32 FUSE_BOOT_DEVICE_INFO;
|
||||
u32 FUSE_RESERVED_SW;
|
||||
u32 FUSE_OPT_VP9_DISABLE;
|
||||
u32 FUSE_RESERVED_ODM_0[8 - 0];
|
||||
u32 FUSE_OBS_DIS;
|
||||
u32 _0x1EC;
|
||||
u32 FUSE_USB_CALIB;
|
||||
u32 FUSE_SKU_DIRECT_CONFIG;
|
||||
u32 FUSE_KFUSE_PRIVKEY_CTRL;
|
||||
u32 FUSE_PACKAGE_INFO;
|
||||
u32 FUSE_OPT_VENDOR_CODE;
|
||||
u32 FUSE_OPT_FAB_CODE;
|
||||
u32 FUSE_OPT_LOT_CODE_0;
|
||||
u32 FUSE_OPT_LOT_CODE_1;
|
||||
u32 FUSE_OPT_WAFER_ID;
|
||||
u32 FUSE_OPT_X_COORDINATE;
|
||||
u32 FUSE_OPT_Y_COORDINATE;
|
||||
u32 FUSE_OPT_SEC_DEBUG_EN;
|
||||
u32 FUSE_OPT_OPS_RESERVED;
|
||||
u32 _0x224;
|
||||
u32 FUSE_GPU_IDDQ_CALIB;
|
||||
u32 FUSE_TSENSOR3_CALIB;
|
||||
u32 FUSE_CLOCK_BONDOUT0;
|
||||
u32 FUSE_CLOCK_BONDOUT1;
|
||||
u32 FUSE_RESERVED_ODM_26[29 - 26];
|
||||
u32 FUSE_OPT_SAMPLE_TYPE;
|
||||
u32 FUSE_OPT_SUBREVISION;
|
||||
u32 FUSE_OPT_SW_RESERVED_0;
|
||||
u32 FUSE_OPT_SW_RESERVED_1;
|
||||
u32 FUSE_TSENSOR4_CALIB;
|
||||
u32 FUSE_TSENSOR5_CALIB;
|
||||
u32 FUSE_TSENSOR6_CALIB;
|
||||
u32 FUSE_TSENSOR7_CALIB;
|
||||
u32 FUSE_OPT_PRIV_SEC_EN;
|
||||
u32 FUSE_BOOT_SECURITY_INFO;
|
||||
u32 _0x26C;
|
||||
u32 _0x270;
|
||||
u32 _0x274;
|
||||
u32 _0x278;
|
||||
u32 FUSE_FUSE2TSEC_DEBUG_DISABLE;
|
||||
u32 FUSE_TSENSOR_COMMON;
|
||||
u32 FUSE_OPT_CP_BIN;
|
||||
u32 FUSE_OPT_GPU_DISABLE;
|
||||
u32 FUSE_OPT_FT_BIN;
|
||||
u32 FUSE_OPT_DONE_MAP;
|
||||
u32 FUSE_RESERVED_ODM_29[30 - 29];
|
||||
u32 FUSE_APB2JTAG_DISABLE;
|
||||
u32 FUSE_ODM_INFO;
|
||||
u32 _0x2A0;
|
||||
u32 _0x2A4;
|
||||
u32 FUSE_ARM_CRYPT_DE_FEATURE;
|
||||
u32 _0x2AC;
|
||||
u32 _0x2B0;
|
||||
u32 _0x2B4;
|
||||
u32 _0x2B8;
|
||||
u32 _0x2BC;
|
||||
u32 FUSE_WOA_SKU_FLAG;
|
||||
u32 FUSE_ECO_RESERVE_1;
|
||||
u32 FUSE_GCPLEX_CONFIG_FUSE;
|
||||
u32 FUSE_PRODUCTION_MONTH;
|
||||
u32 FUSE_RAM_REPAIR_INDICATOR;
|
||||
u32 FUSE_TSENSOR9_CALIB;
|
||||
u32 _0x2D8;
|
||||
u32 FUSE_VMIN_CALIBRATION;
|
||||
u32 FUSE_AGING_SENSOR_CALIBRATION;
|
||||
u32 FUSE_DEBUG_AUTHENTICATION;
|
||||
u32 FUSE_SECURE_PROVISION_INDEX;
|
||||
u32 FUSE_SECURE_PROVISION_INFO;
|
||||
u32 FUSE_OPT_GPU_DISABLE_CP1;
|
||||
u32 FUSE_SPARE_ENDIS;
|
||||
u32 FUSE_ECO_RESERVE_0;
|
||||
u32 _0x2FC;
|
||||
u32 _0x300;
|
||||
u32 FUSE_RESERVED_CALIB0;
|
||||
u32 FUSE_RESERVED_CALIB1;
|
||||
u32 FUSE_OPT_GPU_TPC0_DISABLE;
|
||||
u32 FUSE_OPT_GPU_TPC0_DISABLE_CP1;
|
||||
u32 FUSE_OPT_CPU_DISABLE;
|
||||
u32 FUSE_OPT_CPU_DISABLE_CP1;
|
||||
u32 FUSE_TSENSOR10_CALIB;
|
||||
u32 FUSE_TSENSOR10_CALIB_AUX;
|
||||
u32 _0x324;
|
||||
u32 _0x328;
|
||||
u32 _0x32C;
|
||||
u32 _0x330;
|
||||
u32 _0x334;
|
||||
u32 FUSE_OPT_GPU_TPC0_DISABLE_CP2;
|
||||
u32 FUSE_OPT_GPU_TPC1_DISABLE;
|
||||
u32 FUSE_OPT_GPU_TPC1_DISABLE_CP1;
|
||||
u32 FUSE_OPT_GPU_TPC1_DISABLE_CP2;
|
||||
u32 FUSE_OPT_CPU_DISABLE_CP2;
|
||||
u32 FUSE_OPT_GPU_DISABLE_CP2;
|
||||
u32 FUSE_USB_CALIB_EXT;
|
||||
u32 FUSE_RESERVED_FIELD;
|
||||
u32 _0x358;
|
||||
u32 _0x35C;
|
||||
u32 _0x360;
|
||||
u32 _0x364;
|
||||
u32 _0x368;
|
||||
u32 _0x36C;
|
||||
u32 _0x370;
|
||||
u32 _0x374;
|
||||
u32 _0x378;
|
||||
u32 FUSE_SPARE_REALIGNMENT_REG;
|
||||
u32 FUSE_SPARE_BIT[0x20];
|
||||
};
|
||||
static_assert(util::is_pod<FuseChipRegisters>::value);
|
||||
static_assert(sizeof(FuseChipRegisters) == 0x300);
|
||||
static_assert(util::is_pod<FuseChipRegistersMariko>::value);
|
||||
static_assert(sizeof(FuseChipRegistersMariko) == 0x400 - 0x98);
|
||||
|
||||
struct FuseRegisterRegion {
|
||||
FuseRegisters fuse;
|
||||
FuseChipRegisters chip;
|
||||
union {
|
||||
FuseChipRegistersCommon chip_common;
|
||||
FuseChipRegistersErista chip_erista;
|
||||
FuseChipRegistersMariko chip_mariko;
|
||||
};
|
||||
};
|
||||
static_assert(util::is_pod<FuseRegisterRegion>::value);
|
||||
static_assert(sizeof(FuseRegisterRegion) == secmon::MemoryRegionPhysicalDeviceFuses.GetSize());
|
||||
|
|
|
@ -48,12 +48,14 @@ namespace ams::log {
|
|||
clkrst::EnableUartAClock();
|
||||
} else if constexpr (UartLogPort == uart::Port_LeftJoyCon) {
|
||||
/* Logging to left joy-con (e.g. with Joyless). */
|
||||
pinmux::SetupUartB();
|
||||
clkrst::EnableUartBClock();
|
||||
} else if constexpr (UartLogPort == uart::Port_RightJoyCon) {
|
||||
/* Logging to right joy-con (e.g. with Joyless). */
|
||||
static_assert(uart::Port_LeftJoyCon == uart::Port_C);
|
||||
pinmux::SetupUartC();
|
||||
clkrst::EnableUartCClock();
|
||||
} else if constexpr (UartLogPort == uart::Port_RightJoyCon) {
|
||||
/* Logging to right joy-con (e.g. with Joyless). */
|
||||
static_assert(uart::Port_RightJoyCon == uart::Port_B);
|
||||
pinmux::SetupUartB();
|
||||
clkrst::EnableUartBClock();
|
||||
} else {
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
|
|
@ -104,7 +104,7 @@ namespace ams::pinmux {
|
|||
/* Get the registers. */
|
||||
const uintptr_t PINMUX = g_pinmux_address;
|
||||
|
||||
/* Configure Uart-B. */
|
||||
/* Configure Uart-C. */
|
||||
reg::Write(PINMUX + PINMUX_AUX_UART3_TX, PINMUX_REG_BITS_ENUM(AUX_UART3_PM, UARTC),
|
||||
PINMUX_REG_BITS_ENUM(AUX_PUPD, NONE),
|
||||
PINMUX_REG_BITS_ENUM(AUX_TRISTATE, PASSTHROUGH),
|
||||
|
@ -114,13 +114,13 @@ namespace ams::pinmux {
|
|||
|
||||
reg::Write(PINMUX + PINMUX_AUX_UART3_RX, PINMUX_REG_BITS_ENUM(AUX_UART3_PM, UARTC),
|
||||
PINMUX_REG_BITS_ENUM(AUX_PUPD, NONE),
|
||||
PINMUX_REG_BITS_ENUM(AUX_TRISTATE, PASSTHROUGH),
|
||||
PINMUX_REG_BITS_ENUM(AUX_TRISTATE, TRISTATE),
|
||||
PINMUX_REG_BITS_ENUM(AUX_E_INPUT, ENABLE),
|
||||
PINMUX_REG_BITS_ENUM(AUX_LOCK, DISABLE),
|
||||
PINMUX_REG_BITS_ENUM(AUX_E_OD, DISABLE));
|
||||
|
||||
reg::Write(PINMUX + PINMUX_AUX_UART3_RTS, PINMUX_REG_BITS_ENUM(AUX_UART3_PM, UARTC),
|
||||
PINMUX_REG_BITS_ENUM(AUX_PUPD, NONE),
|
||||
PINMUX_REG_BITS_ENUM(AUX_PUPD, PULL_DOWN),
|
||||
PINMUX_REG_BITS_ENUM(AUX_TRISTATE, PASSTHROUGH),
|
||||
PINMUX_REG_BITS_ENUM(AUX_E_INPUT, DISABLE),
|
||||
PINMUX_REG_BITS_ENUM(AUX_LOCK, DISABLE),
|
||||
|
@ -128,13 +128,16 @@ namespace ams::pinmux {
|
|||
|
||||
reg::Write(PINMUX + PINMUX_AUX_UART3_CTS, PINMUX_REG_BITS_ENUM(AUX_UART3_PM, UARTC),
|
||||
PINMUX_REG_BITS_ENUM(AUX_PUPD, NONE),
|
||||
PINMUX_REG_BITS_ENUM(AUX_TRISTATE, PASSTHROUGH),
|
||||
PINMUX_REG_BITS_ENUM(AUX_TRISTATE, TRISTATE),
|
||||
PINMUX_REG_BITS_ENUM(AUX_E_INPUT, ENABLE),
|
||||
PINMUX_REG_BITS_ENUM(AUX_LOCK, DISABLE),
|
||||
PINMUX_REG_BITS_ENUM(AUX_E_OD, DISABLE));
|
||||
|
||||
/* Configure GPIO for Uart-C. */
|
||||
reg::ReadWrite(g_gpio_address + 0x00C, REG_BITS_VALUE(1, 4, 0));
|
||||
reg::ReadWrite(g_gpio_address + 0x118, REG_BITS_VALUE(0, 1, 1));
|
||||
reg::Read(g_gpio_address + 0x118);
|
||||
reg::ReadWrite(g_gpio_address + 0x00C, REG_BITS_VALUE(1, 1, 0));
|
||||
reg::Read(g_gpio_address + 0x00C);
|
||||
}
|
||||
|
||||
void SetupI2c1() {
|
||||
|
|
|
@ -362,22 +362,29 @@ namespace ams::se {
|
|||
StartOperationRaw(SE, SE_OPERATION_OP_START, out_ll_address, in_ll_address);
|
||||
}
|
||||
|
||||
void ClearAesKeySlot(volatile SecurityEngineRegisters *SE, int slot) {
|
||||
/* Validate the key slot. */
|
||||
AMS_ABORT_UNLESS(0 <= slot && slot < AesKeySlotCount);
|
||||
|
||||
for (int i = 0; i < 16; ++i) {
|
||||
/* Select the keyslot. */
|
||||
reg::Write(SE->SE_CRYPTO_KEYTABLE_ADDR, SE_REG_BITS_VALUE(CRYPTO_KEYTABLE_ADDR_KEYIV_KEY_SLOT, slot), SE_REG_BITS_VALUE(CRYPTO_KEYTABLE_ADDR_KEYIV_WORD, i));
|
||||
|
||||
/* Write the data. */
|
||||
SE->SE_CRYPTO_KEYTABLE_DATA = 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void ClearAesKeySlot(int slot) {
|
||||
/* Validate the key slot. */
|
||||
AMS_ABORT_UNLESS(0 <= slot && slot < AesKeySlotCount);
|
||||
/* Clear the slot in SE1. */
|
||||
ClearAesKeySlot(GetRegisters(), slot);
|
||||
}
|
||||
|
||||
/* Get the engine. */
|
||||
auto *SE = GetRegisters();
|
||||
|
||||
for (int i = 0; i < 16; ++i) {
|
||||
/* Select the keyslot. */
|
||||
reg::Write(SE->SE_CRYPTO_KEYTABLE_ADDR, SE_REG_BITS_VALUE(CRYPTO_KEYTABLE_ADDR_KEYIV_KEY_SLOT, slot), SE_REG_BITS_VALUE(CRYPTO_KEYTABLE_ADDR_KEYIV_WORD, i));
|
||||
|
||||
/* Write the data. */
|
||||
SE->SE_CRYPTO_KEYTABLE_DATA = 0;
|
||||
}
|
||||
void ClearAesKeySlot2(int slot) {
|
||||
/* Clear the slot in SE2. */
|
||||
ClearAesKeySlot(GetRegisters2(), slot);
|
||||
}
|
||||
|
||||
void ClearAesKeyIv(int slot) {
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
namespace ams::se {
|
||||
|
||||
volatile SecurityEngineRegisters *GetRegisters();
|
||||
volatile SecurityEngineRegisters *GetRegisters2();
|
||||
|
||||
void ExecuteOperation(volatile SecurityEngineRegisters *SE, SE_OPERATION_OP op, void *dst, size_t dst_size, const void *src, size_t src_size);
|
||||
void ExecuteOperationSingleBlock(volatile SecurityEngineRegisters *SE, void *dst, size_t dst_size, const void *src, size_t src_size);
|
||||
|
|
|
@ -20,17 +20,35 @@ namespace ams::se {
|
|||
|
||||
namespace {
|
||||
|
||||
constinit uintptr_t g_register_address = secmon::MemoryRegionPhysicalDeviceSecurityEngine.GetAddress();
|
||||
constinit uintptr_t g_register_address = secmon::MemoryRegionPhysicalDeviceSecurityEngine.GetAddress();
|
||||
constinit uintptr_t g_register2_address = secmon::MemoryRegionPhysicalDeviceSecurityEngine2.GetAddress();
|
||||
constinit DoneHandler g_done_handler = nullptr;
|
||||
|
||||
void SetSecure(volatile SecurityEngineRegisters *SE, bool secure) {
|
||||
/* Set the security software setting. */
|
||||
if (secure) {
|
||||
reg::ReadWrite(SE->SE_SE_SECURITY, SE_REG_BITS_ENUM(SECURITY_SOFT_SETTING, SECURE));
|
||||
} else {
|
||||
reg::ReadWrite(SE->SE_SE_SECURITY, SE_REG_BITS_ENUM(SECURITY_SOFT_SETTING, NONSECURE));
|
||||
}
|
||||
|
||||
/* Read the status register to force an update. */
|
||||
reg::Read(SE->SE_SE_SECURITY);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
volatile SecurityEngineRegisters *GetRegisters() {
|
||||
return reinterpret_cast<volatile SecurityEngineRegisters *>(g_register_address);
|
||||
}
|
||||
|
||||
void SetRegisterAddress(uintptr_t address) {
|
||||
g_register_address = address;
|
||||
volatile SecurityEngineRegisters *GetRegisters2() {
|
||||
return reinterpret_cast<volatile SecurityEngineRegisters *>(g_register2_address);
|
||||
}
|
||||
|
||||
void SetRegisterAddress(uintptr_t address, uintptr_t address2) {
|
||||
g_register_address = address;
|
||||
g_register2_address = address2;
|
||||
}
|
||||
|
||||
void Initialize() {
|
||||
|
@ -39,17 +57,13 @@ namespace ams::se {
|
|||
}
|
||||
|
||||
void SetSecure(bool secure) {
|
||||
auto *SE = GetRegisters();
|
||||
/* Set security for SE1. */
|
||||
SetSecure(GetRegisters(), secure);
|
||||
|
||||
/* Set the security software setting. */
|
||||
if (secure) {
|
||||
reg::ReadWrite(SE->SE_SE_SECURITY, SE_REG_BITS_ENUM(SECURITY_SOFT_SETTING, SECURE));
|
||||
} else {
|
||||
reg::ReadWrite(SE->SE_SE_SECURITY, SE_REG_BITS_ENUM(SECURITY_SOFT_SETTING, NONSECURE));
|
||||
/* If SE2 is present, set security for SE2. */
|
||||
if (fuse::GetSocType() == fuse::SocType_Mariko) {
|
||||
SetSecure(GetRegisters2(), secure);
|
||||
}
|
||||
|
||||
/* Read the status register to force an update. */
|
||||
reg::Read(SE->SE_SE_SECURITY);
|
||||
}
|
||||
|
||||
void SetTzramSecure() {
|
||||
|
@ -66,6 +80,18 @@ namespace ams::se {
|
|||
reg::ReadWrite(SE->SE_SE_SECURITY, SE_REG_BITS_ENUM(SECURITY_PERKEY_SETTING, SECURE));
|
||||
}
|
||||
|
||||
|
||||
void SetContextSaveSecure() {
|
||||
/* Context save lock to trustzone secure is only available on mariko. */
|
||||
if (fuse::GetSocType() == fuse::SocType_Mariko) {
|
||||
auto *SE = GetRegisters();
|
||||
auto *SE2 = GetRegisters2();
|
||||
|
||||
reg::ReadWrite(SE->SE_SE_SECURITY, SE_REG_BITS_ENUM(SECURITY_CTX_SAVE_TZ_LOCK, SECURE));
|
||||
reg::ReadWrite(SE2->SE_SE_SECURITY, SE_REG_BITS_ENUM(SECURITY_CTX_SAVE_TZ_LOCK, SECURE));
|
||||
}
|
||||
}
|
||||
|
||||
void Lockout() {
|
||||
auto *SE = GetRegisters();
|
||||
|
||||
|
|
|
@ -32,7 +32,8 @@ namespace ams::se {
|
|||
u32 SE_OUT_CUR_LL_ID;
|
||||
u32 SE_HASH_RESULT[0x10];
|
||||
u32 SE_CTX_SAVE_CONFIG;
|
||||
u32 _0x74[0x63];
|
||||
u32 SE_CTX_SAVE_AUTO;
|
||||
u32 _0x78[0x62];
|
||||
u32 SE_SHA_CONFIG;
|
||||
u32 SE_SHA_MSG_LENGTH[0x4];
|
||||
u32 SE_SHA_MSG_LEFT[0x4];
|
||||
|
@ -61,7 +62,9 @@ namespace ams::se {
|
|||
u32 SE_RSA_KEYTABLE_ADDR;
|
||||
u32 SE_RSA_KEYTABLE_DATA;
|
||||
u32 SE_RSA_OUTPUT[0x40];
|
||||
u32 _0x528[0xB6];
|
||||
u32 _0x528[0x6];
|
||||
u32 SE_TZRAM_OPERATION;
|
||||
u32 _0x544[0xAF];
|
||||
u32 SE_STATUS;
|
||||
u32 SE_ERR_STATUS;
|
||||
u32 SE_MISC;
|
||||
|
@ -100,17 +103,26 @@ namespace ams::se {
|
|||
|
||||
/* SE_STATUS. */
|
||||
DEFINE_SE_REG_TWO_BIT_ENUM(STATUS_STATE, 0, IDLE, BUSY, WAIT_OUT, WAIT_IN);
|
||||
DEFINE_SE_REG_BIT_ENUM(STATUS_MEM_INTERFACE, 2, IDLE, BUSY);
|
||||
|
||||
/* SE_SECURITY */
|
||||
DEFINE_SE_REG_BIT_ENUM(SECURITY_HARD_SETTING, 0, SECURE, NONSECURE);
|
||||
DEFINE_SE_REG_BIT_ENUM(SECURITY_ENG_DIS, 1, DISABLE, ENABLE);
|
||||
DEFINE_SE_REG_BIT_ENUM(SECURITY_PERKEY_SETTING, 2, SECURE, NONSECURE);
|
||||
DEFINE_SE_REG_BIT_ENUM(SECURITY_SOFT_SETTING, 16, SECURE, NONSECURE);
|
||||
DEFINE_SE_REG_BIT_ENUM(SECURITY_HARD_SETTING, 0, SECURE, NONSECURE);
|
||||
DEFINE_SE_REG_BIT_ENUM(SECURITY_ENG_DIS, 1, DISABLE, ENABLE);
|
||||
DEFINE_SE_REG_BIT_ENUM(SECURITY_PERKEY_SETTING, 2, SECURE, NONSECURE);
|
||||
DEFINE_SE_REG_BIT_ENUM(SECURITY_CTX_SAVE_TZ_LOCK, 4, SECURE, NONSECURE);
|
||||
DEFINE_SE_REG_BIT_ENUM(SECURITY_CTX_TZ_LOCK_SOFT, 5, SECURE, NONSECURE);
|
||||
DEFINE_SE_REG_BIT_ENUM(SECURITY_SOFT_SETTING, 16, SECURE, NONSECURE);
|
||||
|
||||
/* SE_TZRAM_SECURITY */
|
||||
DEFINE_SE_REG(TZRAM_SETTING, 0, BITSIZEOF(u32));
|
||||
constexpr inline u32 SE_TZRAM_SETTING_SECURE = 0;
|
||||
|
||||
/* SE_TZRAM_OPERATION */
|
||||
DEFINE_SE_REG_BIT_ENUM(TZRAM_OPERATION_REQ, 0, IDLE, INITIATE);
|
||||
DEFINE_SE_REG_BIT_ENUM(TZRAM_OPERATION_MODE, 1, SAVE, RESTORE);
|
||||
DEFINE_SE_REG_BIT_ENUM(TZRAM_OPERATION_BUSY, 2, NO, YES);
|
||||
DEFINE_SE_REG(TZRAM_OPERATION_CURR_ADDR, 16, 16);
|
||||
|
||||
/* SE_OPERATION */
|
||||
DEFINE_SE_REG_THREE_BIT_ENUM(OPERATION_OP, 0, ABORT, START, RESTART_OUT, CTX_SAVE, RESTART_IN, RESERVED_5, RESERVED_6, RESERVED_7);
|
||||
|
||||
|
@ -168,6 +180,11 @@ namespace ams::se {
|
|||
DEFINE_SE_REG_BIT_ENUM(CTX_SAVE_CONFIG_STICKY_WORD_QUAD, 24, WORDS_0_3, WORDS_4_7);
|
||||
DEFINE_SE_REG_THREE_BIT_ENUM(CTX_SAVE_CONFIG_SRC, 29, STICKY_BITS, RSA_KEYTABLE, AES_KEYTABLE, PKA1_STICKY_BITS, MEM, RESERVED5, SRK, PKA1_KEYTABLE);
|
||||
|
||||
/* SE_CTX_SAVE_AUTO */
|
||||
DEFINE_SE_REG_BIT_ENUM(CTX_SAVE_AUTO_ENABLE, 0, NO, YES);
|
||||
DEFINE_SE_REG_BIT_ENUM(CTX_SAVE_AUTO_LOCK, 8, NO, YES);
|
||||
DEFINE_SE_REG(CTX_SAVE_AUTO_CURR_CNT, 16, 10);
|
||||
|
||||
/* SE_SHA_CONFIG */
|
||||
DEFINE_SE_REG(SHA_CONFIG_HW_INIT_HASH, 0, 1);
|
||||
|
||||
|
|
|
@ -44,31 +44,50 @@ namespace ams::se {
|
|||
reg::Write(SE->SE_RNG_CONFIG, SE_REG_BITS_ENUM(RNG_CONFIG_SRC, ENTROPY), SE_REG_BITS_VALUE(RNG_CONFIG_MODE, mode));
|
||||
}
|
||||
|
||||
}
|
||||
void InitializeRandom(volatile SecurityEngineRegisters *SE) {
|
||||
/* Lock the entropy source. */
|
||||
reg::Write(SE->SE_RNG_SRC_CONFIG, SE_REG_BITS_ENUM(RNG_SRC_CONFIG_RO_ENTROPY_SOURCE, ENABLE),
|
||||
SE_REG_BITS_ENUM(RNG_SRC_CONFIG_RO_ENTROPY_SOURCE_LOCK, ENABLE));
|
||||
|
||||
void InitializeRandom() {
|
||||
/* Get the engine. */
|
||||
auto *SE = GetRegisters();
|
||||
/* Set the reseed interval to force a reseed every 70000 blocks. */
|
||||
SE->SE_RNG_RESEED_INTERVAL = RngReseedInterval;
|
||||
|
||||
/* Lock the entropy source. */
|
||||
reg::Write(SE->SE_RNG_SRC_CONFIG, SE_REG_BITS_ENUM(RNG_SRC_CONFIG_RO_ENTROPY_SOURCE, ENABLE),
|
||||
SE_REG_BITS_ENUM(RNG_SRC_CONFIG_RO_ENTROPY_SOURCE_LOCK, ENABLE));
|
||||
/* Initialize the DRBG. */
|
||||
{
|
||||
u8 dummy_buf[AesBlockSize];
|
||||
|
||||
/* Set the reseed interval to force a reseed every 70000 blocks. */
|
||||
SE->SE_RNG_RESEED_INTERVAL = RngReseedInterval;
|
||||
/* Configure the engine to force drbg instantiation by writing random to memory. */
|
||||
ConfigRng(SE, SE_CONFIG_DST_MEMORY, SE_RNG_CONFIG_MODE_FORCE_INSTANTIATION);
|
||||
|
||||
/* Initialize the DRBG. */
|
||||
{
|
||||
u8 dummy_buf[AesBlockSize];
|
||||
/* Configure to do a single RNG block operation to trigger DRBG init. */
|
||||
SE->SE_CRYPTO_LAST_BLOCK = 0;
|
||||
|
||||
/* Configure the engine to force drbg instantiation by writing random to memory. */
|
||||
ConfigRng(SE, SE_CONFIG_DST_MEMORY, SE_RNG_CONFIG_MODE_FORCE_INSTANTIATION);
|
||||
/* Execute the operation. */
|
||||
ExecuteOperation(SE, SE_OPERATION_OP_START, dummy_buf, sizeof(dummy_buf), nullptr, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/* Configure to do a single RNG block operation to trigger DRBG init. */
|
||||
void GenerateSrk(volatile SecurityEngineRegisters *SE) {
|
||||
/* Configure the RNG to output to SRK and force a reseed. */
|
||||
ConfigRng(SE, SE_CONFIG_DST_SRK, SE_RNG_CONFIG_MODE_FORCE_RESEED);
|
||||
|
||||
/* Configure a single block operation. */
|
||||
SE->SE_CRYPTO_LAST_BLOCK = 0;
|
||||
|
||||
/* Execute the operation. */
|
||||
ExecuteOperation(SE, SE_OPERATION_OP_START, dummy_buf, sizeof(dummy_buf), nullptr, 0);
|
||||
ExecuteOperation(SE, SE_OPERATION_OP_START, nullptr, 0, nullptr, 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void InitializeRandom() {
|
||||
/* Initialize random for SE1. */
|
||||
InitializeRandom(GetRegisters());
|
||||
|
||||
/* If we have SE2, initialize random for SE2. */
|
||||
/* NOTE: Nintendo's implementation of this is incorrect. */
|
||||
if (fuse::GetSocType() == fuse::SocType_Mariko) {
|
||||
InitializeRandom(GetRegisters2());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -130,17 +149,14 @@ namespace ams::se {
|
|||
}
|
||||
|
||||
void GenerateSrk() {
|
||||
/* Get the engine. */
|
||||
auto *SE = GetRegisters();
|
||||
/* Generate SRK for SE1. */
|
||||
GenerateSrk(GetRegisters());
|
||||
|
||||
/* Configure the RNG to output to SRK and force a reseed. */
|
||||
ConfigRng(SE, SE_CONFIG_DST_SRK, SE_RNG_CONFIG_MODE_FORCE_RESEED);
|
||||
|
||||
/* Configure a single block operation. */
|
||||
SE->SE_CRYPTO_LAST_BLOCK = 0;
|
||||
|
||||
/* Execute the operation. */
|
||||
ExecuteOperation(SE, SE_OPERATION_OP_START, nullptr, 0, nullptr, 0);
|
||||
/* If we have SE2, generate SRK for SE2. */
|
||||
/* NOTE: Nintendo's implementation of this is incorrect. */
|
||||
if (fuse::GetSocType() == fuse::SocType_Mariko) {
|
||||
GenerateSrk(GetRegisters2());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -20,6 +20,10 @@ namespace ams::se {
|
|||
|
||||
namespace {
|
||||
|
||||
constexpr inline size_t SE1ContextSaveOperationCount = 133;
|
||||
constexpr inline size_t SE2ContextSaveOperationCount = 646;
|
||||
static_assert(((SE1ContextSaveOperationCount - 2) + 1) * se::AesBlockSize == sizeof(se::Context));
|
||||
|
||||
constinit const u8 FixedPattern[AesBlockSize] = {
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
|
||||
};
|
||||
|
@ -64,6 +68,44 @@ namespace ams::se {
|
|||
ExecuteContextSaveOperation(SE, dst, AesBlockSize, nullptr, 0);
|
||||
}
|
||||
|
||||
void ConfigureForAutomaticContextSave(volatile SecurityEngineRegisters *SE) {
|
||||
/* Configure the engine to do RNG encryption. */
|
||||
reg::Write(SE->SE_CONFIG, SE_REG_BITS_ENUM(CONFIG_ENC_MODE, AESMODE_KEY128),
|
||||
SE_REG_BITS_ENUM(CONFIG_DEC_MODE, AESMODE_KEY128),
|
||||
SE_REG_BITS_ENUM(CONFIG_ENC_ALG, RNG),
|
||||
SE_REG_BITS_ENUM(CONFIG_DEC_ALG, NOP),
|
||||
SE_REG_BITS_ENUM(CONFIG_DST, MEMORY));
|
||||
|
||||
reg::Write(SE->SE_CRYPTO_CONFIG, SE_REG_BITS_ENUM (CRYPTO_CONFIG_MEMIF, AHB),
|
||||
SE_REG_BITS_VALUE(CRYPTO_CONFIG_CTR_CNTN, 0),
|
||||
SE_REG_BITS_ENUM (CRYPTO_CONFIG_KEYSCH_BYPASS, DISABLE),
|
||||
SE_REG_BITS_ENUM (CRYPTO_CONFIG_CORE_SEL, ENCRYPT),
|
||||
SE_REG_BITS_ENUM (CRYPTO_CONFIG_IV_SELECT, ORIGINAL),
|
||||
SE_REG_BITS_ENUM (CRYPTO_CONFIG_VCTRAM_SEL, MEMORY),
|
||||
SE_REG_BITS_ENUM (CRYPTO_CONFIG_INPUT_SEL, RANDOM),
|
||||
SE_REG_BITS_ENUM (CRYPTO_CONFIG_XOR_POS, BYPASS),
|
||||
SE_REG_BITS_ENUM (CRYPTO_CONFIG_HASH_ENB, DISABLE));
|
||||
}
|
||||
|
||||
void WaitAutomaticContextSaveDone(volatile SecurityEngineRegisters *SE) {
|
||||
/* Wait for operation. */
|
||||
while (!reg::HasValue(SE->SE_INT_STATUS, SE_REG_BITS_ENUM(INT_STATUS_SE_OP_DONE, ACTIVE))) { /* ... */ }
|
||||
|
||||
/* Wait for the engine to be idle. */
|
||||
while (!reg::HasValue(SE->SE_STATUS, SE_REG_BITS_ENUM(STATUS_STATE, IDLE))) { /* ... */ }
|
||||
|
||||
/* Wait for the memory interface to be idle. */
|
||||
while (!reg::HasValue(SE->SE_STATUS, SE_REG_BITS_ENUM(STATUS_MEM_INTERFACE, IDLE))) { /* ... */ }
|
||||
}
|
||||
|
||||
void ValidateErrStatus(volatile SecurityEngineRegisters *SE) {
|
||||
/* Ensure there is no error status. */
|
||||
AMS_ABORT_UNLESS(reg::Read(SE->SE_ERR_STATUS) == 0);
|
||||
|
||||
/* Ensure no error occurred. */
|
||||
AMS_ABORT_UNLESS(reg::HasValue(SE->SE_INT_STATUS, SE_REG_BITS_ENUM(INT_STATUS_ERR_STAT, CLEAR)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool ValidateStickyBits(const StickyBits &bits) {
|
||||
|
@ -237,15 +279,76 @@ namespace ams::se {
|
|||
}
|
||||
}
|
||||
|
||||
void ConfigureAutomaticContextSave() {
|
||||
/* Get registers. */
|
||||
auto *SE = GetRegisters();
|
||||
auto *SE2 = GetRegisters2();
|
||||
|
||||
/* Automatic context save is supported only on mariko. */
|
||||
if (fuse::GetSocType() == fuse::SocType_Mariko) {
|
||||
/* Configure SE1 to do automatic context save. */
|
||||
reg::Write(SE->SE_CTX_SAVE_AUTO, SE_REG_BITS_ENUM(CTX_SAVE_AUTO_ENABLE, YES),
|
||||
SE_REG_BITS_ENUM(CTX_SAVE_AUTO_LOCK, YES));
|
||||
|
||||
/* Configure SE2 to do automatic context save. */
|
||||
reg::Write(SE2->SE_CTX_SAVE_AUTO, SE_REG_BITS_ENUM(CTX_SAVE_AUTO_ENABLE, YES),
|
||||
SE_REG_BITS_ENUM(CTX_SAVE_AUTO_LOCK, YES));
|
||||
}
|
||||
}
|
||||
|
||||
void SaveContextAutomatic() {
|
||||
/* Get registers. */
|
||||
auto *SE = GetRegisters();
|
||||
auto *SE2 = GetRegisters2();
|
||||
|
||||
/* Ensure there's no error status before or after we save context. */
|
||||
ValidateErrStatus();
|
||||
ON_SCOPE_EXIT { ValidateErrStatus(); };
|
||||
|
||||
/* Perform atomic context save. */
|
||||
{
|
||||
/* Check that context save has not already been performed. */
|
||||
AMS_ABORT_UNLESS(reg::HasValue(SE->SE_CTX_SAVE_AUTO, SE_REG_BITS_VALUE(CTX_SAVE_AUTO_CURR_CNT, 0)));
|
||||
AMS_ABORT_UNLESS(reg::HasValue(SE2->SE_CTX_SAVE_AUTO, SE_REG_BITS_VALUE(CTX_SAVE_AUTO_CURR_CNT, 0)));
|
||||
|
||||
/* Configure SE1 to do context save. */
|
||||
ConfigureForAutomaticContextSave(SE);
|
||||
ConfigureForAutomaticContextSave(SE2);
|
||||
|
||||
/* Start the context save operation. */
|
||||
reg::Write(SE->SE_OPERATION, SE_REG_BITS_ENUM(OPERATION_OP, CTX_SAVE));
|
||||
reg::Write(SE2->SE_OPERATION, SE_REG_BITS_ENUM(OPERATION_OP, CTX_SAVE));
|
||||
|
||||
/* Wait for the context save operation to complete. */
|
||||
WaitAutomaticContextSaveDone(SE);
|
||||
WaitAutomaticContextSaveDone(SE2);
|
||||
|
||||
/* Check that the correct sizes were written. */
|
||||
AMS_ABORT_UNLESS(reg::HasValue(SE->SE_CTX_SAVE_AUTO, SE_REG_BITS_VALUE(CTX_SAVE_AUTO_CURR_CNT, SE1ContextSaveOperationCount)));
|
||||
AMS_ABORT_UNLESS(reg::HasValue(SE2->SE_CTX_SAVE_AUTO, SE_REG_BITS_VALUE(CTX_SAVE_AUTO_CURR_CNT, SE2ContextSaveOperationCount)));
|
||||
}
|
||||
}
|
||||
|
||||
void SaveTzramAutomatic() {
|
||||
/* Get registers. */
|
||||
auto *SE = GetRegisters();
|
||||
|
||||
/* Begin save-to-shadow-tzram operation. */
|
||||
reg::Write(SE->SE_TZRAM_OPERATION, SE_REG_BITS_ENUM(TZRAM_OPERATION_MODE, SAVE),
|
||||
SE_REG_BITS_ENUM(TZRAM_OPERATION_REQ, INITIATE));
|
||||
|
||||
/* Wait for operation to complete. */
|
||||
while (reg::HasValue(SE->SE_TZRAM_OPERATION, SE_REG_BITS_ENUM(TZRAM_OPERATION_BUSY, YES))) { /* ... */ }
|
||||
}
|
||||
|
||||
void ValidateErrStatus() {
|
||||
/* Get the registers. */
|
||||
auto *SE = GetRegisters();
|
||||
/* Ensure SE has no error status. */
|
||||
ValidateErrStatus(GetRegisters());
|
||||
|
||||
/* Ensure there is no error status. */
|
||||
AMS_ABORT_UNLESS(reg::Read(SE->SE_ERR_STATUS) == 0);
|
||||
|
||||
/* Ensure no error occurred. */
|
||||
AMS_ABORT_UNLESS(reg::HasValue(SE->SE_INT_STATUS, SE_REG_BITS_ENUM(INT_STATUS_ERR_STAT, CLEAR)));
|
||||
/* If on mariko, ensure SE2 has no error status. */
|
||||
if (fuse::GetSocType() == fuse::SocType_Mariko) {
|
||||
ValidateErrStatus(GetRegisters2());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -33,11 +33,11 @@ namespace ams::uart {
|
|||
}
|
||||
|
||||
void WaitSymbols(int baud, u32 num) {
|
||||
util::WaitMicroSeconds(util::DivideUp(1'000'000, baud) * num);
|
||||
util::WaitMicroSeconds(util::DivideUp(num * 1'000'000, baud));
|
||||
}
|
||||
|
||||
void WaitCycles(int baud, u32 num) {
|
||||
util::WaitMicroSeconds(util::DivideUp(1'000'000, 16 * baud) * num);
|
||||
util::WaitMicroSeconds(util::DivideUp(num * 1'000'000, 16 * baud));
|
||||
}
|
||||
|
||||
ALWAYS_INLINE void WaitFifoNotFull(volatile UartRegisters *uart) {
|
||||
|
@ -60,26 +60,6 @@ namespace ams::uart {
|
|||
|
||||
constexpr inline u32 LockBit = (1 << 6);
|
||||
|
||||
void Lock(volatile UartRegisters *reg) {
|
||||
while (true) {
|
||||
if (reg->mie != 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
reg->irda_csr = LockBit;
|
||||
|
||||
if (reg->mie == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
reg->irda_csr = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void Unlock(volatile UartRegisters *reg) {
|
||||
reg->irda_csr = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void SetRegisterAddress(uintptr_t address) {
|
||||
|
@ -97,7 +77,13 @@ namespace ams::uart {
|
|||
constexpr u32 UartClock = 408000000;
|
||||
const u32 divisor = (UartClock + (baud_rate * 16) / 2) / (baud_rate * 16);
|
||||
|
||||
/* Disable DLAB and all interrupts. */
|
||||
/* Wait for idle state. */
|
||||
WaitIdle(uart, UART_VENDOR_STATE_TX_IDLE);
|
||||
|
||||
/* Wait 100 us. */
|
||||
util::WaitMicroSeconds(100);
|
||||
|
||||
/* Disable interrupts. */
|
||||
uart->lcr = uart->lcr & ~UART_LCR_DLAB;
|
||||
uart->ier = 0;
|
||||
uart->mcr = 0;
|
||||
|
@ -128,8 +114,8 @@ namespace ams::uart {
|
|||
/* Wait for idle state. */
|
||||
WaitIdle(uart, UART_VENDOR_STATE_TX_IDLE | UART_VENDOR_STATE_RX_IDLE);
|
||||
|
||||
/* Set scratch register to 0. */
|
||||
uart->spr = 0;
|
||||
/* Wait 100 us. */
|
||||
util::WaitMicroSeconds(100);
|
||||
}
|
||||
|
||||
void SendText(Port port, const void *data, size_t size) {
|
||||
|
@ -139,10 +125,6 @@ namespace ams::uart {
|
|||
/* Get pointer to data. */
|
||||
const u8 *p = static_cast<const u8 *>(data);
|
||||
|
||||
/* Lock the uart registers. */
|
||||
Lock(uart);
|
||||
ON_SCOPE_EXIT { Unlock(uart); };
|
||||
|
||||
/* Send each byte. */
|
||||
for (size_t i = 0; i < size; ++i) {
|
||||
WaitFifoNotFull(uart);
|
||||
|
|
|
@ -23,6 +23,17 @@
|
|||
|
||||
namespace ams::kern::arch::arm64::init {
|
||||
|
||||
inline void ClearPhysicalMemory(KPhysicalAddress address, size_t size) {
|
||||
MESOSPHERE_INIT_ABORT_UNLESS(util::IsAligned(size, sizeof(u64)));
|
||||
|
||||
/* This Physical Address -> void * conversion is valid, because this is init page table code. */
|
||||
/* The MMU is necessarily not yet turned on, if we are creating an initial page table. */
|
||||
volatile u64 *ptr = reinterpret_cast<volatile u64 *>(GetInteger(address));
|
||||
for (size_t i = 0; i < size / sizeof(u64); ++i) {
|
||||
ptr[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
class KInitialPageTable {
|
||||
public:
|
||||
class IPageAllocator {
|
||||
|
@ -61,9 +72,7 @@ namespace ams::kern::arch::arm64::init {
|
|||
}
|
||||
|
||||
static ALWAYS_INLINE void ClearNewPageTable(KPhysicalAddress address) {
|
||||
/* This Physical Address -> void * conversion is valid, because this is page table code. */
|
||||
/* The MMU is necessarily not yet turned on, if we are creating an initial page table. */
|
||||
std::memset(reinterpret_cast<void *>(GetInteger(address)), 0, PageSize);
|
||||
ClearPhysicalMemory(address, PageSize);
|
||||
}
|
||||
private:
|
||||
size_t NOINLINE GetBlockCount(KVirtualAddress virt_addr, size_t size, size_t block_size) {
|
||||
|
@ -705,7 +714,7 @@ namespace ams::kern::arch::arm64::init {
|
|||
this->state.next_address += PageSize;
|
||||
}
|
||||
|
||||
std::memset(reinterpret_cast<void *>(allocated), 0, PageSize);
|
||||
ClearPhysicalMemory(allocated, PageSize);
|
||||
return allocated;
|
||||
}
|
||||
|
||||
|
|
|
@ -64,6 +64,10 @@ namespace ams::kern::arch::arm64::cpu {
|
|||
EnsureInstructionConsistency();
|
||||
}
|
||||
|
||||
ALWAYS_INLINE void Yield() {
|
||||
__asm__ __volatile__("yield" ::: "memory");
|
||||
}
|
||||
|
||||
ALWAYS_INLINE void SwitchProcess(u64 ttbr, u32 proc_id) {
|
||||
SetTtbr0El1(ttbr);
|
||||
ContextIdRegisterAccessor(0).SetProcId(proc_id).Store();
|
||||
|
@ -149,6 +153,25 @@ namespace ams::kern::arch::arm64::cpu {
|
|||
return true;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE bool CanAccessAtomic(KProcessAddress addr, bool privileged = false) {
|
||||
const uintptr_t va = GetInteger(addr);
|
||||
|
||||
if (privileged) {
|
||||
__asm__ __volatile__("at s1e1w, %[va]" :: [va]"r"(va) : "memory");
|
||||
} else {
|
||||
__asm__ __volatile__("at s1e0w, %[va]" :: [va]"r"(va) : "memory");
|
||||
}
|
||||
InstructionMemoryBarrier();
|
||||
|
||||
u64 par = GetParEl1();
|
||||
|
||||
if (par & 0x1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (par >> (BITSIZEOF(par) - BITSIZEOF(u8))) == 0xFF;
|
||||
}
|
||||
|
||||
/* Synchronization helpers. */
|
||||
NOINLINE void SynchronizeAllCores();
|
||||
|
||||
|
@ -173,7 +196,7 @@ namespace ams::kern::arch::arm64::cpu {
|
|||
|
||||
ALWAYS_INLINE void InvalidateTlbByAsid(u32 asid) {
|
||||
const u64 value = (static_cast<u64>(asid) << 48);
|
||||
__asm__ __volatile__("tlbi aside1is, %[value]" :: [value]"r"(static_cast<u64>(value) << 48) : "memory");
|
||||
__asm__ __volatile__("tlbi aside1is, %[value]" :: [value]"r"(value) : "memory");
|
||||
EnsureInstructionConsistency();
|
||||
}
|
||||
|
||||
|
|
|
@ -56,26 +56,69 @@ namespace ams::kern::arch::arm64::cpu {
|
|||
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(OslarEl1, oslar_el1)
|
||||
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(TpidrEl0, tpidr_el0)
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(TpidrRoEl0, tpidrro_el0)
|
||||
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(EsrEl1, esr_el1)
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(ElrEl1, elr_el1)
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(EsrEl1, esr_el1)
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(SpsrEl1, spsr_el1)
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(Afsr0El1, afsr0_el1)
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(Afsr1El1, afsr1_el1)
|
||||
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(PmUserEnrEl0, pmuserenr_el0)
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(PmcCntrEl0, pmccntr_el0)
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(PmevCntr0El0, pmevcntr0_el0)
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(PmevCntr1El0, pmevcntr1_el0)
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(PmevCntr2El0, pmevcntr2_el0)
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(PmevCntr3El0, pmevcntr3_el0)
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(PmevCntr4El0, pmevcntr4_el0)
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(PmevCntr5El0, pmevcntr5_el0)
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(MdscrEl1, mdscr_el1)
|
||||
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(CpacrEl1, cpacr_el1)
|
||||
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(ContextidrEl1, contextidr_el1)
|
||||
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(CntkCtlEl1, cntkctl_el1)
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(CntpCtlEl0, cntp_ctl_el0)
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(CntpCvalEl0, cntp_cval_el0)
|
||||
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(Daif, daif)
|
||||
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(SpEl0, sp_el0)
|
||||
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(IdAa64Dfr0El1, id_aa64dfr0_el1)
|
||||
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(PmcrEl0, pmcr_el0)
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(PmUserEnrEl0, pmuserenr_el0)
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(PmcCntrEl0, pmccntr_el0)
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(PmSelrEl0, pmselr_el0)
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(PmcCfiltrEl0, pmccfiltr_el0)
|
||||
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(PmIntEnSetEl1, pmintenset_el1)
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(PmCntEnSetEl0, pmcntenset_el0)
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(PmOvsSetEl0, pmovsset_el0)
|
||||
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(PmIntEnClrEl1, pmintenclr_el1)
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(PmCntEnClrEl0, pmcntenclr_el0)
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(PmOvsClrEl0, pmovsclr_el0)
|
||||
|
||||
#define FOR_I_IN_0_TO_30(HANDLER, ...) \
|
||||
HANDLER(0, ## __VA_ARGS__) HANDLER(1, ## __VA_ARGS__) HANDLER(2, ## __VA_ARGS__) HANDLER(3, ## __VA_ARGS__) \
|
||||
HANDLER(4, ## __VA_ARGS__) HANDLER(5, ## __VA_ARGS__) HANDLER(6, ## __VA_ARGS__) HANDLER(7, ## __VA_ARGS__) \
|
||||
HANDLER(8, ## __VA_ARGS__) HANDLER(9, ## __VA_ARGS__) HANDLER(10, ## __VA_ARGS__) HANDLER(11, ## __VA_ARGS__) \
|
||||
HANDLER(12, ## __VA_ARGS__) HANDLER(13, ## __VA_ARGS__) HANDLER(14, ## __VA_ARGS__) HANDLER(15, ## __VA_ARGS__) \
|
||||
HANDLER(16, ## __VA_ARGS__) HANDLER(17, ## __VA_ARGS__) HANDLER(18, ## __VA_ARGS__) HANDLER(19, ## __VA_ARGS__) \
|
||||
HANDLER(20, ## __VA_ARGS__) HANDLER(21, ## __VA_ARGS__) HANDLER(22, ## __VA_ARGS__) HANDLER(23, ## __VA_ARGS__) \
|
||||
HANDLER(24, ## __VA_ARGS__) HANDLER(25, ## __VA_ARGS__) HANDLER(26, ## __VA_ARGS__) HANDLER(27, ## __VA_ARGS__) \
|
||||
HANDLER(28, ## __VA_ARGS__) HANDLER(29, ## __VA_ARGS__) HANDLER(30, ## __VA_ARGS__)
|
||||
|
||||
#define MESOSPHERE_CPU_DEFINE_PMEV_ACCESSORS(ID, ...) \
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(PmevCntr##ID##El0, pmevcntr##ID##_el0) \
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(PmevTyper##ID##El0, pmevtyper##ID##_el0)
|
||||
|
||||
FOR_I_IN_0_TO_30(MESOSPHERE_CPU_DEFINE_PMEV_ACCESSORS)
|
||||
|
||||
#undef MESOSPHERE_CPU_DEFINE_PMEV_ACCESSORS
|
||||
#undef FOR_I_IN_0_TO_30
|
||||
|
||||
#define FOR_I_IN_0_TO_15(HANDLER, ...) \
|
||||
HANDLER(0, ## __VA_ARGS__) HANDLER(1, ## __VA_ARGS__) HANDLER(2, ## __VA_ARGS__) HANDLER(3, ## __VA_ARGS__) \
|
||||
HANDLER(4, ## __VA_ARGS__) HANDLER(5, ## __VA_ARGS__) HANDLER(6, ## __VA_ARGS__) HANDLER(7, ## __VA_ARGS__) \
|
||||
HANDLER(8, ## __VA_ARGS__) HANDLER(9, ## __VA_ARGS__) HANDLER(10, ## __VA_ARGS__) HANDLER(11, ## __VA_ARGS__) \
|
||||
HANDLER(12, ## __VA_ARGS__) HANDLER(13, ## __VA_ARGS__) HANDLER(14, ## __VA_ARGS__) HANDLER(15, ## __VA_ARGS__) \
|
||||
HANDLER(12, ## __VA_ARGS__) HANDLER(13, ## __VA_ARGS__) HANDLER(14, ## __VA_ARGS__) HANDLER(15, ## __VA_ARGS__)
|
||||
|
||||
#define MESOSPHERE_CPU_DEFINE_DBG_SYSREG_ACCESSORS(ID, ...) \
|
||||
MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(DbgWcr##ID##El1, dbgwcr##ID##_el1) \
|
||||
|
@ -158,6 +201,15 @@ namespace ams::kern::arch::arm64::cpu {
|
|||
const size_t shift_value = this->GetBits(16, 6);
|
||||
return size_t(1) << (size_t(64) - shift_value);
|
||||
}
|
||||
|
||||
constexpr ALWAYS_INLINE bool GetEpd0() const {
|
||||
return this->GetBits(7, 1) != 0;
|
||||
}
|
||||
|
||||
constexpr ALWAYS_INLINE decltype(auto) SetEpd0(bool set) {
|
||||
this->SetBit(7, set);
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
MESOSPHERE_CPU_SYSREG_ACCESSOR_CLASS(ArchitecturalFeatureAccessControl) {
|
||||
|
@ -189,6 +241,10 @@ namespace ams::kern::arch::arm64::cpu {
|
|||
constexpr ALWAYS_INLINE size_t GetNumBreakpoints() const {
|
||||
return this->GetBits(12, 4);
|
||||
}
|
||||
|
||||
constexpr ALWAYS_INLINE size_t GetNumContextAwareBreakpoints() const {
|
||||
return this->GetBits(28, 4);
|
||||
}
|
||||
};
|
||||
|
||||
MESOSPHERE_CPU_SYSREG_ACCESSOR_CLASS(MonitorDebugSystemControl) {
|
||||
|
@ -387,6 +443,27 @@ namespace ams::kern::arch::arm64::cpu {
|
|||
/* TODO: Other bitfield accessors? */
|
||||
};
|
||||
|
||||
MESOSPHERE_CPU_SYSREG_ACCESSOR_CLASS(PerformanceMonitorsControl) {
|
||||
public:
|
||||
MESOSPHERE_CPU_SYSREG_ACCESSOR_CLASS_FUNCTIONS(PerformanceMonitorsControl, pmcr_el0)
|
||||
public:
|
||||
constexpr ALWAYS_INLINE u64 GetN() const {
|
||||
return this->GetBits(11, 5);
|
||||
}
|
||||
|
||||
constexpr ALWAYS_INLINE decltype(auto) SetEventCounterReset(bool en) {
|
||||
this->SetBit(1, en);
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr ALWAYS_INLINE decltype(auto) SetCycleCounterReset(bool en) {
|
||||
this->SetBit(2, en);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/* TODO: Other bitfield accessors? */
|
||||
};
|
||||
|
||||
#undef FOR_I_IN_0_TO_15
|
||||
#undef MESOSPHERE_CPU_SYSREG_ACCESSOR_CLASS_FUNCTIONS
|
||||
#undef MESOSPHERE_CPU_SYSREG_ACCESSOR_CLASS
|
||||
|
|
|
@ -30,6 +30,38 @@ namespace ams::kern::arch::arm64 {
|
|||
class KDebug final : public KAutoObjectWithSlabHeapAndContainer<KDebug, KDebugBase> {
|
||||
MESOSPHERE_AUTOOBJECT_TRAITS(KDebug, KSynchronizationObject);
|
||||
public:
|
||||
explicit KDebug() { /* ... */ }
|
||||
virtual ~KDebug() { /* ... */ }
|
||||
|
||||
static void PostDestroy(uintptr_t arg) { /* ... */ }
|
||||
public:
|
||||
virtual Result GetThreadContextImpl(ams::svc::ThreadContext *out, KThread *thread, u32 context_flags) override;
|
||||
virtual Result SetThreadContextImpl(const ams::svc::ThreadContext &ctx, KThread *thread, u32 context_flags) override;
|
||||
private:
|
||||
Result GetFpuContext(ams::svc::ThreadContext *out, KThread *thread, u32 context_flags);
|
||||
Result SetFpuContext(const ams::svc::ThreadContext &ctx, KThread *thread, u32 context_flags);
|
||||
public:
|
||||
static uintptr_t GetProgramCounter(const KThread &thread);
|
||||
static void SetPreviousProgramCounter();
|
||||
|
||||
static Result BreakIfAttached(ams::svc::BreakReason break_reason, uintptr_t address, size_t size);
|
||||
static Result SetHardwareBreakPoint(ams::svc::HardwareBreakPointRegisterName name, u64 flags, u64 value);
|
||||
|
||||
static constexpr bool IsBreakInstruction(u32 insn, u32 psr) {
|
||||
constexpr u32 BreakInstructionAarch64 = 0xE7FFFFFF;
|
||||
constexpr u32 BreakInstructionAarch32 = 0xE7FFDEFE;
|
||||
constexpr u32 BreakInstructionThumb32 = 0xB68E;
|
||||
if ((psr & 0x10) == 0) {
|
||||
return insn == BreakInstructionAarch64;
|
||||
} else {
|
||||
if ((psr & 0x20) == 0) {
|
||||
return insn == BreakInstructionAarch32;
|
||||
} else {
|
||||
return insn == BreakInstructionThumb32;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* TODO: This is a placeholder definition. */
|
||||
};
|
||||
|
||||
|
|
|
@ -22,9 +22,26 @@ namespace ams::kern::arch::arm64 {
|
|||
u64 x[(30 - 0) + 1];
|
||||
u64 sp;
|
||||
u64 pc;
|
||||
u64 psr;
|
||||
u32 psr;
|
||||
u32 write;
|
||||
u64 tpidr;
|
||||
u64 reserved;
|
||||
|
||||
constexpr void GetSvcThreadContext(ams::svc::LastThreadContext *out) const {
|
||||
if ((this->psr & 0x10) == 0) {
|
||||
/* aarch64 thread. */
|
||||
out->fp = this->x[29];
|
||||
out->sp = this->sp;
|
||||
out->lr = this->x[30];
|
||||
out->pc = this->pc;
|
||||
} else {
|
||||
/* aarch32 thread. */
|
||||
out->fp = this->x[11];
|
||||
out->sp = this->x[13];
|
||||
out->lr = this->x[14];
|
||||
out->pc = this->pc;
|
||||
}
|
||||
}
|
||||
};
|
||||
static_assert(sizeof(KExceptionContext) == 0x120);
|
||||
|
||||
|
|
|
@ -112,17 +112,17 @@ namespace ams::kern::arch::arm64 {
|
|||
static constexpr s32 NumPriorityLevels = 4;
|
||||
public:
|
||||
struct LocalState {
|
||||
u32 local_isenabler[NumLocalInterrupts / 32];
|
||||
u32 local_ipriorityr[NumLocalInterrupts / 4];
|
||||
u32 local_targetsr[NumLocalInterrupts / 4];
|
||||
u32 local_icfgr[NumLocalInterrupts / 16];
|
||||
u32 isenabler[NumLocalInterrupts / 32];
|
||||
u32 ipriorityr[NumLocalInterrupts / 4];
|
||||
u32 itargetsr[NumLocalInterrupts / 4];
|
||||
u32 icfgr[NumLocalInterrupts / 16];
|
||||
};
|
||||
|
||||
struct GlobalState {
|
||||
u32 global_isenabler[NumGlobalInterrupts / 32];
|
||||
u32 global_ipriorityr[NumGlobalInterrupts / 4];
|
||||
u32 global_targetsr[NumGlobalInterrupts / 4];
|
||||
u32 global_icfgr[NumGlobalInterrupts / 16];
|
||||
u32 isenabler[NumGlobalInterrupts / 32];
|
||||
u32 ipriorityr[NumGlobalInterrupts / 4];
|
||||
u32 itargetsr[NumGlobalInterrupts / 4];
|
||||
u32 icfgr[NumGlobalInterrupts / 16];
|
||||
};
|
||||
|
||||
enum PriorityLevel : u8 {
|
||||
|
@ -142,6 +142,11 @@ namespace ams::kern::arch::arm64 {
|
|||
|
||||
void Initialize(s32 core_id);
|
||||
void Finalize(s32 core_id);
|
||||
|
||||
void SaveCoreLocal(LocalState *state) const;
|
||||
void SaveGlobal(GlobalState *state) const;
|
||||
void RestoreCoreLocal(const LocalState *state) const;
|
||||
void RestoreGlobal(const GlobalState *state) const;
|
||||
public:
|
||||
u32 GetIrq() const {
|
||||
return this->gicc->iar;
|
||||
|
@ -213,12 +218,10 @@ namespace ams::kern::arch::arm64 {
|
|||
this->gicc->eoir = irq;
|
||||
}
|
||||
|
||||
bool IsInterruptDefined(s32 irq) {
|
||||
bool IsInterruptDefined(s32 irq) const {
|
||||
const s32 num_interrupts = std::min(32 + 32 * (this->gicd->typer & 0x1F), static_cast<u32>(NumInterrupts));
|
||||
return (0 <= irq && irq < num_interrupts);
|
||||
}
|
||||
|
||||
/* TODO: Implement more KInterruptController functionality. */
|
||||
public:
|
||||
static constexpr ALWAYS_INLINE bool IsSoftware(s32 id) {
|
||||
MESOSPHERE_ASSERT(0 <= id && id < NumInterrupts);
|
||||
|
|
|
@ -67,10 +67,21 @@ namespace ams::kern::arch::arm64 {
|
|||
NOINLINE void Initialize(s32 core_id);
|
||||
NOINLINE void Finalize(s32 core_id);
|
||||
|
||||
bool IsInterruptDefined(s32 irq) {
|
||||
NOINLINE void Save(s32 core_id);
|
||||
NOINLINE void Restore(s32 core_id);
|
||||
|
||||
bool IsInterruptDefined(s32 irq) const {
|
||||
return this->interrupt_controller.IsInterruptDefined(irq);
|
||||
}
|
||||
|
||||
bool IsGlobal(s32 irq) const {
|
||||
return this->interrupt_controller.IsGlobal(irq);
|
||||
}
|
||||
|
||||
bool IsLocal(s32 irq) const {
|
||||
return this->interrupt_controller.IsLocal(irq);
|
||||
}
|
||||
|
||||
NOINLINE Result BindHandler(KInterruptHandler *handler, s32 irq, s32 core_id, s32 priority, bool manual_clear, bool level);
|
||||
NOINLINE Result UnbindHandler(s32 irq, s32 core);
|
||||
|
||||
|
|
|
@ -182,25 +182,31 @@ namespace ams::kern::arch::arm64 {
|
|||
NOINLINE Result InitializeForProcess(u32 id, ams::svc::CreateProcessFlag as_type, bool enable_aslr, bool from_back, KMemoryManager::Pool pool, KProcessAddress code_address, size_t code_size, KMemoryBlockSlabManager *mem_block_slab_manager, KBlockInfoManager *block_info_manager, KPageTableManager *pt_manager);
|
||||
Result Finalize();
|
||||
private:
|
||||
Result Map(KProcessAddress virt_addr, KPhysicalAddress phys_addr, size_t num_pages, PageTableEntry entry_template, PageLinkedList *page_list, bool reuse_ll);
|
||||
Result MapL1Blocks(KProcessAddress virt_addr, KPhysicalAddress phys_addr, size_t num_pages, PageTableEntry entry_template, PageLinkedList *page_list, bool reuse_ll);
|
||||
Result MapL2Blocks(KProcessAddress virt_addr, KPhysicalAddress phys_addr, size_t num_pages, PageTableEntry entry_template, PageLinkedList *page_list, bool reuse_ll);
|
||||
Result MapL3Blocks(KProcessAddress virt_addr, KPhysicalAddress phys_addr, size_t num_pages, PageTableEntry entry_template, PageLinkedList *page_list, bool reuse_ll);
|
||||
|
||||
Result Unmap(KProcessAddress virt_addr, size_t num_pages, PageLinkedList *page_list, bool force, bool reuse_ll);
|
||||
|
||||
Result Map(KProcessAddress virt_addr, KPhysicalAddress phys_addr, size_t num_pages, PageTableEntry entry_template, size_t page_size, PageLinkedList *page_list, bool reuse_ll) {
|
||||
switch (page_size) {
|
||||
case L1BlockSize:
|
||||
return this->MapL1Blocks(virt_addr, phys_addr, num_pages, entry_template, page_list, reuse_ll);
|
||||
case L2ContiguousBlockSize:
|
||||
entry_template.SetContiguous(true);
|
||||
[[fallthrough]];
|
||||
#ifdef ATMOSPHERE_BOARD_NINTENDO_NX
|
||||
case L2TegraSmmuBlockSize:
|
||||
#endif
|
||||
case L2BlockSize:
|
||||
case L3BlockSize:
|
||||
break;
|
||||
case L2ContiguousBlockSize:
|
||||
return this->MapL2Blocks(virt_addr, phys_addr, num_pages, entry_template, page_list, reuse_ll);
|
||||
case L3ContiguousBlockSize:
|
||||
entry_template.SetContiguous(true);
|
||||
break;
|
||||
[[fallthrough]];
|
||||
case L3BlockSize:
|
||||
return this->MapL3Blocks(virt_addr, phys_addr, num_pages, entry_template, page_list, reuse_ll);
|
||||
MESOSPHERE_UNREACHABLE_DEFAULT_CASE();
|
||||
}
|
||||
return this->Map(virt_addr, phys_addr, num_pages, entry_template, page_list, reuse_ll);
|
||||
}
|
||||
|
||||
Result MapContiguous(KProcessAddress virt_addr, KPhysicalAddress phys_addr, size_t num_pages, PageTableEntry entry_template, PageLinkedList *page_list, bool reuse_ll);
|
||||
|
|
|
@ -106,6 +106,8 @@ namespace ams::kern::arch::arm64 {
|
|||
NOINLINE void InitializeForProcess(void *tb, KVirtualAddress start, KVirtualAddress end);
|
||||
L1PageTableEntry *Finalize();
|
||||
|
||||
void Dump(uintptr_t start, size_t size) const;
|
||||
|
||||
bool BeginTraversal(TraversalEntry *out_entry, TraversalContext *out_context, KProcessAddress address) const;
|
||||
bool ContinueTraversal(TraversalEntry *out_entry, TraversalContext *context) const;
|
||||
|
||||
|
|
|
@ -44,6 +44,10 @@ namespace ams::kern::arch::arm64 {
|
|||
return this->page_table.SetProcessMemoryPermission(addr, size, perm);
|
||||
}
|
||||
|
||||
Result SetMemoryAttribute(KProcessAddress addr, size_t size, u32 mask, u32 attr) {
|
||||
return this->page_table.SetMemoryAttribute(addr, size, mask, attr);
|
||||
}
|
||||
|
||||
Result SetHeapSize(KProcessAddress *out, size_t size) {
|
||||
return this->page_table.SetHeapSize(out, size);
|
||||
}
|
||||
|
@ -56,6 +60,34 @@ namespace ams::kern::arch::arm64 {
|
|||
return this->page_table.QueryInfo(out_info, out_page_info, addr);
|
||||
}
|
||||
|
||||
Result QueryPhysicalAddress(ams::svc::PhysicalMemoryInfo *out, KProcessAddress address) const {
|
||||
return this->page_table.QueryPhysicalAddress(out, address);
|
||||
}
|
||||
|
||||
Result QueryStaticMapping(KProcessAddress *out, KPhysicalAddress address, size_t size) const {
|
||||
return this->page_table.QueryStaticMapping(out, address, size);
|
||||
}
|
||||
|
||||
Result QueryIoMapping(KProcessAddress *out, KPhysicalAddress address, size_t size) const {
|
||||
return this->page_table.QueryIoMapping(out, address, size);
|
||||
}
|
||||
|
||||
Result MapMemory(KProcessAddress dst_address, KProcessAddress src_address, size_t size) {
|
||||
return this->page_table.MapMemory(dst_address, src_address, size);
|
||||
}
|
||||
|
||||
Result UnmapMemory(KProcessAddress dst_address, KProcessAddress src_address, size_t size) {
|
||||
return this->page_table.UnmapMemory(dst_address, src_address, size);
|
||||
}
|
||||
|
||||
Result MapCodeMemory(KProcessAddress dst_address, KProcessAddress src_address, size_t size) {
|
||||
return this->page_table.MapCodeMemory(dst_address, src_address, size);
|
||||
}
|
||||
|
||||
Result UnmapCodeMemory(KProcessAddress dst_address, KProcessAddress src_address, size_t size) {
|
||||
return this->page_table.UnmapCodeMemory(dst_address, src_address, size);
|
||||
}
|
||||
|
||||
Result MapIo(KPhysicalAddress phys_addr, size_t size, KMemoryPermission perm) {
|
||||
return this->page_table.MapIo(phys_addr, size, perm);
|
||||
}
|
||||
|
@ -72,6 +104,10 @@ namespace ams::kern::arch::arm64 {
|
|||
return this->page_table.MapPageGroup(addr, pg, state, perm);
|
||||
}
|
||||
|
||||
Result UnmapPageGroup(KProcessAddress address, const KPageGroup &pg, KMemoryState state) {
|
||||
return this->page_table.UnmapPageGroup(address, pg, state);
|
||||
}
|
||||
|
||||
Result MapPages(KProcessAddress *out_addr, size_t num_pages, size_t alignment, KPhysicalAddress phys_addr, KMemoryState state, KMemoryPermission perm) {
|
||||
return this->page_table.MapPages(out_addr, num_pages, alignment, phys_addr, state, perm);
|
||||
}
|
||||
|
@ -80,6 +116,10 @@ namespace ams::kern::arch::arm64 {
|
|||
return this->page_table.MapPages(out_addr, num_pages, state, perm);
|
||||
}
|
||||
|
||||
Result MapPages(KProcessAddress address, size_t num_pages, KMemoryState state, KMemoryPermission perm) {
|
||||
return this->page_table.MapPages(address, num_pages, state, perm);
|
||||
}
|
||||
|
||||
Result UnmapPages(KProcessAddress addr, size_t num_pages, KMemoryState state) {
|
||||
return this->page_table.UnmapPages(addr, num_pages, state);
|
||||
}
|
||||
|
@ -88,11 +128,123 @@ namespace ams::kern::arch::arm64 {
|
|||
return this->page_table.MakeAndOpenPageGroup(out, address, num_pages, state_mask, state, perm_mask, perm, attr_mask, attr);
|
||||
}
|
||||
|
||||
Result MakeAndOpenPageGroupContiguous(KPageGroup *out, KProcessAddress address, size_t num_pages, u32 state_mask, u32 state, u32 perm_mask, u32 perm, u32 attr_mask, u32 attr) {
|
||||
return this->page_table.MakeAndOpenPageGroupContiguous(out, address, num_pages, state_mask, state, perm_mask, perm, attr_mask, attr);
|
||||
}
|
||||
|
||||
Result InvalidateProcessDataCache(KProcessAddress address, size_t size) {
|
||||
return this->page_table.InvalidateProcessDataCache(address, size);
|
||||
}
|
||||
|
||||
Result ReadDebugMemory(void *buffer, KProcessAddress address, size_t size) {
|
||||
return this->page_table.ReadDebugMemory(buffer, address, size);
|
||||
}
|
||||
|
||||
Result WriteDebugMemory(KProcessAddress address, const void *buffer, size_t size) {
|
||||
return this->page_table.WriteDebugMemory(address, buffer, size);
|
||||
}
|
||||
|
||||
Result LockForDeviceAddressSpace(KPageGroup *out, KProcessAddress address, size_t size, KMemoryPermission perm, bool is_aligned) {
|
||||
return this->page_table.LockForDeviceAddressSpace(out, address, size, perm, is_aligned);
|
||||
}
|
||||
|
||||
Result UnlockForDeviceAddressSpace(KProcessAddress address, size_t size) {
|
||||
return this->page_table.UnlockForDeviceAddressSpace(address, size);
|
||||
}
|
||||
|
||||
Result LockForIpcUserBuffer(KPhysicalAddress *out, KProcessAddress address, size_t size) {
|
||||
return this->page_table.LockForIpcUserBuffer(out, address, size);
|
||||
}
|
||||
|
||||
Result UnlockForIpcUserBuffer(KProcessAddress address, size_t size) {
|
||||
return this->page_table.UnlockForIpcUserBuffer(address, size);
|
||||
}
|
||||
|
||||
Result LockForTransferMemory(KPageGroup *out, KProcessAddress address, size_t size, KMemoryPermission perm) {
|
||||
return this->page_table.LockForTransferMemory(out, address, size, perm);
|
||||
}
|
||||
|
||||
Result UnlockForTransferMemory(KProcessAddress address, size_t size, const KPageGroup &pg) {
|
||||
return this->page_table.UnlockForTransferMemory(address, size, pg);
|
||||
}
|
||||
|
||||
Result LockForCodeMemory(KPageGroup *out, KProcessAddress address, size_t size) {
|
||||
return this->page_table.LockForCodeMemory(out, address, size);
|
||||
}
|
||||
|
||||
Result UnlockForCodeMemory(KProcessAddress address, size_t size, const KPageGroup &pg) {
|
||||
return this->page_table.UnlockForCodeMemory(address, size, pg);
|
||||
}
|
||||
|
||||
Result CopyMemoryFromLinearToUser(KProcessAddress dst_addr, size_t size, KProcessAddress src_addr, u32 src_state_mask, u32 src_state, KMemoryPermission src_test_perm, u32 src_attr_mask, u32 src_attr) {
|
||||
return this->page_table.CopyMemoryFromLinearToUser(dst_addr, size, src_addr, src_state_mask, src_state, src_test_perm, src_attr_mask, src_attr);
|
||||
}
|
||||
|
||||
Result CopyMemoryFromLinearToKernel(KProcessAddress dst_addr, size_t size, KProcessAddress src_addr, u32 src_state_mask, u32 src_state, KMemoryPermission src_test_perm, u32 src_attr_mask, u32 src_attr) {
|
||||
return this->page_table.CopyMemoryFromLinearToKernel(dst_addr, size, src_addr, src_state_mask, src_state, src_test_perm, src_attr_mask, src_attr);
|
||||
}
|
||||
|
||||
Result CopyMemoryFromUserToLinear(KProcessAddress dst_addr, size_t size, u32 dst_state_mask, u32 dst_state, KMemoryPermission dst_test_perm, u32 dst_attr_mask, u32 dst_attr, KProcessAddress src_addr) {
|
||||
return this->page_table.CopyMemoryFromUserToLinear(dst_addr, size, dst_state_mask, dst_state, dst_test_perm, dst_attr_mask, dst_attr, src_addr);
|
||||
}
|
||||
|
||||
Result CopyMemoryFromKernelToLinear(KProcessAddress dst_addr, size_t size, u32 dst_state_mask, u32 dst_state, KMemoryPermission dst_test_perm, u32 dst_attr_mask, u32 dst_attr, KProcessAddress src_addr) {
|
||||
return this->page_table.CopyMemoryFromKernelToLinear(dst_addr, size, dst_state_mask, dst_state, dst_test_perm, dst_attr_mask, dst_attr, src_addr);
|
||||
}
|
||||
|
||||
Result CopyMemoryFromHeapToHeap(KProcessPageTable &dst_page_table, KProcessAddress dst_addr, size_t size, u32 dst_state_mask, u32 dst_state, KMemoryPermission dst_test_perm, u32 dst_attr_mask, u32 dst_attr, KProcessAddress src_addr, u32 src_state_mask, u32 src_state, KMemoryPermission src_test_perm, u32 src_attr_mask, u32 src_attr) {
|
||||
return this->page_table.CopyMemoryFromHeapToHeap(dst_page_table.page_table, dst_addr, size, dst_state_mask, dst_state, dst_test_perm, dst_attr_mask, dst_attr, src_addr, src_state_mask, src_state, src_test_perm, src_attr_mask, src_attr);
|
||||
}
|
||||
|
||||
Result CopyMemoryFromHeapToHeapWithoutCheckDestination(KProcessPageTable &dst_page_table, KProcessAddress dst_addr, size_t size, u32 dst_state_mask, u32 dst_state, KMemoryPermission dst_test_perm, u32 dst_attr_mask, u32 dst_attr, KProcessAddress src_addr, u32 src_state_mask, u32 src_state, KMemoryPermission src_test_perm, u32 src_attr_mask, u32 src_attr) {
|
||||
return this->page_table.CopyMemoryFromHeapToHeapWithoutCheckDestination(dst_page_table.page_table, dst_addr, size, dst_state_mask, dst_state, dst_test_perm, dst_attr_mask, dst_attr, src_addr, src_state_mask, src_state, src_test_perm, src_attr_mask, src_attr);
|
||||
}
|
||||
|
||||
Result SetupForIpc(KProcessAddress *out_dst_addr, size_t size, KProcessAddress src_addr, KProcessPageTable &src_page_table, KMemoryPermission test_perm, KMemoryState dst_state, bool send) {
|
||||
return this->page_table.SetupForIpc(out_dst_addr, size, src_addr, src_page_table.page_table, test_perm, dst_state, send);
|
||||
}
|
||||
|
||||
Result CleanupForIpcServer(KProcessAddress address, size_t size, KMemoryState dst_state, KProcess *server_process) {
|
||||
return this->page_table.CleanupForIpcServer(address, size, dst_state, server_process);
|
||||
}
|
||||
|
||||
Result CleanupForIpcClient(KProcessAddress address, size_t size, KMemoryState dst_state) {
|
||||
return this->page_table.CleanupForIpcClient(address, size, dst_state);
|
||||
}
|
||||
|
||||
Result MapPhysicalMemory(KProcessAddress address, size_t size) {
|
||||
return this->page_table.MapPhysicalMemory(address, size);
|
||||
}
|
||||
|
||||
Result UnmapPhysicalMemory(KProcessAddress address, size_t size) {
|
||||
return this->page_table.UnmapPhysicalMemory(address, size);
|
||||
}
|
||||
|
||||
Result MapPhysicalMemoryUnsafe(KProcessAddress address, size_t size) {
|
||||
return this->page_table.MapPhysicalMemoryUnsafe(address, size);
|
||||
}
|
||||
|
||||
Result UnmapPhysicalMemoryUnsafe(KProcessAddress address, size_t size) {
|
||||
return this->page_table.UnmapPhysicalMemoryUnsafe(address, size);
|
||||
}
|
||||
|
||||
void DumpTable() const {
|
||||
return this->page_table.DumpTable();
|
||||
}
|
||||
|
||||
void DumpMemoryBlocks() const {
|
||||
return this->page_table.DumpMemoryBlocks();
|
||||
}
|
||||
|
||||
bool GetPhysicalAddress(KPhysicalAddress *out, KProcessAddress address) const {
|
||||
return this->page_table.GetPhysicalAddress(out, address);
|
||||
}
|
||||
|
||||
bool Contains(KProcessAddress addr, size_t size) const { return this->page_table.Contains(addr, size); }
|
||||
|
||||
bool IsInAliasRegion(KProcessAddress addr, size_t size) const { return this->page_table.IsInAliasRegion(addr, size); }
|
||||
bool IsInUnsafeAliasRegion(KProcessAddress addr, size_t size) const { return this->page_table.IsInUnsafeAliasRegion(addr, size); }
|
||||
|
||||
bool CanContain(KProcessAddress addr, size_t size, KMemoryState state) const { return this->page_table.CanContain(addr, size, state); }
|
||||
|
||||
KProcessAddress GetAddressSpaceStart() const { return this->page_table.GetAddressSpaceStart(); }
|
||||
|
@ -109,8 +261,11 @@ namespace ams::kern::arch::arm64 {
|
|||
size_t GetKernelMapRegionSize() const { return this->page_table.GetKernelMapRegionSize(); }
|
||||
size_t GetAliasCodeRegionSize() const { return this->page_table.GetAliasCodeRegionSize(); }
|
||||
|
||||
size_t GetNormalMemorySize() const { return this->page_table.GetNormalMemorySize(); }
|
||||
|
||||
u32 GetAllocateOption() const { return this->page_table.GetAllocateOption(); }
|
||||
|
||||
KPhysicalAddress GetHeapPhysicalAddress(KVirtualAddress address) const {
|
||||
/* TODO: Better way to convert address type? */
|
||||
return this->page_table.GetHeapPhysicalAddress(address);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
#pragma once
|
||||
#include <vapours.hpp>
|
||||
#include <mesosphere/kern_select_cpu.hpp>
|
||||
|
||||
namespace ams::kern::arch::arm64 {
|
||||
|
||||
template<typename T>
|
||||
concept SlabHeapNode = requires (T &t) {
|
||||
{ t.next } -> std::convertible_to<T *>;
|
||||
};
|
||||
|
||||
template<typename T> requires SlabHeapNode<T>
|
||||
ALWAYS_INLINE T *AllocateFromSlabAtomic(T **head) {
|
||||
u32 tmp;
|
||||
T *node, *next;
|
||||
|
||||
__asm__ __volatile__(
|
||||
"1:\n"
|
||||
" ldaxr %[node], [%[head]]\n"
|
||||
" cbz %[node], 2f\n"
|
||||
" ldr %[next], [%[node]]\n"
|
||||
" stlxr %w[tmp], %[next], [%[head]]\n"
|
||||
" cbnz %w[tmp], 1b\n"
|
||||
" b 3f\n"
|
||||
"2:\n"
|
||||
" clrex\n"
|
||||
"3:\n"
|
||||
: [tmp]"=&r"(tmp), [node]"=&r"(node), [next]"=&r"(next), [head]"+&r"(head)
|
||||
:
|
||||
: "cc", "memory"
|
||||
);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
template<typename T> requires SlabHeapNode<T>
|
||||
ALWAYS_INLINE void FreeToSlabAtomic(T **head, T *node) {
|
||||
u32 tmp;
|
||||
T *next;
|
||||
|
||||
__asm__ __volatile__(
|
||||
"1:\n"
|
||||
" ldaxr %[next], [%[head]]\n"
|
||||
" str %[next], [%[node]]\n"
|
||||
" stlxr %w[tmp], %[node], [%[head]]\n"
|
||||
" cbnz %w[tmp], 1b\n"
|
||||
"2:\n"
|
||||
: [tmp]"=&r"(tmp), [node]"+&r"(node), [next]"=&r"(next), [head]"+&r"(head)
|
||||
:
|
||||
: "cc", "memory"
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -23,9 +23,9 @@ namespace ams::kern::arch::arm64 {
|
|||
class KSupervisorPageTable {
|
||||
private:
|
||||
KPageTable page_table;
|
||||
u64 ttbr0[cpu::NumCores];
|
||||
u64 ttbr0_identity[cpu::NumCores];
|
||||
public:
|
||||
constexpr KSupervisorPageTable() : page_table(), ttbr0() { /* ... */ }
|
||||
constexpr KSupervisorPageTable() : page_table(), ttbr0_identity() { /* ... */ }
|
||||
|
||||
NOINLINE void Initialize(s32 core_id);
|
||||
|
||||
|
@ -41,8 +41,6 @@ namespace ams::kern::arch::arm64 {
|
|||
cpu::InvalidateEntireTlb();
|
||||
}
|
||||
|
||||
void Finalize(s32 core_id);
|
||||
|
||||
Result MapPages(KProcessAddress *out_addr, size_t num_pages, size_t alignment, KPhysicalAddress phys_addr, KProcessAddress region_start, size_t region_num_pages, KMemoryState state, KMemoryPermission perm) {
|
||||
return this->page_table.MapPages(out_addr, num_pages, alignment, phys_addr, region_start, region_num_pages, state, perm);
|
||||
}
|
||||
|
@ -62,6 +60,8 @@ namespace ams::kern::arch::arm64 {
|
|||
bool GetPhysicalAddress(KPhysicalAddress *out, KProcessAddress address) const {
|
||||
return this->page_table.GetPhysicalAddress(out, address);
|
||||
}
|
||||
|
||||
constexpr u64 GetIdentityMapTtbr0(s32 core_id) const { return this->ttbr0_identity[core_id]; }
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -66,7 +66,21 @@ namespace ams::kern::arch::arm64 {
|
|||
|
||||
static void FpuContextSwitchHandler(KThread *thread);
|
||||
|
||||
/* TODO: More methods (especially FPU management) */
|
||||
u32 GetFpcr() const { return this->fpcr; }
|
||||
u32 GetFpsr() const { return this->fpsr; }
|
||||
|
||||
void SetFpcr(u32 v) { this->fpcr = v; }
|
||||
void SetFpsr(u32 v) { this->fpsr = v; }
|
||||
|
||||
void CloneFpuStatus();
|
||||
|
||||
void SetFpuRegisters(const u128 *v, bool is_64_bit);
|
||||
|
||||
const u128 *GetFpuRegisters() const { return this->fpu_registers; }
|
||||
public:
|
||||
static void OnThreadTerminating(const KThread *thread);
|
||||
};
|
||||
|
||||
void GetUserContext(ams::svc::ThreadContext *out, const KThread *thread);
|
||||
|
||||
}
|
|
@ -39,6 +39,10 @@ namespace ams::kern::arch::arm64 {
|
|||
static bool ClearMemoryAligned64Bit(void *dst, size_t size);
|
||||
static bool ClearMemorySize32Bit(void *dst);
|
||||
|
||||
static bool UpdateLockAtomic(u32 *out, u32 *address, u32 if_zero, u32 new_orr_mask);
|
||||
static bool UpdateIfEqualAtomic(s32 *out, s32 *address, s32 compare_value, s32 new_value);
|
||||
static bool DecrementIfLessThanAtomic(s32 *out, s32 *address, s32 compare);
|
||||
|
||||
static bool StoreDataCache(uintptr_t start, uintptr_t end);
|
||||
static bool FlushDataCache(uintptr_t start, uintptr_t end);
|
||||
static bool InvalidateDataCache(uintptr_t start, uintptr_t end);
|
||||
|
|
|
@ -35,6 +35,14 @@ namespace ams::kern::board::nintendo::nx {
|
|||
u32 hs_attached_value;
|
||||
u32 hs_detached_value;
|
||||
private:
|
||||
static ALWAYS_INLINE bool IsHeapVirtualAddress(KVirtualAddress addr) {
|
||||
return KMemoryLayout::IsHeapVirtualAddress(nullptr, addr);
|
||||
}
|
||||
|
||||
static ALWAYS_INLINE bool IsHeapPhysicalAddress(KPhysicalAddress addr) {
|
||||
return KMemoryLayout::IsHeapPhysicalAddress(nullptr, addr);
|
||||
}
|
||||
|
||||
static ALWAYS_INLINE KVirtualAddress GetHeapVirtualAddress(KPhysicalAddress addr) {
|
||||
return KPageTable::GetHeapVirtualAddress(addr);
|
||||
}
|
||||
|
@ -53,7 +61,30 @@ namespace ams::kern::board::nintendo::nx {
|
|||
public:
|
||||
constexpr KDevicePageTable() : tables(), table_asids(), attached_device(), attached_value(), detached_value(), hs_attached_value(), hs_detached_value() { /* ... */ }
|
||||
|
||||
Result Initialize(u64 space_address, u64 space_size);
|
||||
void Finalize();
|
||||
|
||||
Result Attach(ams::svc::DeviceName device_name, u64 space_address, u64 space_size);
|
||||
Result Detach(ams::svc::DeviceName device_name);
|
||||
|
||||
Result Map(size_t *out_mapped_size, const KPageGroup &pg, KDeviceVirtualAddress device_address, ams::svc::MemoryPermission device_perm, bool refresh_mappings);
|
||||
Result Unmap(const KPageGroup &pg, KDeviceVirtualAddress device_address);
|
||||
private:
|
||||
Result MapDevicePage(size_t *out_mapped_size, s32 &num_pt, s32 max_pt, KPhysicalAddress phys_addr, u64 size, KDeviceVirtualAddress address, ams::svc::MemoryPermission device_perm);
|
||||
|
||||
Result MapImpl(size_t *out_mapped_size, s32 &num_pt, s32 max_pt, const KPageGroup &pg, KDeviceVirtualAddress device_address, ams::svc::MemoryPermission device_perm);
|
||||
void UnmapImpl(KDeviceVirtualAddress address, u64 size, bool force);
|
||||
|
||||
bool IsFree(KDeviceVirtualAddress address, u64 size) const;
|
||||
Result MakePageGroup(KPageGroup *out, KDeviceVirtualAddress address, u64 size) const;
|
||||
bool Compare(const KPageGroup &pg, KDeviceVirtualAddress device_address) const;
|
||||
public:
|
||||
static void Initialize();
|
||||
|
||||
static void Lock();
|
||||
static void Unlock();
|
||||
static void Sleep();
|
||||
static void Wakeup();
|
||||
};
|
||||
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
#pragma once
|
||||
#include <mesosphere/kern_common.hpp>
|
||||
|
||||
namespace ams::kern {
|
||||
|
||||
constexpr inline size_t MainMemorySize = 4_GB;
|
||||
|
||||
}
|
|
@ -48,7 +48,7 @@ namespace ams::kern::board::nintendo::nx {
|
|||
|
||||
/* Privileged Access. */
|
||||
static void ReadWriteRegisterPrivileged(u32 *out, ams::svc::PhysicalAddress address, u32 mask, u32 value);
|
||||
static void ReadWriteRegister(u32 *out, ams::svc::PhysicalAddress address, u32 mask, u32 value);
|
||||
static Result ReadWriteRegister(u32 *out, ams::svc::PhysicalAddress address, u32 mask, u32 value);
|
||||
|
||||
static ALWAYS_INLINE u32 ReadRegisterPrivileged(ams::svc::PhysicalAddress address) {
|
||||
u32 v;
|
||||
|
@ -67,6 +67,11 @@ namespace ams::kern::board::nintendo::nx {
|
|||
|
||||
/* User access. */
|
||||
static void CallSecureMonitorFromUser(ams::svc::lp64::SecureMonitorArguments *args);
|
||||
|
||||
/* Secure Memory. */
|
||||
static size_t CalculateRequiredSecureMemorySize(size_t size, u32 pool);
|
||||
static Result AllocateSecureMemory(KVirtualAddress *out, size_t size, u32 pool);
|
||||
static void FreeSecureMemory(KVirtualAddress address, size_t size, u32 pool);
|
||||
};
|
||||
|
||||
}
|
|
@ -15,6 +15,7 @@
|
|||
*/
|
||||
#pragma once
|
||||
#include <mesosphere/kern_common.hpp>
|
||||
#include <mesosphere/svc/kern_svc_k_user_pointer.hpp>
|
||||
|
||||
namespace ams::kern {
|
||||
|
||||
|
@ -26,6 +27,12 @@ namespace ams::kern {
|
|||
|
||||
static NOINLINE void Printf(const char *format, ...) __attribute__((format(printf, 1, 2)));
|
||||
static NOINLINE void VPrintf(const char *format, ::std::va_list vl);
|
||||
|
||||
static NOINLINE Result PrintUserString(ams::kern::svc::KUserPointer<const char *> user_str, size_t len);
|
||||
|
||||
/* Functionality for preserving across sleep. */
|
||||
static NOINLINE void Save();
|
||||
static NOINLINE void Restore();
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -22,9 +22,9 @@ namespace ams::kern {
|
|||
struct KAddressSpaceInfo {
|
||||
public:
|
||||
enum Type {
|
||||
Type_32Bit = 0,
|
||||
Type_Small64Bit = 1,
|
||||
Type_Large64Bit = 2,
|
||||
Type_MapSmall = 0,
|
||||
Type_MapLarge = 1,
|
||||
Type_Map39Bit = 2,
|
||||
Type_Heap = 3,
|
||||
Type_Stack = 4,
|
||||
Type_Alias = 5,
|
||||
|
|
|
@ -127,6 +127,7 @@ namespace ams::kern {
|
|||
u32 cur_ref_count = this->ref_count.load(std::memory_order_acquire);
|
||||
do {
|
||||
if (AMS_UNLIKELY(cur_ref_count == 0)) {
|
||||
MESOSPHERE_AUDIT(cur_ref_count != 0);
|
||||
return false;
|
||||
}
|
||||
MESOSPHERE_ABORT_UNLESS(cur_ref_count < cur_ref_count + 1);
|
||||
|
@ -177,10 +178,12 @@ namespace ams::kern {
|
|||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
template<typename T> requires std::derived_from<T, KAutoObject>
|
||||
class KScopedAutoObject {
|
||||
static_assert(std::is_base_of<KAutoObject, T>::value);
|
||||
NON_COPYABLE(KScopedAutoObject);
|
||||
private:
|
||||
template<typename U>
|
||||
friend class KScopedAutoObject;
|
||||
private:
|
||||
T *obj;
|
||||
private:
|
||||
|
@ -202,12 +205,28 @@ namespace ams::kern {
|
|||
this->obj = nullptr;
|
||||
}
|
||||
|
||||
constexpr ALWAYS_INLINE KScopedAutoObject(KScopedAutoObject &&rhs) {
|
||||
this->obj = rhs.obj;
|
||||
rhs.obj = nullptr;
|
||||
template<typename U> requires (std::derived_from<T, U> || std::derived_from<U, T>)
|
||||
constexpr ALWAYS_INLINE KScopedAutoObject(KScopedAutoObject<U> &&rhs) {
|
||||
if constexpr (std::derived_from<U, T>) {
|
||||
/* Upcast. */
|
||||
this->obj = rhs.obj;
|
||||
rhs.obj = nullptr;
|
||||
} else {
|
||||
/* Downcast. */
|
||||
T *derived = nullptr;
|
||||
if (rhs.obj != nullptr) {
|
||||
derived = rhs.obj->template DynamicCast<T *>();
|
||||
if (derived == nullptr) {
|
||||
rhs.obj->Close();
|
||||
}
|
||||
}
|
||||
|
||||
this->obj = derived;
|
||||
rhs.obj = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
constexpr ALWAYS_INLINE KScopedAutoObject &operator=(KScopedAutoObject &&rhs) {
|
||||
constexpr ALWAYS_INLINE KScopedAutoObject<T> &operator=(KScopedAutoObject<T> &&rhs) {
|
||||
rhs.Swap(*this);
|
||||
return *this;
|
||||
}
|
||||
|
@ -221,6 +240,8 @@ namespace ams::kern {
|
|||
|
||||
constexpr ALWAYS_INLINE T *GetPointerUnsafe() { return this->obj; }
|
||||
|
||||
constexpr ALWAYS_INLINE T *ReleasePointerUnsafe() { T *ret = this->obj; this->obj = nullptr; return ret; }
|
||||
|
||||
constexpr ALWAYS_INLINE bool IsNull() const { return this->obj == nullptr; }
|
||||
constexpr ALWAYS_INLINE bool IsNotNull() const { return this->obj != nullptr; }
|
||||
};
|
||||
|
|
|
@ -23,7 +23,7 @@ namespace ams::kern {
|
|||
class KAutoObjectWithListContainer {
|
||||
NON_COPYABLE(KAutoObjectWithListContainer);
|
||||
NON_MOVEABLE(KAutoObjectWithListContainer);
|
||||
private:
|
||||
public:
|
||||
using ListType = util::IntrusiveRedBlackTreeMemberTraits<&KAutoObjectWithList::list_node>::TreeType<KAutoObjectWithList>;
|
||||
public:
|
||||
class ListAccessor : public KScopedLightLock {
|
||||
|
|
|
@ -221,6 +221,12 @@ namespace ams::kern {
|
|||
data[id / BitsPerWord] &= ~(1ul << (id % BitsPerWord));
|
||||
}
|
||||
|
||||
static constexpr ALWAYS_INLINE bool GetSvcAllowedImpl(u8 *data, u32 id) {
|
||||
constexpr size_t BitsPerWord = BITSIZEOF(*data);
|
||||
MESOSPHERE_ASSERT(id < svc::SvcId_Count);
|
||||
return (data[id / BitsPerWord] & (1ul << (id % BitsPerWord))) != 0;
|
||||
}
|
||||
|
||||
bool SetSvcAllowed(u32 id) {
|
||||
if (id < BITSIZEOF(this->svc_access_flags)) {
|
||||
SetSvcAllowedImpl(this->svc_access_flags, id);
|
||||
|
@ -230,10 +236,10 @@ namespace ams::kern {
|
|||
}
|
||||
}
|
||||
|
||||
bool SetInterruptAllowed(u32 id) {
|
||||
bool SetInterruptPermitted(u32 id) {
|
||||
constexpr size_t BitsPerWord = BITSIZEOF(this->irq_access_flags[0]);
|
||||
if (id < BITSIZEOF(this->irq_access_flags)) {
|
||||
this->irq_access_flags[id / BitsPerWord] = (1ul << (id % BitsPerWord));
|
||||
this->irq_access_flags[id / BitsPerWord] |= (1ul << (id % BitsPerWord));
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
|
@ -253,10 +259,12 @@ namespace ams::kern {
|
|||
|
||||
Result SetCapability(const util::BitPack32 cap, u32 &set_flags, u32 &set_svc, KProcessPageTable *page_table);
|
||||
Result SetCapabilities(const u32 *caps, s32 num_caps, KProcessPageTable *page_table);
|
||||
Result SetCapabilities(svc::KUserPointer<const u32 *> user_caps, s32 num_caps, KProcessPageTable *page_table);
|
||||
public:
|
||||
constexpr KCapabilities() = default;
|
||||
|
||||
Result Initialize(const u32 *caps, s32 num_caps, KProcessPageTable *page_table);
|
||||
Result Initialize(svc::KUserPointer<const u32 *> user_caps, s32 num_caps, KProcessPageTable *page_table);
|
||||
|
||||
constexpr u64 GetCoreMask() const { return this->core_mask; }
|
||||
constexpr u64 GetPriorityMask() const { return this->priority_mask; }
|
||||
|
@ -264,17 +272,88 @@ namespace ams::kern {
|
|||
|
||||
ALWAYS_INLINE void CopySvcPermissionsTo(KThread::StackParameters &sp) const {
|
||||
static_assert(sizeof(svc_access_flags) == sizeof(sp.svc_permission));
|
||||
/* Copy permissions. */
|
||||
std::memcpy(sp.svc_permission, this->svc_access_flags, sizeof(this->svc_access_flags));
|
||||
|
||||
/* Clear specific SVCs based on our state. */
|
||||
ClearSvcAllowedImpl(sp.svc_permission, svc::SvcId_ReturnFromException);
|
||||
ClearSvcAllowedImpl(sp.svc_permission, svc::SvcId_SynchronizePreemptionState);
|
||||
if (sp.is_preemption_state_pinned) {
|
||||
if (sp.is_pinned) {
|
||||
ClearSvcAllowedImpl(sp.svc_permission, svc::SvcId_GetInfo);
|
||||
}
|
||||
}
|
||||
|
||||
/* TODO: Member functions. */
|
||||
ALWAYS_INLINE void CopyPinnedSvcPermissionsTo(KThread::StackParameters &sp) const {
|
||||
static_assert(sizeof(svc_access_flags) == sizeof(sp.svc_permission));
|
||||
/* Clear all permissions. */
|
||||
std::memset(sp.svc_permission, 0, sizeof(this->svc_access_flags));
|
||||
|
||||
/* Set specific SVCs based on our state. */
|
||||
SetSvcAllowedImpl(sp.svc_permission, svc::SvcId_SynchronizePreemptionState);
|
||||
if (GetSvcAllowedImpl(sp.svc_permission, svc::SvcId_ReturnFromException)) {
|
||||
SetSvcAllowedImpl(sp.svc_permission, svc::SvcId_ReturnFromException);
|
||||
SetSvcAllowedImpl(sp.svc_permission, svc::SvcId_GetInfo);
|
||||
}
|
||||
}
|
||||
|
||||
ALWAYS_INLINE void CopyUnpinnedSvcPermissionsTo(KThread::StackParameters &sp) const {
|
||||
static_assert(sizeof(svc_access_flags) == sizeof(sp.svc_permission));
|
||||
/* Get whether we have access to return from exception. */
|
||||
const bool return_from_exception = GetSvcAllowedImpl(sp.svc_permission, svc::SvcId_ReturnFromException);
|
||||
|
||||
/* Copy permissions. */
|
||||
std::memcpy(sp.svc_permission, this->svc_access_flags, sizeof(this->svc_access_flags));
|
||||
|
||||
/* Clear/Set specific SVCs based on our state. */
|
||||
ClearSvcAllowedImpl(sp.svc_permission, svc::SvcId_ReturnFromException);
|
||||
ClearSvcAllowedImpl(sp.svc_permission, svc::SvcId_SynchronizePreemptionState);
|
||||
if (return_from_exception) {
|
||||
SetSvcAllowedImpl(sp.svc_permission, svc::SvcId_ReturnFromException);
|
||||
}
|
||||
}
|
||||
|
||||
ALWAYS_INLINE void CopyEnterExceptionSvcPermissionsTo(KThread::StackParameters &sp) {
|
||||
static_assert(sizeof(svc_access_flags) == sizeof(sp.svc_permission));
|
||||
|
||||
/* Set ReturnFromException if allowed. */
|
||||
if (GetSvcAllowedImpl(this->svc_access_flags, svc::SvcId_ReturnFromException)) {
|
||||
SetSvcAllowedImpl(sp.svc_permission, svc::SvcId_ReturnFromException);
|
||||
}
|
||||
|
||||
/* Set GetInfo if allowed. */
|
||||
if (GetSvcAllowedImpl(this->svc_access_flags, svc::SvcId_GetInfo)) {
|
||||
SetSvcAllowedImpl(sp.svc_permission, svc::SvcId_GetInfo);
|
||||
}
|
||||
}
|
||||
|
||||
ALWAYS_INLINE void CopyLeaveExceptionSvcPermissionsTo(KThread::StackParameters &sp) {
|
||||
static_assert(sizeof(svc_access_flags) == sizeof(sp.svc_permission));
|
||||
|
||||
/* Clear ReturnFromException. */
|
||||
ClearSvcAllowedImpl(sp.svc_permission, svc::SvcId_ReturnFromException);
|
||||
|
||||
/* If pinned, clear GetInfo. */
|
||||
if (sp.is_pinned) {
|
||||
ClearSvcAllowedImpl(sp.svc_permission, svc::SvcId_GetInfo);
|
||||
}
|
||||
}
|
||||
|
||||
constexpr bool IsPermittedInterrupt(u32 id) const {
|
||||
constexpr size_t BitsPerWord = BITSIZEOF(this->irq_access_flags[0]);
|
||||
if (id < BITSIZEOF(this->irq_access_flags)) {
|
||||
return (this->irq_access_flags[id / BitsPerWord] & (1ul << (id % BitsPerWord))) != 0;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
constexpr bool IsPermittedDebug() const {
|
||||
return this->debug_capabilities.Get<DebugFlags::AllowDebug>();
|
||||
}
|
||||
|
||||
constexpr bool CanForceDebug() const {
|
||||
return this->debug_capabilities.Get<DebugFlags::ForceDebug>();
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -20,6 +20,10 @@
|
|||
namespace ams::kern {
|
||||
|
||||
class KPort;
|
||||
class KSession;
|
||||
class KClientSession;
|
||||
class KLightSession;
|
||||
class KLightClientSession;
|
||||
|
||||
class KClientPort final : public KSynchronizationObject {
|
||||
MESOSPHERE_AUTOOBJECT_TRAITS(KClientPort, KSynchronizationObject);
|
||||
|
@ -33,6 +37,8 @@ namespace ams::kern {
|
|||
virtual ~KClientPort() { /* ... */ }
|
||||
|
||||
void Initialize(KPort *parent, s32 max_sessions);
|
||||
void OnSessionFinalized();
|
||||
void OnServerClosed();
|
||||
|
||||
constexpr const KPort *GetParent() const { return this->parent; }
|
||||
|
||||
|
@ -42,7 +48,8 @@ namespace ams::kern {
|
|||
virtual void Destroy() override;
|
||||
virtual bool IsSignaled() const override;
|
||||
|
||||
/* TODO: More of KClientPort. */
|
||||
Result CreateSession(KClientSession **out);
|
||||
Result CreateLightSession(KLightClientSession **out);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -34,11 +34,15 @@ namespace ams::kern {
|
|||
this->parent = parent;
|
||||
}
|
||||
|
||||
virtual void Destroy() override;
|
||||
static void PostDestroy(uintptr_t arg) { /* ... */ }
|
||||
|
||||
constexpr const KSession *GetParent() const { return this->parent; }
|
||||
constexpr KSession *GetParent() const { return this->parent; }
|
||||
|
||||
/* TODO: More of KClientSession. */
|
||||
Result SendSyncRequest(uintptr_t address, size_t size);
|
||||
Result SendAsyncRequest(KWritableEvent *event, uintptr_t address, size_t size);
|
||||
|
||||
void OnServerClosed();
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -22,8 +22,35 @@ namespace ams::kern {
|
|||
|
||||
class KCodeMemory final : public KAutoObjectWithSlabHeapAndContainer<KCodeMemory, KAutoObjectWithList> {
|
||||
MESOSPHERE_AUTOOBJECT_TRAITS(KCodeMemory, KAutoObject);
|
||||
private:
|
||||
TYPED_STORAGE(KPageGroup) page_group;
|
||||
KProcess *owner;
|
||||
KProcessAddress address;
|
||||
KLightLock lock;
|
||||
bool is_initialized;
|
||||
bool is_owner_mapped;
|
||||
bool is_mapped;
|
||||
public:
|
||||
/* TODO: This is a placeholder definition. */
|
||||
explicit KCodeMemory() : owner(nullptr), address(Null<KProcessAddress>), is_initialized(false), is_owner_mapped(false), is_mapped(false) {
|
||||
/* ... */
|
||||
}
|
||||
|
||||
virtual ~KCodeMemory() { /* ... */ }
|
||||
|
||||
Result Initialize(KProcessAddress address, size_t size);
|
||||
virtual void Finalize() override;
|
||||
|
||||
Result Map(KProcessAddress address, size_t size);
|
||||
Result Unmap(KProcessAddress address, size_t size);
|
||||
Result MapToOwner(KProcessAddress address, size_t size, ams::svc::MemoryPermission perm);
|
||||
Result UnmapFromOwner(KProcessAddress address, size_t size);
|
||||
|
||||
virtual bool IsInitialized() const override { return this->is_initialized; }
|
||||
static void PostDestroy(uintptr_t arg) { /* ... */ }
|
||||
|
||||
KProcess *GetOwner() const { return this->owner; }
|
||||
KProcessAddress GetSourceAddress() { return this->address; }
|
||||
size_t GetSize() const { return this->is_initialized ? GetReference(this->page_group).GetNumPages() * PageSize : 0; }
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -20,26 +20,11 @@
|
|||
|
||||
namespace ams::kern {
|
||||
|
||||
struct KConditionVariableComparator {
|
||||
static constexpr ALWAYS_INLINE int Compare(const KThread &lhs, const KThread &rhs) {
|
||||
const uintptr_t l_key = lhs.GetConditionVariableKey();
|
||||
const uintptr_t r_key = rhs.GetConditionVariableKey();
|
||||
|
||||
if (l_key < r_key) {
|
||||
/* Sort first by key */
|
||||
return -1;
|
||||
} else if (l_key == r_key && lhs.GetPriority() < rhs.GetPriority()) {
|
||||
/* And then by priority. */
|
||||
return -1;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
};
|
||||
extern KThread g_cv_arbiter_compare_thread;
|
||||
|
||||
class KConditionVariable {
|
||||
public:
|
||||
using ThreadTree = util::IntrusiveRedBlackTreeMemberTraits<&KThread::condvar_arbiter_tree_node>::TreeType<KConditionVariableComparator>;
|
||||
using ThreadTree = typename KThread::ConditionVariableThreadTreeType;
|
||||
private:
|
||||
ThreadTree tree;
|
||||
public:
|
||||
|
@ -52,18 +37,20 @@ namespace ams::kern {
|
|||
/* Condition variable. */
|
||||
void Signal(uintptr_t cv_key, s32 count);
|
||||
Result Wait(KProcessAddress addr, uintptr_t key, u32 value, s64 timeout);
|
||||
|
||||
ALWAYS_INLINE void BeforeUpdatePriority(KThread *thread) {
|
||||
MESOSPHERE_ASSERT(KScheduler::IsSchedulerLockedByCurrentThread());
|
||||
|
||||
this->tree.erase(this->tree.iterator_to(*thread));
|
||||
}
|
||||
|
||||
ALWAYS_INLINE void AfterUpdatePriority(KThread *thread) {
|
||||
MESOSPHERE_ASSERT(KScheduler::IsSchedulerLockedByCurrentThread());
|
||||
|
||||
this->tree.insert(*thread);
|
||||
}
|
||||
private:
|
||||
KThread *SignalImpl(KThread *thread);
|
||||
};
|
||||
|
||||
ALWAYS_INLINE void BeforeUpdatePriority(KConditionVariable::ThreadTree *tree, KThread *thread) {
|
||||
MESOSPHERE_ASSERT(KScheduler::IsSchedulerLockedByCurrentThread());
|
||||
|
||||
tree->erase(tree->iterator_to(*thread));
|
||||
}
|
||||
|
||||
ALWAYS_INLINE void AfterUpdatePriority(KConditionVariable::ThreadTree *tree, KThread *thread) {
|
||||
MESOSPHERE_ASSERT(KScheduler::IsSchedulerLockedByCurrentThread());
|
||||
|
||||
tree->insert(*thread);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -30,6 +30,7 @@ namespace ams::kern {
|
|||
KInterruptTaskManager *interrupt_task_manager;
|
||||
s32 core_id;
|
||||
void *exception_stack_top;
|
||||
ams::svc::ThreadLocalRegion *tlr;
|
||||
};
|
||||
static_assert(std::is_standard_layout<KCurrentContext>::value && std::is_trivially_destructible<KCurrentContext>::value);
|
||||
static_assert(sizeof(KCurrentContext) <= cpu::DataCacheLineSize);
|
||||
|
@ -80,6 +81,10 @@ namespace ams::kern {
|
|||
return impl::GetCurrentContext().core_id;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE ams::svc::ThreadLocalRegion *GetCurrentThreadLocalRegion() {
|
||||
return impl::GetCurrentContext().tlr;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE void SetCurrentThread(KThread *new_thread) {
|
||||
impl::GetCurrentContext().current_thread = new_thread;
|
||||
}
|
||||
|
@ -88,4 +93,8 @@ namespace ams::kern {
|
|||
impl::GetCurrentContext().current_process = new_process;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE void SetCurrentThreadLocalRegion(void *address) {
|
||||
impl::GetCurrentContext().tlr = static_cast<ams::svc::ThreadLocalRegion *>(address);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -16,12 +16,68 @@
|
|||
#pragma once
|
||||
#include <mesosphere/kern_common.hpp>
|
||||
#include <mesosphere/kern_k_synchronization_object.hpp>
|
||||
#include <mesosphere/kern_k_process.hpp>
|
||||
#include <mesosphere/kern_k_event_info.hpp>
|
||||
#include <mesosphere/kern_k_light_lock.hpp>
|
||||
|
||||
namespace ams::kern {
|
||||
|
||||
class KDebugBase : public KSynchronizationObject {
|
||||
protected:
|
||||
using DebugEventList = util::IntrusiveListBaseTraits<KEventInfo>::ListType;
|
||||
private:
|
||||
DebugEventList event_info_list;
|
||||
u32 continue_flags;
|
||||
KProcess *process;
|
||||
KLightLock lock;
|
||||
KProcess::State old_process_state;
|
||||
public:
|
||||
/* TODO: This is a placeholder definition. */
|
||||
explicit KDebugBase() { /* ... */ }
|
||||
virtual ~KDebugBase() { /* ... */ }
|
||||
protected:
|
||||
bool Is64Bit() const;
|
||||
public:
|
||||
void Initialize();
|
||||
|
||||
Result Attach(KProcess *process);
|
||||
Result BreakProcess();
|
||||
Result TerminateProcess();
|
||||
|
||||
Result ContinueDebug(const u32 flags, const u64 *thread_ids, size_t num_thread_ids);
|
||||
|
||||
Result QueryMemoryInfo(ams::svc::MemoryInfo *out_memory_info, ams::svc::PageInfo *out_page_info, KProcessAddress address);
|
||||
Result ReadMemory(KProcessAddress buffer, KProcessAddress address, size_t size);
|
||||
Result WriteMemory(KProcessAddress buffer, KProcessAddress address, size_t size);
|
||||
|
||||
Result GetThreadContext(ams::svc::ThreadContext *out, u64 thread_id, u32 context_flags);
|
||||
Result SetThreadContext(const ams::svc::ThreadContext &ctx, u64 thread_id, u32 context_flags);
|
||||
|
||||
virtual Result GetThreadContextImpl(ams::svc::ThreadContext *out, KThread *thread, u32 context_flags) = 0;
|
||||
virtual Result SetThreadContextImpl(const ams::svc::ThreadContext &ctx, KThread *thread, u32 context_flags) = 0;
|
||||
|
||||
Result GetRunningThreadInfo(ams::svc::LastThreadContext *out_context, u64 *out_thread_id);
|
||||
|
||||
Result GetDebugEventInfo(ams::svc::lp64::DebugEventInfo *out);
|
||||
Result GetDebugEventInfo(ams::svc::ilp32::DebugEventInfo *out);
|
||||
|
||||
KScopedAutoObject<KProcess> GetProcess();
|
||||
private:
|
||||
void PushDebugEvent(ams::svc::DebugEvent event, uintptr_t param0 = 0, uintptr_t param1 = 0, uintptr_t param2 = 0, uintptr_t param3 = 0, uintptr_t param4 = 0);
|
||||
void EnqueueDebugEventInfo(KEventInfo *info);
|
||||
|
||||
template<typename T> requires (std::same_as<T, ams::svc::lp64::DebugEventInfo> || std::same_as<T, ams::svc::ilp32::DebugEventInfo>)
|
||||
Result GetDebugEventInfoImpl(T *out);
|
||||
public:
|
||||
virtual void OnFinalizeSynchronizationObject() override;
|
||||
virtual bool IsSignaled() const override;
|
||||
private:
|
||||
static Result ProcessDebugEvent(ams::svc::DebugEvent event, uintptr_t param0, uintptr_t param1, uintptr_t param2, uintptr_t param3, uintptr_t param4);
|
||||
public:
|
||||
static Result OnDebugEvent(ams::svc::DebugEvent event, uintptr_t param0 = 0, uintptr_t param1 = 0, uintptr_t param2 = 0, uintptr_t param3 = 0, uintptr_t param4 = 0);
|
||||
static Result OnExitProcess(KProcess *process);
|
||||
static Result OnTerminateProcess(KProcess *process);
|
||||
static Result OnExitThread(KThread *thread);
|
||||
static KEventInfo *CreateDebugEvent(ams::svc::DebugEvent event, uintptr_t param0, uintptr_t param1, uintptr_t param2, uintptr_t param3, uintptr_t param4, u64 thread_id);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -23,10 +23,39 @@ namespace ams::kern {
|
|||
|
||||
class KDeviceAddressSpace final : public KAutoObjectWithSlabHeapAndContainer<KDeviceAddressSpace, KAutoObjectWithList> {
|
||||
MESOSPHERE_AUTOOBJECT_TRAITS(KDeviceAddressSpace, KAutoObject);
|
||||
private:
|
||||
KLightLock lock;
|
||||
KDevicePageTable table;
|
||||
u64 space_address;
|
||||
u64 space_size;
|
||||
bool is_initialized;
|
||||
public:
|
||||
constexpr KDeviceAddressSpace() : lock(), table(), space_address(), space_size(), is_initialized() { /* ... */ }
|
||||
virtual ~KDeviceAddressSpace() { /* ... */ }
|
||||
|
||||
Result Initialize(u64 address, u64 size);
|
||||
virtual void Finalize() override;
|
||||
|
||||
virtual bool IsInitialized() const override { return this->is_initialized; }
|
||||
static void PostDestroy(uintptr_t arg) { /* ... */ }
|
||||
|
||||
Result Attach(ams::svc::DeviceName device_name);
|
||||
Result Detach(ams::svc::DeviceName device_name);
|
||||
|
||||
Result Map(size_t *out_mapped_size, KProcessPageTable *page_table, KProcessAddress process_address, size_t size, u64 device_address, ams::svc::MemoryPermission device_perm, bool refresh_mappings) {
|
||||
return this->Map(out_mapped_size, page_table, process_address, size, device_address, device_perm, false, refresh_mappings);
|
||||
}
|
||||
|
||||
Result MapAligned(KProcessPageTable *page_table, KProcessAddress process_address, size_t size, u64 device_address, ams::svc::MemoryPermission device_perm) {
|
||||
size_t dummy;
|
||||
return this->Map(std::addressof(dummy), page_table, process_address, size, device_address, device_perm, true, false);
|
||||
}
|
||||
|
||||
Result Unmap(KProcessPageTable *page_table, KProcessAddress process_address, size_t size, u64 device_address);
|
||||
private:
|
||||
Result Map(size_t *out_mapped_size, KProcessPageTable *page_table, KProcessAddress process_address, size_t size, u64 device_address, ams::svc::MemoryPermission device_perm, bool is_aligned, bool refresh_mappings);
|
||||
public:
|
||||
static void Initialize();
|
||||
|
||||
/* TODO: This is a placeholder definition. */
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -18,13 +18,38 @@
|
|||
#include <mesosphere/kern_k_auto_object.hpp>
|
||||
#include <mesosphere/kern_slab_helpers.hpp>
|
||||
#include <mesosphere/kern_k_readable_event.hpp>
|
||||
#include <mesosphere/kern_k_writable_event.hpp>
|
||||
|
||||
namespace ams::kern {
|
||||
|
||||
class KEvent final : public KAutoObjectWithSlabHeapAndContainer<KEvent, KAutoObjectWithList> {
|
||||
MESOSPHERE_AUTOOBJECT_TRAITS(KEvent, KAutoObject);
|
||||
private:
|
||||
KReadableEvent readable_event;
|
||||
KWritableEvent writable_event;
|
||||
KProcess *owner;
|
||||
bool initialized;
|
||||
public:
|
||||
/* TODO: This is a placeholder definition. */
|
||||
constexpr KEvent()
|
||||
: readable_event(), writable_event(), owner(), initialized()
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
virtual ~KEvent() { /* ... */ }
|
||||
|
||||
void Initialize();
|
||||
virtual void Finalize() override;
|
||||
|
||||
virtual bool IsInitialized() const override { return this->initialized; }
|
||||
virtual uintptr_t GetPostDestroyArgument() const override { return reinterpret_cast<uintptr_t>(this->owner); }
|
||||
|
||||
static void PostDestroy(uintptr_t arg);
|
||||
|
||||
virtual KProcess *GetOwner() const override { return this->owner; }
|
||||
|
||||
KReadableEvent &GetReadableEvent() { return this->readable_event; }
|
||||
KWritableEvent &GetWritableEvent() { return this->writable_event; }
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -21,7 +21,50 @@ namespace ams::kern {
|
|||
|
||||
class KEventInfo : public KSlabAllocated<KEventInfo>, public util::IntrusiveListBaseNode<KEventInfo> {
|
||||
public:
|
||||
/* TODO: This is a placeholder definition. */
|
||||
struct InfoCreateThread {
|
||||
u32 thread_id;
|
||||
uintptr_t tls_address;
|
||||
uintptr_t entrypoint;
|
||||
};
|
||||
|
||||
struct InfoExitProcess {
|
||||
ams::svc::ProcessExitReason reason;
|
||||
};
|
||||
|
||||
struct InfoExitThread {
|
||||
ams::svc::ThreadExitReason reason;
|
||||
};
|
||||
|
||||
struct InfoException {
|
||||
ams::svc::DebugException exception_type;
|
||||
s32 exception_data_count;
|
||||
uintptr_t exception_address;
|
||||
uintptr_t exception_data[4];
|
||||
};
|
||||
|
||||
struct InfoSystemCall {
|
||||
s64 tick;
|
||||
s32 id;
|
||||
};
|
||||
public:
|
||||
ams::svc::DebugEvent event;
|
||||
u32 thread_id;
|
||||
u32 flags;
|
||||
bool is_attached;
|
||||
bool continue_flag;
|
||||
bool ignore_continue;
|
||||
bool close_once;
|
||||
union {
|
||||
InfoCreateThread create_thread;
|
||||
InfoExitProcess exit_process;
|
||||
InfoExitThread exit_thread;
|
||||
InfoException exception;
|
||||
InfoSystemCall system_call;
|
||||
} info;
|
||||
KThread *debug_thread;
|
||||
public:
|
||||
explicit KEventInfo() : is_attached(), continue_flag(), ignore_continue() { /* ... */ }
|
||||
~KEventInfo() { /* ... */ }
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -126,20 +126,7 @@ namespace ams::kern {
|
|||
NOINLINE bool Remove(ams::svc::Handle handle);
|
||||
|
||||
template<typename T = KAutoObject>
|
||||
ALWAYS_INLINE KScopedAutoObject<T> GetObject(ams::svc::Handle handle) const {
|
||||
MESOSPHERE_ASSERT_THIS();
|
||||
|
||||
/* Handle pseudo-handles. */
|
||||
if constexpr (std::is_base_of<T, KProcess>::value) {
|
||||
if (handle == ams::svc::PseudoHandle::CurrentProcess) {
|
||||
return GetCurrentProcessPointer();
|
||||
}
|
||||
} else if constexpr (std::is_base_of<T, KThread>::value) {
|
||||
if (handle == ams::svc::PseudoHandle::CurrentThread) {
|
||||
return GetCurrentThreadPointer();
|
||||
}
|
||||
}
|
||||
|
||||
ALWAYS_INLINE KScopedAutoObject<T> GetObjectWithoutPseudoHandle(ams::svc::Handle handle) const {
|
||||
/* Lock and look up in table. */
|
||||
KScopedDisableDispatch dd;
|
||||
KScopedSpinLock lk(this->lock);
|
||||
|
@ -147,25 +134,33 @@ namespace ams::kern {
|
|||
if constexpr (std::is_same<T, KAutoObject>::value) {
|
||||
return this->GetObjectImpl(handle);
|
||||
} else {
|
||||
return this->GetObjectImpl(handle)->DynamicCast<T*>();
|
||||
if (auto *obj = this->GetObjectImpl(handle); obj != nullptr) {
|
||||
return obj->DynamicCast<T*>();
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T = KAutoObject>
|
||||
ALWAYS_INLINE KScopedAutoObject<T> GetObjectForIpc(ams::svc::Handle handle) const {
|
||||
static_assert(!std::is_base_of<KInterruptEvent, T>::value);
|
||||
ALWAYS_INLINE KScopedAutoObject<T> GetObject(ams::svc::Handle handle) const {
|
||||
MESOSPHERE_ASSERT_THIS();
|
||||
|
||||
/* Handle pseudo-handles. */
|
||||
if constexpr (std::is_base_of<T, KProcess>::value) {
|
||||
if constexpr (std::derived_from<KProcess, T>) {
|
||||
if (handle == ams::svc::PseudoHandle::CurrentProcess) {
|
||||
return GetCurrentProcessPointer();
|
||||
}
|
||||
} else if constexpr (std::is_base_of<T, KThread>::value) {
|
||||
} else if constexpr (std::derived_from<KThread, T>) {
|
||||
if (handle == ams::svc::PseudoHandle::CurrentThread) {
|
||||
return GetCurrentThreadPointer();
|
||||
}
|
||||
}
|
||||
|
||||
return this->template GetObjectWithoutPseudoHandle<T>(handle);
|
||||
}
|
||||
|
||||
ALWAYS_INLINE KScopedAutoObject<KAutoObject> GetObjectForIpcWithoutPseudoHandle(ams::svc::Handle handle) const {
|
||||
/* Lock and look up in table. */
|
||||
KScopedDisableDispatch dd;
|
||||
KScopedSpinLock lk(this->lock);
|
||||
|
@ -174,11 +169,20 @@ namespace ams::kern {
|
|||
if (obj->DynamicCast<KInterruptEvent *>() != nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
if constexpr (std::is_same<T, KAutoObject>::value) {
|
||||
return obj;
|
||||
} else {
|
||||
return obj->DynamicCast<T*>();
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE KScopedAutoObject<KAutoObject> GetObjectForIpc(ams::svc::Handle handle, KThread *cur_thread) const {
|
||||
/* Handle pseudo-handles. */
|
||||
if (handle == ams::svc::PseudoHandle::CurrentProcess) {
|
||||
return static_cast<KAutoObject *>(static_cast<void *>(cur_thread->GetOwnerProcess()));
|
||||
}
|
||||
if (handle == ams::svc::PseudoHandle::CurrentThread) {
|
||||
return static_cast<KAutoObject *>(cur_thread);
|
||||
}
|
||||
|
||||
return GetObjectForIpcWithoutPseudoHandle(handle);
|
||||
}
|
||||
|
||||
ALWAYS_INLINE KScopedAutoObject<KAutoObject> GetObjectByIndex(ams::svc::Handle *out_handle, size_t index) const {
|
||||
|
@ -203,6 +207,49 @@ namespace ams::kern {
|
|||
static_assert(std::is_base_of<KAutoObject, T>::value);
|
||||
return this->Register(handle, obj, obj->GetTypeObj().GetClassToken());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
ALWAYS_INLINE bool GetMultipleObjects(T **out, const ams::svc::Handle *handles, size_t num_handles) const {
|
||||
/* Try to convert and open all the handles. */
|
||||
size_t num_opened;
|
||||
{
|
||||
/* Lock the table. */
|
||||
KScopedDisableDispatch dd;
|
||||
KScopedSpinLock lk(this->lock);
|
||||
for (num_opened = 0; num_opened < num_handles; num_opened++) {
|
||||
/* Get the current handle. */
|
||||
const auto cur_handle = handles[num_opened];
|
||||
|
||||
/* Get the object for the current handle. */
|
||||
KAutoObject *cur_object = this->GetObjectImpl(cur_handle);
|
||||
if (AMS_UNLIKELY(cur_object == nullptr)) {
|
||||
break;
|
||||
}
|
||||
|
||||
/* Cast the current object to the desired type. */
|
||||
T *cur_t = cur_object->DynamicCast<T*>();
|
||||
if (AMS_UNLIKELY(cur_t == nullptr)) {
|
||||
break;
|
||||
}
|
||||
|
||||
/* Open a reference to the current object. */
|
||||
cur_t->Open();
|
||||
out[num_opened] = cur_t;
|
||||
}
|
||||
}
|
||||
|
||||
/* If we converted every object, succeed. */
|
||||
if (AMS_LIKELY(num_opened == num_handles)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/* If we didn't convert entry object, close the ones we opened. */
|
||||
for (size_t i = 0; i < num_opened; i++) {
|
||||
out[i]->Close();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
private:
|
||||
NOINLINE Result Add(ams::svc::Handle *out_handle, KAutoObject *obj, u16 type);
|
||||
NOINLINE void Register(ams::svc::Handle handle, KAutoObject *obj, u16 type);
|
||||
|
@ -310,49 +357,6 @@ namespace ams::kern {
|
|||
*out_handle = EncodeHandle(index, entry->GetLinearId());
|
||||
return entry->GetObject();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
ALWAYS_INLINE bool GetMultipleObjects(T **out, const ams::svc::Handle *handles, size_t num_handles) const {
|
||||
/* Try to convert and open all the handles. */
|
||||
size_t num_opened;
|
||||
{
|
||||
/* Lock the table. */
|
||||
KScopedDisableDispatch dd;
|
||||
KScopedSpinLock lk(this->lock);
|
||||
for (num_opened = 0; num_opened < num_handles; num_opened++) {
|
||||
/* Get the current handle. */
|
||||
const auto cur_handle = handles[num_opened];
|
||||
|
||||
/* Get the object for the current handle. */
|
||||
KAutoObject *cur_object = this->GetObjectImpl(cur_handle);
|
||||
if (AMS_UNLIKELY(cur_object == nullptr)) {
|
||||
break;
|
||||
}
|
||||
|
||||
/* Cast the current object to the desired type. */
|
||||
T *cur_t = cur_object->DynamicCast<T*>();
|
||||
if (AMS_UNLIKELY(cur_t == nullptr)) {
|
||||
break;
|
||||
}
|
||||
|
||||
/* Open a reference to the current object. */
|
||||
cur_t->Open();
|
||||
out[num_opened] = cur_t;
|
||||
}
|
||||
}
|
||||
|
||||
/* If we converted every object, succeed. */
|
||||
if (AMS_LIKELY(num_opened == num_handles)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/* If we didn't convert entry object, close the ones we opened. */
|
||||
for (size_t i = 0; i < num_opened; i++) {
|
||||
out[i]->Close();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -26,13 +26,40 @@ namespace ams::kern {
|
|||
|
||||
class KInterruptEvent final : public KAutoObjectWithSlabHeapAndContainer<KInterruptEvent, KReadableEvent> {
|
||||
MESOSPHERE_AUTOOBJECT_TRAITS(KInterruptEvent, KReadableEvent);
|
||||
private:
|
||||
KInterruptEventTask *task;
|
||||
s32 interrupt_id;
|
||||
bool is_initialized;
|
||||
public:
|
||||
/* TODO: This is a placeholder definition. */
|
||||
constexpr KInterruptEvent() : task(nullptr), interrupt_id(-1), is_initialized(false) { /* ... */ }
|
||||
virtual ~KInterruptEvent() { /* ... */ }
|
||||
|
||||
Result Initialize(int32_t interrupt_name, ams::svc::InterruptType type);
|
||||
virtual void Finalize() override;
|
||||
|
||||
virtual Result Reset() override;
|
||||
|
||||
virtual bool IsInitialized() const override { return this->is_initialized; }
|
||||
|
||||
static void PostDestroy(uintptr_t arg) { /* ... */ }
|
||||
|
||||
constexpr s32 GetInterruptId() const { return this->interrupt_id; }
|
||||
};
|
||||
|
||||
class KInterruptEventTask : public KSlabAllocated<KInterruptEventTask>, public KInterruptTask {
|
||||
private:
|
||||
KInterruptEvent *event;
|
||||
s32 interrupt_id;
|
||||
public:
|
||||
/* TODO: This is a placeholder definition. */
|
||||
constexpr KInterruptEventTask() : event(nullptr), interrupt_id(-1) { /* ... */ }
|
||||
~KInterruptEventTask() { /* ... */ }
|
||||
|
||||
virtual KInterruptTask *OnInterrupt(s32 interrupt_id) override;
|
||||
virtual void DoTask() override;
|
||||
|
||||
void Unregister();
|
||||
public:
|
||||
static Result Register(KInterruptEventTask **out, s32 interrupt_id, bool level, KInterruptEvent *event);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -49,8 +49,6 @@ namespace ams::kern {
|
|||
|
||||
NOINLINE void Initialize();
|
||||
void EnqueueTask(KInterruptTask *task);
|
||||
|
||||
/* TODO: Actually implement KInterruptTaskManager. This is a placeholder. */
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -34,11 +34,14 @@ namespace ams::kern {
|
|||
this->parent = parent;
|
||||
}
|
||||
|
||||
virtual void Destroy() override;
|
||||
static void PostDestroy(uintptr_t arg) { /* ... */ }
|
||||
|
||||
constexpr const KLightSession *GetParent() const { return this->parent; }
|
||||
|
||||
/* TODO: More of KLightClientSession. */
|
||||
Result SendSyncRequest(u32 *data);
|
||||
|
||||
void OnServerClosed();
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -31,15 +31,18 @@ namespace ams::kern {
|
|||
MESOSPHERE_ASSERT_THIS();
|
||||
|
||||
const uintptr_t cur_thread = reinterpret_cast<uintptr_t>(GetCurrentThreadPointer());
|
||||
const uintptr_t cur_thread_tag = (cur_thread | 1);
|
||||
|
||||
while (true) {
|
||||
uintptr_t old_tag = this->tag.load(std::memory_order_relaxed);
|
||||
|
||||
while (!this->tag.compare_exchange_weak(old_tag, (old_tag == 0) ? cur_thread : old_tag | 1, std::memory_order_acquire)) {
|
||||
/* ... */
|
||||
if ((old_tag | 1) == cur_thread_tag) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if ((old_tag == 0) || ((old_tag | 1) == (cur_thread | 1))) {
|
||||
if ((old_tag == 0) || ((old_tag | 1) == cur_thread_tag)) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -52,9 +55,11 @@ namespace ams::kern {
|
|||
|
||||
const uintptr_t cur_thread = reinterpret_cast<uintptr_t>(GetCurrentThreadPointer());
|
||||
uintptr_t expected = cur_thread;
|
||||
if (!this->tag.compare_exchange_weak(expected, 0, std::memory_order_release)) {
|
||||
this->UnlockSlowPath(cur_thread);
|
||||
}
|
||||
do {
|
||||
if (expected != cur_thread) {
|
||||
return this->UnlockSlowPath(cur_thread);
|
||||
}
|
||||
} while (!this->tag.compare_exchange_weak(expected, 0, std::memory_order_release));
|
||||
}
|
||||
|
||||
void LockSlowPath(uintptr_t owner, uintptr_t cur_thread);
|
||||
|
|
|
@ -35,13 +35,22 @@ namespace ams::kern {
|
|||
constexpr KLightServerSession() : parent(), request_queue(), server_queue(), current_request(), server_thread() { /* ... */ }
|
||||
virtual ~KLightServerSession() { /* ... */ }
|
||||
|
||||
void Initialize(KLightSession *parent);
|
||||
void Initialize(KLightSession *parent) {
|
||||
/* Set member variables. */
|
||||
this->parent = parent;
|
||||
}
|
||||
|
||||
virtual void Destroy() override;
|
||||
static void PostDestroy(uintptr_t arg) { /* ... */ }
|
||||
|
||||
constexpr const KLightSession *GetParent() const { return this->parent; }
|
||||
|
||||
/* TODO: More of KLightServerSession. */
|
||||
Result OnRequest(KThread *request_thread);
|
||||
Result ReplyAndReceive(u32 *data);
|
||||
|
||||
void OnClientClosed();
|
||||
private:
|
||||
void CleanupRequests();
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Reference in a new issue