1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "composite_ptable.h"
17 #include "log/log.h"
18 
19 namespace Updater {
ParsePartitionFromBuffer(uint8_t * ptbImgBuffer,const uint32_t imgBufSize)20 bool CompositePtable::ParsePartitionFromBuffer(uint8_t *ptbImgBuffer, const uint32_t imgBufSize)
21 {
22     if (!CheckBuff(ptbImgBuffer, static_cast<const uint64_t>(imgBufSize))) {
23         LOG(ERROR) << "input invalid";
24         return false;
25     }
26     std::vector<PtnInfo>().swap(partitionInfo_);
27     for (const auto &iter : childs_) {
28         if (iter == nullptr) {
29             LOG(ERROR) << "invalid iter ptable";
30             return false;
31         }
32         if (!iter->ParsePartitionFromBuffer(ptbImgBuffer, imgBufSize)) {
33             LOG(ERROR) << "parse partition from buffer failed";
34             return false;
35         }
36         AppendChildPtnInfo(iter->GetPtablePartitionInfo());
37     }
38     return true;
39 }
40 
LoadPtableFromDevice()41 bool CompositePtable::LoadPtableFromDevice()
42 {
43     std::vector<PtnInfo>().swap(partitionInfo_);
44     for (const auto &iter : childs_) {
45         if (iter == nullptr) {
46             LOG(ERROR) << "invalid iter ptable";
47             return false;
48         }
49         if (!iter->LoadPtableFromDevice()) {
50             LOG(ERROR) << "load ptable from device failed";
51             return false;
52         }
53         AppendChildPtnInfo(iter->GetPtablePartitionInfo());
54     }
55     return true;
56 }
57 
WritePartitionTable()58 bool CompositePtable::WritePartitionTable()
59 {
60     for (const auto &iter : childs_) {
61         if (iter == nullptr) {
62             LOG(ERROR) << "invalid iter ptable";
63             return false;
64         }
65         if (!iter->WritePartitionTable()) {
66             LOG(ERROR) << "write ptable failed";
67             return false;
68         }
69     }
70     return true;
71 }
72 
GetPtableImageBuffer(uint8_t * imageBuf,const uint32_t imgBufSize)73 bool CompositePtable::GetPtableImageBuffer(uint8_t *imageBuf, const uint32_t imgBufSize)
74 {
75     if (!CheckBuff(imageBuf, static_cast<const uint64_t>(imgBufSize))) {
76         LOG(ERROR) << "input invalid";
77         return false;
78     }
79     for (const auto &iter : childs_) {
80         if (iter == nullptr) {
81             LOG(ERROR) << "invalid iter ptable";
82             return false;
83         }
84         if (!iter->GetPtableImageBuffer(imageBuf, imgBufSize)) {
85             LOG(ERROR) << "get ptable image buffer failed";
86             return false;
87         }
88     }
89     return true;
90 }
91 
EditPartitionBuf(uint8_t * imageBuf,uint64_t imgBufSize,std::vector<PtnInfo> & modifyList)92 bool CompositePtable::EditPartitionBuf(uint8_t *imageBuf, uint64_t imgBufSize, std::vector<PtnInfo> &modifyList)
93 {
94     if (!CheckBuff(imageBuf, imgBufSize)) {
95         LOG(ERROR) << "input invalid";
96         return false;
97     }
98     for (const auto &iter : childs_) {
99         if (iter == nullptr) {
100             LOG(ERROR) << "invalid iter ptable";
101             return false;
102         }
103         if (!iter->EditPartitionBuf(imageBuf, imgBufSize, modifyList)) {
104             LOG(ERROR) << "get ptable image buffer failed";
105             return false;
106         }
107     }
108     return true;
109 }
110 
AddChildPtable(std::unique_ptr<Ptable> child)111 void CompositePtable::AddChildPtable(std::unique_ptr<Ptable> child)
112 {
113     if (child == nullptr) {
114         LOG(ERROR) << "input is null";
115         return;
116     }
117     if (!(child->InitPtable())) {
118         LOG(ERROR) << "init child ptable failed";
119         return;
120     }
121     childs_.emplace_back(std::move(child));
122 }
123 
AppendChildPtnInfo(const std::vector<PtnInfo> & ptnInfo)124 void CompositePtable::AppendChildPtnInfo(const std::vector<PtnInfo> &ptnInfo)
125 {
126     partitionInfo_.insert(partitionInfo_.end(), ptnInfo.begin(), ptnInfo.end());
127 }
128 }
129