1 //
2 // Copyright (C) 2019 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 #pragma once
18
19 #include <string>
20
21 #include <android-base/file.h>
22 #include <android-base/strings.h>
23
24 namespace android {
25 namespace gsi {
26
27 #define DSU_METADATA_PREFIX "/metadata/gsi/dsu/"
28
29 // These files need to be globally readable so that fs_mgr_fstab, which is
30 // statically linked into processes, can return consistent result for non-root
31 // processes:
32 // * kDsuActiveFile
33 // * kGsiBootedIndicatorFile
34 // * kGsiLpNamesFile
35 // * DsuMetadataKeyDirFile(slot)
36
37 static constexpr char kGsiBootedIndicatorFile[] = DSU_METADATA_PREFIX "booted";
38
39 static constexpr char kGsiLpNamesFile[] = DSU_METADATA_PREFIX "lp_names";
40
41 static constexpr char kDsuActiveFile[] = DSU_METADATA_PREFIX "active";
42
43 static constexpr char kDsuAvbKeyDir[] = DSU_METADATA_PREFIX "avb/";
44
45 static constexpr char kDsuMetadataKeyDirPrefix[] = "/metadata/vold/metadata_encryption/dsu/";
46
47 static constexpr char kDsuSDPrefix[] = "/mnt/media_rw/";
48
49 // GSI-specific init script defined in build/make/target/product/gsi/Android.mk
50 static constexpr char kGsiSpecificInitRcFile[] = "/system/system_ext/etc/init/init.gsi.rc";
51
DsuLpMetadataFile(const std::string & dsu_slot)52 static inline std::string DsuLpMetadataFile(const std::string& dsu_slot) {
53 return DSU_METADATA_PREFIX + dsu_slot + "/lp_metadata";
54 }
55
DsuInstallDirFile(const std::string & dsu_slot)56 static inline std::string DsuInstallDirFile(const std::string& dsu_slot) {
57 return DSU_METADATA_PREFIX + dsu_slot + "/install_dir";
58 }
59
DsuMetadataKeyDirFile(const std::string & dsu_slot)60 static inline std::string DsuMetadataKeyDirFile(const std::string& dsu_slot) {
61 return DSU_METADATA_PREFIX + dsu_slot + "/metadata_encryption_dir";
62 }
63
DefaultDsuMetadataKeyDir(const std::string & dsu_slot)64 static inline std::string DefaultDsuMetadataKeyDir(const std::string& dsu_slot) {
65 return kDsuMetadataKeyDirPrefix + dsu_slot;
66 }
67
GetDsuMetadataKeyDir(const std::string & dsu_slot)68 static inline std::string GetDsuMetadataKeyDir(const std::string& dsu_slot) {
69 auto key_dir_file = DsuMetadataKeyDirFile(dsu_slot);
70 std::string key_dir;
71 if (android::base::ReadFileToString(key_dir_file, &key_dir) &&
72 android::base::StartsWith(key_dir, kDsuMetadataKeyDirPrefix)) {
73 return key_dir;
74 }
75 return DefaultDsuMetadataKeyDir(dsu_slot);
76 }
77
78 // install_dir "/data/gsi/dsu/dsu" has a slot name "dsu"
79 // install_dir "/data/gsi/dsu/dsu2" has a slot name "dsu2"
80 std::string GetDsuSlot(const std::string& install_dir);
81
82 static constexpr char kDsuSlotProp[] = "ro.gsid.dsu_slot";
83
84 static constexpr char kGsiBootedProp[] = "ro.gsid.image_running";
85
86 static constexpr char kGsiInstalledProp[] = "gsid.image_installed";
87
88 static constexpr char kDsuPostfix[] = "_gsi";
89
90 inline constexpr char kDsuScratch[] = "scratch_gsi";
91 inline constexpr char kDsuUserdata[] = "userdata_gsi";
92
93 static constexpr int kMaxBootAttempts = 1;
94
95 // Get the currently active dsu slot
96 // Return true on success
GetActiveDsu(std::string * active_dsu)97 static inline bool GetActiveDsu(std::string* active_dsu) {
98 return android::base::ReadFileToString(kDsuActiveFile, active_dsu);
99 }
100
101 // Returns true if the currently running system image is a GSI (both dynamic and flashed).
102 bool IsGsiImage();
103
104 // Returns true if the currently running system image is a live (dynamic) GSI.
105 bool IsGsiRunning();
106
107 // Return true if a GSI is installed (but not necessarily running).
108 bool IsGsiInstalled();
109
110 // Set the GSI as no longer bootable. This effectively removes the GSI. If no
111 // GSI was bootable, false is returned.
112 bool UninstallGsi();
113
114 // Set the GSI as no longer bootable, without removing its installed files.
115 bool DisableGsi();
116
117 // Returns true if init should attempt to boot into a live GSI image, false
118 // otherwise. If false, an error message is set.
119 //
120 // This is only called by first-stage init.
121 bool CanBootIntoGsi(std::string* error);
122
123 // Called by first-stage init to indicate that we're about to boot into a
124 // GSI.
125 bool MarkSystemAsGsi();
126
127 } // namespace gsi
128 } // namespace android
129