1fb299fa2Sopenharmony_ci/* 2fb299fa2Sopenharmony_ci * Copyright (c) 2024 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 "composite_ptable.h" 17fb299fa2Sopenharmony_ci#include "log/log.h" 18fb299fa2Sopenharmony_ci 19fb299fa2Sopenharmony_cinamespace Updater { 20fb299fa2Sopenharmony_cibool CompositePtable::ParsePartitionFromBuffer(uint8_t *ptbImgBuffer, const uint32_t imgBufSize) 21fb299fa2Sopenharmony_ci{ 22fb299fa2Sopenharmony_ci if (!CheckBuff(ptbImgBuffer, static_cast<const uint64_t>(imgBufSize))) { 23fb299fa2Sopenharmony_ci LOG(ERROR) << "input invalid"; 24fb299fa2Sopenharmony_ci return false; 25fb299fa2Sopenharmony_ci } 26fb299fa2Sopenharmony_ci std::vector<PtnInfo>().swap(partitionInfo_); 27fb299fa2Sopenharmony_ci for (const auto &iter : childs_) { 28fb299fa2Sopenharmony_ci if (iter == nullptr) { 29fb299fa2Sopenharmony_ci LOG(ERROR) << "invalid iter ptable"; 30fb299fa2Sopenharmony_ci return false; 31fb299fa2Sopenharmony_ci } 32fb299fa2Sopenharmony_ci if (!iter->ParsePartitionFromBuffer(ptbImgBuffer, imgBufSize)) { 33fb299fa2Sopenharmony_ci LOG(ERROR) << "parse partition from buffer failed"; 34fb299fa2Sopenharmony_ci return false; 35fb299fa2Sopenharmony_ci } 36fb299fa2Sopenharmony_ci AppendChildPtnInfo(iter->GetPtablePartitionInfo()); 37fb299fa2Sopenharmony_ci } 38fb299fa2Sopenharmony_ci return true; 39fb299fa2Sopenharmony_ci} 40fb299fa2Sopenharmony_ci 41fb299fa2Sopenharmony_cibool CompositePtable::LoadPtableFromDevice() 42fb299fa2Sopenharmony_ci{ 43fb299fa2Sopenharmony_ci std::vector<PtnInfo>().swap(partitionInfo_); 44fb299fa2Sopenharmony_ci for (const auto &iter : childs_) { 45fb299fa2Sopenharmony_ci if (iter == nullptr) { 46fb299fa2Sopenharmony_ci LOG(ERROR) << "invalid iter ptable"; 47fb299fa2Sopenharmony_ci return false; 48fb299fa2Sopenharmony_ci } 49fb299fa2Sopenharmony_ci if (!iter->LoadPtableFromDevice()) { 50fb299fa2Sopenharmony_ci LOG(ERROR) << "load ptable from device failed"; 51fb299fa2Sopenharmony_ci return false; 52fb299fa2Sopenharmony_ci } 53fb299fa2Sopenharmony_ci AppendChildPtnInfo(iter->GetPtablePartitionInfo()); 54fb299fa2Sopenharmony_ci } 55fb299fa2Sopenharmony_ci return true; 56fb299fa2Sopenharmony_ci} 57fb299fa2Sopenharmony_ci 58fb299fa2Sopenharmony_cibool CompositePtable::WritePartitionTable() 59fb299fa2Sopenharmony_ci{ 60fb299fa2Sopenharmony_ci for (const auto &iter : childs_) { 61fb299fa2Sopenharmony_ci if (iter == nullptr) { 62fb299fa2Sopenharmony_ci LOG(ERROR) << "invalid iter ptable"; 63fb299fa2Sopenharmony_ci return false; 64fb299fa2Sopenharmony_ci } 65fb299fa2Sopenharmony_ci if (!iter->WritePartitionTable()) { 66fb299fa2Sopenharmony_ci LOG(ERROR) << "write ptable failed"; 67fb299fa2Sopenharmony_ci return false; 68fb299fa2Sopenharmony_ci } 69fb299fa2Sopenharmony_ci } 70fb299fa2Sopenharmony_ci return true; 71fb299fa2Sopenharmony_ci} 72fb299fa2Sopenharmony_ci 73fb299fa2Sopenharmony_cibool CompositePtable::GetPtableImageBuffer(uint8_t *imageBuf, const uint32_t imgBufSize) 74fb299fa2Sopenharmony_ci{ 75fb299fa2Sopenharmony_ci if (!CheckBuff(imageBuf, static_cast<const uint64_t>(imgBufSize))) { 76fb299fa2Sopenharmony_ci LOG(ERROR) << "input invalid"; 77fb299fa2Sopenharmony_ci return false; 78fb299fa2Sopenharmony_ci } 79fb299fa2Sopenharmony_ci for (const auto &iter : childs_) { 80fb299fa2Sopenharmony_ci if (iter == nullptr) { 81fb299fa2Sopenharmony_ci LOG(ERROR) << "invalid iter ptable"; 82fb299fa2Sopenharmony_ci return false; 83fb299fa2Sopenharmony_ci } 84fb299fa2Sopenharmony_ci if (!iter->GetPtableImageBuffer(imageBuf, imgBufSize)) { 85fb299fa2Sopenharmony_ci LOG(ERROR) << "get ptable image buffer failed"; 86fb299fa2Sopenharmony_ci return false; 87fb299fa2Sopenharmony_ci } 88fb299fa2Sopenharmony_ci } 89fb299fa2Sopenharmony_ci return true; 90fb299fa2Sopenharmony_ci} 91fb299fa2Sopenharmony_ci 92fb299fa2Sopenharmony_cibool CompositePtable::EditPartitionBuf(uint8_t *imageBuf, uint64_t imgBufSize, std::vector<PtnInfo> &modifyList) 93fb299fa2Sopenharmony_ci{ 94fb299fa2Sopenharmony_ci if (!CheckBuff(imageBuf, imgBufSize)) { 95fb299fa2Sopenharmony_ci LOG(ERROR) << "input invalid"; 96fb299fa2Sopenharmony_ci return false; 97fb299fa2Sopenharmony_ci } 98fb299fa2Sopenharmony_ci for (const auto &iter : childs_) { 99fb299fa2Sopenharmony_ci if (iter == nullptr) { 100fb299fa2Sopenharmony_ci LOG(ERROR) << "invalid iter ptable"; 101fb299fa2Sopenharmony_ci return false; 102fb299fa2Sopenharmony_ci } 103fb299fa2Sopenharmony_ci if (!iter->EditPartitionBuf(imageBuf, imgBufSize, modifyList)) { 104fb299fa2Sopenharmony_ci LOG(ERROR) << "get ptable image buffer failed"; 105fb299fa2Sopenharmony_ci return false; 106fb299fa2Sopenharmony_ci } 107fb299fa2Sopenharmony_ci } 108fb299fa2Sopenharmony_ci return true; 109fb299fa2Sopenharmony_ci} 110fb299fa2Sopenharmony_ci 111fb299fa2Sopenharmony_civoid CompositePtable::AddChildPtable(std::unique_ptr<Ptable> child) 112fb299fa2Sopenharmony_ci{ 113fb299fa2Sopenharmony_ci if (child == nullptr) { 114fb299fa2Sopenharmony_ci LOG(ERROR) << "input is null"; 115fb299fa2Sopenharmony_ci return; 116fb299fa2Sopenharmony_ci } 117fb299fa2Sopenharmony_ci if (!(child->InitPtable())) { 118fb299fa2Sopenharmony_ci LOG(ERROR) << "init child ptable failed"; 119fb299fa2Sopenharmony_ci return; 120fb299fa2Sopenharmony_ci } 121fb299fa2Sopenharmony_ci childs_.emplace_back(std::move(child)); 122fb299fa2Sopenharmony_ci} 123fb299fa2Sopenharmony_ci 124fb299fa2Sopenharmony_civoid CompositePtable::AppendChildPtnInfo(const std::vector<PtnInfo> &ptnInfo) 125fb299fa2Sopenharmony_ci{ 126fb299fa2Sopenharmony_ci partitionInfo_.insert(partitionInfo_.end(), ptnInfo.begin(), ptnInfo.end()); 127fb299fa2Sopenharmony_ci} 128fb299fa2Sopenharmony_ci} 129