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 // Returns "_a" or "_b" based on two possible values in kernel cmdline:
25 //   - androidboot.slot = a or b OR
26 //   - androidboot.slot_suffix = _a or _b
27 // TODO: remove slot_suffix once it's deprecated.
fs_mgr_get_slot_suffix()28 std::string fs_mgr_get_slot_suffix() {
29     std::string slot;
30     std::string ab_suffix;
31 
32     if (fs_mgr_get_boot_config("slot", &slot)) {
33         ab_suffix = "_" + slot;
34     } else if (!fs_mgr_get_boot_config("slot_suffix", &ab_suffix)) {
35         ab_suffix = "";
36     }
37     return ab_suffix;
38 }
39 
40 // Updates |fstab| for slot_suffix. Returns true on success, false on error.
fs_mgr_update_for_slotselect(struct fstab * fstab)41 bool fs_mgr_update_for_slotselect(struct fstab *fstab) {
42     int n;
43     std::string ab_suffix;
44 
45     for (n = 0; n < fstab->num_entries; n++) {
46         if (fstab->recs[n].fs_mgr_flags & MF_SLOTSELECT) {
47             char *tmp;
48             if (ab_suffix.empty()) {
49                 ab_suffix = fs_mgr_get_slot_suffix();
50                 // Returns false as non A/B devices should not have MF_SLOTSELECT.
51                 if (ab_suffix.empty()) return false;
52             }
53             if (asprintf(&tmp, "%s%s", fstab->recs[n].blk_device, ab_suffix.c_str()) > 0) {
54                 free(fstab->recs[n].blk_device);
55                 fstab->recs[n].blk_device = tmp;
56             } else {
57                 return false;
58             }
59         }
60     }
61     return true;
62 }
63