1 /* 2 * Copyright (c) 2021 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 #ifndef UPDATER_PARTITION_UPDATE_RECORD_H 16 #define UPDATER_PARTITION_UPDATE_RECORD_H 17 18 #include <cstdio> 19 #include <unistd.h> 20 #include <cassert> 21 #include "fs_manager/mount.h" 22 #include "misc_info/misc_info.h" 23 24 #ifndef O_BINARY 25 #define O_BINARY 0 26 #endif 27 28 namespace Updater { 29 // Each partition takes 126 bytes 30 // So total size of partition record is 31 // 126 * 8 = 1008. round up to 1024 bytes 32 constexpr int MAX_PARTITION_NUM = 8; 33 // Refer to updater package format restriction. 34 constexpr int PARTITION_NAME_LEN = 125; 35 constexpr int PARTITION_RECORD_INFO_LEN = 126; 36 constexpr off_t PARTITION_RECORD_OFFSET = MISC_PARTITION_RECORD_OFFSET; 37 constexpr off_t PARTITION_UPDATER_RECORD_SIZE = MISC_PARTITION_RECORD_SIZE; 38 constexpr off_t PARTITION_RECORD_OFFSET_SIZE = 16; 39 constexpr off_t PARTITION_UPDATER_RECORD_MSG_SIZE = PARTITION_UPDATER_RECORD_SIZE - PARTITION_RECORD_OFFSET_SIZE; 40 constexpr off_t PARTITION_RECORD_START = MISC_PARTITION_RECORD_OFFSET + PARTITION_RECORD_OFFSET_SIZE; 41 struct PartitionRecordInfo { 42 char partitionName[PARTITION_NAME_LEN]; 43 // true: this partition is updated already. 44 // false: this partition is not updated. 45 bool updated; 46 }; 47 48 static_assert(sizeof(PartitionRecordInfo) == PARTITION_RECORD_INFO_LEN, 49 "Size of PartitionRecord is not correct"); 50 51 class PartitionRecord { 52 public: 53 static PartitionRecord &GetInstance(); 54 55 // Check if partition updated in previous updating. 56 bool IsPartitionUpdated(const std::string &partitionName); 57 58 bool RecordPartitionUpdateStatus(const std::string &partitionName, bool status); 59 60 bool ClearRecordPartitionOffset(); 61 62 private: 63 bool RecordPartitionSetOffset(int fd); 64 bool RecordPartitionSetInfo(const std::string &partitionName, bool updated, int fd); PartitionRecord()65 PartitionRecord() 66 { 67 offset_ = 0; 68 } 69 ~PartitionRecord()70 ~PartitionRecord() {} 71 72 private: 73 std::string GetMiscPartitionPath(const std::string &mountPoint = "/misc"); 74 75 PartitionRecordInfo info_ {}; 76 // offset of partition record in misc. 77 // offset is not start from zero, but 78 // start from the global offset of misc partition. 79 off_t offset_; 80 }; 81 } // namespace Updater 82 #endif // UPDATER_PARTITION_UPDATE_RECORD_H 83