1/*
2 * Copyright (C) 2017 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 "VintfObjectRecovery.h"
18
19#include <android-base/properties.h>
20#include <sys/mount.h>
21#include <fs_mgr.h>
22
23#include "utils.h"
24
25namespace android {
26namespace vintf {
27
28namespace details {
29using FstabMgr = std::unique_ptr<struct fstab, decltype(&fs_mgr_free_fstab)>;
30
31static status_t mountAt(const FstabMgr &fstab, const char* path, const char* mount_point) {
32    fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab.get(), path);
33    if (rec == nullptr) {
34        return UNKNOWN_ERROR;
35    }
36    int result = mount(rec->blk_device, mount_point, rec->fs_type, rec->flags, rec->fs_options);
37    return result == 0 ? OK : -errno;
38}
39
40static FstabMgr defaultFstabMgr() {
41    return FstabMgr{fs_mgr_read_fstab_default(), &fs_mgr_free_fstab};
42}
43
44class RecoveryPartitionMounter : public PartitionMounter {
45   public:
46    status_t mountSystem() const override {
47        FstabMgr fstab = defaultFstabMgr();
48        if (fstab == NULL) {
49            return UNKNOWN_ERROR;
50        }
51        if (android::base::GetBoolProperty("ro.build.system_root_image", false)) {
52            return mountAt(fstab, "/", "/system_root");
53        } else {
54            return mountAt(fstab, "/system", "/system");
55        }
56    }
57
58    status_t mountVendor() const override {
59        FstabMgr fstab = defaultFstabMgr();
60        if (fstab == NULL) {
61            return UNKNOWN_ERROR;
62        }
63        return mountAt(fstab, "/vendor", "/vendor");
64    }
65
66    status_t umountSystem() const override {
67        if (android::base::GetBoolProperty("ro.build.system_root_image", false)) {
68            return umount("/system_root");
69        } else {
70            return umount("/system");
71        }
72    }
73
74    status_t umountVendor() const override {
75        return umount("/vendor");
76    }
77};
78
79} // namespace details
80
81// static
82int32_t VintfObjectRecovery::CheckCompatibility(
83        const std::vector<std::string> &xmls, std::string *error) {
84    static details::RecoveryPartitionMounter mounter;
85    return details::checkCompatibility(xmls, true /* mount */, mounter, error);
86}
87
88
89} // namespace vintf
90} // namespace android
91