1. 結構關係
基類FirstStageMount.基類static std::unique_ptr Create(),就是返回類型爲FirstStageMount類型。
66 class FirstStageMount {
67 public:
68 FirstStageMount(Fstab fstab);
69 virtual ~FirstStageMount() = default;
70
71 // The factory method to create either FirstStageMountVBootV1 or FirstStageMountVBootV2
72 // based on device tree configurations.
73 static std::unique_ptr<FirstStageMount> Create();
74 bool DoFirstStageMount(); // Mounts fstab entries read from device tree.
75 bool InitDevices();
.......
77 protected:
78 ListenerAction HandleBlockDevice(const std::string& name, const Uevent&);
79 bool InitRequiredDevices();
80 bool InitMappedDevice(const std::string& verity_device);
81 bool InitDeviceMapper();
82 bool CreateLogicalPartitions();
83 bool MountPartition(const Fstab::iterator& begin, bool erase_same_mounts,
84 Fstab::iterator* end = nullptr);
85
86 bool MountPartitions();
87 bool TrySwitchSystemAsRoot();
88 bool TrySkipMountingPartitions();
89 bool IsDmLinearEnabled();
90 bool GetDmLinearMetadataDevice();
91 bool InitDmLinearBackingDevices(const android::fs_mgr::LpMetadata& metadata);
92 void UseGsiIfPresent();
93
94 ListenerAction UeventCallback(const Uevent& uevent);
95
96 // Pure virtual functions.
97 virtual bool GetDmVerityDevices() = 0;
98 virtual bool SetUpDmVerity(FstabEntry* fstab_entry) = 0;
99
100 bool need_dm_verity_;
101 bool gsi_not_on_userdata_ = false;
102
103 Fstab fstab_;
FirstStageMountVBootV1和FirstStageMountVBootV2類是FirstStageMount的子類。
111 class FirstStageMountVBootV1 : public FirstStageMount {
112 public:
113 FirstStageMountVBootV1(Fstab fstab) : FirstStageMount(std::move(fstab)) {}
114 ~FirstStageMountVBootV1() override = default;
115
116 protected:
117 bool GetDmVerityDevices() override;
118 bool SetUpDmVerity(FstabEntry* fstab_entry) override;
119 };
120
121 class FirstStageMountVBootV2 : public FirstStageMount {
122 public:
123 friend void SetInitAvbVersionInRecovery();
124
125 FirstStageMountVBootV2(Fstab fstab);
126 ~FirstStageMountVBootV2() override = default;
127
128 protected:
129 bool GetDmVerityDevices() override;
130 bool SetUpDmVerity(FstabEntry* fstab_entry) override;
131 bool InitAvbHandle();
132
133 std::vector<std::string> vbmeta_partitions_;
134 AvbUniquePtr avb_handle_;
135 };
create函數。
230 std::unique_ptr<FirstStageMount> FirstStageMount::Create() {
231 auto fstab = ReadFirstStageFstab();
232 if (IsDtVbmetaCompatible(fstab)) {
233 return std::make_unique<FirstStageMountVBootV2>(std::move(fstab));
234 } else {
235 return std::make_unique<FirstStageMountVBootV1>(std::move(fstab));
236 }
237 }
如果執行到235行,則執行FirstStageMountVBootV1的構造函數,參數是fstab;然後構造出FirstStageMountVBootV1的對象。
FirstStageMountVBootV2類的構造函數是FirstStageMountVBootV2()定義如下:
712 FirstStageMountVBootV2::FirstStageMountVBootV2(Fstab fstab)
713 : FirstStageMount(std::move(fstab)), avb_handle_(nullptr) {
714 std::string device_tree_vbmeta_parts;
715 read_android_dt_file("vbmeta/parts", &device_tree_vbmeta_parts);
716
717 for (auto&& partition : Split(device_tree_vbmeta_parts, ",")) {
718 if (!partition.empty()) {
719 vbmeta_partitions_.emplace_back(std::move(partition));
720 }
721 }
722
723 for (const auto& entry : fstab_) {
724 if (!entry.vbmeta_partition.empty()) {
725 vbmeta_partitions_.emplace_back(entry.vbmeta_partition);
726 }
727 }
728
729 if (vbmeta_partitions_.empty()) {
730 LOG(ERROR) << "Failed to read vbmeta partitions.";
731 }
732 }
構造函數的初始化分別執行了FirstStageMount(std::move(fstab)), avb_handle_(nullptr) .
220 FirstStageMount::FirstStageMount(Fstab fstab)
221 : need_dm_verity_(false), fstab_(std::move(fstab)), uevent_listener_(16 * 1024 * 1024) {
222 auto boot_devices = android::fs_mgr::GetBootDevices();
223 device_handler_ = std::make_unique<DeviceHandler>(
224 std::vector<Permissions>{}, std::vector<SysfsPermissions>{}, std::vector<Subsystem>{},
225 std::move(boot_devices), false);
226
227 super_partition_name_ = fs_mgr_get_super_partition_name();
228 }
顯然類中fstab_成員是傳進來的fstab.
從上面的分析可以看出,在create創建的FirstStageMountVBootV1對象的fstab_成員是由ReadFirstStageFstab()函數中fstab賦值的,ReadFirstStageFstab會從目錄/odm/etc/或者/vendor/etc/或者根目錄/中找文件fstab.,如果hardware=uml,則會找文件fstab.uml文件。
448 // Identify path to fstab file. Lookup is based on pattern fstab.<hardware>,
449 // fstab.<hardware.platform> in folders /odm/etc, vendor/etc, or /.
450 std::string GetFstabPath() {
451 for (const char* prop : {"hardware", "hardware.platform"}) {
452 std::string hw;
453
454 if (!fs_mgr_get_boot_config(prop, &hw)) continue;
455
456 for (const char* prefix : {"/odm/etc/fstab.", "/vendor/etc/fstab.", "/fstab."}) {
457 std::string fstab_path = prefix + hw;
458 if (access(fstab_path.c_str(), F_OK) == 0) {
459 return fstab_path;
460 }
461 }
462 }
463
464 return "";
465 }
2.
參考
template<typename T, typename... Ts>
std::unique_ptr<T> make_unique(Ts&&... params)
{
return std::unique_ptr<T>(new T(std::forward<Ts>(params)...));
}
make_unique只是完美轉發了它的參數給要創建的對象的構造函數,然後用new產生的原始指針來構造一個std::unique_ptr,最後返回一個創建的std::unique_ptr.