1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "libdm/dm_table.h" 18 19 #include <android-base/logging.h> 20 #include <android-base/macros.h> 21 22 namespace android { 23 namespace dm { 24 AddTarget(std::unique_ptr<DmTarget> && target)25bool DmTable::AddTarget(std::unique_ptr<DmTarget>&& target) { 26 if (!target->Valid()) { 27 return false; 28 } 29 targets_.push_back(std::move(target)); 30 return true; 31 } 32 RemoveTarget(std::unique_ptr<DmTarget> &&)33bool DmTable::RemoveTarget(std::unique_ptr<DmTarget>&& /* target */) { 34 return true; 35 } 36 valid() const37bool DmTable::valid() const { 38 if (targets_.empty()) { 39 LOG(ERROR) << "Device-mapper table must have at least one target."; 40 return ""; 41 } 42 if (targets_[0]->start() != 0) { 43 LOG(ERROR) << "Device-mapper table must start at logical sector 0."; 44 return ""; 45 } 46 return true; 47 } 48 num_sectors() const49uint64_t DmTable::num_sectors() const { 50 return valid() ? num_sectors_ : 0; 51 } 52 53 // Returns a string representation of the table that is ready to be passed 54 // down to the kernel for loading. 55 // 56 // Implementation must verify there are no gaps in the table, table starts 57 // with sector == 0, and iterate over each target to get its table 58 // serialized. Serialize() const59std::string DmTable::Serialize() const { 60 if (!valid()) { 61 return ""; 62 } 63 64 std::string table; 65 for (const auto& target : targets_) { 66 table += target->Serialize(); 67 } 68 return table; 69 } 70 71 } // namespace dm 72 } // namespace android 73