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