1 /*
2  * Copyright (C) 2015 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 <stdio.h>
18 
19 #include <string>
20 
21 #include "fs_mgr.h"
22 #include "fs_mgr_priv.h"
23 
24 // Realistically, this file should be part of the android::fs_mgr namespace;
25 using namespace android::fs_mgr;
26 
27 // https://source.android.com/devices/tech/ota/ab/ab_implement#partitions
28 // All partitions that are A/B-ed should be named as follows (slots are always
29 // named a, b, etc.): boot_a, boot_b, system_a, system_b, vendor_a, vendor_b.
other_suffix(const std::string & slot_suffix)30 static std::string other_suffix(const std::string& slot_suffix) {
31     if (slot_suffix == "_a") {
32         return "_b";
33     }
34     if (slot_suffix == "_b") {
35         return "_a";
36     }
37     return "";
38 }
39 
40 // Returns "_b" or "_a", which is *the other* slot of androidboot.slot_suffix
41 // in kernel cmdline, or an empty string if that parameter does not exist.
fs_mgr_get_other_slot_suffix()42 std::string fs_mgr_get_other_slot_suffix() {
43     return other_suffix(fs_mgr_get_slot_suffix());
44 }
45 
46 // Returns "_a" or "_b" based on androidboot.slot_suffix in kernel cmdline, or an empty string
47 // if that parameter does not exist.
fs_mgr_get_slot_suffix()48 std::string fs_mgr_get_slot_suffix() {
49     std::string ab_suffix;
50 
51     fs_mgr_get_boot_config("slot_suffix", &ab_suffix);
52     return ab_suffix;
53 }
54 
55 // Updates |fstab| for slot_suffix. Returns true on success, false on error.
fs_mgr_update_for_slotselect(Fstab * fstab)56 bool fs_mgr_update_for_slotselect(Fstab* fstab) {
57     std::string ab_suffix;
58 
59     for (auto& entry : *fstab) {
60         if (!entry.fs_mgr_flags.slot_select && !entry.fs_mgr_flags.slot_select_other) {
61             continue;
62         }
63 
64         if (ab_suffix.empty()) {
65             ab_suffix = fs_mgr_get_slot_suffix();
66             // Return false if failed to get ab_suffix when MF_SLOTSELECT is specified.
67             if (ab_suffix.empty()) return false;
68         }
69 
70         const auto& update_suffix =
71                 entry.fs_mgr_flags.slot_select ? ab_suffix : other_suffix(ab_suffix);
72         entry.blk_device = entry.blk_device + update_suffix;
73         entry.logical_partition_name = entry.logical_partition_name + update_suffix;
74     }
75     return true;
76 }
77