Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | #ifndef _RECOVERY_DEVICE_H |
| 18 | #define _RECOVERY_DEVICE_H |
| 19 | |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 20 | #include <stddef.h> |
| 21 | |
Tao Bao | 551d2c3 | 2018-05-09 20:53:13 -0700 | [diff] [blame] | 22 | #include <memory> |
Tianjie Xu | b63a221 | 2019-07-29 14:21:49 -0700 | [diff] [blame] | 23 | #include <optional> |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 24 | #include <string> |
| 25 | #include <vector> |
| 26 | |
Tao Bao | c16fd8a | 2018-04-30 17:12:03 -0700 | [diff] [blame] | 27 | // Forward declaration to avoid including "ui.h". |
| 28 | class RecoveryUI; |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 29 | |
Tianjie Xu | b63a221 | 2019-07-29 14:21:49 -0700 | [diff] [blame] | 30 | class BootState; |
| 31 | |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 32 | class Device { |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 33 | public: |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 34 | static constexpr const int kNoAction = -1; |
| 35 | static constexpr const int kHighlightUp = -2; |
| 36 | static constexpr const int kHighlightDown = -3; |
| 37 | static constexpr const int kInvokeItem = -4; |
| 38 | |
Tao Bao | d9cb014 | 2019-04-23 11:46:25 -0700 | [diff] [blame] | 39 | // ENTER vs REBOOT: The latter will trigger a reboot that goes through bootloader, which allows |
| 40 | // using a new bootloader / recovery image if applicable. For example, REBOOT_RESCUE goes from |
| 41 | // rescue -> bootloader -> rescue, whereas ENTER_RESCUE switches from recovery -> rescue |
| 42 | // directly. |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 43 | enum BuiltinAction { |
| 44 | NO_ACTION = 0, |
| 45 | REBOOT = 1, |
| 46 | APPLY_SDCARD = 2, |
| 47 | // APPLY_CACHE was 3. |
| 48 | APPLY_ADB_SIDELOAD = 4, |
| 49 | WIPE_DATA = 5, |
| 50 | WIPE_CACHE = 6, |
| 51 | REBOOT_BOOTLOADER = 7, |
| 52 | SHUTDOWN = 8, |
| 53 | VIEW_RECOVERY_LOGS = 9, |
| 54 | MOUNT_SYSTEM = 10, |
| 55 | RUN_GRAPHICS_TEST = 11, |
| 56 | RUN_LOCALE_TEST = 12, |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 57 | KEY_INTERRUPTED = 13, |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 58 | ENTER_FASTBOOT = 14, |
| 59 | ENTER_RECOVERY = 15, |
Tao Bao | c6dc325 | 2019-04-16 14:22:25 -0700 | [diff] [blame] | 60 | ENTER_RESCUE = 16, |
Tao Bao | d9cb014 | 2019-04-23 11:46:25 -0700 | [diff] [blame] | 61 | REBOOT_FASTBOOT = 17, |
| 62 | REBOOT_RECOVERY = 18, |
| 63 | REBOOT_RESCUE = 19, |
Mark Salyzyn | 488cc05 | 2019-05-20 10:36:16 -0700 | [diff] [blame] | 64 | REBOOT_FROM_FASTBOOT = 20, |
| 65 | SHUTDOWN_FROM_FASTBOOT = 21, |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 68 | explicit Device(RecoveryUI* ui); |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 69 | virtual ~Device() {} |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 70 | |
Tao Bao | 551d2c3 | 2018-05-09 20:53:13 -0700 | [diff] [blame] | 71 | // Returns a raw pointer to the RecoveryUI object. |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 72 | virtual RecoveryUI* GetUI() { |
Tao Bao | 551d2c3 | 2018-05-09 20:53:13 -0700 | [diff] [blame] | 73 | return ui_.get(); |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 74 | } |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 75 | |
Tao Bao | 551d2c3 | 2018-05-09 20:53:13 -0700 | [diff] [blame] | 76 | // Resets the UI object to the given UI. Used to override the default UI in case initialization |
| 77 | // failed, or we want a different UI for some reason. The device object will take the ownership. |
| 78 | virtual void ResetUI(RecoveryUI* ui) { |
| 79 | ui_.reset(ui); |
Jerry Zhang | 2dea53e | 2018-05-02 17:15:03 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Hongguang Chen | 0426727 | 2020-04-21 20:58:04 -0700 | [diff] [blame] | 82 | // Called before recovery mode started up, to perform whatever device-specific recovery mode |
| 83 | // preparation as needed. |
| 84 | virtual void PreRecovery() {} |
| 85 | |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 86 | // Called when recovery starts up (after the UI has been obtained and initialized and after the |
| 87 | // arguments have been parsed, but before anything else). |
Tianjie Xu | 8f39730 | 2018-08-20 13:40:47 -0700 | [diff] [blame] | 88 | virtual void StartRecovery() {} |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 89 | |
Hongguang Chen | 0426727 | 2020-04-21 20:58:04 -0700 | [diff] [blame] | 90 | // Called before fastboot mode is started up, to perform whatever device-specific fastboot mode |
| 91 | // preparation as needed. |
| 92 | virtual void PreFastboot() {} |
| 93 | |
| 94 | // Called when fastboot starts up (after the UI has been obtained and initialized and after the |
| 95 | // arguments have been parsed, but before anything else). |
| 96 | virtual void StartFastboot() {} |
| 97 | |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 98 | // Called from the main thread when recovery is at the main menu and waiting for input, and a key |
| 99 | // is pressed. (Note that "at" the main menu does not necessarily mean the menu is visible; |
Tao Bao | 551d2c3 | 2018-05-09 20:53:13 -0700 | [diff] [blame] | 100 | // recovery will be at the main menu with it invisible after an unsuccessful operation, such as |
| 101 | // failed to install an OTA package, or if recovery is started with no command.) |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 102 | // |
| 103 | // 'key' is the code of the key just pressed. (You can call IsKeyPressed() on the RecoveryUI |
Tao Bao | 551d2c3 | 2018-05-09 20:53:13 -0700 | [diff] [blame] | 104 | // object you returned from GetUI() if you want to find out if other keys are held down.) |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 105 | // |
| 106 | // 'visible' is true if the menu is visible. |
| 107 | // |
| 108 | // Returns one of the defined constants below in order to: |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 109 | // - move the menu highlight (kHighlight{Up,Down}: negative value) |
| 110 | // - invoke the highlighted item (kInvokeItem: negative value) |
| 111 | // - do nothing (kNoAction: negative value) |
| 112 | // - invoke a specific action (a menu position: non-negative value) |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 113 | virtual int HandleMenuKey(int key, bool visible); |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 114 | |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 115 | // Returns the list of menu items (a vector of strings). The menu_position passed to |
Tao Bao | 551d2c3 | 2018-05-09 20:53:13 -0700 | [diff] [blame] | 116 | // InvokeMenuItem() will correspond to the indexes into this array. |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 117 | virtual const std::vector<std::string>& GetMenuItems(); |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 118 | |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 119 | // Performs a recovery action selected from the menu. 'menu_position' will be the index of the |
| 120 | // selected menu item, or a non-negative value returned from HandleMenuKey(). The menu will be |
Tao Bao | bc982a4 | 2019-03-29 15:43:09 -0700 | [diff] [blame] | 121 | // hidden when this is called; implementations can call GetUI()->Print() to print information to |
| 122 | // the screen. If the menu position is one of the builtin actions, you can just return the |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 123 | // corresponding enum value. If it is an action specific to your device, you actually perform it |
| 124 | // here and return NO_ACTION. |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 125 | virtual BuiltinAction InvokeMenuItem(size_t menu_position); |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 126 | |
Tao Bao | e5d2c25 | 2018-05-09 11:05:44 -0700 | [diff] [blame] | 127 | // Removes the menu item for the given action. This allows tailoring the menu based on the |
| 128 | // runtime info, such as the availability of /cache or /sdcard. |
| 129 | virtual void RemoveMenuItemForAction(Device::BuiltinAction action); |
| 130 | |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 131 | // Called before and after we do a wipe data/factory reset operation, either via a reboot from the |
| 132 | // main system with the --wipe_data flag, or when the user boots into recovery image manually and |
| 133 | // selects the option from the menu, to perform whatever device-specific wiping actions as needed. |
| 134 | // Returns true on success; returning false from PreWipeData will prevent the regular wipe, and |
| 135 | // returning false from PostWipeData will cause the wipe to be considered a failure. |
| 136 | virtual bool PreWipeData() { |
| 137 | return true; |
| 138 | } |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 139 | |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 140 | virtual bool PostWipeData() { |
| 141 | return true; |
| 142 | } |
| 143 | |
Tianjie Xu | b63a221 | 2019-07-29 14:21:49 -0700 | [diff] [blame] | 144 | void SetBootState(const BootState* state); |
| 145 | // The getters for reason and stage may return std::nullopt until StartRecovery() is called. It's |
| 146 | // the caller's responsibility to perform the check and handle the exception. |
| 147 | std::optional<std::string> GetReason() const; |
| 148 | std::optional<std::string> GetStage() const; |
| 149 | |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 150 | private: |
Tao Bao | 551d2c3 | 2018-05-09 20:53:13 -0700 | [diff] [blame] | 151 | // The RecoveryUI object that should be used to display the user interface for this device. |
| 152 | std::unique_ptr<RecoveryUI> ui_; |
Tianjie Xu | b63a221 | 2019-07-29 14:21:49 -0700 | [diff] [blame] | 153 | const BootState* boot_state_{ nullptr }; |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 154 | }; |
| 155 | |
Tao Bao | 42c45e2 | 2018-07-31 09:37:12 -0700 | [diff] [blame] | 156 | // Disable name mangling, as this function will be loaded via dlsym(3). |
| 157 | extern "C" { |
| 158 | |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 159 | // The device-specific library must define this function (or the default one will be used, if there |
| 160 | // is no device-specific library). It returns the Device object that recovery should use. |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 161 | Device* make_device(); |
Tao Bao | 42c45e2 | 2018-07-31 09:37:12 -0700 | [diff] [blame] | 162 | } |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 163 | |
| 164 | #endif // _DEVICE_H |