From 7b6050a0cb44e5c2c59d05975dd23dfd5e3e92ec Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Thu, 20 Jun 2019 00:57:17 -0700 Subject: [PATCH] boot: refactor for R_TRY --- .../boot/source/boot_battery_driver.cpp | 161 ++----- stratosphere/boot/source/boot_calibration.cpp | 26 +- .../boot/source/boot_charger_driver.cpp | 78 +--- stratosphere/boot/source/boot_i2c_utils.cpp | 14 +- stratosphere/boot/source/boot_pmic_driver.cpp | 11 +- stratosphere/boot/source/boot_rtc_driver.cpp | 5 +- .../boot/source/i2c_driver/i2c_api.cpp | 15 +- .../source/i2c_driver/i2c_bus_accessor.cpp | 28 +- .../source/i2c_driver/i2c_command_list.cpp | 9 +- .../source/i2c_driver/i2c_driver_session.cpp | 50 +-- .../boot/source/updater/updater_api.cpp | 402 +++++------------- .../source/updater/updater_bis_management.cpp | 33 +- .../source/updater/updater_bis_management.hpp | 5 +- .../boot/source/updater/updater_bis_save.cpp | 5 +- .../dmnt/source/dmnt_cheat_manager.cpp | 2 +- stratosphere/libstratosphere | 2 +- 16 files changed, 223 insertions(+), 623 deletions(-) diff --git a/stratosphere/boot/source/boot_battery_driver.cpp b/stratosphere/boot/source/boot_battery_driver.cpp index 7416e4789..4bbcd966c 100644 --- a/stratosphere/boot/source/boot_battery_driver.cpp +++ b/stratosphere/boot/source/boot_battery_driver.cpp @@ -53,16 +53,12 @@ Result BatteryDriver::Write(u8 addr, u16 val) { } Result BatteryDriver::ReadWrite(u8 addr, u16 mask, u16 val) { - Result rc; u16 cur_val; - if (R_FAILED((rc = this->Read(addr, &cur_val)))) { - return rc; - } + R_TRY(this->Read(addr, &cur_val)); const u16 new_val = (cur_val & ~mask) | val; - if (R_FAILED((rc = this->Write(addr, new_val)))) { - return rc; - } + R_TRY(this->Write(addr, new_val)); + return ResultSuccess; } @@ -93,35 +89,21 @@ Result BatteryDriver::UnlockVfSoc() { } Result BatteryDriver::LockModelTable() { - Result rc; - if (R_FAILED((rc = this->Write(Max17050ModelAccess0, 0x0000)))) { - return rc; - } - if (R_FAILED((rc = this->Write(Max17050ModelAccess1, 0x0000)))) { - return rc; - } + R_TRY(this->Write(Max17050ModelAccess0, 0x0000)); + R_TRY(this->Write(Max17050ModelAccess1, 0x0000)); return ResultSuccess; } Result BatteryDriver::UnlockModelTable() { - Result rc; - if (R_FAILED((rc = this->Write(Max17050ModelAccess0, 0x0059)))) { - return rc; - } - if (R_FAILED((rc = this->Write(Max17050ModelAccess1, 0x00C4)))) { - return rc; - } + R_TRY(this->Write(Max17050ModelAccess0, 0x0059)); + R_TRY(this->Write(Max17050ModelAccess1, 0x00C4)); return ResultSuccess; } Result BatteryDriver::SetModelTable(const u16 *model_table) { - Result rc; for (size_t i = 0; i < Max17050ModelChrTblSize; i++) { - if (R_FAILED((rc = this->Write(Max17050ModelChrTblStart + i, model_table[i])))) { - return rc; - } + R_TRY(this->Write(Max17050ModelChrTblStart + i, model_table[i])); } - return ResultSuccess; } @@ -151,52 +133,31 @@ bool BatteryDriver::IsModelTableSet(const u16 *model_table) { Result BatteryDriver::InitializeBatteryParameters() { const Max17050Parameters *params = GetBatteryParameters(); - Result rc = ResultSuccess; if (IsPowerOnReset()) { /* Do initial config. */ - if (R_FAILED((rc = this->ReadWrite(Max17050MiscCfg, 0x8000, 0x8000)))) { - return rc; - } + R_TRY(this->ReadWrite(Max17050MiscCfg, 0x8000, 0x8000)); svcSleepThread(500'000'000ul); - if (R_FAILED((rc = this->Write(Max17050Config, 0x7210)))) { - return rc; - } - if (R_FAILED((rc = this->Write(Max17050FilterCfg, 0x8784)))) { - return rc; - } - if (R_FAILED((rc = this->Write(Max17050RelaxCfg, params->relaxcfg)))) { - return rc; - } - if (R_FAILED((rc = this->Write(Max17050LearnCfg, 0x2603)))) { - return rc; - } - if (R_FAILED((rc = this->Write(Max17050FullSocThr, params->fullsocthr)))) { - return rc; - } - if (R_FAILED((rc = this->Write(Max17050IAvgEmpty, params->iavgempty)))) { - return rc; - } + R_TRY(this->Write(Max17050Config, 0x7210)); + R_TRY(this->Write(Max17050FilterCfg, 0x8784)); + R_TRY(this->Write(Max17050RelaxCfg, params->relaxcfg)); + R_TRY(this->Write(Max17050LearnCfg, 0x2603)); + R_TRY(this->Write(Max17050FullSocThr, params->fullsocthr)); + R_TRY(this->Write(Max17050IAvgEmpty, params->iavgempty)); /* Unlock model table, write model table. */ do { - if (R_FAILED((rc = this->UnlockModelTable()))) { - return rc; - } - if (R_FAILED((rc = this->SetModelTable(params->modeltbl)))) { - return rc; - } + R_TRY(this->UnlockModelTable()); + R_TRY(this->SetModelTable(params->modeltbl)); } while (!this->IsModelTableSet(params->modeltbl)); /* Lock model table. */ size_t lock_i = 0; while (true) { lock_i++; - if (R_FAILED((rc = this->LockModelTable()))) { - return rc; - } + R_TRY(this->LockModelTable()); if (this->IsModelTableLocked()) { break; @@ -212,15 +173,9 @@ Result BatteryDriver::InitializeBatteryParameters() { while (!this->WriteValidate(Max17050RComp0, params->rcomp0)) { /* ... */ } while (!this->WriteValidate(Max17050TempCo, params->tempco)) { /* ... */ } - if (R_FAILED((rc = this->Write(Max17050IChgTerm, params->ichgterm)))) { - return rc; - } - if (R_FAILED((rc = this->Write(Max17050TGain, params->tgain)))) { - return rc; - } - if (R_FAILED((rc = this->Write(Max17050TOff, params->toff)))) { - return rc; - } + R_TRY(this->Write(Max17050IChgTerm, params->ichgterm)); + R_TRY(this->Write(Max17050TGain, params->tgain)); + R_TRY(this->Write(Max17050TOff, params->toff)); while (!this->WriteValidate(Max17050VEmpty, params->vempty)) { /* ... */ } while (!this->WriteValidate(Max17050QResidual00, params->qresidual00)) { /* ... */ } @@ -231,9 +186,7 @@ Result BatteryDriver::InitializeBatteryParameters() { /* Write full capacity parameters. */ while (!this->WriteValidate(Max17050FullCap, params->fullcap)) { /* ... */ } - if (R_FAILED((rc = this->Write(Max17050DesignCap, params->vffullcap)))) { - return rc; - } + R_TRY(this->Write(Max17050DesignCap, params->vffullcap)); while (!this->WriteValidate(Max17050FullCapNom, params->vffullcap)) { /* ... */ } svcSleepThread(350'000'000ul); @@ -241,24 +194,12 @@ Result BatteryDriver::InitializeBatteryParameters() { /* Write VFSOC to VFSOC 0. */ u16 vfsoc, qh; { - if (R_FAILED((rc = this->Read(Max17050SocVf, &vfsoc)))) { - return rc; - } - if (R_FAILED((rc = this->UnlockVfSoc()))) { - return rc; - } - if (R_FAILED((rc = this->Write(Max17050SocVf0, vfsoc)))) { - return rc; - } - if (R_FAILED((rc = this->Read(Max17050Qh, &qh)))) { - return rc; - } - if (R_FAILED((rc = this->Write(Max17050Qh0, qh)))) { - return rc; - } - if (R_FAILED((rc = this->LockVfSoc()))) { - return rc; - } + R_TRY(this->Read(Max17050SocVf, &vfsoc)); + R_TRY(this->UnlockVfSoc()); + R_TRY(this->Write(Max17050SocVf0, vfsoc)); + R_TRY(this->Read(Max17050Qh, &qh)); + R_TRY(this->Write(Max17050Qh0, qh)); + R_TRY(this->LockVfSoc()); } /* Write cycles. */ @@ -273,25 +214,17 @@ Result BatteryDriver::InitializeBatteryParameters() { while (!this->WriteValidate(Max17050DPAcc, 0x0C80)) { /* ... */ } while (!this->WriteValidate(Max17050DQAcc, dqacc)) { /* ... */ } while (!this->WriteValidate(Max17050FullCap, params->fullcap)) { /* ... */ } - if (R_FAILED((rc = this->Write(Max17050DesignCap, params->vffullcap)))) { - return rc; - } + R_TRY(this->Write(Max17050DesignCap, params->vffullcap)); while (!this->WriteValidate(Max17050FullCapNom, params->vffullcap)) { /* ... */ } - if (R_FAILED((rc = this->Write(Max17050SocRep, vfsoc)))) { - return rc; - } + R_TRY(this->Write(Max17050SocRep, vfsoc)); /* Finish initialization. */ { u16 status; - if (R_FAILED((rc = this->Read(Max17050Status, &status)))) { - return rc; - } + R_TRY(this->Read(Max17050Status, &status)); while (!this->WriteValidate(Max17050Status, status & 0xFFFD)) { /* ... */ } } - if (R_FAILED((rc = this->Write(Max17050CGain, 0x7FFF)))) { - return rc; - } + R_TRY(this->Write(Max17050CGain, 0x7FFF)); } return ResultSuccess; @@ -300,50 +233,35 @@ Result BatteryDriver::InitializeBatteryParameters() { Result BatteryDriver::IsBatteryRemoved(bool *out) { /* N doesn't check result, but we will. */ u16 val = 0; - Result rc = this->Read(Max17050Status, &val); - if (R_FAILED(rc)) { - return rc; - } + R_TRY(this->Read(Max17050Status, &val)); *out = (val & 0x0008) == 0x0008; return ResultSuccess; } Result BatteryDriver::GetTemperature(double *out) { u16 val = 0; - Result rc = this->Read(Max17050Temperature, &val); - if (R_FAILED(rc)) { - return rc; - } + R_TRY(this->Read(Max17050Temperature, &val)); *out = static_cast(val) * double(0.00390625); return ResultSuccess; } Result BatteryDriver::GetAverageVCell(u32 *out) { u16 val = 0; - Result rc = this->Read(Max17050AverageVCell, &val); - if (R_FAILED(rc)) { - return rc; - } + R_TRY(this->Read(Max17050AverageVCell, &val)); *out = (625 * u32(val >> 3)) / 1000; return ResultSuccess; } Result BatteryDriver::GetSocRep(double *out) { u16 val = 0; - Result rc = this->Read(Max17050SocRep, &val); - if (R_FAILED(rc)) { - return rc; - } + R_TRY(this->Read(Max17050SocRep, &val)); *out = static_cast(val) * double(0.00390625); return ResultSuccess; } Result BatteryDriver::GetBatteryPercentage(size_t *out) { double raw_charge; - Result rc = this->GetSocRep(&raw_charge); - if (R_FAILED(rc)) { - return rc; - } + R_TRY(this->GetSocRep(&raw_charge)); int converted_percentage = (((raw_charge - 3.93359375) * 98.0) / 94.2304688) + 2.0; if (converted_percentage < 1) { *out = 1; @@ -361,10 +279,7 @@ Result BatteryDriver::SetShutdownTimer() { Result BatteryDriver::GetShutdownEnabled(bool *out) { u16 val = 0; - Result rc = this->Read(Max17050Config, &val); - if (R_FAILED(rc)) { - return rc; - } + R_TRY(this->Read(Max17050Config, &val)); *out = (val & 0x0040) != 0; return ResultSuccess; } diff --git a/stratosphere/boot/source/boot_calibration.cpp b/stratosphere/boot/source/boot_calibration.cpp index 9971b255b..d74136064 100644 --- a/stratosphere/boot/source/boot_calibration.cpp +++ b/stratosphere/boot/source/boot_calibration.cpp @@ -55,20 +55,13 @@ static Result ValidateCalibrationCrc16(const void *data, size_t size) { static Result GetBatteryVendorImpl(u32 *vendor) { FsStorage s; - Result rc = fsOpenBisStorage(&s, FsBisStorageId_CalibrationBinary); - if (R_FAILED(rc)) { - return rc; - } + R_TRY(fsOpenBisStorage(&s, FsBisStorageId_CalibrationBinary)); ON_SCOPE_EXIT { fsStorageClose(&s); }; u8 battery_lot[BatteryLotSize]; - if (R_FAILED((rc = fsStorageRead(&s, BatteryLotOffset, battery_lot, sizeof(battery_lot))))) { - return rc; - } + R_TRY(fsStorageRead(&s, BatteryLotOffset, battery_lot, sizeof(battery_lot))); - if (R_FAILED((rc = ValidateCalibrationCrc16(battery_lot, sizeof(battery_lot))))) { - return rc; - } + R_TRY(ValidateCalibrationCrc16(battery_lot, sizeof(battery_lot))); *vendor = battery_lot[7]; return ResultSuccess; @@ -76,20 +69,13 @@ static Result GetBatteryVendorImpl(u32 *vendor) { static Result GetBatteryVersionImpl(u32 *version) { FsStorage s; - Result rc = fsOpenBisStorage(&s, FsBisStorageId_CalibrationBinary); - if (R_FAILED(rc)) { - return rc; - } + R_TRY(fsOpenBisStorage(&s, FsBisStorageId_CalibrationBinary)); ON_SCOPE_EXIT { fsStorageClose(&s); }; u8 battery_version[BatteryVersionSize]; - if (R_FAILED((rc = fsStorageRead(&s, BatteryVersionOffset, battery_version, sizeof(battery_version))))) { - return rc; - } + R_TRY(fsStorageRead(&s, BatteryVersionOffset, battery_version, sizeof(battery_version))); - if (R_FAILED((rc = ValidateCalibrationCrc16(battery_version, sizeof(battery_version))))) { - return rc; - } + R_TRY(ValidateCalibrationCrc16(battery_version, sizeof(battery_version))); *version = battery_version[0]; return ResultSuccess; diff --git a/stratosphere/boot/source/boot_charger_driver.cpp b/stratosphere/boot/source/boot_charger_driver.cpp index 472ffbe5b..f509779bf 100644 --- a/stratosphere/boot/source/boot_charger_driver.cpp +++ b/stratosphere/boot/source/boot_charger_driver.cpp @@ -27,16 +27,11 @@ Result ChargerDriver::Write(u8 addr, u8 val) { } Result ChargerDriver::ReadWrite(u8 addr, u8 mask, u8 val) { - Result rc; u8 cur_val; - if (R_FAILED((rc = this->Read(addr, &cur_val)))) { - return rc; - } + R_TRY(this->Read(addr, &cur_val)); const u8 new_val = (cur_val & ~mask) | val; - if (R_FAILED((rc = this->Write(addr, new_val)))) { - return rc; - } + R_TRY(this->Write(addr, new_val)); return ResultSuccess; } @@ -45,56 +40,21 @@ Result ChargerDriver::Initialize() { } Result ChargerDriver::Initialize(bool set_input_current_limit) { - Result rc; if (set_input_current_limit) { - if (R_FAILED((rc = this->SetInputCurrentLimit(InputCurrentLimit_500mA)))) { - return rc; - } + R_TRY(this->SetInputCurrentLimit(InputCurrentLimit_500mA)); } - if (R_FAILED((rc = this->SetChargeVoltageLimit(4208)))) { - return rc; - } - - if (R_FAILED((rc = this->SetFastChargeCurrentLimit(512)))) { - return rc; - } - - if (R_FAILED((rc = this->SetForce20PercentChargeCurrent(false)))) { - return rc; - } - - if (R_FAILED((rc = this->SetPreChargeCurrentLimit(128)))) { - return rc; - } - - if (R_FAILED((rc = this->SetTerminationCurrentLimit(128)))) { - return rc; - } - - if (R_FAILED((rc = this->SetMinimumSystemVoltageLimit(3000)))) { - return rc; - } - - if (R_FAILED((rc = this->SetWatchdogTimerSetting(WatchdogTimerSetting_Disabled)))) { - return rc; - } - - if (R_FAILED((rc = this->SetChargingSafetyTimerEnabled(false)))) { - return rc; - } - - if (R_FAILED((rc = this->ResetWatchdogTimer()))) { - return rc; - } - - if (R_FAILED((rc = this->SetBoostModeCurrentLimit(BoostModeCurrentLimit_500mA)))) { - return rc; - } - - if (R_FAILED((rc = this->SetHiZEnabled(false)))) { - return rc; - } + R_TRY(this->SetChargeVoltageLimit(4208)); + R_TRY(this->SetFastChargeCurrentLimit(512)); + R_TRY(this->SetForce20PercentChargeCurrent(false)); + R_TRY(this->SetPreChargeCurrentLimit(128)); + R_TRY(this->SetTerminationCurrentLimit(128)); + R_TRY(this->SetMinimumSystemVoltageLimit(3000)); + R_TRY(this->SetWatchdogTimerSetting(WatchdogTimerSetting_Disabled)); + R_TRY(this->SetChargingSafetyTimerEnabled(false)); + R_TRY(this->ResetWatchdogTimer()); + R_TRY(this->SetBoostModeCurrentLimit(BoostModeCurrentLimit_500mA)); + R_TRY(this->SetHiZEnabled(false)); return ResultSuccess; } @@ -158,20 +118,14 @@ Result ChargerDriver::SetHiZEnabled(bool enabled) { Result ChargerDriver::GetInputCurrentLimit(InputCurrentLimit *out) { u8 limit; - Result rc = this->Read(Bq24193InputSourceControl, &limit); - if (R_FAILED(rc)) { - return rc; - } + R_TRY(this->Read(Bq24193InputSourceControl, &limit)); *out = static_cast(limit); return ResultSuccess; } Result ChargerDriver::GetChargeVoltageLimit(u32 *out) { u8 reg; - Result rc = this->Read(Bq24193ChargeVoltageControl, ®); - if (R_FAILED(rc)) { - return rc; - } + R_TRY(this->Read(Bq24193ChargeVoltageControl, ®)); *out = DecodeChargeVoltageLimit(reg); return ResultSuccess; } diff --git a/stratosphere/boot/source/boot_i2c_utils.cpp b/stratosphere/boot/source/boot_i2c_utils.cpp index f447cedb0..6487ccea9 100644 --- a/stratosphere/boot/source/boot_i2c_utils.cpp +++ b/stratosphere/boot/source/boot_i2c_utils.cpp @@ -24,16 +24,14 @@ static Result RetryUntilSuccess(F f) { u64 cur_time = 0; while (true) { - Result rc = f(); - if (R_SUCCEEDED(rc)) { - return rc; - } else { + R_TRY_CLEANUP(f(), { cur_time += retry_interval; - if (cur_time >= timeout) { - return rc; + if (cur_time < timeout) { + svcSleepThread(retry_interval); + continue; } - } - svcSleepThread(retry_interval); + }); + return ResultSuccess; } } diff --git a/stratosphere/boot/source/boot_pmic_driver.cpp b/stratosphere/boot/source/boot_pmic_driver.cpp index 24c8d6151..39b10be85 100644 --- a/stratosphere/boot/source/boot_pmic_driver.cpp +++ b/stratosphere/boot/source/boot_pmic_driver.cpp @@ -33,11 +33,7 @@ void PmicDriver::RebootSystem() { Result PmicDriver::GetAcOk(bool *out) { u8 power_status; - Result rc = this->GetPowerStatus(&power_status); - if (R_FAILED(rc)) { - return rc; - } - + R_TRY(this->GetPowerStatus(&power_status)); *out = (power_status & 0x02) != 0; return ResultSuccess; } @@ -59,10 +55,7 @@ Result PmicDriver::GetNvErc(u8 *out) { Result PmicDriver::GetPowerButtonPressed(bool *out) { u8 power_intr; - Result rc = this->GetPowerIntr(&power_intr); - if (R_FAILED(rc)) { - return rc; - } + R_TRY(this->GetPowerIntr(&power_intr)); *out = (power_intr & 0x08) != 0; return ResultSuccess; } diff --git a/stratosphere/boot/source/boot_rtc_driver.cpp b/stratosphere/boot/source/boot_rtc_driver.cpp index f142c655e..889ee2419 100644 --- a/stratosphere/boot/source/boot_rtc_driver.cpp +++ b/stratosphere/boot/source/boot_rtc_driver.cpp @@ -22,10 +22,7 @@ Result RtcDriver::ReadRtcRegister(u8 *out, u8 address) { const u8 update_addr = 0x04; const u8 update_val = 0x10; - Result rc = Boot::WriteI2cRegister(this->i2c_session, &update_val, sizeof(update_val), &update_addr, sizeof(update_addr)); - if (R_FAILED(rc)) { - return rc; - } + R_TRY(Boot::WriteI2cRegister(this->i2c_session, &update_val, sizeof(update_val), &update_addr, sizeof(update_addr))); svcSleepThread(16'000'000ul); return Boot::ReadI2cRegister(this->i2c_session, out, sizeof(*out), &address, sizeof(address)); } diff --git a/stratosphere/boot/source/i2c_driver/i2c_api.cpp b/stratosphere/boot/source/i2c_driver/i2c_api.cpp index 89226f033..2ff55f694 100644 --- a/stratosphere/boot/source/i2c_driver/i2c_api.cpp +++ b/stratosphere/boot/source/i2c_driver/i2c_api.cpp @@ -31,10 +31,7 @@ static Result I2cSendHandler(const u8 **cur_cmd, u8 **cur_dst, I2cSessionImpl& s size_t num_bytes = (**cur_cmd); (*cur_cmd)++; - Result rc = I2cDriver::Send(session, *cur_cmd, num_bytes, option); - if (R_FAILED(rc)) { - return rc; - } + R_TRY(I2cDriver::Send(session, *cur_cmd, num_bytes, option)); (*cur_cmd) += num_bytes; return ResultSuccess; @@ -49,10 +46,7 @@ static Result I2cReceiveHandler(const u8 **cur_cmd, u8 **cur_dst, I2cSessionImpl size_t num_bytes = (**cur_cmd); (*cur_cmd)++; - Result rc = I2cDriver::Receive(session, *cur_dst, num_bytes, option); - if (R_FAILED(rc)) { - return rc; - } + R_TRY(I2cDriver::Receive(session, *cur_dst, num_bytes, option)); (*cur_dst) += num_bytes; return ResultSuccess; @@ -156,10 +150,7 @@ Result I2cDriver::ExecuteCommandList(I2cSessionImpl &session, void *dst, size_t std::abort(); } - Result rc = g_cmd_handlers[cmd](&cur_cmd, &cur_dst, session); - if (R_FAILED(rc)) { - return rc; - } + R_TRY(g_cmd_handlers[cmd](&cur_cmd, &cur_dst, session)); } return ResultSuccess; diff --git a/stratosphere/boot/source/i2c_driver/i2c_bus_accessor.cpp b/stratosphere/boot/source/i2c_driver/i2c_bus_accessor.cpp index e6f372e46..508b3e779 100644 --- a/stratosphere/boot/source/i2c_driver/i2c_bus_accessor.cpp +++ b/stratosphere/boot/source/i2c_driver/i2c_bus_accessor.cpp @@ -146,7 +146,6 @@ Result I2cBusAccessor::Send(const u8 *data, size_t num_bytes, I2cTransactionOpti std::scoped_lock lk(this->register_mutex); const u8 *cur_src = data; size_t remaining = num_bytes; - Result rc; /* Set interrupt enable, clear interrupt status. */ WriteRegister(&this->i2c_registers->I2C_INTERRUPT_MASK_REGISTER_0, 0x8E); @@ -185,25 +184,19 @@ Result I2cBusAccessor::Send(const u8 *data, size_t num_bytes, I2cTransactionOpti return ResultI2cTimedOut; } - if (R_FAILED((rc = this->GetAndHandleTransactionResult()))) { - return rc; - } + R_TRY(this->GetAndHandleTransactionResult()); } WriteRegister(&this->i2c_registers->I2C_INTERRUPT_MASK_REGISTER_0, 0x8C); /* Wait for successful completion. */ while (true) { - if (R_FAILED((rc = this->GetAndHandleTransactionResult()))) { - return rc; - } + R_TRY(this->GetAndHandleTransactionResult()); /* Check PACKET_XFER_COMPLETE */ const u32 interrupt_status = ReadRegister(&this->i2c_registers->I2C_INTERRUPT_STATUS_REGISTER_0); if (interrupt_status & 0x80) { - if (R_FAILED((rc = this->GetAndHandleTransactionResult()))) { - return rc; - } + R_TRY(this->GetAndHandleTransactionResult()); break; } @@ -222,7 +215,6 @@ Result I2cBusAccessor::Receive(u8 *out_data, size_t num_bytes, I2cTransactionOpt std::scoped_lock lk(this->register_mutex); u8 *cur_dst = out_data; size_t remaining = num_bytes; - Result rc; /* Set interrupt enable, clear interrupt status. */ WriteRegister(&this->i2c_registers->I2C_INTERRUPT_MASK_REGISTER_0, 0x8D); @@ -241,9 +233,7 @@ Result I2cBusAccessor::Receive(u8 *out_data, size_t num_bytes, I2cTransactionOpt return ResultI2cTimedOut; } - if (R_FAILED((rc = this->GetAndHandleTransactionResult()))) { - return rc; - } + R_TRY(this->GetAndHandleTransactionResult()); const u32 fifo_status = ReadRegister(&this->i2c_registers->I2C_FIFO_STATUS_0); const size_t fifo_cnt = std::min((remaining + 3) >> 2, static_cast(fifo_status & 0xF)); @@ -477,14 +467,12 @@ void I2cBusAccessor::HandleTransactionResult(Result result) { } Result I2cBusAccessor::GetAndHandleTransactionResult() { - Result rc = this->GetTransactionResult(); - this->HandleTransactionResult(rc); - - if (R_FAILED(rc)) { + const Result transaction_res = this->GetTransactionResult(); + R_TRY_CLEANUP(transaction_res, { + this->HandleTransactionResult(transaction_res); this->ClearInterruptMask(); eventClear(&this->interrupt_event); - return rc; - } + }); return ResultSuccess; } diff --git a/stratosphere/boot/source/i2c_driver/i2c_command_list.cpp b/stratosphere/boot/source/i2c_driver/i2c_command_list.cpp index 44915029b..e57a75060 100644 --- a/stratosphere/boot/source/i2c_driver/i2c_command_list.cpp +++ b/stratosphere/boot/source/i2c_driver/i2c_command_list.cpp @@ -27,8 +27,7 @@ Result I2cCommandListFormatter::CanEnqueue(size_t size) const { } Result I2cCommandListFormatter::EnqueueSendCommand(I2cTransactionOption option, const void *src, size_t size) { - Result rc = this->CanEnqueue(SendCommandSize + size); - if (R_FAILED(rc)) { return rc; } + R_TRY(this->CanEnqueue(SendCommandSize + size)); this->cmd_list[this->cur_index] = I2cCommand_Send; this->cmd_list[this->cur_index] |= ((option & I2cTransactionOption_Start) != 0) << 6; @@ -45,8 +44,7 @@ Result I2cCommandListFormatter::EnqueueSendCommand(I2cTransactionOption option, } Result I2cCommandListFormatter::EnqueueReceiveCommand(I2cTransactionOption option, size_t size) { - Result rc = this->CanEnqueue(ReceiveCommandSize); - if (R_FAILED(rc)) { return rc; } + R_TRY(this->CanEnqueue(ReceiveCommandSize)); this->cmd_list[this->cur_index] = I2cCommand_Receive; this->cmd_list[this->cur_index] |= ((option & I2cTransactionOption_Start) != 0) << 6; @@ -58,8 +56,7 @@ Result I2cCommandListFormatter::EnqueueReceiveCommand(I2cTransactionOption optio } Result I2cCommandListFormatter::EnqueueSleepCommand(size_t us) { - Result rc = this->CanEnqueue(SleepCommandSize); - if (R_FAILED(rc)) { return rc; } + R_TRY(this->CanEnqueue(SleepCommandSize)); this->cmd_list[this->cur_index] = I2cCommand_SubCommand; this->cmd_list[this->cur_index] |= I2cSubCommand_Sleep << 2; diff --git a/stratosphere/boot/source/i2c_driver/i2c_driver_session.cpp b/stratosphere/boot/source/i2c_driver/i2c_driver_session.cpp index 47e430554..711dda5a6 100644 --- a/stratosphere/boot/source/i2c_driver/i2c_driver_session.cpp +++ b/stratosphere/boot/source/i2c_driver/i2c_driver_session.cpp @@ -57,49 +57,43 @@ bool I2cDriverSession::IsOpen() const{ Result I2cDriverSession::DoTransaction(void *dst, const void *src, size_t num_bytes, I2cTransactionOption option, DriverCommand command){ std::scoped_lock lk(this->bus_accessor_mutex); - Result rc; if (this->bus_accessor->GetBusy()) { return ResultI2cBusBusy; } this->bus_accessor->OnStartTransaction(); + ON_SCOPE_EXIT { this->bus_accessor->OnStopTransaction(); }; - if (R_SUCCEEDED((rc = this->bus_accessor->StartTransaction(command, this->addressing_mode, this->slave_address)))) { - switch (command) { - case DriverCommand_Send: - rc = this->bus_accessor->Send(reinterpret_cast(src), num_bytes, option, this->addressing_mode, this->slave_address); - break; - case DriverCommand_Receive: - rc = this->bus_accessor->Receive(reinterpret_cast(dst), num_bytes, option, this->addressing_mode, this->slave_address); - break; - default: - std::abort(); - } + R_TRY(this->bus_accessor->StartTransaction(command, this->addressing_mode, this->slave_address)); + + switch (command) { + case DriverCommand_Send: + R_TRY(this->bus_accessor->Send(reinterpret_cast(src), num_bytes, option, this->addressing_mode, this->slave_address)); + break; + case DriverCommand_Receive: + R_TRY(this->bus_accessor->Receive(reinterpret_cast(dst), num_bytes, option, this->addressing_mode, this->slave_address)); + break; + default: + std::abort(); } - this->bus_accessor->OnStopTransaction(); - - return rc; + return ResultSuccess; } Result I2cDriverSession::DoTransactionWithRetry(void *dst, const void *src, size_t num_bytes, I2cTransactionOption option, DriverCommand command){ - Result rc; - size_t i = 0; while (true) { - rc = this->DoTransaction(dst, src, num_bytes, option, command); - if (rc == ResultI2cTimedOut) { - i++; - if (i <= this->max_retries) { - svcSleepThread(this->retry_wait_time); - continue; + R_TRY_CATCH(this->DoTransaction(dst, src, num_bytes, option, command)) { + R_CATCH(ResultI2cTimedOut) { + i++; + if (i <= this->max_retries) { + svcSleepThread(this->retry_wait_time); + continue; + } + return ResultI2cBusBusy; } - return ResultI2cBusBusy; - } else if (R_FAILED(rc)) { - return rc; - } + } R_END_TRY_CATCH; return ResultSuccess; } - return rc; } diff --git a/stratosphere/boot/source/updater/updater_api.cpp b/stratosphere/boot/source/updater/updater_api.cpp index ec31d342d..eca58a62b 100644 --- a/stratosphere/boot/source/updater/updater_api.cpp +++ b/stratosphere/boot/source/updater/updater_api.cpp @@ -80,28 +80,20 @@ u32 Updater::GetNcmTitleType(BootModeType mode) { } Result Updater::GetVerificationState(VerificationState *out, void *work_buffer, size_t work_buffer_size) { - Result rc; - /* Always set output to true before doing anything else. */ out->needs_verify_normal = true; out->needs_verify_safe = true; /* Ensure work buffer is big enough for us to do what we want to do. */ - if (R_FAILED((rc = ValidateWorkBuffer(work_buffer, work_buffer_size)))) { - return rc; - } + R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size)); /* Initialize boot0 save accessor. */ BisSave save; - if (R_FAILED((rc = save.Initialize(work_buffer, work_buffer_size)))) { - return rc; - } + R_TRY(save.Initialize(work_buffer, work_buffer_size)); ON_SCOPE_EXIT { save.Finalize(); }; /* Load save from NAND. */ - if (R_FAILED((rc = save.Load()))) { - return rc; - } + R_TRY(save.Load()); /* Read data from save. */ out->needs_verify_normal = save.GetNeedsVerification(BootModeType_Normal); @@ -110,22 +102,17 @@ Result Updater::GetVerificationState(VerificationState *out, void *work_buffer, } Result Updater::VerifyBootImagesAndRepairIfNeeded(bool *out_repaired_normal, bool *out_repaired_safe, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type) { - Result rc; /* Always set output to false before doing anything else. */ *out_repaired_normal = false; *out_repaired_safe = false; /* Ensure work buffer is big enough for us to do what we want to do. */ - if (R_FAILED((rc = ValidateWorkBuffer(work_buffer, work_buffer_size)))) { - return rc; - } + R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size)); /* Get verification state from NAND. */ VerificationState verification_state; - if (R_FAILED((rc = GetVerificationState(&verification_state, work_buffer, work_buffer_size)))) { - return rc; - } + R_TRY(GetVerificationState(&verification_state, work_buffer, work_buffer_size)); /* If we don't need to verify anything, we're done. */ if (!verification_state.needs_verify_normal && !verification_state.needs_verify_safe) { @@ -142,58 +129,43 @@ Result Updater::VerifyBootImagesAndRepairIfNeeded(bool *out_repaired_normal, boo /* Verify normal, verify safe as needed. */ if (verification_state.needs_verify_normal) { - rc = VerifyBootImagesAndRepairIfNeeded(out_repaired_normal, BootModeType_Normal, work_buffer, work_buffer_size, boot_image_update_type); - if (rc == ResultUpdaterBootImagePackageNotFound) { - /* Nintendo considers failure to locate bip a success. TODO: don't do that? */ - rc = ResultSuccess; - } - if (R_FAILED(rc)) { - return rc; - } + R_TRY_CATCH(VerifyBootImagesAndRepairIfNeeded(out_repaired_normal, BootModeType_Normal, work_buffer, work_buffer_size, boot_image_update_type)) { + R_CATCH(ResultUpdaterBootImagePackageNotFound) { + /* Nintendo considers failure to locate bip a success. TODO: don't do that? */ + } + } R_END_TRY_CATCH; } + if (verification_state.needs_verify_safe) { - rc = VerifyBootImagesAndRepairIfNeeded(out_repaired_safe, BootModeType_Safe, work_buffer, work_buffer_size, boot_image_update_type); - if (rc == ResultUpdaterBootImagePackageNotFound) { - /* Nintendo considers failure to locate bip a success. TODO: don't do that? */ - rc = ResultSuccess; - } - if (R_FAILED(rc)) { - return rc; - } + R_TRY_CATCH(VerifyBootImagesAndRepairIfNeeded(out_repaired_safe, BootModeType_Safe, work_buffer, work_buffer_size, boot_image_update_type)) { + R_CATCH(ResultUpdaterBootImagePackageNotFound) { + /* Nintendo considers failure to locate bip a success. TODO: don't do that? */ + } + } R_END_TRY_CATCH; } return ResultSuccess; } Result Updater::VerifyBootImagesAndRepairIfNeeded(bool *out_repaired, BootModeType mode, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type) { - Result rc; - /* Get system data id for boot images (819/81A/81B/81C). */ u64 bip_data_id; - if (R_FAILED((rc = GetBootImagePackageDataId(&bip_data_id, mode, work_buffer, work_buffer_size)))) { - return rc; - } + R_TRY(GetBootImagePackageDataId(&bip_data_id, mode, work_buffer, work_buffer_size)); /* Verify the boot images in NAND. */ - if (R_FAILED((rc = VerifyBootImages(bip_data_id, mode, work_buffer, work_buffer_size, boot_image_update_type)))) { - /* If we failed for a reason other than repair needed, bail out. */ - if (rc != ResultUpdaterNeedsRepairBootImages) { - return rc; + R_TRY_CATCH(VerifyBootImages(bip_data_id, mode, work_buffer, work_buffer_size, boot_image_update_type)) { + R_CATCH(ResultUpdaterNeedsRepairBootImages) { + /* Perform repair. */ + *out_repaired = true; + R_TRY(UpdateBootImages(bip_data_id, mode, work_buffer, work_buffer_size, boot_image_update_type)); } - /* Perform repair. */ - *out_repaired = true; - if (R_FAILED((rc = UpdateBootImages(bip_data_id, mode, work_buffer, work_buffer_size, boot_image_update_type)))) { - return rc; - } - } + } R_END_TRY_CATCH; /* We've either just verified or just repaired. Either way, we don't need to verify any more. */ return SetVerificationNeeded(mode, false, work_buffer, work_buffer_size); } Result Updater::GetBootImagePackageDataId(u64 *out_data_id, BootModeType mode, void *work_buffer, size_t work_buffer_size) { - Result rc; - /* Ensure we can read content metas. */ constexpr size_t MaxContentMetas = 0x40; if (work_buffer_size < sizeof(NcmMetaRecord) * MaxContentMetas) { @@ -202,9 +174,7 @@ Result Updater::GetBootImagePackageDataId(u64 *out_data_id, BootModeType mode, v /* Open NAND System meta database, list contents. */ NcmContentMetaDatabase meta_db; - if (R_FAILED((rc = ncmOpenContentMetaDatabase(FsStorageId_NandSystem, &meta_db)))) { - return rc; - } + R_TRY(ncmOpenContentMetaDatabase(FsStorageId_NandSystem, &meta_db)); ON_SCOPE_EXIT { serviceClose(&meta_db.s); }; NcmMetaRecord *records = reinterpret_cast(work_buffer); @@ -212,9 +182,7 @@ Result Updater::GetBootImagePackageDataId(u64 *out_data_id, BootModeType mode, v const u32 title_type = GetNcmTitleType(mode); u32 written_entries; u32 total_entries; - if (R_FAILED((rc = ncmContentMetaDatabaseList(&meta_db, title_type, 0, 0, UINT64_MAX, records, MaxContentMetas * sizeof(*records), &written_entries, &total_entries)))) { - return rc; - } + R_TRY(ncmContentMetaDatabaseList(&meta_db, title_type, 0, 0, UINT64_MAX, records, MaxContentMetas * sizeof(*records), &written_entries, &total_entries)); if (total_entries == 0) { return ResultUpdaterBootImagePackageNotFound; } @@ -227,9 +195,7 @@ Result Updater::GetBootImagePackageDataId(u64 *out_data_id, BootModeType mode, v if (total_entries > 1) { for (size_t i = 0; i < total_entries; i++) { u8 attr; - if (R_FAILED((rc = ncmContentMetaDatabaseGetAttributes(&meta_db, &records[i], &attr)))) { - return rc; - } + R_TRY(ncmContentMetaDatabaseGetAttributes(&meta_db, &records[i], &attr)); if (attr & NcmContentMetaAttribute_Exfat) { *out_data_id = records[i].titleId; @@ -255,29 +221,19 @@ Result Updater::VerifyBootImages(u64 data_id, BootModeType mode, void *work_buff } Result Updater::ValidateBctFileHash(Boot0Accessor &accessor, Boot0Partition which, const void *stored_hash, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type) { - Result rc; - /* Ensure work buffer is big enough for us to do what we want to do. */ - if (R_FAILED((rc = ValidateWorkBuffer(work_buffer, work_buffer_size)))) { - return rc; - } + R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size)); void *bct = reinterpret_cast(reinterpret_cast(work_buffer) + 0); void *work = reinterpret_cast(reinterpret_cast(work_buffer) + BctSize); size_t size; - if (R_FAILED((rc = ReadFile(&size, bct, BctSize, GetBctPath(boot_image_update_type))))) { - return rc; - } + R_TRY(ReadFile(&size, bct, BctSize, GetBctPath(boot_image_update_type))); if (HasEks(boot_image_update_type)) { - if (R_FAILED((rc = accessor.UpdateEks(bct, work)))) { - return rc; - } + R_TRY(accessor.UpdateEks(bct, work)); } if (HasAutoRcmPreserve(boot_image_update_type)) { - if (R_FAILED((rc = accessor.PreserveAutoRcm(bct, work, which)))) { - return rc; - } + R_TRY(accessor.PreserveAutoRcm(bct, work, which)); } u8 file_hash[SHA256_HASH_SIZE]; @@ -291,19 +247,14 @@ Result Updater::ValidateBctFileHash(Boot0Accessor &accessor, Boot0Partition whic } Result Updater::VerifyBootImagesNormal(u64 data_id, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type) { - Result rc; - /* Ensure work buffer is big enough for us to do what we want to do. */ - if (R_FAILED((rc = ValidateWorkBuffer(work_buffer, work_buffer_size)))) { - return rc; - } + R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size)); - if (R_FAILED((rc = romfsMountFromDataArchive(data_id, FsStorageId_NandSystem, GetBootImagePackageMountPath())))) { - if (rc == ResultFsTargetNotFound) { + R_TRY_CATCH(romfsMountFromDataArchive(data_id, FsStorageId_NandSystem, GetBootImagePackageMountPath())) { + R_CATCH(ResultFsTargetNotFound) { return ResultUpdaterBootImagePackageNotFound; } - return rc; - } + } R_END_TRY_CATCH; ON_SCOPE_EXIT { if (R_FAILED(romfsUnmount(GetBootImagePackageMountPath()))) { std::abort(); } }; /* Read and validate hashes of boot images. */ @@ -313,57 +264,35 @@ Result Updater::VerifyBootImagesNormal(u64 data_id, void *work_buffer, size_t wo u8 file_hash[SHA256_HASH_SIZE]; Boot0Accessor boot0_accessor; - if (R_FAILED((rc = boot0_accessor.Initialize()))) { - return rc; - } + R_TRY(boot0_accessor.Initialize()); ON_SCOPE_EXIT { boot0_accessor.Finalize(); }; /* Compare BCT hashes. */ - if (R_FAILED((rc = boot0_accessor.GetHash(nand_hash, BctSize, work_buffer, work_buffer_size, Boot0Partition::BctNormalMain)))) { - return rc; - } - if (R_FAILED((rc = ValidateBctFileHash(boot0_accessor, Boot0Partition::BctNormalMain, nand_hash, work_buffer, work_buffer_size, boot_image_update_type)))) { - return rc; - } + R_TRY(boot0_accessor.GetHash(nand_hash, BctSize, work_buffer, work_buffer_size, Boot0Partition::BctNormalMain)); + R_TRY(ValidateBctFileHash(boot0_accessor, Boot0Partition::BctNormalMain, nand_hash, work_buffer, work_buffer_size, boot_image_update_type)); /* Compare BCT Sub hashes. */ - if (R_FAILED((rc = boot0_accessor.GetHash(nand_hash, BctSize, work_buffer, work_buffer_size, Boot0Partition::BctNormalSub)))) { - return rc; - } - if (R_FAILED((rc = ValidateBctFileHash(boot0_accessor, Boot0Partition::BctNormalSub, nand_hash, work_buffer, work_buffer_size, boot_image_update_type)))) { - return rc; - } + R_TRY(boot0_accessor.GetHash(nand_hash, BctSize, work_buffer, work_buffer_size, Boot0Partition::BctNormalSub)); + R_TRY(ValidateBctFileHash(boot0_accessor, Boot0Partition::BctNormalSub, nand_hash, work_buffer, work_buffer_size, boot_image_update_type)); /* Compare Package1 Normal/Sub hashes. */ - if (R_FAILED((rc = GetFileHash(&size, file_hash, GetPackage1Path(boot_image_update_type), work_buffer, work_buffer_size)))) { - return rc; - } - if (R_FAILED((rc = boot0_accessor.GetHash(nand_hash, size, work_buffer, work_buffer_size, Boot0Partition::Package1NormalMain)))) { - return rc; - } + R_TRY(GetFileHash(&size, file_hash, GetPackage1Path(boot_image_update_type), work_buffer, work_buffer_size)); + R_TRY(boot0_accessor.GetHash(nand_hash, size, work_buffer, work_buffer_size, Boot0Partition::Package1NormalMain)); if (std::memcmp(file_hash, nand_hash, SHA256_HASH_SIZE) != 0) { return ResultUpdaterNeedsRepairBootImages; } - if (R_FAILED((rc = boot0_accessor.GetHash(nand_hash, size, work_buffer, work_buffer_size, Boot0Partition::Package1NormalSub)))) { - return rc; - } + R_TRY(boot0_accessor.GetHash(nand_hash, size, work_buffer, work_buffer_size, Boot0Partition::Package1NormalSub)); if (std::memcmp(file_hash, nand_hash, SHA256_HASH_SIZE) != 0) { return ResultUpdaterNeedsRepairBootImages; } /* Compare Package2 Normal/Sub hashes. */ - if (R_FAILED((rc = GetFileHash(&size, file_hash, GetPackage2Path(boot_image_update_type), work_buffer, work_buffer_size)))) { - return rc; - } - if (R_FAILED((rc = GetPackage2Hash(nand_hash, size, work_buffer, work_buffer_size, Package2Type::NormalMain)))) { - return rc; - } + R_TRY(GetFileHash(&size, file_hash, GetPackage2Path(boot_image_update_type), work_buffer, work_buffer_size)); + R_TRY(GetPackage2Hash(nand_hash, size, work_buffer, work_buffer_size, Package2Type::NormalMain)); if (std::memcmp(file_hash, nand_hash, SHA256_HASH_SIZE) != 0) { return ResultUpdaterNeedsRepairBootImages; } - if (R_FAILED((rc = GetPackage2Hash(nand_hash, size, work_buffer, work_buffer_size, Package2Type::NormalSub)))) { - return rc; - } + R_TRY(GetPackage2Hash(nand_hash, size, work_buffer, work_buffer_size, Package2Type::NormalSub)); if (std::memcmp(file_hash, nand_hash, SHA256_HASH_SIZE) != 0) { return ResultUpdaterNeedsRepairBootImages; } @@ -373,19 +302,14 @@ Result Updater::VerifyBootImagesNormal(u64 data_id, void *work_buffer, size_t wo } Result Updater::VerifyBootImagesSafe(u64 data_id, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type) { - Result rc; - /* Ensure work buffer is big enough for us to do what we want to do. */ - if (R_FAILED((rc = ValidateWorkBuffer(work_buffer, work_buffer_size)))) { - return rc; - } + R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size)); - if (R_FAILED((rc = romfsMountFromDataArchive(data_id, FsStorageId_NandSystem, GetBootImagePackageMountPath())))) { - if (rc == ResultFsTargetNotFound) { + R_TRY_CATCH(romfsMountFromDataArchive(data_id, FsStorageId_NandSystem, GetBootImagePackageMountPath())) { + R_CATCH(ResultFsTargetNotFound) { return ResultUpdaterBootImagePackageNotFound; } - return rc; - } + } R_END_TRY_CATCH; ON_SCOPE_EXIT { if (R_FAILED(romfsUnmount(GetBootImagePackageMountPath()))) { std::abort(); } }; /* Read and validate hashes of boot images. */ @@ -395,64 +319,40 @@ Result Updater::VerifyBootImagesSafe(u64 data_id, void *work_buffer, size_t work u8 file_hash[SHA256_HASH_SIZE]; Boot0Accessor boot0_accessor; - if (R_FAILED((rc = boot0_accessor.Initialize()))) { - return rc; - } + R_TRY(boot0_accessor.Initialize()); ON_SCOPE_EXIT { boot0_accessor.Finalize(); }; Boot1Accessor boot1_accessor; - if (R_FAILED((rc = boot1_accessor.Initialize()))) { - return rc; - } + R_TRY(boot1_accessor.Initialize()); ON_SCOPE_EXIT { boot1_accessor.Finalize(); }; /* Compare BCT hashes. */ - if (R_FAILED((rc = boot0_accessor.GetHash(nand_hash, BctSize, work_buffer, work_buffer_size, Boot0Partition::BctSafeMain)))) { - return rc; - } - if (R_FAILED((rc = ValidateBctFileHash(boot0_accessor, Boot0Partition::BctSafeMain, nand_hash, work_buffer, work_buffer_size, boot_image_update_type)))) { - return rc; - } + R_TRY(boot0_accessor.GetHash(nand_hash, BctSize, work_buffer, work_buffer_size, Boot0Partition::BctSafeMain)); + R_TRY(ValidateBctFileHash(boot0_accessor, Boot0Partition::BctSafeMain, nand_hash, work_buffer, work_buffer_size, boot_image_update_type)); /* Compare BCT Sub hashes. */ - if (R_FAILED((rc = boot0_accessor.GetHash(nand_hash, BctSize, work_buffer, work_buffer_size, Boot0Partition::BctSafeSub)))) { - return rc; - } - if (R_FAILED((rc = ValidateBctFileHash(boot0_accessor, Boot0Partition::BctSafeSub, nand_hash, work_buffer, work_buffer_size, boot_image_update_type)))) { - return rc; - } + R_TRY(boot0_accessor.GetHash(nand_hash, BctSize, work_buffer, work_buffer_size, Boot0Partition::BctSafeSub)); + R_TRY(ValidateBctFileHash(boot0_accessor, Boot0Partition::BctSafeSub, nand_hash, work_buffer, work_buffer_size, boot_image_update_type)); /* Compare Package1 Normal/Sub hashes. */ - if (R_FAILED((rc = GetFileHash(&size, file_hash, GetPackage1Path(boot_image_update_type), work_buffer, work_buffer_size)))) { - return rc; - } - if (R_FAILED((rc = boot1_accessor.GetHash(nand_hash, size, work_buffer, work_buffer_size, Boot1Partition::Package1SafeMain)))) { - return rc; - } + R_TRY(GetFileHash(&size, file_hash, GetPackage1Path(boot_image_update_type), work_buffer, work_buffer_size)); + R_TRY(boot1_accessor.GetHash(nand_hash, size, work_buffer, work_buffer_size, Boot1Partition::Package1SafeMain)); if (std::memcmp(file_hash, nand_hash, SHA256_HASH_SIZE) != 0) { return ResultUpdaterNeedsRepairBootImages; } - if (R_FAILED((rc = boot1_accessor.GetHash(nand_hash, size, work_buffer, work_buffer_size, Boot1Partition::Package1SafeSub)))) { - return rc; - } + R_TRY(boot1_accessor.GetHash(nand_hash, size, work_buffer, work_buffer_size, Boot1Partition::Package1SafeSub)); if (std::memcmp(file_hash, nand_hash, SHA256_HASH_SIZE) != 0) { return ResultUpdaterNeedsRepairBootImages; } /* Compare Package2 Normal/Sub hashes. */ - if (R_FAILED((rc = GetFileHash(&size, file_hash, GetPackage2Path(boot_image_update_type), work_buffer, work_buffer_size)))) { - return rc; - } - if (R_FAILED((rc = GetPackage2Hash(nand_hash, size, work_buffer, work_buffer_size, Package2Type::SafeMain)))) { - return rc; - } + R_TRY(GetFileHash(&size, file_hash, GetPackage2Path(boot_image_update_type), work_buffer, work_buffer_size)); + R_TRY(GetPackage2Hash(nand_hash, size, work_buffer, work_buffer_size, Package2Type::SafeMain)); if (std::memcmp(file_hash, nand_hash, SHA256_HASH_SIZE) != 0) { return ResultUpdaterNeedsRepairBootImages; } - if (R_FAILED((rc = GetPackage2Hash(nand_hash, size, work_buffer, work_buffer_size, Package2Type::SafeSub)))) { - return rc; - } + R_TRY(GetPackage2Hash(nand_hash, size, work_buffer, work_buffer_size, Package2Type::SafeSub)); if (std::memcmp(file_hash, nand_hash, SHA256_HASH_SIZE) != 0) { return ResultUpdaterNeedsRepairBootImages; } @@ -473,40 +373,27 @@ Result Updater::UpdateBootImages(u64 data_id, BootModeType mode, void *work_buff } Result Updater::UpdateBootImagesNormal(u64 data_id, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type) { - Result rc; - /* Ensure work buffer is big enough for us to do what we want to do. */ - if (R_FAILED((rc = ValidateWorkBuffer(work_buffer, work_buffer_size)))) { - return rc; - } + R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size)); - if (R_FAILED((rc = romfsMountFromDataArchive(data_id, FsStorageId_NandSystem, GetBootImagePackageMountPath())))) { - if (rc == ResultFsTargetNotFound) { + R_TRY_CATCH(romfsMountFromDataArchive(data_id, FsStorageId_NandSystem, GetBootImagePackageMountPath())) { + R_CATCH(ResultFsTargetNotFound) { return ResultUpdaterBootImagePackageNotFound; } - return rc; - } + } R_END_TRY_CATCH; ON_SCOPE_EXIT { if (R_FAILED(romfsUnmount(GetBootImagePackageMountPath()))) { std::abort(); } }; { Boot0Accessor boot0_accessor; - if (R_FAILED((rc = boot0_accessor.Initialize()))) { - return rc; - } + R_TRY(boot0_accessor.Initialize()); ON_SCOPE_EXIT { boot0_accessor.Finalize(); }; /* Write Package1 sub. */ - if (R_FAILED((rc = boot0_accessor.Clear(work_buffer, work_buffer_size, Boot0Partition::Package1NormalSub)))) { - return rc; - } - if (R_FAILED((rc = boot0_accessor.Write(GetPackage1Path(boot_image_update_type), work_buffer, work_buffer_size, Boot0Partition::Package1NormalSub)))) { - return rc; - } + R_TRY(boot0_accessor.Clear(work_buffer, work_buffer_size, Boot0Partition::Package1NormalSub)); + R_TRY(boot0_accessor.Write(GetPackage1Path(boot_image_update_type), work_buffer, work_buffer_size, Boot0Partition::Package1NormalSub)); /* Write Package2 sub. */ - if (R_FAILED((rc = WritePackage2(work_buffer, work_buffer_size, Package2Type::NormalSub, boot_image_update_type)))) { - return rc; - } + R_TRY(WritePackage2(work_buffer, work_buffer_size, Package2Type::NormalSub, boot_image_update_type)); /* Write BCT sub + BCT main, in that order. */ { @@ -514,98 +401,61 @@ Result Updater::UpdateBootImagesNormal(u64 data_id, void *work_buffer, size_t wo void *work = reinterpret_cast(reinterpret_cast(work_buffer) + BctSize); size_t size; - if (R_FAILED((rc = ReadFile(&size, bct, BctSize, GetBctPath(boot_image_update_type))))) { - return rc; - } + R_TRY(ReadFile(&size, bct, BctSize, GetBctPath(boot_image_update_type))); if (HasEks(boot_image_update_type)) { - if (R_FAILED((rc = boot0_accessor.UpdateEks(bct, work)))) { - return rc; - } + R_TRY(boot0_accessor.UpdateEks(bct, work)); } /* Only preserve autorcm if on a unit with unpatched rcm bug. */ if (HasAutoRcmPreserve(boot_image_update_type) && !IsRcmBugPatched()) { - if (R_FAILED((rc = boot0_accessor.PreserveAutoRcm(bct, work, Boot0Partition::BctNormalSub)))) { - return rc; - } - if (R_FAILED((rc = boot0_accessor.Write(bct, BctSize, Boot0Partition::BctNormalSub)))) { - return rc; - } - if (R_FAILED((rc = boot0_accessor.PreserveAutoRcm(bct, work, Boot0Partition::BctNormalMain)))) { - return rc; - } - if (R_FAILED((rc = boot0_accessor.Write(bct, BctSize, Boot0Partition::BctNormalMain)))) { - return rc; - } + R_TRY(boot0_accessor.PreserveAutoRcm(bct, work, Boot0Partition::BctNormalSub)); + R_TRY(boot0_accessor.Write(bct, BctSize, Boot0Partition::BctNormalSub)); + R_TRY(boot0_accessor.PreserveAutoRcm(bct, work, Boot0Partition::BctNormalMain)); + R_TRY(boot0_accessor.Write(bct, BctSize, Boot0Partition::BctNormalMain)); } else { - if (R_FAILED((rc = boot0_accessor.Write(bct, BctSize, Boot0Partition::BctNormalSub)))) { - return rc; - } - if (R_FAILED((rc = boot0_accessor.Write(bct, BctSize, Boot0Partition::BctNormalMain)))) { - return rc; - } + R_TRY(boot0_accessor.Write(bct, BctSize, Boot0Partition::BctNormalSub)); + R_TRY(boot0_accessor.Write(bct, BctSize, Boot0Partition::BctNormalMain)); } } /* Write Package2 main. */ - if (R_FAILED((rc = WritePackage2(work_buffer, work_buffer_size, Package2Type::NormalMain, boot_image_update_type)))) { - return rc; - } + R_TRY(WritePackage2(work_buffer, work_buffer_size, Package2Type::NormalMain, boot_image_update_type)); /* Write Package1 main. */ - if (R_FAILED((rc = boot0_accessor.Clear(work_buffer, work_buffer_size, Boot0Partition::Package1NormalMain)))) { - return rc; - } - if (R_FAILED((rc = boot0_accessor.Write(GetPackage1Path(boot_image_update_type), work_buffer, work_buffer_size, Boot0Partition::Package1NormalMain)))) { - return rc; - } + R_TRY(boot0_accessor.Clear(work_buffer, work_buffer_size, Boot0Partition::Package1NormalMain)); + R_TRY(boot0_accessor.Write(GetPackage1Path(boot_image_update_type), work_buffer, work_buffer_size, Boot0Partition::Package1NormalMain)); } return ResultSuccess; } Result Updater::UpdateBootImagesSafe(u64 data_id, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type) { - Result rc; - /* Ensure work buffer is big enough for us to do what we want to do. */ - if (R_FAILED((rc = ValidateWorkBuffer(work_buffer, work_buffer_size)))) { - return rc; - } + R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size)); - if (R_FAILED((rc = romfsMountFromDataArchive(data_id, FsStorageId_NandSystem, GetBootImagePackageMountPath())))) { - if (rc == ResultFsTargetNotFound) { + R_TRY_CATCH(romfsMountFromDataArchive(data_id, FsStorageId_NandSystem, GetBootImagePackageMountPath())) { + R_CATCH(ResultFsTargetNotFound) { return ResultUpdaterBootImagePackageNotFound; } - return rc; - } + } R_END_TRY_CATCH; ON_SCOPE_EXIT { if (R_FAILED(romfsUnmount(GetBootImagePackageMountPath()))) { std::abort(); } }; { Boot0Accessor boot0_accessor; - if (R_FAILED((rc = boot0_accessor.Initialize()))) { - return rc; - } + R_TRY(boot0_accessor.Initialize()); ON_SCOPE_EXIT { boot0_accessor.Finalize(); }; Boot1Accessor boot1_accessor; - if (R_FAILED((rc = boot1_accessor.Initialize()))) { - return rc; - } + R_TRY(boot1_accessor.Initialize()); ON_SCOPE_EXIT { boot1_accessor.Finalize(); }; /* Write Package1 sub. */ - if (R_FAILED((rc = boot1_accessor.Clear(work_buffer, work_buffer_size, Boot1Partition::Package1SafeSub)))) { - return rc; - } - if (R_FAILED((rc = boot1_accessor.Write(GetPackage1Path(boot_image_update_type), work_buffer, work_buffer_size, Boot1Partition::Package1SafeSub)))) { - return rc; - } + R_TRY(boot1_accessor.Clear(work_buffer, work_buffer_size, Boot1Partition::Package1SafeSub)); + R_TRY(boot1_accessor.Write(GetPackage1Path(boot_image_update_type), work_buffer, work_buffer_size, Boot1Partition::Package1SafeSub)); /* Write Package2 sub. */ - if (R_FAILED((rc = WritePackage2(work_buffer, work_buffer_size, Package2Type::SafeSub, boot_image_update_type)))) { - return rc; - } + R_TRY(WritePackage2(work_buffer, work_buffer_size, Package2Type::SafeSub, boot_image_update_type)); /* Write BCT sub + BCT main, in that order. */ { @@ -613,103 +463,63 @@ Result Updater::UpdateBootImagesSafe(u64 data_id, void *work_buffer, size_t work void *work = reinterpret_cast(reinterpret_cast(work_buffer) + BctSize); size_t size; - if (R_FAILED((rc = ReadFile(&size, bct, BctSize, GetBctPath(boot_image_update_type))))) { - return rc; - } + R_TRY(ReadFile(&size, bct, BctSize, GetBctPath(boot_image_update_type))); if (HasEks(boot_image_update_type)) { - if (R_FAILED((rc = boot0_accessor.UpdateEks(bct, work)))) { - return rc; - } + R_TRY(boot0_accessor.UpdateEks(bct, work)); } /* Only preserve autorcm if on a unit with unpatched rcm bug. */ if (HasAutoRcmPreserve(boot_image_update_type) && !IsRcmBugPatched()) { - if (R_FAILED((rc = boot0_accessor.PreserveAutoRcm(bct, work, Boot0Partition::BctSafeSub)))) { - return rc; - } - if (R_FAILED((rc = boot0_accessor.Write(bct, BctSize, Boot0Partition::BctSafeSub)))) { - return rc; - } - if (R_FAILED((rc = boot0_accessor.PreserveAutoRcm(bct, work, Boot0Partition::BctSafeMain)))) { - return rc; - } - if (R_FAILED((rc = boot0_accessor.Write(bct, BctSize, Boot0Partition::BctSafeMain)))) { - return rc; - } + R_TRY(boot0_accessor.PreserveAutoRcm(bct, work, Boot0Partition::BctSafeSub)); + R_TRY(boot0_accessor.Write(bct, BctSize, Boot0Partition::BctSafeSub)); + R_TRY(boot0_accessor.PreserveAutoRcm(bct, work, Boot0Partition::BctSafeMain)); + R_TRY(boot0_accessor.Write(bct, BctSize, Boot0Partition::BctSafeMain)); } else { - if (R_FAILED((rc = boot0_accessor.Write(bct, BctSize, Boot0Partition::BctSafeSub)))) { - return rc; - } - if (R_FAILED((rc = boot0_accessor.Write(bct, BctSize, Boot0Partition::BctSafeMain)))) { - return rc; - } + R_TRY(boot0_accessor.Write(bct, BctSize, Boot0Partition::BctSafeSub)); + R_TRY(boot0_accessor.Write(bct, BctSize, Boot0Partition::BctSafeMain)); } } /* Write Package2 main. */ - if (R_FAILED((rc = WritePackage2(work_buffer, work_buffer_size, Package2Type::SafeMain, boot_image_update_type)))) { - return rc; - } + R_TRY(WritePackage2(work_buffer, work_buffer_size, Package2Type::SafeMain, boot_image_update_type)); /* Write Package1 main. */ - if (R_FAILED((rc = boot1_accessor.Clear(work_buffer, work_buffer_size, Boot1Partition::Package1SafeMain)))) { - return rc; - } - if (R_FAILED((rc = boot1_accessor.Write(GetPackage1Path(boot_image_update_type), work_buffer, work_buffer_size, Boot1Partition::Package1SafeMain)))) { - return rc; - } + R_TRY(boot1_accessor.Clear(work_buffer, work_buffer_size, Boot1Partition::Package1SafeMain)); + R_TRY(boot1_accessor.Write(GetPackage1Path(boot_image_update_type), work_buffer, work_buffer_size, Boot1Partition::Package1SafeMain)); } return ResultSuccess; } Result Updater::SetVerificationNeeded(BootModeType mode, bool needed, void *work_buffer, size_t work_buffer_size) { - Result rc; - /* Ensure work buffer is big enough for us to do what we want to do. */ - if (R_FAILED((rc = ValidateWorkBuffer(work_buffer, work_buffer_size)))) { - return rc; - } + R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size)); /* Initialize boot0 save accessor. */ BisSave save; - if (R_FAILED((rc = save.Initialize(work_buffer, work_buffer_size)))) { - return rc; - } + R_TRY(save.Initialize(work_buffer, work_buffer_size)); ON_SCOPE_EXIT { save.Finalize(); }; /* Load save from NAND. */ - if (R_FAILED((rc = save.Load()))) { - return rc; - } + R_TRY(save.Load()); /* Set whether we need to verify, then save to nand. */ save.SetNeedsVerification(mode, needed); - if (R_FAILED((rc = save.Save()))) { - return rc; - } + R_TRY(save.Save()); return ResultSuccess; } Result Updater::GetPackage2Hash(void *dst_hash, size_t package2_size, void *work_buffer, size_t work_buffer_size, Package2Type which) { - Result rc; - Package2Accessor accessor(which); - if (R_FAILED((rc = accessor.Initialize()))) { - return rc; - } + R_TRY(accessor.Initialize()); ON_SCOPE_EXIT { accessor.Finalize(); }; return accessor.GetHash(dst_hash, package2_size, work_buffer, work_buffer_size, Package2Partition::Package2); } Result Updater::WritePackage2(void *work_buffer, size_t work_buffer_size, Package2Type which, BootImageUpdateType boot_image_update_type) { - Result rc; - Package2Accessor accessor(which); - if (R_FAILED((rc = accessor.Initialize()))) { - return rc; - } + R_TRY(accessor.Initialize()); ON_SCOPE_EXIT { accessor.Finalize(); }; return accessor.Write(GetPackage2Path(boot_image_update_type), work_buffer, work_buffer_size, Package2Partition::Package2); diff --git a/stratosphere/boot/source/updater/updater_bis_management.cpp b/stratosphere/boot/source/updater/updater_bis_management.cpp index 388ef779e..355976569 100644 --- a/stratosphere/boot/source/updater/updater_bis_management.cpp +++ b/stratosphere/boot/source/updater/updater_bis_management.cpp @@ -20,11 +20,9 @@ #include "updater_bis_management.hpp" Result BisAccessor::Initialize() { - Result rc = fsOpenBisStorage(&this->storage, this->partition_id); - if (R_SUCCEEDED(rc)) { - this->active = true; - } - return rc; + R_TRY(fsOpenBisStorage(&this->storage, this->partition_id)); + this->active = true; + return ResultSuccess; } void BisAccessor::Finalize() { @@ -49,7 +47,6 @@ Result BisAccessor::Write(u64 offset, const void *src, size_t size) { } Result BisAccessor::Write(u64 offset, size_t size, const char *bip_path, void *work_buffer, size_t work_buffer_size) { - Result rc; if (offset % SectorAlignment != 0 || work_buffer_size % SectorAlignment != 0) { std::abort(); } @@ -74,9 +71,7 @@ Result BisAccessor::Write(u64 offset, size_t size, const char *bip_path, void *w } size_t aligned_size = ((read_size + SectorAlignment - 1) / SectorAlignment) * SectorAlignment; - if (R_FAILED((rc = this->Write(offset + written, work_buffer, aligned_size)))) { - return rc; - } + R_TRY(this->Write(offset + written, work_buffer, aligned_size)); written += read_size; if (read_size != work_buffer_size) { @@ -87,7 +82,6 @@ Result BisAccessor::Write(u64 offset, size_t size, const char *bip_path, void *w } Result BisAccessor::Clear(u64 offset, u64 size, void *work_buffer, size_t work_buffer_size) { - Result rc; if (offset % SectorAlignment != 0 || work_buffer_size % SectorAlignment != 0) { std::abort(); } @@ -97,16 +91,13 @@ Result BisAccessor::Clear(u64 offset, u64 size, void *work_buffer, size_t work_b size_t written = 0; while (written < size) { size_t cur_write_size = std::min(work_buffer_size, size - written); - if (R_FAILED((rc = this->Write(offset + written, work_buffer, cur_write_size)))) { - return rc; - } + R_TRY(this->Write(offset + written, work_buffer, cur_write_size)); written += cur_write_size; } return ResultSuccess; } Result BisAccessor::GetHash(void *dst, u64 offset, u64 size, u64 hash_size, void *work_buffer, size_t work_buffer_size) { - Result rc; if (offset % SectorAlignment != 0 || work_buffer_size % SectorAlignment != 0) { std::abort(); } @@ -118,9 +109,7 @@ Result BisAccessor::GetHash(void *dst, u64 offset, u64 size, u64 hash_size, void while (total_read < hash_size) { size_t cur_read_size = std::min(work_buffer_size, size - total_read); size_t cur_update_size = std::min(cur_read_size, hash_size - total_read); - if (R_FAILED((rc = this->Read(work_buffer, cur_read_size, offset + total_read)))) { - return rc; - } + R_TRY(this->Read(work_buffer, cur_read_size, offset + total_read)); sha256ContextUpdate(&sha_ctx, work_buffer, cur_update_size); total_read += cur_read_size; } @@ -152,10 +141,7 @@ void Boot0Accessor::CopyEks(void *dst_bct, const void *src_eks, size_t eks_index Result Boot0Accessor::UpdateEks(void *dst_bct, void *eks_work_buffer) { size_t read_size; - Result rc = this->Read(&read_size, eks_work_buffer, EksSize, Boot0Partition::Eks); - if (R_FAILED(rc)) { - return rc; - } + R_TRY(this->Read(&read_size, eks_work_buffer, EksSize, Boot0Partition::Eks)); return this->UpdateEksManually(dst_bct, eks_work_buffer); } @@ -169,10 +155,7 @@ Result Boot0Accessor::PreserveAutoRcm(void *dst_bct, void *work_buffer, Boot0Par std::memset(work_buffer, 0, BctSize); size_t read_size; - Result rc = this->Read(&read_size, work_buffer, BctSize, which); - if (R_FAILED(rc)) { - return rc; - } + R_TRY(this->Read(&read_size, work_buffer, BctSize, which)); void *dst_pubk = reinterpret_cast(reinterpret_cast(dst_bct) + BctPubkOffset); void *src_pubk = reinterpret_cast(reinterpret_cast(work_buffer) + BctPubkOffset); diff --git a/stratosphere/boot/source/updater/updater_bis_management.hpp b/stratosphere/boot/source/updater/updater_bis_management.hpp index 299109eab..f8f3f196b 100644 --- a/stratosphere/boot/source/updater/updater_bis_management.hpp +++ b/stratosphere/boot/source/updater/updater_bis_management.hpp @@ -143,10 +143,7 @@ class PartitionAccessor : public BisAccessor { std::abort(); } - Result rc = BisAccessor::Read(dst, entry->size, entry->offset); - if (R_FAILED(rc)) { - return rc; - } + R_TRY(BisAccessor::Read(dst, entry->size, entry->offset)); *out_size = entry->size; return ResultSuccess; diff --git a/stratosphere/boot/source/updater/updater_bis_save.cpp b/stratosphere/boot/source/updater/updater_bis_save.cpp index 2258b170a..0df33affc 100644 --- a/stratosphere/boot/source/updater/updater_bis_save.cpp +++ b/stratosphere/boot/source/updater/updater_bis_save.cpp @@ -35,10 +35,7 @@ Result BisSave::Initialize(void *work_buffer, size_t work_buffer_size) { std::abort(); } - Result rc = this->accessor.Initialize(); - if (R_FAILED(rc)) { - return rc; - } + R_TRY(this->accessor.Initialize()); this->save_buffer = work_buffer; return ResultSuccess; } diff --git a/stratosphere/dmnt/source/dmnt_cheat_manager.cpp b/stratosphere/dmnt/source/dmnt_cheat_manager.cpp index 2f74cb291..543115c99 100644 --- a/stratosphere/dmnt/source/dmnt_cheat_manager.cpp +++ b/stratosphere/dmnt/source/dmnt_cheat_manager.cpp @@ -852,7 +852,7 @@ Result DmntCheatManager::ForceOpenCheatProcess() { /* Get the current application process ID. */ R_TRY(pmdmntGetApplicationPid(&g_cheat_process_metadata.process_id)); - auto proc_guard = SCOPE_EXIT { + auto proc_guard = SCOPE_GUARD { g_cheat_process_metadata.process_id = 0; }; diff --git a/stratosphere/libstratosphere b/stratosphere/libstratosphere index 7e0ed3b38..3274848b5 160000 --- a/stratosphere/libstratosphere +++ b/stratosphere/libstratosphere @@ -1 +1 @@ -Subproject commit 7e0ed3b38f437791fdd398c7058c376c9d2a6853 +Subproject commit 3274848b5612a9fa3a7306c7c4ca0a4b041c150c