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