1fb299fa2Sopenharmony_ci/* 2fb299fa2Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License. 5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at 6fb299fa2Sopenharmony_ci * 7fb299fa2Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fb299fa2Sopenharmony_ci * 9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and 13fb299fa2Sopenharmony_ci * limitations under the License. 14fb299fa2Sopenharmony_ci */ 15fb299fa2Sopenharmony_ci 16fb299fa2Sopenharmony_ci#include "log/log.h" 17fb299fa2Sopenharmony_ci#include "ptable_manager.h" 18fb299fa2Sopenharmony_ci#include "ptable_process.h" 19fb299fa2Sopenharmony_ci#include "utils.h" 20fb299fa2Sopenharmony_ci 21fb299fa2Sopenharmony_ciusing namespace Hpackage; 22fb299fa2Sopenharmony_cinamespace Updater { 23fb299fa2Sopenharmony_ciextern "C" __attribute__((constructor)) void RegisterPtableHelper(void) 24fb299fa2Sopenharmony_ci{ 25fb299fa2Sopenharmony_ci PtablePreProcess::GetInstance().RegisterPtableHelper(PtableProcess); 26fb299fa2Sopenharmony_ci} 27fb299fa2Sopenharmony_ci 28fb299fa2Sopenharmony_civoid PtablePreProcess::RegisterPtableHelper(PtablePreProcessFunc ptr) 29fb299fa2Sopenharmony_ci{ 30fb299fa2Sopenharmony_ci helper_ = std::move(ptr); 31fb299fa2Sopenharmony_ci} 32fb299fa2Sopenharmony_ci 33fb299fa2Sopenharmony_cibool PtablePreProcess::DoPtableProcess(UpdaterParams &upParams) 34fb299fa2Sopenharmony_ci{ 35fb299fa2Sopenharmony_ci if (helper_ == nullptr) { 36fb299fa2Sopenharmony_ci LOG(INFO) << "PtablePreProcess helper_ is null"; 37fb299fa2Sopenharmony_ci return false; 38fb299fa2Sopenharmony_ci } 39fb299fa2Sopenharmony_ci return helper_(upParams); 40fb299fa2Sopenharmony_ci} 41fb299fa2Sopenharmony_ci 42fb299fa2Sopenharmony_cibool PtableProcess(UpdaterParams &upParams) 43fb299fa2Sopenharmony_ci{ 44fb299fa2Sopenharmony_ci for (auto &path : upParams.updatePackage) { 45fb299fa2Sopenharmony_ci Hpackage::PkgManager::PkgManagerPtr pkgManager = Hpackage::PkgManager::CreatePackageInstance(); 46fb299fa2Sopenharmony_ci std::vector<std::string> components; 47fb299fa2Sopenharmony_ci if (pkgManager == nullptr) { 48fb299fa2Sopenharmony_ci LOG(ERROR) << "Fail to CreatePackageInstance"; 49fb299fa2Sopenharmony_ci return false; 50fb299fa2Sopenharmony_ci } 51fb299fa2Sopenharmony_ci int32_t ret = pkgManager->LoadPackage(path, Utils::GetCertName(), components); 52fb299fa2Sopenharmony_ci if (ret != Hpackage::PKG_SUCCESS) { 53fb299fa2Sopenharmony_ci LOG(ERROR) << "LoadPackage fail ret:" << ret; 54fb299fa2Sopenharmony_ci Hpackage::PkgManager::ReleasePackageInstance(pkgManager); 55fb299fa2Sopenharmony_ci return false; 56fb299fa2Sopenharmony_ci } 57fb299fa2Sopenharmony_ci DevicePtable& devicePtb = DevicePtable::GetInstance(); 58fb299fa2Sopenharmony_ci devicePtb.LoadPartitionInfo(); 59fb299fa2Sopenharmony_ci PackagePtable& packagePtb = PackagePtable::GetInstance(); 60fb299fa2Sopenharmony_ci packagePtb.LoadPartitionInfo(pkgManager); 61fb299fa2Sopenharmony_ci if (!devicePtb.ComparePtable(packagePtb)) { 62fb299fa2Sopenharmony_ci LOG(INFO) << "Ptable NOT changed, no need to process!"; 63fb299fa2Sopenharmony_ci Hpackage::PkgManager::ReleasePackageInstance(pkgManager); 64fb299fa2Sopenharmony_ci continue; 65fb299fa2Sopenharmony_ci } 66fb299fa2Sopenharmony_ci if (upParams.updateMode != SDCARD_UPDATE) { 67fb299fa2Sopenharmony_ci if (devicePtb.ComparePartition(packagePtb, "USERDATA")) { 68fb299fa2Sopenharmony_ci LOG(ERROR) << "Hota update not allow userdata partition change!"; 69fb299fa2Sopenharmony_ci Hpackage::PkgManager::ReleasePackageInstance(pkgManager); 70fb299fa2Sopenharmony_ci return false; 71fb299fa2Sopenharmony_ci } 72fb299fa2Sopenharmony_ci } 73fb299fa2Sopenharmony_ci if (!packagePtb.WritePtableToDevice()) { 74fb299fa2Sopenharmony_ci LOG(ERROR) << "Ptable changed, write new ptable failed!"; 75fb299fa2Sopenharmony_ci Hpackage::PkgManager::ReleasePackageInstance(pkgManager); 76fb299fa2Sopenharmony_ci return false; 77fb299fa2Sopenharmony_ci } 78fb299fa2Sopenharmony_ci Hpackage::PkgManager::ReleasePackageInstance(pkgManager); 79fb299fa2Sopenharmony_ci } 80fb299fa2Sopenharmony_ci return true; 81fb299fa2Sopenharmony_ci} 82fb299fa2Sopenharmony_ci}