mirror of
https://github.com/Atmosphere-NX/Atmosphere
synced 2024-11-09 22:56:35 +00:00
boot: refactor for R_TRY
This commit is contained in:
parent
491383c637
commit
7b6050a0cb
16 changed files with 223 additions and 623 deletions
|
@ -53,16 +53,12 @@ Result BatteryDriver::Write(u8 addr, u16 val) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Result BatteryDriver::ReadWrite(u8 addr, u16 mask, u16 val) {
|
Result BatteryDriver::ReadWrite(u8 addr, u16 mask, u16 val) {
|
||||||
Result rc;
|
|
||||||
u16 cur_val;
|
u16 cur_val;
|
||||||
if (R_FAILED((rc = this->Read(addr, &cur_val)))) {
|
R_TRY(this->Read(addr, &cur_val));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
const u16 new_val = (cur_val & ~mask) | val;
|
const u16 new_val = (cur_val & ~mask) | val;
|
||||||
if (R_FAILED((rc = this->Write(addr, new_val)))) {
|
R_TRY(this->Write(addr, new_val));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,35 +89,21 @@ Result BatteryDriver::UnlockVfSoc() {
|
||||||
}
|
}
|
||||||
|
|
||||||
Result BatteryDriver::LockModelTable() {
|
Result BatteryDriver::LockModelTable() {
|
||||||
Result rc;
|
R_TRY(this->Write(Max17050ModelAccess0, 0x0000));
|
||||||
if (R_FAILED((rc = this->Write(Max17050ModelAccess0, 0x0000)))) {
|
R_TRY(this->Write(Max17050ModelAccess1, 0x0000));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
if (R_FAILED((rc = this->Write(Max17050ModelAccess1, 0x0000)))) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result BatteryDriver::UnlockModelTable() {
|
Result BatteryDriver::UnlockModelTable() {
|
||||||
Result rc;
|
R_TRY(this->Write(Max17050ModelAccess0, 0x0059));
|
||||||
if (R_FAILED((rc = this->Write(Max17050ModelAccess0, 0x0059)))) {
|
R_TRY(this->Write(Max17050ModelAccess1, 0x00C4));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
if (R_FAILED((rc = this->Write(Max17050ModelAccess1, 0x00C4)))) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result BatteryDriver::SetModelTable(const u16 *model_table) {
|
Result BatteryDriver::SetModelTable(const u16 *model_table) {
|
||||||
Result rc;
|
|
||||||
for (size_t i = 0; i < Max17050ModelChrTblSize; i++) {
|
for (size_t i = 0; i < Max17050ModelChrTblSize; i++) {
|
||||||
if (R_FAILED((rc = this->Write(Max17050ModelChrTblStart + i, model_table[i])))) {
|
R_TRY(this->Write(Max17050ModelChrTblStart + i, model_table[i]));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,52 +133,31 @@ bool BatteryDriver::IsModelTableSet(const u16 *model_table) {
|
||||||
|
|
||||||
Result BatteryDriver::InitializeBatteryParameters() {
|
Result BatteryDriver::InitializeBatteryParameters() {
|
||||||
const Max17050Parameters *params = GetBatteryParameters();
|
const Max17050Parameters *params = GetBatteryParameters();
|
||||||
Result rc = ResultSuccess;
|
|
||||||
|
|
||||||
if (IsPowerOnReset()) {
|
if (IsPowerOnReset()) {
|
||||||
/* Do initial config. */
|
/* Do initial config. */
|
||||||
if (R_FAILED((rc = this->ReadWrite(Max17050MiscCfg, 0x8000, 0x8000)))) {
|
R_TRY(this->ReadWrite(Max17050MiscCfg, 0x8000, 0x8000));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
svcSleepThread(500'000'000ul);
|
svcSleepThread(500'000'000ul);
|
||||||
|
|
||||||
if (R_FAILED((rc = this->Write(Max17050Config, 0x7210)))) {
|
R_TRY(this->Write(Max17050Config, 0x7210));
|
||||||
return rc;
|
R_TRY(this->Write(Max17050FilterCfg, 0x8784));
|
||||||
}
|
R_TRY(this->Write(Max17050RelaxCfg, params->relaxcfg));
|
||||||
if (R_FAILED((rc = this->Write(Max17050FilterCfg, 0x8784)))) {
|
R_TRY(this->Write(Max17050LearnCfg, 0x2603));
|
||||||
return rc;
|
R_TRY(this->Write(Max17050FullSocThr, params->fullsocthr));
|
||||||
}
|
R_TRY(this->Write(Max17050IAvgEmpty, params->iavgempty));
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Unlock model table, write model table. */
|
/* Unlock model table, write model table. */
|
||||||
do {
|
do {
|
||||||
if (R_FAILED((rc = this->UnlockModelTable()))) {
|
R_TRY(this->UnlockModelTable());
|
||||||
return rc;
|
R_TRY(this->SetModelTable(params->modeltbl));
|
||||||
}
|
|
||||||
if (R_FAILED((rc = this->SetModelTable(params->modeltbl)))) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
} while (!this->IsModelTableSet(params->modeltbl));
|
} while (!this->IsModelTableSet(params->modeltbl));
|
||||||
|
|
||||||
/* Lock model table. */
|
/* Lock model table. */
|
||||||
size_t lock_i = 0;
|
size_t lock_i = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
lock_i++;
|
lock_i++;
|
||||||
if (R_FAILED((rc = this->LockModelTable()))) {
|
R_TRY(this->LockModelTable());
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this->IsModelTableLocked()) {
|
if (this->IsModelTableLocked()) {
|
||||||
break;
|
break;
|
||||||
|
@ -212,15 +173,9 @@ Result BatteryDriver::InitializeBatteryParameters() {
|
||||||
while (!this->WriteValidate(Max17050RComp0, params->rcomp0)) { /* ... */ }
|
while (!this->WriteValidate(Max17050RComp0, params->rcomp0)) { /* ... */ }
|
||||||
while (!this->WriteValidate(Max17050TempCo, params->tempco)) { /* ... */ }
|
while (!this->WriteValidate(Max17050TempCo, params->tempco)) { /* ... */ }
|
||||||
|
|
||||||
if (R_FAILED((rc = this->Write(Max17050IChgTerm, params->ichgterm)))) {
|
R_TRY(this->Write(Max17050IChgTerm, params->ichgterm));
|
||||||
return rc;
|
R_TRY(this->Write(Max17050TGain, params->tgain));
|
||||||
}
|
R_TRY(this->Write(Max17050TOff, params->toff));
|
||||||
if (R_FAILED((rc = this->Write(Max17050TGain, params->tgain)))) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
if (R_FAILED((rc = this->Write(Max17050TOff, params->toff)))) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (!this->WriteValidate(Max17050VEmpty, params->vempty)) { /* ... */ }
|
while (!this->WriteValidate(Max17050VEmpty, params->vempty)) { /* ... */ }
|
||||||
while (!this->WriteValidate(Max17050QResidual00, params->qresidual00)) { /* ... */ }
|
while (!this->WriteValidate(Max17050QResidual00, params->qresidual00)) { /* ... */ }
|
||||||
|
@ -231,9 +186,7 @@ Result BatteryDriver::InitializeBatteryParameters() {
|
||||||
|
|
||||||
/* Write full capacity parameters. */
|
/* Write full capacity parameters. */
|
||||||
while (!this->WriteValidate(Max17050FullCap, params->fullcap)) { /* ... */ }
|
while (!this->WriteValidate(Max17050FullCap, params->fullcap)) { /* ... */ }
|
||||||
if (R_FAILED((rc = this->Write(Max17050DesignCap, params->vffullcap)))) {
|
R_TRY(this->Write(Max17050DesignCap, params->vffullcap));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
while (!this->WriteValidate(Max17050FullCapNom, params->vffullcap)) { /* ... */ }
|
while (!this->WriteValidate(Max17050FullCapNom, params->vffullcap)) { /* ... */ }
|
||||||
|
|
||||||
svcSleepThread(350'000'000ul);
|
svcSleepThread(350'000'000ul);
|
||||||
|
@ -241,24 +194,12 @@ Result BatteryDriver::InitializeBatteryParameters() {
|
||||||
/* Write VFSOC to VFSOC 0. */
|
/* Write VFSOC to VFSOC 0. */
|
||||||
u16 vfsoc, qh;
|
u16 vfsoc, qh;
|
||||||
{
|
{
|
||||||
if (R_FAILED((rc = this->Read(Max17050SocVf, &vfsoc)))) {
|
R_TRY(this->Read(Max17050SocVf, &vfsoc));
|
||||||
return rc;
|
R_TRY(this->UnlockVfSoc());
|
||||||
}
|
R_TRY(this->Write(Max17050SocVf0, vfsoc));
|
||||||
if (R_FAILED((rc = this->UnlockVfSoc()))) {
|
R_TRY(this->Read(Max17050Qh, &qh));
|
||||||
return rc;
|
R_TRY(this->Write(Max17050Qh0, qh));
|
||||||
}
|
R_TRY(this->LockVfSoc());
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Write cycles. */
|
/* Write cycles. */
|
||||||
|
@ -273,25 +214,17 @@ Result BatteryDriver::InitializeBatteryParameters() {
|
||||||
while (!this->WriteValidate(Max17050DPAcc, 0x0C80)) { /* ... */ }
|
while (!this->WriteValidate(Max17050DPAcc, 0x0C80)) { /* ... */ }
|
||||||
while (!this->WriteValidate(Max17050DQAcc, dqacc)) { /* ... */ }
|
while (!this->WriteValidate(Max17050DQAcc, dqacc)) { /* ... */ }
|
||||||
while (!this->WriteValidate(Max17050FullCap, params->fullcap)) { /* ... */ }
|
while (!this->WriteValidate(Max17050FullCap, params->fullcap)) { /* ... */ }
|
||||||
if (R_FAILED((rc = this->Write(Max17050DesignCap, params->vffullcap)))) {
|
R_TRY(this->Write(Max17050DesignCap, params->vffullcap));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
while (!this->WriteValidate(Max17050FullCapNom, params->vffullcap)) { /* ... */ }
|
while (!this->WriteValidate(Max17050FullCapNom, params->vffullcap)) { /* ... */ }
|
||||||
if (R_FAILED((rc = this->Write(Max17050SocRep, vfsoc)))) {
|
R_TRY(this->Write(Max17050SocRep, vfsoc));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Finish initialization. */
|
/* Finish initialization. */
|
||||||
{
|
{
|
||||||
u16 status;
|
u16 status;
|
||||||
if (R_FAILED((rc = this->Read(Max17050Status, &status)))) {
|
R_TRY(this->Read(Max17050Status, &status));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
while (!this->WriteValidate(Max17050Status, status & 0xFFFD)) { /* ... */ }
|
while (!this->WriteValidate(Max17050Status, status & 0xFFFD)) { /* ... */ }
|
||||||
}
|
}
|
||||||
if (R_FAILED((rc = this->Write(Max17050CGain, 0x7FFF)))) {
|
R_TRY(this->Write(Max17050CGain, 0x7FFF));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
|
@ -300,50 +233,35 @@ Result BatteryDriver::InitializeBatteryParameters() {
|
||||||
Result BatteryDriver::IsBatteryRemoved(bool *out) {
|
Result BatteryDriver::IsBatteryRemoved(bool *out) {
|
||||||
/* N doesn't check result, but we will. */
|
/* N doesn't check result, but we will. */
|
||||||
u16 val = 0;
|
u16 val = 0;
|
||||||
Result rc = this->Read(Max17050Status, &val);
|
R_TRY(this->Read(Max17050Status, &val));
|
||||||
if (R_FAILED(rc)) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
*out = (val & 0x0008) == 0x0008;
|
*out = (val & 0x0008) == 0x0008;
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result BatteryDriver::GetTemperature(double *out) {
|
Result BatteryDriver::GetTemperature(double *out) {
|
||||||
u16 val = 0;
|
u16 val = 0;
|
||||||
Result rc = this->Read(Max17050Temperature, &val);
|
R_TRY(this->Read(Max17050Temperature, &val));
|
||||||
if (R_FAILED(rc)) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
*out = static_cast<double>(val) * double(0.00390625);
|
*out = static_cast<double>(val) * double(0.00390625);
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result BatteryDriver::GetAverageVCell(u32 *out) {
|
Result BatteryDriver::GetAverageVCell(u32 *out) {
|
||||||
u16 val = 0;
|
u16 val = 0;
|
||||||
Result rc = this->Read(Max17050AverageVCell, &val);
|
R_TRY(this->Read(Max17050AverageVCell, &val));
|
||||||
if (R_FAILED(rc)) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
*out = (625 * u32(val >> 3)) / 1000;
|
*out = (625 * u32(val >> 3)) / 1000;
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result BatteryDriver::GetSocRep(double *out) {
|
Result BatteryDriver::GetSocRep(double *out) {
|
||||||
u16 val = 0;
|
u16 val = 0;
|
||||||
Result rc = this->Read(Max17050SocRep, &val);
|
R_TRY(this->Read(Max17050SocRep, &val));
|
||||||
if (R_FAILED(rc)) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
*out = static_cast<double>(val) * double(0.00390625);
|
*out = static_cast<double>(val) * double(0.00390625);
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result BatteryDriver::GetBatteryPercentage(size_t *out) {
|
Result BatteryDriver::GetBatteryPercentage(size_t *out) {
|
||||||
double raw_charge;
|
double raw_charge;
|
||||||
Result rc = this->GetSocRep(&raw_charge);
|
R_TRY(this->GetSocRep(&raw_charge));
|
||||||
if (R_FAILED(rc)) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
int converted_percentage = (((raw_charge - 3.93359375) * 98.0) / 94.2304688) + 2.0;
|
int converted_percentage = (((raw_charge - 3.93359375) * 98.0) / 94.2304688) + 2.0;
|
||||||
if (converted_percentage < 1) {
|
if (converted_percentage < 1) {
|
||||||
*out = 1;
|
*out = 1;
|
||||||
|
@ -361,10 +279,7 @@ Result BatteryDriver::SetShutdownTimer() {
|
||||||
|
|
||||||
Result BatteryDriver::GetShutdownEnabled(bool *out) {
|
Result BatteryDriver::GetShutdownEnabled(bool *out) {
|
||||||
u16 val = 0;
|
u16 val = 0;
|
||||||
Result rc = this->Read(Max17050Config, &val);
|
R_TRY(this->Read(Max17050Config, &val));
|
||||||
if (R_FAILED(rc)) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
*out = (val & 0x0040) != 0;
|
*out = (val & 0x0040) != 0;
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,20 +55,13 @@ static Result ValidateCalibrationCrc16(const void *data, size_t size) {
|
||||||
|
|
||||||
static Result GetBatteryVendorImpl(u32 *vendor) {
|
static Result GetBatteryVendorImpl(u32 *vendor) {
|
||||||
FsStorage s;
|
FsStorage s;
|
||||||
Result rc = fsOpenBisStorage(&s, FsBisStorageId_CalibrationBinary);
|
R_TRY(fsOpenBisStorage(&s, FsBisStorageId_CalibrationBinary));
|
||||||
if (R_FAILED(rc)) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
ON_SCOPE_EXIT { fsStorageClose(&s); };
|
ON_SCOPE_EXIT { fsStorageClose(&s); };
|
||||||
|
|
||||||
u8 battery_lot[BatteryLotSize];
|
u8 battery_lot[BatteryLotSize];
|
||||||
if (R_FAILED((rc = fsStorageRead(&s, BatteryLotOffset, battery_lot, sizeof(battery_lot))))) {
|
R_TRY(fsStorageRead(&s, BatteryLotOffset, battery_lot, sizeof(battery_lot)));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (R_FAILED((rc = ValidateCalibrationCrc16(battery_lot, sizeof(battery_lot))))) {
|
R_TRY(ValidateCalibrationCrc16(battery_lot, sizeof(battery_lot)));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
*vendor = battery_lot[7];
|
*vendor = battery_lot[7];
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
|
@ -76,20 +69,13 @@ static Result GetBatteryVendorImpl(u32 *vendor) {
|
||||||
|
|
||||||
static Result GetBatteryVersionImpl(u32 *version) {
|
static Result GetBatteryVersionImpl(u32 *version) {
|
||||||
FsStorage s;
|
FsStorage s;
|
||||||
Result rc = fsOpenBisStorage(&s, FsBisStorageId_CalibrationBinary);
|
R_TRY(fsOpenBisStorage(&s, FsBisStorageId_CalibrationBinary));
|
||||||
if (R_FAILED(rc)) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
ON_SCOPE_EXIT { fsStorageClose(&s); };
|
ON_SCOPE_EXIT { fsStorageClose(&s); };
|
||||||
|
|
||||||
u8 battery_version[BatteryVersionSize];
|
u8 battery_version[BatteryVersionSize];
|
||||||
if (R_FAILED((rc = fsStorageRead(&s, BatteryVersionOffset, battery_version, sizeof(battery_version))))) {
|
R_TRY(fsStorageRead(&s, BatteryVersionOffset, battery_version, sizeof(battery_version)));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (R_FAILED((rc = ValidateCalibrationCrc16(battery_version, sizeof(battery_version))))) {
|
R_TRY(ValidateCalibrationCrc16(battery_version, sizeof(battery_version)));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
*version = battery_version[0];
|
*version = battery_version[0];
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
|
|
|
@ -27,16 +27,11 @@ Result ChargerDriver::Write(u8 addr, u8 val) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Result ChargerDriver::ReadWrite(u8 addr, u8 mask, u8 val) {
|
Result ChargerDriver::ReadWrite(u8 addr, u8 mask, u8 val) {
|
||||||
Result rc;
|
|
||||||
u8 cur_val;
|
u8 cur_val;
|
||||||
if (R_FAILED((rc = this->Read(addr, &cur_val)))) {
|
R_TRY(this->Read(addr, &cur_val));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
const u8 new_val = (cur_val & ~mask) | val;
|
const u8 new_val = (cur_val & ~mask) | val;
|
||||||
if (R_FAILED((rc = this->Write(addr, new_val)))) {
|
R_TRY(this->Write(addr, new_val));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,56 +40,21 @@ Result ChargerDriver::Initialize() {
|
||||||
}
|
}
|
||||||
|
|
||||||
Result ChargerDriver::Initialize(bool set_input_current_limit) {
|
Result ChargerDriver::Initialize(bool set_input_current_limit) {
|
||||||
Result rc;
|
|
||||||
if (set_input_current_limit) {
|
if (set_input_current_limit) {
|
||||||
if (R_FAILED((rc = this->SetInputCurrentLimit(InputCurrentLimit_500mA)))) {
|
R_TRY(this->SetInputCurrentLimit(InputCurrentLimit_500mA));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (R_FAILED((rc = this->SetChargeVoltageLimit(4208)))) {
|
R_TRY(this->SetChargeVoltageLimit(4208));
|
||||||
return rc;
|
R_TRY(this->SetFastChargeCurrentLimit(512));
|
||||||
}
|
R_TRY(this->SetForce20PercentChargeCurrent(false));
|
||||||
|
R_TRY(this->SetPreChargeCurrentLimit(128));
|
||||||
if (R_FAILED((rc = this->SetFastChargeCurrentLimit(512)))) {
|
R_TRY(this->SetTerminationCurrentLimit(128));
|
||||||
return rc;
|
R_TRY(this->SetMinimumSystemVoltageLimit(3000));
|
||||||
}
|
R_TRY(this->SetWatchdogTimerSetting(WatchdogTimerSetting_Disabled));
|
||||||
|
R_TRY(this->SetChargingSafetyTimerEnabled(false));
|
||||||
if (R_FAILED((rc = this->SetForce20PercentChargeCurrent(false)))) {
|
R_TRY(this->ResetWatchdogTimer());
|
||||||
return rc;
|
R_TRY(this->SetBoostModeCurrentLimit(BoostModeCurrentLimit_500mA));
|
||||||
}
|
R_TRY(this->SetHiZEnabled(false));
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
}
|
}
|
||||||
|
@ -158,20 +118,14 @@ Result ChargerDriver::SetHiZEnabled(bool enabled) {
|
||||||
|
|
||||||
Result ChargerDriver::GetInputCurrentLimit(InputCurrentLimit *out) {
|
Result ChargerDriver::GetInputCurrentLimit(InputCurrentLimit *out) {
|
||||||
u8 limit;
|
u8 limit;
|
||||||
Result rc = this->Read(Bq24193InputSourceControl, &limit);
|
R_TRY(this->Read(Bq24193InputSourceControl, &limit));
|
||||||
if (R_FAILED(rc)) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
*out = static_cast<InputCurrentLimit>(limit);
|
*out = static_cast<InputCurrentLimit>(limit);
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result ChargerDriver::GetChargeVoltageLimit(u32 *out) {
|
Result ChargerDriver::GetChargeVoltageLimit(u32 *out) {
|
||||||
u8 reg;
|
u8 reg;
|
||||||
Result rc = this->Read(Bq24193ChargeVoltageControl, ®);
|
R_TRY(this->Read(Bq24193ChargeVoltageControl, ®));
|
||||||
if (R_FAILED(rc)) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
*out = DecodeChargeVoltageLimit(reg);
|
*out = DecodeChargeVoltageLimit(reg);
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,16 +24,14 @@ static Result RetryUntilSuccess(F f) {
|
||||||
|
|
||||||
u64 cur_time = 0;
|
u64 cur_time = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
Result rc = f();
|
R_TRY_CLEANUP(f(), {
|
||||||
if (R_SUCCEEDED(rc)) {
|
|
||||||
return rc;
|
|
||||||
} else {
|
|
||||||
cur_time += retry_interval;
|
cur_time += retry_interval;
|
||||||
if (cur_time >= timeout) {
|
if (cur_time < timeout) {
|
||||||
return rc;
|
svcSleepThread(retry_interval);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
svcSleepThread(retry_interval);
|
return ResultSuccess;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,11 +33,7 @@ void PmicDriver::RebootSystem() {
|
||||||
|
|
||||||
Result PmicDriver::GetAcOk(bool *out) {
|
Result PmicDriver::GetAcOk(bool *out) {
|
||||||
u8 power_status;
|
u8 power_status;
|
||||||
Result rc = this->GetPowerStatus(&power_status);
|
R_TRY(this->GetPowerStatus(&power_status));
|
||||||
if (R_FAILED(rc)) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
*out = (power_status & 0x02) != 0;
|
*out = (power_status & 0x02) != 0;
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
}
|
}
|
||||||
|
@ -59,10 +55,7 @@ Result PmicDriver::GetNvErc(u8 *out) {
|
||||||
|
|
||||||
Result PmicDriver::GetPowerButtonPressed(bool *out) {
|
Result PmicDriver::GetPowerButtonPressed(bool *out) {
|
||||||
u8 power_intr;
|
u8 power_intr;
|
||||||
Result rc = this->GetPowerIntr(&power_intr);
|
R_TRY(this->GetPowerIntr(&power_intr));
|
||||||
if (R_FAILED(rc)) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
*out = (power_intr & 0x08) != 0;
|
*out = (power_intr & 0x08) != 0;
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,10 +22,7 @@
|
||||||
Result RtcDriver::ReadRtcRegister(u8 *out, u8 address) {
|
Result RtcDriver::ReadRtcRegister(u8 *out, u8 address) {
|
||||||
const u8 update_addr = 0x04;
|
const u8 update_addr = 0x04;
|
||||||
const u8 update_val = 0x10;
|
const u8 update_val = 0x10;
|
||||||
Result rc = Boot::WriteI2cRegister(this->i2c_session, &update_val, sizeof(update_val), &update_addr, sizeof(update_addr));
|
R_TRY(Boot::WriteI2cRegister(this->i2c_session, &update_val, sizeof(update_val), &update_addr, sizeof(update_addr)));
|
||||||
if (R_FAILED(rc)) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
svcSleepThread(16'000'000ul);
|
svcSleepThread(16'000'000ul);
|
||||||
return Boot::ReadI2cRegister(this->i2c_session, out, sizeof(*out), &address, sizeof(address));
|
return Boot::ReadI2cRegister(this->i2c_session, out, sizeof(*out), &address, sizeof(address));
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,10 +31,7 @@ static Result I2cSendHandler(const u8 **cur_cmd, u8 **cur_dst, I2cSessionImpl& s
|
||||||
size_t num_bytes = (**cur_cmd);
|
size_t num_bytes = (**cur_cmd);
|
||||||
(*cur_cmd)++;
|
(*cur_cmd)++;
|
||||||
|
|
||||||
Result rc = I2cDriver::Send(session, *cur_cmd, num_bytes, option);
|
R_TRY(I2cDriver::Send(session, *cur_cmd, num_bytes, option));
|
||||||
if (R_FAILED(rc)) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
(*cur_cmd) += num_bytes;
|
(*cur_cmd) += num_bytes;
|
||||||
|
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
|
@ -49,10 +46,7 @@ static Result I2cReceiveHandler(const u8 **cur_cmd, u8 **cur_dst, I2cSessionImpl
|
||||||
size_t num_bytes = (**cur_cmd);
|
size_t num_bytes = (**cur_cmd);
|
||||||
(*cur_cmd)++;
|
(*cur_cmd)++;
|
||||||
|
|
||||||
Result rc = I2cDriver::Receive(session, *cur_dst, num_bytes, option);
|
R_TRY(I2cDriver::Receive(session, *cur_dst, num_bytes, option));
|
||||||
if (R_FAILED(rc)) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
(*cur_dst) += num_bytes;
|
(*cur_dst) += num_bytes;
|
||||||
|
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
|
@ -156,10 +150,7 @@ Result I2cDriver::ExecuteCommandList(I2cSessionImpl &session, void *dst, size_t
|
||||||
std::abort();
|
std::abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
Result rc = g_cmd_handlers[cmd](&cur_cmd, &cur_dst, session);
|
R_TRY(g_cmd_handlers[cmd](&cur_cmd, &cur_dst, session));
|
||||||
if (R_FAILED(rc)) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
|
|
|
@ -146,7 +146,6 @@ Result I2cBusAccessor::Send(const u8 *data, size_t num_bytes, I2cTransactionOpti
|
||||||
std::scoped_lock<HosMutex> lk(this->register_mutex);
|
std::scoped_lock<HosMutex> lk(this->register_mutex);
|
||||||
const u8 *cur_src = data;
|
const u8 *cur_src = data;
|
||||||
size_t remaining = num_bytes;
|
size_t remaining = num_bytes;
|
||||||
Result rc;
|
|
||||||
|
|
||||||
/* Set interrupt enable, clear interrupt status. */
|
/* Set interrupt enable, clear interrupt status. */
|
||||||
WriteRegister(&this->i2c_registers->I2C_INTERRUPT_MASK_REGISTER_0, 0x8E);
|
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;
|
return ResultI2cTimedOut;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (R_FAILED((rc = this->GetAndHandleTransactionResult()))) {
|
R_TRY(this->GetAndHandleTransactionResult());
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WriteRegister(&this->i2c_registers->I2C_INTERRUPT_MASK_REGISTER_0, 0x8C);
|
WriteRegister(&this->i2c_registers->I2C_INTERRUPT_MASK_REGISTER_0, 0x8C);
|
||||||
|
|
||||||
/* Wait for successful completion. */
|
/* Wait for successful completion. */
|
||||||
while (true) {
|
while (true) {
|
||||||
if (R_FAILED((rc = this->GetAndHandleTransactionResult()))) {
|
R_TRY(this->GetAndHandleTransactionResult());
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Check PACKET_XFER_COMPLETE */
|
/* Check PACKET_XFER_COMPLETE */
|
||||||
const u32 interrupt_status = ReadRegister(&this->i2c_registers->I2C_INTERRUPT_STATUS_REGISTER_0);
|
const u32 interrupt_status = ReadRegister(&this->i2c_registers->I2C_INTERRUPT_STATUS_REGISTER_0);
|
||||||
if (interrupt_status & 0x80) {
|
if (interrupt_status & 0x80) {
|
||||||
if (R_FAILED((rc = this->GetAndHandleTransactionResult()))) {
|
R_TRY(this->GetAndHandleTransactionResult());
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -222,7 +215,6 @@ Result I2cBusAccessor::Receive(u8 *out_data, size_t num_bytes, I2cTransactionOpt
|
||||||
std::scoped_lock<HosMutex> lk(this->register_mutex);
|
std::scoped_lock<HosMutex> lk(this->register_mutex);
|
||||||
u8 *cur_dst = out_data;
|
u8 *cur_dst = out_data;
|
||||||
size_t remaining = num_bytes;
|
size_t remaining = num_bytes;
|
||||||
Result rc;
|
|
||||||
|
|
||||||
/* Set interrupt enable, clear interrupt status. */
|
/* Set interrupt enable, clear interrupt status. */
|
||||||
WriteRegister(&this->i2c_registers->I2C_INTERRUPT_MASK_REGISTER_0, 0x8D);
|
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;
|
return ResultI2cTimedOut;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (R_FAILED((rc = this->GetAndHandleTransactionResult()))) {
|
R_TRY(this->GetAndHandleTransactionResult());
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
const u32 fifo_status = ReadRegister(&this->i2c_registers->I2C_FIFO_STATUS_0);
|
const u32 fifo_status = ReadRegister(&this->i2c_registers->I2C_FIFO_STATUS_0);
|
||||||
const size_t fifo_cnt = std::min((remaining + 3) >> 2, static_cast<size_t>(fifo_status & 0xF));
|
const size_t fifo_cnt = std::min((remaining + 3) >> 2, static_cast<size_t>(fifo_status & 0xF));
|
||||||
|
@ -477,14 +467,12 @@ void I2cBusAccessor::HandleTransactionResult(Result result) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Result I2cBusAccessor::GetAndHandleTransactionResult() {
|
Result I2cBusAccessor::GetAndHandleTransactionResult() {
|
||||||
Result rc = this->GetTransactionResult();
|
const Result transaction_res = this->GetTransactionResult();
|
||||||
this->HandleTransactionResult(rc);
|
R_TRY_CLEANUP(transaction_res, {
|
||||||
|
this->HandleTransactionResult(transaction_res);
|
||||||
if (R_FAILED(rc)) {
|
|
||||||
this->ClearInterruptMask();
|
this->ClearInterruptMask();
|
||||||
eventClear(&this->interrupt_event);
|
eventClear(&this->interrupt_event);
|
||||||
return rc;
|
});
|
||||||
}
|
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,8 +27,7 @@ Result I2cCommandListFormatter::CanEnqueue(size_t size) const {
|
||||||
}
|
}
|
||||||
|
|
||||||
Result I2cCommandListFormatter::EnqueueSendCommand(I2cTransactionOption option, const void *src, size_t size) {
|
Result I2cCommandListFormatter::EnqueueSendCommand(I2cTransactionOption option, const void *src, size_t size) {
|
||||||
Result rc = this->CanEnqueue(SendCommandSize + size);
|
R_TRY(this->CanEnqueue(SendCommandSize + size));
|
||||||
if (R_FAILED(rc)) { return rc; }
|
|
||||||
|
|
||||||
this->cmd_list[this->cur_index] = I2cCommand_Send;
|
this->cmd_list[this->cur_index] = I2cCommand_Send;
|
||||||
this->cmd_list[this->cur_index] |= ((option & I2cTransactionOption_Start) != 0) << 6;
|
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 I2cCommandListFormatter::EnqueueReceiveCommand(I2cTransactionOption option, size_t size) {
|
||||||
Result rc = this->CanEnqueue(ReceiveCommandSize);
|
R_TRY(this->CanEnqueue(ReceiveCommandSize));
|
||||||
if (R_FAILED(rc)) { return rc; }
|
|
||||||
|
|
||||||
this->cmd_list[this->cur_index] = I2cCommand_Receive;
|
this->cmd_list[this->cur_index] = I2cCommand_Receive;
|
||||||
this->cmd_list[this->cur_index] |= ((option & I2cTransactionOption_Start) != 0) << 6;
|
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 I2cCommandListFormatter::EnqueueSleepCommand(size_t us) {
|
||||||
Result rc = this->CanEnqueue(SleepCommandSize);
|
R_TRY(this->CanEnqueue(SleepCommandSize));
|
||||||
if (R_FAILED(rc)) { return rc; }
|
|
||||||
|
|
||||||
this->cmd_list[this->cur_index] = I2cCommand_SubCommand;
|
this->cmd_list[this->cur_index] = I2cCommand_SubCommand;
|
||||||
this->cmd_list[this->cur_index] |= I2cSubCommand_Sleep << 2;
|
this->cmd_list[this->cur_index] |= I2cSubCommand_Sleep << 2;
|
||||||
|
|
|
@ -57,49 +57,43 @@ bool I2cDriverSession::IsOpen() const{
|
||||||
|
|
||||||
Result I2cDriverSession::DoTransaction(void *dst, const void *src, size_t num_bytes, I2cTransactionOption option, DriverCommand command){
|
Result I2cDriverSession::DoTransaction(void *dst, const void *src, size_t num_bytes, I2cTransactionOption option, DriverCommand command){
|
||||||
std::scoped_lock<HosMutex> lk(this->bus_accessor_mutex);
|
std::scoped_lock<HosMutex> lk(this->bus_accessor_mutex);
|
||||||
Result rc;
|
|
||||||
|
|
||||||
if (this->bus_accessor->GetBusy()) {
|
if (this->bus_accessor->GetBusy()) {
|
||||||
return ResultI2cBusBusy;
|
return ResultI2cBusBusy;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->bus_accessor->OnStartTransaction();
|
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)))) {
|
R_TRY(this->bus_accessor->StartTransaction(command, this->addressing_mode, this->slave_address));
|
||||||
switch (command) {
|
|
||||||
case DriverCommand_Send:
|
switch (command) {
|
||||||
rc = this->bus_accessor->Send(reinterpret_cast<const u8 *>(src), num_bytes, option, this->addressing_mode, this->slave_address);
|
case DriverCommand_Send:
|
||||||
break;
|
R_TRY(this->bus_accessor->Send(reinterpret_cast<const u8 *>(src), num_bytes, option, this->addressing_mode, this->slave_address));
|
||||||
case DriverCommand_Receive:
|
break;
|
||||||
rc = this->bus_accessor->Receive(reinterpret_cast<u8 *>(dst), num_bytes, option, this->addressing_mode, this->slave_address);
|
case DriverCommand_Receive:
|
||||||
break;
|
R_TRY(this->bus_accessor->Receive(reinterpret_cast<u8 *>(dst), num_bytes, option, this->addressing_mode, this->slave_address));
|
||||||
default:
|
break;
|
||||||
std::abort();
|
default:
|
||||||
}
|
std::abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
this->bus_accessor->OnStopTransaction();
|
return ResultSuccess;
|
||||||
|
|
||||||
return rc;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Result I2cDriverSession::DoTransactionWithRetry(void *dst, const void *src, size_t num_bytes, I2cTransactionOption option, DriverCommand command){
|
Result I2cDriverSession::DoTransactionWithRetry(void *dst, const void *src, size_t num_bytes, I2cTransactionOption option, DriverCommand command){
|
||||||
Result rc;
|
|
||||||
|
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
rc = this->DoTransaction(dst, src, num_bytes, option, command);
|
R_TRY_CATCH(this->DoTransaction(dst, src, num_bytes, option, command)) {
|
||||||
if (rc == ResultI2cTimedOut) {
|
R_CATCH(ResultI2cTimedOut) {
|
||||||
i++;
|
i++;
|
||||||
if (i <= this->max_retries) {
|
if (i <= this->max_retries) {
|
||||||
svcSleepThread(this->retry_wait_time);
|
svcSleepThread(this->retry_wait_time);
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
return ResultI2cBusBusy;
|
||||||
}
|
}
|
||||||
return ResultI2cBusBusy;
|
} R_END_TRY_CATCH;
|
||||||
} else if (R_FAILED(rc)) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
}
|
}
|
||||||
return rc;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,28 +80,20 @@ u32 Updater::GetNcmTitleType(BootModeType mode) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Result Updater::GetVerificationState(VerificationState *out, void *work_buffer, size_t work_buffer_size) {
|
Result Updater::GetVerificationState(VerificationState *out, void *work_buffer, size_t work_buffer_size) {
|
||||||
Result rc;
|
|
||||||
|
|
||||||
/* Always set output to true before doing anything else. */
|
/* Always set output to true before doing anything else. */
|
||||||
out->needs_verify_normal = true;
|
out->needs_verify_normal = true;
|
||||||
out->needs_verify_safe = true;
|
out->needs_verify_safe = true;
|
||||||
|
|
||||||
/* Ensure work buffer is big enough for us to do what we want to do. */
|
/* 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)))) {
|
R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Initialize boot0 save accessor. */
|
/* Initialize boot0 save accessor. */
|
||||||
BisSave save;
|
BisSave save;
|
||||||
if (R_FAILED((rc = save.Initialize(work_buffer, work_buffer_size)))) {
|
R_TRY(save.Initialize(work_buffer, work_buffer_size));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
ON_SCOPE_EXIT { save.Finalize(); };
|
ON_SCOPE_EXIT { save.Finalize(); };
|
||||||
|
|
||||||
/* Load save from NAND. */
|
/* Load save from NAND. */
|
||||||
if (R_FAILED((rc = save.Load()))) {
|
R_TRY(save.Load());
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Read data from save. */
|
/* Read data from save. */
|
||||||
out->needs_verify_normal = save.GetNeedsVerification(BootModeType_Normal);
|
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 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. */
|
/* Always set output to false before doing anything else. */
|
||||||
*out_repaired_normal = false;
|
*out_repaired_normal = false;
|
||||||
*out_repaired_safe = false;
|
*out_repaired_safe = false;
|
||||||
|
|
||||||
/* Ensure work buffer is big enough for us to do what we want to do. */
|
/* 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)))) {
|
R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Get verification state from NAND. */
|
/* Get verification state from NAND. */
|
||||||
VerificationState verification_state;
|
VerificationState verification_state;
|
||||||
if (R_FAILED((rc = GetVerificationState(&verification_state, work_buffer, work_buffer_size)))) {
|
R_TRY(GetVerificationState(&verification_state, work_buffer, work_buffer_size));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* If we don't need to verify anything, we're done. */
|
/* If we don't need to verify anything, we're done. */
|
||||||
if (!verification_state.needs_verify_normal && !verification_state.needs_verify_safe) {
|
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. */
|
/* Verify normal, verify safe as needed. */
|
||||||
if (verification_state.needs_verify_normal) {
|
if (verification_state.needs_verify_normal) {
|
||||||
rc = VerifyBootImagesAndRepairIfNeeded(out_repaired_normal, BootModeType_Normal, work_buffer, work_buffer_size, boot_image_update_type);
|
R_TRY_CATCH(VerifyBootImagesAndRepairIfNeeded(out_repaired_normal, BootModeType_Normal, work_buffer, work_buffer_size, boot_image_update_type)) {
|
||||||
if (rc == ResultUpdaterBootImagePackageNotFound) {
|
R_CATCH(ResultUpdaterBootImagePackageNotFound) {
|
||||||
/* Nintendo considers failure to locate bip a success. TODO: don't do that? */
|
/* Nintendo considers failure to locate bip a success. TODO: don't do that? */
|
||||||
rc = ResultSuccess;
|
}
|
||||||
}
|
} R_END_TRY_CATCH;
|
||||||
if (R_FAILED(rc)) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (verification_state.needs_verify_safe) {
|
if (verification_state.needs_verify_safe) {
|
||||||
rc = VerifyBootImagesAndRepairIfNeeded(out_repaired_safe, BootModeType_Safe, work_buffer, work_buffer_size, boot_image_update_type);
|
R_TRY_CATCH(VerifyBootImagesAndRepairIfNeeded(out_repaired_safe, BootModeType_Safe, work_buffer, work_buffer_size, boot_image_update_type)) {
|
||||||
if (rc == ResultUpdaterBootImagePackageNotFound) {
|
R_CATCH(ResultUpdaterBootImagePackageNotFound) {
|
||||||
/* Nintendo considers failure to locate bip a success. TODO: don't do that? */
|
/* Nintendo considers failure to locate bip a success. TODO: don't do that? */
|
||||||
rc = ResultSuccess;
|
}
|
||||||
}
|
} R_END_TRY_CATCH;
|
||||||
if (R_FAILED(rc)) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result Updater::VerifyBootImagesAndRepairIfNeeded(bool *out_repaired, BootModeType mode, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type) {
|
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). */
|
/* Get system data id for boot images (819/81A/81B/81C). */
|
||||||
u64 bip_data_id;
|
u64 bip_data_id;
|
||||||
if (R_FAILED((rc = GetBootImagePackageDataId(&bip_data_id, mode, work_buffer, work_buffer_size)))) {
|
R_TRY(GetBootImagePackageDataId(&bip_data_id, mode, work_buffer, work_buffer_size));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Verify the boot images in NAND. */
|
/* Verify the boot images in NAND. */
|
||||||
if (R_FAILED((rc = VerifyBootImages(bip_data_id, mode, work_buffer, work_buffer_size, boot_image_update_type)))) {
|
R_TRY_CATCH(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. */
|
R_CATCH(ResultUpdaterNeedsRepairBootImages) {
|
||||||
if (rc != ResultUpdaterNeedsRepairBootImages) {
|
/* Perform repair. */
|
||||||
return rc;
|
*out_repaired = true;
|
||||||
|
R_TRY(UpdateBootImages(bip_data_id, mode, work_buffer, work_buffer_size, boot_image_update_type));
|
||||||
}
|
}
|
||||||
/* Perform repair. */
|
} R_END_TRY_CATCH;
|
||||||
*out_repaired = true;
|
|
||||||
if (R_FAILED((rc = UpdateBootImages(bip_data_id, mode, work_buffer, work_buffer_size, boot_image_update_type)))) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* We've either just verified or just repaired. Either way, we don't need to verify any more. */
|
/* 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);
|
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 Updater::GetBootImagePackageDataId(u64 *out_data_id, BootModeType mode, void *work_buffer, size_t work_buffer_size) {
|
||||||
Result rc;
|
|
||||||
|
|
||||||
/* Ensure we can read content metas. */
|
/* Ensure we can read content metas. */
|
||||||
constexpr size_t MaxContentMetas = 0x40;
|
constexpr size_t MaxContentMetas = 0x40;
|
||||||
if (work_buffer_size < sizeof(NcmMetaRecord) * MaxContentMetas) {
|
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. */
|
/* Open NAND System meta database, list contents. */
|
||||||
NcmContentMetaDatabase meta_db;
|
NcmContentMetaDatabase meta_db;
|
||||||
if (R_FAILED((rc = ncmOpenContentMetaDatabase(FsStorageId_NandSystem, &meta_db)))) {
|
R_TRY(ncmOpenContentMetaDatabase(FsStorageId_NandSystem, &meta_db));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
ON_SCOPE_EXIT { serviceClose(&meta_db.s); };
|
ON_SCOPE_EXIT { serviceClose(&meta_db.s); };
|
||||||
|
|
||||||
NcmMetaRecord *records = reinterpret_cast<NcmMetaRecord *>(work_buffer);
|
NcmMetaRecord *records = reinterpret_cast<NcmMetaRecord *>(work_buffer);
|
||||||
|
@ -212,9 +182,7 @@ Result Updater::GetBootImagePackageDataId(u64 *out_data_id, BootModeType mode, v
|
||||||
const u32 title_type = GetNcmTitleType(mode);
|
const u32 title_type = GetNcmTitleType(mode);
|
||||||
u32 written_entries;
|
u32 written_entries;
|
||||||
u32 total_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)))) {
|
R_TRY(ncmContentMetaDatabaseList(&meta_db, title_type, 0, 0, UINT64_MAX, records, MaxContentMetas * sizeof(*records), &written_entries, &total_entries));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
if (total_entries == 0) {
|
if (total_entries == 0) {
|
||||||
return ResultUpdaterBootImagePackageNotFound;
|
return ResultUpdaterBootImagePackageNotFound;
|
||||||
}
|
}
|
||||||
|
@ -227,9 +195,7 @@ Result Updater::GetBootImagePackageDataId(u64 *out_data_id, BootModeType mode, v
|
||||||
if (total_entries > 1) {
|
if (total_entries > 1) {
|
||||||
for (size_t i = 0; i < total_entries; i++) {
|
for (size_t i = 0; i < total_entries; i++) {
|
||||||
u8 attr;
|
u8 attr;
|
||||||
if (R_FAILED((rc = ncmContentMetaDatabaseGetAttributes(&meta_db, &records[i], &attr)))) {
|
R_TRY(ncmContentMetaDatabaseGetAttributes(&meta_db, &records[i], &attr));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (attr & NcmContentMetaAttribute_Exfat) {
|
if (attr & NcmContentMetaAttribute_Exfat) {
|
||||||
*out_data_id = records[i].titleId;
|
*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 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. */
|
/* 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)))) {
|
R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
void *bct = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(work_buffer) + 0);
|
void *bct = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(work_buffer) + 0);
|
||||||
void *work = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(work_buffer) + BctSize);
|
void *work = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(work_buffer) + BctSize);
|
||||||
|
|
||||||
size_t size;
|
size_t size;
|
||||||
if (R_FAILED((rc = ReadFile(&size, bct, BctSize, GetBctPath(boot_image_update_type))))) {
|
R_TRY(ReadFile(&size, bct, BctSize, GetBctPath(boot_image_update_type)));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
if (HasEks(boot_image_update_type)) {
|
if (HasEks(boot_image_update_type)) {
|
||||||
if (R_FAILED((rc = accessor.UpdateEks(bct, work)))) {
|
R_TRY(accessor.UpdateEks(bct, work));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (HasAutoRcmPreserve(boot_image_update_type)) {
|
if (HasAutoRcmPreserve(boot_image_update_type)) {
|
||||||
if (R_FAILED((rc = accessor.PreserveAutoRcm(bct, work, which)))) {
|
R_TRY(accessor.PreserveAutoRcm(bct, work, which));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
u8 file_hash[SHA256_HASH_SIZE];
|
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 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. */
|
/* 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)))) {
|
R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (R_FAILED((rc = romfsMountFromDataArchive(data_id, FsStorageId_NandSystem, GetBootImagePackageMountPath())))) {
|
R_TRY_CATCH(romfsMountFromDataArchive(data_id, FsStorageId_NandSystem, GetBootImagePackageMountPath())) {
|
||||||
if (rc == ResultFsTargetNotFound) {
|
R_CATCH(ResultFsTargetNotFound) {
|
||||||
return ResultUpdaterBootImagePackageNotFound;
|
return ResultUpdaterBootImagePackageNotFound;
|
||||||
}
|
}
|
||||||
return rc;
|
} R_END_TRY_CATCH;
|
||||||
}
|
|
||||||
ON_SCOPE_EXIT { if (R_FAILED(romfsUnmount(GetBootImagePackageMountPath()))) { std::abort(); } };
|
ON_SCOPE_EXIT { if (R_FAILED(romfsUnmount(GetBootImagePackageMountPath()))) { std::abort(); } };
|
||||||
|
|
||||||
/* Read and validate hashes of boot images. */
|
/* 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];
|
u8 file_hash[SHA256_HASH_SIZE];
|
||||||
|
|
||||||
Boot0Accessor boot0_accessor;
|
Boot0Accessor boot0_accessor;
|
||||||
if (R_FAILED((rc = boot0_accessor.Initialize()))) {
|
R_TRY(boot0_accessor.Initialize());
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
ON_SCOPE_EXIT { boot0_accessor.Finalize(); };
|
ON_SCOPE_EXIT { boot0_accessor.Finalize(); };
|
||||||
|
|
||||||
/* Compare BCT hashes. */
|
/* Compare BCT hashes. */
|
||||||
if (R_FAILED((rc = boot0_accessor.GetHash(nand_hash, BctSize, work_buffer, work_buffer_size, Boot0Partition::BctNormalMain)))) {
|
R_TRY(boot0_accessor.GetHash(nand_hash, BctSize, work_buffer, work_buffer_size, Boot0Partition::BctNormalMain));
|
||||||
return rc;
|
R_TRY(ValidateBctFileHash(boot0_accessor, Boot0Partition::BctNormalMain, nand_hash, work_buffer, work_buffer_size, boot_image_update_type));
|
||||||
}
|
|
||||||
if (R_FAILED((rc = ValidateBctFileHash(boot0_accessor, Boot0Partition::BctNormalMain, nand_hash, work_buffer, work_buffer_size, boot_image_update_type)))) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Compare BCT Sub hashes. */
|
/* Compare BCT Sub hashes. */
|
||||||
if (R_FAILED((rc = boot0_accessor.GetHash(nand_hash, BctSize, work_buffer, work_buffer_size, Boot0Partition::BctNormalSub)))) {
|
R_TRY(boot0_accessor.GetHash(nand_hash, BctSize, work_buffer, work_buffer_size, Boot0Partition::BctNormalSub));
|
||||||
return rc;
|
R_TRY(ValidateBctFileHash(boot0_accessor, Boot0Partition::BctNormalSub, nand_hash, work_buffer, work_buffer_size, boot_image_update_type));
|
||||||
}
|
|
||||||
if (R_FAILED((rc = ValidateBctFileHash(boot0_accessor, Boot0Partition::BctNormalSub, nand_hash, work_buffer, work_buffer_size, boot_image_update_type)))) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Compare Package1 Normal/Sub hashes. */
|
/* Compare Package1 Normal/Sub hashes. */
|
||||||
if (R_FAILED((rc = GetFileHash(&size, file_hash, GetPackage1Path(boot_image_update_type), work_buffer, work_buffer_size)))) {
|
R_TRY(GetFileHash(&size, file_hash, GetPackage1Path(boot_image_update_type), work_buffer, work_buffer_size));
|
||||||
return rc;
|
R_TRY(boot0_accessor.GetHash(nand_hash, size, work_buffer, work_buffer_size, Boot0Partition::Package1NormalMain));
|
||||||
}
|
|
||||||
if (R_FAILED((rc = boot0_accessor.GetHash(nand_hash, size, work_buffer, work_buffer_size, Boot0Partition::Package1NormalMain)))) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
if (std::memcmp(file_hash, nand_hash, SHA256_HASH_SIZE) != 0) {
|
if (std::memcmp(file_hash, nand_hash, SHA256_HASH_SIZE) != 0) {
|
||||||
return ResultUpdaterNeedsRepairBootImages;
|
return ResultUpdaterNeedsRepairBootImages;
|
||||||
}
|
}
|
||||||
if (R_FAILED((rc = boot0_accessor.GetHash(nand_hash, size, work_buffer, work_buffer_size, Boot0Partition::Package1NormalSub)))) {
|
R_TRY(boot0_accessor.GetHash(nand_hash, size, work_buffer, work_buffer_size, Boot0Partition::Package1NormalSub));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
if (std::memcmp(file_hash, nand_hash, SHA256_HASH_SIZE) != 0) {
|
if (std::memcmp(file_hash, nand_hash, SHA256_HASH_SIZE) != 0) {
|
||||||
return ResultUpdaterNeedsRepairBootImages;
|
return ResultUpdaterNeedsRepairBootImages;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Compare Package2 Normal/Sub hashes. */
|
/* Compare Package2 Normal/Sub hashes. */
|
||||||
if (R_FAILED((rc = GetFileHash(&size, file_hash, GetPackage2Path(boot_image_update_type), work_buffer, work_buffer_size)))) {
|
R_TRY(GetFileHash(&size, file_hash, GetPackage2Path(boot_image_update_type), work_buffer, work_buffer_size));
|
||||||
return rc;
|
R_TRY(GetPackage2Hash(nand_hash, size, work_buffer, work_buffer_size, Package2Type::NormalMain));
|
||||||
}
|
|
||||||
if (R_FAILED((rc = GetPackage2Hash(nand_hash, size, work_buffer, work_buffer_size, Package2Type::NormalMain)))) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
if (std::memcmp(file_hash, nand_hash, SHA256_HASH_SIZE) != 0) {
|
if (std::memcmp(file_hash, nand_hash, SHA256_HASH_SIZE) != 0) {
|
||||||
return ResultUpdaterNeedsRepairBootImages;
|
return ResultUpdaterNeedsRepairBootImages;
|
||||||
}
|
}
|
||||||
if (R_FAILED((rc = GetPackage2Hash(nand_hash, size, work_buffer, work_buffer_size, Package2Type::NormalSub)))) {
|
R_TRY(GetPackage2Hash(nand_hash, size, work_buffer, work_buffer_size, Package2Type::NormalSub));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
if (std::memcmp(file_hash, nand_hash, SHA256_HASH_SIZE) != 0) {
|
if (std::memcmp(file_hash, nand_hash, SHA256_HASH_SIZE) != 0) {
|
||||||
return ResultUpdaterNeedsRepairBootImages;
|
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 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. */
|
/* 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)))) {
|
R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (R_FAILED((rc = romfsMountFromDataArchive(data_id, FsStorageId_NandSystem, GetBootImagePackageMountPath())))) {
|
R_TRY_CATCH(romfsMountFromDataArchive(data_id, FsStorageId_NandSystem, GetBootImagePackageMountPath())) {
|
||||||
if (rc == ResultFsTargetNotFound) {
|
R_CATCH(ResultFsTargetNotFound) {
|
||||||
return ResultUpdaterBootImagePackageNotFound;
|
return ResultUpdaterBootImagePackageNotFound;
|
||||||
}
|
}
|
||||||
return rc;
|
} R_END_TRY_CATCH;
|
||||||
}
|
|
||||||
ON_SCOPE_EXIT { if (R_FAILED(romfsUnmount(GetBootImagePackageMountPath()))) { std::abort(); } };
|
ON_SCOPE_EXIT { if (R_FAILED(romfsUnmount(GetBootImagePackageMountPath()))) { std::abort(); } };
|
||||||
|
|
||||||
/* Read and validate hashes of boot images. */
|
/* 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];
|
u8 file_hash[SHA256_HASH_SIZE];
|
||||||
|
|
||||||
Boot0Accessor boot0_accessor;
|
Boot0Accessor boot0_accessor;
|
||||||
if (R_FAILED((rc = boot0_accessor.Initialize()))) {
|
R_TRY(boot0_accessor.Initialize());
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
ON_SCOPE_EXIT { boot0_accessor.Finalize(); };
|
ON_SCOPE_EXIT { boot0_accessor.Finalize(); };
|
||||||
|
|
||||||
Boot1Accessor boot1_accessor;
|
Boot1Accessor boot1_accessor;
|
||||||
if (R_FAILED((rc = boot1_accessor.Initialize()))) {
|
R_TRY(boot1_accessor.Initialize());
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
ON_SCOPE_EXIT { boot1_accessor.Finalize(); };
|
ON_SCOPE_EXIT { boot1_accessor.Finalize(); };
|
||||||
|
|
||||||
|
|
||||||
/* Compare BCT hashes. */
|
/* Compare BCT hashes. */
|
||||||
if (R_FAILED((rc = boot0_accessor.GetHash(nand_hash, BctSize, work_buffer, work_buffer_size, Boot0Partition::BctSafeMain)))) {
|
R_TRY(boot0_accessor.GetHash(nand_hash, BctSize, work_buffer, work_buffer_size, Boot0Partition::BctSafeMain));
|
||||||
return rc;
|
R_TRY(ValidateBctFileHash(boot0_accessor, Boot0Partition::BctSafeMain, nand_hash, work_buffer, work_buffer_size, boot_image_update_type));
|
||||||
}
|
|
||||||
if (R_FAILED((rc = ValidateBctFileHash(boot0_accessor, Boot0Partition::BctSafeMain, nand_hash, work_buffer, work_buffer_size, boot_image_update_type)))) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Compare BCT Sub hashes. */
|
/* Compare BCT Sub hashes. */
|
||||||
if (R_FAILED((rc = boot0_accessor.GetHash(nand_hash, BctSize, work_buffer, work_buffer_size, Boot0Partition::BctSafeSub)))) {
|
R_TRY(boot0_accessor.GetHash(nand_hash, BctSize, work_buffer, work_buffer_size, Boot0Partition::BctSafeSub));
|
||||||
return rc;
|
R_TRY(ValidateBctFileHash(boot0_accessor, Boot0Partition::BctSafeSub, nand_hash, work_buffer, work_buffer_size, boot_image_update_type));
|
||||||
}
|
|
||||||
if (R_FAILED((rc = ValidateBctFileHash(boot0_accessor, Boot0Partition::BctSafeSub, nand_hash, work_buffer, work_buffer_size, boot_image_update_type)))) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Compare Package1 Normal/Sub hashes. */
|
/* Compare Package1 Normal/Sub hashes. */
|
||||||
if (R_FAILED((rc = GetFileHash(&size, file_hash, GetPackage1Path(boot_image_update_type), work_buffer, work_buffer_size)))) {
|
R_TRY(GetFileHash(&size, file_hash, GetPackage1Path(boot_image_update_type), work_buffer, work_buffer_size));
|
||||||
return rc;
|
R_TRY(boot1_accessor.GetHash(nand_hash, size, work_buffer, work_buffer_size, Boot1Partition::Package1SafeMain));
|
||||||
}
|
|
||||||
if (R_FAILED((rc = boot1_accessor.GetHash(nand_hash, size, work_buffer, work_buffer_size, Boot1Partition::Package1SafeMain)))) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
if (std::memcmp(file_hash, nand_hash, SHA256_HASH_SIZE) != 0) {
|
if (std::memcmp(file_hash, nand_hash, SHA256_HASH_SIZE) != 0) {
|
||||||
return ResultUpdaterNeedsRepairBootImages;
|
return ResultUpdaterNeedsRepairBootImages;
|
||||||
}
|
}
|
||||||
if (R_FAILED((rc = boot1_accessor.GetHash(nand_hash, size, work_buffer, work_buffer_size, Boot1Partition::Package1SafeSub)))) {
|
R_TRY(boot1_accessor.GetHash(nand_hash, size, work_buffer, work_buffer_size, Boot1Partition::Package1SafeSub));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
if (std::memcmp(file_hash, nand_hash, SHA256_HASH_SIZE) != 0) {
|
if (std::memcmp(file_hash, nand_hash, SHA256_HASH_SIZE) != 0) {
|
||||||
return ResultUpdaterNeedsRepairBootImages;
|
return ResultUpdaterNeedsRepairBootImages;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Compare Package2 Normal/Sub hashes. */
|
/* Compare Package2 Normal/Sub hashes. */
|
||||||
if (R_FAILED((rc = GetFileHash(&size, file_hash, GetPackage2Path(boot_image_update_type), work_buffer, work_buffer_size)))) {
|
R_TRY(GetFileHash(&size, file_hash, GetPackage2Path(boot_image_update_type), work_buffer, work_buffer_size));
|
||||||
return rc;
|
R_TRY(GetPackage2Hash(nand_hash, size, work_buffer, work_buffer_size, Package2Type::SafeMain));
|
||||||
}
|
|
||||||
if (R_FAILED((rc = GetPackage2Hash(nand_hash, size, work_buffer, work_buffer_size, Package2Type::SafeMain)))) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
if (std::memcmp(file_hash, nand_hash, SHA256_HASH_SIZE) != 0) {
|
if (std::memcmp(file_hash, nand_hash, SHA256_HASH_SIZE) != 0) {
|
||||||
return ResultUpdaterNeedsRepairBootImages;
|
return ResultUpdaterNeedsRepairBootImages;
|
||||||
}
|
}
|
||||||
if (R_FAILED((rc = GetPackage2Hash(nand_hash, size, work_buffer, work_buffer_size, Package2Type::SafeSub)))) {
|
R_TRY(GetPackage2Hash(nand_hash, size, work_buffer, work_buffer_size, Package2Type::SafeSub));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
if (std::memcmp(file_hash, nand_hash, SHA256_HASH_SIZE) != 0) {
|
if (std::memcmp(file_hash, nand_hash, SHA256_HASH_SIZE) != 0) {
|
||||||
return ResultUpdaterNeedsRepairBootImages;
|
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 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. */
|
/* 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)))) {
|
R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (R_FAILED((rc = romfsMountFromDataArchive(data_id, FsStorageId_NandSystem, GetBootImagePackageMountPath())))) {
|
R_TRY_CATCH(romfsMountFromDataArchive(data_id, FsStorageId_NandSystem, GetBootImagePackageMountPath())) {
|
||||||
if (rc == ResultFsTargetNotFound) {
|
R_CATCH(ResultFsTargetNotFound) {
|
||||||
return ResultUpdaterBootImagePackageNotFound;
|
return ResultUpdaterBootImagePackageNotFound;
|
||||||
}
|
}
|
||||||
return rc;
|
} R_END_TRY_CATCH;
|
||||||
}
|
|
||||||
ON_SCOPE_EXIT { if (R_FAILED(romfsUnmount(GetBootImagePackageMountPath()))) { std::abort(); } };
|
ON_SCOPE_EXIT { if (R_FAILED(romfsUnmount(GetBootImagePackageMountPath()))) { std::abort(); } };
|
||||||
|
|
||||||
{
|
{
|
||||||
Boot0Accessor boot0_accessor;
|
Boot0Accessor boot0_accessor;
|
||||||
if (R_FAILED((rc = boot0_accessor.Initialize()))) {
|
R_TRY(boot0_accessor.Initialize());
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
ON_SCOPE_EXIT { boot0_accessor.Finalize(); };
|
ON_SCOPE_EXIT { boot0_accessor.Finalize(); };
|
||||||
|
|
||||||
/* Write Package1 sub. */
|
/* Write Package1 sub. */
|
||||||
if (R_FAILED((rc = boot0_accessor.Clear(work_buffer, work_buffer_size, Boot0Partition::Package1NormalSub)))) {
|
R_TRY(boot0_accessor.Clear(work_buffer, work_buffer_size, Boot0Partition::Package1NormalSub));
|
||||||
return rc;
|
R_TRY(boot0_accessor.Write(GetPackage1Path(boot_image_update_type), work_buffer, work_buffer_size, Boot0Partition::Package1NormalSub));
|
||||||
}
|
|
||||||
if (R_FAILED((rc = boot0_accessor.Write(GetPackage1Path(boot_image_update_type), work_buffer, work_buffer_size, Boot0Partition::Package1NormalSub)))) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Write Package2 sub. */
|
/* Write Package2 sub. */
|
||||||
if (R_FAILED((rc = WritePackage2(work_buffer, work_buffer_size, Package2Type::NormalSub, boot_image_update_type)))) {
|
R_TRY(WritePackage2(work_buffer, work_buffer_size, Package2Type::NormalSub, boot_image_update_type));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Write BCT sub + BCT main, in that order. */
|
/* 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<void *>(reinterpret_cast<uintptr_t>(work_buffer) + BctSize);
|
void *work = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(work_buffer) + BctSize);
|
||||||
|
|
||||||
size_t size;
|
size_t size;
|
||||||
if (R_FAILED((rc = ReadFile(&size, bct, BctSize, GetBctPath(boot_image_update_type))))) {
|
R_TRY(ReadFile(&size, bct, BctSize, GetBctPath(boot_image_update_type)));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
if (HasEks(boot_image_update_type)) {
|
if (HasEks(boot_image_update_type)) {
|
||||||
if (R_FAILED((rc = boot0_accessor.UpdateEks(bct, work)))) {
|
R_TRY(boot0_accessor.UpdateEks(bct, work));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Only preserve autorcm if on a unit with unpatched rcm bug. */
|
/* Only preserve autorcm if on a unit with unpatched rcm bug. */
|
||||||
if (HasAutoRcmPreserve(boot_image_update_type) && !IsRcmBugPatched()) {
|
if (HasAutoRcmPreserve(boot_image_update_type) && !IsRcmBugPatched()) {
|
||||||
if (R_FAILED((rc = boot0_accessor.PreserveAutoRcm(bct, work, Boot0Partition::BctNormalSub)))) {
|
R_TRY(boot0_accessor.PreserveAutoRcm(bct, work, Boot0Partition::BctNormalSub));
|
||||||
return rc;
|
R_TRY(boot0_accessor.Write(bct, BctSize, Boot0Partition::BctNormalSub));
|
||||||
}
|
R_TRY(boot0_accessor.PreserveAutoRcm(bct, work, Boot0Partition::BctNormalMain));
|
||||||
if (R_FAILED((rc = boot0_accessor.Write(bct, BctSize, Boot0Partition::BctNormalSub)))) {
|
R_TRY(boot0_accessor.Write(bct, BctSize, Boot0Partition::BctNormalMain));
|
||||||
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;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (R_FAILED((rc = boot0_accessor.Write(bct, BctSize, Boot0Partition::BctNormalSub)))) {
|
R_TRY(boot0_accessor.Write(bct, BctSize, Boot0Partition::BctNormalSub));
|
||||||
return rc;
|
R_TRY(boot0_accessor.Write(bct, BctSize, Boot0Partition::BctNormalMain));
|
||||||
}
|
|
||||||
if (R_FAILED((rc = boot0_accessor.Write(bct, BctSize, Boot0Partition::BctNormalMain)))) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Write Package2 main. */
|
/* Write Package2 main. */
|
||||||
if (R_FAILED((rc = WritePackage2(work_buffer, work_buffer_size, Package2Type::NormalMain, boot_image_update_type)))) {
|
R_TRY(WritePackage2(work_buffer, work_buffer_size, Package2Type::NormalMain, boot_image_update_type));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Write Package1 main. */
|
/* Write Package1 main. */
|
||||||
if (R_FAILED((rc = boot0_accessor.Clear(work_buffer, work_buffer_size, Boot0Partition::Package1NormalMain)))) {
|
R_TRY(boot0_accessor.Clear(work_buffer, work_buffer_size, Boot0Partition::Package1NormalMain));
|
||||||
return rc;
|
R_TRY(boot0_accessor.Write(GetPackage1Path(boot_image_update_type), work_buffer, work_buffer_size, Boot0Partition::Package1NormalMain));
|
||||||
}
|
|
||||||
if (R_FAILED((rc = boot0_accessor.Write(GetPackage1Path(boot_image_update_type), work_buffer, work_buffer_size, Boot0Partition::Package1NormalMain)))) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result Updater::UpdateBootImagesSafe(u64 data_id, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type) {
|
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. */
|
/* 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)))) {
|
R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (R_FAILED((rc = romfsMountFromDataArchive(data_id, FsStorageId_NandSystem, GetBootImagePackageMountPath())))) {
|
R_TRY_CATCH(romfsMountFromDataArchive(data_id, FsStorageId_NandSystem, GetBootImagePackageMountPath())) {
|
||||||
if (rc == ResultFsTargetNotFound) {
|
R_CATCH(ResultFsTargetNotFound) {
|
||||||
return ResultUpdaterBootImagePackageNotFound;
|
return ResultUpdaterBootImagePackageNotFound;
|
||||||
}
|
}
|
||||||
return rc;
|
} R_END_TRY_CATCH;
|
||||||
}
|
|
||||||
ON_SCOPE_EXIT { if (R_FAILED(romfsUnmount(GetBootImagePackageMountPath()))) { std::abort(); } };
|
ON_SCOPE_EXIT { if (R_FAILED(romfsUnmount(GetBootImagePackageMountPath()))) { std::abort(); } };
|
||||||
|
|
||||||
{
|
{
|
||||||
Boot0Accessor boot0_accessor;
|
Boot0Accessor boot0_accessor;
|
||||||
if (R_FAILED((rc = boot0_accessor.Initialize()))) {
|
R_TRY(boot0_accessor.Initialize());
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
ON_SCOPE_EXIT { boot0_accessor.Finalize(); };
|
ON_SCOPE_EXIT { boot0_accessor.Finalize(); };
|
||||||
|
|
||||||
Boot1Accessor boot1_accessor;
|
Boot1Accessor boot1_accessor;
|
||||||
if (R_FAILED((rc = boot1_accessor.Initialize()))) {
|
R_TRY(boot1_accessor.Initialize());
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
ON_SCOPE_EXIT { boot1_accessor.Finalize(); };
|
ON_SCOPE_EXIT { boot1_accessor.Finalize(); };
|
||||||
|
|
||||||
|
|
||||||
/* Write Package1 sub. */
|
/* Write Package1 sub. */
|
||||||
if (R_FAILED((rc = boot1_accessor.Clear(work_buffer, work_buffer_size, Boot1Partition::Package1SafeSub)))) {
|
R_TRY(boot1_accessor.Clear(work_buffer, work_buffer_size, Boot1Partition::Package1SafeSub));
|
||||||
return rc;
|
R_TRY(boot1_accessor.Write(GetPackage1Path(boot_image_update_type), work_buffer, work_buffer_size, Boot1Partition::Package1SafeSub));
|
||||||
}
|
|
||||||
if (R_FAILED((rc = boot1_accessor.Write(GetPackage1Path(boot_image_update_type), work_buffer, work_buffer_size, Boot1Partition::Package1SafeSub)))) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Write Package2 sub. */
|
/* Write Package2 sub. */
|
||||||
if (R_FAILED((rc = WritePackage2(work_buffer, work_buffer_size, Package2Type::SafeSub, boot_image_update_type)))) {
|
R_TRY(WritePackage2(work_buffer, work_buffer_size, Package2Type::SafeSub, boot_image_update_type));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Write BCT sub + BCT main, in that order. */
|
/* 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<void *>(reinterpret_cast<uintptr_t>(work_buffer) + BctSize);
|
void *work = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(work_buffer) + BctSize);
|
||||||
|
|
||||||
size_t size;
|
size_t size;
|
||||||
if (R_FAILED((rc = ReadFile(&size, bct, BctSize, GetBctPath(boot_image_update_type))))) {
|
R_TRY(ReadFile(&size, bct, BctSize, GetBctPath(boot_image_update_type)));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
if (HasEks(boot_image_update_type)) {
|
if (HasEks(boot_image_update_type)) {
|
||||||
if (R_FAILED((rc = boot0_accessor.UpdateEks(bct, work)))) {
|
R_TRY(boot0_accessor.UpdateEks(bct, work));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
/* Only preserve autorcm if on a unit with unpatched rcm bug. */
|
/* Only preserve autorcm if on a unit with unpatched rcm bug. */
|
||||||
if (HasAutoRcmPreserve(boot_image_update_type) && !IsRcmBugPatched()) {
|
if (HasAutoRcmPreserve(boot_image_update_type) && !IsRcmBugPatched()) {
|
||||||
if (R_FAILED((rc = boot0_accessor.PreserveAutoRcm(bct, work, Boot0Partition::BctSafeSub)))) {
|
R_TRY(boot0_accessor.PreserveAutoRcm(bct, work, Boot0Partition::BctSafeSub));
|
||||||
return rc;
|
R_TRY(boot0_accessor.Write(bct, BctSize, Boot0Partition::BctSafeSub));
|
||||||
}
|
R_TRY(boot0_accessor.PreserveAutoRcm(bct, work, Boot0Partition::BctSafeMain));
|
||||||
if (R_FAILED((rc = boot0_accessor.Write(bct, BctSize, Boot0Partition::BctSafeSub)))) {
|
R_TRY(boot0_accessor.Write(bct, BctSize, Boot0Partition::BctSafeMain));
|
||||||
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;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (R_FAILED((rc = boot0_accessor.Write(bct, BctSize, Boot0Partition::BctSafeSub)))) {
|
R_TRY(boot0_accessor.Write(bct, BctSize, Boot0Partition::BctSafeSub));
|
||||||
return rc;
|
R_TRY(boot0_accessor.Write(bct, BctSize, Boot0Partition::BctSafeMain));
|
||||||
}
|
|
||||||
if (R_FAILED((rc = boot0_accessor.Write(bct, BctSize, Boot0Partition::BctSafeMain)))) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Write Package2 main. */
|
/* Write Package2 main. */
|
||||||
if (R_FAILED((rc = WritePackage2(work_buffer, work_buffer_size, Package2Type::SafeMain, boot_image_update_type)))) {
|
R_TRY(WritePackage2(work_buffer, work_buffer_size, Package2Type::SafeMain, boot_image_update_type));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Write Package1 main. */
|
/* Write Package1 main. */
|
||||||
if (R_FAILED((rc = boot1_accessor.Clear(work_buffer, work_buffer_size, Boot1Partition::Package1SafeMain)))) {
|
R_TRY(boot1_accessor.Clear(work_buffer, work_buffer_size, Boot1Partition::Package1SafeMain));
|
||||||
return rc;
|
R_TRY(boot1_accessor.Write(GetPackage1Path(boot_image_update_type), work_buffer, work_buffer_size, Boot1Partition::Package1SafeMain));
|
||||||
}
|
|
||||||
if (R_FAILED((rc = boot1_accessor.Write(GetPackage1Path(boot_image_update_type), work_buffer, work_buffer_size, Boot1Partition::Package1SafeMain)))) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result Updater::SetVerificationNeeded(BootModeType mode, bool needed, void *work_buffer, size_t work_buffer_size) {
|
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. */
|
/* 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)))) {
|
R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Initialize boot0 save accessor. */
|
/* Initialize boot0 save accessor. */
|
||||||
BisSave save;
|
BisSave save;
|
||||||
if (R_FAILED((rc = save.Initialize(work_buffer, work_buffer_size)))) {
|
R_TRY(save.Initialize(work_buffer, work_buffer_size));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
ON_SCOPE_EXIT { save.Finalize(); };
|
ON_SCOPE_EXIT { save.Finalize(); };
|
||||||
|
|
||||||
/* Load save from NAND. */
|
/* Load save from NAND. */
|
||||||
if (R_FAILED((rc = save.Load()))) {
|
R_TRY(save.Load());
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Set whether we need to verify, then save to nand. */
|
/* Set whether we need to verify, then save to nand. */
|
||||||
save.SetNeedsVerification(mode, needed);
|
save.SetNeedsVerification(mode, needed);
|
||||||
if (R_FAILED((rc = save.Save()))) {
|
R_TRY(save.Save());
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result Updater::GetPackage2Hash(void *dst_hash, size_t package2_size, void *work_buffer, size_t work_buffer_size, Package2Type which) {
|
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);
|
Package2Accessor accessor(which);
|
||||||
if (R_FAILED((rc = accessor.Initialize()))) {
|
R_TRY(accessor.Initialize());
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
ON_SCOPE_EXIT { accessor.Finalize(); };
|
ON_SCOPE_EXIT { accessor.Finalize(); };
|
||||||
|
|
||||||
return accessor.GetHash(dst_hash, package2_size, work_buffer, work_buffer_size, Package2Partition::Package2);
|
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 Updater::WritePackage2(void *work_buffer, size_t work_buffer_size, Package2Type which, BootImageUpdateType boot_image_update_type) {
|
||||||
Result rc;
|
|
||||||
|
|
||||||
Package2Accessor accessor(which);
|
Package2Accessor accessor(which);
|
||||||
if (R_FAILED((rc = accessor.Initialize()))) {
|
R_TRY(accessor.Initialize());
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
ON_SCOPE_EXIT { accessor.Finalize(); };
|
ON_SCOPE_EXIT { accessor.Finalize(); };
|
||||||
|
|
||||||
return accessor.Write(GetPackage2Path(boot_image_update_type), work_buffer, work_buffer_size, Package2Partition::Package2);
|
return accessor.Write(GetPackage2Path(boot_image_update_type), work_buffer, work_buffer_size, Package2Partition::Package2);
|
||||||
|
|
|
@ -20,11 +20,9 @@
|
||||||
#include "updater_bis_management.hpp"
|
#include "updater_bis_management.hpp"
|
||||||
|
|
||||||
Result BisAccessor::Initialize() {
|
Result BisAccessor::Initialize() {
|
||||||
Result rc = fsOpenBisStorage(&this->storage, this->partition_id);
|
R_TRY(fsOpenBisStorage(&this->storage, this->partition_id));
|
||||||
if (R_SUCCEEDED(rc)) {
|
this->active = true;
|
||||||
this->active = true;
|
return ResultSuccess;
|
||||||
}
|
|
||||||
return rc;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void BisAccessor::Finalize() {
|
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 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) {
|
if (offset % SectorAlignment != 0 || work_buffer_size % SectorAlignment != 0) {
|
||||||
std::abort();
|
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;
|
size_t aligned_size = ((read_size + SectorAlignment - 1) / SectorAlignment) * SectorAlignment;
|
||||||
if (R_FAILED((rc = this->Write(offset + written, work_buffer, aligned_size)))) {
|
R_TRY(this->Write(offset + written, work_buffer, aligned_size));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
written += read_size;
|
written += read_size;
|
||||||
|
|
||||||
if (read_size != work_buffer_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 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) {
|
if (offset % SectorAlignment != 0 || work_buffer_size % SectorAlignment != 0) {
|
||||||
std::abort();
|
std::abort();
|
||||||
}
|
}
|
||||||
|
@ -97,16 +91,13 @@ Result BisAccessor::Clear(u64 offset, u64 size, void *work_buffer, size_t work_b
|
||||||
size_t written = 0;
|
size_t written = 0;
|
||||||
while (written < size) {
|
while (written < size) {
|
||||||
size_t cur_write_size = std::min(work_buffer_size, size - written);
|
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)))) {
|
R_TRY(this->Write(offset + written, work_buffer, cur_write_size));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
written += cur_write_size;
|
written += cur_write_size;
|
||||||
}
|
}
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result BisAccessor::GetHash(void *dst, u64 offset, u64 size, u64 hash_size, void *work_buffer, size_t work_buffer_size) {
|
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) {
|
if (offset % SectorAlignment != 0 || work_buffer_size % SectorAlignment != 0) {
|
||||||
std::abort();
|
std::abort();
|
||||||
}
|
}
|
||||||
|
@ -118,9 +109,7 @@ Result BisAccessor::GetHash(void *dst, u64 offset, u64 size, u64 hash_size, void
|
||||||
while (total_read < hash_size) {
|
while (total_read < hash_size) {
|
||||||
size_t cur_read_size = std::min(work_buffer_size, size - total_read);
|
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);
|
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)))) {
|
R_TRY(this->Read(work_buffer, cur_read_size, offset + total_read));
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
sha256ContextUpdate(&sha_ctx, work_buffer, cur_update_size);
|
sha256ContextUpdate(&sha_ctx, work_buffer, cur_update_size);
|
||||||
total_read += cur_read_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) {
|
Result Boot0Accessor::UpdateEks(void *dst_bct, void *eks_work_buffer) {
|
||||||
size_t read_size;
|
size_t read_size;
|
||||||
Result rc = this->Read(&read_size, eks_work_buffer, EksSize, Boot0Partition::Eks);
|
R_TRY(this->Read(&read_size, eks_work_buffer, EksSize, Boot0Partition::Eks));
|
||||||
if (R_FAILED(rc)) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
return this->UpdateEksManually(dst_bct, eks_work_buffer);
|
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);
|
std::memset(work_buffer, 0, BctSize);
|
||||||
|
|
||||||
size_t read_size;
|
size_t read_size;
|
||||||
Result rc = this->Read(&read_size, work_buffer, BctSize, which);
|
R_TRY(this->Read(&read_size, work_buffer, BctSize, which));
|
||||||
if (R_FAILED(rc)) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
void *dst_pubk = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(dst_bct) + BctPubkOffset);
|
void *dst_pubk = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(dst_bct) + BctPubkOffset);
|
||||||
void *src_pubk = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(work_buffer) + BctPubkOffset);
|
void *src_pubk = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(work_buffer) + BctPubkOffset);
|
||||||
|
|
|
@ -143,10 +143,7 @@ class PartitionAccessor : public BisAccessor {
|
||||||
std::abort();
|
std::abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
Result rc = BisAccessor::Read(dst, entry->size, entry->offset);
|
R_TRY(BisAccessor::Read(dst, entry->size, entry->offset));
|
||||||
if (R_FAILED(rc)) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
*out_size = entry->size;
|
*out_size = entry->size;
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
|
|
|
@ -35,10 +35,7 @@ Result BisSave::Initialize(void *work_buffer, size_t work_buffer_size) {
|
||||||
std::abort();
|
std::abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
Result rc = this->accessor.Initialize();
|
R_TRY(this->accessor.Initialize());
|
||||||
if (R_FAILED(rc)) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
this->save_buffer = work_buffer;
|
this->save_buffer = work_buffer;
|
||||||
return ResultSuccess;
|
return ResultSuccess;
|
||||||
}
|
}
|
||||||
|
|
|
@ -852,7 +852,7 @@ Result DmntCheatManager::ForceOpenCheatProcess() {
|
||||||
|
|
||||||
/* Get the current application process ID. */
|
/* Get the current application process ID. */
|
||||||
R_TRY(pmdmntGetApplicationPid(&g_cheat_process_metadata.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;
|
g_cheat_process_metadata.process_id = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit 7e0ed3b38f437791fdd398c7058c376c9d2a6853
|
Subproject commit 3274848b5612a9fa3a7306c7c4ca0a4b041c150c
|
Loading…
Reference in a new issue