mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2025-06-02 23:59:49 -04:00
boot: refactor battery checking to use new powctl apis
This commit is contained in:
parent
485304bd17
commit
708f5bf1fb
44 changed files with 1426 additions and 1554 deletions
53
libraries/libstratosphere/source/cal/cal_battery_api.cpp
Normal file
53
libraries/libstratosphere/source/cal/cal_battery_api.cpp
Normal file
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020 Atmosphère-NX
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms and conditions of the GNU General Public License,
|
||||
* version 2, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "cal_fs_utils.hpp"
|
||||
|
||||
namespace ams::cal {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr inline s64 BatteryLotOffset = 0x2CE0;
|
||||
constexpr inline size_t BatteryLotSize = 0x20;
|
||||
|
||||
constexpr inline s64 BatteryVersionOffset = 0x4310;
|
||||
constexpr inline size_t BatteryVersionSize = 0x10;
|
||||
|
||||
constexpr inline size_t BatteryVendorSizeMax = 0x18;
|
||||
|
||||
}
|
||||
|
||||
Result GetBatteryVersion(u8 *out) {
|
||||
/* Read the battery version. */
|
||||
u8 battery_version[BatteryVersionSize];
|
||||
R_TRY(cal::impl::ReadCalibrationBlock(BatteryVersionOffset, battery_version, sizeof(battery_version)));
|
||||
|
||||
/* Write the output. */
|
||||
*out = battery_version[0];
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
Result GetBatteryVendor(size_t *out_vendor_size, void *dst, size_t dst_size) {
|
||||
/* Read the battery lot. */
|
||||
char battery_lot[BatteryLotSize];
|
||||
R_TRY(cal::impl::ReadCalibrationBlock(BatteryLotOffset, battery_lot, sizeof(battery_lot)));
|
||||
|
||||
/* Copy output. */
|
||||
*out_vendor_size = static_cast<size_t>(util::Strlcpy(static_cast<char *>(dst), battery_lot, std::min(dst_size, BatteryVendorSizeMax)));
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
}
|
53
libraries/libstratosphere/source/cal/cal_crc_utils.cpp
Normal file
53
libraries/libstratosphere/source/cal/cal_crc_utils.cpp
Normal file
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020 Atmosphère-NX
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms and conditions of the GNU General Public License,
|
||||
* version 2, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "cal_crc_utils.hpp"
|
||||
|
||||
namespace ams::cal::impl {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr inline const u16 CrcTable[0x10] = {
|
||||
0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401,
|
||||
0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
u16 CalculateCrc16(const void *data, size_t size) {
|
||||
AMS_ASSERT(data != nullptr);
|
||||
|
||||
u16 crc = 0x55AA;
|
||||
const u8 *data_u8 = static_cast<const u8 *>(data);
|
||||
for (size_t i = 0; i < size; ++i) {
|
||||
crc = (crc >> 4) ^ (CrcTable[crc & 0xF]) ^ (CrcTable[(data_u8[i] >> 0) & 0xF]);
|
||||
crc = (crc >> 4) ^ (CrcTable[crc & 0xF]) ^ (CrcTable[(data_u8[i] >> 4) & 0xF]);
|
||||
}
|
||||
|
||||
return crc;
|
||||
}
|
||||
|
||||
Result ValidateCalibrationCrc(const void *data, size_t size) {
|
||||
AMS_ASSERT(data != nullptr);
|
||||
AMS_ASSERT(size >= sizeof(u16));
|
||||
|
||||
const u16 crc = *reinterpret_cast<const u16 *>(reinterpret_cast<uintptr_t>(data) + size - sizeof(u16));
|
||||
R_UNLESS(CalculateCrc16(data, size - sizeof(u16)) == crc, cal::ResultCalibrationDataCrcError());
|
||||
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
}
|
24
libraries/libstratosphere/source/cal/cal_crc_utils.hpp
Normal file
24
libraries/libstratosphere/source/cal/cal_crc_utils.hpp
Normal file
|
@ -0,0 +1,24 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020 Atmosphère-NX
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms and conditions of the GNU General Public License,
|
||||
* version 2, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
#include <stratosphere.hpp>
|
||||
|
||||
namespace ams::cal::impl {
|
||||
|
||||
u16 CalculateCrc16(const void *data, size_t size);
|
||||
Result ValidateCalibrationCrc(const void *data, size_t size);
|
||||
|
||||
}
|
36
libraries/libstratosphere/source/cal/cal_fs_utils.cpp
Normal file
36
libraries/libstratosphere/source/cal/cal_fs_utils.cpp
Normal file
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020 Atmosphère-NX
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms and conditions of the GNU General Public License,
|
||||
* version 2, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "cal_crc_utils.hpp"
|
||||
#include "cal_fs_utils.hpp"
|
||||
|
||||
namespace ams::cal::impl {
|
||||
|
||||
Result ReadCalibrationBlock(s64 offset, void *dst, size_t block_size) {
|
||||
/* Open the calibration binary partition. */
|
||||
std::unique_ptr<fs::IStorage> storage;
|
||||
R_TRY(fs::OpenBisPartition(std::addressof(storage), fs::BisPartitionId::CalibrationBinary));
|
||||
|
||||
/* Read data from the partition. */
|
||||
R_TRY(storage->Read(offset, dst, block_size));
|
||||
|
||||
/* Validate the crc. */
|
||||
R_TRY(ValidateCalibrationCrc(dst, block_size));
|
||||
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
}
|
23
libraries/libstratosphere/source/cal/cal_fs_utils.hpp
Normal file
23
libraries/libstratosphere/source/cal/cal_fs_utils.hpp
Normal file
|
@ -0,0 +1,23 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020 Atmosphère-NX
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms and conditions of the GNU General Public License,
|
||||
* version 2, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
#include <stratosphere.hpp>
|
||||
|
||||
namespace ams::cal::impl {
|
||||
|
||||
Result ReadCalibrationBlock(s64 offset, void *dst, size_t block_size);
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue