1 /*
2 * Copyright (c) 2022 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 "ptable_manager.h"
17
18 #include "log/log.h"
19 #include "securec.h"
20 #include "updater/updater_const.h"
21
22 namespace Updater {
23 std::string PtableManager::ptbImgTag_ = "";
24 // class PtableManager
PtableManager()25 PtableManager::PtableManager() : pPtable_(nullptr)
26 {
27 InitPtablePtr();
28 PtableManager::ptbImgTag_ = "ptable.img";
29 }
30
GetDeviceStorageType()31 PtableManager::StorageType PtableManager::GetDeviceStorageType()
32 {
33 return storage_;
34 }
35
SetDeviceStorageType()36 void PtableManager::SetDeviceStorageType()
37 {
38 if (storage_ != StorageType::STORAGE_UNKNOWN) {
39 return;
40 }
41 if (IsUfsDevice()) {
42 storage_ = StorageType::STORAGE_UFS;
43 LOG(INFO) << "is UFS DEVICE";
44 } else {
45 storage_ = StorageType::STORAGE_EMMC;
46 LOG(INFO) << "is EMMC DEVICE";
47 }
48 }
49
IsUfsDevice()50 bool PtableManager::IsUfsDevice()
51 {
52 std::string filePath = std::string(BOOTDEV_TYPE);
53 if (filePath.empty()) {
54 LOG(ERROR) << "filePath is empty or lunCapacity is nullptr";
55 return false;
56 }
57 std::ifstream fin(filePath, std::ios::in);
58 if (!fin.is_open()) {
59 LOG(ERROR) << "open " << filePath << " fail";
60 return false;
61 }
62 uint64_t type = 0;
63 fin >> type;
64 fin.close();
65 return type != 0;
66 }
67
ReloadDevicePartition(Hpackage::PkgManager * pkgManager)68 void PtableManager::ReloadDevicePartition(Hpackage::PkgManager *pkgManager)
69 {
70 return LoadPartitionInfo(pkgManager);
71 }
72
InitPtablePtr()73 void PtableManager::InitPtablePtr()
74 {
75 SetDeviceStorageType();
76 if (pPtable_ == nullptr) {
77 if (GetDeviceStorageType() == StorageType::STORAGE_UFS) {
78 pPtable_ = std::make_unique<UfsPtable>();
79 } else {
80 pPtable_ = std::make_unique<EmmcPtable>();
81 }
82 }
83 }
84
InitPtableManager()85 bool PtableManager::InitPtableManager()
86 {
87 if (pPtable_ == nullptr) {
88 LOG(ERROR) << "pPtable_ is nullptr";
89 return false;
90 }
91 if (!pPtable_->InitPtable()) {
92 LOG(ERROR) << "init ptable error";
93 return false;
94 }
95 return true;
96 }
97
GetPartitionInfoIndexByName(const std::vector<Ptable::PtnInfo> & ptnInfo,const std::string & name)98 int32_t PtableManager::GetPartitionInfoIndexByName(const std::vector<Ptable::PtnInfo> &ptnInfo,
99 const std::string &name)
100 {
101 if (ptnInfo.empty() || name.size() == 0) {
102 LOG(ERROR) << "invalid input: ptnInfo is empty or name is null";
103 return -1;
104 }
105
106 for (size_t i = 0; i < ptnInfo.size(); i++) {
107 if (ptnInfo[i].dispName == name) {
108 return i;
109 }
110 }
111 return -1;
112 }
113
IsPartitionChanged(const std::vector<Ptable::PtnInfo> & devicePtnInfo,const std::vector<Ptable::PtnInfo> & pkgPtnInfo,const std::string & partitionName)114 bool PtableManager::IsPartitionChanged(const std::vector<Ptable::PtnInfo> &devicePtnInfo,
115 const std::vector<Ptable::PtnInfo> &pkgPtnInfo, const std::string &partitionName)
116 {
117 if (pkgPtnInfo.empty()) {
118 LOG(INFO) << "No ptable in package. Ptable no changed!";
119 return false;
120 }
121 if (devicePtnInfo.empty()) {
122 LOG(WARNING) << "ptable sizes in device and package are different, partition is changed";
123 return true;
124 }
125 int32_t deviceIndex = GetPartitionInfoIndexByName(devicePtnInfo, partitionName);
126 if (deviceIndex < 0) {
127 LOG(ERROR) << "can't find the " << partitionName << " partition in device ptable!";
128 return true;
129 }
130 int32_t updateIndex = GetPartitionInfoIndexByName(pkgPtnInfo, partitionName);
131 if (updateIndex < 0) {
132 LOG(ERROR) << "can't find the " << partitionName << " partition in package ptable!";
133 return true;
134 }
135 bool ret = false;
136 if (devicePtnInfo[deviceIndex].startAddr != pkgPtnInfo[updateIndex].startAddr) {
137 LOG(INFO) << partitionName << " start address is changed:";
138 LOG(INFO) << "[" << partitionName << "]: device ptable[" << deviceIndex << "] startAddr = 0x" <<
139 devicePtnInfo[deviceIndex].startAddr << ", in package ptable[" << updateIndex << "] startAddr is 0x" <<
140 pkgPtnInfo[updateIndex].startAddr;
141 ret = true;
142 }
143 if (devicePtnInfo[deviceIndex].partitionSize != pkgPtnInfo[updateIndex].partitionSize) {
144 LOG(INFO) << partitionName << " partition size is changed:";
145 LOG(INFO) << "[" << partitionName << "]: device ptable[" << deviceIndex << "] partitionSize = 0x" <<
146 devicePtnInfo[deviceIndex].partitionSize << ", in package ptable[" << updateIndex <<
147 "] partitionSize is 0x" << pkgPtnInfo[updateIndex].partitionSize;
148 ret = true;
149 }
150 return ret;
151 }
152
IsPtableChanged(const std::vector<Ptable::PtnInfo> & devicePtnInfo,const std::vector<Ptable::PtnInfo> & pkgPtnInfo)153 bool PtableManager::IsPtableChanged(const std::vector<Ptable::PtnInfo> &devicePtnInfo,
154 const std::vector<Ptable::PtnInfo> &pkgPtnInfo)
155 {
156 if (pkgPtnInfo.empty()) {
157 LOG(INFO) << "No ptable in package. Ptable no changed!";
158 return false;
159 }
160 if (devicePtnInfo.empty() || pkgPtnInfo.size() != devicePtnInfo.size()) {
161 LOG(WARNING) << "ptable sizes in device and package are different, ptable is changed";
162 return true;
163 }
164 for (size_t i = 0; i < pkgPtnInfo.size(); i++) {
165 if (devicePtnInfo[i].dispName != pkgPtnInfo[i].dispName) {
166 LOG(WARNING) << "module_name in ptable is different:";
167 LOG(WARNING) << "ptable NAME in device is " << devicePtnInfo[i].dispName <<
168 ", in package is " << pkgPtnInfo[i].dispName;
169 return true;
170 }
171 if (devicePtnInfo[i].startAddr != pkgPtnInfo[i].startAddr) {
172 LOG(WARNING) << pkgPtnInfo[i].dispName << " start address is different:";
173 LOG(WARNING) << "Device ptable [" << devicePtnInfo[i].dispName << "] startAddr is 0x" <<
174 devicePtnInfo[i].startAddr;
175 LOG(WARNING) << "Package ptable [" << pkgPtnInfo[i].dispName << "] startAddr is 0x" <<
176 pkgPtnInfo[i].startAddr;
177 return true;
178 }
179 if (devicePtnInfo[i].partitionSize != pkgPtnInfo[i].partitionSize) {
180 LOG(WARNING) << pkgPtnInfo[i].dispName << " partition size is different:";
181 LOG(WARNING) << "Device ptable [" << devicePtnInfo[i].dispName << "] partitionSize is 0x" <<
182 devicePtnInfo[i].partitionSize;
183 LOG(WARNING) << "Package ptable [" << pkgPtnInfo[i].dispName << "] partitionSize is 0x" <<
184 pkgPtnInfo[i].partitionSize;
185 return true;
186 }
187 }
188 return false;
189 }
190
WritePtableToDevice()191 bool PtableManager::WritePtableToDevice()
192 {
193 if (pPtable_ == nullptr) {
194 LOG(ERROR) << "Write ptable to device failed! pPtable_ is nullptr";
195 return false;
196 }
197 if (!pPtable_->WritePartitionTable()) {
198 LOG(ERROR) << "Write ptable to device failed! Please load ptable first!";
199 return false;
200 }
201 LOG(INFO) << "Write ptable to device success!";
202 return true;
203 }
204
PrintPtableInfo()205 void PtableManager::PrintPtableInfo()
206 {
207 if (pPtable_ != nullptr) {
208 LOG(ERROR) << "print partition info:";
209 pPtable_->PrintPtableInfo();
210 return;
211 }
212
213 LOG(INFO) << "print partition info failed!";
214 return;
215 }
216
GetPartionInfoByName(const std::string & partitionName,Ptable::PtnInfo & ptnInfo,int32_t & index)217 bool PtableManager::GetPartionInfoByName(const std::string &partitionName, Ptable::PtnInfo &ptnInfo, int32_t &index)
218 {
219 if (pPtable_ == nullptr) {
220 LOG(ERROR) << "GetPartionInfoByName failed! pPtable_ is nullptr";
221 return false;
222 }
223 std::string standardPtnName = partitionName;
224 standardPtnName.erase(std::remove(standardPtnName.begin(), standardPtnName.end(), '/'), standardPtnName.end());
225 if (pPtable_->GetPartionInfoByName(standardPtnName, ptnInfo, index)) {
226 LOG(INFO) << "GetPartionInfoByName success!";
227 return true;
228 }
229 LOG(ERROR) << "GetPartionInfoByName failed! Not found " << standardPtnName;
230 return false;
231 }
232
GetPartionInfoByName(const std::string & partitionName,Ptable::PtnInfo & ptnInfo)233 bool PtableManager::GetPartionInfoByName(const std::string &partitionName, Ptable::PtnInfo &ptnInfo)
234 {
235 int32_t index = -1;
236 return GetPartionInfoByName(partitionName, ptnInfo, index);
237 }
238
239 // class PackagePtable
PackagePtable()240 PackagePtable::PackagePtable() : PtableManager() {}
241
LoadPartitionInfo(Hpackage::PkgManager * pkgManager)242 void PackagePtable::LoadPartitionInfo([[maybe_unused]] Hpackage::PkgManager *pkgManager)
243 {
244 if (pkgManager == nullptr) {
245 LOG(ERROR) << "pkgManager is nullptr";
246 return;
247 }
248 if (!InitPtableManager()) {
249 LOG(ERROR) << "init ptable manager error";
250 return;
251 }
252
253 uint32_t imgBufSize = pPtable_->GetDefaultImageSize();
254 if (imgBufSize <= 0) {
255 LOG(ERROR) << "Invalid imgBufSize";
256 return;
257 }
258 uint8_t *imageBuf = new(std::nothrow) uint8_t[imgBufSize]();
259
260 if (imageBuf == nullptr) {
261 LOG(ERROR) << "new ptable_buffer error";
262 return;
263 }
264 if (!GetPtableBufferFromPkg(pkgManager, imageBuf, imgBufSize)) {
265 LOG(ERROR) << "get ptable buffer failed";
266 delete [] imageBuf;
267 return;
268 }
269
270 if (!pPtable_->ParsePartitionFromBuffer(imageBuf, imgBufSize)) {
271 LOG(ERROR) << "get ptable from ptable image buffer failed";
272 delete [] imageBuf;
273 return;
274 }
275 delete [] imageBuf;
276 LOG(INFO) << "print package partition info:";
277 pPtable_->PrintPtableInfo();
278 return;
279 }
280
GetPtableBufferFromPkg(Hpackage::PkgManager * pkgManager,uint8_t * & imageBuf,uint32_t size)281 bool PackagePtable::GetPtableBufferFromPkg(Hpackage::PkgManager *pkgManager, uint8_t *&imageBuf, uint32_t size)
282 {
283 if (pkgManager == nullptr) {
284 LOG(ERROR) << "pkgManager is nullptr";
285 return false;
286 }
287
288 const Hpackage::FileInfo *info = pkgManager->GetFileInfo(PtableManager::ptbImgTag_);
289 if (info == nullptr) {
290 info = pkgManager->GetFileInfo("/ptable");
291 if (info == nullptr) {
292 LOG(ERROR) << "Can not get file info " << PtableManager::ptbImgTag_;
293 return false;
294 }
295 }
296
297 Hpackage::PkgManager::StreamPtr outStream = nullptr;
298 (void)pkgManager->CreatePkgStream(outStream, PtableManager::ptbImgTag_, info->unpackedSize,
299 Hpackage::PkgStream::PkgStreamType_MemoryMap);
300 if (outStream == nullptr) {
301 LOG(ERROR) << "Error to create output stream";
302 return false;
303 }
304
305 if (pkgManager->ExtractFile(PtableManager::ptbImgTag_, outStream) != Hpackage::PKG_SUCCESS) {
306 LOG(ERROR) << "Error to extract ptable";
307 pkgManager->ClosePkgStream(outStream);
308 return false;
309 }
310
311 size_t bufSize = 0;
312 uint8_t* buffer = nullptr;
313 outStream->GetBuffer(buffer, bufSize);
314 if (memcpy_s(imageBuf, size, buffer, std::min(static_cast<size_t>(size), bufSize))) {
315 LOG(ERROR) << "memcpy to imageBuf fail";
316 pkgManager->ClosePkgStream(outStream);
317 return false;
318 }
319 pkgManager->ClosePkgStream(outStream);
320 return true;
321 }
322
323 // class DevicePtable
DevicePtable()324 DevicePtable::DevicePtable() : PtableManager() {}
325
LoadPartitionInfo(Hpackage::PkgManager * pkgManager)326 void DevicePtable::LoadPartitionInfo([[maybe_unused]] Hpackage::PkgManager *pkgManager)
327 {
328 (void)pkgManager;
329 if (!InitPtableManager()) {
330 LOG(ERROR) << "init ptable manager error";
331 return;
332 }
333 if (!pPtable_->LoadPtableFromDevice()) {
334 LOG(ERROR) << "load device parititon to ram fail";
335 return;
336 }
337
338 LOG(INFO) << "print device partition info:";
339 pPtable_->PrintPtableInfo();
340 return;
341 }
342
ComparePartition(PtableManager & newPtbManager,const std::string partitionName)343 bool DevicePtable::ComparePartition(PtableManager &newPtbManager, const std::string partitionName)
344 {
345 if (pPtable_ == nullptr || newPtbManager.pPtable_ == nullptr) {
346 LOG(ERROR) << "input pPtable point is nullptr, compare failed!";
347 return false;
348 }
349 if (IsPartitionChanged(pPtable_->GetPtablePartitionInfo(),
350 newPtbManager.pPtable_->GetPtablePartitionInfo(), partitionName)) {
351 LOG(INFO) << partitionName << " are different";
352 return true;
353 }
354 LOG(INFO) << partitionName << " are the same";
355 return false;
356 }
357
ComparePtable(PtableManager & newPtbManager)358 bool DevicePtable::ComparePtable(PtableManager &newPtbManager)
359 {
360 if (pPtable_ == nullptr || newPtbManager.pPtable_ == nullptr) {
361 LOG(ERROR) << "input pPtable point is nullptr, compare failed!";
362 return false;
363 }
364 if (IsPtableChanged(pPtable_->GetPtablePartitionInfo(),
365 newPtbManager.pPtable_->GetPtablePartitionInfo())) {
366 LOG(INFO) << "two ptables are different";
367 return true;
368 }
369 LOG(INFO) << "two ptables are the same";
370 return false;
371 }
372 } // namespace Updater
373