Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2016 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 | |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 17 | #include "update_engine/aosp/daemon_state_android.h" |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 18 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 19 | #include <base/logging.h> |
| 20 | |
Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 21 | #include "update_engine/aosp/apex_handler_android.h" |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 22 | #include "update_engine/aosp/update_attempter_android.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 23 | #include "update_engine/common/boot_control.h" |
| 24 | #include "update_engine/common/boot_control_stub.h" |
| 25 | #include "update_engine/common/hardware.h" |
| 26 | #include "update_engine/common/prefs.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 27 | |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 28 | namespace chromeos_update_engine { |
| 29 | |
| 30 | bool DaemonStateAndroid::Initialize() { |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 31 | boot_control_ = boot_control::CreateBootControl(); |
| 32 | if (!boot_control_) { |
| 33 | LOG(WARNING) << "Unable to create BootControl instance, using stub " |
| 34 | << "instead. All update attempts will fail."; |
| 35 | boot_control_.reset(new BootControlStub()); |
| 36 | } |
| 37 | |
| 38 | hardware_ = hardware::CreateHardware(); |
| 39 | if (!hardware_) { |
Sen Jiang | 771f648 | 2018-04-04 17:59:10 -0700 | [diff] [blame] | 40 | LOG(ERROR) << "Error initializing the HardwareInterface."; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 41 | return false; |
| 42 | } |
| 43 | |
| 44 | LOG_IF(INFO, !hardware_->IsNormalBootMode()) << "Booted in dev mode."; |
| 45 | LOG_IF(INFO, !hardware_->IsOfficialBuild()) << "Booted non-official build."; |
| 46 | |
| 47 | // Initialize prefs. |
| 48 | base::FilePath non_volatile_path; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 49 | if (!hardware_->GetNonVolatileDirectory(&non_volatile_path)) { |
Kelvin Zhang | 33d1809 | 2020-06-29 16:33:10 -0400 | [diff] [blame] | 50 | prefs_.reset(new MemoryPrefs()); |
| 51 | LOG(WARNING) |
| 52 | << "Could not get a non-volatile directory, fall back to memory prefs"; |
| 53 | } else { |
| 54 | Prefs* prefs = new Prefs(); |
| 55 | prefs_.reset(prefs); |
| 56 | if (!prefs->Init(non_volatile_path.Append(kPrefsSubDirectory))) { |
| 57 | LOG(ERROR) << "Failed to initialize preferences."; |
| 58 | return false; |
| 59 | } |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Alex Deymo | 765580d | 2016-06-22 19:23:13 -0700 | [diff] [blame] | 62 | // The CertificateChecker singleton is used by the update attempter. |
| 63 | certificate_checker_.reset( |
| 64 | new CertificateChecker(prefs_.get(), &openssl_wrapper_)); |
| 65 | certificate_checker_->Init(); |
| 66 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 67 | // Initialize the UpdateAttempter before the UpdateManager. |
Nikita Ioffe | aedfef3 | 2021-04-28 13:54:14 +0100 | [diff] [blame^] | 68 | update_attempter_.reset(new UpdateAttempterAndroid(this, |
| 69 | prefs_.get(), |
| 70 | boot_control_.get(), |
| 71 | hardware_.get(), |
| 72 | CreateApexHandler())); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 73 | |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 74 | return true; |
| 75 | } |
| 76 | |
| 77 | bool DaemonStateAndroid::StartUpdater() { |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 78 | // The DaemonState in Android is a passive daemon. It will only start applying |
| 79 | // an update when instructed to do so from the exposed binder API. |
Alex Deymo | 0e061ae | 2016-02-09 17:49:03 -0800 | [diff] [blame] | 80 | update_attempter_->Init(); |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 81 | return true; |
| 82 | } |
| 83 | |
| 84 | void DaemonStateAndroid::AddObserver(ServiceObserverInterface* observer) { |
| 85 | service_observers_.insert(observer); |
| 86 | } |
| 87 | |
| 88 | void DaemonStateAndroid::RemoveObserver(ServiceObserverInterface* observer) { |
| 89 | service_observers_.erase(observer); |
| 90 | } |
| 91 | |
Alex Deymo | f8bfcff | 2016-02-02 21:22:11 -0800 | [diff] [blame] | 92 | ServiceDelegateAndroidInterface* DaemonStateAndroid::service_delegate() { |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 93 | return update_attempter_.get(); |
Alex Deymo | f8bfcff | 2016-02-02 21:22:11 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 96 | } // namespace chromeos_update_engine |